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

pipe = StableDiffusionPipeline.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> StableDiffusionPipeline

pipe = StableDiffusionPipeline.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=o("meta"),Ae=h(),y=o("h1"),k=o("a"),ie=o("span"),j(N.$$.fragment),rt=h(),le=o("span"),nt=n("What is safetensors ?"),Ie=h(),v=o("p"),O=o("a"),ft=n("safetensors"),ht=n(` is a different format
from the classic `),re=o("code"),pt=n(".bin"),ut=n(` which uses Pytorch which uses pickle. It contains the
exact same data, which is just the model weights (or tensors).`),je=h(),z=o("p"),dt=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.`),xe=h(),b=o("p"),ne=o("code"),ct=n("safetensors"),mt=n(" first and foremost goal is to make loading machine learning models "),fe=o("em"),wt=n("safe"),yt=n(`
in the sense that no takeover of your computer can be done.`),Te=h(),F=o("p"),vt=n("Hence the name."),De=h(),_=o("h1"),E=o("a"),he=o("span"),j(L.$$.fragment),bt=h(),pe=o("span"),_t=n("Why use safetensors ?"),Ce=h(),R=o("p"),ue=o("strong"),gt=n("Safety"),$t=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.`),qe=h(),P=o("p"),kt=n("And a secondary reason, is "),de=o("strong"),Et=n("the speed of loading"),Pt=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.`),Ne=h(),K=o("p"),St=n("Numbers taken AMD EPYC 7742 64-Core Processor"),Oe=h(),j(H.$$.fragment),Le=h(),Q=o("p"),At=n("This is for the entire loading time, the actual weights loading time to load 500MB:"),Re=h(),j(M.$$.fragment),He=h(),V=o("p"),It=n("Performance in general is a tricky business, and there are a few things to understand:"),Me=h(),d=o("ul"),ce=o("li"),jt=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`),xt=h(),me=o("li"),Tt=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).`),Dt=h(),we=o("li"),Ct=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."),Be=h(),g=o("h1"),S=o("a"),ye=o("span"),j(B.$$.fragment),qt=h(),ve=o("span"),Nt=n("How to use safetensors ?"),We=h(),c=o("p"),Ot=n("If you have "),be=o("code"),Lt=n("safetensors"),Rt=n(" installed, and all the weights are available in "),_e=o("code"),Ht=n("safetensors"),Mt=n(` format, \\
then by default it will use that instead of the pytorch weights.`),Ye=h(),A=o("p"),Bt=n("If you are really paranoid about this, the ultimate weapon would be disabling "),ge=o("code"),Wt=n("torch.load"),Yt=n(":"),Ge=h(),j(W.$$.fragment),Je=h(),$=o("h1"),I=o("a"),$e=o("span"),j(Y.$$.fragment),Gt=h(),ke=o("span"),Jt=n("I want to use model X but it doesn't have safetensors weights."),Ue=h(),m=o("p"),Ut=n("Just go to this "),G=o("a"),Xt=n("space"),zt=n(`.
This will create a new PR with the weights, let\u2019s say `),Ee=o("code"),Ft=n("refs/pr/22"),Kt=n("."),Xe=h(),Z=o("p"),Qt=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.`),ze=h(),ee=o("p"),Vt=n("Then in order to use the model, even before the branch gets accepted by the original author you can do:"),Fe=h(),j(J.$$.fragment),Ke=h(),te=o("p"),Zt=n("And that\u2019s it !"),Qe=h(),U=o("p"),es=n("Anything unclear, concerns, or found a bugs ? "),X=o("a"),ts=n("Open an issue"),this.h()},l(e){const a=Ys('[data-svelte="svelte-1phssyn"]',document.head);w=i(a,"META",{name:!0,content:!0}),a.forEach(t),Ae=p(e),y=i(e,"H1",{class:!0});var Ze=l(y);k=i(Ze,"A",{id:!0,class:!0,href:!0});var is=l(k);ie=i(is,"SPAN",{});var ls=l(ie);x(N.$$.fragment,ls),ls.forEach(t),is.forEach(t),rt=p(Ze),le=i(Ze,"SPAN",{});var rs=l(le);nt=f(rs,"What is safetensors ?"),rs.forEach(t),Ze.forEach(t),Ie=p(e),v=i(e,"P",{});var Pe=l(v);O=i(Pe,"A",{href:!0,rel:!0});var ns=l(O);ft=f(ns,"safetensors"),ns.forEach(t),ht=f(Pe,` is a different format
from the classic `),re=i(Pe,"CODE",{});var fs=l(re);pt=f(fs,".bin"),fs.forEach(t),ut=f(Pe,` which uses Pytorch which uses pickle. It contains the
exact same data, which is just the model weights (or tensors).`),Pe.forEach(t),je=p(e),z=i(e,"P",{});var hs=l(z);dt=f(hs,`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.`),hs.forEach(t),xe=p(e),b=i(e,"P",{});var Se=l(b);ne=i(Se,"CODE",{});var ps=l(ne);ct=f(ps,"safetensors"),ps.forEach(t),mt=f(Se," first and foremost goal is to make loading machine learning models "),fe=i(Se,"EM",{});var us=l(fe);wt=f(us,"safe"),us.forEach(t),yt=f(Se,`
in the sense that no takeover of your computer can be done.`),Se.forEach(t),Te=p(e),F=i(e,"P",{});var ds=l(F);vt=f(ds,"Hence the name."),ds.forEach(t),De=p(e),_=i(e,"H1",{class:!0});var et=l(_);E=i(et,"A",{id:!0,class:!0,href:!0});var cs=l(E);he=i(cs,"SPAN",{});var ms=l(he);x(L.$$.fragment,ms),ms.forEach(t),cs.forEach(t),bt=p(et),pe=i(et,"SPAN",{});var ws=l(pe);_t=f(ws,"Why use safetensors ?"),ws.forEach(t),et.forEach(t),Ce=p(e),R=i(e,"P",{});var ss=l(R);ue=i(ss,"STRONG",{});var ys=l(ue);gt=f(ys,"Safety"),ys.forEach(t),$t=f(ss,` 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.`),ss.forEach(t),qe=p(e),P=i(e,"P",{});var tt=l(P);kt=f(tt,"And a secondary reason, is "),de=i(tt,"STRONG",{});var vs=l(de);Et=f(vs,"the speed of loading"),vs.forEach(t),Pt=f(tt,`. 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.`),tt.forEach(t),Ne=p(e),K=i(e,"P",{});var bs=l(K);St=f(bs,"Numbers taken AMD EPYC 7742 64-Core Processor"),bs.forEach(t),Oe=p(e),x(H.$$.fragment,e),Le=p(e),Q=i(e,"P",{});var _s=l(Q);At=f(_s,"This is for the entire loading time, the actual weights loading time to load 500MB:"),_s.forEach(t),Re=p(e),x(M.$$.fragment,e),He=p(e),V=i(e,"P",{});var gs=l(V);It=f(gs,"Performance in general is a tricky business, and there are a few things to understand:"),gs.forEach(t),Me=p(e),d=i(e,"UL",{});var se=l(d);ce=i(se,"LI",{});var $s=l(ce);jt=f($s,`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`),$s.forEach(t),xt=p(se),me=i(se,"LI",{});var ks=l(me);Tt=f(ks,`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).`),ks.forEach(t),Dt=p(se),we=i(se,"LI",{});var Es=l(we);Ct=f(Es,"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."),Es.forEach(t),se.forEach(t),Be=p(e),g=i(e,"H1",{class:!0});var st=l(g);S=i(st,"A",{id:!0,class:!0,href:!0});var Ps=l(S);ye=i(Ps,"SPAN",{});var Ss=l(ye);x(B.$$.fragment,Ss),Ss.forEach(t),Ps.forEach(t),qt=p(st),ve=i(st,"SPAN",{});var As=l(ve);Nt=f(As,"How to use safetensors ?"),As.forEach(t),st.forEach(t),We=p(e),c=i(e,"P",{});var ae=l(c);Ot=f(ae,"If you have "),be=i(ae,"CODE",{});var Is=l(be);Lt=f(Is,"safetensors"),Is.forEach(t),Rt=f(ae," installed, and all the weights are available in "),_e=i(ae,"CODE",{});var js=l(_e);Ht=f(js,"safetensors"),js.forEach(t),Mt=f(ae,` format, \\
then by default it will use that instead of the pytorch weights.`),ae.forEach(t),Ye=p(e),A=i(e,"P",{});var at=l(A);Bt=f(at,"If you are really paranoid about this, the ultimate weapon would be disabling "),ge=i(at,"CODE",{});var xs=l(ge);Wt=f(xs,"torch.load"),xs.forEach(t),Yt=f(at,":"),at.forEach(t),Ge=p(e),x(W.$$.fragment,e),Je=p(e),$=i(e,"H1",{class:!0});var ot=l($);I=i(ot,"A",{id:!0,class:!0,href:!0});var Ts=l(I);$e=i(Ts,"SPAN",{});var Ds=l($e);x(Y.$$.fragment,Ds),Ds.forEach(t),Ts.forEach(t),Gt=p(ot),ke=i(ot,"SPAN",{});var Cs=l(ke);Jt=f(Cs,"I want to use model X but it doesn't have safetensors weights."),Cs.forEach(t),ot.forEach(t),Ue=p(e),m=i(e,"P",{});var oe=l(m);Ut=f(oe,"Just go to this "),G=i(oe,"A",{href:!0,rel:!0});var qs=l(G);Xt=f(qs,"space"),qs.forEach(t),zt=f(oe,`.
This will create a new PR with the weights, let\u2019s say `),Ee=i(oe,"CODE",{});var Ns=l(Ee);Ft=f(Ns,"refs/pr/22"),Ns.forEach(t),Kt=f(oe,"."),oe.forEach(t),Xe=p(e),Z=i(e,"P",{});var Os=l(Z);Qt=f(Os,`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.`),Os.forEach(t),ze=p(e),ee=i(e,"P",{});var Ls=l(ee);Vt=f(Ls,"Then in order to use the model, even before the branch gets accepted by the original author you can do:"),Ls.forEach(t),Fe=p(e),x(J.$$.fragment,e),Ke=p(e),te=i(e,"P",{});var Rs=l(te);Zt=f(Rs,"And that\u2019s it !"),Rs.forEach(t),Qe=p(e),U=i(e,"P",{});var as=l(U);es=f(as,"Anything unclear, concerns, or found a bugs ? "),X=i(as,"A",{href:!0,rel:!0});var Hs=l(X);ts=f(Hs,"Open an issue"),Hs.forEach(t),as.forEach(t),this.h()},h(){u(w,"name","hf:doc:metadata"),u(w,"content",JSON.stringify(Xs)),u(k,"id","what-is-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","#what-is-safetensors"),u(y,"class","relative group"),u(O,"href","https://github.com/huggingface/safetensors"),u(O,"rel","nofollow"),u(E,"id","why-use-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","#why-use-safetensors"),u(_,"class","relative group"),u(S,"id","how-to-use-safetensors"),u(S,"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(S,"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(G,"href","https://huggingface.co/spaces/safetensors/convert"),u(G,"rel","nofollow"),u(X,"href","https://github.com/huggingface/diffusers/issues/new/choose"),u(X,"rel","nofollow")},m(e,a){s(document.head,w),r(e,Ae,a),r(e,y,a),s(y,k),s(k,ie),T(N,ie,null),s(y,rt),s(y,le),s(le,nt),r(e,Ie,a),r(e,v,a),s(v,O),s(O,ft),s(v,ht),s(v,re),s(re,pt),s(v,ut),r(e,je,a),r(e,z,a),s(z,dt),r(e,xe,a),r(e,b,a),s(b,ne),s(ne,ct),s(b,mt),s(b,fe),s(fe,wt),s(b,yt),r(e,Te,a),r(e,F,a),s(F,vt),r(e,De,a),r(e,_,a),s(_,E),s(E,he),T(L,he,null),s(_,bt),s(_,pe),s(pe,_t),r(e,Ce,a),r(e,R,a),s(R,ue),s(ue,gt),s(R,$t),r(e,qe,a),r(e,P,a),s(P,kt),s(P,de),s(de,Et),s(P,Pt),r(e,Ne,a),r(e,K,a),s(K,St),r(e,Oe,a),T(H,e,a),r(e,Le,a),r(e,Q,a),s(Q,At),r(e,Re,a),T(M,e,a),r(e,He,a),r(e,V,a),s(V,It),r(e,Me,a),r(e,d,a),s(d,ce),s(ce,jt),s(d,xt),s(d,me),s(me,Tt),s(d,Dt),s(d,we),s(we,Ct),r(e,Be,a),r(e,g,a),s(g,S),s(S,ye),T(B,ye,null),s(g,qt),s(g,ve),s(ve,Nt),r(e,We,a),r(e,c,a),s(c,Ot),s(c,be),s(be,Lt),s(c,Rt),s(c,_e),s(_e,Ht),s(c,Mt),r(e,Ye,a),r(e,A,a),s(A,Bt),s(A,ge),s(ge,Wt),s(A,Yt),r(e,Ge,a),T(W,e,a),r(e,Je,a),r(e,$,a),s($,I),s(I,$e),T(Y,$e,null),s($,Gt),s($,ke),s(ke,Jt),r(e,Ue,a),r(e,m,a),s(m,Ut),s(m,G),s(G,Xt),s(m,zt),s(m,Ee),s(Ee,Ft),s(m,Kt),r(e,Xe,a),r(e,Z,a),s(Z,Qt),r(e,ze,a),r(e,ee,a),s(ee,Vt),r(e,Fe,a),T(J,e,a),r(e,Ke,a),r(e,te,a),s(te,Zt),r(e,Qe,a),r(e,U,a),s(U,es),s(U,X),s(X,ts),Ve=!0},p:Gs,i(e){Ve||(D(N.$$.fragment,e),D(L.$$.fragment,e),D(H.$$.fragment,e),D(M.$$.fragment,e),D(B.$$.fragment,e),D(W.$$.fragment,e),D(Y.$$.fragment,e),D(J.$$.fragment,e),Ve=!0)},o(e){C(N.$$.fragment,e),C(L.$$.fragment,e),C(H.$$.fragment,e),C(M.$$.fragment,e),C(B.$$.fragment,e),C(W.$$.fragment,e),C(Y.$$.fragment,e),C(J.$$.fragment,e),Ve=!1},d(e){t(w),e&&t(Ae),e&&t(y),q(N),e&&t(Ie),e&&t(v),e&&t(je),e&&t(z),e&&t(xe),e&&t(b),e&&t(Te),e&&t(F),e&&t(De),e&&t(_),q(L),e&&t(Ce),e&&t(R),e&&t(qe),e&&t(P),e&&t(Ne),e&&t(K),e&&t(Oe),q(H,e),e&&t(Le),e&&t(Q),e&&t(Re),q(M,e),e&&t(He),e&&t(V),e&&t(Me),e&&t(d),e&&t(Be),e&&t(g),q(B),e&&t(We),e&&t(c),e&&t(Ye),e&&t(A),e&&t(Ge),q(W,e),e&&t(Je),e&&t($),q(Y),e&&t(Ue),e&&t(m),e&&t(Xe),e&&t(Z),e&&t(ze),e&&t(ee),e&&t(Fe),q(J,e),e&&t(Ke),e&&t(te),e&&t(Qe),e&&t(U)}}}const Xs={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 zs(os){return Js(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Vs extends Ms{constructor(w){super();Bs(this,w,zs,Us,Ws,{})}}export{Vs as default,Xs as metadata};
