Buckets:

rtrm's picture
download
raw
15.1 kB
import{s as lt,n as at,o as it}from"../chunks/scheduler.182ea377.js";import{S as rt,i as ft,g as n,s as a,p as m,A as ct,h as l,f as t,c as i,j as p,q as y,m as f,k as c,v as r,a as o,r as g,d as b,t as w,u as v}from"../chunks/index.008d68e4.js";import{I as Pe}from"../chunks/IconCopyLink.96bbb92b.js";import{C as te}from"../chunks/CodeBlock.5ed6eb7b.js";import{D as ht}from"../chunks/DocNotebookDropdown.bb388256.js";function pt(ze){let M,le,T,_,se,x,Ze,A,Le="Load safetensors",ae,J,ie,G,Ne='<a href="https://github.com/huggingface/safetensors" rel="nofollow">safetensors</a> is a safe and fast file format for storing and loading tensors. Typically, PyTorch model weights are saved or <em>pickled</em> into a <code>.bin</code> file with Python’s <a href="https://docs.python.org/3/library/pickle.html" rel="nofollow"><code>pickle</code></a> utility. However, <code>pickle</code> is not secure and pickled files may contain malicious code that can be executed. safetensors is a secure alternative to <code>pickle</code>, making it ideal for sharing model weights.',re,B,Se="This guide will show you how you load <code>.safetensor</code> files, and how to convert Stable Diffusion model weights stored in other formats to <code>.safetensor</code>. Before you start, make sure you have safetensors installed:",fe,C,ce,j,He='If you look at the <a href="https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main" rel="nofollow"><code>runwayml/stable-diffusion-v1-5</code></a> repository, you’ll see weights inside the <code>text_encoder</code>, <code>unet</code> and <code>vae</code> subfolders are stored in the <code>.safetensors</code> format. By default, 🤗 Diffusers automatically loads these <code>.safetensors</code> files from their subfolders if they’re available in the model repository.',he,W,Ie="For more explicit control, you can optionally set <code>use_safetensors=True</code> (if <code>safetensors</code> is not installed, you’ll get an error message asking you to install it):",pe,P,de,z,Re='However, model weights are not necessarily stored in separate subfolders like in the example above. Sometimes, all the weights are stored in a single <code>.safetensors</code> file. In this case, if the weights are Stable Diffusion weights, you can load the file directly with the <a href="/docs/diffusers/v0.25.0/zh/api/loaders/single_file#diffusers.loaders.FromSingleFileMixin.from_single_file">from_single_file()</a> method:',ue,L,me,$,Z,oe,N,Ue,E,Ve="Convert to safetensors",ye,S,Xe='Not all weights on the Hub are available in the <code>.safetensors</code> format, and you may encounter weights stored as <code>.bin</code>. In this case, use the <a href="https://huggingface.co/spaces/diffusers/convert" rel="nofollow">Convert Space</a> to convert the weights to <code>.safetensors</code>. The Convert Space downloads the pickled weights, converts them, and opens a Pull Request to upload the newly converted <code>.safetensors</code> file on the Hub. This way, if there is any malicious code contained in the pickled files, they’re uploaded to the Hub - which has a <a href="https://huggingface.co/docs/hub/security-pickle#hubs-security-scanner" rel="nofollow">security scanner</a> to detect unsafe files and suspicious pickle imports - instead of your computer.',ge,H,Ye='You can use the model with the new <code>.safetensors</code> weights by specifying the reference to the Pull Request in the <code>revision</code> parameter (you can also test it in this <a href="https://huggingface.co/spaces/diffusers/check_pr" rel="nofollow">Check PR</a> Space on the Hub), for example <code>refs/pr/22</code>:',be,I,we,k,U,ne,R,xe,F,Ae="Why use safetensors?",ve,V,Ee="There are several reasons for using safetensors:",Me,d,q,Fe="<p>Safety is the number one reason for using safetensors. As open-source and model distribution grows, it is important to be able to trust the model weights you downloaded don’t contain any malicious code. The current size of the header in safetensors prevents parsing extremely large JSON files.</p>",Je,h,Q,qe="Loading speed between switching models is another reason to use safetensors, which performs zero-copy of the tensors. It is especially fast compared to <code>pickle</code> if you’re loading the weights to CPU (the default case), and just as fast if not faster when directly loading the weights to GPU. You’ll only notice the performance difference if the model is already loaded, and not if you’re downloading the weights or loading the model for the first time.",Ge,D,Qe="The time it takes to load the entire pipeline:",Be,X,Ce,O,De="But the actual time it takes to load 500MB of the model weights is only:",je,Y,We,K,Oe='<p>Lazy loading is also supported in safetensors, which is useful in distributed settings to only load some of the tensors. This format allowed the <a href="https://huggingface.co/bigscience/bloom" rel="nofollow">BLOOM</a> model to be loaded in 45 seconds on 8 GPUs instead of 10 minutes with regular PyTorch weights.</p>',Te;return x=new Pe({}),J=new ht({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/zh/using_safetensors.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/zh/pytorch/using_safetensors.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/zh/tensorflow/using_safetensors.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/zh/using_safetensors.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/zh/pytorch/using_safetensors.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/zh/tensorflow/using_safetensors.ipynb"}]}}),C=new te({props:{code:"JTIzJTIwdW5jb21tZW50JTIwdG8lMjBpbnN0YWxsJTIwdGhlJTIwbmVjZXNzYXJ5JTIwbGlicmFyaWVzJTIwaW4lMjBDb2xhYiUwQSUyMyFwaXAlMjBpbnN0YWxsJTIwc2FmZXRlbnNvcnM=",highlighted:`<span class="hljs-comment"># uncomment to install the necessary libraries in Colab</span>
<span class="hljs-comment">#!pip install safetensors</span>`}}),P=new te({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZWxpbmUlMjAlM0QlMjBEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIycnVud2F5bWwlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIlMkMlMjB1c2Vfc2FmZXRlbnNvcnMlM0RUcnVlKQ==",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>, use_safetensors=<span class="hljs-literal">True</span>)`}}),L=new te({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZWxpbmUlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3NpbmdsZV9maWxlKCUwQSUyMCUyMCUyMCUyMCUyMmh0dHBzJTNBJTJGJTJGaHVnZ2luZ2ZhY2UuY28lMkZXYXJyaW9yTWFtYTc3NyUyRk9yYW5nZU1peHMlMkZibG9iJTJGbWFpbiUyRk1vZGVscyUyRkFieXNzT3JhbmdlTWl4JTJGQWJ5c3NPcmFuZ2VNaXguc2FmZXRlbnNvcnMlMjIlMEEp",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
pipeline = StableDiffusionPipeline.from_single_file(
<span class="hljs-string">&quot;https://huggingface.co/WarriorMama777/OrangeMixs/blob/main/Models/AbyssOrangeMix/AbyssOrangeMix.safetensors&quot;</span>
)`}}),N=new Pe({}),I=new te({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZWxpbmUlMjAlM0QlMjBEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIyc3RhYmlsaXR5YWklMkZzdGFibGUtZGlmZnVzaW9uLTItMSUyMiUyQyUyMHJldmlzaW9uJTNEJTIycmVmcyUyRnByJTJGMjIlMjIlMkMlMjB1c2Vfc2FmZXRlbnNvcnMlM0RUcnVlJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
pipeline = 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>, use_safetensors=<span class="hljs-literal">True</span>
)`}}),R=new Pe({}),X=new te({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZWxpbmUlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIyc3RhYmlsaXR5YWklMkZzdGFibGUtZGlmZnVzaW9uLTItMSUyMiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUpJTBBJTIyTG9hZGVkJTIwaW4lMjBzYWZldGVuc29ycyUyMDAlM0EwMCUzQTAyLjAzMzY1OCUyMiUwQSUyMkxvYWRlZCUyMGluJTIwUHlUb3JjaCUyMDAlM0EwMCUzQTAyLjY2MzM3OSUyMg==",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
pipeline = StableDiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;stabilityai/stable-diffusion-2-1&quot;</span>, use_safetensors=<span class="hljs-literal">True</span>)
<span class="hljs-string">&quot;Loaded in safetensors 0:00:02.033658&quot;</span>
<span class="hljs-string">&quot;Loaded in PyTorch 0:00:02.663379&quot;</span>`}}),Y=new te({props:{code:"c2FmZXRlbnNvcnMlM0ElMjAzLjQ4NzNtcyUwQVB5VG9yY2glM0ElMjAxNzIuNzUzN21z",highlighted:`safetensors: 3.4873ms
PyTorch: 172.7537ms`}}),{c(){M=n("meta"),le=a(),T=n("h1"),_=n("a"),se=n("span"),m(x.$$.fragment),Ze=a(),A=n("span"),A.textContent=Le,ae=a(),m(J.$$.fragment),ie=a(),G=n("p"),G.innerHTML=Ne,re=a(),B=n("p"),B.innerHTML=Se,fe=a(),m(C.$$.fragment),ce=a(),j=n("p"),j.innerHTML=He,he=a(),W=n("p"),W.innerHTML=Ie,pe=a(),m(P.$$.fragment),de=a(),z=n("p"),z.innerHTML=Re,ue=a(),m(L.$$.fragment),me=a(),$=n("h2"),Z=n("a"),oe=n("span"),m(N.$$.fragment),Ue=a(),E=n("span"),E.textContent=Ve,ye=a(),S=n("p"),S.innerHTML=Xe,ge=a(),H=n("p"),H.innerHTML=Ye,be=a(),m(I.$$.fragment),we=a(),k=n("h2"),U=n("a"),ne=n("span"),m(R.$$.fragment),xe=a(),F=n("span"),F.textContent=Ae,ve=a(),V=n("p"),V.textContent=Ee,Me=a(),d=n("ul"),q=n("li"),q.innerHTML=Fe,Je=a(),h=n("li"),Q=n("p"),Q.innerHTML=qe,Ge=a(),D=n("p"),D.textContent=Qe,Be=a(),m(X.$$.fragment),Ce=a(),O=n("p"),O.textContent=De,je=a(),m(Y.$$.fragment),We=a(),K=n("li"),K.innerHTML=Oe,this.h()},l(e){const s=ct("svelte-1phssyn",document.head);M=l(s,"META",{name:!0,content:!0}),s.forEach(t),le=i(e),T=l(e,"H1",{class:!0});var $e=p(T);_=l($e,"A",{id:!0,class:!0,href:!0});var Ke=p(_);se=l(Ke,"SPAN",{});var et=p(se);y(x.$$.fragment,et),et.forEach(t),Ke.forEach(t),Ze=i($e),A=l($e,"SPAN",{"data-svelte-h":!0}),f(A)!=="svelte-pd6hqe"&&(A.textContent=Le),$e.forEach(t),ae=i(e),y(J.$$.fragment,e),ie=i(e),G=l(e,"P",{"data-svelte-h":!0}),f(G)!=="svelte-ejaj9i"&&(G.innerHTML=Ne),re=i(e),B=l(e,"P",{"data-svelte-h":!0}),f(B)!=="svelte-19452ph"&&(B.innerHTML=Se),fe=i(e),y(C.$$.fragment,e),ce=i(e),j=l(e,"P",{"data-svelte-h":!0}),f(j)!=="svelte-1sm59ne"&&(j.innerHTML=He),he=i(e),W=l(e,"P",{"data-svelte-h":!0}),f(W)!=="svelte-1qgf6oc"&&(W.innerHTML=Ie),pe=i(e),y(P.$$.fragment,e),de=i(e),z=l(e,"P",{"data-svelte-h":!0}),f(z)!=="svelte-hbss20"&&(z.innerHTML=Re),ue=i(e),y(L.$$.fragment,e),me=i(e),$=l(e,"H2",{class:!0});var ke=p($);Z=l(ke,"A",{id:!0,class:!0,href:!0});var tt=p(Z);oe=l(tt,"SPAN",{});var st=p(oe);y(N.$$.fragment,st),st.forEach(t),tt.forEach(t),Ue=i(ke),E=l(ke,"SPAN",{"data-svelte-h":!0}),f(E)!=="svelte-17wbef6"&&(E.textContent=Ve),ke.forEach(t),ye=i(e),S=l(e,"P",{"data-svelte-h":!0}),f(S)!=="svelte-i3hl9t"&&(S.innerHTML=Xe),ge=i(e),H=l(e,"P",{"data-svelte-h":!0}),f(H)!=="svelte-uqf6y9"&&(H.innerHTML=Ye),be=i(e),y(I.$$.fragment,e),we=i(e),k=l(e,"H2",{class:!0});var _e=p(k);U=l(_e,"A",{id:!0,class:!0,href:!0});var ot=p(U);ne=l(ot,"SPAN",{});var nt=p(ne);y(R.$$.fragment,nt),nt.forEach(t),ot.forEach(t),xe=i(_e),F=l(_e,"SPAN",{"data-svelte-h":!0}),f(F)!=="svelte-1lb6zam"&&(F.textContent=Ae),_e.forEach(t),ve=i(e),V=l(e,"P",{"data-svelte-h":!0}),f(V)!=="svelte-kqqg29"&&(V.textContent=Ee),Me=i(e),d=l(e,"UL",{});var ee=p(d);q=l(ee,"LI",{"data-svelte-h":!0}),f(q)!=="svelte-1ci1eo5"&&(q.innerHTML=Fe),Je=i(ee),h=l(ee,"LI",{});var u=p(h);Q=l(u,"P",{"data-svelte-h":!0}),f(Q)!=="svelte-x9zzp2"&&(Q.innerHTML=qe),Ge=i(u),D=l(u,"P",{"data-svelte-h":!0}),f(D)!=="svelte-gqu57m"&&(D.textContent=Qe),Be=i(u),y(X.$$.fragment,u),Ce=i(u),O=l(u,"P",{"data-svelte-h":!0}),f(O)!=="svelte-15nsa67"&&(O.textContent=De),je=i(u),y(Y.$$.fragment,u),u.forEach(t),We=i(ee),K=l(ee,"LI",{"data-svelte-h":!0}),f(K)!=="svelte-bvwr8d"&&(K.innerHTML=Oe),ee.forEach(t),this.h()},h(){c(M,"name","hf:doc:metadata"),c(M,"content",JSON.stringify(dt)),c(_,"id","load-safetensors"),c(_,"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"),c(_,"href","#load-safetensors"),c(T,"class","relative group"),c(Z,"id","convert-to-safetensors"),c(Z,"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"),c(Z,"href","#convert-to-safetensors"),c($,"class","relative group"),c(U,"id","why-use-safetensors"),c(U,"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"),c(U,"href","#why-use-safetensors"),c(k,"class","relative group")},m(e,s){r(document.head,M),o(e,le,s),o(e,T,s),r(T,_),r(_,se),g(x,se,null),r(T,Ze),r(T,A),o(e,ae,s),g(J,e,s),o(e,ie,s),o(e,G,s),o(e,re,s),o(e,B,s),o(e,fe,s),g(C,e,s),o(e,ce,s),o(e,j,s),o(e,he,s),o(e,W,s),o(e,pe,s),g(P,e,s),o(e,de,s),o(e,z,s),o(e,ue,s),g(L,e,s),o(e,me,s),o(e,$,s),r($,Z),r(Z,oe),g(N,oe,null),r($,Ue),r($,E),o(e,ye,s),o(e,S,s),o(e,ge,s),o(e,H,s),o(e,be,s),g(I,e,s),o(e,we,s),o(e,k,s),r(k,U),r(U,ne),g(R,ne,null),r(k,xe),r(k,F),o(e,ve,s),o(e,V,s),o(e,Me,s),o(e,d,s),r(d,q),r(d,Je),r(d,h),r(h,Q),r(h,Ge),r(h,D),r(h,Be),g(X,h,null),r(h,Ce),r(h,O),r(h,je),g(Y,h,null),r(d,We),r(d,K),Te=!0},p:at,i(e){Te||(b(x.$$.fragment,e),b(J.$$.fragment,e),b(C.$$.fragment,e),b(P.$$.fragment,e),b(L.$$.fragment,e),b(N.$$.fragment,e),b(I.$$.fragment,e),b(R.$$.fragment,e),b(X.$$.fragment,e),b(Y.$$.fragment,e),Te=!0)},o(e){w(x.$$.fragment,e),w(J.$$.fragment,e),w(C.$$.fragment,e),w(P.$$.fragment,e),w(L.$$.fragment,e),w(N.$$.fragment,e),w(I.$$.fragment,e),w(R.$$.fragment,e),w(X.$$.fragment,e),w(Y.$$.fragment,e),Te=!1},d(e){e&&(t(le),t(T),t(ae),t(ie),t(G),t(re),t(B),t(fe),t(ce),t(j),t(he),t(W),t(pe),t(de),t(z),t(ue),t(me),t($),t(ye),t(S),t(ge),t(H),t(be),t(we),t(k),t(ve),t(V),t(Me),t(d)),t(M),v(x),v(J,e),v(C,e),v(P,e),v(L,e),v(N),v(I,e),v(R),v(X),v(Y)}}}const dt={local:"load-safetensors",sections:[{local:"convert-to-safetensors",title:"Convert to safetensors"},{local:"why-use-safetensors",title:"Why use safetensors?"}],title:"Load safetensors"};function ut(ze){return it(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class vt extends rt{constructor(M){super(),ft(this,M,ut,pt,lt,{})}}export{vt as component};

Xet Storage Details

Size:
15.1 kB
·
Xet hash:
63e331d84484d43c0305f30c903d6335feb13da8af3bc49bf08fa13df7e784e0

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.