import{s as _t,o as yt,n as vt}from"../chunks/scheduler.8a2cc2fa.js";import{S as wt,i as Mt,e as p,s,c as h,h as Lt,a as d,d as n,b as l,f as D,g as $,j as P,k as Q,l as m,m as i,n as g,t as _,o as y,p as v}from"../chunks/index.7079e750.js";import{C as Tt,H as ut,E as jt}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.b2e4ed49.js";import{D as st}from"../chunks/Docstring.7dc3c45d.js";import{C as xt}from"../chunks/CodeBlock.af9da676.js";import{E as Jt}from"../chunks/ExampleCodeBlock.d2544fd1.js";function It(X){let r,x="Example:",w,b,c;return b=new xt({props:{code:"aW1wb3J0JTIwdG9yY2glMEFpbXBvcnQlMjB0b3JjaC5ubiUyMGFzJTIwbm4lMEElMEFpbXBvcnQlMjBiaXRzYW5kYnl0ZXMlMjBhcyUyMGJuYiUwQWZyb20lMjBibmIubm4lMjBpbXBvcnQlMjBMaW5lYXI4Yml0THQlMEElMEFmcDE2X21vZGVsJTIwJTNEJTIwbm4uU2VxdWVudGlhbCglMEElMjAlMjAlMjAlMjBubi5MaW5lYXIoNjQlMkMlMjA2NCklMkMlMEElMjAlMjAlMjAlMjBubi5MaW5lYXIoNjQlMkMlMjA2NCklMEEpJTBBJTBBaW50OF9tb2RlbCUyMCUzRCUyMG5uLlNlcXVlbnRpYWwoJTBBJTIwJTIwJTIwJTIwTGluZWFyOGJpdEx0KDY0JTJDJTIwNjQlMkMlMjBoYXNfZnAxNl93ZWlnaHRzJTNERmFsc2UpJTJDJTBBJTIwJTIwJTIwJTIwTGluZWFyOGJpdEx0KDY0JTJDJTIwNjQlMkMlMjBoYXNfZnAxNl93ZWlnaHRzJTNERmFsc2UpJTBBKSUwQSUwQWludDhfbW9kZWwubG9hZF9zdGF0ZV9kaWN0KGZwMTZfbW9kZWwuc3RhdGVfZGljdCgpKSUwQWludDhfbW9kZWwlMjAlM0QlMjBpbnQ4X21vZGVsLnRvKDApJTIwJTIzJTIwUXVhbnRpemF0aW9uJTIwaGFwcGVucyUyMGhlcmU=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> torch.nn <span class="hljs-keyword">as</span> nn

<span class="hljs-keyword">import</span> bitsandbytes <span class="hljs-keyword">as</span> bnb
<span class="hljs-keyword">from</span> bnb.nn <span class="hljs-keyword">import</span> Linear8bitLt

fp16_model = nn.Sequential(
    nn.Linear(<span class="hljs-number">64</span>, <span class="hljs-number">64</span>),
    nn.Linear(<span class="hljs-number">64</span>, <span class="hljs-number">64</span>)
)

int8_model = nn.Sequential(
    Linear8bitLt(<span class="hljs-number">64</span>, <span class="hljs-number">64</span>, has_fp16_weights=<span class="hljs-literal">False</span>),
    Linear8bitLt(<span class="hljs-number">64</span>, <span class="hljs-number">64</span>, has_fp16_weights=<span class="hljs-literal">False</span>)
)

int8_model.load_state_dict(fp16_model.state_dict())
int8_model = int8_model.to(<span class="hljs-number">0</span>) <span class="hljs-comment"># Quantization happens here</span>`,wrap:!1}}),{c(){r=p("p"),r.textContent=x,w=s(),h(b.$$.fragment)},l(a){r=d(a,"P",{"data-svelte-h":!0}),P(r)!=="svelte-11lpom8"&&(r.textContent=x),w=l(a),$(b.$$.fragment,a)},m(a,f){i(a,r,f),i(a,w,f),g(b,a,f),c=!0},p:vt,i(a){c||(_(b.$$.fragment,a),c=!0)},o(a){y(b.$$.fragment,a),c=!1},d(a){a&&(n(r),n(w)),v(b,a)}}}function Et(X){let r,x,w,b,c,a,f,H,J,bt='<a href="https://hf.co/papers/2208.07339" rel="nofollow">LLM.int8()</a> is a quantization method that aims to make large language model inference more accessible without significant degradation. Unlike naive 8-bit quantization, which can result in loss of critical information and accuracy, LLM.int8() dynamically adapts to ensure sensitive components of the computation retain higher precision when needed. The key is to extract the outliers from the inputs and weights and multiply them in 16-bit. All other values are multiplied in 8-bit before being dequantized back to 16-bits. The outputs from the 16-bit and 8-bit multiplication are combined to produce the final output.',R,I,ct='<a href="../../explanations/resources#llm-int8">Further Resources</a>',V,E,Y,o,k,lt,Z,ft=`This class is the base module for the <a href="https://arxiv.org/abs/2208.07339" rel="nofollow">LLM.int8()</a> algorithm.
To read more about it, have a look at the paper.`,it,z,ht=`In order to quantize a linear layer one should first load the original fp16 / bf16 weights into
the Linear8bitLt module, then call <code>int8_module.to(&quot;cuda&quot;)</code> to quantize the fp16 weights.`,rt,L,ot,T,C,mt,F,$t="Initialize Linear8bitLt class.",q,B,S,M,N,pt,j,W,dt,U,gt="Initialize self.  See help(type(self)) for accurate signature.",O,G,K,A,tt;return c=new Tt({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),f=new ut({props:{title:"LLM.int8()",local:"llmint8",headingTag:"h1"}}),E=new ut({props:{title:"Linear8bitLt",local:"bitsandbytes.nn.Linear8bitLt",headingTag:"h2"}}),k=new st({props:{name:"class bitsandbytes.nn.Linear8bitLt",anchor:"bitsandbytes.nn.Linear8bitLt",parameters:[{name:"input_features",val:": int"},{name:"output_features",val:": int"},{name:"bias",val:" = True"},{name:"has_fp16_weights",val:" = True"},{name:"threshold",val:" = 0.0"},{name:"index",val:" = None"},{name:"device",val:" = None"}],source:"https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/bitsandbytes/nn/modules.py#L935"}}),L=new Jt({props:{anchor:"bitsandbytes.nn.Linear8bitLt.example",$$slots:{default:[It]},$$scope:{ctx:X}}}),C=new st({props:{name:"__init__",anchor:"bitsandbytes.nn.Linear8bitLt.__init__",parameters:[{name:"input_features",val:": int"},{name:"output_features",val:": int"},{name:"bias",val:" = True"},{name:"has_fp16_weights",val:" = True"},{name:"threshold",val:" = 0.0"},{name:"index",val:" = None"},{name:"device",val:" = None"}],parametersDescription:[{anchor:"bitsandbytes.nn.Linear8bitLt.__init__.input_features",description:`<strong>input_features</strong> (<code>int</code>) &#x2014;
Number of input features of the linear layer.`,name:"input_features"},{anchor:"bitsandbytes.nn.Linear8bitLt.__init__.output_features",description:`<strong>output_features</strong> (<code>int</code>) &#x2014;
Number of output features of the linear layer.`,name:"output_features"},{anchor:"bitsandbytes.nn.Linear8bitLt.__init__.bias",description:`<strong>bias</strong> (<code>bool</code>, defaults to <code>True</code>) &#x2014;
Whether the linear class uses the bias term as well.`,name:"bias"}],source:"https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/bitsandbytes/nn/modules.py#L967"}}),B=new ut({props:{title:"Int8Params",local:"bitsandbytes.nn.Int8Params",headingTag:"h2"}}),N=new st({props:{name:"class bitsandbytes.nn.Int8Params",anchor:"bitsandbytes.nn.Int8Params",parameters:[{name:"data",val:": typing.Optional[torch.Tensor] = None"},{name:"requires_grad",val:" = True"},{name:"has_fp16_weights",val:" = False"},{name:"CB",val:": typing.Optional[torch.Tensor] = None"},{name:"SCB",val:": typing.Optional[torch.Tensor] = None"}],source:"https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/bitsandbytes/nn/modules.py#L637"}}),W=new st({props:{name:"__init__",anchor:"bitsandbytes.nn.Int8Params.__init__",parameters:[{name:"*args",val:""},{name:"**kwargs",val:""}]}}),G=new jt({props:{source:"https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/docs/source/reference/nn/linear8bit.mdx"}}),{c(){r=p("meta"),x=s(),w=p("p"),b=s(),h(c.$$.fragment),a=s(),h(f.$$.fragment),H=s(),J=p("p"),J.innerHTML=bt,R=s(),I=p("p"),I.innerHTML=ct,V=s(),h(E.$$.fragment),Y=s(),o=p("div"),h(k.$$.fragment),lt=s(),Z=p("p"),Z.innerHTML=ft,it=s(),z=p("p"),z.innerHTML=ht,rt=s(),h(L.$$.fragment),ot=s(),T=p("div"),h(C.$$.fragment),mt=s(),F=p("p"),F.textContent=$t,q=s(),h(B.$$.fragment),S=s(),M=p("div"),h(N.$$.fragment),pt=s(),j=p("div"),h(W.$$.fragment),dt=s(),U=p("p"),U.textContent=gt,O=s(),h(G.$$.fragment),K=s(),A=p("p"),this.h()},l(t){const e=Lt("svelte-u9bgzb",document.head);r=d(e,"META",{name:!0,content:!0}),e.forEach(n),x=l(t),w=d(t,"P",{}),D(w).forEach(n),b=l(t),$(c.$$.fragment,t),a=l(t),$(f.$$.fragment,t),H=l(t),J=d(t,"P",{"data-svelte-h":!0}),P(J)!=="svelte-3b0d0i"&&(J.innerHTML=bt),R=l(t),I=d(t,"P",{"data-svelte-h":!0}),P(I)!=="svelte-1a4aty"&&(I.innerHTML=ct),V=l(t),$(E.$$.fragment,t),Y=l(t),o=d(t,"DIV",{class:!0});var u=D(o);$(k.$$.fragment,u),lt=l(u),Z=d(u,"P",{"data-svelte-h":!0}),P(Z)!=="svelte-xo5v9s"&&(Z.innerHTML=ft),it=l(u),z=d(u,"P",{"data-svelte-h":!0}),P(z)!=="svelte-13qed9e"&&(z.innerHTML=ht),rt=l(u),$(L.$$.fragment,u),ot=l(u),T=d(u,"DIV",{class:!0});var et=D(T);$(C.$$.fragment,et),mt=l(et),F=d(et,"P",{"data-svelte-h":!0}),P(F)!=="svelte-p49gdy"&&(F.textContent=$t),et.forEach(n),u.forEach(n),q=l(t),$(B.$$.fragment,t),S=l(t),M=d(t,"DIV",{class:!0});var nt=D(M);$(N.$$.fragment,nt),pt=l(nt),j=d(nt,"DIV",{class:!0});var at=D(j);$(W.$$.fragment,at),dt=l(at),U=d(at,"P",{"data-svelte-h":!0}),P(U)!=="svelte-gef1cn"&&(U.textContent=gt),at.forEach(n),nt.forEach(n),O=l(t),$(G.$$.fragment,t),K=l(t),A=d(t,"P",{}),D(A).forEach(n),this.h()},h(){Q(r,"name","hf:doc:metadata"),Q(r,"content",kt),Q(T,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),Q(o,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),Q(j,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),Q(M,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8")},m(t,e){m(document.head,r),i(t,x,e),i(t,w,e),i(t,b,e),g(c,t,e),i(t,a,e),g(f,t,e),i(t,H,e),i(t,J,e),i(t,R,e),i(t,I,e),i(t,V,e),g(E,t,e),i(t,Y,e),i(t,o,e),g(k,o,null),m(o,lt),m(o,Z),m(o,it),m(o,z),m(o,rt),g(L,o,null),m(o,ot),m(o,T),g(C,T,null),m(T,mt),m(T,F),i(t,q,e),g(B,t,e),i(t,S,e),i(t,M,e),g(N,M,null),m(M,pt),m(M,j),g(W,j,null),m(j,dt),m(j,U),i(t,O,e),g(G,t,e),i(t,K,e),i(t,A,e),tt=!0},p(t,[e]){const u={};e&2&&(u.$$scope={dirty:e,ctx:t}),L.$set(u)},i(t){tt||(_(c.$$.fragment,t),_(f.$$.fragment,t),_(E.$$.fragment,t),_(k.$$.fragment,t),_(L.$$.fragment,t),_(C.$$.fragment,t),_(B.$$.fragment,t),_(N.$$.fragment,t),_(W.$$.fragment,t),_(G.$$.fragment,t),tt=!0)},o(t){y(c.$$.fragment,t),y(f.$$.fragment,t),y(E.$$.fragment,t),y(k.$$.fragment,t),y(L.$$.fragment,t),y(C.$$.fragment,t),y(B.$$.fragment,t),y(N.$$.fragment,t),y(W.$$.fragment,t),y(G.$$.fragment,t),tt=!1},d(t){t&&(n(x),n(w),n(b),n(a),n(H),n(J),n(R),n(I),n(V),n(Y),n(o),n(q),n(S),n(M),n(O),n(K),n(A)),n(r),v(c,t),v(f,t),v(E,t),v(k),v(L),v(C),v(B,t),v(N),v(W),v(G,t)}}}const kt='{"title":"LLM.int8()","local":"llmint8","sections":[{"title":"Linear8bitLt","local":"bitsandbytes.nn.Linear8bitLt","sections":[],"depth":2},{"title":"Int8Params","local":"bitsandbytes.nn.Int8Params","sections":[],"depth":2}],"depth":1}';function Ct(X){return yt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class zt extends wt{constructor(r){super(),Mt(this,r,Ct,Et,_t,{})}}export{zt as component};
