import{S as zs,i as Fs,s as Ks,e as a,k as h,w as x,t as n,M as Qs,c as i,d as t,m as p,a as l,x as T,h as f,b as u,G as s,g as r,y as D,L as Vs,q as C,o as q,B as N,v as Zs}from"../../chunks/vendor-hf-doc-builder.js";import{I as ft}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as ht}from"../../chunks/CodeBlock-hf-doc-builder.js";function eo(ps){let w,je,y,E,re,O,pt,ne,ut,xe,v,L,dt,ct,fe,mt,wt,Te,K,yt,De,b,he,vt,bt,pe,_t,gt,Ce,Q,$t,qe,_,k,ue,R,Et,de,kt,Ne,H,ce,Pt,At,Oe,P,St,me,It,jt,Le,V,xt,Re,M,He,Z,Tt,Me,B,Be,ee,Dt,We,d,we,Ct,qt,ye,Nt,Ot,ve,Lt,Ye,g,A,be,W,Rt,_e,Ht,Ge,c,Mt,ge,Bt,Wt,$e,Yt,Gt,Je,S,Jt,Ee,Ut,Xt,Ue,Y,Xe,$,I,ke,G,zt,Pe,Ft,ze,m,Kt,J,Qt,Vt,Ae,Zt,es,Fe,te,ts,Ke,se,ss,Qe,U,Ve,j,os,X,as,is,Ze,oe,ls,et,z,rs,F,ns,tt;return O=new ft({}),R=new ft({}),M=new ht({props:{code:`from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")

# Loaded in safetensors 0:00:02.033658
# Loaded in Pytorch 0:00:02.663379`,highlighted:`<span class="hljs-keyword">from</span> diffusers import S<span class="hljs-built_in">table</span>DiffusionPipeline

pipe = S<span class="hljs-built_in">table</span>DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;stabilityai/stable-diffusion-2-1&quot;</span>)

<span class="hljs-comment"># Loaded in safetensors 0:00:02.033658</span>
<span class="hljs-comment"># Loaded in Pytorch 0:00:02.663379</span>`}}),B=new ht({props:{code:`Safetensors: 3.4873ms
PyTorch: 172.7537ms`,highlighted:`<span class="hljs-attribute">Safetensors</span>: <span class="hljs-number">3</span>.<span class="hljs-number">4873</span>ms
<span class="hljs-attribute">PyTorch</span>: <span class="hljs-number">172</span>.<span class="hljs-number">7537</span>ms`}}),W=new ft({}),Y=new ht({props:{code:`import torch


def _raise():
    raise RuntimeError("I don't want to use pickle")


torch.load = lambda *args, **kwargs: _raise()`,highlighted:`<span class="hljs-keyword">import</span> torch


<span class="hljs-keyword">def</span> <span class="hljs-title function_">_raise</span>():
    <span class="hljs-keyword">raise</span> RuntimeError(<span class="hljs-string">&quot;I don&#x27;t want to use pickle&quot;</span>)


torch.load = <span class="hljs-keyword">lambda</span> *args, **kwargs: _<span class="hljs-keyword">raise</span>()`}}),G=new ft({}),U=new ht({props:{code:`from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", revision="refs/pr/22")`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;stabilityai/stable-diffusion-2-1&quot;</span>, revision=<span class="hljs-string">&quot;refs/pr/22&quot;</span>)`}}),{c(){w=a("meta"),je=h(),y=a("h1"),E=a("a"),re=a("span"),x(O.$$.fragment),pt=h(),ne=a("span"),ut=n("What is safetensors ?"),xe=h(),v=a("p"),L=a("a"),dt=n("safetensors"),ct=n(` is a different format
from the classic `),fe=a("code"),mt=n(".bin"),wt=n(` which uses Pytorch which uses pickle. It contains the
exact same data, which is just the model weights (or tensors).`),Te=h(),K=a("p"),yt=n(`Pickle is notoriously unsafe which allow any malicious file to execute arbitrary code.
The hub itself tries to prevent issues from it, but it\u2019s not a silver bullet.`),De=h(),b=a("p"),he=a("code"),vt=n("safetensors"),bt=n(" first and foremost goal is to make loading machine learning models "),pe=a("em"),_t=n("safe"),gt=n(`
in the sense that no takeover of your computer can be done.`),Ce=h(),Q=a("p"),$t=n("Hence the name."),qe=h(),_=a("h1"),k=a("a"),ue=a("span"),x(R.$$.fragment),Et=h(),de=a("span"),kt=n("Why use safetensors ?"),Ne=h(),H=a("p"),ce=a("strong"),Pt=n("Safety"),At=n(` can be one reason, if you\u2019re attempting to use a not well known model and
you\u2019re not sure about the source of the file.`),Oe=h(),P=a("p"),St=n("And a secondary reason, is "),me=a("strong"),It=n("the speed of loading"),jt=n(`. Safetensors can load models much faster
than regular pickle files. If you spend a lot of times switching models, this can be
a huge timesave.`),Le=h(),V=a("p"),xt=n("Numbers taken AMD EPYC 7742 64-Core Processor"),Re=h(),x(M.$$.fragment),He=h(),Z=a("p"),Tt=n("This is for the entire loading time, the actual weights loading time to load 500MB:"),Me=h(),x(B.$$.fragment),Be=h(),ee=a("p"),Dt=n("Performance in general is a tricky business, and there are a few things to understand:"),We=h(),d=a("ul"),we=a("li"),Ct=n(`If you\u2019re using the model for the first time from the hub, you will have to download the weights.
That\u2019s extremely likely to be much slower than any loading method, therefore you will not see any difference`),qt=h(),ye=a("li"),Nt=n(`If you\u2019re loading the model for the first time (let\u2019s say after a reboot) then your machine will have to
actually read the disk. It\u2019s likely to be as slow in both cases. Again the speed difference may not be as visible (this depends on hardware and the actual model).`),Ot=h(),ve=a("li"),Lt=n("The best performance benefit is when the model was already loaded previously on your computer and you\u2019re switching from one model to another. Your OS, is trying really hard not to read from disk, since this is slow, so it will keep the files around in RAM, making it loading again much faster. Since safetensors is doing zero-copy of the tensors, reloading will be faster than pytorch since it has at least once extra copy to do."),Ye=h(),g=a("h1"),A=a("a"),be=a("span"),x(W.$$.fragment),Rt=h(),_e=a("span"),Ht=n("How to use safetensors ?"),Ge=h(),c=a("p"),Mt=n("If you have "),ge=a("code"),Bt=n("safetensors"),Wt=n(" installed, and all the weights are available in "),$e=a("code"),Yt=n("safetensors"),Gt=n(` format, \\
then by default it will use that instead of the pytorch weights.`),Je=h(),S=a("p"),Jt=n("If you are really paranoid about this, the ultimate weapon would be disabling "),Ee=a("code"),Ut=n("torch.load"),Xt=n(":"),Ue=h(),x(Y.$$.fragment),Xe=h(),$=a("h1"),I=a("a"),ke=a("span"),x(G.$$.fragment),zt=h(),Pe=a("span"),Ft=n("I want to use model X but it doesn't have safetensors weights."),ze=h(),m=a("p"),Kt=n("Just go to this "),J=a("a"),Qt=n("space"),Vt=n(`.
This will create a new PR with the weights, let\u2019s say `),Ae=a("code"),Zt=n("refs/pr/22"),es=n("."),Fe=h(),te=a("p"),ts=n(`This space will download the pickled version, convert it, and upload it on the hub as a PR.
If anything bad is contained in the file, it\u2019s Huggingface hub that will get issues, not your own computer.
And we\u2019re equipped with dealing with it.`),Ke=h(),se=a("p"),ss=n("Then in order to use the model, even before the branch gets accepted by the original author you can do:"),Qe=h(),x(U.$$.fragment),Ve=h(),j=a("p"),os=n("or you can test it directly online with this "),X=a("a"),as=n("space"),is=n("."),Ze=h(),oe=a("p"),ls=n("And that\u2019s it !"),et=h(),z=a("p"),rs=n("Anything unclear, concerns, or found a bugs ? "),F=a("a"),ns=n("Open an issue"),this.h()},l(e){const o=Qs('[data-svelte="svelte-1phssyn"]',document.head);w=i(o,"META",{name:!0,content:!0}),o.forEach(t),je=p(e),y=i(e,"H1",{class:!0});var st=l(y);E=i(st,"A",{id:!0,class:!0,href:!0});var us=l(E);re=i(us,"SPAN",{});var ds=l(re);T(O.$$.fragment,ds),ds.forEach(t),us.forEach(t),pt=p(st),ne=i(st,"SPAN",{});var cs=l(ne);ut=f(cs,"What is safetensors ?"),cs.forEach(t),st.forEach(t),xe=p(e),v=i(e,"P",{});var Se=l(v);L=i(Se,"A",{href:!0,rel:!0});var ms=l(L);dt=f(ms,"safetensors"),ms.forEach(t),ct=f(Se,` is a different format
from the classic `),fe=i(Se,"CODE",{});var ws=l(fe);mt=f(ws,".bin"),ws.forEach(t),wt=f(Se,` which uses Pytorch which uses pickle. It contains the
exact same data, which is just the model weights (or tensors).`),Se.forEach(t),Te=p(e),K=i(e,"P",{});var ys=l(K);yt=f(ys,`Pickle is notoriously unsafe which allow any malicious file to execute arbitrary code.
The hub itself tries to prevent issues from it, but it\u2019s not a silver bullet.`),ys.forEach(t),De=p(e),b=i(e,"P",{});var Ie=l(b);he=i(Ie,"CODE",{});var vs=l(he);vt=f(vs,"safetensors"),vs.forEach(t),bt=f(Ie," first and foremost goal is to make loading machine learning models "),pe=i(Ie,"EM",{});var bs=l(pe);_t=f(bs,"safe"),bs.forEach(t),gt=f(Ie,`
in the sense that no takeover of your computer can be done.`),Ie.forEach(t),Ce=p(e),Q=i(e,"P",{});var _s=l(Q);$t=f(_s,"Hence the name."),_s.forEach(t),qe=p(e),_=i(e,"H1",{class:!0});var ot=l(_);k=i(ot,"A",{id:!0,class:!0,href:!0});var gs=l(k);ue=i(gs,"SPAN",{});var $s=l(ue);T(R.$$.fragment,$s),$s.forEach(t),gs.forEach(t),Et=p(ot),de=i(ot,"SPAN",{});var Es=l(de);kt=f(Es,"Why use safetensors ?"),Es.forEach(t),ot.forEach(t),Ne=p(e),H=i(e,"P",{});var fs=l(H);ce=i(fs,"STRONG",{});var ks=l(ce);Pt=f(ks,"Safety"),ks.forEach(t),At=f(fs,` can be one reason, if you\u2019re attempting to use a not well known model and
you\u2019re not sure about the source of the file.`),fs.forEach(t),Oe=p(e),P=i(e,"P",{});var at=l(P);St=f(at,"And a secondary reason, is "),me=i(at,"STRONG",{});var Ps=l(me);It=f(Ps,"the speed of loading"),Ps.forEach(t),jt=f(at,`. Safetensors can load models much faster
than regular pickle files. If you spend a lot of times switching models, this can be
a huge timesave.`),at.forEach(t),Le=p(e),V=i(e,"P",{});var As=l(V);xt=f(As,"Numbers taken AMD EPYC 7742 64-Core Processor"),As.forEach(t),Re=p(e),T(M.$$.fragment,e),He=p(e),Z=i(e,"P",{});var Ss=l(Z);Tt=f(Ss,"This is for the entire loading time, the actual weights loading time to load 500MB:"),Ss.forEach(t),Me=p(e),T(B.$$.fragment,e),Be=p(e),ee=i(e,"P",{});var Is=l(ee);Dt=f(Is,"Performance in general is a tricky business, and there are a few things to understand:"),Is.forEach(t),We=p(e),d=i(e,"UL",{});var ae=l(d);we=i(ae,"LI",{});var js=l(we);Ct=f(js,`If you\u2019re using the model for the first time from the hub, you will have to download the weights.
That\u2019s extremely likely to be much slower than any loading method, therefore you will not see any difference`),js.forEach(t),qt=p(ae),ye=i(ae,"LI",{});var xs=l(ye);Nt=f(xs,`If you\u2019re loading the model for the first time (let\u2019s say after a reboot) then your machine will have to
actually read the disk. It\u2019s likely to be as slow in both cases. Again the speed difference may not be as visible (this depends on hardware and the actual model).`),xs.forEach(t),Ot=p(ae),ve=i(ae,"LI",{});var Ts=l(ve);Lt=f(Ts,"The best performance benefit is when the model was already loaded previously on your computer and you\u2019re switching from one model to another. Your OS, is trying really hard not to read from disk, since this is slow, so it will keep the files around in RAM, making it loading again much faster. Since safetensors is doing zero-copy of the tensors, reloading will be faster than pytorch since it has at least once extra copy to do."),Ts.forEach(t),ae.forEach(t),Ye=p(e),g=i(e,"H1",{class:!0});var it=l(g);A=i(it,"A",{id:!0,class:!0,href:!0});var Ds=l(A);be=i(Ds,"SPAN",{});var Cs=l(be);T(W.$$.fragment,Cs),Cs.forEach(t),Ds.forEach(t),Rt=p(it),_e=i(it,"SPAN",{});var qs=l(_e);Ht=f(qs,"How to use safetensors ?"),qs.forEach(t),it.forEach(t),Ge=p(e),c=i(e,"P",{});var ie=l(c);Mt=f(ie,"If you have "),ge=i(ie,"CODE",{});var Ns=l(ge);Bt=f(Ns,"safetensors"),Ns.forEach(t),Wt=f(ie," installed, and all the weights are available in "),$e=i(ie,"CODE",{});var Os=l($e);Yt=f(Os,"safetensors"),Os.forEach(t),Gt=f(ie,` format, \\
then by default it will use that instead of the pytorch weights.`),ie.forEach(t),Je=p(e),S=i(e,"P",{});var lt=l(S);Jt=f(lt,"If you are really paranoid about this, the ultimate weapon would be disabling "),Ee=i(lt,"CODE",{});var Ls=l(Ee);Ut=f(Ls,"torch.load"),Ls.forEach(t),Xt=f(lt,":"),lt.forEach(t),Ue=p(e),T(Y.$$.fragment,e),Xe=p(e),$=i(e,"H1",{class:!0});var rt=l($);I=i(rt,"A",{id:!0,class:!0,href:!0});var Rs=l(I);ke=i(Rs,"SPAN",{});var Hs=l(ke);T(G.$$.fragment,Hs),Hs.forEach(t),Rs.forEach(t),zt=p(rt),Pe=i(rt,"SPAN",{});var Ms=l(Pe);Ft=f(Ms,"I want to use model X but it doesn't have safetensors weights."),Ms.forEach(t),rt.forEach(t),ze=p(e),m=i(e,"P",{});var le=l(m);Kt=f(le,"Just go to this "),J=i(le,"A",{href:!0,rel:!0});var Bs=l(J);Qt=f(Bs,"space"),Bs.forEach(t),Vt=f(le,`.
This will create a new PR with the weights, let\u2019s say `),Ae=i(le,"CODE",{});var Ws=l(Ae);Zt=f(Ws,"refs/pr/22"),Ws.forEach(t),es=f(le,"."),le.forEach(t),Fe=p(e),te=i(e,"P",{});var Ys=l(te);ts=f(Ys,`This space will download the pickled version, convert it, and upload it on the hub as a PR.
If anything bad is contained in the file, it\u2019s Huggingface hub that will get issues, not your own computer.
And we\u2019re equipped with dealing with it.`),Ys.forEach(t),Ke=p(e),se=i(e,"P",{});var Gs=l(se);ss=f(Gs,"Then in order to use the model, even before the branch gets accepted by the original author you can do:"),Gs.forEach(t),Qe=p(e),T(U.$$.fragment,e),Ve=p(e),j=i(e,"P",{});var nt=l(j);os=f(nt,"or you can test it directly online with this "),X=i(nt,"A",{href:!0,rel:!0});var Js=l(X);as=f(Js,"space"),Js.forEach(t),is=f(nt,"."),nt.forEach(t),Ze=p(e),oe=i(e,"P",{});var Us=l(oe);ls=f(Us,"And that\u2019s it !"),Us.forEach(t),et=p(e),z=i(e,"P",{});var hs=l(z);rs=f(hs,"Anything unclear, concerns, or found a bugs ? "),F=i(hs,"A",{href:!0,rel:!0});var Xs=l(F);ns=f(Xs,"Open an issue"),Xs.forEach(t),hs.forEach(t),this.h()},h(){u(w,"name","hf:doc:metadata"),u(w,"content",JSON.stringify(to)),u(E,"id","what-is-safetensors"),u(E,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),u(E,"href","#what-is-safetensors"),u(y,"class","relative group"),u(L,"href","https://github.com/huggingface/safetensors"),u(L,"rel","nofollow"),u(k,"id","why-use-safetensors"),u(k,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),u(k,"href","#why-use-safetensors"),u(_,"class","relative group"),u(A,"id","how-to-use-safetensors"),u(A,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),u(A,"href","#how-to-use-safetensors"),u(g,"class","relative group"),u(I,"id","i-want-to-use-model-x-but-it-doesnt-have-safetensors-weights"),u(I,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),u(I,"href","#i-want-to-use-model-x-but-it-doesnt-have-safetensors-weights"),u($,"class","relative group"),u(J,"href","https://huggingface.co/spaces/diffusers/convert"),u(J,"rel","nofollow"),u(X,"href","https://huggingface.co/spaces/diffusers/check_pr"),u(X,"rel","nofollow"),u(F,"href","https://github.com/huggingface/diffusers/issues/new/choose"),u(F,"rel","nofollow")},m(e,o){s(document.head,w),r(e,je,o),r(e,y,o),s(y,E),s(E,re),D(O,re,null),s(y,pt),s(y,ne),s(ne,ut),r(e,xe,o),r(e,v,o),s(v,L),s(L,dt),s(v,ct),s(v,fe),s(fe,mt),s(v,wt),r(e,Te,o),r(e,K,o),s(K,yt),r(e,De,o),r(e,b,o),s(b,he),s(he,vt),s(b,bt),s(b,pe),s(pe,_t),s(b,gt),r(e,Ce,o),r(e,Q,o),s(Q,$t),r(e,qe,o),r(e,_,o),s(_,k),s(k,ue),D(R,ue,null),s(_,Et),s(_,de),s(de,kt),r(e,Ne,o),r(e,H,o),s(H,ce),s(ce,Pt),s(H,At),r(e,Oe,o),r(e,P,o),s(P,St),s(P,me),s(me,It),s(P,jt),r(e,Le,o),r(e,V,o),s(V,xt),r(e,Re,o),D(M,e,o),r(e,He,o),r(e,Z,o),s(Z,Tt),r(e,Me,o),D(B,e,o),r(e,Be,o),r(e,ee,o),s(ee,Dt),r(e,We,o),r(e,d,o),s(d,we),s(we,Ct),s(d,qt),s(d,ye),s(ye,Nt),s(d,Ot),s(d,ve),s(ve,Lt),r(e,Ye,o),r(e,g,o),s(g,A),s(A,be),D(W,be,null),s(g,Rt),s(g,_e),s(_e,Ht),r(e,Ge,o),r(e,c,o),s(c,Mt),s(c,ge),s(ge,Bt),s(c,Wt),s(c,$e),s($e,Yt),s(c,Gt),r(e,Je,o),r(e,S,o),s(S,Jt),s(S,Ee),s(Ee,Ut),s(S,Xt),r(e,Ue,o),D(Y,e,o),r(e,Xe,o),r(e,$,o),s($,I),s(I,ke),D(G,ke,null),s($,zt),s($,Pe),s(Pe,Ft),r(e,ze,o),r(e,m,o),s(m,Kt),s(m,J),s(J,Qt),s(m,Vt),s(m,Ae),s(Ae,Zt),s(m,es),r(e,Fe,o),r(e,te,o),s(te,ts),r(e,Ke,o),r(e,se,o),s(se,ss),r(e,Qe,o),D(U,e,o),r(e,Ve,o),r(e,j,o),s(j,os),s(j,X),s(X,as),s(j,is),r(e,Ze,o),r(e,oe,o),s(oe,ls),r(e,et,o),r(e,z,o),s(z,rs),s(z,F),s(F,ns),tt=!0},p:Vs,i(e){tt||(C(O.$$.fragment,e),C(R.$$.fragment,e),C(M.$$.fragment,e),C(B.$$.fragment,e),C(W.$$.fragment,e),C(Y.$$.fragment,e),C(G.$$.fragment,e),C(U.$$.fragment,e),tt=!0)},o(e){q(O.$$.fragment,e),q(R.$$.fragment,e),q(M.$$.fragment,e),q(B.$$.fragment,e),q(W.$$.fragment,e),q(Y.$$.fragment,e),q(G.$$.fragment,e),q(U.$$.fragment,e),tt=!1},d(e){t(w),e&&t(je),e&&t(y),N(O),e&&t(xe),e&&t(v),e&&t(Te),e&&t(K),e&&t(De),e&&t(b),e&&t(Ce),e&&t(Q),e&&t(qe),e&&t(_),N(R),e&&t(Ne),e&&t(H),e&&t(Oe),e&&t(P),e&&t(Le),e&&t(V),e&&t(Re),N(M,e),e&&t(He),e&&t(Z),e&&t(Me),N(B,e),e&&t(Be),e&&t(ee),e&&t(We),e&&t(d),e&&t(Ye),e&&t(g),N(W),e&&t(Ge),e&&t(c),e&&t(Je),e&&t(S),e&&t(Ue),N(Y,e),e&&t(Xe),e&&t($),N(G),e&&t(ze),e&&t(m),e&&t(Fe),e&&t(te),e&&t(Ke),e&&t(se),e&&t(Qe),N(U,e),e&&t(Ve),e&&t(j),e&&t(Ze),e&&t(oe),e&&t(et),e&&t(z)}}}const to={local:"i-want-to-use-model-x-but-it-doesnt-have-safetensors-weights",title:"I want to use model X but it doesn't have safetensors weights."};function so(ps){return Zs(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class lo extends zs{constructor(w){super();Fs(this,w,so,eo,Ks,{})}}export{lo as default,to as metadata};
