@slexisvn/mlfw 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.js +284 -0
- package/dist/index.node.js +428 -0
- package/package.json +9 -24
- package/dist/cli.js +0 -411
- package/dist/index.js +0 -207
- package/patches/terminal-kit+3.1.2.patch +0 -140
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
var mN=Object.defineProperty;var l=(n,e)=>mN(n,"name",{value:e,configurable:!0});var M=(n,e)=>()=>(n&&(e=n(n=0)),e);var at=(n,e)=>{for(var t in e)mN(n,t,{get:e[t],enumerable:!0})};function Ah(n,e){let t=e||"mlc",r=n.indexOf("->"),s=r>=0?n.substring(0,r).trim():n.trim(),o=r>=0?n.substring(r+2).trim():"",i=s.indexOf("("),a=s.lastIndexOf(")"),u,p="",c=s.substring(0,i).trim(),f=c.indexOf(".");f>=0?(u=c.substring(0,f),p=c.substring(f+1)):u=c;let d=s.substring(i+1,a).trim(),h=d.length>0?tP(d):[],m=o.length>0?nP(o):[{kind:"Tensor"}];return new t0(t,u,p,h,m)}function tP(n){return gN(n,",").map(t=>{let r=t.trim(),s=r.indexOf("="),o=r,i;s>=0&&(o=r.substring(0,s).trim(),i=r.substring(s+1).trim());let a=o.endsWith("!");a&&(o=o.substring(0,o.length-1).trim());let u=o.lastIndexOf(" "),p,c;u>=0?(p=o.substring(0,u).trim(),c=o.substring(u+1).trim()):(p=o,c="");let f=n0.get(p)||"Scalar";return new e0(c,f,i,a)})}function nP(n){let e=n.trim();if(e.startsWith("(")&&e.endsWith(")")){let r=e.substring(1,e.length-1);return gN(r,",").map(o=>{let i=o.trim();return{kind:n0.get(i)||"Tensor"}})}return[{kind:n0.get(e)||"Tensor"}]}function gN(n,e){let t=[],r=0,s=0;for(let o=0;o<n.length;o++){let i=n[o];i==="("||i==="["?r++:i===")"||i==="]"?r--:i===e&&r===0&&(t.push(n.substring(s,o)),s=o+1)}return t.push(n.substring(s)),t}var eP,e0,t0,n0,r0=M(()=>{"use strict";eP=new Set(["Tensor","Tensor[]"]),e0=class{static{l(this,"SchemaArg")}name;kind;defaultValue;isOut;constructor(e,t,r,s){this.name=e,this.kind=t,this.defaultValue=r??null,this.isOut=s??!1}get isTensor(){return eP.has(this.kind)}},t0=class{static{l(this,"OperatorSchema")}namespace;name;overload;args;returns;_key;_tensorArgIndices;constructor(e,t,r,s,o){this.namespace=e,this.name=t,this.overload=r||"",this.args=Object.freeze([...s]),this.returns=Object.freeze([...o]),this._key=null,this._tensorArgIndices=null}qualifiedName(){return`${this.namespace}::${this.name}`}key(){return this._key||(this._key=this.overload?`${this.namespace}::${this.name}.${this.overload}`:`${this.namespace}::${this.name}`),this._key}get tensorArgIndices(){if(!this._tensorArgIndices){let e=[];for(let t=0;t<this.args.length;t++)this.args[t].isTensor&&e.push(t);this._tensorArgIndices=Object.freeze(e)}return this._tensorArgIndices}get numTensorArgs(){return this.tensorArgIndices.length}},n0=new Map([["Tensor","Tensor"],["Scalar","Scalar"],["int","int"],["float","float"],["bool","bool"],["int[]","int[]"],["Tensor[]","Tensor[]"],["Dtype","Dtype"],["Device","Device"],["str","str"],["MemoryFormat","MemoryFormat"]]);l(Ah,"parseSchema");l(tP,"_parseArgs");l(nP,"_parseReturns");l(gN,"_splitTopLevel")});function yN(n){return typeof n=="object"&&n!==null&&"_impl"in n}function _N(n){return n instanceof Yr?n:yN(n)?Yr.tensor(n):typeof n=="number"?Number.isInteger(n)?Yr.int(n):Yr.float(n):typeof n=="boolean"?Yr.bool(n):typeof n=="string"?Yr.string(n):Array.isArray(n)?n.length>0&&yN(n[0])?Yr.tensorList(n):Yr.intList(n):(n==null,Yr.none())}var Yr,Mr,sp=M(()=>{"use strict";Yr=class n{static{l(this,"IValue")}tag;value;constructor(e,t){this.tag=e,this.value=t}static tensor(e){return new n(0,e)}static int(e){return new n(1,e)}static float(e){return new n(2,e)}static bool(e){return new n(3,e)}static intList(e){return new n(4,e)}static tensorList(e){return new n(5,e)}static string(e){return new n(6,e)}static none(){return new n(7,null)}static device(e){return new n(8,e)}static dtype(e){return new n(9,e)}isTensor(){return this.tag===0}isInt(){return this.tag===1}isFloat(){return this.tag===2}isBool(){return this.tag===3}isIntList(){return this.tag===4}isTensorList(){return this.tag===5}isString(){return this.tag===6}isNone(){return this.tag===7}toTensor(){return this.value}toInt(){return this.value}toFloat(){return this.value}toBool(){return this.value}toIntList(){return this.value}toTensorList(){return this.value}toString(){return this.value}toDevice(){return this.value}toDtype(){return this.value}},Mr=class n{static{l(this,"KernelFunction")}_boxed;_unboxed;constructor(e,t){this._boxed=e||null,this._unboxed=t||null}static fromBoxed(e){return new n(e)}static fromUnboxed(e){return new n(void 0,e)}static fromBoth(e,t){return new n(e,t)}get isBoxed(){return this._boxed!==null}get isUnboxed(){return this._unboxed!==null}callUnboxed(e,...t){return this._unboxed?this._unboxed(e,...t):this._callBoxedAsUnboxed(e,t)}callBoxed(e,t){return this._boxed?this._boxed(e,t):this._callUnboxedAsBoxed(e,t)}_callBoxedAsUnboxed(e,t){let r=t.map(i=>_N(i)),s=this._boxed;if(!s)return;let o=s(e,r);return Array.isArray(o)?o.length===1?o[0].value:o.map(i=>i.value):o instanceof Yr?o.value:o}_callUnboxedAsBoxed(e,t){let r=t.map(i=>i.value),s=this._unboxed;if(!s)return[];let o=s(e,...r);return o==null?[]:[_N(o)]}};l(yN,"hasImpl");l(_N,"_toIValue")});function bN(n){Jo=n}var Jo,Vt,ys=M(()=>{"use strict";r0();sp();Jo=null;l(bN,"_setDispatcher");Vt=class{static{l(this,"Library")}_namespace;_kind;_registrations;constructor(e,t){this._namespace=e,this._kind=t,this._registrations=[]}def(e){let t=Ah(e,this._namespace);return Jo&&Jo.registerOp(t),this._registrations.push({type:"def",schema:t}),this}impl(e,t,r){let s=r instanceof Mr?r:Mr.fromUnboxed(r);return Jo&&Jo.registerKernel(`${this._namespace}::${e}`,t,s),this._registrations.push({type:"impl",name:e,key:t,kernelFn:s}),this}implBoxed(e,t,r){let s=Mr.fromBoxed(r);return Jo&&Jo.registerKernel(`${this._namespace}::${e}`,t,s),this._registrations.push({type:"impl",name:e,key:t,kernelFn:s}),this}fallback(e,t){let r=t instanceof Mr?t:Mr.fromBoxed(t);return Jo&&Jo.registerFallback(e,r),this._registrations.push({type:"fallback",key:e,kernelFn:r}),this}replay(e){for(let t of this._registrations)t.type==="def"?e.registerOp(t.schema):t.type==="impl"?e.registerKernel(`${this._namespace}::${t.name}`,t.key,t.kernelFn):t.type==="fallback"&&e.registerFallback(t.key,t.kernelFn)}}});function Uu(n){let e=rP[n];if(e===void 0)throw new Error(`No backend key for device: ${n}`);return e}function xN(n){return sP[n]??40}function wN(n){return n=n-(n>>1&1431655765),n=(n&858993459)+(n>>2&858993459),(n+(n>>4)&252645135)*16843009>>24}var Xn,TN,rP,sP,Jr,op,qq,ip,Wq,An=M(()=>{"use strict";Xn=(w=>(w[w.CPU=0]="CPU",w[w.GPU=1]="GPU",w[w.WASM=2]="WASM",w[w.META=3]="META",w[w.LAZY=4]="LAZY",w[w.CUSTOM_0=5]="CUSTOM_0",w[w.CUSTOM_1=6]="CUSTOM_1",w[w.CUSTOM_2=7]="CUSTOM_2",w[w.CUSTOM_3=8]="CUSTOM_3",w[w.BATCHED=20]="BATCHED",w[w.VMAP=24]="VMAP",w[w.FUNCTIONALIZE=28]="FUNCTIONALIZE",w[w.AUTOCAST=32]="AUTOCAST",w[w.AUTOGRAD=40]="AUTOGRAD",w[w.AUTOGRAD_CPU=41]="AUTOGRAD_CPU",w[w.AUTOGRAD_GPU=42]="AUTOGRAD_GPU",w[w.AUTOGRAD_WASM=43]="AUTOGRAD_WASM",w[w.TRACING=48]="TRACING",w[w.NUM_KEYS=49]="NUM_KEYS",w))(Xn||{}),TN=new Array(49).fill(null);for(let[n,e]of Object.entries(Xn))n!=="NUM_KEYS"&&typeof e=="number"&&e<49&&(TN[e]=n);rP=Object.freeze({cpu:0,gpu:1,wasm:2,webgpu:5,meta:3,lazy:4}),sP=Object.freeze({0:41,1:42,2:43});l(Uu,"backendKeyForDevice");l(xN,"autogradKeyForBackend");Jr=class n{static{l(this,"DispatchKeySet")}_lo;_hi;constructor(e,t){this._lo=e|0,this._hi=t|0}static fromKey(e){return e<32?new n(1<<e,0):new n(0,1<<e-32)}static fromKeys(...e){let t=0,r=0;for(let s=0;s<e.length;s++){let o=e[s];o<32?t|=1<<o:r|=1<<o-32}return new n(t,r)}add(e){return e<32?new n(this._lo|1<<e,this._hi):new n(this._lo,this._hi|1<<e-32)}remove(e){return e<32?new n(this._lo&~(1<<e),this._hi):new n(this._lo,this._hi&~(1<<e-32))}has(e){return e<32?(this._lo&1<<e)!==0:(this._hi&1<<e-32)!==0}without(e){return this.remove(e)}union(e){return new n(this._lo|e._lo,this._hi|e._hi)}intersect(e){return new n(this._lo&e._lo,this._hi&e._hi)}subtract(e){return new n(this._lo&~e._lo,this._hi&~e._hi)}isEmpty(){return this._lo===0&&this._hi===0}equals(e){return this._lo===e._lo&&this._hi===e._hi}highestPriority(){return this._hi!==0?63-Math.clz32(this._hi):this._lo!==0?31-Math.clz32(this._lo):-1}lowestPriority(){return this._lo!==0?31-Math.clz32(this._lo&-this._lo):this._hi!==0?63-Math.clz32(this._hi&-this._hi):-1}count(){return wN(this._lo)+wN(this._hi)}*[Symbol.iterator](){let e=this._hi;for(;e!==0;){let r=31-Math.clz32(e);yield r+32,e&=~(1<<r)}let t=this._lo;for(;t!==0;){let r=31-Math.clz32(t);yield r,t&=~(1<<r)}}toString(){let e=[];for(let t of this)e.push(TN[t]||String(t));return`DispatchKeySet(${e.join(", ")})`}};l(wN,"_popcount32");op=new Jr(0,0),qq=Jr.fromKeys(0,1,2,3,4,5,6,7,8),ip=Jr.fromKeys(40,41,42,43),Wq=Jr.fromKeys(20,24,28,32,40,41,42,43,48)});var Oh,SN=M(()=>{"use strict";An();Oh=class{static{l(this,"OperatorEntry")}_schema;_kernels;_catchAll;constructor(e){this._schema=e,this._kernels=new Array(49).fill(null),this._catchAll=null}get schema(){return this._schema}registerKernel(e,t){this._kernels[e]=t}removeKernel(e){this._kernels[e]=null}lookupKernel(e){return this._kernels[e]}hasKernel(e){return this._kernels[e]!==null}get catchAll(){return this._catchAll}setCatchAll(e){this._catchAll=e}bestKernel(e){for(let t of e){let r=this._kernels[t];if(r)return{key:t,kernel:r}}return this._catchAll?{key:-1,kernel:this._catchAll}:null}registeredKeys(){let e=[];for(let t=0;t<this._kernels.length;t++)this._kernels[t]&&e.push(t);return e}}});var Bh,vN=M(()=>{"use strict";Bh=class{static{l(this,"OperatorHandle")}_entry;_schema;constructor(e,t){this._entry=e,this._schema=t}get entry(){return this._entry}get schema(){return this._schema}get name(){return this._schema.name}get qualifiedName(){return this._schema.qualifiedName()}get key(){return this._schema.key()}get tensorArgIndices(){return this._schema.tensorArgIndices}lookupKernel(e){return this._entry.lookupKernel(e)}bestKernel(e){return this._entry.bestKernel(e)}}});var Rh,kN=M(()=>{"use strict";An();Rh=class{static{l(this,"FallbackTable")}_kernels;constructor(){this._kernels=new Array(49).fill(null)}register(e,t){this._kernels[e]=t}remove(e){this._kernels[e]=null}lookup(e){return this._kernels[e]}has(e){return this._kernels[e]!==null}registeredKeys(){let e=[];for(let t=0;t<this._kernels.length;t++)this._kernels[t]&&e.push(t);return e}}});function oP(n){return typeof n=="object"&&n!==null&&"then"in n&&typeof n.then=="function"}function NN(n,e){Ra.push(void 0,n);let t;try{t=e()}catch(r){throw Ra.pop(),r}return oP(t)?t.then(r=>(Ra.pop(),r),r=>{throw Ra.pop(),r}):(Ra.pop(),t)}var s0,Ra,o0=M(()=>{"use strict";An();s0=class{static{l(this,"GuardStack")}_frames;constructor(){this._frames=[]}push(e,t){this._frames.push({exclude:e||op,include:t||op})}pop(){return this._frames.pop()}apply(e){let t=e;for(let r=this._frames.length-1;r>=0;r--){let s=this._frames[r];t=t.subtract(s.exclude),t=t.union(s.include)}return t}get depth(){return this._frames.length}clear(){this._frames.length=0}},Ra=new s0;l(oP,"isThenable");l(NN,"withIncludedKeys")});function iP(n){return typeof n=="object"&&n!==null&&"dispatchKeySet"in n}function a0(n,e){if(!e)return n;if(iP(e)&&e.dispatchKeySet)return n.union(e.dispatchKeySet);if(Array.isArray(e))for(let t=0;t<e.length;t++)n=a0(n,e[t]);return n}function u0(n,e){let t=op;if(e){let r=e.tensorArgIndices;for(let s=0;s<r.length;s++){let o=r[s];o<n.length&&(t=a0(t,n[o]))}}else for(let r=0;r<n.length;r++)t=a0(t,n[r]);return t}var i0,Lt,Ma=M(()=>{"use strict";An();SN();vN();kN();o0();ys();r0();l(iP,"hasDispatchKeySet");i0=class{static{l(this,"Dispatcher")}_entries;_handles;_fallbacks;constructor(){this._entries=new Map,this._handles=new Map,this._fallbacks=new Rh}registerOp(e){let t=e.key(),r=this._handles.get(t);if(r)return r;let s=new Oh(e);this._entries.set(t,s);let o=new Bh(s,e);return this._handles.set(t,o),o}findOp(e){let t=e.includes("::")?e:`mlc::${e}`;return this._handles.get(t)||null}findOrRegisterOp(e){let t=this.findOp(e);if(!t){let r=Ah(`${e}() -> Tensor`,"mlc");t=this.registerOp(r)}return t}registerKernel(e,t,r){let s=e.includes("::")?e:`mlc::${e}`,o=this._entries.get(s);if(!o)throw new Error(`Op '${s}' not registered`);o.registerKernel(t,r)}registerFallback(e,t){this._fallbacks.register(e,t)}dispatch(e,t,...r){let s=Ra.apply(t);return this._dispatchInternal(e,s,r)}redispatch(e,t,...r){return this._dispatchInternal(e,t,r)}_dispatchInternal(e,t,r){let s=t.highestPriority();if(s<0)throw new Error(`No dispatch key found for op '${e.name}'`);let o=e.lookupKernel(s);if(o||(o=this._fallbacks.lookup(s)),!o){let a=e.entry.catchAll;a&&(o=a)}if(!o)throw new Error(`No kernel registered for op '${e.name}' with dispatch key ${s}`);let i=t.without(s);return o.callUnboxed(i,...r)}callOp(e,...t){let r=this.findOp(e);if(!r)throw new Error(`Op '${e}' not found`);let s=u0(t,r.schema);return this.dispatch(r,s,...t)}listOps(){return[...this._handles.keys()]}hasOp(e){let t=e.includes("::")?e:`mlc::${e}`;return this._handles.has(t)}get fallbacks(){return this._fallbacks}};l(a0,"_unionArg");l(u0,"computeKeySet");Lt=new i0;bN(Lt)});function qu(n){return uP+n}var aP,uP,Pe,po=M(()=>{"use strict";aP={ceildiv:"ceilDiv"},uP="_sym_";l(qu,"symVarName");Pe=class n{static{l(this,"SymInt")}constructor(e,t=null,r=[]){this.type=e,this.name=t,this.args=r}static var(e){return new n("var",e)}static const(e){return e}static add(e,t){return typeof e=="number"&&typeof t=="number"?e+t:e===0?t:t===0?e:n.equals(e,t)?n.mul(2,e):new n("add",null,[e,t])}static sub(e,t){return typeof e=="number"&&typeof t=="number"?e-t:t===0?e:n.equals(e,t)?0:new n("sub",null,[e,t])}static neg(e){return typeof e=="number"?-e:e instanceof n&&e.type==="neg"?e.args[0]:new n("neg",null,[e])}static mul(e,t){return typeof e=="number"&&typeof t=="number"?e*t:e===0||t===0?0:e===1?t:t===1?e:new n("mul",null,[e,t])}static div(e,t){if(t===0)throw new Error("SymInt.div: division by zero");return typeof e=="number"&&typeof t=="number"?Math.floor(e/t):e===0?0:t===1?e:n.equals(e,t)?1:new n("div",null,[e,t])}static mod(e,t){if(t===0)throw new Error("SymInt.mod: modulo by zero");return typeof e=="number"&&typeof t=="number"?(e%t+t)%t:e===0||t===1||n.equals(e,t)?0:new n("mod",null,[e,t])}static max(e,t){return typeof e=="number"&&typeof t=="number"?Math.max(e,t):e===t||n.equals(e,t)?e:new n("max",null,[e,t])}static min(e,t){return typeof e=="number"&&typeof t=="number"?Math.min(e,t):e===t||n.equals(e,t)?e:new n("min",null,[e,t])}static ceilDiv(e,t){return typeof e=="number"&&typeof t=="number"?Math.ceil(e/t):new n("ceildiv",null,[e,t])}static equals(e,t){if(e===t)return!0;if(typeof e=="number"||typeof t=="number"||!(e instanceof n)||!(t instanceof n)||e.type!==t.type)return!1;if(e.type==="var")return e.name===t.name;if(e.args.length!==t.args.length)return!1;if(e.type==="add"||e.type==="mul"||e.type==="max"||e.type==="min"){let r=n.equals(e.args[0],t.args[0])&&n.equals(e.args[1],t.args[1]),s=n.equals(e.args[0],t.args[1])&&n.equals(e.args[1],t.args[0]);return r||s}for(let r=0;r<e.args.length;r++)if(!n.equals(e.args[r],t.args[r]))return!1;return!0}static substitute(e,t,r){if(typeof e=="number"||!(e instanceof n))return e;if(e.type==="var")return e.name===t?r:e;let s=e.args.map(i=>n.substitute(i,t,r)),o=n[aP[e.type]||e.type];return o&&s.length===2?o(s[0],s[1]):o&&s.length===1?o(s[0]):new n(e.type,e.name,s)}static evaluate(e,t){if(typeof e=="number"||!(e instanceof n))return e;if(e.type==="var"){if(t.has(e.name))return t.get(e.name);throw new Error(`Unbound symbolic variable: ${e.name}`)}let r=e.args.map(s=>n.evaluate(s,t));switch(e.type){case"add":return r[0]+r[1];case"sub":return r[0]-r[1];case"mul":return r[0]*r[1];case"div":return Math.floor(r[0]/r[1]);case"mod":return(r[0]%r[1]+r[1])%r[1];case"max":return Math.max(r[0],r[1]);case"min":return Math.min(r[0],r[1]);case"neg":return-r[0];case"ceildiv":return Math.ceil(r[0]/r[1]);default:throw new Error(`Unknown SymInt op: ${e.type}`)}}static freeVars(e,t=new Set){if(typeof e=="number"||!(e instanceof n))return t;if(e.type==="var")return t.add(e.name),t;for(let r of e.args)n.freeVars(r,t);return t}static isConst(e){return typeof e=="number"}static toConst(e){return typeof e=="number"?e:null}toString(){return this.type==="var"?this.name:this.type==="add"?`(${this.args[0]} + ${this.args[1]})`:this.type==="sub"?`(${this.args[0]} - ${this.args[1]})`:this.type==="mul"?`(${this.args[0]} * ${this.args[1]})`:this.type==="div"?`(${this.args[0]} / ${this.args[1]})`:this.type==="mod"?`(${this.args[0]} % ${this.args[1]})`:this.type==="max"?`max(${this.args[0]}, ${this.args[1]})`:this.type==="min"?`min(${this.args[0]}, ${this.args[1]})`:this.type==="neg"?`(-${this.args[0]})`:this.type==="ceildiv"?`ceildiv(${this.args[0]}, ${this.args[1]})`:"unknown"}}});function Mh(n,e){return n===e?!0:n instanceof Pe&&e instanceof Pe?Pe.equals(n,e):!1}function ap(n,e){let t=1;for(let r=0;r<n.length;r++){let s=n[r];if(typeof s!="number"||s<0)return e;t*=s}return t}function f0(n){let e=1;for(let t=0;t<n.length;t++){let r=n[t];if(typeof r=="number"){if(r<0)return X;e=Pe.mul(e,r)}else if(r instanceof Pe)e=Pe.mul(e,r);else return X}return e}function fo(n){let e=AN[n];if(e===void 0)throw new Error(`Unknown dtype: ${n}`);return e}function jt(n){return lP.has(n)}function d0(n){return cP.has(n)}function pP(n,e){return Mh(n,e)?n:n===1?e:e===1?n:n===X?e===X?X:e:e===X?n:n instanceof Pe||e instanceof Pe?X:null}function p0(n,e){return n===e?!0:n&&typeof n.equals=="function"?n.equals(e):!1}function Gn(n){if(n instanceof I)return`tensor<${n.shape.map(t=>t===X?"?":String(t)).join("x")}x${n.dtype}>`;if(n instanceof Ia)return`tuple<${n.elements.map(Gn).join(", ")}>`;if(n instanceof l0)return"token";if(n instanceof c0){let e=n.inputs.map(Gn).join(", "),t=n.outputs.map(Gn).join(", ");return`(${e}) -> (${t})`}return"unknown"}var er,AN,lP,cP,X,Gt,I,Ia,l0,c0,Re=M(()=>{"use strict";po();l(Mh,"dimEquals");er=(f=>(f.F16="f16",f.BF16="bf16",f.F32="f32",f.F64="f64",f.I8="i8",f.I16="i16",f.I32="i32",f.I64="i64",f.UI8="ui8",f.BOOL="bool",f.INDEX="index",f))(er||{}),AN=Object.freeze({f16:2,bf16:2,f32:4,f64:8,i8:1,i16:2,i32:4,i64:8,ui8:1,bool:1,index:4}),lP=new Set(["f16","bf16","f32","f64"]),cP=new Set(["i8","i16","i32","i64","ui8"]),X=-1;l(ap,"shapeProduct");l(f0,"symbolicShapeProduct");l(fo,"scalarBytes");l(jt,"isFloatType");l(d0,"isIntType");Gt=class n{static{l(this,"Layout")}order;_hash;constructor(e){this.order=Object.freeze([...e]),this._hash=null}static rowMajor(e){let t=new Array(e);for(let r=0;r<e;r++)t[r]=r;return new n(t)}static columnMajor(e){let t=new Array(e);for(let r=0;r<e;r++)t[r]=e-1-r;return new n(t)}get rank(){return this.order.length}isIdentity(){for(let e=0;e<this.order.length;e++)if(this.order[e]!==e)return!1;return!0}inverse(){let e=new Array(this.order.length);for(let t=0;t<this.order.length;t++)e[this.order[t]]=t;return new n(e)}compose(e){if(this.order.length!==e.order.length)throw new Error("Cannot compose layouts of different ranks");let t=new Array(this.order.length);for(let r=0;r<this.order.length;r++)t[r]=this.order[e.order[r]];return new n(t)}computeStrides(e){let t=e.length,r=new Array(t),s=1;for(let o=t-1;o>=0;o--){let i=this.order[o];r[i]=s,e[i]===X||e[i]instanceof Pe?s=X:s!==X&&(s*=e[i])}return r}equals(e){if(this===e)return!0;if(!(e instanceof n)||this.order.length!==e.order.length)return!1;for(let t=0;t<this.order.length;t++)if(this.order[t]!==e.order[t])return!1;return!0}hash(){if(this._hash!==null)return this._hash;let e=2166136261;for(let t=0;t<this.order.length;t++)e=(e^this.order[t])*16777619&2147483647;return this._hash=e,e}};l(pP,"broadcastDim");I=class n{static{l(this,"TensorType")}shape;dtype;layout;_hash;constructor(e,t,r=null){this.shape=Object.freeze([...e]),this.dtype=t,this.layout=r||Gt.rowMajor(e.length),this._hash=null}get rank(){return this.shape.length}get isScalar(){return this.shape.length===0}get isFullyStatic(){return this.shape.every(e=>typeof e=="number"&&e>=0)}get hasDynamic(){return this.shape.some(e=>e===X||e instanceof Pe)}numel(){return ap(this.shape,X)}symbolicNumel(){return f0(this.shape)}sizeInBytes(){let e=this.numel();return e===X?X:e*AN[this.dtype]}strides(){return this.layout.computeStrides(this.shape)}withShape(e){return new n(e,this.dtype,this.layout)}withDtype(e){return new n(this.shape,e,this.layout)}withLayout(e){return new n(this.shape,this.dtype,e)}equals(e){if(this===e)return!0;if(!(e instanceof n)||this.dtype!==e.dtype||this.shape.length!==e.shape.length)return!1;for(let t=0;t<this.shape.length;t++)if(!Mh(this.shape[t],e.shape[t]))return!1;return this.layout.equals(e.layout)}shapeEquals(e){if(!(e instanceof n)||this.dtype!==e.dtype||this.shape.length!==e.shape.length)return!1;for(let t=0;t<this.shape.length;t++)if(!Mh(this.shape[t],e.shape[t]))return!1;return!0}shapeCompatible(e){if(this.shape.length!==e.shape.length)return!1;for(let t=0;t<this.shape.length;t++){let r=this.shape[t],s=e.shape[t];if(!(r===X||s===X)&&!Mh(r,s)&&typeof r=="number"&&typeof s=="number")return!1}return!0}hash(){if(this._hash!==null)return this._hash;let e=2166136261;for(let t=0;t<this.shape.length;t++){let r=this.shape[t],s=typeof r=="number"?r&65535:32767;e=(e^s)*16777619&2147483647}return e=(e^this.dtype.charCodeAt(0))*16777619&2147483647,this._hash=e,e}static broadcastShape(...e){let t=0;for(let s=0;s<e.length;s++)e[s].length>t&&(t=e[s].length);let r=new Array(t);for(let s=0;s<t;s++){let o=1;for(let i=0;i<e.length;i++){let a=e[i],u=s-(t-a.length);if(u<0)continue;let p=pP(o,a[u]);if(p===null)return null;o=p}r[s]=o}return r}static broadcastCompatible(...e){return n.broadcastShape(...e)!==null}},Ia=class n{static{l(this,"TupleType")}elements;constructor(e){this.elements=Object.freeze([...e])}equals(e){if(this===e)return!0;if(!(e instanceof n)||this.elements.length!==e.elements.length)return!1;for(let t=0;t<this.elements.length;t++)if(!p0(this.elements[t],e.elements[t]))return!1;return!0}},l0=class n{static{l(this,"TokenType")}equals(e){return e instanceof n}},c0=class n{static{l(this,"FunctionType")}inputs;outputs;constructor(e,t){this.inputs=Object.freeze([...e]),this.outputs=Object.freeze([...t])}equals(e){if(this===e)return!0;if(!(e instanceof n)||this.inputs.length!==e.inputs.length||this.outputs.length!==e.outputs.length)return!1;for(let t=0;t<this.inputs.length;t++)if(!p0(this.inputs[t],e.inputs[t]))return!1;for(let t=0;t<this.outputs.length;t++)if(!p0(this.outputs[t],e.outputs[t]))return!1;return!0}};l(p0,"typeEquals");l(Gn,"typeToString")});var fP,up,Li,lp,Ih=M(()=>{"use strict";fP=0,up=class{static{l(this,"UseLink")}constructor(e,t){this.user=e,this.operandIndex=t,this.prev=null,this.next=null}},Li=class{static{l(this,"Value")}constructor(e,t=null,r=0){this.type=e,this.definingOp=t,this.resultIndex=r,this.id=fP++,this._useHead=null,this._useTail=null,this._useCount=0}get hasUses(){return this._useHead!==null}get useCount(){return this._useCount}addUse(e){e.prev=this._useTail,e.next=null,this._useTail?this._useTail.next=e:this._useHead=e,this._useTail=e,this._useCount++}removeUse(e){e.prev?e.prev.next=e.next:this._useHead=e.next,e.next?e.next.prev=e.prev:this._useTail=e.prev,e.prev=null,e.next=null,this._useCount--}*uses(){let e=this._useHead;for(;e;){let t=e.next;yield e,e=t}}getUsers(){let e=[],t=this._useHead;for(;t;)e.push(t.user),t=t.next;return e}replaceAllUsesWith(e){if(this===e)return;let t=this._useHead!==null,r=this._useHead;for(;r;)r.user.operands[r.operandIndex]=e,r=r.next;if(this._useHead&&(e._useTail?(e._useTail.next=this._useHead,this._useHead.prev=e._useTail):e._useHead=this._useHead,e._useTail=this._useTail,e._useCount+=this._useCount),this._useHead=null,this._useTail=null,this._useCount=0,t){let s=this._owningFunction();s&&s.bumpVersion()}}_owningFunction(){return this.definingOp?this.definingOp.getParentFunction():null}isBlockArgument(){return!1}},lp=class extends Li{static{l(this,"BlockArgument")}constructor(e,t,r){super(e,null,0),this.ownerBlock=t,this.argIndex=r}_owningFunction(){return this.ownerBlock?this.ownerBlock._owningFunction():null}isBlockArgument(){return!0}}});var On,Ut,Ea=M(()=>{"use strict";Ih();On=class{static{l(this,"Block")}constructor(e=[]){this.parentRegion=null,this._head=null,this._tail=null,this._size=0,this.arguments=e.map((t,r)=>new lp(t,this,r))}get size(){return this._size}get isEmpty(){return this._head===null}get firstOp(){return this._head}get lastOp(){return this._tail}get parentOp(){return this.parentRegion?this.parentRegion.parentOp:null}_owningFunction(){let e=this;for(;e;){if(e._parentFunction)return e._parentFunction;let t=e.parentOp;e=t?t.parentBlock:null}return null}_notifyMutation(){let e=this._owningFunction();e&&e.bumpVersion()}getArgument(e){return this.arguments[e]}addArgument(e){let t=new lp(e,this,this.arguments.length);return this.arguments.push(t),this._notifyMutation(),t}removeArguments(e){let t=e instanceof Set?e:new Set(e);if(t.size===0)return this;for(let s=0;s<this.arguments.length;s++)if(t.has(s)&&this.arguments[s].hasUses)throw new Error(`removeArguments: block argument ${s} still has uses`);let r=[];for(let s=0;s<this.arguments.length;s++)t.has(s)||r.push(this.arguments[s]);this.arguments=r;for(let s=0;s<r.length;s++)r[s].argIndex=s;return this._notifyMutation(),this}pushOp(e){if(e.parentBlock!==null)throw new Error(`pushOp: operation '${e.opName}' is already attached to a block`);e.parentBlock=this,e._prev=this._tail,e._next=null,this._tail?this._tail._next=e:this._head=e,this._tail=e,this._size++,this._notifyMutation()}insertBefore(e,t){if(e.parentBlock!==null)throw new Error(`insertBefore: operation '${e.opName}' is already attached to a block`);if(!t||t.parentBlock!==this)throw new Error("insertBefore: reference operation is not in this block");e.parentBlock=this,e._prev=t._prev,e._next=t,t._prev?t._prev._next=e:this._head=e,t._prev=e,this._size++,this._notifyMutation()}insertAfter(e,t){if(e.parentBlock!==null)throw new Error(`insertAfter: operation '${e.opName}' is already attached to a block`);if(!t||t.parentBlock!==this)throw new Error("insertAfter: reference operation is not in this block");e.parentBlock=this,e._prev=t,e._next=t._next,t._next?t._next._prev=e:this._tail=e,t._next=e,this._size++,this._notifyMutation()}removeOp(e){e.parentBlock===this&&(e._prev?e._prev._next=e._next:this._head=e._next,e._next?e._next._prev=e._prev:this._tail=e._prev,e._prev=null,e._next=null,e.parentBlock=null,this._size--,this._notifyMutation())}*ops(){let e=this._head;for(;e;){let t=e._next;yield e,e=t}}*opsReverse(){let e=this._tail;for(;e;){let t=e._prev;yield e,e=t}}opsArray(){let e=[],t=this._head;for(;t;)e.push(t),t=t._next;return e}[Symbol.iterator](){return this.ops()}},Ut=class{static{l(this,"Region")}constructor(e=[]){this.parentOp=null,this.blocks=[];for(let t=0;t<e.length;t++)this.addBlock(e[t])}get entryBlock(){return this.blocks[0]||null}get isEmpty(){return this.blocks.length===0}addBlock(e){if(e.parentRegion!==null)throw new Error("addBlock: block already belongs to a region");return e.parentRegion=this,this.blocks.push(e),e}insertBlock(e,t){if(t.parentRegion!==null)throw new Error("insertBlock: block already belongs to a region");t.parentRegion=this,this.blocks.splice(e,0,t)}removeBlock(e){let t=this.blocks.indexOf(e);t!==-1&&(this.blocks.splice(t,1),e.parentRegion=null)}*[Symbol.iterator](){yield*this.blocks}}});var gn,Y,Z,Eh,Bn=M(()=>{"use strict";gn=Object.freeze({NONE:0,READ:1,WRITE:2,ALLOCATE:4,CONTROL:8}),Y=Object.freeze({COMMUTATIVE:"commutative",ASSOCIATIVE:"associative",IDEMPOTENT:"idempotent",ELEMENTWISE:"elementwise",SAME_OPERAND_AND_RESULT_TYPE:"same_type",SAME_OPERAND_AND_RESULT_SHAPE:"same_shape",TERMINATOR:"terminator",CONSTANT:"constant",BROADCAST:"broadcast",REDUCTION:"reduction",VIEW:"view",INJECTIVE:"injective",OUT_EWISE_FUSABLE:"out_ewise_fusable",OPAQUE:"opaque"}),Z=class{static{l(this,"OpDef")}constructor(e){this.name=e.name,this.numOperands=e.numOperands,this.numResults=e.numResults!==void 0?e.numResults:1,this.attrs=Object.freeze(e.attrs||[]),this.sideEffects=e.sideEffects||gn.NONE,this.traits=new Set(e.traits||[]),this.inferResultTypes=e.inferResultTypes||null,this.propagateSymbolicShapes=e.propagateSymbolicShapes||null,this.verify=e.verify||null,this.getMemoryEffects=e.getMemoryEffects||null,this.fold=e.fold||null,this.getCanonicalizationPatterns=e.getCanonicalizationPatterns||null,this.getFlops=e.getFlops||null,this.hasRegions=e.hasRegions||!1,this.numRegions=e.numRegions||0,this.regionSpecs=e.regions||null,this.genericAttrs=new Map(Object.entries(e.opAttrs||{}))}setAttr(e,t){return this.genericAttrs.set(e,t),this}getAttr(e){return this.genericAttrs.has(e)?this.genericAttrs.get(e):null}hasAttr(e){return this.genericAttrs.has(e)}hasTrait(e){return this.traits.has(e)}addTrait(e){return this.traits.add(e),this}get isCommutative(){return this.traits.has(Y.COMMUTATIVE)}get isAssociative(){return this.traits.has(Y.ASSOCIATIVE)}get isElementwise(){return this.traits.has(Y.ELEMENTWISE)}get isTerminator(){return this.traits.has(Y.TERMINATOR)}get isConstant(){return this.traits.has(Y.CONSTANT)}get isReduction(){return this.traits.has(Y.REDUCTION)}get isBroadcast(){return this.traits.has(Y.BROADCAST)}get isInjective(){return this.traits.has(Y.INJECTIVE)}get isOutEWiseFusable(){return this.traits.has(Y.OUT_EWISE_FUSABLE)}get isOpaque(){return this.traits.has(Y.OPAQUE)}get hasSideEffects(){return this.sideEffects!==gn.NONE}},Eh=class{static{l(this,"OpRegistry")}constructor(){this._defs=new Map}register(e){if(this._defs.has(e.name))throw new Error(`Op '${e.name}' already registered`);this._defs.set(e.name,e)}registerOpAttr(e,t,r){let s=this._defs.get(e);if(!s)throw new Error(`registerOpAttr: op '${e}' not registered`);return s.setAttr(t,r),s}registerTrait(e,t){let r=this._defs.get(e);if(!r)throw new Error(`registerTrait: op '${e}' not registered`);return r.addTrait(t),r}get(e){return this._defs.get(e)||null}has(e){return this._defs.has(e)}allOps(){return[...this._defs.values()]}names(){return[...this._defs.keys()]}}});var dt,Wu,cp=M(()=>{"use strict";dt=class{static{l(this,"Pattern")}constructor(e,t=1){this.name=e,this.benefit=t,this.rootOpName=null}match(e){return!1}rewrite(e,t){return!1}},Wu=class{static{l(this,"PatternSet")}constructor(){this.patterns=[],this._byOp=new Map,this._generic=[],this._sorted=!1}add(e){if(this.patterns.push(e),this._sorted=!1,e.rootOpName){let t=this._byOp.get(e.rootOpName);t||(t=[],this._byOp.set(e.rootOpName,t)),t.push(e)}else this._generic.push(e)}_ensureSorted(){if(this._sorted)return;let e=l((t,r)=>r.benefit-t.benefit,"cmp");for(let[,t]of this._byOp)t.sort(e);this._generic.sort(e),this._sorted=!0}get(){return[...this.patterns].sort((e,t)=>t.benefit-e.benefit)}getForOp(e){this._ensureSorted();let t=this._byOp.get(e);if(!t)return this._generic;if(this._generic.length===0)return t;let r=new Array(t.length+this._generic.length),s=0,o=0,i=0;for(;s<t.length&&o<this._generic.length;)t[s].benefit>=this._generic[o].benefit?r[i++]=t[s++]:r[i++]=this._generic[o++];for(;s<t.length;)r[i++]=t[s++];for(;o<this._generic.length;)r[i++]=this._generic[o++];return r}hasPatterns(){return this.patterns.length>0}}});function Fs(n){return(pp[n]||Ch).wasm}function Dh(n){return(pp[n]||Ch).load}function RN(n){return(pp[n]||Ch).store}function fp(n){return(pp[n]||Ch).bytes}function h0(n){return BN[n]||null}function zs(n,e){let t=BN[n];return t&&t[e]||null}function Ci(n){return ON[n]||dP}function Ph(n){return Ci(n).js}function Zr(n){return Ci(n).c}function m0(n){return Ci(n).cPtr}function g0(n){return Ci(n).suffix}function hP(n){return Ci(n).mathSuffix}function Rn(n){return Ci(n).bytes}function gr(n){return Ci(n).isFloat}function mr(n){return Ci(n).isInt}function MN(n,e){return n==="sum"||n==="mean"?0:n==="prod"?1:n==="max"?mr(e)?(Lh[e]||Lh.i32)[0]:-1/0:n==="min"?mr(e)?(Lh[e]||Lh.i32)[1]:1/0:0}function $h(n,e){let t=mP[n];return t?t+hP(e):n}function Ku(n){let e=gP[n];if(!e)throw new Error(`unsupported compare direction '${n}'`);return e}function IN(n){let e=yP[n];if(!e)throw new Error(`unsupported compare direction '${n}'`);return e}function EN(n){return _P.has(n)}function jn(n){return(y0[n]||LN).wgsl}function Vs(n){return(y0[n]||LN).bytes}function DN(n){return CN[n]||n}function PN(n){return n in CN}var ON,pp,Ch,BN,dP,Lh,mP,gP,yP,_P,y0,LN,CN,tr=M(()=>{"use strict";ON={f16:{js:"Uint16Array",c:"__half",cPtr:"__half*",bytes:2,suffix:"h",mathSuffix:"h",isFloat:!0,isInt:!1},bf16:{js:"Uint16Array",c:"__nv_bfloat16",cPtr:"__nv_bfloat16*",bytes:2,suffix:"",mathSuffix:"",isFloat:!0,isInt:!1},f32:{js:"Float32Array",c:"float",cPtr:"float*",bytes:4,suffix:"f",mathSuffix:"f",isFloat:!0,isInt:!1},f64:{js:"Float64Array",c:"double",cPtr:"double*",bytes:8,suffix:"",mathSuffix:"",isFloat:!0,isInt:!1},i8:{js:"Int8Array",c:"int8_t",cPtr:"int8_t*",bytes:1,suffix:"",mathSuffix:"",isFloat:!1,isInt:!0},i16:{js:"Int16Array",c:"int16_t",cPtr:"int16_t*",bytes:2,suffix:"",mathSuffix:"",isFloat:!1,isInt:!0},i32:{js:"Int32Array",c:"int",cPtr:"int*",bytes:4,suffix:"",mathSuffix:"",isFloat:!1,isInt:!0},i64:{js:"BigInt64Array",c:"int64_t",cPtr:"int64_t*",bytes:8,suffix:"LL",mathSuffix:"",isFloat:!1,isInt:!0},ui8:{js:"Uint8Array",c:"uint8_t",cPtr:"uint8_t*",bytes:1,suffix:"",mathSuffix:"",isFloat:!1,isInt:!0},bool:{js:"Uint8Array",c:"bool",cPtr:"bool*",bytes:1,suffix:"",mathSuffix:"",isFloat:!1,isInt:!1},index:{js:"Int32Array",c:"int",cPtr:"int*",bytes:4,suffix:"",mathSuffix:"",isFloat:!1,isInt:!0}},pp={f16:{wasm:"f32",load:"i32.load16_u",store:"i32.store16",bytes:2},bf16:{wasm:"f32",load:"i32.load16_u",store:"i32.store16",bytes:2},f32:{wasm:"f32",load:"f32.load",store:"f32.store",bytes:4},f64:{wasm:"f64",load:"f64.load",store:"f64.store",bytes:8},i8:{wasm:"i32",load:"i32.load8_s",store:"i32.store8",bytes:1},i16:{wasm:"i32",load:"i32.load16_s",store:"i32.store16",bytes:2},i32:{wasm:"i32",load:"i32.load",store:"i32.store",bytes:4},i64:{wasm:"i64",load:"i64.load",store:"i64.store",bytes:8},ui8:{wasm:"i32",load:"i32.load8_u",store:"i32.store8",bytes:1},bool:{wasm:"i32",load:"i32.load8_u",store:"i32.store8",bytes:1},index:{wasm:"i32",load:"i32.load",store:"i32.store",bytes:4}},Ch=pp.f32,BN={f32:{laneType:"f32x4",lanes:4,laneBytes:4,vecLoad:"v128.load",vecStore:"v128.store",splat:"f32x4.splat",extractLane:"f32x4.extract_lane",replaceLane:"f32x4.replace_lane",add:"f32x4.add",sub:"f32x4.sub",mul:"f32x4.mul",div:"f32x4.div",neg:"f32x4.neg",abs:"f32x4.abs",sqrt:"f32x4.sqrt",min:"f32x4.min",max:"f32x4.max",ceil:"f32x4.ceil",floor:"f32x4.floor",eq:"f32x4.eq",ne:"f32x4.ne",lt:"f32x4.lt",le:"f32x4.le",gt:"f32x4.gt",ge:"f32x4.ge",bitselect:"v128.bitselect"},i32:{laneType:"i32x4",lanes:4,laneBytes:4,vecLoad:"v128.load",vecStore:"v128.store",splat:"i32x4.splat",extractLane:"i32x4.extract_lane",replaceLane:"i32x4.replace_lane",add:"i32x4.add",sub:"i32x4.sub",mul:"i32x4.mul",div:null,neg:null,abs:"i32x4.abs",sqrt:null,min:"i32x4.min_s",max:"i32x4.max_s",ceil:null,floor:null,eq:"i32x4.eq",ne:"i32x4.ne",lt:"i32x4.lt_s",le:"i32x4.le_s",gt:"i32x4.gt_s",ge:"i32x4.ge_s",bitselect:"v128.bitselect"}};l(Fs,"wasmType");l(Dh,"wasmLoad");l(RN,"wasmStore");l(fp,"wasmBytes");l(h0,"wasmSimdEntry");l(zs,"wasmVecOp");dP=ON.f32;l(Ci,"dtypeInfo");l(Ph,"jsTypedArray");l(Zr,"cType");l(m0,"cPtrType");l(g0,"cLiteralSuffix");l(hP,"cMathFuncSuffix");l(Rn,"dtypeBytes");l(gr,"isDtypeFloat");l(mr,"isDtypeInt");Lh={i8:[-128,127],i16:[-32768,32767],i32:[-2147483648,2147483647],i64:[Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER],ui8:[0,255],index:[-2147483648,2147483647]};l(MN,"reduceInitValue");mP={exp:"exp",log:"log",sqrt:"sqrt",tanh:"tanh",abs:"fabs",sin:"sin",cos:"cos",ceil:"ceil",floor:"floor",max:"fmax",min:"fmin",pow:"pow",round:"round",fmod:"fmod",rsqrt:"rsqrt",erf:"erf",erfc:"erfc",lgamma:"lgamma",gamma:"tgamma",log2:"log2",log10:"log10",exp2:"exp2"};l($h,"cMathFunc");gP={eq:"==",ne:"!=",lt:"<",le:"<=",gt:">",ge:">="},yP={eq:"===",ne:"!==",lt:"<",le:"<=",gt:">",ge:">="};l(Ku,"cCompareOp");l(IN,"jsCompareOp");_P=new Set(["exp","log","sqrt","tanh","abs","ceil","floor","sin","cos","max","min","pow","round","sign","log2","log10"]);l(EN,"isJSMathFunc");y0={f16:{wgsl:"f16",bytes:2},bf16:{wgsl:"f32",bytes:4},f32:{wgsl:"f32",bytes:4},f64:{wgsl:"f32",bytes:4},i8:{wgsl:"i32",bytes:4},i16:{wgsl:"i32",bytes:4},i32:{wgsl:"i32",bytes:4},i64:{wgsl:"i32",bytes:4},ui8:{wgsl:"u32",bytes:4},bool:{wgsl:"u32",bytes:4},index:{wgsl:"u32",bytes:4}},LN=y0.f32;l(jn,"wgslType");l(Vs,"wgslBytes");CN={exp:"exp",log:"log",sqrt:"sqrt",tanh:"tanh",abs:"abs",sin:"sin",cos:"cos",ceil:"ceil",floor:"floor",max:"max",min:"min",pow:"pow",round:"round",sign:"sign",rsqrt:"inverseSqrt",fabs:"abs",log2:"log2",exp2:"exp2"};l(DN,"wgslMathFunc");l(PN,"hasWgslMathFunc")});function Yu(n,e){let t={};return n.match(e,t)?t:null}var Fh,_0,b0,Hu,Gs,$N=M(()=>{"use strict";Fh=class{static{l(this,"DFPattern")}match(e,t){return!1}},_0=class extends Fh{static{l(this,"AnyPattern")}match(){return!0}},b0=class extends Fh{static{l(this,"OpPattern")}constructor(e,t){super(),this.name=e,this.operandPatterns=t}match(e,t){if(!e||e.opName!==this.name)return!1;if(this.operandPatterns.length===0)return!0;if(e.numOperands<this.operandPatterns.length)return!1;for(let r=0;r<this.operandPatterns.length;r++){let s=e.getOperand(r).definingOp;if(!this.operandPatterns[r].match(s,t))return!1}return!0}},Hu=l(()=>new _0,"wildcard"),Gs=l((n,...e)=>new b0(n,e),"isOp");l(Yu,"matchPattern")});function ho(n,e){return n&&n.opName==="constant"&&n.getAttr("value")===e}function T0(n){return new w0(n)}var bP,wP,TP,xP,SP,zh,Ju,Vh,Gh,jh,Uh,w0,Zu,Qu,Xu,el,tl,nl,rl,qh,Wh,Kh,Hh,Yh,Jh,Zh,Qh,Xh,em,sl=M(()=>{"use strict";cp();Re();tr();$N();bP=Gs("transpose",Gs("transpose",Hu())),wP=Gs("reshape",Gs("reshape",Hu())),TP=Gs("neg",Gs("neg",Hu())),xP=Gs("exp",Gs("log",Hu())),SP=Gs("log",Gs("exp",Hu()));l(ho,"isConstantVal");zh=class extends dt{static{l(this,"FoldTrivialReshape")}constructor(){super("fold_trivial_reshape",10),this.rootOpName="reshape"}match(e){let t=e.getOperand(0).type,r=e.getResult(0).type;return t instanceof I&&r instanceof I&&t.shapeEquals(r)}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0)]),e.erase(),!0}},Ju=class extends dt{static{l(this,"ReshapeReshape")}constructor(){super("reshape_reshape",10),this.rootOpName="reshape"}match(e){return Yu(wP,e)!==null}rewrite(e,t){let r=e.getOperand(0).definingOp.getOperand(0),s=e.getAttr("new_shape"),o=t.reshape(r,s);return e.replaceAllResultsWith([o.getResult(0)]),e.erase(),!0}},Vh=class extends dt{static{l(this,"FoldTrivialTranspose")}constructor(){super("fold_trivial_transpose",10),this.rootOpName="transpose"}match(e){let t=e.getAttr("permutation");if(!t)return!1;for(let r=0;r<t.length;r++)if(t[r]!==r)return!1;return!0}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0)]),e.erase(),!0}},Gh=class extends dt{static{l(this,"TransposeTranspose")}constructor(){super("transpose_transpose",10),this.rootOpName="transpose"}match(e){return Yu(bP,e)!==null}rewrite(e,t){let r=e.getOperand(0).definingOp.getOperand(0),s=e.getOperand(0).definingOp.getAttr("permutation"),o=e.getAttr("permutation"),i=new Array(o.length);for(let u=0;u<o.length;u++)i[u]=s[o[u]];let a=t.transpose(r,i);return e.replaceAllResultsWith([a.getResult(0)]),e.erase(),!0}},jh=class extends dt{static{l(this,"FoldTrivialPad")}constructor(){super("fold_trivial_pad",10),this.rootOpName="pad"}match(e){let t=e.getAttr("low"),r=e.getAttr("high"),s=e.getAttr("interior")||[];return!(t&&t.some(o=>o!==0)||r&&r.some(o=>o!==0)||s&&s.some(o=>o!==0))}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0)]),e.erase(),!0}},Uh=class extends dt{static{l(this,"FoldTrivialSlice")}constructor(){super("fold_trivial_slice",10),this.rootOpName="slice"}match(e){let t=e.getOperand(0).type,r=e.getResult(0).type;if(!(t instanceof I)||!(r instanceof I))return!1;let s=e.getAttr("starts"),o=e.getAttr("strides")||s.map(()=>1);return s.some(i=>i!==0)||o.some(i=>i!==1)?!1:t.shapeEquals(r)}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0)]),e.erase(),!0}},w0=class extends dt{static{l(this,"CommutativeConstantRight")}constructor(e=null){super(`commutative_constant_right${e?"_"+e:""}`,5),this.rootOpName=e}match(e){if(e.numOperands!==2)return!1;let t=e.getOperand(0).definingOp,r=e.getOperand(1).definingOp;return t&&t.opName==="constant"&&!(r&&r.opName==="constant")}rewrite(e,t){let r=e.getOperand(0),s=e.getOperand(1);return e.replaceOperand(0,s),e.replaceOperand(1,r),!0}};l(T0,"commutativeConstantRightFor");Zu=class extends dt{static{l(this,"AddZero")}constructor(){super("add_zero",5),this.rootOpName="add"}match(e){return ho(e.getOperand(1).definingOp,0)||ho(e.getOperand(0).definingOp,0)}rewrite(e,t){let r=ho(e.getOperand(1).definingOp,0)?e.getOperand(0):e.getOperand(1);return r.type.equals(e.getResult(0).type)?(e.replaceAllResultsWith([r]),e.erase(),!0):!1}},Qu=class extends dt{static{l(this,"SubZero")}constructor(){super("sub_zero",5),this.rootOpName="sub"}match(e){return ho(e.getOperand(1).definingOp,0)}rewrite(e,t){let r=e.getOperand(0);return r.type.equals(e.getResult(0).type)?(e.replaceAllResultsWith([r]),e.erase(),!0):!1}},Xu=class extends dt{static{l(this,"SubSelf")}constructor(e=!1){super("sub_self",5),this.rootOpName="sub",this.fastMath=e}match(e){return e.getOperand(0)!==e.getOperand(1)?!1:mr(e.getResult(0).type.dtype)||this.fastMath}rewrite(e,t){let s=t.scalarConstant(0,e.getResult(0).type.dtype).getResult(0),o=e.getResult(0).type.shape;return o.length>0&&(s=t.broadcast(s,o,[]).getResult(0)),e.replaceAllResultsWith([s]),e.erase(),!0}},el=class extends dt{static{l(this,"MulOne")}constructor(){super("mul_one",5),this.rootOpName="mul"}match(e){return ho(e.getOperand(1).definingOp,1)||ho(e.getOperand(0).definingOp,1)}rewrite(e,t){let r=ho(e.getOperand(1).definingOp,1)?e.getOperand(0):e.getOperand(1);return r.type.equals(e.getResult(0).type)?(e.replaceAllResultsWith([r]),e.erase(),!0):!1}},tl=class extends dt{static{l(this,"MulZero")}constructor(e=!1){super("mul_zero",5),this.rootOpName="mul",this.fastMath=e}match(e){return!mr(e.getResult(0).type.dtype)&&!this.fastMath?!1:ho(e.getOperand(1).definingOp,0)||ho(e.getOperand(0).definingOp,0)}rewrite(e,t){let s=t.scalarConstant(0,e.getResult(0).type.dtype).getResult(0),o=e.getResult(0).type.shape;return o.length>0&&(s=t.broadcast(s,o,[]).getResult(0)),e.replaceAllResultsWith([s]),e.erase(),!0}},nl=class extends dt{static{l(this,"DivOne")}constructor(){super("div_one",5),this.rootOpName="div"}match(e){return ho(e.getOperand(1).definingOp,1)}rewrite(e,t){let r=e.getOperand(0);return r.type.equals(e.getResult(0).type)?(e.replaceAllResultsWith([r]),e.erase(),!0):!1}},rl=class extends dt{static{l(this,"DoubleNeg")}constructor(){super("double_neg",5),this.rootOpName="neg"}match(e){return Yu(TP,e)!==null}rewrite(e,t){let r=e.getOperand(0).definingOp.getOperand(0);return e.replaceAllResultsWith([r]),e.erase(),!0}},qh=class extends dt{static{l(this,"ExpLog")}constructor(e=!1){super("exp_log",5),this.rootOpName="exp",this.fastMath=e}match(e){return this.fastMath&&Yu(xP,e)!==null}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0).definingOp.getOperand(0)]),e.erase(),!0}},Wh=class extends dt{static{l(this,"LogExp")}constructor(e=!1){super("log_exp",5),this.rootOpName="log",this.fastMath=e}match(e){return this.fastMath&&Yu(SP,e)!==null}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0).definingOp.getOperand(0)]),e.erase(),!0}},Kh=class extends dt{static{l(this,"DivSelf")}constructor(e=!1){super("div_self",5),this.rootOpName="div",this.fastMath=e}match(e){return this.fastMath?e.getOperand(0)===e.getOperand(1):!1}rewrite(e,t){let s=t.scalarConstant(1,e.getResult(0).type.dtype).getResult(0),o=e.getResult(0).type.shape;return o.length>0&&(s=t.broadcast(s,o,[]).getResult(0)),e.replaceAllResultsWith([s]),e.erase(),!0}},Hh=class extends dt{static{l(this,"MulNegNeg")}constructor(){super("mul_neg_neg",4),this.rootOpName="mul"}match(e){let t=e.getOperand(0).definingOp,r=e.getOperand(1).definingOp;return t&&t.opName==="neg"&&r&&r.opName==="neg"}rewrite(e,t){let r=e.getOperand(0).definingOp.getOperand(0),s=e.getOperand(1).definingOp.getOperand(0),o=t.mul(r,s);return e.replaceAllResultsWith([o.getResult(0)]),e.erase(),!0}},Yh=class extends dt{static{l(this,"AddNegToSub")}constructor(){super("add_neg_to_sub",4),this.rootOpName="add"}match(e){let t=e.getOperand(1).definingOp;return t&&t.opName==="neg"}rewrite(e,t){let r=e.getOperand(0),s=e.getOperand(1).definingOp.getOperand(0),o=t.sub(r,s);return e.replaceAllResultsWith([o.getResult(0)]),e.erase(),!0}},Jh=class extends dt{static{l(this,"SubNegToAdd")}constructor(){super("sub_neg_to_add",4),this.rootOpName="sub"}match(e){let t=e.getOperand(1).definingOp;return t&&t.opName==="neg"}rewrite(e,t){let r=e.getOperand(0),s=e.getOperand(1).definingOp.getOperand(0),o=t.add(r,s);return e.replaceAllResultsWith([o.getResult(0)]),e.erase(),!0}},Zh=class extends dt{static{l(this,"DoubleConvert")}constructor(){super("double_convert",6),this.rootOpName="convert"}match(e){let t=e.getOperand(0).definingOp;if(!t||t.opName!=="convert")return!1;let r=t.getOperand(0).type.dtype,s=e.getAttr("target_dtype");return r===s}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0).definingOp.getOperand(0)]),e.erase(),!0}},Qh=class extends dt{static{l(this,"LayoutTransformIdentity")}constructor(){super("layout_transform_identity",10),this.rootOpName="layout_transform"}match(e){let t=e.getAttr("src_layout"),r=e.getAttr("dst_layout");if(!t||!r||t.length!==r.length)return!1;for(let s=0;s<t.length;s++)if(t[s]!==r[s])return!1;return!0}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0)]),e.erase(),!0}},Xh=class extends dt{static{l(this,"FoldTransposeIntoDot")}constructor(){super("fold_transpose_into_dot",10),this.rootOpName="dot"}match(e){for(let t=0;t<2;t++){let r=e.getOperand(t).definingOp;if(!r||r.opName!=="transpose")continue;let s=r.getAttr("permutation");if(!(!s||s.length!==2)&&!(s[0]!==1||s[1]!==0))return!0}return!1}rewrite(e,t){let r=[e.getOperand(0),e.getOperand(1)],s=[...e.getAttr("lhs_contracting")],o=[...e.getAttr("rhs_contracting")],i=[...e.getAttr("lhs_batch")||[]],a=[...e.getAttr("rhs_batch")||[]];for(let p=0;p<2;p++){let c=r[p].definingOp;if(!c||c.opName!=="transpose")continue;let f=c.getAttr("permutation");if(!f||f.length!==2||f[0]!==1||f[1]!==0)continue;r[p]=c.getOperand(0);let d=p===0?s:o,h=p===0?i:a;for(let m=0;m<d.length;m++)d[m]=d[m]===0?1:0;for(let m=0;m<h.length;m++)h[m]=h[m]===0?1:0}let u=t.dot(r[0],r[1],s,o);return i.length>0&&u.setAttr("lhs_batch",i),a.length>0&&u.setAttr("rhs_batch",a),e.replaceAllResultsWith([u.getResult(0)]),e.erase(),!0}},em=class extends dt{static{l(this,"LayoutTransformCompose")}constructor(){super("layout_transform_compose",10),this.rootOpName="layout_transform"}match(e){let t=e.getOperand(0).definingOp;return t&&t.opName==="layout_transform"}rewrite(e,t){let r=e.getOperand(0).definingOp,s=r.getAttr("src_layout"),o=r.getAttr("dst_layout"),i=e.getAttr("dst_layout"),a=new Array(s.length);for(let c=0;c<i.length;c++)a[c]=s[o.indexOf(i[c])];let u=r.getOperand(0),p=t._inferAndBuild("layout_transform",[u],{src_layout:s,dst_layout:a});return e.replaceAllResultsWith([p.getResult(0)]),e.erase(),!0}}});function La(n){if(n.length!==2)return null;let e=n[0],t=n[1];if(!(e instanceof I)||!(t instanceof I)||e.dtype!==t.dtype)return null;let r=I.broadcastShape(e.shape,t.shape);return r?[new I(r,e.dtype)]:null}function tm(n){if(n.length!==1)return null;let e=n[0];return e instanceof I?[new I(e.shape,e.dtype)]:null}function FN(n){if(n.length!==2)return null;let e=n[0],t=n[1];if(!(e instanceof I)||!(t instanceof I))return null;let r=I.broadcastShape(e.shape,t.shape);return r?[new I(r,"bool")]:null}function x0(n){if(n.length!==1)return null;let e=n[0];return!(e instanceof I)||!jt(e.dtype)?null:[new I(e.shape,e.dtype)]}function Ca(n){let e=[];if(n.numOperands!==2)return e.push(`${n.opName} expects 2 operands, got ${n.numOperands}`),e;let t=n.getOperand(0).type,r=n.getOperand(1).type;return t instanceof I||e.push(`${n.opName} operand 0 is not tensor`),r instanceof I||e.push(`${n.opName} operand 1 is not tensor`),t instanceof I&&r instanceof I&&t.dtype!==r.dtype&&e.push(`${n.opName} dtype mismatch: ${t.dtype} vs ${r.dtype}`),e}function hp(n){let e=[];return n.numOperands!==1?(e.push(`${n.opName} expects 1 operand, got ${n.numOperands}`),e):(n.getOperand(0).type instanceof I||e.push(`${n.opName} operand is not tensor`),e)}function S0(n){let e=hp(n);if(e.length===0){let t=n.getOperand(0).type.dtype;jt(t)||e.push(`${n.opName} requires float type, got ${t}`)}return e}var ol,v0,nm=M(()=>{"use strict";Re();Bn();l(La,"inferBinaryElementwise");l(tm,"inferUnaryElementwise");l(FN,"inferCompare");l(x0,"inferUnaryFloat");l(Ca,"verifyBinaryElementwise");l(hp,"verifyUnaryElementwise");l(S0,"verifyUnaryFloat");ol=[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],v0=[...ol,Y.COMMUTATIVE,Y.ASSOCIATIVE]});function rm(n){return e=>{if(!(typeof e[0]!="number"||typeof e[1]!="number"))return n(e[0],e[1])}}function vP(n){return e=>typeof e[0]=="number"?n(e[0]):void 0}function zN(n){n.register(new Z({name:"add",numOperands:2,numResults:1,traits:v0,inferResultTypes:La,verify:Ca,getCanonicalizationPatterns(){return[T0("add"),new Zu]},fold:rm((e,t)=>e+t)})),n.register(new Z({name:"mul",numOperands:2,numResults:1,traits:v0,inferResultTypes:La,verify:Ca,getCanonicalizationPatterns(){return[T0("mul"),new el,new tl]},fold:rm((e,t)=>e*t)})),n.register(new Z({name:"sub",numOperands:2,numResults:1,traits:ol,inferResultTypes:La,verify:Ca,getCanonicalizationPatterns(){return[new Qu,new Xu]},fold:rm((e,t)=>e-t)})),n.register(new Z({name:"div",numOperands:2,numResults:1,traits:ol,inferResultTypes:La,verify:Ca,getCanonicalizationPatterns(){return[new nl]},fold:rm((e,t)=>e/t)}));for(let e of["rem","pow"])n.register(new Z({name:e,numOperands:2,numResults:1,traits:ol,inferResultTypes:La,verify:Ca}));n.register(new Z({name:"neg",numOperands:1,numResults:1,traits:[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:tm,verify:hp,getCanonicalizationPatterns(){return[new rl]},fold:vP(e=>-e)}));for(let e of["maximum","minimum"])n.register(new Z({name:e,numOperands:2,numResults:1,traits:[...ol,Y.COMMUTATIVE],inferResultTypes:La,verify:Ca}))}var VN=M(()=>{"use strict";Bn();sl();nm();l(rm,"scalarBinaryFold");l(vP,"scalarUnaryFold");l(zN,"register")});function GN(n){for(let e of["abs","floor","ceil","round","sign","square","reciprocal"])n.register(new Z({name:e,numOperands:1,numResults:1,traits:[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:tm,verify:hp}));n.register(new Z({name:"exp",numOperands:1,numResults:1,traits:[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:x0,fold(e){return typeof e[0]=="number"?Math.exp(e[0]):void 0},verify:S0}));for(let e of["log","sqrt","rsqrt","tanh","sin","cos","erf","erfc","lgamma","gamma","log2","log10","exp2"])n.register(new Z({name:e,numOperands:1,numResults:1,traits:[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:x0,verify:S0}))}var jN=M(()=>{"use strict";Bn();nm();l(GN,"register")});function kP(n){if(n.length!==1)return null;let e=n[0];return!(e instanceof I)||e.dtype!=="bool"?null:[new I(e.shape,"bool")]}function NP(n){if(n.length!==2)return null;let[e,t]=n;if(!(e instanceof I)||e.dtype!=="bool"||!(t instanceof I)||t.dtype!=="bool")return null;let r=I.broadcastShape(e.shape,t.shape);return r?[new I(r,"bool")]:null}function UN(n){n.register(new Z({name:"compare",numOperands:2,numResults:1,attrs:[{name:"direction",type:"string",required:!0}],traits:[Y.ELEMENTWISE],inferResultTypes:FN,verify(e){let t=[];if(e.numOperands!==2)return t.push("compare expects 2 operands"),t;if(!e.hasAttr("direction"))t.push("compare missing direction attr");else{let r=e.getAttr("direction");AP.has(r)||t.push(`compare invalid direction: ${r}`)}return t}})),n.register(new Z({name:"select",numOperands:3,numResults:1,traits:[Y.ELEMENTWISE],inferResultTypes(e){if(e.length!==3)return null;let[t,r,s]=e;if(!(t instanceof I)||t.dtype!=="bool"||!(r instanceof I)||!(s instanceof I)||r.dtype!==s.dtype)return null;let o=I.broadcastShape(t.shape,r.shape,s.shape);return o?[new I(o,r.dtype)]:null}})),n.register(new Z({name:"where",numOperands:3,numResults:1,traits:[Y.ELEMENTWISE],inferResultTypes(e){if(e.length!==3)return null;let[t,r,s]=e;if(!(t instanceof I)||!(r instanceof I)||!(s instanceof I)||r.dtype!==s.dtype)return null;let o=I.broadcastShape(t.shape,r.shape,s.shape);return o?[new I(o,r.dtype)]:null}})),n.register(new Z({name:"logical_not",numOperands:1,numResults:1,traits:[Y.ELEMENTWISE],inferResultTypes:kP}));for(let e of["logical_and","logical_or"])n.register(new Z({name:e,numOperands:2,numResults:1,traits:[Y.ELEMENTWISE],inferResultTypes:NP}));n.register(new Z({name:"clamp",numOperands:3,numResults:1,traits:[Y.ELEMENTWISE],inferResultTypes(e){if(e.length!==3)return null;let t=e[1];return t instanceof I?[new I(t.shape,t.dtype)]:null}}))}var AP,qN=M(()=>{"use strict";Bn();Re();nm();l(kP,"inferUnaryBool");l(NP,"inferBinaryBool");AP=new Set(["eq","ne","lt","le","gt","ge"]);l(UN,"register")});function WN(n){n.register(new Z({name:"stop_gradient",numOperands:1,numResults:1,traits:[Y.VIEW],inferResultTypes(e){return e.length!==1?null:[e[0]]},propagateSymbolicShapes(e,t){let r=t.get(e.getOperand(0));return r?[r]:null}})),n.register(new Z({name:"reverse",numOperands:1,numResults:1,traits:[Y.INJECTIVE],attrs:[{name:"dimensions",type:"array",required:!0}],inferResultTypes(e){return e.length!==1?null:[e[0]]},propagateSymbolicShapes(e,t){let r=t.get(e.getOperand(0));return r?[r]:null}})),n.register(new Z({name:"broadcast_in_dim",numOperands:1,numResults:1,traits:[Y.BROADCAST],attrs:[{name:"broadcast_dimensions",type:"array",required:!0},{name:"result_shape",type:"array",required:!0}],inferResultTypes(e,t){if(e.length!==1)return null;let r=e[0];if(!(r instanceof I))return null;let s=t.get?t.get("result_shape"):t.result_shape;return s?[new I(s,r.dtype)]:null},propagateSymbolicShapes(e,t){let r=t.get(e.getOperand(0));if(!r)return null;let s=e.getAttr("broadcast_dimensions"),o=e.getAttr("result_shape");if(!s||!o)return null;let i=o.map(a=>a===X?null:a);for(let a=0;a<s.length;a++)(typeof r[a]!="number"||i[s[a]]===null)&&(i[s[a]]=r[a]);return[i]},fold(e){if(e.length===1&&typeof e[0]=="number")return e[0]},verify(e){let t=[];if(e.hasAttr("broadcast_dimensions")||t.push("broadcast_in_dim missing broadcast_dimensions"),e.hasAttr("result_shape")||t.push("broadcast_in_dim missing result_shape"),e.numOperands!==1&&t.push("broadcast_in_dim expects 1 operand"),t.length===0){let r=e.getAttr("broadcast_dimensions"),s=e.getAttr("result_shape"),o=e.getOperand(0).type;r.length!==o.rank&&t.push(`broadcast_dimensions length ${r.length} != input rank ${o.rank}`);for(let a=0;a<r.length;a++)r[a]<0||r[a]>=s.length?t.push(`broadcast_dimensions[${a}]=${r[a]} out of range for result rank ${s.length}`):o instanceof I&&o.shape[a]!==X&&o.shape[a]!==1&&s[r[a]]!==X&&o.shape[a]!==s[r[a]]&&t.push(`broadcast_in_dim: input dim ${a} size ${o.shape[a]} incompatible with result dim ${r[a]} size ${s[r[a]]}`);let i=new Set;for(let a of r)i.has(a)&&t.push(`broadcast_dimensions has duplicate: ${a}`),i.add(a)}return t}})),n.register(new Z({name:"reshape",numOperands:1,numResults:1,traits:[Y.VIEW],attrs:[{name:"new_shape",type:"array",required:!0}],inferResultTypes(e,t){if(e.length!==1)return null;let r=e[0];if(!(r instanceof I))return null;let s=t.get?t.get("new_shape"):t.new_shape;return s?[new I(s,r.dtype)]:null},verify(e){let t=[];if(e.hasAttr("new_shape")||t.push("reshape missing new_shape"),e.numOperands!==1&&t.push("reshape expects 1 operand"),t.length===0){let r=e.getOperand(0).type,s=e.getAttr("new_shape");if(r instanceof I&&r.isFullyStatic){let o=s.filter(i=>i===X).length;if(o>1&&t.push("reshape can have at most one dynamic dimension"),o===0){let i=r.numel(),a=s.reduce((u,p)=>u*p,1);i!==a&&t.push(`reshape numel mismatch: input ${i} vs output ${a}`)}}}return t},propagateSymbolicShapes(e,t){let r=t.get(e.getOperand(0));if(!r)return null;let s=e.getAttr("new_shape"),o=[];for(let i of s)if(i===-1){let a=r.find(u=>typeof u!="number");o.push(a||-1)}else o.push(i);return[o]},getCanonicalizationPatterns(){return[new zh,new Ju]},fold(e){return e[0]}})),n.register(new Z({name:"transpose",numOperands:1,numResults:1,traits:[Y.VIEW],attrs:[{name:"permutation",type:"array",required:!0}],inferResultTypes(e,t){if(e.length!==1)return null;let r=e[0];if(!(r instanceof I))return null;let s=t.get?t.get("permutation"):t.permutation;if(!s)return null;let o=s.map(i=>r.shape[i]);return[new I(o,r.dtype)]},propagateSymbolicShapes(e,t){let r=t.get(e.getOperand(0));if(!r)return null;let s=e.getAttr("permutation");return s?[s.map(o=>r[o])]:null},getCanonicalizationPatterns(){return[new Vh]},fold(e,t,r){let s=e[0];if(s==null||typeof s=="number"||typeof s.length!="number")return;let o=t.get?t.get("permutation"):t.permutation;if(!o)return;let i=r&&r[0]?r[0].getResult(0).type:null,a=i instanceof I?i.shape:null;if(!a||a.length!==o.length)return;let u=new Array(a.length),p=1;for(let m=a.length-1;m>=0;m--)u[m]=p,p*=a[m];let c=o.map(m=>a[m]),f=s.length,d=new Array(f),h=new Array(o.length).fill(0);for(let m=0;m<f;m++){let g=0;for(let y=0;y<o.length;y++)g+=h[y]*u[o[y]];d[m]=s[g];for(let y=o.length-1;y>=0&&!(++h[y]<c[y]);y--)h[y]=0}return d},verify(e){let t=[];if(!e.hasAttr("permutation"))return t.push("transpose missing permutation"),t;if(e.numOperands!==1)return t.push("transpose expects 1 operand"),t;let r=e.getAttr("permutation"),s=e.getOperand(0).type;s instanceof I&&r.length!==s.rank&&t.push(`transpose permutation length ${r.length} != input rank ${s.rank}`);let o=new Set;for(let i of r)o.has(i)&&t.push(`transpose duplicate in permutation: ${i}`),o.add(i);return t}})),n.register(new Z({name:"slice",numOperands:1,numResults:1,traits:[Y.VIEW],attrs:[{name:"starts",type:"array",required:!0},{name:"limits",type:"array",required:!0},{name:"strides",type:"array",required:!1}],inferResultTypes(e,t){if(e.length!==1)return null;let r=e[0];if(!(r instanceof I))return null;let s=t.get?t.get("starts"):t.starts,o=t.get?t.get("limits"):t.limits,i=(t.get?t.get("strides"):t.strides)||s.map(()=>1),a=[];for(let u=0;u<s.length;u++)a.push(Math.ceil((o[u]-s[u])/i[u]));return[new I(a,r.dtype)]},getCanonicalizationPatterns(){return[new Uh]}})),n.register(new Z({name:"concat",numOperands:-1,numResults:1,attrs:[{name:"dimension",type:"number",required:!0}],inferResultTypes(e,t){if(e.length<1)return null;let r=e[0];if(!(r instanceof I))return null;let s=t.get?t.get("dimension"):t.dimension;if(s===void 0)return null;let o=[...r.shape];for(let i=1;i<e.length;i++){let a=e[i];if(!(a instanceof I)||a.dtype!==r.dtype||a.rank!==r.rank)return null;o[s]===X||a.shape[s]===X?o[s]=X:o[s]+=a.shape[s]}return[new I(o,r.dtype)]}})),n.register(new Z({name:"pad",numOperands:2,numResults:1,traits:[Y.INJECTIVE],attrs:[{name:"low",type:"array",required:!0},{name:"high",type:"array",required:!0},{name:"interior",type:"array",required:!1}],inferResultTypes(e,t){if(e.length<1)return null;let r=e[0];if(!(r instanceof I))return null;let s=t.get?t.get("low"):t.low,o=t.get?t.get("high"):t.high,i=(t.get?t.get("interior"):t.interior)||s.map(()=>0),a=[];for(let u=0;u<r.rank;u++)r.shape[u]===X?a.push(X):a.push(s[u]+r.shape[u]+(r.shape[u]-1)*i[u]+o[u]);return[new I(a,r.dtype)]},getCanonicalizationPatterns(){return[new jh]}})),n.register(new Z({name:"gather",numOperands:2,numResults:1,traits:[Y.INJECTIVE],attrs:[{name:"offset_dims",type:"array",required:!0},{name:"collapsed_slice_dims",type:"array",required:!0},{name:"start_index_map",type:"array",required:!0},{name:"slice_sizes",type:"array",required:!0},{name:"index_vector_dim",type:"number",required:!0}],inferResultTypes(e,t){if(e.length!==2)return null;let r=e[0],s=e[1];if(!(r instanceof I)||!(s instanceof I))return null;let o=t.get?t.get("offset_dims"):t.offset_dims,i=new Set(t.get?t.get("collapsed_slice_dims"):t.collapsed_slice_dims),a=t.get?t.get("slice_sizes"):t.slice_sizes,u=t.get?t.get("index_vector_dim"):t.index_vector_dim,p=[];for(let y=0;y<s.rank;y++)y!==u&&p.push(s.shape[y]);let c=[];for(let y=0;y<a.length;y++)i.has(y)||c.push(a[y]);let f=new Set(o),d=[],h=0,m=0,g=p.length+c.length;for(let y=0;y<g;y++)f.has(y)?d.push(c[m++]):d.push(p[h++]);return[new I(d,r.dtype)]}})),n.register(new Z({name:"scatter",numOperands:3,numResults:1,traits:[Y.INJECTIVE],attrs:[{name:"update_window_dims",type:"array",required:!0},{name:"inserted_window_dims",type:"array",required:!0},{name:"scatter_dims_to_operand_dims",type:"array",required:!0},{name:"index_vector_dim",type:"number",required:!0}],hasRegions:!0,numRegions:1,sideEffects:2,inferResultTypes(e){if(e.length<1)return null;let t=e[0];return t instanceof I?[new I(t.shape,t.dtype)]:null}})),n.register(new Z({name:"split",numOperands:1,numResults:-1,attrs:[{name:"dimension",type:"number",required:!0},{name:"split_sizes",type:"array",required:!0}],inferResultTypes(e,t){if(e.length<1)return null;let r=e[0];if(!(r instanceof I))return null;let s=t.get?t.get("dimension"):t.dimension,o=t.get?t.get("split_sizes"):t.split_sizes;return s===void 0||!o?null:o.map(i=>{let a=[...r.shape];return a[s]=i,new I(a,r.dtype)})}}))}var KN=M(()=>{"use strict";Bn();Re();sl();l(WN,"register")});function HN(n){n.register(new Z({name:"reduce",numOperands:2,numResults:1,attrs:[{name:"dimensions",type:"array",required:!0},{name:"reduce_type",type:"string",required:!0}],traits:[Y.REDUCTION],hasRegions:!0,numRegions:1,inferResultTypes(t,r){if(t.length<1)return null;let s=t[0];if(!(s instanceof I))return null;let o=r.get?r.get("dimensions"):r.dimensions;if(!o)return null;let i=new Set(o),a=[];for(let u=0;u<s.rank;u++)i.has(u)||a.push(s.shape[u]);return[new I(a,s.dtype)]},propagateSymbolicShapes(t,r){let s=r.get(t.getOperand(0));if(!s)return null;let o=t.getAttr("dimensions");if(!o)return null;let i=new Set(o),a=[];for(let u=0;u<s.length;u++)i.has(u)||a.push(s[u]);return[a]},verify(t){let r=[];if(t.hasAttr("dimensions")||r.push("reduce missing dimensions"),!t.hasAttr("reduce_type"))r.push("reduce missing reduce_type");else{let s=t.getAttr("reduce_type");OP.has(s)||r.push(`reduce invalid reduce_type: ${s}`)}return r}}));function e(t,r){if(t.length<1)return null;let s=t[0];if(!(s instanceof I))return null;let o=r.get?r.get("axis"):r.axis;if(o===void 0)return null;let i=(r.get?r.get("keep_dims"):r.keep_dims)||!1,a=[];for(let u=0;u<s.rank;u++)u===o?i&&a.push(1):a.push(s.shape[u]);return[new I(a,"i32")]}l(e,"inferArgReduceTypes"),n.register(new Z({name:"argmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"keep_dims",type:"boolean",required:!1}],traits:[Y.REDUCTION],inferResultTypes:e})),n.register(new Z({name:"argmin",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"keep_dims",type:"boolean",required:!1}],traits:[Y.REDUCTION],inferResultTypes:e}))}var OP,YN=M(()=>{"use strict";Bn();Re();OP=new Set(["sum","max","min","prod","mean","and","or"]);l(HN,"register")});function BP(n,e){let t=n.getAttr("scale"),r=e.getAttr("scale"),s=n.getAttr("zero_point"),o=e.getAttr("zero_point"),i=n.getAttr("scheme"),a=e.getAttr("scheme");if(i!==a)return!1;if(typeof t=="number"&&typeof r=="number"){if(t!==r)return!1}else return!1;if(typeof s=="number"&&typeof o=="number"){if(s!==o)return!1}else return!1;return!0}var sm,om,im,k0=M(()=>{"use strict";cp();Re();l(BP,"quantParamsMatch");sm=class extends dt{static{l(this,"QuantizeDequantizeIdentity")}constructor(){super("quantize_dequantize_identity",20),this.rootOpName="quantize"}match(e){let t=e.getOperand(0).definingOp;return!t||t.opName!=="dequantize"?!1:BP(e,t)}rewrite(e,t){let r=e.getOperand(0).definingOp.getOperand(0);if(!r.type||!e.getResult(0).type)return!1;let s=r.type,o=e.getResult(0).type;return s instanceof I&&o instanceof I&&s.dtype===o.dtype?(e.replaceAllResultsWith([r]),e.erase(),!0):!1}},om=class extends dt{static{l(this,"ConstantQuantize")}constructor(){super("constant_quantize",15),this.rootOpName="quantize"}match(e){let t=e.getOperand(0).definingOp;return!t||t.opName!=="constant"?!1:typeof t.getAttr("value")=="number"}rewrite(e,t){let s=e.getOperand(0).definingOp.getAttr("value"),o=e.getAttr("scale"),i=e.getAttr("zero_point");if(typeof o!="number"||typeof i!="number")return!1;let a=e.getAttr("target_dtype"),u=fo(a)*8,p=a==="ui8",c=p?0:-(1<<u-1),f=p?(1<<u)-1:(1<<u-1)-1,d=Math.max(c,Math.min(f,Math.round(s/o+i))),h=e.getResult(0).type,m=t.constant(d,h);return e.replaceAllResultsWith([m.getResult(0)]),e.erase(),!0}},im=class extends dt{static{l(this,"DequantizeFoldIntoDot")}constructor(){super("dequantize_fold_into_dot",15),this.rootOpName="dot"}match(e){if(e.numOperands!==2)return!1;let t=e.getOperand(0).definingOp,r=e.getOperand(1).definingOp;return t&&t.opName==="dequantize"&&r&&r.opName==="dequantize"}rewrite(e,t){let r=e.getOperand(0).definingOp,s=e.getOperand(1).definingOp,o=r.getOperand(0),i=s.getOperand(0),a=r.getAttr("scale"),u=s.getAttr("scale"),p=r.getAttr("zero_point"),c=s.getAttr("zero_point");if(typeof a!="number"||typeof u!="number"||typeof p!="number"||typeof c!="number"||p!==0||c!==0)return!1;let f=a*u,d=e.getAttr("lhs_contracting"),h=e.getAttr("rhs_contracting"),m=e.getAttr("lhs_batch")||[],g=e.getAttr("rhs_batch")||[],y=new I(e.getResult(0).type.shape,"i32"),_=t._buildOp("quantized_dot",[o,i],[y],{lhs_contracting:d,rhs_contracting:h,lhs_batch:m,rhs_batch:g,lhs_scale:a,lhs_zero_point:p,rhs_scale:u,rhs_zero_point:c,output_scale:f,output_zero_point:0}),b=t._inferAndBuild("dequantize",[_.getResult(0)],{scale:f,zero_point:0,scheme:r.getAttr("scheme"),target_dtype:e.getResult(0).type.dtype});return e.replaceAllResultsWith([b.getResult(0)]),e.erase(),!0}}});function ZN(n){n.register(new Z({name:"dot",numOperands:2,numResults:1,opAttrs:{gpuCapable:!0},traits:[Y.OPAQUE,Y.OUT_EWISE_FUSABLE],attrs:[{name:"lhs_contracting",type:"array",required:!0},{name:"rhs_contracting",type:"array",required:!0},{name:"lhs_batch",type:"array",required:!1},{name:"rhs_batch",type:"array",required:!1}],getFlops(e){let t=e.getOperand(0).type,r=e.getOperand(1).type;if(!(t instanceof I)||!(r instanceof I))return 0;let s=e.getAttr("lhs_contracting")||[],o=1;for(let u of s)t.shape[u]!==X&&(o*=t.shape[u]);let i=e.getResult(0).type;if(!(i instanceof I))return 0;let a=i.numel();return a===X?0:2*a*o},inferResultTypes:am,getCanonicalizationPatterns(){return[new Xh,new im]},verify(e){let t=[];if(e.numOperands!==2)return t.push("dot expects 2 operands"),t;e.hasAttr("lhs_contracting")||t.push("dot missing lhs_contracting"),e.hasAttr("rhs_contracting")||t.push("dot missing rhs_contracting");let r=e.getOperand(0).type,s=e.getOperand(1).type;if(r instanceof I&&s instanceof I){r.dtype!==s.dtype&&t.push(`dot dtype mismatch: ${r.dtype} vs ${s.dtype}`);let o=e.getAttr("lhs_contracting")||[],i=e.getAttr("rhs_contracting")||[];if(o.length!==i.length)t.push(`dot contracting dimensions count mismatch: lhs ${o.length} vs rhs ${i.length}`);else for(let a=0;a<o.length;a++){let u=o[a],p=i[a];u>=r.rank&&t.push(`dot lhs_contracting[${a}]=${u} out of range (rank ${r.rank})`),p>=s.rank&&t.push(`dot rhs_contracting[${a}]=${p} out of range (rank ${s.rank})`);let c=r.shape[u],f=s.shape[p];u<r.rank&&p<s.rank&&typeof c=="number"&&c!==X&&typeof f=="number"&&f!==X&&c!==f&&t.push(`dot contracting dim size mismatch at [${a}]: lhs dim ${u} size ${c} vs rhs dim ${p} size ${f}`)}}return t}})),n.register(new Z({name:"cublas_gemm",numOperands:2,numResults:1,traits:[Y.OPAQUE],attrs:[{name:"lhs_contracting",type:"array",required:!0},{name:"rhs_contracting",type:"array",required:!0},{name:"lhs_batch",type:"array",required:!1},{name:"rhs_batch",type:"array",required:!1}],inferResultTypes:am,getFlops(e){let t=e.getOperand(0).type,r=e.getResult(0).type;if(!(t instanceof I)||!(r instanceof I))return 0;let s=1;for(let i of e.getAttr("lhs_contracting")||[])t.shape[i]!==X&&(s*=t.shape[i]);let o=r.numel();return o===X?0:2*o*s},verify(e){let t=[];return e.numOperands!==2?(t.push("cublas_gemm expects 2 operands"),t):(e.hasAttr("lhs_contracting")||t.push("cublas_gemm missing lhs_contracting"),e.hasAttr("rhs_contracting")||t.push("cublas_gemm missing rhs_contracting"),t)}})),n.register(new Z({name:"conv",numOperands:2,numResults:1,opAttrs:{gpuCapable:!0},traits:[Y.OPAQUE],attrs:[{name:"strides",type:"array",required:!0},{name:"padding",type:"array",required:!0},{name:"dilation",type:"array",required:!1},{name:"groups",type:"number",required:!1},{name:"input_layout",type:"string",required:!0},{name:"kernel_layout",type:"string",required:!0}],getFlops(e){let t=e.getResult(0).type,r=e.getOperand(1).type;if(!(t instanceof I)||!(r instanceof I))return 0;let s=t.numel(),o=r.numel();return s===X||o===X?0:2*s*o/(r.shape[0]||1)},inferResultTypes:N0,verify(e){let t=[];return e.numOperands!==2?(t.push("conv expects 2 operands"),t):(e.hasAttr("strides")||t.push("conv missing strides"),e.hasAttr("padding")||t.push("conv missing padding"),e.hasAttr("input_layout")||t.push("conv missing input_layout"),e.hasAttr("kernel_layout")||t.push("conv missing kernel_layout"),t)}}))}function am(n,e,t){if(n.length!==2)return null;let r=n[0],s=n[1];if(!(r instanceof I)||!(s instanceof I))return null;let o=t&&!Array.isArray(t)?t:{};if(!o.allowMixedDtype&&r.dtype!==s.dtype)return null;let i=new Set(e.get?e.get("lhs_contracting"):e.lhs_contracting),a=new Set(e.get?e.get("rhs_contracting"):e.rhs_contracting),u=new Set((e.get?e.get("lhs_batch"):e.lhs_batch)||[]),p=new Set((e.get?e.get("rhs_batch"):e.rhs_batch)||[]),c=[];for(let f=0;f<r.rank;f++)u.has(f)&&c.push(r.shape[f]);for(let f=0;f<r.rank;f++)!i.has(f)&&!u.has(f)&&c.push(r.shape[f]);for(let f=0;f<s.rank;f++)!a.has(f)&&!p.has(f)&&c.push(s.shape[f]);return[new I(c,o.outputDtype||r.dtype)]}function N0(n,e,t){if(n.length!==2)return null;let r=n[0],s=n[1];if(!(r instanceof I)||!(s instanceof I))return null;let o=t&&!Array.isArray(t)?t:{};if(!o.allowMixedDtype&&r.dtype!==s.dtype)return null;let i=e.get?e.get("strides"):e.strides,a=e.get?e.get("padding"):e.padding,u=(e.get?e.get("dilation"):e.dilation)||i.map(()=>1),p=i.length,c=r.shape[0],f=s.shape[0],d=[];for(let h=0;h<p;h++){let m=r.shape[h+2],g=s.shape[h+2],y=a[h][0]+a[h][1];if(m===X||g===X)d.push(X);else{let _=(g-1)*u[h]+1;d.push(Math.floor((m+y-_)/i[h])+1)}}return[new I([c,f,...d],o.outputDtype||r.dtype)]}var A0=M(()=>{"use strict";Bn();Re();sl();k0();l(ZN,"register");l(am,"inferDotResultTypes");l(N0,"inferConvResultTypes")});function QN(n){let e={numOperands:0,numResults:1,attrs:[{name:"value",type:"any",required:!0},{name:"tensor_type",type:"object",required:!0}],traits:[Y.CONSTANT],inferResultTypes(t,r){let s=r.get?r.get("tensor_type"):r.tensor_type;return s?[s]:null}};for(let t of["constant","scalar_constant"])n.register(new Z({name:t,...e}));n.register(new Z({name:"iota",numOperands:0,numResults:1,attrs:[{name:"iota_dimension",type:"number",required:!0},{name:"tensor_type",type:"object",required:!0}],inferResultTypes(t,r){let s=r.get?r.get("tensor_type"):r.tensor_type;return s?[s]:null}})),n.register(new Z({name:"tuple",numOperands:-1,numResults:1,inferResultTypes(t){return[new Ia(t)]}})),n.register(new Z({name:"get_tuple_element",numOperands:1,numResults:1,attrs:[{name:"index",type:"number",required:!0}],inferResultTypes(t,r){if(t.length!==1)return null;let s=t[0];if(!s||!s.elements)return null;let o=r.get?r.get("index"):r.index;return o===void 0||o<0||o>=s.elements.length?null:[s.elements[o]]}})),n.register(new Z({name:"convert",numOperands:1,numResults:1,attrs:[{name:"target_dtype",type:"string",required:!0}],traits:[Y.ELEMENTWISE],inferResultTypes(t,r){if(t.length!==1)return null;let s=t[0];if(!(s instanceof I))return null;let o=r.get?r.get("target_dtype"):r.target_dtype;return[new I(s.shape,o)]}})),n.register(new Z({name:"one_hot",numOperands:1,numResults:1,attrs:[{name:"depth",type:"number",required:!0},{name:"axis",type:"number",required:!1},{name:"on_value",type:"number",required:!1},{name:"off_value",type:"number",required:!1},{name:"dtype",type:"string",required:!1}],inferResultTypes(t,r){if(t.length<1)return null;let s=t[0];if(!(s instanceof I))return null;let o=r.get?r.get("depth"):r.depth,i=(r.get?r.get("axis"):r.axis)??-1,a=(r.get?r.get("dtype"):r.dtype)||"f32",u=[...s.shape],p=i<0?u.length+1+i:i;return u.splice(p,0,o),[new I(u,a)]}}))}var XN=M(()=>{"use strict";Bn();Re();l(QN,"register")});function eA(n){n.register(new Z({name:"return",numOperands:-1,numResults:0,traits:[Y.TERMINATOR]})),n.register(new Z({name:"yield",numOperands:-1,numResults:0,traits:[Y.TERMINATOR]})),n.register(new Z({name:"if",numOperands:1,numResults:-1,hasRegions:!0,numRegions:2,sideEffects:gn.CONTROL,inferResultTypes(e,t,r){return r||null}})),n.register(new Z({name:"while",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:2,sideEffects:gn.CONTROL,inferResultTypes(e){return[...e]}})),n.register(new Z({name:"scan",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:1,sideEffects:gn.CONTROL,attrs:[{name:"num_carry",type:"number",required:!0},{name:"num_xs",type:"number",required:!0}],inferResultTypes(e,t,r){return r||null}})),n.register(new Z({name:"custom_call",numOperands:-1,numResults:-1,attrs:[{name:"call_target_name",type:"string",required:!0},{name:"backend_config",type:"any",required:!1}],sideEffects:gn.WRITE,inferResultTypes(e,t,r){return r||null}})),n.register(new Z({name:"fused_dot_epilogue",numOperands:-1,numResults:1,traits:[Y.OPAQUE],attrs:[{name:"lhs_contracting",type:"array",required:!0},{name:"rhs_contracting",type:"array",required:!0},{name:"lhs_batch",type:"array",required:!1},{name:"rhs_batch",type:"array",required:!1},{name:"epilogue_ops",type:"array",required:!0},{name:"epilogue_tags",type:"array",required:!0},{name:"num_dot_operands",type:"number",required:!0},{name:"num_extra_inputs",type:"number",required:!0}],inferResultTypes(e,t,r){return r||null}})),n.register(new Z({name:"fusion",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:1,attrs:[{name:"fusion_kind",type:"string",required:!1}],inferResultTypes(e,t,r){return r||null}}))}var tA=M(()=>{"use strict";Bn();l(eA,"register")});function nA(n){n.register(new Z({name:"layout_transform",numOperands:1,numResults:1,traits:[Y.VIEW],attrs:[{name:"src_layout",type:"array",required:!0},{name:"dst_layout",type:"array",required:!0}],inferResultTypes(e,t){if(e.length!==1)return null;let r=e[0];if(!(r instanceof I))return null;let s=t.get?t.get("dst_layout"):t.dst_layout;return s?[new I(r.shape,r.dtype,new Gt(s))]:null},verify(e){let t=[];if(e.numOperands!==1)return t.push("layout_transform expects 1 operand"),t;if(e.hasAttr("src_layout")||t.push("layout_transform missing src_layout"),e.hasAttr("dst_layout")||t.push("layout_transform missing dst_layout"),t.length===0){let r=e.getOperand(0).type,s=e.getAttr("src_layout"),o=e.getAttr("dst_layout");r instanceof I&&(s.length!==r.rank&&t.push(`src_layout length ${s.length} != input rank ${r.rank}`),o.length!==r.rank&&t.push(`dst_layout length ${o.length} != input rank ${r.rank}`))}return t},getCanonicalizationPatterns(){return[new Qh,new em]}}))}var rA=M(()=>{"use strict";Bn();Re();sl();l(nA,"register")});function sA(n){n.register(new Z({name:"quantize",numOperands:1,numResults:1,traits:[Y.ELEMENTWISE],attrs:[{name:"scale",type:"any",required:!0},{name:"zero_point",type:"any",required:!0},{name:"scheme",type:"string",required:!0},{name:"target_dtype",type:"string",required:!0},{name:"axis",type:"number",required:!1}],inferResultTypes(e,t){if(e.length!==1)return null;let r=e[0];if(!(r instanceof I))return null;let s=t.get?t.get("target_dtype"):t.target_dtype;return s?[new I(r.shape,s)]:null},verify(e){let t=[];if(e.numOperands!==1)return t.push("quantize expects 1 operand"),t;let r=e.getOperand(0).type;r instanceof I&&!jt(r.dtype)&&t.push(`quantize input must be float, got ${r.dtype}`);let s=e.getAttr("target_dtype");s&&!O0.has(s)&&t.push(`quantize target_dtype must be i8 or ui8, got ${s}`);let o=e.getAttr("scale");return typeof o=="number"&&o<=0&&t.push("quantize scale must be positive"),t},fold(e,t){if(typeof e[0]!="number")return;let r=t.get?t.get("scale"):t.scale,s=t.get?t.get("zero_point"):t.zero_point;if(!(typeof r!="number"||typeof s!="number"))return Math.round(e[0]/r+s)},getCanonicalizationPatterns(){return[new sm,new om]}})),n.register(new Z({name:"dequantize",numOperands:1,numResults:1,traits:[Y.ELEMENTWISE],attrs:[{name:"scale",type:"any",required:!0},{name:"zero_point",type:"any",required:!0},{name:"scheme",type:"string",required:!0},{name:"target_dtype",type:"string",required:!0},{name:"axis",type:"number",required:!1}],inferResultTypes(e,t){if(e.length!==1)return null;let r=e[0];if(!(r instanceof I))return null;let s=t.get?t.get("target_dtype"):t.target_dtype;return s?[new I(r.shape,s)]:null},verify(e){let t=[];if(e.numOperands!==1)return t.push("dequantize expects 1 operand"),t;let r=e.getOperand(0).type;r instanceof I&&!RP.has(r.dtype)&&t.push("dequantize input must be i8, ui8, or i32, got "+r.dtype);let s=e.getAttr("target_dtype");return s&&!jt(s)&&t.push(`dequantize target_dtype must be float, got ${s}`),t},fold(e,t){if(typeof e[0]!="number")return;let r=t.get?t.get("scale"):t.scale,s=t.get?t.get("zero_point"):t.zero_point;if(!(typeof r!="number"||typeof s!="number"))return(e[0]-s)*r}})),n.register(new Z({name:"quantized_dot",numOperands:2,numResults:1,traits:[Y.OPAQUE],attrs:[{name:"lhs_contracting",type:"array",required:!0},{name:"rhs_contracting",type:"array",required:!0},{name:"lhs_batch",type:"array",required:!1},{name:"rhs_batch",type:"array",required:!1},{name:"lhs_scale",type:"number",required:!0},{name:"lhs_zero_point",type:"number",required:!0},{name:"rhs_scale",type:"number",required:!0},{name:"rhs_zero_point",type:"number",required:!0},{name:"output_scale",type:"number",required:!0},{name:"output_zero_point",type:"number",required:!0}],getFlops(e){let t=e.getOperand(0).type,r=e.getOperand(1).type;if(!(t instanceof I)||!(r instanceof I))return 0;let s=e.getAttr("lhs_contracting")||[],o=1;for(let u of s)t.shape[u]!==X&&(o*=t.shape[u]);let i=e.getResult(0).type;if(!(i instanceof I))return 0;let a=i.numel();return a===X?0:2*a*o},inferResultTypes(e,t){return am(e,t,{outputDtype:"i32",allowMixedDtype:!0})},verify(e){let t=[];if(e.numOperands!==2)return t.push("quantized_dot expects 2 operands"),t;e.hasAttr("lhs_contracting")||t.push("quantized_dot missing lhs_contracting"),e.hasAttr("rhs_contracting")||t.push("quantized_dot missing rhs_contracting"),e.hasAttr("lhs_scale")||t.push("quantized_dot missing lhs_scale"),e.hasAttr("rhs_scale")||t.push("quantized_dot missing rhs_scale");let r=e.getOperand(0).type,s=e.getOperand(1).type;return r instanceof I&&!O0.has(r.dtype)&&t.push(`quantized_dot lhs must be i8/ui8, got ${r.dtype}`),s instanceof I&&!O0.has(s.dtype)&&t.push(`quantized_dot rhs must be i8/ui8, got ${s.dtype}`),t}})),n.register(new Z({name:"quantized_conv",numOperands:2,numResults:1,traits:[Y.OPAQUE],attrs:[{name:"strides",type:"array",required:!0},{name:"padding",type:"array",required:!0},{name:"dilation",type:"array",required:!1},{name:"groups",type:"number",required:!1},{name:"input_layout",type:"string",required:!0},{name:"kernel_layout",type:"string",required:!0},{name:"input_scale",type:"number",required:!0},{name:"input_zero_point",type:"number",required:!0},{name:"kernel_scale",type:"number",required:!0},{name:"kernel_zero_point",type:"number",required:!0},{name:"output_scale",type:"number",required:!0},{name:"output_zero_point",type:"number",required:!0}],inferResultTypes(e,t){return N0(e,t,{outputDtype:"i32",allowMixedDtype:!0})},verify(e){let t=[];return e.numOperands!==2?(t.push("quantized_conv expects 2 operands"),t):(e.hasAttr("strides")||t.push("quantized_conv missing strides"),e.hasAttr("padding")||t.push("quantized_conv missing padding"),e.hasAttr("input_layout")||t.push("quantized_conv missing input_layout"),e.hasAttr("kernel_layout")||t.push("quantized_conv missing kernel_layout"),e.hasAttr("input_scale")||t.push("quantized_conv missing input_scale"),e.hasAttr("kernel_scale")||t.push("quantized_conv missing kernel_scale"),t)}}))}var O0,RP,oA=M(()=>{"use strict";Bn();Re();k0();A0();O0=new Set(["i8","ui8"]),RP=new Set(["i8","ui8","i32"]);l(sA,"register")});function MP(n){if(n.length<1)return null;let e=n[0];return e instanceof I?[new I(e.shape,e.dtype)]:null}function js(n){if(n.length<1)return null;let e=n[0];return!(e instanceof I)||!jt(e.dtype)?null:[new I(e.shape,e.dtype)]}function Zo(n){let e=[];if(n.numOperands<1)return e.push(`${n.opName} expects at least 1 operand`),e;let t=n.getOperand(0).type;return t instanceof I&&!jt(t.dtype)&&e.push(`${n.opName} requires float input, got ${t.dtype}`),e}function iA(n){n.register(new Z({name:"all_reduce",numOperands:1,numResults:1,traits:[Y.OPAQUE],attrs:[{name:"reduce_op",type:"string",required:!1},{name:"mesh_axis",type:"number",required:!1}],inferResultTypes:MP})),n.register(new Z({name:"all_gather",numOperands:1,numResults:1,traits:[Y.OPAQUE],attrs:[{name:"mesh_axis",type:"number",required:!1},{name:"gather_dim",type:"number",required:!1}],inferResultTypes(e,t){let r=e[0];if(!(r instanceof I))return null;let s=t&&t.has("mesh_axis")?t.get("mesh_axis"):0,o=t&&t.has("gather_dim")?t.get("gather_dim"):1,i=[...r.shape];return i[o]=i[o]*i[s],[new I(i,r.dtype)]}})),n.register(new Z({name:"scaled_dot_product_attention",numOperands:3,numResults:1,traits:[Y.OPAQUE],attrs:[{name:"scale",type:"number",required:!0},{name:"causal",type:"boolean",required:!1}],inferResultTypes(e){let t=e[0],r=e[2];return!(t instanceof I)||!(r instanceof I)?null:[new I([...t.shape.slice(0,t.rank-1),r.shape[r.rank-1]],t.dtype)]}})),n.register(new Z({name:"softmax",numOperands:1,numResults:1,opAttrs:{gpuCapable:!0},attrs:[{name:"axis",type:"number",required:!0}],inferResultTypes:js,verify:Zo})),n.register(new Z({name:"log_softmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0}],inferResultTypes:js,verify:Zo})),n.register(new Z({name:"gelu",numOperands:1,numResults:1,traits:[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:js,verify:Zo})),n.register(new Z({name:"sigmoid",numOperands:1,numResults:1,traits:[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:js,verify:Zo})),n.register(new Z({name:"silu",numOperands:1,numResults:1,traits:[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:js,verify:Zo})),n.register(new Z({name:"layer_norm",numOperands:3,numResults:1,opAttrs:{gpuCapable:!0},attrs:[{name:"axis",type:"number",required:!0},{name:"epsilon",type:"number",required:!0}],inferResultTypes:js,verify(e){let t=[];if(e.numOperands!==3)return t.push("layer_norm expects 3 operands (input, gamma, beta)"),t;let r=e.getOperand(0).type;return r instanceof I&&!jt(r.dtype)&&t.push(`layer_norm requires float input, got ${r.dtype}`),t}})),n.register(new Z({name:"batch_norm",numOperands:5,numResults:1,opAttrs:{gpuCapable:!0},attrs:[{name:"axis",type:"number",required:!0},{name:"epsilon",type:"number",required:!0}],inferResultTypes:js,verify(e){let t=[];if(e.numOperands!==5)return t.push("batch_norm expects 5 operands (input, gamma, beta, mean, var)"),t;let r=e.getOperand(0).type;return r instanceof I&&!jt(r.dtype)&&t.push(`batch_norm requires float input, got ${r.dtype}`),t}}));for(let e of["selu","mish","hardswish","hardsigmoid"])n.register(new Z({name:e,numOperands:1,numResults:1,traits:[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:js,verify:Zo}));n.register(new Z({name:"elu",numOperands:1,numResults:1,attrs:[{name:"alpha",type:"number"}],traits:[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:js,verify:Zo})),n.register(new Z({name:"leaky_relu",numOperands:1,numResults:1,attrs:[{name:"negative_slope",type:"number"}],traits:[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:js,verify:Zo})),n.register(new Z({name:"celu",numOperands:1,numResults:1,attrs:[{name:"alpha",type:"number"}],traits:[Y.ELEMENTWISE,Y.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:js,verify:Zo})),n.register(new Z({name:"embedding",numOperands:2,numResults:1,inferResultTypes(e){if(e.length<2)return null;let t=e[0],r=e[1];if(!(t instanceof I)||!(r instanceof I))return null;let s=[...r.shape,t.shape[t.rank-1]];return[new I(s,t.dtype)]}}))}var aA=M(()=>{"use strict";Bn();Re();l(MP,"inferSameAsInput");l(js,"inferSameAsInputFloat");l(Zo,"verifyUnaryFloat");l(iA,"register")});function uA(n){n.register(new Z({name:"copy_to_device",numOperands:1,numResults:1,attrs:[{name:"src_device",type:"string",required:!0},{name:"dst_device",type:"string",required:!0}],sideEffects:gn.READ|gn.WRITE,traits:[Y.INJECTIVE],inferResultTypes(e){return e.length!==1?null:[e[0]]},getFlops(){return 0}}))}var lA=M(()=>{"use strict";Bn();l(uA,"register")});function cA(n,e,t,r,s,o){let i=n+r+s-e;return(o?Math.ceil(i/t):Math.floor(i/t))+1}function pA(n){n.register(new Z({name:"pool2d",numOperands:1,numResults:1,traits:[Y.OPAQUE],attrs:[{name:"pool_type",type:"string",required:!0},{name:"kernel_size",type:"array",required:!0},{name:"strides",type:"array",required:!0},{name:"padding",type:"array",required:!0},{name:"ceil_mode",type:"boolean",required:!1},{name:"count_include_pad",type:"boolean",required:!1},{name:"layout",type:"string",required:!1}],inferResultTypes(e,t){if(e.length<1)return null;let r=e[0];if(!(r instanceof I)||r.rank!==4)return null;let s=t.get?t.get("kernel_size"):t.kernel_size,o=t.get?t.get("strides"):t.strides,i=t.get?t.get("padding"):t.padding,a=(t.get?t.get("ceil_mode"):t.ceil_mode)||!1,u=cA(r.shape[2],s[0],o[0],i[0][0],i[0][1],a),p=cA(r.shape[3],s[1],o[1],i[1][0],i[1][1],a);return[new I([r.shape[0],r.shape[1],u,p],r.dtype)]}}))}var fA=M(()=>{"use strict";Bn();Re();l(cA,"computePoolOutputDim");l(pA,"register")});function dA(n){n.register(new Z({name:"resize",numOperands:1,numResults:1,traits:[Y.OPAQUE],attrs:[{name:"output_size",type:"array",required:!0},{name:"method",type:"string",required:!0},{name:"coordinate_mode",type:"string",required:!1},{name:"layout",type:"string",required:!1}],inferResultTypes(e,t){if(e.length<1)return null;let r=e[0];if(!(r instanceof I)||r.rank!==4)return null;let s=t.get?t.get("output_size"):t.output_size;return[new I([r.shape[0],r.shape[1],s[0],s[1]],r.dtype)]}}))}var hA=M(()=>{"use strict";Bn();Re();l(dA,"register")});function EP(n=IP){let e=new Eh;for(let t of n)t(e);return e}var IP,ne,Ct=M(()=>{"use strict";Bn();VN();jN();qN();KN();YN();A0();XN();tA();rA();oA();aA();lA();fA();hA();IP=[zN,GN,UN,WN,HN,ZN,QN,eA,nA,sA,iA,uA,pA,dA];l(EP,"buildRegistry");ne=EP()});function Qo(n,e,t="ignore"){let r=[],s=new Map;for(let o of n){if(s.get(o)!==void 0)continue;s.set(o,1);let i=[{op:o,i:0}];for(;i.length>0;){let a=i[i.length-1],u=a.op;if(a.i<u.numOperands){let p=u.getOperand(a.i).definingOp;if(a.i++,p&&e(p)){let c=s.get(p);if(c===void 0)s.set(p,1),i.push({op:p,i:0});else if(c===1){if(t==="throw")throw new Error("topo sort: cycle detected");if(t==="null")return null}}continue}s.set(u,2),r.push(u),i.pop()}}return r}function um(n,e="throw"){let t=Array.isArray(n)?n:[...n],r=new Set(t);return Qo(t,s=>r.has(s),e)}var Da=M(()=>{"use strict";l(Qo,"topoSortByOperands");l(um,"topoSortOpSet")});function mA(n){return Array.isArray(n)?n.map(mA):n}function gA(n){if(typeof n=="number")return n*2654435761&2147483647;if(typeof n=="string"){let e=0;for(let t=0;t<n.length;t++)e=(e<<5)-e+n.charCodeAt(t)&2147483647;return e}if(typeof n=="boolean")return n?1:0;if(Array.isArray(n)){let e=2654435769;for(let t=0;t<n.length;t++)e=(e^gA(n[t]))*16777619&2147483647;return e}if(ArrayBuffer.isView(n)&&n.buffer instanceof ArrayBuffer){let e=new Uint8Array(n.buffer,n.byteOffset,n.byteLength),t=2654435769;t=(t^e.length)*16777619&2147483647;let r=e.length>256?Math.ceil(e.length/256):1;for(let s=0;s<e.length;s+=r)t=(t^e[s])*16777619&2147483647;return t}return typeof n=="object"&&n!==null&&typeof n.hash=="function"?n.hash():0}function yA(n,e){if(n===e)return!0;if(typeof n!=typeof e)return!1;if(Array.isArray(n)){if(!Array.isArray(e)||n.length!==e.length)return!1;for(let t=0;t<n.length;t++)if(!yA(n[t],e[t]))return!1;return!0}return typeof n=="object"&&n!==null&&typeof n.equals=="function"?n.equals(e):!1}function CP(n,e=new Map){let t=new Ut;for(let r of n.blocks){let s=r.arguments.map(p=>p.type),o=new r.constructor(s);for(let p=0;p<r.arguments.length;p++)e.set(r.arguments[p],o.arguments[p]);let i=r.opsArray(),a=new Set(i),u=new Map;for(let p of Qo(i,c=>a.has(c),"ignore"))u.set(p,p.clone(e));for(let p of i)o.pushOp(u.get(p));t.addBlock(o)}return t}var LP,Me,yr=M(()=>{"use strict";Ih();Ea();Ct();Da();LP=0,Me=class n{static{l(this,"Operation")}constructor(e,t,r,s=null,o=null){if(this.opName=e,this.id=LP++,this.parentBlock=null,this._prev=null,this._next=null,this.attributes=new Map,s)if(s instanceof Map)for(let[i,a]of s)this.attributes.set(i,a);else for(let i of Object.keys(s))this.attributes.set(i,s[i]);this.operands=new Array(t.length),this._operandLinks=new Array(t.length);for(let i=0;i<t.length;i++){this.operands[i]=t[i];let a=new up(this,i);t[i].addUse(a),this._operandLinks[i]=a}this.results=new Array(r.length);for(let i=0;i<r.length;i++)this.results[i]=new Li(r[i],this,i);if(this.regions=[],o)for(let i=0;i<o.length;i++){let a=o[i]instanceof Ut?o[i]:new Ut;a.parentOp=this,this.regions.push(a)}}get numOperands(){return this.operands.length}get numResults(){return this.results.length}get numRegions(){return this.regions.length}getOperand(e){return this.operands[e]}getResult(e){return this.results[e]}getRegion(e){return this.regions[e]}getAttr(e){return this.attributes.get(e)}hasAttr(e){return this.attributes.has(e)}setAttr(e,t){this.attributes.set(e,t)}removeAttr(e){return this.attributes.delete(e)}replaceOperand(e,t){if(e<0||e>=this.operands.length)throw new Error(`replaceOperand: index ${e} out of range for '${this.opName}' (${this.operands.length} operands)`);if(!(t instanceof Li))throw new Error(`replaceOperand: new operand for '${this.opName}' is not a Value`);let r=this.operands[e];if(r===t)return;r.removeUse(this._operandLinks[e]),this.operands[e]=t;let s=new up(this,e);t.addUse(s),this._operandLinks[e]=s,this.parentBlock&&this.parentBlock._notifyMutation()}dropAllOperands(){let e=this.operands.length>0;for(let t=0;t<this.operands.length;t++)this.operands[t].removeUse(this._operandLinks[t]);this.operands=[],this._operandLinks=[],e&&this.parentBlock&&this.parentBlock._notifyMutation()}erase(){for(let e=0;e<this.results.length;e++)if(this.results[e].hasUses)throw new Error(`Cannot erase ${this.opName}: result ${e} still has uses`);this.dropAllOperands(),this.parentBlock&&this.parentBlock.removeOp(this)}replaceAllResultsWith(e){if(!e||e.length!==this.results.length)throw new Error(`replaceAllResultsWith: '${this.opName}' has ${this.results.length} results, got ${e?e.length:0}`);for(let t=0;t<this.results.length;t++)this.results[t].replaceAllUsesWith(e[t])}isTerminator(){let e=ne.get(this.opName);return!!(e&&e.isTerminator)}hasSideEffects(){let e=ne.get(this.opName);return!!(e&&e.hasSideEffects)}getParentFunction(){return this.parentBlock?this.parentBlock._owningFunction():null}clone(e=new Map){let t=this.operands.map(i=>e.get(i)||i),r=this.regions.map(i=>CP(i,e)),s=new Map;for(let[i,a]of this.attributes)s.set(i,mA(a));let o=new n(this.opName,t,this.results.map(i=>i.type),s,r);for(let i=0;i<this.results.length;i++)e.set(this.results[i],o.results[i]);return o}structuralHash(){let e=2166136261;for(let t=0;t<this.opName.length;t++)e=(e^this.opName.charCodeAt(t))*16777619&2147483647;for(let t=0;t<this.operands.length;t++)e=(e^this.operands[t].id)*16777619&2147483647;for(let[t,r]of this.attributes){for(let s=0;s<t.length;s++)e=(e^t.charCodeAt(s))*16777619&2147483647;e=(e^gA(r))*16777619&2147483647}return this.regions.length>0&&(e=(e^2654435769+this.regions.length)*16777619&2147483647),e}structuralEquals(e){if(this.opName!==e.opName||this.operands.length!==e.operands.length||this.results.length!==e.results.length||this.attributes.size!==e.attributes.size||this.regions.length>0||e.regions.length>0)return!1;for(let t=0;t<this.operands.length;t++)if(this.operands[t]!==e.operands[t])return!1;for(let[t,r]of this.attributes)if(!e.attributes.has(t)||!yA(r,e.attributes.get(t)))return!1;for(let t=0;t<this.results.length;t++)if(!this.results[t].type.equals(e.results[t].type))return!1;return!0}};l(mA,"cloneAttrValue");l(gA,"hashAttrValue");l(yA,"attrValueEquals");l(CP,"cloneRegion")});function*_A(n){if(!(!n.regions||n.regions.length===0))for(let e of n.regions)for(let t of e.blocks)for(let r of t.ops())yield r,yield*_A(r)}function*bA(n){if(!(!n.regions||n.regions.length===0))for(let e of n.regions)for(let t of e.blocks){yield t;for(let r of t.ops())yield*bA(r)}}function DP(n){let e=n.opsArray(),t=new Set(e);return Qo(e,r=>t.has(r),"ignore")}function lm(n){let e=new yn(n.name,n.inputTypes,n.outputTypes),t=new Map,r=n.entryBlock,s=e.entryBlock;for(let i=0;i<r.arguments.length;i++)t.set(r.arguments[i],s.arguments[i]);let o=new Map;for(let i of DP(r))o.set(i,i.clone(t));for(let i of r)s.pushOp(o.get(i));return e._version=n._version,e}var yn,Us=M(()=>{"use strict";Ea();Da();l(_A,"opsInRegions");l(bA,"blocksInRegions");yn=class{static{l(this,"GraphFunction")}constructor(e,t,r){this.name=e,this.inputTypes=Object.freeze([...t]),this.outputTypes=Object.freeze([...r]),this.body=new Ut;let s=new On(t);s._parentFunction=this,this.body.addBlock(s),this._version=0}get entryBlock(){return this.body.entryBlock}get args(){return this.entryBlock.arguments}bumpVersion(){this._version++}get version(){return this._version}*ops(){for(let e of this.body)yield*e}*opsRecursive(){for(let e of this.ops())yield e,yield*_A(e)}*blocksRecursive(){for(let e of this.body){yield e;for(let t of e)yield*bA(t)}}opsArray(){let e=[];for(let t of this.body)for(let r of t)e.push(r);return e}numOps(){let e=0;for(let t of this.body)e+=t.size;return e}findOp(e){for(let t of this.ops())if(e(t))return t;return null}findOps(e){let t=[];for(let r of this.ops())e(r)&&t.push(r);return t}getReturnOp(){let e=this.entryBlock.lastOp;return e&&e.opName==="return"?e:null}getReturnValues(){let e=this.getReturnOp();return e?[...e.operands]:[]}verify(){let e=[];if(!this.entryBlock)return e.push("Function has no entry block"),e;this.entryBlock.arguments.length!==this.inputTypes.length&&e.push(`Entry block has ${this.entryBlock.arguments.length} args but function expects ${this.inputTypes.length}`);let t=this.getReturnOp();return t?t.numOperands!==this.outputTypes.length&&e.push(`Return has ${t.numOperands} operands but function declares ${this.outputTypes.length} outputs`):e.push("Function body has no return op"),e}};l(DP,"topoOrderTopLevel");l(lm,"cloneGraphFunction")});function wA(n){let e=new Mn(n.name);for(let t of n)e.addFunction(lm(t));return e._version=n._version,e}var Mn,Di=M(()=>{"use strict";Us();Mn=class{static{l(this,"GraphModule")}constructor(e="module"){this.name=e,this._functions=new Map,this._version=0}get version(){return this._version}addFunction(e){return this._functions.set(e.name,e),e._module=this,this._version++,e}getFunction(e){return this._functions.get(e)||null}hasFunction(e){return this._functions.has(e)}removeFunction(e){let t=this._functions.delete(e);return t&&this._version++,t}get functionCount(){return this._functions.size}functionNames(){return[...this._functions.keys()]}*functions(){yield*this._functions.values()}*[Symbol.iterator](){yield*this._functions.values()}verify(){let e=[];this._functions.size===0&&e.push("Module has no functions");for(let t of this._functions.values()){let r=t.verify();for(let s=0;s<r.length;s++)e.push(`${t.name}: ${r[s]}`)}return e}};l(wA,"cloneGraphModule")});function TA(n){return n?n instanceof Ia?`tuple(${n.types.map(TA).join(", ")})`:n.shape!==void 0&&n.dtype!==void 0?`[${n.shape.join(",")}]:${n.dtype}`:String(n):"?"}function xA(n,e,t){let r=n.rank,s=e<0?r+e:e,o=n.shape.map((a,u)=>u===s?1:a),i=[];for(let a=0;a<r;a++)a!==s&&i.push(a<s?a:a-1+t);return{offsetDims:i,collapsedSliceDims:[s],startIndexMap:[s],indexVectorDim:t,sliceSizes:o}}function PP(n,e){let t=Math.max(n.length,e.length),r=new Array(t);for(let s=0;s<t;s++){let o=s<n.length?n[n.length-1-s]:1,i=s<e.length?e[e.length-1-s]:1;r[t-1-s]=o===1?i:o}return r}function Xo(n,e){let t=[];for(let r=0;r<n;r++)r!==e&&t.push(r);return t}function $P(n){if(!n)return new Map;if(n instanceof Map)return n;let e=new Map;for(let t of Object.keys(n))e.set(t,n[t]);return e}function mp(n,e,t,r){let s=new yn(n,e,t),o=new $n(s);return r(o,s.args),s}var $n,_s=M(()=>{"use strict";Re();yr();Ea();Us();Di();Ct();l(TA,"describeType");l(xA,"indexSelectGatherOpts");l(PP,"bcastBatchDims");$n=class n{static{l(this,"IRBuilder")}constructor(e){this.func=e,this.block=e.entryBlock,this._insertionPoint=null}setInsertionPoint(e){this._insertionPoint=e}setInsertionPointToEnd(){this._insertionPoint=null}_insert(e){return this._insertionPoint?this.block.insertBefore(e,this._insertionPoint):this.block.pushOp(e),e}_buildOp(e,t,r,s=null,o=null){let i=new Me(e,t,r,s,o);return this._insert(i)}_inferAndBuild(e,t,r=null,s=null,o=null){let i=ne.get(e),a=o;if(!a&&i&&i.inferResultTypes){let u=t.map(c=>c.type),p=r instanceof Map?r:$P(r);a=i.inferResultTypes(u,p,o)}if(!a){let u=t.map(c=>TA(c.type)).join(", "),p=i?i.inferResultTypes?"inferResultTypes returned no types":"op has no inferResultTypes and none were given":"op is not registered";throw new Error(`Cannot infer result types for op '${e}' (${p}); operands: [${u}]`)}return this._buildOp(e,t,a,r,s)}constant(e,t){return this._buildOp("constant",[],[t],{value:e,tensor_type:t})}scalarConstant(e,t="f32"){let r=new I([],t);return this.constant(e,r)}tensorConstant(e,t,r="f32"){let s=new I(t,r);return this.constant(e,s)}iota(e,t){return this._buildOp("iota",[],[t],{iota_dimension:e,tensor_type:t})}add(e,t){return this._inferAndBuild("add",[e,t])}sub(e,t){return this._inferAndBuild("sub",[e,t])}mul(e,t){return this._inferAndBuild("mul",[e,t])}div(e,t){return this._inferAndBuild("div",[e,t])}rem(e,t){return this._inferAndBuild("rem",[e,t])}pow(e,t){return this._inferAndBuild("pow",[e,t])}maximum(e,t){return this._inferAndBuild("maximum",[e,t])}minimum(e,t){return this._inferAndBuild("minimum",[e,t])}neg(e){return this._inferAndBuild("neg",[e])}stopGradient(e){return this._inferAndBuild("stop_gradient",[e])}reverse(e,t){return this._inferAndBuild("reverse",[e],{dimensions:t})}scaledDotProductAttention(e,t,r,s,o=!1){return this._inferAndBuild("scaled_dot_product_attention",[e,t,r],{scale:s,causal:o})}allReduce(e,t={}){return this._inferAndBuild("all_reduce",[e],{reduce_op:t.reduceOp||"sum",mesh_axis:t.meshAxis??0})}allGather(e,t={}){return this._inferAndBuild("all_gather",[e],{mesh_axis:t.meshAxis??0,gather_dim:t.gatherDim??1})}abs(e){return this._inferAndBuild("abs",[e])}exp(e){return this._inferAndBuild("exp",[e])}log(e){return this._inferAndBuild("log",[e])}sqrt(e){return this._inferAndBuild("sqrt",[e])}rsqrt(e){return this._inferAndBuild("rsqrt",[e])}tanh(e){return this._inferAndBuild("tanh",[e])}sin(e){return this._inferAndBuild("sin",[e])}cos(e){return this._inferAndBuild("cos",[e])}floor(e){return this._inferAndBuild("floor",[e])}ceil(e){return this._inferAndBuild("ceil",[e])}sign(e){return this._inferAndBuild("sign",[e])}erf(e){return this._inferAndBuild("erf",[e])}log2(e){return this._inferAndBuild("log2",[e])}log10(e){return this._inferAndBuild("log10",[e])}exp2(e){return this._inferAndBuild("exp2",[e])}square(e){return this._inferAndBuild("square",[e])}reciprocal(e){return this._inferAndBuild("reciprocal",[e])}logicalNot(e){return this._inferAndBuild("logical_not",[e])}logicalAnd(e,t){return this._inferAndBuild("logical_and",[e,t])}logicalOr(e,t){return this._inferAndBuild("logical_or",[e,t])}compare(e,t,r){return this._inferAndBuild("compare",[e,t],{direction:r})}select(e,t,r){return this._inferAndBuild("select",[e,t,r])}clamp(e,t,r){return this._inferAndBuild("clamp",[e,t,r])}broadcast(e,t,r){return this._inferAndBuild("broadcast_in_dim",[e],{result_shape:t,broadcast_dimensions:r})}reshape(e,t){return this._inferAndBuild("reshape",[e],{new_shape:t})}transpose(e,t){return this._inferAndBuild("transpose",[e],{permutation:t})}slice(e,t,r,s=null){let o={starts:t,limits:r};return s&&(o.strides=s),this._inferAndBuild("slice",[e],o)}concat(e,t){return this._inferAndBuild("concat",e,{dimension:t})}pad(e,t,r,s,o=null){let i={low:r,high:s};return o&&(i.interior=o),this._inferAndBuild("pad",[e,t],i)}reduce(e,t,r,s){let o=new I([],e.type.dtype),i=new Ut,a=new On([o,o]);return i.addBlock(a),this._inferAndBuild("reduce",[e,t],{dimensions:r,reduce_type:s},[i])}dot(e,t,r,s,o=[],i=[]){return this._inferAndBuild("dot",[e,t],{lhs_contracting:r,rhs_contracting:s,lhs_batch:o,rhs_batch:i})}matmul(e,t){let r=e.type.rank,s=t.type.rank;if(r===1&&s===1)return this.dot(e,t,[0],[0]);let o=e,i=t,a=!1,u=!1;r===1&&(o=this.reshape(e,[1,e.type.shape[0]]).getResult(0),a=!0),s===1&&(i=this.reshape(t,[t.type.shape[0],1]).getResult(0),u=!0);let p=o.type.rank,c=i.type.rank,f=i.type.shape.slice(0,c-2),d;if(f.length===0)d=this.dot(o,i,[p-1],[0]);else{let g=o.type.shape.slice(0,p-2),y=PP(g,f),_=y.length,b=this._broadcastBatch(o,g,y),w=this._broadcastBatch(i,f,y),x=Array.from({length:_},(S,T)=>T);d=this.dot(b,w,[_+1],[_],x,x)}if(!a&&!u)return d;let h=d.getResult(0).type.shape,m=new Set;return u&&m.add(h.length-1),a&&m.add(h.length-2),this.reshape(d.getResult(0),h.filter((g,y)=>!m.has(y)))}_broadcastBatch(e,t,r){let s=e.type.shape.slice(e.type.rank-2),o=[...r,...s];if(t.length===r.length&&t.every((u,p)=>u===r[p]))return e;let i=r.length-t.length,a=[];for(let u=0;u<t.length;u++)a.push(i+u);return a.push(o.length-2,o.length-1),this.broadcast(e,o,a).getResult(0)}conv(e,t,r,s,o={}){return this._inferAndBuild("conv",[e,t],{strides:r,padding:s,dilation:o.dilation||r.map(()=>1),groups:o.groups||1,input_layout:o.inputLayout||"NCHW",kernel_layout:o.kernelLayout||"OIHW"})}convert(e,t){return this._inferAndBuild("convert",[e],{target_dtype:t})}customCall(e,t,r,s=null){let o={call_target_name:e};return s&&(o.backend_config=s),this._buildOp("custom_call",t,r,o)}fusion(e,t,r,s){let o=new Ut,i=new On(e.map(u=>u.type));o.addBlock(i);let a=this._buildOp("fusion",e,t,{fusion_kind:r},[o]);if(s){let u=new n(this.func);u.block=i,s(u,i.arguments)}return a}ifOp(e,t,r,s){let o=new Ut,i=new On([]);o.addBlock(i);let a=new Ut,u=new On([]);a.addBlock(u);let p=this._buildOp("if",[e],t,null,[o,a]);if(r){let c=new n(this.func);c.block=i,r(c)}if(s){let c=new n(this.func);c.block=u,s(c)}return p}whileOp(e,t,r){let s=e.map(c=>c.type),o=new Ut,i=new On(s);o.addBlock(i);let a=new Ut,u=new On(s);a.addBlock(u);let p=this._buildOp("while",e,s,null,[o,a]);if(t){let c=new n(this.func);c.block=i,t(c,i.arguments)}if(r){let c=new n(this.func);c.block=u,r(c,u.arguments)}return p}scanOp(e,t,r){let s=e.map(g=>g.type.withShape(g.type.shape.slice(1))),o=t.map(g=>g.type),i=new Ut,a=new On([...s,...o]);i.addBlock(a);let u=new n(this.func);u.block=a;let p=a.arguments.slice(0,s.length),c=a.arguments.slice(s.length),[f,d]=r(u,p,c);if(u.yieldOp([...f,...d]),e.length===0)throw new Error("scanOp requires at least one xs input");let h=e[0].type.shape[0];if(typeof h!="number"||h<0)throw new Error(`scanOp requires a static, non-negative leading dim on xs, got ${h}`);for(let g=1;g<e.length;g++)if(e[g].type.shape[0]!==h)throw new Error("scanOp requires all xs inputs to share the same leading length");let m=d.map(g=>g.type.withShape([h,...g.type.shape]));return this._buildOp("scan",[...e,...t],[...o,...m],{num_carry:t.length,num_xs:e.length},[i])}returnOp(e){return this._buildOp("return",e,[])}yieldOp(e){return this._buildOp("yield",e,[])}relu(e){let t=this.scalarConstant(0,e.type.dtype),r=this.broadcast(t.getResult(0),e.type.shape,[]);return this.maximum(e,r.getResult(0))}softmax(e,t=-1){let r=t<0?e.type.rank+t:t;return this._inferAndBuild("softmax",[e],{axis:r})}logSoftmax(e,t=-1){let r=t<0?e.type.rank+t:t;return this._inferAndBuild("log_softmax",[e],{axis:r})}sigmoid(e){return this._inferAndBuild("sigmoid",[e])}gelu(e){return this._inferAndBuild("gelu",[e])}silu(e){return this._inferAndBuild("silu",[e])}elu(e,t=1){return this._inferAndBuild("elu",[e],{alpha:t})}leakyRelu(e,t=.01){return this._inferAndBuild("leaky_relu",[e],{negative_slope:t})}celu(e,t=1){return this._inferAndBuild("celu",[e],{alpha:t})}selu(e){return this._inferAndBuild("selu",[e])}mish(e){return this._inferAndBuild("mish",[e])}hardswish(e){return this._inferAndBuild("hardswish",[e])}hardsigmoid(e){return this._inferAndBuild("hardsigmoid",[e])}layernorm(e,t,r,s=-1,o=1e-5){let i=s<0?e.type.rank+s:s;return this._inferAndBuild("layer_norm",[e,t,r],{axis:i,epsilon:o})}batchnorm(e,t,r,s,o,i=1,a=1e-5){return this._inferAndBuild("batch_norm",[e,t,r,s,o],{axis:i,epsilon:a})}where(e,t,r){return this._inferAndBuild("where",[e,t,r])}split(e,t,r){return this._inferAndBuild("split",[e],{dimension:t,split_sizes:r})}oneHot(e,t,r={}){return this._inferAndBuild("one_hot",[e],{depth:t,axis:r.axis??-1,on_value:r.onValue??1,off_value:r.offValue??0,dtype:r.dtype||e.type.dtype})}embedding(e,t){return this._inferAndBuild("embedding",[e,t])}gather(e,t,r){return this._inferAndBuild("gather",[e,t],{offset_dims:r.offsetDims,collapsed_slice_dims:r.collapsedSliceDims,start_index_map:r.startIndexMap,index_vector_dim:r.indexVectorDim,slice_sizes:r.sliceSizes})}scatter(e,t,r,s){let o=new I([],e.type.dtype),i=new Ut;return i.addBlock(new On([o,o])),this._inferAndBuild("scatter",[e,t,r],{update_window_dims:s.updateWindowDims,inserted_window_dims:s.insertedWindowDims,scatter_dims_to_operand_dims:s.scatterDimsToOperandDims,index_vector_dim:s.indexVectorDim},[i])}scatterAdd(e,t,r,s){return this.scatter(e,t,r,s)}_dimCoordIndices(e,t,r){let s=r.length,o=e.type.dtype==="i32"?e:this.convert(e,"i32").getResult(0),i=[...r,1],a=[];for(let u=0;u<s;u++){let p=u===t?o:this.iota(u,new I(r,"i32")).getResult(0);a.push(this.reshape(p,i).getResult(0))}return this.concat(a,s).getResult(0)}gatherDim(e,t,r){let s=e.type.rank,o=r<0?s+r:r,i=[],a=[];for(let p=0;p<s;p++)i.push(p),a.push(1);let u=this._dimCoordIndices(t,o,t.type.shape);return this.gather(e,u,{offsetDims:[],collapsedSliceDims:i,startIndexMap:i,indexVectorDim:s,sliceSizes:a})}scatterAddDim(e,t,r,s){let o=e.type.rank,i=s<0?o+s:s,a=[];for(let p=0;p<o;p++)a.push(p);let u=this._dimCoordIndices(t,i,t.type.shape);return this.scatterAdd(e,u,r,{updateWindowDims:[],insertedWindowDims:a,scatterDimsToOperandDims:a,indexVectorDim:o})}argmax(e,t,r=!1){let s=t<0?e.type.rank+t:t;return this._inferAndBuild("argmax",[e],{axis:s,keep_dims:r})}argmin(e,t,r=!1){let s=t<0?e.type.rank+t:t;return this._inferAndBuild("argmin",[e],{axis:s,keep_dims:r})}pool2d(e,t,r,s,o,i={}){return this._inferAndBuild("pool2d",[e],{pool_type:t,kernel_size:r,strides:s,padding:o,ceil_mode:i.ceilMode||!1,count_include_pad:i.countIncludePad||!1,layout:i.layout||"NCHW"})}resize(e,t,r,s={}){return this._inferAndBuild("resize",[e],{output_size:t,method:r,coordinate_mode:s.coordinateMode||"asymmetric",layout:s.layout||"NCHW"})}};l(Xo,"broadcastDimsExcluding");l($P,"toMap");l(mp,"buildFunction")});function Un(n,e,t){if(t==null)return new P(n,e,t);let r=e instanceof L,s=t instanceof L;if(r&&s){let o=e.value,i=t.value;switch(n){case"+":return new L(o+i);case"-":return new L(o-i);case"*":return new L(o*i);case"//":if(i!==0)return new L(Math.trunc(o/i));break;case"%":if(i!==0)return new L((o%i+i)%i);break}}if(s){let o=t.value;if((n==="+"||n==="-")&&o===0||n==="*"&&o===1)return e;if(n==="*"&&o===0)return new L(0);if(n==="//"&&o===1)return e;if(n==="%"&&o===1)return new L(0)}if(r){let o=e.value;if(n==="+"&&o===0||n==="*"&&o===1)return t;if(n==="*"&&o===0)return new L(0)}return new P(n,e,t)}var z,Bt,Pi,he,fe,B0,Pa,J,q,Se,et,Ue,te,$i,il,Fn,al,Ne,P,Ke,ut,Oe,L,de,Fe=M(()=>{"use strict";z=Object.freeze({SERIAL:"serial",PARALLEL:"parallel",VECTORIZED:"vectorized",UNROLLED:"unrolled",THREAD_BINDING:"thread_binding",RECURRENCE:"recurrence"}),Bt=class n{static{l(this,"TensorNode")}constructor(){this.type=this.constructor.name.replace(/^_+/,""),this._parent=null,this._parentKey=null,this._parentIdx=-1}_setChild(e,t,r=-1){t instanceof n&&(t._parent=this,t._parentKey=e,t._parentIdx=r)}_setChildren(e,t){if(t)for(let r=0;r<t.length;r++)t[r]instanceof n&&(t[r]._parent=this,t[r]._parentKey=e,t[r]._parentIdx=r)}replaceWith(e){let t=this._parent;return t?(this._parentIdx>=0?t[this._parentKey][this._parentIdx]=e:t[this._parentKey]=e,e instanceof n&&(e._parent=t,e._parentKey=this._parentKey,e._parentIdx=this._parentIdx),this._parent=null,this._parentKey=null,this._parentIdx=-1,!0):!1}},Pi=class extends Bt{static{l(this,"PrimFunc")}constructor(e,t,r,s=new Map,o=[],i=null){super(),this.name=e,this.params=t,this.body=r,this.bufferMap=s,this.shapeParams=o,this.shapeParamMap=i||new Map,this._setChild("body",r)}},he=class extends Bt{static{l(this,"ForNode")}constructor(e,t,r,s,o,i=null){super(),this.loopVar=e,this.min=t,this.extent=r,this.kind=s,this.body=o,this.threadTag=i,this._setChild("body",o)}},fe=class extends Bt{static{l(this,"BlockNode")}constructor(e,t,r,s,o,i=null){super(),this.name=e,this.iterVars=t,this.reads=r,this.writes=s,this.body=o,this.initBody=i,this._setChild("body",o),this._setChild("initBody",i)}},B0=Object.freeze({DATA_PAR:"DataPar",COMM_REDUCE:"CommReduce"}),Pa=class extends Bt{static{l(this,"BlockRealizeNode")}constructor(e,t,r=B0.DATA_PAR){super(),this.iterVar=e,this.binding=t,this.kind=r}},J=class extends Bt{static{l(this,"BufferStoreNode")}constructor(e,t,r){super(),this.buffer=e,this.indices=t,this.value=r}},q=class extends Bt{static{l(this,"BufferLoadNode")}constructor(e,t){super(),this.buffer=e,this.indices=t}},Se=class extends Bt{static{l(this,"IfThenElseNode")}constructor(e,t,r=null){super(),this.condition=e,this.thenBody=t,this.elseBody=r,this._setChild("thenBody",t),this._setChild("elseBody",r)}},et=class extends Bt{static{l(this,"LetStmtNode")}constructor(e,t,r){super(),this.variable=e,this.value=t,this.body=r,this._setChild("body",r)}},Ue=class extends Bt{static{l(this,"AllocateNode")}constructor(e,t,r){super(),this.buffer=e,this.scope=t,this.body=r,this._setChild("body",r)}},te=class extends Bt{static{l(this,"SeqNode")}constructor(e){super(),this.stmts=e,this._setChildren("stmts",e)}},$i=class extends Bt{static{l(this,"WhileNode")}constructor(e,t,r){super(),this.condVar=e,this.condBody=t,this.loopBody=r,this._setChild("condBody",t),this._setChild("loopBody",r)}},il=class extends Bt{static{l(this,"EvaluateNode")}constructor(e){super(),this.value=e}},Fn=class extends Bt{static{l(this,"SyncThreadsNode")}constructor(){super()}},al=class extends Bt{static{l(this,"VecCopyNode")}constructor(e,t,r,s,o){super(),this.dstBuffer=e,this.dstIndex=t,this.srcBuffer=r,this.srcIndex=s,this.width=o}},Ne=class extends Bt{static{l(this,"CallExternNode")}constructor(e,t,r){super(),this.externName=e,this.args=t,this.dtype=r}},P=class extends Bt{static{l(this,"MathOpNode")}constructor(e,t,r=null){super(),this.op=e,this.a=t,this.b=r}},Ke=class extends Bt{static{l(this,"CompareNode")}constructor(e,t,r){super(),this.direction=e,this.a=t,this.b=r}},ut=class extends Bt{static{l(this,"CastNode")}constructor(e,t,r){super(),this.expr=e,this.fromDtype=t,this.toDtype=r}},Oe=class extends Bt{static{l(this,"VariableNode")}constructor(e,t){super(),this.name=e,this.dtype=t}},L=class extends Bt{static{l(this,"IntImmNode")}constructor(e){super(),this.value=e}},de=class extends Bt{static{l(this,"FloatImmNode")}constructor(e){super(),this.value=e}};l(Un,"mathOp")});var ul,R0=M(()=>{"use strict";ul=Object.freeze({GLOBAL:"global",SHARED:"shared",LOCAL:"local",REGISTER:"register"})});var me,ei=M(()=>{"use strict";tr();Re();me=class{static{l(this,"Buffer")}constructor(e,t,r,s,o=null,i=0,a=64){if(this.name=e,this.shape=t,this.dtype=r,this.scope=s,this.offset=i,this.alignment=a,this.broadcastDims=null,o)this.strides=o;else{this.strides=new Array(t.length);let u=1;for(let p=t.length-1;p>=0;p--)this.strides[p]=u,u!==X&&(typeof t[p]=="number"?u*=t[p]:u=X)}}get rank(){return this.shape.length}get isScalar(){return this.shape.length===0}numel(){return ap(this.shape,-1)}symbolicNumel(){return f0(this.shape)}sizeInBytes(){let e=this.numel();return e<0?-1:e*Rn(this.dtype)}}});function FP(n,e,t){switch(n){case"add":return new P("+",e,t);case"sub":return new P("-",e,t);case"mul":return new P("*",e,t);case"div":return new P("//",e,t);case"mod":return new P("%",e,t);case"neg":return new P("-",e);case"ceildiv":return new P("//",new P("-",new P("+",e,t),new L(1)),t);default:throw new Error(`symIntToNode: unsupported op '${n}' in extent/index context`)}}function ll(n,e){if(typeof n=="number")return new L(n);if(!(n instanceof Pe))return new L(n);if(n.type==="var")return e(n.name);let t=ll(n.args[0],e),r=n.args.length>1?ll(n.args[1],e):null;return FP(n.type,t,r)}var M0=M(()=>{"use strict";po();Fe();l(FP,"symOpToNode");l(ll,"symIntToNode")});function _n(n){let e=ne.get(n);return e!==null&&e.isConstant}function Xt(n){let e=ne.get(n);return e!==null&&e.isTerminator}function cl(n){let e=ne.get(n);return e!==null&&e.isBroadcast}var Ir=M(()=>{"use strict";Ct();l(_n,"isConstantOp");l(Xt,"isTerminatorOp");l(cl,"isBroadcastOp")});function fm(n,{name:e,compute:t,plevel:r=10,targetKind:s=null}){let o=I0.get(n);return o||(o=new gp(n),I0.set(n,o)),o.addImplementation(e,t,r,s),cm.clear(),o}function dm(n,e=null){let t=I0.get(n);if(!t)return null;let r=e?e.kind:null,s=`${n}|${r===null?"":r}`;if(cm.has(s))return cm.get(s);let o=new gp(n);for(let a of t.implementations)(a.targetKind===null||a.targetKind===r)&&o.implementations.push(a);let i=o.implementations.length>0?o:null;return cm.set(s,i),i}function E0(n,e=null){let t=dm(n,e);return t?t.best():null}var pm,gp,I0,cm,L0=M(()=>{"use strict";pm=class{static{l(this,"OpImplementation")}constructor(e,t,r=10,s=null){this.name=e,this.compute=t,this.plevel=r,this.targetKind=s}},gp=class{static{l(this,"OpStrategy")}constructor(e){this.opName=e,this.implementations=[]}addImplementation(e,t,r=10,s=null){let o=new pm(e,t,r,s),i=this.implementations.findIndex(a=>a.name===e);return i>=0?this.implementations[i]=o:this.implementations.push(o),this}get candidates(){return this.implementations}best(){let e=null;for(let t of this.implementations)(e===null||t.plevel>e.plevel)&&(e=t);return e}},I0=new Map,cm=new Map;l(fm,"registerOpStrategy");l(dm,"getOpStrategy");l(E0,"selectImplementation")});function D0(n,e){return dm(n,e)?!0:_n(n)}function Ie(n,e,t=zP){fm(n,{name:`${n}.generic`,compute:e,plevel:t,targetKind:null})}function vA(n,e,t,r=VP){fm(n,{name:`${n}.${e}`,compute:t,plevel:r,targetKind:e})}function Fi(n,e,t=null){if(t){let s=t.getLoweringRule(n);if(s)return s}let r=E0(n,e);return r?r.compute:void 0}function Rt(n,e,t){let r=e.length,s=n.allocVarArray("i",r),o=n.allocBindArray("v",s),i=new Array(r);for(let u=0;u<r;u++)i[u]=o[u].iterVar;let a=t?n.extentNodes(e,t):null;return{loopVars:s,loopBinds:o,indices:i,extentNodes:a}}function _p(n,e,t){let r=n;for(let s=e.length-1;s>=0;s--)r=new he(e[s],new L(0),t[s],z.SERIAL,r);return r}function GP(n,e,t){let r=n;for(let s=e.length-1;s>=0;s--)r=new he(e[s],new L(0),new L(t[s]),z.SERIAL,r);return r}function Mt(n,e,t,r){return r?_p(n,e,r):GP(n,e,t)}function bp(n,e,t,r,s,{prefix:o,initBlockName:i,accBlockName:a,initVal:u,accLeaf:p}){let c=jP(n,e,t,r),f=_r(n,o,Array.from({length:s.shape.length},(A,N)=>N),s.shape,s),d=new J(s,f.indices,u()),h=new fe(n.blockName(i),f.ivs,[],[{buffer:s}],d),m=f.wrap(h),g=new q(t,c.lhsIdx),y=new q(r,c.rhsIdx),_=e.getAttr("lhs_prologue_cast"),b=e.getAttr("rhs_prologue_cast");_&&(g=new ut(g,t.dtype,_)),b&&(y=new ut(y,r.dtype,b));let w=p(g,y),x=new P("+",new q(s,c.outIdx),w),S=new J(s,c.outIdx,x),T=new fe(n.blockName(a),c.allIvs,[{buffer:t},{buffer:r}],[{buffer:s}],S),v=c.wrapAccBody(T);return{geo:c,initBody:m,accBody:v}}function gm(n,e,t,r,s,{prefix:o,blockPrefix:i,initVal:a,guardFill:u,leafBuilder:p}){let c=e.getAttr("strides"),f=e.getAttr("padding"),d=e.getAttr("dilation")||c.map(()=>1),h=e.getAttr("groups")||1,m=mm(e.getAttr("input_layout")),g=mm(e.getAttr("kernel_layout")),y=c.length,_=t.shape[m.N],b=r.shape[g.O],w=r.shape[g.I],x=s.shape,S=_r(n,o+"i",Array.from({length:x.length},(ye,Xe)=>Xe),x,s),T=new J(s,S.indices,a()),v=new fe(n.blockName(i+"_init"),S.ivs,[],[{buffer:s}],T),A=S.wrap(v),N=n.allocVar(o+"n"),k=n.allocVar(o+"oc"),O=n.allocVar(o+"ic"),R=n.allocVarArray(o+"o",y),B=n.allocVarArray(o+"k",y),E=[N,k,...R,O,...B],F=n.allocBindArray(o+"v",E),C=F[0].iterVar,H=F[1].iterVar,G=F.slice(2,2+y),U=F[2+y].iterVar,D=F.slice(3+y),V=new Array(x.length);V[m.N]=C,V[m.C]=H;let ue=Object.keys(m).filter(ye=>ye!=="N"&&ye!=="C").sort();for(let ye=0;ye<y;ye++)V[m[ue[ye]]]=G[ye].iterVar;let ce=new Array(t.shape.length);ce[m.N]=C;let Le=Math.floor(b/h);h>1?ce[m.C]=new P("+",new P("*",new P("//",H,new L(Le)),new L(w)),U):ce[m.C]=U;let j=new Array(r.shape.length);j[g.O]=H,j[g.I]=U;let W=null;for(let ye=0;ye<y;ye++){let Xe=ue[ye],Nn=Xe.toUpperCase(),Qt=Un("+",Un("*",G[ye].iterVar,new L(c[ye])),Un("+",Un("*",D[ye].iterVar,new L(d[ye])),new L(-f[ye][0])));if(ce[m[Xe]]=Qt,j[g[Nn]]=D[ye].iterVar,f[ye][0]!==0||f[ye][1]!==0){let an=new Ke("ge",Qt,new L(0)),un=new Ke("lt",Qt,new L(t.shape[m[Xe]])),mn=new P("*",an,un);W=W?new P("*",W,mn):mn}}let ie=p(ce,j),je=W?new Se(W,ie,u()):ie,ke=new q(s,V),rt=new P("+",ke,je),zt=new J(s,V,rt),on=new fe(n.blockName(i+"_acc"),F,[{buffer:t},{buffer:r}],[{buffer:s}],zt),Zt=new Array(y);for(let ye=0;ye<y;ye++){let Xe=ue[ye].toUpperCase();Zt[ye]=r.shape[g[Xe]]}let it=on;for(let ye=y-1;ye>=0;ye--){let Xe=ue[ye].toUpperCase();it=new he(B[ye],new L(0),n.extentNode(Zt[ye],r,g[Xe]),z.SERIAL,it)}it=new he(O,new L(0),n.extentNode(w,r,g.I),z.SERIAL,it);for(let ye=y-1;ye>=0;ye--){let Xe=m[ue[ye]];it=new he(R[ye],new L(0),n.extentNode(x[Xe],s,Xe),z.SERIAL,it)}return it=new he(k,new L(0),n.extentNode(b,r,g.O),z.SERIAL,it),it=new he(N,new L(0),n.extentNode(_,t,m.N),z.SERIAL,it),new te([A,it])}function _r(n,e,t,r,s){let o=t.length,i=new Array(o),a=new Array(o),u=new Array(o),p=new Array(o);for(let c=0;c<o;c++)i[c]=n.allocVar(`${e}${t[c]}`),a[c]=new Pa(n.allocVar(`${e}v${t[c]}`),i[c]),u[c]=a[c].iterVar,p[c]=n.extentNode(r[t[c]],s,t[c]);return{vars:i,ivs:a,indices:u,extentNodes:p,wrap(c){return _p(c,i,p)}}}function ti(n,e,t){let r=n.shape.length;if(n.broadcastDims){let a=n.broadcastDims,u=new Array(r);for(let p=0;p<r;p++)u[p]=n.shape[p]===1?new L(0):t[a[p]];return u}let o=e.shape.length-r,i=new Array(r);for(let a=0;a<r;a++)i[a]=n.shape[a]===1?new L(0):t[o+a];return i}function pl(n){let e=new Array(n.length);for(let t=0;t<n.length;t++)e[t]={buffer:n[t]};return e}function yp(){let n=0;for(let r=0;r<arguments.length;r++)n+=arguments[r].length;let e=new Array(n),t=0;for(let r=0;r<arguments.length;r++){let s=arguments[r];for(let o=0;o<s.length;o++)e[t++]=s[o]}return e}function C0(n){let e=new Array(n.length);for(let t=0;t<n.length;t++)e[t]=n[t].iterVar;return e}function mo(n,e,t,r,s){let o=r[0],{loopVars:i,loopBinds:a,indices:u,extentNodes:p}=Rt(n,o.shape,o),c=new Array(t.length);for(let m=0;m<t.length;m++){let g=ti(t[m],o,u);c[m]=new q(t[m],g)}let f=s(e,c,o.dtype),d=new J(o,u,f),h=new fe(n.blockName(`${e.opName}_block`),a,pl(t),[{buffer:o}],d);return Mt(h,i,o.shape,p)}function fl(n,e){let t=e.getResult(0),r=e.getAttr("value"),s=t.type&&t.type.shape||[];if(typeof r=="number"&&s.length>0&&!n.bufferMap.has(t)){let g=t.type&&t.type.dtype||"f32",y=new Array(s.length).fill(1),_=new me(`buf_${n.varCounter++}`,y,g,ul.GLOBAL);_.broadcastDims=Array.from({length:s.length},(w,x)=>x),n.bufferMap.set(t,_);let b=mr(g)?new L(r):new de(r);return new J(_,y.map(()=>new L(0)),b)}let o=n.getOrAllocBuffer(t),i=mr(o.dtype),a=l(g=>i?new L(g):new de(g),"imm");if(r&&typeof r!="number"&&typeof r.length=="number"){if(o.shape.length===0)return new J(o,[],a(r[0]));let g=new Array(o.shape.length),y=1;for(let b=o.shape.length-1;b>=0;b--)g[b]=y,y*=o.shape[b];let _=[];for(let b=0;b<r.length;b++){let w=new Array(o.shape.length);for(let x=0;x<o.shape.length;x++)w[x]=new L(Math.floor(b/g[x])%o.shape[x]);_.push(new J(o,w,a(r[b])))}return new te(_)}let u=a(typeof r=="number"?r:0);if(o.shape.length===0)return new J(o,[],u);let{loopVars:p,loopBinds:c,indices:f,extentNodes:d}=Rt(n,o.shape,o),h=new J(o,f,u),m=new fe(n.blockName(`${e.opName}_block`),c,[],[{buffer:o}],h);return Mt(m,p,o.shape,d)}function mm(n){let e={};for(let t=0;t<n.length;t++)e[n[t]]=t;return e}function SA(n,e){if(!n.broadcastDims)return e;let t=n.broadcastDims,r=new Array(n.shape.length);for(let s=0;s<n.shape.length;s++)r[s]=n.shape[s]===1?new L(0):e[t[s]];return r}function jP(n,e,t,r){let s=e.getAttr("lhs_contracting")||[],o=e.getAttr("rhs_contracting")||[],i=e.getAttr("lhs_batch")||[],a=e.getAttr("rhs_batch")||[],u=e.getOperand(0).type.shape,p=e.getOperand(1).type.shape,c=new Set(s),f=new Set(i),d=new Set(o),h=new Set(a),m=[];for(let C=0;C<u.length;C++)!c.has(C)&&!f.has(C)&&m.push(C);let g=[];for(let C=0;C<p.length;C++)!d.has(C)&&!h.has(C)&&g.push(C);let y=n.allocVarArray("b",i.length),_=n.allocVarArray("ls",m.length),b=n.allocVarArray("rs",g.length),w=n.allocVarArray("c",s.length),x=n.allocBindArray("vb",y),S=n.allocBindArray("vls",_),T=n.allocBindArray("vrs",b),v=n.allocBindArray("vc",w),A=yp(C0(x),C0(S),C0(T)),N=new Array(u.length);for(let C=0;C<i.length;C++)N[i[C]]=x[C].iterVar;for(let C=0;C<m.length;C++)N[m[C]]=S[C].iterVar;for(let C=0;C<s.length;C++)N[s[C]]=v[C].iterVar;let k=new Array(p.length);for(let C=0;C<a.length;C++)k[a[C]]=x[C].iterVar;for(let C=0;C<g.length;C++)k[g[C]]=T[C].iterVar;for(let C=0;C<o.length;C++)k[o[C]]=v[C].iterVar;let O=SA(t,N),R=SA(r,k),B=yp(x,S,T,v),E=[{vars:y,dims:i,shape:u,buf:t},{vars:_,dims:m,shape:u,buf:t},{vars:b,dims:g,shape:p,buf:r},{vars:w,dims:s,shape:u,buf:t}];function F(C){let H=C;for(let G=E.length-1;G>=0;G--){let{vars:U,dims:D,shape:V,buf:ue}=E[G];for(let ce=U.length-1;ce>=0;ce--){let Le=ue.broadcastDims?new L(V[D[ce]]):n.extentNode(V[D[ce]],ue,D[ce]);H=new he(U[ce],new L(0),Le,z.SERIAL,H)}}return H}return l(F,"wrapAccBody"),{outIdx:A,lhsIdx:O,rhsIdx:R,allIvs:B,wrapAccBody:F}}var zP,VP,hm,Qr=M(()=>{"use strict";Re();po();R0();ei();tr();Fe();M0();Ir();L0();L0();zP=10,VP=20;l(D0,"hasLoweringRule");l(Ie,"registerLoweringRule");l(vA,"registerTargetLoweringRule");l(Fi,"getLoweringRule");hm=class{static{l(this,"LoweringContext")}constructor(){this.bufferMap=new Map,this.varCounter=0,this.shapeParams=new Map,this.symbolToVar=new Map,this.symVars=new Map,this._blockCounter=0}blockName(e){return`${e}_${this._blockCounter++}`}allocVar(e,t="int32"){return new Oe(`${e}_${this.varCounter++}`,t)}getOrAllocBuffer(e){let t=this.bufferMap.get(e);if(t)return t;let r=e.type,s=r.shape||[],o=r.dtype||"f32",i=r.layout?r.layout.computeStrides(s):null;return t=new me(`buf_${this.varCounter++}`,s,o,ul.GLOBAL,i),e.symbolicShape&&(t.symbolicShape=e.symbolicShape),this.bufferMap.set(e,t),this._registerDynamicDims(t),t}allocFreshBuffer(e){let t=e.type,r=t.shape||[],s=t.dtype||"f32",o=t.layout?t.layout.computeStrides(r):null,i=new me(`buf_${this.varCounter++}`,r,s,ul.GLOBAL,o);return e.symbolicShape&&(i.symbolicShape=e.symbolicShape),this._registerDynamicDims(i),i}_registerDynamicDims(e){for(let t=0;t<e.shape.length;t++){let r=e.shape[t];r===X?this.extentNode(X,e,t):r instanceof Pe&&this._registerSymIntDim(e,t,r)}}_symVarNode(e){let t=this.symVars.get(e);return t||(t=new Oe(qu(e),"int32"),this.symVars.set(e,t)),t}_registerSymIntDim(e,t,r){for(let s of Pe.freeVars(r))this._symVarNode(s);if(r.type==="var"){let s=`${e.name}:${t}`;this.shapeParams.has(s)||this.shapeParams.set(s,this._symVarNode(r.name))}}symIntToExtentNode(e){return ll(e,t=>this._symVarNode(t))}_shapeParamVar(e,t){let r=t>=0?`${e.name}:${t}`:`${e.name}:dyn`,s=this.shapeParams.get(r);if(s)return s;let o=e.symbolicShape&&t>=0&&typeof e.symbolicShape[t]!="number"?e.symbolicShape[t]:null;return o!==null&&this.symbolToVar.has(o)?s=this.symbolToVar.get(o):(s=this.allocVar("_ds"),o!==null&&this.symbolToVar.set(o,s)),this.shapeParams.set(r,s),s}extentNode(e,t,r=-1){return e instanceof Pe?this.symIntToExtentNode(e):e!==X?new L(e):this._shapeParamVar(t,r)}extentNodes(e,t){let r=new Array(e.length);for(let s=0;s<e.length;s++)r[s]=this.extentNode(e[s],t,s);return r}allocVarArray(e,t){let r=new Array(t);for(let s=0;s<t;s++)r[s]=this.allocVar(`${e}${s}`);return r}allocBindArray(e,t){let r=new Array(t.length);for(let s=0;s<t.length;s++)r[s]=new Pa(this.allocVar(`${e}${s}`),t[s]);return r}};l(Rt,"makeLoopNest");l(_p,"wrapLoopsWithNodes");l(GP,"wrapLoops");l(Mt,"wrapInLoops");l(bp,"emitMatmulInitAcc");l(gm,"buildConvNest");l(_r,"buildSpatialNest");l(ti,"computeBroadcastIndices");l(pl,"bufRefs");l(yp,"concatIterVars");l(C0,"extractIterVars");l(mo,"lowerPointwise");l(fl,"lowerConstant");l(mm,"parseLayout");l(SA,"physicalDotIndices");l(jP,"buildDotGeometry")});function HP(n){let e=ne.get(n);return e?e.getAttr(P0):null}function wp(){let n=[];for(let e of ne.allOps())e.hasAttr(P0)&&n.push(e.name);return n}function $0(n,e,t){let r=HP(n);return r?kA[n]?kA[n](e):e.length===2&&UP.has(r)?new P(r,e[0],e[1]):e.length===2&&qP.has(r)?new P(r,e[0],e[1]):e.length===1&&WP.has(r)?new P(r,e[0]):e.length===1&&r==="-"?new P("-",e[0]):new Ne(r,e,t):null}function NA(){for(let n of wp())Ie(n,(e,t,r,s)=>mo(e,t,r,s,(o,i,a)=>$0(o.opName,i,a)));Ie("compare",(n,e,t,r)=>mo(n,e,t,r,(s,o)=>new Ke(s.getAttr("direction")||"eq",o[0],o[1]))),Ie("select",(n,e,t,r)=>mo(n,e,t,r,(s,o)=>new Se(o[0],o[1],o[2]))),Ie("clamp",(n,e,t,r)=>mo(n,e,t,r,(s,o,i)=>new Ne("min",[new Ne("max",[o[1],o[0]],i),o[2]],i))),Ie("convert",(n,e,t,r)=>mo(n,e,t,r,(s,o)=>new ut(o[0],t[0].dtype,r[0].dtype))),Ie("copy_to_device",(n,e,t,r)=>mo(n,e,t,r,(s,o)=>o[0]))}var UP,qP,WP,kA,P0,KP,ym=M(()=>{"use strict";Fe();Ct();Qr();UP=new Set(["+","-","*","/"]),qP=new Set(["&&","||"]),WP=new Set(["!"]),kA={square:l(n=>new P("*",n[0],n[0]),"square"),reciprocal:l(n=>new P("/",new de(1),n[0]),"reciprocal")},P0="elementwiseScalarOp",KP={add:"+",sub:"-",mul:"*",div:"/",max:"max",min:"min",exp:"exp",log:"log",sqrt:"sqrt",rsqrt:"rsqrt",tanh:"tanh",abs:"abs",ceil:"ceil",floor:"floor",neg:"-",maximum:"max",minimum:"min",sin:"sin",cos:"cos",round:"round",sign:"sign",pow:"pow",rem:"fmod",erf:"erf",erfc:"erfc",lgamma:"lgamma",gamma:"gamma",log2:"log2",log10:"log10",exp2:"exp2",square:"square",reciprocal:"reciprocal",logical_not:"!",logical_and:"&&",logical_or:"||"};for(let[n,e]of Object.entries(KP))ne.has(n)&&ne.registerOpAttr(n,P0,e);l(HP,"elementwiseScalarOp");l(wp,"elementwiseOpNames");l($0,"buildElementwiseExpr");l(NA,"register")});function OA(){Ie("broadcast_in_dim",AA),Ie("broadcast",AA),Ie("transpose",(n,e,t,r)=>{let s=e.getAttr("permutation"),o=t[0],i=r[0],{loopVars:a,loopBinds:u,indices:p,extentNodes:c}=Rt(n,i.shape,i),f=new Array(o.shape.length);for(let m=0;m<s.length;m++)f[s[m]]=p[m];let d=new J(i,p,new q(o,f)),h=new fe(n.blockName("transpose_block"),u,[{buffer:o}],[{buffer:i}],d);return Mt(h,a,i.shape,c)}),Ie("reverse",(n,e,t,r)=>{let s=new Set(e.getAttr("dimensions")),o=t[0],i=r[0],{loopVars:a,loopBinds:u,indices:p,extentNodes:c}=Rt(n,i.shape,i),f=new Array(o.shape.length);for(let m=0;m<o.shape.length;m++)f[m]=s.has(m)?new P("-",new P("-",c[m],new L(1)),p[m]):p[m];let d=new J(i,p,new q(o,f)),h=new fe(n.blockName("reverse_block"),u,[{buffer:o}],[{buffer:i}],d);return Mt(h,a,i.shape,c)}),Ie("reshape",(n,e,t,r)=>{let s=t[0],o=r[0],{loopVars:i,loopBinds:a,indices:u,extentNodes:p}=Rt(n,o.shape,o),c;if(s.shape.length===o.shape.length&&s.shape.every((m,g)=>m===o.shape[g]))c=u;else{for(let _=1;_<s.shape.length;_++)if(typeof s.shape[_]!="number"||s.shape[_]<0)throw new Error("reshape lowering requires static non-leading input dims");for(let _=1;_<o.shape.length;_++)if(typeof o.shape[_]!="number"||o.shape[_]<0)throw new Error("reshape lowering requires static non-leading output dims");let m=u[o.shape.length-1],g=1;for(let _=o.shape.length-2;_>=0;_--)g*=o.shape[_+1],m=Un("+",m,Un("*",u[_],new L(g)));c=new Array(s.shape.length);let y=m;for(let _=s.shape.length-1;_>=0;_--)_===0?c[_]=y:(c[_]=Un("%",y,new L(s.shape[_])),y=Un("//",y,new L(s.shape[_])))}let d=new J(o,u,new q(s,c)),h=new fe(n.blockName("reshape_block"),a,[{buffer:s}],[{buffer:o}],d);return Mt(h,i,o.shape,p)}),Ie("slice",(n,e,t,r)=>{let s=t[0],o=r[0],i=e.getAttr("starts"),a=e.getAttr("strides")||i.map(()=>1),{loopVars:u,loopBinds:p,indices:c,extentNodes:f}=Rt(n,o.shape,o),d=new Array(s.shape.length);for(let g=0;g<s.shape.length;g++){let y=new L(i[g]);a[g]===1?d[g]=new P("+",y,c[g]):d[g]=new P("+",y,new P("*",c[g],new L(a[g])))}let h=new J(o,c,new q(s,d)),m=new fe(n.blockName("slice_block"),p,[{buffer:s}],[{buffer:o}],h);return Mt(m,u,o.shape,f)}),Ie("pad",(n,e,t,r)=>{let s=t[0],o=t[1],i=r[0],a=e.getAttr("low"),u=e.getAttr("interior")||a.map(()=>0),{loopVars:p,loopBinds:c,indices:f,extentNodes:d}=Rt(n,i.shape,i),h=new Array(s.shape.length),m=new L(1);for(let x=0;x<s.shape.length;x++){let S=new P("+",f[x],new L(-a[x]));if(u[x]>0){let A=u[x]+1,N=new P("%",S,new L(A)),k=new Ke("eq",N,new L(0));m=new P("*",m,k),h[x]=new P("//",S,new L(A))}else h[x]=S;let T=new Ke("ge",h[x],new L(0)),v=new Ke("lt",h[x],new L(s.shape[x]));m=new P("*",m,new P("*",T,v))}let g=new q(s,h),y=new q(o,[]),_=new Se(m,g,y),b=new J(i,f,_),w=new fe(n.blockName("pad_block"),c,[{buffer:s},{buffer:o}],[{buffer:i}],b);return Mt(w,p,i.shape,d)}),Ie("concat",(n,e,t,r)=>{let s=r[0],o=e.getAttr("dimension"),i=[],a=0;for(let u=0;u<t.length;u++){let p=t[u],{loopVars:c,loopBinds:f,indices:d,extentNodes:h}=Rt(n,p.shape,p),m=new Array(p.shape.length);for(let _=0;_<p.shape.length;_++)m[_]=_===o&&a>0?new P("+",d[_],new L(a)):d[_];let g=new J(s,m,new q(p,d)),y=new fe(n.blockName("concat"),f,[{buffer:p}],[{buffer:s}],g);i.push(Mt(y,c,p.shape,h)),a+=p.shape[o]}return i.length===1?i[0]:new te(i)}),Ie("iota",(n,e,t,r)=>{let s=r[0],o=e.getAttr("iota_dimension"),{loopVars:i,loopBinds:a,indices:u,extentNodes:p}=Rt(n,s.shape,s),c=new ut(u[o],"index",s.dtype),f=new J(s,u,c),d=new fe(n.blockName("iota_block"),a,[],[{buffer:s}],f);return Mt(d,i,s.shape,p)})}function AA(n,e,t,r){let s=t[0],o=r[0],i=e.getAttr("broadcast_dimensions")||[],{loopVars:a,loopBinds:u,indices:p,extentNodes:c}=Rt(n,o.shape,o),f=new Array(s.shape.length);for(let m=0;m<s.shape.length;m++){let g=i.length>0?i[m]:m+(o.shape.length-s.shape.length);f[m]=s.shape[m]===1?new L(0):p[g]}let d=new J(o,p,new q(s,f)),h=new fe(n.blockName("broadcast_block"),u,[{buffer:s}],[{buffer:o}],d);return Mt(h,a,o.shape,c)}var BA=M(()=>{"use strict";Fe();Qr();l(OA,"register");l(AA,"lowerBroadcast")});function ZP(n,e){if(mr(n)){let t=e?YP[n]??-2147483648:JP[n]??2147483647;return new L(t)}return new de(e?-1/0:1/0)}function RA(){Ie("reduce",(e,t,r,s)=>{let o=r[0],i=r[1],a=s[0],u=t.getAttr("dimensions")||[],p=t.getAttr("reduce_type")||"sum",c=new Set(u),f=[],d=[];for(let B=0;B<o.shape.length;B++)(c.has(B)?d:f).push(B);let h=_r(e,"si",f,o.shape,o),m=new J(a,h.indices,new q(i,[])),g=new fe(e.blockName("reduce_init"),h.ivs,[{buffer:i}],[{buffer:a}],m),y=f.length>0?h.wrap(g):g,_=_r(e,"sa",f,o.shape,o),b=e.allocVarArray("r",d.length),w=e.allocBindArray("rv",b);for(let B of w)B.kind=B0.COMM_REDUCE;let x=new Array(o.shape.length);for(let B=0;B<f.length;B++)x[f[B]]=_.ivs[B].iterVar;for(let B=0;B<d.length;B++)x[d[B]]=w[B].iterVar;let S=new q(a,_.indices),T=new q(o,x),v=QP[p];if(!v)throw new Error(`reduction lowering: unsupported reduce_type '${p}'`);let A=new J(a,_.indices,v(S,T,a.dtype)),N=new Array(d.length);for(let B=0;B<d.length;B++)N[B]=e.extentNode(o.shape[d[B]],o,d[B]);let k=new fe(e.blockName("reduce_acc"),yp(_.ivs,w),[{buffer:o}],[{buffer:a}],A),O=_p(k,b,N);O=_.wrap(O);let R=[y,O];if(p==="mean"){let B=1,E=[];for(let D=0;D<d.length;D++){let V=o.shape[d[D]];V===X?E.push(e.extentNode(X,o,d[D])):B*=V}let F=_r(e,"sm",f,o.shape,o),C=new q(a,F.indices),H;if(E.length===0)H=new P("*",C,new de(1/B));else{let D=new L(B);for(let V of E)D=new P("*",D,V);H=new P("/",C,D)}let G=new J(a,F.indices,H),U=new fe(e.blockName("mean_div"),F.ivs,[{buffer:a}],[{buffer:a}],G);R.push(f.length>0?F.wrap(U):U)}return new te(R)});function n(e,t){Ie(e,(r,s,o,i)=>{let a=o[0],u=i[0],p=s.getAttr("axis"),c=s.getAttr("keep_dims")||!1,f=new Set([p]),d=[],h=p;for(let V=0;V<a.shape.length;V++)f.has(V)||d.push(V);let m=l(V=>{if(!c)return V.indices;let ue=new Array(a.shape.length);for(let ce=0;ce<d.length;ce++)ue[d[ce]]=V.indices[ce];return ue[h]=new L(0),ue},"outIndicesFor"),g=new me("_argval_"+r.varCounter,d.map(V=>a.shape[V]),a.dtype,"global");r.varCounter++;let y=_r(r,"ai",d,a.shape,a),_=new J(g,y.indices,ZP(a.dtype,t==="gt")),b=new J(u,m(y),new L(0)),w=new fe(r.blockName("arg_init"),y.ivs,[],[{buffer:g},{buffer:u}],new te([_,b])),x=d.length>0?y.wrap(w):w,S=_r(r,"as",d,a.shape,a),T=r.allocVar("ar"),v=r.allocBindArray("arv",[T]),A=new Array(a.shape.length);for(let V=0;V<d.length;V++)A[d[V]]=S.ivs[V].iterVar;A[h]=v[0].iterVar;let N=new q(a,A),k=new q(g,S.indices),O=new Ke(t,N,k),R=new Se(O,N,k),B=m(S),E=new q(u,B),F=new Se(O,v[0].iterVar,E),C=new J(u,B,F),H=new J(g,S.indices,R),G=new fe(r.blockName("arg_acc"),yp(S.ivs,v),[{buffer:a},{buffer:g}],[{buffer:g},{buffer:u}],new te([C,H])),U=r.extentNode(a.shape[h],a,h),D=_p(G,[T],[U]);return D=S.wrap(D),new te([x,D])})}l(n,"registerArgReduce"),n("argmax","gt"),n("argmin","lt")}var YP,JP,QP,MA=M(()=>{"use strict";Fe();ei();Re();Qr();tr();YP={i8:-128,i16:-32768,i32:-2147483648,i64:-2147483648,ui8:0,ui16:0,ui32:0,bool:0},JP={i8:127,i16:32767,i32:2147483647,i64:2147483647,ui8:255,ui16:65535,ui32:4294967295,bool:1};l(ZP,"argReduceSentinel");QP={sum:l((n,e)=>new P("+",n,e),"sum"),mean:l((n,e)=>new P("+",n,e),"mean"),prod:l((n,e)=>new P("*",n,e),"prod"),max:l((n,e,t)=>new Ne("max",[n,e],t),"max"),min:l((n,e,t)=>new Ne("min",[n,e],t),"min"),and:l((n,e)=>new P("*",n,e),"and"),or:l((n,e,t)=>new Ne("max",[n,e],t),"or")};l(RA,"register")});function IA(n,e){return jt(e)?new ut(n,e,"i32"):n}function EA(){Ie("dot",(n,e,t,r)=>{let{initBody:s,accBody:o}=bp(n,e,t[0],t[1],r[0],{prefix:"di",initBlockName:"matmul_init",accBlockName:"matmul",initVal:l(()=>new de(0),"initVal"),accLeaf:l((i,a)=>new P("*",i,a),"accLeaf")});return new te([s,o])}),Ie("conv",(n,e,t,r)=>{let s=t[0],o=t[1];return gm(n,e,s,o,r[0],{prefix:"c",blockPrefix:"conv",initVal:l(()=>new de(0),"initVal"),guardFill:l(()=>new de(0),"guardFill"),leafBuilder:l((i,a)=>new P("*",new q(s,i),new q(o,a)),"leafBuilder")})}),Ie("gather",(n,e,t,r)=>{let s=t[0],o=t[1],i=r[0],a=new Set(e.getAttr("offset_dims")),u=new Set(e.getAttr("collapsed_slice_dims")),p=e.getAttr("start_index_map"),c=e.getAttr("index_vector_dim"),{loopVars:f,loopBinds:d,indices:h,extentNodes:m}=Rt(n,i.shape,i),g=[],y=[];for(let S=0;S<i.shape.length;S++)a.has(S)?y.push(h[S]):g.push(h[S]);let _=new Array(s.shape.length),b=0;for(let S=0;S<s.shape.length;S++)u.has(S)?_[S]=new L(0):_[S]=y[b++];for(let S=0;S<p.length;S++){let T=new Array(o.shape.length),v=0;for(let k=0;k<o.shape.length;k++)k===c?T[k]=new L(S):T[k]=g[v++];let A=IA(new q(o,T),o.dtype),N=p[S];_[N]=new P("+",_[N],A)}let w=new J(i,h,new q(s,_)),x=new fe(n.blockName("gather_block"),d,[{buffer:s},{buffer:o}],[{buffer:i}],w);return Mt(x,f,i.shape,m)}),Ie("scatter",(n,e,t,r)=>{let s=t[0],o=t[1],i=t[2],a=r[0],u=new Set(e.getAttr("inserted_window_dims")),p=e.getAttr("scatter_dims_to_operand_dims"),c=e.getAttr("index_vector_dim"),f=new Set(e.getAttr("update_window_dims")),d=Rt(n,s.shape,s),h=new J(a,d.indices,new q(s,d.indices)),m=new fe(n.blockName("scatter_copy"),d.loopBinds,[{buffer:s}],[{buffer:a}],h),g=Mt(m,d.loopVars,s.shape,d.extentNodes),{loopVars:y,loopBinds:_,indices:b,extentNodes:w}=Rt(n,i.shape,i),x=[],S=[];for(let E=0;E<i.shape.length;E++)f.has(E)?S.push(b[E]):x.push(b[E]);let T=new Array(s.shape.length),v=0;for(let E=0;E<s.shape.length;E++)u.has(E)?T[E]=new L(0):T[E]=S[v++];for(let E=0;E<p.length;E++){let F=new Array(o.shape.length),C=0;for(let U=0;U<o.shape.length;U++)U===c?F[U]=new L(E):F[U]=x[C++];let H=IA(new q(o,F),o.dtype),G=p[E];T[G]=new P("+",T[G],H)}let A=new q(i,b),N=new q(a,T),k=new P("+",N,A),O=new J(a,T,k),R=new fe(n.blockName("scatter_update"),_,[{buffer:i},{buffer:o}],[{buffer:a}],O),B=Mt(R,y,i.shape,w);return new te([g,B])}),Ie("fused_dot_epilogue",(n,e,t,r)=>{let s=e.getAttr("num_dot_operands")||2,o=t[0],i=t[1],a=t.slice(s),u=r[0],p=e.getAttr("epilogue_tags")||[],{initBody:c,accBody:f}=bp(n,e,o,i,u,{prefix:"ei",initBlockName:"matmul_init",accBlockName:"matmul_acc",initVal:l(()=>new de(0),"initVal"),accLeaf:l((x,S)=>new P("*",x,S),"accLeaf")});if(p.length===0)return new te([c,f]);let d=_r(n,"ep",Array.from({length:u.shape.length},(x,S)=>S),u.shape,u),h=d.indices,m=new q(u,h),g={v:0};for(let x of p){let S=Xr.get(x);S&&(m=S(m,a,g,u,h))}let y=pl([u,...a]),_=new J(u,h,m),b=new fe(n.blockName("epilogue"),d.ivs,y,[{buffer:u}],_),w=d.wrap(b);return new te([c,f,w])}),Ie("cublas_gemm",Fi("dot"))}var Xr,LA=M(()=>{"use strict";Fe();Re();Qr();l(IA,"asIndexValue");Xr=new Map;Xr.set("bias",(n,e,t,r,s)=>{let o=e[t.v++];return new P("+",n,new q(o,ti(o,r,s)))});Xr.set("residual_add",(n,e,t,r,s)=>{let o=e[t.v++];return new P("+",n,new q(o,ti(o,r,s)))});Xr.set("scale",(n,e,t,r,s)=>{let o=e[t.v++];return new P("*",n,new q(o,ti(o,r,s)))});Xr.set("relu",(n,e,t,r)=>new Ne("max",[n,new de(0)],r.dtype));Xr.set("clamp",(n,e,t,r,s)=>{let o=e[t.v++],i=e[t.v++];return new Ne("min",[new Ne("max",[n,new q(o,ti(o,r,s))],r.dtype),new q(i,ti(i,r,s))],r.dtype)});Xr.set("neg",n=>new P("-",n));Xr.set("exp",(n,e,t,r)=>new Ne("exp",[n],r.dtype));Xr.set("tanh",(n,e,t,r)=>new Ne("tanh",[n],r.dtype));Xr.set("sqrt",(n,e,t,r)=>new Ne("sqrt",[n],r.dtype));Xr.set("abs",(n,e,t,r)=>new Ne("abs",[n],r.dtype));Xr.set("log",(n,e,t,r)=>new Ne("log",[n],r.dtype));Xr.set("activation",n=>n);l(EA,"register")});function dl(n,e,t){let{loopVars:r,loopBinds:s,indices:o,extentNodes:i}=Rt(n,t.shape,t),a=new J(t,o,new q(e,o)),u=new fe(n.blockName("cf_copy"),s,[{buffer:e}],[{buffer:t}],a);return Mt(u,r,t.shape,i)}function XP(n,e,t,r){let{loopVars:s,loopBinds:o,indices:i,extentNodes:a}=Rt(n,t.shape,t),u=new J(t,i,new q(e,[r,...i])),p=new fe(n.blockName("scan_in"),o,[{buffer:e}],[{buffer:t}],u);return Mt(p,s,t.shape,a)}function e$(n,e,t,r){let{loopVars:s,loopBinds:o,indices:i,extentNodes:a}=Rt(n,e.shape,e),u=new J(t,[r,...i],new q(e,i)),p=new fe(n.blockName("scan_out"),o,[{buffer:e}],[{buffer:t}],u);return Mt(p,s,e.shape,a)}function Tp(n,e,t){let r=e.entryBlock,s=new Map;for(let i=0;i<r.arguments.length;i++)s.set(r.arguments[i],t[i]),t[i]&&n.bufferMap.set(r.arguments[i],t[i]);let o=[];for(let i of r.ops()){if(i.opName==="yield"){let d=new Array(i.numOperands);for(let h=0;h<i.numOperands;h++)d[h]=s.get(i.getOperand(h))||n.getOrAllocBuffer(i.getOperand(h));return{stmts:o,yieldBuffers:d}}if(_n(i.opName)){o.push(fl(n,i));continue}let a=new Array(i.numOperands);for(let d=0;d<i.numOperands;d++)a[d]=s.get(i.getOperand(d))||i.getOperand(d);let u=new Array(a.length);for(let d=0;d<a.length;d++)u[d]=a[d]instanceof me?a[d]:n.getOrAllocBuffer(a[d]);let p=new Array(i.numResults);for(let d=0;d<i.numResults;d++){let h={type:i.getResult(d).type};p[d]=n.getOrAllocBuffer(h),s.set(i.getResult(d),p[d]),n.bufferMap.set(i.getResult(d),p[d])}let c=Fi(i.opName);if(!c)throw new Error(`No lowering rule for op '${i.opName}' inside region`);let f=c(n,i,u,p);f&&o.push(f)}return{stmts:o,yieldBuffers:[]}}function CA(){Ie("if",(n,e,t,r)=>{let s=t[0],o=new q(s,[]),i=e.regions[0],a=e.regions[1],u=new Array(e.numResults);for(let h=0;h<e.numResults;h++)u[h]=r[h]||n.getOrAllocBuffer(e.getResult(h)),n.bufferMap.set(e.getResult(h),u[h]);let p=Tp(n,i,[]),c=p.stmts.slice();for(let h=0;h<u.length&&h<p.yieldBuffers.length;h++){let m=p.yieldBuffers[h];m&&m!==u[h]&&c.push(dl(n,m,u[h]))}let f=c.length===1?c[0]:new te(c),d=null;if(a&&a.entryBlock){let h=Tp(n,a,[]),m=h.stmts.slice();for(let g=0;g<u.length&&g<h.yieldBuffers.length;g++){let y=h.yieldBuffers[g];y&&y!==u[g]&&m.push(dl(n,y,u[g]))}m.length>0&&(d=m.length===1?m[0]:new te(m))}return new Se(o,f,d)}),Ie("while",(n,e,t,r)=>{let s=e.regions[0],o=e.regions[1],i=new Array(t.length),a=[];for(let y=0;y<t.length;y++){let _=r[y]||n.getOrAllocBuffer({type:e.getResult(y).type});i[y]=_,a.push(dl(n,t[y],_))}for(let y=0;y<e.numResults;y++)n.bufferMap.set(e.getResult(y),i[y]);let u=new me(`_wcond_${n.varCounter++}`,[],"bool",ul.GLOBAL),p=Tp(n,s,i),c=p.stmts.slice(),f=p.yieldBuffers[0];c.push(new J(u,[],new q(f,[])));let d=c.length===1?c[0]:new te(c),h=Tp(n,o,i),m=h.stmts.slice();for(let y=0;y<i.length&&y<h.yieldBuffers.length;y++){let _=h.yieldBuffers[y];_&&_!==i[y]&&m.push(dl(n,_,i[y]))}let g=m.length===1?m[0]:new te(m);return new te([...a,new $i(u,d,g)])}),Ie("scan",(n,e,t,r)=>{let s=e.getAttr("num_xs"),o=e.getAttr("num_carry"),i=t.slice(0,s),a=t.slice(s),u=new Array(o),p=[];for(let b=0;b<o;b++){let w=r[b]||n.getOrAllocBuffer({type:e.getResult(b).type});u[b]=w,p.push(dl(n,a[b],w)),n.bufferMap.set(e.getResult(b),w)}let c=r.length-o,f=new Array(c);for(let b=0;b<c;b++){let w=r[o+b]||n.getOrAllocBuffer({type:e.getResult(o+b).type});f[b]=w,n.bufferMap.set(e.getResult(o+b),w)}let d=n.allocVar("t"),h=n.extentNode(i[0].shape[0],i[0],0),m=[],g=new Array(s);for(let b=0;b<s;b++){let w=n.getOrAllocBuffer({type:{shape:i[b].shape.slice(1),dtype:i[b].dtype}});g[b]=w,m.push(XP(n,i[b],w,d))}let y=Tp(n,e.regions[0],[...g,...u]);for(let b of y.stmts)m.push(b);for(let b=0;b<o;b++){let w=y.yieldBuffers[b];w&&w!==u[b]&&m.push(dl(n,w,u[b]))}for(let b=0;b<c;b++)m.push(e$(n,y.yieldBuffers[o+b],f[b],d));m.push(new Fn);let _=m.length===1?m[0]:new te(m);return new te([...p,new he(d,new L(0),h,z.RECURRENCE,_)])})}var DA=M(()=>{"use strict";Fe();ei();R0();Qr();l(dl,"copyBuffer");l(XP,"sliceCopyIn");l(e$,"sliceCopyOut");l(Tp,"lowerRegionBody");l(CA,"register")});function PA(){Ie("layout_transform",(n,e,t,r)=>{let s=t[0],o=r[0],i=e.getAttr("src_layout"),a=e.getAttr("dst_layout"),{loopVars:u,loopBinds:p,indices:c,extentNodes:f}=Rt(n,o.shape,o),d=new J(o,c,new q(s,c)),h=new fe(n.blockName("layout_transform_block"),p,[{buffer:s}],[{buffer:o}],d);return Mt(h,u,o.shape,f)})}var $A=M(()=>{"use strict";Fe();Qr();l(PA,"register")});function t$(n){let e=n==="ui8",t=8;return{cMin:e?0:-(1<<t-1),cMax:e?(1<<t)-1:(1<<t-1)-1}}function _m(n,{scale:e,zeroPoint:t,targetDtype:r}){let s=r||"i8",{cMin:o,cMax:i}=t$(s),a=new P("/",n,new de(e)),u=t!==0?new P("+",a,new de(t)):a,p=new Ne("round",[u],"f32"),c=new Ne("min",[new Ne("max",[p,new de(o)],"f32"),new de(i)],"f32");return new ut(c,"f32",s)}function bm(n,{scale:e,zeroPoint:t,srcDtype:r,targetDtype:s}){let o=s||"f32",i=new ut(n,r||"i8",o),a=t!==0?new P("-",i,new de(t)):i;return new P("*",a,new de(e))}var F0=M(()=>{"use strict";Fe();l(t$,"clampRange");l(_m,"buildQuantizeExpr");l(bm,"buildDequantizeExpr")});function FA(){Ie("quantize",(n,e,t,r)=>{let s=e.getAttr("scale"),o=e.getAttr("zero_point"),i=e.getAttr("target_dtype")||"i8";return mo(n,e,t,r,(a,u)=>_m(u[0],{scale:s,zeroPoint:o,targetDtype:i}))}),Ie("dequantize",(n,e,t,r)=>{let s=e.getAttr("scale"),o=e.getAttr("zero_point"),i=t[0].dtype||"i8",a=e.getAttr("target_dtype")||"f32";return mo(n,e,t,r,(u,p)=>bm(p[0],{scale:s,zeroPoint:o,srcDtype:i,targetDtype:a}))}),Ie("quantized_dot",(n,e,t,r)=>{let s=t[0],o=t[1],i=e.getAttr("lhs_zero_point")||0,a=e.getAttr("rhs_zero_point")||0,{initBody:u,accBody:p}=bp(n,e,s,o,r[0],{prefix:"qdi",initBlockName:"qmatmul_init",accBlockName:"qmatmul_acc",initVal:l(()=>new L(0),"initVal"),accLeaf:l((c,f)=>{let d=new ut(c,s.dtype,"i32"),h=new ut(f,o.dtype,"i32"),m=i!==0?new P("-",d,new L(i)):d,g=a!==0?new P("-",h,new L(a)):h;return new P("*",m,g)},"accLeaf")});return new te([u,p])}),Ie("quantized_conv",(n,e,t,r)=>{let s=t[0],o=t[1],i=e.getAttr("input_zero_point")||0,a=e.getAttr("kernel_zero_point")||0;return gm(n,e,s,o,r[0],{prefix:"qc",blockPrefix:"qconv",initVal:l(()=>new L(0),"initVal"),guardFill:l(()=>new L(0),"guardFill"),leafBuilder:l((u,p)=>{let c=new ut(new q(s,u),s.dtype,"i32"),f=new ut(new q(o,p),o.dtype,"i32"),d=i!==0?new P("-",c,new L(i)):c,h=a!==0?new P("-",f,new L(a)):f;return new P("*",d,h)},"leafBuilder")})})}var zA=M(()=>{"use strict";Fe();Qr();F0();l(FA,"register")});function ni(n){return es.has(n)}function n$(){for(let n of wp())es.set(n,(e,t,r)=>$0(e.opName,t,r));es.set("compare",(n,e)=>new Ke(n.getAttr("direction")||"eq",e[0],e[1])),es.set("select",(n,e)=>new Se(e[0],e[1],e[2])),es.set("clamp",(n,e,t)=>new Ne("min",[new Ne("max",[e[1],e[0]],t),e[2]],t)),es.set("convert",(n,e)=>new ut(e[0],n.getOperand(0).type.dtype,n.getAttr("target_dtype")||n.getResult(0).type.dtype)),es.set("broadcast_in_dim",(n,e)=>e[0]),es.set("broadcast",(n,e)=>e[0]),es.set("iota",()=>{throw new Error("iota fusion must be handled by the index-aware path in lowerFusion")}),es.set("quantize",(n,e)=>_m(e[0],{scale:n.getAttr("scale"),zeroPoint:n.getAttr("zero_point"),targetDtype:n.getAttr("target_dtype")||"i8"})),es.set("dequantize",(n,e)=>bm(e[0],{scale:n.getAttr("scale"),zeroPoint:n.getAttr("zero_point"),srcDtype:n.getOperand(0).type?.dtype||"i8",targetDtype:n.getAttr("target_dtype")||"f32"}))}function VA(n,e){let t=e.numOperands,r=e.numResults,s=new Array(t);for(let N=0;N<t;N++)s[N]=n.getOrAllocBuffer(e.getOperand(N));let o=new Array(r);for(let N=0;N<r;N++)o[N]=n.getOrAllocBuffer(e.getResult(N));let i=o[0],{loopVars:a,loopBinds:u,indices:p,extentNodes:c}=Rt(n,i.shape,i),f=new Map,d=e.regions[0].entryBlock,h=new Map,m=[...d.ops()];for(let N=m.length-1;N>=0;N--){let k=m[N];if(cl(k.opName)){let O=k.getAttr("broadcast_dimensions");O&&O.length>0&&h.set(k.getOperand(0),O);continue}if(!(k.opName==="yield"||_n(k.opName)))for(let O=0;O<k.numResults;O++){let R=h.get(k.getResult(O));if(R)for(let B=0;B<k.numOperands;B++)h.has(k.getOperand(B))||h.set(k.getOperand(B),R)}}let g=new Map,y=d.arguments;for(let N=0;N<y.length;N++){let k=h.get(y[N]);k&&g.set(N,k)}for(let N=0;N<d.arguments.length;N++){let k=g.get(N),O;if(k){let R=s[N];O=new Array(R.shape.length);for(let B=0;B<R.shape.length;B++)O[B]=R.shape[B]===1?new L(0):p[k[B]]}else O=ti(s[N],i,p);f.set(d.arguments[N],new q(s[N],O))}let _=new Map;for(let N of d.ops())for(let k=0;k<N.numOperands;k++){let O=N.getOperand(k);_.set(O,(_.get(O)||0)+1)}let b=new Map,w=0,x=[];function S(N){let k=f.get(N);if(k===void 0)throw new Error(`Fusion lowering: unmapped operand from '${N.definingOp?N.definingOp.opName:"unknown"}'`);if((_.get(N)||0)>1&&!r$.has(k.type)){if(!b.has(N)){let O=k.type==="CompareNode"?"i32":k.type==="CastNode"?k.toDtype:N.type&&N.type.dtype?N.type.dtype:i.dtype,R=n.allocVar(`cse${w++}`,O);b.set(N,R),x.push({variable:R,value:k}),f.set(N,R)}return b.get(N)}return k}l(S,"getExpr");let T=[];for(let N of d.ops()){if(N.opName==="yield"){for(let B=0;B<N.numOperands;B++)T.push(new J(o[B],p,S(N.getOperand(B))));break}if(_n(N.opName)){let B=N.getAttr("value");f.set(N.getResult(0),new de(typeof B=="number"?B:0));continue}if(N.opName==="iota"){let B=N.getAttr("iota_dimension")??N.getAttr("dimension")??0;f.set(N.getResult(0),p[B]);continue}let k=es.get(N.opName);if(!k)throw new Error(`Fusion lowering: unsupported op '${N.opName}' inside fusion body`);let O=new Array(N.numOperands);for(let B=0;B<N.numOperands;B++)O[B]=S(N.getOperand(B));let R=N.getResult(0).type.dtype;f.set(N.getResult(0),k(N,O,R))}let v=T.length===1?T[0]:new te(T);for(let N=x.length-1;N>=0;N--)v=new et(x[N].variable,x[N].value,v);let A=new fe(n.blockName("fusion_block"),u,pl(s),pl(o),v);return Mt(A,a,i.shape,c)}function s$(n,e){if(n.length!==e.length)return!1;for(let t=0;t<n.length;t++)if(n[t]!==e[t])return!1;return!0}function GA(n){let e=n.regions[0];if(!e)return!1;for(let t of e.entryBlock.ops())if(t.opName!=="yield"){if(_n(t.opName)){if(typeof t.getAttr("value")=="number")continue;return!1}if(!es.has(t.opName))return!1}if(n.numResults>1){let t=n.getResult(0).type.shape;for(let r=1;r<n.numResults;r++)if(!s$(n.getResult(r).type.shape,t))return!1}return!0}function jA(n,e,t){let r=e.regions[0].entryBlock,s=new Map;for(let i=0;i<r.arguments.length;i++)s.set(r.arguments[i],e.getOperand(i));let o=new Map;for(let i of r.ops())if(i.opName==="yield"){for(let a=0;a<i.numOperands;a++)o.set(i.getOperand(a),e.getResult(a));break}for(let i of r.ops()){if(i.opName==="yield")continue;let a=new Array(i.numOperands);for(let d=0;d<i.numOperands;d++)a[d]=s.get(i.getOperand(d))||i.getOperand(d);let u=new Array(a.length);for(let d=0;d<a.length;d++)u[d]=n.getOrAllocBuffer(a[d]);let p=new Array(i.numResults);for(let d=0;d<i.numResults;d++){let h=i.getResult(d),m=o.get(h);if(m){let g=n.getOrAllocBuffer(m);p[d]=g,s.set(h,m)}else{let g={type:h.type};p[d]=n.getOrAllocBuffer(g),s.set(h,g)}}if(_n(i.opName)){t.push(fl(n,i));continue}let c=Fi(i.opName);if(!c)throw new Error(`Fusion lowering: no lowering rule for op '${i.opName}' inside fusion body`);let f=c(n,i,u,p);f&&t.push(f)}}function UA(){n$()}var es,r$,qA=M(()=>{"use strict";Fe();Qr();ym();F0();Ir();es=new Map;l(ni,"canInlineFuse");l(n$,"initBuiltinFusionBuilders");r$=new Set(["BufferLoadNode","VariableNode","IntImmNode","FloatImmNode"]);l(VA,"lowerFusion");l(s$,"shapesEqual");l(GA,"canLowerAsElementwiseFusion");l(jA,"lowerFusionAsIndividualOps");l(UA,"register")});function WA(n,e,t,r,s){let o=new P("-",new P("*",n,new L(e)),new L(r)),i=new P("+",o,new L(t)),a=new Ne("max",[o,new L(0)],"index"),u=new Ne("min",[i,new L(s)],"index"),p=new P("-",u,a);return new Ne("max",[p,new L(0)],"index")}function o$(n,e,t,r,s,o,i,a,u,p,c){let f=WA(n,t,s,i,u),d=WA(e,r,o,a,p),h=new P("*",f,d);return new ut(h,"index",c)}function KA(){Ie("pool2d",(n,e,t,r)=>{let s=t[0],o=r[0],i=e.getAttr("pool_type"),a=e.getAttr("kernel_size"),u=e.getAttr("strides"),p=e.getAttr("padding"),c=e.getAttr("count_include_pad")||!1,f=mm(e.getAttr("layout")||"NCHW"),d=f.N,h=f.C,m=f.H,g=f.W,y=s.shape[d],_=s.shape[h],b=s.shape[m],w=s.shape[g],x=o.shape[m],S=o.shape[g],T=a[0],v=a[1],A=u[0],N=u[1],k=p[0][0],O=p[1][0],R=i==="max",B=R?new de(-1/0):new de(0),E=_r(n,"pi",[0,1,2,3],o.shape,o),F=new J(o,E.indices,B),C=new fe(n.blockName("pool_init"),E.ivs,[],[{buffer:o}],F),H=E.wrap(C),G=n.allocVar("pn"),U=n.allocVar("pc"),D=n.allocVar("poh"),V=n.allocVar("pow"),ue=n.allocVar("pkh"),ce=n.allocVar("pkw"),Le=[G,U,D,V,ue,ce],j=n.allocBindArray("pv",Le),W=j[0].iterVar,ie=j[1].iterVar,je=j[2].iterVar,ke=j[3].iterVar,rt=j[4].iterVar,zt=j[5].iterVar,on=new P("+",new P("*",je,new L(A)),new P("-",rt,new L(k))),Zt=new P("+",new P("*",ke,new L(N)),new P("-",zt,new L(O))),it=new Ke("ge",on,new L(0)),ye=new Ke("lt",on,new L(b)),Xe=new Ke("ge",Zt,new L(0)),Nn=new Ke("lt",Zt,new L(w)),Qt=new P("*",new P("*",it,ye),new P("*",Xe,Nn)),an=new Array(4);an[d]=W,an[h]=ie,an[m]=je,an[g]=ke;let un=new Array(4);un[d]=W,un[h]=ie,un[m]=on,un[g]=Zt;let mn=new q(s,un),cr=new q(o,an),Vn;if(R){let xt=new Se(Qt,mn,new de(-1/0));Vn=new Ne("max",[cr,xt],o.dtype)}else{let xt=new Se(Qt,mn,new de(0));Vn=new P("+",cr,xt)}let Or=new J(o,an,Vn),yt=new fe(n.blockName("pool_acc"),j,[{buffer:s}],[{buffer:o}],Or);yt=new he(ce,new L(0),new L(v),z.SERIAL,yt),yt=new he(ue,new L(0),new L(T),z.SERIAL,yt),yt=new he(V,new L(0),new L(S),z.SERIAL,yt),yt=new he(D,new L(0),new L(x),z.SERIAL,yt),yt=new he(U,new L(0),new L(_),z.SERIAL,yt),yt=new he(G,new L(0),new L(y),z.SERIAL,yt);let fr=[H,yt];if(!R){let xt=_r(n,"pd",[0,1,2,3],o.shape,o),Vr=xt.indices[m],Ds=xt.indices[g],uo=c?new de(T*v):o$(Vr,Ds,A,N,T,v,k,O,b,w,o.dtype),Gr=new q(o,xt.indices),dr=c?new P("*",Gr,new de(1/(T*v))):new P("/",Gr,uo),Br=new J(o,xt.indices,dr),qo=new fe(n.blockName("pool_div"),xt.ivs,[{buffer:o}],[{buffer:o}],Br);fr.push(xt.wrap(qo))}return new te(fr)})}var HA=M(()=>{"use strict";Fe();Qr();l(WA,"spatialCount");l(o$,"avgPoolDivisorExpr");l(KA,"register")});function YA(){Ie("resize",(n,e,t,r)=>{let s=t[0],o=r[0],i=e.getAttr("method"),a=s.shape[2],u=s.shape[3],p=o.shape[2],c=o.shape[3],f=_r(n,"rz",[0,1,2,3],o.shape,o),[d,h,m,g]=f.indices,y=a/p,_=u/c,b;if(i==="nearest"){let S=new Ne("floor",[new P("*",m,new de(y))],"f32"),T=new Ne("floor",[new P("*",g,new de(_))],"f32"),v=new ut(new Ne("min",[new Ne("max",[S,new de(0)],"f32"),new de(a-1)],"f32"),"f32","i32"),A=new ut(new Ne("min",[new Ne("max",[T,new de(0)],"f32"),new de(u-1)],"f32"),"f32","i32");b=new q(s,[d,h,v,A])}else{let S=new P("*",m,new de(y)),T=new P("*",g,new de(_)),v=new Ne("floor",[S],"f32"),A=new Ne("floor",[T],"f32"),N=new Ne("min",[new P("+",v,new de(1)),new de(a-1)],"f32"),k=new Ne("min",[new P("+",A,new de(1)),new de(u-1)],"f32"),O=new P("-",S,v),R=new P("-",T,A),B=new Ne("max",[v,new de(0)],"f32"),E=new Ne("max",[A,new de(0)],"f32"),F=new ut(B,"f32","i32"),C=new ut(E,"f32","i32"),H=new ut(N,"f32","i32"),G=new ut(k,"f32","i32"),U=new q(s,[d,h,F,C]),D=new q(s,[d,h,F,G]),V=new q(s,[d,h,H,C]),ue=new q(s,[d,h,H,G]),ce=new P("-",new de(1),R),Le=new P("-",new de(1),O),j=new P("+",new P("*",U,ce),new P("*",D,R)),W=new P("+",new P("*",V,ce),new P("*",ue,R));b=new P("+",new P("*",j,Le),new P("*",W,O))}let w=new J(o,f.indices,b),x=new fe(n.blockName("resize_block"),f.ivs,[{buffer:s}],[{buffer:o}],w);return f.wrap(x)})}var JA=M(()=>{"use strict";Fe();Qr();l(YA,"register")});function z0(n,e){let[t,r,s]=e;return{Q:t,K:r,V:s,O:null,scale:n.getAttr("scale"),causal:!!n.getAttr("causal"),dtype:t.dtype,B:t.shape[0],H:t.shape[1],Lq:t.shape[2],Dk:t.shape[3],Lk:r.shape[2],Dv:s.shape[3]}}function ZA(n,e,t,r){let{Q:s,K:o,V:i,scale:a,causal:u,dtype:p,B:c,H:f,Lq:d,Dk:h,Lk:m,Dv:g}=z0(e,t),y=r[0],_=new me(n.blockName("fa_m"),[1],p,"local"),b=new me(n.blockName("fa_l"),[1],p,"local"),w=new me(n.blockName("fa_s"),[1],p,"local"),x=new me(n.blockName("fa_mn"),[1],p,"local"),S=new me(n.blockName("fa_p"),[1],p,"local"),T=new me(n.blockName("fa_cr"),[1],p,"local"),v=new me(n.blockName("fa_o"),[g],p,"local"),A=new Oe(n.blockName("fa_b"),"int32"),N=new Oe(n.blockName("fa_h"),"int32"),k=new Oe(n.blockName("fa_i"),"i32"),O=new Oe(n.blockName("fa_j"),"int32"),R=new Oe(n.blockName("fa_d"),"int32"),B=new Oe(n.blockName("fa_di"),"int32"),E=new Oe(n.blockName("fa_do"),"int32"),F=new Oe(n.blockName("fa_dw"),"int32"),C=new fe(n.blockName("fa_initm"),[],[],[{buffer:_}],mt(_,[_e],new de(-1/0))),H=new fe(n.blockName("fa_initl"),[],[],[{buffer:b}],mt(b,[_e],new de(0))),G=Er(B,g,new fe(n.blockName("fa_inito"),[],[],[{buffer:v}],mt(v,[B],new de(0)))),U=new fe(n.blockName("fa_sinit"),[],[],[{buffer:w}],mt(w,[_e],new de(0))),D=Er(R,h,new fe(n.blockName("fa_sred"),[],[{buffer:s},{buffer:o},{buffer:w}],[{buffer:w}],mt(w,[_e],lt("+",Ce(w,[_e]),lt("*",Ce(s,[A,N,k,R]),Ce(o,[A,N,O,R])))))),V=new te([U,D]),ue=new fe(n.blockName("fa_supd"),[],[{buffer:w},{buffer:_},{buffer:b}],[{buffer:w},{buffer:x},{buffer:S},{buffer:T},{buffer:b}],new te([mt(w,[_e],lt("*",Ce(w,[_e]),new de(a))),mt(x,[_e],hl("max",Ce(_,[_e]),Ce(w,[_e]))),mt(S,[_e],hl("exp",lt("-",Ce(w,[_e]),Ce(x,[_e])))),mt(T,[_e],hl("exp",lt("-",Ce(_,[_e]),Ce(x,[_e])))),mt(b,[_e],lt("+",lt("*",Ce(b,[_e]),Ce(T,[_e])),Ce(S,[_e])))])),ce=Er(E,g,new fe(n.blockName("fa_oupd"),[],[{buffer:v},{buffer:T},{buffer:S},{buffer:i}],[{buffer:v}],mt(v,[E],lt("+",lt("*",Ce(v,[E]),Ce(T,[_e])),lt("*",Ce(S,[_e]),Ce(i,[A,N,O,E])))))),Le=new fe(n.blockName("fa_mupd"),[],[{buffer:x}],[{buffer:_}],mt(_,[_e],Ce(x,[_e]))),j=new te([V,ue,ce,Le]),W=m-d,ie=u?new Se(new Ke("le",O,lt("+",k,zi(W))),j):j,je=Er(O,m,ie),ke=Er(F,g,new fe(n.blockName("fa_finw"),[],[{buffer:v},{buffer:b}],[{buffer:y}],mt(y,[A,N,k,F],lt("/",Ce(v,[F]),Ce(b,[_e]))))),rt=new te([C,H,G,je,ke]);for(let ye of[v,T,S,x,w,b,_])rt=new Ue(ye,ye.scope,rt);let zt=Math.min(d,256),on=Math.ceil(d/zt),Zt;if(on===1)Zt=new he(k,_e,new L(d),z.THREAD_BINDING,rt,"threadIdx.x");else{let ye=new Oe(n.blockName("fa_ii"),"i32"),Xe=new Oe(n.blockName("fa_io"),"i32"),Nn=lt("+",lt("*",Xe,new L(zt)),ye),Qt=on*zt===d?rt:new Se(new Ke("lt",k,new L(d)),rt),an=new he(ye,_e,new L(zt),z.THREAD_BINDING,new et(k,Nn,Qt),"threadIdx.x");Zt=new he(Xe,_e,new L(on),z.THREAD_BINDING,an,"blockIdx.z")}let it=new he(N,_e,new L(f),z.THREAD_BINDING,Zt,"blockIdx.y");return new he(A,_e,new L(c),z.THREAD_BINDING,it,"blockIdx.x")}function i$(n,e,t,r,s){let{Q:o,K:i,V:a,scale:u,causal:p,dtype:c,B:f,H:d,Lq:h,Dk:m,Lk:g,Dv:y}=z0(e,t),_=r[0],b=Math.ceil(g/s),w=Math.ceil(h/s),x=g-h,S=new me(n.blockName("fa_Ks"),[s,m],c,"shared"),T=new me(n.blockName("fa_Vs"),[s,y],c,"shared"),v=new me(n.blockName("fa_q"),[m],c,"local"),A=new me(n.blockName("fa_m"),[1],c,"local"),N=new me(n.blockName("fa_l"),[1],c,"local"),k=new me(n.blockName("fa_s"),[1],c,"local"),O=new me(n.blockName("fa_mn"),[1],c,"local"),R=new me(n.blockName("fa_p"),[1],c,"local"),B=new me(n.blockName("fa_cr"),[1],c,"local"),E=new me(n.blockName("fa_o"),[y],c,"local"),F=new Oe(n.blockName("fa_b"),"int32"),C=new Oe(n.blockName("fa_h"),"int32"),H=new Oe(n.blockName("fa_qb"),"int32"),G=new Oe(n.blockName("fa_t"),"int32"),U=new Oe(n.blockName("fa_i"),"i32"),D=new Oe(n.blockName("fa_kt"),"int32"),V=new Oe(n.blockName("fa_jj"),"int32"),ue=new Oe(n.blockName("fa_dq"),"int32"),ce=new Oe(n.blockName("fa_dk"),"int32"),Le=new Oe(n.blockName("fa_dv"),"int32"),j=new Oe(n.blockName("fa_ds"),"int32"),W=new Oe(n.blockName("fa_doo"),"int32"),ie=new Oe(n.blockName("fa_dw"),"int32"),je=new Oe(n.blockName("fa_din"),"int32"),ke=lt("+",lt("*",H,zi(s)),G),rt=lt("+",lt("*",D,zi(s)),G),zt=lt("+",lt("*",D,zi(s)),V),on=new Ke("lt",U,zi(h)),Zt=new Ke("lt",rt,zi(g)),it=Er(ue,m,mt(v,[ue],Ce(o,[F,C,U,ue]))),ye=mt(A,[_e],new de(-1/0)),Xe=mt(N,[_e],new de(0)),Nn=Er(je,y,mt(E,[je],new de(0))),Qt=new Se(on,new te([it,ye,Xe,Nn])),an=new Se(Zt,Er(ce,m,mt(S,[G,ce],Ce(i,[F,C,rt,ce])))),un=new Se(Zt,Er(Le,y,mt(T,[G,Le],Ce(a,[F,C,rt,Le])))),mn=mt(k,[_e],new de(0)),cr=Er(j,m,mt(k,[_e],lt("+",Ce(k,[_e]),lt("*",Ce(v,[j]),Ce(S,[V,j]))))),Vn=new te([mt(k,[_e],lt("*",Ce(k,[_e]),new de(u))),mt(O,[_e],hl("max",Ce(A,[_e]),Ce(k,[_e]))),mt(R,[_e],hl("exp",lt("-",Ce(k,[_e]),Ce(O,[_e])))),mt(B,[_e],hl("exp",lt("-",Ce(A,[_e]),Ce(O,[_e])))),mt(N,[_e],lt("+",lt("*",Ce(N,[_e]),Ce(B,[_e])),Ce(R,[_e])))]),Or=Er(W,y,mt(E,[W],lt("+",lt("*",Ce(E,[W]),Ce(B,[_e])),lt("*",Ce(R,[_e]),Ce(T,[V,W]))))),pr=mt(A,[_e],Ce(O,[_e])),yt=new te([mn,cr,Vn,Or,pr]),fr=new Ke("lt",zt,zi(g)),xt=p?new Se(new Ke("le",zt,lt("+",U,zi(x))),yt):yt,Vr=Er(V,s,new Se(fr,xt)),Ds=new Se(on,Vr),uo=new te([an,un,new Fn,Ds,new Fn]),Gr=Er(D,b,uo),dr=new Se(on,Er(ie,y,mt(_,[F,C,U,ie],lt("/",Ce(E,[ie]),Ce(N,[_e]))))),Br=new et(U,ke,new te([Qt,Gr,dr]));for(let ms of[E,B,R,O,k,N,A,v])Br=new Ue(ms,ms.scope,Br);let qo=wm(G,"threadIdx.x",s,Br),jr=new Ue(S,"shared",new Ue(T,"shared",qo)),Ur=wm(H,"blockIdx.z",w,jr),Ps=wm(C,"blockIdx.y",d,Ur);return wm(F,"blockIdx.x",f,Ps)}function QA(){Ie("scaled_dot_product_attention",ZA),vA("scaled_dot_product_attention","cuda",(n,e,t,r)=>{let{Lq:s,Dk:o,Dv:i,dtype:a}=z0(e,t),u=n.target,p=u&&u.sharedMemoryBytes||0,c=u&&u.maxThreadsPerBlock||1024,f=Math.min(c,s,Math.floor(p/((o+i)*Rn(a)*2)));return f<8?ZA(n,e,t,r):i$(n,e,t,r,f)})}var _e,zi,Ce,mt,lt,hl,Er,wm,XA=M(()=>{"use strict";Fe();ei();Qr();tr();_e=new L(0),zi=l(n=>new L(n),"I"),Ce=l((n,e)=>new q(n,e),"ld"),mt=l((n,e,t)=>new J(n,e,t),"st"),lt=l((n,e,t)=>new P(n,e,t),"mop"),hl=l((n,...e)=>new Ne(n,e),"ext"),Er=l((n,e,t,r=z.SERIAL)=>new he(n,_e,new L(e),r,t),"forL"),wm=l((n,e,t,r)=>new he(n,_e,new L(t),z.THREAD_BINDING,r,e),"forT");l(z0,"dims");l(ZA,"buildNaive");l(i$,"buildTiled");l(QA,"register")});function e1(n,e,t=new Set){let r=ne.get(e.opName);if(!r||!r.getAttr("broadcastViewSafe"))return!1;if(e.opName!=="fusion")return!0;let s=e.regions[0];if(!s)return!1;let o=s.entryBlock;for(let i=0;i<e.numOperands;i++){if(e.getOperand(i)!==n)continue;let a=o.arguments[i];for(let u of a.getUsers()){let p=`${a.id}:${u.id}`;if(!t.has(p)&&(t.add(p),!e1(a,u,t)))return!1}}return!0}function u$(n){return um([...n.ops()],"ignore")}function Tm(n,e=null,t=null){let r=new hm;r.target=e;let s=[],o=new Map;for(let g of n.args){let y=r.allocVar("arg");s.push(y),o.set(y,r.getOrAllocBuffer(g))}let i=n.getReturnOp(),a=new Set;for(let g of n.args)a.add(r.getOrAllocBuffer(g));let u=[],p=new Set;for(let g=0;g<i.numOperands;g++){let y=r.allocVar("ret");s.push(y);let _=r.getOrAllocBuffer(i.getOperand(g));if(a.has(_)||p.has(_)){let b=r.allocFreshBuffer(i.getOperand(g));o.set(y,b),u.push({src:_,dst:b})}else o.set(y,_),p.add(_)}let c=new Set;for(let g=0;g<i.numOperands;g++)c.add(i.getOperand(g));let f=[];for(let g of n.ops())_n(g.opName)&&f.push(fl(r,g));for(let g of u$(n)){if(Xt(g.opName)||_n(g.opName))continue;if(g.opName==="fusion"){GA(g)?f.push(VA(r,g)):jA(r,g,f);continue}if(cl(g.opName)&&!c.has(g.getResult(0))&&g.getOperand(0).getUsers().length===1&&g.getResult(0).getUsers().every(x=>e1(g.getResult(0),x))){let x=r.getOrAllocBuffer(g.getOperand(0)),S=g.getResult(0).type.shape,T=g.getAttr("broadcast_dimensions"),v;if(T&&T.length>0)v=T;else{let A=S.length-x.shape.length;v=Array.from({length:x.shape.length},(N,k)=>k+A)}x.broadcastDims=v,r.bufferMap.set(g.getResult(0),x);continue}let y=Fi(g.opName,e,t);if(!y)throw new Error(`No lowering rule defined for op: ${g.opName}`);let _=new Array(g.numOperands);for(let x=0;x<g.numOperands;x++)_[x]=r.getOrAllocBuffer(g.getOperand(x));let b=new Array(g.numResults);for(let x=0;x<g.numResults;x++)b[x]=r.getOrAllocBuffer(g.getResult(x));let w=y(r,g,_,b);w&&f.push(w)}for(let{src:g,dst:y}of u){let{loopVars:_,loopBinds:b,indices:w,extentNodes:x}=Rt(r,g.shape,g),S=new q(g,w),T=new J(y,w,S),v=new fe(r.blockName("copy_block"),b,[{buffer:g}],[{buffer:y}],T);f.push(Mt(v,_,g.shape,x))}let d=[],h=new Set;for(let g of r.shapeParams.values())h.has(g.name)||(h.add(g.name),d.push(g));for(let[g,y]of r.symVars)if(!h.has(y.name))throw new Error(`Symbolic dimension '${g}' has no input dimension to bind it to at runtime`);for(let g of d)s.push(g);let m=new Pi(n.name,s,f.length===1?f[0]:new te(f),o,d,new Map(r.shapeParams));return n._partitionTarget&&(m._partitionTarget=n._partitionTarget),m}var a$,$a=M(()=>{"use strict";Fe();Da();Ct();Qr();Ir();ym();BA();MA();LA();DA();$A();zA();qA();HA();JA();XA();ym();a$=["compare","select","clamp","convert","copy_to_device","dot","fusion"];for(let n of[...wp(),...a$])ne.has(n)&&ne.registerOpAttr(n,"broadcastViewSafe",!0);l(e1,"broadcastViewSafeForUser");NA();OA();RA();EA();CA();PA();FA();UA();KA();YA();QA();l(u$,"topologicalOps");l(Tm,"lowerGraphToPrimFunc")});var t1={};at(t1,{CPUTarget:()=>go,CUDATarget:()=>xm,TargetFeatures:()=>Fa,TargetKind:()=>Dt,WasmTarget:()=>Sm,WebGPUTarget:()=>vm});var Dt,Fa,go,xm,Sm,vm,yo=M(()=>{"use strict";Dt=Object.freeze({CPU:"cpu",CUDA:"cuda",WEBGPU:"webgpu",WASM:"wasm",ACCELERATOR:"accelerator"}),Fa=class{static{l(this,"TargetFeatures")}constructor(e){this.kind=e.kind,this.name=e.name,this.vectorWidth=e.vectorWidth||1,this.numCores=e.numCores||1,this.maxThreadsPerBlock=e.maxThreadsPerBlock||1,this.maxBlockDimX=e.maxBlockDimX||1,this.maxBlockDimY=e.maxBlockDimY||1,this.maxBlockDimZ=e.maxBlockDimZ||1,this.maxGridDimX=e.maxGridDimX||1,this.maxGridDimY=e.maxGridDimY||1,this.maxGridDimZ=e.maxGridDimZ||1,this.sharedMemoryBytes=e.sharedMemoryBytes||0,this.memoryBudgetBytes=e.memoryBudgetBytes||0,this.registersPerThread=e.registersPerThread||0,this.warpSize=e.warpSize||1,this.memoryBandwidthGBs=e.memoryBandwidthGBs||0,this.computeTFLOPs=e.computeTFLOPs||0,this.cacheLineSizeBytes=e.cacheLineSizeBytes||64,this.l1CacheBytes=e.l1CacheBytes||0,this.l2CacheBytes=e.l2CacheBytes||0,this.supportsFloat16=e.supportsFloat16??!1,this.supportsTensorCore=e.supportsTensorCore??!1,this.libraryOps=e.libraryOps||new Set,this.enableEpilogueFusion=e.enableEpilogueFusion??!1,this.preferredConvLayout=e.preferredConvLayout||null,this.layoutAwareOps=e.layoutAwareOps instanceof Set?e.layoutAwareOps:new Set(e.layoutAwareOps||[]),this.preferredBlockFactor=e.preferredBlockFactor||0,this.supportsBlockedLayout=e.supportsBlockedLayout??!1,this.supportsInt8=e.supportsInt8??!1,this.simd=e.simd??!1,this.host=e.host||null,this.attrs=new Map(Object.entries(e.attrs||{}))}getAttr(e,t=null){return this.attrs.has(e)?this.attrs.get(e):t}hasAttr(e){return this.attrs.has(e)}withAttr(e,t){return this.attrs.set(e,t),this}isGPU(){return this.kind===Dt.CUDA||this.kind===Dt.WEBGPU}isWebGPU(){return this.kind===Dt.WEBGPU}isCPU(){return this.kind===Dt.CPU}isWasm(){return this.kind===Dt.WASM}supportsThreadBinding(){return this.isGPU()||this.isWebGPU()}supportsVectorization(){return this.vectorWidth>1}maxParallelism(){return this.isGPU()?this.maxThreadsPerBlock*this.maxGridDimX:this.numCores}supportsSimd(){return this.simd&&this.vectorWidth>1}hasLibraryOp(e){return this.libraryOps.has(e)}},go=l((n={})=>new Fa({kind:Dt.CPU,name:"cpu_generic",vectorWidth:8,numCores:8,cacheLineSizeBytes:64,l1CacheBytes:32*1024,l2CacheBytes:256*1024,memoryBandwidthGBs:50,computeTFLOPs:.5,supportsBlockedLayout:!0,preferredBlockFactor:8,supportsInt8:!0,...n}),"CPUTarget"),xm=l((n={})=>new Fa({kind:Dt.CUDA,name:"cuda_generic",vectorWidth:1,numCores:80,maxThreadsPerBlock:1024,maxBlockDimX:1024,maxBlockDimY:1024,maxBlockDimZ:64,maxGridDimX:2147483647,maxGridDimY:65535,maxGridDimZ:65535,sharedMemoryBytes:48*1024,registersPerThread:255,warpSize:32,memoryBandwidthGBs:900,computeTFLOPs:15,supportsFloat16:!0,supportsTensorCore:!1,libraryOps:new Set(["dot","conv"]),enableEpilogueFusion:!0,supportsInt8:!0,...n}),"CUDATarget"),Sm=l((n={})=>new Fa({kind:Dt.WASM,name:"wasm_generic",vectorWidth:4,numCores:1,cacheLineSizeBytes:64,memoryBandwidthGBs:10,computeTFLOPs:.1,supportsInt8:!0,simd:!0,...n}),"WasmTarget"),vm=l((n={})=>new Fa({kind:Dt.WEBGPU,name:"webgpu_generic",vectorWidth:1,numCores:32,maxThreadsPerBlock:256,maxBlockDimX:256,maxBlockDimY:256,maxBlockDimZ:64,maxGridDimX:65535,maxGridDimY:65535,maxGridDimZ:65535,sharedMemoryBytes:16384,warpSize:32,memoryBandwidthGBs:400,computeTFLOPs:8,supportsFloat16:!0,...n}),"WebGPUTarget")});function ml(n,e,t,r,s=!1){if(e.length===0)return"0";if(e.length===1)return t(e[0]);let o=[];for(let i=0;i<e.length;i++){let a=t(e[i]);if(s&&a==="0")continue;let u=n.strides[i];u===1?o.push(a):typeof u=="number"&&u>=0?o.push(`${a} * ${u}`):o.push(`${a} * ${r(n,i)}`)}return o.length===0?"0":o.join(" + ")}var km=M(()=>{"use strict";l(ml,"flattenRowMajorIndex")});function Vi(n){return c$[n]||"f32"}function br(n){if(!n)return"f32";if(n._dtype)return n._dtype;switch(n.type){case"IntImmNode":return"i32";case"FloatImmNode":return"f32";case"LIRFlatLoadNode":return Vi(n.dtype);case"BufferLoadNode":return Vi(n.buffer.dtype);case"CastNode":return Vi(n.toDtype);case"CallExternNode":return Vi(n.dtype||"f32");case"CompareNode":return"i32";case"VariableNode":return Vi(n.dtype||"i32");case"MathOpNode":{let e=br(n.a);if(gr(e))return e;if(n.b){let t=br(n.b);if(gr(t))return t}return e}case"IfThenElseNode":return br(n.thenBody);default:return"f32"}}function si(n){!n||typeof n!="object"||!n.type||(n._dtype=br(n))}function n1(n){return l$.has(n)}var ri,Nm,xp,Sp,Am,gl,Om,l$,c$,vp=M(()=>{"use strict";tr();ri=class n{static{l(this,"LIRNode")}constructor(){this.type=this.constructor.name.replace(/^_+/,""),this._parent=null,this._parentKey=null,this._parentIdx=-1}_setChild(e,t,r=-1){(t instanceof n||t&&t._parent!==void 0)&&(t._parent=this,t._parentKey=e,t._parentIdx=r)}_setChildren(e,t){if(t)for(let r=0;r<t.length;r++){let s=t[r];(s instanceof n||s&&s._parent!==void 0)&&(s._parent=this,s._parentKey=e,s._parentIdx=r)}}replaceWith(e){let t=this._parent;return t?(this._parentIdx>=0?t[this._parentKey][this._parentIdx]=e:t[this._parentKey]=e,e&&(e instanceof n||e._parent!==void 0)&&(e._parent=t,e._parentKey=this._parentKey,e._parentIdx=this._parentIdx),this._parent=null,this._parentKey=null,this._parentIdx=-1,!0):!1}},Nm=class extends ri{static{l(this,"LIRFunc")}constructor(e,t,r,s,o,i,a){super(),this.name=e,this.params=t,this.body=r,this.bufferMap=s,this.shapeParams=o,this.shapeParamMap=i,this.metadata=a,this._setChild("body",r)}},xp=class extends ri{static{l(this,"LIRFlatLoadNode")}constructor(e,t,r){super(),this.buffer=e,this.offsetExpr=t,this.dtype=r,this._setChild("offsetExpr",t)}},Sp=class extends ri{static{l(this,"LIRFlatStoreNode")}constructor(e,t,r,s){super(),this.buffer=e,this.offsetExpr=t,this.value=r,this.dtype=s,this._setChild("offsetExpr",t),this._setChild("value",r)}},Am=class extends ri{static{l(this,"LIRAccumulatorNode")}constructor(e){super(),this.localName=e.localName,this.dtype=e.dtype,this.op=e.op||"+",this.initLoad=e.initLoad,this.loopVar=e.loopVar,this.extent=e.extent,this.loopKind=e.loopKind,this.body=e.body,this.flushStore=e.flushStore,this.initBody=e.initBody||null,this._setChild("initLoad",e.initLoad),this._setChild("body",e.body),this._setChild("flushStore",e.flushStore),this._setChild("initBody",e.initBody||null)}},gl=class extends ri{static{l(this,"LIRBindingsNode")}constructor(e,t){super(),this.bindings=e,this.body=t,this._setChild("body",t)}},Om=class{static{l(this,"LIRMetadata")}constructor(){this.locals=new Map,this.externCalls=new Map,this.memoryLayout={bufferOffsets:new Map,totalBytes:0,alignment:16},this.threadBindings=new Map,this.sharedBuffers=[],this.zeroBuffers=new Set,this.constantBuffers=new Map,this.usedBuffers=new Map,this.allocatedBuffers=new Set,this.paramBuffers=new Set}},l$=new Set(["sqrt","abs","ceil","floor","min","max"]),c$={int32:"i32",index:"i32",i32:"i32",float32:"f32",f32:"f32",f64:"f64",f16:"f32",bf16:"f32",i8:"i32",i16:"i32",i64:"i64",ui8:"i32",bool:"i32"};l(Vi,"normalizeDtype");l(br,"inferDtype");l(si,"annotateDtype");l(n1,"isWasmNativeOp")});function nr(n){return n instanceof Bt||n instanceof ri}function Ae(n,e,t={}){return{key:n,kind:e,array:!!t.array,poly:!!t.poly,bind:!!t.bind,param:!!t.param,region:!!t.region,iterVarBinding:!!t.iterVarBinding,bindingsExpr:!!t.bindingsExpr}}function G0(n){return{kinds:n.kinds||"both",descendParams:n.descendParams===!0,bindVars:n.bindVars!==!1}}function p$(n,e){return e.iterVarBinding?{read(){return(n.iterVars||[]).map(t=>t&&t.binding).filter(nr)},write(t){let r=0;for(let s of n.iterVars||[])s&&nr(s.binding)&&(s.binding=t[r++])}}:e.bindingsExpr?{read(){return(n.bindings||[]).map(t=>t&&t.expr).filter(nr)},write(t){let r=0;for(let s of n.bindings||[])s&&nr(s.expr)&&(s.expr=t[r++])}}:e.poly?{read(){let t=n[e.key];return Array.isArray(t)?t.filter(nr):nr(t)?[t]:[]},write(t){let r=n[e.key];if(Array.isArray(r)){let s=0;n[e.key]=r.map(o=>nr(o)?t[s++]:o)}else t.length&&(n[e.key]=t[0])}}:e.array?{read(){let t=n[e.key];return Array.isArray(t)?t.filter(nr):[]},write(t){let r=n[e.key];if(!Array.isArray(r))return;let s=0;n[e.key]=r.map(o=>nr(o)?t[s++]:o)}}:{read(){return nr(n[e.key])?[n[e.key]]:[]},write(t){t.length&&nr(n[e.key])&&(n[e.key]=t[0])}}}function j0(n,e={}){let t=G0(e),r=V0[n.type];if(r===void 0)throw new Error(`ir_visitor: no child schema for node type '${n.type}'`);let s=[];for(let o of r)o.region||o.param&&!t.descendParams||o.bind&&!t.bindVars||t.kinds!=="both"&&o.kind!==t.kinds||s.push(p$(n,o));return s}function ln(n,e={}){if(!nr(n)||V0[n.type]===void 0)return[];let t=[];for(let r of j0(n,e)){let s=r.read();for(let o=0;o<s.length;o++)t.push(s[o])}return t}function r1(n,e,t,r){n._parent=e,n._parentKey=t,n._parentIdx=r}function f$(n){let e=V0[n.type];if(e)for(let t of e){if(t.iterVarBinding||t.bindingsExpr||t.region)continue;let r=n[t.key];if(Array.isArray(r))for(let s=0;s<r.length;s++)nr(r[s])&&r1(r[s],n,t.key,s);else nr(r)&&r1(r,n,t.key,-1)}}function s1(n,e,t,r,s,o){if(o.stop||!nr(n))return;let i={parent:r,depth:s},a=!0;if(e.pre){let u=e.pre(n,i);if(u===Bm){o.stop=!0;return}u===!1&&(a=!1)}if(a)for(let u of j0(n,t)){let p=u.read();for(let c=0;c<p.length;c++)if(s1(p[c],e,t,n,s+1,o),o.stop)return}e.post&&e.post(n,i)}function qs(n,e,t={}){s1(n,typeof e=="function"?{pre:e}:e||{},G0(t),null,0,{stop:!1})}function yl(n,e,t={}){let r=[];return qs(n,s=>{e(s)&&r.push(s)},t),r}function kp(n,e,t={}){let r=!1;return qs(n,s=>{if(e(s))return r=!0,Bm},t),r}function Gi(n,e,t={}){let r=G0(t);return o1(n,e,r)}function o1(n,e,t){if(!nr(n))return n;let r=!1;for(let o of j0(n,t)){let i=o.read();if(i.length===0)continue;let a=!1,u=new Array(i.length);for(let p=0;p<i.length;p++){let c=o1(i[p],e,t);c!==i[p]&&(a=!0),u[p]=c}a&&(o.write(u),r=!0)}r&&f$(n);let s=e(n);return s??n}var Bm,V0,ts=M(()=>{"use strict";Fe();vp();Bm=Symbol("ir-visitor-stop");l(nr,"isIRNode");l(Ae,"field");V0={VariableNode:[],IntImmNode:[],FloatImmNode:[],SyncThreadsNode:[],MathOpNode:[Ae("a","expr"),Ae("b","expr")],CompareNode:[Ae("a","expr"),Ae("b","expr")],CastNode:[Ae("expr","expr")],CallExternNode:[Ae("args","expr",{array:!0})],BufferLoadNode:[Ae("indices","expr",{array:!0})],IfThenElseNode:[Ae("condition","expr"),Ae("thenBody","stmt"),Ae("elseBody","stmt")],BlockRealizeNode:[Ae("binding","expr",{poly:!0}),Ae("iterVar","expr",{bind:!0})],LIRFlatLoadNode:[Ae("offsetExpr","expr")],PrimFunc:[Ae("params","expr",{array:!0,param:!0}),Ae("body","stmt")],ForNode:[Ae("min","expr"),Ae("extent","expr"),Ae("loopVar","expr",{bind:!0}),Ae("body","stmt")],BlockNode:[Ae("iterVars","expr",{iterVarBinding:!0}),Ae("reads","expr",{region:!0,array:!0}),Ae("writes","expr",{region:!0,array:!0}),Ae("initBody","stmt"),Ae("body","stmt")],SeqNode:[Ae("stmts","stmt",{array:!0})],VecCopyNode:[Ae("dstIndex","expr"),Ae("srcIndex","expr")],LetStmtNode:[Ae("value","expr"),Ae("variable","expr",{bind:!0}),Ae("body","stmt")],AllocateNode:[Ae("body","stmt")],WhileNode:[Ae("condBody","stmt"),Ae("condVar","expr",{bind:!0}),Ae("loopBody","stmt")],EvaluateNode:[Ae("value","expr")],BufferStoreNode:[Ae("indices","expr",{array:!0}),Ae("value","expr")],LIRFunc:[Ae("body","stmt")],LIRFlatStoreNode:[Ae("offsetExpr","expr"),Ae("value","expr")],LIRAccumulatorNode:[Ae("loopVar","expr",{bind:!0}),Ae("extent","expr"),Ae("initLoad","stmt"),Ae("initBody","stmt"),Ae("body","expr"),Ae("flushStore","stmt")],LIRBindingsNode:[Ae("bindings","expr",{bindingsExpr:!0}),Ae("body","stmt")]};l(G0,"normOpts");l(p$,"buildAccessor");l(j0,"childAccessors");l(ln,"irChildNodes");l(r1,"setParent");l(f$,"relink");l(s1,"walkInner");l(qs,"walk");l(yl,"collect");l(kp,"some");l(Gi,"transform");l(o1,"transformInner")});function d$(n,e,t,r){switch(n){case"add":return`(${e} + ${t})`;case"sub":return`(${e} - ${t})`;case"mul":return`(${e} * ${t})`;case"neg":return`(-${e})`;case"div":return r==="js"?`((${e} / ${t}) | 0)`:`(${e} / ${t})`;case"mod":return r==="js"?`((${e} % ${t} + ${t}) % ${t})`:`(${e} % ${t})`;case"ceildiv":return r==="js"?`(((${e} + ${t} - 1) / ${t}) | 0)`:`((${e} + ${t} - 1) / ${t})`;case"max":return r==="js"?`Math.max(${e}, ${t})`:`max(${e}, ${t})`;case"min":return r==="js"?`Math.min(${e}, ${t})`:`min(${e}, ${t})`;default:throw new Error(`emitSymInt: unsupported op '${n}'`)}}function U0(n,e,t="c"){if(typeof n=="number"||!(n instanceof Pe))return String(n);if(n.type==="var")return e({name:qu(n.name)});if(t==="wat")throw new Error("emitSymInt: compound symbolic expressions are not supported on the WASM backend");let r=U0(n.args[0],e,t),s=n.args.length>1?U0(n.args[1],e,t):null;return d$(n.type,r,s,t)}function za(n){let e=n.indexOf(".");if(e<0)return null;let t=n.charCodeAt(e+1)-120;if(t<0||t>2)return null;let r=n.substring(0,e);return r==="threadIdx"?{space:"thread",axis:t}:r==="blockIdx"?{space:"block",axis:t}:null}function Rm(n,e){let t=[e];for(;t.length>0;){let r=t.pop();if(r)switch(r.type){case"SeqNode":for(let s=r.stmts.length-1;s>=0;s--)t.push(r.stmts[s]);continue;case"AllocateNode":n._visitAllocateNode(r),t.push(r.body);continue;case"ForNode":n._visitForNode(r);continue;case"BlockNode":n._visitBlockNode(r);continue;case"IfThenElseNode":n._visitIfStmt(r);continue;case"LetStmtNode":n._visitLetStmtNode(r);continue;case"BufferStoreNode":n._visitBufferStoreNode(r);continue;case"LIRFlatStoreNode":n._visitLIRFlatStore(r);continue;case"LIRBindingsNode":n._visitLIRBindings(r);continue;case"LIRAccumulatorNode":n._visitLIRAccumulator(r);continue;case"WhileNode":n._visitWhileNode(r);continue;case"SyncThreadsNode":n._emitSync();continue;case"VecCopyNode":n._visitVecCopyNode(r);continue;case"EvaluateNode":continue;default:throw new Error(`${n.constructor.name}: unhandled statement node '${r.type}'`)}}}function _l(n,e){let t=[n];for(;t.length>0;){let r=t.pop();if(!(!r||typeof r!="object")&&e(r)!==!1)for(let s of h$){let o=r[s];if(o!=null)if(Array.isArray(o))for(let i=o.length-1;i>=0;i--)t.push(o[i]);else t.push(o)}}}function Mm(n){let e=n;for(;e;){if(e.type==="ForNode"||e.type==="BlockNode"){e=e.body;continue}if(e.type==="BufferStoreNode"||e.type==="LIRFlatStoreNode"){let t=e.value;return t.type==="FloatImmNode"&&t.value===0||t.type==="IntImmNode"&&t.value===0}return!1}return!1}function ji(n,e,t,r,s,o="c"){let i=e.shape[t];if(i instanceof Pe)return U0(i,r,o);if(n&&n.shapeParamMap){let a=n.shapeParamMap.get(`${e.name}:${t}`);if(a)return r(a)}throw new Error(`${s} codegen: missing shape param for ${e.name}:${t}`)}function Im(n){let e=1;for(let t of n.shape)typeof t=="number"&&t>0&&(e*=t);return e}function Ui(n,e,t){let r=[];for(let s=e;s<n.shape.length;s++){let o=n.shape[s];typeof o=="number"&&o>=0?r.push(String(o)):r.push(t(n,s))}return r.length===0?"1":r.join(" * ")}function Em(n,e){let t=n.get(e);if(!t)return 0;let r=0;for(let s of t)s.extent>r&&(r=s.extent);return r}var h$,Np=M(()=>{"use strict";po();l(d$,"symOpToString");l(U0,"emitSymInt");l(za,"parseThreadAxis");l(Rm,"visitStatements");h$=["body","stmts","thenBody","elseBody","loopBody","condBody","initBody"];l(_l,"walkStmtTree");l(Mm,"isZeroFillBody");l(ji,"resolveShapeParam");l(Im,"estimateBufferSize");l(Ui,"dynamicDimProduct");l(Em,"maxBindingExtent")});function Wi(n){let e=n>=0?1:-1,t=Math.abs(n),r=1/(1+Va*t),s=0;for(let o=qi.length-1;o>=0;o--)s=s*r+qi[o];return e*(1-s*r*Math.exp(-t*t))}function Ki(n){return 1-Wi(n)}function cn(n){if(n<.5)return Math.log(Math.PI/Math.abs(Math.sin(Math.PI*n)))-cn(1-n);let e=n-1,t=oi[0];for(let s=1;s<oi.length;s++)t+=oi[s]/(e+s);let r=e+7+.5;return .5*Math.log(2*Math.PI)+(e+.5)*Math.log(r)-r+Math.log(t)}function Hi(n){return n<.5?Math.PI/(Math.sin(Math.PI*n)*Hi(1-n)):Math.exp(cn(n))}function ja(n){let e=0,t=n;for(;t<ii;)e-=1/t,t+=1;let r=1/t,s=r*r;e+=Math.log(t)-.5*r;let o=s;for(let i of Ga)e+=i*o,o*=s;return e}var oi,qi,Va,ii,Ga,Yi=M(()=>{"use strict";oi=[.9999999999998099,676.5203681218851,-1259.1392167224028,771.3234287776531,-176.6150291621406,12.507343278686905,-.13857109526572012,9984369578019572e-21,15056327351493116e-23],qi=[.254829592,-.284496736,1.421413741,-1.453152027,1.061405429],Va=.3275911,ii=6,Ga=[-1/12,1/120,-1/252,1/240,-1/132];l(Wi,"erfScalar");l(Ki,"erfcScalar");l(cn,"lgammaScalar");l(Hi,"gammaScalar");l(ja,"digammaScalar")});function u1(n){return ai[0]=(n&32767)<<13,Ji[0]*=a1,Ji[0]>=65536&&(ai[0]|=2139095040),ai[0]|=(n&32768)<<16,Ji[0]}function l1(n){Ji[0]=n;let e=ai[0]&2147483648,t=(ai[0]^e)>>>0,r;if(t>=1199570944)r=t>2139095040?32256:31744;else if(t<947912704)ai[0]=t,Ji[0]+=.5,r=ai[0]-1056964608&65535;else{let s=t>>13&1;t=t+i1>>>0,t=t+s>>>0,r=t>>13&65535}return(r|e>>>16)&65535}function Lm(n){return ai[0]=n<<16>>>0,Ji[0]}function Cm(n){Ji[0]=n;let e=ai[0];if((e>>>23&255)===255)return e>>>16&65535;let t=32767+(e>>>16&1)>>>0;return e+t>>>0>>>16&65535}function Dm(n,e){return n==="f16"?l1(Number(e)):n==="bf16"?Cm(Number(e)):n==="i64"?typeof e=="bigint"?e:BigInt(Math.trunc(e)):e}function Pm(n,e){return n==="f16"?u1(Number(e)):n==="bf16"?Lm(Number(e)):n==="i64"&&typeof e=="bigint"?Number(e):e}var Ji,ai,i1,a1,q0,$m,bl=M(()=>{"use strict";Ji=new Float32Array(1),ai=new Uint32Array(Ji.buffer),i1=(-112<<23>>>0)+4095>>>0,a1=5192296858534828e18;l(u1,"f16ToF32");l(l1,"f32ToF16");l(Lm,"bf16ToF32");l(Cm,"f32ToBf16");l(Dm,"coerceForStorage");l(Pm,"readFromStorage");q0={F16_ADD_BIAS:i1|0,F16_MAGIC_MUL:a1},$m=globalThis;$m.__mlfw_f16_to_f32=u1;$m.__mlfw_f32_to_f16=l1;$m.__mlfw_bf16_to_f32=Lm;$m.__mlfw_f32_to_bf16=Cm});function p1(n){return`((x_erf => { ${g$} })(${n}))`}function _$(n){return`((x_lg => { const lg = ${f1}; return x_lg < 0.5 ? Math.log(Math.PI / Math.abs(Math.sin(Math.PI * x_lg))) - lg(1 - x_lg) : lg(x_lg); })(${n}))`}function b$(n){return`((x_g => { const lg = ${f1}; return x_g < 0.5 ? Math.PI / (Math.sin(Math.PI * x_g) * Math.exp(lg(1 - x_g))) : Math.exp(lg(x_g)); })(${n}))`}var W0,c1,m$,g$,y$,f1,Fm,d1=M(()=>{"use strict";Fe();tr();km();ts();Np();Yi();bl();W0={f16:"__mlfw_f16_to_f32",bf16:"__mlfw_bf16_to_f32"},c1={f16:"__mlfw_f32_to_f16",bf16:"__mlfw_f32_to_bf16"},m$=qi.slice().reverse().reduce((n,e)=>`(${e} + t * ${n})`,"0"),g$=`const t = 1.0 / (1.0 + ${Va} * Math.abs(x_erf)); const p = t * ${m$}; return (x_erf >= 0 ? 1 : -1) * (1.0 - p * Math.exp(-x_erf * x_erf));`;l(p1,"_erfExpr");y$=oi.map((n,e)=>e===0?`${n}`:`${n} / (zz + ${e})`).join(" + "),f1=`(zg => { const zz = zg - 1; const s = ${y$}; const t = zz + ${7} + 0.5; return ${.5*Math.log(2*Math.PI)} + (zz + 0.5) * Math.log(t) - t + Math.log(s); })`;l(_$,"_lgammaExpr");l(b$,"_gammaExpr");Fm=class{static{l(this,"CPUCodegen")}constructor(e){this.target=e,this._indent=0,this._lines=[],this._loopStack=[]}generate(e){this._indent=0,this._lines=[],this._aliases=new Map,this._accTarget=null,this._accVar=null,this._accCounter=0;let t=e.type==="LIRFunc",r=new Set,s=[];for(let[,c]of e.bufferMap)s.push(c.name),r.add(c.name);for(let c of e.shapeParams)s.push(c.name);this._paramBuffers=r,this._readBuffers=new Set;let o,i,a,u;t?(o=e.metadata.usedBuffers,i=e.metadata.allocatedBuffers,a=e.metadata.zeroBuffers,u=e.metadata.constantBuffers):(o=new Map,i=new Set,this._scanTree(e.body,o,i,this._readBuffers),a=this._findZeroOnlyBuffers(e.body,r),u=this._constantBuffers),this._zeroBuffers=a,this._constantBuffers=u,this._localBuffers=new Set,this._primFunc=e,this._emit(`function ${e.name}(${s.join(", ")}) {`),this._indent++;let p=0;for(let[,c]of o)if(c.poolByteOffset!==void 0&&c.poolByteOffset!==null){let f=c.numel();f>0&&(p=Math.max(p,c.poolByteOffset+f*Rn(c.dtype)))}p>0&&this._emit(`const _mem_pool = new ArrayBuffer(${p});`);for(let[c,f]of o)if(!a.has(c)&&!u.has(c)&&!r.has(c)&&!i.has(c)){let d=f.numel();if(d>0)this._emit(`const ${c} = ${this._allocRhs(f,d)};`);else if(d<0){let h=[];for(let m=0;m<f.shape.length;m++)h.push(typeof f.shape[m]=="number"&&f.shape[m]>=0?String(f.shape[m]):this._resolveShapeParam(f,m));this._emit(`const ${c} = new ${Ph(f.dtype)}(${h.join(" * ")});`)}}for(let[,c]of o)r.has(c.name)||this._localBuffers.add(c.name);return this._visitNode(e.body),this._indent--,this._emit("}"),this._cleanupSource(this._lines.join(`
|
|
2
|
+
`))}_emit(e){this._lines.push(" ".repeat(this._indent)+e)}_allocRhs(e,t){let r=Ph(e.dtype);return e.poolByteOffset!==void 0&&e.poolByteOffset!==null&&t>0?`new ${r}(_mem_pool, ${e.poolByteOffset}, ${t})`:`new ${r}(${t})`}_wrapLoad(e,t){let r=W0[e];return r?`${r}(${t})`:t}_wrapStoreVal(e,t){let r=c1[e];return r?`${r}(${t})`:e==="i64"?`BigInt(${t})`:t}_zeroLit(e){return e==="i64"?"0n":"0"}_visitNode(e){let t=e;for(;t;)switch(t.type){case"SeqNode":for(let r=0;r<t.stmts.length-1;r++)this._visitNode(t.stmts[r]);t=t.stmts[t.stmts.length-1];continue;case"AllocateNode":{let r=t.buffer,s=r.numel();s>0?this._emit(`const ${r.name} = ${this._allocRhs(r,s)};`):s<0&&this._emit(`const ${r.name} = new ${Ph(r.dtype)}(${this._dynamicNumel(r)});`),t=t.body;continue}case"LetStmtNode":this._emit(`const ${t.variable.name} = ${this._exprToJS(t.value)};`),t=t.body;continue;case"ForNode":this._visitForNode(t);return;case"BlockNode":this._visitBlockNode(t);return;case"IfThenElseNode":this._visitIfThenElseStmt(t);return;case"BufferStoreNode":this._visitBufferStoreNode(t);return;case"LIRFlatStoreNode":this._visitLIRFlatStore(t);return;case"LIRBindingsNode":this._visitLIRBindings(t);return;case"LIRAccumulatorNode":this._visitLIRAccumulator(t);return;case"WhileNode":this._visitWhileNode(t);return;case"EvaluateNode":return;case"SyncThreadsNode":return;default:throw new Error(`CPU codegen: unhandled statement node '${t.type}'`)}}_visitForNode(e){if(this._isRedundantZeroFill(e))return;let t=e.loopVar.name;if(e.extent.type==="IntImmNode"&&e.extent.value===1){this._aliases.set(t,"0"),this._visitNode(e.body);return}let r=this._exprToJS(e.extent);if(e.kind===z.UNROLLED){let o=e.extent.type==="IntImmNode"?e.extent.value:null;if(o&&o<=32&&!this._isZeroFillBody(e.body)){for(let i=0;i<o;i++)this._emit("{ const "+t+" = "+i+";"),this._indent++,this._loopStack.push(t),this._visitNode(e.body),this._loopStack.pop(),this._indent--,this._emit("}");return}}let s=this._detectReductionAcc(e);if(s){let o="_acc_"+(this._accCounter=(this._accCounter||0)+1),i=this._accTarget,a=this._accVar;this._accTarget=s,this._accVar=o,this._emit("let "+o+" = "+this._wrapLoad(s.dtype,s.bufName+"["+s.idxExpr+"]")+";"),this._emit("for (let "+t+" = 0; "+t+" < "+r+"; "+t+"++) {"),this._indent++,this._loopStack.push(t),this._visitNode(e.body),this._loopStack.pop(),this._indent--,this._emit("}"),this._emit(s.bufName+"["+s.idxExpr+"] = "+this._wrapStoreVal(s.dtype,o)+";"),this._accTarget=i,this._accVar=a;return}this._emit("for (let "+t+" = 0; "+t+" < "+r+"; "+t+"++) {"),this._indent++,this._loopStack.push(t),this._visitNode(e.body),this._loopStack.pop(),this._indent--,this._emit("}")}_visitLIRFlatStore(e){if(!(this._zeroBuffers&&this._zeroBuffers.has(e.buffer.name))&&!(this._constantBuffers&&this._constantBuffers.has(e.buffer.name))){if(this._accTarget&&e.buffer.name===this._accTarget.bufName&&this._exprToJS(e.offsetExpr)===this._accTarget.idxExpr){this._emit(this._accVar+" = "+this._exprToJS(e.value)+";");return}this._emit(e.buffer.name+"["+this._exprToJS(e.offsetExpr)+"] = "+this._wrapStoreVal(e.dtype||e.buffer.dtype,this._exprToJS(e.value))+";")}}_visitLIRBindings(e){for(let t of e.bindings){let r=this._exprToJS(t.expr);this._aliases.set(t.name,r)}this._visitNode(e.body)}_visitLIRAccumulator(e){let t=e.localName,r=this._exprToJS(e.initLoad);this._emit("let "+t+" = "+r+";");let s=this._accTarget,o=this._accVar;this._accTarget={bufName:e.flushStore.buffer.name,idxExpr:this._exprToJS(e.flushStore.offsetExpr)},this._accVar=t;let i=e.loopVar.name,a=this._exprToJS(e.extent);this._emit("for (let "+i+" = 0; "+i+" < "+a+"; "+i+"++) {"),this._indent++,this._loopStack.push(i);let u=this._exprToJS(e.body),p=e.op||"+",c;p==="max"?c="Math.max("+t+", "+u+")":p==="min"?c="Math.min("+t+", "+u+")":c="("+t+" "+p+" "+u+")",this._emit(t+" = "+c+";"),this._loopStack.pop(),this._indent--,this._emit("}"),this._emit(e.flushStore.buffer.name+"["+this._accTarget.idxExpr+"] = "+this._wrapStoreVal(e.flushStore.dtype||e.flushStore.buffer.dtype,t)+";"),this._accTarget=s,this._accVar=o}_detectReductionAcc(e){let t=e.body;if(!t||t.type!=="BlockNode")return null;let r=t.body;if(!r||r.type!=="BufferStoreNode")return null;let s=r,o=s.value;if(!o||o.type!=="MathOpNode"||o.op!=="+")return null;let i=null;if(o.a&&o.a.type==="BufferLoadNode"&&o.a.buffer.name===s.buffer.name?i=o.a:o.b&&o.b.type==="BufferLoadNode"&&o.b.buffer.name===s.buffer.name&&(i=o.b),!i)return null;for(let c of t.iterVars)c.iterVar&&c.binding&&this._aliases.set(c.iterVar.name,this._exprToJS(c.binding));let a=this._flatIndex(s.buffer,s.indices),u=this._flatIndex(i.buffer,i.indices);if(a!==u)return null;let p=e.loopVar.name;return a.includes(p)?null:{bufName:s.buffer.name,idxExpr:a,dtype:s.buffer.dtype}}_visitBlockNode(e){for(let t of e.iterVars)if(t.iterVar&&t.binding){let r=this._exprToJS(t.binding);this._aliases.set(t.iterVar.name,r)}if(e.initBody){let t=this._loopStack.length>0?this._loopStack[this._loopStack.length-1]:null;t?(this._emit("if ("+t+" === 0) {"),this._indent++,this._visitNode(e.initBody),this._indent--,this._emit("}")):this._visitNode(e.initBody)}this._visitNode(e.body)}_visitIfThenElseStmt(e){this._emit(`if (${this._exprToJS(e.condition)}) {`),this._indent++,this._visitNode(e.thenBody),this._indent--,e.elseBody&&(this._emit("} else {"),this._indent++,this._visitNode(e.elseBody),this._indent--),this._emit("}")}_visitWhileNode(e){this._visitNode(e.condBody),this._emit(`while (${e.condVar.name}[0]) {`),this._indent++,this._visitNode(e.loopBody),this._visitNode(e.condBody),this._indent--,this._emit("}")}_visitBufferStoreNode(e){if(!(this._zeroBuffers&&this._zeroBuffers.has(e.buffer.name))&&!(this._constantBuffers&&this._constantBuffers.has(e.buffer.name))){if(this._accTarget&&e.buffer.name===this._accTarget.bufName&&this._flatIndex(e.buffer,e.indices)===this._accTarget.idxExpr){this._emit(this._accVar+" = "+this._exprToJS(e.value)+";");return}this._emit(e.buffer.name+"["+this._flatIndex(e.buffer,e.indices)+"] = "+this._wrapStoreVal(e.buffer.dtype,this._exprToJS(e.value))+";")}}_exprToJS(e){if(!e)return"0";let t=[],r=[{node:e,phase:0}];for(;r.length>0;){let s=r[r.length-1],o=s.node;if(!o){r.pop(),t.push("0");continue}switch(o.type){case"IntImmNode":r.pop(),t.push(String(o.value));continue;case"FloatImmNode":r.pop(),t.push(String(o.value));continue;case"VariableNode":r.pop(),t.push(this._aliases.get(o.name)||o.name);continue;case"BufferLoadNode":{if(r.pop(),this._zeroBuffers&&this._zeroBuffers.has(o.buffer.name))t.push(this._zeroLit(o.buffer.dtype));else if(this._constantBuffers&&this._constantBuffers.has(o.buffer.name)){let i=this._constantBuffers.get(o.buffer.name);t.push(o.buffer.dtype==="i64"?`BigInt(${i})`:i)}else this._accTarget&&o.buffer.name===this._accTarget.bufName&&this._flatIndex(o.buffer,o.indices)===this._accTarget.idxExpr?t.push(this._accVar):t.push(this._wrapLoad(o.buffer.dtype,o.buffer.name+"["+this._flatIndex(o.buffer,o.indices)+"]"));continue}case"LIRFlatLoadNode":{if(r.pop(),this._zeroBuffers&&this._zeroBuffers.has(o.buffer.name))t.push(this._zeroLit(o.dtype||o.buffer.dtype));else if(this._constantBuffers&&this._constantBuffers.has(o.buffer.name)){let i=String(this._constantBuffers.get(o.buffer.name));t.push((o.dtype||o.buffer.dtype)==="i64"?`BigInt(${i})`:i)}else this._accTarget&&o.buffer.name===this._accTarget.bufName&&this._exprToJS(o.offsetExpr)===this._accTarget.idxExpr?t.push(this._accVar):t.push(this._wrapLoad(o.dtype||o.buffer.dtype,o.buffer.name+"["+this._exprToJS(o.offsetExpr)+"]"));continue}case"MathOpNode":if(s.phase===0)s.phase=1,r.push({node:o.a,phase:0});else if(s.phase===1&&o.b)s.phase=2,r.push({node:o.b,phase:0});else if(r.pop(),!o.b)t.push(`(${o.op}${t.pop()})`);else{let i=t.pop(),a=t.pop();(o.op==="+"||o.op==="-")&&i==="0"?t.push(a):o.op==="+"&&a==="0"?t.push(i):o.op==="*"&&(a==="0"||i==="0")?t.push("0"):o.op==="*"&&i==="1"?t.push(a):o.op==="*"&&a==="1"?t.push(i):o.op==="%"?t.push(`((${a} % ${i} + ${i}) % ${i})`):o.op==="//"?t.push(`((${a} / ${i}) | 0)`):t.push(`(${a} ${o.op} ${i})`)}continue;case"CompareNode":if(s.phase===0)s.phase=1,r.push({node:o.a,phase:0});else if(s.phase===1)s.phase=2,r.push({node:o.b,phase:0});else{r.pop();let i=t.pop(),a=t.pop();t.push(`(${a} ${IN(o.direction)} ${i})`)}continue;case"IfThenElseNode":if(s.phase===0)s.phase=1,r.push({node:o.condition,phase:0});else if(s.phase===1)s.phase=2,r.push({node:o.thenBody,phase:0});else if(s.phase===2)s.phase=3,r.push({node:o.elseBody,phase:0});else{r.pop();let i=t.pop(),a=t.pop(),u=t.pop();t.push(`(${u} ? ${a} : ${i})`)}continue;case"CastNode":if(s.phase===0)s.phase=1,r.push({node:o.expr,phase:0});else{r.pop();let i=t.pop();o.toDtype==="bool"?t.push(`(${i} ? 1 : 0)`):mr(o.toDtype)?t.push(`(${i} | 0)`):W0[o.toDtype]?t.push(`${W0[o.toDtype]}(${c1[o.toDtype]}(${i}))`):t.push(`(+${i})`)}continue;case"CallExternNode":if(s.phase<o.args.length){let i=s.phase;s.phase++,r.push({node:o.args[i],phase:0})}else{r.pop();let i=[];for(let u=0;u<o.args.length;u++)i.unshift(t.pop());let a=i.join(", ");if(EN(o.externName))t.push(`Math.${o.externName}(${a})`);else if(o.externName==="rsqrt")t.push(`(1.0 / Math.sqrt(${a}))`);else if(o.externName==="fmod")t.push(`((${i[0]} % ${i[1]} + ${i[1]}) % ${i[1]})`);else if(o.externName==="exp2")t.push(`Math.pow(2, ${a})`);else if(o.externName==="erf")t.push(p1(a));else if(o.externName==="erfc")t.push(`(1.0 - ${p1(a)})`);else if(o.externName==="lgamma")t.push(_$(a));else if(o.externName==="gamma")t.push(b$(a));else if(o.externName==="log10")t.push(`(Math.log(${a}) * ${1/Math.LN10})`);else throw new Error(`CPU codegen: unsupported extern function "${o.externName}"`)}continue;default:throw new Error(`CPU codegen: unhandled expr node '${o.type}'`)}}return t.length>0?t[0]:"0"}_dynamicNumel(e){return Ui(e,0,(t,r)=>this._resolveShapeParam(t,r))}_flatIndex(e,t){return ml(e,t,r=>this._exprToJS(r),(r,s)=>this._computeDynamicStride(r,s),!0)}_computeDynamicStride(e,t){return Ui(e,t+1,(r,s)=>this._resolveShapeParam(r,s))}_resolveShapeParam(e,t){return ji(this._primFunc,e,t,r=>r.name,"CPU","js")}_cleanupSource(e){let t=e.split(`
|
|
3
|
+
`),r=new Array(t.length).fill(null),s=new Map;for(let i=0;i<t.length;i++){let a=t[i].match(/^\s*const (\w+) = new \w+Array\(\d+\);\s*$/);a&&(r[i]=a[1]);let u=t[i].match(/[A-Za-z_]\w*/g);if(u)for(let p of u)s.set(p,(s.get(p)||0)+1)}let o=[];for(let i=0;i<t.length;i++){if(r[i]!==null&&s.get(r[i])===1)continue;let a=t[i];if(/^\s*\}\s*$/.test(a)&&o.length>0&&/^\s*for\s*\(.*\{\s*$/.test(o[o.length-1])){o.pop();continue}o.push(a)}return o.join(`
|
|
4
|
+
`)}_isRedundantZeroFill(e){let t=e.body;for(;t;){if(t.type==="ForNode"){if(t.extent.type==="IntImmNode"&&t.extent.value===1){t=t.body;continue}return this._isRedundantZeroFill(t)}if(t.type==="BlockNode"){t=t.body;continue}if(t.type==="BufferStoreNode"||t.type==="LIRFlatStoreNode"){let r=t.value;if(!(r.type==="FloatImmNode"&&r.value===0||r.type==="IntImmNode"&&r.value===0)||!t.buffer)return!1;let o=t.buffer.name,i=this._zeroBuffers&&this._zeroBuffers.has(o),a=this._readBuffers&&this._readBuffers.has(o);return i||!a}return!1}return!1}_isZeroFillBody(e){return Mm(e)}_scanTree(e,t,r,s){let o=[e];for(;o.length>0;){let i=o.pop();if(!(!i||typeof i!="object")){switch(i.type){case"BufferStoreNode":case"BufferLoadNode":i.buffer&&t.set(i.buffer.name,i.buffer),i.type==="BufferLoadNode"&&i.buffer&&s&&s.add(i.buffer.name);break;case"AllocateNode":i.buffer&&r.add(i.buffer.name);break}if(i.reads)for(let a of i.reads)a.buffer&&t.set(a.buffer.name,a.buffer);if(i.writes)for(let a of i.writes)a.buffer&&t.set(a.buffer.name,a.buffer);for(let a of ln(i))o.push(a)}}}_findZeroOnlyBuffers(e,t){let r=new Map,s=[e];for(;s.length>0;){let i=s.pop();if(!(!i||typeof i!="object")){if(i.type==="BufferStoreNode"){let a=i.buffer.name;t.has(a)||(r.has(a)||r.set(a,[]),r.get(a).push(i.value))}for(let a of ln(i))s.push(a)}}let o=new Set;this._constantBuffers=new Map;for(let[i,a]of r){if(a.length===0)continue;if(a.every(c=>c.type==="FloatImmNode"&&c.value===0||c.type==="IntImmNode"&&c.value===0)){o.add(i);continue}let u=a[0];(u.type==="FloatImmNode"||u.type==="IntImmNode")&&a.every(c=>c.type===u.type&&c.value===u.value)&&this._constantBuffers.set(i,String(u.value))}return o}}});function m1(n,e){h1.set(n,e)}function g1(n){return h1.get(n)||null}var h1,y1=M(()=>{"use strict";h1=new Map;l(m1,"registerCudaIntrin");l(g1,"getCudaIntrin");m1("wmma_16x16x16_f16f16f32",(n,e)=>{let{M:t,N:r,K:s,a:o,b:i,c:a}=e;n._blockDim=[32,1,1],n._gridDim=[Math.ceil(t/16),Math.ceil(r/16),1],n._emit("const int warpM = blockIdx.x;"),n._emit("const int warpN = blockIdx.y;"),n._emit("fragment<accumulator, 16, 16, 16, float> cf;"),n._emit("fill_fragment(cf, 0.0f);"),n._emit(`for (int kk = 0; kk < ${s}; kk += 16) {`),n._indent++,n._emit("fragment<matrix_a, 16, 16, 16, half, row_major> af;"),n._emit("fragment<matrix_b, 16, 16, 16, half, row_major> bf;"),n._emit(`load_matrix_sync(af, ${o} + warpM * 16 * ${s} + kk, ${s});`),n._emit(`load_matrix_sync(bf, ${i} + kk * ${r} + warpN * 16, ${r});`),n._emit("mma_sync(cf, af, bf, cf);"),n._indent--,n._emit("}"),n._emit(`store_matrix_sync(${a} + warpM * 16 * ${r} + warpN * 16, cf, ${r}, mem_row_major);`)});m1("gemm_pipelined_f32",(n,e)=>{let{M:t,N:r,K:s,a:o,b:i,c:a,tile:u=16}=e;n._blockDim=[u,u,1],n._gridDim=[Math.ceil(r/u),Math.ceil(t/u),1];let p=u;n._emit(`__shared__ float As[2][${p}][${p}];`),n._emit(`__shared__ float Bs[2][${p}][${p}];`),n._emit(`const int row = blockIdx.y * ${p} + threadIdx.y;`),n._emit(`const int col = blockIdx.x * ${p} + threadIdx.x;`),n._emit("float acc = 0.0f;"),n._emit(`const int nTiles = ${s} / ${p};`),n._emit(`__pipeline_memcpy_async(&As[0][threadIdx.y][threadIdx.x], &${o}[row * ${s} + threadIdx.x], sizeof(float));`),n._emit(`__pipeline_memcpy_async(&Bs[0][threadIdx.y][threadIdx.x], &${i}[threadIdx.y * ${r} + col], sizeof(float));`),n._emit("__pipeline_commit();"),n._emit("for (int t = 0; t < nTiles; t++) {"),n._indent++,n._emit("const int cur = t & 1, nxt = (t + 1) & 1;"),n._emit("if (t + 1 < nTiles) {"),n._indent++,n._emit(`__pipeline_memcpy_async(&As[nxt][threadIdx.y][threadIdx.x], &${o}[row * ${s} + (t + 1) * ${p} + threadIdx.x], sizeof(float));`),n._emit(`__pipeline_memcpy_async(&Bs[nxt][threadIdx.y][threadIdx.x], &${i}[((t + 1) * ${p} + threadIdx.y) * ${r} + col], sizeof(float));`),n._emit("__pipeline_commit();"),n._indent--,n._emit("}"),n._emit("__pipeline_wait_prior(t + 1 < nTiles ? 1 : 0);"),n._emit("__syncthreads();"),n._emit(`for (int kk = 0; kk < ${p}; kk++) acc += As[cur][threadIdx.y][kk] * Bs[cur][kk][threadIdx.x];`),n._emit("__syncthreads();"),n._indent--,n._emit("}"),n._emit(`${a}[row * ${r} + col] = acc;`)})});var _1,K0,zm,b1=M(()=>{"use strict";Fe();tr();km();ts();Np();y1();_1=256*1024,K0=class{static{l(this,"CUDAKernel")}constructor(e,t,r,s,o,i,a,u){this.name=e,this.source=t,this.blockDim=r,this.gridDim=s,this.sharedMemBytes=o,this.params=i,this.outputIndices=a,this.scratch=u||[]}},zm=class{static{l(this,"CUDACodegen")}constructor(e){this.target=e,this._indent=0,this._lines=[],this._threadBindings=new Map,this._sharedBuffers=[],this._blockDim=[1,1,1],this._gridDim=[1,1,1],this._defaultDtype="f32",this._storeBuffers=new Set,this._promotedBuffers=new Set,this._promotedBufferDecls=[],this._declaredLocals=new Set,this._needsBarriers=!1,this._globalScratch=[],this._scratchNames=new Set,this._serializeThreads=!1}generate(e){if(this._indent=0,this._lines=[],this._threadBindings.clear(),this._sharedBuffers=[],this._blockDim=[1,1,1],this._gridDim=[1,1,1],this._didParallelReduce=!1,this._primFunc=e,this._storeBuffers=new Set,this._promotedBuffers=new Set,this._promotedBufferDecls=[],this._declaredLocals=new Set,this._needsBarriers=!1,this._globalScratch=[],this._scratchNames=new Set,this._serializeThreads=!1,e.type==="LIRFunc"){for(let[m,g]of e.metadata.threadBindings){this._threadBindings.set(m,g);for(let y of g)y.isDynamic||this._applyBindingDim(m,y.extent)}this._sharedBuffers=e.metadata.sharedBuffers}else this._scanBindings(e.body);this._scanStoreTargets(e.body),this._analyzeSharing(e),this._collectGlobalScratch(e);let r=[],s=[],o=[],i=0;for(let[,m]of e.bufferMap)s.push(m.name),r.push(`${m0(m.dtype)} ${m.name}`),this._defaultDtype=m.dtype,this._storeBuffers.has(m.name)&&o.push(i),i++;for(let m of this._globalScratch)s.push(m.name),r.push(`${m0(m.dtype)} ${m.name}`);for(let m of e.shapeParams)s.push(m.name),r.push(`int ${m.name}`);this._emit(`__global__ void ${e.name}(${r.join(", ")}) {`),this._indent++;let a=new Set;for(let m of this._sharedBuffers){if(a.has(m.name))continue;a.add(m.name);let g=m.numel(),y=m.align16?"__align__(16) ":"";this._emit(`__shared__ ${y}${Zr(m.dtype)} ${m.name}[${g>0?g:1}];`)}for(let m of this._promotedBufferDecls)this._emit(`__shared__ ${Zr(m.dtype)} ${m.name}[${m.size}];`);let u=new Set;if(!this._serializeThreads)for(let[m,g]of this._threadBindings)for(let y of g)u.has(y.varName)||(this._emit(`const int ${y.varName} = ${m};`),u.add(y.varName));if(this._emitMissingLocalDecls(e),e._tensorIntrin){let m=g1(e._tensorIntrin.name);if(!m)throw new Error(`CUDA codegen: unknown tensor intrinsic '${e._tensorIntrin.name}'`);m(this,e._tensorIntrin.info)}else this._visitNode(e.body);this._indent--,this._emit("}");let p=this.target,c=this._serializeThreads?[1,1,1]:[Math.min(this._blockDim[0],p.maxBlockDimX),Math.min(this._blockDim[1],p.maxBlockDimY),Math.min(this._blockDim[2],p.maxBlockDimZ)],f=this._serializeThreads?[1,1,1]:[Math.min(this._gridDim[0],p.maxGridDimX),Math.min(this._gridDim[1],p.maxGridDimY),Math.min(this._gridDim[2],p.maxGridDimZ)],d=c[0]*c[1]*c[2];if(d>p.maxThreadsPerBlock)throw new Error(`[codegen] kernel '${e.name}' block ${c.join("x")} = ${d} threads exceeds maxThreadsPerBlock ${p.maxThreadsPerBlock}`);let h=this._sharedBuffers.reduce((m,g)=>m+Math.max(g.sizeInBytes(),0),0)+this._promotedBufferDecls.reduce((m,g)=>m+Math.max(g.size,0)*Rn(g.dtype),0);if(h>p.sharedMemoryBytes)throw new Error(`[codegen] kernel '${e.name}' shared memory ${h} bytes exceeds device limit ${p.sharedMemoryBytes}`);return new K0(e.name,this._lines.join(`
|
|
5
|
+
`),c,f,h,s,o,this._globalScratch)}_scanBindings(e){let t=[e];for(;t.length>0;){let r=t.pop();if(r){if(r.type==="ForNode"&&r.kind===z.THREAD_BINDING&&r.threadTag){let s=r.extent.type==="IntImmNode"?r.extent.value:0,o=r.extent.type!=="IntImmNode",i={varName:r.loopVar.name,extent:s,isDynamic:o,extentNode:r.extent};this._threadBindings.has(r.threadTag)?this._threadBindings.get(r.threadTag).push(i):this._threadBindings.set(r.threadTag,[i]),o||this._applyBindingDim(r.threadTag,s)}if(r.type==="AllocateNode"&&r.scope==="shared"&&this._sharedBuffers.push(r.buffer),r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody)}}}_applyBindingDim(e,t){let r=za(e);r&&(r.space==="thread"?this._blockDim[r.axis]=Math.max(this._blockDim[r.axis],t):this._gridDim[r.axis]=Math.max(this._gridDim[r.axis],t))}_emit(e){this._lines.push(" ".repeat(this._indent)+e)}_visitNode(e){Rm(this,e)}_emitSync(){this._emit("__syncthreads();")}_matchFullReduction(e){let t=[],r=e;for(;r&&r.type==="ForNode";){if(r.kind!==z.SERIAL)return null;let d=r.extent.type==="IntImmNode"?r.extent.value:0;if(d<=0)return null;t.push({extC:this._exprToC(r.extent),extVal:d,varName:r.loopVar.name}),r=r.body}if(!r||r.type!=="BlockNode")return null;let s=r,o=s.body;if(!o||o.type!=="BufferStoreNode"||s.iterVars&&s.iterVars.length!==t.length||typeof o.buffer.numel!="function"||o.buffer.numel()!==1)return null;let i=o.value;if(!i||i.type!=="MathOpNode"||i.op!=="+")return null;let a=o.indices.map(d=>this._exprToC(d)).join(","),u=l(d=>d&&d.type==="BufferLoadNode"&&d.buffer===o.buffer&&d.indices.map(h=>this._exprToC(h)).join(",")===a,"isOutLoad"),p=null;if(u(i.a))p=i.b;else if(u(i.b))p=i.a;else return null;let c=new Set(t.map(d=>d.varName));if(a.split(/[^A-Za-z0-9_]/).some(d=>c.has(d)))return null;let f=t.reduce((d,h)=>d*h.extVal,1);return f<2048?null:{loops:t,block:s,store:o,valExpr:p,total:f}}_emitParallelReduction(e,t){this._blockDim=[256,1,1],this._didParallelReduce=!0;let s=Zr(t.store.buffer.dtype),o=this._flatIndex(t.store.buffer,t.store.indices);this._emit(`__shared__ ${s} _redsh[256];`),this._emit(`${s} _racc = 0;`),this._emit(`for (int _rf = threadIdx.x; _rf < ${t.total}; _rf += 256) {`),this._indent++,this._emit("int _rem = _rf;");for(let i=t.loops.length-1;i>=0;i--)this._emit(`const int ${t.loops[i].varName} = _rem % ${t.loops[i].extC}; _rem /= ${t.loops[i].extC};`);for(let i of t.block.iterVars)i.iterVar&&i.binding&&this._emit(`const int ${i.iterVar.name} = ${this._exprToC(i.binding)};`);this._emit(`_racc = _racc + ${this._exprToC(t.valExpr)};`),this._indent--,this._emit("}"),this._emit("_redsh[threadIdx.x] = _racc;"),this._emit("__syncthreads();"),this._emit("for (int _rs = 256 / 2; _rs > 0; _rs >>= 1) {"),this._indent++,this._emit("if (threadIdx.x < _rs) _redsh[threadIdx.x] = _redsh[threadIdx.x] + _redsh[threadIdx.x + _rs];"),this._emit("__syncthreads();"),this._indent--,this._emit("}"),this._emit(`if (threadIdx.x == 0) ${t.store.buffer.name}[${o}] = _redsh[0];`)}_visitForNode(e){if(e.kind===z.THREAD_BINDING&&this._serializeThreads){let s=e.loopVar.name,o=this._exprToC(e.extent);this._emit(`for (int ${s} = 0; ${s} < ${o}; ${s}++) {`),this._indent++,this._visitNode(e.body),this._indent--,this._emit("}");return}if(e.kind===z.THREAD_BINDING){let s=e.extent.type==="IntImmNode"?e.extent.value:0,o=e.threadTag,i=this._getMaxBindingExtent(o);s>0&&i>0&&s<i?(this._emit(`if (${o} < ${s}) {`),this._indent++,this._visitNode(e.body),this._indent--,this._emit("}")):this._visitNode(e.body),this._needsBarriers&&this._emit("__syncthreads();");return}if(this._threadBindings.size===0&&!this._didParallelReduce){let s=this._matchFullReduction(e);if(s){this._emitParallelReduction(e,s);return}}let t=e.loopVar.name,r=this._exprToC(e.extent);(e.kind===z.UNROLLED||e.kind===z.VECTORIZED)&&this._emit("#pragma unroll"),this._emit(`for (int ${t} = 0; ${t} < ${r}; ${t}++) {`),this._indent++,this._visitNode(e.body),this._indent--,this._emit("}")}_visitBlockNode(e){for(let t of e.iterVars)t.iterVar&&t.binding&&this._emit(`const int ${t.iterVar.name} = ${this._exprToC(t.binding)};`);e.initBody&&this._visitNode(e.initBody),this._visitNode(e.body)}_visitAllocateNode(e){if(e.scope!=="shared"){if(this._promotedBuffers.has(e.buffer.name)||this._scratchNames.has(e.buffer.name)||this._declaredLocals.has(e.buffer.name))return;this._declaredLocals.add(e.buffer.name);let t=e.buffer.numel();if(t>0){let r=e.buffer.align16?"__align__(16) ":"";this._emit(`${r}${Zr(e.buffer.dtype)} ${e.buffer.name}[${t}];`)}else this._emit(`${Zr(e.buffer.dtype)}* ${e.buffer.name} = (${Zr(e.buffer.dtype)}*)alloca(${this._dynamicNumel(e.buffer)} * sizeof(${Zr(e.buffer.dtype)}));`)}}_visitIfStmt(e){this._emit(`if (${this._exprToC(e.condition)}) {`),this._indent++,this._visitNode(e.thenBody),this._indent--,e.elseBody&&(this._emit("} else {"),this._indent++,this._visitNode(e.elseBody),this._indent--),this._emit("}")}_visitLetStmtNode(e){let t=e.variable.dtype||this._defaultDtype;this._emit(`${Zr(t)} ${e.variable.name} = ${this._exprToC(e.value)};`),this._visitNode(e.body)}_visitWhileNode(e){this._visitNode(e.condBody);let t=Array.isArray(e.condVar.shape)?`${e.condVar.name}[0]`:e.condVar.name;this._emit(`while (${t}) {`),this._indent++,this._visitNode(e.loopBody),this._visitNode(e.condBody),this._indent--,this._emit("}")}_visitBufferStoreNode(e){this._emit(`${e.buffer.name}[${this._flatIndex(e.buffer,e.indices)}] = ${this._exprToC(e.value)};`)}_visitVecCopyNode(e){let t=`${Zr(e.dstBuffer.dtype)}${e.width}`,r=`${e.dstBuffer.name}[${this._flatIndex(e.dstBuffer,[e.dstIndex])}]`,s=`${e.srcBuffer.name}[${this._flatIndex(e.srcBuffer,[e.srcIndex])}]`;this._emit(`*reinterpret_cast<${t}*>(&${r}) = *reinterpret_cast<const ${t}*>(&${s});`)}_visitLIRFlatStore(e){this._emit(`${e.buffer.name}[${this._exprToC(e.offsetExpr)}] = ${this._exprToC(e.value)};`)}_visitLIRBindings(e){for(let t of e.bindings)this._emit(`const int ${t.name} = ${this._exprToC(t.expr)};`);this._visitNode(e.body)}_visitLIRAccumulator(e){let t=e.localName,r=e.dtype||this._defaultDtype;this._emit(`${Zr(r)} ${t} = ${this._exprToC(e.initLoad)};`);let s=e.loopVar.name,o=this._exprToC(e.extent);this._emit(`for (int ${s} = 0; ${s} < ${o}; ${s}++) {`),this._indent++;let i=this._exprToC(e.body),a=e.op||"+",u;a==="max"||a==="min"?mr(e.dtype)?u=`((${t}) ${a==="max"?">":"<"} (${i}) ? (${t}) : (${i}))`:u=`${a==="max"?"fmaxf":"fminf"}(${t}, ${i})`:u=`(${t} ${a} ${i})`,this._emit(`${t} = ${u};`),this._indent--,this._emit("}"),this._emit(`${e.flushStore.buffer.name}[${this._exprToC(e.flushStore.offsetExpr)}] = ${t};`)}_exprToC(e){if(!e)return"0";switch(e.type){case"IntImmNode":return String(e.value);case"FloatImmNode":return this._emitFloatLiteral(e.value);case"VariableNode":return e.name;case"BufferLoadNode":return`${e.buffer.name}[${this._flatIndex(e.buffer,e.indices)}]`;case"LIRFlatLoadNode":return`${e.buffer.name}[${this._exprToC(e.offsetExpr)}]`;case"MathOpNode":{let t=this._exprToC(e.a);if(!e.b)return`(${e.op}${t})`;let r=this._exprToC(e.b);return e.op==="//"?`(${t} / ${r})`:`(${t} ${e.op} ${r})`}case"CompareNode":return`(${this._exprToC(e.a)} ${Ku(e.direction)} ${this._exprToC(e.b)})`;case"IfThenElseNode":return`(${this._exprToC(e.condition)} ? ${this._exprToC(e.thenBody)} : ${this._exprToC(e.elseBody)})`;case"CastNode":return`((${Zr(e.toDtype)})(${this._exprToC(e.expr)}))`;case"CallExternNode":return this._emitExternCall(e);default:throw new Error(`CUDA codegen: unhandled expr node '${e.type}'`)}}_emitFloatLiteral(e){if(e===1/0)return"INFINITY";if(e===-1/0)return"(-INFINITY)";let t=g0(this._defaultDtype),r=String(e);return`${/[.eEnN]/.test(r)?r:r+".0"}${t}`}_emitExternCall(e){let t=e.args.length,r=new Array(t);for(let a=0;a<t;a++)r[a]=this._exprToC(e.args[a]);let s=r.join(", "),o=e.dtype||this._defaultDtype;if(e.externName==="rsqrt")return`${$h("rsqrt",o)}(${s})`;if(e.externName==="sign"){let a=r[0],u=`0.0${g0(o)}`;return`((${a} > ${u}) - (${a} < ${u}))`}if(e.externName==="min"||e.externName==="max"){if(mr(o)){let a=e.externName==="min"?"<":">";return`((${r[0]}) ${a} (${r[1]}) ? (${r[0]}) : (${r[1]}))`}return`${$h(e.externName,o)}(${s})`}return`${$h(e.externName,o)}(${s})`}_flatIndex(e,t){return ml(e,t,r=>this._exprToC(r),(r,s)=>this._computeDynamicStride(r,s),!1)}_computeDynamicStride(e,t){return Ui(e,t+1,(r,s)=>this._resolveShapeParam(r,s))}_dynamicNumel(e){return Ui(e,0,(t,r)=>this._resolveShapeParam(t,r))}_getMaxBindingExtent(e){return Em(this._threadBindings,e)}_scanStoreTargets(e){let t=[e];for(;t.length>0;){let r=t.pop();if(r){(r.type==="BufferStoreNode"||r.type==="LIRFlatStoreNode")&&this._storeBuffers.add(r.buffer.name),r.type==="VecCopyNode"&&this._storeBuffers.add(r.dstBuffer.name),r.type==="LIRAccumulatorNode"&&r.flushStore&&this._storeBuffers.add(r.flushStore.buffer.name);for(let s of ln(r))t.push(s)}}}_hasCrossBlockGlobalRAW(e){let t=new Set;for(let[,a]of e.bufferMap)t.add(a.name);if(t.size===0)return!1;let r=new Map,s=new Map,o=l((a,u,p)=>{let c=a.get(u);c||(c=new Set,a.set(u,c)),c.add(p)},"record"),i=l((a,u)=>{if(!a||typeof a!="object")return;let p=u;if(a.type==="ForNode"&&a.kind===z.THREAD_BINDING&&a.threadTag){let c=a.extent&&a.extent.type==="IntImmNode"?a.extent.value:0;p=u.concat(`${a.threadTag}:${c}`)}(a.type==="BufferStoreNode"||a.type==="LIRFlatStoreNode")&&a.buffer&&t.has(a.buffer.name)&&o(r,a.buffer.name,[...p].sort().join(",")),(a.type==="BufferLoadNode"||a.type==="LIRFlatLoadNode")&&a.buffer&&t.has(a.buffer.name)&&o(s,a.buffer.name,[...p].sort().join(","));for(let c of ln(a))i(c,p)},"walk");i(e.body,[]);for(let[a,u]of r){let p=s.get(a);if(!p)continue;let c=new Set(u);for(let f of p)c.add(f);if(c.size>1)return!0}return!1}_findThreadPrivateAllocs(e,t){let r=l((s,o)=>{if(!s||typeof s!="object")return;let i=o;if(s.type==="ForNode"&&s.kind===z.THREAD_BINDING&&s.threadTag){let a=za(s.threadTag);a&&a.space==="thread"&&(i=!0)}i&&s.type==="AllocateNode"&&s.buffer&&t.add(s.buffer.name);for(let a of ln(s))r(a,i)},"walk");r(e,!1)}_findCrossThreadBuffers(e){let t=new Set;for(let[,h]of e.bufferMap)t.add(h.name);let r=new Set(this._sharedBuffers.map(h=>h.name)),s=new Set;this._findThreadPrivateAllocs(e.body,s);let o=l(h=>h&&!t.has(h.name)&&!r.has(h.name)&&!s.has(h.name),"isIntermediate"),i=l(h=>typeof h.numel!="function"||h.numel()!==1,"isMulti"),a=new Set,u=new Set,p=new Set,c=new Set,f=l((h,m)=>{if(!h||typeof h!="object")return;let g=m;if(h.type==="ForNode"&&h.kind===z.THREAD_BINDING&&h.threadTag){let y=h.extent&&h.extent.type==="IntImmNode"?h.extent.value:0,_=this._getMaxBindingExtent(h.threadTag);y>0&&_>0&&y<_&&(g=!0)}(h.type==="BufferStoreNode"||h.type==="LIRFlatStoreNode")&&o(h.buffer)&&(i(h.buffer)?a.add(h.buffer.name):g&&p.add(h.buffer.name)),(h.type==="BufferLoadNode"||h.type==="LIRFlatLoadNode")&&o(h.buffer)&&(i(h.buffer)?u.add(h.buffer.name):c.add(h.buffer.name));for(let y of ln(h))f(y,g)},"walk");f(e.body,!1);let d=new Set;for(let h of a)u.has(h)&&d.add(h);for(let h of p)c.has(h)&&d.add(h);return d}_crossThreadBuffersAreBlockLocal(e,t){let r=!0,s=l((o,i)=>{if(!o||typeof o!="object")return;let a=i;if(o.type==="ForNode"&&o.kind===z.THREAD_BINDING&&o.threadTag){let u=za(o.threadTag);u&&u.space==="block"&&(a=!0)}a&&(o.type==="BufferStoreNode"||o.type==="LIRFlatStoreNode")&&o.buffer&&t.has(o.buffer.name)&&(r=!1);for(let u of ln(o))s(u,a)},"walk");return s(e.body,!1),r}_promoteCrossThreadToShared(e,t){let r=new Set;for(let[,p]of e.bufferMap)r.add(p.name);let s=new Map;this._scanBufferRefs(e.body,s);let o=this.target.sharedMemoryBytes||49152,i=new Set(this._sharedBuffers.map(p=>p.name)),a=this._sharedBuffers.reduce((p,c)=>p+Math.max(c.sizeInBytes(),0),0),u=[];for(let p of t){if(r.has(p)||this._promotedBuffers.has(p)||i.has(p))continue;let c=s.get(p);if(!c)return!1;let f=c.numel(),d=f>0?f:this._estimateBufferSize(c);if(d<=0||(a+=d*Rn(c.dtype),a>o))return!1;u.push({name:p,dtype:c.dtype,size:d})}for(let p of u)this._promotedBuffers.add(p.name),this._promotedBufferDecls.push(p);return!0}_analyzeSharing(e){if(e._tensorIntrin){this._needsBarriers=!1;return}if(e.gpuRegisterBlocked){this._needsBarriers=!1;return}let t=this._gridDim[0]*this._gridDim[1]*this._gridDim[2];if(t>1&&this._hasCrossBlockGlobalRAW(e)){this._serializeThreads=!0,this._needsBarriers=!1;return}if(this._threadBindings.size>0){let u=this._blockDim[0]*this._blockDim[1]*this._blockDim[2],p=this._findCrossThreadBuffers(e);if(u*t>1&&p.size>0){if(this._crossThreadBuffersAreBlockLocal(e,p)&&this._promoteCrossThreadToShared(e,p)){this._needsBarriers=!0;return}this._serializeThreads=!0,this._needsBarriers=!1;return}}let r=!1;for(let[u,p]of this._threadBindings){let c=new Set;for(let d of p)d.extent>0&&c.add(d.extent);if(c.size<=1)continue;let f=za(u);if(f&&f.space==="block"){this._serializeThreads=!0,this._needsBarriers=!1;return}r=!0}if(!r)return;this._needsBarriers=!0;let s=new Set;for(let[,u]of e.bufferMap)s.add(u.name);let o=[e.body];for(;o.length>0;){let u=o.pop();if(u){if(u.type==="AllocateNode"&&u.scope!=="shared"&&!s.has(u.buffer.name)){let p=u.buffer.numel(),c=p>0?p:this._estimateBufferSize(u.buffer);c>0&&(this._promotedBuffers.add(u.buffer.name),this._promotedBufferDecls.push({name:u.buffer.name,dtype:u.buffer.dtype,size:c}))}for(let p of ln(u))o.push(p)}}let i=new Map;this._scanBufferRefs(e.body,i);let a=new Set;this._scanAllocateNodes(e.body,a);for(let[u,p]of i){if(s.has(u)||a.has(u)||this._promotedBuffers.has(u))continue;let c=p.numel(),f=c>0?c:this._estimateBufferSize(p);f>0&&(this._promotedBuffers.add(u),this._promotedBufferDecls.push({name:u,dtype:p.dtype,size:f}))}}_emitMissingLocalDecls(e){let t=new Set;for(let[,o]of e.bufferMap)t.add(o.name);let r=new Set;this._scanAllocateNodes(e.body,r);let s=new Map;this._scanBufferRefs(e.body,s);for(let[o,i]of s){if(t.has(o)||r.has(o)||this._promotedBuffers.has(o)||this._scratchNames.has(o)||this._declaredLocals.has(o))continue;let a=i.numel(),u=a>0?a:this._estimateBufferSize(i);u>0&&(this._declaredLocals.add(o),this._emit(`${Zr(i.dtype)} ${o}[${u}];`))}}_collectGlobalScratch(e){let t=this._serializeThreads;if(this._threadBindings.size>0&&!t)return;let r=32768,s=new Set;for(let[,d]of e.bufferMap)s.add(d.name);let o=[],i=new Set,a=l((d,h)=>{if(!h||s.has(d)||this._promotedBuffers.has(d)||this._scratchNames.has(d)||i.has(d))return;let m=typeof h.numel=="function"?h.numel():0,g=m>0?m:this._estimateBufferSize(h);g<=0||(i.add(d),o.push({name:d,dtype:h.dtype,size:g,bytes:g*Rn(h.dtype)}))},"consider"),u=[e.body];for(;u.length>0;){let d=u.pop();if(d){d.type==="AllocateNode"&&d.scope!=="shared"&&a(d.buffer.name,d.buffer);for(let h of ln(d))u.push(h)}}let p=new Map;this._scanBufferRefs(e.body,p);for(let[d,h]of p)a(d,h);let c=l(d=>{this._scratchNames.add(d.name),this._globalScratch.push({name:d.name,dtype:d.dtype,size:d.size})},"offload");if(!t){for(let d of o)d.size>r&&c(d);return}let f=o.reduce((d,h)=>d+h.bytes,0);if(!(f<=_1))for(let d of[...o].sort((h,m)=>m.bytes-h.bytes)){if(f<=_1)break;c(d),f-=d.bytes}}_scanAllocateNodes(e,t){let r=[e];for(;r.length>0;){let s=r.pop();if(s){s.type==="AllocateNode"&&t.add(s.buffer.name);for(let o of ln(s))r.push(o)}}}_scanBufferRefs(e,t){let r=[e];for(;r.length>0;){let s=r.pop();if(s){(s.type==="BufferLoadNode"||s.type==="BufferStoreNode"||s.type==="LIRFlatLoadNode"||s.type==="LIRFlatStoreNode")&&s.buffer&&t.set(s.buffer.name,s.buffer),s.type==="VecCopyNode"&&(s.dstBuffer&&t.set(s.dstBuffer.name,s.dstBuffer),s.srcBuffer&&t.set(s.srcBuffer.name,s.srcBuffer)),s.type==="LIRAccumulatorNode"&&(s.flushStore&&s.flushStore.buffer&&t.set(s.flushStore.buffer.name,s.flushStore.buffer),s.initLoad&&s.initLoad.buffer&&t.set(s.initLoad.buffer.name,s.initLoad.buffer));for(let o of ln(s))r.push(o)}}}_estimateBufferSize(e){return Im(e)}_resolveShapeParam(e,t){return ji(this._primFunc,e,t,r=>r.name,"CUDA","c")}}});var wl,Vm,w1=M(()=>{"use strict";Fe();tr();vp();bl();ts();Np();wl=new Set(["f16","bf16"]),Vm=class{static{l(this,"WasmCodegen")}constructor(e){this.target=e,this._lines=[],this._indent=0,this._locals=new Map,this._localCounter=0,this._imports=new Map,this._bufferOffsets=new Map,this._totalMemBytes=0,this._defaultDtype="f32",this._vectorMode=null,this._vecTmpCounter=0,this._loopVarStack=[]}generate(e){this._lines=[],this._indent=0,this._locals.clear(),this._localCounter=0,this._imports.clear(),this._bufferOffsets.clear(),this._totalMemBytes=0,this._primFunc=e,this._wasmAcc=null,this._waccCounter=0,this._hasParallel=!1,this._parallelExtent=0,this._vectorMode=null,this._vecTmpCounter=0;let t=e.type==="LIRFunc";if(t){for(let[c,f]of e.metadata.memoryLayout.bufferOffsets)this._bufferOffsets.set(c,f);this._totalMemBytes=e.metadata.memoryLayout.totalBytes;for(let[c,f]of e.metadata.externCalls){let d=this._mathImportSig(c,f.argCount);this._imports.set(c,d)}for(let[c,f]of e.metadata.locals)this._ensureLocal(c,Fs(f))}else this._layoutBuffers(e),this._scanMathImports(e.body);this._scanParallel(e.body);let r=[];for(let[,c]of e.bufferMap)r.push(c.name);for(let c of e.shapeParams)r.push(c.name);this._emit("(module"),this._indent++;let s=Math.max(1,Math.ceil(this._totalMemBytes/65536)),o=Math.max(256,s);this._emit(`(memory (export "memory") ${s} ${o})`);for(let[c,f]of this._imports)this._emit(`(import "math" "${c}" (func $math_${c} ${f}))`);let i=[];for(let[,c]of e.bufferMap)i.push("(param i32)");let a=[];for(let c of e.shapeParams)i.push("(param i32)"),this._ensureLocal(c.name,"i32"),a.push(c.name);this._hasParallel&&(i.push("(param i32)"),i.push("(param i32)"),this._ensureLocal("_par_start","i32"),this._ensureLocal("_par_end","i32"),r.push("_par_start"),r.push("_par_end")),this._emit('(func (export "'+e.name+'") '+i.join(" ")),this._indent++,t||(this._prescanLocals(e.body),this._vecTmpCounter=0),this.target.supportsSimd()&&(this._prescanVecLocalsAll(e.body),this._vecTmpCounter=0),this._fixLetStmtLocals(e.body),this._intMinMaxDepth=0,this._prescanIntMinMax(e.body),this._ensureHalfScratch(e);let u=[];for(let[c,f]of this._locals)u.push("(local $"+c+" "+f+")");u.length>0&&this._emit(u.join(" "));let p=e.bufferMap.size;for(let c=0;c<a.length;c++)this._emit("(local.get "+(p+c)+")"),this._emit("local.set $"+a[c]);if(this._hasParallel){let c=p+a.length;this._emit("(local.get "+c+")"),this._emit("local.set $_par_start"),this._emit("(local.get "+(c+1)+")"),this._emit("local.set $_par_end")}return this._visitNode(e.body),this._indent--,this._emit(")"),this._indent--,this._emit(")"),{name:e.name,wat:this._lines.join(`
|
|
6
|
+
`),memoryPages:s,bufferOffsets:new Map(this._bufferOffsets),imports:this._imports,params:r,parallel:this._hasParallel?{extent:this._parallelExtent,outputIndices:this._findOutputIndices(e),poolSafe:this._isParallelSafe(e)}:null}}_isParallelSafe(e){let t=[],r=[e.body];for(;r.length>0;){let p=r.pop();if(p){if(p.type==="ForNode"&&p.kind===z.PARALLEL&&t.push(p),p.body&&r.push(p.body),p.stmts)for(let c of p.stmts)r.push(c);p.thenBody&&r.push(p.thenBody),p.elseBody&&r.push(p.elseBody),p.loopBody&&r.push(p.loopBody)}}if(t.length!==1)return!1;let s=t[0];if(!(e.body&&e.body.stmts?e.body.stmts:[e.body]).includes(s))return!1;let i=l((p,c)=>{let f=[p];for(;f.length>0;){let d=f.pop();if(d){if((d.type==="BufferStoreNode"||d.type==="LIRFlatStoreNode")&&c.add(d),d.type==="LIRAccumulatorNode"&&d.flushStore&&c.add(d.flushStore),d.body&&f.push(d.body),d.stmts)for(let h of d.stmts)f.push(h);d.thenBody&&f.push(d.thenBody),d.elseBody&&f.push(d.elseBody),d.loopBody&&f.push(d.loopBody)}}},"collectStores"),a=new Set,u=new Set;i(e.body,a),i(s.body,u);for(let p of a)if(!u.has(p))return!1;return!0}_ensureLocal(e,t){this._locals.has(e)||this._locals.set(e,t)}_emit(e){this._lines.push(" ".repeat(this._indent)+e)}_emitLoadOp(e){if(wl.has(e)){this._emitHalfDecode(e);return}this._emit(Dh(e))}_emitStoreOp(e){if(wl.has(e)){this._emitHalfEncode(e);return}this._emit(RN(e))}_emitHalfDecode(e){if(this._emit("i32.load16_u"),this._emit("local.set $_half_i"),e==="bf16"){this._emit("(local.get $_half_i)"),this._emit("(i32.const 16)"),this._emit("i32.shl"),this._emit("f32.reinterpret_i32");return}this._emit("(local.get $_half_i)"),this._emit("(i32.const 32767)"),this._emit("i32.and"),this._emit("(i32.const 13)"),this._emit("i32.shl"),this._emit("f32.reinterpret_i32"),this._emit("(f32.const "+q0.F16_MAGIC_MUL+")"),this._emit("f32.mul"),this._emit("local.set $_half_f"),this._emit("(local.get $_half_f)"),this._emit("i32.reinterpret_f32"),this._emit("local.set $_half_i2"),this._emit("(local.get $_half_i2)"),this._emit("(i32.const 2139095040)"),this._emit("i32.or"),this._emit("(local.get $_half_i2)"),this._emit("(local.get $_half_f)"),this._emit("(f32.const 65536)"),this._emit("f32.ge"),this._emit("select"),this._emit("(local.get $_half_i)"),this._emit("(i32.const 32768)"),this._emit("i32.and"),this._emit("(i32.const 16)"),this._emit("i32.shl"),this._emit("i32.or"),this._emit("f32.reinterpret_i32")}_emitHalfEncode(e){if(this._emit("local.set $_half_f"),this._emit("(local.get $_half_f)"),this._emit("i32.reinterpret_f32"),this._emit("local.set $_half_i"),e==="bf16"){this._emit("(local.get $_half_i)"),this._emit("(i32.const 16)"),this._emit("i32.shr_u"),this._emit("(i32.const 1)"),this._emit("i32.and"),this._emit("(i32.const 32767)"),this._emit("i32.add"),this._emit("(local.get $_half_i)"),this._emit("i32.add"),this._emit("(i32.const 16)"),this._emit("i32.shr_u"),this._emit("i32.store16");return}this._emit("(local.get $_half_i)"),this._emit("(i32.const 2147483647)"),this._emit("i32.and"),this._emit("local.set $_half_i2"),this._emit("(i32.const 32256)"),this._emit("(i32.const 31744)"),this._emit("(local.get $_half_i2)"),this._emit("(i32.const 2139095040)"),this._emit("i32.gt_s"),this._emit("select"),this._emit("(local.get $_half_i2)"),this._emit("f32.reinterpret_i32"),this._emit("(f32.const 0.5)"),this._emit("f32.add"),this._emit("i32.reinterpret_f32"),this._emit("(i32.const 1056964608)"),this._emit("i32.sub"),this._emit("(local.get $_half_i2)"),this._emit("(i32.const "+q0.F16_ADD_BIAS+")"),this._emit("i32.add"),this._emit("(local.get $_half_i2)"),this._emit("(i32.const 13)"),this._emit("i32.shr_u"),this._emit("(i32.const 1)"),this._emit("i32.and"),this._emit("i32.add"),this._emit("(i32.const 13)"),this._emit("i32.shr_u"),this._emit("(local.get $_half_i2)"),this._emit("(i32.const 947912704)"),this._emit("i32.lt_s"),this._emit("select"),this._emit("(local.get $_half_i2)"),this._emit("(i32.const 1199570944)"),this._emit("i32.ge_s"),this._emit("select"),this._emit("(local.get $_half_i)"),this._emit("(i32.const 16)"),this._emit("i32.shr_u"),this._emit("(i32.const 32768)"),this._emit("i32.and"),this._emit("i32.or"),this._emit("i32.store16")}_ensureHalfScratch(e){let t=!1;for(let[,r]of e.bufferMap)if(wl.has(r.dtype)){t=!0;break}if(!t&&e.metadata&&e.metadata.locals){for(let[,r]of e.metadata.locals)if(wl.has(r)){t=!0;break}}t||(t=this._treeHasHalf(e.body)),t&&(this._ensureLocal("_half_f","f32"),this._ensureLocal("_half_i","i32"),this._ensureLocal("_half_i2","i32"))}_treeHasHalf(e){let t=[e];for(;t.length>0;){let r=t.pop();if(!(!r||typeof r!="object")){if((r.type==="BufferLoadNode"||r.type==="BufferStoreNode")&&r.buffer&&wl.has(r.buffer.dtype)||(r.type==="LIRFlatLoadNode"||r.type==="LIRFlatStoreNode")&&wl.has(r.dtype))return!0;if(r.body&&t.push(r.body),r.value&&typeof r.value=="object"&&t.push(r.value),r.stmts)for(let s of r.stmts)t.push(s);if(r.a&&t.push(r.a),r.b&&t.push(r.b),r.expr&&t.push(r.expr),r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),r.initBody&&t.push(r.initBody),r.condition&&t.push(r.condition),r.offsetExpr&&t.push(r.offsetExpr),r.args)for(let s of r.args)t.push(s);if(r.indices)for(let s of r.indices)t.push(s)}}return!1}_visitNode(e){let t=e;for(;t;)switch(t.type){case"SeqNode":for(let r=0;r<t.stmts.length-1;r++)this._visitNode(t.stmts[r]);t=t.stmts[t.stmts.length-1];continue;case"AllocateNode":t=t.body;continue;case"LetStmtNode":{let r=this._vectorMode;if(r&&r.simd&&this._dependsOnVecVar(t.value)){let o=t.variable.name+"_vlet";this._ensureLocal(o,"v128"),r.vecLets||(r.vecLets=new Set),r.vecLets.add(t.variable.name),this._emitVecExpr(t.value),this._emit("local.set $"+o),t=t.body;continue}let s=br(t.value)||t.variable.dtype||this._defaultDtype;this._locals.set(t.variable.name,Fs(s)),this._emitCoercedTo(t.value,this._numPrefix(s)),this._emit(`local.set $${t.variable.name}`),t=t.body;continue}case"ForNode":this._visitFor(t);return;case"BlockNode":this._visitBlock(t);return;case"IfThenElseNode":this._visitIf(t);return;case"BufferStoreNode":if(this._vectorMode){this._emitVecStore(t);return}this._visitStore(t);return;case"LIRFlatStoreNode":if(this._vectorMode){this._emitVecFlatStore(t);return}this._visitLIRFlatStore(t);return;case"LIRBindingsNode":this._visitLIRBindings(t);return;case"LIRAccumulatorNode":this._visitLIRAccumulator(t);return;case"WhileNode":this._visitWhile(t);return;case"EvaluateNode":return;case"SyncThreadsNode":return;default:throw new Error(`WASM codegen: unhandled statement node '${t.type}'`)}}_findOutputIndices(e){let t=new Set,r=[e.body];for(;r.length>0;){let i=r.pop();if(i){if((i.type==="BufferStoreNode"||i.type==="LIRFlatStoreNode")&&i.buffer&&t.add(i.buffer.name),i.type==="LIRAccumulatorNode"&&i.flushStore&&i.flushStore.buffer&&t.add(i.flushStore.buffer.name),i.body&&r.push(i.body),i.stmts)for(let a of i.stmts)r.push(a);i.thenBody&&r.push(i.thenBody),i.elseBody&&r.push(i.elseBody),i.loopBody&&r.push(i.loopBody)}}let s=[],o=0;for(let[,i]of e.bufferMap)t.has(i.name)&&s.push(o),o++;return s}_scanParallel(e){let t=[e];for(;t.length>0;){let r=t.pop();if(r){if(r.type==="ForNode"&&r.kind===z.PARALLEL){this._hasParallel=!0,this._parallelExtent=r.extent&&r.extent.type==="IntImmNode"?r.extent.value:0;return}if(r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody)}}}_visitFor(e){let t=e.loopVar.name,r=this._constExtent(e.extent);if(e.kind===z.PARALLEL){if(r!==null&&this._parallelExtent&&r!==this._parallelExtent){this._emitForLoop(t,e.extent,e.body);return}this._emit("(local.get $_par_start)"),this._emit("local.set $"+t),this._emit("(block $break_"+t),this._indent++,this._emit("(loop $loop_"+t),this._indent++,this._emit("(local.get $"+t+")"),this._emit("(local.get $_par_end)"),this._emit("i32.ge_s"),this._emit("br_if $break_"+t),this._visitNode(e.body),this._emit("(local.get $"+t+")"),this._emit("(i32.const 1)"),this._emit("i32.add"),this._emit("local.set $"+t),this._emit("br $loop_"+t),this._indent--,this._emit(")"),this._indent--,this._emit(")");return}if(e.kind===z.VECTORIZED&&this.target.supportsSimd()&&r!==null){this._visitVectorizedFor(e);return}if((e.kind===z.UNROLLED||e.kind===z.VECTORIZED)&&r!==null&&r<=32&&!this._isZeroFillBody(e.body)){for(let o=0;o<r;o++)this._emit("(i32.const "+o+")"),this._emit("local.set $"+t),this._visitNode(e.body);return}let s=this._detectWasmAcc(e);if(s){let o="_wacc_"+(this._waccCounter=(this._waccCounter||0)+1);this._ensureLocal(o,Fs(s.buf.dtype)),this._emitAddr(s.buf,s.outerIndices),this._emitLoadOp(s.buf.dtype),this._emit("local.set $"+o),this._wasmAcc={local:o,bufName:s.buf.name,indices:s.indices},this._emitForLoop(t,e.extent,e.body),this._emitAddr(s.buf,s.outerIndices),this._emit("(local.get $"+o+")"),this._emitStoreOp(s.buf.dtype),this._wasmAcc=null;return}this._emitForLoop(t,e.extent,e.body)}_visitLIRFlatStore(e){if(this._wasmAcc&&e.buffer.name===this._wasmAcc.bufName){this._emitCoercedTo(e.value,this._numPrefix(e.dtype)),this._emit("local.set $"+this._wasmAcc.local);return}this._emitFlatAddr(e.buffer,e.offsetExpr),this._emitCoercedTo(e.value,this._numPrefix(e.dtype)),this._emitStoreOp(e.dtype)}_visitLIRBindings(e){for(let t of e.bindings)this._emitExpr(t.expr),this._emit(`local.set $${t.name}`);this._visitNode(e.body)}_vecAccumOperandsUnitStride(e){let t=e.loopVar&&e.loopVar.name;if(!t)return!1;let r=l(i=>{let a=[i];for(;a.length>0;){let u=a.pop();if(!(!u||typeof u!="object")){if(u.type==="VariableNode"&&u.name===t)return!0;if(u.a&&a.push(u.a),u.b&&a.push(u.b),u.expr&&a.push(u.expr),u.args)for(let p of u.args)a.push(p);if(u.indices)for(let p of u.indices)a.push(p);u.offsetExpr&&a.push(u.offsetExpr)}}return!1},"usesVar"),s=l(i=>{let a=[i];for(;a.length>0;){let u=a.pop();if(!(!u||typeof u!="object")){if(u.type==="MathOpNode"&&u.op==="*"&&(r(u.a)||r(u.b)))return!0;if(u.a&&a.push(u.a),u.b&&a.push(u.b),u.expr&&a.push(u.expr),u.args)for(let p of u.args)a.push(p)}}return!1},"stridedMul"),o=[e.body];for(;o.length>0;){let i=o.pop();if(!(!i||typeof i!="object")){if(i.type==="BufferLoadNode"&&Array.isArray(i.indices)){for(let a=0;a<i.indices.length-1;a++)if(r(i.indices[a]))return!1}if(i.type==="LIRFlatLoadNode"&&i.offsetExpr&&s(i.offsetExpr))return!1;if(i.a&&o.push(i.a),i.b&&o.push(i.b),i.expr&&o.push(i.expr),i.args)for(let a of i.args)o.push(a);i.body&&o.push(i.body)}}return!0}_accumInstr(e,t){if(e==="*")return"mul";if(e==="max"||e==="min"){if(gr(t))return e;throw new Error(`wasm accumulator: integer ${e} reduction not supported (dtype ${t})`)}return"add"}_visitLIRAccumulator(e){let t=e.localName,r=e.dtype;this._ensureLocal(t,Fs(r));let s=this._constExtent(e.extent),o=e.op||"+",i=o==="+"&&s!==null&&e.loopKind===z.VECTORIZED&&this.target.supportsSimd()?h0(r):null,a=i?this.target.vectorWidth:0;if(i&&s>=a&&this._vecAccumOperandsUnitStride(e)){this._visitVecAccumulator(e,i,a,s);return}this._emitExpr(e.initLoad),this._emit("local.set $"+t);let u=this._wasmAcc;this._wasmAcc={local:t,bufName:e.flushStore.buffer.name};let p=e.loopVar.name;this._emit("(i32.const 0)"),this._emit("local.set $"+p),this._emit("(block $break_"+p),this._indent++,this._emit("(loop $loop_"+p),this._indent++,this._emit("(local.get $"+p+")"),this._emitExpr(e.extent),this._emit("i32.ge_s"),this._emit("br_if $break_"+p),this._emit("(local.get $"+t+")"),this._emitCoercedTo(e.body,this._numPrefix(r)),this._emit(this._numPrefix(r)+"."+this._accumInstr(o,r)),this._emit("local.set $"+t),this._emit("(local.get $"+p+")"),this._emit("(i32.const 1)"),this._emit("i32.add"),this._emit("local.set $"+p),this._emit("br $loop_"+p),this._indent--,this._emit(")"),this._indent--,this._emit(")"),this._emitFlatAddr(e.flushStore.buffer,e.flushStore.offsetExpr),this._emit("(local.get $"+t+")"),this._emitStoreOp(e.flushStore.dtype),this._wasmAcc=u}_visitVecAccumulator(e,t,r,s){let o=e.localName,i=e.dtype,a=e.loopVar.name,u=gr(i),p=u?"f32.add":"i32.add",c=zs(i,"add"),f=Math.floor(s/r)*r,d=f,h=o+"_vec";this._ensureLocal(h,"v128"),this._emitExpr(e.initLoad),this._emit("local.set $"+o),this._emit(u?"(f32.const 0)":"(i32.const 0)"),this._emit(t.splat),this._emit("local.set $"+h);let m=this._wasmAcc;this._wasmAcc={local:o,bufName:e.flushStore.buffer.name},this._vectorMode={dtype:i,lanes:r,loopVar:a,simd:t,laneVars:this._computeLaneVars(e)},this._emit("(i32.const 0)"),this._emit("local.set $"+a),this._emit("(block $vbreak_"+a),this._indent++,this._emit("(loop $vloop_"+a),this._indent++,this._emit("(local.get $"+a+")"),this._emit("(i32.const "+f+")"),this._emit("i32.ge_s"),this._emit("br_if $vbreak_"+a),this._emit("(local.get $"+h+")"),this._emitVecExpr(e.body),this._emit(c),this._emit("local.set $"+h),this._emit("(local.get $"+a+")"),this._emit("(i32.const "+r+")"),this._emit("i32.add"),this._emit("local.set $"+a),this._emit("br $vloop_"+a),this._indent--,this._emit(")"),this._indent--,this._emit(")"),this._vectorMode=null,this._emit("(local.get $"+o+")");for(let g=0;g<r;g++)this._emit("(local.get $"+h+")"),this._emit(t.extractLane+" "+g),this._emit(p);this._emit("local.set $"+o),d<s&&(this._emit("(i32.const "+d+")"),this._emit("local.set $"+a),this._emit("(block $tbreak_"+a),this._indent++,this._emit("(loop $tloop_"+a),this._indent++,this._emit("(local.get $"+a+")"),this._emit("(i32.const "+s+")"),this._emit("i32.ge_s"),this._emit("br_if $tbreak_"+a),this._emit("(local.get $"+o+")"),this._emitCoerced(e.body,u),this._emit(p),this._emit("local.set $"+o),this._emit("(local.get $"+a+")"),this._emit("(i32.const 1)"),this._emit("i32.add"),this._emit("local.set $"+a),this._emit("br $tloop_"+a),this._indent--,this._emit(")"),this._indent--,this._emit(")")),this._emitFlatAddr(e.flushStore.buffer,e.flushStore.offsetExpr),this._emit("(local.get $"+o+")"),this._emitStoreOp(e.flushStore.dtype),this._wasmAcc=m}_emitFlatAddr(e,t){let r=this._bufferOffsets.get(e.name)||0,s=fp(e.dtype||"f32");if(!t||t.type==="IntImmNode"&&t.value===0){this._emit(`(i32.const ${r})`);return}let o=this._vectorMode,i=s+"#"+this._exprKey(t);if(o&&o.addrLocal&&this._exprKey(t)!=="?"){(!o._addrEmitted||o._addrKey!==i)&&(this._emitExpr(t),this._emit(`(i32.const ${s})`),this._emit("i32.mul"),this._emit("local.set $"+o.addrLocal),o._addrEmitted=!0,o._addrKey=i),this._emit("(local.get $"+o.addrLocal+")"),r>0&&(this._emit(`(i32.const ${r})`),this._emit("i32.add"));return}this._emitExpr(t),this._emit(`(i32.const ${s})`),this._emit("i32.mul"),r>0&&(this._emit(`(i32.const ${r})`),this._emit("i32.add"))}_emitForLoop(e,t,r){this._emit("(i32.const 0)"),this._emit("local.set $"+e),this._emit("(block $break_"+e),this._indent++,this._emit("(loop $loop_"+e),this._indent++,this._emit("(local.get $"+e+")"),this._emitExpr(t),this._emit("i32.ge_s"),this._emit("br_if $break_"+e),this._loopVarStack.push(e),this._visitNode(r),this._loopVarStack.pop(),this._emit("(local.get $"+e+")"),this._emit("(i32.const 1)"),this._emit("i32.add"),this._emit("local.set $"+e),this._emit("br $loop_"+e),this._indent--,this._emit(")"),this._indent--,this._emit(")")}_visitBlock(e){for(let t of e.iterVars)if(t.iterVar&&t.binding){this._emitExpr(t.binding),this._emit(`local.set $${t.iterVar.name}`);let r=this._vectorMode;r&&r.laneVars&&this._dependsOnVecVar(t.binding)&&r.laneVars.add(t.iterVar.name)}if(e.initBody){let t=this._loopVarStack.length>0?this._loopVarStack[this._loopVarStack.length-1]:null;t?(this._emit("(local.get $"+t+")"),this._emit("i32.eqz"),this._emit("(if"),this._indent++,this._emit("(then"),this._indent++,this._visitNode(e.initBody),this._indent--,this._emit(")"),this._indent--,this._emit(")")):this._visitNode(e.initBody)}this._visitNode(e.body)}_visitStore(e){if(this._wasmAcc&&this._isAccTarget(e.buffer,e.indices)){this._emitCoercedTo(e.value,this._numPrefix(e.buffer.dtype)),this._emit("local.set $"+this._wasmAcc.local);return}this._emitAddr(e.buffer,e.indices),this._emitCoercedTo(e.value,this._numPrefix(e.buffer.dtype)),this._emitStoreOp(e.buffer.dtype)}_visitIf(e){this._emitExpr(e.condition),this._emit("(if"),this._indent++,this._emit("(then"),this._indent++,this._visitNode(e.thenBody),this._indent--,this._emit(")"),e.elseBody&&(this._emit("(else"),this._indent++,this._visitNode(e.elseBody),this._indent--,this._emit(")")),this._indent--,this._emit(")")}_visitWhile(e){this._visitNode(e.condBody),this._emit("(block $wbreak"),this._indent++,this._emit("(loop $wloop"),this._indent++,this._emitAddr(e.condVar,[]),this._emitLoadOp(e.condVar.dtype),this._emit("i32.eqz"),this._emit("br_if $wbreak"),this._visitNode(e.loopBody),this._visitNode(e.condBody),this._emit("br $wloop"),this._indent--,this._emit(")"),this._indent--,this._emit(")")}_emitAddr(e,t){let r=this._bufferOffsets.get(e.name)||0,s=fp(e.dtype);if(t.length===0){this._emit(`(i32.const ${r})`);return}let o=this._vectorMode;if(o&&o.addrLocal&&t.length>0){let i=s+"#"+t.map(a=>this._exprKey(a)).join(",");(!o._addrEmitted||o._addrKey!==i)&&(this._emitFlatIndex(e,t),this._emit(`(i32.const ${s})`),this._emit("i32.mul"),this._emit("local.set $"+o.addrLocal),o._addrEmitted=!0,o._addrKey=i),this._emit("(local.get $"+o.addrLocal+")"),r>0&&(this._emit(`(i32.const ${r})`),this._emit("i32.add"));return}this._emitFlatIndex(e,t),this._emit(`(i32.const ${s})`),this._emit("i32.mul"),r>0&&(this._emit(`(i32.const ${r})`),this._emit("i32.add"))}_emitFlatIndex(e,t){if(t.length===1){this._emitExpr(t[0]);return}let r=!0;for(let s=0;s<t.length;s++){this._emitExpr(t[s]);let o=e.strides[s];typeof o=="number"&&o>=0?o!==1&&(this._emit(`(i32.const ${o})`),this._emit("i32.mul")):(this._emitDynamicStride(e,s),this._emit("i32.mul")),r||this._emit("i32.add"),r=!1}}_emitDynamicStride(e,t){let r=0;for(let s=t+1;s<e.shape.length;s++){let o=e.shape[s];if(typeof o=="number"&&o>=0)this._emit(`(i32.const ${o})`);else{let i=this._resolveShapeParam(e,s);this._emit(`(local.get $${i})`)}r>0&&this._emit("i32.mul"),r++}r===0&&this._emit("(i32.const 1)")}_resolveShapeParam(e,t){return ji(this._primFunc,e,t,r=>r.name,"WASM","wat")}_emitExpr(e){if(!e){this._emit("(i32.const 0)");return}switch(e.type){case"IntImmNode":this._emit(`(i32.const ${e.value})`);break;case"FloatImmNode":this._emit(`(f32.const ${e.value})`);break;case"VariableNode":this._emit(`(local.get $${e.name})`);break;case"BufferLoadNode":this._wasmAcc&&this._isAccTarget(e.buffer,e.indices)?this._emit("(local.get $"+this._wasmAcc.local+")"):(this._emitAddr(e.buffer,e.indices),this._emitLoadOp(e.buffer.dtype));break;case"LIRFlatLoadNode":this._wasmAcc&&e.buffer.name===this._wasmAcc.bufName?this._emit("(local.get $"+this._wasmAcc.local+")"):(this._emitFlatAddr(e.buffer,e.offsetExpr),this._emitLoadOp(e.dtype));break;case"MathOpNode":this._emitMathOp(e);break;case"CompareNode":this._emitCompare(e);break;case"CastNode":this._emitCast(e);break;case"CallExternNode":this._emitCallExtern(e);break;case"IfThenElseNode":{let t=e._dtype||br(e.thenBody),r=this._numPrefix(t);this._emitExpr(e.condition),gr(this._wasmExprDtype(e.condition))&&(this._emit("(f32.const 0)"),this._emit("f32.ne")),this._emit("(if (result "+r+")"),this._indent++,this._emit("(then"),this._indent++,this._emitCoercedTo(e.thenBody,r),this._indent--,this._emit(")"),this._emit("(else"),this._indent++,this._emitCoercedTo(e.elseBody,r),this._indent--,this._emit(")"),this._indent--,this._emit(")");break}default:this._emit("(i32.const 0)");break}}_numPrefix(e){return e==="f64"?"f64":e==="i64"?"i64":gr(e)?"f32":"i32"}_exprPrefix(e){return this._numPrefix(e&&e._dtype||br(e))}_convertTo(e,t){e!==t&&(t==="f64"?e==="f32"?this._emit("f64.promote_f32"):e==="i32"?this._emit("f64.convert_i32_s"):e==="i64"&&this._emit("f64.convert_i64_s"):t==="f32"?e==="f64"?this._emit("f32.demote_f64"):e==="i32"?this._emit("f32.convert_i32_s"):e==="i64"&&this._emit("f32.convert_i64_s"):t==="i32"?e==="f64"?this._emit("i32.trunc_f64_s"):e==="f32"?this._emit("i32.trunc_f32_s"):e==="i64"&&this._emit("i32.wrap_i64"):t==="i64"&&(e==="i32"?this._emit("i64.extend_i32_s"):e==="f32"?this._emit("i64.trunc_f32_s"):e==="f64"&&this._emit("i64.trunc_f64_s")))}_emitCoercedTo(e,t){this._emitExpr(e),this._convertTo(this._exprPrefix(e),t)}_emitCoerced(e,t){this._emitCoercedTo(e,t?"f32":"i32")}_emitMathOp(e){let t=this._exprPrefix(e.a),r=e.b?this._exprPrefix(e.b):t,s=this._joinPrefix(t,r);if(!e.b){e.op==="-"?s==="f32"||s==="f64"?(this._emitCoercedTo(e.a,s),this._emit(s+".neg")):(this._emit("("+s+".const 0)"),this._emitCoercedTo(e.a,s),this._emit(s+".sub")):e.op==="!"&&(this._emitExpr(e.a),this._emit("i32.eqz"));return}if(e.op==="&&"){this._emitExpr(e.a),this._emitExpr(e.b),this._emit("i32.and");return}if(e.op==="||"){this._emitExpr(e.a),this._emitExpr(e.b),this._emit("i32.or");return}let o=s==="f32"||s==="f64";if(!o){if(e.op==="/"||e.op==="//"){this._emitIntDiv(e,s);return}if(e.op==="%"){this._emitIntRem(e,s);return}}switch(this._emitCoercedTo(e.a,s),this._emitCoercedTo(e.b,s),e.op){case"+":this._emit(`${s}.add`);break;case"-":this._emit(`${s}.sub`);break;case"*":this._emit(`${s}.mul`);break;case"/":this._emit(o?`${s}.div`:`${s}.div_s`);break;case"%":this._emit(`${s}.rem_s`);break;case"//":this._emit(`${s}.div_s`);break;case"<":this._emit(o?`${s}.lt`:`${s}.lt_s`);break;case">":this._emit(o?`${s}.gt`:`${s}.gt_s`);break;case"<=":this._emit(o?`${s}.le`:`${s}.le_s`);break;case">=":this._emit(o?`${s}.ge`:`${s}.ge_s`);break;default:this._emit(`${s}.add`);break}}_joinPrefix(e,t){return e==="f64"||t==="f64"?"f64":e==="f32"||t==="f32"?"f32":e==="i64"||t==="i64"?"i64":"i32"}_emitCompare(e){let t=this._exprPrefix(e.a),r=this._exprPrefix(e.b),s=this._joinPrefix(t,r),o=s==="f32"||s==="f64";this._emitCoercedTo(e.a,s),this._emitCoercedTo(e.b,s);let a={eq:"eq",ne:"ne",lt:o?"lt":"lt_s",le:o?"le":"le_s",gt:o?"gt":"gt_s",ge:o?"ge":"ge_s"}[e.direction];if(!a)throw new Error(`WASM codegen: unhandled compare direction '${e.direction}'`);this._emit(s+"."+a)}_emitCast(e){this._emitExpr(e.expr),this._convertTo(this._numPrefix(e.fromDtype),this._numPrefix(e.toDtype))}_emitCallExtern(e){if((e.externName==="min"||e.externName==="max")&&!gr(e.dtype)){this._emitIntMinMax(e);return}if(e.externName==="abs"&&!gr(e.dtype)){this._emitIntAbs(e);return}let t=new Set(["sqrt","abs","ceil","floor","min","max","rsqrt"]),r=e.externName==="abs"||t.has(e.externName)?this._numPrefix(e.dtype)==="i32"?"f32":this._numPrefix(e.dtype):"f32";if(e.externName==="rsqrt"){this._emit("("+r+".const 1)");for(let s of e.args)this._emitCoercedTo(s,r);this._emit(r+".sqrt"),this._emit(r+".div");return}for(let s of e.args)this._emitCoercedTo(s,r);switch(e.externName){case"sqrt":this._emit(r+".sqrt");break;case"abs":this._emit(r+".abs");break;case"ceil":this._emit(r+".ceil");break;case"floor":this._emit(r+".floor");break;case"min":this._emit(r+".min");break;case"max":this._emit(r+".max");break;default:this._imports.has(e.externName)&&(this._emit(`call $math_${e.externName}`),this._numPrefix(e.dtype)==="f64"&&this._emit("f64.promote_f32"));break}}_emitIntAbs(e){let t=this._intMinMaxEmitDepth||0,r="_iabs"+t;this._intMinMaxEmitDepth=t+1,this._emitExpr(e.args[0]),this._emit("local.set $"+r),this._intMinMaxEmitDepth=t,this._emit("(i32.const 0)"),this._emit("(local.get $"+r+")"),this._emit("i32.sub"),this._emit("(local.get $"+r+")"),this._emit("(local.get $"+r+")"),this._emit("(i32.const 0)"),this._emit("i32.lt_s"),this._emit("select")}_emitIntMinMax(e){let t=this._intMinMaxEmitDepth||0,r="_immm_a"+t,s="_immm_b"+t;this._intMinMaxEmitDepth=t+1,this._emitExpr(e.args[0]),this._emit("local.set $"+r),this._emitExpr(e.args[1]),this._emit("local.set $"+s),this._intMinMaxEmitDepth=t,this._emit("(local.get $"+r+")"),this._emit("(local.get $"+s+")"),this._emit("(local.get $"+r+")"),this._emit("(local.get $"+s+")"),this._emit(e.externName==="min"?"i32.lt_s":"i32.gt_s"),this._emit("select")}_isIntDivNode(e){if(e.type!=="MathOpNode"||!e.b||e.op!=="/"&&e.op!=="//"&&e.op!=="%")return!1;let t=this._joinPrefix(this._exprPrefix(e.a),this._exprPrefix(e.b));return t==="i32"||t==="i64"}_emitIntDiv(e,t){let r=this._intDivEmitDepth||0,s="_idiv_a"+r,o="_idiv_b"+r;this._intDivEmitDepth=r+1,this._emitCoercedTo(e.a,t),this._emit("local.set $"+s),this._emitCoercedTo(e.b,t),this._emit("local.set $"+o),this._intDivEmitDepth=r;let i=t==="i64"?"-9223372036854775808":"-2147483648";this._emit("(local.get $"+s+")"),this._emit("("+t+".const 1)"),this._emit("(local.get $"+o+")"),this._emit("(local.get $"+o+")"),this._emit(t+".eqz"),this._emit("(local.get $"+s+")"),this._emit("("+t+".const "+i+")"),this._emit(t+".eq"),this._emit("(local.get $"+o+")"),this._emit("("+t+".const -1)"),this._emit(t+".eq"),this._emit("i32.and"),this._emit("i32.or"),this._emit("select"),this._emit(t+".div_s"),this._emit("local.set $"+s),this._emit("("+t+".const 0)"),this._emit("(local.get $"+s+")"),this._emit("(local.get $"+o+")"),this._emit(t+".eqz"),this._emit("select")}_emitIntRem(e,t){let r=this._intDivEmitDepth||0,s="_idiv_a"+r,o="_idiv_b"+r;this._intDivEmitDepth=r+1,this._emitCoercedTo(e.a,t),this._emit("local.set $"+s),this._emitCoercedTo(e.b,t),this._emit("local.set $"+o),this._intDivEmitDepth=r,this._emit("(local.get $"+s+")"),this._emit("("+t+".const 1)"),this._emit("(local.get $"+o+")"),this._emit("(local.get $"+o+")"),this._emit(t+".eqz"),this._emit("select"),this._emit(t+".rem_s"),this._emit("local.set $"+s),this._emit("("+t+".const 0)"),this._emit("(local.get $"+s+")"),this._emit("(local.get $"+o+")"),this._emit(t+".eqz"),this._emit("select")}_prescanIntMinMax(e){let t=l((r,s,o)=>{if(!r||typeof r!="object")return;let i=s,a=o;if(r.type==="CallExternNode"&&(r.externName==="min"||r.externName==="max")&&!gr(r.dtype))this._ensureLocal("_immm_a"+s,"i32"),this._ensureLocal("_immm_b"+s,"i32"),s+1>this._intMinMaxDepth&&(this._intMinMaxDepth=s+1),i=s+1;else if(r.type==="CallExternNode"&&r.externName==="abs"&&!gr(r.dtype))this._ensureLocal("_iabs"+s,"i32"),s+1>this._intMinMaxDepth&&(this._intMinMaxDepth=s+1),i=s+1;else if(this._isIntDivNode(r)){let u=Fs(this._joinPrefix(this._exprPrefix(r.a),this._exprPrefix(r.b)));this._ensureLocal("_idiv_a"+o,u),this._ensureLocal("_idiv_b"+o,u),a=o+1}if(r.body&&t(r.body,i,a),r.value&&typeof r.value=="object"&&t(r.value,i,a),r.a&&t(r.a,i,a),r.b&&t(r.b,i,a),r.expr&&t(r.expr,i,a),r.condition&&t(r.condition,i,a),r.offsetExpr&&t(r.offsetExpr,i,a),r.thenBody&&t(r.thenBody,i,a),r.elseBody&&t(r.elseBody,i,a),r.initBody&&t(r.initBody,i,a),r.stmts)for(let u of r.stmts)t(u,i,a);if(r.args)for(let u of r.args)t(u,i,a);if(r.indices)for(let u of r.indices)t(u,i,a);if(r.bindings)for(let u of r.bindings)t(u.expr,i,a);if(r.iterVars)for(let u of r.iterVars)u.binding&&t(u.binding,i,a)},"visit");t(e,0,0)}_mathImportSig(e,t){return`${Array(t).fill("(param f32)").join(" ")} (result f32)`}_constExtent(e){return e.type==="IntImmNode"?e.value:null}_isZeroFillBody(e){return Mm(e)}_collectBindings(e,t){let r=[e];for(;r.length>0;){let s=r.pop();if(!(!s||typeof s!="object")){if(s.type==="BlockNode"&&s.iterVars)for(let o of s.iterVars)o.iterVar&&o.binding&&t.push({name:o.iterVar.name,expr:o.binding});if(s.type==="LIRBindingsNode"&&s.bindings)for(let o of s.bindings)t.push({name:o.name,expr:o.expr});if(s.body&&r.push(s.body),s.stmts)for(let o of s.stmts)r.push(o);s.thenBody&&r.push(s.thenBody),s.elseBody&&r.push(s.elseBody),s.initBody&&r.push(s.initBody),s.loopBody&&r.push(s.loopBody)}}}_computeLaneVars(e){let t=new Set([e.loopVar.name]),r=[];this._collectBindings(e.body,r);let s=l(i=>{let a=[],u=[i];for(;u.length>0;){let p=u.pop();if(!(!p||typeof p!="object")&&(p.type==="VariableNode"&&a.push(p.name),p.a&&u.push(p.a),p.b&&u.push(p.b),p.expr&&u.push(p.expr),p.args))for(let c of p.args)u.push(c)}return a},"varsIn"),o=!0;for(;o;){o=!1;for(let i of r)t.has(i.name)||s(i.expr).some(a=>t.has(a))&&(t.add(i.name),o=!0)}return t}_vecLoadsContiguous(e,t){let r=l(i=>{let a=[i];for(;a.length>0;){let u=a.pop();if(!(!u||typeof u!="object")){if(u.type==="VariableNode"&&t.has(u.name))return!0;if(u.a&&a.push(u.a),u.b&&a.push(u.b),u.expr&&a.push(u.expr),u.args)for(let p of u.args)a.push(p);if(u.indices)for(let p of u.indices)a.push(p);u.offsetExpr&&a.push(u.offsetExpr)}}return!1},"usesLane"),s=l(i=>{let a=[i];for(;a.length>0;){let u=a.pop();if(!(!u||typeof u!="object")){if(u.type==="MathOpNode"&&u.op==="*"&&(r(u.a)||r(u.b)))return!0;if(u.a&&a.push(u.a),u.b&&a.push(u.b),u.expr&&a.push(u.expr),u.args)for(let p of u.args)a.push(p)}}return!1},"stridedMul"),o=[e];for(;o.length>0;){let i=o.pop();if(!(!i||typeof i!="object")){if(i.type==="BufferLoadNode"&&Array.isArray(i.indices)){for(let a=0;a<i.indices.length-1;a++)if(r(i.indices[a]))return!1}if(i.type==="LIRFlatLoadNode"&&i.offsetExpr&&s(i.offsetExpr))return!1;if(i.a&&o.push(i.a),i.b&&o.push(i.b),i.expr&&o.push(i.expr),i.args)for(let a of i.args)o.push(a);if(i.value&&typeof i.value=="object"&&o.push(i.value),i.body&&o.push(i.body),i.stmts)for(let a of i.stmts)o.push(a);i.thenBody&&o.push(i.thenBody),i.elseBody&&o.push(i.elseBody),i.loopBody&&o.push(i.loopBody)}}return!0}_vecStoresLaneIndexed(e,t){let r=[e];for(;r.length>0;){let s=r.pop();if(!s)continue;let o=null;if(s.type==="BufferStoreNode"?o=s.indices:s.type==="LIRFlatStoreNode"&&(o=s.offsetExpr),o!=null){let i=[],a=Array.isArray(o)?[...o]:[o];for(;a.length>0;){let u=a.pop();if(!(!u||typeof u!="object")){if(u.type==="VariableNode"&&i.push(u.name),u.a&&a.push(u.a),u.b&&a.push(u.b),u.expr&&a.push(u.expr),u.args)for(let p of u.args)a.push(p);if(u.indices)for(let p of u.indices)a.push(p);u.offsetExpr&&a.push(u.offsetExpr)}}if(!i.some(u=>t.has(u)))return!1}if(s.body&&r.push(s.body),s.stmts)for(let i of s.stmts)r.push(i);s.thenBody&&r.push(s.thenBody),s.elseBody&&r.push(s.elseBody),s.loopBody&&r.push(s.loopBody)}return!0}_visitVectorizedFor(e){let t=e.loopVar.name,r=this._constExtent(e.extent),s=this._inferBodyDtype(e.body)||this._defaultDtype,o=this.target.vectorWidth,i=h0(s);if(!i||r<o||this._treeHasHalf(e.body)){this._emitForLoop(t,e.extent,e.body);return}let a=this._computeLaneVars(e);if(!this._vecStoresLaneIndexed(e.body,a)||!this._vecLoadsContiguous(e.body,a)){this._emitForLoop(t,e.extent,e.body);return}let u=Math.floor(r/o)*o,p=r-u,c=fp(s),h=this._countBufAccesses(e.body)>=2?"_vaddr_"+t:null;if(u>0&&(this._vectorMode={dtype:s,lanes:o,loopVar:t,simd:i,addrLocal:h,laneVars:a},u===o?(this._emit("(i32.const 0)"),this._emit("local.set $"+t),this._emitVecAddrReset(),this._visitNode(e.body)):(this._emit("(i32.const 0)"),this._emit("local.set $"+t),this._emit("(block $vbreak_"+t),this._indent++,this._emit("(loop $vloop_"+t),this._indent++,this._emit("(local.get $"+t+")"),this._emit("(i32.const "+u+")"),this._emit("i32.ge_s"),this._emit("br_if $vbreak_"+t),this._emitVecAddrReset(),this._visitNode(e.body),this._emit("(local.get $"+t+")"),this._emit("(i32.const "+o+")"),this._emit("i32.add"),this._emit("local.set $"+t),this._emit("br $vloop_"+t),this._indent--,this._emit(")"),this._indent--,this._emit(")")),this._vectorMode=null),p>0)for(let m=u;m<r;m++)this._emit("(i32.const "+m+")"),this._emit("local.set $"+t),this._visitNode(e.body)}_emitVecAddrReset(){this._vectorMode&&this._vectorMode.addrLocal&&(this._vectorMode._addrEmitted=!1,this._vectorMode._addrKey=null)}_countBufAccesses(e){let t=0,r=[e];for(;r.length>0;){let s=r.pop();if(!(!s||typeof s!="object")){if((s.type==="BufferLoadNode"||s.type==="BufferStoreNode"||s.type==="LIRFlatLoadNode"||s.type==="LIRFlatStoreNode")&&t++,s.body&&r.push(s.body),s.value&&typeof s.value=="object"&&r.push(s.value),s.a&&r.push(s.a),s.b&&r.push(s.b),s.stmts)for(let o of s.stmts)r.push(o);if(s.args)for(let o of s.args)r.push(o);if(s.indices)for(let o of s.indices)r.push(o);s.thenBody&&r.push(s.thenBody),s.elseBody&&r.push(s.elseBody),s.expr&&r.push(s.expr),s.condition&&r.push(s.condition),s.offsetExpr&&r.push(s.offsetExpr)}}return t}_inferBodyDtype(e){let t=[e];for(;t.length>0;){let r=t.pop();if(r){if(r.type==="BufferStoreNode"&&r.buffer)return r.buffer.dtype;if(r.type==="LIRFlatStoreNode")return r.dtype||this._defaultDtype;if(r.type==="BufferLoadNode"&&r.buffer)return r.buffer.dtype;if(r.type==="LIRFlatLoadNode")return r.dtype||this._defaultDtype;if(r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),r.value&&typeof r.value=="object"&&t.push(r.value)}}return null}_emitVecStore(e){let t=this._vectorMode;this._emitAddr(e.buffer,e.indices),this._emitVecExpr(e.value),this._emit(t.simd.vecStore)}_emitVecFlatStore(e){let t=this._vectorMode;this._emitFlatAddr(e.buffer,e.offsetExpr),this._emitVecExpr(e.value),this._emit(t.simd.vecStore)}_dependsOnVecVar(e){let t=this._vectorMode;if(!t)return!0;let r=t.laneVars,s=Array.isArray(e)?[...e]:[e];for(;s.length>0;){let o=s.pop();if(!(!o||typeof o!="object")){if(o.type==="VariableNode"&&(o.name===t.loopVar||r&&r.has(o.name)))return!0;if(o.a&&s.push(o.a),o.b&&s.push(o.b),o.expr&&s.push(o.expr),o.args)for(let i of o.args)s.push(i);if(o.indices)for(let i of o.indices)s.push(i);o.offsetExpr&&s.push(o.offsetExpr)}}return!1}_emitVecExpr(e){if(!e){this._emit("(i32.const 0)");return}let t=this._vectorMode,r=t.dtype;switch(e.type){case"BufferLoadNode":this._dependsOnVecVar(e.indices)?(this._emitAddr(e.buffer,e.indices),this._emit(t.simd.vecLoad)):(this._emitAddr(e.buffer,e.indices),this._emit(Dh(e.buffer.dtype)),this._emit(t.simd.splat));break;case"LIRFlatLoadNode":this._dependsOnVecVar(e.offsetExpr)?(this._emitFlatAddr(e.buffer,e.offsetExpr),this._emit(t.simd.vecLoad)):(this._emitFlatAddr(e.buffer,e.offsetExpr),this._emit(Dh(e.dtype)),this._emit(t.simd.splat));break;case"FloatImmNode":this._emit("(f32.const "+e.value+")"),this._emit(t.simd.splat);break;case"IntImmNode":gr(r)?(this._emit("(f32.const "+e.value+")"),this._emit(t.simd.splat)):(this._emit("(i32.const "+e.value+")"),this._emit(t.simd.splat));break;case"VariableNode":t.vecLets&&t.vecLets.has(e.name)?this._emit("(local.get $"+e.name+"_vlet)"):e.name===t.loopVar?this._emit("(local.get $"+e.name+")"):this._locals.get(e.name)==="f32"?(this._emit("(local.get $"+e.name+")"),this._emit(t.simd.splat)):(this._emit("(local.get $"+e.name+")"),gr(r)&&this._emit("f32.convert_i32_s"),this._emit(t.simd.splat));break;case"MathOpNode":this._emitVecMathOp(e);break;case"CompareNode":this._emitVecCompare(e);break;case"CallExternNode":this._emitVecCallExtern(e);break;case"CastNode":this._emitVecExpr(e.expr),gr(e.toDtype)&&this._isVecMaskExpr(e.expr)&&(this._emit("(f32.const 1)"),this._emit("f32x4.splat"),this._emit("v128.and"));break;case"IfThenElseNode":this._emitVecSelect(e);break;default:this._emitExpr(e);break}}_isVecMaskExpr(e){return e?e.type==="CompareNode"||e.type==="MathOpNode"&&(e.op==="&&"||e.op==="||"||e.op==="!"):!1}_emitVecMathOp(e){let t=this._vectorMode,r=t.dtype;if(!e.b){if(e.op==="-"){let u=zs(r,"neg");u?(this._emitVecExpr(e.a),this._emit(u)):(this._emit("(i32.const 0)"),this._emit(t.simd.splat),this._emitVecExpr(e.a),this._emit(zs(r,"sub")))}else e.op==="!"&&(this._emitVecExpr(e.a),this._emit("v128.not"));return}if(e.op==="&&"){this._emitVecExpr(e.a),this._emitVecExpr(e.b),this._emit("v128.and");return}if(e.op==="||"){this._emitVecExpr(e.a),this._emitVecExpr(e.b),this._emit("v128.or");return}let o={"+":"add","-":"sub","*":"mul","/":"div"}[e.op];if(o){let u=zs(r,o);if(u){this._emitVecExpr(e.a),this._emitVecExpr(e.b),this._emit(u);return}}let a={"<":"lt",">":"gt","<=":"le",">=":"ge"}[e.op];if(a){let u=zs(r,a);if(u){this._emitVecExpr(e.a),this._emitVecExpr(e.b),this._emit(u);return}}this._emitExpr(e),this._emit(t.simd.splat)}_emitVecCompare(e){let t=this._vectorMode,r=t.dtype,s=zs(r,e.direction);s?(this._emitVecExpr(e.a),this._emitVecExpr(e.b),this._emit(s)):(this._emitExpr(e),this._emit(t.simd.splat))}_emitVecCallExtern(e){let t=this._vectorMode,r=t.dtype,s=zs(r,e.externName);if(s){e.externName==="min"||e.externName==="max"?(this._emitVecExpr(e.args[0]),this._emitVecExpr(e.args[1])):this._emitVecExpr(e.args[0]),this._emit(s);return}if(e.externName==="rsqrt"){let o=zs(r,"sqrt");if(o){this._emit("(f32.const 1)"),this._emit(t.simd.splat),this._emitVecExpr(e.args[0]),this._emit(o),this._emit(zs(r,"div"));return}}this._emitScalarizeFallback(e)}_emitVecSelect(e){let t=this._vectorMode;this._emitVecExpr(e.thenBody),this._emitVecExpr(e.elseBody),this._emitVecExpr(e.condition),this._emit(t.simd.bitselect)}_emitScalarizeFallback(e){let t=this._vectorMode,r=t.lanes,s=t.simd.extractLane,o=t.simd.replaceLane,i=t.simd.splat,a="_vtmp_"+this._vecTmpCounter++;this._ensureLocal(a,"v128");let u=[];for(let p=0;p<r;p++){let c="_vl_"+a+"_"+p;this._ensureLocal(c,Fs(t.dtype)),u.push(c)}this._emitVecExpr(e.args[0]),this._emit("local.set $"+a);for(let p=0;p<r;p++){if(this._emit("(local.get $"+a+")"),this._emit(s+" "+p),e.args.length>1){let c="_vtmp2_"+a;if(p===0){this._ensureLocal(c,"v128");let f=this._vectorMode;this._vectorMode=t,this._emitVecExpr(e.args[1]),this._vectorMode=f,this._emit("local.set $"+c)}this._emit("(local.get $"+c+")"),this._emit(s+" "+p)}this._imports.has(e.externName)&&this._emit("call $math_"+e.externName),this._emit("local.set $"+u[p])}this._emit("(local.get $"+u[r-1]+")"),this._emit(i);for(let p=r-2;p>=0;p--)this._emit("(local.get $"+u[p]+")"),this._emit(o+" "+p)}_prescanVecLocalsAll(e){let t=[e];for(;t.length>0;){let r=t.pop();if(r){if(r.type==="ForNode"&&r.kind===z.VECTORIZED&&(this._prescanVecLocals(r.body),this._countBufAccesses(r.body)>=2&&this._ensureLocal("_vaddr_"+r.loopVar.name,"i32"),this._prescanVecLets(r)),r.type==="LIRAccumulatorNode"&&r.loopKind===z.VECTORIZED&&(this._ensureLocal(r.localName+"_vec","v128"),this._prescanVecLocals(r.body)),r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),r.initBody&&t.push(r.initBody),r.loopBody&&t.push(r.loopBody)}}}_prescanVecLets(e){let t=this._computeLaneVars(e),r=l(o=>{let i=[o];for(;i.length>0;){let a=i.pop();if(!(!a||typeof a!="object")){if(a.type==="VariableNode"&&t.has(a.name))return!0;if(a.a&&i.push(a.a),a.b&&i.push(a.b),a.expr&&i.push(a.expr),a.args)for(let u of a.args)i.push(u);if(a.indices)for(let u of a.indices)i.push(u);a.offsetExpr&&i.push(a.offsetExpr)}}return!1},"dependsOn"),s=[e.body];for(;s.length>0;){let o=s.pop();if(!(!o||typeof o!="object")){if(o.type==="LetStmtNode"&&o.variable&&r(o.value)&&this._ensureLocal(o.variable.name+"_vlet","v128"),o.body&&s.push(o.body),o.stmts)for(let i of o.stmts)s.push(i);o.thenBody&&s.push(o.thenBody),o.elseBody&&s.push(o.elseBody),o.initBody&&s.push(o.initBody)}}}_prescanVecLocals(e){let t=[e];for(;t.length>0;){let r=t.pop();if(r){if(r.type==="CallExternNode"&&r.externName&&!zs(this._defaultDtype,r.externName)&&r.externName!=="rsqrt"){let o="_vtmp_"+this._vecTmpCounter;this._ensureLocal(o,"v128");let i=this.target.vectorWidth;for(let a=0;a<i;a++)this._ensureLocal("_vl_"+o+"_"+a,Fs(this._defaultDtype));r.args.length>1&&this._ensureLocal("_vtmp2_"+o,"v128"),this._vecTmpCounter++}if(r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);if(r.value&&typeof r.value=="object"&&r.value.type&&t.push(r.value),r.a&&typeof r.a=="object"&&t.push(r.a),r.b&&typeof r.b=="object"&&t.push(r.b),r.args)for(let s of r.args)typeof s=="object"&&t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody)}}}_layoutBuffers(e){let t=0,r=16;this._dynamicBuffers=new Set;let s=l(i=>{t=Math.ceil(t/r)*r,this._bufferOffsets.set(i.name,t);let a=i.numel();!i.shape.some(p=>typeof p!="number"||p<0)&&a>0?t+=a*fp(i.dtype):(this._dynamicBuffers.add(i.name),t+=65536)},"place");for(let[,i]of e.bufferMap)s(i);let o=new Map;this._collectBuffers(e.body,o);for(let[i,a]of o)this._bufferOffsets.has(i)||s(a);this._totalMemBytes=t}_collectBuffers(e,t){let r=[e];for(;r.length>0;){let s=r.pop();if(s){if((s.type==="BufferStoreNode"||s.type==="BufferLoadNode")&&s.buffer&&t.set(s.buffer.name,s.buffer),s.reads)for(let o of s.reads)o.buffer&&t.set(o.buffer.name,o.buffer);if(s.writes)for(let o of s.writes)o.buffer&&t.set(o.buffer.name,o.buffer);for(let o of ln(s))r.push(o)}}}_scanMathImports(e){let t=[e];for(;t.length>0;){let r=t.pop();if(!(!r||typeof r!="object")){if(r.type==="CallExternNode"&&r.externName){let s=r.externName;if(s!=="sqrt"&&s!=="min"&&s!=="max"){let o=this._mathImportSig(s,r.args.length);this._imports.set(s,o)}}if(r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);if(r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),r.initBody&&t.push(r.initBody),r.value&&typeof r.value=="object"&&r.value.type&&t.push(r.value),r.a&&typeof r.a=="object"&&t.push(r.a),r.b&&typeof r.b=="object"&&t.push(r.b),r.expr&&typeof r.expr=="object"&&t.push(r.expr),r.args)for(let s of r.args)typeof s=="object"&&t.push(s);if(r.indices)for(let s of r.indices)typeof s=="object"&&t.push(s);r.condition&&typeof r.condition=="object"&&t.push(r.condition)}}}_prescanLocals(e){this._waccCounter=0;let t=[e];for(;t.length>0;){let r=t.pop();if(r){if(r.type==="ForNode"){this._ensureLocal(r.loopVar.name,"i32");let s=this._accPatternDtype(r);s&&this._ensureLocal("_wacc_"+ ++this._waccCounter,Fs(s)),r.kind===z.VECTORIZED&&this.target.supportsSimd()&&this._prescanVecLocals(r.body)}if(r.type==="BlockNode")for(let s of r.iterVars)s.iterVar&&this._ensureLocal(s.iterVar.name,"i32");if(r.type==="LetStmtNode"&&r.variable){let s=br(r.value)||r.variable.dtype||this._defaultDtype;this._ensureLocal(r.variable.name,Fs(s))}if(r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),r.initBody&&t.push(r.initBody)}}this._waccCounter=0}_wasmExprDtype(e){if(!e||e.type==="CompareNode"||e.type==="MathOpNode"&&(e.op==="!"||e.op==="&&"||e.op==="||"||e.op==="<"||e.op===">"||e.op==="<="||e.op===">="))return"i32";if(e.type==="VariableNode"){let t=this._locals.get(e.name);if(t)return t==="f32"?"f32":"i32"}return br(e)}_fixLetStmtLocals(e){let t=[e];for(;t.length>0;){let r=t.pop();if(r){if(r.type==="LetStmtNode"&&r.variable&&r.value){let s=br(r.value);s&&this._locals.set(r.variable.name,Fs(s))}if(r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),r.initBody&&t.push(r.initBody)}}}_accPatternDtype(e){let t=e.body;if(!t||t.type!=="BlockNode")return null;let r=t.body;if(!r||r.type!=="BufferStoreNode")return null;let s=r.value;return!s||s.type!=="MathOpNode"||s.op!=="+"?null:s.a&&s.a.type==="BufferLoadNode"&&s.a.buffer.name===r.buffer.name||s.b&&s.b.type==="BufferLoadNode"&&s.b.buffer.name===r.buffer.name?r.buffer.dtype:null}_inferDtype(e){return e&&e._dtype||br(e)}_detectWasmAcc(e){let t=e.body;if(!t||t.type!=="BlockNode")return null;let r=t.body;if(!r||r.type!=="BufferStoreNode")return null;let s=r,o=s.value;if(!o||o.type!=="MathOpNode"||o.op!=="+")return null;let i=null;if(o.a&&o.a.type==="BufferLoadNode"&&o.a.buffer.name===s.buffer.name?i=o.a:o.b&&o.b.type==="BufferLoadNode"&&o.b.buffer.name===s.buffer.name&&(i=o.b),!i)return null;let a=this._indicesKey(s.buffer,s.indices),u=this._indicesKey(i.buffer,i.indices);if(a!==u)return null;let p=s.indices.map(c=>{if(c.type!=="VariableNode")return c;for(let f of t.iterVars)if(f.iterVar&&f.iterVar.name===c.name&&f.binding)return f.binding;return c});return{buf:s.buffer,indices:s.indices,outerIndices:p}}_isAccTarget(e,t){return!this._wasmAcc||e.name!==this._wasmAcc.bufName?!1:this._indicesKey(e,t)===this._indicesKey({name:this._wasmAcc.bufName,shape:e.shape,strides:e.strides},this._wasmAcc.indices)}_indicesKey(e,t){let r=[];for(let s=0;s<t.length;s++)r.push(this._exprKey(t[s]));return e.name+":"+r.join(",")}_exprKey(e){return e?e.type==="VariableNode"?"$"+e.name:e.type==="IntImmNode"?String(e.value):e.type==="MathOpNode"?"("+this._exprKey(e.a)+e.op+(e.b?this._exprKey(e.b):"")+")":"?":"?"}}});var Tl,H0=M(()=>{"use strict";Tl=class{static{l(this,"MinHeap")}_items;_compare;constructor(e){this._items=[],this._compare=e}get size(){return this._items.length}peek(){return this._items.length>0?this._items[0]:null}push(e){let t=this._items;t.push(e);let r=t.length-1;for(;r>0;){let s=r-1>>1;if(this._compare(t[r],t[s])>=0)break;[t[r],t[s]]=[t[s],t[r]],r=s}}pop(){let e=this._items;if(e.length===0)return null;let t=e[0],r=e.pop();if(e.length>0){e[0]=r;let s=0,o=e.length;for(;;){let i=2*s+1,a=2*s+2,u=s;if(i<o&&this._compare(e[i],e[u])<0&&(u=i),a<o&&this._compare(e[a],e[u])<0&&(u=a),u===s)break;[e[s],e[u]]=[e[u],e[s]],s=u}}return t}}});function Zi(n){let e=String(n);return/[.e]/.test(e)?e:`${e}.0`}function T1(n){let e=`(1.0 / (1.0 + ${Zi(Va)} * abs(${n})))`,t=qi.slice().reverse().reduce((r,s)=>`(${Zi(s)} + ${e} * ${r})`,"0.0");return`((select(-1.0, 1.0, ${n} >= 0.0)) * (1.0 - ${e} * ${t} * exp(-${n} * ${n})))`}function Gm(n){let e=`(${n} - 1.0)`,t=oi.map((s,o)=>o===0?Zi(s):`${Zi(s)} / (${e} + ${Zi(o)})`).join(" + "),r=`(${e} + ${Zi(7+.5)})`;return`(${Zi(.5*Math.log(2*Math.PI))} + (${e} + 0.5) * log(${r}) - ${r} + log(${t}))`}function x$(n){let e=`(log(${jm} / abs(sin(${jm} * ${n}))) - ${Gm(`(1.0 - ${n})`)})`;return`(select(${Gm(n)}, ${e}, ${n} < 0.5))`}function S$(n){let e=`(${jm} / (sin(${jm} * ${n}) * exp(${Gm(`(1.0 - ${n})`)})))`;return`(select(exp(${Gm(n)}), ${e}, ${n} < 0.5))`}function x1(n,e){for(let t of v$)n[t]&&e(n[t]);if(n.stmts)for(let t of n.stmts)e(t);if(n.indices)for(let t of n.indices)e(t);if(n.args)for(let t of n.args)e(t);n.initLoad&&e(n.initLoad),n.flushStore&&e(n.flushStore)}var w$,T$,jm,v$,k$,Y0,Um,S1=M(()=>{"use strict";Fe();tr();km();H0();ts();Np();Yi();w$=new Set(["!","&&","||"]),T$=new Set(["<",">","<=",">=","==","!="]);l(Zi,"_wgslFloat");l(T1,"_wgslErf");l(Gm,"_wgslLanczosCore");jm=Zi(Math.PI);l(x$,"_wgslLgamma");l(S$,"_wgslGamma");v$=["body","loopBody","condBody","initBody","thenBody","elseBody","value","a","b","condition","expr","offsetExpr"];l(x1,"walkFullChildren");k$={"threadIdx.x":"local_invocation_id.x","threadIdx.y":"local_invocation_id.y","threadIdx.z":"local_invocation_id.z","blockIdx.x":"workgroup_id.x","blockIdx.y":"workgroup_id.y","blockIdx.z":"workgroup_id.z"},Y0=class{static{l(this,"WebGPUKernel")}constructor(e,t,r,s,o,i,a){this.name=e,this.source=t,this.workgroupSize=r,this.dispatchSize=s,this.sharedMemBytes=o,this.params=i,this.bindings=a}},Um=class{static{l(this,"WebGPUCodegen")}constructor(e){this.target=e,this._indent=0,this._lines=[],this._threadBindings=new Map,this._sharedBuffers=[],this._workgroupSize=[1,1,1],this._dispatchSize=[1,1,1],this._defaultDtype="f32",this._storeBuffers=new Set}generate(e){if(this._indent=0,this._lines=[],this._threadBindings.clear(),this._sharedBuffers=[],this._workgroupSize=[1,1,1],this._dispatchSize=[1,1,1],this._primFunc=e,this._storeBuffers=new Set,this._promotedBuffers=new Set,this._promotedBufferDecls=[],this._wgPoolOffsets=null,this._wgPoolDecls=[],this._needsBarriers=!1,this._serializeThreads=!1,this._localSlots=null,this._slotDecls=[],this._scalarSlotNames=new Set,this._crossThread=null,this._crossExtent=null,e.type==="LIRFunc"){for(let[T,v]of e.metadata.threadBindings){this._threadBindings.set(T,v);for(let A of v)A.isDynamic||this._applyBindingDim(T,A.extent)}this._sharedBuffers=e.metadata.sharedBuffers}else this._scanBindings(e.body);this._scanStoreTargets(e.body),this._analyzeSharing(e);let r=[],s=[],o=0,i=this._checkF16Usage(e);i&&this._emit("enable f16;"),i&&this._emit("");let a=e.bufferMap.size+(e.shapeParams.length>0?1:0),u=[...e.bufferMap.values()],p=a>6&&u.every(T=>T.numel()>0);this._packedMode=p,this._packedOffsets=null;let c=new Map;{let T=0;for(let[,v]of e.bufferMap)c.set(v.name,T++)}{let T=null;for(let[,v]of e.bufferMap)if(this._storeBuffers.has(v.name)){T=v.dtype;break}this._defaultDtype=T||(u.length?u[0].dtype:"f32")}let f={f32:"f32",i32:"i32",u32:"u32",f16:"f16"};if(p){this._packedOffsets=new Map;let T=l(N=>Math.ceil(N/4)*4,"align4"),v=new Map;for(let[,N]of e.bufferMap){r.push(N.name);let k=this._storeBuffers.has(N.name),O=jn(N.dtype),R=(k?"w:":"r:")+O,B=v.get(R);B||(B={isWrite:k,wt:O,dtype:f[O]||N.dtype,bufs:[],off:0},v.set(R,B));let E=N.numel(),F=(k?"_pw_":"_pr_")+O;this._packedOffsets.set(N.name,{storage:F,offset:B.off}),B.bufs.push({name:N.name,offset:B.off,size:E,dtype:N.dtype,argIndex:c.get(N.name)}),B.off+=T(E)}let A=[...v.values()].sort((N,k)=>N.isWrite===k.isWrite?N.wt.localeCompare(k.wt):N.isWrite?1:-1);for(let N of A){let k=(N.isWrite?"_pw_":"_pr_")+N.wt,O=N.isWrite?"storage, read_write":"storage, read";s.push({index:o,name:k,mode:N.isWrite?"read_write":"read",packed:N.bufs,packedSize:N.off,dtype:N.dtype}),this._emit(`@group(0) @binding(${o}) var<${O}> ${k}: array<${N.wt}>;`),o++}}else for(let[,T]of e.bufferMap){r.push(T.name);let v=this._storeBuffers.has(T.name)?"read_write":"read",A=v==="read_write"?"storage, read_write":"storage, read";s.push({index:o,name:T.name,mode:v,dtype:T.dtype,argIndex:c.get(T.name)}),this._emit(`@group(0) @binding(${o}) var<${A}> ${T.name}: array<${jn(T.dtype)}>;`),o++}if(e.shapeParams.length>0){this._emit(""),this._emit("struct ShapeParams {"),this._indent++;for(let T of e.shapeParams)r.push(T.name),this._emit(`${T.name}: u32,`);this._indent--,this._emit("}"),s.push({index:o,name:"_shapes",mode:"read"}),this._emit(`@group(0) @binding(${o}) var<uniform> _shapes: ShapeParams;`),o++}this._emit("");for(let T of this._sharedBuffers){let v=T.numel();this._emit(`var<workgroup> ${T.name}: array<${jn(T.dtype)}, ${v>0?v:1}>;`)}for(let T of this._promotedBufferDecls)this._emit(`var<workgroup> ${T.name}: array<${jn(T.dtype)}, ${T.size}>;`);for(let T of this._wgPoolDecls)this._emit(`var<workgroup> ${T.pool}: array<${jn(T.dtype)}, ${T.size>0?T.size:1}>;`);(this._sharedBuffers.length>0||this._promotedBufferDecls.length>0||this._wgPoolDecls.length>0)&&this._emit("");let d=[],h=!this._serializeThreads&&this._hasBindingPrefix("threadIdx"),m=!this._serializeThreads&&this._hasBindingPrefix("blockIdx");h&&d.push("@builtin(local_invocation_id) _lid: vec3u"),m&&d.push("@builtin(workgroup_id) _wid: vec3u"),!h&&!m&&d.push("@builtin(global_invocation_id) _gid: vec3u");let g=this.target.maxThreadsPerBlock||256;for(;this._workgroupSize[0]*this._workgroupSize[1]*this._workgroupSize[2]>g;){let T=0;this._workgroupSize[1]>this._workgroupSize[T]&&(T=1),this._workgroupSize[2]>this._workgroupSize[T]&&(T=2),this._workgroupSize[T]=Math.max(1,this._workgroupSize[T]>>1)}let y=this._workgroupSize[0],_=this._workgroupSize[1],b=this._workgroupSize[2];if(this._emit(`@compute @workgroup_size(${y}, ${_}, ${b})`),this._emit(`fn ${e.name}(${d.join(", ")}) {`),this._indent++,!this._serializeThreads){let T=new Set;for(let[v,A]of this._threadBindings)if(k$[v]){for(let k of A)if(!T.has(k.varName)){let O=this._wgslBuiltinAccess(v);this._emit(`let ${k.varName}: i32 = i32(${O});`),T.add(k.varName)}}}this._assignLocalSlots(e),this._emitMissingLocalDecls(e),this._visitNode(e.body),this._indent--,this._emit("}");let w=this.target,x=[Math.min(this._workgroupSize[0],w.maxBlockDimX),Math.min(this._workgroupSize[1],w.maxBlockDimY),Math.min(this._workgroupSize[2],w.maxBlockDimZ)],S=[Math.min(this._dispatchSize[0],w.maxGridDimX),Math.min(this._dispatchSize[1],w.maxGridDimY),Math.min(this._dispatchSize[2],w.maxGridDimZ)];return new Y0(e.name,this._lines.join(`
|
|
7
|
+
`),x,S,this._sharedBuffers.reduce((T,v)=>T+Math.max(v.sizeInBytes(),0),0),r,s)}_checkF16Usage(e){for(let[,t]of e.bufferMap)if(t.dtype==="f16")return!0;return!1}_hasBindingPrefix(e){for(let t of this._threadBindings.keys())if(t.startsWith(e))return!0;return!1}_wgslBuiltinAccess(e){let t=e.indexOf(".");if(t<0)return"_gid.x";let r=e.substring(0,t),s=e.substring(t+1);return r==="threadIdx"?`_lid.${s}`:r==="blockIdx"?`_wid.${s}`:`_gid.${s}`}_scanStoreTargets(e){let t=[e];for(;t.length>0;){let r=t.pop();if(r){(r.type==="BufferStoreNode"||r.type==="LIRFlatStoreNode")&&this._storeBuffers.add(r.buffer.name),r.type==="LIRAccumulatorNode"&&r.flushStore&&this._storeBuffers.add(r.flushStore.buffer.name);for(let s of ln(r))t.push(s)}}}_scanBindings(e){_l(e,t=>{if(t.type==="ForNode"&&t.kind===z.THREAD_BINDING&&t.threadTag){let r=t.extent.type==="IntImmNode"?t.extent.value:0,s=t.extent.type!=="IntImmNode",o={varName:t.loopVar.name,extent:r,isDynamic:s,extentNode:t.extent};this._threadBindings.has(t.threadTag)?this._threadBindings.get(t.threadTag).push(o):this._threadBindings.set(t.threadTag,[o]),s||this._applyBindingDim(t.threadTag,r)}t.type==="AllocateNode"&&t.scope==="shared"&&this._sharedBuffers.push(t.buffer)})}_applyBindingDim(e,t){let r=za(e);r&&(r.space==="thread"?this._workgroupSize[r.axis]=Math.max(this._workgroupSize[r.axis],t):this._dispatchSize[r.axis]=Math.max(this._dispatchSize[r.axis],t))}_getMaxBindingExtent(e){return Em(this._threadBindings,e)}_hasRecurrence(e){let t=!1;return _l(e.body,r=>{if(r.type==="SyncThreadsNode")return t=!0,!1}),t}_analyzeSharing(e){let t=new Set;for(let[,f]of this._threadBindings)for(let d of f)d.extent>0&&t.add(d.extent);let r=this._threadBindings.size>0?this._findCrossThreadBuffers(e):new Set,s=this._threadBindings.size>0?this._findCrossExtentBuffers(e):new Set;this._crossThread=r,this._crossExtent=s;let o=this._hasRecurrence(e);if(!o&&t.size<=1&&r.size===0&&s.size===0)return;let i=this.target.sharedMemoryBytes||16384,a=this._sharedBuffers.reduce((f,d)=>f+Math.max(d.sizeInBytes(),0),0),u=new Set;for(let[,f]of e.bufferMap)u.add(f.name);let p=this._collectPromotionCandidates(e,u);p.sort((f,d)=>(r.has(d.name)?1:0)-(r.has(f.name)?1:0));let c=this._dispatchSize[0]*this._dispatchSize[1]*this._dispatchSize[2];if(o){let f=this.target.maxThreadsPerBlock||256,d=this._workgroupSize[0]*this._workgroupSize[1]*this._workgroupSize[2],h=this._packWorkgroupPool(e,p),m=a+h.bytes<=i;if(c===1&&d<=f&&m){this._needsBarriers=!0,this._wgPoolOffsets=h.offsets,this._wgPoolDecls=h.decls;for(let g of p)this._promotedBuffers.add(g.name);return}this._serializeThreads=!0,this._workgroupSize=[1,1,1],this._dispatchSize=[1,1,1],this._needsBarriers=!1}else this._serializeThreads=(r.size>0||s.size>0)&&c>1,this._serializeThreads&&(this._workgroupSize=[1,1,1],this._dispatchSize=[1,1,1]),this._needsBarriers=!this._serializeThreads;for(let f of p){let d=f.size*(Vs(f.dtype)||4);a+d<=i&&(this._promotedBuffers.add(f.name),this._promotedBufferDecls.push(f),a+=d)}}_collectPromotionCandidates(e,t){let r=[];_l(e.body,a=>{if(a.type==="AllocateNode"&&a.scope!=="shared"&&!t.has(a.buffer.name)){let u=a.buffer.numel(),p=u>0?u:this._estimateBufferSize(a.buffer);p>0&&r.push({name:a.buffer.name,dtype:a.buffer.dtype,size:p})}});let s=new Map;this._scanBufferRefs(e.body,s);let o=new Set;this._scanAllocateNodes(e.body,o);let i=new Set(r.map(a=>a.name));for(let[a,u]of s){if(t.has(a)||o.has(a)||i.has(a))continue;let p=u.numel(),c=p>0?p:this._estimateBufferSize(u);c>0&&(r.push({name:a,dtype:u.dtype,size:c}),i.add(a))}return r}_findRecurrenceBody(e){let t=null;return _l(e,r=>{if(!t&&r.type==="ForNode"&&r.kind===z.RECURRENCE)return t=r.body,!1}),t}_namesTouchedOutside(e,t,r){let s=new Set,o=l(i=>{if(!(!i||i===t)){(i.type==="BufferLoadNode"||i.type==="BufferStoreNode"||i.type==="LIRFlatLoadNode"||i.type==="LIRFlatStoreNode")&&i.buffer&&r.has(i.buffer.name)&&s.add(i.buffer.name),i.type==="LIRAccumulatorNode"&&(i.flushStore&&i.flushStore.buffer&&r.has(i.flushStore.buffer.name)&&s.add(i.flushStore.buffer.name),i.initLoad&&i.initLoad.buffer&&r.has(i.initLoad.buffer.name)&&s.add(i.initLoad.buffer.name));for(let a of["stmts","body","initBody","condBody","loopBody","thenBody","elseBody","value","a","b","condition","expr","offsetExpr","extent","indices","args"]){let u=i[a];if(u)if(Array.isArray(u))for(let p of u)o(p);else typeof u=="object"&&o(u)}}},"walk");return o(e),s}_packWorkgroupPool(e,t){let r=new Set(t.map(d=>d.name)),s=this._findRecurrenceBody(e.body),{minPos:o,maxPos:i}=s?this._livenessWalk(s,r):{minPos:new Map,maxPos:new Map},a=s?this._namesTouchedOutside(e.body,s,r):r,u=new Map;for(let d of t){u.has(d.dtype)||u.set(d.dtype,[]);let h=a.has(d.name)||!o.has(d.name),m=h?0:o.get(d.name),g=h?Number.MAX_SAFE_INTEGER:i.get(d.name);u.get(d.dtype).push({name:d.name,size:d.size,first:m,last:g})}let p=new Map,c=[],f=0;for(let[d,h]of u){h.sort((_,b)=>_.first-b.first||b.size-_.size);let m=[],g=`_wg_${jn(d)}`,y=0;for(let _ of h){let b=m.filter(x=>x.first<=_.last&&_.first<=x.last).map(x=>[x.offset,x.offset+x.size]).sort((x,S)=>x[0]-S[0]),w=0;for(let[x,S]of b){if(w+_.size<=x)break;w<S&&(w=S)}m.push({offset:w,size:_.size,first:_.first,last:_.last}),p.set(_.name,{pool:g,offset:w}),w+_.size>y&&(y=w+_.size)}c.push({pool:g,dtype:d,size:y}),f+=y*(Vs(d)||4)}return{offsets:p,decls:c,bytes:f}}_findCrossThreadBuffers(e){let t=new Set;for(let[,a]of e.bufferMap)t.add(a.name);let r=new Set,s=l((a,u)=>{if(u.length===0)return!1;let p=new Set;for(let c of a)this._collectVarNames(c,p);return u.some(c=>p.has(c))},"indexUsesLoopVar"),o=l((a,u)=>{let p=new Set;this._collectVarNames(a,p);for(let c of p)if(u.includes(c))return!0;return!1},"aliasOf"),i=l((a,u)=>{if(!a)return;let p=u;if((a.type==="ForNode"&&a.kind!==z.THREAD_BINDING||a.type==="WhileNode"||a.type==="LIRAccumulatorNode")&&a.loopVar&&(p=[...u,a.loopVar.name]),a.type==="LIRBindingsNode"&&a.bindings)for(let f of a.bindings)!p.includes(f.name)&&o(f.expr,p)&&(p=[...p,f.name]);if(a.type==="BlockNode"&&a.iterVars)for(let f of a.iterVars)f.iterVar&&f.binding&&!p.includes(f.iterVar.name)&&o(f.binding,p)&&(p=[...p,f.iterVar.name]);a.type==="LetStmtNode"&&a.variable&&!p.includes(a.variable.name)&&o(a.value,p)&&(p=[...p,a.variable.name]),a.type==="BufferLoadNode"&&a.buffer&&!t.has(a.buffer.name)&&s(a.indices||[],p)&&r.add(a.buffer.name),a.type==="LIRFlatLoadNode"&&a.buffer&&!t.has(a.buffer.name)&&a.offsetExpr&&s([a.offsetExpr],p)&&r.add(a.buffer.name),x1(a,f=>i(f,p))},"walk");return i(e.body,[]),r}_findCrossExtentBuffers(e){let t=new Set;for(let[,c]of e.bufferMap)t.add(c.name);let r=new Map,s=new Map,o=new Set,i=l(c=>c&&(c.type==="FloatImmNode"||c.type==="IntImmNode"),"isConst"),a=l((c,f,d)=>{if(t.has(f))return;let h=c.get(f);h||(h=new Set,c.set(f,h)),h.add(d)},"record"),u=l((c,f)=>{if(!c)return;let d=f;if(c.type==="ForNode"&&c.kind===z.THREAD_BINDING){let h=c.extent&&c.extent.type==="IntImmNode"?c.extent.value:0;h>0&&(d=f*h)}c.type==="BufferStoreNode"&&c.buffer&&(a(r,c.buffer.name,d),i(c.value)||o.add(c.buffer.name)),c.type==="LIRFlatStoreNode"&&c.buffer&&(a(r,c.buffer.name,d),i(c.value)||o.add(c.buffer.name)),c.type==="LIRAccumulatorNode"&&c.flushStore&&c.flushStore.buffer&&(a(r,c.flushStore.buffer.name,d),o.add(c.flushStore.buffer.name)),c.type==="BufferLoadNode"&&c.buffer&&a(s,c.buffer.name,d),c.type==="LIRFlatLoadNode"&&c.buffer&&a(s,c.buffer.name,d),x1(c,h=>u(h,d))},"walk");u(e.body,1);let p=new Set;for(let[c,f]of s){if(!o.has(c))continue;let d=r.get(c);if(d){for(let h of f)if(!d.has(h)){p.add(c);break}}}return p}_collectVarNames(e,t){let r=[e];for(;r.length>0;){let s=r.pop();if(!(!s||typeof s!="object")){if(s.type==="VariableNode"){t.add(s.name);continue}for(let o of["a","b","condition","thenBody","elseBody","expr","offsetExpr"])s[o]&&r.push(s[o]);if(s.indices)for(let o of s.indices)r.push(o);if(s.args)for(let o of s.args)r.push(o)}}}_emit(e){this._lines.push(" ".repeat(this._indent)+e)}_visitNode(e){Rm(this,e)}_emitSync(){this._needsBarriers&&(this._emit("storageBarrier();"),this._emit("workgroupBarrier();"))}_visitForNode(e){if(e.kind===z.THREAD_BINDING){if(this._serializeThreads){let a=e.loopVar.name,u=this._exprToWGSL(e.extent);this._emit(`for (var ${a}: i32 = 0; ${a} < ${u}; ${a} = ${a} + 1) {`),this._indent++,this._visitNode(e.body),this._indent--,this._emit("}");return}let s=e.extent.type==="IntImmNode"?e.extent.value:0,o=e.threadTag,i=this._getMaxBindingExtent(o);if(s>0&&i>0&&s<i){let a=this._wgslBuiltinAccess(o);this._emit(`if (i32(${a}) < ${s}) {`),this._indent++,this._visitNode(e.body),this._indent--,this._emit("}")}else this._visitNode(e.body);this._needsBarriers&&this._emit("workgroupBarrier();");return}let t=e.loopVar.name,r=this._exprToWGSL(e.extent);this._emit(`for (var ${t}: i32 = 0; ${t} < ${r}; ${t} = ${t} + 1) {`),this._indent++,this._visitNode(e.body),this._indent--,this._emit("}")}_visitBlockNode(e){for(let t of e.iterVars)t.iterVar&&t.binding&&this._emit(`let ${t.iterVar.name}: i32 = ${this._exprToWGSL(t.binding)};`);e.initBody&&this._visitNode(e.initBody),this._visitNode(e.body)}_emitMissingLocalDecls(){for(let e of this._slotDecls)e.scalar?this._emit(`var ${e.name}: ${jn(e.dtype)};`):this._emit(`var ${e.name}: array<${jn(e.dtype)}, ${e.size}>;`)}_collectLocalBuffers(e){let t=new Set;for(let[,a]of e.bufferMap)t.add(a.name);let r=new Set;for(let a of this._sharedBuffers)r.add(a.name);let s=new Map,o=l(a=>!t.has(a)&&!r.has(a)&&!this._promotedBuffers.has(a),"isLocal"),i=new Map;this._scanBufferRefs(e.body,i);for(let[a,u]of i)o(a)&&s.set(a,u);return _l(e.body,a=>{a.type==="AllocateNode"&&a.scope!=="shared"&&a.buffer&&o(a.buffer.name)&&s.set(a.buffer.name,a.buffer)}),s}_assignLocalSlots(e){let t=this._collectLocalBuffers(e);if(t.size===0)return;let{minPos:r,maxPos:s}=this._computeBufferLiveness(e,t),o=[...t.keys()].sort((h,m)=>r.get(h)-r.get(m)||s.get(h)-s.get(m));this._localSlots=new Map;let i=new Map,a=0,u=0,p=this._workgroupSize[0]*this._workgroupSize[1]*this._workgroupSize[2]*(this._dispatchSize[0]*this._dispatchSize[1]*this._dispatchSize[2]),c=this._crossThread||new Set,f=this._crossExtent||new Set,d=l((h,m)=>!this._serializeThreads&&this._threadBindings.size>0&&!c.has(h)&&!f.has(h)&&m.numel()>1&&m.numel()<=p,"scalarEligible");for(let h of o){let m=t.get(h),g=m.numel()>0?m.numel():this._estimateBufferSize(m),y=Math.max(g,1),_=r.get(h),b=s.get(h);if(d(h,m)){let T=`_s${u++}`;this._slotDecls.push({name:T,dtype:m.dtype,size:1,scalar:!0}),this._localSlots.set(h,T),this._scalarSlotNames.add(T);continue}let w=i.get(m.dtype);w||(w=new Tl((T,v)=>T.freeAt-v.freeAt),i.set(m.dtype,w));let x=null,S=w.peek();if(S&&S.freeAt<_&&(x=w.pop()),x)y>x.decl.size&&(x.decl.size=y);else{let T={name:`_lt${a++}`,dtype:m.dtype,size:y};this._slotDecls.push(T),x={decl:T}}x.freeAt=b,this._localSlots.set(h,x.decl.name),w.push(x)}}_computeBufferLiveness(e,t){return this._livenessWalk(e.body,t)}_livenessWalk(e,t){let r=new Map,s=new Map,o=new Set(["ForNode","WhileNode","LIRAccumulatorNode"]),i=["stmts","body","initBody","condBody","loopBody","thenBody","elseBody","value","a","b","condition","expr","offsetExpr","extent","indices","args"],a=0,u=[],p=null,c=0,f=l(h=>{t.has(h)&&(r.has(h)||r.set(h,a),s.set(h,a),u.length>0&&p.add(h))},"touch"),d=l(h=>{if(!h)return;a++;let m=o.has(h.type),g=!1;m&&(u.length===0&&(p=new Set,c=a,g=!0),u.push(h)),(h.type==="BufferLoadNode"||h.type==="BufferStoreNode"||h.type==="LIRFlatLoadNode"||h.type==="LIRFlatStoreNode")&&h.buffer&&f(h.buffer.name),h.type==="LIRAccumulatorNode"&&(h.flushStore&&h.flushStore.buffer&&f(h.flushStore.buffer.name),h.initLoad&&h.initLoad.buffer&&f(h.initLoad.buffer.name));for(let y of i){let _=h[y];if(_)if(Array.isArray(_))for(let b of _)d(b);else typeof _=="object"&&d(_)}if(m&&(u.pop(),g)){let y=a;for(let _ of p)r.set(_,Math.min(r.get(_),c)),s.set(_,Math.max(s.get(_),y));p=null}},"walk");return d(e),{minPos:r,maxPos:s}}_scanAllocateNodes(e,t){let r=[e];for(;r.length>0;){let s=r.pop();if(s){s.type==="AllocateNode"&&t.add(s.buffer.name);for(let o of ln(s))r.push(o)}}}_scanBufferRefs(e,t){let r=[e];for(;r.length>0;){let s=r.pop();if(s){(s.type==="BufferLoadNode"||s.type==="BufferStoreNode"||s.type==="LIRFlatLoadNode"||s.type==="LIRFlatStoreNode")&&s.buffer&&t.set(s.buffer.name,s.buffer),s.type==="LIRAccumulatorNode"&&(s.flushStore&&s.flushStore.buffer&&t.set(s.flushStore.buffer.name,s.flushStore.buffer),s.initLoad&&s.initLoad.buffer&&t.set(s.initLoad.buffer.name,s.initLoad.buffer));for(let o of ln(s))r.push(o)}}}_estimateBufferSize(e){return Im(e)}_visitAllocateNode(e){if(e.scope!=="shared"){if(this._promotedBuffers.has(e.buffer.name)||this._localSlots&&this._localSlots.has(e.buffer.name))return;let t=e.buffer.numel(),r=t>0?t:this._estimateBufferSize(e.buffer);r>0&&this._emit(`var ${e.buffer.name}: array<${jn(e.buffer.dtype)}, ${r}>;`)}}_visitIfStmt(e){this._emit(`if (${this._boolExpr(e.condition)}) {`),this._indent++,this._visitNode(e.thenBody),this._indent--,e.elseBody&&(this._emit("} else {"),this._indent++,this._visitNode(e.elseBody),this._indent--),this._emit("}")}_visitLetStmtNode(e){let t=e.variable.dtype||this._defaultDtype,r=jn(t),s=this._numericExpr(e.value,t);this._emit(`var ${e.variable.name}: ${r} = ${s};`),this._visitNode(e.body)}_visitWhileNode(e){this._visitNode(e.condBody);let t=Array.isArray(e.condVar.shape)?`${this._packedBufAccess(e.condVar.name,"0")} != 0`:e.condVar.name;this._emit(`while (${t}) {`),this._indent++,this._visitNode(e.loopBody),this._visitNode(e.condBody),this._indent--,this._emit("}")}_visitBufferStoreNode(e){let t=this._flatIndex(e.buffer,e.indices),r=this._packedBufAccess(e.buffer.name,t),s=this._numericExpr(e.value,e.buffer.dtype);this._emit(`${r} = ${s};`)}_visitLIRFlatStore(e){let t=this._exprToWGSL(e.offsetExpr),r=this._packedBufAccess(e.buffer.name,t),s=this._numericExpr(e.value,e.buffer.dtype);this._emit(`${r} = ${s};`)}_visitLIRBindings(e){for(let t of e.bindings)this._emit(`let ${t.name}: i32 = ${this._numericExpr(t.expr,"i32")};`);this._visitNode(e.body)}_visitLIRAccumulator(e){let t=e.localName,r=jn(e.dtype||this._defaultDtype);this._emit(`var ${t}: ${r} = ${this._exprToWGSL(e.initLoad)};`);let s=e.loopVar.name,o=this._exprToWGSL(e.extent);this._emit(`for (var ${s}: i32 = 0; ${s} < ${o}; ${s} = ${s} + 1) {`),this._indent++;let i=e.op||"+",a=this._exprToWGSL(e.body);this._emit(`${t} = ${i==="max"||i==="min"?`${i}(${t}, ${a})`:`(${t} ${i} ${a})`};`),this._indent--,this._emit("}");let u=this._exprToWGSL(e.flushStore.offsetExpr),p=this._packedBufAccess(e.flushStore.buffer.name,u);this._emit(`${p} = ${t};`)}_isBoolExpr(e){return e?!!(e.type==="CompareNode"||e.type==="MathOpNode"&&(w$.has(e.op)||T$.has(e.op))):!1}_numericExpr(e,t){if(this._isBoolExpr(e)){let r=t?jn(t):"i32";return r==="f32"?`select(0.0, 1.0, ${this._exprToWGSL(e)})`:`select(${r}(0), ${r}(1), ${this._exprToWGSL(e)})`}return this._exprToWGSL(e)}_boolExpr(e){return this._isBoolExpr(e)?this._exprToWGSL(e):`(${this._exprToWGSL(e)} != 0)`}_exprToWGSL(e){if(!e)return"0";switch(e.type){case"IntImmNode":return String(e.value);case"FloatImmNode":return this._emitFloatLiteral(e.value);case"VariableNode":return this._resolveVariable(e.name);case"BufferLoadNode":return this._packedBufAccess(e.buffer.name,this._flatIndex(e.buffer,e.indices));case"LIRFlatLoadNode":return this._packedBufAccess(e.buffer.name,this._exprToWGSL(e.offsetExpr));case"MathOpNode":{if(!e.b)return e.op==="!"?`(!${this._boolExpr(e.a)})`:`(${e.op}${this._numericExpr(e.a)})`;if(e.op==="&&")return`(${this._boolExpr(e.a)} && ${this._boolExpr(e.b)})`;if(e.op==="||")return`(${this._boolExpr(e.a)} || ${this._boolExpr(e.b)})`;let t=this._numericExpr(e.a),r=this._numericExpr(e.b);return e.op==="//"?`(${t} / ${r})`:e.op==="%"?`(${t} % ${r})`:`(${t} ${e.op} ${r})`}case"CompareNode":return`(${this._numericExpr(e.a)} ${Ku(e.direction)} ${this._numericExpr(e.b)})`;case"IfThenElseNode":return`select(${this._exprToWGSL(e.elseBody)}, ${this._exprToWGSL(e.thenBody)}, ${this._boolExpr(e.condition)})`;case"CastNode":return`${jn(e.toDtype)}(${this._exprToWGSL(e.expr)})`;case"CallExternNode":return this._emitExternCall(e);default:throw new Error(`WebGPU codegen: unhandled expr node '${e.type}'`)}}_resolveVariable(e){if(this._primFunc&&this._primFunc.shapeParams){for(let t of this._primFunc.shapeParams)if(t.name===e)return`i32(_shapes.${e})`}return e}_emitFloatLiteral(e){return e===1/0?"f32(0x1.fffffep+127)":e===-1/0?"f32(-0x1.fffffep+127)":Number.isInteger(e)?`${e}.0`:String(e)}_emitExternCall(e){let t=e.args.length,r=new Array(t);for(let i=0;i<t;i++)r[i]=this._exprToWGSL(e.args[i]);let s=r.join(", ");if(e.externName==="fmod")return`(${r[0]} % ${r[1]})`;if(e.externName==="erf")return T1(r[0]);if(e.externName==="erfc")return`(1.0 - ${T1(r[0])})`;if(e.externName==="lgamma")return x$(r[0]);if(e.externName==="gamma")return S$(r[0]);if(e.externName==="log10")return`(log(${r[0]}) * ${1/Math.LN10})`;let o=DN(e.externName);if(o===e.externName&&!PN(e.externName))throw new Error(`WebGPU codegen: unsupported extern function "${e.externName}"`);return`${o}(${s})`}_packedBufAccess(e,t){if(this._wgPoolOffsets&&this._wgPoolOffsets.has(e)){let r=this._wgPoolOffsets.get(e);return r.offset===0?`${r.pool}[${t}]`:`${r.pool}[${r.offset}u + u32(${t})]`}if(this._packedMode&&this._packedOffsets&&this._packedOffsets.has(e)){let r=this._packedOffsets.get(e);return r.offset===0?`${r.storage}[${t}]`:`${r.storage}[${r.offset}u + u32(${t})]`}if(this._localSlots&&this._localSlots.has(e)){let r=this._localSlots.get(e);return this._scalarSlotNames.has(r)?r:`${r}[${t}]`}return`${e}[${t}]`}_flatIndex(e,t){return ml(e,t,r=>this._exprToWGSL(r),(r,s)=>this._computeDynamicStride(r,s),!1)}_computeDynamicStride(e,t){return Ui(e,t+1,(r,s)=>this._resolveShapeParam(r,s))}_resolveShapeParam(e,t){return ji(this._primFunc,e,t,r=>`i32(_shapes.${r.name})`,"WebGPU","wgsl")}}});function v1(n){let{source:e,name:t,metadata:r}=n,s=[];for(let i=0;i<r.paramCount;i++)s.push(`buf_${i}`);let o=[];return o.push(e),o.push(""),o.push(`// ${t}(${s.map(i=>`/* ${i}: Float32Array */`).join(", ")});`),o.join(`
|
|
8
|
+
`)}var k1=M(()=>{"use strict";l(v1,"buildSnippet")});function re(n){let e=[];do{let t=n&127;n>>>=7,n&&(t|=128),e.push(t)}while(n);return e}function C$(n){let e=[],t=!0;for(;t;){let r=n&127;n>>=7,n===0&&!(r&64)||n===-1&&r&64?t=!1:r|=128,e.push(r)}return e}function D$(n){n=BigInt(n);let e=[],t=!0;for(;t;){let r=Number(n&0x7fn);n>>=7n,n===0n&&!(r&64)||n===-1n&&r&64?t=!1:r|=128,e.push(r)}return e}function qm(n){let e=new TextEncoder().encode(n);return[...re(e.length),...e]}function P$(n){let e=new ArrayBuffer(4);return new Float32Array(e)[0]=n,[...new Uint8Array(e)]}function $$(n){let e=new ArrayBuffer(8);return new Float64Array(e)[0]=n,[...new Uint8Array(e)]}function _o(n,e){for(let t=0;t<e.length;t++)n.push(e[t]);return n}function xl(n,e){let t=[n];return _o(t,re(e.length)),_o(t,e),t}function Ua(n){let e=[];_o(e,re(n.length));for(let t of n)_o(e,t);return e}function F$(n){let e=[],t=0;for(;t<n.length;){let r=n[t];if(r<=" "){t++;continue}if(r===";"&&n[t+1]===";"){t=n.indexOf(`
|
|
9
|
+
`,t),t<0&&(t=n.length);continue}if(r==="("&&n[t+1]===";"){t=n.indexOf(";)",t+2),t=t<0?n.length:t+2;continue}if(r==="("||r===")"){e.push(r),t++;continue}if(r==='"'){let o=t+1;for(;o<n.length&&n[o]!=='"';)o++;e.push(n.substring(t,o+1)),t=o+1;continue}let s=t;for(;s<n.length&&n[s]>" "&&n[s]!=="("&&n[s]!==")";)s++;e.push(n.substring(t,s)),t=s}return e}function z$(n){let e=0,t=l(()=>n[e++],"eat"),r=l(()=>n[e],"peek"),s=l(y=>{if(t()!==y)throw new Error("expected "+y+" at "+(e-1))},"expect");s("("),s("module");let o=[],i=1,a=256,u="",p=[],c=[],f=[],d=[],h=-1,m=-1;for(;r()!==")";){if(r()!=="("){e++;continue}e++;let y=t();if(y==="memory"){for(;r()==="(";)g();r()!==")"&&(i=parseInt(t())||1),r()!==")"&&(a=parseInt(t())||256),s(")")}else if(y==="import"){let _=t().replace(/"/g,""),b=t().replace(/"/g,"");s("("),s("func"),r().startsWith("$")&&e++;let w=[],x=[];for(;r()==="(";){e++;let S=t();if(S==="param")for(;r()!==")";){let T=t();T==="f32"?w.push(Rp):T==="f64"?w.push(Bp):T==="i32"?w.push(Op):T==="v128"&&w.push(Mp)}else if(S==="result")for(;r()!==")";){let T=t();T==="f32"?x.push(Rp):T==="f64"?x.push(Bp):T==="i32"?x.push(Op):T==="v128"&&x.push(Mp)}s(")")}s(")"),s(")"),o.push({module:_,name:b,params:w,results:x})}else if(y==="func"){for(;r()==="(";){let b=e;e++;let w=t();if(w==="export")u=t().replace(/"/g,""),s(")");else if(w==="param"){let x="";for(;r()!==")";){let S=t();if(S.startsWith("$")){x=S.replace("$","");continue}let T=null;S==="i32"?T=Op:S==="i64"?T=Q0:S==="f32"?T=Rp:S==="f64"?T=Bp:S==="v128"&&(T=Mp),T!==null&&(p.push(T),c.push(x),x="")}s(")")}else if(w==="result"){for(;r()!==")";)t();s(")")}else if(w==="local"){for(;r()!==")";){let x=t();x.startsWith("$")?d.push(x.replace("$","")):x==="i32"?f.push(Op):x==="i64"?f.push(Q0):x==="f32"?f.push(Rp):x==="f64"?f.push(Bp):x==="v128"&&f.push(Mp)}s(")")}else{e=b;break}}h=e;let _=1;for(;_>0;){let b=t();b==="("?_++:b===")"&&_--}m=e-1}else g()}function g(){if(r()!=="("){e++;return}let y=0;do{let _=t();_==="("?y++:_===")"&&y--}while(y>0)}return l(g,"skipSExpr"),{imports:o,memMin:i,memMax:a,funcExportName:u,funcParams:p,funcParamNames:c,funcLocals:f,funcLocalNames:d,bodyTokens:n.slice(h,m)}}function V$(n,e,t){let r=[],s=0,o=l(()=>n[s],"peek"),i=l(()=>n[s++],"eat"),a=[],u=-1;for(let y of e.values())y>u&&(u=y);function p(y){let _=y.replace("$","");if(e.has(_))return e.get(_);let b=parseInt(_,10);if(!isNaN(b))return b>u&&(u=b),b;let w=u+1;return u=w,e.set(_,w),w}l(p,"localIdx");function c(){return o()&&o().startsWith("$")?i().replace("$",""):""}l(c,"eatLabel");function f(y,_){let b=y.indexOf(_);return b>=0?y.substring(b+_.length):""}l(f,"extractName");function d(){for(;s<n.length&&o()!==")";){let y=o();if(y==="("){s++;let _=i();if(_==="i32.const")r.push(65),r.push(...C$(parseInt(i()))),m(")");else if(_==="i64.const")r.push(66),r.push(...D$(i())),m(")");else if(_==="f32.const")r.push(67),r.push(...P$(parseFloat(i()))),m(")");else if(_==="f64.const")r.push(68),r.push(...$$(parseFloat(i()))),m(")");else if(_==="local.get")r.push(32),r.push(...re(p(i()))),m(")");else if(_==="local.set")r.push(33),r.push(...re(p(i()))),m(")");else if(_==="block"){let b=i().replace("$","");a.push(b),r.push(2,J0),d(),r.push(11),a.pop(),m(")")}else if(_==="loop"){let b=i().replace("$","");a.push(b),r.push(3,J0),d(),r.push(11),a.pop(),m(")")}else if(_==="if"){let b=J0;if(o()==="("){let w=s;if(s++,o()==="result"){i();let x=i();b=x==="f32"?Rp:x==="f64"?Bp:x==="v128"?Mp:x==="i64"?Q0:Op,m(")")}else s=w}for(r.push(4,b),a.push("_if");s<n.length&&o()!==")"&&o()==="(";){let w=s;s++;let x=i();if(x==="then")d(),m(")");else if(x==="else")r.push(5),d(),m(")");else{s=w;break}}r.push(11),a.pop(),m(")")}else s-=2,g();continue}if(s++,y==="br_if")r.push(13),r.push(...re(h(c())));else if(y==="br")r.push(12),r.push(...re(h(c())));else if(y.startsWith("call")){r.push(16);let _=f(y,"math_")||(o()&&o().startsWith("$")?f(i(),"math_"):""),b=t.get(_);if(b===void 0)throw new Error(`WAT encoder: call to unresolved import '${_}'`);r.push(...re(b))}else y==="local.get"?(r.push(32),r.push(...re(p(c())))):y==="local.set"?(r.push(33),r.push(...re(p(c())))):y.endsWith(".extract_lane")||y.endsWith(".replace_lane")?(r.push(...Z0.get(y)),r.push(parseInt(i(),10))):Z0.has(y)&&r.push(...Z0.get(y))}}l(d,"emitBlock");function h(y){for(let _=a.length-1;_>=0;_--)if(a[_]===y)return a.length-1-_;return 0}l(h,"resolveBr");function m(y){if(i()!==y)throw new Error("expect "+y)}l(m,"expect");function g(){if(o()!=="("){s++;return}let y=0;do{let _=i();_==="("?y++:_===")"&&y--}while(y>0)}return l(g,"skipSExpr"),d(),r}function Sl(n){let e=F$(n),t=z$(e),r=new Map,s=t.funcParams.length;for(let T=0;T<t.funcParamNames.length;T++)t.funcParamNames[T]&&r.set(t.funcParamNames[T],T);for(let T=0;T<t.funcLocalNames.length;T++)r.set(t.funcLocalNames[T],s+T);let o=new Map;for(let T=0;T<t.imports.length;T++)o.set(t.imports[T].name,T);let i=[],a=new Map;function u(T,v){let A=T.join(",")+">"+v.join(",");if(a.has(A))return a.get(A);let N=i.length;return i.push({params:T,results:v}),a.set(A,N),N}l(u,"getType");for(let T of t.imports)T.typeIdx=u(T.params,T.results);let p=u(t.funcParams,[]),c=xl(O$,Ua(i.map(T=>[L$,...re(T.params.length),...T.params,...re(T.results.length),...T.results]))),f=[];t.imports.length>0&&(f=xl(B$,Ua(t.imports.map(T=>[...qm(T.module),...qm(T.name),0,...re(T.typeIdx)]))));let d=xl(R$,Ua([[...re(p)]])),h=xl(M$,Ua([[1,...re(t.memMin),...re(t.memMax)]])),m=t.imports.length,g=xl(I$,Ua([[...qm("memory"),2,...re(0)],[...qm(t.funcExportName),0,...re(m)]])),y=[];if(t.funcLocals.length>0){let T=[],v=t.funcLocals[0],A=1;for(let N=1;N<t.funcLocals.length;N++)t.funcLocals[N]===v?A++:(T.push([...re(A),v]),v=t.funcLocals[N],A=1);T.push([...re(A),v]),_o(y,Ua(T))}else y.push(0);let _=V$(t.bodyTokens,r,o),b=[];_o(b,y),_o(b,_),b.push(11);let w=[];_o(w,re(b.length)),_o(w,b);let x=xl(E$,Ua([w])),S=[];for(let T of[N$,A$,c,f,d,h,g,x])_o(S,T);return new Uint8Array(S)}var N$,A$,O$,B$,R$,M$,I$,E$,Op,Q0,Bp,Rp,Mp,L$,J0,Z0,Wm=M(()=>{"use strict";N$=[0,97,115,109],A$=[1,0,0,0],O$=1,B$=2,R$=3,M$=5,I$=7,E$=10,Op=127,Q0=126,Bp=124,Rp=125,Mp=123,L$=96,J0=64;l(re,"uleb");l(C$,"sleb");l(D$,"slebBig");l(qm,"encStr");l(P$,"encF32");l($$,"encF64");l(_o,"pushAll");l(xl,"section");l(Ua,"vec");Z0=new Map([["i32.const",[65]],["f32.const",[67]],["local.get",[32]],["local.set",[33]],["i32.add",[106]],["i32.sub",[107]],["i32.mul",[108]],["i32.div_s",[109]],["i32.rem_s",[111]],["i32.eq",[70]],["i32.ne",[71]],["i32.lt_s",[72]],["i32.gt_s",[74]],["i32.le_s",[76]],["i32.ge_s",[78]],["i32.eqz",[69]],["i32.lt_u",[73]],["i32.gt_u",[75]],["i32.le_u",[77]],["i32.ge_u",[79]],["i32.and",[113]],["i32.or",[114]],["i32.xor",[115]],["i32.shl",[116]],["i32.shr_s",[117]],["i32.shr_u",[118]],["i32.reinterpret_f32",[188]],["f32.reinterpret_i32",[190]],["i32.trunc_f32_s",[168]],["f32.add",[146]],["f32.sub",[147]],["f32.mul",[148]],["f32.div",[149]],["f32.neg",[140]],["f32.abs",[139]],["f32.ceil",[141]],["f32.floor",[142]],["f32.sqrt",[145]],["f32.min",[150]],["f32.max",[151]],["f32.eq",[91]],["f32.ne",[92]],["f32.lt",[93]],["f32.gt",[94]],["f32.le",[95]],["f32.ge",[96]],["f32.convert_i32_s",[178]],["f64.add",[160]],["f64.sub",[161]],["f64.mul",[162]],["f64.div",[163]],["f64.min",[164]],["f64.max",[165]],["f64.neg",[154]],["f64.abs",[153]],["f64.ceil",[155]],["f64.floor",[156]],["f64.sqrt",[159]],["f64.eq",[97]],["f64.ne",[98]],["f64.lt",[99]],["f64.gt",[100]],["f64.le",[101]],["f64.ge",[102]],["f64.convert_i32_s",[183]],["f64.promote_f32",[187]],["f32.demote_f64",[182]],["i32.trunc_f64_s",[170]],["i64.load",[41,3,0]],["i64.store",[55,3,0]],["i64.add",[124]],["i64.sub",[125]],["i64.mul",[126]],["i64.div_s",[127]],["i64.rem_s",[129]],["i64.and",[131]],["i64.or",[132]],["i64.xor",[133]],["i64.shl",[134]],["i64.shr_s",[135]],["i64.shr_u",[136]],["i64.eqz",[80]],["i64.eq",[81]],["i64.ne",[82]],["i64.lt_s",[83]],["i64.gt_s",[85]],["i64.le_s",[87]],["i64.ge_s",[89]],["i64.extend_i32_s",[172]],["i32.wrap_i64",[167]],["f64.convert_i64_s",[185]],["i64.trunc_f64_s",[176]],["f32.convert_i64_s",[180]],["i64.trunc_f32_s",[174]],["select",[27]],["f32.load",[42,2,0]],["f32.store",[56,2,0]],["f64.load",[43,3,0]],["f64.store",[57,3,0]],["i32.load",[40,2,0]],["i32.store",[54,2,0]],["i32.load8_s",[44,0,0]],["i32.load8_u",[45,0,0]],["i32.store8",[58,0,0]],["i32.load16_s",[46,1,0]],["i32.load16_u",[47,1,0]],["i32.store16",[59,1,0]],["v128.load",[253,...re(0),4,0]],["v128.store",[253,...re(11),4,0]],["v128.bitselect",[253,...re(82)]],["v128.and",[253,...re(78)]],["v128.or",[253,...re(80)]],["v128.not",[253,...re(77)]],["f32x4.splat",[253,...re(19)]],["i32x4.splat",[253,...re(17)]],["f32x4.add",[253,...re(228)]],["f32x4.sub",[253,...re(229)]],["f32x4.mul",[253,...re(230)]],["f32x4.div",[253,...re(231)]],["f32x4.neg",[253,...re(225)]],["f32x4.abs",[253,...re(224)]],["f32x4.sqrt",[253,...re(227)]],["f32x4.ceil",[253,...re(103)]],["f32x4.floor",[253,...re(104)]],["f32x4.min",[253,...re(232)]],["f32x4.max",[253,...re(233)]],["f32x4.eq",[253,...re(65)]],["f32x4.ne",[253,...re(66)]],["f32x4.lt",[253,...re(67)]],["f32x4.gt",[253,...re(68)]],["f32x4.le",[253,...re(69)]],["f32x4.ge",[253,...re(70)]],["f32x4.extract_lane",[253,...re(31)]],["f32x4.replace_lane",[253,...re(32)]],["i32x4.add",[253,...re(174)]],["i32x4.sub",[253,...re(177)]],["i32x4.mul",[253,...re(181)]],["i32x4.abs",[253,...re(160)]],["i32x4.min_s",[253,...re(182)]],["i32x4.max_s",[253,...re(184)]],["i32x4.eq",[253,...re(55)]],["i32x4.ne",[253,...re(56)]],["i32x4.lt_s",[253,...re(57)]],["i32x4.gt_s",[253,...re(58)]],["i32x4.le_s",[253,...re(59)]],["i32x4.ge_s",[253,...re(60)]],["i32x4.extract_lane",[253,...re(27)]],["i32x4.replace_lane",[253,...re(28)]],["f64x2.splat",[253,...re(20)]],["f64x2.add",[253,...re(240)]],["f64x2.sub",[253,...re(241)]],["f64x2.mul",[253,...re(242)]],["f64x2.div",[253,...re(243)]],["f64x2.neg",[253,...re(237)]],["f64x2.abs",[253,...re(236)]],["f64x2.sqrt",[253,...re(239)]],["f64x2.min",[253,...re(244)]],["f64x2.max",[253,...re(245)]],["f64x2.eq",[253,...re(71)]],["f64x2.ne",[253,...re(72)]],["f64x2.lt",[253,...re(73)]],["f64x2.gt",[253,...re(74)]],["f64x2.le",[253,...re(75)]],["f64x2.ge",[253,...re(76)]],["f64x2.extract_lane",[253,...re(33)]],["f64x2.replace_lane",[253,...re(34)]]]);l(F$,"tokenize");l(z$,"parseModule");l(V$,"encodeBody");l(Sl,"encodeWat")});function N1(n){let{source:e,name:t,metadata:r}=n,{bufferOffsets:s,imports:o}=r,i=[...s.entries()],a=Sl(e),u=[];if(u.push("(async () => {"),u.push(""),u.push("const binary = new Uint8Array(["+a.join(",")+"]);"),u.push(""),o&&o.size>0){u.push("const mathImports = {");for(let[c]of o)c==="fmod"?u.push(" fmod: (a, b) => a % b,"):c==="rsqrt"?u.push(" rsqrt: x => 1 / Math.sqrt(x),"):u.push(` ${c}: Math.${c},`);u.push("};"),u.push("const { instance } = await WebAssembly.instantiate(binary, { math: mathImports });")}else u.push("const { instance } = await WebAssembly.instantiate(binary);");u.push("const memory = instance.exports.memory;"),u.push("");for(let[c,f]of i)u.push(`// ${c}: new Float32Array(memory.buffer, ${f}, N).set(data);`);u.push(""),u.push(`instance.exports.${t}(${i.map(([,c])=>c).join(", ")});`),u.push("");let p=i[i.length-1];return p&&(u.push(`const result = new Float32Array(memory.buffer, ${p[1]}, N);`),u.push("console.log(result);")),u.push("})();"),u.join(`
|
|
10
|
+
`)}var A1=M(()=>{"use strict";Wm();l(N1,"buildSnippet")});function O1(n){let{source:e,name:t,metadata:r}=n,{bindings:s,dispatchSize:o}=r,i=[];i.push("(async () => {"),i.push("const adapter = await navigator.gpu.requestAdapter();"),i.push("const device = await adapter.requestDevice();"),i.push(""),i.push("const wgsl = "+JSON.stringify(e)+";"),i.push(""),i.push("const pipeline = device.createComputePipeline({"),i.push(' layout: "auto",'),i.push(" compute: { module: device.createShaderModule({ code: wgsl }), entryPoint: "+JSON.stringify(t)+" }"),i.push("});"),i.push(""),i.push("const entries = [];"),i.push("const gpuBuffers = [];"),i.push("");for(let a of s)a.name==="_shapes"?G$(i,a):a.packed?j$(i,a):a.mode==="read_write"?U$(i,a):q$(i,a),i.push("");return i.push("const bindGroup = device.createBindGroup({ layout: pipeline.getBindGroupLayout(0), entries });"),i.push("const encoder = device.createCommandEncoder();"),i.push("const pass = encoder.beginComputePass();"),i.push("pass.setPipeline(pipeline);"),i.push("pass.setBindGroup(0, bindGroup);"),i.push(`pass.dispatchWorkgroups(${o[0]}, ${o[1]}, ${o[2]});`),i.push("pass.end();"),i.push(""),W$(i,s),i.push(""),i.push("gpuBuffers.forEach(b => b.destroy());"),i.push("device.destroy();"),i.push("})();"),i.join(`
|
|
11
|
+
`)}function G$(n,e){n.push(`// binding ${e.index}: uniform (_shapes)`),n.push("{"),n.push(" const shapeData = new Uint32Array([/* shape values */]);"),n.push(" const size = Math.max(Math.ceil(shapeData.byteLength / 16) * 16, 16);"),n.push(" const buf = device.createBuffer({ size, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });"),n.push(" device.queue.writeBuffer(buf, 0, shapeData);"),n.push(" entries.push({ binding: "+e.index+", resource: { buffer: buf } });"),n.push(" gpuBuffers.push(buf);"),n.push("}")}function j$(n,e){let t=e.mode==="read_write",r=t?"GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST":"GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST";if(n.push(`// binding ${e.index}: packed ${e.name} (${e.packed.length} buffers, ${e.packedSize} elems)`),n.push("{"),n.push(` const buf = device.createBuffer({ size: ${e.packedSize*4}, usage: ${r}, mappedAtCreation: true });`),n.push(" const mapped = new Float32Array(buf.getMappedRange());"),t)n.push(" mapped.fill(0);");else for(let s of e.packed)n.push(` // mapped.set(${s.name}_data, ${s.offset}); // size=${s.size}`);n.push(" buf.unmap();"),n.push(" entries.push({ binding: "+e.index+", resource: { buffer: buf } });"),n.push(" gpuBuffers.push(buf);"),n.push("}")}function U$(n,e){n.push(`// binding ${e.index}: output ${e.name}`),n.push("{"),n.push(" const size = N * 4; // N = output element count"),n.push(" const buf = device.createBuffer({ size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, mappedAtCreation: true });"),n.push(" new Float32Array(buf.getMappedRange()).fill(0);"),n.push(" buf.unmap();"),n.push(" entries.push({ binding: "+e.index+", resource: { buffer: buf } });"),n.push(" gpuBuffers.push(buf);"),n.push("}")}function q$(n,e){n.push(`// binding ${e.index}: input ${e.name}`),n.push("{"),n.push(` const data = ${e.name}_data; // Float32Array`),n.push(" const buf = device.createBuffer({ size: Math.max(data.byteLength, 4), usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST, mappedAtCreation: true });"),n.push(" new Float32Array(buf.getMappedRange()).set(data);"),n.push(" buf.unmap();"),n.push(" entries.push({ binding: "+e.index+", resource: { buffer: buf } });"),n.push(" gpuBuffers.push(buf);"),n.push("}")}function W$(n,e){if(e.filter(r=>r.mode==="read_write").length===0){n.push("device.queue.submit([encoder.finish()]);");return}for(let r=0;r<e.length;r++){let s=e[r];if(s.mode==="read_write")if(s.packed)for(let o of s.packed)n.push("{"),n.push(` const rb = device.createBuffer({ size: ${o.size*4}, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST });`),n.push(` encoder.copyBufferToBuffer(gpuBuffers[${r}], ${o.offset*4}, rb, 0, ${o.size*4});`),n.push(" device.queue.submit([encoder.finish()]);"),n.push(" await rb.mapAsync(GPUMapMode.READ);"),n.push(` console.log("${o.name}:", new Float32Array(rb.getMappedRange()));`),n.push(" rb.unmap(); rb.destroy();"),n.push("}");else n.push("{"),n.push(` const src = gpuBuffers[${r}];`),n.push(" const rb = device.createBuffer({ size: src.size, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST });"),n.push(" encoder.copyBufferToBuffer(src, 0, rb, 0, src.size);"),n.push(" device.queue.submit([encoder.finish()]);"),n.push(" await rb.mapAsync(GPUMapMode.READ);"),n.push(` console.log("${s.name}:", new Float32Array(rb.getMappedRange()));`),n.push(" rb.unmap(); rb.destroy();"),n.push("}")}}var B1=M(()=>{"use strict";l(O1,"buildSnippet");l(G$,"emitShapeBinding");l(j$,"emitPackedBinding");l(U$,"emitOutputBinding");l(q$,"emitInputBinding");l(W$,"emitReadback")});function R1(n){let{source:e,name:t,metadata:r}=n,{blockDim:s,gridDim:o,sharedMemBytes:i,params:a}=r,u=[];return u.push(e),u.push(""),u.push("// Launch:"),u.push(`// dim3 block(${s[0]}, ${s[1]}, ${s[2]});`),u.push(`// dim3 grid(${o[0]}, ${o[1]}, ${o[2]});`),i>0?u.push(`// ${t}<<<grid, block, ${i}>>>(${a.join(", ")});`):u.push(`// ${t}<<<grid, block>>>(${a.join(", ")});`),u.join(`
|
|
12
|
+
`)}var M1=M(()=>{"use strict";l(R1,"buildSnippet")});function Km(n,e){I1.set(n,e),e.runtimeKind&&e.snippet&&E1.set(e.runtimeKind,e.snippet)}function L1(n){return I1.get(n)||null}function C1(n){return E1.get(n)||null}var I1,E1,D1=M(()=>{"use strict";yo();d1();b1();w1();S1();k1();A1();B1();M1();I1=new Map,E1=new Map;l(Km,"registerCodegen");l(L1,"getCodegenEntry");l(C1,"getSnippetBuilder");Km(Dt.CPU,{runtimeKind:"js",snippet:v1,compile(n,e){return{source:new Fm(e).generate(n),metadata:{kind:"js",paramCount:n.params.length}}}});Km(Dt.WASM,{runtimeKind:"wasm",snippet:N1,compile(n,e){let t=new Vm(e).generate(n),r={kind:"wasm",memoryPages:t.memoryPages,bufferOffsets:t.bufferOffsets,imports:t.imports,params:t.params,bufferMap:n.bufferMap};return t.parallel&&(r.parallel=t.parallel),{source:t.wat,metadata:r}}});Km(Dt.WEBGPU,{runtimeKind:"webgpu",snippet:O1,compile(n,e){let t=new Um(e).generate(n);return{source:t.source,metadata:{kind:"webgpu",workgroupSize:t.workgroupSize,dispatchSize:t.dispatchSize,sharedMemBytes:t.sharedMemBytes,params:t.params,bindings:t.bindings}}}});Km(Dt.CUDA,{runtimeKind:"cuda",snippet:R1,compile(n,e,t){if(t&&t.matmulBackend==="cublas"&&n.cublasInfo)return{source:"",metadata:{kind:"cuda",cublas:n.cublasInfo,outputIndices:[n.cublasInfo.cIdx]}};let r=new zm(e).generate(n);return{source:r.source,metadata:{kind:"cuda",blockDim:r.blockDim,gridDim:r.gridDim,sharedMemBytes:r.sharedMemBytes,params:r.params,outputIndices:r.outputIndices,scratch:r.scratch}}}})});function P1(n){let e=[],t=[n.body];for(;t.length>0;){let m=t.pop();if(m){if(m.type==="BlockNode"){e.push(m),t.push(m.body);continue}if(m.body&&t.push(m.body),m.stmts)for(let g of m.stmts)t.push(g);m.thenBody&&t.push(m.thenBody),m.elseBody&&t.push(m.elseBody)}}let r=null;for(let m of e)if(m.name.includes("matmul"))m.reads.length>=2&&m.writes.length>=1&&(r=m);else return null;if(!r)return null;let s=r.reads[0].buffer,o=r.reads[1].buffer,i=r.writes[0].buffer;if(s.dtype!=="f32"||o.dtype!=="f32"||i.dtype!=="f32"||s.shape.length!==2||o.shape.length!==2||i.shape.length!==2)return null;let a=i.shape[0],u=i.shape[1],p=s.shape[1];if(![a,u,p].every(m=>typeof m=="number"))return null;let c=[];for(let[,m]of n.bufferMap)c.push(m.name);let f=c.indexOf(s.name),d=c.indexOf(o.name),h=c.indexOf(i.name);return f<0||d<0||h<0?null:{M:a,N:u,K:p,aIdx:f,bIdx:d,cIdx:h}}var Ip,bo,Ep=M(()=>{"use strict";D1();l(P1,"detectPureMatmul");Ip=class{static{l(this,"CompiledKernel")}constructor(e,t,r,s={}){this.name=e,this.source=t,this.target=r,this.metadata=s}snippet(){let e=C1(this.metadata.kind);if(!e)throw new Error(`No snippet for kind: ${this.metadata.kind}`);return e(this)}},bo=class{static{l(this,"BackendPipeline")}constructor(e,t={}){this.target=e,this.matmulBackend=t.matmulBackend||"native",this.context=t.context||null}compile(e){let t=this.context&&this.context.getCodegenEntry(this.target.kind)||L1(this.target.kind);if(!t)throw new Error(`Unsupported target kind: ${this.target.kind}`);let{source:r,metadata:s}=t.compile(e,this.target,this);return new Ip(e.name,r,this.target,s)}compileAll(e){return e.map(t=>this.compile(t))}}});function Hm(n,e){$1.set(n,e)}function F1(n){return $1.get(n)||null}var $1,Ym=M(()=>{"use strict";$1=new Map;l(Hm,"registerMeasurer");l(F1,"getMeasurer")});var z1={};at(z1,{runWasmParallel:()=>Z$});import{Worker as K$}from"node:worker_threads";import H$ from"node:os";function J$(){if(!X0){let n=Math.max(1,Math.min(H$.cpus().length-1,16));X0=new eT(n)}return X0}async function Z$(n,e,t,r,s,o){let i=J$();i._ensureWorkers();let{binary:a,bufferOffsets:u}=n,p=[...u.values()],c=Math.min(p.length,t.length),{extent:f,outputIndices:d}=s,h=new Set(d||[]),m=Math.min(i.numWorkers,Math.max(1,f)),g=Math.ceil(f/m),y=[];for(let T=0;T<c;T++){let v=t[T],N=(v instanceof Float32Array?v.length:0)/f;y.push(N>=1&&Number.isInteger(N)?N:0)}let _=p.slice(0,c);if(r)for(let T of r)_.push(T);let b=new SharedArrayBuffer(4*m),w=a.buffer.slice(a.byteOffset,a.byteOffset+a.byteLength),x=[];for(let T=0;T<m;T++){let v=T*g,A=Math.min(v+g,f);if(v>=f)break;let N=[];for(let O=0;O<c;O++){let R=t[O];R instanceof Float32Array&&N.push({offset:p[O],length:R.length,data:R.buffer.slice(0),fullLength:R.length,elemStart:0})}let k=[..._,v,A];x.push(i.dispatch(T,{wasmBinary:w,mathImportNames:o,bufferEntries:N,callArgs:k,controlBuffer:b,workerIdx:T,outputIndices:d||[]}))}let S=await Promise.all(x);for(let T=0;T<c;T++){if(!h.has(T)&&h.size>0)continue;let v=t[T];if(!(v instanceof Float32Array))continue;let A=y[T];if(A>0)for(let N of S){let k=N.outputs[T];if(!k)continue;let R=N.workerIdx*g,B=Math.min(R+g,f),E=new Float32Array(k),F=R*A,C=B*A;v.set(E.subarray(F,C),F)}else{let N=S.find(k=>k.workerIdx===0);N&&N.outputs[T]&&v.set(new Float32Array(N.outputs[T]))}}}var Y$,X0,eT,V1=M(()=>{"use strict";Yi();Y$=`
|
|
13
|
+
const { parentPort } = require('node:worker_threads');
|
|
14
|
+
|
|
15
|
+
const LANCZOS_G = ${7};
|
|
16
|
+
const LANCZOS_COEFFS = ${JSON.stringify(oi)};
|
|
17
|
+
const ERF_A = ${JSON.stringify(qi)};
|
|
18
|
+
const ERF_P = ${Va};
|
|
19
|
+
const DIGAMMA_SHIFT = ${ii};
|
|
20
|
+
const DIGAMMA_SERIES = ${JSON.stringify(Ga)};
|
|
21
|
+
const erfScalar = ${Wi.toString()};
|
|
22
|
+
const erfcScalar = ${Ki.toString()};
|
|
23
|
+
const lgammaScalar = ${cn.toString()};
|
|
24
|
+
const gammaScalar = ${Hi.toString()};
|
|
25
|
+
const digammaScalar = ${ja.toString()};
|
|
26
|
+
|
|
27
|
+
const MATH = {
|
|
28
|
+
exp: Math.exp, log: Math.log, sin: Math.sin, cos: Math.cos,
|
|
29
|
+
tan: Math.tan, tanh: Math.tanh, pow: Math.pow,
|
|
30
|
+
fmod: (a, b) => a % b,
|
|
31
|
+
rsqrt: x => 1 / Math.sqrt(x),
|
|
32
|
+
sign: Math.sign, round: Math.round,
|
|
33
|
+
erf: erfScalar, erfc: erfcScalar, lgamma: lgammaScalar, gamma: gammaScalar, digamma: digammaScalar,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
parentPort.on('message', (msg) => {
|
|
37
|
+
const { wasmBinary, mathImportNames, bufferEntries,
|
|
38
|
+
callArgs, controlBuffer, workerIdx, outputIndices } = msg;
|
|
39
|
+
|
|
40
|
+
const compiled = new WebAssembly.Module(new Uint8Array(wasmBinary));
|
|
41
|
+
|
|
42
|
+
const mathImports = {};
|
|
43
|
+
for (const name of mathImportNames) {
|
|
44
|
+
mathImports[name] = MATH[name] || Math[name] || (x => x);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const instance = new WebAssembly.Instance(compiled, { math: mathImports });
|
|
48
|
+
const wasmMem = instance.exports.memory;
|
|
49
|
+
const fn = Object.values(instance.exports).find(v => typeof v === 'function');
|
|
50
|
+
|
|
51
|
+
for (const entry of bufferEntries) {
|
|
52
|
+
new Float32Array(wasmMem.buffer, entry.offset, entry.length).set(new Float32Array(entry.data));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
fn(...callArgs);
|
|
56
|
+
|
|
57
|
+
const outSet = new Set(outputIndices);
|
|
58
|
+
const outputs = [];
|
|
59
|
+
const transferable = [];
|
|
60
|
+
for (let i = 0; i < bufferEntries.length; i++) {
|
|
61
|
+
if (outSet.has(i)) {
|
|
62
|
+
const entry = bufferEntries[i];
|
|
63
|
+
const out = new Float32Array(entry.length);
|
|
64
|
+
out.set(new Float32Array(wasmMem.buffer, entry.offset, entry.length));
|
|
65
|
+
outputs.push(out.buffer);
|
|
66
|
+
transferable.push(out.buffer);
|
|
67
|
+
} else {
|
|
68
|
+
outputs.push(null);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const ctrl = new Int32Array(controlBuffer);
|
|
72
|
+
parentPort.postMessage({ workerIdx, outputs }, transferable);
|
|
73
|
+
Atomics.store(ctrl, workerIdx, 1);
|
|
74
|
+
Atomics.notify(ctrl, workerIdx);
|
|
75
|
+
});
|
|
76
|
+
`,X0=null;l(J$,"getPool");eT=class{static{l(this,"WasmThreadPool")}constructor(e){this.numWorkers=e,this._workers=[],this._ready=[]}_ensureWorkers(){if(!(this._workers.length>0))for(let e=0;e<this.numWorkers;e++){let t=new K$(Y$,{eval:!0});t.unref(),this._workers.push(t)}}dispatch(e,t){return this._ensureWorkers(),new Promise(r=>{let s=l(o=>{o.workerIdx===e&&(this._workers[e].removeListener("message",s),r(o))},"handler");this._workers[e].on("message",s),this._workers[e].postMessage(t)})}terminate(){for(let e of this._workers)e.terminate();this._workers=[]}};l(Z$,"runWasmParallel")});import{readdirSync as Lp,existsSync as qa}from"fs";import{join as ui,delimiter as G1}from"path";function q1(){let n=[];return process.env.CUDA_PATH&&n.push(process.env.CUDA_PATH),!Wa&&process.env.CUDA_HOME&&n.push(process.env.CUDA_HOME),n}function W1(){let n=[];if(Wa){let e="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA";if(qa(e))for(let t of Lp(e))n.push(ui(e,t))}else{let e="/usr/local";if(qa(e))for(let t of Lp(e))(t==="cuda"||t.startsWith("cuda-"))&&n.push(ui(e,t))}return n}function Q$(n){return Wa?[ui(n,"bin")]:[ui(n,"lib64"),ui(n,"targets/x86_64-linux/lib")]}function X$(){return Wa?[]:["/usr/lib/x86_64-linux-gnu","/lib/x86_64-linux-gnu"]}function U1(n){let e=n.match(/\d+/g);return e?e.map(Number):[]}function eF(n,e){let t=U1(n),r=U1(e),s=Math.max(t.length,r.length);for(let o=0;o<s;o++){let i=(t[o]||0)-(r[o]||0);if(i!==0)return i}return 0}function tF(n){let e=process.env[j1]||"";e.split(G1).includes(n)||(process.env[j1]=e?n+G1+e:n)}function bs(n){let{pattern:e,fallback:t,extraDirs:r}=Wa?n.win:n.linux,s=[];for(let o of[...q1(),...W1()])for(let i of Q$(o))s.push(i);for(let o of X$())s.push(o);if(r)for(let o of r())s.push(o);for(let o of s){if(!qa(o))continue;let i=Lp(o).filter(a=>e.test(a)&&!a.includes(".alt"));if(i.length>0)return tF(o),i.sort(eF),ui(o,i.pop())}return t}function nF(){for(let n of[...q1(),...W1()]){let e=ui(n,"include");if(qa(e))return e}return Wa?null:qa("/usr/local/cuda/include")?"/usr/local/cuda/include":null}function rF(){let n=[],e="C:/Program Files/NVIDIA/CUDNN";if(qa(e))for(let t of Lp(e).sort().reverse()){let r=ui(e,t,"bin");if(qa(r))for(let s of Lp(r).filter(o=>o.startsWith("12.")).sort().reverse())n.push(ui(r,s,"x64"))}return n}var Wa,j1,Jm,K1,H1,Zm,Y1,J1,Z1,vl=M(()=>{"use strict";Wa=process.platform==="win32",j1=Wa?"PATH":"LD_LIBRARY_PATH";l(q1,"envRoots");l(W1,"toolkitRoots");l(Q$,"libDirs");l(X$,"systemDirs");l(U1,"versionKey");l(eF,"compareVersion");l(tF,"prependSearchPath");l(bs,"loadCudaLib");l(nF,"resolveIncludeDir");Jm=nF(),K1={win:{pattern:/^nvcuda\.dll$/,fallback:"nvcuda.dll"},linux:{pattern:/^libcuda\.so(\.\d+)*$/,fallback:"libcuda.so.1"}},H1={win:{pattern:/^nvrtc64_\d+_\d+\.dll$/,fallback:"nvrtc64_120_0.dll"},linux:{pattern:/^libnvrtc\.so(\.\d+)*$/,fallback:"libnvrtc.so.12"}},Zm={win:{pattern:/^cudart64_\d+\.dll$/,fallback:"cudart64_12.dll"},linux:{pattern:/^libcudart\.so(\.\d+)*$/,fallback:"libcudart.so.12"}},Y1={win:{pattern:/^cublas64_\d+\.dll$/,fallback:"cublas64_12.dll"},linux:{pattern:/^libcublas\.so(\.\d+)*$/,fallback:"libcublas.so.12"}},J1={win:{pattern:/^cusolver64_\d+\.dll$/,fallback:"cusolver64_11.dll"},linux:{pattern:/^libcusolver\.so(\.\d+)*$/,fallback:"libcusolver.so.11"}};l(rF,"winCudnnDirs");Z1={win:{pattern:/^cudnn64_9\.dll$/,fallback:"cudnn64_9.dll",extraDirs:rF},linux:{pattern:/^libcudnn\.so(\.\d+)*$/,fallback:"libcudnn.so.9"}}});import Q1 from"koffi";function $e(n,e){if(e!==0)throw new Error("CUDA driver error "+e+" in "+n)}function tO(n){let e=[0n];li.getProgramLogSize(n,e);let t=Number(e[0]);if(t<=1)return"";let r=new Uint8Array(t);li.getProgramLog(n,r);let s="";for(let o of r){if(o===0)break;s+=String.fromCharCode(o)}return s}var gt,Ka,we,li,X1,eO,ws=M(()=>{"use strict";vl();gt=Q1.load(bs(K1)),Ka=Q1.load(bs(H1)),we={init:gt.func("int cuInit(uint)"),deviceGet:gt.func("int cuDeviceGet(_Out_ int *dev, int ordinal)"),deviceGetAttribute:gt.func("int cuDeviceGetAttribute(_Out_ int *pi, int attrib, int dev)"),primaryCtxRetain:gt.func("int cuDevicePrimaryCtxRetain(_Out_ void **pctx, int dev)"),primaryCtxRelease:gt.func("int cuDevicePrimaryCtxRelease_v2(int dev)"),ctxSynchronize:gt.func("int cuCtxSynchronize()"),moduleLoadData:gt.func("int cuModuleLoadData(_Out_ void **mod, void *image)"),moduleGetFunction:gt.func("int cuModuleGetFunction(_Out_ void **func, void *mod, str name)"),memGetInfo:gt.func("int cuMemGetInfo_v2(_Out_ uint64 *free, _Out_ uint64 *total)"),memAlloc:gt.func("int cuMemAlloc_v2(_Out_ uint64 *dptr, size_t bytes)"),memFree:gt.func("int cuMemFree_v2(uint64 dptr)"),memcpyHtoD:gt.func("int cuMemcpyHtoD_v2(uint64 dst, void *src, size_t n)"),memcpyDtoH:gt.func("int cuMemcpyDtoH_v2(void *dst, uint64 src, size_t n)"),memcpyDtoD:gt.func("int cuMemcpyDtoD_v2(uint64 dst, uint64 src, size_t n)"),memcpyHtoDAsync:gt.func("int cuMemcpyHtoDAsync_v2(uint64 dst, void *src, size_t n, void *stream)"),memcpyDtoHAsync:gt.func("int cuMemcpyDtoHAsync_v2(void *dst, uint64 src, size_t n, void *stream)"),memcpyDtoDAsync:gt.func("int cuMemcpyDtoDAsync_v2(uint64 dst, uint64 src, size_t n, void *stream)"),memsetD8:gt.func("int cuMemsetD8_v2(uint64 dst, uint8 uc, size_t n)"),memsetD8Async:gt.func("int cuMemsetD8Async(uint64 dst, uint8 uc, size_t n, void *stream)"),launchKernel:gt.func("int cuLaunchKernel(void *f, uint gx, uint gy, uint gz, uint bx, uint by, uint bz, uint shmem, void *stream, void **params, void **extra)"),streamCreate:gt.func("int cuStreamCreate(_Out_ void **stream, uint flags)"),streamSynchronize:gt.func("int cuStreamSynchronize(void *stream)"),ctxSetCurrent:gt.func("int cuCtxSetCurrent(void *ctx)"),streamBeginCapture:gt.func("int cuStreamBeginCapture_v2(void *stream, int mode)"),streamEndCapture:gt.func("int cuStreamEndCapture(void *stream, _Out_ void **graph)"),graphInstantiate:gt.func("int cuGraphInstantiateWithFlags(_Out_ void **exec, void *graph, uint64 flags)"),graphLaunch:gt.func("int cuGraphLaunch(void *exec, void *stream)"),graphExecDestroy:gt.func("int cuGraphExecDestroy(void *exec)"),graphDestroy:gt.func("int cuGraphDestroy(void *graph)")},li={createProgram:Ka.func("int nvrtcCreateProgram(_Out_ void **prog, str src, str name, int n, void *h, void *inc)"),compileProgram:Ka.func("int nvrtcCompileProgram(void *prog, int n, str *opts)"),destroyProgram:Ka.func("int nvrtcDestroyProgram(void **prog)"),getPTXSize:Ka.func("int nvrtcGetPTXSize(void *prog, _Out_ size_t *sz)"),getPTX:Ka.func("int nvrtcGetPTX(void *prog, _Out_ uint8_t *ptx)"),getProgramLogSize:Ka.func("int nvrtcGetProgramLogSize(void *prog, _Out_ size_t *sz)"),getProgramLog:Ka.func("int nvrtcGetProgramLog(void *prog, _Out_ uint8_t *log)")},X1=75,eO=76;l($e,"checkCU");l(tO,"readProgramLog")});function He(){if(kl)return $e("cuCtxSetCurrent",we.ctxSetCurrent(kl.ctx)),kl;$e("cuInit",we.init(0));let n=[0];$e("cuDeviceGet",we.deviceGet(n,0));let e=[0],t=[0];$e("cuDeviceGetAttribute",we.deviceGetAttribute(e,X1,n[0])),$e("cuDeviceGetAttribute",we.deviceGetAttribute(t,eO,n[0]));let r=[null];$e("cuDevicePrimaryCtxRetain",we.primaryCtxRetain(r,n[0])),$e("cuCtxSetCurrent",we.ctxSetCurrent(r[0]));let s=[null];$e("cuStreamCreate",we.streamCreate(s,0));let o=[0n],i=[0n];return $e("cuMemGetInfo",we.memGetInfo(o,i)),kl={dev:n[0],ctx:r[0],stream:s[0],arch:"sm_"+e[0]+t[0],totalMem:Number(i[0])},process.on("exit",()=>{try{we.primaryCtxRelease(kl.dev)}catch{}}),kl}var kl,Ts=M(()=>{"use strict";ws();kl=null;l(He,"getDevice")});function Qm(n){let e=2166136261;for(let t=0;t<n.length;t++)e^=n.charCodeAt(t),e=Math.imul(e,16777619);return(e>>>0).toString(16)}function Xm(n,e){let t=Qm(n),r=rO.get(t);if(r)return r;let{arch:s}=He(),o="";n.includes("__half")&&(o+=`#include <cuda_fp16.h>
|
|
77
|
+
`),n.includes("__nv_bfloat16")&&(o+=`#include <cuda_bf16.h>
|
|
78
|
+
`),/u?int(8|16|64)_t/.test(n)&&(o+=oF),/mma_sync|wmma::|fragment</.test(n)&&(o+=`#include <mma.h>
|
|
79
|
+
using namespace nvcuda::wmma;
|
|
80
|
+
`),n.includes("__pipeline_memcpy_async")&&(o+=`#include <cuda_pipeline.h>
|
|
81
|
+
`);let i=o+sF+`extern "C" {
|
|
82
|
+
`+n+`
|
|
83
|
+
}
|
|
84
|
+
`,a=[null];$e("nvrtcCreateProgram",li.createProgram(a,i,e+".cu",0,null,null));let u=["--gpu-architecture="+s];if(Jm&&u.push("--include-path="+Jm),li.compileProgram(a[0],u.length,u)!==0){let d=tO(a[0]);throw li.destroyProgram(a),new Error("NVRTC compile failed for kernel "+e+`:
|
|
85
|
+
`+d+`
|
|
86
|
+
--- source ---
|
|
87
|
+
`+n)}let c=[0n];$e("nvrtcGetPTXSize",li.getPTXSize(a[0],c));let f=new Uint8Array(Number(c[0]));return $e("nvrtcGetPTX",li.getPTX(a[0],f)),li.destroyProgram(a),rO.set(t,f),f}function tT(n){let e=sO.get(n);return e||(e=wo(n.source,n.name),sO.set(n,e),e)}function wo(n,e){let t=Qm(n)+":"+e,r=nO.get(t);if(r)return r;let s=Xm(n,e),o=[null];$e("cuModuleLoadData",we.moduleLoadData(o,s));let i=[null];$e("cuModuleGetFunction",we.moduleGetFunction(i,o[0],e));let a={func:i[0],module:o[0]};return nO.set(t,a),a}var nO,sF,oF,rO,sO,nT=M(()=>{"use strict";ws();Ts();nO=new Map,sF=`#ifndef INFINITY
|
|
88
|
+
#define INFINITY __int_as_float(0x7f800000)
|
|
89
|
+
#endif
|
|
90
|
+
#ifndef NAN
|
|
91
|
+
#define NAN __int_as_float(0x7fffffff)
|
|
92
|
+
#endif
|
|
93
|
+
`,oF=`typedef signed char int8_t;
|
|
94
|
+
typedef short int16_t;
|
|
95
|
+
typedef int int32_t;
|
|
96
|
+
typedef long long int64_t;
|
|
97
|
+
typedef unsigned char uint8_t;
|
|
98
|
+
typedef unsigned short uint16_t;
|
|
99
|
+
typedef unsigned int uint32_t;
|
|
100
|
+
typedef unsigned long long uint64_t;
|
|
101
|
+
`;l(Qm,"hashSource");rO=new Map;l(Xm,"compileToPTX");sO=new WeakMap;l(tT,"getProgramFor");l(wo,"getProgram")});var iO={};at(iO,{acquire:()=>ht,alloc:()=>oO,copyDeviceToHost:()=>qt,copyDeviceToHostAsync:()=>rT,copyHostToDevice:()=>xs,copyHostToDeviceAsync:()=>eg,drainPool:()=>tg,free:()=>ci,release:()=>_t});function oO(n){let e=[0n];return $e("cuMemAlloc",we.memAlloc(e,n)),e[0]}function xs(n,e){$e("cuMemcpyHtoD",we.memcpyHtoD(n,e,e.byteLength))}function qt(n,e){$e("cuMemcpyDtoH",we.memcpyDtoH(n,e,n.byteLength))}function eg(n,e){$e("cuMemcpyHtoDAsync",we.memcpyHtoDAsync(n,e,e.byteLength,He().stream))}function rT(n,e){$e("cuMemcpyDtoHAsync",we.memcpyDtoHAsync(n,e,n.byteLength,He().stream))}function ci(n){we.memFree(n)}function ht(n){let e=Cp.get(n);return e&&e.length>0?e.pop():oO(n)}function _t(n,e){let t=Cp.get(e);t||(t=[],Cp.set(e,t)),t.push(n)}function tg(){let n=0;for(let e of Cp.values())for(let t of e)ci(t),n++;return Cp.clear(),n}var Cp,pi=M(()=>{"use strict";ws();Ts();l(oO,"alloc");l(xs,"copyHostToDevice");l(qt,"copyDeviceToHost");l(eg,"copyHostToDeviceAsync");l(rT,"copyDeviceToHostAsync");l(ci,"free");Cp=new Map;l(ht,"acquire");l(_t,"release");l(tg,"drainPool")});function oT(n){aO=n}function In(){return aO}function ng(n){uO=n}function zn(){return uO}function Nl(n){lO=n}function cO(){return lO}function pO(n){sT=n}function iT(){sT&&sT()}var aO,sT,uO,lO,fi=M(()=>{"use strict";aO=!1,sT=null,uO=!1,lO=!1;l(oT,"setEagerDeferred");l(In,"isEagerDeferred");l(ng,"setEagerCapturing");l(zn,"isEagerCapturing");l(Nl,"setCudaGraphArmed");l(cO,"isCudaGraphArmed");l(pO,"setEagerFlushHook");l(iT,"eagerFlush")});function iF(n){let e=Buffer.alloc(8);return e.writeBigUInt64LE(BigInt(n)),e}function rr(n){return new rg("f32",n)}function aT(n){let e=Buffer.alloc(4);return n instanceof rg?(n.kind==="f32"?e.writeFloatLE(n.value):e.writeInt32LE(n.value|0),e):(Number.isInteger(n)?e.writeInt32LE(n|0):e.writeFloatLE(n),e)}function ns(n,e,t,r,s,o,i=!0){let{stream:a}=He(),u=[];for(let p of s)u.push(iF(p));for(let p of o)u.push(aT(p));$e("cuLaunchKernel",we.launchKernel(n,e[0],e[1],e[2],t[0],t[1],t[2],r,a,u,null)),i&&!zn()&&$e("cuStreamSynchronize",we.streamSynchronize(a))}var rg,uT=M(()=>{"use strict";ws();Ts();fi();l(iF,"devicePtrParam");rg=class{static{l(this,"TypedScalar")}constructor(e,t){this.kind=e,this.value=t}};l(rr,"f32");l(aT,"scalarParam");l(ns,"launch")});import dO from"koffi";function Qi(){aF(0)}function cT(){let n=lF();if(n!==0)throw new Error("cudaDeviceSynchronize failed: "+n)}function fF(n){let e=[null],t=uF(e,n);if(t!==0)throw new Error("cudaMalloc failed: "+t);return e[0]}function Pp(n,e){let t=hO(n,e,e.byteLength,cF);if(t!==0)throw new Error("cudaMemcpy H2D failed: "+t);fO&&fO(e.byteLength)}function sg(n,e){let t=hO(n,e,n.byteLength,pF);if(t!==0)throw new Error("cudaMemcpy D2H failed: "+t)}function pT(n){return typeof n=="bigint"?n:typeof n=="number"?BigInt(n):dO.address(n)}function og(n){let e=lT.get(n);return e&&e.length>0?e.pop():fF(n)}function fT(n,e){let t=lT.get(e);t||(t=[],lT.set(e,t)),t.push(n)}var Dp,aF,uF,hO,s9,lF,cF,pF,fO,lT,ig=M(()=>{"use strict";vl();Dp=dO.load(bs(Zm)),aF=Dp.func("int cudaSetDevice(int d)"),uF=Dp.func("int cudaMalloc(_Out_ void **p, size_t s)"),hO=Dp.func("int cudaMemcpy(void *dst, void *src, size_t n, int kind)"),s9=Dp.func("int cudaFree(void *p)"),lF=Dp.func("int cudaDeviceSynchronize()"),cF=1,pF=2,fO=null;l(Qi,"setDevice");l(cT,"devSync");l(fF,"devAlloc");l(Pp,"devH2D");l(sg,"devD2H");l(pT,"devAddr");lT=new Map;l(og,"acquireDevice");l(fT,"releaseDevice")});var gO={};at(gO,{CU_STREAM_CAPTURE_MODE_GLOBAL:()=>dF,CU_STREAM_CAPTURE_MODE_RELAXED:()=>hF,CU_STREAM_CAPTURE_MODE_THREAD_LOCAL:()=>mO,beginEagerCapture:()=>dT,destroyEagerGraph:()=>gT,endEagerCapture:()=>ag,isGraphCapturing:()=>mF,replay:()=>hT,syncStream:()=>mT});function mF(){return zn()}function dT(n=mO){let{stream:e}=He();ng(!0);try{$e("cuStreamBeginCapture",we.streamBeginCapture(e,n))}catch(t){throw ng(!1),t}}function ag(){let{stream:n}=He(),e=[null],t=we.streamEndCapture(n,e);ng(!1),$e("cuStreamEndCapture",t);let r=[null];return $e("cuGraphInstantiateWithFlags",we.graphInstantiate(r,e[0],0n)),{graph:e[0],exec:r[0]}}function hT(n){let{stream:e}=He();$e("cuGraphLaunch",we.graphLaunch(n,e))}function mT(){let{stream:n}=He();$e("cuStreamSynchronize",we.streamSynchronize(n))}function gT(n){n&&(n.exec&&we.graphExecDestroy(n.exec),n.graph&&we.graphDestroy(n.graph))}var dF,mO,hF,ug=M(()=>{"use strict";ws();Ts();fi();dF=0,mO=1,hF=2;l(mF,"isGraphCapturing");l(dT,"beginEagerCapture");l(ag,"endEagerCapture");l(hT,"replay");l(mT,"syncStream");l(gT,"destroyEagerGraph")});var wO={};at(wO,{cublasGemmBatchedDevice:()=>TT,cublasGemmDevice:()=>zp,cublasMatmul:()=>vF,cublasMatmulDevice:()=>mg});import _O from"koffi";function hg(){if(!$p){let n=[null],e=_F(n);if(e!==0)throw new Error("cublasCreate failed: "+e);$p=n[0],bF($p,He().stream),wF($p,3)}return $p}function _T(n){let e=wT.get(n);if(e&&e.length>0)return e.pop();let t=[null];if(gF(t,n))throw new Error("cudaMalloc failed");return t[0]}function bT(n,e){let t=wT.get(e);t||(t=[],wT.set(e,t)),t.push(n)}function mg(n,e,t,r,s,o,i=!1){pg(0);let a=hg(),c=bO(a,i?1:0,0,e,n,t,fg,s,i?t:e,r,t,dg,o,e);if(c!==0)throw new Error("cublasSgemm failed: "+c)}function zp(n,e,t,r,s,o,i,a){pg(0);let u=hg(),h=TF(u,i?cg:lg,s?cg:lg,e,n,t,fg,o,i?t:e,r,s?n:t,dg,a,e);if(h!==0)throw new Error("cublasSgemm failed: "+h)}function TT(n,e,t,r,s,o,i,a,u,p,c,f){pg(0);let d=hg(),h=i?cg:lg,m=p?cg:lg,g=i?e:r,_=xF(d,m,h,t,e,r,fg,a,p?r:t,BigInt(u),s,g,BigInt(o),dg,c,t,BigInt(f),n);if(_!==0)throw new Error("cublasSgemmStridedBatched failed: "+_)}function vF(n,e,t,r,s,o,i=!1){pg(0);let a=hg(),u=_T(r.byteLength),p=_T(s.byteLength),c=_T(o.byteLength);try{yT(u,r,r.byteLength,yO),yT(p,s,s.byteLength,yO);let h=bO(a,i?1:0,0,e,n,t,fg,p,i?t:e,u,t,dg,c,e);if(h!==0)throw new Error("cublasSgemm failed: "+h);yF(),yT(o,c,o.byteLength,SF)}finally{bT(u,r.byteLength),bT(p,s.byteLength),bT(c,o.byteLength)}}var Fp,Al,pg,gF,yT,h9,yF,_F,bF,wF,bO,TF,xF,yO,SF,lg,cg,fg,dg,$p,wT,Vp=M(()=>{"use strict";Ts();vl();Fp=_O.load(bs(Zm)),Al=_O.load(bs(Y1)),pg=Fp.func("int cudaSetDevice(int d)"),gF=Fp.func("int cudaMalloc(_Out_ void **p, size_t s)"),yT=Fp.func("int cudaMemcpy(void *dst, void *src, size_t n, int kind)"),h9=Fp.func("int cudaFree(void *p)"),yF=Fp.func("int cudaDeviceSynchronize()"),_F=Al.func("int cublasCreate_v2(_Out_ void **h)"),bF=Al.func("int cublasSetStream_v2(void *h, void *streamId)"),wF=Al.func("int cublasSetMathMode(void *h, int mode)"),bO=Al.func("int cublasSgemm_v2(void *h, int ta, int tb, int m, int n, int k, float *alpha, void *A, int lda, void *B, int ldb, float *beta, void *C, int ldc)"),TF=Al.func("int cublasSgemm_v2(void *h, int ta, int tb, int m, int n, int k, float *alpha, uint64 A, int lda, uint64 B, int ldb, float *beta, uint64 C, int ldc)"),xF=Al.func("int cublasSgemmStridedBatched(void *h, int ta, int tb, int m, int n, int k, float *alpha, uint64 A, int lda, int64 sa, uint64 B, int ldb, int64 sb, float *beta, uint64 C, int ldc, int64 sc, int batch)"),yO=1,SF=2,lg=0,cg=1,fg=new Float32Array([1]),dg=new Float32Array([0]),$p=null;l(hg,"handle");wT=new Map;l(_T,"acquire");l(bT,"release");l(mg,"cublasMatmulDevice");l(zp,"cublasGemmDevice");l(TT,"cublasGemmBatchedDevice");l(vF,"cublasMatmul")});function vO(n){xT=n}function kO(){return xT}function NO(n,e){let t=Qm(n)+":"+e,r=TO.get(t);if(r)return r;let s=Xm(n,e);Qi();let o=[null];$e("cuModuleLoadData",we.moduleLoadData(o,s));let i=[null];return $e("cuModuleGetFunction",we.moduleGetFunction(i,o[0],e)),TO.set(t,i[0]),i[0]}function kF(n){let e=Buffer.alloc(8);return e.writeBigUInt64LE(n),e}function AO(n,e,t,r,s,o,i=null){let a=[];for(let u of s)a.push(kF(u));for(let u of o)a.push(aT(u));$e("cuLaunchKernel",we.launchKernel(n,e[0],e[1],e[2],t[0],t[1],t[2],r,i,a,null))}function NF(n,e,t){let r=n.inputSlots.concat(n.outputSlots),s=n.kernel.metadata;if(s.cublas){let{M:o,N:i,K:a,aIdx:u,bIdx:p,cIdx:c,transB:f}=s.cublas;mg(o,i,a,e[r[u]],e[r[p]],e[r[c]],f)}else{let o=r.map(i=>pT(e[i]));AO(AF(n),s.gridDim,s.blockDim,0,o,n.shapeValues||[],t)}}function AF(n){return OO.get(n.name)}function OF(n,e,t){Qi();let r=He().stream,s=new Map;for(let u of t)u.kernel.metadata.cublas||s.set(u.name,NO(u.kernel.source,u.kernel.name));OO=s;let o=new Set;for(let u of t)for(let p of u.outputSlots)o.add(p);let i=new Set(n.argSlots),a=xO.get(n);if(a)for(let u=0;u<n.numSlots;u++)e[u]&&!o.has(u)&&Pp(a.dptr[u],e[u].data);else{let u=new Array(n.numSlots).fill(null);for(let p=0;p<n.numSlots;p++){let c=e[p];c&&(u[p]=og(Math.max(c.data.byteLength,1)),o.has(p)||Pp(u[p],c.data))}dT();try{for(let c of t)NF(c,u,r);let p=ag();a={dptr:u,graph:p.graph,exec:p.exec}}catch(p){try{ag()}catch{}throw p}xO.set(n,a)}hT(a.exec),mT();for(let u=0;u<n.numSlots;u++)a.dptr[u]!==null&&o.has(u)&&i.has(u)&&sg(e[u].data,a.dptr[u])}function BO(n,e,t){for(let r of n){let s=r.inputSlots.concat(r.outputSlots),o=r.kernel.metadata;if(o.cublas){let{M:i,N:a,K:u,aIdx:p,bIdx:c,cIdx:f,transB:d}=o.cublas;mg(i,a,u,e[s[p]],e[s[c]],e[s[f]],d)}else{let i=s.map(a=>pT(e[a]));AO(t.get(r.name),o.gridDim,o.blockDim,0,i,r.shapeValues||[])}}}function BF(n,e,t,r,s){let o=SO.get(n);o||(o={dptr:new Array(n.numSlots).fill(null),sizes:new Array(n.numSlots).fill(0),pin:new Map},SO.set(n,o));let{dptr:i,sizes:a,pin:u}=o,p=new Set(n.argSlots);for(let c=0;c<n.numSlots;c++){let f=e[c];if(!f)continue;let d=Math.max(f.data.byteLength,1);if((i[c]===null||a[c]!==d)&&(i[c]!==null&&fT(i[c],a[c]),i[c]=og(d),a[c]=d,u.delete(c)),s.has(c))continue;let h=f.resident;if(h){let m=u.get(c);if(m&&m.key===h.key&&m.version===h.version)continue;u.set(c,{key:h.key,version:h.version})}Pp(i[c],f.data)}BO(t,i,r),cT();for(let c=0;c<n.numSlots;c++)i[c]!==null&&s.has(c)&&p.has(c)&&sg(e[c].data,i[c])}async function RO(n,e,t,r){if(xT)return OF(n,e,t);for(let p of t)p.kernel.metadata.cublas||Xm(p.kernel.source,p.kernel.name);Qi();let s=new Map;for(let p of t)p.kernel.metadata.cublas||s.set(p.name,NO(p.kernel.source,p.kernel.name));let o=new Set;for(let p of t)for(let c of p.outputSlots)o.add(c);if(r&&r.resident){BF(n,e,t,s,o);return}let i=new Array(n.numSlots).fill(null),a=new Array(n.numSlots).fill(0);for(let p=0;p<n.numSlots;p++){let c=e[p];if(!c)continue;let f=Math.max(c.data.byteLength,1);a[p]=f,i[p]=og(f),o.has(p)||Pp(i[p],c.data)}BO(t,i,s),cT();let u=new Set(n.argSlots);for(let p=0;p<n.numSlots;p++)i[p]!==null&&o.has(p)&&u.has(p)&&sg(e[p].data,i[p]);for(let p=0;p<n.numSlots;p++)i[p]!==null&&fT(i[p],a[p])}var TO,xO,SO,xT,OO,MO=M(()=>{"use strict";ws();nT();ig();Ts();ug();Vp();uT();TO=new Map,xO=new WeakMap,SO=new WeakMap,xT=!1;l(vO,"setCudaGraphEnabled");l(kO,"isCudaGraphEnabled");l(NO,"loadFunctionOnPrimary");l(kF,"bufferParam");l(AO,"launchOnPrimary");l(NF,"_runStepGraphed");OO=null;l(AF,"funcsFor");l(OF,"runCudaPlanGraphed");l(BO,"_launchSteps");l(BF,"_runCudaPlanResident");l(RO,"runCudaPlan")});var EO={};at(EO,{clearCapturePins:()=>LF,deviceBufferDptr:()=>CF,deviceBufferForInplace:()=>gg,deviceBufferForInput:()=>To,deviceBufferForOutput:()=>Xi,downloadAndValidate:()=>vT,downloadStaticAsync:()=>PF,flushDeferred:()=>yg,hostReadHook:()=>BT,isEagerDeferred:()=>In,pinResident:()=>kT,releaseAllResident:()=>OT,setCudaGraphArmed:()=>Nl,setEagerDeferred:()=>oT,unpinResident:()=>EF,uploadIfStale:()=>Ha,uploadStaticAsync:()=>DF});function MF(n){let e=Ss.get(n);if(e)return Ss.delete(n),Ss.set(n,e),e;if(Ss.size>=RF){let t=Ss.keys().next().value,r=Ss.get(t);Ss.delete(t),_t(r.dptr,r.bytes)}return e={dptr:ht(n.byteLength),bytes:n.byteLength,valid:!1},Ss.set(n,e),e}function Ha(n){let e=MF(n);return e.valid||(xs(e.dptr,n),e.valid=!0),e.dptr}function vT(n,e){qt(n,e);let t=Ss.get(n);t&&(t.valid=!0)}function kT(n){jp.add(n)}function EF(n){jp.delete(n)}function LF(){Up.clear()}function CF(n){let e=vs.get(n);return e?e.dptr:null}function DF(n,e){eg(n,e)}function PF(n,e){rT(n,e)}function $F(){return ST||(ST=Math.floor(He().totalMem*IF)),ST}function NT(n,e){if(zn())throw new Error("illegal device sync during CUDA graph capture");$e("cuStreamSynchronize",we.streamSynchronize(He().stream)),qt(n,e)}function FF(n){let e=$F();for(let[t,r]of vs){if(Gp+n<=e)break;jp.has(t)||Up.has(t)||(r.hostStale&&NT(t,r.dptr),_t(r.dptr,r.bytes),Gp-=r.bytes,vs.delete(t))}}function AT(n){let e=vs.get(n);return e?(vs.delete(n),vs.set(n,e)):(zn()||FF(n.byteLength),e={dptr:ht(n.byteLength),bytes:n.byteLength,deviceFresh:!1,hostStale:!1},vs.set(n,e),Gp+=e.bytes),zn()&&Up.add(n),e}function IO(n,e){zn()?eg(n,e):xs(n,e)}function To(n){let e=AT(n);return e.deviceFresh||(IO(e.dptr,n),e.deviceFresh=!0),e.dptr}function Xi(n){let e=AT(n);return e.deviceFresh=!0,e.hostStale=!0,e.dptr}function gg(n){let e=AT(n);return e.deviceFresh||(IO(e.dptr,n),e.deviceFresh=!0),e.hostStale=!0,e.dptr}function yg(){for(let[n,e]of vs)jp.has(n)||Up.has(n)||(_t(e.dptr,e.bytes),Gp-=e.bytes,vs.delete(n))}function OT(){for(let[n,e]of vs)e.hostStale&&NT(n,e.dptr),_t(e.dptr,e.bytes);vs.clear(),Gp=0,jp.clear(),Up.clear();for(let n of Ss.values())_t(n.dptr,n.bytes);Ss.clear()}function BT(n){let e=vs.get(n);if(zn()){if(e&&e.hostStale)throw new Error("illegal host read of device-resident tensor during CUDA graph capture");return}let t=Ss.get(n);t&&(t.valid=!1),e&&(e.hostStale&&(NT(n,e.dptr),e.hostStale=!1),e.deviceFresh=!1)}var RF,Ss,IF,vs,jp,Up,Gp,ST,qp=M(()=>{"use strict";pi();Ts();ws();fi();RF=1024,Ss=new Map;l(MF,"_safeEntry");l(Ha,"uploadIfStale");l(vT,"downloadAndValidate");IF=.5,vs=new Map,jp=new Set,Up=new Set,Gp=0,ST=0;l(kT,"pinResident");l(EF,"unpinResident");l(LF,"clearCapturePins");l(CF,"deviceBufferDptr");l(DF,"uploadStaticAsync");l(PF,"downloadStaticAsync");l($F,"_cap");l(NT,"_syncDownload");l(FF,"_evict");l(AT,"_defEntry");l(IO,"_upload");l(To,"deviceBufferForInput");l(Xi,"deviceBufferForOutput");l(gg,"deviceBufferForInplace");l(yg,"flushDeferred");l(OT,"releaseAllResident");l(BT,"hostReadHook");pO(yg);oT(!0)});function St(n){return VF[n]||Float32Array}function _g(n,e){if(n===e)return n;let t=Ol.get(n)??0,r=Ol.get(e)??0;return t>=r?n:e}function st(n,e){let t=DO.get(n);t||(t=new Set,DO.set(n,t)),t.add(e)}function Ya(n){return zF(n)}var K,zF,VF,LO,CO,Ol,DO,rs=M(()=>{"use strict";Re();K=er,zF=fo,VF=Object.freeze({[K.F16]:Uint16Array,[K.BF16]:Uint16Array,[K.F32]:Float32Array,[K.F64]:Float64Array,[K.I8]:Int8Array,[K.I16]:Int16Array,[K.I32]:Int32Array,[K.I64]:BigInt64Array,[K.UI8]:Uint8Array,[K.BOOL]:Uint8Array,[K.INDEX]:Int32Array});l(St,"typedArrayCtor");LO=[K.F16,K.BF16,K.F32,K.F64],CO=[K.UI8,K.I8,K.I16,K.I32,K.I64],Ol=new Map;for(let n=0;n<LO.length;n++)Ol.set(LO[n],100+n);for(let n=0;n<CO.length;n++)Ol.set(CO[n],n);Ol.set(K.BOOL,-1);Ol.set(K.INDEX,50);l(_g,"resultDtype");DO=new Map;l(st,"_addCastPair");st(K.BOOL,K.I8);st(K.BOOL,K.I16);st(K.BOOL,K.I32);st(K.BOOL,K.I64);st(K.BOOL,K.F16);st(K.BOOL,K.F32);st(K.BOOL,K.F64);st(K.UI8,K.I16);st(K.UI8,K.I32);st(K.UI8,K.I64);st(K.UI8,K.F16);st(K.UI8,K.F32);st(K.UI8,K.F64);st(K.I8,K.I16);st(K.I8,K.I32);st(K.I8,K.I64);st(K.I8,K.F16);st(K.I8,K.F32);st(K.I8,K.F64);st(K.I16,K.I32);st(K.I16,K.I64);st(K.I16,K.F32);st(K.I16,K.F64);st(K.I32,K.I64);st(K.I32,K.F64);st(K.F16,K.F32);st(K.F16,K.F64);st(K.BF16,K.F32);st(K.BF16,K.F64);st(K.BOOL,K.BF16);st(K.UI8,K.BF16);st(K.I8,K.BF16);st(K.F32,K.F64);l(Ya,"dtypeSize")});function ea(){return PO}function GF(n){PO=n}var Ja,Ws,bg,RT,xo,Bl,PO,qn=M(()=>{"use strict";An();Ja=class{static{l(this,"Device")}type;index;constructor(e,t=0){this.type=e,this.index=t}dispatchKey(){return Uu(this.type)}equals(e){return this.type===e.type&&this.index===e.index}hash(){let e=2166136261;for(let t=0;t<this.type.length;t++)e=(e^this.type.charCodeAt(t))*16777619&2147483647;return e=(e^this.index)*16777619&2147483647,e}toString(){return this.index===0?this.type:`${this.type}:${this.index}`}},Ws=new Ja("cpu"),bg=new Ja("gpu"),RT=new Ja("wasm"),xo=new Ja("webgpu"),Bl=new Ja("meta"),PO=Ws;l(ea,"getDefaultDevice");l(GF,"setDefaultDevice")});function MT(n){return $O.get(n)||$O.get("cpu")}var wg,Tg,$O,Za,IT=M(()=>{"use strict";rs();qn();wg=class{static{l(this,"CPUAllocator")}allocate(e,t){let r=St(t);return new r(e)}free(e){}},Tg=class{static{l(this,"MetaAllocator")}allocate(){return null}free(e){}},$O=new Map([["cpu",new wg],["wasm",new wg],["meta",new Tg],["lazy",new Tg]]);l(MT,"getAllocator");Za=class n{static{l(this,"StorageImpl")}static#e=null;_data;_nbytes;_device;_allocator;_refCount;static setHostReadHook(e){n.#e=e}constructor(e,t,r,s){this._data=e,this._nbytes=t,this._device=r,this._allocator=s,this._refCount=1}static allocate(e,t,r){let s=MT(r.type),o=Ya(t),i=Math.max(Math.ceil(e/o),1),a=s.allocate(i,t);return new n(a,e,r,s)}static fromData(e,t){let r=e?e.byteLength:0,s=MT(t.type);return new n(e,r,t,s)}retain(){return this._refCount++,this}release(){this._refCount--,this._refCount===0&&(this._allocator&&this._data&&this._allocator.free(this._data),this._data=null)}get refCount(){return this._refCount}get data(){return n.#e&&this._data&&n.#e(this._data),this._data}get rawData(){return this._data}get nbytes(){return this._nbytes}get device(){return this._device}get isValid(){return this._refCount>0&&this._data!==null}get isMeta(){return this._data===null}resize(e,t){if(e<=this._nbytes&&this._data)return;let r=Ya(t),s=Math.max(Math.ceil(e/r),1),o=this._allocator.allocate(s,t);if(this._data&&o){let i=Math.min(this._data.length,o.length);for(let a=0;a<i;a++)o[a]=this._data[a]}this._allocator&&this._data&&this._allocator.free(this._data),this._data=o,this._nbytes=e}clone(){let e=MT(this._device.type),t=null;if(this._data){n.#e&&n.#e(this._data);let r=this._data.constructor;t=new r(this._data.length),t.set(this._data)}return new n(t,this._nbytes,this._device,e)}}});function zO(n){let e=FO.get(n)??null;return e||(e=Lt.findOp(n),e&&FO.set(n,e)),e||null}var FO,VO=M(()=>{"use strict";Ma();FO=new Map;l(zO,"getHandle")});function Ks(n,e){return n<0?e+n:n}function En(n){let e=n.length,t=new Array(e),r=1;for(let s=e-1;s>=0;s--)t[s]=r,r*=n[s];return t}function bn(n){let e=1;for(let t=0;t<n.length;t++)e*=n[t];return e}function ET(n,e){let t=n.length;if(t===0)return!0;let r=1;for(let s=t-1;s>=0;s--){let o=n[s];if(o===0)return!0;if(o!==1&&e[s]!==r)return!1;r*=o}return!0}function Rl(n,e){let t=n.length,r=e.length,s=Math.max(t,r),o=new Array(s);for(let i=0;i<s;i++){let a=i<t?n[t-1-i]:1,u=i<r?e[r-1-i]:1;if(a===u)o[s-1-i]=a;else if(a===1)o[s-1-i]=u;else if(u===1)o[s-1-i]=a;else return null}return o}function xg(n,e){let t=n.length,r=e.length;if(t===1&&r===1)return[];let s=t===1?[1,n[0]]:[...n],o=r===1?[e[0],1]:[...e],i=s.length,a=o.length,u=Rl(s.slice(0,i-2),o.slice(0,a-2));if(u===null)return null;let p=[...u,s[i-2],o[a-1]],c=new Set;return r===1&&c.add(p.length-1),t===1&&c.add(p.length-2),p.filter((f,d)=>!c.has(d))}function GO(n,e,t){let r=bn(n),s=-1,o=1,i=new Array(t.length);for(let a=0;a<t.length;a++){let u=t[a];if(u===-1){if(s!==-1)return null;s=a,i[a]=-1}else i[a]=u,o*=u}if(s!==-1){if(o===0||(i[s]=r/o|0,i[s]*o!==r))return null}else if(bn(i)!==r)return null;return ET(n,e)?{sizes:i,strides:En(i),needsCopy:!1}:{sizes:i,strides:En(i),needsCopy:!0}}var ss=M(()=>{"use strict";l(Ks,"normalizeAxis");l(En,"computeStrides");l(bn,"computeNumel");l(ET,"isContiguous");l(Rl,"broadcastShapes");l(xg,"matmulOutputShape");l(GO,"inferReshape")});function jO(n,e){if(n.length<4)return!1;let t=n.length,r=1,s=t===4?jF:UF;if(!s)return!1;for(let o=t-1;o>=0;o--){let i=s[o];if(i===void 0||n[i]===0||e[i]!==r)return!1;r*=n[i]}return!0}var jF,UF,LT=M(()=>{"use strict";jF=Object.freeze([0,2,3,1]),UF=Object.freeze([0,2,3,4,1]);l(jO,"isChannelsLast")});var Pt,di=M(()=>{"use strict";An();ss();LT();LT();Pt=class{static{l(this,"TensorImpl")}_storage;_storageOffset;_sizes;_strides;_dtype;_device;_numel;_keySet;_autogradMeta;_version;_contiguousCache;constructor(e,t,r,s,o,i){this._storage=e,this._storageOffset=t,this._sizes=Object.freeze([...r]),this._strides=Object.freeze(s?[...s]:En(r)),this._dtype=o,this._device=i,this._numel=bn(r),this._keySet=Jr.fromKey(Uu(i.type)),this._autogradMeta=null,this._version=0,this._contiguousCache=null}get storage(){return this._storage}get storageOffset(){return this._storageOffset}size(e){let t=e<0?this._sizes.length+e:e;return this._sizes[t]}stride(e){let t=e<0?this._strides.length+e:e;return this._strides[t]}sizes(){return this._sizes}strides(){return this._strides}dim(){return this._sizes.length}numel(){return this._numel}get dtype(){return this._dtype}get device(){return this._device}isContiguous(e){return!e||e==="contiguous"?(this._contiguousCache===null&&(this._contiguousCache=ET(this._sizes,this._strides)),this._contiguousCache):e==="channels_last"?jO(this._sizes,this._strides):!1}setSizesAndStrides(e,t){this._sizes=Object.freeze([...e]),this._strides=Object.freeze(t?[...t]:En(e)),this._numel=bn(e),this._contiguousCache=null}setStorageOffset(e){this._storageOffset=e}bumpVersion(){this._version++}get version(){return this._version}get autogradMeta(){return this._autogradMeta}setAutogradMeta(e){this._autogradMeta=e,this._updateKeySet()}keySet(){return this._keySet}addKeyToSet(e){this._keySet=this._keySet.add(e)}removeKeyFromSet(e){this._keySet=this._keySet.remove(e)}_updateKeySet(){let e=Jr.fromKey(Uu(this._device.type));if(this._autogradMeta&&this._autogradMeta.requiresGrad){let t=Uu(this._device.type);e=e.add(xN(t))}this._keySet=e}get isMeta(){return this._storage&&this._storage.isMeta}shallowCopyFrom(e){this._storage=e._storage,this._storageOffset=e._storageOffset,this._sizes=e._sizes,this._strides=e._strides,this._dtype=e._dtype,this._device=e._device,this._numel=e._numel,this._keySet=e._keySet,this._contiguousCache=null}}});var So,Wp=M(()=>{"use strict";So=class{static{l(this,"AutogradMeta")}_grad;_gradFn;_outputNr;_requiresGrad;_retainGrad;_gradAccumulator;_versionAtCreation;constructor(){this._grad=null,this._gradFn=null,this._outputNr=0,this._requiresGrad=!1,this._retainGrad=!1,this._gradAccumulator=null,this._versionAtCreation=0}get grad(){return this._grad}set grad(e){this._grad=e}get gradFn(){return this._gradFn}setGradFn(e,t){this._gradFn=e,this._outputNr=t??0}get outputNr(){return this._outputNr}get requiresGrad(){return this._requiresGrad}set requiresGrad(e){this._requiresGrad=e}get retainGrad(){return this._retainGrad}set retainGrad(e){this._retainGrad=e}get isLeaf(){return this._gradFn===null}get versionAtCreation(){return this._versionAtCreation}set versionAtCreation(e){this._versionAtCreation=e}getGradAccumulator(){if(this._gradAccumulator){let e=this._gradAccumulator instanceof WeakRef?this._gradAccumulator.deref():this._gradAccumulator;if(e)return e}return null}setGradAccumulator(e){this._gradAccumulator=typeof WeakRef<"u"?new WeakRef(e):e}clearGrad(){this._grad=null}clearGradFn(){this._gradFn=null,this._outputNr=0}}});function qO(n,e,t,r,s,o){let i=e[s];if(s===e.length-1){let u=new Array(i);if(o)for(let p=0;p<i;p++)u[p]=Pm(o,n[r+p*t[s]]);else for(let p=0;p<i;p++)u[p]=n[r+p*t[s]];return u}let a=new Array(i);for(let u=0;u<i;u++)a[u]=qO(n,e,t,r+u*t[s],s+1,o);return a}function qF(){if(!CT)throw new Error("Autograd engine not initialized. Import autograd/engine.js first.");return CT}function WO(n){CT=n}var UO,Ze,CT,sr=M(()=>{"use strict";di();Wp();bl();UO=new Set(["f16","bf16","i64"]),Ze=class n{static{l(this,"Tensor")}_impl;constructor(e){this._impl=e}get impl(){return this._impl}get shape(){return this._impl.sizes()}get strides(){return this._impl.strides()}get dtype(){return this._impl.dtype}get device(){return this._impl.device}get ndim(){return this._impl.dim()}get rank(){return this._impl.dim()}get numel(){return this._impl.numel()}get length(){return this._impl.numel()}get isContiguous(){return this._impl.isContiguous()}get dispatchKeySet(){return this._impl.keySet()}get storage(){return this._impl.storage}get storageOffset(){return this._impl.storageOffset}get data(){let e=this._impl.storage;if(!e||e.isMeta)return null;let t=e.data,r=this._impl.storageOffset;return t&&r===0&&this._impl.isContiguous()&&t.length===this.numel,t}get requiresGrad(){let e=this._impl.autogradMeta;return e?e.requiresGrad:!1}get gradFn(){let e=this._impl.autogradMeta;return e?e.gradFn:null}get grad(){let e=this._impl.autogradMeta;return e?e.grad:null}set grad(e){this._ensureAutogradMeta().grad=e}get isLeaf(){let e=this._impl.autogradMeta;return e?e.isLeaf:!0}get version(){return this._impl.version}requiresGrad_(e=!0){let t=this._ensureAutogradMeta();return t.requiresGrad=e,this._impl._updateKeySet(),this}retainGrad(){return this._ensureAutogradMeta().retainGrad=!0,this}detach(){let e=new Pt(this._impl.storage,this._impl.storageOffset,this._impl.sizes(),this._impl.strides(),this._impl.dtype,this._impl.device);return new n(e)}item(){if(this.numel!==1)throw new Error(`item() requires tensor with exactly 1 element, got ${this.numel}`);let t=this._impl.storage.data[this._impl.storageOffset];return UO.has(this._impl.dtype)?Pm(this._impl.dtype,t):t}toArray(){let e=this.shape,t=this.strides,r=this._impl.storage.data,s=this._impl.storageOffset,o=UO.has(this._impl.dtype)?this._impl.dtype:null;return e.length===0?o?Pm(o,r[s]):r[s]:qO(r,e,t,s,0,o)}toString(){return`Tensor(shape=[${this.shape.join(", ")}], dtype=${this.dtype}, device=${this.device})`}*[Symbol.iterator](){let e=this.shape[0];if(e===void 0)throw new Error("Cannot iterate over a 0-d tensor");for(let t=0;t<e;t++)yield this._select(0,t)}_select(e,t){let r=this._impl.sizes(),s=this._impl.strides(),o=[],i=[];for(let p=0;p<r.length;p++)p!==e&&(o.push(r[p]),i.push(s[p]));let a=this._impl.storageOffset+t*s[e],u=new Pt(this._impl.storage,a,o,i,this._impl.dtype,this._impl.device);return new n(u)}_ensureAutogradMeta(){if(!this._impl.autogradMeta){let e=new So;e.versionAtCreation=this._impl.version,this._impl.setAutogradMeta(e)}return this._impl.autogradMeta}backward(e){let{backward:t}=qF();t(this,e)}};l(qO,"_toNestedArray");CT=null;l(qF,"require_autograd_engine");l(WO,"setAutogradEngine")});var Wn,Xa=M(()=>{"use strict";IT();Wn=class n{static{l(this,"Storage")}_impl;constructor(e){this._impl=e}static allocate(e,t,r){return new n(Za.allocate(e,t,r))}static fromData(e,t){return new n(Za.fromData(e,t))}get impl(){return this._impl}get data(){return this._impl.data}get rawData(){return this._impl.rawData}get nbytes(){return this._impl.nbytes}get device(){return this._impl.device}get isValid(){return this._impl.isValid}get isMeta(){return this._impl.isMeta}retain(){return this._impl.retain(),this}release(){this._impl.release()}clone(){return new n(this._impl.clone())}resize(e,t){this._impl.resize(e,t)}}});function Wt(n,e){let t=e?.dtype??K.F32,r=e?.device??ea(),s=e?.requiresGrad??!1;if(ArrayBuffer.isView(n)&&"length"in n)return WF(n,e?.shape,t,r,s);if(Array.isArray(n)){let{flat:o,shape:i}=HF(n),a=e?.shape??i;return KF(o,a,t,r,s)}if(typeof n=="number")return KO(n,t,r,s);throw new Error("Unsupported data type for tensor()")}function eu(n,e,t,r){let s=r?.device??ea(),o=r?.requiresGrad??!1,i=En(e),a=Wn.fromData(n,s),u=r?.offset??0,p=new Pt(a,u,e,i,t,s),c=new Ze(p);return o&&c.requiresGrad_(!0),c}function Kp(n,e){let t=e?.dtype??K.F32,r=e?.device??ea();return KO(n,t,r,e?.requiresGrad??!1)}function KO(n,e,t,r){let s=St(e),o=new s(1),i=o;i[0]=DT.has(e)?Dm(e,n):n;let a=Wn.fromData(o,t),u=new Pt(a,0,[],[],e,t),p=new Ze(u);return r&&p.requiresGrad_(!0),p}function WF(n,e,t,r,s){let o=St(t),i=new o(n.length),a=i;if(DT.has(t))for(let h=0;h<n.length;h++)a[h]=Dm(t,n[h]);else for(let h=0;h<n.length;h++)a[h]=n[h];let u=e??[n.length],p=En(u),c=Wn.fromData(i,r),f=new Pt(c,0,u,p,t,r),d=new Ze(f);return s&&d.requiresGrad_(!0),d}function KF(n,e,t,r,s){let o=St(t),i=new o(n.length),a=i;if(DT.has(t))for(let d=0;d<n.length;d++)a[d]=Dm(t,n[d]);else for(let d=0;d<n.length;d++)a[d]=n[d];let u=En(e),p=Wn.fromData(i,r),c=new Pt(p,0,e,u,t,r),f=new Ze(c);return s&&f.requiresGrad_(!0),f}function HF(n){let e=[],t=n;for(;Array.isArray(t);)e.push(t.length),t=t[0];let r=[];return HO(n,r,e,0),{flat:r,shape:e}}function HO(n,e,t,r){if(r===t.length-1){for(let s=0;s<n.length;s++)e.push(n[s]);return}for(let s=0;s<n.length;s++)HO(n[s],e,t,r+1)}var DT,os=M(()=>{"use strict";sr();di();Xa();rs();qn();ss();bl();l(Wt,"tensor");l(eu,"fromBuffer");l(Kp,"scalar");DT=new Set(["f16","bf16","i64"]);l(KO,"_fromScalar");l(WF,"_fromTypedArray");l(KF,"_fromFlatArray");l(HF,"_flattenNested");l(HO,"_recurFlatten")});var Ht={};at(Ht,{_dispatch:()=>ct,abs:()=>Hp,add:()=>ae,argmax:()=>Cg,argmin:()=>iB,argsort:()=>gB,batch_norm:()=>$T,broadcast_in_dim:()=>cB,cat:()=>ru,ceil:()=>nB,chunk:()=>pB,clamp:()=>Mg,clone:()=>lB,contiguous:()=>ls,conv2d:()=>FT,cos:()=>ZO,cumsum:()=>hB,div:()=>Kt,dot:()=>uB,embedding:()=>QF,eq:()=>Yp,erf:()=>QO,erfc:()=>XO,exp:()=>as,expand:()=>Ll,fill:()=>ZF,flip:()=>dB,floor:()=>tB,gamma:()=>kg,gather:()=>Eg,ge:()=>Rg,gelu:()=>Ng,gt:()=>Il,index_select:()=>ra,layer_norm:()=>PT,le:()=>Bg,lgamma:()=>eB,log:()=>ko,log_softmax:()=>$g,lt:()=>sB,matmul:()=>tt,max:()=>No,maximum:()=>ta,mean:()=>Tr,min:()=>oB,minimum:()=>vg,mul:()=>ee,narrow:()=>ir,ne:()=>rB,neg:()=>is,one_hot:()=>Ig,pad:()=>tu,permute:()=>El,pool2d:()=>Fg,pow:()=>vo,prod:()=>aB,relu:()=>Ml,remainder:()=>JF,repeat:()=>Dl,reshape:()=>Hn,roll:()=>fB,rsqrt:()=>YO,scatter:()=>Lg,scatter_add:()=>nu,select:()=>en,sigmoid:()=>wr,sign:()=>Og,silu:()=>Ag,sin:()=>JO,slice:()=>Dg,softmax:()=>mi,sort:()=>mB,split:()=>hi,sqrt:()=>na,squeeze:()=>Cl,stack:()=>us,sub:()=>ze,sum:()=>wt,tanh:()=>Hs,tile:()=>Pg,topk:()=>yB,transpose:()=>vt,unsqueeze:()=>Lr,where:()=>or});function YF(n){return typeof n=="object"&&n!==null&&"_impl"in n}function Kn(n,e){return YF(n)?n:Kp(n,{dtype:e.dtype,device:e.device})}function ct(n,...e){let t=zO(n);if(!t)return Lt.callOp(n,...e);let r=u0(e,t.schema);return Lt.dispatch(t,r,...e)}function se(n,...e){return ct(n,...e)}function Sg(n,...e){return ct(n,...e)}function ae(n,e){return se("add",n,Kn(e,n))}function ze(n,e){return se("sub",n,Kn(e,n))}function ee(n,e){return se("mul",n,Kn(e,n))}function Kt(n,e){return se("div",n,Kn(e,n))}function is(n){return se("neg",n)}function vo(n,e){return se("pow",n,Kn(e,n))}function JF(n,e){return se("rem",n,Kn(e,n))}function ta(n,e){return se("maximum",n,Kn(e,n))}function vg(n,e){return se("minimum",n,Kn(e,n))}function as(n){return se("exp",n)}function ko(n){return se("log",n)}function na(n){return se("sqrt",n)}function YO(n){return se("rsqrt",n)}function Hp(n){return se("abs",n)}function JO(n){return se("sin",n)}function ZO(n){return se("cos",n)}function Hs(n){return se("tanh",n)}function QO(n){return se("erf",n)}function XO(n){return se("erfc",n)}function eB(n){return se("lgamma",n)}function kg(n){return se("gamma",n)}function wr(n){return se("sigmoid",n)}function Ml(n){return se("relu",n)}function Ng(n){return se("gelu",n)}function Ag(n){return se("silu",n)}function Og(n){return se("sign",n)}function tB(n){return se("floor",n)}function nB(n){return se("ceil",n)}function Yp(n,e){return se("eq",n,Kn(e,n))}function rB(n,e){return se("ne",n,Kn(e,n))}function sB(n,e){return se("lt",n,Kn(e,n))}function Bg(n,e){return se("le",n,Kn(e,n))}function Il(n,e){return se("gt",n,Kn(e,n))}function Rg(n,e){return se("ge",n,Kn(e,n))}function or(n,e,t){return se("where",n,e,t)}function Mg(n,e,t){return se("clamp",n,Kn(e,n),Kn(t,n))}function tu(n,e,t,r=0){return se("pad",n,Kn(r,n),e,t)}function Ig(n,e){return se("one_hot",n,e)}function ra(n,e,t){return se("index_select",n,t,e)}function Eg(n,e,t){return se("gather",n,t,e)}function nu(n,e,t,r){return se("scatter_add",n,t,r,e)}function Lg(n,e,t,r){return se("scatter",n,e,t,r)}function wt(n,e,t){return se("sum",n,e,t)}function Tr(n,e,t){return se("mean",n,e,t)}function No(n,e,t){return se("max",n,e,t)}function oB(n,e,t){return se("min",n,e,t)}function Cg(n,e,t){return se("argmax",n,e,t)}function iB(n,e,t){return se("argmin",n,e,t)}function aB(n,e,t){return se("prod",n,e,t)}function tt(n,e){let t=zT();if(t){let r=t(n,e);if(r!==null)return r}return se("matmul",n,e)}function uB(n,e){return se("dot",n,e)}function ru(n,e){return se("cat",n,e)}function us(n,e){return se("stack",n,e)}function lB(n){return se("clone",n)}function ZF(n,e){return se("fill",n,e)}function Hn(n,e){return se("reshape",n,e)}function vt(n,e,t){return se("transpose",n,e,t)}function El(n,e){return se("permute",n,e)}function cB(n,e,t){return se("broadcast_in_dim",n,e,t)}function Ll(n,e){return se("expand",n,e)}function Dg(n,e,t,r=null,s=1){return se("slice",n,e,t,r,s)}function Lr(n,e){return se("unsqueeze",n,e)}function Cl(n,e=null){return se("squeeze",n,e)}function ir(n,e,t,r){return se("narrow",n,e,t,r)}function en(n,e,t){return se("select",n,e,t)}function ls(n){return se("contiguous",n)}function Dl(n,e){return se("repeat",n,e)}function Pg(n,e){return se("tile",n,e)}function hi(n,e,t=0){if(typeof e!="number")return Sg("split",n,e,t);let r=n.shape.length,s=t<0?r+t:t,o=n.shape[s],i=[];for(let a=0;a<o;a+=e)i.push(Math.min(e,o-a));return Sg("split",n,i,t)}function pB(n,e,t=0){return Sg("chunk",n,e,t)}function fB(n,e,t=0){return se("roll",n,e,t)}function dB(n,e){return se("flip",n,Array.isArray(e)?e:[e])}function hB(n,e=0){return se("cumsum",n,e)}function mB(n,e=-1,t=!1){return se("sort",n,e,t)}function gB(n,e=-1,t=!1){return se("argsort",n,e,t)}function yB(n,e,t=-1,r=!0){return Sg("topk",n,e,t,r)}function mi(n,e){return se("softmax",n,e)}function $g(n,e){return se("log_softmax",n,e)}function PT(n,e,t,r,s){return se("layer_norm",n,e,t,r,s)}function $T(n,e,t,r,s,o,i){return se("batch_norm",n,e,t,r,s,o,i)}function FT(n,e,t,r,s,o){return se("conv2d",n,e,t,r,s,o)}function Fg(n,e,t,r,s){return se("pool2d",n,e,t,r,s)}function QF(n,e){return se("embedding",n,e)}var be=M(()=>{"use strict";Ma();VO();os();wn();l(YF,"hasTensorImpl");l(Kn,"_asTensor");l(ct,"_dispatch");l(se,"_dispatchTensor");l(Sg,"_dispatchTensorArray");l(ae,"add");l(ze,"sub");l(ee,"mul");l(Kt,"div");l(is,"neg");l(vo,"pow");l(JF,"remainder");l(ta,"maximum");l(vg,"minimum");l(as,"exp");l(ko,"log");l(na,"sqrt");l(YO,"rsqrt");l(Hp,"abs");l(JO,"sin");l(ZO,"cos");l(Hs,"tanh");l(QO,"erf");l(XO,"erfc");l(eB,"lgamma");l(kg,"gamma");l(wr,"sigmoid");l(Ml,"relu");l(Ng,"gelu");l(Ag,"silu");l(Og,"sign");l(tB,"floor");l(nB,"ceil");l(Yp,"eq");l(rB,"ne");l(sB,"lt");l(Bg,"le");l(Il,"gt");l(Rg,"ge");l(or,"where");l(Mg,"clamp");l(tu,"pad");l(Ig,"one_hot");l(ra,"index_select");l(Eg,"gather");l(nu,"scatter_add");l(Lg,"scatter");l(wt,"sum");l(Tr,"mean");l(No,"max");l(oB,"min");l(Cg,"argmax");l(iB,"argmin");l(aB,"prod");l(tt,"matmul");l(uB,"dot");l(ru,"cat");l(us,"stack");l(lB,"clone");l(ZF,"fill");l(Hn,"reshape");l(vt,"transpose");l(El,"permute");l(cB,"broadcast_in_dim");l(Ll,"expand");l(Dg,"slice");l(Lr,"unsqueeze");l(Cl,"squeeze");l(ir,"narrow");l(en,"select");l(ls,"contiguous");l(Dl,"repeat");l(Pg,"tile");l(hi,"split");l(pB,"chunk");l(fB,"roll");l(dB,"flip");l(hB,"cumsum");l(mB,"sort");l(gB,"argsort");l(yB,"topk");l(mi,"softmax");l($g,"log_softmax");l(PT,"layer_norm");l($T,"batch_norm");l(FT,"conv2d");l(Fg,"pool2d");l(QF,"embedding")});function _B(n,e,t,r){let s=n.length,o=t<0?s+t:t,i=r<0?s+r:r,a=[...n],u=[...e];return a[o]=n[i],a[i]=n[o],u[o]=e[i],u[i]=e[o],{sizes:a,strides:u}}function bB(n,e,t){let r=n.length,s=new Array(r),o=new Array(r);for(let i=0;i<r;i++){let a=t[i],u=a<0?r+a:a;s[i]=n[u],o[i]=e[u]}return{sizes:s,strides:o}}function wB(n,e,t){let r=n.length,s=t.length,o=s-r,i=new Array(s),a=new Array(s);for(let u=0;u<s;u++){let p=u-o;if(p<0)a[u]=t[u],i[u]=0;else if(n[p]===1&&t[u]!==1)a[u]=t[u],i[u]=0;else if(n[p]===t[u]||t[u]===-1)a[u]=n[p],i[u]=e[p];else throw new Error(`Cannot expand size ${n[p]} to ${t[u]} at dim ${u}`)}return{sizes:a,strides:i}}function TB(n,e,t,r,s,o){let i=n.length,a=t<0?i+t:t,u=n[a],p=r??0,c=s??u,f=o??1;p<0&&(p+=u),c<0&&(c+=u),p=Math.max(0,Math.min(p,u)),c=Math.max(0,Math.min(c,u));let d=Math.max(0,Math.ceil((c-p)/f)),h=p*e[a],m=[...n],g=[...e];return m[a]=d,g[a]=e[a]*f,{sizes:m,strides:g,offsetDelta:h}}function xB(n,e,t){let r=n.length,s=t<0?r+1+t:t,o=[...n],i=[...e],a=s<r?n[s]*e[s]:1;return o.splice(s,0,1),i.splice(s,0,a),{sizes:o,strides:i}}function SB(n,e,t){if(t!=null){let o=n.length,i=t<0?o+t:t;if(n[i]!==1)return{sizes:[...n],strides:[...e]};let a=[...n],u=[...e];return a.splice(i,1),u.splice(i,1),{sizes:a,strides:u}}let r=[],s=[];for(let o=0;o<n.length;o++)n[o]!==1&&(r.push(n[o]),s.push(e[o]));return{sizes:r,strides:s}}function vB(n,e,t,r,s){let o=n.length,i=t<0?o+t:t,a=[...n];a[i]=s;let u=r*e[i];return{sizes:a,strides:[...e],offsetDelta:u}}function kB(n,e,t,r){let s=n.length,o=t<0?s+t:t,i=r<0?n[o]+r:r,a=[],u=[];for(let c=0;c<s;c++)c!==o&&(a.push(n[c]),u.push(e[c]));let p=i*e[o];return{sizes:a,strides:u,offsetDelta:p}}var NB=M(()=>{"use strict";l(_B,"computeTranspose");l(bB,"computePermute");l(wB,"computeExpand");l(TB,"computeSlice");l(xB,"computeUnsqueeze");l(SB,"computeSqueeze");l(vB,"computeNarrow");l(kB,"computeSelect")});function Ys(n,e,t,r){let s=new Pt(n._impl.storage,n._impl.storageOffset+(r||0),e,t,n._impl.dtype,n._impl.device),o=n._impl.autogradMeta;return o&&s.setAutogradMeta(o),new Ze(s)}function XF(n,e){let t=GO(n.shape,n.strides,e);if(!t)throw new Error(`Cannot reshape tensor of shape [${n.shape}] to [${e}]`);if(!t.needsCopy)return Ys(n,t.sizes,t.strides,0);let r=n.isContiguous?n:OB(n);return Ys(r,t.sizes,t.strides,0)}function ez(n,e,t){let{sizes:r,strides:s}=_B(n.shape,n.strides,e,t);return Ys(n,r,s,0)}function tz(n,e){let{sizes:t,strides:r}=bB(n.shape,n.strides,e);return Ys(n,t,r,0)}function nz(n,e){let{sizes:t,strides:r}=wB(n.shape,n.strides,e);return Ys(n,t,r,0)}function rz(n,e,t){let r=n.shape,s=n.strides,o=[...e],i=new Array(e.length).fill(0);for(let a=0;a<t.length;a++){let u=t[a];i[u]=r[a]===1&&e[u]!==1?0:s[a]}return Ys(n,o,i,0)}function sz(n,e,t,r,s){let{sizes:o,strides:i,offsetDelta:a}=TB(n.shape,n.strides,e,t,r,s);return Ys(n,o,i,a)}function oz(n,e){let{sizes:t,strides:r}=xB(n.shape,n.strides,e);return Ys(n,t,r,0)}function iz(n,e){let{sizes:t,strides:r}=SB(n.shape,n.strides,e);return Ys(n,t,r,0)}function az(n,e,t,r){let{sizes:s,strides:o,offsetDelta:i}=vB(n.shape,n.strides,e,t,r);return Ys(n,s,o,i)}function uz(n,e,t){let{sizes:r,strides:s,offsetDelta:o}=kB(n.shape,n.strides,e,t);return Ys(n,r,s,o)}function AB(n){VT=n}function OB(n){let e=n._impl;return n.isContiguous&&e.storageOffset===0&&e.storage.rawData.length===n.numel?n:lz(n)}function lz(n){if(VT){let g=VT(n);if(g)return g}let e=n.shape,t=n.strides,r=n._impl.storage.data,s=n._impl.storageOffset,o=n.numel,i=En(e),a=n._impl.dtype,u=Wn.allocate(o*Ya(a),a,n._impl.device),p=u.data,c=e.length,f=new Int32Array(c),d=s;for(let g=0;g<o;g++){p[g]=r[d];for(let y=c-1;y>=0&&(f[y]++,d+=t[y],!(f[y]<e[y]));y--)d-=f[y]*t[y],f[y]=0}let h=new Pt(u,0,e,i,n._impl.dtype,n._impl.device),m=n._impl.autogradMeta;return m&&h.setAutogradMeta(m),new Ze(h)}var VT,BB,GT=M(()=>{"use strict";sr();di();Xa();rs();ss();NB();l(Ys,"makeView");l(XF,"reshapeKernel");l(ez,"transposeKernel");l(tz,"permuteKernel");l(nz,"expandKernel");l(rz,"broadcastInDimKernel");l(sz,"sliceKernel");l(oz,"unsqueezeKernel");l(iz,"squeezeKernel");l(az,"narrowKernel");l(uz,"selectKernel");VT=null;l(AB,"setGpuContiguousHook");l(OB,"contiguousKernel");BB=Object.freeze({reshape:l((n,e,t)=>XF(e,t),"reshape"),transpose:l((n,e,t,r)=>ez(e,t,r),"transpose"),permute:l((n,e,t)=>tz(e,t),"permute"),broadcast_in_dim:l((n,e,t,r)=>rz(e,t,r),"broadcast_in_dim"),expand:l((n,e,t)=>nz(e,t),"expand"),slice:l((n,e,t,r,s,o)=>sz(e,t,r,s,o),"slice"),unsqueeze:l((n,e,t)=>oz(e,t),"unsqueeze"),squeeze:l((n,e,t)=>iz(e,t),"squeeze"),narrow:l((n,e,t,r,s)=>az(e,t,r,s),"narrow"),select:l((n,e,t,r)=>uz(e,t,r),"select"),contiguous:l((n,e)=>OB(e),"contiguous")});l(lz,"copyContiguous")});var cz,zg,ve,Ao=M(()=>{"use strict";cz=0,zg=class{static{l(this,"Edge")}node;inputNr;constructor(e,t){this.node=e,this.inputNr=t}},ve=class{static{l(this,"AutogradNode")}_id;_numInputs;_nextEdges;_savedTensors;_inputMetadata;_opArgs;constructor(e){this._id=cz++,this._numInputs=e||0,this._nextEdges=[],this._savedTensors=[],this._inputMetadata=[],this._opArgs=null}setOpArgs(e){this._opArgs=e}opArgs(){return this._opArgs}get id(){return this._id}get numInputs(){return this._numInputs}get nextEdges(){return this._nextEdges}addNextEdge(e,t){this._nextEdges.push(new zg(e,t))}setNextEdge(e,t,r){for(;this._nextEdges.length<=e;)this._nextEdges.push(null);this._nextEdges[e]=new zg(t,r)}saveTensor(e){this._savedTensors.push(e)}savedTensors(){return this._savedTensors}saveInputMetadata(e,t,r){this._inputMetadata[e]={shape:t,dtype:r}}inputMetadata(e){return this._inputMetadata[e]||null}apply(e){throw new Error(`${this.name()}.apply() not implemented`)}name(){return this.constructor.name}releaseVariables(){this._savedTensors=[],this._inputMetadata=[]}}});var EB={};at(EB,{arange:()=>RB,empty:()=>Tn,eye:()=>jg,full:()=>ot,linspace:()=>IB,ones:()=>tn,randn:()=>Gg,randperm:()=>MB,zeros:()=>Ve});function Vg(n){return{dtype:n?.dtype??K.F32,device:n?.device??ea(),requiresGrad:n?.requiresGrad??!1}}function Jp(n,e,t,r){let s=En(n),i=bn(n)*Ya(e),a=Wn.allocate(i,e,t),u=new Pt(a,0,n,s,e,t),p=new Ze(u);return r&&p.requiresGrad_(!0),p}function Tn(n,e){let{dtype:t,device:r,requiresGrad:s}=Vg(e);return Jp(n,t,r,s)}function jT(n,e,t){let r=Tn(n,e),s=r.data;if(s)return s.fill(t),r;if(r.device&&r.device.type==="meta"){let o=Tn(n,{dtype:e?.dtype});return o.data.fill(t),o}return r}function Ve(n,e){return jT(n,e,0)}function tn(n,e){return jT(n,e,1)}function ot(n,e,t){return jT(n,t,e)}function Gg(n,e){let t=Tn(n,e),r=t.data;if(r){let s=r.length;for(let o=0;o<s;o+=2){let i=Math.random()||1e-10,a=Math.random(),u=Math.sqrt(-2*Math.log(i)),p=6.283185307179586*a;r[o]=u*Math.cos(p),o+1<s&&(r[o+1]=u*Math.sin(p))}}return t}function RB(n,e,t,r){let s,o,i;e===void 0&&t===void 0?(s=0,o=n,i=1):t===void 0?(s=n,o=e,i=1):(s=n,o=e,i=t);let a=Math.max(0,Math.ceil((o-s)/i)),{dtype:u,device:p,requiresGrad:c}=Vg(r),f=Jp([a],u,p,c),d=f.data;if(d)for(let h=0;h<a;h++)d[h]=s+h*i;return f}function jg(n,e,t){let r=e??n,{dtype:s,device:o,requiresGrad:i}=Vg(t),a=Jp([n,r],s,o,i),u=a.data;if(u){u.fill(0);let p=Math.min(n,r);for(let c=0;c<p;c++)u[c*r+c]=1}return a}function MB(n,e){let t=e?.dtype??K.I32,r=e?.device??ea(),s=Jp([n],t,r,e?.requiresGrad??!1),o=s.data;for(let i=0;i<n;i++)o[i]=i;for(let i=n-1;i>0;i--){let a=Math.floor(Math.random()*(i+1)),u=o[i];o[i]=o[a],o[a]=u}return s}function IB(n,e,t,r){let{dtype:s,device:o,requiresGrad:i}=Vg(r),a=Jp([t],s,o,i),u=a.data;if(u&&t>0)if(t===1)u[0]=n;else{let p=(e-n)/(t-1);for(let c=0;c<t;c++)u[c]=n+c*p}return a}var kt=M(()=>{"use strict";sr();di();Xa();rs();qn();ss();l(Vg,"_defaultOpts");l(Jp,"_makeTensor");l(Tn,"empty");l(jT,"_filled");l(Ve,"zeros");l(tn,"ones");l(ot,"full");l(Gg,"randn");l(RB,"arange");l(jg,"eye");l(MB,"randperm");l(IB,"linspace")});function pz(n,e,t){n[e]+=t}var Ug,qg,Wg,Kg,Pl,Hg,UT=M(()=>{"use strict";Ao();be();kt();be();ss();l(pz,"_addAt");Ug=class extends ve{static{l(this,"CatBackward")}constructor(){super(0)}apply(e){let t=e[0],r=this.opArgs(),s=t.shape.length,o=Ks(r&&r.length>1?r[1]??0:0,s),i=[],a=0,u=0;for(;this.inputMetadata(u);){let c=this.inputMetadata(u).shape[o];i.push(ls(ir(t,o,a,c))),a+=c,u++}return i}},qg=class extends ve{static{l(this,"StackBackward")}constructor(){super(0)}apply(e){let t=e[0],r=this.opArgs(),s=t.shape.length,o=Ks(r&&r.length>1?r[1]??0:0,s),i=[],a=0;for(;this.inputMetadata(a);)i.push(ls(en(t,o,a))),a++;return i}},Wg=class extends ve{static{l(this,"ClampBackward")}constructor(){super(3)}apply(e){let t=e[0],[r,s,o]=this.savedTensors(),i=Ve(t.shape,{dtype:t.dtype,device:t.device}),a=Rg(r.detach(),s.detach()),u=or(a,t,i),p=Bg(r.detach(),o.detach());return[or(p,u,i),null,null]}},Kg=class extends ve{static{l(this,"PadBackward")}constructor(){super(2)}apply(e){let t=e[0],s=this.opArgs()[2],i=this.inputMetadata(0).shape,a=t;for(let u=0;u<i.length;u++){let p=s[u]||0;a=ir(a,u,p,i[u])}return[ls(a),null]}},Pl=class n extends ve{static{l(this,"IndexSelectBackward")}static#e=null;static setGpuBackward(e){n.#e=e}constructor(){super(2)}apply(e){let t=e[0],[,r]=this.savedTensors(),o=this.inputMetadata(0).shape,i=o.length,a=this.opArgs(),u=Ks(a&&a.length>2?a[2]??0:0,i);if(n.#e){let v=n.#e(t,r,o,u);if(v)return[v]}let p=Ve(o,{dtype:t.dtype,device:t.device}),c=p._impl.storage.data,f=p.strides,d=ls(t),h=d._impl.storage.data,m=d._impl.storageOffset,g=d.shape,y=d.strides,_=ls(r),b=_._impl.storage.data,w=_._impl.storageOffset,x=g.length,S=new Int32Array(x),T=m;for(let v=0;v<d.numel;v++){let A=0;for(let N=0;N<x;N++){let k=N===u?Number(b[w+S[N]]):S[N];A+=k*f[N]}pz(c,A,h[T]);for(let N=x-1;N>=0;N--){if(S[N]++,S[N]<g[N]){T+=y[N];break}T-=(g[N]-1)*y[N],S[N]=0}}return[p,null]}},Hg=class extends ve{static{l(this,"WhereBackward")}constructor(){super(3)}apply(e){let t=e[0],[r]=this.savedTensors(),s=Ve(t.shape,{dtype:t.dtype,device:t.device}),o=or(r.detach(),t,s),i=or(r.detach(),s,t);return[null,o,i]}}});import fz from"koffi";function Yg(n,e,t){zn()?we.memcpyDtoDAsync(n,e,t,He().stream):we.memcpyDtoD(n,e,t)}function dz(n,e){zn()?we.memsetD8Async(n,0,e,He().stream):we.memsetD8(n,0,e)}function DB(){if(Zp!==null)return Zp;try{let n=fz.load(bs(Z1));pn={create:n.func("int cudnnCreate(_Out_ void **h)"),setStream:n.func("int cudnnSetStream(void *h, void *stream)"),getErrorString:n.func("str cudnnGetErrorString(int status)"),createRNNDesc:n.func("int cudnnCreateRNNDescriptor(_Out_ void **d)"),setRNNDesc:n.func("int cudnnSetRNNDescriptor_v8(void *d, int algo, int cellMode, int biasMode, int dirMode, int inputMode, int dataType, int mathPrec, int mathType, int inputSize, int hiddenSize, int projSize, int numLayers, void *dropoutDesc, uint auxFlags)"),createRNNDataDesc:n.func("int cudnnCreateRNNDataDescriptor(_Out_ void **d)"),setRNNDataDesc:n.func("int cudnnSetRNNDataDescriptor(void *d, int dataType, int layout, int maxSeqLength, int batchSize, int vectorSize, void *seqLengthArray, void *paddingFill)"),createTensorDesc:n.func("int cudnnCreateTensorDescriptor(_Out_ void **d)"),setTensorNdDesc:n.func("int cudnnSetTensorNdDescriptor(void *d, int dataType, int nbDims, void *dimA, void *strideA)"),createDropoutDesc:n.func("int cudnnCreateDropoutDescriptor(_Out_ void **d)"),setDropoutDesc:n.func("int cudnnSetDropoutDescriptor(void *d, void *h, float dropout, uint64 states, size_t stateSize, uint64 seed)"),dropoutGetStatesSize:n.func("int cudnnDropoutGetStatesSize(void *h, _Out_ size_t *sz)"),getRNNWeightSpaceSize:n.func("int cudnnGetRNNWeightSpaceSize(void *h, void *d, _Out_ size_t *sz)"),getRNNTempSpaceSizes:n.func("int cudnnGetRNNTempSpaceSizes(void *h, void *d, int fwdMode, void *xDesc, _Out_ size_t *work, _Out_ size_t *reserve)"),getRNNWeightParams:n.func("int cudnnGetRNNWeightParams(void *h, void *d, int pseudoLayer, size_t wss, uint64 weightSpace, int linLayerID, void *mDesc, _Out_ uint64 *mAddr, void *bDesc, _Out_ uint64 *bAddr)"),rnnForward:n.func("int cudnnRNNForward(void *h, void *d, int fwdMode, uint64 devSeqLengths, void *xDesc, uint64 x, void *yDesc, uint64 y, void *hDesc, uint64 hx, uint64 hy, void *cDesc, uint64 cx, uint64 cy, size_t wss, uint64 weightSpace, size_t workSize, uint64 workSpace, size_t reserveSize, uint64 reserveSpace)"),rnnBackwardData:n.func("int cudnnRNNBackwardData_v8(void *h, void *d, uint64 devSeqLengths, void *yDesc, uint64 y, uint64 dy, void *xDesc, uint64 dx, void *hDesc, uint64 hx, uint64 dhy, uint64 dhx, void *cDesc, uint64 cx, uint64 dcy, uint64 dcx, size_t wss, uint64 weightSpace, size_t workSize, uint64 workSpace, size_t reserveSize, uint64 reserveSpace)"),rnnBackwardWeights:n.func("int cudnnRNNBackwardWeights_v8(void *h, void *d, int addGrad, uint64 devSeqLengths, void *xDesc, uint64 x, void *hDesc, uint64 hx, void *yDesc, uint64 y, size_t wss, uint64 dweightSpace, size_t workSize, uint64 workSpace, size_t reserveSize, uint64 reserveSpace)")},Zp=!0}catch{Zp=!1}return Zp}function PB(){return DB()}function Ln(n,e){if(e!==0)throw new Error("cuDNN "+n+" failed: "+e+" ("+pn.getErrorString(e)+")")}function Qp(){if(!DB())throw new Error("cuDNN not available");let n=He();if(Qi(),!Jg){let e=su();Ln("create",pn.create(e)),Jg=e[0],Ln("setStream",pn.setStream(Jg,n.stream))}return Jg}function Sz(){if(!qT){let n=Qp(),e=su();Ln("createDropout",pn.createDropoutDesc(e));let t=[0n];Ln("dropStatesSize",pn.dropoutGetStatesSize(n,t));let r=ht(Math.max(Number(t[0]),1));Ln("setDropout",pn.setDropoutDesc(e[0],n,0,r,t[0],0n)),qT=e[0]}return qT}function vz(n,e){let t=su();return Ln("createTensor",pn.createTensorDesc(t)),Ln("setTensorNd",pn.setTensorNdDesc(t[0],Zg,n.length,new Int32Array(n),new Int32Array(e))),t[0]}function LB(n,e,t,r){let s=su();return Ln("createRNNData",pn.createRNNDataDesc(s)),Ln("setRNNData",pn.setRNNDataDesc(s[0],Zg,wz,n,e,t,r,null)),s[0]}function kz({inputSize:n,hiddenSize:e,seqLen:t,batch:r,numLayers:s,cellMode:o=$B,gates:i=4}){let a=`${o}_${n}_${e}_${t}_${r}_${s}`,u=CB.get(a);if(u)return u;let p=Qp(),c=su();Ln("createRNN",pn.createRNNDesc(c)),Ln("setRNN",pn.setRNNDesc(c[0],hz,o,gz,yz,_z,Zg,Zg,bz,n,e,e,s,Sz(),0));let f=c[0],d=new Int32Array(r).fill(t),h=LB(t,r,n,d),m=LB(t,r,e,d),g=vz([s,r,e],[r*e,e,1]),y=[0n];Ln("wss",pn.getRNNWeightSpaceSize(p,f,y));let _=[0n],b=[0n];Ln("temp",pn.getRNNTempSpaceSizes(p,f,VB,h,_,b));let w=ht(r*4);return we.memcpyHtoD(w,d,r*4),u={rd:f,xDesc:h,yDesc:m,hDesc:g,devSeq:w,wss:y[0],wssN:Number(y[0]),workSize:_[0],workN:Math.max(Number(_[0]),1),reserveSize:b[0],reserveN:Math.max(Number(b[0]),1),inputSize:n,hiddenSize:e,seqLen:t,batch:r,numLayers:s,gates:i},CB.set(a,u),u}function GB(n,e,t,r){let s=Qp(),o=su(),i=su();Ln("cwm",pn.createTensorDesc(o)),Ln("cwb",pn.createTensorDesc(i));let{numLayers:a,hiddenSize:u,inputSize:p,wss:c,gates:f}=n;for(let d=0;d<a;d++){let h=d===0?p:u,m=t[d];for(let g=0;g<2*f;g++){let y=g<f,_=g%f,b=[0n],w=[0n];Ln("wparam",pn.getRNNWeightParams(s,n.rd,d,c,e,g,o[0],b,i[0],w));let S=(y?u*h:u*u)*4,T=u*4,v=BigInt(y?m.x2hW:m.h2hW)+BigInt(_*S),A=BigInt(y?m.x2hB:m.h2hB)+BigInt(_*T);r?(Yg(v,b[0],S),Yg(A,w[0],T)):(Yg(b[0],v,S),Yg(w[0],A,T))}}}function jB(n,e,t,r,s,o,i,a,u=!0){let p=Qp(),c=kz({...t,numLayers:e.length}),f=ht(c.wssN);GB(c,f,e,!1);let d=ht(c.workN),h=u?ht(c.reserveN):0n;return Ln("forward",pn.rnnForward(p,c.rd,u?VB:Tz,c.devSeq,c.xDesc,n,c.yDesc,o,c.hDesc,r||0n,i,c.hDesc,s||0n,a,c.wss,f,c.workSize,d,u?c.reserveSize:0n,h)),{p:c,weightSpace:f,workSpace:d,reserveSpace:h,training:u}}function UB(n){n&&(_t(n.weightSpace,n.p.wssN),_t(n.workSpace,n.p.workN),n.reserveSpace&&n.reserveSpace!==0n&&_t(n.reserveSpace,n.p.reserveN))}function qB(n,e,t,r,s,o,i,a,u,p,c,f){let{p:d,weightSpace:h,workSpace:m,reserveSpace:g}=n,y=Qp(),_=ht(d.wssN);dz(_,d.wssN),Ln("backwardData",pn.rnnBackwardData(y,d.rd,d.devSeq,d.yDesc,t,o,d.xDesc,u,d.hDesc,r||0n,i||0n,p||0n,d.hDesc,s||0n,a||0n,c||0n,d.wss,h,d.workSize,m,d.reserveSize,g)),Ln("backwardWeights",pn.rnnBackwardWeights(y,d.rd,xz,d.devSeq,d.xDesc,e,d.hDesc,r||0n,d.yDesc,t,d.wss,_,d.workSize,m,d.reserveSize,g)),GB(d,_,f,!0),_t(h,d.wssN),_t(m,d.workN),_t(g,d.reserveN),_t(_,d.wssN)}var pn,Zp,Zg,hz,$B,mz,gz,yz,_z,bz,FB,zB,wz,VB,Tz,xz,su,Jg,qT,CB,WT=M(()=>{"use strict";ws();Ts();ig();pi();vl();fi();l(Yg,"d2d");l(dz,"memZero");Zp=null;l(DB,"ensure");l(PB,"cudnnAvailable");Zg=0,hz=0,$B=2,mz=3,gz=2,yz=0,_z=0,bz=2,FB=$B,zB=mz,wz=0,VB=1,Tz=0,xz=0;l(Ln,"ck");su=l(()=>[null],"o2"),Jg=null,qT=null;l(Qp,"handle");l(Sz,"dropoutDesc");l(vz,"tensorDesc");l(LB,"dataDesc");CB=new Map;l(kz,"plan");l(GB,"packWeights");l(jB,"cudnnRNNForward");l(UB,"releaseRNNForward");l(qB,"cudnnRNNBackward")});var HB={};at(HB,{GradMode:()=>Oo,enableGrad:()=>KB,noGrad:()=>WB});function WB(n){let e=sa;sa=!1;try{return n()}finally{sa=e}}function KB(n){let e=sa;sa=!0;try{return n()}finally{sa=e}}var sa,Oo,ou=M(()=>{"use strict";sa=!0,Oo={isEnabled(){return sa},setEnabled(n){sa=n}};l(WB,"noGrad");l(KB,"enableGrad")});function Qg(n,e){for(let t=0;t<e.length;t++){n.saveInputMetadata(t,[...e[t].shape],e[t].dtype);let r=e[t]._impl.autogradMeta;if(r&&r.requiresGrad)if(r.gradFn)n.setNextEdge(t,r.gradFn,r.outputNr||0);else{let s=r.getGradAccumulator();s||(s=new Xp(e[t]),r.setGradAccumulator(s)),n.setNextEdge(t,s,0)}else n.setNextEdge(t,null,0)}}var Xp,Xg=M(()=>{"use strict";Ao();be();Xp=class extends ve{static{l(this,"GradAccumulator")}_variable;constructor(e){super(0),this._variable=typeof WeakRef<"u"?new WeakRef(e):{deref:l(()=>e,"deref")}}apply(e){let t=e[0],r=this._variable.deref();if(!r)return[];let s=r._impl.autogradMeta;return s?(s.grad===null?s.grad=t:s.grad=ae(s.grad,t),[]):[]}name(){return"GradAccumulator"}};l(Qg,"wireInputEdges")});function Cr(n){return In()?To(n):Ha(n)}function Ro(n,e){if(In())return Xi(n);let t=ht(n.byteLength);return e.push([n,t]),t}function YB(n){for(let[e,t]of n)qt(e,t),_t(t,e.byteLength)}function KT(n,e,t){let r=new So;r.setGradFn(n,t),r.requiresGrad=!0,e._impl.setAutogradMeta(r),e._impl._updateKeySet()}function JB(n,e,t,r,s=null,o=null){let i=Nz[n],{inputSize:a,hiddenSize:u,seqLen:p,batch:c}=r,f=t.length,d=[],h={...r,numLayers:f,cellMode:i.cellMode,gates:i.gates},m=Bo(e),g=Cr(m),y=t.map(D=>({x2hW:Cr(Bo(D.x2h.weight)),x2hB:Cr(Bo(D.x2h.bias)),h2hW:Cr(Bo(D.h2h.weight)),h2hB:Cr(Bo(D.h2h.bias))})),_=s?Bo(s):null,b=i.hasCell&&o?Bo(o):null,w=s?Cr(_):0n,x=b?Cr(b):0n,S=f*c*u,T=[f,c,u],v=new Float32Array(p*c*u),A=new Float32Array(S),N=i.hasCell?new Float32Array(S):null,k=Ro(v,d),O=Ro(A,d),R=N?Ro(N,d):0n,B=[e];for(let D of t)B.push(D.x2h.weight,D.x2h.bias,D.h2h.weight,D.h2h.bias);s&&(B.push(s),i.hasCell&&B.push(o));let E=!1;for(let D of B)if(D._impl.autogradMeta&&D.requiresGrad){E=!0;break}let F=Oo.isEnabled()&&E,C=jB(g,y,h,w,x,k,O,R,F);YB(d);let H=Ee(v,[p,c,u],e.dtype,e.device),G=Ee(A,T,e.dtype,e.device),U=N?Ee(N,T,e.dtype,e.device):null;if(F){let D={xArr:m,yArr:v,hxArr:_,cxArr:b,opts:h,numLayers:f,hasInit:!!s,hasCell:i.hasCell,dtype:e.dtype,device:e.device,inputShape:[...e.shape],stateShape:T,weightShapes:t.map(ue=>({x2hW:[...ue.x2h.weight.shape],x2hB:[...ue.x2h.bias.shape],h2hW:[...ue.h2h.weight.shape],h2hB:[...ue.h2h.bias.shape]}))},V=new HT(C,D,B.length);Qg(V,B),KT(V,H,0),KT(V,G,1),U&&KT(V,U,2)}else UB(C);return i.hasCell?[H,G,U]:[H,G]}function ZB(n,e,t,r=null,s=null){return JB("lstm",n,e,t,r,s)}function QB(n,e,t,r=null){return JB("gru",n,e,t,r,null)}var Bo,ey,Nz,HT,XB=M(()=>{"use strict";Ao();Wp();ou();Xg();wn();qp();pi();WT();Bo=l(n=>YT(n),"carr"),ey=l(n=>n.reduce((e,t)=>e*t,1),"prod");l(Cr,"devIn");l(Ro,"devOut");l(YB,"flushOut");Nz={lstm:{cellMode:FB,gates:4,hasCell:!0},gru:{cellMode:zB,gates:3,hasCell:!1}},HT=class extends ve{static{l(this,"CudnnRNNBackward")}constructor(e,t,r){super(r),this.fwd=e,this.info=t}apply(e){let[t,r,s]=e,o=this.info,{seqLen:i,batch:a,inputSize:u,hiddenSize:p}=o.opts,c=o.numLayers,f=c*a*p,d=[],h=Cr(o.xArr),m=Cr(o.yArr),g=o.hasInit?Cr(o.hxArr):0n,y=o.hasCell&&o.hasInit?Cr(o.cxArr):0n,_=Cr(Bo(t)),b=r?Cr(Bo(r)):0n,w=o.hasCell&&s?Cr(Bo(s)):0n,x=new Float32Array(i*a*u),S=Ro(x,d),T=null,v=null,A=0n,N=0n;o.hasInit&&(T=new Float32Array(f),A=Ro(T,d),o.hasCell&&(v=new Float32Array(f),N=Ro(v,d)));let k=o.weightShapes.map(E=>({x2hW:new Float32Array(ey(E.x2hW)),x2hB:new Float32Array(ey(E.x2hB)),h2hW:new Float32Array(ey(E.h2hW)),h2hB:new Float32Array(ey(E.h2hB))})),O=k.map(E=>({x2hW:Ro(E.x2hW,d),x2hB:Ro(E.x2hB,d),h2hW:Ro(E.h2hW,d),h2hB:Ro(E.h2hB,d)}));qB(this.fwd,h,m,g,y,_,b,w,S,A,N,O),YB(d);let R=l((E,F)=>Ee(E,F,o.dtype,o.device),"w"),B=[R(x,o.inputShape)];for(let E=0;E<k.length;E++){let F=k[E],C=o.weightShapes[E];B.push(R(F.x2hW,C.x2hW),R(F.x2hB,C.x2hB),R(F.h2hW,C.h2hW),R(F.h2hB,C.h2hB))}return o.hasInit&&(B.push(R(T,o.stateShape)),o.hasCell&&B.push(R(v,o.stateShape))),B}};l(KT,"attach");l(JB,"cudnnRNNOp");l(ZB,"cudnnLSTMOp");l(QB,"cudnnGRUOp")});function eR(n){return In()?To(n):Ha(n)}function Az(n,e){if(In())return Xi(n);let t=ht(n.byteLength);return e.push([n,t]),t}function Oz(n){for(let[e,t]of n)qt(e,t),_t(t,e.byteLength)}function tR(n){let e=nR(n);if(e)return{data:n._impl.storage.rawData,lay:e};let t=ZT();if(In()&&t){let s=t(n._impl.storage.rawData,n.shape,n.strides,n._impl.storageOffset,n.dtype),o=n.shape,i=o.length,a=o[i-2],u=o[i-1],p=1;for(let c=0;c<i-2;c++)p*=o[c];return{data:s,lay:{trans:!1,batchStride:a*u,batch:p,rows:a,cols:u}}}let r=ls(n);return{data:r._impl.storage.rawData,lay:nR(r)}}function nR(n){if(n.storageOffset!==0)return null;let e=n.shape,t=n.strides,r=e.length;if(r<2)return null;let s=e[r-2],o=e[r-1],i=t[r-2],a=t[r-1],u;if(i===o&&a===1)u=!1;else if(i===1&&a===s)u=!0;else return null;let p=s*o;for(let f=r-3;f>=0;f--){if(t[f]!==p)return null;p*=e[f]}let c=1;for(let f=0;f<r-2;f++)c*=e[f];return{trans:u,batchStride:s*o,batch:c,rows:s,cols:o}}function Bz(n,e){let t=new So;t.setGradFn(n,0),t.requiresGrad=!0,e._impl.setAutogradMeta(t),e._impl._updateKeySet()}function rR(n,e){if(!n.device||n.device.type!=="gpu"||e.device.type!=="gpu"||n.dtype!=="f32"||e.dtype!=="f32")return null;let t=n.shape.length,r=e.shape.length;if(t<2||r<2||t!==r)return null;let s=n.shape[t-2],o=n.shape[t-1],i=e.shape[r-1];if(e.shape[r-2]!==o)return null;let a=tR(n),u=tR(e),p=a.lay,c=u.lay;if(!p||!c||p.batch!==c.batch)return null;let f=p.batch,d=[],h=eR(a.data),m=eR(u.data),g=new Float32Array(f*s*i),y=Az(g,d);f>1?TT(f,s,i,o,h,p.batchStride,p.trans,m,c.batchStride,c.trans,y,s*i):zp(s,i,o,h,p.trans,m,c.trans,y),Oz(d);let _=[...n.shape.slice(0,t-2),s,i],b=Ee(g,_,n.dtype,n.device);if(Oo.isEnabled()&&(n._impl.autogradMeta&&n.requiresGrad||e._impl.autogradMeta&&e.requiresGrad)){let w=new JT(n,e);Qg(w,[n,e]),Bz(w,b)}return b}var JT,sR=M(()=>{"use strict";Ao();Wp();ou();Xg();wn();be();qn();qp();pi();Vp();be();l(eR,"devIn");l(Az,"devOut");l(Oz,"flushOut");l(tR,"operandData");l(nR,"operandLayout");JT=class extends ve{static{l(this,"MatmulBackward")}constructor(e,t){super(2),this.A=e,this.B=t}apply(e){let t=e[0],r=this.A,s=this.B,o=r.shape.length,i=s.shape.length,a=tt(t,s.transpose(i-2,i-1)),u=tt(r.transpose(o-2,o-1),t);return[a,u]}};l(Bz,"attach");l(rR,"gpuMatmul")});function $l(){if(oR)return;oR=!0;let n=new Vt("mlc","DEF");for(let e of Rz)n.def(e)}var Rz,oR,ty=M(()=>{"use strict";ys();Rz=["svd(Tensor input) -> (Tensor, Tensor, Tensor)","eigh(Tensor input) -> (Tensor, Tensor)","cholesky(Tensor input) -> Tensor","inv(Tensor input) -> Tensor","pinv(Tensor input) -> Tensor","det(Tensor input) -> Tensor","cov(Tensor input) -> Tensor","solve(Tensor a, Tensor b) -> Tensor","lstsq(Tensor a, Tensor b) -> Tensor"],oR=!1;l($l,"ensureLinalgSchemas")});var ef=M(()=>{"use strict"});import Mz from"koffi";function Iz(){if(tf!==null)return tf;try{let n=Mz.load(bs(J1));Dr={create:n.func("int cusolverDnCreate(_Out_ void **h)"),destroy:n.func("int cusolverDnDestroy(void *h)"),setStream:n.func("int cusolverDnSetStream(void *h, void *stream)"),gesvdBufferSize:n.func("int cusolverDnSgesvd_bufferSize(void *h, int m, int n, _Out_ int *lwork)"),gesvd:n.func("int cusolverDnSgesvd(void *h, int8 jobu, int8 jobvt, int m, int n, uint64 A, int lda, uint64 S, uint64 U, int ldu, uint64 VT, int ldvt, uint64 work, int lwork, uint64 rwork, uint64 info)"),syevdBufferSize:n.func("int cusolverDnSsyevd_bufferSize(void *h, int jobz, int uplo, int n, uint64 A, int lda, uint64 W, _Out_ int *lwork)"),syevd:n.func("int cusolverDnSsyevd(void *h, int jobz, int uplo, int n, uint64 A, int lda, uint64 W, uint64 work, int lwork, uint64 info)"),potrfBufferSize:n.func("int cusolverDnSpotrf_bufferSize(void *h, int uplo, int n, uint64 A, int lda, _Out_ int *lwork)"),potrf:n.func("int cusolverDnSpotrf(void *h, int uplo, int n, uint64 A, int lda, uint64 work, int lwork, uint64 info)"),getrfBufferSize:n.func("int cusolverDnSgetrf_bufferSize(void *h, int m, int n, uint64 A, int lda, _Out_ int *lwork)"),getrf:n.func("int cusolverDnSgetrf(void *h, int m, int n, uint64 A, int lda, uint64 work, uint64 ipiv, uint64 info)"),getrs:n.func("int cusolverDnSgetrs(void *h, int trans, int n, int nrhs, uint64 A, int lda, uint64 ipiv, uint64 B, int ldb, uint64 info)")},tf=!0}catch{tf=!1}return tf}function rf(){if(!Iz())throw new Error("cuSOLVER library not found; install the CUDA Toolkit (cusolver) to use GPU linalg");let n=He();if(Qi(),!ny){let e=[null];cs("create",Dr.create(e)),ny=e[0],cs("setStream",Dr.setStream(ny,n.stream))}return ny}function cs(n,e){if(e!==0)throw new Error("cuSOLVER "+n+" failed: "+e)}function Fl(){$e("cuStreamSynchronize",we.streamSynchronize(He().stream))}function sf(){let n=[];return{up(e){let t=ht(e.byteLength);return xs(t,e),n.push([t,e.byteLength]),t},out(e){let t=Math.max(e,1),r=ht(t);return n.push([r,t]),r},free(){for(let[e,t]of n)_t(e,t)}}}function nf(n){let e=new Int32Array(1);return qt(e,n),e[0]}function QT(n,e,t){let r=rf(),s=sf();try{let o=s.up(n),i=[0];cs("gesvd_bufferSize",Dr.gesvdBufferSize(r,e,t,i));let a=s.out(t*4),u=s.out(e*t*4),p=s.out(t*t*4),c=s.out(i[0]*4),f=s.out(4);cs("gesvd",Dr.gesvd(r,iR,iR,e,t,o,e,a,u,e,p,t,c,i[0],0n,f)),Fl();let d=nf(f);if(d<0)throw new Error("cuSOLVER gesvd: invalid argument "+-d);if(d>0)throw new Error("cuSOLVER gesvd: did not converge");let h=new Float32Array(t),m=new Float32Array(e*t),g=new Float32Array(t*t);return qt(h,a),qt(m,u),qt(g,p),{S:h,U:m,VT:g}}finally{s.free()}}function uR(n,e){let t=rf(),r=sf();try{let s=r.up(n),o=r.out(e*4),i=[0];cs("syevd_bufferSize",Dr.syevdBufferSize(t,aR,ry,e,s,e,o,i));let a=r.out(i[0]*4),u=r.out(4);cs("syevd",Dr.syevd(t,aR,ry,e,s,e,o,a,i[0],u)),Fl();let p=nf(u);if(p!==0)throw new Error("cuSOLVER syevd: failed to converge (info "+p+")");let c=new Float32Array(e),f=new Float32Array(e*e);return qt(c,o),qt(f,s),{W:c,V:f}}finally{r.free()}}function lR(n,e){let t=rf(),r=sf();try{let s=r.up(n),o=[0];cs("potrf_bufferSize",Dr.potrfBufferSize(t,ry,e,s,e,o));let i=r.out(o[0]*4),a=r.out(4);cs("potrf",Dr.potrf(t,ry,e,s,e,i,o[0],a)),Fl();let u=nf(a);if(u>0)throw new Error("linalg.cholesky: matrix is not positive definite");if(u<0)throw new Error("cuSOLVER potrf: invalid argument "+-u);let p=new Float32Array(e*e);return qt(p,s),p}finally{r.free()}}function cR(n,e){let t=rf(),r=sf();try{let s=r.up(n),o=[0];cs("getrf_bufferSize",Dr.getrfBufferSize(t,e,e,s,e,o));let i=r.out(o[0]*4),a=r.out(e*4),u=r.out(4);cs("getrf",Dr.getrf(t,e,e,s,e,i,a,u)),Fl();let p=new Float32Array(e*e),c=new Int32Array(e);return qt(p,s),qt(c,a),{LU:p,ipiv:c}}finally{r.free()}}function XT(n,e,t,r,s){let o=rf(),i=sf();try{let a=i.up(n),u=[0];cs("getrf_bufferSize",Dr.getrfBufferSize(o,e,e,a,e,u));let p=i.out(u[0]*4),c=i.out(e*4),f=i.out(4);cs("getrf",Dr.getrf(o,e,e,a,e,p,c,f)),Fl();let d=nf(f);if(d>0)throw new Error("linalg: matrix is singular");let h=i.up(t);if(cs("getrs",Dr.getrs(o,s,e,r,a,e,c,h,e,f)),Fl(),d=nf(f),d<0)throw new Error("cuSOLVER getrs: invalid argument "+-d);let m=new Float32Array(e*r);return qt(m,h),m}finally{i.free()}}var Dr,tf,iR,aR,ry,ny,pR=M(()=>{"use strict";Ts();ig();ws();pi();vl();tf=null;l(Iz,"ensure");iR=83,aR=1,ry=0,ny=null;l(rf,"handle");l(cs,"ck");l(Fl,"sync");l(sf,"scope");l(nf,"readInt");l(QT,"csGesvd");l(uR,"csSyevd");l(lR,"csPotrf");l(cR,"csGetrf");l(XT,"csSolveLU")});function Js(n){return Float32Array.from(Qe(n))}function gi(n,e){if(n.ndim!==2)throw new Error(`linalg.${e}: expected a 2-D matrix, got ${n.ndim}-D`)}function Zs(n,e){if(n.dtype!=="f32")throw new Error(`linalg.${e}: GPU backend supports f32 tensors only, got '${n.dtype}'`)}function af(n,e){if(n.shape[0]!==n.shape[1])throw new Error(`linalg.${e}: matrix must be square`)}function Ez(){$e("cuStreamSynchronize",we.streamSynchronize(He().stream))}function ex(n,e,t){let r=Math.min(e,t),s=new Float32Array(e*r),o=new Float32Array(r),i=new Float32Array(t*r);if(e>=t){let a=new Float32Array(e*t);for(let p=0;p<e;p++)for(let c=0;c<t;c++)a[p+c*e]=n[p*t+c];let u=QT(a,e,t);for(let p=0;p<r;p++){o[p]=u.S[p];for(let c=0;c<e;c++)s[c*r+p]=u.U[c+p*e];for(let c=0;c<t;c++)i[c*r+p]=u.VT[p+c*t]}}else{let a=QT(n,t,e);for(let u=0;u<r;u++){o[u]=a.S[u];for(let p=0;p<e;p++)s[p*r+u]=a.VT[u+p*e];for(let p=0;p<t;p++)i[p*r+u]=a.U[p+u*t]}}return{U:s,S:o,V:i,k:r}}function dR(n,e){gi(e,"svd"),Zs(e,"svd");let[t,r]=e.shape,{U:s,S:o,V:i,k:a}=ex(Js(e),t,r);return[Ee(s,[t,a],e.dtype,e.device),Ee(o,[a],e.dtype,e.device),Ee(i,[r,a],e.dtype,e.device)]}function hR(n,e){gi(e,"eigh"),Zs(e,"eigh"),af(e,"eigh");let t=e.shape[0],{W:r,V:s}=uR(Js(e),t),o=new Float32Array(t*t);for(let i=0;i<t;i++)for(let a=0;a<t;a++)o[i*t+a]=s[i+a*t];return[Ee(r,[t],e.dtype,e.device),Ee(o,[t,t],e.dtype,e.device)]}function mR(n,e){gi(e,"cholesky"),Zs(e,"cholesky"),af(e,"cholesky");let t=e.shape[0],r=lR(Js(e),t),s=new Float32Array(t*t);for(let o=0;o<t;o++)for(let i=0;i<=o;i++)s[o*t+i]=r[o+i*t];return Ee(s,[t,t],e.dtype,e.device)}function gR(n,e,t){gi(e,"solve"),Zs(e,"solve"),af(e,"solve"),Zs(t,"solve");let r=e.shape[0],s=t.ndim===1,o=s?1:t.shape[1];if(t.shape[0]!==r)throw new Error("linalg.solve: right-hand side rows must match matrix");let i=Js(t),a=new Float32Array(r*o);for(let c=0;c<r;c++)for(let f=0;f<o;f++)a[c+f*r]=i[c*o+f];let u=XT(Js(e),r,a,o,fR),p=new Float32Array(r*o);for(let c=0;c<r;c++)for(let f=0;f<o;f++)p[c*o+f]=u[c+f*r];return Ee(p,s?[r]:[r,o],e.dtype,e.device)}function yR(n,e){gi(e,"inv"),Zs(e,"inv"),af(e,"inv");let t=e.shape[0],r=new Float32Array(t*t);for(let i=0;i<t;i++)r[i+i*t]=1;let s=XT(Js(e),t,r,t,fR),o=new Float32Array(t*t);for(let i=0;i<t;i++)for(let a=0;a<t;a++)o[i*t+a]=s[i+a*t];return Ee(o,[t,t],e.dtype,e.device)}function _R(n,e){gi(e,"det"),Zs(e,"det"),af(e,"det");let t=e.shape[0],{LU:r,ipiv:s}=cR(Js(e),t),o=1,i=1;for(let a=0;a<t;a++)i*=r[a+a*t],s[a]!==a+1&&(o=-o);return Ee(new Float32Array([o*i]),[],e.dtype,e.device)}function bR(n,e){gi(e,"pinv"),Zs(e,"pinv");let[t,r]=e.shape,{U:s,S:o,V:i,k:a}=ex(Js(e),t,r),u=1e-12*(a?o[0]:0),p=new Float32Array(r*t);for(let c=0;c<r;c++)for(let f=0;f<t;f++){let d=0;for(let h=0;h<a;h++){let m=o[h];m>u&&(d+=i[c*a+h]*(1/m)*s[f*a+h])}p[c*t+f]=d}return Ee(p,[r,t],e.dtype,e.device)}function wR(n,e,t){gi(e,"lstsq"),Zs(e,"lstsq"),Zs(t,"lstsq");let[r,s]=e.shape,o=t.ndim===1,i=o?1:t.shape[1];if(t.shape[0]!==r)throw new Error("linalg.lstsq: right-hand side rows must match matrix");let a=Js(t),{U:u,S:p,V:c,k:f}=ex(Js(e),r,s),d=1e-12*(f?p[0]:0),h=new Float32Array(f*i);for(let g=0;g<f;g++){let y=p[g];for(let _=0;_<i;_++){let b=0;for(let w=0;w<r;w++)b+=u[w*f+g]*a[w*i+_];h[g*i+_]=y>d?b/y:0}}let m=new Float32Array(s*i);for(let g=0;g<s;g++)for(let y=0;y<i;y++){let _=0;for(let b=0;b<f;b++)_+=c[g*f+b]*h[b*i+y];m[g*i+y]=_}return Ee(m,o?[s]:[s,i],e.dtype,e.device)}function TR(n,e){gi(e,"cov"),Zs(e,"cov");let[t,r]=e.shape,s=Js(e),o=new Float32Array(r);for(let f=0;f<r;f++){let d=0;for(let h=0;h<t;h++)d+=s[h*r+f];o[f]=d/t}let i=new Float32Array(t*r);for(let f=0;f<t;f++)for(let d=0;d<r;d++)i[f*r+d]=s[f*r+d]-o[d];let a=t>1?t-1:1;He();let u=ht(i.byteLength),p=new Float32Array(r*r),c=ht(p.byteLength);try{xs(u,i),zp(r,r,t,u,!0,u,!1,c),Ez(),qt(p,c)}finally{_t(u,i.byteLength),_t(c,p.byteLength)}for(let f=0;f<r*r;f++)p[f]/=a;return Ee(p,[r,r],e.dtype,e.device)}var fR,xR=M(()=>{"use strict";wn();ef();pR();Vp();pi();ws();Ts();fR=1;l(Js,"hostData");l(gi,"require2D");l(Zs,"requireF32");l(af,"requireSquare");l(Ez,"syncStream");l(ex,"_svdRowMajor");l(dR,"gpuSvd");l(hR,"gpuEigh");l(mR,"gpuCholesky");l(gR,"gpuSolve");l(yR,"gpuInv");l(_R,"gpuDet");l(bR,"gpuPinv");l(wR,"gpuLstsq");l(TR,"gpuCov")});function vR(){if(SR)return;SR=!0,$l();let n=new Vt("mlc","IMPL");for(let[e,t]of Object.entries(Lz))n.impl(e,1,t)}var Lz,SR,kR=M(()=>{"use strict";ys();An();ty();xR();Lz={svd:dR,eigh:hR,cholesky:mR,inv:yR,pinv:bR,det:_R,cov:TR,solve:gR,lstsq:wR},SR=!1;l(vR,"registerCudaLinalg")});var oy={};at(oy,{adamGraphKernelSource:()=>CR,deviceAdam:()=>DR,deviceClipGradNorm:()=>qz,deviceConcat:()=>MR,deviceContiguous:()=>rx,freeOptimizerDeviceState:()=>IR,isCudaGraphEnabled:()=>kO,measureCudaKernel:()=>NR,preloadCublas:()=>AR,releaseCudaMemory:()=>Wz,runCudaKernel:()=>Cz,runCudaKernelResident:()=>nx,runCudaKernelSync:()=>OR,runCudaPlan:()=>RO,setCudaGraphEnabled:()=>vO,stepIncKernelSource:()=>LR,teardownAfterFit:()=>Kz});function NR(n,e,t=[],r={}){He();let{func:s}=wo(n.source,n.name),o=n.metadata,i=r.warmup??5,a=r.repeat??30,u=r.minWarmupMs??25,p=r.maxWarmup??1e5,c=o.gridDim,f=o.blockDim,d=0,h=e.map(g=>Math.max(g,1)),m=h.map(g=>ht(g));try{let g=performance.now(),y=0;for(;(y<i||performance.now()-g<u)&&(ns(s,c,f,d,m,t),!(++y>=p)););let _=[];for(let b=0;b<a;b++){let w=performance.now();ns(s,c,f,d,m,t),_.push(performance.now()-w)}return _}finally{for(let g=0;g<m.length;g++)_t(m[g],h[g])}}async function AR(){return uf||(uf=await Promise.resolve().then(()=>(Vp(),wO))),uf}function OR(n,e,t){let r=n.metadata;if(r.cublas){if(!uf)throw new Error("cuBLAS module not preloaded; call preloadCublas() before sync execution");let{M:f,N:d,K:h,aIdx:m,bIdx:g,cIdx:y,transB:_}=r.cublas;uf.cublasMatmul(f,d,h,e[m],e[g],e[y],_);return}He();let{func:s}=tT(n),o=[],i=[];for(let f of e)ArrayBuffer.isView(f)?o.push(f):i.push(f);if(t)for(let f of t)i.push(f);let a=r.outputIndices||o.map((f,d)=>d),u=[];for(let f of o){let d=ht(f.byteLength);xs(d,f),u.push(d)}let p=BR(r.scratch);ns(s,r.gridDim,r.blockDim,0,[...u,...p.ptrs],i);let c=new Set(a);for(let f=0;f<o.length;f++)c.has(f)&&qt(o[f],u[f]);for(let f=0;f<u.length;f++)_t(u[f],o[f].byteLength);tx(p)}function nx(n,e,t){let r=n.metadata;if(r.cublas)throw new Error("cuBLAS kernels are not supported on the eager device-resident path");He();let{func:s}=tT(n),o=[],i=[];for(let c of e)ArrayBuffer.isView(c)?o.push(c):i.push(c);if(t)for(let c of t)i.push(c);let a=r._outputSet||(r._outputSet=new Set(r.outputIndices||o.map((c,f)=>f))),u=new Array(o.length),p=BR(r.scratch);if(In()){for(let c=0;c<o.length;c++)u[c]=a.has(c)?Xi(o[c]):To(o[c]);ns(s,r.gridDim,r.blockDim,0,[...u,...p.ptrs],i,!1),tx(p);return}for(let c=0;c<o.length;c++)u[c]=Ha(o[c]);ns(s,r.gridDim,r.blockDim,0,[...u,...p.ptrs],i,!1);for(let c=0;c<o.length;c++)a.has(c)&&vT(o[c],u[c]);tx(p)}function BR(n){if(!n||n.length===0)return{ptrs:[],bufs:[]};let e=[],t=[];for(let r of n){let s=r.size*St(r.dtype).BYTES_PER_ELEMENT,o=ht(s);e.push(o),t.push([o,s])}return{ptrs:e,bufs:t}}function tx(n){for(let[e,t]of n.bufs)_t(e,t)}async function Cz(n,e,t){n.metadata.cublas&&await AR(),OR(n,e,t)}function sy(n){return Dz[n]||"float"}function RR(n,e){return{gridDim:[Math.max(Math.ceil(n/256),1),1,1],blockDim:[256,1,1],sharedMemBytes:0,outputIndices:e}}function Pz(n,e){return`extern "C" __global__ void ${e}(const ${n}* in, ${n}* out, int n, int rank,
|
|
102
|
+
int s0,int s1,int s2,int s3,int s4,int s5,int s6,int s7,
|
|
103
|
+
int t0,int t1,int t2,int t3,int t4,int t5,int t6,int t7, int off) {
|
|
104
|
+
int i = blockIdx.x*blockDim.x + threadIdx.x; if (i >= n) return;
|
|
105
|
+
int shp[8] = {s0,s1,s2,s3,s4,s5,s6,s7};
|
|
106
|
+
int strd[8] = {t0,t1,t2,t3,t4,t5,t6,t7};
|
|
107
|
+
long long src = off; int rem = i;
|
|
108
|
+
for (int d = rank-1; d >= 0; d--) { int x = rem % shp[d]; rem /= shp[d]; src += (long long)x*strd[d]; }
|
|
109
|
+
out[i] = in[src];
|
|
110
|
+
}`}function rx(n,e,t,r,s){let o=sy(s),i=`gather_${s}`,a=Mo.get(i);a||(a=Pz(o,i),Mo.set(i,a));let u=e.length,p=1;for(let h=0;h<u;h++)p*=e[h];let c=new(St(s))(Math.max(p,1)),f=new Array(8).fill(1),d=new Array(8).fill(0);for(let h=0;h<u;h++)f[h]=e[h],d[h]=t[h];return nx({source:a,name:i,metadata:RR(p,[1])},[n,c,p,u,...f,...d,r|0],null),c}function $z(n,e){return`extern "C" __global__ void ${e}(const ${n}* in, ${n}* out, int pre, int dk, int tail, int total, int offset) {
|
|
111
|
+
int i = blockIdx.x*blockDim.x + threadIdx.x; int n = pre*dk*tail; if (i >= n) return;
|
|
112
|
+
int t = i % tail; int r = i / tail; int j = r % dk; int p = r / dk;
|
|
113
|
+
out[(long long)p*total*tail + (long long)(offset+j)*tail + t] = in[i];
|
|
114
|
+
}`}function MR(n,e,t,r,s,o,i){let a=sy(i),u=`catcopy_${i}`,p=Mo.get(u);p||(p=$z(a,u),Mo.set(u,p));let c=n==="stack",f=s.length,d=r<0?f+r:r,h=1;for(let _=0;_<d;_++)h*=s[_];let m=1;for(let _=d+1;_<f;_++)m*=s[_];let g=s[d],y=0;for(let _=0;_<e.length;_++){let b=c?1:t[_][d],w=h*b*m;nx({source:p,name:u,metadata:RR(w,[1])},[e[_],o,h,b,m,g,y],null),y+=b}}function Fz(n,e){return`extern "C" __global__ void ${e}(${n}* w, const ${n}* g, ${n}* m, ${n}* v, int n, float b1, float b2, float ob1, float ob2, float eps, float ss, float bc2s, float wd) {
|
|
115
|
+
int i = blockIdx.x*blockDim.x + threadIdx.x; if (i >= n) return;
|
|
116
|
+
float gi = (float)g[i] + wd * (float)w[i];
|
|
117
|
+
float mi = b1*(float)m[i] + ob1*gi;
|
|
118
|
+
float vi = b2*(float)v[i] + ob2*gi*gi;
|
|
119
|
+
m[i] = (${n})mi; v[i] = (${n})vi;
|
|
120
|
+
w[i] = (${n})((float)w[i] - ss * mi / (sqrtf(vi)/bc2s + eps));
|
|
121
|
+
}`}function zz(n){return`extern "C" __global__ void ${n}(int* t) {
|
|
122
|
+
if (threadIdx.x == 0 && blockIdx.x == 0) t[0] = t[0] + 1;
|
|
123
|
+
}`}function Vz(n,e){return`extern "C" __global__ void ${e}(${n}* w, const ${n}* g, ${n}* m, ${n}* v, const int* t, int n, float b1, float b2, float ob1, float ob2, float eps, float lr, float wd) {
|
|
124
|
+
int i = blockIdx.x*blockDim.x + threadIdx.x; if (i >= n) return;
|
|
125
|
+
int step = t[0];
|
|
126
|
+
double bc1 = 1.0 - pow((double)b1, (double)step);
|
|
127
|
+
double bc2 = 1.0 - pow((double)b2, (double)step);
|
|
128
|
+
float ss = (float)((double)lr / bc1);
|
|
129
|
+
float bc2s = (float)sqrt(bc2);
|
|
130
|
+
float gi = (float)g[i] + wd * (float)w[i];
|
|
131
|
+
float mi = b1*(float)m[i] + ob1*gi;
|
|
132
|
+
float vi = b2*(float)v[i] + ob2*gi*gi;
|
|
133
|
+
m[i] = (${n})mi; v[i] = (${n})vi;
|
|
134
|
+
w[i] = (${n})((float)w[i] - ss * mi / (sqrtf(vi)/bc2s + eps));
|
|
135
|
+
}`}function Gz(n){return`extern "C" __global__ void ${n}(const float* g, double* acc, int n) {
|
|
136
|
+
extern __shared__ double sdata[];
|
|
137
|
+
int tid = threadIdx.x;
|
|
138
|
+
int i = blockIdx.x*blockDim.x + threadIdx.x;
|
|
139
|
+
double v = (i < n) ? (double)g[i]*(double)g[i] : 0.0;
|
|
140
|
+
sdata[tid] = v;
|
|
141
|
+
__syncthreads();
|
|
142
|
+
for (int s = blockDim.x/2; s > 0; s >>= 1) { if (tid < s) sdata[tid] += sdata[tid+s]; __syncthreads(); }
|
|
143
|
+
if (tid == 0) atomicAdd(acc, sdata[0]);
|
|
144
|
+
}`}function jz(n){return`extern "C" __global__ void ${n}(const double* acc, const float* mp, float* coef) {
|
|
145
|
+
if (threadIdx.x == 0 && blockIdx.x == 0) {
|
|
146
|
+
double norm = sqrt(acc[0]);
|
|
147
|
+
double c = (double)mp[0] / (norm + (double)mp[1]);
|
|
148
|
+
coef[0] = c < 1.0 ? (float)c : 1.0f;
|
|
149
|
+
}
|
|
150
|
+
}`}function Uz(n){return`extern "C" __global__ void ${n}(float* g, const float* coef, int n) {
|
|
151
|
+
int i = blockIdx.x*blockDim.x + threadIdx.x; if (i < n) g[i] = g[i] * coef[0];
|
|
152
|
+
}`}function qz(n,e,t=1e-6){He(),yi===null&&(yi=ht(8),lf=ht(4),cf=ht(8));let r=He().stream;zn()||xs(cf,new Float32Array([e,t])),zn()?we.memsetD8Async(yi,0,8,r):we.memsetD8(yi,0,8);let s=wo(Gz("clip_accum"),"clip_accum").func,o=wo(jz("clip_coef"),"clip_coef").func,i=wo(Uz("clip_scale"),"clip_scale").func,a=[];for(let u of n){if(!u.grad)continue;let p=u.grad._impl.storage.rawData;if(!p)continue;let c=p.length,f=gg(p);a.push([f,c]),ns(s,[Math.ceil(c/256),1,1],[256,1,1],256*8,[f,yi],[c],!1)}if(a.length!==0){ns(o,[1,1,1],[1,1,1],0,[yi,cf,lf],[],!1);for(let[u,p]of a)ns(i,[Math.ceil(p/256),1,1],[256,1,1],0,[u,lf],[p],!1)}}function IR(n){let e=n&&n._state;if(!(!e||typeof e.values!="function"))for(let t of e.values())t&&(t._mDev!==void 0&&(ci(t._mDev),delete t._mDev),t._vDev!==void 0&&(ci(t._vDev),delete t._vDev),t._tDev!==void 0&&(ci(t._tDev),delete t._tDev))}function ER(){yi!==null&&(ci(yi),ci(lf),ci(cf),yi=null,lf=null,cf=null)}function Wz(){return OT(),ER(),tg()}function Kz(n,e){let t=n&&n.__eagerGraphRunner;if(t){try{gT(t.captured)}catch{}n._cudaGraphPhase=t.phase,delete n.__eagerGraphRunner}if(e)for(let r of e)IR(r);return Nl(!1),yg(),ER(),tg()}function LR(){let n="step_inc",e=Mo.get(n);return e||(e=zz(n),Mo.set(n,e)),{source:e,name:n}}function CR(n="f32"){let e=`adam_graph_${n}`,t=Mo.get(e);return t||(t=Vz(sy(n),e),Mo.set(e,t)),{source:t,name:e}}function Hz(n,e){xs(n,new Int32Array([e|0]))}function DR(n,e,t){if(!In())return!1;let r=n.dtype||"f32";if(r!=="f32")return!1;let s=n._impl.storage.rawData,o=n.grad._impl.storage.rawData;if(!s||!o)return!1;let i=s.length;He();let a=cO();e._mDev||(e._mDev=ht(i*4),we.memsetD8(e._mDev,0,i*4),e._vDev=ht(i*4),we.memsetD8(e._vDev,0,i*4),a&&(e._tDev=ht(4),we.memsetD8(e._tDev,0,4)),kT(s));let u=gg(s),p=To(o);if(zn()){if(e._tDev===void 0)throw new Error("CUDA graph Adam requires a device step counter; warmup must run before capture");let h=LR(),m=CR(r),g=wo(h.source,h.name).func,y=wo(m.source,m.name).func;return ns(g,[1,1,1],[1,1,1],0,[e._tDev],[],!1),ns(y,[Math.ceil(i/256),1,1],[256,1,1],0,[u,p,e._mDev,e._vDev,e._tDev],[i,rr(t.beta1),rr(t.beta2),rr(t.omb1),rr(t.omb2),rr(t.eps),rr(t.lr),rr(t.wd)],!1),!0}let c=`adam_${r}`,f=Mo.get(c);f||(f=Fz(sy(r),c),Mo.set(c,f));let{func:d}=wo(f,c);return ns(d,[Math.ceil(i/256),1,1],[256,1,1],0,[u,p,e._mDev,e._vDev],[i,rr(t.beta1),rr(t.beta2),rr(t.omb1),rr(t.omb2),rr(t.eps),rr(t.stepSize),rr(t.bc2sqrt),rr(t.wd)],!1),a&&e._tDev!==void 0&&Hz(e._tDev,e.step),!0}function Yz(n){if(!In()||!n.device||n.device.type!=="gpu")return null;let e=rx(n._impl.storage.rawData,n.shape,n.strides,n._impl.storageOffset,n.dtype);return Ee(e,[...n.shape],n.dtype,n.device)}function Zz(n,e,t,r){if(!In()||r!==0||t.length!==2||!n.device||n.device.type!=="gpu"||e.dtype!=="i32")return null;let s=t[0],o=t[1],i=ls(n),a=ls(e),u=a.numel;if(i.numel!==u*o)return null;He();let p=new Float32Array(s*o),{func:c}=wo(Jz,"index_add0"),f=Xi(p),d=He().stream;zn()?we.memsetD8Async(f,0,s*o*4,d):we.memsetD8(f,0,s*o*4);let h=To(i._impl.storage.rawData),m=To(a._impl.storage.rawData);return ns(c,[Math.ceil(u*o/256),1,1],[256,1,1],0,[h,m,f],[u,o],!1),Ee(p,[s,o],n.dtype,n.device)}var uf,Dz,Mo,yi,lf,cf,Jz,iy=M(()=>{"use strict";Ts();nT();pi();uT();MO();qp();fi();ug();ws();IT();Ym();wn();be();GT();UT();qn();rs();WT();XB();sR();kR();Za.setHostReadHook(BT);l(NR,"measureCudaKernel");uf=null;l(AR,"preloadCublas");l(OR,"runCudaKernelSync");l(nx,"runCudaKernelResident");l(BR,"_acquireScratch");l(tx,"_releaseScratch");l(Cz,"runCudaKernel");Dz={f32:"float",f64:"double",i64:"long long",i32:"int",i16:"short",i8:"signed char",u8:"unsigned char",bool:"unsigned char"};l(sy,"_ctype");Mo=new Map;l(RR,"_meta");l(Pz,"_gatherKernel");l(rx,"deviceContiguous");l($z,"_catKernel");l(MR,"deviceConcat");l(Fz,"_adamKernel");l(zz,"_stepIncKernel");l(Vz,"_adamGraphKernel");l(Gz,"_clipAccumKernel");l(jz,"_clipCoefKernel");l(Uz,"_clipScaleKernel");yi=null,lf=null,cf=null;l(qz,"deviceClipGradNorm");l(IR,"freeOptimizerDeviceState");l(ER,"freeClipScratch");l(Wz,"releaseCudaMemory");l(Kz,"teardownAfterFit");l(LR,"stepIncKernelSource");l(CR,"adamGraphKernelSource");l(Hz,"_setDeviceInt");l(DR,"deviceAdam");l(Yz,"gpuContiguousTensor");AB(Yz);Jz=`extern "C" __global__ void index_add0(const float* g, const int* idx, float* out, int K, int E) {
|
|
153
|
+
int t = blockIdx.x*blockDim.x + threadIdx.x; if (t >= K*E) return;
|
|
154
|
+
int i = t / E, j = t % E;
|
|
155
|
+
atomicAdd(&out[idx[i]*E + j], g[i*E + j]);
|
|
156
|
+
}`;l(Zz,"gpuIndexSelectBackward");Pl.setGpuBackward(Zz);sx(rx);ox(MR);ux(DR);lx(rR);PB()&&(ix(ZB),ax(QB));vR();Hm("cuda",NR)});var JR={};at(JR,{ensureWebGPUEager:()=>s3,flushWebGPUEager:()=>HR,instantiateWebGPU:()=>e3,prewarmPipelines:()=>WR,recordWebGPUEager:()=>hf,resetDevice:()=>Qz,runWebGPUKernel:()=>t3,runWebGPUPlan:()=>n3,webgpuEagerOp:()=>a3,webgpuEagerReady:()=>o3,webgpuRNN:()=>p3});function Vl(n){switch(jn(n)){case"i32":return Int32Array;case"u32":return Uint32Array;case"f16":return Uint16Array;default:return Float32Array}}function ly(n,e,t,r){if(t==="bf16")for(let s=0;s<e.length;s++)n[r+s]=Lm(e[s]);else if(t==="i64")for(let s=0;s<e.length;s++)n[r+s]=Number(BigInt.asIntN(32,e[s]));else n.set(e,r)}function hx(n,e,t,r,s){if(t==="bf16")for(let o=0;o<s;o++)n[o]=Cm(e[r+o]);else if(t==="i64")for(let o=0;o<s;o++)n[o]=BigInt(e[r+o]);else n.set(e.subarray(r,r+s))}function Eo(n){return Math.ceil(n/4)*4}async function mx(){return It||pf||(pf=(async()=>{let n=typeof navigator<"u"&&navigator.gpu;if(!n)try{let i=await import("webgpu");VR=i,n=i.create([]),i.globals&&(ff=i.globals.GPUBufferUsage,ay=i.globals.GPUMapMode,uy=i.globals.GPUShaderStage)}catch{throw new Error('WebGPU not available: install the "webgpu" npm package or run in a browser with WebGPU support')}!ff&&typeof GPUBufferUsage<"u"&&(ff=GPUBufferUsage,ay=GPUMapMode,uy=GPUShaderStage);let e=await n.requestAdapter();if(!e)throw new Error("WebGPU: no adapter found");let t=e.limits||{},r={},s=["maxStorageBuffersPerShaderStage","maxStorageBufferBindingSize","maxBufferSize","maxBindingsPerBindGroup"];for(let i of s)t[i]!==void 0&&(r[i]=t[i]);let o=[];return e.features&&e.features.has("shader-f16")&&o.push("shader-f16"),It=await e.requestDevice({requiredLimits:r,requiredFeatures:o}),!PR&&typeof process<"u"&&process.on&&(PR=!0,process.on("exit",()=>{It&&(It.destroy(),It=null)})),It})(),pf)}function Gl(){return ff}function gx(){return ay.READ}function Qz(){It&&(It.destroy(),It=null),pf=null,ff=null,ay=null,uy=null,VR=null}function Xz(n){return n.name==="_shapes"?"uniform":n.mode==="read_write"?"storage":"read-only-storage"}function GR(n,e){let t=n.createShaderModule({code:e.source}),r=[];for(let i of e.metadata.bindings)r.push({binding:i.index,visibility:uy.COMPUTE,buffer:{type:Xz(i)}});let s=n.createBindGroupLayout({entries:r}),o=n.createPipelineLayout({bindGroupLayouts:[s]});return{shaderModule:t,bindGroupLayout:s,pipelineLayout:o}}function jR(n,e){let{shaderModule:t,bindGroupLayout:r,pipelineLayout:s}=GR(n,e);return{pipeline:n.createComputePipeline({layout:s,compute:{module:t,entryPoint:e.name}}),bindGroupLayout:r}}function UR(n,e){let t=cy.get(e);return t||(t=jR(n,e),cy.set(e,t)),t}async function e3(n){let e=await mx(),{pipeline:t,bindGroupLayout:r}=jR(e,n);return{device:e,pipeline:t,bindGroupLayout:r,kernel:n,workgroupSize:n.metadata.workgroupSize,dispatchSize:n.metadata.dispatchSize,bindings:n.metadata.bindings}}function qR(n){let e=new Map,t=0;for(let r of n)if(r.name!=="_shapes")if(r.packed)for(let s of r.packed)e.set(s.name,s.argIndex!==void 0?s.argIndex:t++);else e.set(r.name,r.argIndex!==void 0?r.argIndex:t++);return e}async function t3(n,e,t){let{device:r,pipeline:s,bindGroupLayout:o,bindings:i,dispatchSize:a}=n,u=Gl(),p=[],c=[],f=qR(i);for(let y=0;y<i.length;y++){let _=i[y];if(_.name==="_shapes"){let k=new Uint32Array(t||[]),O=Math.max(Math.ceil(k.byteLength/16)*16,16),R=r.createBuffer({size:O,usage:u.UNIFORM|u.COPY_DST});r.queue.writeBuffer(R,0,k),p.push(R),c.push({binding:_.index,resource:{buffer:R}});continue}let b=Vs(_.dtype),w=Vl(_.dtype);if(_.packed){let k=Eo(_.packedSize*b),R=_.mode==="read_write"?u.STORAGE|u.COPY_SRC|u.COPY_DST:u.STORAGE|u.COPY_DST,B=r.createBuffer({size:Math.max(k,4),usage:R,mappedAtCreation:!0}),E=new w(B.getMappedRange());for(let F of _.packed){let C=f.get(F.name),H=e[C];H&&ly(E,H,F.dtype,F.offset)}B.unmap(),p.push(B),c.push({binding:_.index,resource:{buffer:B}});continue}let x=f.get(_.name),S=e[x],T=Eo(S.length*b),A=_.mode==="read_write"?u.STORAGE|u.COPY_SRC|u.COPY_DST:u.STORAGE|u.COPY_DST,N=r.createBuffer({size:Math.max(T,4),usage:A,mappedAtCreation:!0});ly(new w(N.getMappedRange()),S,_.dtype,0),N.unmap(),p.push(N),c.push({binding:_.index,resource:{buffer:N}})}let d=r.createBindGroup({layout:o,entries:c}),h=r.createCommandEncoder(),m=h.beginComputePass();m.setPipeline(s),m.setBindGroup(0,d),m.dispatchWorkgroups(a[0],a[1],a[2]),m.end();let g=[];for(let y=0;y<i.length;y++){let _=i[y];if(_.name==="_shapes"||_.mode!=="read_write")continue;let b=p[y],w=Vs(_.dtype),x=Vl(_.dtype);if(_.packed)for(let S of _.packed){let T=S.offset*w,v=Eo(S.size*w),A=r.createBuffer({size:v,usage:u.MAP_READ|u.COPY_DST});h.copyBufferToBuffer(b,T,A,0,v),g.push({readBuf:A,tensorIdx:f.get(S.name),size:S.size,dtype:S.dtype,ViewCtor:x})}else{let S=b.size,T=r.createBuffer({size:S,usage:u.MAP_READ|u.COPY_DST});h.copyBufferToBuffer(b,0,T,0,S),g.push({readBuf:T,tensorIdx:f.get(_.name),size:e[f.get(_.name)].length,dtype:_.dtype,ViewCtor:x})}}r.queue.submit([h.finish()]);for(let y of g){await y.readBuf.mapAsync(gx());let _=new y.ViewCtor(y.readBuf.getMappedRange());hx(e[y.tensorIdx],_,y.dtype,0,y.size),y.readBuf.unmap(),y.readBuf.destroy()}for(let y of p)y.destroy()}async function WR(n,e){if(typeof n.createComputePipelineAsync!="function")return;let t=[],r=new Set;for(let s of e){if(!s||r.has(s)||cy.has(s))continue;r.add(s);let{shaderModule:o,bindGroupLayout:i,pipelineLayout:a}=GR(n,s);t.push(n.createComputePipelineAsync({layout:a,compute:{module:o,entryPoint:s.name}}).then(u=>cy.set(s,{pipeline:u,bindGroupLayout:i}),()=>{}))}t.length>0&&await Promise.all(t)}async function n3(n,e,t){let r=await mx(),s=Gl();await WR(r,t.map(b=>b.kernel));let o=new Set;for(let b of t)for(let w of b.outputSlots)o.add(w);let i=new Array(n.numSlots).fill(null),a=new Array(n.numSlots).fill("f32");for(let b=0;b<n.numSlots;b++){let w=e[b];if(!w)continue;let x=w.dtype||"f32";a[b]=x;let S=Math.max(Eo(w.data.length*Vs(x)),4),T=!o.has(b),v=r.createBuffer({size:S,usage:s.STORAGE|s.COPY_DST|s.COPY_SRC,mappedAtCreation:T});T&&(ly(new(Vl(x))(v.getMappedRange()),w.data,x,0),v.unmap()),i[b]=v}let u=32,p=[],c=[],f={encoder:r.createCommandEncoder(),pending:0},d=l(()=>{++f.pending>=u&&(r.queue.submit([f.encoder.finish()]),f.encoder=r.createCommandEncoder(),f.pending=0)},"maybeFlush"),h=l(b=>{let w=f.encoder,{pipeline:x,bindGroupLayout:S}=UR(r,b.kernel),T=b.inputSlots.concat(b.outputSlots),v=[],A=[],N=0;for(let B of b.kernel.metadata.bindings)if(B.name==="_shapes"){let E=new Uint32Array(b.shapeValues||[]),F=Math.max(Math.ceil(E.byteLength/16)*16,16),C=r.createBuffer({size:F,usage:s.UNIFORM|s.COPY_DST});r.queue.writeBuffer(C,0,E),p.push(C),v.push({binding:B.index,resource:{buffer:C}})}else if(B.packed){let E=Vs(B.dtype),F=r.createBuffer({size:Math.max(Eo(B.packedSize*E),4),usage:s.STORAGE|s.COPY_DST|s.COPY_SRC});c.push(F);let C=B.mode==="read_write";for(let H of B.packed){let G=T[H.argIndex],U=Eo(H.size*E);C?A.push({slot:G,src:F,srcOff:H.offset*E,bytes:U}):w.copyBufferToBuffer(i[G],0,F,H.offset*E,U)}v.push({binding:B.index,resource:{buffer:F}})}else v.push({binding:B.index,resource:{buffer:i[T[N++]]}});let k=r.createBindGroup({layout:S,entries:v}),O=w.beginComputePass();O.setPipeline(x),O.setBindGroup(0,k);let R=b.kernel.metadata.dispatchSize;O.dispatchWorkgroups(R[0],R[1],R[2]),O.end();for(let B of A)w.copyBufferToBuffer(B.src,B.srcOff,i[B.slot],0,B.bytes);d()},"encodeStep"),m=n.scanLoops?[...n.scanLoops].sort((b,w)=>b.loopStart-w.loopStart):n.scanLoop?[n.scanLoop]:null;if(m&&m.length){let b=0;for(let w of m){for(;b<w.loopStart;b++)h(t[b]);for(let x of w.carry)f.encoder.copyBufferToBuffer(i[x.initSlot],0,i[x.a],0,x.bytes);for(let x=0;x<w.T;x++){for(let S of w.xs)f.encoder.copyBufferToBuffer(i[S.xsSlot],x*S.stepBytes,i[S.xtSlot],0,S.stepBytes);for(let S=w.loopStart;S<w.loopEnd;S++)h(t[S]);for(let S of w.ys)f.encoder.copyBufferToBuffer(i[S.ytSlot],0,i[S.ysSlot],x*S.stepBytes,S.stepBytes);for(let S of w.carry){let T=i[S.a];i[S.a]=i[S.b],i[S.b]=T}}for(let x of w.carry)f.encoder.copyBufferToBuffer(i[x.a],0,i[x.finalSlot],0,x.bytes);b=w.loopEnd}for(;b<t.length;b++)h(t[b])}else for(let b of t)h(b);let g=f.encoder,y=new Set(n.argSlots),_=[];for(let b=0;b<n.numSlots;b++){if(!i[b]||!o.has(b)||!y.has(b))continue;let w=e[b],x=Math.max(Eo(w.data.length*Vs(a[b])),4),S=r.createBuffer({size:x,usage:s.MAP_READ|s.COPY_DST});g.copyBufferToBuffer(i[b],0,S,0,x),_.push({rb:S,dtype:a[b],size:w.data.length,dst:w.data})}r.queue.submit([g.finish()]);for(let b of _)await b.rb.mapAsync(gx()),hx(b.dst,new(Vl(b.dtype))(b.rb.getMappedRange()),b.dtype,0,b.size),b.rb.unmap(),b.rb.destroy();for(let b of i)b&&b.destroy();for(let b of p)b.destroy();for(let b of c)b.destroy()}function KR(n){let e=dx.get(n);if(e&&e.length)return e.pop();let t=Gl();return It.createBuffer({size:n,usage:t.STORAGE|t.COPY_SRC|t.COPY_DST})}function $R(n,e){let t=dx.get(e);t||(t=[],dx.set(e,t)),t.push(n)}async function s3(){return await mx(),Qs||(Qs=It.createCommandEncoder()),It}function o3(){return!!It&&!!Qs}function FR(n,e,t){let r=df.get(n);if(r)return r.buf;let s=Gl(),o=Math.max(Eo(n.length*Vs(e)),4);if(t){let a=It.createBuffer({size:o,usage:s.STORAGE|s.COPY_SRC|s.COPY_DST,mappedAtCreation:!0});return ly(new(Vl(e))(a.getMappedRange()),n,e,0),a.unmap(),df.set(n,{buf:a,bytes:o,dtype:e,persistent:t}),a}let i=KR(o);return df.set(n,{buf:i,bytes:o,dtype:e,persistent:!1}),i}function hf(n,e,t){let{pipeline:r,bindGroupLayout:s}=UR(It,n),{bindings:o,dispatchSize:i}=n.metadata,a=qR(o),u=Gl(),p=[],c=[];for(let h of o){if(h.name==="_shapes"){let y=new Uint32Array(t||[]),_=Math.max(Math.ceil(y.byteLength/16)*16,16),b=It.createBuffer({size:_,usage:u.UNIFORM|u.COPY_DST});It.queue.writeBuffer(b,0,y),px.push(b),p.push({binding:h.index,resource:{buffer:b}});continue}if(h.packed){let y=Vs(h.dtype),_=Math.max(Eo(h.packedSize*y),4),b=KR(_);fx.push({buf:b,bytes:_});let w=h.mode==="read_write";for(let x of h.packed){let S=FR(e[a.get(x.name)],x.dtype,!w),T=Eo(x.size*y);w?c.push({pbuf:b,off:x.offset*y,sub:S,bytes:T}):Qs.copyBufferToBuffer(S,0,b,x.offset*y,T)}p.push({binding:h.index,resource:{buffer:b}});continue}let m=e[a.get(h.name)],g=FR(m,h.dtype,h.mode!=="read_write");p.push({binding:h.index,resource:{buffer:g}})}let f=It.createBindGroup({layout:s,entries:p}),d=Qs.beginComputePass();d.setPipeline(r),d.setBindGroup(0,f),d.dispatchWorkgroups(i[0],i[1],i[2]),d.end();for(let h of c)Qs.copyBufferToBuffer(h.pbuf,h.off,h.sub,0,h.bytes);++cx>=r3&&(It.queue.submit([Qs.finish()]),Qs=It.createCommandEncoder(),cx=0)}async function HR(){if(!It)return;let n=Gl(),e=[];for(let[t,r]of df){if(r.persistent)continue;let s=It.createBuffer({size:r.bytes,usage:n.MAP_READ|n.COPY_DST});Qs.copyBufferToBuffer(r.buf,0,s,0,r.bytes),e.push({rb:s,ha:t,buf:r.buf,bytes:r.bytes,dtype:r.dtype})}It.queue.submit([Qs.finish()]),Qs=It.createCommandEncoder(),cx=0;for(let t of px)t.destroy();px.length=0,await Promise.all(e.map(t=>t.rb.mapAsync(gx())));for(let t of e)hx(t.ha,new(Vl(t.dtype))(t.rb.getMappedRange()),t.dtype,0,t.ha.length),t.rb.unmap(),t.rb.destroy();for(let t of fx)$R(t.buf,t.bytes);fx.length=0;for(let t of e)$R(t.buf,t.bytes),df.delete(t.ha)}function i3(n,e,t,r,s){let o=`${n.join(",")}|${e.join(",")}|${t}|${r}`,i=zR.get(o);if(i)return i;let a=jn(r),u="";for(let c=n.length-1;c>=0;c--)u+=` let idx${c} = rem % ${n[c]}u; rem = rem / ${n[c]}u; src = src + idx${c} * ${e[c]}u;
|
|
157
|
+
`;return i={name:"contig",source:`@group(0) @binding(0) var<storage, read> inp : array<${a}>;
|
|
158
|
+
@group(0) @binding(1) var<storage, read_write> outp : array<${a}>;
|
|
159
|
+
@compute @workgroup_size(64)
|
|
160
|
+
fn contig(@builtin(global_invocation_id) gid : vec3<u32>) {
|
|
161
|
+
let i = gid.x;
|
|
162
|
+
if (i >= ${s}u) { return; }
|
|
163
|
+
var rem = i;
|
|
164
|
+
var src = ${t}u;
|
|
165
|
+
`+u+` outp[i] = inp[src];
|
|
166
|
+
}`,metadata:{bindings:[{index:0,name:"inp",dtype:r,mode:"read"},{index:1,name:"outp",dtype:r,mode:"read_write"}],dispatchSize:[Math.ceil(s/64),1,1]}},zR.set(o,i),i}function zl(n){let e=n._impl.storage.rawData;if(n.isContiguous&&n._impl.storageOffset===0&&e.length===n.numel)return e;let t=new e.constructor(n.numel);return hf(i3(n.shape,n.strides,n._impl.storageOffset,n.dtype,n.numel),[e,t],void 0),t}function a3(n,e,t){let r=e.map(zl);r.push(t),hf(n,r,void 0)}function u3(n,e,t){let r=`lstm|${n}|${e}|${t}`,s=py.get(r);if(s)return s;let o=4*e,i=o*n,a=o*n+o*e,u=a+o;return s={name:"rnn",source:`fn sig(x: f32) -> f32 { return 1.0 / (1.0 + exp(-x)); }
|
|
167
|
+
@group(0) @binding(0) var<storage, read> xin : array<f32>;
|
|
168
|
+
@group(0) @binding(1) var<storage, read> w : array<f32>;
|
|
169
|
+
@group(0) @binding(2) var<storage, read> h0 : array<f32>;
|
|
170
|
+
@group(0) @binding(3) var<storage, read> c0 : array<f32>;
|
|
171
|
+
@group(0) @binding(4) var<storage, read_write> ys : array<f32>;
|
|
172
|
+
@group(0) @binding(5) var<storage, read_write> hn : array<f32>;
|
|
173
|
+
@group(0) @binding(6) var<storage, read_write> cn : array<f32>;
|
|
174
|
+
var<workgroup> wh : array<f32, ${e}>;
|
|
175
|
+
var<workgroup> wc : array<f32, ${e}>;
|
|
176
|
+
var<workgroup> wg : array<f32, ${o}>;
|
|
177
|
+
@compute @workgroup_size(${Io})
|
|
178
|
+
fn rnn(@builtin(local_invocation_id) lid : vec3<u32>) {
|
|
179
|
+
let tid = lid.x;
|
|
180
|
+
for (var p = tid; p < ${e}u; p = p + ${Io}u) { wh[p] = h0[p]; wc[p] = c0[p]; }
|
|
181
|
+
workgroupBarrier();
|
|
182
|
+
for (var t = 0u; t < ${t}u; t = t + 1u) {
|
|
183
|
+
for (var j = tid; j < ${o}u; j = j + ${Io}u) {
|
|
184
|
+
var acc = w[${a}u + j] + w[${u}u + j];
|
|
185
|
+
let xb = t * ${n}u;
|
|
186
|
+
let wxb = j * ${n}u;
|
|
187
|
+
for (var e = 0u; e < ${n}u; e = e + 1u) { acc = acc + xin[xb + e] * w[wxb + e]; }
|
|
188
|
+
let whb = ${i}u + j * ${e}u;
|
|
189
|
+
for (var q = 0u; q < ${e}u; q = q + 1u) { acc = acc + wh[q] * w[whb + q]; }
|
|
190
|
+
wg[j] = acc;
|
|
191
|
+
}
|
|
192
|
+
workgroupBarrier();
|
|
193
|
+
for (var j = tid; j < ${e}u; j = j + ${Io}u) {
|
|
194
|
+
let ii = sig(wg[j]);
|
|
195
|
+
let ff = sig(wg[${e}u + j]);
|
|
196
|
+
let gg = tanh(wg[${2*e}u + j]);
|
|
197
|
+
let oo = sig(wg[${3*e}u + j]);
|
|
198
|
+
let cv = ff * wc[j] + ii * gg;
|
|
199
|
+
wc[j] = cv;
|
|
200
|
+
let nh = oo * tanh(cv);
|
|
201
|
+
wh[j] = nh;
|
|
202
|
+
ys[t * ${e}u + j] = nh;
|
|
203
|
+
}
|
|
204
|
+
workgroupBarrier();
|
|
205
|
+
}
|
|
206
|
+
for (var p = tid; p < ${e}u; p = p + ${Io}u) { hn[p] = wh[p]; cn[p] = wc[p]; }
|
|
207
|
+
}`,metadata:{bindings:[{index:0,name:"xin",dtype:"f32",mode:"read"},{index:1,name:"w",dtype:"f32",mode:"read"},{index:2,name:"h0",dtype:"f32",mode:"read"},{index:3,name:"c0",dtype:"f32",mode:"read"},{index:4,name:"ys",dtype:"f32",mode:"read_write"},{index:5,name:"hn",dtype:"f32",mode:"read_write"},{index:6,name:"cn",dtype:"f32",mode:"read_write"}],dispatchSize:[1,1,1]}},py.set(r,s),s}function YR(n,e,t,r){let s=`${e}|${t}|${r}`;if(n._webgpuPacked&&n._webgpuPackedTag===s)return n._webgpuPacked;let o=l(f=>f.contiguous()._impl.storage.rawData,"raw"),i=o(n.x2h.weight),a=o(n.h2h.weight),u=n.x2h.bias?o(n.x2h.bias):new Float32Array(r),p=n.h2h.bias?o(n.h2h.bias):new Float32Array(r),c=new Float32Array(r*e+r*t+r+r);return c.set(i,0),c.set(a,r*e),c.set(u,r*e+r*t),c.set(p,r*e+r*t+r),n._webgpuPacked=c,n._webgpuPackedTag=s,c}function l3(n,e,t){return YR(n,e,t,4*t)}function c3(n,e,t){let r=`gru|${n}|${e}|${t}`,s=py.get(r);if(s)return s;let o=3*e,i=o*n,a=o*n+o*e,u=a+o;return s={name:"rnn",source:`fn sig(x: f32) -> f32 { return 1.0 / (1.0 + exp(-x)); }
|
|
208
|
+
@group(0) @binding(0) var<storage, read> xin : array<f32>;
|
|
209
|
+
@group(0) @binding(1) var<storage, read> w : array<f32>;
|
|
210
|
+
@group(0) @binding(2) var<storage, read> h0 : array<f32>;
|
|
211
|
+
@group(0) @binding(3) var<storage, read_write> ys : array<f32>;
|
|
212
|
+
@group(0) @binding(4) var<storage, read_write> hn : array<f32>;
|
|
213
|
+
var<workgroup> wh : array<f32, ${e}>;
|
|
214
|
+
var<workgroup> wgx : array<f32, ${o}>;
|
|
215
|
+
var<workgroup> wgh : array<f32, ${o}>;
|
|
216
|
+
@compute @workgroup_size(${Io})
|
|
217
|
+
fn rnn(@builtin(local_invocation_id) lid : vec3<u32>) {
|
|
218
|
+
let tid = lid.x;
|
|
219
|
+
for (var p = tid; p < ${e}u; p = p + ${Io}u) { wh[p] = h0[p]; }
|
|
220
|
+
workgroupBarrier();
|
|
221
|
+
for (var t = 0u; t < ${t}u; t = t + 1u) {
|
|
222
|
+
for (var j = tid; j < ${o}u; j = j + ${Io}u) {
|
|
223
|
+
var ax = w[${a}u + j];
|
|
224
|
+
var ah = w[${u}u + j];
|
|
225
|
+
let xb = t * ${n}u;
|
|
226
|
+
let wxb = j * ${n}u;
|
|
227
|
+
for (var e = 0u; e < ${n}u; e = e + 1u) { ax = ax + xin[xb + e] * w[wxb + e]; }
|
|
228
|
+
let whb = ${i}u + j * ${e}u;
|
|
229
|
+
for (var q = 0u; q < ${e}u; q = q + 1u) { ah = ah + wh[q] * w[whb + q]; }
|
|
230
|
+
wgx[j] = ax;
|
|
231
|
+
wgh[j] = ah;
|
|
232
|
+
}
|
|
233
|
+
workgroupBarrier();
|
|
234
|
+
for (var j = tid; j < ${e}u; j = j + ${Io}u) {
|
|
235
|
+
let rr = sig(wgx[j] + wgh[j]);
|
|
236
|
+
let zz = sig(wgx[${e}u + j] + wgh[${e}u + j]);
|
|
237
|
+
let nn = tanh(wgx[${2*e}u + j] + rr * wgh[${2*e}u + j]);
|
|
238
|
+
let nh = (1.0 - zz) * nn + zz * wh[j];
|
|
239
|
+
wh[j] = nh;
|
|
240
|
+
ys[t * ${e}u + j] = nh;
|
|
241
|
+
}
|
|
242
|
+
workgroupBarrier();
|
|
243
|
+
}
|
|
244
|
+
for (var p = tid; p < ${e}u; p = p + ${Io}u) { hn[p] = wh[p]; }
|
|
245
|
+
}`,metadata:{bindings:[{index:0,name:"xin",dtype:"f32",mode:"read"},{index:1,name:"w",dtype:"f32",mode:"read"},{index:2,name:"h0",dtype:"f32",mode:"read"},{index:3,name:"ys",dtype:"f32",mode:"read_write"},{index:4,name:"hn",dtype:"f32",mode:"read_write"}],dispatchSize:[1,1,1]}},py.set(r,s),s}function p3(n,e,t,r,s){if(t.kind==="gru")return f3(n,e,t,r);if(t.batch!==1)return null;let o=t.hiddenSize,i=t.seqLen,a=e.length,u=zl(n),p=[],c=[];for(let h=0;h<a;h++){let m=h===0?t.inputSize:o,g=l3(e[h],m,o),y,_;r!=null&&s!=null?(y=zl(en(r,0,h)),_=zl(en(s,0,h))):(y=new Float32Array(o),_=new Float32Array(o));let b=new Float32Array(i*o),w=new Float32Array(o),x=new Float32Array(o);hf(u3(m,o,i),[u,g,y,_,b,w,x],void 0),u=b,p.push(w),c.push(x)}let f=Ee(u,[i,1,o],"f32",xo),d=l(h=>a===1?Ee(h[0],[1,1,o],"f32",xo):us(h.map(m=>Ee(m,[1,o],"f32",xo)),0),"wrapState");return[f,d(p),d(c)]}function f3(n,e,t,r){if(t.batch!==1)return null;let s=t.hiddenSize,o=t.seqLen,i=e.length,a=zl(n),u=[];for(let f=0;f<i;f++){let d=f===0?t.inputSize:s,h=YR(e[f],d,s,3*s),m=r!=null?zl(en(r,0,f)):new Float32Array(s),g=new Float32Array(o*s),y=new Float32Array(s);hf(c3(d,s,o),[a,h,m,g,y],void 0),a=g,u.push(y)}let p=Ee(a,[o,1,s],"f32",xo),c=i===1?Ee(u[0],[1,1,s],"f32",xo):us(u.map(f=>Ee(f,[1,s],"f32",xo)),0);return[p,c]}var It,pf,ff,ay,uy,VR,PR,cy,Qs,cx,r3,df,px,fx,dx,zR,Io,py,yx=M(()=>{"use strict";tr();bl();wn();qn();be();be();It=null,pf=null,ff=null,ay=null,uy=null,VR=null,PR=!1;l(Vl,"wgslViewCtor");l(ly,"packTensorInto");l(hx,"unpackTensorFrom");l(Eo,"align4");l(mx,"ensureDevice");l(Gl,"bufUsage");l(gx,"mapModeRead");l(Qz,"resetDevice");l(Xz,"bindingBufferType");l(GR,"pipelineParts");l(jR,"createPipeline");cy=new WeakMap;l(UR,"pipelineFor");l(e3,"instantiateWebGPU");l(qR,"buildParamIndex");l(t3,"runWebGPUKernel");l(WR,"prewarmPipelines");l(n3,"runWebGPUPlan");Qs=null,cx=0,r3=64,df=new Map,px=[],fx=[],dx=new Map;l(KR,"acquireStorage");l($R,"releaseStorage");l(s3,"ensureWebGPUEager");l(o3,"webgpuEagerReady");l(FR,"eagerBufferFor");l(hf,"recordWebGPUEager");l(HR,"flushWebGPUEager");zR=new Map;l(i3,"contigKernel");l(zl,"webgpuEagerInput");l(a3,"webgpuEagerOp");Io=256,py=new Map;l(u3,"lstmKernelWGSL");l(YR,"rnnPackedWeights");l(l3,"lstmPackedWeights");l(c3,"gruKernelWGSL");l(p3,"webgpuRNN");l(f3,"webgpuGRU")});var vx={};at(vx,{getBackend:()=>Sx,hasBackend:()=>d3,preloadCudaRuntime:()=>ZR,preloadWebGPU:()=>gf,registerBackend:()=>yf});function yf(n,e){xx.set(n,e)}function Sx(n){return xx.get(n)||null}function d3(n){return xx.has(n)}function h3(){return _x||(_x=Promise.resolve().then(()=>(V1(),z1))),_x}function Tx(){return bx||(bx=Promise.resolve().then(()=>(iy(),oy))),bx}async function ZR(){if(mf)return mf;let n=await Tx();return n.preloadCublas&&await n.preloadCublas(),mf=n,n}async function gf(){if(wx)return wx;let n=await Promise.resolve().then(()=>(yx(),JR)),{setWebGPUEagerFn:e,setWebgpuRNN:t}=await Promise.resolve().then(()=>(wn(),XR));return await n.ensureWebGPUEager(),e(n.webgpuEagerOp),t(n.webgpuRNN),wx=n,n}function QR(n){let e;try{e=Sl(n.source)}catch(o){throw new Error("encodeWat: "+o.message+`
|
|
246
|
+
`+n.source)}let t;try{t=new WebAssembly.Module(e)}catch(o){throw new Error("WASM: "+o.message+`
|
|
247
|
+
`+n.source)}let r={};if(n.metadata.imports)for(let[o]of n.metadata.imports)r[o]=m3[o]||Math[o]||(i=>i);let s=new WebAssembly.Instance(t,{math:r});return{exports:s.exports,memory:s.exports.memory,bufferOffsets:n.metadata.bufferOffsets,funcName:n.name,binary:e,parallel:n.metadata.parallel||null,mathNames:n.metadata.imports?[...n.metadata.imports.keys()]:[]}}function fy(n,e,t,r,s){let{exports:o,memory:i,bufferOffsets:a,funcName:u}=n,p=o[u],c=[...a.values()],f=Math.min(c.length,e.length);for(let h=0;h<f;h++){let m=e[h];ArrayBuffer.isView(m)&&new m.constructor(i.buffer,c[h],m.length).set(m)}let d=c.slice(0,f);if(t)for(let h of t)d.push(h);r!==void 0&&s!==void 0&&d.push(r,s),p(...d);for(let h=0;h<f;h++){let m=e[h];ArrayBuffer.isView(m)&&m.set(new m.constructor(i.buffer,c[h],m.length))}}function g3(n,e,t=[],r={}){let s=r.warmup??5,o=r.repeat??30,i=QR(n),a=e.map(f=>new Float32Array(Math.max(1,Math.ceil(f/4)))),u=t||[],p=l(()=>{i.parallel?fy(i,a,u,0,i.parallel.extent):fy(i,a,u)},"once");for(let f=0;f<s;f++)p();let c=[];for(let f=0;f<o;f++){let d=performance.now();p(),c.push(performance.now()-d)}return c}var xx,_x,bx,mf,wx,m3,_f=M(()=>{"use strict";Wm();Yi();Ym();xx=new Map;l(yf,"registerBackend");l(Sx,"getBackend");l(d3,"hasBackend");_x=null;l(h3,"getWasmPool");bx=null;l(Tx,"getCudaRuntime");mf=null;l(ZR,"preloadCudaRuntime");wx=null;l(gf,"preloadWebGPU");m3={exp:Math.exp,log:Math.log,sin:Math.sin,cos:Math.cos,tan:Math.tan,tanh:Math.tanh,pow:Math.pow,fmod:l((n,e)=>n%e,"fmod"),rsqrt:l(n=>1/Math.sqrt(n),"rsqrt"),sign:Math.sign,round:Math.round,erf:Wi,erfc:Ki,lgamma:cn,gamma:Hi,digamma:ja};l(QR,"instantiateWasm");l(fy,"runWasmKernel");yf("js",{instantiate(n){return new Function("return "+n.source)()},runSync(n,e,t){let r=t?[...e,...t]:e;return n(...r)},runAsync(n,e,t){let r=t?[...e,...t]:e;return n(...r)},isAsync(){return!1}});yf("wasm",{instantiate(n){return QR(n)},runSync(n,e,t){n.parallel?fy(n,e,t,0,n.parallel.extent):fy(n,e,t)},async runAsync(n,e,t){if(n.parallel&&n.parallel.poolSafe){let{runWasmParallel:r}=await h3();await r(n,n.funcName,e,t,n.parallel,n.mathNames);return}this.runSync(n,e,t)},isAsync(n){return!!(n&&n.parallel&&n.parallel.poolSafe)}});l(g3,"measureWasm");Hm("wasm",g3);yf("webgpu",{instantiate(n){return gf().then(e=>e.instantiateWebGPU(n))},runSync(){throw new Error("WebGPU kernel requires async execution \u2014 use runAsync()")},async runAsync(n,e,t){let{runWebGPUKernel:r}=await gf();await r(n,e,t)},async runPlan(n,e,t){let{runWebGPUPlan:r}=await gf();await r(n,e,t)},isAsync(){return!0}});yf("cuda",{instantiate(n){return{kernel:n}},runSync(n,e,t){if(!mf)throw new Error("CUDA sync runtime not preloaded \u2014 call preloadCudaRuntime() before synchronous execution");mf.runCudaKernelResident(n.kernel,e,t)},async runAsync(n,e,t){let{runCudaKernel:r}=await Tx();await r(n.kernel,e,t)},async runPlan(n,e,t,r){let{runCudaPlan:s}=await Tx();await s(n,e,t,r)},isAsync(){return!0}})});function kx(n){return y3[n]||Float32Array}function _3(n){return n instanceof Float64Array?"f64":n instanceof Int32Array?"i32":n instanceof Int16Array?"i16":n instanceof Int8Array?"i8":n instanceof Uint16Array?"ui16":n instanceof Uint8Array?"ui8":n instanceof BigInt64Array?"i64":"f32"}function eM(n,e){if(!(!n.returnFixups||n.returnFixups.length===0))for(let t of n.returnFixups){let r=e[n.argSlots[t.pos]];if(!(!r||!r.data))if(t.kind==="copy"){let s=e[t.srcSlot];s&&s.data&&r.data.set(s.data.subarray(0,r.data.length))}else t.kind==="const"&&r.data.fill(t.value)}}var y3,Lo,Nx,jl,dy=M(()=>{"use strict";_f();Ep();y3={f16:Uint16Array,bf16:Uint16Array,f32:Float32Array,f64:Float64Array,i8:Int8Array,i16:Int16Array,i32:Int32Array,i64:BigInt64Array,ui8:Uint8Array,bool:Uint8Array,index:Int32Array};l(kx,"typedArrayCtor");l(_3,"dtypeOfTypedArray");l(eM,"_applyReturnFixups");Lo=class n{static{l(this,"RuntimeTensor")}data;shape;dtype;strides;resident;constructor(e,t,r,s=null){this.data=e,this.shape=[...t],this.dtype=r,this.strides=s?[...s]:n.defaultStrides(t)}static defaultStrides(e){let t=new Array(e.length),r=1;for(let s=e.length-1;s>=0;s--)t[s]=r,r*=e[s];return t}get numel(){let e=1;for(let t=0;t<this.shape.length;t++)e*=this.shape[t];return e}get rank(){return this.shape.length}static zeros(e,t="f32"){let r=1;for(let s=0;s<e.length;s++)r*=e[s];return r=Math.max(r,1),new n(new(kx(t))(r),e,t)}static fromArray(e,t,r="f32"){let s=kx(r);return new n(new s(e),t,r)}get(e){let t=0;for(let r=0;r<e.length;r++)t+=e[r]*this.strides[r];return this.data[t]}set(e,t){let r=0;for(let s=0;s<e.length;s++)r+=e[s]*this.strides[s];this.data[r]=t}},Nx=class{static{l(this,"KernelRegistry")}_kernels;constructor(){this._kernels=new Map}register(e,t){this._kernels.set(e,t)}get(e){return this._kernels.get(e)||null}has(e){return this._kernels.has(e)}names(){return[...this._kernels.keys()]}},jl=class n{static{l(this,"RuntimeModule")}name;kernels;_instances;_shapeParamMaps;_bufferMaps;constructor(e){this.name=e,this.kernels=new Nx,this._instances=new Map}addCompiledKernel(e){this.kernels.register(e.name,e);let t=Sx(e.metadata.kind);if(!t)throw new Error("No runtime backend registered for kind: "+e.metadata.kind);this._instances.set(e.name,{backend:t,instance:t.instantiate(e)})}setShapeParamMap(e,t,r){this._shapeParamMaps||(this._shapeParamMaps=new Map),this._shapeParamMaps.set(e,t),r&&(this._bufferMaps||(this._bufferMaps=new Map),this._bufferMaps.set(e,r))}_prepareArgs(e,t){let r=[],s=new Map;for(let a=0;a<t.length;a++){let u=t[a];u instanceof Lo?(r.push(u.data),s.set(a,u.shape)):r.push(u)}let o=this._shapeParamMaps&&this._shapeParamMaps.get(e),i=null;if(o&&o.size>0){let a=this._bufferMaps&&this._bufferMaps.get(e);i=n._extractShapeParams(o,s,t,a)}return{tensorArgs:r,shapeValues:i}}run(e,...t){let r=this._instances.get(e);if(!r)throw new Error("Kernel '"+e+"' not found or not executable");if(r.instance instanceof Promise)throw new Error("Kernel '"+e+"' requires async execution \u2014 use runAsync()");let{tensorArgs:s,shapeValues:o}=this._prepareArgs(e,t);return r.backend.runSync(r.instance,s,o)}async runAsync(e,...t){let r=this._instances.get(e);if(!r)throw new Error("Kernel '"+e+"' not found or not executable");let{tensorArgs:s,shapeValues:o}=this._prepareArgs(e,t),i=await r.instance;return r.backend.runAsync(i,s,o)}isAsync(e){let t=this._instances.get(e);if(!t)return!1;let r=t.instance instanceof Promise?null:t.instance;return t.backend.isAsync(r)}async runPlanAsync(e,t,r){let s=new Array(e.numSlots).fill(null);for(let a=0;a<t.length;a++){let u=t[a];s[e.argSlots[a]]=u instanceof Lo?u:new Lo(u,[u.length],_3(u))}for(let a of e.intermediates){let u=1;for(let p of a.shape)u*=p;s[a.slot]=new Lo(new(kx(a.dtype))(Math.max(u,1)),a.shape,a.dtype)}for(let a of e.steps){let u=this._instances.get(a.name);u&&u.instance instanceof Promise&&(u.instance=await u.instance)}let o=this._uniformPlanBackend(e),i=e.steps.some(a=>{let u=this.kernels.get(a.name);return u&&u.metadata&&u.metadata.scratch&&u.metadata.scratch.length>0});if(o&&o.runPlan&&!i){let a=e.steps.map(u=>{let p=[];for(let f of u.inputSlots)p.push(s[f]);for(let f of u.outputSlots)p.push(s[f]);let{shapeValues:c}=this._prepareArgs(u.name,p);return{name:u.name,inputSlots:u.inputSlots,outputSlots:u.outputSlots,kernel:this.kernels.get(u.name),shapeValues:c}});await o.runPlan(e,s,a,r),eM(e,s);return}for(let a of e.steps){let u=[];for(let p of a.inputSlots)u.push(s[p]);for(let p of a.outputSlots)u.push(s[p]);await this.runAsync(a.name,...u)}eM(e,s)}_uniformPlanBackend(e){let t=null;for(let r of e.steps){let s=this._instances.get(r.name);if(!s||s.instance instanceof Promise)return null;if(t===null)t=s.backend;else if(s.backend!==t)return null}return t}static _extractShapeParams(e,t,r,s){let o=new Map;if(s){let u=0;for(let[p,c]of s){let f=typeof p=="string"?p:c&&c.name;typeof f=="string"&&o.set(f,u),u++}}let i=new Map,a=[];for(let[u,p]of e){if(i.has(p.name))continue;i.set(p.name,!0);let c=u.lastIndexOf(":"),f=u.substring(0,c),d=parseInt(u.substring(c+1),10),h=null;if(o.has(f)){let m=t.get(o.get(f));m&&d<m.length&&m[d]>0&&(h=m[d])}if(h===null){for(let[,m]of t)if(d<m.length&&m[d]>0){h=m[d];break}}a.push(h!==null?h:1)}return a}getKernelSource(e){let t=this.kernels.get(e);return t?t.source:null}getKernelSnippet(e){let t=this.kernels.get(e);return t?t.snippet():null}listKernels(){return this.kernels.names()}serialize(){let e=[];for(let t of this.kernels.names()){let r=this.kernels.get(t);e.push({name:r.name,source:r.source,target:r.target.name,metadata:r.metadata})}return{name:this.name,kernels:e}}static deserialize(e){let t=new n(e.name);for(let r of e.kernels)t.addCompiledKernel(new Ip(r.name,r.source,{name:r.target},r.metadata));return t}}});var hy,tM=M(()=>{"use strict";hy=class{static{l(this,"AnalysisManager")}constructor(){this._cache=new WeakMap}getAnalysis(e,t){let r=this._cache.get(t);r||(r=new Map,this._cache.set(t,r));let s=r.get(e);if(!s||s.version!==t.version){let o=this._resolveDeps(e,t,r);s={data:e.compute(t,o),version:t.version},r.set(e,s)}return s.data}_resolveDeps(e,t,r){let s=e.dependencies;if(!s||s.length===0)return{};let o={};for(let i of s){let a=i.depKey||i.name;o[a]=this.getAnalysis(i,t)}return o}invalidate(e,t=null){let r=this._cache.get(e);if(!r)return;if(!t){this._cache.delete(e);return}let s=l(u=>t.has(u)||t.has(u.name),"isPreserved"),o=new Map,i=l(u=>{let p=o.get(u);if(p!==void 0)return p;let c=u.dependencies,f=!1;if(c){for(let d of c)if(!s(d)||i(d)){f=!0;break}}return o.set(u,f),f},"isStale"),a=[];for(let u of r.keys())if(!s(u)||i(u))a.push(u);else{let p=r.get(u);p&&(p.version=e.version)}for(let u of a)r.delete(u)}invalidateFunctions(e,t=null){for(let r of e)this.invalidate(r,t)}invalidateAll(){this._cache=new WeakMap}}});var oe,my,gy,qe,yy,fn=M(()=>{"use strict";oe=Object.freeze({UNCHANGED:0,CHANGED:1,FAILED:2}),my=class{static{l(this,"Pass")}constructor(e){this.name=e,this.preservedAnalyses=new Set,this.invalidatedAnalyses=new Set,this.requiredAnalyses=[],this.optLevel=0,this.trace=null}run(e,t){throw new Error("Not implemented")}},gy=class{static{l(this,"PassContext")}constructor({optLevel:e=1/0,disabledPasses:t=[],requiredPasses:r=[],config:s={}}={}){this.optLevel=e,this.disabledPasses=t instanceof Set?t:new Set(t),this.requiredPasses=r instanceof Set?r:new Set(r),this.config=s instanceof Map?s:new Map(Object.entries(s))}shouldRun(e){return this.disabledPasses.has(e.name)?!1:this.requiredPasses.has(e.name)?!0:!((e.optLevel||0)>this.optLevel)}},qe=class extends my{static{l(this,"FunctionPass")}run(e,t){throw new Error("Not implemented")}},yy=class extends my{static{l(this,"ModulePass")}run(e,t){throw new Error("Not implemented")}}});var le,Yn,b3,_y,nn=M(()=>{"use strict";fn();le=Object.freeze({SILENT:0,INFO:1,VERBOSE:2,DEBUG:3}),Yn=class{static{l(this,"CompilationError")}constructor(e,t,r,s){this.phase=e,this.funcName=t,this.message=r,this.passName=s||null}toString(){let e="["+this.phase+"]";return this.funcName&&(e+=" "+this.funcName),this.passName&&(e+=" ("+this.passName+")"),e+=": "+this.message,e}},b3=l(()=>{},"NOOP"),_y=class{static{l(this,"TraceLog")}constructor(e={}){this.level=e.level??le.SILENT,this.sink=typeof e.sink=="function"?e.sink:b3,this.irSnapshot={afterGraphPasses:!1,afterLowering:!1,afterScheduling:!1,...e.irSnapshot||{}},this._compileStart=0}emit(e){e.level>this.level||(e.timestamp=performance.now(),this.sink(e))}phaseStart(e){this.emit({type:"phase",action:"start",phase:e,level:le.INFO})}phaseEnd(e,t){this.emit({type:"phase",action:"end",phase:e,durationMs:t,level:le.INFO})}passRun(e,t,r,s,o){this.emit({type:"pass",passName:e,changed:t===oe.CHANGED,durationMs:r,opCountBefore:s,opCountAfter:o,level:le.VERBOSE})}functionEvent(e,t,r){this.emit({type:"function",phase:e,funcName:t,...r,level:le.INFO})}irDump(e,t){this.emit({type:"ir_snapshot",label:e,text:t,level:le.DEBUG})}memoryStats(e,t){this.emit({type:"memory",funcName:e,...t,level:le.VERBOSE})}autotuneStats(e,t){this.emit({type:"autotune",funcName:e,...t,level:le.VERBOSE})}codegenStats(e,t){this.emit({type:"codegen",funcName:e,...t,level:le.VERBOSE})}errorEvent(e,t,r,s){this.emit({type:"error",phase:e,funcName:t,message:r,passName:s||null,level:le.INFO})}warn(e,t,r,s){this.emit({type:"warning",phase:e,funcName:t,message:r,detail:s||null,level:le.INFO})}explain(e,t,r,s,o){this.emit({type:"explain",category:e,subject:t,decision:r,reason:s,...o||{},level:le.DEBUG})}get explainsEnabled(){return this.level>=le.DEBUG}shouldSnapshot(e){return this.level>=le.DEBUG&&!!this.irSnapshot[e]}}});function bf(n){if(typeof n.numOps=="function")return n.numOps();if(typeof n[Symbol.iterator]=="function"){let e=0;for(let t of n)typeof t.numOps=="function"&&(e+=t.numOps());return e}return-1}var Ul,iu,by=M(()=>{"use strict";tM();fn();nn();nn();l(bf,"countOps");Ul=class{static{l(this,"FixedPointGroup")}constructor(e,t,r=8){this.name=e,this.passes=t,this.maxIterations=r}},iu=class{static{l(this,"PassManager")}constructor(){this.passes=[],this.analysisManager=new hy,this.trace=null,this.verifyHook=null,this.instruments=[]}addPass(e){this.passes.push(e)}addInstrument(e){this.instruments.push(e)}_notify(e,t,r,s){for(let o of this.instruments)typeof o[e]=="function"&&o[e](t,r,s)}setTrace(e){this.trace=e}setVerifyHook(e){this.verifyHook=e}_verifyAfter(e,t,r){if(!this.verifyHook)return null;let s=this.verifyHook(t,r);if(!s||s.length===0)return null;let o=r?t.name||"<module>":t.name;return new Yn("verification",o,`pass '${e.name}' produced invalid IR: ${s.join("; ")}`,e.name)}_applyPass(e,t,r,s){this.trace&&(e.trace=this.trace),this.instruments.length&&this._notify("runBeforePass",e,t,null);let o=r.verbose,i=r.resilient,a=!1,u=!1;if(e instanceof yy){let p=o?bf(t):-1,c=o?performance.now():0,f;try{f=e.run(t,this.analysisManager)}catch(d){if(!i)throw d;return this.analysisManager.invalidateAll(),s.push(oe.FAILED),r.errors.push(new Yn("graphPasses",t.name||"<module>",d.message,e.name)),{changed:a,fatal:!1}}if(s.push(f),o&&this.trace.passRun(e.name,f,performance.now()-c,p,bf(t)),f===oe.CHANGED){a=!0,r.anyChanged=!0,this.analysisManager.invalidateFunctions(t,e.preservedAnalyses);let d=this._verifyAfter(e,t,!0);d&&(r.errors.push(d),i||(u=!0))}else f===oe.FAILED&&(this.analysisManager.invalidateAll(),r.errors.push(new Yn("graphPasses",t.name||"<module>",`pass '${e.name}' failed`,e.name)),i||(u=!0))}else if(e instanceof qe){let p=!1;for(let c of t){if(r.failedFunctions.has(c.name))continue;let f=o?bf(c):-1,d=o?performance.now():0;if(i)try{for(let m of e.requiredAnalyses)this.analysisManager.getAnalysis(m,c);let h=e.run(c,this.analysisManager);if(o&&this.trace.passRun(e.name,h,performance.now()-d,f,bf(c)),h===oe.CHANGED){p=!0,r.anyChanged=!0,c.bumpVersion(),this.analysisManager.invalidate(c,e.preservedAnalyses);let m=this._verifyAfter(e,c,!1);m&&(r.errors.push(m),r.failedFunctions.add(c.name))}else h===oe.FAILED&&(this.analysisManager.invalidate(c),r.errors.push(new Yn("graphPasses",c.name,`pass '${e.name}' failed`,e.name)),r.failedFunctions.add(c.name))}catch(h){r.errors.push(new Yn("graphPasses",c.name,h.message,e.name)),r.failedFunctions.add(c.name)}else{for(let m of e.requiredAnalyses)this.analysisManager.getAnalysis(m,c);let h=e.run(c,this.analysisManager);if(o&&this.trace.passRun(e.name,h,performance.now()-d,f,bf(c)),h===oe.CHANGED){p=!0,r.anyChanged=!0,c.bumpVersion(),this.analysisManager.invalidate(c,e.preservedAnalyses);let m=this._verifyAfter(e,c,!1);if(m){r.errors.push(m),r.failedFunctions.add(c.name),u=!0;break}}else if(h===oe.FAILED){this.analysisManager.invalidate(c),r.errors.push(new Yn("graphPasses",c.name,`pass '${e.name}' failed`,e.name)),r.failedFunctions.add(c.name),u=!0;break}}}s.push(p?oe.CHANGED:oe.UNCHANGED),a=p}return this.instruments.length&&this._notify("runAfterPass",e,t,a?oe.CHANGED:oe.UNCHANGED),e.trace=null,{changed:a,fatal:u}}_runGroup(e,t,r,s){let o=e.maxIterations>0?e.maxIterations:1;for(let i=0;i<o;i++){let a=!1;for(let u of e.passes){if(!r.passContext.shouldRun(u))continue;let{changed:p,fatal:c}=this._applyPass(u,t,r,s);if(c)return!0;p&&(a=!0)}if(!a)return!1}return this.trace&&this.trace.passRun(`${e.name}:max-iter`,oe.UNCHANGED,0,-1,-1),!1}run(e,t={}){let r={verbose:this.trace&&this.trace.level>=le.VERBOSE,resilient:t.errorMode==="resilient",errors:[],failedFunctions:new Set,anyChanged:!1,passContext:t.passContext||new gy},s=[];for(let o of this.passes){if(!(o instanceof Ul)&&!r.passContext.shouldRun(o))continue;if(o instanceof Ul?this._runGroup(o,e,r,s):this._applyPass(o,e,r,s).fatal)return{changed:r.anyChanged,results:s,errors:r.errors,failedFunctions:r.failedFunctions.size>0?r.failedFunctions:null}}return{changed:r.anyChanged,results:s,errors:r.errors.length>0?r.errors:null,failedFunctions:r.failedFunctions.size>0?r.failedFunctions:null}}}});function dn(n,e){Ax.set(n,e)}function Ox(n,e,t,r){let s=n.neg(e).getResult(0),o=n.exp(s).getResult(0),i=n.broadcast(n.scalarConstant(1,t).getResult(0),r,[]).getResult(0),a=n.add(i,o).getResult(0);return n.div(i,a).getResult(0)}function xn(n,e,t,r){return n.broadcast(n.scalarConstant(e,t).getResult(0),r,[]).getResult(0)}var Ax,ql,Bx=M(()=>{"use strict";fn();_s();Re();nn();Ax=new Map;l(dn,"registerDecomposition");ql=class extends qe{static{l(this,"DecompositionPass")}constructor(e=null){super("DecompositionPass"),this.target=e}_shouldDecompose(e){if(!this.target)return!0;let t=this.target.getAttr?this.target.getAttr("nativeOps"):null;return!(t&&t.has(e.opName))}run(e){let t=[];for(let o of e.opsRecursive())Ax.has(o.opName)&&this._shouldDecompose(o)&&t.push(o);if(t.length===0)return oe.UNCHANGED;let r=new $n(e),s=[];for(let o of t){if(!o.parentBlock)continue;let i=Ax.get(o.opName);r.block=o.parentBlock,r.setInsertionPoint(o),s.push(o.opName),i(o,r)}if(this.trace&&this.trace.level>=le.DEBUG){let o={};for(let i of s)o[i]=(o[i]||0)+1;this.trace.emit({type:"pass_detail",passName:this.name,decomposed:o,totalDecomposed:s.length,level:le.DEBUG})}return oe.CHANGED}};dn("stop_gradient",n=>{n.replaceAllResultsWith([n.getOperand(0)]),n.erase()});dn("all_reduce",(n,e)=>{let t=n.getOperand(0),r=n.getAttr("mesh_axis")??0,s=n.getAttr("reduce_op")||"sum",o=t.type.shape,i=t.type.dtype,a=s==="max"?-1/0:s==="min"?1/0:s==="prod"||s==="and"?1:0,u=e.reduce(t,e.scalarConstant(a,i).getResult(0),[r],s).getResult(0),p=[];for(let f=0;f<o.length;f++)f!==r&&p.push(f);let c=e.broadcast(u,o,p).getResult(0);n.replaceAllResultsWith([c]),n.erase()});dn("all_gather",(n,e)=>{let t=n.getOperand(0),r=t.type.shape,s=n.getAttr("mesh_axis")??0,o=n.getAttr("gather_dim")??1,i=r[s],a=r.filter((m,g)=>g!==s),u=o<s?o:o-1,p=[];for(let m=0;m<i;m++){let g=r.map((b,w)=>w===s?m:0),y=r.map((b,w)=>w===s?m+1:b),_=e.slice(t,g,y).getResult(0);p.push(e.reshape(_,a).getResult(0))}let c=p.length===1?p[0]:e.concat(p,u).getResult(0),f=[...r];f[o]=i*r[o];let d=[];for(let m=0;m<f.length;m++)m!==s&&d.push(m);let h=e.broadcast(c,f,d).getResult(0);n.replaceAllResultsWith([h]),n.erase()});dn("softmax",(n,e)=>{let t=n.getOperand(0),r=n.getAttr("axis"),s=t.type.rank,o=t.type.dtype,i=t.type.shape,a=Xo(s,r),u=e.reduce(t,e.scalarConstant(-1/0,o).getResult(0),[r],"max"),p=e.broadcast(u.getResult(0),i,a),c=e.sub(t,p.getResult(0)),f=e.exp(c.getResult(0)),d=e.reduce(f.getResult(0),e.scalarConstant(0,o).getResult(0),[r],"sum"),h=e.broadcast(d.getResult(0),i,a),m=e.div(f.getResult(0),h.getResult(0));n.replaceAllResultsWith([m.getResult(0)]),n.erase()});dn("log_softmax",(n,e)=>{let t=n.getOperand(0),r=n.getAttr("axis"),s=t.type.rank,o=t.type.dtype,i=t.type.shape,a=Xo(s,r),u=e.reduce(t,e.scalarConstant(-1/0,o).getResult(0),[r],"max"),p=e.broadcast(u.getResult(0),i,a),c=e.sub(t,p.getResult(0)),f=e.exp(c.getResult(0)),d=e.reduce(f.getResult(0),e.scalarConstant(0,o).getResult(0),[r],"sum"),h=e.log(d.getResult(0)),m=e.broadcast(h.getResult(0),i,a),g=e.sub(c.getResult(0),m.getResult(0));n.replaceAllResultsWith([g.getResult(0)]),n.erase()});l(Ox,"emitSigmoid");dn("sigmoid",(n,e)=>{let t=n.getOperand(0),r=Ox(e,t,t.type.dtype,t.type.shape);n.replaceAllResultsWith([r]),n.erase()});dn("gelu",(n,e)=>{let t=n.getOperand(0),r=t.type.dtype,s=t.type.shape,o=e.broadcast(e.scalarConstant(1.702,r).getResult(0),s,[]).getResult(0),i=e.mul(o,t).getResult(0),a=Ox(e,i,r,s),u=e.mul(t,a).getResult(0);n.replaceAllResultsWith([u]),n.erase()});dn("silu",(n,e)=>{let t=n.getOperand(0),r=Ox(e,t,t.type.dtype,t.type.shape),s=e.mul(t,r).getResult(0);n.replaceAllResultsWith([s]),n.erase()});dn("layer_norm",(n,e)=>{let t=n.getOperand(0),r=n.getOperand(1),s=n.getOperand(2),o=n.getAttr("axis"),i=n.getAttr("epsilon"),a=t.type.rank,u=t.type.dtype,p=t.type.shape,c=Xo(a,o),f=e.reduce(t,e.scalarConstant(0,u).getResult(0),[o],"mean"),d=e.broadcast(f.getResult(0),p,c),h=e.sub(t,d.getResult(0)),m=e.mul(h.getResult(0),h.getResult(0)),g=e.reduce(m.getResult(0),e.scalarConstant(0,u).getResult(0),[o],"mean"),y=e.broadcast(e.scalarConstant(i,u).getResult(0),g.getResult(0).type.shape,[]),_=e.add(g.getResult(0),y.getResult(0)),b=e.rsqrt(_.getResult(0)),w=e.broadcast(b.getResult(0),p,c),x=e.mul(h.getResult(0),w.getResult(0)),S=e.broadcast(r,p,[o]),T=e.mul(x.getResult(0),S.getResult(0)),v=e.broadcast(s,p,[o]),A=e.add(T.getResult(0),v.getResult(0));n.replaceAllResultsWith([A.getResult(0)]),n.erase()});dn("batch_norm",(n,e)=>{let t=n.getOperand(0),r=n.getOperand(1),s=n.getOperand(2),o=n.getOperand(3),i=n.getOperand(4),a=n.getAttr("axis"),u=n.getAttr("epsilon"),p=t.type.rank,c=t.type.dtype,f=t.type.shape,d=e.broadcast(e.scalarConstant(u,c).getResult(0),i.type.shape,[]),h=e.add(i,d.getResult(0)),m=e.rsqrt(h.getResult(0)),g=e.broadcast(o,f,[a]),y=e.sub(t,g.getResult(0)),_=e.broadcast(m.getResult(0),f,[a]),b=e.mul(y.getResult(0),_.getResult(0)),w=e.broadcast(r,f,[a]),x=e.mul(b.getResult(0),w.getResult(0)),S=e.broadcast(s,f,[a]),T=e.add(x.getResult(0),S.getResult(0));n.replaceAllResultsWith([T.getResult(0)]),n.erase()});dn("where",(n,e)=>{let t=n.getOperand(0);if(t.type.dtype!=="bool"){let s=e.broadcast(e.scalarConstant(0,t.type.dtype).getResult(0),t.type.shape,[]);t=e.compare(t,s.getResult(0),"ne").getResult(0)}let r=e.select(t,n.getOperand(1),n.getOperand(2));n.replaceAllResultsWith([r.getResult(0)]),n.erase()});dn("split",(n,e)=>{let t=n.getOperand(0),r=n.getAttr("dimension"),s=n.getAttr("split_sizes"),o=t.type.shape,i=[],a=0;for(let u of s){let p=o.map((f,d)=>d===r?a:0),c=o.map((f,d)=>d===r?a+u:f);i.push(e.slice(t,p,c).getResult(0)),a+=u}n.replaceAllResultsWith(i),n.erase()});dn("one_hot",(n,e)=>{let t=n.getOperand(0),r=n.getAttr("depth"),s=n.getAttr("axis")??-1,o=n.getAttr("on_value")??1,i=n.getAttr("off_value")??0,a=n.getResult(0).type,u=a.dtype,p=a.shape,c=s<0?t.type.rank+1+s:s,f=new I(p,"i32"),d=e._inferAndBuild("iota",[],{iota_dimension:c,tensor_type:f}),h=t.type.rank,m=[];for(let S=0;S<h;S++)m.push(S<c?S:S+1);let g=e.broadcast(t,p,m),y=e.convert(g.getResult(0),"i32"),_=e.compare(y.getResult(0),d.getResult(0),"eq"),b=e.broadcast(e.scalarConstant(o,u).getResult(0),p,[]),w=e.broadcast(e.scalarConstant(i,u).getResult(0),p,[]),x=e.select(_.getResult(0),b.getResult(0),w.getResult(0));n.replaceAllResultsWith([x.getResult(0)]),n.erase()});l(xn,"bcast");dn("elu",(n,e)=>{let t=n.getOperand(0),r=n.getAttr("alpha")??1,s=t.type.dtype,o=t.type.shape,i=xn(e,0,s,o),a=e.compare(t,i,"gt").getResult(0),u=xn(e,1,s,o),p=e.exp(t).getResult(0),c=e.sub(p,u).getResult(0),f=xn(e,r,s,o),d=e.mul(f,c).getResult(0),h=e.select(a,t,d);n.replaceAllResultsWith([h.getResult(0)]),n.erase()});dn("leaky_relu",(n,e)=>{let t=n.getOperand(0),r=n.getAttr("negative_slope")??.01,s=t.type.dtype,o=t.type.shape,i=xn(e,0,s,o),a=e.compare(t,i,"gt").getResult(0),u=xn(e,r,s,o),p=e.mul(u,t).getResult(0),c=e.select(a,t,p);n.replaceAllResultsWith([c.getResult(0)]),n.erase()});dn("celu",(n,e)=>{let t=n.getOperand(0),r=n.getAttr("alpha")??1,s=t.type.dtype,o=t.type.shape,i=xn(e,0,s,o),a=e.maximum(t,i).getResult(0),u=xn(e,r,s,o),p=e.div(t,u).getResult(0),c=e.exp(p).getResult(0),f=xn(e,1,s,o),d=e.sub(c,f).getResult(0),h=e.mul(u,d).getResult(0),m=e.minimum(i,h).getResult(0),g=e.add(a,m);n.replaceAllResultsWith([g.getResult(0)]),n.erase()});dn("selu",(n,e)=>{let t=n.getOperand(0),r=t.type.dtype,s=t.type.shape,o=1.0507009873554805,i=1.6732632423543772,a=xn(e,0,r,s),u=e.compare(t,a,"gt").getResult(0),p=xn(e,1,r,s),c=e.exp(t).getResult(0),f=e.sub(c,p).getResult(0),d=xn(e,i,r,s),h=e.mul(d,f).getResult(0),m=e.select(u,t,h).getResult(0),g=xn(e,o,r,s),y=e.mul(g,m);n.replaceAllResultsWith([y.getResult(0)]),n.erase()});dn("mish",(n,e)=>{let t=n.getOperand(0),r=t.type.dtype,s=t.type.shape,o=xn(e,1,r,s),i=e.exp(t).getResult(0),a=e.add(o,i).getResult(0),u=e.log(a).getResult(0),p=e.tanh(u).getResult(0),c=e.mul(t,p);n.replaceAllResultsWith([c.getResult(0)]),n.erase()});dn("hardswish",(n,e)=>{let t=n.getOperand(0),r=t.type.dtype,s=t.type.shape,o=xn(e,3,r,s),i=xn(e,0,r,s),a=xn(e,6,r,s),u=e.add(t,o).getResult(0),p=e.minimum(e.maximum(u,i).getResult(0),a).getResult(0),c=e.div(p,a).getResult(0),f=e.mul(t,c);n.replaceAllResultsWith([f.getResult(0)]),n.erase()});dn("hardsigmoid",(n,e)=>{let t=n.getOperand(0),r=t.type.dtype,s=t.type.shape,o=xn(e,6,r,s),i=xn(e,.5,r,s),a=xn(e,0,r,s),u=xn(e,1,r,s),p=e.div(t,o).getResult(0),c=e.add(p,i).getResult(0),f=e.minimum(e.maximum(c,a).getResult(0),u);n.replaceAllResultsWith([f.getResult(0)]),n.erase()});dn("embedding",(n,e)=>{let t=n.getOperand(0),r=n.getOperand(1),s=t.type.shape[t.type.rank-1],o=r.type.rank,i=e._inferAndBuild("gather",[t,r],{offset_dims:Array.from({length:1},(a,u)=>o+u),collapsed_slice_dims:[0],start_index_map:[0],slice_sizes:[1,s],index_vector_dim:o});n.replaceAllResultsWith([i.getResult(0)]),n.erase()})});var Wl,Rx=M(()=>{"use strict";fn();_s();nn();Wl=class{static{l(this,"PatternApplicator")}constructor(e){this.patternSet=e}applyPatterns(e,t=10,r=null){let s=new $n(e),o=0,i=[...e.opsRecursive()],a=0,u=new Set(i),p=l(h=>{!h||!h.parentBlock||u.has(h)||(u.add(h),i.push(h))},"enqueue"),c=Math.max(t,1)*Math.max(i.length,1)*4+1e3,f=0,d=!1;for(;a<i.length;){if(++f>c){d=!0;break}let h=i[a++];if(u.delete(h),!h.parentBlock)continue;let m=this.patternSet.getForOp(h.opName);for(let g of m){if(!g.match(h))continue;let y=h.parentBlock,_=h._prev,b=h._next,w=[];for(let T=0;T<h.numResults;T++)for(let v of h.getResult(T).getUsers())w.push(v);for(let T=0;T<h.numOperands;T++){let v=h.getOperand(T).definingOp;v&&w.push(v)}if(s.block=y,s.setInsertionPoint(h),!g.rewrite(h,s))continue;o++;for(let T of w)p(T);let x=_?_._next:y._head,S=y._size+2;for(;x&&x!==b&&S-- >0;)p(x),x=x._next;p(h);break}}return r&&(d&&r.level>=le.INFO&&r.emit({type:"pass_detail",passName:"PatternApplicator",message:`pattern rewriting hit safety budget (${c}) without converging`,totalRewrites:o,level:le.INFO}),r.level>=le.DEBUG&&o>0&&r.emit({type:"pass_detail",passName:"PatternApplicator",totalRewrites:o,patternCount:this.patternSet.patterns.length,level:le.DEBUG})),o>0?oe.CHANGED:oe.UNCHANGED}}});function w3(){if(wf)return wf;wf=new Wu;for(let n of ne.allOps())if(n.getCanonicalizationPatterns){let e=n.getCanonicalizationPatterns();if(e)for(let t of e)wf.add(t)}return wf}var wf,au,Mx=M(()=>{"use strict";fn();cp();Rx();Ct();wf=null;l(w3,"getCanonicalizationPatterns");au=class extends qe{static{l(this,"CanonicalizePass")}constructor(){super("canonicalize")}run(e,t){let r=w3();return new Wl(r).applyPatterns(e,10,this.trace)}}});var wy,Ix,Kl,nM=M(()=>{"use strict";Ct();Bn();wy=class{static{l(this,"MemoryEffect")}constructor(e,t){this.kind=e,this.value=t}},Ix=class{static{l(this,"MemoryEffectResult")}constructor(e,t){this.opEffects=e,this._valueEffects=t}hasSideEffect(e){let t=this.opEffects.get(e);return t&&t.length>0}getEffects(e){return this.opEffects.get(e)||[]}getEffectsOn(e){return this._valueEffects.get(e)||[]}getReadersOf(e){let t=this._valueEffects.get(e);if(!t)return[];let r=[];for(let s=0;s<t.length;s++)t[s].effect.kind===gn.READ&&r.push(t[s].op);return r}getWritersOf(e){let t=this._valueEffects.get(e);if(!t)return[];let r=[];for(let s=0;s<t.length;s++)t[s].effect.kind===gn.WRITE&&r.push(t[s].op);return r}},Kl=class{static get name(){return"memory_effect"}static get depKey(){return"memoryEffect"}static get dependencies(){return[]}static compute(e){let t=new Map,r=new Map,s=l((i,a,u)=>{let p=r.get(i);p||(p=[],r.set(i,p)),p.push({op:a,effect:u})},"addValueEffect"),o=typeof e.opsRecursive=="function"?e.opsRecursive():e.ops();for(let i of o){let a=ne.get(i.opName),u=[],p=gn.NONE;if(a&&a.sideEffects?p=a.sideEffects:i.hasSideEffects&&i.hasSideEffects()&&(p=gn.WRITE),p!==gn.NONE)if(a&&a.getMemoryEffects){let c=a.getMemoryEffects(i);for(let f of c)u.push(f),s(f.value,i,f)}else{if(p&gn.READ)for(let c=0;c<i.numOperands;c++){let f=new wy(gn.READ,i.getOperand(c));u.push(f),s(i.getOperand(c),i,f)}if(p&gn.WRITE)for(let c=0;c<i.numResults;c++){let f=new wy(gn.WRITE,i.getResult(c));u.push(f),s(i.getResult(c),i,f)}}t.set(i,u)}return new Ix(t,r)}}});var Co,Ex=M(()=>{"use strict";fn();nM();Ir();nn();Co=class extends qe{static{l(this,"DCEPass")}constructor(){super("dce"),this.preservedAnalyses=new Set,this.requiredAnalyses=[Kl]}run(e,t){let r=!1,s=t?t.getAnalysis(Kl,e):Kl.compute(e),o=[];for(let a of e.opsRecursive())this._isDead(a,s)&&o.push(a);let i=0;for(;o.length>0;){let a=o.pop();if(!a.parentBlock||!this._isDead(a,s))continue;let u=[];for(let p=0;p<a.numOperands;p++){let c=a.getOperand(p).definingOp;c&&c.parentBlock&&u.push(c)}a.erase(),r=!0,i++;for(let p of u)p.parentBlock&&this._isDead(p,s)&&o.push(p)}return this.trace&&this.trace.level>=le.DEBUG&&i>0&&this.trace.emit({type:"pass_detail",passName:this.name,erasedCount:i,level:le.DEBUG}),r?oe.CHANGED:oe.UNCHANGED}_isDead(e,t){if(Xt(e.opName)||e.regions&&e.regions.length>0)return!1;for(let r=0;r<e.numResults;r++)if(e.getResult(r).hasUses)return!1;return!t.hasSideEffect(e)}}});var Ty,xy,rM=M(()=>{"use strict";Ty=class n{static{l(this,"ScheduleStep")}constructor(e,t){this.primitive=e,this.args=t}serialize(){return{primitive:this.primitive,args:this.args}}static deserialize(e){return new n(e.primitive,e.args)}},xy=class n{static{l(this,"ScheduleTrace")}constructor(){this.steps=[]}record(e,t){let r=new Ty(e,t);return this.steps.push(r),r}serialize(){return this.steps.map(e=>e.serialize())}static deserialize(e){let t=new n;for(let r of e)t.steps.push(Ty.deserialize(r));return t}replay(e){for(let t of this.steps){let r=e[t.primitive];if(typeof r!="function")throw new Error(`Unknown schedule primitive: ${t.primitive}`);e._replaying=!0;try{r.call(e,...t.args)}finally{e._replaying=!1}}}get length(){return this.steps.length}clear(){this.steps.length=0}}});function uu(n){return Number.isFinite(n)}function sM(n){return Number.isNaN(n)?iM:n}function oM(n){return Number.isNaN(n)?aM:n}var iM,aM,ps,Hl,Xs,Yl=M(()=>{"use strict";po();iM=-1/0,aM=1/0;l(uu,"isFinite2");l(sM,"boundLo");l(oM,"boundHi");ps=class{static{l(this,"IntBound")}constructor(e,t){this.min=e,this.max=t}isConst(){return this.min===this.max&&uu(this.min)}},Hl=new ps(iM,aM),Xs=class{static{l(this,"Analyzer")}constructor(){this._varBounds=new Map}bind(e,t,r){return this._varBounds.set(e,new ps(t,r)),this}getVarBound(e){return this._varBounds.get(e)||null}setVarBound(e,t){return t?this._varBounds.set(e,t):this._varBounds.delete(e),this}bindShape(e){for(let[t,r]of e)typeof r=="number"&&r>0&&this.bind(t,0,r-1);return this}constIntBound(e){if(typeof e=="number")return new ps(e,e);if(!(e instanceof Pe))return Hl;if(e.type==="var")return this._varBounds.get(e.name)||Hl;let t=e.args.length>0?this.constIntBound(e.args[0]):null,r=e.args.length>1?this.constIntBound(e.args[1]):null;switch(e.type){case"add":return new ps(sM(t.min+r.min),oM(t.max+r.max));case"sub":return new ps(sM(t.min-r.max),oM(t.max-r.min));case"neg":return new ps(-t.max,-t.min);case"mul":return this._mulBound(t,r);case"max":return new ps(Math.max(t.min,r.min),Math.max(t.max,r.max));case"min":return new ps(Math.min(t.min,r.min),Math.min(t.max,r.max));case"div":return this._divBound(t,r,Math.floor);case"ceildiv":return this._divBound(t,r,Math.ceil);case"mod":return this._modBound(t,r);default:return Hl}}_mulBound(e,t){if(!uu(e.min)||!uu(e.max)||!uu(t.min)||!uu(t.max))return Hl;let r=[e.min*t.min,e.min*t.max,e.max*t.min,e.max*t.max];return new ps(Math.min(...r),Math.max(...r))}_divBound(e,t,r){if(t.isConst()&&t.min>0){let s=uu(e.min)?r(e.min/t.min):e.min,o=uu(e.max)?r(e.max/t.min):e.max;return new ps(s,o)}return Hl}_modBound(e,t){return t.isConst()&&t.min>0?new ps(0,t.min-1):Hl}canProveGreaterEqual(e,t){return this.constIntBound(Pe.sub(e,t)).min>=0}canProveLess(e,t){return this.constIntBound(Pe.sub(e,t)).max<0}canProveNonNegative(e){return this.canProveGreaterEqual(e,0)}canProveEqual(e,t){if(Pe.equals(e,t))return!0;let r=this.constIntBound(Pe.sub(e,t));return r.min===0&&r.max===0}}});function oa(n){if(n==null)return null;if(typeof n=="number")return n;switch(n.type){case"IntImmNode":return n.value;case"VariableNode":return Pe.var(n.name);case"MathOpNode":{if(n.b===null||n.b===void 0){if(n.op==="-"){let s=oa(n.a);return s===null?null:Pe.neg(s)}return null}let e=T3[n.op];if(!e)return null;let t=oa(n.a);if(t===null)return null;let r=oa(n.b);return r===null||(n.op==="//"||n.op==="%")&&(typeof r!="number"||r<=0)?null:Pe[e](t,r)}case"CallExternNode":{if(n.args.length!==2||n.externName!=="max"&&n.externName!=="min")return null;let e=oa(n.args[0]);if(e===null)return null;let t=oa(n.args[1]);return t===null?null:n.externName==="max"?Pe.max(e,t):Pe.min(e,t)}default:return null}}function cu(n,e){let t=oa(e);return t===null?null:n.constIntBound(t)}function x3(n,e,t){let r=oa(e);if(r===null)return null;let s=oa(t);return s===null?null:n.constIntBound(Pe.sub(r,s))}function Sy(n,e,t,r,s){let o=s[e];if(!o)return!1;let i=x3(n,t,r);return i===null?!1:o(i)}function _i(n,e){if(e==null)return!1;if(e.type==="IntImmNode")return e.value!==0;if(e.type==="CompareNode")return Sy(n,e.direction,e.a,e.b,uM);if(e.type==="MathOpNode"){if(e.op==="*"&&e.b)return _i(n,e.a)&&_i(n,e.b);if(Lx.has(e.op))return Sy(n,fM[e.op],e.a,e.b,uM)}return!1}function lu(n,e){if(e==null)return!1;if(e.type==="IntImmNode")return e.value===0;if(e.type==="CompareNode")return Sy(n,e.direction,e.a,e.b,lM);if(e.type==="MathOpNode"){if(e.op==="*"&&e.b)return lu(n,e.a)||lu(n,e.b);if(Lx.has(e.op))return Sy(n,fM[e.op],e.a,e.b,lM)}return!1}function cM(n){return!n||n.type!=="MathOpNode"||n.op!=="*"?null:n.b&&n.b.type==="IntImmNode"&&n.b.value>0?{factor:n.a,c:n.b.value}:n.a&&n.a.type==="IntImmNode"&&n.a.value>0?{factor:n.b,c:n.a.value}:null}function pM(n,e,t,r){let s=cu(n,e);return s===null?!1:s.min>=t&&s.max<=r}var T3,Lx,fM,uM,lM,vy,Tf=M(()=>{"use strict";Yl();po();Fe();T3={"+":"add","-":"sub","*":"mul","//":"div","%":"mod"},Lx=new Set(["<","<=",">",">=","==","!="]),fM={"<":"lt","<=":"le",">":"gt",">=":"ge","==":"eq","!=":"ne"};l(oa,"irToSymInt");l(cu,"irBound");l(x3,"diffBound");uM={lt:l(n=>n.max<0,"lt"),le:l(n=>n.max<=0,"le"),gt:l(n=>n.min>0,"gt"),ge:l(n=>n.min>=0,"ge"),eq:l(n=>n.min===0&&n.max===0,"eq"),ne:l(n=>n.min>0||n.max<0,"ne")},lM={lt:l(n=>n.min>=0,"lt"),le:l(n=>n.min>0,"le"),gt:l(n=>n.max<=0,"gt"),ge:l(n=>n.max<0,"ge"),eq:l(n=>n.min>0||n.max<0,"eq"),ne:l(n=>n.min===0&&n.max===0,"ne")};l(Sy,"proveCompare");l(_i,"proveTrue");l(lu,"proveFalse");l(cM,"asScaledVar");l(pM,"boundWithin");vy=class{static{l(this,"RewriteSimplify")}constructor(e=new Xs){this.analyzer=e}simplify(e){if(e==null||typeof e!="object")return e;switch(e.type){case"IntImmNode":case"VariableNode":return e;case"MathOpNode":return this._simplifyMathOp(e);case"CompareNode":return this._simplifyCompare(e);default:return e}}_simplifyCompare(e){let t=this.simplify(e.a),r=this.simplify(e.b);return _i(this.analyzer,new Ke(e.direction,t,r))?new L(1):lu(this.analyzer,new Ke(e.direction,t,r))?new L(0):t===e.a&&r===e.b?e:new Ke(e.direction,t,r)}_simplifyMathOp(e){if(e.b===null||e.b===void 0){let o=this.simplify(e.a);return o===e.a?e:new P(e.op,o)}let t=this.simplify(e.a),r=this.simplify(e.b),s=Un(e.op,t,r);if(!s||s.type!=="MathOpNode")return s;if(s.op==="//"&&s.b.type==="IntImmNode"&&s.b.value>0){let o=s.b.value,i=cM(s.a);if(i&&i.c===o)return i.factor;if(pM(this.analyzer,s.a,0,o-1))return new L(0)}if(s.op==="%"&&s.b.type==="IntImmNode"&&s.b.value>0){let o=s.b.value,i=cM(s.a);if(i&&i.c===o)return new L(0);if(pM(this.analyzer,s.a,0,o-1))return s.a}if(Lx.has(s.op)){if(_i(this.analyzer,s))return new L(1);if(lu(this.analyzer,s))return new L(0)}return s}}});function dM(n,e,t){if(typeof t!="number"||t<0)return"unknown";let r=cu(n,e);return r===null?"unknown":r.min>=0&&r.max<=t-1?"in":r.min>t-1||r.max<0?"oob":"unknown"}function Nt(n,e){if(!(!n||typeof n!="object"))switch(n.type){case"VariableNode":n.name&&e.add(n.name);return;case"BufferLoadNode":if(n.indices)for(let t of n.indices)Nt(t,e);return;case"BufferStoreNode":if(n.indices)for(let t of n.indices)Nt(t,e);Nt(n.value,e);return;case"MathOpNode":Nt(n.a,e),n.b&&Nt(n.b,e);return;case"CompareNode":Nt(n.a,e),Nt(n.b,e);return;case"CastNode":Nt(n.expr,e);return;case"CallExternNode":for(let t of n.args)Nt(t,e);return;case"IfThenElseNode":Nt(n.condition,e),Nt(n.thenBody,e),n.elseBody&&Nt(n.elseBody,e);return;case"SeqNode":for(let t of n.stmts)Nt(t,e);return;case"LetStmtNode":Nt(n.value,e),Nt(n.body,e);return;default:return}}function ky(n,e){let t=new Set((n.writes||[]).map(s=>s.buffer&&s.buffer.name)),r=[n.body,n.initBody];for(;r.length>0;){let s=r.pop();if(!(!s||typeof s!="object")){if(s.type==="BufferStoreNode"&&s.buffer&&t.has(s.buffer.name)&&s.indices)for(let o of s.indices)Nt(o,e);if(s.type==="BufferStoreNode"){s.value&&r.push(s.value);continue}if(s.type==="SeqNode"){for(let o of s.stmts)r.push(o);continue}if(s.type==="IfThenElseNode"){r.push(s.thenBody),s.elseBody&&r.push(s.elseBody);continue}if(s.type==="LetStmtNode"){r.push(s.body);continue}}}}function S3(n,e){let t=new Set;if(Nt(e.body,t),e.initBody&&Nt(e.initBody,t),!t.has(n))return!1;let r=new Set;return ky(e,r),!r.has(n)}function pu(n,e){if(!(!n||typeof n!="object"))switch(n.type){case"BlockNode":e.push(n),pu(n.body,e),n.initBody&&pu(n.initBody,e);return;case"ForNode":case"AllocateNode":case"LetStmtNode":pu(n.body,e);return;case"SeqNode":for(let t of n.stmts)pu(t,e);return;case"IfThenElseNode":pu(n.thenBody,e),n.elseBody&&pu(n.elseBody,e);return;default:return}}function Cx(n){let e=n.loopVar.name,t=[];pu(n.body,t);for(let r of t)if(S3(e,r))return r.name;return null}var Dx=M(()=>{"use strict";Yl();po();Tf();l(dM,"classifyBufferIndex");l(Nt,"collectVarsUsed");l(ky,"collectWriteIndexVars");l(S3,"loopVarIsReductionOf");l(pu,"collectBlocksUnder");l(Cx,"loopCarriesReduction")});var Jl,Px=M(()=>{"use strict";Fe();Dx();Yl();Tf();Jl=class n{static{l(this,"ScheduleValidator")}static validate(e){let t=[],r={boundVars:new Set,threadBindings:new Map,parallelExtents:new Map,parLoops:[],innermostLoopVar:null,loopStack:[],analyzer:new Xs,condDepth:0,errors:t};for(let s of e.params)r.boundVars.add(s.name);return n._visitNode(e.body,r),n._checkPartitionConsistency(r),t}static _checkPartitionConsistency(e){if(e.parallelExtents.size<=1)return;let t=[...e.parallelExtents.entries()].map(([r,s])=>`'${s}'(extent ${r})`);e.errors.push(`Ambiguous parallel partition: ${e.parallelExtents.size} distinct parallel extents [${t.join(", ")}] \u2014 runtime partitions a single axis, mismatched-extent parallel loops corrupt buffers`)}static _visitNode(e,t){if(e)switch(e.type){case"ForNode":n._visitFor(e,t);break;case"BlockNode":n._visitBlock(e,t);break;case"SeqNode":for(let r of e.stmts)n._visitNode(r,t);break;case"IfThenElseNode":n._visitExpr(e.condition,t),t.condDepth++,n._visitNode(e.thenBody,t),e.elseBody&&n._visitNode(e.elseBody,t),t.condDepth--;break;case"AllocateNode":n._visitNode(e.body,t);break;case"LetStmtNode":t.boundVars.add(e.variable.name),n._visitNode(e.body,t),t.boundVars.delete(e.variable.name);break;case"BufferStoreNode":n._validateBufferAccess(e,t);break;case"EvaluateNode":break;default:break}}static _visitFor(e,t){let r=e.loopVar.name;if(t.boundVars.has(r)&&t.errors.push(`Duplicate loop variable: ${r}`),e.kind===z.THREAD_BINDING&&(e.threadTag?(t.threadBindings.has(e.threadTag)&&t.errors.push(`Duplicate thread binding '${e.threadTag}': already bound to '${t.threadBindings.get(e.threadTag)}'`),t.threadBindings.set(e.threadTag,r)):t.errors.push(`Thread-bound loop '${r}' missing threadTag`)),e.kind===z.VECTORIZED){let a=e.extent;a.type==="IntImmNode"&&a.value<=0&&t.errors.push(`Vectorized loop '${r}' has non-positive extent ${a.value}`)}if(e.kind===z.PARALLEL){let a=e.extent;a&&a.type==="IntImmNode"&&!t.parallelExtents.has(a.value)&&t.parallelExtents.set(a.value,r)}(e.kind===z.PARALLEL||e.kind===z.THREAD_BINDING)&&n._checkNoNestedParallel(e.body,r,t);let s=e.kind===z.PARALLEL||e.kind===z.VECTORIZED;s&&t.parLoops.push({varName:r,kind:e.kind}),t.boundVars.add(r);let o=t.analyzer.getVarBound(r);e.extent&&e.extent.type==="IntImmNode"&&e.extent.value>0?t.analyzer.bind(r,0,e.extent.value-1):t.analyzer.setVarBound(r,null);let i=t.innermostLoopVar;t.innermostLoopVar=r,t.loopStack.push(r),n._visitNode(e.body,t),t.loopStack.pop(),t.innermostLoopVar=i,t.boundVars.delete(r),t.analyzer.setVarBound(r,o),s&&t.parLoops.pop(),e.kind===z.THREAD_BINDING&&e.threadTag&&t.threadBindings.delete(e.threadTag)}static _checkNoNestedParallel(e,t,r){e&&e.type==="ForNode"&&e.kind===z.PARALLEL&&r.errors.push(`Parallel loop '${e.loopVar.name}' nested inside parallel/thread-bound loop '${t}'`)}static _visitBlock(e,t){let r=[];for(let s of e.iterVars)s.iterVar&&(t.boundVars.add(s.iterVar.name),r.push([s.iterVar.name,t.analyzer.getVarBound(s.iterVar.name)]),t.analyzer.setVarBound(s.iterVar.name,s.binding?cu(t.analyzer,s.binding):null));if(e.initBody&&t.innermostLoopVar){let s=new Set;ky(e,s);let o=new Set;Nt(e.body,o),Nt(e.initBody,o);let i=0;for(let a of t.loopStack)o.has(a)&&!s.has(a)&&i++;(s.has(t.innermostLoopVar)||i>1)&&t.errors.push(`Reduction block '${e.name}' violates the init contract: codegen zeroes the accumulator at the single innermost loop, so an init-bearing block needs exactly one enclosing reduction loop and it must be innermost (innermost '${t.innermostLoopVar}', ${i} enclosing reduction loops)`)}if(t.parLoops.length>0){let s=new Set;Nt(e.body,s),e.initBody&&Nt(e.initBody,s);let o=new Set;ky(e,o);for(let i of t.parLoops)if(s.has(i.varName)&&!o.has(i.varName)){let a=i.kind===z.VECTORIZED?"Vectorized":"Parallel";t.errors.push(`${a} loop '${i.varName}' carries a reduction in block '${e.name}': the loop variable is read but never written, so parallel iterations race on the accumulator`)}}n._visitNode(e.body,t),e.initBody&&n._visitNode(e.initBody,t);for(let s of e.iterVars)s.iterVar&&t.boundVars.delete(s.iterVar.name);for(let[s,o]of r)t.analyzer.setVarBound(s,o)}static _validateBufferAccess(e,t){if(e.buffer?(n._checkRank(e,t),n._checkBounds(e,t)):t.errors.push("BufferStore with null buffer"),e.indices)for(let r of e.indices)n._visitExpr(r,t);e.value&&n._visitExpr(e.value,t)}static _checkRank(e,t){e.indices&&e.buffer.shape&&e.indices.length!==e.buffer.shape.length&&t.errors.push(`Buffer '${e.buffer.name}' rank mismatch: ${e.indices.length} indices for rank-${e.buffer.shape.length} buffer`)}static _checkBounds(e,t){if(!(t.condDepth>0)&&!(!e.buffer||!e.buffer.shape||!e.indices)&&e.indices.length===e.buffer.shape.length)for(let r=0;r<e.indices.length;r++){let s=e.buffer.shape[r];dM(t.analyzer,e.indices[r],s)==="oob"&&t.errors.push(`Buffer '${e.buffer.name}' access is out of bounds on axis ${r}: index is provably outside [0, ${s-1}]`)}}static _visitExpr(e,t){if(!(!e||typeof e!="object"))switch(e.type){case"BufferLoadNode":if(e.buffer?(n._checkRank(e,t),n._checkBounds(e,t)):t.errors.push("BufferLoad with null buffer"),e.indices)for(let r of e.indices)n._visitExpr(r,t);break;case"BufferStoreNode":n._validateBufferAccess(e,t);break;case"MathOpNode":case"CompareNode":n._visitExpr(e.a,t),n._visitExpr(e.b,t);break;case"CastNode":n._visitExpr(e.expr,t);break;case"CallExternNode":for(let r of e.args)n._visitExpr(r,t);break;case"IfThenElseNode":n._visitExpr(e.condition,t),t.condDepth++,n._visitNode(e.thenBody,t),e.elseBody&&n._visitNode(e.elseBody,t),t.condDepth--;break}}}});var $x,Zl,Fx=M(()=>{"use strict";Fe();$x=class{static{l(this,"SRef")}constructor(e,t=null){this.node=e,this.parent=t,this.children=[]}get type(){return this.node.type}get isLoop(){return this.node.type==="ForNode"}get isBlock(){return this.node.type==="BlockNode"}get isRoot(){return this.parent===null}ancestors(){let e=[],t=this.parent;for(;t;)e.push(t),t=t.parent;return e}loopAncestors(){let e=[],t=this.parent;for(;t;)t.isLoop&&e.push(t),t=t.parent;return e}childBlocks(){let e=[],t=[...this.children];for(;t.length>0;){let r=t.pop();if(r.isBlock)e.push(r);else for(let s of r.children)t.push(s)}return e}childLoops(){return this.children.filter(e=>e.isLoop)}},Zl=class{static{l(this,"SRefTree")}constructor(e){this._nodeToSRef=new Map,this._blockNameToSRef=new Map,this._loopSRefs=new Set,this._blockSRefs=new Set,this.root=this._build(e.body,null)}_register(e){this._nodeToSRef.set(e.node,e),e.isLoop?this._loopSRefs.add(e):e.isBlock&&(this._blockSRefs.add(e),this._blockNameToSRef.set(e.node.name,e))}_unregisterSubtree(e){let t=[e];for(;t.length>0;){let r=t.pop();this._nodeToSRef.delete(r.node),r.isLoop?this._loopSRefs.delete(r):r.isBlock&&(this._blockSRefs.delete(r),this._blockNameToSRef.get(r.node.name)===r&&this._blockNameToSRef.delete(r.node.name));for(let s of r.children)t.push(s)}}_buildSubtree(e,t){let r=[],s=[{node:e,parentSRef:t,isTop:!0}];for(;s.length>0;){let{node:o,parentSRef:i,isTop:a}=s.pop();if(o)switch(o.type){case"ForNode":case"BlockNode":{let u=new $x(o,i);this._register(u),a?r.push(u):i.children.push(u),s.push({node:o.body,parentSRef:u,isTop:!1}),o.type==="BlockNode"&&o.initBody&&s.push({node:o.initBody,parentSRef:u,isTop:!1});break}case"SeqNode":for(let u=o.stmts.length-1;u>=0;u--)s.push({node:o.stmts[u],parentSRef:i,isTop:a});break;case"IfThenElseNode":o.elseBody&&s.push({node:o.elseBody,parentSRef:i,isTop:a}),s.push({node:o.thenBody,parentSRef:i,isTop:a});break;case"AllocateNode":case"LetStmtNode":s.push({node:o.body,parentSRef:i,isTop:a});break}}return r}_build(e,t){let r=this._buildSubtree(e,t);if(t)for(let s of r)t.children.push(s);return this._nodeToSRef.get(e)||null}getSRef(e){return this._nodeToSRef.get(e)||null}getBlockSRef(e){return this._blockNameToSRef.get(e)||null}allBlocks(){return[...this._blockSRefs]}allLoops(){return[...this._loopSRefs]}loopsOf(e){let t=this._blockNameToSRef.get(e);return t?t.loopAncestors().filter(r=>r.node.kind!==z.RECURRENCE).reverse():[]}rebuildFrom(e){this._nodeToSRef.clear(),this._blockNameToSRef.clear(),this._loopSRefs.clear(),this._blockSRefs.clear(),this.root=this._build(e,null)}replaceNode(e,t){let r=this._nodeToSRef.get(e);if(!r)return!1;let s=r.parent,o=this.root===r;this._unregisterSubtree(r);let i=this._buildSubtree(t,s);if(s){let a=s.children.indexOf(r);if(a>=0)s.children.splice(a,1,...i);else for(let u of i)s.children.push(u)}else o&&(this.root=this._nodeToSRef.get(t)||null);return!0}removeNode(e){let t=this._nodeToSRef.get(e);if(!t)return!1;let r=t.parent,s=this.root===t;if(this._unregisterSubtree(t),r){let o=r.children.indexOf(t);o>=0&&r.children.splice(o,1)}else s&&(this.root=null);return!0}}});var zx,Vx,Ny,hM=M(()=>{"use strict";Fx();Fe();zx=class{static{l(this,"LoopBinding")}constructor(e,t,r,s,o){this.loopSRef=e,this.iterVar=t,this.extent=r,this.kind=s,this.threadTag=o}},Vx=class{static{l(this,"BlockBinding")}constructor(e,t,r,s){this.blockSRef=e,this.iterVars=t,this.readBuffers=r,this.writeBuffers=s}},Ny=class{static{l(this,"ScheduleState")}constructor(e){this.primFunc=e,this._dirty=!0,this._tree=null,this._loopBindings=null,this._blockBindings=null}_ensureBuilt(){if(this._dirty){this._tree=new Zl(this.primFunc),this._loopBindings=new Map,this._blockBindings=new Map;for(let e of this._tree.allLoops()){let t=e.node,r=t.extent&&t.extent.type==="IntImmNode"?t.extent.value:null;this._loopBindings.set(t.loopVar.name,new zx(e,t.loopVar.name,r,t.kind,t.threadTag))}for(let e of this._tree.allBlocks()){let t=e.node,r=[];for(let i of t.iterVars)i.iterVar&&r.push(i.iterVar.name);let s=[];for(let i of t.reads)s.push(i.buffer.name);let o=[];for(let i of t.writes)o.push(i.buffer.name);this._blockBindings.set(t.name,new Vx(e,r,s,o))}this._dirty=!1}}get tree(){return this._ensureBuilt(),this._tree}invalidate(){this._dirty=!0,this._tree=null,this._loopBindings=null,this._blockBindings=null}getLoopBinding(e){return this._ensureBuilt(),this._loopBindings.get(e)||null}getBlockBinding(e){return this._ensureBuilt(),this._blockBindings.get(e)||null}getBlock(e){let t=this.tree.getBlockSRef(e);if(!t)throw new Error(`Block '${e}' not found in schedule state`);return t}getLoopsOf(e){return this.tree.loopsOf(e)}threadBindingSummary(){this._ensureBuilt();let e={};for(let[,t]of this._loopBindings)t.kind===z.THREAD_BINDING&&t.threadTag&&(e[t.threadTag]={varName:t.iterVar,extent:t.extent});return e}blockDim(){let e=this.threadBindingSummary();return[e["threadIdx.x"]?.extent||1,e["threadIdx.y"]?.extent||1,e["threadIdx.z"]?.extent||1]}gridDim(){let e=this.threadBindingSummary();return[e["blockIdx.x"]?.extent||1,e["blockIdx.y"]?.extent||1,e["blockIdx.z"]?.extent||1]}allBlockNames(){return this._ensureBuilt(),[...this._blockBindings.keys()]}allLoopVarNames(){return this._ensureBuilt(),[...this._loopBindings.keys()]}summary(){return{blocks:this.allBlockNames(),loops:this.allLoopVarNames(),threadBindings:this.threadBindingSummary(),blockDim:this.blockDim(),gridDim:this.gridDim()}}}});var Ay,mM=M(()=>{"use strict";ts();Ay=class{static{l(this,"ScheduleMutator")}constructor(e){this.func=e}replaceNode(e,t){if(e._parent){e.replaceWith(t);return}(this.func.body===e||this.func.body===void 0)&&(this.func.body=t,this.func._setChild&&this.func._setChild("body",t))}removeNode(e){let t=e._parent;if(t&&t.type==="SeqNode"&&Array.isArray(t.stmts)){let r=t.stmts.indexOf(e);if(r>=0){t.stmts.splice(r,1),t._setChildren&&t._setChildren("stmts",t.stmts);return}}throw new Error("removeNode: node parent is not a SeqNode; cannot remove without duplicating it")}redirectReads(e,t,r){e&&qs(e,s=>{s.type==="BufferLoadNode"&&s.buffer===t&&(s.buffer=r)})}redirectBuffer(e,t,r){e&&qs(e,s=>{(s.type==="BufferLoadNode"||s.type==="BufferStoreNode")&&s.buffer===t&&(s.buffer=r)})}}});function Oy(n,e,t){if(!n||typeof n!="object")return n;if(Array.isArray(n))return n.map(e);let r=Object.create(Object.getPrototypeOf(n));switch(r.type=n.type,r._parent=null,r._parentKey=null,r._parentIdx=-1,n.type){case"ForNode":return r.loopVar=n.loopVar,r.min=e(n.min),r.extent=e(n.extent),r.kind=n.kind,r.body=e(n.body),r.threadTag=n.threadTag,r._setChild("body",r.body),r;case"BlockNode":return r.name=n.name,r.iterVars=n.iterVars.map(e),r.reads=n.reads,r.writes=n.writes,r.body=e(n.body),r.initBody=n.initBody?e(n.initBody):null,r._setChild("body",r.body),r._setChild("initBody",r.initBody),r;case"SeqNode":return r.stmts=n.stmts.map(e),r._setChildren("stmts",r.stmts),r;case"IfThenElseNode":return r.condition=e(n.condition),r.thenBody=e(n.thenBody),r.elseBody=n.elseBody?e(n.elseBody):null,r._setChild("thenBody",r.thenBody),r._setChild("elseBody",r.elseBody),r;case"BufferStoreNode":return r.buffer=n.buffer,r.indices=n.indices.map(e),r.value=e(n.value),r;case"BufferLoadNode":return r.buffer=n.buffer,r.indices=n.indices.map(e),r;case"MathOpNode":return r.op=n.op,r.a=e(n.a),r.b=e(n.b),r;case"CompareNode":return r.direction=n.direction,r.a=e(n.a),r.b=e(n.b),r;case"CastNode":return r.expr=e(n.expr),r.fromDtype=n.fromDtype,r.toDtype=n.toDtype,r;case"CallExternNode":return r.externName=n.externName,r.args=n.args.map(e),r.dtype=n.dtype,r;default:return t(n,r,e)}}var Gx=M(()=>{"use strict";l(Oy,"cloneIRShared")});function ia(n,e,t){return Gi(n,r=>r.type==="VariableNode"&&r.name===e?t():r,{bindVars:!1})}function k3(n,e,t,r){return Gi(n,s=>s.type==="BufferLoadNode"&&s.buffer&&s.buffer.name===e?(r.n++,t(s)):s,{bindVars:!1})}function jx(n,e){return kp(n,t=>t.type==="BufferLoadNode"&&t.buffer&&t.buffer.name===e)}function Xl(n,e){if(!n||typeof n!="object")return!1;if((n.type==="BufferLoadNode"||n.type==="BufferStoreNode")&&n.indices){for(let t of n.indices)if(jx(t,e))return!0}for(let t of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])if(n[t]&&Xl(n[t],e))return!0;if(n.args){for(let t of n.args)if(Xl(t,e))return!0}if(n.indices){for(let t of n.indices)if(Xl(t,e))return!0}if(n.stmts){for(let t of n.stmts)if(Xl(t,e))return!0}return!1}function rn(n){return Oy(n,rn,(e,t,r)=>{if(e.type==="BlockRealizeNode")return t.iterVar=e.iterVar,t.binding=r(e.binding),t;for(let s of Object.keys(e))s==="_parent"||s==="_parentKey"||s==="_parentIdx"||(t[s]=e[s]);return t})}function By(n,e){if(n){if(n.type==="BufferStoreNode"){e.push(n);return}if(n.type==="SeqNode"){for(let t of n.stmts)By(t,e);return}(n.type==="ForNode"||n.type==="BlockNode")&&By(n.body,e)}}function gM(n){return n.indices.map(e=>e&&e.type==="VariableNode"?e.name:null)}function yM(n,e,t){let r=e.indices.map(i=>i.name),s=r.map((i,a)=>`__inl_${t}_${a}`),o={n:0};return k3(n,e.buffer.name,i=>{let a=rn(e.value);for(let u=0;u<r.length;u++)a=ia(a,r[u],()=>new Oe(s[u],"int32"));for(let u=0;u<r.length;u++)a=ia(a,s[u],()=>rn(i.indices[u]));return a},o),o.n}function _M(n,e){let t=[n];for(;t.length>0;){let r=t.pop();if(!(!r||typeof r!="object")){if(r.type==="BlockNode"&&r.reads){let s=r.reads.filter(o=>!(o.buffer&&o.buffer.name===e));s.length!==r.reads.length&&(r.reads=s)}if(r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),r.initBody&&t.push(r.initBody)}}}function fu(n){return n.type==="IntImmNode"?n.value:null}function Ql(n,e="int32"){return new Oe(`${n}_${N3++}`,e)}function xf(n,e,t){if(!(!n||typeof n!="object")){n.type==="BufferLoadNode"&&n.buffer&&e.set(n.buffer.name,n.buffer),n.type==="BufferStoreNode"&&n.buffer&&t.set(n.buffer.name,n.buffer);for(let r of["a","b","value","expr","condition","thenBody","elseBody","body","initBody"])n[r]&&xf(n[r],e,t);if(n.indices)for(let r of n.indices)xf(r,e,t);if(n.args)for(let r of n.args)xf(r,e,t);if(n.stmts)for(let r of n.stmts)xf(r,e,t)}}var v3,N3,Cn,du=M(()=>{"use strict";Fe();ei();rM();Px();hM();mM();Fx();Dx();Gx();ts();v3=new Set(["+","*","min","max"]);l(ia,"substituteVar");l(k3,"replaceBufferLoads");l(jx,"loadsBuffer");l(Xl,"usesBufferInIndex");l(rn,"cloneExprTree");l(By,"collectStores");l(gM,"storeIndexNames");l(yM,"inlineStoreValue");l(_M,"dropBufferReads");l(fu,"getConstExtent");N3=0;l(Ql,"freshVar");Cn=class{static{l(this,"Schedule")}constructor(e){this.func=e,this.trace=new xy,this.state=new Ny(e),this._replaying=!1,this._srefTree=new Zl(e),this.mutator=new Ay(e)}_replaceInTree(e,t){this._srefTree.replaceNode(e,t)||this._srefTree.rebuildFrom(this.func.body),this.state.invalidate()}_removeFromTree(e){this._srefTree.removeNode(e)||this._srefTree.rebuildFrom(this.func.body),this.state.invalidate()}getBlock(e){let t=this._srefTree.getBlockSRef(e);if(!t)throw new Error(`Block '${e}' not found`);return t.node}getLoops(e){return this._srefTree.loopsOf(e).map(t=>t.node)}_resolveLoop(e){if(typeof e!="string")return e;let t=null,r=l(s=>{if(!(!s||typeof s!="object"||t)){if(s.type==="ForNode"&&s.loopVar&&s.loopVar.name===e){t=s;return}if(s.body&&r(s.body),s.initBody&&r(s.initBody),s.stmts)for(let o of s.stmts)r(o);s.thenBody&&r(s.thenBody),s.elseBody&&r(s.elseBody)}},"walk");return r(this.func.body),t||e}split(e,t){e=this._resolveLoop(e);let r=fu(e.extent);if(r===null)throw new Error(`Cannot split loop '${e.loopVar.name}' with non-constant extent`);if(t<=0||!Number.isInteger(t))throw new Error(`Split factor must be a positive integer, got ${t}`);let s=Math.ceil(r/t),o=Ql(`${e.loopVar.name}_o`),i=Ql(`${e.loopVar.name}_i`),a=e.loopVar.name,u=rn(e.body),p=new he(i,new L(0),new L(t),e.kind,u,e.threadTag);if(r%t!==0){let d=new P("+",new P("*",o,new L(t)),i),h=new P("<",d,new L(r)),m=new Se(h,p.body);p.body=m,p._setChild("body",m)}let f=new he(o,new L(0),new L(s),e.kind,p,e.threadTag);return ia(p.body,a,()=>new P("+",new P("*",o,new L(t)),i)),this.mutator.replaceNode(e,f),this._replaceInTree(e,f),this._replaying||this.trace.record("split",[e.loopVar.name,t]),[f,p]}reorder(...e){if(e.length===1&&Array.isArray(e[0])&&(e=e[0]),e=e.map(c=>this._resolveLoop(c)),e.length<2)return;for(let c of e)if(c.type!=="ForNode")throw new Error("reorder expects ForNode arguments");let t=new Set(e);if(t.size!==e.length)throw new Error("reorder: duplicate loop in requested order");let r=null,s=1/0,o=new Map,i=l((c,f)=>{if(c){if(c.type==="ForNode"){t.has(c)&&(o.set(c,f),f<s&&(s=f,r=c)),i(c.body,f+1);return}if(c.type==="SeqNode")for(let d of c.stmts)i(d,f);else c.type==="IfThenElseNode"?i(c.thenBody,f):(c.type==="BlockNode"||c.type==="AllocateNode"||c.type==="LetStmtNode")&&i(c.body,f)}},"findDepths");if(i(this.func.body,0),o.size!==e.length)throw new Error("reorder: not all requested loops were found in the function nest");let{wrappers:a,innermostBody:u}=this._collectReorderNest(t,r);this.mutator.replaceNode(r,e[0]);for(let c of e)c!==e[0]&&(c._parent=null,c._parentKey=null,c._parentIdx=-1);let p=u;for(let c=a.length-1;c>=0;c--)this._setWrapperChild(a[c],p),p=a[c];for(let c=0;c<e.length;c++){let f=c<e.length-1?e[c+1]:p;e[c].body=f,e[c]._setChild("body",f)}this._replaceInTree(r,e[0]),this._replaying||this.trace.record("reorder",[e.map(c=>c.loopVar.name)])}_setWrapperChild(e,t){e.type==="IfThenElseNode"?(e._parent=null,e._parentKey=null,e._parentIdx=-1,e.thenBody=t,e._setChild("thenBody",t)):(e._parent=null,e._parentKey=null,e._parentIdx=-1,e.body=t,e._setChild("body",t))}_collectReorderNest(e,t){let r=new Set(e),s=[],o=t,i=!1,a=null;for(;o;)if(o.type==="ForNode"){if(e.has(o)){if(r.delete(o),i=!0,r.size===0){a=o.body;break}o=o.body;continue}if(i)throw new Error(`reorder: loops are not a perfect nest \u2014 non-reordered loop '${o.loopVar.name}' is interleaved between reordered loops`);o=o.body}else if(o.type==="IfThenElseNode"){if(o.elseBody)throw new Error("reorder: cannot reorder across a conditional with an else-branch");i&&s.push(o),o=o.thenBody}else if(o.type==="AllocateNode"||o.type==="LetStmtNode")i&&s.push(o),o=o.body;else if(o.type==="BlockNode"){if(i)throw new Error("reorder: a compute block separates the reordered loops");o=o.body}else if(o.type==="SeqNode"){if(o.stmts.length!==1)throw new Error("reorder: loops are not a perfect nest \u2014 multiple statements separate the reordered loops");o=o.stmts[0]}else break;if(r.size>0)throw new Error("reorder: loops do not form a single perfect nest");return{wrappers:s,innermostBody:a}}fuseLoops(e,t){if(e=this._resolveLoop(e),t=this._resolveLoop(t),e.type!=="ForNode"||t.type!=="ForNode")throw new Error("fuseLoops expects two ForNode arguments");if(e.body!==t)throw new Error("fuseLoops requires inner loop to be direct child of outer loop");let r=fu(e.extent),s=fu(t.extent);if(r===null||s===null)throw new Error("Cannot fuse loops with non-constant extents");let o=r*s,i=Ql(`${e.loopVar.name}_${t.loopVar.name}_fused`),a=e.loopVar.name,u=t.loopVar.name,p=new he(i,new L(0),new L(o),e.kind,t.body);return ia(p.body,a,()=>new P("//",i,new L(s))),ia(p.body,u,()=>new P("%",i,new L(s))),this.mutator.replaceNode(e,p),this._replaceInTree(e,p),this._replaying||this.trace.record("fuseLoops",[a,u]),p}tile(e,t,r){if(t.length!==r.length)throw new Error("tile: loopIndices and tileSizes must have same length");let s=this.getLoops(e),o=t.map(f=>{if(f>=s.length)throw new Error(`tile: loop index ${f} out of range`);return s[f]}),i=[],a=[];for(let f=0;f<o.length;f++){let h=this.getLoops(e).find(y=>y.loopVar.name===o[f].loopVar.name||y===o[f]);if(!h)throw new Error(`tile: lost track of loop at index ${f}`);let[m,g]=this.split(h,r[f]);i.push(m),a.push(g)}let u=this.getLoops(e),p=[],c=[];for(let f of u)i.some(d=>d.loopVar.name===f.loopVar.name)?p.push(f):a.some(d=>d.loopVar.name===f.loopVar.name)&&c.push(f);return p.length>0&&c.length>0&&this.reorder(...p,...c),{outerLoops:p,innerLoops:c}}vectorize(e){if(e=this._resolveLoop(e),e.type!=="ForNode")throw new Error("vectorize expects ForNode");if(fu(e.extent)===null)throw new Error("Cannot vectorize loop with non-constant extent");let r=Cx(e);if(r!==null)throw new Error(`Cannot vectorize reduction loop '${e.loopVar.name}' (loop-carried dependency in block '${r}')`);e.kind=z.VECTORIZED,this.state.invalidate(),this._replaying||this.trace.record("vectorize",[e.loopVar.name])}unroll(e){if(e=this._resolveLoop(e),e.type!=="ForNode")throw new Error("unroll expects ForNode");e.kind=z.UNROLLED,this.state.invalidate(),this._replaying||this.trace.record("unroll",[e.loopVar.name])}parallelize(e){if(e=this._resolveLoop(e),e.type!=="ForNode")throw new Error("parallelize expects ForNode");let t=Cx(e);if(t!==null)throw new Error(`Cannot parallelize reduction loop '${e.loopVar.name}' (loop-carried dependency in block '${t}')`);e.kind=z.PARALLEL,this.state.invalidate(),this._replaying||this.trace.record("parallelize",[e.loopVar.name])}bindThread(e,t){if(e=this._resolveLoop(e),e.type!=="ForNode")throw new Error("bindThread expects ForNode");let r=["blockIdx.x","blockIdx.y","blockIdx.z","threadIdx.x","threadIdx.y","threadIdx.z"];if(!r.includes(t))throw new Error(`Invalid thread tag: ${t}. Must be one of: ${r.join(", ")}`);e.kind=z.THREAD_BINDING,e.threadTag=t,this.state.invalidate(),this._replaying||this.trace.record("bindThread",[e.loopVar.name,t])}rfactor(e,t,r){let s=this.getBlock(e),o=this.getLoops(e),i=o.find(C=>C.loopVar.name===t);if(!i)throw new Error(`rfactor: reduction loop '${t}' not found for block '${e}'`);let a=fu(i.extent);if(a===null)throw new Error(`rfactor: reduction loop '${t}' has non-constant extent`);if(!Number.isInteger(r)||r<=1||r>=a||a%r!==0)throw new Error(`rfactor: factor ${r} must divide reduction extent ${a} with 1 < factor < ${a}`);let u=s.body;if(!u||u.type!=="BufferStoreNode"||!u.value||u.value.type!=="MathOpNode")throw new Error(`rfactor: block '${e}' body is not a single accumulating store`);let p=u.buffer,c=u.indices,f=u.value.op,d=l(C=>C&&C.type==="BufferLoadNode"&&C.buffer===p,"isAccLoad"),h;if(d(u.value.a))h=u.value.b;else if(d(u.value.b))h=u.value.a;else throw new Error(`rfactor: accumulator load not found in block '${e}' body`);if(!v3.has(f))throw new Error(`rfactor: op '${f}' is not associative+commutative; cannot factor reduction`);let m=s.initBody&&s.initBody.type==="BufferStoreNode"&&s.initBody.value?s.initBody.value:new L(0),g=o.filter(C=>C.loopVar.name!==t),y=a/r,_=new me(`${p.name}_rf`,[r,...p.shape],p.dtype,p.scope),b=Ql(`${t}_rfi`),w=Ql(`${t}_rfo`),x=Ql(`${t}_rfp`),S=l(C=>[C,...c.map(rn)],"cfIdx"),T=ia(rn(h),t,()=>new P("+",new P("*",w,new L(r)),b)),v=new J(_,S(b),new P(f,new q(_,S(b)),T)),A=new J(_,S(b),rn(m)),N=new fe(`${e}_rf_p`,[],s.reads.map(C=>({buffer:C.buffer})),[{buffer:_}],v,A),k=new he(w,new L(0),new L(y),z.SERIAL,N);k=new he(b,new L(0),new L(r),z.SERIAL,k);for(let C=g.length-1;C>=0;C--)k=new he(g[C].loopVar,new L(0),rn(g[C].extent),z.SERIAL,k);let O=new J(p,c.map(rn),new P(f,new q(p,c.map(rn)),new q(_,S(x)))),R=new J(p,c.map(rn),rn(m)),B=new fe(`${e}_rf_c`,[],[{buffer:_}],[{buffer:p}],O,R),E=new he(x,new L(0),new L(r),z.SERIAL,B);for(let C=g.length-1;C>=0;C--)E=new he(g[C].loopVar,new L(0),rn(g[C].extent),z.SERIAL,E);let F=new te([k,E]);return this.mutator.replaceNode(o[0],F),this._replaceInTree(o[0],F),this._replaying||this.trace.record("rfactor",[e,t,r]),_}decomposeReduction(e){let t=this.getBlock(e);if(!t.initBody)throw new Error(`decomposeReduction: block '${e}' has no initBody`);let r=this.getLoops(e),s=t.body;if(!s||s.type!=="BufferStoreNode")throw new Error(`decomposeReduction: block '${e}' body is not a store`);let o=s.buffer,i=new Set;for(let g of s.indices)Nt(g,i);let a=r.filter(g=>i.has(g.loopVar.name));if(r.filter(g=>!i.has(g.loopVar.name)).length===0)throw new Error(`decomposeReduction: block '${e}' has no reduction loop`);let p=new J(o,s.indices.map(rn),rn(t.initBody.value)),f=new fe(`${e}_init`,[],[],[{buffer:o}],p);for(let g=a.length-1;g>=0;g--)f=new he(a[g].loopVar,new L(0),rn(a[g].extent),z.SERIAL,f);let h=new fe(`${e}_upd`,[],t.reads.map(g=>({buffer:g.buffer})),[{buffer:o}],rn(s));for(let g=r.length-1;g>=0;g--)h=new he(r[g].loopVar,new L(0),rn(r[g].extent),z.SERIAL,h);let m=new te([f,h]);this.mutator.replaceNode(r[0],m),this._replaceInTree(r[0],m),this._replaying||this.trace.record("decomposeReduction",[e])}cacheWrite(e,t,r="local"){let s=this.getBlock(e),o=this.getLoops(e);if(o.length===0)throw new Error("cacheWrite: block has no enclosing loops");let i=(s.writes||[]).find(y=>y.buffer&&y.buffer.name===t);if(!i)throw new Error(`cacheWrite: block '${e}' does not write '${t}'`);let a=i.buffer,u=new me(`${t}_${e}_cachew`,[...a.shape],a.dtype,r);this.mutator.redirectBuffer(s.body,a,u),s.initBody&&this.mutator.redirectBuffer(s.initBody,a,u),i.buffer=u;let p=a.shape.map((y,_)=>new Oe(`${u.name}_o${_}`,"int32")),c=new J(a,p,new q(u,p)),d=new fe(`${u.name}_flush`,p.map(y=>({iterVar:y,binding:y})),[{buffer:u}],[{buffer:a}],c);for(let y=a.shape.length-1;y>=0;y--)d=new he(p[y],new L(0),new L(a.shape[y]),z.SERIAL,d);let h=o[0],m=new te([]),g=new Ue(u,r,m);this.mutator.replaceNode(h,g),m.stmts.push(h,d),this._replaceInTree(h,g),this._replaying||this.trace.record("cacheWrite",[e,t,r])}setScope(e,t,r){let o=(this.getBlock(e).writes||[]).find(i=>i.buffer&&i.buffer.name===t);if(!o)throw new Error(`setScope: block '${e}' does not write '${t}'`);o.buffer.scope=r,this.state.invalidate(),this._replaying||this.trace.record("setScope",[e,t,r])}storageAlign(e,t,r,s,o){let i=this.getBlock(e),a=[...i.writes||[],...i.reads||[]].find(u=>u.buffer&&u.buffer.name===t);if(!a)throw new Error(`storageAlign: block '${e}' does not access '${t}'`);if(!Number.isInteger(s)||s<=0)throw new Error("storageAlign: factor must be a positive integer");a.buffer.storageAlign={axis:r,factor:s,offset:o||0},this.state.invalidate(),this._replaying||this.trace.record("storageAlign",[e,t,r,s,o||0])}_removeBlockNest(e,t){let r=this.getLoops(e),s=r.length>0?r[0]:t,o=new te([]);this.mutator.replaceNode(s,o),this._replaceInTree(s,o)}computeInline(e){let t=this.getBlock(e);if(!t)throw new Error(`computeInline: block '${e}' not found`);if(t.initBody)throw new Error("computeInline: cannot inline a reduction block (has init)");let r=[];By(t.body,r);let s=r[0];if(!s)throw new Error("computeInline: producer has no single store to inline");let o=s.buffer;if(gM(s).some(i=>i===null))throw new Error("computeInline: producer indices must be simple loop variables");if(jx(s.value,o.name))throw new Error("computeInline: producer is self-referential (recurrence), cannot inline");if(Xl(this.func.body,o.name))throw new Error(`computeInline: buffer '${o.name}' is used inside an index expression (indirect), cannot safely inline`);if(yM(this.func.body,s,e)===0)throw new Error(`computeInline: buffer '${o.name}' has no consumers to inline into`);_M(this.func.body,o.name),this._removeBlockNest(e,t),this._replaying||this.trace.record("computeInline",[e])}computeInlineBlock(e){let t=this.getBlock(e);if(!t)throw new Error(`computeInlineBlock: block '${e}' not found`);if(t.initBody)throw new Error("computeInlineBlock: cannot inline a reduction block (has init)");let r=[];if(By(t.body,r),r.length===0)throw new Error("computeInlineBlock: producer has no stores to inline");let s=new Set(r.map(i=>i.buffer.name));if(s.size!==r.length)throw new Error("computeInlineBlock: buffer written more than once in block");for(let i of r){if(gM(i).some(a=>a===null))throw new Error("computeInlineBlock: producer indices must be simple loop variables");for(let a of s)if(jx(i.value,a))throw new Error("computeInlineBlock: producer store depends on a co-produced buffer");if(Xl(this.func.body,i.buffer.name))throw new Error(`computeInlineBlock: buffer '${i.buffer.name}' is used inside an index expression (indirect), cannot safely inline`)}let o=0;for(let i of r)o+=yM(this.func.body,i,`${e}_${i.buffer.name}`);if(o===0)throw new Error(`computeInlineBlock: block '${e}' has no consumers to inline into`);for(let i of s)_M(this.func.body,i);this._removeBlockNest(e,t),this._replaying||this.trace.record("computeInlineBlock",[e])}_relocateBlockToLoop(e,t,r){if(!this.getBlock(e))throw new Error(`computeAt: block '${e}' not found`);let o=this._resolveLoop(t);if(!o||o.type!=="ForNode")throw new Error("computeAt: target must be a loop");let i=this.getLoops(e);if(i.length!==1)throw new Error("computeAt: aligned case requires exactly one enclosing loop on the moved block");let a=i[0];if(a===o)throw new Error("computeAt: block already at target loop");let u=fu(a.extent),p=fu(o.extent);if(u===null||p===null||u!==p)throw new Error("computeAt: aligned case requires equal static extent on the block and target loops");let c=rn(a.body);ia(c,a.loopVar.name,()=>o.loopVar);let f=new te([]);this.mutator.replaceNode(a,f);let d=o.body;if(d&&d.type==="SeqNode")r?d.stmts.unshift(c):d.stmts.push(c);else{let h=r?new te([c,d]):new te([d,c]);o.body=h,o._setChild("body",h)}return this._replaceInTree(a,f),this._replaceInTree(o,o),o.loopVar.name}computeAt(e,t){let r=this._relocateBlockToLoop(e,t,!0);this._replaying||this.trace.record("computeAt",[e,r])}reverseComputeAt(e,t){let r=this._relocateBlockToLoop(e,t,!1);this._replaying||this.trace.record("reverseComputeAt",[e,r])}cacheRead(e,t,r="local"){let s=this.getBlock(e),o=this.getLoops(e);if(o.length===0)throw new Error("cacheRead: block has no enclosing loops");let i=(s.reads||[]).find(y=>y.buffer&&y.buffer.name===t);if(!i)throw new Error(`cacheRead: block '${e}' does not read '${t}'`);let a=i.buffer,u=new me(`${t}_${e}_cache`,[...a.shape],a.dtype,r),p=a.shape.map((y,_)=>new Oe(`${u.name}_i${_}`,"int32")),c=new J(u,p,new q(a,p)),d=new fe(`${u.name}_fill`,p.map(y=>({iterVar:y,binding:y})),[{buffer:a}],[{buffer:u}],c);for(let y=a.shape.length-1;y>=0;y--)d=new he(p[y],new L(0),new L(a.shape[y]),z.SERIAL,d);this.mutator.redirectReads(s.body,a,u),s.initBody&&this.mutator.redirectReads(s.initBody,a,u),i.buffer=u;let h=o[0],m=new te([d]),g=new Ue(u,r,m);this.mutator.replaceNode(h,g),m.stmts.push(h),this._replaceInTree(h,g),this._replaying||this.trace.record("cacheRead",[e,t,r])}fuseConsumer(e,t){let r=this.getBlock(e),s=this.getBlock(t),o=this.getLoops(e),i=this.getLoops(t);if(!r.body||r.body.type!=="BufferStoreNode")throw new Error(`fuseConsumer: producer '${e}' body is not a store`);let a=new Set;for(let g of r.body.indices)Nt(g,a);let u=o.filter(g=>a.has(g.loopVar.name));if(u.length===0||i.length!==u.length)throw new Error(`fuseConsumer: producer/consumer spatial rank mismatch (${u.length} vs ${i.length})`);if(!i[0]._parent||i[0]._parent.type!=="SeqNode")throw new Error("fuseConsumer: consumer loop nest is not a direct SeqNode sibling; cannot fuse without duplicating it");let p=u[u.length-1],c=rn(p.body),f=rn(s.body);for(let g=0;g<i.length;g++){let y=u[g].loopVar.name;f=ia(f,i[g].loopVar.name,()=>new Oe(y,"int32"))}let d=new fe(`${t}_fused`,[],s.reads.map(g=>({buffer:g.buffer})),s.writes.map(g=>({buffer:g.buffer})),f),h=new te([c,d]);for(let g=u.length-1;g>=0;g--){let y=u[g];h=new he(y.loopVar,new L(0),rn(y.extent),y.kind,h,y.threadTag)}let m=i[0];this.mutator.replaceNode(o[0],h),this.mutator.removeNode(m),this._replaceInTree(o[0],h),this._removeFromTree(m),this._replaying||this.trace.record("fuseConsumer",[e,t])}annotate(e,t,r){if(e=this._resolveLoop(e),e.type!=="ForNode")throw new Error("annotate expects ForNode");e.annotations||(e.annotations={}),e.annotations[t]=r,this.state.invalidate(),this._replaying||this.trace.record("annotate",[e.loopVar.name,t,r])}tensorize(e,t){if(typeof e!="string")throw new Error("tensorize expects an intrinsic name");if(!t||typeof t.M!="number"||typeof t.N!="number"||typeof t.K!="number")throw new Error("tensorize expects info { M, N, K, a, b, c }");this.func._tensorIntrin={name:e,info:t},this.state.invalidate()}blockize(e,t=null){let r=this._resolveLoop(e);if(!r||r.type!=="ForNode")throw new Error("blockize expects a loop");let s=new Map,o=new Map;xf(r,s,o);let i=t||`blockized_${r.loopVar.name}`,a=new fe(i,[],[...s.values()].map(u=>({buffer:u})),[...o.values()].map(u=>({buffer:u})),new te([]));return this.mutator.replaceNode(r,a),a.body=r,a._setChild("body",r),this._replaceInTree(r,a),this._replaying||this.trace.record("blockize",[r.loopVar.name]),a}getTrace(){return this.trace}verify(){return Jl.validate(this.func)}};l(xf,"collectBufferAccess")});function Tt(n,e){let t=Ry.get(n);return t&&t.has(e)?t.get(e):(t||(t=new Map,Ry.set(n,t)),O3(n.body,t,[]),t.has(e)?t.get(e):null)}function Xx(n){n&&Ry.delete(n)}function A3(n,e){let t=Ry.get(n);t&&t.delete(e)}function bM(n,e){let t=[n];for(;t.length>0;){let r=t.pop();if(!(!r||typeof r!="object")){if(r.type==="VariableNode"){e.add(r.name);continue}for(let s of["a","b","condition","thenBody","elseBody","expr","value","offsetExpr","extent"])r[s]&&t.push(r[s]);if(r.indices)for(let s of r.indices)t.push(s);if(r.args)for(let s of r.args)t.push(s)}}}function wM(n){let e=new Set((n.writes||[]).map(o=>o.buffer&&o.buffer.name)),t=new Set,r=[n.body,n.initBody];for(;r.length>0;){let o=r.pop();if(o){if(o.type==="BufferStoreNode"&&o.buffer&&e.has(o.buffer.name))for(let i of o.indices)bM(i,t);if(o.body&&r.push(o.body),o.stmts)for(let i of o.stmts)r.push(i);o.thenBody&&r.push(o.thenBody),o.elseBody&&r.push(o.elseBody),o.value&&r.push(o.value)}}let s=new Set;for(let o of n.iterVars||[]){let i=new Set;if(bM(o.binding,i),!(o.iterVar&&t.has(o.iterVar.name)||[...i].some(u=>t.has(u))))for(let u of i)s.add(u)}return s}function O3(n,e,t){let r=[{node:n,loops:[...t]}];for(;r.length>0;){let{node:s,loops:o}=r.pop();if(s)if(s.type==="ForNode")r.push({node:s.body,loops:[...o,s]});else if(s.type==="BlockNode"){if(!e.has(s.name)){let i=wM(s);e.set(s.name,{loopCount:o.length,hasReduction:s.initBody!==null||i.size>0,reductionLoopVars:i,readBuffers:s.reads.map(a=>a.buffer.name),writeBuffers:s.writes.map(a=>a.buffer.name),loops:[...o]})}r.push({node:s.body,loops:o})}else if(s.type==="SeqNode")for(let i=s.stmts.length-1;i>=0;i--)r.push({node:s.stmts[i],loops:o});else s.type==="IfThenElseNode"?(s.elseBody&&r.push({node:s.elseBody,loops:o}),r.push({node:s.thenBody,loops:o})):s.type==="AllocateNode"?r.push({node:s.body,loops:o}):s.type==="LetStmtNode"&&r.push({node:s.body,loops:o})}}function TM(n){return n.hasReduction&&n.readBuffers.length===2&&n.writeBuffers.length===1&&n.loopCount>=3}function B3(n,e){let t=Tt(n,e);return t?t.loops.some(r=>r.extent&&r.extent.type!=="IntImmNode"):!1}function xM(n){let e=0,t=[n.body];for(;t.length>0;){let r=t.pop();if(r){if(r.type==="BlockNode"&&(e++,e>1))return!0;if(r.type==="ForNode"&&t.push(r.body),r.type==="SeqNode")for(let s of r.stmts)t.push(s)}}return!1}function eS(n){return kp(n.body,e=>e.type==="BlockNode"&&(e.initBody!==null||wM(e).size>0),{kinds:"stmt"})}function tS(n){return kp(n.body,e=>e.type==="ForNode"&&e.kind===z.RECURRENCE,{kinds:"stmt"})}function nS(n,e,t){let r=Math.min(t.maxThreadsPerBlock,256),s=e.extent;if(s.type==="IntImmNode"&&s.value>r){let[o,i]=n.split(e,r);n.bindThread(i,"threadIdx.x"),tS(n.func)||n.bindThread(o,"blockIdx.x")}else n.bindThread(e,"threadIdx.x")}function ec(n,e){return e.reductionLoopVars?e.reductionLoopVars.has(n.loopVar.name):!1}function rS(n,e){return n.body===e}function R3(n){return yl(n,e=>e.type==="BlockNode",{kinds:"stmt"}).map(e=>e.name)}var eo,Ry,Ux,qx,Wx,Kx,Hx,Yx,Jx,Zx,Qx,to,Do=M(()=>{"use strict";yo();Fe();ts();eo=class{static{l(this,"ScheduleRule")}constructor(e){this.name=e}matches(e,t,r){throw new Error("ScheduleRule.matches must be implemented")}apply(e,t,r){throw new Error("ScheduleRule.apply must be implemented")}},Ry=new WeakMap;l(Tt,"classifyBlock");l(Xx,"invalidateClassifyCache");l(A3,"invalidateClassifyBlock");l(bM,"collectVarNames");l(wM,"computeReductionLoopVars");l(O3,"collectBlockInfo");l(TM,"isMatmulShape");l(B3,"blockHasNonConstExtent");l(xM,"hasMultipleBlocks");Ux=class extends eo{static{l(this,"ElementwiseCPURule")}constructor(){super("elementwise_cpu")}matches(e,t,r){if(r.kind!==Dt.CPU)return!1;let s=Tt(e,t);if(!s||s.hasReduction||s.loopCount<1||s.loops.length>0&&xM(s.loops[0]))return!1;let o=1;for(let i of s.loops){let a=i.extent&&i.extent.type==="IntImmNode"?i.extent.value:1;o*=a}return o>=r.numCores*r.vectorWidth}apply(e,t,r){let s=e.getLoops(t);if(s.length===0)return;if(s.length===1){let a=s[0].extent;if(a.type==="IntImmNode"&&a.value>=r.vectorWidth*2){let[u,p]=e.split(s[0],r.vectorWidth);e.parallelize(u),e.vectorize(p);return}e.parallelize(s[0]);return}e.parallelize(s[0]);let o=s[s.length-1],i=o.extent;if(i.type==="IntImmNode"&&i.value>=r.vectorWidth&&i.value%r.vectorWidth===0){let[a,u]=e.split(o,r.vectorWidth);e.vectorize(u)}}};l(eS,"primFuncHasReduction");l(tS,"primFuncHasRecurrence");l(nS,"bindFusedSpatialGPU");qx=class extends eo{static{l(this,"ElementwiseGPURule")}constructor(){super("elementwise_gpu")}matches(e,t,r){if(!r.isGPU())return!1;let s=Tt(e,t);return s?!s.hasReduction&&s.loopCount>=1:!1}apply(e,t,r){let s=e.getLoops(t);if(s.length===0)return;let o=s[0];for(let p=1;p<s.length;p++){let f=e.getLoops(t).find(d=>d.loopVar.name===s[p].loopVar.name);f&&rS(o,f)&&(o=e.fuseLoops(o,f))}let i=o.extent;if(i.type!=="IntImmNode"){e.bindThread(o,"threadIdx.x");return}let a=i.value,u=Math.min(r.maxThreadsPerBlock,1024);if(eS(e.func)&&a<=u){e.bindThread(o,"threadIdx.x");return}nS(e,o,r)}},Wx=class extends eo{static{l(this,"ReductionCPURule")}constructor(){super("reduction_cpu")}matches(e,t,r){if(r.kind!==Dt.CPU)return!1;let s=Tt(e,t);return s?s.hasReduction:!1}apply(e,t,r){let s=e.getLoops(t);if(s.length===0)return;let o=[],i=[],a=Tt(e.func,t);for(let u of s)a&&ec(u,a)?i.push(u):o.push(u);o.length>0&&e.parallelize(o[0])}},Kx=class extends eo{static{l(this,"ReductionGPURule")}constructor(){super("reduction_gpu")}matches(e,t,r){if(!r.isGPU())return!1;let s=Tt(e,t);return s?s.hasReduction:!1}apply(e,t,r){let s=e.getLoops(t);if(s.length===0)return;let o=[],i=Tt(e.func,t);for(let u of s)(!i||!ec(u,i))&&o.push(u);if(o.length===0)return;let a=o[0];for(let u=1;u<o.length;u++){let c=e.getLoops(t).find(f=>f.loopVar.name===o[u].loopVar.name);c&&rS(a,c)&&(a=e.fuseLoops(a,c))}nS(e,a,r)}},Hx=class extends eo{static{l(this,"MatmulTiledCPURule")}constructor(){super("matmul_tiled_cpu")}matches(e,t,r){if(r.kind!==Dt.CPU)return!1;let s=Tt(e,t);if(!s||!TM(s))return!1;let o=r.l1CacheBytes||32768,i=Math.max(8,Math.min(64,Math.floor(Math.sqrt(o/4))));return s.loops.reduce((u,p)=>{let c=p.extent&&p.extent.type==="IntImmNode"?p.extent.value:0;return c>u?c:u},0)>=i}apply(e,t,r){let s=e.getLoops(t);if(s.length<3)return;let o=r.l1CacheBytes||32768,i=Math.max(8,Math.min(64,Math.floor(Math.sqrt(o/4)))),a=[],u=[];for(let c=0;c<Math.min(2,s.length);c++){let f=s[c].extent.type==="IntImmNode"?s[c].extent.value:null;f&&f>=i&&(a.push(c),u.push(i))}if(a.length===0)return;let{outerLoops:p}=e.tile(t,a,u);p.length>0&&e.parallelize(p[0])}},Yx=class extends eo{static{l(this,"MatmulTiledGPURule")}constructor(){super("matmul_tiled_gpu")}matches(e,t,r){if(!r.isGPU())return!1;let s=Tt(e,t);if(!s||!TM(s))return!1;let o=r.sharedMemoryBytes||49152,a=Math.max(16,Math.min(128,Math.floor(Math.sqrt(o/8))));return s.loops.reduce((p,c)=>{let f=c.extent&&c.extent.type==="IntImmNode"?c.extent.value:0;return f>p?f:p},0)>=a}apply(e,t,r){let s=e.getLoops(t);if(s.length<3)return;let o=Tt(e.func,t),i=s.filter(u=>!o||!ec(u,o));if(i.length===0)return;let a=i[0];for(let u=1;u<i.length;u++){let c=e.getLoops(t).find(f=>f.loopVar.name===i[u].loopVar.name);c&&rS(a,c)&&(a=e.fuseLoops(a,c))}nS(e,a,r)}},Jx=class extends eo{static{l(this,"ElementwiseWasmRule")}constructor(){super("elementwise_wasm")}matches(e,t,r){if(r.kind!==Dt.WASM)return!1;let s=Tt(e,t);return!(!s||s.hasReduction||s.loopCount<1||s.loops.length>0&&xM(s.loops[0]))}apply(e,t,r){let s=e.getLoops(t);if(s.length===0)return;let o=r.numCores||1,i=r.vectorWidth||4;if(o>1&&s.length>=1){let p=s[0].extent,c=p&&p.type==="IntImmNode"?p.value:0;if(c>=o*4){if(s.length===1&&r.supportsSimd&&r.supportsSimd()&&c>=i*2&&c%i===0){let[f,d]=e.split(s[0],i);e.parallelize(f),e.vectorize(d);return}if(e.parallelize(s[0]),s.length>1){let f=s[s.length-1],d=f.extent;if(d.type==="IntImmNode"&&d.value>=i&&d.value%i===0){let[,h]=e.split(f,i);e.vectorize(h)}}return}}let a=s[s.length-1],u=a.extent;if(u.type==="IntImmNode"&&u.value>=i*2){let[p,c]=e.split(a,i);e.vectorize(c);return}u.type==="IntImmNode"&&u.value>=i&&e.vectorize(a)}},Zx=class extends eo{static{l(this,"ReductionWasmRule")}constructor(){super("reduction_wasm")}matches(e,t,r){if(r.kind!==Dt.WASM)return!1;let s=r.numCores>1,o=r.supportsSimd&&r.supportsSimd();if(!s&&!o)return!1;let i=Tt(e,t);return i?i.hasReduction&&i.loopCount>=2:!1}apply(e,t,r){let s=e.getLoops(t);if(s.length<2)return;let o=Tt(e.func,t),i=[],a=[];for(let u of s)!o||!ec(u,o)?i.push(u):a.push(u);if(i.length>0){let u=i[0].extent;(u&&u.type==="IntImmNode"?u.value:0)>=(r.numCores||1)*4&&e.parallelize(i[0])}if(r.supportsSimd&&r.supportsSimd()&&a.length>0){let u=a[a.length-1];(u.extent&&u.extent.type==="IntImmNode"?u.extent.value:0)>=r.vectorWidth*2&&e.vectorize(u)}}},Qx=class extends eo{static{l(this,"FallbackRule")}constructor(){super("fallback")}matches(){return!0}apply(e,t,r){let s=e.getLoops(t);s.length!==0&&r.isCPU()&&s.length>=1&&e.parallelize(s[0])}};l(ec,"isReductionLoop");l(rS,"findDirectChild");to=class n{static{l(this,"SchedulePolicy")}constructor(e,t=null,r=null){this.target=e,this.rules=t||n.defaultRules(),this.trace=r}static defaultRules(){return[new Hx,new Yx,new Wx,new Kx,new Zx,new Ux,new qx,new Jx,new Qx]}selectRule(e,t){for(let r of this.rules)if(r.matches(e,t,this.target))return r;return null}applyToBlock(e,t){if(this.target.isGPU()&&B3(e.func,t))return this._explain(t,"none","block has dynamic loop extents; runs sequentially (no dynamic grid)"),null;let r=this.selectRule(e.func,t);return r?(r.apply(e,t,this.target),A3(e.func,t),this._explain(t,r.name,`matched rule '${r.name}' for ${this.target.name}`),r.name):(this._explain(t,"none","no schedule rule matched; runs sequentially"),null)}_explain(e,t,r){this.trace&&this.trace.explainsEnabled&&this.trace.explain("schedule",e,t,r,{target:this.target.name})}applyToAllBlocks(e){Xx(e.func);let t=R3(e.func.body),r=new Set,s=new Map;for(let o of t){if(r.has(o))continue;r.add(o);let i=this.applyToBlock(e,o);i&&s.set(o,i)}return s}};l(R3,"collectAllBlockNames")});function My(n,e,t,r){if(M3[e])return I3(n,e,t,r);let s=L3[e];return s?s(n,t,r||{}):typeof n[e]=="function"?E3(n,e,t,r):n._inferAndBuild(e,t,r||null)}function I3(n,e,t,r){let s=t[0].type.rank,o=r?.dim,i=o!=null?(Array.isArray(o)?o:[o]).map(f=>f<0?s+f:f):Array.from({length:s},(f,d)=>d),a=n.scalarConstant(MN(e,t[0].type.dtype),t[0].type.dtype),u=n.reduce(t[0],a.getResult(0),i,e);if(!r?.keepdim)return u;let p=new Set(i),c=t[0].type.shape.map((f,d)=>p.has(d)?1:f);return n.reshape(u.getResult(0),c)}function E3(n,e,t,r){return t.length===1?De(n,e,t[0]):t.length===2?De(n,e,t[0],t[1]):t.length===3?De(n,e,t[0],t[1],t[2]):n._inferAndBuild(e,t,r||null)}function De(n,e,...t){return n[e].call(n,...t)}function bi(n,e){return n<0?e+n:n}function C3(n,e){let t=1,r=-1,s=1;for(let i of e)s*=i;let o=[...n];for(let i=0;i<o.length;i++)o[i]===-1?r=i:t*=o[i];return r>=0&&(o[r]=t===0?0:s/t),o}function SM(n,e){return n<0?e+1+n:n}function D3(n,e,t,r,s){let o=n.length,i=bi(e,o),a=n[i],u=t??0,p=r??a,c=s??1;u<0&&(u+=a),p<0&&(p+=a),u=Math.max(0,Math.min(u,a)),p=Math.max(0,Math.min(p,a));let f=new Array(o).fill(0),d=[...n],h=new Array(o).fill(1);return f[i]=u,d[i]=p,h[i]=c,{starts:f,limits:d,strides:h}}var M3,L3,sS=M(()=>{"use strict";_s();Re();tr();M3=Object.freeze({sum:"sum",mean:"mean",max:"max",min:"min",prod:"prod"});l(My,"buildMappedOp");l(I3,"buildReduce");l(E3,"callBuilder");l(De,"callMethod");L3=Object.freeze({matmul:l((n,e)=>De(n,"matmul",e[0],e[1]),"matmul"),dot:l((n,e)=>De(n,"dot",e[0],e[1],[e[0].type.rank-1],[0]),"dot"),clone:l((n,e)=>n._inferAndBuild("add",[e[0],n.scalarConstant(0,e[0].type.dtype).getResult(0)],null),"clone"),relu:l((n,e)=>De(n,"relu",e[0]),"relu"),sigmoid:l((n,e)=>De(n,"sigmoid",e[0]),"sigmoid"),gelu:l((n,e)=>De(n,"gelu",e[0]),"gelu"),silu:l((n,e)=>De(n,"silu",e[0]),"silu"),softmax:l((n,e,t)=>De(n,"softmax",e[0],t?.dim??-1),"softmax"),log_softmax:l((n,e,t)=>De(n,"logSoftmax",e[0],t?.dim??-1),"log_softmax"),layer_norm:l((n,e,t)=>De(n,"layernorm",e[0],e[1],e[2],t?.axis??-1,t?.eps??1e-5),"layer_norm"),batch_norm:l((n,e,t)=>De(n,"batchnorm",e[0],e[1],e[2],e[3],e[4],t?.axis??1,t?.eps??1e-5),"batch_norm"),conv2d:l((n,e,t)=>De(n,"conv",e[0],e[1],t?.strides??[1,1],t?.padding??[[0,0],[0,0]],{dilation:t?.dilation??[1,1],groups:t?.groups??1}),"conv2d"),pool2d:l((n,e,t)=>De(n,"pool2d",e[0],t?.pool_type??"max",t?.kernel_size??[2,2],t?.strides??[2,2],t?.padding??[[0,0],[0,0]]),"pool2d"),embedding:l((n,e)=>De(n,"embedding",e[0],e[1]),"embedding"),argmax:l((n,e,t)=>De(n,"argmax",e[0],t?.dim??0,t?.keepdim??!1),"argmax"),argmin:l((n,e,t)=>De(n,"argmin",e[0],t?.dim??0,t?.keepdim??!1),"argmin"),maximum:l((n,e)=>De(n,"maximum",e[0],e[1]),"maximum"),minimum:l((n,e)=>De(n,"minimum",e[0],e[1]),"minimum"),eq:l((n,e)=>De(n,"compare",e[0],e[1],"eq"),"eq"),ne:l((n,e)=>De(n,"compare",e[0],e[1],"ne"),"ne"),lt:l((n,e)=>De(n,"compare",e[0],e[1],"lt"),"lt"),le:l((n,e)=>De(n,"compare",e[0],e[1],"le"),"le"),gt:l((n,e)=>De(n,"compare",e[0],e[1],"gt"),"gt"),ge:l((n,e)=>De(n,"compare",e[0],e[1],"ge"),"ge"),clamp:l((n,e)=>De(n,"clamp",e[1],e[0],e[2]),"clamp"),pad:l((n,e,t)=>De(n,"pad",e[0],e[1],t.low,t.high),"pad"),one_hot:l((n,e,t)=>De(n,"oneHot",e[0],t.depth,{dtype:"f32"}),"one_hot"),index_select:l((n,e,t)=>De(n,"gather",e[0],e[1],xA(e[0].type,t?.dim??0,e[1].type.rank)),"index_select"),gather:l((n,e,t)=>De(n,"gatherDim",e[0],e[1],t?.dim??0),"gather"),scatter_add:l((n,e,t)=>De(n,"scatterAddDim",e[0],e[1],e[2],t?.dim??0),"scatter_add"),cat:l((n,e,t)=>De(n,"concat",e,bi(t?.dim??0,e[0].type.rank)),"cat"),stack:l((n,e,t)=>{let r=SM(t?.dim??0,e[0].type.rank),s=e.map(o=>{let i=[...o.type.shape];return i.splice(r,0,1),n.reshape(o,i).getResult(0)});return De(n,"concat",s,r)},"stack"),reshape:l((n,e,t)=>n.reshape(e[0],C3(t.shape??t.new_shape,e[0].type.shape)),"reshape"),transpose:l((n,e,t)=>{let r=e[0].type.rank,s=bi(t?.dim0??0,r),o=bi(t?.dim1??1,r),i=Array.from({length:r},(a,u)=>u);return i[s]=o,i[o]=s,n.transpose(e[0],i)},"transpose"),permute:l((n,e,t)=>De(n,"transpose",e[0],t.dims),"permute"),broadcast_in_dim:l((n,e,t)=>De(n,"broadcast",e[0],t.result_shape,t.broadcast_dimensions),"broadcast_in_dim"),expand:l((n,e,t)=>{let r=e[0].type.rank,s=t.shape,o=s.length-r,i=s.map((u,p)=>u===-1?e[0].type.shape[p-o]:u),a=Array.from({length:r},(u,p)=>p+o);return De(n,"broadcast",e[0],i,a)},"expand"),slice:l((n,e,t)=>{let r=D3(e[0].type.shape,t.dim,t.start,t.end,t.step);return De(n,"slice",e[0],r.starts,r.limits,r.strides)},"slice"),unsqueeze:l((n,e,t)=>{let r=[...e[0].type.shape];return r.splice(SM(t.dim,r.length),0,1),n.reshape(e[0],r)},"unsqueeze"),squeeze:l((n,e,t)=>{let r=[...e[0].type.shape];if(t.dim===void 0||t.dim===null)return n.reshape(e[0],r.filter(o=>o!==1));let s=bi(t.dim,r.length);return r[s]===1&&r.splice(s,1),n.reshape(e[0],r)},"squeeze"),narrow:l((n,e,t)=>{let r=bi(t.dim,e[0].type.rank),s=new Array(e[0].type.rank).fill(0),o=[...e[0].type.shape],i=new Array(e[0].type.rank).fill(1);return s[r]=t.start,o[r]=t.start+t.length,De(n,"slice",e[0],s,o,i)},"narrow"),select:l((n,e,t)=>{let r=e[0].type.rank,s=bi(t.dim,r),o=t.index,i=o<0?e[0].type.shape[s]+o:o,a=new Array(r).fill(0),u=[...e[0].type.shape];a[s]=i,u[s]=i+1;let p=De(n,"slice",e[0],a,u,new Array(r).fill(1)).getResult(0);return n.reshape(p,e[0].type.shape.filter((c,f)=>f!==s))},"select"),split:l((n,e,t)=>De(n,"split",e[0],bi(t.dim??0,e[0].type.rank),t.sizes),"split"),chunk:l((n,e,t)=>{let r=bi(t.dim??0,e[0].type.rank),s=e[0].type.shape[r],o=Math.ceil(s/t.chunks),i=[];for(let a=0;a<s;a+=o)i.push(Math.min(o,s-a));return De(n,"split",e[0],r,i)},"chunk")});l(bi,"normalizeDim");l(C3,"resolveShape");l(SM,"normalizeInsertDim");l(D3,"sliceAttrs")});function $3(n,e,t,r){let s=n;for(let o=0;o<e.length;o++)s+="|"+e[o].shape.join(",")+":"+e[o].dtype;if(t)for(let[o,i]of Object.entries(t))s+="|"+o+"="+JSON.stringify(i);return s+="|"+r.name,s}function F3(n){let e=kM.get(n);return e||(e=new jl("jit_"+n),kM.set(n,e)),e}function z3(n){let e=1;for(let t of n.shape)e*=typeof t=="number"&&t>0?t:1;return Math.max(e,1)}function V3(n,e,t){let r=[];for(let[,s]of t.bufferMap)r.push(new(St(s.dtype))(z3(s)));n.run(e.name,...r)}function G3(n,e,t,r){try{let s=NM(n,e);if(s.shapeParams&&s.shapeParams.length>0)return null;new to(e).applyToAllBlocks(new Cn(s));let o=t.compile(s);return r.addCompiledKernel(o),e.isWebGPU()||V3(r,o,s),o}catch{return null}}function U3(n,e,t){let r=e.map(a=>new I(a.shape,a.dtype)),s=n+"_jit_"+j3++,o=mp(s,r,[],(a,u)=>{let p;p=My(a,n,u,t),a.returnOp([p.getResult(0)])}),i=o.getReturnOp();return i&&i.operands.length>0&&(o.outputTypes=Object.freeze([i.operands[0].type])),o}function AM(n,e,t=null,r){let s=$3(n,e,t,r),o=vM.get(s);if(o)return o;let i=U3(n,e,t),a=new Mn(n+"_jit_mod");a.addFunction(i);let u=new iu;u.addPass(new ql),u.addPass(new au),u.addPass(new Co),u.run(a);let p=F3(r.name),c=new P3(r),d=typeof r.isGPU=="function"&&r.isGPU()?G3(i,r,c,p):null;d||(d=c.compile(NM(i,r)),p.addCompiledKernel(d));let h=i.getReturnOp(),m=h&&h.operands.length>0?h.operands[0].type.dtype:null;return o={funcName:d.name,runtime:p,numInputs:e.length,outDtype:m,compiled:d},vM.set(s,o),o}var vM,kM,NM,P3,j3,OM=M(()=>{"use strict";_s();Di();Re();$a();Ep();dy();by();Bx();Mx();Ex();du();Do();rs();sS();vM=new Map,kM=new Map,NM=Tm,P3=bo;l($3,"_cacheKey");l(F3,"_getRuntime");l(z3,"_bufferNumel");l(V3,"_trialLaunch");l(G3,"_compileScheduledGPU");j3=0;l(U3,"_buildGraphFunc");l(AM,"jitCompile")});function Iy(n){return n.slice(0,n.indexOf("(")).split(".")[0].trim()}function BM(){return Ey.map(n=>n.schema)}function Ly(n){return W3.get(n)||null}function K3(n){let e=n.indexOf("(")+1,t=n.lastIndexOf(")"),r=n.slice(e,t).trim();if(!r)return[];let s=[];for(let o of RM(r)){let i=o.trim(),a=i.lastIndexOf(" ");if(a<0)continue;let u=i.slice(0,a).trim();if(u==="Tensor"||u==="Tensor[]")continue;let p=i.slice(a+1).trim().replace(/=.*/,"").trim();s.push(p)}return s}function RM(n){let e=[],t=0,r=0;for(let s=0;s<n.length;s++){let o=n[s];o==="("||o==="["?t++:o===")"||o==="]"?t--:o===","&&t===0&&(e.push(n.slice(r,s)),r=s+1)}return e.push(n.slice(r)),e}function H3(n){let t=n.lastIndexOf("->"),r=t>=0?n.slice(t+2).trim():"Tensor";return r==="Tensor"?{kind:"tensor",arity:1}:r==="Tensor[]"?{kind:"tensor_list"}:r.startsWith("(")&&r.endsWith(")")?{kind:"tuple",arity:RM(r.slice(1,-1)).length}:{kind:"value",arity:1}}var Ey,q3,W3,Yee,Cy=M(()=>{"use strict";Ey=Object.freeze([{schema:"add(Tensor self, Tensor other) -> Tensor"},{schema:"sub(Tensor self, Tensor other) -> Tensor"},{schema:"mul(Tensor self, Tensor other) -> Tensor"},{schema:"div(Tensor self, Tensor other) -> Tensor"},{schema:"neg(Tensor self) -> Tensor"},{schema:"pow(Tensor self, Tensor exponent) -> Tensor"},{schema:"rem(Tensor self, Tensor other) -> Tensor"},{schema:"maximum(Tensor self, Tensor other) -> Tensor"},{schema:"minimum(Tensor self, Tensor other) -> Tensor"},{schema:"exp(Tensor self) -> Tensor"},{schema:"log(Tensor self) -> Tensor"},{schema:"sqrt(Tensor self) -> Tensor"},{schema:"rsqrt(Tensor self) -> Tensor"},{schema:"abs(Tensor self) -> Tensor"},{schema:"sin(Tensor self) -> Tensor"},{schema:"cos(Tensor self) -> Tensor"},{schema:"tanh(Tensor self) -> Tensor"},{schema:"erf(Tensor self) -> Tensor"},{schema:"erfc(Tensor self) -> Tensor"},{schema:"lgamma(Tensor self) -> Tensor"},{schema:"gamma(Tensor self) -> Tensor"},{schema:"sigmoid(Tensor self) -> Tensor"},{schema:"relu(Tensor self) -> Tensor"},{schema:"gelu(Tensor self) -> Tensor"},{schema:"silu(Tensor self) -> Tensor"},{schema:"sign(Tensor self) -> Tensor"},{schema:"floor(Tensor self) -> Tensor"},{schema:"ceil(Tensor self) -> Tensor"},{schema:"eq(Tensor self, Tensor other) -> Tensor"},{schema:"ne(Tensor self, Tensor other) -> Tensor"},{schema:"lt(Tensor self, Tensor other) -> Tensor"},{schema:"le(Tensor self, Tensor other) -> Tensor"},{schema:"gt(Tensor self, Tensor other) -> Tensor"},{schema:"ge(Tensor self, Tensor other) -> Tensor"},{schema:"where(Tensor condition, Tensor self, Tensor other) -> Tensor"},{schema:"clamp(Tensor self, Tensor min, Tensor max) -> Tensor"},{schema:"pad(Tensor self, Tensor value, int[] low, int[] high) -> Tensor"},{schema:"one_hot(Tensor indices, int depth) -> Tensor"},{schema:"index_select(Tensor self, Tensor index, int dim) -> Tensor"},{schema:"gather(Tensor self, Tensor index, int dim) -> Tensor"},{schema:"scatter_add(Tensor self, Tensor index, Tensor src, int dim) -> Tensor"},{schema:"scatter(Tensor self, int dim, Tensor index, Tensor src) -> Tensor"},{schema:"sum(Tensor self, int[] dim, bool keepdim) -> Tensor"},{schema:"mean(Tensor self, int[] dim, bool keepdim) -> Tensor"},{schema:"max(Tensor self, int[] dim, bool keepdim) -> Tensor"},{schema:"min(Tensor self, int[] dim, bool keepdim) -> Tensor"},{schema:"prod(Tensor self, int[] dim, bool keepdim) -> Tensor"},{schema:"argmax(Tensor self, int dim, bool keepdim) -> Tensor"},{schema:"argmin(Tensor self, int dim, bool keepdim) -> Tensor"},{schema:"matmul(Tensor self, Tensor other) -> Tensor"},{schema:"dot(Tensor self, Tensor other) -> Tensor"},{schema:"cat(Tensor[] tensors, int dim) -> Tensor"},{schema:"stack(Tensor[] tensors, int dim) -> Tensor"},{schema:"clone(Tensor self) -> Tensor"},{schema:"fill(Tensor self, Scalar value) -> Tensor"},{schema:"reshape(Tensor self, int[] shape) -> Tensor",ir:"reshape",scalarArgs:["shape"]},{schema:"transpose(Tensor self, int dim0, int dim1) -> Tensor",ir:"transpose"},{schema:"permute(Tensor self, int[] dims) -> Tensor",ir:"permute"},{schema:"broadcast_in_dim(Tensor self, int[] result_shape, int[] broadcast_dimensions) -> Tensor",ir:"broadcast_in_dim"},{schema:"expand(Tensor self, int[] shape) -> Tensor",ir:"expand",scalarArgs:["shape"]},{schema:"slice(Tensor self, int dim, int start, int end, int step) -> Tensor"},{schema:"unsqueeze(Tensor self, int dim) -> Tensor"},{schema:"squeeze(Tensor self, int dim) -> Tensor"},{schema:"narrow(Tensor self, int dim, int start, int length) -> Tensor"},{schema:"select(Tensor self, int dim, int index) -> Tensor"},{schema:"contiguous(Tensor self) -> Tensor"},{schema:"repeat(Tensor self, int[] reps) -> Tensor"},{schema:"tile(Tensor self, int[] reps) -> Tensor"},{schema:"split(Tensor self, int[] sizes, int dim) -> Tensor[]",returns:{kind:"tensor_list"}},{schema:"chunk(Tensor self, int chunks, int dim) -> Tensor[]",returns:{kind:"tensor_list"}},{schema:"roll(Tensor self, int shift, int dim) -> Tensor"},{schema:"flip(Tensor self, int[] dims) -> Tensor"},{schema:"cumsum(Tensor self, int dim) -> Tensor"},{schema:"sort(Tensor self, int dim, bool descending) -> Tensor"},{schema:"argsort(Tensor self, int dim, bool descending) -> Tensor"},{schema:"topk(Tensor self, int k, int dim, bool largest) -> (Tensor, Tensor)",returns:{kind:"tuple",arity:2}},{schema:"softmax(Tensor self, int dim) -> Tensor"},{schema:"log_softmax(Tensor self, int dim) -> Tensor"},{schema:"layer_norm(Tensor input, Tensor weight, Tensor bias, int axis, float eps) -> Tensor"},{schema:"batch_norm(Tensor input, Tensor weight, Tensor bias, Tensor mean, Tensor var, int axis, float eps) -> Tensor"},{schema:"conv2d(Tensor input, Tensor weight, int[] strides, int[] padding, int[] dilation, int groups) -> Tensor"},{schema:"pool2d(Tensor input, str pool_type, int[] kernel_size, int[] strides, int[] padding) -> Tensor"},{schema:"embedding(Tensor weight, Tensor indices) -> Tensor"}]),q3=new Map(Ey.filter(n=>n.scalarArgs).map(n=>[Iy(n.schema),n.scalarArgs])),W3=new Map(Ey.map(n=>[Iy(n.schema),q3.get(Iy(n.schema))||K3(n.schema)])),Yee=new Map(Ey.map(n=>{let e=Iy(n.schema);return[e,Object.freeze({...n,name:e,returns:n.returns||H3(n.schema)})]}));l(Iy,"opNameFromSchema");l(BM,"tensorOpSchemas");l(Ly,"scalarArgNames");l(K3,"scalarArgNamesFromSchema");l(RM,"splitTopLevel");l(H3,"returnsFromSchema")});var XR={};at(XR,{getCudnnGRU:()=>uS,getCudnnLSTM:()=>aS,getGpuAdamFn:()=>lS,getGpuContiguousFn:()=>ZT,getGpuMatmul:()=>zT,getWebgpuRNN:()=>Sf,gpuContiguousArray:()=>YT,registerJITKernels:()=>pS,setCudnnGRU:()=>ax,setCudnnLSTM:()=>ix,setGpuAdamFn:()=>ux,setGpuConcatFn:()=>ox,setGpuContiguousFn:()=>sx,setGpuMatmul:()=>lx,setWebGPUEagerFn:()=>eV,setWebgpuRNN:()=>oV,tensorToContiguous:()=>Qe,wrapResult:()=>Ee});function eV(n){IM=n}function oS(n){return typeof n=="object"&&n!==null&&"_impl"in n}function tV(n,e){let t=[],r={},s=Ly(n),o=0;for(let i=0;i<e.length;i++){let a=e[i];if(oS(a))t.push(a);else if(Array.isArray(a)&&a.length>0&&oS(a[0]))for(let u of a)oS(u)&&t.push(u);else a!=null&&(s&&o<s.length&&(r[s[o]]=a),o++)}return{tensors:t,scalars:r}}function hu(n,e){return typeof n=="number"?n:e}function tc(n,e){return Array.isArray(n)?n:e}function MM(n){return Array.isArray(n)?n:[[0,0],[0,0]]}function EM(n,e,t){if(e.length===0)return[];if(new Set(["sum","mean","max","min","prod","argmax","argmin"]).has(n)){let o=e[0].shape,i=t.dim,a=t.keepdim;if(i==null)return a?o.map(()=>1):[];let u=new Set((Array.isArray(i)?i:[i]).map(c=>typeof c=="number"&&c<0?o.length+c:Number(c))),p=[];for(let c=0;c<o.length;c++)u.has(c)?a&&p.push(1):p.push(o[c]);return p}if(n==="matmul")return xg(e[0].shape,e[1].shape)||[];if(n==="dot")return[];if(n==="transpose"){let o=[...e[0].shape],i=hu(t.dim0,0),a=hu(t.dim1,1),u=o[i];return o[i]=o[a],o[a]=u,o}if(n==="conv2d"){let o=e[0].shape,i=e[1].shape,a=tc(t.strides,[1,1]),u=MM(t.padding),p=tc(t.dilation,[1,1]),c=[];for(let f=0;f<2;f++){let d=u[f][0]+u[f][1],h=(i[f+2]-1)*p[f]+1;c.push(Math.floor((o[f+2]+d-h)/a[f])+1)}return[o[0],i[0],...c]}if(n==="pool2d"){let o=e[0].shape,i=tc(t.kernel_size,[2,2]),a=tc(t.strides,i),u=MM(t.padding),p=[];for(let c=0;c<2;c++){let f=u[c][0]+u[c][1];p.push(Math.floor((o[c+2]+f-i[c])/a[c])+1)}return[o[0],o[1],...p]}if(n==="clamp"){let o=[...e[0].shape];for(let i=1;i<e.length;i++)o=Rl(o,e[i].shape)||o;return o}if(n==="pad"){let o=e[0].shape,i=tc(t.low,[]),a=tc(t.high,[]);return o.map((u,p)=>u+(i[p]||0)+(a[p]||0))}if(n==="one_hot")return[...e[0].shape,hu(t.depth,0)];if(n==="cat"){let o=e[0].shape.length,i=hu(t.dim,0),a=i<0?o+i:i,u=[...e[0].shape];return u[a]=e.reduce((p,c)=>p+c.shape[a],0),u}if(n==="stack"){let o=e[0].shape.length,i=hu(t.dim,0),a=i<0?o+1+i:i,u=[...e[0].shape];return u.splice(a,0,e.length),u}if(n==="index_select"){let o=[...e[0].shape],i=o.length,a=hu(t.dim,0),u=a<0?i+a:a;return o[u]=e[1].shape.reduce((p,c)=>p*c,1),o}if(n==="gather")return[...e[1].shape];if(n==="scatter_add")return[...e[0].shape];if(n==="softmax"||n==="log_softmax")return[...e[0].shape];if(n==="layer_norm"||n==="batch_norm")return[...e[0].shape];if(n==="embedding"){let o=e[0].shape;return[...e[1].shape,o[1]]}return e.length===1?[...e[0].shape]:Rl(e[0].shape,e[1].shape)||[...e[0].shape]}function Qe(n){let e=n._impl.storage.data,t=n._impl.storageOffset,r=n.numel;if(n.isContiguous&&t===0&&e.length===r)return n.data||e;let s=n.shape,o=n.strides,i=e.constructor,a=new i(r),u=a,p=s.length,c=new Int32Array(p),f=t;for(let d=0;d<r;d++){u[d]=e[f];for(let h=p-1;h>=0;h--){if(c[h]++,c[h]<s[h]){f+=o[h];break}f-=(s[h]-1)*o[h],c[h]=0}}return a}function Ee(n,e,t,r){let s=En(e),o=Wn.fromData(n,r),i=new Pt(o,0,e,s,t,r);return new Ze(i)}function Dy(n,e,t){let r=1;for(let s=e;s<t;s++)r*=n[s];return r}function nV(n,e,t,r){let s=e.length,o=t<0?s+1+t:t,i=Dy(e,0,o),a=Dy(e,o,s),u=n.length;for(let p=0;p<i;p++)for(let c=0;c<u;c++)r.set(n[c].subarray(p*a,(p+1)*a),(p*u+c)*a)}function rV(n,e,t,r){let s=e[0].length,o=t<0?s+t:t,i=Dy(e[0],0,o),a=Dy(e[0],o+1,s),u=0;for(let p of e)u+=p[o];for(let p=0;p<i;p++){let c=p*u*a;for(let f=0;f<n.length;f++){let d=e[f][o]*a;r.set(n[f].subarray(p*d,(p+1)*d),c),c+=d}}}function sV(n,e,t){let r=EM(n,e,t),s=e[0].dtype,o=St(s),i=new o(Math.max(bn(r),1)),a=hu(t.dim,0);if(In()&&iS){let p=e.map(c=>cS(c));return iS(n,p,e.map(c=>[...c.shape]),a,r,i,s),Ee(i,r,s,e[0].device)}let u=e.map(p=>Qe(p));return n==="stack"?nV(u,e[0].shape,a,i):rV(u,e.map(p=>p.shape),a,i),Ee(i,r,s,e[0].device)}function sx(n){Py=n}function ZT(){return Py}function ox(n){iS=n}function ix(n){LM=n}function aS(){return LM}function ax(n){CM=n}function uS(){return CM}function oV(n){DM=n}function Sf(){return DM}function ux(n){PM=n}function lS(){return PM}function lx(n){$M=n}function zT(){return $M}function cS(n){if(n.isContiguous&&n._impl.storageOffset===0){let e=n._impl.storage.rawData;if(e&&e.length===n.numel)return e}return In()&&Py?Py(n._impl.storage.rawData,n.shape,n.strides,n._impl.storageOffset,n.dtype):Qe(n)}function YT(n){return cS(n)}function iV(n,e){let t=X3[e];if(!t)return null;let r=e===1,s=e===5,o=r&&(n==="stack"||n==="cat");return(i,...a)=>{let{tensors:u,scalars:p}=tV(n,a);if(u.length===0)throw new Error(`JIT dispatch: no tensor args for op '${n}'`);if(o)return sV(n,u,p);let c=t(),f=AM(n,u,p,c),d=EM(n,u,p),h=f.outDtype||_g(u[0].dtype,u.length>1?u[1].dtype:u[0].dtype),m=bn(d),g=St(h),y=new g(Math.max(m,1));if(s)return IM(f.compiled,u,y),Ee(y,d,h,u[0].device);let _=u.map(b=>r?cS(b):Qe(b));return _.push(y),f.runtime.run(f.funcName,..._),Ee(y,d,h,u[0].device)}}function pS(){let n=Lt.listOps(),e=[0,1,2,5];for(let t of n){let r=Lt.findOp(t);if(!r)continue;let s=r.name;for(let o of e){if(r.entry.hasKernel(o))continue;let i=iV(s,o);i&&r.entry.registerKernel(o,Mr.fromUnboxed(i))}}}var Y3,J3,Z3,Q3,X3,IM,Py,iS,LM,CM,DM,PM,$M,wn=M(()=>{"use strict";An();sp();Ma();OM();fi();yo();sr();di();Xa();ss();rs();Cy();X3={0:()=>Y3??=go(),1:()=>J3??=xm(),2:()=>Z3??=Sm(),5:()=>Q3??=vm()},IM=null;l(eV,"setWebGPUEagerFn");l(oS,"hasTensorImpl");l(tV,"_extractTensorsAndScalars");l(hu,"numberScalar");l(tc,"numberArrayScalar");l(MM,"paddingScalar");l(EM,"_inferOutputShape");l(Qe,"tensorToContiguous");l(Ee,"wrapResult");l(Dy,"_prod");l(nV,"_hostStack");l(rV,"_hostCat");l(sV,"_runHostConcatLike");Py=null;l(sx,"setGpuContiguousFn");l(ZT,"getGpuContiguousFn");iS=null;l(ox,"setGpuConcatFn");LM=null;l(ix,"setCudnnLSTM");l(aS,"getCudnnLSTM");CM=null;l(ax,"setCudnnGRU");l(uS,"getCudnnGRU");DM=null;l(oV,"setWebgpuRNN");l(Sf,"getWebgpuRNN");PM=null;l(ux,"setGpuAdamFn");l(lS,"getGpuAdamFn");$M=null;l(lx,"setGpuMatmul");l(zT,"getGpuMatmul");l(cS,"_gpuInputArray");l(YT,"gpuContiguousArray");l(iV,"_wrapOpForJIT");l(pS,"registerJITKernels")});var CG,bt,yu=M(()=>{"use strict";sr();di();Xa();An();qn();ss();CG=Jr.fromKey(48),bt=class extends Ze{static{l(this,"SymbolicTensor")}_irValue;_tracer;_symbolicShape;constructor(e,t,r,s,o){let i=En(t),a=Wn.allocate(0,r,Bl),u=new Pt(a,0,t,i,r,Bl);super(u),this._irValue=e,this._tracer=s,this._symbolicShape=o}get irValue(){return this._irValue}get tracer(){return this._tracer}get symbolicShape(){return this._symbolicShape}get dispatchKeySet(){return super.dispatchKeySet.union(CG)}get isSymbolic(){return!0}}});function DG(n){let e=[...n.entries()];return e.sort((t,r)=>t[0]<r[0]?-1:t[0]>r[0]?1:0),e}function b_(n){return n==null?"null":typeof n=="number"?Number.isFinite(n)?String(n):n===1/0?"inf":n===-1/0?"-inf":"nan":typeof n=="boolean"?String(n):typeof n=="string"?`"${n}"`:n instanceof I?Gn(n):Array.isArray(n)?n.length===0?"[]":Array.isArray(n[0])?"["+n.map(e=>b_(e)).join(", ")+"]":"["+n.map(e=>b_(e)).join(", ")+"]":typeof n=="object"&&n.constructor===Object?"{"+Object.entries(n).sort((t,r)=>t[0].localeCompare(r[0])).map(([t,r])=>`${t}: ${b_(r)}`).join(", ")+"}":String(n)}function PG(n){return new Ef().printModule(n)}var Ef,RS=M(()=>{"use strict";Re();Ef=class{static{l(this,"IRPrinter")}constructor(e={}){this.indent=0,this.indentStr=e.indentStr||" ",this.valueNames=new Map,this._nextValueId=0}printModule(e){let t=[];t.push(`module @${e.name} {`),this.indent++;let r=!0;for(let s of e)r||t.push(""),r=!1,this.printFunction(s,t);return this.indent--,t.push("}"),t.join(`
|
|
248
|
+
`)}printFunction(e,t=null){let r=t===null;r&&(t=[]),this.valueNames.clear(),this._nextValueId=0;let s=e.args.map((i,a)=>`${this._nameValue(i)}: ${Gn(i.type)}`),o=e.outputTypes.map(i=>Gn(i)).join(", ");t.push(`${this._indentPrefix()}func @${e.name}(${s.join(", ")}) -> (${o}) {`),this.indent++;for(let i of e.body)this.printBlock(i,t,i===e.entryBlock);return this.indent--,t.push(`${this._indentPrefix()}}`),r?t.join(`
|
|
249
|
+
`):void 0}printBlock(e,t,r=!1){if(!r&&e.arguments.length>0){let s=e.arguments.map(o=>`${this._nameValue(o)}: ${Gn(o.type)}`);t.push(`${this._indentPrefix()}^bb(${s.join(", ")}):`)}for(let s of e)this.printOperation(s,t)}printOperation(e,t=null){let r=t===null;r&&(t=[]);let s=this._indentPrefix(),o=s;if(e.numResults>0){let i=[];for(let a=0;a<e.numResults;a++)i.push(this._nameValue(e.getResult(a)));o+=i.join(", ")+" = "}if(o+=e.opName,e.numOperands>0){let i=[];for(let a=0;a<e.numOperands;a++)i.push(this._valueName(e.getOperand(a)));o+="("+i.join(", ")+")"}else o+="()";if(e.attributes.size>0){let i=[];for(let[a,u]of DG(e.attributes))i.push(`${a} = ${b_(u)}`);o+=" {"+i.join(", ")+"}"}if(e.numResults>0){let i=[];for(let a=0;a<e.numResults;a++)i.push(Gn(e.getResult(a).type));o+=" : "+i.join(", ")}t.push(o);for(let i=0;i<e.regions.length;i++){let a=e.regions[i];t.push(`${s}{`),this.indent++;for(let u of a){if(u.arguments.length>0){let p=u.arguments.map(c=>`${this._nameValue(c)}: ${Gn(c.type)}`);t.push(`${this._indentPrefix()}^bb(${p.join(", ")}):`)}for(let p of u)this.printOperation(p,t)}this.indent--,t.push(`${s}}`)}return r?t.join(`
|
|
250
|
+
`):void 0}_nameValue(e){if(this.valueNames.has(e))return this.valueNames.get(e);let t=`%${this._nextValueId++}`;return this.valueNames.set(e,t),t}_valueName(e){return this.valueNames.has(e)?this.valueNames.get(e):"%?"}_indentPrefix(){let e="";for(let t=0;t<this.indent;t++)e+=this.indentStr;return e}};l(DG,"sortedEntries");l(b_,"formatAttrValue");l(PG,"printModule")});var n6,R_,cE=M(()=>{"use strict";Re();po();n6={eq:l((n,e)=>n===e,"eq"),ne:l((n,e)=>n!==e,"ne"),gt:l((n,e)=>n>e,"gt"),ge:l((n,e)=>n>=e,"ge"),lt:l((n,e)=>n<e,"lt"),le:l((n,e)=>n<=e,"le")},R_=class{static{l(this,"ShapeEnv")}_symbols;_guards;_bindings;_nextId;constructor(){this._symbols=new Map,this._guards=[],this._bindings=new Map,this._nextId=0}allocate(e,t,r){let s=`s${this._nextId++}`;return this._symbols.set(s,{hint:r,inputIdx:e,dimIdx:t}),s}produceShapeSpec(e,t,r){let s=new Array(t.length),o=new Array(t.length);for(let i=0;i<t.length;i++)if(r&&r.has(i)){let a=this.allocate(e,i,t[i]);s[i]=X,o[i]=a}else{let a=this.allocate(e,i,t[i]);this.guardRelation(a,"eq",t[i]),s[i]=t[i],o[i]=t[i]}return{irShape:s,symShape:o}}guardRelation(e,t,r){this._guards.push({lhs:e,op:t,rhs:r})}guardDivisible(e,t){this._guards.push({type:"divisible",sym:e,divisor:t})}bindInputShapes(e){this._bindings.clear();for(let[t,r]of this._symbols)this._bindings.set(t,e[r.inputIdx].shape[r.dimIdx])}evaluateGuards(){for(let e=0;e<this._guards.length;e++){let t=this._guards[e];if("type"in t){if(this._resolve(t.sym)%t.divisor!==0)return{passed:!1,failedGuard:t};continue}let r=this._resolve(t.lhs),s=this._resolve(t.rhs);if(!n6[t.op](r,s))return{passed:!1,failedGuard:t}}return{passed:!0,failedGuard:null}}resolveSymbolicShape(e){let t=new Array(e.length);for(let r=0;r<e.length;r++)t[r]=this._resolve(e[r]);return t}_resolve(e){return typeof e=="number"?e:typeof e=="string"?this._bindings.get(e):e instanceof Pe?Pe.evaluate(e,this._bindings):e}get symbols(){return this._symbols}get guards(){return this._guards}get bindings(){return this._bindings}}});function As(){return M_}var M_,I_,_u=M(()=>{"use strict";_s();Di();Us();Re();Ct();yu();cE();sS();M_=null;l(As,"getActiveTracer");I_=class{static{l(this,"Tracer")}_name;_shapeEnv;_inputTypes;_inputSymShapes;_outputTypes;_outputSymShapes;_inputs;_func;_builder;_module;_capturedParams;_capturedParamOrder;constructor(e){this._name=e||"traced",this._shapeEnv=new R_,this._inputTypes=[],this._inputSymShapes=[],this._outputTypes=[],this._outputSymShapes=[],this._inputs=[],this._func=null,this._builder=null,this._module=null,this._capturedParams=new Map,this._capturedParamOrder=[]}get shapeEnv(){return this._shapeEnv}createInput(e,t,r){let s=this._inputTypes.length,{irShape:o,symShape:i}=this._shapeEnv.produceShapeSpec(s,e,r);if(r)for(let u=0;u<i.length;u++)typeof i[u]=="string"&&this._shapeEnv.guardRelation(i[u],"gt",0);let a=new I(o,t);return this._inputTypes.push(a),this._inputSymShapes.push(i),{shape:o,dtype:t,tensorType:a}}_initGraph(){this._func=new yn(this._name,this._inputTypes,[]),this._func.inputTypes=[...this._func.inputTypes],this._builder=new $n(this._func),this._module=new Mn(this._name);let e=[],t=this._func.args;for(let r=0;r<t.length;r++){let s=t[r],o=this._inputTypes[r];s.symbolicShape=this._inputSymShapes[r];let i=new bt(s,o.shape,o.dtype,this,this._inputSymShapes[r]);e.push(i)}return this._inputs=e,e}recordOp(e,t,r){let s=[];for(let u of t)u instanceof bt&&s.push(u.irValue);let o=this._requireBuilder(),i=My(o,e,s,r),a=[];for(let u=0;u<i.numResults;u++){let p=i.getResult(u),c=p.type,f=this._propagateSymbolicShape(e,i,t,c,u);p.symbolicShape=f,a.push(new bt(p,c.shape,c.dtype,this,f))}return a.length===1?a[0]:a}_propagateSymbolicShape(e,t,r,s,o=0){let i=r.filter(c=>c instanceof bt),a=ne.get(t.opName||e);if(a&&a.propagateSymbolicShapes){let c=new Map;for(let d of i)c.set(d.irValue,d.symbolicShape);let f=a.propagateSymbolicShapes(t,c);if(f&&f[o])return f[o]}let u=s.shape,p=new Array(u.length);for(let c=0;c<u.length;c++){if(u[c]!==X){p[c]=u[c];continue}let f=null;for(let d of i){let h=d.symbolicShape;if(!h)continue;let m=u.length-h.length,g=c-m;if(g>=0&&g<h.length&&typeof h[g]=="string"){f=h[g];break}}p[c]=f!==null?f:X}return p}scan(e,t,r){let s=l(d=>d instanceof bt?d.irValue:this.captureConstant(d).irValue,"toIr"),o=e.map(s),i=t.map(s),u=this._requireBuilder().scanOp(o,i,(d,h,m)=>{let g=this._requireBuilder();this._builder=d;try{let y=l(w=>new bt(w,w.type.shape,w.type.dtype,this,[...w.type.shape]),"wrap"),[_,b]=r(m.map(y),h.map(y));return[_.map(w=>w.irValue),b.map(w=>w.irValue)]}finally{this._builder=g}}),p=t.length,c=[],f=[];for(let d=0;d<u.numResults;d++){let h=u.getResult(d),m=new bt(h,h.type.shape,h.type.dtype,this,[...h.type.shape]);d<p?c.push(m):f.push(m)}return[c,f]}captureConstant(e){let t=this._capturedParams.get(e);if(t)return t;if(e.shape.length===0&&e.data){let u=e.data[0],c=this._requireBuilder().scalarConstant(u,e.dtype).getResult(0),f=new bt(c,[],e.dtype,this,[]);return this._capturedParams.set(e,f),f}let r=new I(e.shape,e.dtype),s=this._requireFunc();s.inputTypes.push(r);let i=s.entryBlock.addArgument(r),a=new bt(i,e.shape,e.dtype,this,[...e.shape]);return this._capturedParams.set(e,a),this._capturedParamOrder.push(e),a}get capturedParams(){return this._capturedParamOrder}markOutput(e){e instanceof bt&&(this._requireBuilder().returnOp([e.irValue]),this._outputSymShapes=[e.symbolicShape]),this._outputTypes=[new I(e.shape,e.dtype)]}markOutputs(e){let t=e.map(r=>r.irValue);this._requireBuilder().returnOp(t),this._outputTypes=e.map(r=>new I(r.shape,r.dtype)),this._outputSymShapes=e.map(r=>r.symbolicShape)}get outputSymShapes(){return this._outputSymShapes}getGraphModule(){let e=this._requireFunc(),t=this._requireModule();return e.outputTypes=Object.freeze(this._outputTypes),Object.isFrozen(e.inputTypes)||(e.inputTypes=Object.freeze(e.inputTypes)),t.addFunction(e),t}activate(){M_=this}deactivate(){M_===this&&(M_=null)}_requireBuilder(){if(!this._builder)throw new Error("Tracer graph has not been initialized");return this._builder}_requireFunc(){if(!this._func)throw new Error("Tracer graph has not been initialized");return this._func}_requireModule(){if(!this._module)throw new Error("Tracer graph has not been initialized");return this._module}}});function NE(){return[...kE]}function z_(n,e,t,r=kE){return r.filter(s=>s.phase===n).sort((s,o)=>s.priority-o.priority).map(s=>s.factory(e,t)).filter(Boolean)}var kE,ev=M(()=>{"use strict";kE=[];l(NE,"snapshotGraphPasses");l(z_,"graphPassesForPhase")});function AE(n){return n instanceof Map?n:n&&typeof n=="object"?new Map(Object.entries(n)):new Map}var V_,OE=M(()=>{"use strict";ev();l(AE,"toMap");V_=class{static{l(this,"CompilerContext")}constructor({loweringRules:e=null,codegenEntries:t=null,graphPasses:r=null}={}){this.loweringRules=AE(e),this.codegenEntries=AE(t),this.graphPasses=r||NE()}get hasOverrides(){return this.loweringRules.size>0||this.codegenEntries.size>0}getLoweringRule(e){return this.loweringRules.get(e)||null}getCodegenEntry(e){return this.codegenEntries.get(e)||null}passesForPhase(e,t,r){return z_(e,t,r,this.graphPasses)}}});function G_(n){return new tv().print(n)}var tv,nv=M(()=>{"use strict";tr();tv=class{static{l(this,"TensorIRPrinter")}constructor(){this.indent=0,this.out=[]}print(e){return this.out=[],this.visit(e),this.out.join("")}push(e){this.out.push(e)}newline(){this.out.push(`
|
|
251
|
+
`+" ".repeat(this.indent))}visit(e){if(!e)return;let t="visit"+e.type;this[t]?this[t](e):this.push(`[UnknownNode: ${e.type}]`)}visitPrimFunc(e){this.push(`prim_func ${e.name}(${e.params.map(t=>t.name).join(", ")}) {`),this.indent++,this.newline();for(let[t,r]of e.bufferMap)this.push(`${r.name} = buffer_map(${t.name}, shape=[${r.shape.join(",")}], dtype=${r.dtype})`),this.newline();this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitSeqNode(e){for(let t=0;t<e.stmts.length;t++)this.visit(e.stmts[t]),t<e.stmts.length-1&&this.newline()}visitForNode(e){let t=e.kind==="serial"?"":`@${e.kind} `,r=e.threadTag?`[${e.threadTag}] `:"";this.push(`for ${e.loopVar.name} in 0..`),this.visit(e.extent),this.push(` ${t}${r}{`),this.indent++,this.newline(),this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitBlockNode(e){this.push(`block ${e.name} {`),this.indent++,this.newline();for(let t of e.iterVars)this.push(`bind ${t.iterVar.name} = `),this.visit(t.binding),this.newline();e.reads.length>0&&(this.push("reads(["),this.push(e.reads.map(t=>`${t.buffer.name}[...]`).join(", ")),this.push("])"),this.newline()),e.writes.length>0&&(this.push("writes(["),this.push(e.writes.map(t=>`${t.buffer.name}[...]`).join(", ")),this.push("])"),this.newline()),e.initBody&&(this.push("init {"),this.indent++,this.newline(),this.visit(e.initBody),this.indent--,this.newline(),this.push("}"),this.newline()),this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitBufferStoreNode(e){this.push(`${e.buffer.name}[`);for(let t=0;t<e.indices.length;t++)this.visit(e.indices[t]),t<e.indices.length-1&&this.push(", ");this.push("] = "),this.visit(e.value)}visitBufferLoadNode(e){this.push(`${e.buffer.name}[`);for(let t=0;t<e.indices.length;t++)this.visit(e.indices[t]),t<e.indices.length-1&&this.push(", ");this.push("]")}visitIfThenElseNode(e){this.push("if ("),this.visit(e.condition),this.push(") {"),this.indent++,this.newline(),this.visit(e.thenBody),this.indent--,this.newline(),this.push("}"),e.elseBody&&(this.push(" else {"),this.indent++,this.newline(),this.visit(e.elseBody),this.indent--,this.newline(),this.push("}"))}visitLetStmtNode(e){this.push(`let ${e.variable.name} = `),this.visit(e.value),this.newline(),this.visit(e.body)}visitAllocateNode(e){this.push(`allocate ${e.buffer.name}[${e.buffer.shape.join(", ")}] (${e.scope}) {`),this.indent++,this.newline(),this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitEvaluateNode(e){this.push("evaluate "),this.visit(e.value)}visitMathOpNode(e){this.push("("),this.visit(e.a),e.b&&(this.push(` ${e.op} `),this.visit(e.b)),this.push(")")}visitCompareNode(e){this.push("("),this.visit(e.a),this.push(` ${Ku(e.direction)} `),this.visit(e.b),this.push(")")}visitCallExternNode(e){this.push(`${e.externName}(`);for(let t=0;t<e.args.length;t++)this.visit(e.args[t]),t<e.args.length-1&&this.push(", ");this.push(")")}visitVariableNode(e){this.push(e.name)}visitIntImmNode(e){this.push(e.value.toString())}visitFloatImmNode(e){this.push(e.value.toString())}visitCastNode(e){this.push(`cast<${e.toDtype}>(`),this.visit(e.expr),this.push(")")}};l(G_,"printTensorIR")});var j_,BE=M(()=>{"use strict";nn();nv();j_=class{static{l(this,"TirPassManager")}constructor(){this.passes=[],this.trace=null,this.verifyHook=null}addPass(e){this.passes.push(e)}setTrace(e){this.trace=e}setVerifyHook(e){this.verifyHook=e}run(e,t){for(let r of this.passes)this._runPass(r,e,t);return e}_runPass(e,t,r){let s=r.trace;e.trace=s,s.phaseStart(e.phase);let o=performance.now();e.begin(r);for(let i=0;i<t.length;i++){let a=t[i];if(!r.failed.has(a.name))try{let u=e.run(a,r);u&&u!==a&&(t[i]=u)}catch(u){if(r.errors.push(new Yn(e.phase,a.name,u.message)),r.failed.add(a.name),s.errorEvent(e.phase,a.name,u.message),!r.resilient)break}}if(e.end(r),s.phaseEnd(e.phase,performance.now()-o),e.snapshotPoint&&s.shouldSnapshot(e.snapshotPoint))for(let i of t)r.failed.has(i.name)||s.irDump(e.snapshotPoint+":"+i.name,G_(i));this.verifyHook&&this._verifyFuncs(t,r),e.trace=null}_verifyFuncs(e,t){for(let r of e){if(t.failed.has(r.name))continue;let s=this.verifyHook(r);if(s&&s.length>0){let o=s.join("; ");if(t.resilient)t.errors.push(new Yn("verification",r.name,o)),t.failed.add(r.name);else throw new Error("TensorIR verification failed for "+r.name+": "+o)}}}}});function RE(n){let e=new Wu;return e.add(new Zu),e.add(new Qu),e.add(new Xu(n)),e.add(new el),e.add(new tl(n)),e.add(new nl),e.add(new rl),e.add(new Gh),e.add(new Ju),e.add(new Hh),e.add(new Yh),e.add(new Jh),e.add(new Zh),n&&(e.add(new Kh(n)),e.add(new qh(n)),e.add(new Wh(n))),e}var N6,A6,U_,ME=M(()=>{"use strict";fn();cp();Rx();sl();l(RE,"buildAlgebraicPatterns");N6=RE(!1),A6=RE(!0),U_=class extends qe{static{l(this,"AlgebraicSimplificationPass")}constructor(e={}){super("algebraic_simplify"),this.preservedAnalyses=new Set,this.patterns=e.fastMath?A6:N6}run(e,t){return new Wl(this.patterns).applyPatterns(e,10,this.trace)}}});function IE(n){let e=ne.get(n);return!!(e&&e.isConstant)}function O6(n,e){return!d0(e)||typeof n!="number"?!0:Number.isInteger(n)&&Number.isSafeInteger(n)}function EE(n,e,t){if(t.has(n))return t.get(n);let r=B6(n,e,t);return t.set(n,r),r}function B6(n,e,t){let r=n.definingOp;if(!r)return;if(IE(r.opName))return r.getAttr("value");if(e.has(r))return;e.add(r);let s=ne.get(r.opName);if(!s||!s.fold||s.hasSideEffects||r.regions.length>0)return;let o=new Array(r.numOperands),i=new Array(r.numOperands);for(let a=0;a<r.numOperands;a++){let u=EE(r.getOperand(a),e,t);if(u===void 0)return;o[a]=u,i[a]=r.getOperand(a).definingOp}try{return s.fold(o,r.attributes,i)}catch{return}}var q_,LE=M(()=>{"use strict";fn();_s();Ct();nn();Re();l(IE,"isConstantProducer");l(O6,"isFoldResultRepresentable");l(EE,"resolveConstantValue");l(B6,"computeConstantValue");q_=class extends qe{static{l(this,"ConstantFoldPass")}constructor(){super("constant_fold")}run(e,t){let r=!1,s=0,o=new $n(e),i=new Map;for(let a of[...e.opsRecursive()]){if(!a.parentBlock||IE(a.opName))continue;let u=ne.get(a.opName);if(!u||a.regions.length>0||u.hasSideEffects||u.getMemoryEffects&&u.getMemoryEffects(a).length>0||!u.fold||a.numOperands===0)continue;let p=new Array(a.numOperands),c=new Array(a.numOperands),f=!0;for(let d=0;d<a.numOperands;d++){let h=EE(a.getOperand(d),new Set,i);if(h===void 0){f=!1;break}p[d]=h,c[d]=a.getOperand(d).definingOp}if(f)try{let d=u.fold(p,a.attributes,c);if(d===void 0||!O6(d,a.getResult(0).type.dtype))continue;o.block=a.parentBlock,o.setInsertionPoint(a);let h=o.constant(d,a.getResult(0).type);a.replaceAllResultsWith([h.getResult(0)]),a.erase(),r=!0,s++}catch(d){this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,foldError:a.opName,message:d.message,level:le.DEBUG})}}return this.trace&&this.trace.level>=le.DEBUG&&s>0&&this.trace.emit({type:"pass_detail",passName:this.name,foldedCount:s,level:le.DEBUG}),r?oe.CHANGED:oe.UNCHANGED}}});var W_,CE=M(()=>{"use strict";fn();Ct();nn();W_=class extends qe{static{l(this,"CSEPass")}constructor(){super("cse"),this.preservedAnalyses=new Set}run(e,t){let r=!1,s=0,o=typeof e.blocksRecursive=="function"?[...e.blocksRecursive()]:[...e.body];for(let i of o){let a=new Map;for(let u of[...i.ops()]){if(!u.parentBlock||u.regions&&u.regions.length>0)continue;let p=ne.get(u.opName);if(p&&p.hasSideEffects||p&&p.getMemoryEffects&&p.getMemoryEffects(u).length>0)continue;let c=u.structuralHash();if(!a.has(c)){a.set(c,[u]);continue}let f=a.get(c),d=!1;for(let h of f)if(h.parentBlock&&h.structuralEquals(u)){let m=[];for(let g=0;g<h.numResults;g++)m.push(h.getResult(g));u.replaceAllResultsWith(m),u.erase(),r=!0,s++,d=!0;break}d||f.push(u)}}return this.trace&&this.trace.level>=le.DEBUG&&s>0&&this.trace.emit({type:"pass_detail",passName:this.name,eliminated:s,level:le.DEBUG}),r?oe.CHANGED:oe.UNCHANGED}}});function sv(n,e){let t=rv[n],r=rv[e];return t===void 0||r===void 0?!1:n===We.REDUCTION?e===We.ELEMENTWISE:e===We.REDUCTION?t<=rv[We.INJECTIVE]:!0}function Tc(n){let e=!1,t=!1,r=!1,s=!1;for(let o of n){let i=ne.get(o.opName);if(!i||i.isOpaque){r=!0;continue}i.isReduction?e=!0:i.isInjective?t=!0:i.isBroadcast?s=!0:i.isElementwise||(r=!0)}return r?We.OPAQUE:e?We.REDUCTION:t?We.INJECTIVE:s?We.BROADCAST:We.ELEMENTWISE}function Ou(n){let e=ne.get(n.opName);return!e||e.isOpaque?We.OPAQUE:e.isReduction?We.REDUCTION:e.isInjective?We.INJECTIVE:e.isBroadcast?We.BROADCAST:e.isElementwise?We.ELEMENTWISE:We.OPAQUE}function DE(n,e){if(n.length!==e.length)return!1;for(let t=0;t<n.length;t++)if(!(n[t]===X||e[t]===X)&&n[t]!==e[t])return!1;return!0}var We,rv,bc,wc,Bu=M(()=>{"use strict";Re();Ct();$a();Ir();We=Object.freeze({ELEMENTWISE:"kElementwise",BROADCAST:"kBroadcast",REDUCTION:"kReduction",INJECTIVE:"kInjective",OPAQUE:"kOpaque",HORIZONTAL:"kHorizontal"}),rv={[We.ELEMENTWISE]:0,[We.BROADCAST]:1,[We.INJECTIVE]:2,[We.REDUCTION]:3};l(sv,"canFusePatterns");l(Tc,"classifyFusionKind");l(Ou,"classifyOpPattern");bc=Object.freeze({legal:!0}),wc=class{static{l(this,"FusionLegality")}constructor(e={}){this.maxFusionSize=e.maxFusionSize||512,this.maxSharedMemory=e.maxSharedMemory||49152,this.libraryOps=e.libraryOps||new Set,this.allowReductionFusion=e.allowReductionFusion!==!1,this._lowerableCache=new Map}isOpLowerable(e){let t=this._lowerableCache.get(e);return t===void 0&&(t=Xt(e)||ni(e)||D0(e),this._lowerableCache.set(e,t)),t}canFuse(e,t){if(!e||!t)return{legal:!1,reason:"null op"};if(e===t)return{legal:!1,reason:"same op"};if(e.regions.length>0&&e.opName!=="fusion"&&e.opName!=="reduce")return{legal:!1,reason:"producer has control flow regions"};if(t.regions.length>0&&t.opName!=="fusion"&&t.opName!=="reduce")return{legal:!1,reason:"consumer has control flow regions"};let r=ne.get(e.opName),s=ne.get(t.opName);if(!r)return{legal:!1,reason:"unknown producer op"};if(!s)return{legal:!1,reason:"unknown consumer op"};if(!this.isOpLowerable(e.opName))return{legal:!1,reason:`producer op '${e.opName}' has no lowering rule`};if(!this.isOpLowerable(t.opName))return{legal:!1,reason:`consumer op '${t.opName}' has no lowering rule`};if(r.isOpaque)return{legal:!1,reason:"producer is opaque (use EpilogueFusionPass for dot/conv epilogues)"};if(s.isOpaque)return{legal:!1,reason:"consumer is opaque"};let o=Ou(e),i=Ou(t);return sv(o,i)?(o===We.REDUCTION||i===We.REDUCTION)&&!this.allowReductionFusion?{legal:!1,reason:"reduction fusion disabled by target"}:o===We.ELEMENTWISE&&i===We.ELEMENTWISE?this._checkElementwisePair(e,t):(o===We.BROADCAST||o===We.REDUCTION)&&i===We.ELEMENTWISE?bc:this._checkProducerConsumerShapes(e,t):{legal:!1,reason:`cannot fuse pattern ${o} -> ${i}`}}canMergeGroups(e,t){if(e.size+t.size>this.maxFusionSize)return{legal:!1,reason:"merged group exceeds max fusion size"};let r=0;for(let s of e.ops){let o=ne.get(s.opName);if(o&&o.isReduction&&r++,o&&o.isOpaque)return{legal:!1,reason:"opaque op in merge (use EpilogueFusionPass)"}}for(let s of t.ops){let o=ne.get(s.opName);if(o&&o.isReduction&&r++,o&&o.isOpaque)return{legal:!1,reason:"opaque op in merge (use EpilogueFusionPass)"}}return r>1?{legal:!1,reason:"merged group would contain multiple reductions"}:bc}_checkElementwisePair(e,t){let r=this._getOutputShape(e),s=this._getOutputShape(t);return r&&s&&!DE(r,s)?{legal:!1,reason:`elementwise shape mismatch: [${r}] vs [${s}]`}:bc}_checkProducerConsumerShapes(e,t){let r=this._getOutputShape(e);if(!r)return bc;for(let s=0;s<t.numOperands;s++)if(t.getOperand(s).definingOp===e){let o=t.getOperand(s).type;if(o instanceof I)return DE(r,o.shape)?bc:{legal:!1,reason:`shape mismatch on data edge: [${r}] vs [${o.shape}]`}}return bc}_getOutputShape(e){for(let t=0;t<e.numResults;t++){let r=e.getResult(t).type;if(r instanceof I)return r.shape}return null}};l(DE,"shapesCompatible")});function Ru(n){return um(n,"throw")}function Nd(n,{labelOf:e,sameLabel:t=l((i,a)=>i===a,"sameLabel"),canMerge:r=l(()=>!0,"canMerge"),onAttach:s=l(()=>{},"onAttach"),sort:o=Ru}){let i=o(n),a=new Map,u=new Map,p=[],c=0,f=l((m,g)=>{if(m===g)return!0;let y=[g],_=new Set;for(;y.length>0;){let b=y.pop();if(b===m)return!0;if(_.has(b))continue;_.add(b);let w=u.get(b);if(w)for(let x of w)y.push(x)}return!1},"isUpstreamOf"),d=l(m=>{let g=new Set;for(let y=0;y<m.numOperands;y++){let _=m.getOperand(y).definingOp;if(!_)continue;let b=a.get(_);b&&g.add(b)}return g},"operandParts"),h=l((m,g)=>{for(let y of d(m)){if(y===g)continue;let _=u.get(g);_||(_=new Set,u.set(g,_)),_.add(y)}},"recordEdges");for(let m of i){let g=e(m);if(g==null)continue;let y=!1;for(let _=0;_<m.numOperands;_++){let b=m.getOperand(_).definingOp;if(!b)continue;let w=a.get(b);if(!w||!t(w.label,g)||!r(w,m,g))continue;let x=!1;for(let S of d(m))if(S!==w&&f(w,S)){x=!0;break}if(!x){w.ops.push(m),w.opSet.add(m),s(w,m),a.set(m,w),h(m,w),y=!0;break}}if(!y){let _={id:c++,label:g,ops:[m],opSet:new Set([m])};s(_,m),p.push(_),a.set(m,_),h(m,_)}}return{partitions:p,opToPart:a,preds:u}}function pa(n,e,{pullConstants:t=!1,isConstant:r=l(()=>!1,"isConstant")}={}){let s=[],o=new Set,i=[],a=new Set,u=[],p=new Set;for(let c of e){for(let f=0;f<c.numOperands;f++){let d=c.getOperand(f),h=d.definingOp;if(!(h&&n.has(h))){if(t&&h&&r(h)){p.has(h)||(p.add(h),u.push(h));continue}o.has(d)||(o.add(d),s.push(d))}}for(let f=0;f<c.numResults;f++){let d=c.getResult(f);if(a.has(d))continue;let h=!1;for(let m of d.uses())if(!n.has(m.user)){h=!0;break}h&&(a.add(d),i.push(d))}}return{inputs:s,outputs:i,constDefs:u}}function ov(n,e){let t=new Map,r=new Map;for(let a of n)t.set(a,0),r.set(a,[]);for(let a of n){let u=e.get(a);if(u)for(let p of u)r.has(p)&&(r.get(p).push(a),t.set(a,t.get(a)+1))}let s=[];for(let a of n)t.get(a)===0&&s.push(a);let o=[],i=0;for(;i<s.length;){let a=s[i++];o.push(a);for(let u of r.get(a)){let p=t.get(u)-1;t.set(u,p),p===0&&s.push(u)}}return o.length===n.length?o:null}var Ad=M(()=>{"use strict";Da();l(Ru,"topoSortOps");l(Nd,"buildPartitions");l(pa,"computePartitionIO");l(ov,"topoSortPartitions")});function R6(n){for(let e=0;e<n.numResults;e++){let t=n.getResult(e).type;if(t instanceof I)return t.shape.join(",")}return null}var wi,K_,H_=M(()=>{"use strict";Re();Ct();Bu();$a();Ad();wi=class{static{l(this,"FusionGroup")}constructor(e){this.id=e,this.ops=[],this.opSet=new Set,this.kind=null,this._inputValues=null,this._outputValues=null,this.minTopoPos=1/0,this.maxTopoPos=-1/0}addOp(e,t){this.opSet.has(e)||(this.ops.push(e),this.opSet.add(e),this._inputValues=null,this._outputValues=null,t!==void 0&&(t<this.minTopoPos&&(this.minTopoPos=t),t>this.maxTopoPos&&(this.maxTopoPos=t)))}hasOp(e){return this.opSet.has(e)}merge(e){for(let t of e.ops)this.addOp(t);e.minTopoPos<this.minTopoPos&&(this.minTopoPos=e.minTopoPos),e.maxTopoPos>this.maxTopoPos&&(this.maxTopoPos=e.maxTopoPos)}computeIO(){if(this._inputValues&&this._outputValues)return;let{inputs:e,outputs:t}=pa(this.opSet,this.ops);this._inputValues=e,this._outputValues=t}getInputValues(){return this.computeIO(),this._inputValues}getOutputValues(){return this.computeIO(),this._outputValues}get size(){return this.ops.length}classifyKind(){return this.kind=Tc(this.ops),this.kind}allOpsInlineFusable(){for(let e of this.ops){let t=ne.get(e.opName);if(!t)return!1;if(!(t.isReduction||t.isConstant)&&!ni(e.opName))return!1}return!0}};l(R6,"outputShapeKey");K_=class{static{l(this,"FusionGroupBuilder")}constructor(e){this.legality=e,this._nextId=0,this._topoIndex=null}buildProducerConsumerGroups(e){this._topoIndex=new Map;let t=0;for(let i of e.ops())this._topoIndex.set(i,t++);let r=[],s=new Map;for(let i of e.ops()){let a=ne.get(i.opName);if(!a||a.isConstant||a.isTerminator||a.isReduction)continue;let u=this._topoIndex.get(i);for(let p=0;p<i.numOperands;p++){let c=i.getOperand(p).definingOp;if(!c)continue;let f=ne.get(c.opName);if(!f||f.isConstant||f.isReduction)continue;let d=s.get(i),h=s.get(c);if(d&&d===h||!this.legality.canFuse(c,i).legal)continue;let g=this._topoIndex.get(c);if(!d&&!h){let y=new wi(this._nextId++);y.addOp(c,g),y.addOp(i,u),s.set(c,y),s.set(i,y)}else if(d&&!h)d.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(d,c)&&(d.addOp(c,g),s.set(c,d));else if(!d&&h)h.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(h,i)&&(h.addOp(i,u),s.set(i,h));else if(this.legality.canMergeGroups(d,h).legal&&!this._mergeWouldCreateCycle(d,h)){d.merge(h);for(let _ of h.ops)s.set(_,d)}}}let o=new Set;for(let i of s.values())o.has(i)||i.size<2||(o.add(i),i.classifyKind(),r.push(i));return r}_wouldCreateCycle(e,t){for(let r=0;r<t.numOperands;r++){let s=t.getOperand(r).definingOp;if(!(!s||e.hasOp(s)||this._topoIndex.get(s)<e.minTopoPos)&&this._dependsOnGroup(s,e))return!0}return!1}_dependsOnOps(e,t,r){let s=new Set,o=[e];for(s.add(e);o.length>0;){let i=o.pop();for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a).definingOp;if(!u||s.has(u))continue;if(t.has(u))return!0;this._topoIndex.get(u)<r||(s.add(u),o.push(u))}}return!1}_dependsOnGroup(e,t){return this._dependsOnOps(e,t.opSet,t.minTopoPos)}_mergeWouldCreateCycle(e,t){let r=Math.min(e.minTopoPos,t.minTopoPos),s=Math.max(e.maxTopoPos,t.maxTopoPos),o=new Set([...e.opSet,...t.opSet]);for(let i of o)for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a).definingOp;if(!u||o.has(u))continue;let p=this._topoIndex.get(u);if(!(p<r||p>s)&&this._dependsOnOps(u,o,r))return!0}return!1}_bucketable(e,t){return t&&!t.isConstant&&!t.isTerminator&&!t.isOpaque&&e.numResults>0&&R6(e)!==null}buildHorizontalGroups(e){let t=[...e.ops()];this._topoIndex=new Map;for(let u=0;u<t.length;u++)this._topoIndex.set(t[u],u);let r=[],s=new Map,o=this.legality.maxFusionSize||t.length,i=new Map,a=0;for(let u=0;u<t.length;u++){let p=t[u];if(s.has(p))continue;let c=ne.get(p.opName);if(!this._bucketable(p,c))continue;let f=p.getResult(0).type;a++,i.set(p,a);let d=new wi(this._nextId++);d.addOp(p,u),s.set(p,d);let h=Math.min(t.length,u+1+o);for(let m=u+1;m<h;m++){let g=t[m],y=!1;for(let b=0;b<g.numOperands;b++){let w=g.getOperand(b).definingOp;if(w&&i.get(w)===a){y=!0;break}}if(y){i.set(g,a);continue}if(d.size>=o||s.has(g))continue;let _=ne.get(g.opName);this._bucketable(g,_)&&f.equals(g.getResult(0).type)&&(this._sharesInput(p,g)||c.isElementwise&&_.isElementwise)&&(d.addOp(g,m),s.set(g,d),i.set(g,a))}d.size>=2&&(d.kind=We.HORIZONTAL,r.push(d))}return r}buildAllGroups(e){let t=this.buildProducerConsumerGroups(e),r=this.buildHorizontalGroups(e),s=new Map;for(let c of t)for(let f of c.ops)s.set(f,c);let o=new Set(s.keys()),i=[];for(let c of r){let f=!1;for(let d of c.ops)if(o.has(d)){f=!0;break}f||i.push(c)}let a=[...e.ops()],u=[];for(let c of a)for(let f=0;f<c.numOperands;f++){let d=c.getOperand(f).definingOp;d&&u.push(c,d)}for(let c of i)for(let f of c.ops)s.set(f,c);if(!this._condensedHasCycle(a,u,s))return[...t,...i];for(let c of i)for(let f of c.ops)s.delete(f);let p=[...t];for(let c of i){for(let f of c.ops)s.set(f,c);if(this._condensedHasCycle(a,u,s)){for(let f of c.ops)s.delete(f);continue}p.push(c)}return p}_condensedHasCycle(e,t,r){let s=l(f=>r.get(f)||f,"repOf"),o=new Map,i=new Set;for(let f of e)i.add(s(f));for(let f=0;f<t.length;f+=2){let d=s(t[f]),h=s(t[f+1]);if(h===d)continue;i.add(h),i.add(d);let m=o.get(h);m||(m=new Set,o.set(h,m)),m.add(d)}let a=0,u=1,p=2,c=new Map;for(let f of i)c.set(f,a);for(let f of i){if(c.get(f)!==a)continue;let d=[f];for(;d.length>0;){let h=d[d.length-1],m=c.get(h);if(m===a){c.set(h,u);let g=o.get(h);if(g)for(let y of g){let _=c.get(y);if(_===u)return!0;_===a&&d.push(y)}}else m===u&&c.set(h,p),d.pop()}}return!1}_sharesInput(e,t){if(e.numOperands===0||t.numOperands===0)return!1;let r=new Set;for(let s=0;s<e.numOperands;s++)r.add(e.getOperand(s));for(let s=0;s<t.numOperands;s++)if(r.has(t.getOperand(s)))return!0;return!1}_hasDependency(e,t){for(let r=0;r<e.numResults;r++){let s=e.getResult(r);for(let o=0;o<t.numOperands;o++)if(t.getOperand(o)===s)return!0}return!1}}});var M6,fa,Y_=M(()=>{"use strict";Re();Ct();M6=Object.freeze({memory:1,launch:1e3}),fa=class{static{l(this,"FusionCostModel")}constructor(e={}){this.memoryBandwidthGBs=e.memoryBandwidthGBs||900,this.computeTFLOPs=e.computeTFLOPs||15,this.launchOverheadUs=e.launchOverheadUs||5,this.minBenefitRatio=e.minBenefitRatio||1.05,this.maxRegistersPerThread=e.maxRegistersPerThread||255,this.maxSharedMemory=e.maxSharedMemory||49152,this.maxCodeSizeOps=e.maxCodeSizeOps||256,this.libraryOps=e.libraryOps||new Set,this.registerBytesPerOp=e.registerBytesPerOp||8,this.policy=e.policy||null,this.benefitWeights={...M6,...e.benefitWeights||{}}}edgeBenefit(e){let t=this.benefitWeights;return t.launch*this.launchOverheadUs+t.memory*e}estimateOpCost(e){let t=this.estimateFLOPs(e),r=this.estimateBytes(e);return{flops:t,bytes:r,arithmeticIntensity:r>0?t/r:0}}estimateFLOPs(e){let t=ne.get(e.opName);if(t&&t.getFlops)return t.getFlops(e);let r=1;for(let s=0;s<e.numResults;s++){let o=e.getResult(s).type;if(o instanceof I){let i=o.numel();i!==X&&(r=i);break}}if(t&&t.isReduction&&e.numOperands>0){let s=e.getOperand(0).type;if(s instanceof I){let o=s.numel();if(o!==X)return o}}return r}estimateBytes(e){let t=0;for(let r=0;r<e.numOperands;r++){let s=e.getOperand(r).type;if(s instanceof I){let o=s.sizeInBytes();o!==X&&(t+=o)}}for(let r=0;r<e.numResults;r++){let s=e.getResult(r).type;if(s instanceof I){let o=s.sizeInBytes();o!==X&&(t+=o)}}return t}estimateGroupCost(e){let t=e.opSet,r=new Map,s=0,o=0,i=0,a=0,u=!1,p=!1,c=0,f=0,d=0,h=0,m=new Set;for(let x of e.ops){let S=this.estimateFLOPs(x);r.set(x,S),s+=S,o+=this.estimateBytes(x),this.libraryOps.has(x.opName)&&a++;let T=ne.get(x.opName);if(T){let v=this._outputElements(x);T.isReduction&&(u=!0,c+=v),T.isElementwise&&(p=!0,f+=v)}for(let v=0;v<x.numResults;v++){let A=x.getResult(v);m.add(A);let N=0,k=!1;for(let O of A.uses())t.has(O.user)?N++:k=!0;N>1&&(i+=(N-1)*S),(N>0||k)&&d++}for(let v=0;v<x.numOperands;v++){let A=x.getOperand(v);if(!m.has(A))continue;let N=!1;for(let k of A.uses())if(t.has(k.user)&&k.user!==x){N=!0;break}N||d--}d>h&&(h=d)}let g=e.getInputValues(),y=e.getOutputValues(),_=0;for(let x of g)if(x.type instanceof I){let S=x.type.sizeInBytes();S!==X&&(_+=S)}for(let x of y)if(x.type instanceof I){let S=x.type.sizeInBytes();S!==X&&(_+=S)}let b=0;for(let x of e.ops)for(let S=0;S<x.numResults;S++){let T=x.getResult(S),v=0;for(let A of T.uses())if(t.has(A.user)&&(v++,v>1))break;if(v>1){let A=T.type;if(A instanceof I){let N=A.sizeInBytes();N!==X&&(b+=N)}}}let w=0;return u&&p&&f>0&&c>0&&(w=Math.abs(f-c)),{unfusedFLOPs:s,unfusedBytes:o,fusedFLOPs:s+i,fusedBytes:_,recomputeCost:i,memorySaved:o-_,launchSaved:(e.size-1)*this.launchOverheadUs,registerPressure:h*this.registerBytesPerOp,sharedMemoryUsage:b,parallelismLoss:w,libraryCallLoss:a}}shouldFuse(e){if(e.size<2)return{fuse:!1,reason:"group too small"};if(this.policy&&typeof this.policy.shouldFuse=="function"){let r=this.policy.shouldFuse(e,this);if(r)return r}let t=this.estimateGroupCost(e);if(t.libraryCallLoss>0)return{fuse:!1,reason:"fusion would lose library call opportunity",cost:t};if(t.registerPressure>this.maxRegistersPerThread)return{fuse:!1,reason:`register pressure ${t.registerPressure} exceeds limit ${this.maxRegistersPerThread}`,cost:t};if(t.sharedMemoryUsage>this.maxSharedMemory)return{fuse:!1,reason:`shared memory ${t.sharedMemoryUsage} exceeds limit ${this.maxSharedMemory}`,cost:t};if(e.size>this.maxCodeSizeOps)return{fuse:!1,reason:`code size ${e.size} exceeds limit ${this.maxCodeSizeOps}`,cost:t};if(t.memorySaved<=0&&t.launchSaved<=0)return{fuse:!1,reason:"no memory or launch benefit",cost:t};if(t.parallelismLoss>0){let r=t.memorySaved+t.launchSaved*1e3;if(t.parallelismLoss>r*this.minBenefitRatio)return{fuse:!1,reason:"parallelism loss outweighs fusion benefit",cost:t}}return{fuse:!0,reason:`saves ${t.memorySaved} bytes, ${t.launchSaved}us launch`,cost:t}}_outputElements(e){for(let t=0;t<e.numResults;t++){let r=e.getResult(t).type;if(r instanceof I){let s=r.numel();if(s!==X)return s}}return 0}}});function xc(n){let e=null;for(let t of n.ops())e=t;return e&&e.opName==="yield"?e:null}function Sc(n){let e=0,t=n.regions[0]?.entryBlock;if(!t)return 0;for(let r of t.ops())r.opName!=="yield"&&e++;return e}function vc(n){let e=0,t=n.regions[0]?.entryBlock;if(!t)return 0;for(let r of t.ops()){let s=ne.get(r.opName);s&&s.isReduction&&e++}return e}function kc(n){let e=n.regions[0]?.entryBlock;if(!e)return!1;for(let t of e.ops())if(t.opName!=="yield"&&!ni(t.opName))return!1;return!0}function I6(n){let e=new Map,t=0;for(let r=n.firstOp;r;r=r._next)e.set(r,t++);return e}function iv(n){let e=I6(n);return(t,r)=>{let s=e.get(t),o=e.get(r);return s!==void 0&&o!==void 0&&s<o}}function Nc(n,e){let t=Qo(n.ops,g=>n.hasOp(g),"null");if(t===null||t.length===0)return null;n._inputValues=null,n._outputValues=null;let r=n.getInputValues(),s=n.getOutputValues(),o=r.map(g=>g.type),i=s.map(g=>g.type),a=new Ut,u=new On(o);a.addBlock(u);let p=new Map;for(let g=0;g<r.length;g++)p.set(r[g],u.arguments[g]);for(let g of t)u.pushOp(g.clone(p));let c=s.map(g=>{let y=p.get(g);if(y===void 0)throw new Error("Fusion materialization: output value not found in valueMap");return y});u.pushOp(new Me("yield",c,[]));let f=new Me("fusion",r,i,{fusion_kind:n.kind||e},[a]),d=t[0].parentBlock;if(!d)return null;let h=iv(d),m=null;for(let g of r){let y=g.definingOp;!y||n.hasOp(y)||(!m||!h(y,m))&&(m=y)}m&&m.parentBlock===d?d.insertAfter(f,m):d.insertBefore(f,t[0]);for(let g=0;g<s.length;g++)s[g].replaceAllUsesWith(f.getResult(g));for(let g of t)g.dropAllOperands(),g.parentBlock&&g.parentBlock.removeOp(g);return f}var Ac=M(()=>{"use strict";Ct();$a();yr();Ea();Da();l(xc,"getYieldOp");l(Sc,"countInnerOps");l(vc,"countReductions");l(kc,"allInnerOpsFusable");l(I6,"blockPositionIndex");l(iv,"makeComesBefore");l(Nc,"materializeFusionGroup")});var J_,PE=M(()=>{"use strict";fn();Bu();H_();Y_();Ac();nn();J_=class extends qe{static{l(this,"FusionPass")}constructor(e={}){super("FusionPass");let t=e.target||{};this.legality=new wc({maxFusionSize:t.maxFusionSize||e.maxFusionSize,maxSharedMemory:t.sharedMemoryBytes||e.maxSharedMemory,libraryOps:t.libraryOps||e.libraryOps,allowReductionFusion:e.allowReductionFusion}),this.costModel=new fa({memoryBandwidthGBs:t.memoryBandwidthGBs,computeTFLOPs:t.computeTFLOPs,maxRegistersPerThread:t.registersPerThread,maxSharedMemory:t.sharedMemoryBytes,libraryOps:t.libraryOps,policy:t.getAttr?t.getAttr("fusionPolicy"):null,...e.cost}),this.groupBuilder=new K_(this.legality)}run(e,t){let r=this.groupBuilder.buildAllGroups(e),s=[];for(let i of r)this._createsCycle(e,i)?this._traceDecision(i,!1,"fusing would create a dependency cycle"):s.push(i);let o=[];for(let i of s){if(!i.allOpsInlineFusable()){this._traceDecision(i,!1,"group contains ops without inline fusion support");continue}let a=this.costModel.shouldFuse(i);this._traceDecision(i,a.fuse,a.reason),a.fuse&&o.push(i)}if(o.length===0)return oe.UNCHANGED;for(let i of o)Nc(i,We.ELEMENTWISE);return oe.CHANGED}_traceDecision(e,t,r){if(!this.trace||this.trace.level<le.DEBUG)return;let s=e.ops.map(o=>o.opName);this.trace.emit({type:"fusion_decision",passName:this.name,groupSize:e.ops.length,ops:s,anchor:s[s.length-1]||null,fuse:t,reason:r||null,level:le.DEBUG}),this.trace.explain("fusion",s.join("+"),t?"fused":"not-fused",r||null,{groupSize:s.length})}_createsCycle(e,t){let r=t.opSet,s=t.getOutputValues(),o=new Set,i=[];for(let u of s)for(let p of u.uses())r.has(p.user)||(i.push(p.user),o.add(p.user));let a=0;for(;a<i.length;){let u=i[a++];if(r.has(u))return!0;for(let p=0;p<u.numResults;p++)for(let c of u.getResult(p).uses())o.has(c.user)||(o.add(c.user),i.push(c.user))}return!1}}});function Z_(n){return cl(n.opName)||_n(n.opName)}function $E(n){if(Z_(n))return!0;let e=ne.get(n.opName);return e!==null&&e.isElementwise}function L6(n,e){let t=n.getOperand(0).definingOp;return t&&e.has(t)&&!Z_(t)?n.getOperand(1).definingOp:t}function C6(n,e){if(!n.parentBlock||n.parentBlock!==e.parentBlock)return!1;let t=n.parentBlock.firstOp;for(;t;){if(t===n)return!0;if(t===e)return!1;t=t._next}return!1}function D6(n,e){for(let t of n)if(t!==e){for(let r=0;r<t.numResults;r++)for(let s of t.getResult(r).uses())if(!n.has(s.user))return!0}return!1}function P6(n,e){let t=ne.get(n.opName),r=t&&t.getAttr("epilogueTag");return r?r(n,e):"activation"}function $6(n){let e=[],t=new Set,r=new Set;function s(d){if(!(r.has(d)||d===n)){r.add(d);for(let h=0;h<d.numOperands;h++){let m=d.getOperand(h).definingOp;m&&m!==n&&!r.has(m)&&Z_(m)&&s(m)}e.push(d),t.add(d)}}l(s,"absorb");let o=[],i=n.getResult(0);for(let d of i.uses())$E(d.user)&&o.push(d.user);for(;o.length>0;){let d=o.pop();if(!r.has(d)){s(d);for(let h=0;h<d.numResults;h++)for(let m of d.getResult(h).uses())!r.has(m.user)&&$E(m.user)&&o.push(m.user)}}let a=[];for(let d of e)Z_(d)||a.push(P6(d,t));let u=e[e.length-1];for(let d=e.length-1;d>=0;d--){let h=e[d];for(let m=0;m<h.numResults;m++){for(let g of h.getResult(m).uses())if(!t.has(g.user)){u=h,d=-1;break}if(d<0)break}}let p=new Set;for(let d=0;d<n.numOperands;d++)p.add(n.getOperand(d));let c=[],f=new Set;for(let d of e)for(let h=0;h<d.numOperands;h++){let m=d.getOperand(h);if(f.has(m))continue;f.add(m);let g=m.definingOp;g!==n&&(g&&t.has(g)||p.has(m)||c.push(m))}return{chain:e,chainSet:t,tags:a,lastOp:u,extras:c}}function F6(n){let e=null,t=null,r=null,s=null,o=new Set;for(let i=0;i<2;i++){let a=n.getOperand(i),u=a.definingOp;if(!u||u.opName!=="convert")continue;let p=!1;for(let f of a.uses())if(f.user!==n){p=!0;break}if(p)continue;let c=u.getAttr("target_dtype")||u.getResult(0).type.dtype;i===0?(e=c,r=u.getOperand(0)):(t=c,s=u.getOperand(0)),o.add(u)}return{lhsCast:e,rhsCast:t,lhsInput:r,rhsInput:s,removed:o}}var E6,Q_,FE=M(()=>{"use strict";fn();yr();Ct();Ir();nn();l(Z_,"isPassthrough");l($E,"isEpilogueCandidate");E6=new Map([["add",(n,e)=>e.has(L6(n,e))?"residual_add":"bias"],["sub",()=>"bias"],["mul",()=>"scale"],["maximum",()=>"relu"],["clamp",()=>"clamp"],["neg",()=>"neg"],["exp",()=>"exp"],["tanh",()=>"tanh"],["sqrt",()=>"sqrt"],["abs",()=>"abs"],["log",()=>"log"]]);l(L6,"resolveOtherOperand");for(let[n,e]of E6)ne.has(n)&&ne.registerOpAttr(n,"epilogueTag",e);l(C6,"comesBefore");l(D6,"hasEscapingUse");l(P6,"classifyTag");l($6,"collectChainAndAnalyze");l(F6,"collectPrologue");Q_=class extends qe{static{l(this,"EpilogueFusionPass")}constructor(e={}){super("EpilogueFusionPass"),this.maxEpilogueOps=e.maxEpilogueOps||16,this.target=e.target||null}run(e){if(this.target&&!this.target.enableEpilogueFusion)return oe.UNCHANGED;let t=!1,r=[];for(let s of e.ops()){let o=ne.get(s.opName);o&&o.isOutEWiseFusable&&r.push(s)}for(let s of r){let o=$6(s);if(o.chain.length>this.maxEpilogueOps)continue;let i=F6(s),a=o.chain.length>0,u=i.lhsCast!==null||i.rhsCast!==null;if(!a&&!u)continue;let{chain:p,tags:c,extras:f}=o,d=0;for(let v of c)v==="bias"||v==="residual_add"||v==="scale"?d++:v==="clamp"&&(d+=2);if(d!==f.length)continue;let h=i.lhsInput||s.getOperand(0),m=i.rhsInput||s.getOperand(1),g=[h,m,...f],y=a?o.lastOp:s,_=y.getResult(0).type,b=new Map(s.attributes);b.set("epilogue_ops",p.map(v=>v.opName)),b.set("epilogue_tags",c),b.set("num_dot_operands",2),b.set("num_extra_inputs",f.length),i.lhsCast&&b.set("lhs_prologue_cast",i.lhsCast),i.rhsCast&&b.set("rhs_prologue_cast",i.rhsCast);let w=new Me("fused_dot_epilogue",g,[_],b),x=s.parentBlock;if(!x)continue;let S=new Set(p);S.add(s);for(let v of i.removed)S.add(v);if(D6(S,y))continue;let T=null;for(let v of g){let A=v.definingOp;!A||S.has(A)||(!T||!C6(A,T))&&(T=A)}T&&T.parentBlock===x?x.insertAfter(w,T):x.insertBefore(w,s),y.getResult(0).replaceAllUsesWith(w.getResult(0));for(let v=p.length-1;v>=0;v--)p[v].dropAllOperands(),p[v].parentBlock&&p[v].parentBlock.removeOp(p[v]);s.dropAllOperands(),s.parentBlock&&s.parentBlock.removeOp(s);for(let v of i.removed)v.dropAllOperands(),v.parentBlock&&v.parentBlock.removeOp(v);t=!0}return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,dotsFound:r.length,changed:t,level:le.DEBUG}),t?oe.CHANGED:oe.UNCHANGED}}});var X_,zE=M(()=>{"use strict";fn();yr();Ea();Re();Ct();nn();Bu();Ac();X_=class extends qe{static{l(this,"FusionMergerPass")}constructor(e={}){super("FusionMergerPass"),this.maxFusionSize=e.maxFusionSize||512,this.maxReductions=e.maxReductions||1,this.launchOverheadUs=e.launchOverheadUs||5,this.minMemorySavings=e.minMemorySavings||0}run(e){let t=[];for(let u of e.ops())u.opName==="fusion"&&t.push(u);if(t.length<2)return oe.UNCHANGED;let r=new Set(t),s=this._buildProducerConsumerEdges(t,r);if(s.length===0)return oe.UNCHANGED;let o=!1,i=0,a=new Set;for(let{producer:u,consumer:p,sharedResults:c}of s)a.has(u)||a.has(p)||this._canMerge(u,p)&&this._shouldMerge(u,p,c)&&(this._mergeCreatesCycle(u,p,r)||(this._merge(u,p,c),a.add(u),a.add(p),o=!0,i++));return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:t.length,edges:s.length,mergeCount:i,level:le.DEBUG}),o?oe.CHANGED:oe.UNCHANGED}_buildProducerConsumerEdges(e,t){let r=[],s=new Map;for(let o of e)for(let i=0;i<o.numResults;i++){let a=o.getResult(i);for(let u of a.uses()){if(!t.has(u.user))continue;let p=u.user;if(p===o)continue;let c=`${o.id}|${p.id}`,f=s.get(c);f||(f={producer:o,consumer:p,sharedResults:new Map},s.set(c,f),r.push(f)),f.sharedResults.set(i,u.operandIndex)}}return r}_canMerge(e,t){let r=Sc(e),s=Sc(t);if(r+s>this.maxFusionSize)return!1;let o=0;return o+=vc(e),o+=vc(t),!(o>this.maxReductions||!kc(e)||!kc(t))}_shouldMerge(e,t,r){let s=0;for(let[u]of r){let p=e.getResult(u);if(p.type instanceof I){let c=p.type.sizeInBytes();c!==X&&(s+=c)}}let o=!0;for(let u=0;u<e.numResults;u++){let p=e.getResult(u);for(let c of p.uses())if(c.user!==t){o=!1;break}if(!o)break}if(o)return!0;let i=this._estimateRecomputeCost(e);return s+this.launchOverheadUs*1e3>i}_mergeCreatesCycle(e,t,r){let s=new Set,o=[];for(let a=0;a<t.numOperands;a++){let p=t.getOperand(a).definingOp;p&&p!==e&&p.opName!=="constant"&&(s.has(p)||(s.add(p),o.push(p)))}let i=0;for(;i<o.length;){let a=o[i++];if(a===e)return!0;for(let u=0;u<a.numOperands;u++){let p=a.getOperand(u).definingOp;p&&!s.has(p)&&(s.add(p),o.push(p))}}return!1}_merge(e,t,r){let s=e.regions[0].entryBlock,o=t.regions[0].entryBlock,i=xc(s),a=xc(o);if(!i||!a)return;let u=new Map;for(let[N,k]of r)u.set(k,N);let p=[],c=new Map;for(let N=0;N<e.numOperands;N++)c.set(N,p.length),p.push(e.getOperand(N));let f=new Map;for(let N=0;N<t.numOperands;N++){if(u.has(N))continue;let k=t.getOperand(N),O=-1;for(let R=0;R<p.length;R++)if(p[R]===k){O=R;break}O>=0?f.set(N,O):(f.set(N,p.length),p.push(k))}let d=p.map(N=>N.type),h=new Ut,m=new On(d);h.addBlock(m);let g=new Map;for(let N=0;N<s.arguments.length;N++)g.set(s.arguments[N],m.arguments[c.get(N)]);for(let N of s.ops())N.opName!=="yield"&&m.pushOp(N.clone(g));for(let N=0;N<i.numOperands;N++){let k=i.getOperand(N),O=g.get(k)||k;for(let[R,B]of u)B===N&&g.set(o.arguments[R],O)}for(let N=0;N<o.arguments.length;N++){if(u.has(N))continue;let k=f.get(N);k!==void 0&&g.set(o.arguments[N],m.arguments[k])}for(let N of o.ops())N.opName!=="yield"&&m.pushOp(N.clone(g));let y=[],_=[],b=[];for(let N=0;N<e.numResults;N++){let k=!1;for(let O of e.getResult(N).uses())if(O.user!==t){k=!0;break}if(k){let O=i.getOperand(N),R=g.get(O)||O;b.push({resultIdx:N,value:R}),y.push(R),_.push(e.getResult(N).type)}}for(let N=0;N<t.numResults;N++){let k=a.getOperand(N),O=g.get(k)||k;y.push(O),_.push(t.getResult(N).type)}let w=new Me("yield",y,[]);m.pushOp(w);let x=[];for(let N of m.ops())N.opName!=="yield"&&x.push(N);let S=Tc(x),T=new Me("fusion",p,_,{fusion_kind:S},[h]),v=t;if(!v.parentBlock)return;v.parentBlock.insertBefore(T,v);let A=0;for(let{resultIdx:N}of b)e.getResult(N).replaceAllUsesWith(T.getResult(A)),A++;for(let N=0;N<t.numResults;N++)t.getResult(N).replaceAllUsesWith(T.getResult(A)),A++;e.dropAllOperands(),e.parentBlock&&e.parentBlock.removeOp(e),t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t)}_estimateRecomputeCost(e){let t=0,r=e.regions[0]?.entryBlock;if(!r)return 0;for(let s of r.ops()){if(s.opName==="yield")continue;let o=ne.get(s.opName);if(o&&o.getFlops)t+=o.getFlops(s);else for(let i=0;i<s.numResults;i++){let a=s.getResult(i).type;if(a instanceof I){let u=a.numel();u!==X&&(t+=u);break}}}return t}}});function z6(n,e){let t=Math.min(n.id,e.id),r=Math.max(n.id,e.id);return`${t}|${r}`}var Od,VE=M(()=>{"use strict";fn();yr();Ea();Re();Bu();nn();Ac();Od=class extends qe{static{l(this,"MultiOutputFusionPass")}constructor(e={}){super("MultiOutputFusionPass"),this.maxFusionSize=e.maxFusionSize||512,this.maxReductions=e.maxReductions||1,this.minSharedInputBytes=e.minSharedInputBytes||0,this.maxOutputs=e.maxOutputs||8,this.maxConsumersPerInput=e.maxConsumersPerInput||64}run(e){let t=[];for(let a of e.ops())a.opName==="fusion"&&t.push(a);if(t.length<2)return oe.UNCHANGED;let r=this._findCandidates(t);if(r.length===0)return oe.UNCHANGED;let s=!1,o=0,i=new Set;for(let{left:a,right:u,sharedInputs:p,sharedBytes:c}of r)i.has(a)||i.has(u)||this._canMerge(a,u)&&(this._mergeCreatesCycle(a,u)||(this._mergeMultiOutput(a,u,p),i.add(a),i.add(u),s=!0,o++));return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:t.length,candidates:r.length,mergeCount:o,level:le.DEBUG}),s?oe.CHANGED:oe.UNCHANGED}_findCandidates(e){let t=new Map;for(let o of e)for(let i=0;i<o.numOperands;i++){let u=o.getOperand(i).id,p=t.get(u);p||(p=[],t.set(u,p)),p.push(o)}let r=new Map;for(let[o,i]of t){if(i.length<2)continue;let a=Math.min(i.length,this.maxConsumersPerInput);for(let u=0;u<a;u++)for(let p=u+1;p<a;p++){let c=i[u],f=i[p];if(c===f)continue;let d=z6(c,f),h=r.get(d);h||(h={left:c,right:f,sharedInputs:new Set,sharedBytes:0},r.set(d,h)),h.sharedInputs.add(o)}}let s=[];for(let o of r.values()){let i=0;for(let a of o.sharedInputs){let u=t.get(a);if(!u||u.length===0)continue;let p=u[0];for(let c=0;c<p.numOperands;c++){let f=p.getOperand(c);if(f.id===a&&f.type instanceof I){let d=f.type.sizeInBytes();d!==X&&(i+=d);break}}}o.sharedBytes=i,i>=this.minSharedInputBytes&&s.push(o)}return s.sort((o,i)=>i.sharedBytes-o.sharedBytes),s}_canMerge(e,t){if(this._hasProducerConsumerEdge(e,t)||this._hasProducerConsumerEdge(t,e))return!1;let r=Sc(e),s=Sc(t);if(r+s>this.maxFusionSize||e.numResults+t.numResults>this.maxOutputs)return!1;let i=0;return i+=vc(e),i+=vc(t),!(i>this.maxReductions||!kc(e)||!kc(t))}_mergeCreatesCycle(e,t){let r=new Set,s=[];for(let i=0;i<e.numResults;i++){let a=e.getResult(i);for(let u of a.uses())u.user!==t&&!r.has(u.user)&&(r.add(u.user),s.push(u.user))}let o=0;for(;o<s.length;){let i=s[o++];if(i===t)return!0;for(let a=0;a<i.numResults;a++)for(let u of i.getResult(a).uses())r.has(u.user)||(r.add(u.user),s.push(u.user))}r.clear(),s.length=0;for(let i=0;i<t.numResults;i++){let a=t.getResult(i);for(let u of a.uses())u.user!==e&&!r.has(u.user)&&(r.add(u.user),s.push(u.user))}for(o=0;o<s.length;){let i=s[o++];if(i===e)return!0;for(let a=0;a<i.numResults;a++)for(let u of i.getResult(a).uses())r.has(u.user)||(r.add(u.user),s.push(u.user))}return!1}_mergeMultiOutput(e,t,r){let s=e.regions[0].entryBlock,o=t.regions[0].entryBlock,i=xc(s),a=xc(o);if(!i||!a)return;let u=[],p=new Map,c=new Map;for(let k=0;k<e.numOperands;k++){let O=e.getOperand(k),R=O.id,B=p.get(R);B===void 0&&(B=u.length,p.set(R,B),u.push(O)),c.set(k,B)}let f=new Map;for(let k=0;k<t.numOperands;k++){let O=t.getOperand(k),R=O.id,B=p.get(R);B===void 0&&(B=u.length,p.set(R,B),u.push(O)),f.set(k,B)}let d=u.map(k=>k.type),h=new Ut,m=new On(d);h.addBlock(m);let g=new Map;for(let k=0;k<s.arguments.length;k++)g.set(s.arguments[k],m.arguments[c.get(k)]);for(let k of s.ops())k.opName!=="yield"&&m.pushOp(k.clone(g));for(let k=0;k<o.arguments.length;k++)g.set(o.arguments[k],m.arguments[f.get(k)]);for(let k of o.ops())k.opName!=="yield"&&m.pushOp(k.clone(g));let y=[],_=[];for(let k=0;k<i.numOperands;k++){let O=i.getOperand(k);y.push(g.get(O)||O),_.push(e.getResult(k).type)}for(let k=0;k<a.numOperands;k++){let O=a.getOperand(k);y.push(g.get(O)||O),_.push(t.getResult(k).type)}let b=new Me("yield",y,[]);m.pushOp(b);let w=[];for(let k of m.ops())k.opName!=="yield"&&w.push(k);let x=Tc(w),S=new Me("fusion",u,_,{fusion_kind:x},[h]),T=e.parentBlock;if(!T)return;let v=iv(T),A=null;for(let k of u){let O=k.definingOp;!O||O===e||O===t||(!A||!v(O,A))&&(A=O)}A&&A.parentBlock===T?T.insertAfter(S,A):T.insertBefore(S,e);for(let k=0;k<e.numResults;k++)e.getResult(k).replaceAllUsesWith(S.getResult(k));let N=e.numResults;for(let k=0;k<t.numResults;k++)t.getResult(k).replaceAllUsesWith(S.getResult(N+k));e.dropAllOperands(),e.parentBlock&&e.parentBlock.removeOp(e),t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t)}_hasProducerConsumerEdge(e,t){for(let r=0;r<e.numResults;r++)for(let s of e.getResult(r).uses())if(s.user===t)return!0;return!1}};l(z6,"pairKey")});var av,ft,ro=M(()=>{"use strict";av=class{static{l(this,"UseDefResult")}constructor(e,t,r,s,o){this.topologicalOrder=e,this.valueToOp=t,this.opUsers=r,this.depth=s,this.height=o}},ft=class{static get name(){return"use_def"}static get depKey(){return"useDef"}static get dependencies(){return[]}static compute(e){let t=[],r=new Map,s=new Map,o=new Set,i=new Set,a=l(c=>{if(o.has(c))return;i.add(c);let f=[{op:c,i:0}];for(;f.length>0;){let d=f[f.length-1],h=d.op;if(d.i<h.numOperands){let m=h.getOperand(d.i).definingOp;if(d.i++,m&&!o.has(m)){if(i.has(m))throw new Error("Cycle detected in UseDefAnalysis");i.add(m),f.push({op:m,i:0})}continue}i.delete(h),o.add(h),t.push(h),f.pop()}},"visit");for(let c of e.ops()){for(let f=0;f<c.numResults;f++)r.set(c.getResult(f),c);s.set(c,new Set)}let u=new Map,p=new Map;for(let c of e.ops())a(c);for(let c of t){let f=0;for(let d=0;d<c.numOperands;d++){let h=c.getOperand(d).definingOp;if(h){s.get(h).add(c);let m=u.get(h)||0;m+1>f&&(f=m+1)}}u.set(c,f)}for(let c=t.length-1;c>=0;c--){let f=t[c],d=0,h=s.get(f)||new Set;for(let m of h){let g=p.get(m)||0;g+1>d&&(d=g+1)}p.set(f,d)}return new av(t,r,s,u,p)}}});var uv,eb,GE=M(()=>{"use strict";ro();Ir();uv=class{static{l(this,"DominanceResult")}constructor(e){this.idom=e}postDominates(e,t){let r=t;for(;r;){if(r===e)return!0;r=this.idom.get(r)}return!1}immediatePDom(e){return this.idom.get(e)||null}pathToPDom(e){let t=[],r=this.idom.get(e);for(;r&&(t.push(r),r!==this.idom.get(r));)r=this.idom.get(r);return t}},eb=class{static get name(){return"post_dominance"}static get depKey(){return"postDominance"}static get dependencies(){return[ft]}static compute(e,t={}){let s=(t.useDef||ft.compute(e)).topologicalOrder,o=new Map;for(let m of s)o.set(m,[]);for(let m of s)for(let g=0;g<m.numResults;g++){let y=m.getResult(g);for(let _ of y.uses()){let b=_.user;o.has(b)&&o.get(m).push(b)}}let i=new Set;for(let m of s)Xt(m.opName)&&i.add(m);let a={opName:"__pdom_root__"},u=Math.max(1,Math.ceil(Math.log2(s.length+2))+1),p=new Map,c=new Map,f=new Map;c.set(a,0),f.set(a,new Array(u).fill(a));let d=l((m,g)=>{p.set(m,g),c.set(m,c.get(g)+1);let y=new Array(u);y[0]=g;for(let _=1;_<u;_++)y[_]=f.get(y[_-1])[_-1];f.set(m,y)},"link"),h=l((m,g)=>{if(c.get(m)<c.get(g)){let _=m;m=g,g=_}let y=c.get(m)-c.get(g);for(let _=0;_<u;_++)y>>_&1&&(m=f.get(m)[_]);if(m===g)return m;for(let _=u-1;_>=0;_--)f.get(m)[_]!==f.get(g)[_]&&(m=f.get(m)[_],g=f.get(g)[_]);return f.get(m)[0]},"lca");for(let m=s.length-1;m>=0;m--){let g=s[m];if(i.has(g)){d(g,a);continue}let y=null;for(let _ of o.get(g))p.has(_)&&(y=y===null?_:h(y,_));y!==null&&d(g,y)}for(let[m,g]of p)g===a&&p.delete(m);return new uv(p,s)}}});function jE(n){return Xt(n)||_n(n)}var tb,UE=M(()=>{"use strict";fn();Ct();Bu();H_();Y_();GE();nn();ro();Ac();Ir();l(jE,"isSkipOp");tb=class extends qe{static{l(this,"DominatorFusionPass")}constructor(e={}){super("DominatorFusionPass"),this.requiredAnalyses=[ft];let t=e.target||{};this.maxFusionSize=t.maxFusionSize||e.maxFusionSize||512,this.maxReductions=e.maxReductions||1,this.libraryOps=t.libraryOps||e.libraryOps||new Set,this.costModel=new fa({memoryBandwidthGBs:t.memoryBandwidthGBs,computeTFLOPs:t.computeTFLOPs,maxRegistersPerThread:t.registersPerThread,maxSharedMemory:t.sharedMemoryBytes,libraryOps:t.libraryOps,...e.cost})}run(e,t){let r=t?t.getAnalysis(ft,e):ft.compute(e),s=eb.compute(e,{useDef:r}),o=r.topologicalOrder,i=this._buildGroups(o,s);if(i.length===0)return oe.UNCHANGED;let a=[];for(let u of i){if(!u.allOpsInlineFusable()){this._explain(u,!1,"group contains ops without inline fusion support");continue}let p=this.costModel.shouldFuse(u);this._explain(u,p.fuse,p.reason),p.fuse&&a.push(u)}if(a.length===0)return oe.UNCHANGED;for(let u of a)Nc(u,We.ELEMENTWISE);return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,groupsBuilt:i.length,groupsFused:a.length,level:le.DEBUG}),oe.CHANGED}_explain(e,t,r){if(!this.trace||!this.trace.explainsEnabled)return;let s=e.ops.map(o=>o.opName);this.trace.explain("fusion",s.join("+"),t?"fused":"not-fused",r||null,{groupSize:s.length,strategy:"dominator"})}_buildGroups(e,t){let r=new Map,s=[],o=0;for(let p=e.length-1;p>=0;p--){let c=e[p];if(jE(c.opName))continue;let f=Ou(c);if(f===We.OPAQUE||this.libraryOps.has(c.opName))continue;let d=t.immediatePDom(c);if(!d||jE(d.opName))continue;let h=Ou(d);if(h===We.OPAQUE||this.libraryOps.has(d.opName)||!this._canFusePatterns(f,h)||!this._pathAllFusable(c,d,e,t))continue;let m=r.get(d);if(m){if(m.size+1>this.maxFusionSize||!this._checkReductionLimit(m,c))continue;m.addOp(c),r.set(c,m),this._absorbIntermediates(c,d,m,r,e,t)}else{let g=r.get(c);if(g){if(g.size+1>this.maxFusionSize||!this._checkReductionLimit(g,d))continue;g.addOp(d),r.set(d,g),this._absorbIntermediates(c,d,g,r,e,t)}else{let y=new wi(o++);if(y.addOp(c),y.addOp(d),!this._checkGroupReductions(y))continue;r.set(c,y),r.set(d,y),s.push(y),this._absorbIntermediates(c,d,y,r,e,t)}}}let i=new Set,a=[];for(let p of s)i.has(p)||p.size<2||(i.add(p),p.classifyKind(),a.push(p));for(let p of r.values())i.has(p)||p.size<2||(i.add(p),p.classifyKind(),a.push(p));let u=[];for(let p of a){if(!this._checkGroupReductions(p)){this._explain(p,!1,`group exceeds the ${this.maxReductions}-reduction limit`);continue}if(this._createsCycle(p)){this._explain(p,!1,"fusing would create a dependency cycle");continue}u.push(p)}return u}_createsCycle(e){let t=new Set;for(let o of e.getInputValues())o.definingOp&&!e.hasOp(o.definingOp)&&t.add(o.definingOp);if(t.size===0)return!1;let r=new Set,s=[];for(let o of e.getOutputValues())for(let i of o.uses())e.hasOp(i.user)||s.push(i.user);for(;s.length>0;){let o=s.pop();if(!r.has(o)){if(r.add(o),t.has(o))return!0;for(let i=0;i<o.numResults;i++)for(let a of o.getResult(i).uses())e.hasOp(a.user)||s.push(a.user)}}return!1}_canFusePatterns(e,t){return sv(e,t)}_pathAllFusable(e,t,r,s){let o=new Set,i=[e];for(o.add(e),o.add(t);i.length>0;){let a=i.pop();for(let u=0;u<a.numResults;u++)for(let p of a.getResult(u).uses()){let c=p.user;if(c===t||o.has(c)||!s.postDominates(t,c))continue;if(o.add(c),Ou(c)===We.OPAQUE||this.libraryOps.has(c.opName))return!1;i.push(c)}}return!0}_absorbIntermediates(e,t,r,s,o,i){let a=new Set,u=[e];for(a.add(e),a.add(t);u.length>0;){let p=u.pop();for(let c=0;c<p.numResults;c++)for(let f of p.getResult(c).uses()){let d=f.user;d===t||a.has(d)||!i.postDominates(t,d)||(a.add(d),Ou(d)===We.OPAQUE||this.libraryOps.has(d.opName))||(r.size<this.maxFusionSize&&this._checkReductionLimit(r,d)&&(r.addOp(d),s.set(d,r)),u.push(d))}}}_checkGroupReductions(e){let t=0;for(let r of e.ops){let s=ne.get(r.opName);s&&s.isReduction&&t++}return t<=this.maxReductions}_checkReductionLimit(e,t){let r=ne.get(t.opName);if(!r||!r.isReduction)return!0;let s=0;for(let o of e.ops){let i=ne.get(o.opName);i&&i.isReduction&&s++}return s<this.maxReductions}}});var nb,qE=M(()=>{"use strict";nb=class{static{l(this,"GraphCycles")}constructor(e,t){this._n=e,this._parent=new Int32Array(e),this._rank=new Int32Array(e),this._nodeAtRank=new Int32Array(e),this._out=new Array(e),this._in=new Array(e);for(let r=0;r<e;r++)this._parent[r]=r,this._rank[r]=r,this._nodeAtRank[r]=r,this._out[r]=new Set,this._in[r]=new Set;if(t)for(let[r,s]of t)r!==s&&(this._out[r].add(s),this._in[s].add(r))}find(e){let t=this._parent,r=e;for(;t[r]!==r;)r=t[r];for(;t[e]!==r;){let s=t[e];t[e]=r,e=s}return r}wouldCreateCycle(e,t){let r=this.find(e),s=this.find(t);if(r===s)return!1;let o=this._rank[r]<this._rank[s]?r:s,i=o===r?s:r;return this._hasIntermediatePath(o,i)}_hasIntermediatePath(e,t){let r=this._rank[t],s=new Set([e]),o=[e];for(;o.length>0;){let i=o.pop();for(let a of this._out[i]){let u=this.find(a);if(!(u===i||u===e)){if(u===t){if(i!==e)return!0;continue}this._rank[u]>=r||s.has(u)||(s.add(u),o.push(u))}}}return!1}merge(e,t){let r=this.find(e),s=this.find(t);if(r===s)return r;let o=this._rank[r]<this._rank[s]?r:s,i=o===r?s:r,a=this._rank[o],u=this._rank[i],p=this._out[r].size+this._in[r].size,c=this._out[s].size+this._in[s].size,f=p>=c?r:s,d=f===r?s:r,h=this._out[f],m=this._in[f];for(let g of this._in[d]){let y=this.find(g);y===f||y===d||(this._out[y].delete(d),this._out[y].add(f),m.add(y))}for(let g of this._out[d]){let y=this.find(g);y===f||y===d||(this._in[y].delete(d),this._in[y].add(f),h.add(y))}return h.delete(d),m.delete(d),h.delete(f),m.delete(f),this._parent[d]=f,this._nodeAtRank[this._rank[d]]=-1,this._reorder(a,u),f}_reorder(e,t){let r=[],s=[];for(let c=e;c<=t;c++){let f=this._nodeAtRank[c];if(!(f<0)){if(this.find(f)!==f){this._nodeAtRank[c]=-1;continue}r.push(c),s.push(f)}}if(s.length<=1){for(let c=0;c<s.length;c++)this._rank[s[c]]=r[c],this._nodeAtRank[r[c]]=s[c];return}let o=new Set(s),i=new Map;for(let c of s)i.set(c,0);for(let c of s)for(let f of this._out[c]){let d=this.find(f);d!==c&&o.has(d)&&i.set(d,i.get(d)+1)}let a=[];for(let c of s)i.get(c)===0&&a.push(c);let u=[],p=0;for(;p<a.length;){let c=a[p++];u.push(c);for(let f of this._out[c]){let d=this.find(f);if(d!==c&&o.has(d)){let h=i.get(d)-1;i.set(d,h),h===0&&a.push(d)}}}for(let c=0;c<u.length;c++){let f=u[c],d=r[c];this._rank[f]=d,this._nodeAtRank[d]=f}}}});var rb,WE=M(()=>{"use strict";rb=class{static{l(this,"MaxHeap")}constructor(){this._items=[]}get size(){return this._items.length}isEmpty(){return this._items.length===0}push(e,t){let r=this._items;r.push({priority:e,value:t});let s=r.length-1;for(;s>0;){let o=s-1>>1;if(r[o].priority>=r[s].priority)break;let i=r[o];r[o]=r[s],r[s]=i,s=o}}pop(){let e=this._items,t=e.length;if(t===0)return;let r=e[0],s=e.pop();if(t>1){e[0]=s;let o=e.length,i=0;for(;;){let a=i*2+1,u=a+1,p=i;if(a<o&&e[a].priority>e[p].priority&&(p=a),u<o&&e[u].priority>e[p].priority&&(p=u),p===i)break;let c=e[p];e[p]=e[i],e[i]=c,i=p}}return r.value}}});function V6(n){let e=ne.get(n.opName);return!e||e.isConstant||e.isTerminator||e.isOpaque?!1:e.isReduction?!0:ni(n.opName)}var sb,KE=M(()=>{"use strict";fn();Ct();Re();Bu();H_();Y_();ro();qE();WE();Ac();$a();nn();l(V6,"isFusibleOp");sb=class extends qe{static{l(this,"PriorityFusionPass")}constructor(e={}){super("PriorityFusionPass"),this.requiredAnalyses=[ft];let t=e.target||{};this.maxReductions=e.maxReductions??1,this.legality=new wc({maxFusionSize:t.maxFusionSize||e.maxFusionSize,maxSharedMemory:t.sharedMemoryBytes||e.maxSharedMemory,libraryOps:t.libraryOps||e.libraryOps,allowReductionFusion:e.allowReductionFusion});let r=t.getAttr&&t.getAttr("fusionBenefitWeights")||e.benefitWeights;this.costModel=new fa({memoryBandwidthGBs:t.memoryBandwidthGBs,computeTFLOPs:t.computeTFLOPs,maxRegistersPerThread:t.registersPerThread,maxSharedMemory:t.sharedMemoryBytes,libraryOps:t.libraryOps,policy:t.getAttr?t.getAttr("fusionPolicy"):null,benefitWeights:r,...e.cost})}run(e,t){let s=(t?t.getAnalysis(ft,e):ft.compute(e)).topologicalOrder,o=s.length;if(o===0)return oe.UNCHANGED;let i=new Map;for(let T=0;T<o;T++)i.set(s[T],T);let a=[];for(let T=0;T<o;T++){let v=s[T];for(let A=0;A<v.numOperands;A++){let N=v.getOperand(A).definingOp;N&&i.has(N)&&a.push([i.get(N),T])}}let u=new nb(o,a),p=new Int32Array(o),c=new Map;for(let T=0;T<o;T++)if(V6(s[T])){let v=new wi(T);v.addOp(s[T]),c.set(T,v)}let f=new rb,d=l((T,v)=>{let A=new wi(-1);for(let N of T.ops)A.addOp(N);for(let N of v.ops)A.addOp(N);return A},"mergedGroup"),h=l(T=>{if(T.size<2||!T.allOpsInlineFusable())return!1;let v=0;for(let A of T.ops){let N=ne.get(A.opName);N&&N.isReduction&&v++}return v>this.maxReductions||(T.classifyKind(),T.kind===We.OPAQUE)?!1:this.costModel.shouldFuse(T).fuse===!0},"legalMerge"),m=l((T,v)=>{let A=0;for(let N=0;N<v.numOperands;N++){let k=v.getOperand(N);if(k.definingOp===T&&k.type instanceof I){let O=k.type.sizeInBytes();O!==X&&(A+=O)}}return A},"edgeBytes"),g=l((T,v)=>{let A=u.find(i.get(T)),N=u.find(i.get(v));if(A===N||!c.has(A)||!c.has(N)||!this.legality.canFuse(T,v).legal)return;let k=this.costModel.edgeBenefit(m(T,v));f.push(k,{a:A,b:N,va:p[A],vb:p[N],prodOp:T,consOp:v})},"pushCandidate"),y=l((T,v)=>{let A=new Set;for(let N of T.ops){for(let k=0;k<N.numResults;k++)for(let O of N.getResult(k).uses()){let R=i.get(O.user);if(R===void 0)continue;let B=u.find(R);if(B===v||!c.has(B))continue;let E=v*o+B;A.has(E)||(A.add(E),g(N,O.user))}for(let k=0;k<N.numOperands;k++){let O=N.getOperand(k).definingOp;if(!O)continue;let R=i.get(O);if(R===void 0)continue;let B=u.find(R);if(B===v||!c.has(B))continue;let E=B*o+v;A.has(E)||(A.add(E),g(O,N))}}},"reEval"),_=new Set;for(let T=0;T<o;T++){if(!c.has(T))continue;let v=s[T];for(let A=0;A<v.numOperands;A++){let N=v.getOperand(A).definingOp;if(!N)continue;let k=i.get(N);if(k===void 0||!c.has(k))continue;let O=k*o+T;_.has(O)||(_.add(O),g(N,v))}}let b=0;for(;!f.isEmpty();){let T=f.pop(),v=u.find(T.a),A=u.find(T.b);if(v===A||p[v]!==T.va||p[A]!==T.vb)continue;let N=c.get(v),k=c.get(A);if(!N||!k||!this.legality.canFuse(T.prodOp,T.consOp).legal)continue;let O=d(N,k);if(!h(O)||u.wouldCreateCycle(v,A))continue;let R=u.merge(v,A),B=N.size>=k.size?N:k,E=B===N?k:N;B.merge(E),c.delete(v),c.delete(A),c.set(R,B),p[R]=(p[v]>p[A]?p[v]:p[A])+1,b++,y(B,R)}if(b===0)return oe.UNCHANGED;let w=[],x=new Set;for(let T of c.values())x.has(T)||T.size<2||(x.add(T),w.push(T));let S=0;for(let T of w)T.classifyKind(),this._explain(T),Nc(T,We.ELEMENTWISE)&&S++;return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,ops:o,merges:b,fusions:S,level:le.DEBUG}),S>0?oe.CHANGED:oe.UNCHANGED}_explain(e){if(!this.trace||!this.trace.explainsEnabled)return;let t=e.ops.map(r=>r.opName);this.trace.explain("fusion",t.join("+"),"fused",null,{groupSize:t.length,strategy:"priority"})}}});function G6(n,e){return n===e?!0:!n||!e?!1:typeof n.equals=="function"?n.equals(e):!1}var da,ob,HE=M(()=>{"use strict";Re();da=class{static{l(this,"LayoutPreference")}constructor(e,t,r=0){this.inputs=e,this.outputs=t,this.cost=r}},ob=class{static{l(this,"LayoutPolicy")}constructor(e){this.target=e,this._rules=new Map,this._initDefaultRules()}registerRule(e,t){this._rules.set(e,t)}getPreference(e){let t=this._rules.get(e.opName);return t?t(e,this.target):null}estimateConversionCost(e,t,r){if(!(r instanceof I)||G6(e,t))return 0;let s=r.numel();return s<0?1024:s*2}estimateBenefit(e,t,r){if(!(t instanceof I))return 0;let s=t.numel();if(s<0)return 0;let o=e.opName;if(o==="dot"||o==="conv"||o==="matmul")return s*4*r;if(o==="reduce")return s*2*r;let i=this.target.cacheLineBytes||64;return s*4<=i*4?0:Math.floor(s*.5)}_initDefaultRules(){this._rules.set("conv",(e,t)=>{let s=e.getOperand(0).type?.rank||4;if(t.preferredConvLayout)return new da([t.preferredConvLayout,null],[t.preferredConvLayout]);if(t.isGPU()&&s===4){let o=new Gt([0,2,3,1]);return new da([o,null],[o])}if(t.isCPU()&&s===4){let o=new Gt([0,2,3,1]);return new da([o,null],[o])}return null}),this._rules.set("dot",(e,t)=>{let r=e.getOperand(0).type,s=e.getOperand(1).type;if(!r||!s)return null;let o=Gt.rowMajor(r.rank);if(t.isCPU()&&s.rank===2){let a=Gt.columnMajor(s.rank);return new da([o,a],[o])}let i=Gt.rowMajor(s.rank);return new da([o,i],[o])}),this._rules.set("reduce",(e,t)=>{let r=e.getResult(0).type;return r?new da([null],[Gt.rowMajor(r.rank)]):null})}};l(G6,"layoutEquals")});function YE(n,e){let t=new Map,r=null,s=0;for(let i=0;i<n.numOperands;i++){let a=n.getOperand(i),u=e.get(a);if(!u)continue;let p=u.hash?u.hash():0,c=(t.get(p)||0)+1;t.set(p,c),c>s&&(s=c,r=u)}if(r)return r;let o=n.getResult(0);return o&&o.type instanceof I?Gt.rowMajor(o.type.rank):Gt.rowMajor(1)}function j6(n,e){return n===e?!0:!n||!e?!1:typeof n.equals=="function"?n.equals(e):!1}function JE(n){if(n instanceof Gt)return n;if(n&&typeof n.toLayout=="function")try{return n.toLayout()}catch{}return n&&n.baseOrder&&!n.isBlocked?.()?new Gt(n.baseOrder):n&&n.order?new Gt(n.order):null}var lv,ib,ZE=M(()=>{"use strict";ro();Re();Ct();Bn();Ir();lv=class{static{l(this,"LayoutAnalysisResult")}constructor(e,t,r){this.assignments=e,this.conversions=t,this.totalCost=r}},ib=class{static get name(){return"layout"}static get depKey(){return"layout"}static get dependencies(){return[ft]}static compute(e,t,r){let s=t.useDef,o=new Map,i=s.topologicalOrder;for(let p of e.args)p.type instanceof I&&o.set(p,p.type.layout||Gt.rowMajor(p.type.rank));for(let p=0;p<i.length;p++){let c=i[p];if(Xt(c.opName))continue;let f=r?r.getPreference(c):null;if(f&&f.outputs.length>0){for(let m=0;m<c.numResults;m++){let g=c.getResult(m);if(!(g.type instanceof I))continue;let y=f.outputs[m]||null;y?o.set(g,y):o.set(g,YE(c,o))}continue}let d=ne.get(c.opName),h=d&&d.hasTrait(Y.ELEMENTWISE);for(let m=0;m<c.numResults;m++){let g=c.getResult(m);g.type instanceof I&&(h?o.set(g,YE(c,o)):o.set(g,Gt.rowMajor(g.type.rank)))}}let a=[],u=0;for(let p=0;p<i.length;p++){let c=i[p],f=r?r.getPreference(c):null;if(f)for(let d=0;d<c.numOperands;d++){let h=c.getOperand(d);if(!(h.type instanceof I))continue;let m=o.get(h);if(!m)continue;let g=f.inputs[d];if(!g||j6(m,g))continue;let y=JE(m),_=JE(g);!y||!_||y.equals(_)||(a.push({value:h,consumer:c,operandIdx:d,from:y,to:_}),r&&(u+=r.estimateConversionCost(y,_,h.type)))}}return new lv(o,a,u)}};l(YE,"resolveFromInputs");l(j6,"layoutEquals");l(JE,"toPlainLayout")});function U6(n,e,t){let r=n.id,s=e.hash?e.hash():0,o=t.hash?t.hash():0;return`${r}:${s}:${o}`}var ab,QE=M(()=>{"use strict";fn();yr();Re();HE();ZE();ro();nn();ab=class extends qe{static{l(this,"LayoutTransformPass")}constructor(e={}){super("LayoutTransformPass"),this.target=e.target||null,this._policy=null}run(e,t){if(!this.target)return oe.UNCHANGED;this._policy||(this._policy=new ob(this.target));let r=t?t.getAnalysis(ft,e):ft.compute(e),s=ib.compute(e,{useDef:r},this._policy);if(s.conversions.length===0)return oe.UNCHANGED;let o=new Map;for(let p of s.conversions){let{value:c,consumer:f,operandIdx:d,from:h,to:m}=p,g=U6(c,h,m),y=o.get(g);y||(y={value:c,from:h,to:m,consumers:[],cost:this._policy.estimateConversionCost(h,m,c.type),benefit:0},o.set(g,y)),y.consumers.push({consumer:f,operandIdx:d});let _=this.target.layoutAwareOps&&this.target.layoutAwareOps.has(f.opName);y.benefit+=_?this._policy.estimateBenefit(f,c.type,1):0}let i=0,a=0,u=[];for(let p of o.values())p.benefit<p.cost||(u.push(p),i+=p.cost,a+=p.benefit);if(u.length===0||i>a)return oe.UNCHANGED;for(let p of u){let c=p.from instanceof Gt?p.from.order:Array.from({length:p.value.type.rank},(y,_)=>_),f=p.to instanceof Gt?p.to.order:Array.from({length:p.value.type.rank},(y,_)=>_),d=new I(p.value.type.shape,p.value.type.dtype,p.to),h=new Me("layout_transform",[p.value],[d],{src_layout:[...c],dst_layout:[...f]}),m=p.value.definingOp;m&&m.parentBlock?m.parentBlock.insertAfter(h,m):p.consumers[0].consumer.parentBlock&&p.consumers[0].consumer.parentBlock.insertBefore(h,p.consumers[0].consumer);let g=h.getResult(0);for(let y of p.consumers)y.consumer.replaceOperand(y.operandIdx,g)}return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,conversions:s.conversions.length,uniqueTransforms:u.length,level:le.DEBUG}),oe.CHANGED}};l(U6,"valueLayoutKey")});function XE(n,e){if(n===e)return!0;if(typeof n=="number"&&typeof e=="number")return n===e;if(typeof n!=typeof e||n.length!==e.length)return!1;for(let t=0;t<n.length;t++)if(n[t]!==e[t])return!1;return!0}function eL(n){let e=0;for(let t=0;t<n.length;t++)e=(e<<5)-e+n.charCodeAt(t)&2147483647;return e}function K6(n){if(typeof n=="number"){let t=new Float64Array([n]),r=new Uint32Array(t.buffer);return(r[0]^r[1])&2147483647}let e=n.length;for(let t=0;t<Math.min(n.length,8);t++){let r=new Float64Array([n[t]]),s=new Uint32Array(r.buffer);e=(e^(s[0]^s[1]))*16777619&2147483647}return e}var Pr,q6,W6,vr,cv=M(()=>{"use strict";Re();Pr=Object.freeze({PER_TENSOR_SYMMETRIC:"per_tensor_symmetric",PER_TENSOR_ASYMMETRIC:"per_tensor_asymmetric",PER_CHANNEL:"per_channel",PER_GROUP:"per_group"}),q6=new Set(Object.values(Pr)),W6=new Set(["i8","ui8"]),vr=class n{static{l(this,"QuantizationParams")}constructor(e){this.scheme=e.scheme,this.scale=e.scale,this.zeroPoint=e.zeroPoint,this.axis=e.axis??null,this.groupSize=e.groupSize??null,this.dtype=e.dtype||"i8",this.numBits=e.numBits||fo(this.dtype)*8,this._hash=null}clampRange(){if(this.isSymmetric()){let r=2**(this.numBits-1)-1;return[-r,r]}if(this.dtype==="ui8")return[0,2**this.numBits-1];let e=-(2**(this.numBits-1)),t=2**(this.numBits-1)-1;return[e,t]}quantize(e){let[t,r]=this.clampRange(),s=Math.round(e/this.getScalarScale()+this.getScalarZeroPoint());return Math.max(t,Math.min(r,s))}dequantize(e){return(e-this.getScalarZeroPoint())*this.getScalarScale()}quantizeArray(e){let[t,r]=this.clampRange(),s=new Array(e.length);if(this.isPerChannel())throw new Error("Use quantizeArrayPerChannel(floatArr, shape) for per-channel quantization");let o=this.getScalarScale(),i=this.getScalarZeroPoint();for(let a=0;a<e.length;a++)s[a]=Math.max(t,Math.min(r,Math.round(e[a]/o+i)));return s}dequantizeArray(e){let t=new Array(e.length);if(this.isPerChannel())throw new Error("Use dequantizeArrayPerChannel(intArr, shape) for per-channel dequantization");let r=this.getScalarScale(),s=this.getScalarZeroPoint();for(let o=0;o<e.length;o++)t[o]=(e[o]-s)*r;return t}_channelStride(e){let t=1;for(let r=this.axis+1;r<e.length;r++)t*=e[r];return t}quantizeArrayPerChannel(e,t){let[r,s]=this.clampRange(),o=this._channelStride(t),i=t[this.axis],a=new Array(e.length);for(let u=0;u<e.length;u++){let p=Math.floor(u/o)%i,c=this.getScaleForChannel(p),f=this.getZeroPointForChannel(p);a[u]=Math.max(r,Math.min(s,Math.round(e[u]/c+f)))}return a}dequantizeArrayPerChannel(e,t){let r=this._channelStride(t),s=t[this.axis],o=new Array(e.length);for(let i=0;i<e.length;i++){let a=Math.floor(i/r)%s;o[i]=(e[i]-this.getZeroPointForChannel(a))*this.getScaleForChannel(a)}return o}quantizeArrayPerGroup(e){let[t,r]=this.clampRange(),s=new Array(e.length);for(let o=0;o<e.length;o++){let i=Math.floor(o/this.groupSize),a=this.scale[i],u=this.zeroPoint[i];s[o]=Math.max(t,Math.min(r,Math.round(e[o]/a+u)))}return s}dequantizeArrayPerGroup(e){let t=new Array(e.length);for(let r=0;r<e.length;r++){let s=Math.floor(r/this.groupSize);t[r]=(e[r]-this.zeroPoint[s])*this.scale[s]}return t}getScaleForGroup(e){return this.scale[e]}getZeroPointForGroup(e){return this.zeroPoint[e]}getScaleForChannel(e){return this.isPerChannel()?this.scale[e]:this.getScalarScale()}getZeroPointForChannel(e){return this.isPerChannel()?this.zeroPoint[e]:this.getScalarZeroPoint()}getScalarScale(){return typeof this.scale=="number"?this.scale:this.scale[0]}getScalarZeroPoint(){return typeof this.zeroPoint=="number"?this.zeroPoint:this.zeroPoint[0]}numChannels(){return this.isPerChannel()?typeof this.scale=="number"?1:this.scale.length:1}isPerChannel(){return this.scheme===Pr.PER_CHANNEL}isPerGroup(){return this.scheme===Pr.PER_GROUP}isSymmetric(){return this.scheme===Pr.PER_TENSOR_SYMMETRIC}equals(e){return this===e?!0:!(!(e instanceof n)||this.scheme!==e.scheme||this.dtype!==e.dtype||this.numBits!==e.numBits||this.axis!==e.axis||this.groupSize!==e.groupSize||!XE(this.scale,e.scale)||!XE(this.zeroPoint,e.zeroPoint))}hash(){if(this._hash!==null)return this._hash;let e=2166136261;return e=(e^eL(this.scheme))*16777619&2147483647,e=(e^eL(this.dtype))*16777619&2147483647,e=(e^this.numBits)*16777619&2147483647,e=(e^K6(this.scale))*16777619&2147483647,this._hash=e,e}serialize(){let e=this.isPerChannel()||this.isPerGroup();return{scheme:this.scheme,scale:e?[...this.scale]:this.scale,zeroPoint:e?[...this.zeroPoint]:this.zeroPoint,axis:this.axis,groupSize:this.groupSize,dtype:this.dtype,numBits:this.numBits}}static deserialize(e){return new n(e)}static fromRange(e,t,r,s="i8",o=8){if(r===Pr.PER_TENSOR_SYMMETRIC){let d=Math.max(Math.abs(e),Math.abs(t)),h=2**(o-1)-1,m=d/h||1e-10;return new n({scheme:r,scale:m,zeroPoint:0,dtype:s,numBits:o})}let[i,a]=s==="ui8"?[0,2**o-1]:[-(2**(o-1)),2**(o-1)-1],p=(t-e||1e-10)/(a-i),c=Math.round(i-e/p),f=Math.max(i,Math.min(a,c));return new n({scheme:r,scale:p,zeroPoint:f,dtype:s,numBits:o})}static fromRangePerChannel(e,t,r,s="i8",o=8){let i=e.length,a=new Float64Array(i),u=new Int32Array(i),p=2**(o-1)-1;for(let c=0;c<i;c++){let f=Math.max(Math.abs(e[c]),Math.abs(t[c]));a[c]=f/p||1e-10,u[c]=0}return new n({scheme:Pr.PER_CHANNEL,scale:a,zeroPoint:u,axis:r,dtype:s,numBits:o})}static defaultForActivation(e,t="i8",r=8){return n.fromRange(-6,6,e,t,r)}static fromConstantArray(e,t,r="i8",s=8){let o=1/0,i=-1/0;for(let a=0;a<e.length;a++)e[a]<o&&(o=e[a]),e[a]>i&&(i=e[a]);return isFinite(o)||(o=-1),isFinite(i)||(i=1),o===i&&(o-=.5,i+=.5),n.fromRange(o,i,t,r,s)}static fromConstantArrayPerChannel(e,t,r,s="i8",o=8){let i=t[r],a=1;for(let c=r+1;c<t.length;c++)a*=t[c];let u=new Array(i).fill(1/0),p=new Array(i).fill(-1/0);for(let c=0;c<e.length;c++){let f=Math.floor(c/a)%i,d=e[c];d<u[f]&&(u[f]=d),d>p[f]&&(p[f]=d)}for(let c=0;c<i;c++)isFinite(u[c])||(u[c]=-1),isFinite(p[c])||(p[c]=1),u[c]===p[c]&&(u[c]-=.5,p[c]+=.5);return n.fromRangePerChannel(u,p,r,s,o)}static fromConstantArrayPerGroup(e,t,r="i8",s=4){let o=Math.ceil(e.length/t),i=new Float64Array(o),a=new Int32Array(o),u=2**(s-1)-1;for(let p=0;p<o;p++){let c=p*t,f=Math.min(c+t,e.length),d=0;for(let h=c;h<f;h++){let m=Math.abs(e[h]);m>d&&(d=m)}i[p]=d/u||1e-10,a[p]=0}return new n({scheme:Pr.PER_GROUP,scale:i,zeroPoint:a,groupSize:t,dtype:r,numBits:s})}static isQuantizableDtype(e){return W6.has(e)}static isValidScheme(e){return q6.has(e)}};l(XE,"scaleEquals");l(eL,"hashStr");l(K6,"hashScaleValue")});function Z6(n){for(let e=0;e<n.numOperands;e++){let t=n.getOperand(e).definingOp;if(t&&t.opName==="constant")return!0}return!1}function Q6(n,e,t){for(let r=0;r<n.numOperands;r++){let s=n.getOperand(r);if(!e.has(s)&&(!(s.type instanceof I)||!jt(s.type.dtype)))return!1}return n.numOperands>0}var H6,Y6,J6,ub,lb,tL=M(()=>{"use strict";fn();yr();Re();Ct();ro();cv();nn();Ir();H6=new Set(["softmax","sqrt","div","rsqrt","log","exp","tanh"]),Y6=new Set(["dot","conv","add","mul","sub"]),J6=new Map([["dot","quantized_dot"],["conv","quantized_conv"]]);for(let[n,e]of J6)ne.has(n)&&ne.registerOpAttr(n,"quantizedVariant",e);ub=class{static{l(this,"QuantizationConfig")}constructor(e={}){this.scheme=e.scheme||Pr.PER_TENSOR_SYMMETRIC,this.calibration=e.calibration||null,this.targetDtype=e.targetDtype||"i8",this.excludeOps=e.excludeOps||H6,this.quantizableOps=e.quantizableOps||Y6,this.sensitivityThreshold=e.sensitivityThreshold||0,this.sensitivityResult=e.sensitivityResult||null,this.weightOnly=e.weightOnly||!1,this.target=e.target||null}},lb=class extends qe{static{l(this,"QuantizationPass")}constructor(e={}){super("QuantizationPass"),this.requiredAnalyses=[ft],this.config=e instanceof ub?e:new ub(e)}run(e,t){let s=(t?t.getAnalysis(ft,e):ft.compute(e)).topologicalOrder,o=new Set;this._paramsByValue=new Map;let i=this.config,a=!1;if(i.target&&!i.target.supportsInt8)return oe.UNCHANGED;for(let p=0;p<s.length;p++){let c=s[p];if(Xt(c.opName))continue;if(i.excludeOps.has(c.opName)||!i.quantizableOps.has(c.opName)){for(let h=0;h<c.numOperands;h++)o.has(c.getOperand(h))&&(a=this._insertDequantBefore(c,h,c.getOperand(h),i)||a);continue}if(i.sensitivityResult&&i.sensitivityThreshold>0&&i.sensitivityResult.isSensitive(c,i.sensitivityThreshold)||i.weightOnly&&!Z6(c))continue;let f=ne.get(c.opName),d=f?f.getAttr("quantizedVariant"):null;if(d&&Q6(c,o,i)){i.scheme===Pr.PER_CHANNEL&&this._canPerChannelDot(c,o)?a=this._replacePerChannelDot(c,i)||a:a=this._replaceWithNativeQuantized(c,d,o,i)||a;continue}a=this._insertDequantQuantBoundary(c,o,i)||a}let u=e.getReturnOp();if(u)for(let p=0;p<u.numOperands;p++){let c=u.getOperand(p);o.has(c)&&(a=this._insertDequantBefore(u,p,c,i)||a)}return this.trace&&this.trace.level>=le.DEBUG&&a&&this.trace.emit({type:"pass_detail",passName:this.name,opsProcessed:s.length,changed:a,level:le.DEBUG}),a?oe.CHANGED:oe.UNCHANGED}_getQuantParams(e,t){let r=fo(t.targetDtype)*8;if(t.calibration&&t.calibration.hasData(e))return t.calibration.getQuantParams(e,t.scheme,t.targetDtype);let s=e.definingOp;if(s&&s.opName==="constant"){let o=s.getAttr("value");if(typeof o=="number")return vr.fromRange(-Math.abs(o)||-1,Math.abs(o)||1,t.scheme,t.targetDtype,r);if(o&&typeof o.length=="number")return vr.fromConstantArray(o,t.scheme,t.targetDtype,r)}return e.type instanceof I&&jt(e.type.dtype)?vr.defaultForActivation(t.scheme,t.targetDtype,r):null}_resolveQuantParams(e,t){let r=this._paramsByValue.get(e);if(r)return r;let s=e.definingOp;return s&&s.opName==="quantize"?new vr({scheme:s.getAttr("scheme")||t.scheme,scale:s.getAttr("scale"),zeroPoint:s.getAttr("zero_point")||0,dtype:s.getAttr("target_dtype")||t.targetDtype}):this._getQuantParams(e,t)}_insertQuantizeAfter(e,t,r){let s=e.getResult(t),o=this._getQuantParams(s,r);if(!o)return null;let i=new I(s.type.shape,r.targetDtype),a=new Me("quantize",[s],[i],{scale:o.getScalarScale(),zero_point:o.getScalarZeroPoint(),scheme:r.scheme,target_dtype:r.targetDtype,...o.axis!==null?{axis:o.axis}:{}});e.parentBlock&&e.parentBlock.insertAfter(a,e);let u=a.getResult(0);return this._paramsByValue.set(u,o),u}_insertDequantBefore(e,t,r,s){let o=this._resolveQuantParams(r,s);if(!o)return!1;let i="f32",a=new I(r.type.shape,i),u=new Me("dequantize",[r],[a],{scale:o.getScalarScale(),zero_point:o.getScalarZeroPoint(),scheme:s.scheme,target_dtype:i});return e.parentBlock&&(e.parentBlock.insertBefore(u,e),e.replaceOperand(t,u.getResult(0))),!0}_insertDequantQuantBoundary(e,t,r){let s=!1;for(let o=0;o<e.numOperands;o++){let i=e.getOperand(o);t.has(i)&&(s=this._insertDequantBefore(e,o,i,r)||s)}for(let o=0;o<e.numResults;o++){let i=e.getResult(o);if(!(i.type instanceof I)||!jt(i.type.dtype))continue;let a=this._insertQuantizeAfter(e,o,r);if(a){let u=[...i.uses()];for(let p of u)p.user!==a.definingOp&&p.user.replaceOperand(p.operandIndex,a);t.add(a),s=!0}}return s}_replaceWithNativeQuantized(e,t,r,s){let o=[],i={};for(let[f,d]of e.attributes||[])i[f]=d;for(let f=0;f<e.numOperands;f++){let d=e.getOperand(f);if(r.has(d)){o.push(d);let _=this._resolveQuantParams(d,s);if(_){let b=f===0?"lhs":"rhs";if(t==="quantized_dot")i[`${b}_scale`]=_.getScalarScale(),i[`${b}_zero_point`]=_.getScalarZeroPoint();else{let w=f===0?"input":"kernel";i[`${w}_scale`]=_.getScalarScale(),i[`${w}_zero_point`]=_.getScalarZeroPoint()}}continue}let h=this._getQuantParams(d,s);if(!h)return!1;let m=new I(d.type.shape,s.targetDtype),g=new Me("quantize",[d],[m],{scale:h.getScalarScale(),zero_point:h.getScalarZeroPoint(),scheme:s.scheme,target_dtype:s.targetDtype});e.parentBlock&&e.parentBlock.insertBefore(g,e),o.push(g.getResult(0));let y=f===0?t==="quantized_dot"?"lhs":"input":t==="quantized_dot"?"rhs":"kernel";i[`${y}_scale`]=h.getScalarScale(),i[`${y}_zero_point`]=h.getScalarZeroPoint()}let a=i.lhs_scale||i.input_scale||1,u=i.rhs_scale||i.kernel_scale||1;i.output_scale=a*u,i.output_zero_point=0;let p=new I(e.getResult(0).type.shape,"i32"),c=new Me(t,o,[p],i);if(e.parentBlock){e.parentBlock.insertBefore(c,e);let f=new I(e.getResult(0).type.shape,e.getResult(0).type.dtype),d=i.output_scale||1,h=new Me("dequantize",[c.getResult(0)],[f],{scale:d,zero_point:i.output_zero_point||0,scheme:s.scheme,target_dtype:e.getResult(0).type.dtype});e.parentBlock.insertBefore(h,e),e.replaceAllResultsWith([h.getResult(0)]),e.erase()}return!0}_canPerChannelDot(e,t){if(e.opName!=="dot")return!1;let r=e.getOperand(0),s=e.getOperand(1);if(t.has(r)||t.has(s)||!(r.type instanceof I)||r.type.shape.length!==2||!(s.type instanceof I)||s.type.shape.length!==2)return!1;let o=s.definingOp;if(!o||o.opName!=="constant")return!1;let i=o.getAttr("value");if(!i||typeof i=="number"||typeof i.length!="number")return!1;let a=e.getAttr("rhs_contracting")||[],u=e.getAttr("lhs_contracting")||[];return!(a.length!==1||u.length!==1||(e.getAttr("rhs_batch")||[]).length!==0||(e.getAttr("lhs_batch")||[]).length!==0)}_activationParams(e,t){let r=fo(t.targetDtype)*8,s=Pr.PER_TENSOR_SYMMETRIC;return t.calibration&&t.calibration.hasData(e)?t.calibration.getQuantParams(e,s,t.targetDtype):vr.defaultForActivation(s,t.targetDtype,r)}_replacePerChannelDot(e,t){let r=e.getOperand(0),s=e.getOperand(1),o=s.type.shape,i=s.definingOp.getAttr("value"),a=fo(t.targetDtype)*8,p=e.getAttr("rhs_contracting")[0]===0?1:0,c=vr.fromConstantArrayPerChannel([...i],o,p,t.targetDtype,a),f=c.quantizeArrayPerChannel([...i],o),d=this._activationParams(r,t),h=d.getScalarScale(),m=d.getScalarZeroPoint(),g=e.parentBlock;if(!g)return!1;let y=new I(o,t.targetDtype),_=new Me("constant",[],[y],{value:f,tensor_type:y});g.insertBefore(_,e);let b=new I(r.type.shape,t.targetDtype),w=new Me("quantize",[r],[b],{scale:h,zero_point:m,scheme:Pr.PER_TENSOR_SYMMETRIC,target_dtype:t.targetDtype});g.insertBefore(w,e);let x=e.getResult(0).type.shape,S={};for(let[U,D]of e.attributes||[])S[U]=D;S.lhs_scale=h,S.lhs_zero_point=m,S.rhs_scale=1,S.rhs_zero_point=0,S.output_scale=1,S.output_zero_point=0;let T=new I(x,"i32"),v=new Me("quantized_dot",[w.getResult(0),_.getResult(0)],[T],S);g.insertBefore(v,e);let A=new I(x,"f32"),N=new Me("convert",[v.getResult(0)],[A],{target_dtype:"f32"});g.insertBefore(N,e);let k=o[p],O=new Array(k);for(let U=0;U<k;U++)O[U]=h*c.getScaleForChannel(U);let R=new Set(e.getAttr("lhs_contracting")||[]),B=0;for(let U=0;U<r.type.shape.length;U++)R.has(U)||B++;let E=B,F=new I([k],"f32"),C=new Me("constant",[],[F],{value:O,tensor_type:F});g.insertBefore(C,e);let H=new Me("broadcast_in_dim",[C.getResult(0)],[A],{broadcast_dimensions:[E],result_shape:x});g.insertBefore(H,e);let G=new Me("mul",[N.getResult(0),H.getResult(0)],[A],{});return g.insertBefore(G,e),e.replaceAllResultsWith([G.getResult(0)]),e.erase(),!0}};l(Z6,"hasConstantOperand");l(Q6,"allOperandsCanQuantize")});var pv,cb,nL=M(()=>{"use strict";ro();Re();pv=class{static{l(this,"LivenessResult")}constructor(e,t,r,s,o,i,a){this.liveIn=e,this.liveOut=t,this.intervals=r,this.opIndex=s,this.peakPressure=o,this.peakOp=i,this.pressureAtOp=a}interfere(e,t){if(e===t)return!0;let r=this.intervals.get(e),s=this.intervals.get(t);return r&&s?r.start<=s.end&&s.start<=r.end:!1}liveAtOp(e){return this.liveIn.get(e)||new Set}intervalOf(e){return this.intervals.get(e)||null}},cb=class n{static get name(){return"liveness"}static get depKey(){return"liveness"}static get dependencies(){return[ft]}static buildIntervals(e,t){let r=new Map;for(let o=0;o<t.length;o++)r.set(t[o],o);let s=new Map;for(let o of e.args)s.set(o,{start:-1,end:-1});for(let o=0;o<t.length;o++){let i=t[o];for(let a=0;a<i.numResults;a++)s.set(i.getResult(a),{start:o,end:o})}for(let o=0;o<t.length;o++){let i=t[o];for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a),p=s.get(u);p&&p.end<o&&(p.end=o)}}return{intervals:s,opIndex:r}}static compute(e,t={}){let r=t.useDef||ft.compute(e),s=r.topologicalOrder,{intervals:o,opIndex:i}=n.buildIntervals(e,s),a=new Map,u=new Map;for(let d of s)a.set(d,new Set),u.set(d,new Set);for(let d=s.length-1;d>=0;d--){let h=s[d],m=u.get(h),g=r.opUsers.get(h);if(g)for(let _ of g){let b=a.get(_);if(b)for(let w of b)m.add(w)}let y=a.get(h);for(let _ of m)y.add(_);for(let _=0;_<h.numResults;_++)y.delete(h.getResult(_));for(let _=0;_<h.numOperands;_++)y.add(h.getOperand(_))}for(let[d,h]of i){let m=a.get(d);for(let g of m){let y=o.get(g);y&&y.end<h&&(y.end=h)}}let p=0,c=null,f=new Map;for(let d=0;d<s.length;d++){let h=s[d],m=a.get(h),g=0;for(let y of m)if(y.type instanceof I){let _=y.type.sizeInBytes();_!==X&&(g+=_)}f.set(h,g),g>p&&(p=g,c=h)}return new pv(a,u,o,i,p,c,f)}}});var pb,fb,rL=M(()=>{"use strict";fn();yr();Re();Ct();nn();ro();nL();Ir();pb=class{static{l(this,"RematerializationConfig")}constructor(e={}){this.memoryBudget=e.memoryBudget||1/0,this.maxIterations=e.maxIterations||100,this.maxRecomputeCost=e.maxRecomputeCost||1/0,this.excludeOps=e.excludeOps||new Set}},fb=class extends qe{static{l(this,"RematerializationPass")}constructor(e={}){super("RematerializationPass"),this.config=e instanceof pb?e:new pb(e)}run(e,t){if(this.config.memoryBudget===1/0)return oe.UNCHANGED;let r=!1,s=0,o=0;for(;s<this.config.maxIterations;){let i=ft.compute(e),{peakPressure:a,candidates:u,opIndex:p}=this._analyzeIntervalPressure(e,i);if(o=a,a<=this.config.memoryBudget||u.length===0)break;u.sort((c,f)=>f.score-c.score),this._rematerialize(e,u[0],p),r=!0,s++}return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,iterations:s,peakPressure:o,budget:this.config.memoryBudget,changed:r,level:le.DEBUG}),r?oe.CHANGED:oe.UNCHANGED}_analyzeIntervalPressure(e,t){let r=t.topologicalOrder,{intervals:s,opIndex:o}=cb.buildIntervals(e,r),i=[];for(let[m,g]of s){if(!(m.type instanceof I))continue;let y=m.type.sizeInBytes();y===X||y<=0||(i.push({idx:g.start,delta:y,value:m}),i.push({idx:g.end+1,delta:-y,value:m}))}i.sort((m,g)=>m.idx-g.idx||m.delta-g.delta);let a=0,u=0,p=0,c=new Set,f=new Set,d=0;for(let m=-1;m<=r.length;m++){for(;d<i.length&&i[d].idx<=m;)a+=i[d].delta,i[d].delta>0&&i[d].value&&f.add(i[d].value),i[d].delta<0&&i[d].value&&f.delete(i[d].value),d++;if(a>u){u=a,p=m,c.clear();for(let g of f)c.add(g)}}let h=[];for(let m of c){if(!this._canRematerialize(m))continue;let g=m.definingOp,y=this._computeMemorySaved(m);if(y<=0)continue;let _=this._estimateRecomputeCost(g);_>=this.config.maxRecomputeCost||_!==0&&h.push({value:m,definingOp:g,memorySaved:y,recomputeCost:_,score:y/_})}return{peakPressure:u,peakIdx:p,candidates:h,opIndex:o}}_canRematerialize(e){if(e.isBlockArgument())return!1;let t=e.definingOp;if(!t||Xt(t.opName)||_n(t.opName)||this.config.excludeOps.has(t.opName)||t.regions.length>0||t.hasSideEffects()||e.useCount<=1)return!1;for(let r=0;r<t.numOperands;r++){let s=t.getOperand(r);if(s.definingOp&&s.definingOp.hasSideEffects())return!1}return!0}_computeMemorySaved(e){if(!(e.type instanceof I))return 0;let t=e.type.sizeInBytes();return t===X?0:t}_operandExtensionCost(e,t,r){let s=0;for(let o=0;o<e.numOperands;o++){let i=e.getOperand(o),a=0;for(let u of i.uses()){let p=r.get(u.user)||0;p>a&&(a=p)}if(t>a&&i.type instanceof I){let u=i.type.sizeInBytes();u!==X&&(s+=u)}}return s}_estimateRecomputeCost(e){let t=ne.get(e.opName);if(!t||t.isOpaque)return 1/0;if(t.getFlops){let r=t.getFlops(e);if(r>0)return r}if(t.isElementwise||t.isBroadcast||t.isView){let r=0;for(let s=0;s<e.numResults;s++){let o=e.getResult(s).type;if(o instanceof I){let i=o.numel();if(i!==X){r=i;break}}}return r||1}if(t.isReduction)for(let r=0;r<e.numOperands;r++){let s=e.getOperand(r).type;if(s instanceof I){let o=s.numel();if(o!==X)return o}}return 1/0}_rematerialize(e,t,r){let{value:s,definingOp:o}=t,i=[];for(let c of s.uses())i.push({user:c.user,operandIndex:c.operandIndex});if(i.length<=1)return;i.sort((c,f)=>(r.get(c.user)||0)-(r.get(f.user)||0));let a=r.get(i[i.length-1].user)||0,u=this._operandExtensionCost(o,a,r),p=this._computeMemorySaved(s);if(!(u>=p))for(let c=1;c<i.length;c++){let f=i[c],d=f.user;if(!d.parentBlock)continue;let h=new Me(o.opName,[...o.operands],o.results.map(m=>m.type),new Map(o.attributes));d.parentBlock.insertBefore(h,d),d.replaceOperand(f.operandIndex,h.getResult(s.resultIndex))}}}});function $o(n){return _n(n.opName)}function sL(n){let e=0;for(let t=0;t<n.numResults;t++){let r=n.getResult(t).type;if(!r||!r.isFullyStatic)continue;let s=r.sizeInBytes();s>e&&(e=s)}return e}function e4(n){let e=n.definingOp;if(e&&e.opName==="broadcast"){let t=e.getOperand(0);e=t&&t.definingOp}if(e&&$o(e)){let t=e.getAttr("value");if(typeof t=="number")return t}return 0}function fv(n){if(n.opName!=="dot"&&n.opName!=="cublas_gemm")return null;let e=n.getOperand(0).type,t=n.getOperand(1).type,r=n.getResult(0).type;if(!e||!t||!r||e.dtype!=="f32"||t.dtype!=="f32"||r.dtype!=="f32")return null;let s=n.getAttr("lhs_batch")||[],o=n.getAttr("rhs_batch")||[];if(s.length>0||o.length>0)return null;let i=n.getAttr("lhs_contracting")||[],a=n.getAttr("rhs_contracting")||[];if(i.length!==1||a.length!==1||t.rank!==2||i[0]!==e.rank-1||a[0]!==0&&a[0]!==1||!e.isFullyStatic||!t.isFullyStatic||!r.isFullyStatic)return null;let u=n.getOperand(0).definingOp,p=n.getOperand(1).definingOp;if(u&&$o(u)||p&&$o(p))return null;let c=a[0]===1,f=e.shape[e.rank-1];if((c?t.shape[1]:t.shape[0])!==f)return null;let h=1;for(let g=0;g<e.rank-1;g++)h*=e.shape[g];let m=c?t.shape[0]:t.shape[1];return h<=0||m<=0||f<=0?null:{M:h,N:m,K:f,transB:c}}function oL(n){return{labelOf:l(e=>n.get(e),"labelOf"),canMerge:l((e,t)=>Math.max(e.maxBuf||0,sL(t))<=X6,"canMerge"),onAttach:l((e,t)=>{e.maxBuf=Math.max(e.maxBuf||0,sL(t))},"onAttach")}}function db(n,e,t){let r=n.opSet,s=Ru(n.ops),{inputs:o,outputs:i,constDefs:a}=pa(r,s,{pullConstants:!0,isConstant:$o});for(let d of o)if(!d.type||!d.type.isFullyStatic)return null;for(let d of i)if(!d.type||!d.type.isFullyStatic)return null;let u=new yn(e,o.map(d=>d.type),i.map(d=>d.type)),p=new Map;for(let d=0;d<o.length;d++)p.set(o[d],u.args[d]);for(let d of a)u.entryBlock.pushOp(d.clone(p));for(let d of s)u.entryBlock.pushOp(d.clone(p));let c=i.map(d=>p.get(d));if(c.some(d=>d===void 0))return null;u.entryBlock.pushOp(new Me("return",c,[]));let f=n.ops.length===1&&t.has(n.ops[0])?n.ops[0]:null;return{part:n,subFunc:u,inputs:o,outputs:i,dotOp:f}}function Bd(n){if(t4.has(n.opName))return!0;if(n.regions)for(let e of n.regions){let t=e.entryBlock;if(t){for(let r of t.ops())if(Bd(r))return!0}}return!1}function n4(n){let e=0;for(let t=0;t<n.numResults;t++){let r=n.getResult(t).type;if(!r||!r.isFullyStatic)continue;let s=1;for(let o of r.shape)s*=o;s>e&&(e=s)}return e}function iL(n,e=256){if(n.functionCount!==1)return!1;let t=n.functions().next().value,r=new Map,s=l(o=>{if(!o)return 0;let i=r.get(o);if(i!==void 0)return i;r.set(o,0);let a=Bd(o)?n4(o):0;for(let u=0;u<o.numOperands;u++){let p=s(o.getOperand(u).definingOp);p>a&&(a=p)}return r.set(o,a),a},"maxBoundaryInSubtree");for(let o of t.ops())if(!(Xt(o.opName)||!Bd(o))){for(let i=0;i<o.numOperands;i++)if(s(o.getOperand(i).definingOp)>e)return!0}return!1}function aL(n,e,t){let r=new Map,s=0,o=l(h=>{let m=r.get(h);return m===void 0&&(m=s++,r.set(h,m)),m},"getSlot");for(let h of n.args)o(h);for(let h of t)for(let m of h.outputs)o(m);let i=[];for(let h of n.args)i.push(o(h));let a=[],u=new Set;for(let h=0;h<e.numOperands;h++){let m=e.getOperand(h),g=i.length,y=m.isBlockArgument&&m.isBlockArgument(),_=m.definingOp&&$o(m.definingOp);if(!y&&!_&&r.has(m)){let b=r.get(m);if(!u.has(b)){u.add(b),i.push(b);continue}i.push(s++),a.push({pos:g,kind:"copy",srcSlot:b});continue}if(y){i.push(s++),a.push({pos:g,kind:"copy",srcSlot:o(m)});continue}if(_){i.push(s++),a.push({pos:g,kind:"const",value:e4(m)});continue}return null}let p=[];for(let h of t){let m=[];for(let y of h.inputs){let _=r.get(y);if(_===void 0)return null;m.push(_)}let g=h.outputs.map(y=>r.get(y));p.push({name:h.subFunc.name,inputSlots:m,outputSlots:g})}let c=new Set(i),f=[],d=new Set;for(let[h,m]of r)if(!(c.has(m)||d.has(m))){if(d.add(m),!h.type||!h.type.isFullyStatic)return null;f.push({slot:m,shape:[...h.type.shape],dtype:h.type.dtype})}return{plan:{numSlots:s,argSlots:i,intermediates:f,steps:p,returnFixups:a}}}function ha(n,e=2){if(n.functionCount!==1)return null;let t=n.functions().next().value,r=t.getReturnOp();if(!r)return null;let s=[],o=new Map,i=0;for(let g of t.ops())Xt(g.opName)||$o(g)||(Bd(g)?o.set(g,"boundary#"+i++):o.set(g,"native"),s.push(g));if(i<e||s.length===0)return null;let{partitions:a,preds:u}=Nd(s,oL(o));if(a.length<2)return null;let p=ov(a,u);if(!p)return null;let c=t.name,f=[],d=new Map,h=0;for(let g of p){let y=db(g,c+"_p"+h++,d);if(!y)return null;f.push(y)}let m=aL(t,r,f);if(!m)return null;n.removeFunction(t.name);for(let g of f)n.addFunction(g.subFunc);return{plan:m.plan}}function uL(n){if(n.functionCount!==1)return null;let e=n.functions().next().value,t=e.getReturnOp();if(!t)return null;let r=[],s=new Map,o=new Map,i=0,a=0;for(let y of e.ops()){if(Xt(y.opName)||$o(y))continue;let _=fv(y);_?(s.set(y,"cublas#"+i),o.set(y,_),i++):Bd(y)?s.set(y,"boundary#"+a++):s.set(y,"native"),r.push(y)}if(i+a===0||r.length===0)return null;let{partitions:u,preds:p}=Nd(r,oL(s));if(u.length<2)return null;let c=ov(u,p);if(!c)return null;let f=e.name,d=[],h=0;for(let y of c){let _=db(y,f+"_p"+h++,o);if(!_)return null;d.push(_)}let m=aL(e,t,d);if(!m)return null;let g=new Map;for(let y of d){if(!y.dotOp)continue;let _=o.get(y.dotOp),b=y.inputs.indexOf(y.dotOp.getOperand(0)),w=y.inputs.indexOf(y.dotOp.getOperand(1)),x=y.outputs.indexOf(y.dotOp.getResult(0));b<0||w<0||x<0||g.set(y.subFunc.name,{M:_.M,N:_.N,K:_.K,transB:_.transB,aIdx:b,bIdx:w,cIdx:y.inputs.length+x})}if(g.size===0)return null;n.removeFunction(e.name);for(let y of d)n.addFunction(y.subFunc);return{plan:m.plan,cublasInfos:g}}var X6,t4,hb=M(()=>{"use strict";Us();yr();Ad();Ir();l($o,"isConstantOp");X6=32*1024;l(sL,"maxResultBytes");l(e4,"constScalarOf");l(fv,"cublasDotInfo");l(oL,"bufferLimitedConfig");l(db,"materializePartition");t4=new Set(["dot","cublas_gemm","reduce","conv","quantized_conv","scaled_dot_product_attention"]);l(Bd,"containsBoundaryOp");l(n4,"maxBoundaryResultBytes");l(iL,"hasDependentBoundaries");l(aL,"buildExecutionPlan");l(ha,"splitGraphForNative");l(uL,"splitGraphForCublas")});function cL(n){return lL.push(n),n}var lL,mb,pL=M(()=>{"use strict";fn();lL=[];l(cL,"registerBackendOpRewrite");mb=class extends qe{static{l(this,"BackendOpRewritePass")}constructor(e={}){super(e.name||"BackendOpRewritePass"),this.config=e,this.rewrites=e.rewrites||lL}run(e){let t=!1;for(let r of[...e.ops()]){let s=r.parentBlock;if(s){for(let o of this.rewrites)if(o.match(r,this.config)){o.build(r,s,this.config),t=!0;break}}}return t?oe.CHANGED:oe.UNCHANGED}}});var r4,gb,fL=M(()=>{"use strict";yr();hb();pL();r4=cL({name:"dot->cublas_gemm",match:l(n=>n.opName==="dot"&&n.numOperands===2&&fv(n)!==null,"match"),build:l((n,e)=>{let t=n.results.map(s=>s.type),r=new Me("cublas_gemm",[n.getOperand(0),n.getOperand(1)],t,new Map(n.attributes),null);e.insertBefore(r,n),n.replaceAllResultsWith(r.results),n.erase()},"build")}),gb=class extends mb{static{l(this,"CublasRewritePass")}constructor(e={}){super({...e,name:"CublasRewritePass",rewrites:[r4]})}}});function dL(n,e,{cudaMatmulChain:t=!1,context:r=null}={}){let s=r?a=>r.passesForPhase(a,n,e):a=>z_(a,n,e),o=[];for(let a of s("pre"))o.push(a);if(o.push(new ql(e)),o.push(new Ul("canonicalize",[new au,new U_({fastMath:n.optimization.fastMath}),new q_,new W_,new Co],n.optimization.maxSimplifyIterations)),n.optimization.layout&&e&&(o.push(new ab({target:e})),o.push(new Co)),n.quantization.enabled&&(o.push(new lb({...n.quantization,target:e})),o.push(new au),o.push(new Co)),n.matmulBackend!=="cublas"&&!t&&(n.fusion.epilogue!==void 0?n.fusion.epilogue:e&&e.enableEpilogueFusion)&&(o.push(new Q_({target:e})),o.push(new Co)),n.fusion.enabled){let a=n.fusion,u=a.launchOverheadUs??s4;a.strategy==="dominator"?o.push(new tb({target:e,...a})):a.strategy==="priority"?(o.push(new sb({target:e,cost:{launchOverheadUs:u},...a})),o.push(new Od({maxFusionSize:e?.maxFusionSize,...a}))):(o.push(new J_({target:e,cost:{launchOverheadUs:u},...a})),o.push(new X_({maxFusionSize:e?.maxFusionSize,...a})),o.push(new Od({maxFusionSize:e?.maxFusionSize,...a}))),o.push(new Co)}if(n.matmulBackend==="cublas"&&o.push(new gb),n.optimization.rematerialization){let a={...n.optimization.rematConfig};a.memoryBudget===void 0&&e&&e.memoryBudgetBytes>0&&(a.memoryBudget=e.memoryBudgetBytes),o.push(new fb(a))}for(let a of s("post"))o.push(a);return o}var s4,hL=M(()=>{"use strict";by();Mx();ME();LE();CE();Ex();PE();FE();zE();VE();UE();KE();QE();tL();Bx();rL();fL();ev();s4=5;l(dL,"buildGraphPipeline")});var ar,ma=M(()=>{"use strict";ar=class{static{l(this,"PrimFuncPass")}constructor(e,t=null){this.name=e,this.phase=t||e,this.snapshotPoint=null,this.trace=null}begin(e){}run(e,t){throw new Error("PrimFuncPass.run not implemented")}end(e){}}});var yb,dv,Mu,hv=M(()=>{"use strict";Fe();yb=["iterCount","depth","parallelLoops","vectorizedLoops","unrolledLoops","threadBoundLoops","serialLoops","threadBlockSize","gridSize","underReduction","numMathOps","numExternCalls","numReads","numWrites","stride1Accesses","stridedAccesses","reuseCount","touchedBytes","arithmeticIntensity","vectorized","parallelized","innermostExtent"],dv=class{static{l(this,"ScheduleFeatures")}constructor(e){this.numLoops=e.numLoops||0,this.numBlocks=e.numBlocks||0,this.totalIterations=e.totalIterations||0,this.maxLoopDepth=e.maxLoopDepth||0,this.numParallelLoops=e.numParallelLoops||0,this.numVectorizedLoops=e.numVectorizedLoops||0,this.numUnrolledLoops=e.numUnrolledLoops||0,this.numThreadBound=e.numThreadBound||0,this.numSerialLoops=e.numSerialLoops||0,this.totalBufferBytes=e.totalBufferBytes||0,this.numBufferReads=e.numBufferReads||0,this.numBufferWrites=e.numBufferWrites||0,this.numMathOps=e.numMathOps||0,this.numExternCalls=e.numExternCalls||0,this.arithmeticIntensity=e.arithmeticIntensity||0,this.innermostExtent=e.innermostExtent||0,this.outermostExtent=e.outermostExtent||0,this.hasReduction=e.hasReduction||!1,this.reductionDepth=e.reductionDepth||0,this.threadBlockSize=e.threadBlockSize||0,this.gridSize=e.gridSize||0,this.strideOneAccesses=e.strideOneAccesses||0,this.nonStrideOneAccesses=e.nonStrideOneAccesses||0}},Mu=class n{static{l(this,"FeatureExtractor")}static extract(e){let t={numLoops:0,numBlocks:0,totalIterations:1,maxLoopDepth:0,currentDepth:0,numParallelLoops:0,numVectorizedLoops:0,numUnrolledLoops:0,numThreadBound:0,numSerialLoops:0,totalBufferBytes:0,buffersSeen:new Set,numBufferReads:0,numBufferWrites:0,numMathOps:0,numExternCalls:0,hasReduction:!1,reductionDepth:0,threadBlockSize:1,gridSize:1,innermostExtent:0,outermostExtent:0,loopExtents:[],strideOneAccesses:0,nonStrideOneAccesses:0};n._visitIterative(e.body,t);let r=t.totalBufferBytes,s=t.numMathOps+t.numExternCalls;return t.arithmeticIntensity=r>0?s/r:0,new dv(t)}static extractStatements(e){let t=[],r=[],s=0,o=[{node:e.body,action:"enter"}];for(;o.length>0;){let{node:i,action:a}=o.pop();if(i){if(a==="leaveFor"){r.pop();continue}if(a==="leaveBlock"){s--;continue}switch(i.type){case"ForNode":r.push(i),o.push({node:null,action:"leaveFor"}),o.push({node:i.body,action:"enter"});break;case"BlockNode":i.initBody&&(s++,o.push({node:null,action:"leaveBlock"}),o.push({node:i.initBody,action:"enter"})),o.push({node:i.body,action:"enter"});break;case"SeqNode":for(let u=i.stmts.length-1;u>=0;u--)o.push({node:i.stmts[u],action:"enter"});break;case"AllocateNode":case"LetStmtNode":o.push({node:i.body,action:"enter"});break;case"IfThenElseNode":i.elseBody&&o.push({node:i.elseBody,action:"enter"}),o.push({node:i.thenBody,action:"enter"});break;case"BufferStoreNode":t.push(n._statementVector(i,r,s));break}}}return t}static _statementVector(e,t,r){let s=1,o=0,i=0,a=0,u=0,p=0,c=1,f=1;for(let T of t){let v=T.extent&&T.extent.type==="IntImmNode"?T.extent.value:1;switch(s*=v,T.kind){case z.PARALLEL:o++;break;case z.VECTORIZED:i++;break;case z.UNROLLED:a++;break;case z.THREAD_BINDING:u++,T.threadTag&&T.threadTag.startsWith("threadIdx")?c*=v:T.threadTag&&T.threadTag.startsWith("blockIdx")&&(f*=v);break;default:p++;break}}let d={math:0,extern:0};n._countExpr(e.value,d);let h=t.map(T=>T.loopVar.name),m=[{buffer:e.buffer,indices:e.indices}];n._collectLoads(e.value,m);let g=0,y=0,_=0,b=0;for(let T of m){let v=T.buffer&&T.buffer.sizeInBytes?T.buffer.sizeInBytes():0;v>0&&(b+=v);let A=T.indices&&T.indices.length>0?T.indices[T.indices.length-1]:null;A&&A.type==="VariableNode"?g++:y++;let N=new Set;if(T.indices)for(let k of T.indices)n._collectVars(k,N);for(let k of h)N.has(k)||_++}let w=t.length>0?t[t.length-1]:null,x=w&&w.extent&&w.extent.type==="IntImmNode"?w.extent.value:0,S={iterCount:s,depth:t.length,parallelLoops:o,vectorizedLoops:i,unrolledLoops:a,threadBoundLoops:u,serialLoops:p,threadBlockSize:c,gridSize:f,underReduction:r>0?1:0,numMathOps:d.math,numExternCalls:d.extern,numReads:m.length-1,numWrites:1,stride1Accesses:g,stridedAccesses:y,reuseCount:_,touchedBytes:b,arithmeticIntensity:b>0?(d.math+d.extern)/b:0,vectorized:i>0?1:0,parallelized:o+u>0?1:0,innermostExtent:x};return yb.map(T=>S[T]||0)}static _countExpr(e,t){if(!(!e||typeof e!="object"))switch(e.type){case"MathOpNode":t.math++,n._countExpr(e.a,t),e.b&&n._countExpr(e.b,t);break;case"CompareNode":t.math++,n._countExpr(e.a,t),n._countExpr(e.b,t);break;case"CallExternNode":t.extern++;for(let r of e.args)n._countExpr(r,t);break;default:break}}static _collectLoads(e,t){if(!(!e||typeof e!="object")){if(e.type==="BufferLoadNode"){t.push({buffer:e.buffer,indices:e.indices});return}if(e.a&&n._collectLoads(e.a,t),e.b&&n._collectLoads(e.b,t),e.args)for(let r of e.args)n._collectLoads(r,t)}}static _collectVars(e,t){if(!(!e||typeof e!="object")){if(e.type==="VariableNode"){t.add(e.name);return}if(e.a&&n._collectVars(e.a,t),e.b&&n._collectVars(e.b,t),e.args)for(let r of e.args)n._collectVars(r,t);if(e.indices)for(let r of e.indices)n._collectVars(r,t)}}static _visitIterative(e,t){let r=[{node:e,action:"enter"}];for(;r.length>0;){let{node:s,action:o}=r.pop();if(s){if(o==="leave_for"){t.currentDepth--;continue}switch(s.type){case"ForNode":{t.numLoops++,t.currentDepth++,t.currentDepth>t.maxLoopDepth&&(t.maxLoopDepth=t.currentDepth);let i=s.extent.type==="IntImmNode"?s.extent.value:1;switch(t.loopExtents.push(i),t.numLoops===1&&(t.outermostExtent=i),t.innermostExtent=i,s.kind){case z.PARALLEL:t.numParallelLoops++;break;case z.VECTORIZED:t.numVectorizedLoops++;break;case z.UNROLLED:t.numUnrolledLoops++;break;case z.THREAD_BINDING:t.numThreadBound++,s.threadTag&&s.threadTag.startsWith("threadIdx")?t.threadBlockSize*=i:s.threadTag&&s.threadTag.startsWith("blockIdx")&&(t.gridSize*=i);break;default:t.numSerialLoops++;break}t.totalIterations*=i,r.push({node:null,action:"leave_for"}),r.push({node:s.body,action:"enter"});break}case"BlockNode":t.numBlocks++,s.initBody&&(t.hasReduction=!0,t.reductionDepth=t.currentDepth);for(let i of s.reads)n._visitBuffer(i.buffer,t);for(let i of s.writes)n._visitBuffer(i.buffer,t);r.push({node:s.body,action:"enter"}),s.initBody&&r.push({node:s.initBody,action:"enter"});break;case"SeqNode":for(let i=s.stmts.length-1;i>=0;i--)r.push({node:s.stmts[i],action:"enter"});break;case"AllocateNode":n._visitBuffer(s.buffer,t),r.push({node:s.body,action:"enter"});break;case"IfThenElseNode":s.elseBody&&r.push({node:s.elseBody,action:"enter"}),r.push({node:s.thenBody,action:"enter"});break;case"LetStmtNode":r.push({node:s.body,action:"enter"});break;case"BufferStoreNode":t.numBufferWrites++,n._visitBuffer(s.buffer,t),n._checkStride(s.buffer,s.indices,t),n._visitExpr(s.value,t);break}}}}static _visitBuffer(e,t){if(!e||t.buffersSeen.has(e))return;t.buffersSeen.add(e);let r=e.sizeInBytes();r>0&&(t.totalBufferBytes+=r)}static _visitExpr(e,t){if(e)switch(e.type){case"MathOpNode":t.numMathOps++,n._visitExpr(e.a,t),e.b&&n._visitExpr(e.b,t);break;case"CallExternNode":t.numExternCalls++;for(let r of e.args)n._visitExpr(r,t);break;case"BufferLoadNode":t.numBufferReads++,n._checkStride(e.buffer,e.indices,t);break;case"CompareNode":t.numMathOps++,n._visitExpr(e.a,t),n._visitExpr(e.b,t);break;default:break}}static _checkStride(e,t,r){if(!e||!t||t.length===0)return;let s=t[t.length-1];for(;s&&s.type==="MathOpNode"&&s.b&&s.b.type==="IntImmNode";)if(s.op==="+"&&s.b.value===0)s=s.a;else if(s.op==="*"&&s.b.value===1)s=s.a;else break;s&&s.type==="VariableNode"?r.strideOneAccesses++:r.nonStrideOneAccesses++}}});function mL(n,e){if(n.length===0)return 0;let t=0;for(let r of n)t+=e[r];return t/n.length}function mv(n,e,t,r,s,o,i){let a=t.length;if(r>=s||a<o*2)return{leaf:mL(t,e)};let u=n[0].length,p=0,c=0,f=new Set(t);for(let g of t){let y=e[g];p+=y,c+=y*y}let d=null,h=null;for(let g=0;g<u;g++){let y=i[g].filter(w=>f.has(w)),_=0,b=0;for(let w=0;w<y.length-1;w++){let x=e[y[w]];_+=x,b+=x*x;let S=n[y[w]][g],T=n[y[w+1]][g];if(S===T)continue;let v=w+1,A=a-v;if(v<o||A<o)continue;let N=p-_,k=c-b,O=b-_*_/v+(k-N*N/A);(!d||O<d.cost)&&(d={f:g,thr:(S+T)/2,sIdx:w,cost:O},h=y)}}if(!d)return{leaf:mL(t,e)};let m=h;return{f:d.f,thr:d.thr,left:mv(n,e,m.slice(0,d.sIdx+1),r+1,s,o,i),right:mv(n,e,m.slice(d.sIdx+1),r+1,s,o,i)}}function o4(n){let e=n.length,t=e>0?n[0].length:0,r=new Array(t),s=[];for(let o=0;o<e;o++)s.push(o);for(let o=0;o<t;o++)r[o]=s.slice().sort((i,a)=>n[i][o]-n[a][o]);return r}function gL(n,e){for(;n&&n.leaf===void 0;)n=e[n.f]<=n.thr?n.left:n.right;return n?n.leaf:0}var Rd,yL=M(()=>{"use strict";l(mL,"leafValue");l(mv,"buildTree");l(o4,"buildFeatureOrder");l(gL,"predictTree");Rd=class n{static{l(this,"GradientBoostedTrees")}constructor(e={}){this.numTrees=e.numTrees??60,this.maxDepth=e.maxDepth??3,this.lr=e.lr??.1,this.minSamples=e.minSamples??1,this.trees=null,this.base=0}fit(e,t){let r=e.length;if(r===0)return;let s=0;for(let p of t)s+=p;s/=r;let o=new Array(r).fill(s),i=[];for(let p=0;p<r;p++)i.push(p);let a=o4(e),u=[];for(let p=0;p<this.numTrees;p++){let c=new Array(r);for(let d=0;d<r;d++)c[d]=t[d]-o[d];let f=mv(e,c,i,0,this.maxDepth,this.minSamples,a);for(let d=0;d<r;d++)o[d]+=this.lr*gL(f,e[d]);u.push(f)}this.trees=u,this.base=s}predict(e){if(!this.trees)return 0;let t=this.base;for(let r of this.trees)t+=this.lr*gL(r,e);return t}serialize(){return{trees:this.trees,base:this.base,lr:this.lr}}static deserialize(e){let t=new n({lr:e.lr});return t.trees=e.trees,t.base=e.base,t}}});function _L(n){let e=n[0].length,t=new Array(e+1).fill(0);for(let r of n)for(let s=0;s<e;s++){let o=r[s]||0;u4.has(s)?o>t[s]&&(t[s]=o):t[s]+=o}for(let r of l4)r<e&&(t[r]/=n.length);return t[e]=n.length,t}var i4,a4,u4,l4,gv,c4,_b,Oc,Bc,yv=M(()=>{"use strict";hv();yL();i4=new Set(["depth","threadBlockSize","gridSize","underReduction","vectorized","parallelized","innermostExtent"]),a4=new Set(["arithmeticIntensity"]),u4=new Set(yb.map((n,e)=>i4.has(n)?e:-1).filter(n=>n>=0)),l4=new Set(yb.map((n,e)=>a4.has(n)?e:-1).filter(n=>n>=0));l(_L,"aggregateStatements");gv=class{static{l(this,"CostEstimate")}constructor(e,t){this.score=e,this.breakdown=t}},c4={parallelism:2,vectorization:1.5,memoryCoalescing:2,occupancy:1,arithmeticIntensity:1,loopOverhead:-.5,codeSize:-.3},_b=class{static{l(this,"AnalyticalCostModel")}constructor(e,t={}){this.target=e,this._weights={...c4,...e&&e.costModelWeights?e.costModelWeights:{},...t.weights||{}}}estimate(e){let t=Mu.extract(e);return this.estimateFromFeatures(t)}score(e){return this.estimate(e).score}estimateFromFeatures(e){let t=this._scoreParallelism(e),r=this._scoreVectorization(e),s=this._scoreMemoryAccess(e),o=this._scoreOccupancy(e),i=this._scoreIntensity(e),a=this._scoreOverhead(e),u=this._scoreCodeSize(e),p=this._weights,c=p.parallelism*t+p.vectorization*r+p.memoryCoalescing*s+p.occupancy*o+p.arithmeticIntensity*i+p.loopOverhead*a+p.codeSize*u;return new gv(c,{parallelism:t,vectorization:r,memoryCoalescing:s,occupancy:o,arithmeticIntensity:i,loopOverhead:a,codeSize:u})}_scoreParallelism(e){if(this.target.isGPU()){let r=e.threadBlockSize*e.gridSize,s=this.target.maxParallelism();return Math.min(1,r/Math.max(s*.1,1))}return e.numParallelLoops/Math.max(e.numLoops,1)}_scoreVectorization(e){return e.numLoops===0?0:this.target.isGPU()?e.strideOneAccesses/Math.max(e.strideOneAccesses+e.nonStrideOneAccesses,1):e.numVectorizedLoops>0?Math.min(1,e.innermostExtent/this.target.vectorWidth):0}_scoreMemoryAccess(e){let t=e.strideOneAccesses+e.nonStrideOneAccesses;return t===0?1:e.strideOneAccesses/t}_scoreOccupancy(e){if(!this.target.isGPU())return 1;if(e.threadBlockSize===0)return 0;let t=this.target.warpSize,r=Math.ceil(e.threadBlockSize/t),s=Math.floor(this.target.maxThreadsPerBlock/t);return Math.min(1,r/s)}_scoreIntensity(e){let t=this.target.supportsFloat16?1.5:1;return Math.min(1,e.arithmeticIntensity*10*t)}_scoreOverhead(e){return e.numSerialLoops/Math.max(e.numLoops,1)}_scoreCodeSize(e){return Math.min(1,(e.numMathOps+e.numExternCalls)/256)}compare(e,t){return this.estimate(e).score-this.estimate(t).score}},Oc=class n{static{l(this,"LearnedCostModel")}constructor(e=null,t={}){this.opts={numTrees:t.numTrees??60,maxDepth:t.maxDepth??3,lr:t.lr??.1,minSamples:t.minSamples??1},this._gbt=e?Rd.deserialize(e):null,this._X=[],this._Y=[]}addSample(e,t){!e||e.length===0||Number.isFinite(t)&&(this._X.push(_L(e)),this._Y.push(t))}train(){if(this._X.length===0)return;let e=new Rd(this.opts);e.fit(this._X,this._Y),this._gbt=e}predict(e){return!this._gbt||!e||e.length===0?0:this._gbt.predict(_L(e))}get trained(){return this._gbt!==null}get sampleCount(){return this._X.length}serialize(){return{gbt:this._gbt?this._gbt.serialize():null,numSamples:this._X.length}}static deserialize(e){return new n(e&&e.gbt?e.gbt:null)}},Bc=class{static{l(this,"GuidedCostModel")}constructor(e,t,r={}){this.analytical=e,this.learned=t,this.confidenceSamples=r.confidenceSamples??8}_learnedConfident(){return this.learned&&this.learned.trained&&this.learned.sampleCount>=this.confidenceSamples}score(e){return this._learnedConfident()?this.learned.predict(Mu.extractStatements(e)):this.analytical.score(e)}}});function*bL(n){let e=[n];for(;e.length>0;){let t=e.pop();if(t){if(yield t,t.body&&e.push(t.body),t.stmts)for(let r of t.stmts)e.push(r);t.thenBody&&e.push(t.thenBody),t.elseBody&&e.push(t.elseBody),t.initBody&&e.push(t.initBody)}}}function Md(n,e){for(let t of bL(n))if(t.type==="BlockNode"&&t.name===e)return t;return null}function Fo(n){let e=[];for(let t of bL(n))t.type==="BlockNode"&&e.push(t.name);return e}function Id(n,e){let t=Tt(n,e);if(!t)return{spatial:0,reduction:0,reads:0,hasReduction:!1};let r=0,s=0;for(let o of t.loops)t.reductionLoopVars.has(o.loopVar.name)?s++:r++;return{spatial:r,reduction:s,reads:t.readBuffers.length,hasReduction:t.hasReduction}}var Rc=M(()=>{"use strict";Do();l(bL,"walkStmts");l(Md,"findBlock");l(Fo,"collectAllBlockNames");l(Id,"analyzeBlockStructure")});function TL(n){return n.isGPU()?f4:p4}function _v(n){let e=0,t=0;for(let[r,s]of n.order)r==="S"?e=Math.max(e,s+1):t=Math.max(t,s+1);return{spatialLevels:e,reductionLevels:t}}var p4,f4,wL,bv=M(()=>{"use strict";p4={name:"mlt_cpu",order:[["S",0],["S",1],["S",2],["S",3],["R",0]],roles:{S0:"parallelize",S3:"vectorize"}},f4={name:"mlt_gpu",order:[["S",0],["S",1],["S",2],["R",0]],roles:{S0:"blockIdx",S1:"threadIdx",S2:"unroll"}},wL={name:"ssrsrs_cpu",order:[["S",0],["S",1],["R",0],["S",2],["R",1],["S",3]],roles:{S0:"parallelize",S3:"vectorize",R1:"unroll"}};l(TL,"getTileStructure");l(_v,"levelCounts")});var Os,$r,bb=M(()=>{"use strict";Os=class{static{l(this,"SearchVariable")}constructor(e,t){this.name=e,this.candidates=t}sample(e){return this.candidates[e(this.candidates.length)]}},$r=class{static{l(this,"ScheduleSketch")}constructor(e,t,r){this.name=e,this.variables=t,this._apply=r}instantiate(e){return(t,r,s)=>{this._apply(t,r,s,e)}}sampleParams(e){let t={};for(let r of this.variables)t[r.name]=r.sample(e);return t}}});function d4(n){let e=[];for(let t=1;t*t<=n;t++){if(n%t!==0)continue;e.push(t);let r=n/t;r!==t&&e.push(r)}return e.sort((t,r)=>t-r),e}function h4(n,e){let t=Math.min(n.length,e.length);for(let r=0;r<t;r++)if(n[r]!==e[r])return n[r]-e[r];return n.length-e.length}function m4(n,e){let t=new Set,r=[];n.sort(h4);for(let i of n){let a=i.join(",");t.has(a)||(t.add(a),r.push(i))}if(r.length<=e)return r;let s=new Set,o=[];for(let i=0;i<e;i++){let a=Math.floor(i*(r.length-1)/(e-1));s.has(a)||(s.add(a),o.push(r[a]))}return o}function wv(n,e,t=48){if(!Number.isFinite(n)||n<1||e<=1)return[[n]];let r=[],s=t*8,o=l((i,a,u)=>{if(!(r.length>=s)){if(a===e-1){r.push([...u,i]);return}for(let p of d4(i))if(u.push(p),o(i/p,a+1,u),u.pop(),r.length>=s)return}},"rec");return o(n,0,[]),m4(r,t)}var xL=M(()=>{"use strict";l(d4,"divisorsOf");l(h4,"cmpTuple");l(m4,"selectDiverse");l(wv,"enumerateFactorizations")});function wb(n){return n.extent&&n.extent.type==="IntImmNode"?n.extent.value:null}function vL(n,e,t,r){let s=[],o=t;for(let i=0;i<r.length-1;i++){let a=n.getLoops(e).find(f=>f.loopVar.name===o);if(!a)return null;let u=1;for(let f=i+1;f<r.length;f++)u*=r[f];let[p,c]=n.split(a,u);s.push(p.loopVar.name),o=c.loopVar.name}return s.push(o),s}function g4(n,e,t,r,s){let o=l(i=>n.getLoops(e).find(a=>a.loopVar.name===i),"find");for(let[i,a]of t.order){let u=t.roles[i+a];if(!u)continue;let p=i==="S"?r:s;if(u==="parallelize"){let c=p[0]&&a<p[0].length?o(p[0][a]):null;c&&n.parallelize(c)}else if(u==="vectorize"){let c=p[p.length-1],f=c&&a<c.length?o(c[a]):null;f&&n.vectorize(f)}else if(u==="unroll")for(let c of p){let f=a<c.length?o(c[a]):null;f&&n.unroll(f)}else(u==="blockIdx"||u==="threadIdx")&&p.forEach((c,f)=>{if(f>=SL.length)return;let d=a<c.length?o(c[a]):null;d&&n.bindThread(d,`${u}.${SL[f]}`)})}}function kL(n,e,t,r,s,o){let i=[];for(let f=0;f<r.length;f++){let d=vL(n,e,r[f],o[`s${f}`]);if(!d)return;i.push(d)}let a=[];for(let f=0;f<s.length;f++){let d=vL(n,e,s[f],o[`r${f}`]);if(!d)return;a.push(d)}let u=[];for(let[f,d]of t.order){let h=f==="S"?i:a;for(let m of h)d<m.length&&u.push(m[d])}let p=new Map(n.getLoops(e).map(f=>[f.loopVar.name,f])),c=u.map(f=>p.get(f)).filter(Boolean);c.length>=2&&n.reorder(...c),g4(n,e,t,i,a)}function NL(n){let e=n.loops.filter(r=>!n.reductionLoopVars.has(r.loopVar.name)),t=n.loops.filter(r=>n.reductionLoopVars.has(r.loopVar.name));return{spatialLoops:e,reductionLoops:t}}function AL(n,e,t,r){let s=[];return n.forEach((o,i)=>s.push(new Os(`s${i}`,wv(wb(o),t)))),e.forEach((o,i)=>s.push(new Os(`r${i}`,wv(wb(o),r)))),s}function OL(n,e){let{spatialLevels:t,reductionLevels:r}=_v(e),{spatialLoops:s,reductionLoops:o}=NL(n);if(s.length===0)return null;for(let p of[...s,...o])if(wb(p)===null)return null;let i=AL(s,o,t,r),a=s.map(p=>p.loopVar.name),u=o.map(p=>p.loopVar.name);return new $r(e.name,i,(p,c,f,d)=>{kL(p,c,e,a,u,d)})}function BL(n,e){let{spatialLevels:t,reductionLevels:r}=_v(e),{spatialLoops:s,reductionLoops:o}=NL(n);if(s.length===0||o.length===0)return null;for(let p of[...s,...o])if(wb(p)===null)return null;let i=AL(s,o,t,r),a=s.map(p=>p.loopVar.name),u=o.map(p=>p.loopVar.name);return new $r(e.name,i,(p,c,f,d)=>{p.decomposeReduction(c),kL(p,`${c}_upd`,e,a,u,d)})}var SL,RL=M(()=>{"use strict";bb();xL();bv();SL=["x","y","z"];l(wb,"staticExtent");l(vL,"multiLevelSplit");l(g4,"applyRoles");l(kL,"tileBlock");l(NL,"splitLoops");l(AL,"tilingVariables");l(OL,"createMultiLevelTilingSketch");l(BL,"createSSRSRSTilingSketch")});function IL(n){return Math.min(n&&n.maxThreadsPerBlock||256,256)}function _4(n){let e=new Set;for(let t=2;t*t<=n;t++){if(n%t!==0)continue;e.add(t);let r=n/t;r>1&&r<n&&e.add(r)}return[...e].sort((t,r)=>t-r)}function EL(n){return new $r("fused",[],(e,t,r,s)=>{e.fuseConsumer(t,n);let o=e.getLoops(t);o.length>0&&e.parallelize(o[0])})}function LL(n){let e=n.loops.filter(i=>n.reductionLoopVars.has(i.loopVar.name));if(e.length!==1)return null;let t=e[0],r=t.extent&&t.extent.type==="IntImmNode"?t.extent.value:null;if(r===null)return null;let s=_4(r);if(s.length===0)return null;let o=t.loopVar.name;return new $r("rfactor",[new Os("rf_factor",s)],(i,a,u,p)=>{i.rfactor(a,o,p.rf_factor);let c=i.getLoops(`${a}_rf_p`);c.length>0&&i.parallelize(c[0])})}function CL(){return new $r("elementwise_cpu",[new Os("vector_width",y4)],(n,e,t,r)=>{let s=n.getLoops(e);if(s.length===0)return;if(s.length===1){let a=s[0].extent;if(a.type==="IntImmNode"&&a.value>=r.vector_width*2){let[u,p]=n.split(s[0],r.vector_width);n.parallelize(u),n.vectorize(p)}else n.parallelize(s[0]);return}n.parallelize(s[0]);let o=s[s.length-1],i=o.extent;if(i.type==="IntImmNode"&&i.value>=r.vector_width){let[,a]=n.split(o,r.vector_width);n.vectorize(a)}})}function DL(){return new $r("elementwise_gpu",[new Os("block_size",ML)],(n,e,t,r)=>{let s=n.getLoops(e);if(s.length===0)return;let o=s[0];for(let c=1;c<s.length;c++){let d=n.getLoops(e).find(h=>h.loopVar.name===s[c].loopVar.name);d&&o.body===d&&(o=n.fuseLoops(o,d))}let i=o.extent;if(i.type!=="IntImmNode"){n.bindThread(o,"threadIdx.x");return}let a=i.value,u=Math.min(t.maxThreadsPerBlock,1024);if(eS(n.func)&&a<=u){n.bindThread(o,"threadIdx.x");return}let p=Math.min(r.block_size,IL(t));if(a>p){let[c,f]=n.split(o,p);n.bindThread(c,"blockIdx.x"),n.bindThread(f,"threadIdx.x")}else n.bindThread(o,"threadIdx.x")})}function PL(){return new $r("reduction_cpu",[],(n,e,t,r)=>{let s=n.getLoops(e);s.length>0&&n.parallelize(s[0])})}function $L(){return new $r("reduction_gpu",[new Os("block_size",ML)],(n,e,t,r)=>{let s=n.getLoops(e);if(s.length===0)return;let o=Tt(n.func,e),i=s.filter(c=>!o||!ec(c,o));if(i.length===0)return;let a=i[0];for(let c=1;c<i.length;c++){let d=n.getLoops(e).find(h=>h.loopVar.name===i[c].loopVar.name);d&&a.body===d&&(a=n.fuseLoops(a,d))}let u=a.extent;if(u.type!=="IntImmNode"){n.bindThread(a,"threadIdx.x");return}let p=Math.min(r.block_size,IL(t));if(u.value>p){let[c,f]=n.split(a,p);n.bindThread(c,"blockIdx.x"),n.bindThread(f,"threadIdx.x")}else n.bindThread(a,"threadIdx.x")})}var ML,y4,FL=M(()=>{"use strict";Do();bb();ML=[32,64,128,256,512,1024],y4=[1,2,4,8,16];l(IL,"gpuThreadCap");l(_4,"reductionDivisors");l(EL,"createFusedTilingSketch");l(LL,"createRfactorSketch");l(CL,"createElementwiseCPUSketch");l(DL,"createElementwiseGPUSketch");l(PL,"createReductionCPUSketch");l($L,"createReductionGPUSketch")});function Tb(n){return Oy(n,Tb,(e,t,r)=>{switch(e.type){case"PrimFunc":return t.name=e.name,t.params=e.params,t.body=r(e.body),t.bufferMap=new Map(e.bufferMap),t.shapeParams=e.shapeParams,t.shapeParamMap=e.shapeParamMap instanceof Map?new Map(e.shapeParamMap):e.shapeParamMap,t._setChild("body",t.body),t;case"AllocateNode":return t.buffer=e.buffer,t.scope=e.scope,t.body=r(e.body),t._setChild("body",t.body),t;case"LetStmtNode":return t.variable=e.variable,t.value=r(e.value),t.body=r(e.body),t._setChild("body",t.body),t;case"WhileNode":return t.condVar=e.condVar,t.condBody=r(e.condBody),t.loopBody=r(e.loopBody),t._setChild("condBody",t.condBody),t._setChild("loopBody",t.loopBody),t;default:for(let s of Object.keys(e)){if(s==="_parent"||s==="_parentKey"||s==="_parentIdx")continue;let o=e[s];o instanceof Map?t[s]=new Map(o):Array.isArray(o)?t[s]=o.map(r):typeof o=="object"&&o!==null&&o.type?t[s]=r(o):t[s]=o}return t}})}function Ti(n){return Tb(n)}function Iu(n){return Tb(n)}function Tv(n){return Tb(n)}function zL(n,e,t){let r=t.get(e);if(!r)return null;let s=[],o=r._parent;for(;o&&o!==n;)o.type==="ForNode"&&s.push(o),o=o._parent;s.reverse();let i=Tv(r);for(let p=s.length-1;p>=0;p--){let c=s[p],f=new he(c.loopVar,Tv(c.min),Tv(c.extent),c.kind,i,c.threadTag);f._setChild("body",i),i=f}let a=new Map;for(let p of r.reads)a.set(p.buffer.name,p.buffer);for(let p of r.writes)a.set(p.buffer.name,p.buffer);let u=[];for(let p of n.params)a.has(p.name)&&u.push(p);return new Pi("__tune_"+e,u,i,a,[])}var Ed=M(()=>{"use strict";Fe();Gx();l(Tb,"deepCloneIR");l(Ti,"clonePrimFunc");l(Iu,"cloneTensorIR");l(Tv,"cloneBlockSubtree");l(zL,"extractBlockMini")});function kv(n,e){let t=[];for(let r=1;r<=e;r*=2)r>=n&&t.push(r);return t}function Dd(n){if(!n)return null;let e=[];for(let t of n){if(!t||t.type!=="VariableNode")return null;e.push(t.name)}return e}function b4(n,e){let t=[n];for(;t.length>0;){let r=t.pop();if(!(!r||typeof r!="object")){if(r.type==="BufferStoreNode"&&r.buffer&&r.buffer.name===e&&r.value&&r.value.type==="MathOpNode"&&r.value.op==="+")return r;if(r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody)}}return null}function Cd(n){if(!n||n.broadcastDims||n.offset&&n.offset!==0)return!1;let e=n.shape,t=n.strides;if(!t||t.length!==e.length)return!1;let r=1;for(let s=e.length-1;s>=0;s--){if(typeof e[s]!="number"||e[s]<=0||t[s]!==r)return!1;r*=e[s]}return!0}function Nv(n,e){if(!Tt(n,e))return null;let r=Md(n.body,e);if(!r||r.reads.length<2||r.writes.length<1)return null;let s=r.writes[0].buffer;if(!s||s.shape.length<2)return null;let o=s.shape.length,i=b4(r.body,s.name);if(!i)return null;let a=Dd(i.indices);if(!a||a.length!==o)return null;let u=i.value,p=l(T=>T&&T.type==="BufferLoadNode"&&T.buffer&&T.buffer.name===s.name,"isCLoad"),c=p(u.a)?u.b:p(u.b)?u.a:null;if(!c||c.type!=="MathOpNode"||c.op!=="*")return null;let f=[c.a,c.b];if(!f.every(T=>T&&T.type==="BufferLoadNode"&&T.buffer))return null;let d=a[o-1],h=a.slice(0,o-1),m=null,g=null;for(let T of f){let v=Dd(T.indices);if(!v||v.length!==o)continue;let A=!0;for(let N=0;N<o-1;N++)if(v[N]!==h[N]){A=!1;break}if(A){m=T,g=v[o-1];break}}if(!m||g==null)return null;let y=f[0]===m?f[1]:f[0],_=Dd(y.indices);if(!_)return null;let b=m.buffer,w=y.buffer;if(b.shape.length!==o||b.dtype!=="f32"||w.dtype!=="f32"||s.dtype!=="f32")return null;for(let T=0;T<o-1;T++)if(b.shape[T]!==s.shape[T])return null;let x=s.shape[o-1],S=b.shape[o-1];if(![x,S].every(T=>typeof T=="number"&&T>0))return null;if(_.length===2&&w.shape.length===2){let T;if(_[0]===g&&_[1]===d)T=!1;else if(_[0]===d&&_[1]===g)T=!0;else return null;if(T){if(w.shape[0]!==x||w.shape[1]!==S)return null}else if(w.shape[0]!==S||w.shape[1]!==x)return null;let v=1;for(let A=0;A<o-1;A++){let N=s.shape[A];if(typeof N!="number"||N<=0)return null;v*=N}return o===2?{A:b,B:w,C:s,M:v,N:x,K:S,transB:T,batch:1}:!Cd(b)||!Cd(s)?null:{A:new me(b.name,[v,S],b.dtype,b.scope),B:w,C:new me(s.name,[v,x],s.dtype,s.scope),M:v,N:x,K:S,transB:T,batch:1}}if(o>=3&&_.length===o&&w.shape.length===o){let T=a.slice(0,o-2);for(let B=0;B<o-2;B++)if(_[B]!==T[B])return null;let v;if(_[o-2]===g&&_[o-1]===d)v=!1;else if(_[o-2]===d&&_[o-1]===g)v=!0;else return null;for(let B=0;B<o-2;B++)if(w.shape[B]!==s.shape[B])return null;if(v){if(w.shape[o-2]!==x||w.shape[o-1]!==S)return null}else if(w.shape[o-2]!==S||w.shape[o-1]!==x)return null;let A=s.shape[o-2];if(typeof A!="number"||A<=0)return null;let N=1;for(let B=0;B<o-2;B++){let E=s.shape[B];if(typeof E!="number"||E<=0)return null;N*=E}if(!Cd(b)||!Cd(w)||!Cd(s))return null;let k=new me(b.name,[N,A,S],b.dtype,b.scope),O=new me(w.name,v?[N,x,S]:[N,S,x],w.dtype,w.scope),R=new me(s.name,[N,A,x],s.dtype,s.scope);return{A:k,B:O,C:R,M:A,N:x,K:S,transB:v,batch:N}}return null}function qL(n,e,t=32){let r=n.maxThreadsPerBlock||1024,s=n.warpSize||32,o=n.sharedMemoryBytes||49152,i=n.registersPerThread||255,a=4,{M:u,N:p,K:c}=e,f=kv(2,8),d=kv(2,s/2),h=kv(s/4,s/2),m=2*s,g=Math.min(r,8*s),y=4*s,_=new Set,b=[];for(let w of f)for(let x of f)if(!(w*x>y)&&!(w*x+w+x+s>i))for(let S of d)for(let T of d){let v=T*S;if(v<m||v>g||v%s!==0)continue;let A=w*S,N=x*T;if(!(A>u||N>p||A>y||N>y))for(let k of h){if(k>c||(A*k+k*N)*a>o)continue;let R=`${A}_${N}_${k}_${w}_${x}`;_.has(R)||(_.add(R),b.push({BM:A,BN:N,BK:k,TM:w,TN:x,threads:v}))}}return b.sort((w,x)=>jL(x,s)-jL(w,s)),b.slice(0,t)}function jL(n,e){let t=n.TM*n.TN,r=-Math.abs(n.TM-n.TN),s=-Math.abs(Math.log2(n.BM)-Math.log2(n.BN)),o=n.threads>=4*e&&n.threads<=8*e?1:0,i=n.BK===e/4?1:0;return o*100+t*4+r*6+s*4+i}function xb(n,e){if(!e)return null;let t=qL(n,e,256);return t.find(s=>s.BM===64&&s.BN===64&&s.BK===8&&s.TM===4&&s.TN===4)||(t.length>0?t[0]:null)}function w4(n,e,t,r){let s=Iu(n.storeValue);return Gi(s,o=>o.type==="BufferLoadNode"&&o.buffer?o.buffer.name===n.inputName?e:n.scalarConsts.has(o.buffer.name)?Iu(n.scalarConsts.get(o.buffer.name)):o:o.type==="VariableNode"?o.name===n.iv0?Iu(t):o.name===n.iv1?Iu(r):o:o)}function Av(n,e,t=null){let{A:r,B:s,C:o,M:i,N:a,K:u,transB:p}=n,c=n.batch||1,{BM:f,BN:d,BK:h,TM:m,TN:g}=e,y=d/g,_=f/m,b=y*_,w=Math.ceil(u/h),x=f*h,S=h*d,T=Math.ceil(x/b),v=Math.ceil(S/b),A=i%f!==0,N=a%d!==0,k=u%h!==0,O=x%b!==0,R=S%b!==0,B=new me("rb_As",[x],"f32","shared"),E=new me("rb_Bs",[S],"f32","shared"),F=new me("rb_acc",[m*g],"f32","local"),C=new me("rb_af",[m],"f32","local"),H=new me("rb_bf",[g],"f32","local"),G=Jt("rb_bx"),U=Jt("rb_by"),D=Jt("rb_tx"),V=Jt("rb_ty"),ue=Jt("rb_tid"),ce=Jt("rb_brow"),Le=Jt("rb_bcol"),j=Jt("rb_k0"),W=Jt("rb_bz"),ie=c>1,je=l((Ot,lo)=>ie?[W,Ot,lo]:[Ot,lo],"gIdx"),ke=l((Ot,lo)=>Jn(Fr(Ot,nt(g)),lo),"accIdx"),rt=Jt("rb_im"),zt=Jt("rb_in"),on=xi(rt,m,xi(zt,g,new J(F,[ke(rt,zt)],xv()))),Zt=Jt("rb_la"),it=Jt("rb_aidx"),ye=Jn(ce,VL(it,nt(h))),Xe=Jn(j,GL(it,nt(h))),Nn=new q(r,je(ye,Xe));if(A||k){let Ot=A?so(ye,nt(i)):null;k&&(Ot=Ot?Sv(Ot,so(Xe,nt(u))):so(Xe,nt(u))),Nn=new Se(Ot,Nn,xv())}let Qt=new J(B,[it],Nn);O&&(Qt=new Se(so(it,nt(x)),Qt));let an=vv(Zt,T,new et(it,Jn(ue,Fr(Zt,nt(b))),Qt)),un=Jt("rb_lb"),mn=Jt("rb_bidx"),cr=Jn(j,VL(mn,nt(d))),Vn=Jn(Le,GL(mn,nt(d))),Or=new q(s,p?je(Vn,cr):je(cr,Vn));if(k||N){let Ot=k?so(cr,nt(u)):null;N&&(Ot=Ot?Sv(Ot,so(Vn,nt(a))):so(Vn,nt(a))),Or=new Se(Ot,Or,xv())}let pr=new J(E,[mn],Or);R&&(pr=new Se(so(mn,nt(S)),pr));let yt=vv(un,v,new et(mn,Jn(ue,Fr(un,nt(b))),pr)),fr=Jt("rb_kk"),xt=Jt("rb_fi"),Vr=Jt("rb_fj"),Ds=xi(xt,m,new J(C,[xt],new q(B,[Jn(Fr(Jn(Fr(V,nt(m)),xt),nt(h)),fr)]))),uo=xi(Vr,g,new J(H,[Vr],new q(E,[Jn(Fr(fr,nt(d)),Jn(Fr(D,nt(g)),Vr))]))),Gr=Jt("rb_mi"),dr=Jt("rb_ni"),Br=xi(Gr,m,xi(dr,g,new J(F,[ke(Gr,dr)],Jn(new q(F,[ke(Gr,dr)]),Fr(new q(C,[Gr]),new q(H,[dr])))))),qo=xi(fr,h,new te([Ds,uo,Br])),jr=Jt("rb_kt"),Ur=new et(j,Fr(jr,nt(h)),new te([an,yt,new Fn,qo,new Fn])),Ps=vv(jr,w,Ur),ms=Jt("rb_wm"),va=Jt("rb_wn"),Oi=Jn(Jn(ce,Fr(V,nt(m))),ms),Bi=Jn(Jn(Le,Fr(D,nt(g))),va),ka=new q(F,[ke(ms,va)]),np=t?t.outBuffer:o,zu=t?w4(t,ka,Oi,Bi):ka,Na=new J(np,je(Oi,Bi),zu);if(A||N){let Ot=A?so(Oi,nt(i)):null;N&&(Ot=Ot?Sv(Ot,so(Bi,nt(a))):so(Bi,nt(a))),Na=new Se(Ot,Na)}let Aa=xi(ms,m,xi(va,g,Na)),Wo=new te([on,Ps,Aa]),Oa=new Ue(F,"local",new Ue(C,"local",new Ue(H,"local",Wo))),Ri=new et(ue,Jn(Fr(V,nt(y)),D),new et(ce,Fr(U,nt(f)),new et(Le,Fr(G,nt(d)),Oa))),Ko=Math.ceil(a/d),Mi=Math.ceil(i/f),Ba=Ld(U,"blockIdx.y",Mi,Ld(G,"blockIdx.x",Ko,Ld(V,"threadIdx.y",_,Ld(D,"threadIdx.x",y,Ri))));return ie&&(Ba=Ld(W,"blockIdx.z",c,Ba)),new Ue(B,"shared",new Ue(E,"shared",Ba))}function T4(n){let e=new Os("config_index",n.map((r,s)=>s)),t=new $r("matmul_register_block_gpu",[e],(r,s,o,i)=>{let a=Nv(r.func,s);if(!a)return;let u=n[i.config_index];if(!u)return;let p=Av(a,u);r.func.body=p,r.func._setChild&&r.func._setChild("body",p),r.func.gpuRegisterBlocked=!0});return t.configs=n,t.enumerate=()=>n.map((r,s)=>({config_index:s})),t}function x4(n){let e=Fo(n.body),t=null;for(let s of e){let o=Id(n,s);if(o.hasReduction&&o.spatial>=2&&o.reads>=2){if(t)return null;t=s}}if(!t)return null;let r=Nv(n,t);if(!r)return null;for(let s of e){if(s===t)continue;let o=Tt(n,s);if(!o||o.hasReduction||o.readBuffers.length>0)return null;for(let i of o.writeBuffers)if(i!==r.C.name)return null}return{reductionBlock:t,dims:r}}function S4(n){let e=new Map,t=new Map;qs(n,r=>{if(r.type==="BufferStoreNode"&&r.buffer){let s=r.buffer.name;t.set(s,(t.get(s)||0)+1),(!r.indices||r.indices.length===0)&&r.value&&(r.value.type==="FloatImmNode"||r.value.type==="IntImmNode")&&e.set(s,r.value)}});for(let r of[...e.keys()])(t.get(r)||0)!==1&&e.delete(r);return e}function v4(n,e){let t=null;return qs(n,r=>{if(r.type==="BufferStoreNode"&&r.buffer&&r.buffer.name===e)return t=r,Bm}),t}function k4(n,e){let t=null,r=0;return qs(n,s=>{s.type==="BufferLoadNode"&&s.buffer&&s.buffer.name===e&&(r++,t=s)}),r===1?t:null}function WL(n){let e=Fo(n.body),t=null;for(let y of e){let _=Id(n,y);if(_.hasReduction&&_.spatial>=2&&_.reads>=2){if(t)return null;t=y}}if(!t)return null;let r=Nv(n,t);if(!r)return null;let s=r.C.name,o=new Set;for(let y of e){let _=Tt(n,y);if(!_)return null;for(let b of _.writeBuffers)o.add(b)}let i=[];for(let y of e){if(y===t)continue;let _=Tt(n,y);if(_.hasReduction)return null;if(_.readBuffers.length===0){if(_.writeBuffers.every(b=>b===s))continue;return null}i.push({name:y,info:_})}if(i.length===0)return{reductionBlock:t,dims:r,epilogue:null};if(i.length!==1||(r.batch||1)!==1)return null;let a=i[0];if(a.info.writeBuffers.length!==1)return null;let u=a.info.writeBuffers[0],p=S4(n.body),c=0;for(let y of a.info.readBuffers){if(y===s){c++;continue}if(o.has(y)&&!p.has(y))return null}if(c!==1)return null;let f=Md(n.body,a.name);if(!f)return null;let d=v4(f.body,u);if(!d)return null;let h=Dd(d.indices);if(!h||h.length!==2)return null;let m=k4(d.value,s);if(!m)return null;let g=Dd(m.indices);return!g||g.length!==h.length||g.some((y,_)=>y!==h[_])?null:{reductionBlock:t,dims:r,epilogue:{outBuffer:d.buffer,storeValue:d.value,inputName:s,iv0:h[0],iv1:h[1],scalarConsts:p}}}function KL(n,e,t){let r=x4(n);if(!r)return null;let s=UL.get(n);if(s===void 0){let o=qL(t,r.dims);s=o.length>0?T4(o):null,UL.set(n,s)}return s?e===r.reductionBlock?[s]:[]:null}var nt,xv,Jt,Jn,Fr,VL,GL,so,Sv,vv,xi,Ld,UL,Sb=M(()=>{"use strict";Fe();ei();Do();bb();Rc();ts();Ed();nt=l(n=>new L(n),"I"),xv=l(()=>new de(0),"FZERO"),Jt=l(n=>new Oe(n,"i32"),"IV"),Jn=l((n,e)=>Un("+",n,e),"ADD"),Fr=l((n,e)=>Un("*",n,e),"MUL"),VL=l((n,e)=>Un("//",n,e),"DIV"),GL=l((n,e)=>Un("%",n,e),"MOD"),so=l((n,e)=>new Ke("lt",n,e),"LT"),Sv=l((n,e)=>new P("&&",n,e),"AND"),vv=l((n,e,t)=>new he(n,nt(0),nt(e),z.SERIAL,t),"forS"),xi=l((n,e,t)=>new he(n,nt(0),nt(e),z.UNROLLED,t),"forU"),Ld=l((n,e,t,r)=>new he(n,nt(0),nt(t),z.THREAD_BINDING,r,e),"forT");l(kv,"pow2Range");l(Dd,"plainVars");l(b4,"findAccStore");l(Cd,"isContiguousRowMajor");l(Nv,"matmulTileDims");l(qL,"enumerateRegisterBlockConfigs");l(jL,"goodness");l(xb,"pickFixedConfig");l(w4,"foldEpilogue");l(Av,"buildRegisterBlockedMatmul");l(T4,"createMatmulRegisterBlockGPUSketch");l(x4,"analyzePureMatmul");l(S4,"collectScalarConstBuffers");l(v4,"findEpilogueStore");l(k4,"singleBufferLoad");l(WL,"analyzeMatmulEpilogue");UL=new WeakMap;l(KL,"richMatmulSketches")});function N4(n,e){let t=[n];for(;t.length;){let r=t.pop();if(!(!r||typeof r!="object")&&(r.type==="BlockNode"&&e(r),r.body&&t.push(r.body),r.initBody&&t.push(r.initBody),r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),Array.isArray(r.stmts)))for(let s of r.stmts)t.push(s)}}function $d(n){let e=[];N4(n.body,r=>{e.push({name:r.name,node:r,reads:(r.reads||[]).map(s=>s.buffer&&s.buffer.name).filter(Boolean),writes:(r.writes||[]).map(s=>s.buffer&&s.buffer.name).filter(Boolean)})});let t=new Map;for(let r of e)for(let s of r.reads)t.has(s)||t.set(s,[]),t.get(s).push(r.name);return{blocks:e,consumersByBuf:t,byName:new Map(e.map(r=>[r.name,r]))}}function Ov(n){let e=[];for(let t of n){if(!t||t.type!=="VariableNode")return null;e.push(t.name)}return e}function Pd(n,e,t){if(!n||typeof n!="object")return t;n.type==="BufferLoadNode"&&n.buffer&&n.buffer.name===e&&t.push(n);for(let r of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])n[r]&&Pd(n[r],e,t);if(Array.isArray(n.args))for(let r of n.args)Pd(r,e,t);if(Array.isArray(n.indices))for(let r of n.indices)Pd(r,e,t);if(Array.isArray(n.stmts))for(let r of n.stmts)Pd(r,e,t);return t}function Mc(n,e){if(!n||typeof n!="object")return e;n.type==="BufferLoadNode"&&n.buffer&&n.buffer.name&&e.add(n.buffer.name);for(let t of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])n[t]&&Mc(n[t],e);if(Array.isArray(n.args))for(let t of n.args)Mc(t,e);if(Array.isArray(n.indices))for(let t of n.indices)Mc(t,e);if(Array.isArray(n.stmts))for(let t of n.stmts)Mc(t,e);return e}function vb(n,e,t,r){let s=e.byName.get(t);if(!s||s.writes.length!==1)return null;let o=s.writes[0],i=[...new Set((e.consumersByBuf.get(o)||[]).filter(w=>w!==t))];if(i.length!==1)return null;let a=i[0],u=r(n,t),p=r(n,a);if(!u||!p||p.hasReduction)return null;let c=e.byName.get(a);if(!c||c.writes.length!==1||c.writes[0]===o)return null;let f=new Set(s.reads);if(Mc(s.node.body,f),Mc(s.node.initBody,f),f.has(c.writes[0]))return null;let d=s.node.body&&s.node.body.type==="BufferStoreNode"?s.node.body:null,h=c.node.body&&c.node.body.type==="BufferStoreNode"?c.node.body:null;if(!d||!h)return null;let m=u.loops.filter(w=>!u.reductionLoopVars.has(w.loopVar.name)).map(w=>w.loopVar.name),g=Ov(d.indices);if(!g||g.join(",")!==m.join(","))return null;let y=p.loops.map(w=>w.loopVar.name);if(y.length!==m.length)return null;let _=Ov(h.indices);if(!_||_.join(",")!==y.join(","))return null;let b=Pd(h.value,o,[]);if(b.length===0)return null;for(let w of b){let x=Ov(w.indices);if(!x||x.join(",")!==_.join(","))return null}return a}var Bv=M(()=>{"use strict";l(N4,"walkBlocks");l($d,"buildBlockDAG");l(Ov,"indexVarNames");l(Pd,"collectLoads");l(Mc,"collectLoadedBuffers");l(vb,"findFusibleConsumer")});function Rv(n){return n.isGPU()?$L():PL()}function A4(n){return n.isGPU()?DL():CL()}function O4(n,e,t,r){let s=Tt(n,e);if(!s)return[Rv(t)];let o=[],i=OL(s,TL(t));if(i&&o.push(i),t.kind===Dt.CPU){let a=BL(s,wL);a&&o.push(a);let u=LL(s);u&&o.push(u);let p=r?vb(n,r,e,Tt):null;p&&o.push(EL(p))}return o.push(Rv(t)),o}function Mv(n,{priority:e=100}={}){kb.some(t=>t.derive===n.derive&&t.matches===n.matches)||(kb.push({matches:n.matches,derive:n.derive,priority:e}),kb.sort((t,r)=>t.priority-r.priority))}function HL(n,e,t,r={}){if(r.richGpu&&t.isGPU()){let i=KL(n,e,t);if(i!==null)return i}if(t.kind!==Dt.CPU&&!t.isGPU())return[];let s=Id(n,e),o=r.dag||$d(n);for(let i of kb)if(i.matches(s,t))return i.derive(n,e,t,o);return[]}var kb,YL=M(()=>{"use strict";yo();Do();Rc();bv();RL();FL();Sb();Bv();l(Rv,"reductionSketch");l(A4,"elementwiseSketch");l(O4,"deriveMultiLevel");kb=[];l(Mv,"registerSketchRule");Mv({matches:l(n=>n.hasReduction&&n.spatial>=1&&n.reads>=2,"matches"),derive:O4},{priority:10});Mv({matches:l(n=>n.hasReduction,"matches"),derive:l((n,e,t)=>[Rv(t)],"derive")},{priority:20});Mv({matches:l(()=>!0,"matches"),derive:l((n,e,t)=>[A4(t)],"derive")},{priority:30});l(HL,"deriveSketches")});function Iv(n,e,t,r,s={}){return HL(n,e,t,s)}var JL=M(()=>{"use strict";YL();l(Iv,"getSketchesForBlock")});function Nb(n){let e=new Map,t=[n];for(;t.length>0;){let r=t.pop();if(r){if(r.type==="BlockNode"&&e.set(r.name,r),r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),r.initBody&&t.push(r.initBody)}}return e}function Ab(n,e,t,r=null){let s=r||Nb(n.body),o=s.get(e)||null,i=[];if(o){let a=[];for(let p of o.reads)a.push(`${p.buffer.shape.join("x")}:${p.buffer.dtype}`);for(let p of o.writes)a.push(`${p.buffer.shape.join("x")}:${p.buffer.dtype}`);i.push(a.join(","));let u=[];if(sn(o.body,u),o.initBody&&sn(o.initBody,u),i.push(u.join(";")),o.writes.length===1){let p=o.writes[0].buffer.name,c=[];for(let f of B4(s).get(p)||[]){if(f===o)continue;let d=[];sn(f.body,d),f.initBody&&sn(f.initBody,d),c.push(d.join(";"))}c.length>0&&(c.sort(),i.push(`consumers:${c.join("|")}`))}}return i.push(t.name),i.push(t.kind),R4(i.join("|"))}function B4(n){if(n.__readersByBuffer)return n.__readersByBuffer;let e=new Map;for(let t of n.values()){if(!t.reads)continue;let r=new Set;for(let s of t.reads)s.buffer&&r.add(s.buffer.name);for(let s of r){let o=e.get(s);o||(o=[],e.set(s,o)),o.push(t)}}return n.__readersByBuffer=e,e}function sn(n,e){if(!(!n||typeof n!="object"))switch(n.type){case"BufferStoreNode":if(e.push("store"),n.indices)for(let t of n.indices)sn(t,e);sn(n.value,e);return;case"BufferLoadNode":if(e.push(`load:${n.buffer.shape.join("x")}:${n.buffer.dtype}`),n.indices)for(let t of n.indices)sn(t,e);return;case"MathOpNode":e.push(`math:${n.op}`),sn(n.a,e),n.b&&sn(n.b,e);return;case"CallExternNode":e.push(`call:${n.externName}`);for(let t of n.args)sn(t,e);return;case"CompareNode":e.push(`cmp:${n.direction}`),sn(n.a,e),sn(n.b,e);return;case"CastNode":e.push(`cast:${n.fromDtype}->${n.toDtype}`),sn(n.expr,e);return;case"IfThenElseNode":sn(n.condition,e),sn(n.thenBody,e),n.elseBody&&sn(n.elseBody,e);return;case"SeqNode":for(let t of n.stmts)sn(t,e);return;case"ForNode":sn(n.body,e);return;case"BlockNode":n.initBody&&sn(n.initBody,e),sn(n.body,e);return;case"LetStmtNode":sn(n.value,e),sn(n.body,e);return;default:return}}function R4(n){let e=2166136261;for(let t=0;t<n.length;t++)e^=n.charCodeAt(t),e=e*16777619>>>0;return e.toString(16).padStart(8,"0")}var Ev=M(()=>{"use strict";l(Nb,"buildBlockMap");l(Ab,"computeWorkloadKey");l(B4,"readersByBuffer");l(sn,"collectBlockOps");l(R4,"fnv1a")});function M4(n,e){let t=n.medianMs!=null?1:0,r=e.medianMs!=null?1:0;return t!==r?r-t:t===1?n.medianMs-e.medianMs:e.score-n.score}var ZL,Fd,zd,QL=M(()=>{"use strict";Ev();ZL="mlfw-codegen-1",Fd=class{static{l(this,"TuningRecord")}constructor(e,t,r,s,o,i){this.workloadKey=e,this.sketchName=t,this.params=r,this.score=s,this.traceData=o,this.version=i,this.timestamp=Date.now(),this.medianMs=null,this.minMs=null}};l(M4,"rankRecords");zd=class n{static{l(this,"TuningDatabase")}constructor(e=1){this.version=e,this._records=new Map}computeWorkloadKey(e,t,r,s=null){return Ab(e,t,r,s)}store(e,t){let r=this._records.get(e);r||(r=[],this._records.set(e,r)),r.push(t),r.sort(M4),r.length>10&&(r.length=10)}lookup(e){let t=this._records.get(e);return!t||t.length===0?null:t[0]}lookupTopK(e,t=5){let r=this._records.get(e);return r?r.slice(0,t):[]}has(e){return this._records.has(e)&&this._records.get(e).length>0}get size(){let e=0;for(let[,t]of this._records)e+=t.length;return e}serialize(){let e=[];for(let[t,r]of this._records)for(let s of r)e.push({workloadKey:s.workloadKey,sketchName:s.sketchName,params:s.params,score:s.score,traceData:s.traceData,version:s.version,timestamp:s.timestamp,medianMs:s.medianMs,minMs:s.minMs});return{version:this.version,codegenVersion:ZL,entries:e}}static deserialize(e){let t=new n(e.version);if(e.codegenVersion!==void 0&&e.codegenVersion!==ZL)return t;for(let r of e.entries){let s=new Fd(r.workloadKey,r.sketchName,r.params,r.score,r.traceData,r.version);s.timestamp=r.timestamp,s.medianMs=r.medianMs??null,s.minMs=r.minMs??null,t.store(r.workloadKey,s)}return t}saveToFile(e,t){return t.writeFile(e,JSON.stringify(this.serialize())),e}static loadFromFile(e,t){return t.exists(e)?n.deserialize(JSON.parse(t.readFile(e))):new n}clear(){this._records.clear()}}});function ga(n,e){if(!n)return 0;if(n.type==="ForNode"){let t=n.extent&&n.extent.type==="IntImmNode"?n.extent.value:1,r=n.threadTag?e:e*t;return ga(n.body,r)}if(n.type==="BlockNode")return Math.max(e,ga(n.body,e),n.initBody?ga(n.initBody,e):0);if(n.type==="SeqNode"){let t=e;for(let r of n.stmts)t=Math.max(t,ga(r,e));return t}return n.type==="IfThenElseNode"?Math.max(ga(n.thenBody,e),n.elseBody?ga(n.elseBody,e):e):n.type==="AllocateNode"||n.type==="LetStmtNode"?ga(n.body,e):e}function XL(n,e=.1){let t=n.slice().sort((g,y)=>g-y),r=t.length;if(r===0)return{median:0,min:0,trimmedMean:0,cv:0};let s=t[r>>1],o=t[0],i=Math.floor(r*e),a=i,u=r-i>a?r-i:r,p=0,c=0;for(let g=a;g<u;g++)p+=t[g],c++;let f=c>0?p/c:s,d=0;for(let g=a;g<u;g++){let y=t[g]-f;d+=y*y}let h=c>1?Math.sqrt(d/(c-1)):0,m=f>0?h/f:0;return{median:s,min:o,trimmedMean:f,cv:m}}var I4,Ob,Bb,e2=M(()=>{"use strict";Ep();I4=1e6;l(ga,"maxSerialTripCount");Ob=class{static{l(this,"BenchmarkResult")}constructor(e,t,r,s,o=null,i=0){this.medianMs=e,this.minMs=t,this.trimmedMeanMs=o??e,this.cv=i,this.samples=r,this._totalBytes=s}get throughputGBs(){return this.minMs<=0||!this._totalBytes?0:this._totalBytes/(this.minMs*1e6)}};l(XL,"robustStats");Bb=class{static{l(this,"BenchmarkRunner")}constructor(e,t={}){this.target=e,this.warmup=t.warmup??3,this.repeat=t.repeat??10,this.minRepeatMs=t.minRepeatMs??0,this.maxCv=t.maxCv??0,this.maxReMeasures=t.maxReMeasures??1,this.measurer=t.measurer||null,this._warn=t.warn||null,this._bufferCache=new Map}_record(e,t){this._warn&&this._warn(e,null,t)}_getOrAllocBuffers(e){let t=0,r=[];for(let[,i]of e.bufferMap){let a=Math.max(i.numel(),1);r.push(a);let u=i.sizeInBytes();u>0&&(t+=u)}let s=r.join(","),o=this._bufferCache.get(s);o||(o=r.map(i=>new Float32Array(i)),this._bufferCache.set(s,o));for(let i of o)for(let a=0;a<i.length;a++)i[a]=Math.random()*2-1;return{buffers:o,totalBytes:t}}run(e){if(!e||!e.body)return null;if(!this.target.isCPU())return this.measurer?this._runMeasured(e):null;let t=new bo(this.target),r;try{r=t.compile(e)}catch(p){return this._record("benchmark-compile",p),null}if(r.metadata.kind!=="js")return null;let s;try{s=new Function("return "+r.source)()}catch(p){return this._record("benchmark-construct-fn",p),null}let{buffers:o,totalBytes:i}=this._getOrAllocBuffers(e);for(let p=0;p<this.warmup;p++)try{s(...o)}catch(c){return this._record("benchmark-warmup-run",c),null}let a=[],u=null;for(let p=0;p<=this.maxReMeasures&&(this._collect(s,o,a),u=XL(a),!(this.maxCv<=0||u.cv<=this.maxCv));p++);return new Ob(u.median,u.min,a,i,u.trimmedMean,u.cv)}_runMeasured(e){if(e.shapeParams&&e.shapeParams.length>0||ga(e.body,1)>I4)return null;let t;try{t=new bo(this.target).compile(e)}catch(a){return this._record("measured-compile",a),null}let r=[],s=0;for(let[,a]of e.bufferMap){let u=Math.max(a.sizeInBytes(),1);r.push(u),s+=u}let o;try{o=this.measurer(t,r,[],{warmup:this.warmup,repeat:this.repeat})}catch(a){return this._record("measurer",a),null}if(!o||o.length===0)return null;let i=XL(o);return new Ob(i.median,i.min,o,s,i.trimmedMean,i.cv)}_collect(e,t,r){let s=0,o=this.repeat*3;for(let i=0;i<o&&(i<this.repeat||s<this.minRepeatMs);i++){let a=performance.now();e(...t);let u=performance.now()-a;r.push(u),s+=u}}}});var Rb,t2=M(()=>{"use strict";Rb=class{static{l(this,"Deadline")}constructor(e=1/0,t=null){this._clock=t||(()=>performance.now()),this._endAt=e===1/0||e==null?1/0:this._clock()+e}get expired(){return this._clock()>=this._endAt}remainingMs(){return this._endAt===1/0?1/0:Math.max(0,this._endAt-this._clock())}}});function n2(n){let e=null;for(let w of n.ops())if(w.opName==="conv"){if(e)return null;e=w}else if(w.opName!=="return")return null;if(!e||(e.getAttr("groups")||1)!==1)return null;let t=e.getAttr("input_layout"),r=e.getAttr("kernel_layout");if(t&&t!=="NCHW"||r&&r!=="OIHW")return null;let s=e.getOperand(0).type,o=e.getOperand(1).type,i=e.getResult(0).type;if(!s||!o||!i||s.shape.length!==4||o.shape.length!==4||i.shape.length!==4||s.dtype!=="f32"||o.dtype!=="f32"||i.dtype!=="f32")return null;let a=e.getAttr("strides")||[1,1],u=e.getAttr("padding")||[[0,0],[0,0]],p=e.getAttr("dilation")||[1,1],c=s.shape[0],f=s.shape[1],d=s.shape[2],h=s.shape[3],m=o.shape[0],g=o.shape[2],y=o.shape[3],_=i.shape[2],b=i.shape[3];return![c,f,d,h,m,g,y,_,b].every(w=>typeof w=="number"&&w>0)||o.shape[1]!==f||i.shape[1]!==m||i.shape[0]!==c?null:{N:c,Cin:f,H:d,W:h,O:m,Kh:g,Kw:y,Oh:_,Ow:b,sH:a[0],sW:a[1],pH:u[0][0],pW:u[1][0],dH:p[0],dW:p[1]}}function E4(n,e,t){let{weight:r,input:s,output:o}=n,{N:i,Cin:a,H:u,W:p,O:c,Kh:f,Kw:d,Oh:h,Ow:m,sH:g,sW:y,pH:_,pW:b,dH:w,dW:x}=e,{BM:S,BN:T,BK:v,TM:A,TN:N}=t,k=c,O=i*h*m,R=a*f*d,B=f*d,E=h*m,F=T/N,C=S/A,H=F*C,G=Math.ceil(R/v),U=S*v,D=v*T,V=Math.ceil(U/H),ue=Math.ceil(D/H),ce=new me("ig_As",[U],"f32","shared"),Le=new me("ig_Bs",[D],"f32","shared"),j=new me("ig_acc",[A*N],"f32","local"),W=new me("ig_af",[A],"f32","local"),ie=new me("ig_bf",[N],"f32","local"),je=Be("ig_bx"),ke=Be("ig_by"),rt=Be("ig_tx"),zt=Be("ig_ty"),on=Be("ig_tid"),Zt=Be("ig_brow"),it=Be("ig_bcol"),ye=Be("ig_k0"),Xe=l((Hr,Jw)=>Te(xe(Hr,$(N)),Jw),"accIdx"),Nn=Be("ig_im"),Qt=Be("ig_in"),an=kn(Nn,A,kn(Qt,N,new J(j,[Xe(Nn,Qt)],Vd()))),un=k%S!==0,mn=R%v!==0,cr=O%T!==0,Vn=U%H!==0,Or=D%H!==0,pr=Be("ig_la"),yt=Be("ig_aidx"),fr=Te(Zt,Dn(yt,$(v))),xt=Te(ye,hn(yt,$(v))),Vr=Dn(xt,$(B)),Ds=hn(xt,$(B)),uo=Dn(Ds,$(d)),Gr=hn(Ds,$(d)),dr=new q(r,[fr,Vr,uo,Gr]);if(un||mn){let Hr=un?kr(fr,$(k)):null;mn&&(Hr=Hr?zo(Hr,kr(xt,$(R))):kr(xt,$(R))),dr=new Se(Hr,dr,Vd())}let Br=new J(ce,[yt],dr);Vn&&(Br=new Se(kr(yt,$(U)),Br));let qo=Mb(pr,V,new et(yt,Te(on,xe(pr,$(H))),Br)),jr=Be("ig_lb"),Ur=Be("ig_bidx"),Ps=Te(ye,Dn(Ur,$(T))),ms=Te(it,hn(Ur,$(T))),va=Dn(Ps,$(B)),Oi=hn(Ps,$(B)),Bi=Dn(Oi,$(d)),ka=hn(Oi,$(d)),np=Dn(ms,$(E)),zu=hn(ms,$(E)),Na=Dn(zu,$(m)),Aa=hn(zu,$(m)),Wo=Ib(Te(xe(Na,$(g)),xe(Bi,$(w))),$(_)),Oa=Ib(Te(xe(Aa,$(y)),xe(ka,$(x))),$(b)),Ri=new q(s,[np,va,Wo,Oa]),Ko=zo(zo(Eb(Wo,$(0)),kr(Wo,$(u))),zo(Eb(Oa,$(0)),kr(Oa,$(p))));mn&&(Ko=zo(Ko,kr(Ps,$(R)))),cr&&(Ko=zo(Ko,kr(ms,$(O)))),Ri=new Se(Ko,Ri,Vd());let Mi=new J(Le,[Ur],Ri);Or&&(Mi=new Se(kr(Ur,$(D)),Mi));let Ba=Mb(jr,ue,new et(Ur,Te(on,xe(jr,$(H))),Mi)),Ot=Be("ig_kk"),lo=Be("ig_fi"),rp=Be("ig_fj"),Gw=kn(lo,A,new J(W,[lo],new q(ce,[Te(xe(Te(xe(zt,$(A)),lo),$(v)),Ot)]))),jw=kn(rp,N,new J(ie,[rp],new q(Le,[Te(xe(Ot,$(T)),Te(xe(rt,$(N)),rp))]))),Vu=Be("ig_mi"),Gu=Be("ig_ni"),Uw=kn(Vu,A,kn(Gu,N,new J(j,[Xe(Vu,Gu)],Te(new q(j,[Xe(Vu,Gu)]),xe(new q(W,[Vu]),new q(ie,[Gu])))))),qw=kn(Ot,v,new te([Gw,jw,Uw])),$s=Be("ig_kt"),gs=new et(ye,xe($s,$(v)),new te([qo,Ba,new Fn,qw,new Fn])),Rr=Mb($s,G,gs),hr=Be("ig_wm"),qr=Be("ig_wn"),Ii=Te(Te(Zt,xe(zt,$(A))),hr),co=Te(Te(it,xe(rt,$(N))),qr),Wr=Dn(co,$(E)),Kr=hn(co,$(E)),Ho=Dn(Kr,$(m)),Qn=hn(Kr,$(m)),Ei=new J(o,[Wr,Ii,Ho,Qn],new q(j,[Xe(hr,qr)]));if(un||cr){let Hr=un?kr(Ii,$(k)):null;cr&&(Hr=Hr?zo(Hr,kr(co,$(O))):kr(co,$(O))),Ei=new Se(Hr,Ei)}let Yo=kn(hr,A,kn(qr,N,Ei)),Ww=new te([an,Rr,Yo]),kh=new Ue(j,"local",new Ue(W,"local",new Ue(ie,"local",Ww))),Kw=new et(on,Te(xe(zt,$(F)),rt),new et(Zt,xe(ke,$(S)),new et(it,xe(je,$(T)),kh))),Hw=Math.ceil(O/T),Yw=Math.ceil(k/S),Nh=ya(ke,"blockIdx.y",Yw,ya(je,"blockIdx.x",Hw,ya(zt,"threadIdx.y",C,ya(rt,"threadIdx.x",F,Kw))));return new Ue(ce,"shared",new Ue(Le,"shared",Nh))}function r2(n,e,t){if(!s2(n,{M:e.O,N:e.N*e.Oh*e.Ow,K:e.Cin*e.Kh*e.Kw},t))return!1;let{BM:r,BN:s,BK:o,TM:i,TN:a}=t,u=e.O,p=e.N*e.Oh*e.Ow,c=e.Cin*e.Kh*e.Kw;if(u%r!==0||p%s!==0||c%o!==0||[r,s,o,i,a].some(d=>d%Lv!==0))return!1;let f=r/i*(s/a);return!(r*o%(Lv*f)!==0||o*s%f!==0)}function L4(n,e,t){let{weight:r,input:s,output:o}=n,{N:i,Cin:a,H:u,W:p,O:c,Kh:f,Kw:d,Oh:h,Ow:m,sH:g,sW:y,pH:_,pW:b,dH:w,dW:x}=e,{BM:S,BN:T,BK:v,TM:A,TN:N}=t,k=Lv,O=c,R=i*h*m,B=a*f*d,E=f*d,F=h*m,C=u*p,H=a*u*p,G=T/N,U=S/A,D=G*U,V=B/v,ue=S*v,ce=v*T,Le=ue/D,j=ce/D,W=Le/k,ie=v/k,je=(h-1)*g+(f-1)*w-_,ke=(m-1)*y+(d-1)*x-b,rt=_>0||je>=u,zt=b>0||ke>=p,on=new me(r.name,[O*B>0?O*B:1],r.dtype,r.scope),Zt=new me(s.name,[i*H>0?i*H:1],s.dtype,s.scope),it=new me("iv_As",[2*ue],"f32","shared");it.align16=!0;let ye=new me("iv_Bs",[2*ce],"f32","shared");ye.align16=!0;let Xe=new me("iv_acc",[A*N],"f32","local"),Nn=new me("iv_af",[A],"f32","local");Nn.align16=!0;let Qt=new me("iv_bf",[N],"f32","local");Qt.align16=!0;let an=new me("iv_ra",[Le],"f32","local");an.align16=!0;let un=new me("iv_rb",[j],"f32","local"),mn=Be("iv_bx"),cr=Be("iv_by"),Vn=Be("iv_tx"),Or=Be("iv_ty"),pr=Be("iv_tid"),yt=Be("iv_brow"),fr=Be("iv_bcol"),xt=l(($s,gs)=>Te(xe($s,$(N)),gs),"accIdx"),Vr=0,Ds=Be("iv_im"),uo=Be("iv_in"),Gr=kn(Ds,A,kn(uo,N,new J(Xe,[xt(Ds,uo)],Vd()))),dr=l($s=>{let gs=Vr++,Rr=Be("iv_la"+gs),hr=Be("iv_c"+gs),qr=Dn(hr,$(ie)),Ii=xe(hn(hr,$(ie)),$(k)),co=kn(Rr,W,new et(hr,Te(pr,xe(Rr,$(D))),new al(an,xe(Rr,$(k)),on,Te(xe(Te(yt,qr),$(B)),Te($s,Ii)),k))),Wr=Be("iv_lb"+gs),Kr=Be("iv_e"+gs),Ho=Dn(Kr,$(T)),Qn=hn(Kr,$(T)),Ei=Te(fr,Qn),Yo=Te($s,Ho),Ww=Dn(Yo,$(E)),kh=hn(Yo,$(E)),Kw=Dn(kh,$(d)),Hw=hn(kh,$(d)),Yw=Dn(Ei,$(F)),Nh=hn(Ei,$(F)),Hr=Dn(Nh,$(m)),Jw=hn(Nh,$(m)),Zw=Ib(Te(xe(Hr,$(g)),xe(Kw,$(w))),$(_)),Qw=Ib(Te(xe(Jw,$(y)),xe(Hw,$(x))),$(b)),QD=Te(Te(Te(xe(Yw,$(H)),xe(Ww,$(C))),xe(Zw,$(p))),Qw),Xw=new q(Zt,[QD]),ju=null;if(rt&&(ju=zo(Eb(Zw,$(0)),kr(Zw,$(u)))),zt){let hN=zo(Eb(Qw,$(0)),kr(Qw,$(p)));ju=ju?zo(ju,hN):hN}ju&&(Xw=new Se(ju,Xw,Vd()));let XD=kn(Wr,j,new et(Kr,Te(pr,xe(Wr,$(D))),new J(un,[Wr],Xw)));return new te([co,XD])},"prefetch"),Br=l(($s,gs)=>{let Rr=Vr++,hr=Be("iv_la"+Rr),qr=Be("iv_c"+Rr),Ii=Dn(qr,$(ie)),co=xe(hn(qr,$(ie)),$(k)),Wr=[];for(let Yo=0;Yo<k;Yo++)Wr.push(new J(it,[Te($s,Te(xe(Te(co,$(Yo)),$(S)),Ii))],new q(an,[Te(xe(hr,$(k)),$(Yo))])));let Kr=kn(hr,W,new et(qr,Te(pr,xe(hr,$(D))),new te(Wr))),Ho=Be("iv_lb"+Rr),Qn=Be("iv_e"+Rr),Ei=kn(Ho,j,new et(Qn,Te(pr,xe(Ho,$(D))),new J(ye,[Te(gs,Qn)],new q(un,[Ho]))));return new te([Kr,Ei])},"commit"),qo=l(($s,gs)=>{let Rr=Vr++,hr=Be("iv_kk"+Rr),qr=[],Ii=Te($s,Te(xe(hr,$(S)),xe(Or,$(A))));for(let Qn=0;Qn<A/k;Qn++)qr.push(new al(Nn,$(Qn*k),it,Te(Ii,$(Qn*k)),k));let co=Te(gs,Te(xe(hr,$(T)),xe(Vn,$(N))));for(let Qn=0;Qn<N/k;Qn++)qr.push(new al(Qt,$(Qn*k),ye,Te(co,$(Qn*k)),k));let Wr=Be("iv_mi"+Rr),Kr=Be("iv_ni"+Rr),Ho=kn(Wr,A,kn(Kr,N,new J(Xe,[xt(Wr,Kr)],Te(new q(Xe,[xt(Wr,Kr)]),xe(new q(Nn,[Wr]),new q(Qt,[Kr]))))));return kn(hr,v,new te([...qr,Ho]))},"computeMMA"),jr=Be("iv_kt"),Ur=Be("iv_p"),Ps=Be("iv_pN"),ms=xe(Ur,$(ue)),va=xe(Ur,$(ce)),Oi=xe(Ps,$(ue)),Bi=xe(Ps,$(ce)),ka=kr(jr,$(V-1)),np=new te([dr($(0)),Br($(0),$(0)),new Fn]),zu=new et(Ur,hn(jr,$(2)),new et(Ps,hn(Te(jr,$(1)),$(2)),new te([new Se(ka,dr(xe(Te(jr,$(1)),$(v)))),qo(ms,va),new Se(ka,new te([Br(Oi,Bi),new Fn]))]))),Na=Mb(jr,V,zu),Aa=Be("iv_wm"),Wo=Be("iv_wn"),Oa=Te(Te(yt,xe(Or,$(A))),Aa),Ri=Te(Te(fr,xe(Vn,$(N))),Wo),Ko=Dn(Ri,$(F)),Mi=hn(Ri,$(F)),Ba=Dn(Mi,$(m)),Ot=hn(Mi,$(m)),lo=new J(o,[Ko,Oa,Ba,Ot],new q(Xe,[xt(Aa,Wo)])),rp=kn(Aa,A,kn(Wo,N,lo)),Gw=new te([Gr,np,Na,rp]),jw=new Ue(Xe,"local",new Ue(Nn,"local",new Ue(Qt,"local",new Ue(an,"local",new Ue(un,"local",Gw))))),Vu=new et(pr,Te(xe(Or,$(G)),Vn),new et(yt,xe(cr,$(S)),new et(fr,xe(mn,$(T)),jw))),Gu=R/T,Uw=O/S,qw=ya(cr,"blockIdx.y",Uw,ya(mn,"blockIdx.x",Gu,ya(Or,"threadIdx.y",U,ya(Vn,"threadIdx.x",G,Vu))));return new Ue(it,"shared",new Ue(ye,"shared",qw))}function s2(n,e,t){let{BM:r,BN:s,BK:o,TM:i,TN:a}=t;if(![r,s,o,i,a].every(m=>typeof m=="number"&&m>0)||r%i!==0||s%a!==0)return!1;let u=s/a,p=r/i,c=u*p,f=n.warpSize||32;return!(c%f!==0||c>(n.maxThreadsPerBlock||1024)||(r*o+o*s)*4*2>(n.sharedMemoryBytes||49152)||i*a+i+a+f>(n.registersPerThread||255))}function D4(n,e){for(let t of C4)if(r2(n,e,t))return t;return null}function o2(n,e,t){let r=n.func,s=r.convInfo;if(!s)return!1;let i=Fo(r.body).find(_=>/^conv_acc_/.test(_));if(!i)return!1;let a=Md(r.body,i);if(!a||a.reads.length<2||a.writes.length<1)return!1;let u=a.reads[0].buffer,p=a.reads[1].buffer,c=a.writes[0].buffer;if(!u||!p||!c)return!1;let f=s.O,d=s.N*s.Oh*s.Ow,h=s.Cin*s.Kh*s.Kw;if(h<128||d<64)return!1;let m=t&&t.convConfig,g=t&&t.convNoVec,y=null;if(!g){let _=m?r2(e,s,m)?m:null:D4(e,s);_&&(y=L4({weight:p,input:u,output:c},s,_))}if(!y){let _=m?s2(e,{M:f,N:d,K:h},m)?m:null:xb(e,{M:f,N:d,K:h});if(!_)return!1;y=E4({weight:p,input:u,output:c},s,_)}return n.func.body=y,n.func._setChild&&n.func._setChild("body",y),n.func.gpuRegisterBlocked=!0,!0}var $,Vd,Be,Te,Ib,xe,Dn,hn,kr,Eb,zo,Mb,kn,ya,Lv,C4,Cv=M(()=>{"use strict";Fe();ei();Rc();Sb();$=l(n=>new L(n),"I"),Vd=l(()=>new de(0),"FZERO"),Be=l(n=>new Oe(n,"i32"),"IV"),Te=l((n,e)=>new P("+",n,e),"ADD"),Ib=l((n,e)=>new P("-",n,e),"SUB"),xe=l((n,e)=>new P("*",n,e),"MUL"),Dn=l((n,e)=>new P("//",n,e),"DIV"),hn=l((n,e)=>new P("%",n,e),"MOD"),kr=l((n,e)=>new Ke("lt",n,e),"LT"),Eb=l((n,e)=>new Ke("ge",n,e),"GE"),zo=l((n,e)=>new P("&&",n,e),"AND"),Mb=l((n,e,t)=>new he(n,$(0),$(e),z.SERIAL,t),"forS"),kn=l((n,e,t)=>new he(n,$(0),$(e),z.UNROLLED,t),"forU"),ya=l((n,e,t,r)=>new he(n,$(0),$(t),z.THREAD_BINDING,r,e),"forT");l(n2,"detectPureConv");l(E4,"buildImplicitGemmConv");Lv=4;l(r2,"vectorizableConvConfig");l(L4,"buildVectorizedImplicitGemmConv");l(s2,"validConvConfig");C4=[{BM:128,BN:64,BK:8,TM:8,TN:8},{BM:64,BN:64,BK:8,TM:8,TN:8},{BM:64,BN:64,BK:8,TM:4,TN:8},{BM:64,BN:32,BK:8,TM:8,TN:8},{BM:32,BN:64,BK:8,TM:4,TN:8}];l(D4,"pickVectorizedConvConfig");l(o2,"applyImplicitGemmConv")});function i2(n,e,t,r,s,o,i,a){let u=new me(n,[t,r],e.dtype,"shared"),p=new J(u,[s,o],new q(e,[i,a]));return{tile:u,fill:p}}function F4(n,e,t){let{A:r,B:s,C:o,M:i,N:a,K:u,transB:p}=n,c=u/t,f=Ic("ts_by"),d=Ic("ts_bx"),h=Ic("ts_ty"),m=Ic("ts_tx"),g=Ic("ts_ko"),y=Ic("ts_ki"),_=new me("ts_acc",[1],"f32","local"),b=Ec(Lb(f,ds(e)),h),w=Ec(Lb(d,ds(e)),m),x=Lb(g,ds(t)),S=i2("ts_As",r,e,t,h,m,b,Ec(x,m)),T=i2("ts_Bs",s,t,e,h,m,p?w:Ec(x,h),p?Ec(x,h):w),v=$4(y,t,new J(_,[ds(0)],Ec(new q(_,[ds(0)]),Lb(new q(S.tile,[h,y]),new q(T.tile,[y,m]))))),A=new te([S.fill,T.fill,new Fn,v,new Fn]),N=P4(g,c,A),k=new J(_,[ds(0)],new de(0)),O=new J(o,[b,w],new q(_,[ds(0)])),R=new Ue(_,"local",new te([k,N,O]));return Cb(f,"blockIdx.y",i/e,Cb(d,"blockIdx.x",a/e,new Ue(S.tile,"shared",new Ue(T.tile,"shared",Cb(h,"threadIdx.y",e,Cb(m,"threadIdx.x",e,R))))))}function z4(n,e){let{M:t,N:r,K:s}=e,o=16,i=16;return o*o>(n.maxThreadsPerBlock||1024)||(o*i+i*o)*4>(n.sharedMemoryBytes||49152)||t%o!==0||r%o!==0||s%i!==0||t<o||r<o||s<i?null:{BS:o,BK:i}}function V4(n,e){let r=Fo(n.func.body).filter(o=>/^q?conv_(init|acc)_/.test(o));if(r.length===0)return!1;let s=new to(e);for(let o of r)s.applyToBlock(n,o);return!0}function G4(n,e,t={}){if(!e.isGPU())return!1;let r=WL(n.func);if(!r)return!1;let s=r.dims;if(t&&t.primitiveMatmul&&(s.batch||1)===1&&!r.epilogue){let a=z4(e,s);if(a){let u=F4(s,a.BS,a.BK);return n.func.body=u,n.func._setChild&&n.func._setChild("body",u),n.func.gpuRegisterBlocked=!0,!0}}let o=xb(e,s);if(!o)return!1;let i=Av(s,o,r.epilogue);return n.func.body=i,n.func._setChild&&n.func._setChild("body",i),n.func.gpuRegisterBlocked=!0,!0}function Db(n,e,t={}){if(!e.isGPU()||e.isWebGPU&&e.isWebGPU())return!1;let r=G4(n,e,t);return r||(r=o2(n,e,t)),r||(r=V4(n,e)),r}var ds,Ic,Ec,Lb,P4,$4,Cb,Dv=M(()=>{"use strict";Sb();Rc();Cv();Do();Fe();ei();ds=l(n=>new L(n),"I"),Ic=l(n=>new Oe(n,"i32"),"IV"),Ec=l((n,e)=>new P("+",n,e),"ADD"),Lb=l((n,e)=>new P("*",n,e),"MUL"),P4=l((n,e,t)=>new he(n,ds(0),ds(e),z.SERIAL,t),"forS"),$4=l((n,e,t)=>new he(n,ds(0),ds(e),z.UNROLLED,t),"forU"),Cb=l((n,e,t,r)=>new he(n,ds(0),ds(t),z.THREAD_BINDING,r,e),"forT");l(i2,"stageTileToShared");l(F4,"buildTiledSharedMatmul");l(z4,"pickTiledConfig");l(V4,"applyDeterministicGpuConv");l(G4,"applyDeterministicGpuMatmul");l(Db,"applyDeterministicGpuSchedule")});function Pv(n){return n*1664525+1013904223&2147483647}function U4(n,e,t){let r={...n};for(let s of e.variables)t.rngFloat()<t.mutationRate&&(r[s.name]=s.sample(o=>t.rng(o)));return r}function a2(n={}){return n.strategy==="random"?new $v({numTrials:n.numTrials,seed:n.seed,deadline:n.deadline}):new Fv({populationSize:n.populationSize,numGenerations:n.numGenerations,mutationRate:n.mutationRate,eliteRatio:n.eliteRatio,seed:n.seed,deadline:n.deadline})}var Pb,$v,Fv,j4,u2=M(()=>{"use strict";l(Pv,"nextLcg");Pb=class{static{l(this,"SearchCandidate")}constructor(e,t,r){this.sketchName=e,this.params=t,this.score=r}},$v=class{static{l(this,"RandomSearch")}constructor(e={}){this.numTrials=e.numTrials||64,this.seed=e.seed||42,this.deadline=e.deadline||null,this._rngState=this.seed}_rng(e){return this._rngState=Pv(this._rngState),this._rngState%e}_expired(){return this.deadline?this.deadline.expired:!1}search(e,t){let r=[];for(let s of e){if(this._expired())break;for(let o=0;o<this.numTrials&&!this._expired();o++){let i=s.sampleParams(u=>this._rng(u)),a=t(s,i);a&&r.push(new Pb(s.name,i,a.score))}}return r.sort((s,o)=>o.score-s.score),{candidates:r,population:null}}},Fv=class{static{l(this,"EvolutionarySearch")}constructor(e={}){this.populationSize=e.populationSize||32,this.numGenerations=e.numGenerations||10,this.mutationRate=e.mutationRate||.3,this.eliteRatio=e.eliteRatio||.2,this.seed=e.seed||42,this.deadline=e.deadline||null,this._rngState=this.seed}_rng(e){return this._rngState=Pv(this._rngState),this._rngState%e}_rngFloat(){return this._rngState=Pv(this._rngState),this._rngState/2147483647}_expired(){return this.deadline?this.deadline.expired:!1}search(e,t,r=null){let s=r&&r.length?r:this._initPopulation(e),o=new Map,i=l((u,p)=>{let c=u.name+"|"+JSON.stringify(p);if(o.has(c))return o.get(c);let f=t(u,p);return o.set(c,f),f},"evalMemo");for(let u=0;u<this.numGenerations&&!this._expired();u++){let p=[];for(let h of s){let m=i(h.sketch,h.params);m&&p.push({...h,score:m.score})}if(p.sort((h,m)=>m.score-h.score),p.length===0)break;let c=Math.max(1,Math.floor(p.length*this.eliteRatio)),f=p.slice(0,c),d=f.map(h=>({sketch:h.sketch,params:{...h.params}}));for(;d.length<this.populationSize;){let h=f[this._rng(f.length)],m=f[this._rng(f.length)];if(h.sketch!==m.sketch){let g=this._rngFloat()<.5?h:m;d.push(this._mutate(g.sketch,{...g.params}))}else d.push(this._mutate(h.sketch,this._crossover(h,m)))}s=d}let a=[];for(let u of s){let p=i(u.sketch,u.params);p&&a.push(new Pb(u.sketch.name,u.params,p.score))}return a.sort((u,p)=>p.score-u.score),{candidates:a,population:s}}_initPopulation(e){let t=[];for(let r=0;r<this.populationSize;r++){let s=e[this._rng(e.length)],o=s.sampleParams(i=>this._rng(i));t.push({sketch:s,params:o})}return t}_crossover(e,t){if(e.sketch!==t.sketch)return{...e.params};let r={};for(let s of e.sketch.variables)r[s.name]=this._rngFloat()<.5?e.params[s.name]:t.params[s.name];return r}_mutate(e,t){let r={rngFloat:l(()=>this._rngFloat(),"rngFloat"),rng:l(i=>this._rng(i),"rng"),mutationRate:this.mutationRate},s=[U4,...j4],o=t;for(let i of s)o=i(o,e,r);return{sketch:e,params:o}}},j4=[];l(U4,"defaultResampleMutator");l(a2,"createSearchStrategy")});function Fb(n){let e={"threadIdx.x":1,"threadIdx.y":1,"threadIdx.z":1},t=new Set,r=l(s=>{if(!(!s||typeof s!="object"||t.has(s))){if(t.add(s),s.type==="ForNode"&&e[s.threadTag]!==void 0){let o=s.extent&&s.extent.type==="IntImmNode"?s.extent.value:1;o>e[s.threadTag]&&(e[s.threadTag]=o)}for(let o in s){let i=s[o];Array.isArray(i)?i.forEach(r):i&&typeof i=="object"&&r(i)}}},"visit");return r(n.body),q4.reduce((s,o)=>s*e[o],1)}var q4,$b,l2=M(()=>{"use strict";du();Px();hv();Ed();u2();yv();q4=["threadIdx.x","threadIdx.y","threadIdx.z"];l(Fb,"gpuThreadBlockSize");$b=class{static{l(this,"BlockTuningSession")}constructor(e){this.target=e.target,this.primFunc=e.primFunc,this.blockName=e.blockName,this.sketches=e.sketches,this.benchmarkRunner=e.benchmarkRunner||null,this.config=e.config,this.deadline=e.deadline||null,this._warn=e.warn||(()=>{}),this._warnedEvalSketches=new Set;let t=this.sketches.some(o=>o.name==="fused");t?(this.learnedModel=new Oc,this.costModel=new Bc(e.costModel.analytical,this.learnedModel)):(this.costModel=e.costModel,this.learnedModel=e.learnedModel);let r=t?null:zL(e.primFunc,e.blockName,e.blockMap);this.evalFunc=r||e.primFunc,this.evalBlockName=e.blockName,this.sketchByName=new Map;for(let o of this.sketches)this.sketchByName.set(o.name,o);let s=this.sketches.length===1&&typeof this.sketches[0].enumerate=="function";this.enumSketch=s?this.sketches[0]:null,this.enumParams=s?this.sketches[0].enumerate():null,this.strategy=a2({...this.config,deadline:this.deadline}),this.population=null,this._best=null,this.plateaued=!1}runRound(){let e=this._best?this._best.measuredScore:-1/0,t=this._produceCandidates();if(t.length===0)return this.plateaued=!0,0;if(this.benchmarkRunner)this._measureAndLearn(t);else{let s=t[0];this._consider({sketchName:s.sketchName,params:s.params,score:s.score,measuredScore:s.score}),this.plateaued=!0}let r=this._best?this._best.measuredScore:-1/0;return Math.max(0,r-e)}best(){return this._best?{sketchName:this._best.sketchName,params:this._best.params,score:this._best.score,medianMs:this._best.medianMs??null,minMs:this._best.minMs??null}:null}bestTrace(){if(!this._best)return null;let e=this.sketchByName.get(this._best.sketchName);if(!e)return null;try{let t=new Cn(Ti(this.primFunc));return e.instantiate(this._best.params)(t,this.blockName,this.target),t.trace.serialize()}catch(t){return this._warn("best-trace",this.blockName,t),null}}_produceCandidates(){if(this.enumSketch){let s=[];for(let o of this.enumParams){let i=this._evaluate(this.enumSketch,o);i&&s.push({sketchName:this.enumSketch.name,params:o,score:i.score})}return s.sort((o,i)=>i.score-o.score),s}let e=l((s,o)=>this._evaluate(s,o),"evaluator"),{candidates:t,population:r}=this.strategy.search(this.sketches,e,this.population);return this.population=r,t}_evaluate(e,t){try{let r=Ti(this.evalFunc),s=new Cn(r);if(e.instantiate(t)(s,this.evalBlockName,this.target),Jl.validate(r).length>0)return null;let i=this.target.maxThreadsPerBlock;return this.target.isGPU&&this.target.isGPU()&&i&&Fb(r)>i?null:{score:this.costModel.score(r)}}catch(r){return this._warnedEvalSketches.has(e.name)||(this._warnedEvalSketches.add(e.name),this._warn("evaluate-candidate",this.blockName,r)),null}}_measureAndLearn(e){let t=e.slice(0,this.config.topKForBenchmark);for(let r of t){if(this.deadline&&this.deadline.expired)break;let s=this._measure(r);if(!s)continue;let o=-s.result.medianMs;this.learnedModel.addSample(s.features,o),this._consider({sketchName:r.sketchName,params:r.params,score:o,measuredScore:o,medianMs:s.result.medianMs,minMs:s.result.minMs})}this.learnedModel.train()}_measure(e){let t=this.sketchByName.get(e.sketchName);if(!t)return null;let r,s;try{r=Ti(this.primFunc),t.instantiate(e.params)(new Cn(r),this.blockName,this.target),s=Ti(this.evalFunc),t.instantiate(e.params)(new Cn(s),this.evalBlockName,this.target)}catch(i){return this._warn("measure-candidate",this.blockName,i),null}let o=this.benchmarkRunner.run(r);return o?{result:o,features:Mu.extractStatements(s)}:null}_consider(e){(!this._best||e.measuredScore>this._best.measuredScore)&&(this._best=e)}}});var zv,zb,c2=M(()=>{"use strict";zv=class{static{l(this,"GradientSchedulerPolicy")}pick(e){let t=e.filter(i=>!i.plateaued);if(t.length===0)return null;let r=t.filter(i=>i.rounds===0);if(r.length>0)return r[0];let s=null,o=-1/0;for(let i of t){let a=i.gainEwma!==void 0?i.gainEwma:i.lastGain,u=i.weight*a;u>o&&(o=u,s=i)}return s}},zb=class{static{l(this,"TaskScheduler")}constructor(e=null){this.policy=e||new zv}run(e,t,r={}){let s=r.maxRoundsPerTask??8,o=r.plateauPatience??2,i=r.gainEwmaAlpha??.5;for(let a of e)a.rounds=0,a.lastGain=0,a.gainEwma=0,a.stale=0,a.plateaued=!1;for(;!(t&&t.expired);){let a=this.policy.pick(e);if(!a)break;let u=a.session.runRound();a.rounds++,a.lastGain=u,a.gainEwma=i*u+(1-i)*a.gainEwma,u<=0?a.stale++:a.stale=0,(a.session.plateaued||a.stale>=o||a.rounds>=s)&&(a.plateaued=!0)}}}});function W4(n){if(n.isCPU())return null;let e=F1(n.kind);if(!e)throw new Error("hardwareMeasure requested for target '"+n.kind+"' but no measurer is registered for it; the corresponding runtime must be loaded (Node: import '#io/cuda_runtime') before compiling");return e}var Vb,Gb,p2=M(()=>{"use strict";du();yv();JL();QL();e2();t2();Ev();Rc();Bv();Do();Dv();l2();Ed();Fe();c2();Ym();l(W4,"resolveMeasurer");Vb=class{static{l(this,"AutotuneConfig")}constructor(e={}){this.strategy=e.strategy||"evolutionary",this.numTrials=e.numTrials||64,this.populationSize=e.populationSize||32,this.numGenerations=e.numGenerations||10,this.mutationRate=e.mutationRate,this.eliteRatio=e.eliteRatio,this.seed=e.seed||42,this.timeBudgetMs=e.timeBudgetMs||3e4,this.clock=e.clock||null,this.tuningDB=e.tuningDB||null,this.useTuningDB=e.useTuningDB!==!1,this.measurer=e.measurer||null,this.hardwareMeasure=e.hardwareMeasure??!1,this.enableBenchmark=e.enableBenchmark??(this.hardwareMeasure||!!e.measurer),this.benchmarkWarmup=e.benchmarkWarmup??3,this.benchmarkRepeat=e.benchmarkRepeat??10,this.benchmarkMaxCv=e.benchmarkMaxCv??0,this.topKForBenchmark=e.topKForBenchmark??5,this.maxRoundsPerTask=e.maxRoundsPerTask??8,this.plateauPatience=e.plateauPatience??2,this.schedulerPolicy=e.schedulerPolicy||null,this.onWarning=e.onWarning||null}},Gb=class{static{l(this,"Autotuner")}constructor(e,t={},r=null){this.target=e,this.config=t instanceof Vb?t:new Vb(t),this.trace=r,this._funcName=null,this.config.hardwareMeasure&&(this.config.measurer=W4(e)),this.analyticalModel=new _b(e),this.learnedModel=new Oc,this.costModel=new Bc(this.analyticalModel,this.learnedModel),this.db=this.config.tuningDB instanceof zd?this.config.tuningDB:new zd;let s=l((o,i,a)=>this._warn(o,i,a),"warn");this.benchmarkRunner=this.config.enableBenchmark?new Bb(e,{warmup:this.config.benchmarkWarmup,repeat:this.config.benchmarkRepeat,maxCv:this.config.benchmarkMaxCv,measurer:this.config.measurer,warn:s}):null,this.scheduler=new zb(this.config.schedulerPolicy)}_warn(e,t,r){let s=r&&r.message?r.message:String(r);if(this.config.onWarning)try{this.config.onWarning({stage:e,func:this._funcName,block:t||null,message:s,error:r})}catch(o){}this.trace&&this.trace.warn("autotune",this._funcName,`${e}${t?" ["+t+"]":""}: ${s}`)}tune(e,t=null){this._funcName=e.name;let r=t?[t]:Fo(e.body),s=Nb(e.body),o=$d(e),i=new Rb(this.config.timeBudgetMs,this.config.clock),a=new Map,u=new Map;for(let f of r){let d=Ab(e,f,this.target,s);u.set(f,d);let h=a.get(d);if(h){h.weight++;continue}if(this.config.useTuningDB&&this.db.has(d)){a.set(d,{key:d,kind:"cache",cached:this.db.lookup(d),weight:1});continue}let m=Iv(e,f,this.target,s,{richGpu:this.config.richGpu??!!this.config.measurer,dag:o});if(m.length===0){a.set(d,{key:d,kind:"empty",weight:1});continue}let g=new $b({target:this.target,primFunc:e,blockName:f,blockMap:s,sketches:m,costModel:this.costModel,learnedModel:this.learnedModel,benchmarkRunner:this.benchmarkRunner,config:this.config,deadline:i,warn:l((y,_,b)=>this._warn(y,_,b),"warn")});a.set(d,{key:d,kind:"session",session:g,weight:1})}let p=[...a.values()].filter(f=>f.kind==="session");p.length>0&&this.scheduler.run(p,i,this.config);let c=new Map;for(let f of r){let d=a.get(u.get(f));if(d.kind==="cache"){c.set(f,{sketchName:d.cached.sketchName,params:d.cached.params,score:d.cached.score,fromCache:!0});continue}if(d.kind==="empty")continue;let h=d.session.best();if(h){if(this.config.useTuningDB&&!d.stored){let m=new Fd(d.key,h.sketchName,h.params,h.score,d.session.bestTrace(),this.db.version);m.medianMs=h.medianMs||null,m.minMs=h.minMs||null,this.db.store(d.key,m),d.stored=!0}c.set(f,{sketchName:h.sketchName,params:h.params,score:h.score,fromCache:!1,medianMs:h.medianMs,minMs:h.minMs})}}return c}tuneAndApply(e,t=null){let r=this.tune(e,t);if(r.size>0){let s=this._applyBestSchedule(e,r);if(s)return{func:s.func,results:r,applied:!0}}return{func:e,results:r,applied:!1}}_applyBestSchedule(e,t){let r=this._buildDefaultSchedule(e),s=!!r&&this._scheduleIsValid(r),o=s&&this._isStrongBackendSchedule(r),i=this._buildTunedSchedule(e,t),a=!!i&&this._scheduleIsValid(i),u=a&&this._isStrongBackendSchedule(i),p=!o||u&&this.config.measurer!=null;return a&&p?(this._adoptSchedule(e,i),{func:e}):(i&&!a?this._warn("tuned-schedule-invalid",null,new Error("tuned schedule exceeds target thread-block limit; falling back to default")):a&&o&&this._warn("baseline-preferred",null,new Error("cost-model-only tuning cannot displace the deterministic GPU schedule without hardware measurement; keeping the deterministic kernel")),s?(this._adoptSchedule(e,r),{func:e}):a?(this._adoptSchedule(e,i),{func:e}):(this._warn("no-valid-schedule",null,new Error("neither tuned nor default schedule is valid; leaving function unscheduled")),null))}_isStrongBackendSchedule(e){return!!e&&e.gpuRegisterBlocked===!0}_buildTunedSchedule(e,t){try{let r=Ti(e),s=new Cn(r),o=Nb(r.body),i=$d(r),a=new Set,u=[];for(let c of t)if(c[1].sketchName==="fused"){let f=vb(r,i,c[0],Tt);f&&a.add(f),u.unshift(c)}else u.push(c);let p=new Set;for(let[c,f]of u)if(!(a.has(c)||p.has(f))&&(p.add(f),!(!f.sketchName||!f.params)))try{let h=Iv(r,c,this.target,o,{richGpu:this.config.richGpu??!!this.config.measurer,dag:i}).find(m=>m.name===f.sketchName);h&&this._fitsThreadBlock(r,c,h,f.params)&&h.instantiate(f.params)(s,c,this.target)}catch(d){this._warn("apply-tuned-block",c,d);continue}return this._scheduleResidualBlocks(s,a),r}catch(r){return this._warn("build-tuned-schedule",null,r),null}}_scheduleResidualBlocks(e,t){let r=null;for(let s of Fo(e.func.body))if(!(t.has(s)||this._blockIsParallelized(e,s))){r||(r=new to(this.target));try{r.applyToBlock(e,s)}catch(o){this._warn("residual-block",s,o)}}}_blockIsParallelized(e,t){let r;try{r=e.getLoops(t)}catch(s){return this._warn("block-loops",t,s),!0}for(let s of r)if(s.kind===z.THREAD_BINDING||s.kind===z.PARALLEL||s.kind===z.VECTORIZED)return!0;return!1}_scheduleIsValid(e){return!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock?!0:Fb(e)<=this.target.maxThreadsPerBlock}_buildDefaultSchedule(e){try{let t=Ti(e),r=new Cn(t);return Db(r,this.target,this.config)||new to(this.target).applyToAllBlocks(r),t}catch(t){return this._warn("build-default-schedule",null,t),null}}_adoptSchedule(e,t){Object.assign(e,t),e._setChild("body",e.body)}_fitsThreadBlock(e,t,r,s){if(!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock)return!0;try{let o=Ti(e);return r.instantiate(s)(new Cn(o),t,this.target),Fb(o)<=this.target.maxThreadsPerBlock}catch(o){return this._warn("fits-thread-block",t,o),!1}}}});var jb,f2=M(()=>{"use strict";ma();du();Do();p2();Dv();jb=class extends ar{static{l(this,"SchedulePass")}constructor(e){super("SchedulePass","scheduling"),this.config=e,this.target=e.target,this.snapshotPoint="afterScheduling",this._autotuner=null,this._policy=null}begin(e){let t=this.config.scheduling;t.autotune?this._autotuner=new Gb(this.target,t,e.trace):t.enabled&&(this._policy=new to(this.target,null,e.trace))}run(e,t){let r=this.config.scheduling,s=t.trace;if(r.autotune){if(e.cublasInfo||e._tensorIntrin)return;let o=performance.now(),i=this._autotuner.tuneAndApply(e),a=performance.now()-o,u=0,p=0;if(i&&i.results){p=i.results.size;for(let[c,f]of i.results)f.fromCache&&u++,s.explainsEnabled&&s.explain("schedule",c,f.sketchName,`autotuned: best of search${f.fromCache?" (cached)":""}, score ${f.score!=null?f.score.toFixed(3):"n/a"}`,{target:this.target.name,params:f.params})}s.autotuneStats(e.name,{durationMs:a,blockCount:p,applied:!!(i&&i.applied),cacheHits:u})}else if(r.enabled||r.gpuTiling){if(e.cublasInfo||e._tensorIntrin)return;let o=performance.now(),i=new Cn(e);!Db(i,this.target,r)&&r.enabled&&this._policy.applyToAllBlocks(i),s.functionEvent("scheduling",e.name,{durationMs:performance.now()-o})}}}});function d2(n){let e={analyzer:new Xs,simp:null};e.simp=new vy(e.analyzer);let t=hs(n.body,e);return n.body=t,n._setChild("body",t),n}function K4(n,e,t){let r=n.analyzer.getVarBound(e);return t&&t.type==="IntImmNode"&&t.value>0?n.analyzer.bind(e,0,t.value-1):n.analyzer.setVarBound(e,null),r}function hs(n,e){if(!n||typeof n!="object")return n;switch(n.type){case"ForNode":{let t=K4(e,n.loopVar.name,n.extent),r=hs(n.body,e);e.analyzer.setVarBound(n.loopVar.name,t);let s=new he(n.loopVar,n.min,n.extent,n.kind,r,n.threadTag);return n.annotations&&(s.annotations=n.annotations),s}case"BlockNode":{let t=[];for(let i of n.iterVars)i.iterVar&&(t.push([i.iterVar.name,e.analyzer.getVarBound(i.iterVar.name)]),e.analyzer.setVarBound(i.iterVar.name,i.binding?cu(e.analyzer,i.binding):null));let r=n.iterVars.map(H4(e)),s=hs(n.body,e),o=n.initBody?hs(n.initBody,e):null;for(let[i,a]of t)e.analyzer.setVarBound(i,a);return new fe(n.name,r,n.reads,n.writes,s,o)}case"SeqNode":return new te(n.stmts.map(t=>hs(t,e)));case"IfThenElseNode":{let t=ur(n.condition,e);return _i(e.analyzer,t)?hs(n.thenBody,e):lu(e.analyzer,t)?n.elseBody?hs(n.elseBody,e):new te([]):new Se(t,hs(n.thenBody,e),n.elseBody?hs(n.elseBody,e):null)}case"BufferStoreNode":return new J(n.buffer,n.indices.map(t=>ur(t,e)),ur(n.value,e));case"LetStmtNode":return new et(n.variable,ur(n.value,e),hs(n.body,e));case"AllocateNode":return new Ue(n.buffer,n.scope,hs(n.body,e));case"WhileNode":return new $i(n.condVar,hs(n.condBody,e),hs(n.loopBody,e));case"EvaluateNode":return new il(ur(n.value,e));default:return n}}function H4(n){return e=>{if(!e.iterVar||!e.binding)return e;let t=ur(e.binding,n);return new Pa(e.iterVar,t,e.kind)}}function ur(n,e){if(!n||typeof n!="object"||!n.type)return n;switch(n.type){case"IntImmNode":case"FloatImmNode":case"VariableNode":return n;case"BufferLoadNode":return new q(n.buffer,n.indices.map(t=>ur(t,e)));case"MathOpNode":{let t=ur(n.a,e),r=n.b?ur(n.b,e):null;return e.simp.simplify(new P(n.op,t,r))}case"CompareNode":{let t=ur(n.a,e),r=ur(n.b,e);return e.simp.simplify(new Ke(n.direction,t,r))}case"CastNode":return new ut(ur(n.expr,e),n.fromDtype,n.toDtype);case"CallExternNode":return new Ne(n.externName,n.args.map(t=>ur(t,e)),n.dtype);case"IfThenElseNode":{let t=ur(n.condition,e),r=ur(n.thenBody,e),s=n.elseBody?ur(n.elseBody,e):null;return _i(e.analyzer,t)?r:s!==null&&lu(e.analyzer,t)?s:new Se(t,r,s)}default:return n}}var h2=M(()=>{"use strict";Yl();Tf();Fe();l(d2,"simplifyPrimFunc");l(K4,"bindLoopVar");l(hs,"simplifyStmt");l(H4,"simplifyIterVar");l(ur,"simplifyExpr")});var Ub,m2=M(()=>{"use strict";ma();h2();Ub=class extends ar{static{l(this,"SimplifyPass")}constructor(){super("SimplifyPass","simplify")}run(e,t){let r=performance.now();return d2(e),t.trace.functionEvent("simplify",e.name,{durationMs:performance.now()-r}),e}}});function g2(n){return!!n&&typeof n=="object"&&typeof n.name=="string"&&n.dtype!==void 0&&n.shape!==void 0&&n.type===void 0}var Vv,Gv,Y4,qb,y2=M(()=>{"use strict";Vv=class{static{l(this,"BufferInterval")}constructor(e,t,r,s){this.buffer=e,this.firstUse=t,this.lastUse=r,this.scope=s}get size(){return this.buffer.sizeInBytes()}overlaps(e){return this.firstUse<=e.lastUse&&e.firstUse<=this.lastUse}},Gv=class{static{l(this,"BufferLivenessResult")}constructor(e,t,r){this.intervals=e,this.stmtOrder=t,this.paramBuffers=r}isParam(e){return this.paramBuffers.has(e)}getTemporaries(){let e=[];for(let[t,r]of this.intervals)this.paramBuffers.has(t)||e.push(r);return e}interfere(e,t){let r=this.intervals.get(e),s=this.intervals.get(t);return!r||!s?!1:r.overlaps(s)}},Y4=new Set(["_parent","_parentKey","_parentIdx"]);l(g2,"isBuffer");qb=class{static{l(this,"BufferLiveness")}static analyze(e){let t=new Map,r=[],s=0,o=new Set;for(let[,f]of e.bufferMap)o.add(f);let i=[],a=l(f=>{if(!f)return;let d=t.get(f);d?(s<d.firstUse&&(d.firstUse=s),s>d.lastUse&&(d.lastUse=s)):(d=new Vv(f,s,s,f.scope),t.set(f,d)),i.push(f)},"touch"),u=l((f,d)=>{if(!(!f||typeof f!="object"||d.has(f))){if(d.add(f),g2(f)){a(f);return}g2(f.buffer)&&a(f.buffer);for(let h of Object.keys(f)){if(Y4.has(h)||h==="buffer")continue;let m=f[h];if(!(!m||typeof m!="object"))if(Array.isArray(m))for(let g of m)u(g,d);else u(m,d)}}},"touchAll"),p=l((f,d)=>{for(let h=f;h<i.length;h++){let m=t.get(i[h]);m&&d>m.lastUse&&(m.lastUse=d)}},"extendRegion"),c=l(f=>{if(f)switch(f.type){case"SeqNode":for(let d of f.stmts)c(d);break;case"ForNode":{let d=s,h=i.length;u(f.min,new Set),u(f.extent,new Set),c(f.body);let m=s>d?s-1:d;p(h,m);break}case"WhileNode":{let d=s,h=i.length;u(f.condVar,new Set),c(f.condBody),c(f.loopBody);let m=s>d?s-1:d;p(h,m);break}case"BlockNode":r.push({idx:s,node:f});for(let d of f.reads)a(d.buffer);for(let d of f.writes)a(d.buffer);u(f.body,new Set),f.initBody&&u(f.initBody,new Set),s++;break;case"AllocateNode":a(f.buffer),c(f.body);break;case"IfThenElseNode":{let d=s,h=i.length;u(f.condition,new Set),c(f.thenBody),f.elseBody&&c(f.elseBody);let m=s>d?s-1:d;p(h,m);break}case"LetStmtNode":u(f.value,new Set),c(f.body);break;case"EvaluateNode":u(f.value,new Set);break;default:u(f,new Set);break}},"walk");return c(e.body),new Gv(t,r,o)}}});function J4(n,e){if(n.shape.length!==e.shape.length)return!1;for(let t=0;t<n.shape.length;t++)if(n.shape[t]!==e.shape[t])return!1;return!0}function Lc(n,e){if(n===e)return!0;if(!n||!e||typeof n!="object"||typeof e!="object"||n.type!==e.type)return!1;switch(n.type){case"VariableNode":return n.name===e.name;case"IntImmNode":return n.value===e.value;case"FloatImmNode":return n.value===e.value;case"MathOpNode":return n.op===e.op&&Lc(n.a,e.a)&&Lc(n.b,e.b);case"CompareNode":return n.direction===e.direction&&Lc(n.a,e.a)&&Lc(n.b,e.b);case"CastNode":return n.toDtype===e.toDtype&&Lc(n.expr,e.expr);case"BufferLoadNode":return n.buffer===e.buffer&&Uv(n.indices,e.indices);default:return!1}}function Uv(n,e){if(n.length!==e.length)return!1;for(let t=0;t<n.length;t++)if(!Lc(n[t],e[t]))return!1;return!0}function _2(n,e){n&&qs(n,e)}function Z4(n,e,t){let r=[],s=[];for(let p of[n.body,n.initBody])p&&_2(p,c=>{c.type==="BufferStoreNode"&&c.buffer===t?r.push(c):c.type==="BufferLoadNode"&&c.buffer===e&&s.push(c)});if(r.length!==1||s.length===0)return!1;let o=r[0],i=o.indices;for(let p of s)if(!Uv(p.indices,i))return!1;let a=new Set;for(let p of[o.value,...o.indices])p&&_2(p,c=>{c.type==="BufferLoadNode"&&c.buffer===e&&a.add(c)});let u=!0;for(let p of s)if(!a.has(p)){u=!1;break}if(!u){let p=o.value;if(!(p&&p.type==="BufferLoadNode"&&p.buffer===e&&Uv(p.indices,i)))return!1}return!0}function Q4(n,e){if(n)for(let t of yl(n,r=>r.type==="BlockNode",{kinds:"stmt"}))e.push(t)}var jv,Wb,b2=M(()=>{"use strict";ts();jv=class{static{l(this,"InplaceCandidate")}constructor(e,t,r){this.srcBuffer=e,this.dstBuffer=t,this.reason=r}},Wb=class{static{l(this,"InplaceAnalysis")}static analyze(e,t,r=new Set){let s=[],o=[];Q4(e.body,o);let i=new Map;for(let p of t.stmtOrder)i.set(p.node,p.idx);let a=new Map;for(let p of t.stmtOrder)for(let c of p.node.reads){let f=a.get(c.buffer);(f===void 0||p.idx>f)&&a.set(c.buffer,p.idx)}let u=new Set;for(let p of o){let c=i.get(p);if(c!==void 0)for(let f of p.writes){let d=f.buffer;if(!(t.isParam(d)&&!r.has(d)))for(let h of p.reads){let m=h.buffer;if(m===d||u.has(m)||t.isParam(m)&&!r.has(m)||!J4(m,d)||m.dtype!==d.dtype||m.scope!==d.scope)continue;let g=t.intervals.get(m),y=t.intervals.get(d);if(!(!g||!y)&&g.lastUse<=y.firstUse){let _=a.get(m);if(_===void 0||_<=c){if(!Z4(p,m,d))continue;s.push(new jv(m,d,`${m.name} last used at ${g.lastUse}, ${d.name} first used at ${y.firstUse}`)),u.add(m);break}}}}}return s}};l(J4,"shapesMatch");l(Lc,"exprEqual");l(Uv,"indexListEqual");l(_2,"walkNodes");l(Z4,"isInplaceComputeSafe");l(Q4,"collectBlocks")});function w2(n,e){let t=0,r=n.length;for(;t<r;){let s=t+r>>1;n[s].offset<=e.offset?t=s+1:r=s}n.splice(t,0,e)}function T2(n){return{best:null,consider(e,t,r){return t<r?null:n==="best-fit"?((this.best===null||t<this.best.gap)&&(this.best={offset:e,gap:t}),null):e},result(e){return this.best!==null?this.best.offset:e}}}var qv,Wv,Kb,x2=M(()=>{"use strict";l(w2,"insertByOffset");l(T2,"gapSelector");qv=class{static{l(this,"MemoryBlock")}constructor(e,t,r){this.offset=e,this.size=t,this.buffer=r}get end(){return this.offset+this.size}overlaps(e){return this.offset<e.end&&e.offset<this.end}},Wv=class{static{l(this,"MemoryPool")}constructor(e,t=64,r="best-fit"){this.scope=e,this.alignment=t,this.strategy=r,this.blocks=[],this.peakUsage=0}allocate(e,t){let r=this._align(e),s=this._findFreeOffset(r);return this.placeAt(s,r,t)}placeAt(e,t,r){let s=this._align(t),o=new qv(e,s,r);w2(this.blocks,o);let i=e+s;return i>this.peakUsage&&(this.peakUsage=i),o}_align(e){return Math.ceil(e/this.alignment)*this.alignment}_findFreeOffset(e){let t=this.blocks,r=0,s=T2(this.strategy);for(let o of t){let i=this._align(r),a=o.offset-i,u=s.consider(i,a,e);if(u!==null)return u;o.end>r&&(r=o.end)}return s.result(this._align(r))}fragmentation(){if(this.peakUsage===0)return 0;let e=this.blocks.reduce((t,r)=>t+r.size,0);return Math.max(0,1-e/this.peakUsage)}release(e){let t=this.blocks.indexOf(e);t>=0&&this.blocks.splice(t,1)}},Kb=class{static{l(this,"BufferAssignment")}constructor(){this.assignments=new Map,this.pools=new Map,this.inplaceMap=new Map,this.effLastUse=new Map}assign(e,t=[],r=64,s="best-fit"){for(let c of t)this.inplaceMap.set(c.dstBuffer,c.srcBuffer);let o=new Map;for(let c of e)o.set(c.buffer,c);let i=new Map;for(let c of e)i.set(c.buffer,c.lastUse);let a=new Map;for(let[c,f]of this.inplaceMap)!o.has(c)||!o.has(f)||(a.has(f)||a.set(f,[]),a.get(f).push(c));if(a.size>0){let c=new Map;for(let f of e){let d=f.buffer;if(c.get(d)===1)continue;let h=[d];for(;h.length;){let m=h[h.length-1],g=a.get(m);if(c.get(m)===void 0){if(c.set(m,0),g)for(let y of g)c.get(y)===void 0&&h.push(y);continue}if(h.pop(),c.get(m)!==1){if(g){let y=i.get(m);for(let _ of g){let b=i.get(_);b>y&&(y=b)}i.set(m,y)}c.set(m,1)}}}}this.effLastUse=i;let u=[...e].sort((c,f)=>{let d=c.size,h=f.size,m=d>0,g=h>0;if(m&&g){let y=h-d;if(y!==0)return y}else if(m!==g)return m?-1:1;return c.firstUse-f.firstUse}),p=new Map;for(let c of u){let f=c.buffer;if(this.inplaceMap.has(f)){let w=this.inplaceMap.get(f),x=this.assignments.get(w);if(x){this.assignments.set(f,{offset:x.offset,size:c.size,scope:c.scope,pool:x.pool,inplaceOf:w});continue}}let d=c.size;if(d===0)continue;if(d<0){this.assignments.set(f,{offset:0,size:0,scope:c.scope,pool:c.scope,inplaceOf:null,isDynamic:!0});continue}let h=c.scope,m=this.pools.get(h);m||(m=new Wv(h,r),this.pools.set(h,m)),p.has(h)||p.set(h,[]);let g=p.get(h),y=i.get(f)??c.lastUse,_=this._interferenceOffset(g,c.firstUse,y,m._align(d),r,s),b=m.placeAt(_,d,f);w2(g,{firstUse:c.firstUse,lastUseEff:y,offset:b.offset,size:b.size}),this.assignments.set(f,{offset:b.offset,size:b.size,scope:h,pool:h,inplaceOf:null})}return this}_interferenceOffset(e,t,r,s,o,i){let a=[];for(let c of e)c.firstUse<=r&&t<=c.lastUseEff&&a.push([c.offset,c.offset+c.size]);let u=0,p=T2(i);for(let[c,f]of a){let d=Math.ceil(u/o)*o,h=c-d,m=p.consider(d,h,s);if(m!==null)return m;f>u&&(u=f)}return p.result(Math.ceil(u/o)*o)}getOffset(e){let t=this.assignments.get(e);return t?t.offset:-1}getAssignment(e){return this.assignments.get(e)||null}peakMemory(e=null){if(e){let r=this.pools.get(e);return r?r.peakUsage:0}let t=0;for(let[,r]of this.pools)t+=r.peakUsage;return t}}});function Yb(n,e,t){if(!n||typeof n!="object"||t.has(n))return!1;if(t.add(n),n.type==="BufferLoadNode"&&(!e||n.buffer===e))return!0;for(let r of Object.keys(n)){if(r==="_parent"||r==="_parentKey"||r==="_parentIdx")continue;let s=n[r];if(!(!s||typeof s!="object")){if(Array.isArray(s)){for(let o of s)if(Yb(o,e,t))return!0}else if(Yb(s,e,t))return!0}}return!1}function S2(n){let e=new Set,t=new Map,r=new Set,s=[n.body];for(;s.length>0;){let o=s.pop();if(!(!o||typeof o!="object"||r.has(o))){if(r.add(o),o.type==="BufferStoreNode"&&o.buffer){let i=o.buffer;for(let u of o.indices)if(Yb(u,null,new Set)){e.add(i);break}Yb(o.value,i,new Set)&&e.add(i);let a=t.get(i);a||(a=[],t.set(i,a)),a.push(o.value)}for(let i of Object.keys(o)){if(i==="_parent"||i==="_parentKey"||i==="_parentIdx")continue;let a=o[i];if(Array.isArray(a))for(let u of a)u&&typeof u=="object"&&s.push(u);else a&&typeof a=="object"&&s.push(a)}}}for(let[o,i]of t){let a=l(u=>u&&(u.type==="IntImmNode"||u.type==="FloatImmNode")&&u.value===0,"isConstZero");i.every(a)&&e.add(o),i.length===1&&i[0]&&(i[0].type==="IntImmNode"||i[0].type==="FloatImmNode")&&e.add(o)}return e}function X4(n,e){let t=new Set,r=[n];for(;r.length>0;){let s=r.pop();if(!(!s||typeof s!="object"||t.has(s))){t.add(s);for(let o of Object.keys(s)){if(o==="_parent"||o==="_parentKey"||o==="_parentIdx")continue;let i=s[o];if(i&&typeof i=="object"&&e.has(i)){s[o]=e.get(i);continue}if(Array.isArray(i)){for(let a=0;a<i.length;a++){let u=i[a];u&&typeof u=="object"&&e.has(u)?i[a]=e.get(u):u&&typeof u=="object"&&r.push(u)}continue}i&&typeof i=="object"&&r.push(i)}}}}var Kv,Hb,v2=M(()=>{"use strict";y2();b2();x2();Fe();H0();Kv=class{static{l(this,"MemoryPlan")}constructor(e,t,r){this.assignment=e,this.liveness=t,this.inplaceCandidates=r,this.aliasMap=new Map}peakMemory(e=null){return this.assignment.peakMemory(e)}getReport(){let e=new Map;for(let[s,o]of this.assignment.pools)e.set(s,{peakUsage:o.peakUsage,numBuffers:0,numReused:0});for(let[s,o]of this.assignment.assignments){let i=e.get(o.scope);i&&(i.numBuffers++,o.inplaceOf&&i.numReused++)}let t=this.liveness.getTemporaries().length,r=this.inplaceCandidates.length;return{peakMemory:this.assignment.peakMemory(),scopeBreakdown:e,totalTemporaries:t,totalInplace:r,materializedReuse:this.aliasMap.size,assignments:this.assignment.assignments}}},Hb=class{static{l(this,"MemoryPlanner")}constructor(e={}){this.alignment=e.alignment||64,this.enableInplace=e.enableInplace!==!1,this.allocStrategy=e.allocStrategy||"best-fit",this.poolAllocation=e.poolAllocation||!1}plan(e){let t=qb.analyze(e),r=t.getTemporaries(),s=[];this.enableInplace&&(s=Wb.analyze(e,t));let o=new Kb;return o.assign(r,s,this.alignment,this.allocStrategy),new Kv(o,t,s)}planAndRewrite(e){let t=this.plan(e);return{func:this._insertAllocations(e,t),plan:t}}_insertAllocations(e,t){let r=t.liveness.getTemporaries();if(r.length===0)return e;let s=new Map;this.poolAllocation?this._assignPoolOffsets(e,t,r):(s=this._buildReuseAliases(r,t,e),s.size>0&&X4(e.body,s)),t.aliasMap=s;let o=[...r].sort((u,p)=>p.firstUse-u.firstUse),i=e.body,a=new Set;for(let u of o){let p=u.buffer;if(s.has(p))continue;let c=t.assignment.getAssignment(p);c&&(c.inplaceOf||a.has(p)||(a.add(p),i=new Ue(p,c.isDynamic?"dynamic":c.scope,i)))}return e.body=i,e._setChild("body",i),e}_assignPoolOffsets(e,t,r){let s=S2(e);for(let o of r){let i=o.buffer;if(s.has(i)||i.scope!=="global")continue;let a=t.assignment.getAssignment(i);!a||a.inplaceOf||a.isDynamic||a.size>0&&(i.poolByteOffset=a.offset)}}_buildReuseAliases(e,t,r){let s=S2(r),o=new Set(t.assignment.inplaceMap.values()),i=t.assignment.effLastUse,a=l(c=>i.get(c.buffer)??c.lastUse,"lastUseOf"),u=new Map;for(let c of e){let f=c.buffer,d=t.assignment.getAssignment(f);if(!d||d.inplaceOf||d.isDynamic||o.has(f)||f.numel()<=0||s.has(f))continue;let h=`${f.scope}|${f.dtype}|${f.shape.join(",")}|${f.strides.join(",")}`,m=u.get(h);m||(m=[],u.set(h,m)),m.push(c)}let p=new Map;for(let c of u.values()){if(c.length<2)continue;c.sort((d,h)=>d.firstUse-h.firstUse||a(d)-a(h));let f=new Tl((d,h)=>d.lastUse-h.lastUse);for(let d of c){let h=f.peek();h&&h.lastUse<d.firstUse?(f.pop(),h.lastUse=a(d),f.push(h),p.set(d.buffer,h.rep)):f.push({rep:d.buffer,lastUse:a(d)})}}return p}};l(Yb,"exprLoadsBuffer");l(S2,"collectFreshZeroDependent");l(X4,"rewriteBufferAliases")});var Jb,k2=M(()=>{"use strict";ma();v2();Jb=class extends ar{static{l(this,"MemoryPlanPass")}constructor(e){super("MemoryPlanPass","memoryPlanning"),this.config=e,this._planner=null}begin(e){let t=this.config.memory.alignment||this.config.target?.cacheLineSizeBytes||64;this._planner=new Hb({alignment:t,enableInplace:this.config.memory.inplaceReuse,allocStrategy:this.config.memory.allocStrategy,poolAllocation:this.config.memory.poolAllocation})}run(e,t){if(e.gpuRegisterBlocked)return;let r=performance.now(),{plan:s}=this._planner.planAndRewrite(e),o=s.getReport();t.trace.memoryStats(e.name,{durationMs:performance.now()-r,peakMemory:o.peakMemory,totalTemporaries:o.totalTemporaries,totalInplace:o.totalInplace})}}});function Cc(n){return n&&n.type==="IntImmNode"?n.value:null}function ej(n){if(!n||n.type!=="ForNode"||!N2.has(n.kind)||Cc(n.min)!==0)return null;let e=Cc(n.extent);if(e===null)return null;let t=n.body;if(!t||t.type!=="ForNode"||!N2.has(t.kind)||Cc(t.min)!==0)return null;let r=Cc(t.extent);if(r===null||r<=0)return null;let s=t.body;if(!s||s.type!=="IfThenElseNode"||s.elseBody)return null;let o=tj(s.condition,n.loopVar.name,t.loopVar.name,r);if(o===null||o<=0)return null;let i=Math.floor(o/r),a=o-i*r;if(a===0||i<1||e!==i+1)return null;let u=new Xs;if(u.bind(n.loopVar.name,0,i-1),u.bind(t.loopVar.name,0,r-1),!_i(u,s.condition))return null;let p=s.thenBody,c=new he(t.loopVar,new L(0),new L(r),t.kind,p,t.threadTag),f=new he(n.loopVar,new L(0),new L(i),n.kind,c,n.threadTag),d=Gi(Iu(p),g=>g.type==="VariableNode"&&g.name===n.loopVar.name?new L(i):g),h=new Oe(t.loopVar.name,t.loopVar.dtype),m=new he(h,new L(0),new L(a),t.kind,d,t.threadTag);return new te([f,m])}function tj(n,e,t,r){if(!n)return null;let s,o;if(n.type==="MathOpNode"&&n.op==="<")s=n.a,o=n.b;else if(n.type==="CompareNode"&&n.direction==="lt")s=n.a,o=n.b;else return null;let i=Cc(o);if(i===null||!s||s.type!=="MathOpNode"||s.op!=="+")return null;let a=s.a,u=s.b;return!u||u.type!=="VariableNode"||u.name!==t||!a||a.type!=="MathOpNode"||a.op!=="*"||!a.a||a.a.type!=="VariableNode"||a.a.name!==e||Cc(a.b)!==r?null:i}var N2,Zb,A2=M(()=>{"use strict";ma();Fe();ts();Ed();Yl();Tf();N2=new Set([z.SERIAL,z.PARALLEL,z.VECTORIZED,z.UNROLLED]),Zb=class extends ar{static{l(this,"LoopPartitionPass")}constructor(){super("LoopPartitionPass","loopPartition")}run(e,t){let r=Gi(e.body,s=>ej(s)||s);return r&&r!==e.body&&(e.body=r,e._setChild("body",r)),e}};l(Cc,"intImm");l(ej,"partitionLoop");l(tj,"matchFlatGuard")});function Qb(n){let e=n.body;if(!e||e.type!=="BlockNode")return null;let t=e.body;if(!t||t.type!=="BufferStoreNode")return null;let r=t,s=r.value;if(!s||s.type!=="MathOpNode"||!nj.has(s.op))return null;let o=null,i=null;if(s.a&&s.a.type==="BufferLoadNode"&&s.a.buffer===r.buffer?(o=s.a,i=s.b):s.b&&s.b.type==="BufferLoadNode"&&s.b.buffer===r.buffer&&(o=s.b,i=s.a),!o)return null;let a=Hv(r.indices),u=Hv(o.indices);if(a!==u||a.includes("?"))return null;let p=r.indices.map(d=>{if(d.type!=="VariableNode")return d;for(let h of e.iterVars)if(h.iterVar&&h.iterVar.name===d.name&&h.binding)return h.binding;return d}),c=n.loopVar.name,f=Hv(p);return f.includes("?")||f.includes("$"+c)?null:{store:r,loadSide:o,valueSide:i,outerIndices:p,block:e,op:r.value.op}}function Hv(n){return n.map(Yv).join(",")}function Yv(n){return n?n.type==="VariableNode"?"$"+n.name:n.type==="IntImmNode"?String(n.value):n.type==="MathOpNode"?"("+Yv(n.a)+n.op+(n.b?Yv(n.b):"")+")":"?":"?"}var nj,Jv=M(()=>{"use strict";nj=new Set(["+","*","max","min"]);l(Qb,"detectAccumulator");l(Hv,"indicesKey");l(Yv,"exprKey")});function Vo(n){if(!(!n||typeof n!="object"||!n.type))switch(n.type){case"ForNode":{let e=Qb(n);n.accumulator=e,e||Vo(n.body);return}case"BlockNode":Vo(n.body),n.initBody&&Vo(n.initBody);return;case"SeqNode":for(let e of n.stmts)Vo(e);return;case"LetStmtNode":case"AllocateNode":Vo(n.body);return;case"IfThenElseNode":Vo(n.thenBody),n.elseBody&&Vo(n.elseBody);return;case"WhileNode":Vo(n.condBody),Vo(n.loopBody);return;default:return}}var Xb,O2=M(()=>{"use strict";ma();Jv();Xb=class extends ar{static{l(this,"AccumulatorDetectionPass")}constructor(){super("AccumulatorDetectionPass","accumulatorDetect")}run(e,t){return Vo(e.body),e}};l(Vo,"annotateStmt")});function sj(n){let e=[],t=[n.body];for(;t.length>0;){let s=t.pop();if(s){if(s.type==="BlockNode"){e.push(s),t.push(s.body);continue}if(s.body&&t.push(s.body),s.stmts)for(let o of s.stmts)t.push(o);s.thenBody&&t.push(s.thenBody),s.elseBody&&t.push(s.elseBody)}}if(e.length===0)return null;let r=null;for(let s of e){if(!s.name.includes("matmul"))return null;s.reads.length>=2&&s.writes.length>=1&&(r=s)}return r}function oj(n){let e=sj(n);if(!e)return null;let t=e.reads[0].buffer,r=e.reads[1].buffer,s=e.writes[0].buffer;if(!B2.has(t.dtype)||!B2.has(r.dtype)||s.dtype!=="f32"||t.shape.length!==2||r.shape.length!==2||s.shape.length!==2)return null;let o=s.shape[0],i=s.shape[1],a=t.shape[1];if(![o,i,a].every(p=>typeof p=="number"&&p>0&&p%rj===0))return null;let u=new Set;for(let[,p]of n.bufferMap)u.add(p.name);return!u.has(t.name)||!u.has(r.name)||!u.has(s.name)?null:{M:o,N:i,K:a,a:t.name,b:r.name,c:s.name}}var rj,B2,ew,R2=M(()=>{"use strict";ma();du();rj=16,B2=new Set(["f16","bf16"]);l(sj,"findMatmulBlock");l(oj,"detectWmmaMatmul");ew=class extends ar{static{l(this,"AutoTensorizePass")}constructor(e){super("AutoTensorizePass","scheduling"),this.config=e,this.target=e.target}run(e,t){if(e.cublasInfo||e._tensorIntrin||!this.target||!this.target.isGPU())return;let r=oj(e);r&&(new Cn(e).tensorize("wmma_16x16x16_f16f16f32",r),t&&t.trace&&t.trace.explainsEnabled&&t.trace.explain("tensorize",e.name,"wmma_16x16x16_f16f16f32",`auto-tensorized ${r.M}x${r.N}x${r.K} f16 GEMM`,{target:this.target.name}))}}});function ij(n){let e=[],t=new Map,r=new Map,s=[{node:n,block:null}];for(;s.length>0;){let{node:o,block:i}=s.pop();if(o.type==="BufferLoadNode"&&o.buffer)t.set(o.buffer.name,(t.get(o.buffer.name)||0)+1),i&&i.reads.add(o.buffer.name);else if(o.type==="BufferStoreNode"&&o.buffer&&i){i.writes.add(o.buffer.name);let u=r.get(o.buffer.name);u||(u=new Set,r.set(o.buffer.name,u)),u.add(i.name)}let a=i;o.type==="BlockNode"&&(a={name:o.name,writes:new Set,reads:new Set,hasInit:o.initBody!=null},e.push(a));for(let u of ln(o))s.push({node:u,block:a})}return{blocks:e,loadCount:t,storeWriters:r}}var tw,M2=M(()=>{"use strict";ma();du();Do();ts();l(ij,"analyzeFunc");tw=class extends ar{static{l(this,"InlineReindexPass")}constructor(e){super("InlineReindexPass","scheduling"),this.config=e,this.target=e.target,this.snapshotPoint="afterInlineReindex"}run(e,t){if(!this.target.isGPU()||this.target.isWebGPU()||e.cublasInfo||e._tensorIntrin||tS(e))return;let r=this.config.scheduling;if(!(r.enabled||r.gpuTiling||r.autotune))return;let s=new Set;for(let[,c]of e.bufferMap)s.add(c.name);let{blocks:o,loadCount:i,storeWriters:a}=ij(e.body),u=new Cn(e),p=!1;for(let c of o){if(c.hasInit||c.writes.size===0)continue;let f=[...c.writes];if(f.every(d=>!s.has(d)&&a.get(d).size===1)&&f.some(d=>(i.get(d)||0)>0)&&[...c.reads].every(d=>(a.get(d)?a.get(d).size:0)<=1))try{u.computeInlineBlock(c.name),p=!0}catch{}}p&&Xx(e)}}});function Zv(n,e,t=aj){return t.filter(r=>r.phase===n).sort((r,s)=>r.priority-s.priority).map(r=>r.factory(e)).filter(Boolean)}var aj,I2=M(()=>{"use strict";aj=[];l(Zv,"tirPassesForPhase")});function E2(n){let e=[];for(let t of Zv("pre",n))e.push(t);e.push(new tw(n)),n.optimization.tensorize&&e.push(new ew(n)),e.push(new jb(n)),n.optimization.loopPartition&&e.push(new Zb),e.push(new Ub),e.push(new Jb(n)),n.optimization.detectAccumulators&&e.push(new Xb);for(let t of Zv("post",n))e.push(t);return e}var L2=M(()=>{"use strict";f2();m2();k2();A2();O2();R2();M2();I2();l(E2,"buildTirPipeline")});var Gd,C2=M(()=>{"use strict";Fe();Gd=class{static{l(this,"TensorVerifier")}constructor(){this.errors=[],this.boundVars=new Set}verify(e){if(this.errors=[],this.boundVars.clear(),!(e instanceof Pi))return this.errors.push("Expected PrimFunc at root"),this.errors;for(let t of e.params)this.boundVars.add(t.name);if(e.shapeParams)for(let t of e.shapeParams)this.boundVars.add(t.name);return this.visit(e.body),this.errors}visit(e){let t=[e];for(;t.length>0;){let r=t.pop();if(r)switch(r.type){case"SeqNode":for(let s=r.stmts.length-1;s>=0;s--)t.push(r.stmts[s]);break;case"ForNode":this.boundVars.has(r.loopVar.name)&&this.errors.push(`Loop variable ${r.loopVar.name} already bound`),this.boundVars.add(r.loopVar.name),t.push({type:"_unbind",name:r.loopVar.name}),t.push(r.body);break;case"BlockNode":for(let s of r.iterVars)s.iterVar&&(this.boundVars.has(s.iterVar.name)&&this.errors.push(`Block variable ${s.iterVar.name} already bound`),this.boundVars.add(s.iterVar.name));for(let s=r.iterVars.length-1;s>=0;s--)r.iterVars[s].iterVar&&t.push({type:"_unbind",name:r.iterVars[s].iterVar.name});t.push(r.body),r.initBody&&t.push(r.initBody);break;case"AllocateNode":r.buffer||this.errors.push("Allocate missing buffer"),t.push(r.body);break;case"LetStmtNode":this._visitExpr(r.value),this.boundVars.add(r.variable.name),t.push({type:"_unbind",name:r.variable.name}),t.push(r.body);break;case"IfThenElseNode":this._visitExpr(r.condition),r.elseBody&&t.push(r.elseBody),t.push(r.thenBody);break;case"WhileNode":r.condVar||this.errors.push("WhileNode missing condition variable"),t.push(r.loopBody),t.push(r.condBody);break;case"BufferStoreNode":if(r.buffer||this.errors.push("BufferStore missing buffer"),(!r.indices||r.indices.length!==r.buffer.shape.length)&&this.errors.push(`BufferStore rank mismatch for ${r.buffer?r.buffer.name:"unknown"}`),r.indices)for(let s of r.indices)this._visitExpr(s);this._visitExpr(r.value);break;case"EvaluateNode":this._visitExpr(r.value);break;case"_unbind":this.boundVars.delete(r.name);break;default:this._visitExpr(r);break}}}_visitExpr(e){if(e)switch(e.type){case"BufferLoadNode":if(e.buffer||this.errors.push("BufferLoad missing buffer"),(!e.indices||e.indices.length!==e.buffer.shape.length)&&this.errors.push(`BufferLoad rank mismatch for ${e.buffer?e.buffer.name:"unknown"}`),e.indices)for(let t of e.indices)this._visitExpr(t);break;case"MathOpNode":this._visitExpr(e.a),e.b&&this._visitExpr(e.b);break;case"CompareNode":this._visitExpr(e.a),this._visitExpr(e.b);break;case"CastNode":this._visitExpr(e.expr);break;case"CallExternNode":e.externName||this.errors.push("CallExtern missing function name");for(let t of e.args)this._visitExpr(t);break;case"IfThenElseNode":this._visitExpr(e.condition),this._visitExpr(e.thenBody),e.elseBody&&this._visitExpr(e.elseBody);break;case"VariableNode":this.boundVars.has(e.name)||this.errors.push(`Unbound variable used: ${e.name}`);break;case"BlockRealizeNode":e.binding&&this._visitExpr(e.binding);break;case"IntImmNode":case"FloatImmNode":break}}}});function Qv(n){let e=[];n.functionCount===0&&e.push(new Et("Module has no functions"));for(let t of n)jd(t,e);return e}function jd(n,e=[]){if(!n.entryBlock)return e.push(new Et("Function has no entry block",null,n)),e;n.entryBlock.arguments.length!==n.inputTypes.length&&e.push(new Et(`Entry block has ${n.entryBlock.arguments.length} args, expected ${n.inputTypes.length}`,null,n));for(let s=0;s<n.entryBlock.arguments.length;s++){let o=n.entryBlock.arguments[s],i=n.inputTypes[s];i&&!o.type.equals(i)&&e.push(new Et(`Block arg ${s} type ${Gn(o.type)} != expected ${Gn(i)}`,null,n))}let t=new Set;for(let s of n.entryBlock.arguments)t.add(s);for(let s of n.body)D2(s,t);for(let s of n.body)P2(s,n,t,e);let r=n.getReturnOp();if(!r)e.push(new Et("Missing return op",null,n));else{r.numOperands!==n.outputTypes.length&&e.push(new Et(`Return has ${r.numOperands} operands, function declares ${n.outputTypes.length} outputs`,r,n));for(let s=0;s<Math.min(r.numOperands,n.outputTypes.length);s++){let o=r.getOperand(s).type,i=n.outputTypes[s];i instanceof I&&o instanceof I&&(o.dtype!==i.dtype&&e.push(new Et(`Return operand ${s} dtype ${o.dtype} != expected ${i.dtype}`,r,n)),o.shapeCompatible(i)||e.push(new Et(`Return operand ${s} shape incompatible: ${Gn(o)} vs ${Gn(i)}`,r,n)))}}return e}function D2(n,e){for(let t of n.arguments)e.add(t);for(let t of n)for(let r=0;r<t.numResults;r++){let s=t.getResult(r);s&&e.add(s)}}function uj(n,e,t){let r=new Set;for(let u of n)r.add(u);let s=1,o=2,i=new Map,a=new Set;for(let u of n){if(i.get(u)!==void 0)continue;let p=[{op:u,i:0}];for(i.set(u,s);p.length>0;){let c=p[p.length-1],f=c.op;if(c.i<f.numOperands){let d=f.getOperand(c.i);c.i++;let h=d&&d.definingOp;if(!h||!r.has(h))continue;let m=i.get(h);m===s?a.has(h)||(a.add(h),t.push(new Et("participates in a value dependency cycle",h,e))):m===void 0&&(i.set(h,s),p.push({op:h,i:0}));continue}i.set(f,o),p.pop()}}}function P2(n,e,t,r){uj(n,e,r);for(let s of n)lj(s,e,t,r);if(n.size>0){let s=n.lastOp;if(n.parentRegion&&n.parentRegion.parentOp){let o=n.parentRegion.parentOp.opName,i=ne.get(o);i&&i.hasRegions&&(s.isTerminator()||r.push(new Et(`Block in ${o} region must end with terminator, got '${s.opName}'`,s,e)))}}}function lj(n,e,t,r){for(let o=0;o<n.numOperands;o++){let i=n.getOperand(o);if(!i){r.push(new Et(`Operand ${o} is null`,n,e));continue}if(!(i instanceof Li)){r.push(new Et(`Operand ${o} is not a Value`,n,e));continue}t.has(i)||r.push(new Et(`Operand ${o} used before definition`,n,e))}for(let o=0;o<n.numResults;o++){let i=n.getResult(o);i||r.push(new Et(`Result ${o} is null`,n,e)),i&&i.definingOp!==n&&r.push(new Et(`Result ${o} definingOp mismatch`,n,e))}let s=ne.get(n.opName);if(!s){!["return","yield"].includes(n.opName)&&!ne.has(n.opName)&&r.push(new Et(`Unknown op '${n.opName}'`,n,e));return}s.numOperands>=0&&n.numOperands!==s.numOperands&&r.push(new Et(`'${n.opName}' expects ${s.numOperands} operands, got ${n.numOperands}`,n,e)),s.numResults>=0&&n.numResults!==s.numResults&&r.push(new Et(`'${n.opName}' expects ${s.numResults} results, got ${n.numResults}`,n,e));for(let o of s.attrs)o.required&&!n.hasAttr(o.name)&&r.push(new Et(`'${n.opName}' missing required attribute '${o.name}'`,n,e));if(s.hasRegions&&s.numRegions>0&&n.numRegions!==s.numRegions&&r.push(new Et(`'${n.opName}' expects ${s.numRegions} regions, got ${n.numRegions}`,n,e)),s.verify){let o=s.verify(n);if(o)for(let i of o)r.push(new Et(i,n,e))}if(s.inferResultTypes&&n.numResults>0){let o=[];for(let a=0;a<n.numOperands;a++)o.push(n.getOperand(a).type);let i=s.inferResultTypes(o,n.attributes,n.results.map(a=>a.type));if(i)for(let a=0;a<Math.min(i.length,n.numResults);a++){let u=n.getResult(a).type,p=i[a];p instanceof I&&u instanceof I&&(u.dtype!==p.dtype&&r.push(new Et(`Result ${a} dtype ${u.dtype} != inferred ${p.dtype}`,n,e)),u.shapeCompatible(p)||r.push(new Et(`Result ${a} shape ${Gn(u)} incompatible with inferred ${Gn(p)}`,n,e)))}}for(let o of n.regions){let i=new Set(t);for(let a of o)D2(a,i);for(let a of o)P2(a,e,i,r)}}var Et,$2=M(()=>{"use strict";Re();Ih();Ct();Et=class{static{l(this,"VerificationError")}constructor(e,t=null,r=null){this.message=e,this.op=t,this.func=r}toString(){let e="";return this.func&&(e+=`[${this.func.name}] `),this.op&&(e+=`op '${this.op.opName}' (id=${this.op.id}): `),e+this.message}};l(Qv,"verifyModule");l(jd,"verifyFunction");l(D2,"collectScopeDefs");l(uj,"detectCycles");l(P2,"verifyBlock");l(lj,"verifyOperation")});var nw,Xv,Dc,ek,tk=M(()=>{"use strict";cv();Re();nw=class{static{l(this,"ValueObserver")}constructor(){this.min=1/0,this.max=-1/0,this.count=0,this.histogram=null}update(e){for(let t=0;t<e.length;t++){let r=e[t];r<this.min&&(this.min=r),r>this.max&&(this.max=r)}this.count+=e.length,this.histogram&&this.histogram.update(e)}enableHistogram(e=2048){this.histogram=new Xv(e)}},Xv=class{static{l(this,"HistogramCollector")}constructor(e=2048){this.numBins=e,this.bins=new Float64Array(e),this.rangeMin=0,this.rangeMax=0,this.initialized=!1,this.totalCount=0}update(e){this.initialized||this._initRange(e);let t=this.rangeMax-this.rangeMin;if(t<=0)return;for(let s=0;s<e.length;s++){let o=e[s];if(o<this.rangeMin||o>this.rangeMax){this._expandAndRebucket(e);return}}let r=this.numBins/t;for(let s=0;s<e.length;s++){let o=Math.min(this.numBins-1,Math.floor((e[s]-this.rangeMin)*r));this.bins[o]++}this.totalCount+=e.length}_initRange(e){let t=1/0,r=-1/0;for(let s=0;s<e.length;s++)e[s]<t&&(t=e[s]),e[s]>r&&(r=e[s]);t===r&&(r=t+1),this.rangeMin=t,this.rangeMax=r,this.initialized=!0}_expandAndRebucket(e){let t=this.rangeMin,r=this.rangeMax;for(let c=0;c<e.length;c++)e[c]<t&&(t=e[c]),e[c]>r&&(r=e[c]);let s=new Float64Array(this.bins),o=this.rangeMin,a=this.rangeMax-o;this.rangeMin=t,this.rangeMax=r;let u=r-t,p=this.numBins/u;if(this.bins.fill(0),a>0){let c=a/this.numBins;for(let f=0;f<this.numBins;f++){if(s[f]===0)continue;let d=o+(f+.5)*c,h=Math.min(this.numBins-1,Math.floor((d-t)*p));this.bins[h]+=s[f]}}for(let c=0;c<e.length;c++){let f=Math.min(this.numBins-1,Math.floor((e[c]-t)*p));this.bins[f]++}this.totalCount+=e.length}computePercentileThreshold(e){let t=this.totalCount*e,r=0,s=(this.rangeMax-this.rangeMin)/this.numBins;for(let o=0;o<this.numBins;o++)if(r+=this.bins[o],r>=t)return this.rangeMin+(o+1)*s;return this.rangeMax}computeEntropyThreshold(e){if(this.totalCount===0)return this.rangeMax;let t=new Float64Array(this.numBins);for(let i=0;i<this.numBins;i++)t[i]=this.bins[i]/this.totalCount;let r=this.numBins,s=1/0;for(let i=e;i<=this.numBins;i++){let a=i/e,u=new Float64Array(i);for(let c=0;c<e;c++){let f=Math.floor(c*a),d=Math.min(i,Math.floor((c+1)*a)),h=0;for(let g=f;g<d;g++)h+=t[g];let m=d-f;if(m>0){let g=h/m;for(let y=f;y<d;y++)u[y]=g}}let p=0;for(let c=0;c<i;c++)t[c]>0&&u[c]>0&&(p+=t[c]*Math.log(t[c]/u[c]));p<s&&(s=p,r=i)}let o=(this.rangeMax-this.rangeMin)/this.numBins;return this.rangeMin+r*o}},Dc=class{static{l(this,"CalibrationCollector")}constructor(e="minmax"){this.observers=new Map,this.mode=e}attach(e){for(let t of e.ops())for(let r=0;r<t.numResults;r++){let s=t.getResult(r);if(s.type instanceof I&&jt(s.type.dtype)){let o=new nw;(this.mode==="entropy"||this.mode==="percentile")&&o.enableHistogram(),this.observers.set(s,o)}}for(let t of e.args)if(t.type instanceof I&&jt(t.type.dtype)){let r=new nw;(this.mode==="entropy"||this.mode==="percentile")&&r.enableHistogram(),this.observers.set(t,r)}}observe(e,t){let r=this.observers.get(e);r&&r.update(t)}getResult(){return new ek(this.observers,this.mode)}},ek=class{static{l(this,"CalibrationResult")}constructor(e,t){this._observers=e,this._mode=t}getRange(e){let t=this._observers.get(e);return!t||t.count===0?null:{min:t.min,max:t.max}}getQuantParams(e,t,r){let s=this.getRange(e);if(!s)return null;let o=t===Pr.PER_TENSOR_ASYMMETRIC;if(this._mode==="percentile"&&this._observers.get(e).histogram){if(o)return vr.fromRange(s.min,s.max,t,r);let a=this._observers.get(e).histogram.computePercentileThreshold(.999);return vr.fromRange(-a,a,t,r)}if(this._mode==="entropy"&&this._observers.get(e).histogram){if(o)return vr.fromRange(s.min,s.max,t,r);let i=this._observers.get(e).histogram,a=r==="ui8"?256:255,u=i.computeEntropyThreshold(a);return vr.fromRange(-u,u,t,r)}return vr.fromRange(s.min,s.max,t,r)}hasData(e){let t=this._observers.get(e);return t&&t.count>0}values(){return this._observers.keys()}}});function pj(n,e){let t=[],r=new Set;for(let s of n.ops())if(e.has(s.opName))for(let o=0;o<s.numOperands;o++){let i=s.getOperand(o);if(r.has(i)||!(i.type instanceof I)||!jt(i.type.dtype))continue;let a=i.definingOp;a&&a.opName==="constant"||(r.add(i),t.push(i))}return t}function fj(n,e){let t=e.map(h=>h.type),r=new yn(n.name,n.inputTypes,[...n.outputTypes,...t]),s=new Map,o=n.entryBlock.arguments,i=r.entryBlock.arguments;for(let h=0;h<o.length;h++)s.set(o[h],i[h]);let a=n.entryBlock.opsArray(),u=new Set(a),p=Qo(a,h=>u.has(h),"ignore"),c=new Map;for(let h of p)h.opName!=="return"&&c.set(h,h.clone(s));for(let h of a)h.opName!=="return"&&r.entryBlock.pushOp(c.get(h));let f=n.getReturnOp(),d=[];if(f)for(let h of f.operands)d.push(s.get(h)||h);for(let h of e)d.push(s.get(h));return r.entryBlock.pushOp(new Me("return",d,[],{})),r}function F2(n,e,t,r={}){if(e.isGPU&&e.isGPU())throw new Error("collectCalibration: synchronous calibration is unavailable for async (GPU) targets; precompute calibration on a CPU/WASM target or supply quantization.calibration directly");if(!t||t.length===0)throw new Error("collectCalibration: at least one calibration batch is required");let s=r.quantizableOps||cj,o=r.mode||"minmax",i=pj(n,s),a=new Dc(o);a.attach(n);let u=a.getResult();if(i.length===0)return u;let p=fj(n,i),c=new Mn("__calibrate__");c.addFunction(p);let f=n.outputTypes.length,h=(r.compileFn||dj)(c,e);for(let m of t){let g=Array.isArray(m)?m:[m],y=p.outputTypes.map(_=>new Float32Array(Math.max(1,_.numel())));h.run(n.name,...g,...y);for(let _=0;_<i.length;_++)a.observe(i[_],y[f+_])}return u}function dj(n,e){throw new Error("collectCalibration: opts.compileFn is required (pass the compileModule function to avoid a circular import)")}var cj,z2=M(()=>{"use strict";Us();Di();yr();Re();Da();tk();cj=new Set(["dot","conv","add","mul","sub"]);l(pj,"activationTargets");l(fj,"buildCaptureFunction");l(F2,"collectCalibration");l(dj,"defaultCompileFn")});function rk(n){let e=0;for(let t=0;t<n.numResults;t++){let r=n.getResult(t).type;r instanceof I&&r.isFullyStatic&&(e+=r.sizeInBytes())}return e}var nk,Pc,sk,rw,V2=M(()=>{"use strict";Ct();Re();Ad();nk=class{static{l(this,"Partition")}constructor(e,t){this.id=e,this.target=t,this.ops=[],this.opSet=new Set,this._inputValues=null,this._outputValues=null,this._memoryBytes=0}addOp(e){this.opSet.has(e)||(this.ops.push(e),this.opSet.add(e),this._inputValues=null,this._outputValues=null,this._memoryBytes+=rk(e))}hasOp(e){return this.opSet.has(e)}merge(e){for(let t of e.ops)this.addOp(t)}computeIO(){if(this._inputValues&&this._outputValues)return;let{inputs:e,outputs:t}=pa(this.opSet,this.ops);this._inputValues=e,this._outputValues=t}getInputValues(){return this.computeIO(),this._inputValues}getOutputValues(){return this.computeIO(),this._outputValues}get size(){return this.ops.length}get memoryBytes(){return this._memoryBytes}};l(rk,"estimateOpMemory");Pc=class{static{l(this,"PartitionerConfig")}constructor(e={}){this.targets=e.targets||[],this.defaultTarget=e.defaultTarget||null,this.opTargetOverrides=e.opTargetOverrides||new Map,this.memoryLimits=e.memoryLimits||new Map,this.minPartitionSize=e.minPartitionSize||1,this.costWeights={transferCost:1,loadBalance:.5,...e.costWeights||{}}}},sk=class{static{l(this,"PartitionResult")}constructor(e,t,r){this.partitions=e,this.opToPartition=t,this.transferEdges=r}getPartition(e){return this.opToPartition.get(e)||null}getPartitionsForTarget(e){return this.partitions.filter(t=>t.target===e||t.target.name===e.name)}get numPartitions(){return this.partitions.length}},rw=class{static{l(this,"GraphPartitioner")}constructor(e){this.config=e instanceof Pc?e:new Pc(e),this._supportCache=new Map,this._buildSupportMap()}partition(e){let t=this._collectPartitionableOps(e),r=this._assignTargets(t),s=this._buildPartitions(t,r),o=this._mergeSmallPartitions(s,r),i=this._computeTransferEdges(o),a=new Map;for(let u of o)for(let p of u.ops)a.set(p,u);return new sk(o,a,i)}_buildSupportMap(){for(let e of this.config.targets){let t=new Set;for(let r of ne.names())this._targetSupportsOp(e,r)&&t.add(r);this._supportCache.set(e,t)}}_targetSupportsOp(e,t){if(e.hasLibraryOp(t))return!0;let r=ne.get(t);return r?r.isConstant||r.isTerminator?!0:e.isGPU()?r.isElementwise||r.isReduction||r.isBroadcast||r.isInjective||r.getAttr("gpuCapable")===!0:e.isCPU()?!0:e.isWasm()?!r.isOpaque&&t!=="custom_call":!1:!1}_collectPartitionableOps(e){let t=[];for(let r of e.ops()){let s=ne.get(r.opName);!s||s.isTerminator||t.push(r)}return t}_assignTargets(e){let t=new Map;for(let r of e){let s=r.getAttr("device");if(s){let a=this._resolveDeviceAttr(s);if(a){t.set(r,a);continue}}let o=this.config.opTargetOverrides.get(r.opName);if(o){t.set(r,o);continue}let i=this._selectBestTarget(r);t.set(r,i)}return t}_resolveDeviceAttr(e){return typeof e=="string"?this.config.targets.find(t=>t.name===e||t.kind===e)||null:e}_selectBestTarget(e){let t=this.config.defaultTarget||this.config.targets[0],r=-1/0;for(let s of this.config.targets){let o=this._supportCache.get(s);if(!o||!o.has(e.opName))continue;let i=this._scoreTargetForOp(s,e);i>r&&(r=i,t=s)}return t}_scoreTargetForOp(e,t){let r=ne.get(t.opName);if(!r)return 0;let s=0;if(e.hasLibraryOp(t.opName)&&(s+=100),r.isReduction||r.isElementwise){let o=0;for(let i=0;i<t.numOperands;i++){let a=t.getOperand(i).type;a instanceof I&&a.isFullyStatic&&(o+=a.numel())}e.isGPU()&&o>1024?s+=50:e.isCPU()&&o<=1024&&(s+=30)}return s+=e.computeTFLOPs*10,s}_buildPartitions(e,t){let{partitions:r}=Nd(e,{sort:l(i=>this._topologicalSort(i),"sort"),labelOf:l(i=>t.get(i),"labelOf"),sameLabel:l((i,a)=>i===a||i.name===a.name,"sameLabel"),canMerge:l((i,a,u)=>this._fitsMemoryLimit(i,a,u),"canMerge"),onAttach:l((i,a)=>{i.memoryBytes=(i.memoryBytes||0)+rk(a)},"onAttach")}),s=new Map;for(let i of r){let a=new nk(i.id,i.label);for(let u of i.ops)a.addOp(u);s.has(i.label.name)||s.set(i.label.name,[]),s.get(i.label.name).push(a)}let o=[];for(let i of s.values())for(let a of i)o.push(a);return o}_fitsMemoryLimit(e,t,r){let s=this.config.memoryLimits.get(r.name);if(!s)return!0;let o=rk(t);return e.memoryBytes+o<=s}_mergeSmallPartitions(e,t){if(e.length<=1)return e;let r=new Map;for(let h of e)for(let m of h.ops)r.set(m,h);let s=new Set,o=new Map,i=new Map;for(let h of e)s.has(h)||o.set(h,new Set);for(let h of e){if(s.has(h))continue;let m=o.get(h);for(let g of h.ops)for(let y=0;y<g.numResults;y++)for(let _ of g.getResult(y).uses()){let b=r.get(_.user);b&&b!==h&&m.add(b)}}let a=l((h,m)=>{let g=o.get(h);for(let y of o.get(m))y!==h&&g.add(y);g.delete(m);for(let[y,_]of o)_.has(m)&&(_.delete(m),y!==h&&_.add(h));o.delete(m),i=new Map},"mergeSucc"),u=l(h=>{let m=i.get(h);if(m)return m;m=new Set;let g=[...o.get(h)];for(;g.length>0;){let y=g.pop();if(m.has(y))continue;m.add(y);let _=o.get(y);if(_)for(let b of _)g.push(b)}return i.set(h,m),m},"reachOf"),p=l((h,m)=>{for(let g of o.get(h))if(g!==m&&u(g).has(m))return!0;return!1},"pathThroughIntermediate"),c=l((h,m)=>p(h,m)||p(m,h),"mergeCreatesCycle"),f=[];for(let h=0;h<e.length;h++){let m=e[h];if(s.has(m))continue;if(m.size>=this.config.minPartitionSize){f.push(m);continue}let g=-1,y=-1/0;for(let _=0;_<e.length;_++){if(h===_)continue;let b=e[_];if(s.has(b)||b.target.name!==m.target.name||c(m,b))continue;let w=this.config.costWeights.transferCost*this._mergeScore(m,b);w>y&&(y=w,g=_)}if(g>=0){let _=e[g];a(_,m),_.merge(m);for(let b of m.ops)r.set(b,_);s.add(m)}else f.push(m)}let d=new Set;for(let h of f)for(let m of h.ops)d.add(m);for(let h of e){let m=!1;for(let g of h.ops)if(!d.has(g)){m=!0;break}if(m){f.push(h);for(let g of h.ops)d.add(g)}}return f}_mergeScore(e,t){let r=0,s=new Set;for(let o of e.ops)for(let i=0;i<o.numResults;i++)s.add(o.getResult(i));for(let o of t.ops)for(let i=0;i<o.numOperands;i++)s.has(o.getOperand(i))&&r++;return r}_computeTransferEdges(e){let t=[],r=new Set,s=new Map;for(let o of e)for(let i of o.ops)s.set(i,o);for(let o of e)for(let i of o.ops)for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a).definingOp;if(!u)continue;let p=s.get(u);if(!p||p===o)continue;let c=i.getOperand(a),f=`${p.id}|${o.id}|${c.id}`;r.has(f)||(r.add(f),t.push({src:p,dst:o,value:c,sizeBytes:c.type instanceof I&&c.type.isFullyStatic?c.type.sizeInBytes():0}))}return t}_topologicalSort(e){let t=new Set(e),r=new Map,s=new Map;for(let u of e)r.set(u,0),s.set(u,[]);for(let u of e)for(let p=0;p<u.numOperands;p++){let c=u.getOperand(p).definingOp;c&&t.has(c)&&(s.get(c).push(u),r.set(u,r.get(u)+1))}let o=[];for(let u of e)r.get(u)===0&&o.push(u);let i=[],a=0;for(;a<o.length;){let u=o[a++];i.push(u);for(let p of s.get(u)){let c=r.get(p)-1;r.set(p,c),c===0&&o.push(p)}}return i}}});var sw,ow,G2=M(()=>{"use strict";fn();yr();Us();V2();Ad();nn();sw=class extends qe{static{l(this,"GraphPartitionPass")}constructor(e={}){super("GraphPartitionPass"),this.partitionerConfig=new Pc(e),this.partitionResult=null}run(e,t){if(this.partitionerConfig.targets.length<2)return oe.UNCHANGED;let r=new rw(this.partitionerConfig);return this.partitionResult=r.partition(e),this.partitionResult.numPartitions<=1?oe.UNCHANGED:(this._annotateOps(),this._insertTransferOps(e),this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,numPartitions:this.partitionResult.numPartitions,transferEdges:this.partitionResult.transferEdges.length,level:le.DEBUG}),e.bumpVersion(),oe.CHANGED)}_annotateOps(){for(let e of this.partitionResult.partitions)for(let t of e.ops)t.setAttr("partition_id",e.id),t.setAttr("partition_target",e.target.name)}_insertTransferOps(e){let t=e.entryBlock,r=this._buildOrderIndex(t),{useMap:s,firstInPart:o}=this._buildInsertionIndex(t);for(let i of this.partitionResult.transferEdges){let a=i.value,u=i.src.target.name,p=i.dst.target.name;if(u===p)continue;let c=new Me("copy_to_device",[a],[a.type],{src_device:u,dst_device:p});c.setAttr("partition_id",i.dst.id),c.setAttr("partition_target",p);let f=s.get(i.dst),d=f&&f.get(a)||o.get(i.dst)||null,h=a.definingOp;if(d&&h&&r.has(h)&&r.has(d)&&r.get(h)>=r.get(d))t.insertAfter(c,h);else if(d)t.insertBefore(c,d);else if(h&&r.has(h))t.insertAfter(c,h);else{let g=e.getReturnOp();g?t.insertBefore(c,g):t.pushOp(c)}let m=c.getResult(0);for(let g of i.dst.ops)for(let y=0;y<g.numOperands;y++)g.getOperand(y)===a&&g.replaceOperand(y,m)}}_buildInsertionIndex(e){let t=[...new Set(this.partitionResult.transferEdges.map(o=>o.dst))],r=new Map,s=new Map;for(let o of t)r.set(o,new Map);for(let o of e.ops())for(let i of t){if(!i.hasOp(o))continue;s.has(i)||s.set(i,o);let a=r.get(i);for(let u=0;u<o.numOperands;u++){let p=o.getOperand(u);a.has(p)||a.set(p,o)}}return{useMap:r,firstInPart:s}}_buildOrderIndex(e){let t=new Map,r=0;for(let s of e.ops())t.set(s,r++);return t}},ow=class extends qe{static{l(this,"PartitionMaterializationPass")}constructor(e={}){super("PartitionMaterializationPass"),this.targets=e.targets||[]}run(e,t){let r=this._collectPartitions(e);if(r.size<=1)return oe.UNCHANGED;let s=this._getModule(e);if(!s)return oe.UNCHANGED;let o=this._materializePartitions(e,r);for(let i of o)s.addFunction(i);return this._rewriteOriginalFunction(e,o,r),this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,partitions:r.size,subFunctions:o.length,level:le.DEBUG}),e.bumpVersion(),oe.CHANGED}_collectPartitions(e){let t=new Map;for(let r of e.ops()){let s=r.getAttr("partition_id");s!==void 0&&(t.has(s)||t.set(s,{id:s,target:r.getAttr("partition_target"),ops:[]}),t.get(s).ops.push(r))}return t}_getModule(e){return e._module||null}_materializePartitions(e,t){let r=[];for(let[s,o]of t){let i=new Set(o.ops),{inputs:a,outputs:u}=pa(i,o.ops),p=a.map(_=>_.type),c=u.map(_=>_.type),f=`${e.name}_partition_${s}`,d=new yn(f,p,c);d._partitionTarget=o.target;let h=new Map;for(let _=0;_<a.length;_++)h.set(a[_],d.args[_]);let m=Ru(o.ops);for(let _ of m)d.entryBlock.pushOp(_.clone(h));let g=u.map(_=>h.get(_)||_),y=new Me("return",g,[]);d.entryBlock.pushOp(y),r.push(d)}return r}_rewriteOriginalFunction(e,t,r){for(let s of t)e.setAttr?.(`sub_${s.name}`,s._partitionTarget)}}});function Ud(n){return ap(n,-1)}function hj(n,e,t){let r=t.sharedMemoryBytes||16384,s=0;for(let u of e.entryBlock.ops())if(u.opName!=="yield")for(let p=0;p<u.numResults;p++){let c=u.getResult(p).type;if(!c||!c.shape)continue;let f=Ud(c.shape);if(f<0)return!0;let d=f*Rn(c.dtype);d>s&&(s=d)}let o=n.getAttr("num_carry"),i=n.getAttr("num_xs"),a=0;for(let u=0;u<o;u++){let p=n.getOperand(i+u).type,c=p&&p.shape?Ud(p.shape):-1;c>0&&(a+=c*Rn(p.dtype))}return 3*s+2*a>r}function mj(n,e){let r=n.regions[0].entryBlock,s=n.getAttr("num_carry"),o=[],i=null;for(let b of r.ops()){if(b.opName==="yield"){i=b;continue}o.push(b)}if(!i)return null;let a=new Set(o),u=new Set(r.arguments),p=[],c=new Set,f=[],d=new Set;for(let b of o)for(let w=0;w<b.numOperands;w++){let x=b.getOperand(w),S=x.definingOp;if(!u.has(x)&&!(S&&a.has(S))){if(S&&$o(S)){d.has(S)||(d.add(S),f.push(S));continue}c.has(x)||(c.add(x),p.push(x))}}let h=[...r.arguments,...p];for(let b of h)if(!b.type||!b.type.isFullyStatic)return null;let m=[];for(let b=0;b<i.numOperands;b++)m.push(i.getOperand(b));let g=new yn(e,h.map(b=>b.type),m.map(b=>b.type)),y=new Map;for(let b=0;b<h.length;b++)y.set(h[b],g.args[b]);for(let b of f)g.entryBlock.pushOp(b.clone(y));for(let b of o)g.entryBlock.pushOp(b.clone(y));let _=m.map(b=>y.get(b));return _.some(b=>b===void 0)?null:(g.entryBlock.pushOp(new Me("return",_,[])),{bodyFunc:g,captured:p,numCarry:s,numYs:m.length-s})}function gj(n,e,t,r,s){let{getSlot:o,newSlot:i,steps:a,addedFuncs:u}=r,p=new Mn(n.name+"_mod");p.addFunction(n);let c=ha(p,s);if(c){let f=c.plan,d=e.length,h=new Map,m=!0;for(let g of f.returnFixups||[]){if(g.kind!=="copy"){m=!1;break}h.set(g.pos,g.srcSlot)}if(m){let g=l(x=>{let S=d+x;return h.has(S)?h.get(S):f.argSlots[S]},"retSlot"),y=new Map;for(let x=0;x<d;x++)y.set(f.argSlots[x],o(e[x]));for(let x of f.intermediates)y.set(x.slot,i(x.shape,x.dtype));for(let x=0;x<t.length;x++){let S=g(x);y.has(S)||y.set(S,o(t[x]))}let _=l(x=>{let S=y.get(x);return S===void 0?null:S},"mapSlot"),b=[],w=!0;for(let x of f.steps){let S=x.inputSlots.map(_),T=x.outputSlots.map(_);if(S.includes(null)||T.includes(null)){w=!1;break}b.push({name:x.name,inputSlots:S,outputSlots:T})}if(w){for(let x of b)a.push(x);for(let x of p.functions())u.push(x);return!0}}}return a.push({name:n.name,inputSlots:e.map(o),outputSlots:t.map(o)}),u.push(n),!0}function j2(n,e,t){let r=db({ops:n,opSet:new Set(n)},e,new Map);return r?gj(r.subFunc,r.inputs,r.outputs,t,1):!1}function yj(n,e,t){let{getSlot:r,newSlot:s,steps:o,scanLoops:i,addedFuncs:a}=t,u=n.regions[0];if(!u||!u.entryBlock)return!1;let p=mj(n,e);if(!p)return!1;let{bodyFunc:c,captured:f,numCarry:d,numYs:h}=p,m=n.getAttr("num_xs"),g=new Mn(e+"_mod");g.addFunction(c);let y=ha(g,2),_=y?y.plan:null,b=[],w=[];for(let D=0;D<d;D++){let V=n.getOperand(m+D).type;b.push(V.shape),w.push(V.dtype)}let x=[],S=[];for(let D=0;D<m;D++){let V=n.getOperand(D).type;x.push(V.shape.slice(1)),S.push(V.dtype)}let T=[],v=[];for(let D=0;D<h;D++){let V=n.getResult(d+D).type;T.push(V.shape.slice(1)),v.push(V.dtype)}let A=b.map((D,V)=>s(D,w[V])),N=b.map((D,V)=>s(D,w[V])),k=x.map((D,V)=>s(D,S[V])),O=[],R=[],B=[],E=[];for(let D=0;D<d;D++)R.push(r(n.getOperand(m+D)));for(let D=0;D<d;D++)B.push(r(n.getResult(D)));for(let D of f)E.push(r(D));let F=[],C=[];for(let D=0;D<m;D++)F.push(r(n.getOperand(D)));for(let D=0;D<h;D++)C.push(r(n.getResult(d+D)));let H=o.length;if(!_)O=T.map((D,V)=>s(D,v[V])),o.push({name:c.name,inputSlots:[...k,...A,...E],outputSlots:[...N,...O]}),a.push(c);else{let D=m+d+f.length,V=new Map;for(let j of _.returnFixups||[]){if(j.kind!=="copy")return!1;V.set(j.pos,j.srcSlot)}let ue=l(j=>{let W=D+j;return V.has(W)?V.get(W):_.argSlots[W]},"bodyReturnSlot"),ce=new Map;for(let j=0;j<m;j++)ce.set(_.argSlots[j],k[j]);for(let j=0;j<d;j++)ce.set(_.argSlots[m+j],A[j]);for(let j=0;j<f.length;j++)ce.set(_.argSlots[m+d+j],E[j]);for(let j of _.intermediates)ce.set(j.slot,s(j.shape,j.dtype));for(let j=0;j<d;j++){let W=ue(j);ce.has(W)||ce.set(W,N[j])}for(let j=0;j<h;j++){let W=ue(d+j),ie=ce.get(W);ie===void 0&&(ie=s(T[j],v[j]),ce.set(W,ie)),O.push(ie)}let Le=l(j=>{let W=ce.get(j);return W===void 0?null:W},"mapSlot");for(let j of _.steps){let W=j.inputSlots.map(Le),ie=j.outputSlots.map(Le);if(W.includes(null)||ie.includes(null))return!1;o.push({name:j.name,inputSlots:W,outputSlots:ie})}for(let j of g.functions())a.push(j)}let G=o.length,U=n.getOperand(0).type.shape[0];return typeof U!="number"||U<0?!1:(i.push({T:U,loopStart:H,loopEnd:G,carry:b.map((D,V)=>({a:A[V],b:N[V],initSlot:R[V],finalSlot:B[V],bytes:Ud(D)*Rn(w[V])})),xs:k.map((D,V)=>({xtSlot:D,xsSlot:F[V],stepBytes:Ud(x[V])*Rn(S[V])})),ys:O.map((D,V)=>({ytSlot:D,ysSlot:C[V],stepBytes:Ud(T[V])*Rn(v[V])}))}),!0)}function ok(n,e,t=!1){if(!e||typeof e.isWebGPU!="function"||!e.isWebGPU()||n.functionCount!==1)return null;let r=n.functions().next().value,s=r.getReturnOp();if(!s)return null;let o=[];for(let R of r.ops())R.opName==="scan"&&o.push(R);if(o.length===0)return null;let i=new Set,a=l(R=>{if(!(!R||i.has(R))){i.add(R);for(let B=0;B<R.numOperands;B++)a(R.getOperand(B).definingOp)}},"visit");for(let R=0;R<s.numOperands;R++)a(s.getOperand(R).definingOp);for(let R of o)if(!i.has(R))return null;if(!t){let R=!1;for(let B of o){let E=B.regions[0];if(E&&E.entryBlock&&hj(B,E,e)){R=!0;break}}if(!R)return null}let u=[];for(let R of i)$o(R)||Xt(R.opName)||u.push(R);let p=Ru(u),c=new Map,f=0,d=l(R=>{let B=c.get(R);return B===void 0&&(B=f++,c.set(R,B)),B},"getSlot");for(let R of r.args)d(R);let h=[],m=l((R,B)=>{let E=f++;return h.push({slot:E,shape:[...R],dtype:B}),E},"newSlot"),g=[],y=[],_=[],b={getSlot:d,newSlot:m,steps:g,scanLoops:y,addedFuncs:_},w=new Set(o),x=[],S=0,T=0;for(let R of p)if(w.has(R)){if(x.length&&!j2(x,r.name+"_seg"+S++,b)||(x=[],!yj(R,r.name+"_scan"+T++,b)))return null}else x.push(R);if(x.length&&!j2(x,r.name+"_seg"+S++,b))return null;let v=r.args.map(d),A=[],N=new Set;for(let R=0;R<s.numOperands;R++){let B=s.getOperand(R),E=v.length;if(c.has(B)&&!(B.isBlockArgument&&B.isBlockArgument())){let F=d(B);N.has(F)?(v.push(f++),A.push({pos:E,kind:"copy",srcSlot:F})):(N.add(F),v.push(F))}else if(B.isBlockArgument&&B.isBlockArgument())v.push(f++),A.push({pos:E,kind:"copy",srcSlot:d(B)});else return null}let k=new Set(v),O=new Set;for(let[R,B]of c)if(!(k.has(B)||O.has(B))){if(O.add(B),!R.type||!R.type.isFullyStatic)return null;h.some(E=>E.slot===B)||h.push({slot:B,shape:[...R.type.shape],dtype:R.type.dtype})}n.removeFunction(r.name);for(let R of _)n.addFunction(R);return{plan:{numSlots:f,argSlots:v,intermediates:h,steps:g,returnFixups:A,scanLoops:y}}}var U2=M(()=>{"use strict";yr();Us();Di();hb();Ir();tr();Re();l(Ud,"numel");l(hj,"isScanOversized");l(mj,"buildScanBodyFunction");l(gj,"inlineNativeSplit");l(j2,"emitSegment");l(yj,"emitScanLoop");l(ok,"splitGraphForScan")});function qd(n){ik.push(n),ik.sort((e,t)=>(e.priority??100)-(t.priority??100))}function _j(n){for(let e of ik)if(e.applies(n))return e;return null}function q2(n,e){let t=_j(e);return t?t.run(n,e):null}var ik,W2=M(()=>{"use strict";hb();U2();ik=[];l(qd,"registerGraphSplitStrategy");l(_j,"selectGraphSplitStrategy");l(q2,"splitGraph");qd({name:"cublas",priority:10,applies:l(n=>n.config.matmulBackend==="cublas","applies"),run:l(n=>uL(n),"run")});qd({name:"cuda-attention",priority:15,applies:l(n=>n.cudaAttention,"applies"),run:l(n=>ha(n,1),"run")});qd({name:"cuda-native-matmul-chain",priority:20,applies:l(n=>n.cudaMatmulChain,"applies"),run:l(n=>ha(n),"run")});qd({name:"cuda-native-conv-chain",priority:25,applies:l(n=>n.cudaConvChain,"applies"),run:l(n=>ha(n),"run")});qd({name:"webgpu",priority:30,applies:l(n=>n.isWebGPU,"applies"),run:l((n,e)=>{let t=ok(n,e.target);return!t&&iL(n,e.target.maxThreadsPerBlock||256)&&(t=ok(n,e.target,!0),t||(t=ha(n,2))),t},"run")})});function K2(n,e){let t=new Om;for(let[,r]of n.bufferMap)t.paramBuffers.add(r.name);return bj(n.body,t,e),Tj(n,t,e),xj(n.body,t),t}function bj(n,e,t){let r=[n];for(;r.length>0;){let s=r.pop();if(!(!s||typeof s!="object")){switch(s.type){case"ForNode":if(s.loopVar&&e.locals.set(s.loopVar.name,"i32"),s.kind===z.THREAD_BINDING&&s.threadTag){let o=s.extent&&s.extent.type==="IntImmNode"?s.extent.value:0,i=!s.extent||s.extent.type!=="IntImmNode",a={varName:s.loopVar.name,extent:o,isDynamic:i,extentNode:s.extent};e.threadBindings.has(s.threadTag)?e.threadBindings.get(s.threadTag).push(a):e.threadBindings.set(s.threadTag,[a])}break;case"LetStmtNode":s.variable&&e.locals.set(s.variable.name,Vi(s.variable.dtype));break;case"BufferStoreNode":case"BufferLoadNode":s.buffer&&e.usedBuffers.set(s.buffer.name,s.buffer);break;case"AllocateNode":s.buffer&&e.allocatedBuffers.add(s.buffer.name),s.scope==="shared"&&s.buffer&&e.sharedBuffers.push(s.buffer);break;case"CallExternNode":s.externName&&!n1(s.externName)&&e.externCalls.set(s.externName,{argCount:s.args?s.args.length:0,dtype:s.dtype||"f32"});break;case"BlockNode":if(s.reads)for(let o of s.reads)o.buffer&&e.usedBuffers.set(o.buffer.name,o.buffer);if(s.writes)for(let o of s.writes)o.buffer&&e.usedBuffers.set(o.buffer.name,o.buffer);if(s.iterVars)for(let o of s.iterVars)o.iterVar&&e.locals.set(o.iterVar.name,Vi(o.iterVar.dtype)),o.binding&&typeof o.binding=="object"&&o.binding.type&&r.push(o.binding);break}if(s.body&&r.push(s.body),s.value&&typeof s.value=="object"&&s.value.type&&r.push(s.value),s.stmts)for(let o of s.stmts)r.push(o);if(s.thenBody&&r.push(s.thenBody),s.elseBody&&r.push(s.elseBody),s.initBody&&r.push(s.initBody),s.condBody&&r.push(s.condBody),s.loopBody&&r.push(s.loopBody),s.condition&&typeof s.condition=="object"&&s.condition.type&&r.push(s.condition),s.a&&typeof s.a=="object"&&s.a.type&&r.push(s.a),s.b&&typeof s.b=="object"&&s.b.type&&r.push(s.b),s.expr&&typeof s.expr=="object"&&s.expr.type&&r.push(s.expr),s.args)for(let o of s.args)typeof o=="object"&&o!==null&&o.type&&r.push(o);if(s.indices)for(let o of s.indices)typeof o=="object"&&o!==null&&o.type&&r.push(o)}}}function Tj(n,e,t){let r=e.memoryLayout.alignment,s=0,o=l(i=>{let a=i.shape.some(c=>typeof c!="number"||c<0),u=i.numel();if(!a&&u>=0)return u*Rn(i.dtype);let p=1;for(let c of i.shape)p*=typeof c=="number"&&c>0?c:1;return Math.max(wj,p*Rn(i.dtype))},"bufBytes");for(let[,i]of n.bufferMap)s=Math.ceil(s/r)*r,e.memoryLayout.bufferOffsets.set(i.name,s),s+=o(i);for(let[i,a]of e.usedBuffers)e.memoryLayout.bufferOffsets.has(i)||(s=Math.ceil(s/r)*r,e.memoryLayout.bufferOffsets.set(i,s),s+=o(a));e.memoryLayout.totalBytes=s}function xj(n,e){let t=new Map;for(let r of yl(n,s=>s.type==="BufferStoreNode"&&s.buffer&&!e.paramBuffers.has(s.buffer.name))){let s=r.buffer.name;t.has(s)||t.set(s,[]),t.get(s).push(r.value)}for(let[r,s]of t)if(s.every(i=>i&&i.type==="IntImmNode"&&i.value===0||i&&i.type==="FloatImmNode"&&i.value===0)&&e.zeroBuffers.add(r),s.length===1&&s[0]){let i=s[0];(i.type==="IntImmNode"||i.type==="FloatImmNode")&&e.constantBuffers.set(r,i.value)}}var wj,H2=M(()=>{"use strict";vp();tr();Fe();ts();l(K2,"scanMetadata");l(bj,"walkTree");wj=65536;l(Tj,"computeMemoryLayout");l(xj,"detectZeroBuffers")});function Wd(n,e,t){let r=typeof n.offset=="number"?n.offset:0;if(e.length===0)return new L(r);if(e.length===1)return r===0?e[0]:new P("+",e[0],new L(r));let s=[];r!==0&&s.push(new L(r));for(let o=0;o<e.length;o++){let i=e[o];if(i.type==="IntImmNode"&&i.value===0)continue;let a=n.strides[o];if(a===1)s.push(i);else if(typeof a=="number"&&a>=0)s.push(new P("*",i,new L(a)));else{let u=Sj(n,o,t);s.push(new P("*",i,u))}}return s.length===0?new L(0):s.reduce((o,i)=>new P("+",o,i))}function Sj(n,e,t){let r=[];for(let s=e+1;s<n.shape.length;s++){let o=n.shape[s];typeof o=="number"&&o>=0?r.push(new L(o)):r.push(vj(n,s,t))}return r.length===0?new L(1):r.reduce((s,o)=>new P("*",s,o))}function vj(n,e,t){let r=n.shape[e];if(r instanceof Pe)return ll(r,s=>new Oe(qu(s),"index"));if(t){let s=`${n.name}:${e}`,o=t.get(s);if(o)return new Oe(o.name,"index")}return new L(1)}var Y2=M(()=>{"use strict";Fe();po();M0();l(Wd,"flattenIndex");l(Sj,"computeDynamicStride");l(vj,"resolveShapeParam")});function J2(n,e){let t=K2(n,e),r={shapeParamMap:n.shapeParamMap,accCounter:0,metadata:t},s=Bs(n.body,r);return new Nm(n.name,n.params,s,n.bufferMap,n.shapeParams,n.shapeParamMap,t)}function Bs(n,e){if(!n||typeof n!="object")return n;switch(n.type){case"ForNode":return kj(n,e);case"BlockNode":return Nj(n,e);case"SeqNode":return Aj(n,e);case"BufferStoreNode":return Oj(n,e);case"LetStmtNode":return Bj(n,e);case"AllocateNode":return Rj(n,e);case"IfThenElseNode":return Mj(n,e);case"WhileNode":return Ij(n,e);case"EvaluateNode":return new il(Nr(n.value,e));case"SyncThreadsNode":return n;default:return n}}function kj(n,e){let t=n.accumulator!==void 0?n.accumulator:Qb(n);if(t)return Ej(n,t,e);let r=Bs(n.body,e);return new he(n.loopVar,n.min,n.extent,n.kind,r,n.threadTag)}function Nj(n,e){let t=[];for(let o of n.iterVars)o.iterVar&&o.binding&&t.push({name:o.iterVar.name,dtype:o.iterVar.dtype,expr:Nr(o.binding,e)});let r=Bs(n.body,e),s=n.initBody?Bs(n.initBody,e):null;return t.length>0?s?(r=new te([new gl(t,s),new gl(t,r)]),s=null):r=new gl(t,r):s&&(r=new te([s,r])),r}function Aj(n,e){let t=[];for(let r of n.stmts)t.push(Bs(r,e));return new te(t)}function Oj(n,e){let t=Wd(n.buffer,n.indices,e.shapeParamMap),r=Nr(n.value,e),s=n.buffer.dtype||br(n.value);return new Sp(n.buffer,t,r,s)}function Bj(n,e){return new et(n.variable,Nr(n.value,e),Bs(n.body,e))}function Rj(n,e){return new Ue(n.buffer,n.scope,Bs(n.body,e))}function Mj(n,e){return new Se(Nr(n.condition,e),Bs(n.thenBody,e),n.elseBody?Bs(n.elseBody,e):null)}function Ij(n,e){return new $i(n.condVar,Bs(n.condBody,e),Bs(n.loopBody,e))}function Nr(n,e){if(!n||typeof n!="object"||!n.type)return n;switch(n.type){case"BufferLoadNode":{let t=Wd(n.buffer,n.indices,e.shapeParamMap),r=new xp(n.buffer,t,n.buffer.dtype);return si(r),r}case"MathOpNode":{let t=Nr(n.a,e),r=n.b?Nr(n.b,e):null,s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),si(s),s}case"CompareNode":{let t=Nr(n.a,e),r=Nr(n.b,e),s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),si(s),s}case"CastNode":{let t=Nr(n.expr,e),r={...n,expr:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),si(r),r}case"CallExternNode":{let t=n.args.map(s=>Nr(s,e)),r={...n,args:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),si(r),r}case"IfThenElseNode":{let t=Nr(n.condition,e),r=Nr(n.thenBody,e),s=n.elseBody?Nr(n.elseBody,e):null,o={...n,condition:t,thenBody:r,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),si(o),o}default:return si(n),n}}function Ej(n,e,t){let r=`_acc_${t.accCounter++}`,s=br(e.loadSide);t.metadata.locals.set(r,s);let o=new Map;for(let h of e.block.iterVars)h.iterVar&&h.binding&&o.set(h.iterVar.name,h.binding);let i=o.size>0?zr(e.valueSide,o):e.valueSide,a=Wd(e.store.buffer,e.outerIndices,t.shapeParamMap),u=new xp(e.store.buffer,a,e.store.buffer.dtype);si(u);let p=Nr(i,t),c=Wd(e.store.buffer,e.outerIndices,t.shapeParamMap),f=new Sp(e.store.buffer,c,null,e.store.buffer.dtype),d=e.block.initBody?o.size>0?Kd(e.block.initBody,o):e.block.initBody:null;return new Am({localName:r,dtype:s,op:e.op,initLoad:u,loopVar:n.loopVar,extent:n.extent,loopKind:n.kind,body:p,flushStore:f,initBody:d?Bs(d,t):null})}function zr(n,e){if(!n||typeof n!="object"||!n.type)return n;if(n.type==="VariableNode"&&e.has(n.name))return e.get(n.name);if(n.type==="BufferLoadNode"){let t=n.indices.map(o=>zr(o,e));if(!t.some((o,i)=>o!==n.indices[i]))return n;let s={...n,indices:t};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="MathOpNode"){let t=zr(n.a,e),r=n.b?zr(n.b,e):null;if(t===n.a&&r===n.b)return n;let s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="CompareNode"){let t=zr(n.a,e),r=zr(n.b,e);if(t===n.a&&r===n.b)return n;let s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="CastNode"){let t=zr(n.expr,e);if(t===n.expr)return n;let r={...n,expr:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}if(n.type==="CallExternNode"){let t=n.args.map(o=>zr(o,e));if(!t.some((o,i)=>o!==n.args[i]))return n;let s={...n,args:t};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="IfThenElseNode"){let t=zr(n.condition,e),r=zr(n.thenBody,e),s=n.elseBody?zr(n.elseBody,e):null;if(t===n.condition&&r===n.thenBody&&s===n.elseBody)return n;let o={...n,condition:t,thenBody:r,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),o}return n}function Kd(n,e){if(!n||typeof n!="object"||!n.type)return n;switch(n.type){case"BufferStoreNode":{let t=n.indices.map(o=>zr(o,e)),r=zr(n.value,e);if(t.every((o,i)=>o===n.indices[i])&&r===n.value)return n;let s={...n,indices:t,value:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}case"SeqNode":{let t=n.stmts.map(s=>Kd(s,e)),r={...n,stmts:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}case"ForNode":{let t=Kd(n.body,e);if(t===n.body)return n;let r={...n,body:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}case"IfThenElseNode":{let t=zr(n.condition,e),r=Kd(n.thenBody,e),s=n.elseBody?Kd(n.elseBody,e):null,o={...n,condition:t,thenBody:r,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),o}default:return n}}var Z2=M(()=>{"use strict";vp();H2();Y2();Fe();Jv();l(J2,"lowerToLIR");l(Bs,"lowerStmt");l(kj,"lowerForNode");l(Nj,"lowerBlockNode");l(Aj,"lowerSeqNode");l(Oj,"lowerBufferStore");l(Bj,"lowerLetStmt");l(Rj,"lowerAllocate");l(Mj,"lowerIfThenElse");l(Ij,"lowerWhile");l(Nr,"lowerExpr");l(Ej,"lowerAccumulator");l(zr,"substituteVars");l(Kd,"substituteVarsStmt")});function Q2(n){let e=[],t={errors:e,boundVars:new Set,path:[],bufferNames:new Set};if(!n||n.type!=="LIRFunc")return e.push(new Go("root must be LIRFunc")),e;for(let[,r]of n.bufferMap)t.bufferNames.add(r.name);for(let r of n.shapeParams)t.boundVars.add(r.name);if(n.metadata)for(let[r]of n.metadata.locals)t.boundVars.add(r);return Rs(n.body,t),e}function Rs(n,e){if(!(!n||typeof n!="object")){switch(e.path.push(n.type),n.type){case"ForNode":Lj(n,e);break;case"SeqNode":for(let t of n.stmts)Rs(t,e);break;case"LIRFlatStoreNode":Cj(n,e);break;case"LIRAccumulatorNode":Dj(n,e);break;case"LIRBindingsNode":Pj(n,e);break;case"LetStmtNode":{Pn(n.value,e);let t=e.boundVars.has(n.variable.name);e.boundVars.add(n.variable.name),Rs(n.body,e),t||e.boundVars.delete(n.variable.name);break}case"AllocateNode":{let t=n.buffer?e.bufferNames.has(n.buffer.name):!0;n.buffer&&e.bufferNames.add(n.buffer.name),Rs(n.body,e),n.buffer&&!t&&e.bufferNames.delete(n.buffer.name);break}case"IfThenElseNode":Pn(n.condition,e),Rs(n.thenBody,e),n.elseBody&&Rs(n.elseBody,e);break;case"WhileNode":Rs(n.condBody,e),Rs(n.loopBody,e);break;case"EvaluateNode":Pn(n.value,e);break;default:break}e.path.pop()}}function Lj(n,e){n.extent?Pn(n.extent,e):e.errors.push(new Go("ForNode missing extent",[...e.path]));let t=!0;n.loopVar?(t=e.boundVars.has(n.loopVar.name),e.boundVars.add(n.loopVar.name)):e.errors.push(new Go("ForNode missing loopVar",[...e.path])),Rs(n.body,e),n.loopVar&&!t&&e.boundVars.delete(n.loopVar.name)}function Cj(n,e){n.buffer||e.errors.push(new Go("LIRFlatStoreNode missing buffer",[...e.path])),Pn(n.offsetExpr,e),Pn(n.value,e)}function Dj(n,e){n.localName||e.errors.push(new Go("LIRAccumulatorNode missing localName",[...e.path])),n.dtype||e.errors.push(new Go("LIRAccumulatorNode missing dtype",[...e.path]));let t=n.localName?e.boundVars.has(n.localName):!0;n.localName&&e.boundVars.add(n.localName),Pn(n.initLoad,e),Pn(n.body,e),Rs(n.flushStore,e),n.initBody&&Rs(n.initBody,e),n.localName&&!t&&e.boundVars.delete(n.localName)}function Pj(n,e){let t=[];for(let r of n.bindings)Pn(r.expr,e),e.boundVars.has(r.name)||t.push(r.name),e.boundVars.add(r.name);Rs(n.body,e);for(let r of t)e.boundVars.delete(r)}function Pn(n,e){if(!(!n||typeof n!="object"||!n.type))switch(n.type){case"LIRFlatLoadNode":n.buffer||e.errors.push(new Go("LIRFlatLoadNode missing buffer",[...e.path])),Pn(n.offsetExpr,e);break;case"MathOpNode":Pn(n.a,e),n.b&&Pn(n.b,e);break;case"CompareNode":Pn(n.a,e),Pn(n.b,e);break;case"CastNode":Pn(n.expr,e);break;case"CallExternNode":if(n.args)for(let t of n.args)Pn(t,e);break;case"IfThenElseNode":Pn(n.condition,e),Pn(n.thenBody,e),n.elseBody&&Pn(n.elseBody,e);break;case"VariableNode":n.name!==void 0&&!e.boundVars.has(n.name)&&e.errors.push(new Go(`unbound variable '${n.name}'`,[...e.path]));break;case"IntImmNode":case"FloatImmNode":break;default:break}}var Go,X2=M(()=>{"use strict";Go=class{static{l(this,"LIRVerificationError")}constructor(e,t){this.message=e,this.nodePath=t||[]}toString(){let e=this.nodePath.length>0?` at ${this.nodePath.join(" > ")}`:"";return`LIR verification: ${this.message}${e}`}};l(Q2,"verifyLIR");l(Rs,"verifyStmt");l(Lj,"verifyForNode");l(Cj,"verifyFlatStore");l(Dj,"verifyAccumulator");l(Pj,"verifyBindings");l(Pn,"verifyExpr")});function eC(n,e,t={}){return new Si({target:e,...t}).compileFunction(n)}var iw,ak,Si,aw=M(()=>{"use strict";Di();Us();OE();by();BE();hL();L2();$a();Ep();dy();C2();$2();tk();z2();G2();W2();Cv();nn();RS();nv();Z2();X2();nn();iw=class{static{l(this,"CompilerConfig")}constructor(e={}){this.target=e.target,this.verify=e.verify!==!1,this.verifyMode=e.verify==="full"?"full":"normal",this.errorMode=e.errorMode||"strict";let t=this.target&&typeof this.target.isWebGPU=="function"&&this.target.isWebGPU(),s=this.target&&typeof this.target.isGPU=="function"&&this.target.isGPU()&&!t;this.fusion={enabled:!0,strategy:"priority",epilogue:void 0,...e.fusion},this.scheduling={enabled:t,autotune:!1,gpuTiling:s,...e.scheduling},this.matmulBackend=e.matmulBackend||"native",this.quantization={enabled:!1,...e.quantization},this.optimization={layout:!1,rematerialization:!1,rematConfig:{},fastMath:!1,maxSimplifyIterations:8,loopPartition:!1,detectAccumulators:!1,tensorize:!1,...e.optimization},this.memory={alignment:64,inplaceReuse:!0,allocStrategy:"best-fit",poolAllocation:!1,...e.memory},this.partition={enabled:!1,targets:[],defaultTarget:null,opTargetOverrides:new Map,memoryLimits:new Map,minPartitionSize:1,costWeights:{},...e.partition},this.passContext=e.passContext||null,this.loweringRules=e.loweringRules||null,this.codegenEntries=e.codegenEntries||null;let o=e.trace||{};this.trace={level:o.level??le.SILENT,sink:o.sink??null,irSnapshot:{afterGraphPasses:!1,afterLowering:!1,afterScheduling:!1,...o.irSnapshot||{}}}}get usePartition(){return this.partition.enabled&&this.partition.targets.length>=2}},ak=class{static{l(this,"CompilationResult")}constructor(e,t,r){this.module=e,this.trace=t,this.errors=r||[]}get succeeded(){return this.errors.length===0}get failedFunctions(){let e=new Set;for(let t of this.errors)t.funcName&&e.add(t.funcName);return e}run(e,...t){return this.module.run(e,...t)}async runAsync(e,...t){return this.module.runAsync(e,...t)}isAsync(e){return this.module.isAsync(e)}getSource(e){return this.module.getKernelSource(e)}getSnippet(e){return this.module.getKernelSnippet(e)}listKernels(){return this.module.listKernels()}},Si=class n{static{l(this,"Compiler")}constructor(e){if(this.config=e instanceof iw?e:new iw(e),!this.config.target)throw new Error("Compiler requires a target");this.context=new V_({loweringRules:this.config.loweringRules,codegenEntries:this.config.codegenEntries})}compile(e){let t=new _y(this.config.trace),r=this.config.errorMode==="resilient",s=[],o=new Set,i=performance.now();t.phaseStart("compile");let a={compiler:this,trace:t,errors:s,failed:o,resilient:r,original:e,working:r?wA(e):e,cudaMatmulChain:!1,split:null,primFuncs:null,lirFuncs:null,runtimeModule:null};for(let u of this._compilePhases())u.when&&!u.when(a)||u.run(a);if(t.phaseEnd("compile",performance.now()-i),!r&&s.length>0)throw new Error(s[0].toString());return new ak(a.runtimeModule,t,s)}_compilePhases(){return[{name:"verify:pre",when:l(e=>e.compiler.config.verify,"when"),run:l(e=>e.compiler._verifyGraph(e.working,"before graph passes",e.trace,e.errors,e.failed,e.resilient),"run")},{name:"calibrate",when:l(e=>{let t=e.compiler.config.quantization;return t.enabled&&t.calibrationData&&!t.calibration},"when"),run:l(e=>e.compiler._runCalibration(e.working,e.trace),"run")},{name:"graphPasses",run:l(e=>{e.cudaMatmulChain=e.compiler._runGraphPasses(e.working,e.original,e.trace,e.errors,e.failed,e.resilient)},"run")},{name:"partition",when:l(e=>e.compiler.config.usePartition,"when"),run:l(e=>e.compiler._runPartitioning(e.working,e.trace),"run")},{name:"split",run:l(e=>{let t=e.compiler.config,r=typeof t.target.isWebGPU=="function"&&t.target.isWebGPU(),s=typeof t.target.isGPU=="function"&&t.target.isGPU()&&!r,o=0,i=0;for(let p of e.working)for(let c of p.ops())c.opName==="conv"||c.opName==="quantized_conv"?o++:c.opName==="scaled_dot_product_attention"&&i++;let a=s&&o>=2,u=s&&i>0;e.split=q2(e.working,{config:t,target:t.target,cudaMatmulChain:e.cudaMatmulChain,cudaConvChain:a,cudaAttention:u,isWebGPU:r})},"run")},{name:"verify:post",when:l(e=>e.compiler.config.verify,"when"),run:l(e=>e.compiler._verifyGraph(e.working,"after graph passes",e.trace,e.errors,e.failed,e.resilient),"run")},{name:"lowering",run:l(e=>{if(e.primFuncs=e.compiler._lowerAll(e.working,e.trace,e.errors,e.failed,e.resilient),e.compiler.config.matmulBackend==="cublas")for(let t of e.primFuncs)t.cublasInfo=e.split&&e.split.cublasInfos?e.split.cublasInfos.get(t.name)||null:P1(t)},"run")},{name:"tirPasses",run:l(e=>e.compiler._runTirPasses(e),"run")},{name:"verify:tensor",when:l(e=>e.compiler.config.verify,"when"),run:l(e=>e.compiler._verifyAll(e.primFuncs,e.errors,e.failed,e.resilient),"run")},{name:"lirLowering",run:l(e=>{e.lirFuncs=e.compiler._lowerToLIR(e.primFuncs,e.trace,e.errors,e.failed,e.resilient)},"run")},{name:"codegen",run:l(e=>{e.runtimeModule=e.compiler._codegen(e.lirFuncs,e.trace,e.errors,e.failed,e.resilient),e.split&&(e.runtimeModule.executionPlan=e.split.plan)},"run")}]}compileFunction(e){let t=new Mn("single");return t.addFunction(e),this.compile(t)}calibrate(e,t="minmax"){let r=new Dc(t);for(let s of e)r.attach(s);return r}_runCalibration(e,t){let r=this.config.quantization,s=e.functionNames()[0],o=e.getFunction(s);if(!o)return;t.phaseStart("calibrate");let i=performance.now(),a=this.config.target,u=l((c,f)=>new n({target:f,verify:this.config.verify}).compile(c),"compileFn"),p=F2(o,a,r.calibrationData,{mode:r.calibrationMode||"minmax",quantizableOps:r.quantizableOps,compileFn:u});this.config.quantization={...r,calibration:p},t.phaseEnd("calibrate",performance.now()-i)}_runGraphPasses(e,t,r,s,o,i){let a=new iu,u=0;for(let m of e)for(let g of m.ops())g.opName==="dot"&&u++;let p=this.config.target,c=(p.getAttr&&p.getAttr("matmulChainThreshold"))??(p.kind==="cuda"?2:1/0),f=u>=c;for(let m of dL(this.config,this.config.target,{cudaMatmulChain:f,context:this.context}))a.addPass(m);a.setTrace(r),this.config.verifyMode==="full"&&a.setVerifyHook((m,g)=>(g?Qv(m):jd(m)).map(_=>_.toString())),r.phaseStart("graphPasses");let d=performance.now(),h=a.run(e,{errorMode:i?"resilient":"strict",passContext:this.config.passContext});if(h.errors){for(let m of h.errors)s.push(m),r.errorEvent(m.phase,m.funcName,m.message,m.passName);if(h.failedFunctions){for(let m of h.failedFunctions)if(o.add(m),i&&t&&t!==e){let g=t.getFunction(m);g&&e.addFunction(lm(g))}}}if(r.phaseEnd("graphPasses",performance.now()-d),r.shouldSnapshot("afterGraphPasses")){let m=new Ef;r.irDump("afterGraphPasses",m.printModule(e))}return f}_runPartitioning(e,t){let r=new iu;r.addPass(new sw(this.config.partition)),r.addPass(new ow({targets:this.config.partition.targets})),r.setTrace(t),t.phaseStart("partition");let s=performance.now();r.run(e),t.phaseEnd("partition",performance.now()-s)}_eachFunc(e,t,r,s,o,i,a){for(let u of e)if(!o.has(u.name))try{a(u)}catch(p){if(s.push(new Yn(t,u.name,p.message)),o.add(u.name),r.errorEvent(t,u.name,p.message),!i)break}}_lowerAll(e,t,r,s,o){t.phaseStart("lowering");let i=performance.now(),a=[];return this._eachFunc(e,"lowering",t,r,s,o,u=>{let p=performance.now(),c=Tm(u,this.config.target,this.context);this.config.target.isGPU&&this.config.target.isGPU()&&!(this.config.target.isWebGPU&&this.config.target.isWebGPU())&&(c.convInfo=n2(u)),t.functionEvent("lowering",u.name,{durationMs:performance.now()-p}),a.push(c),t.shouldSnapshot("afterLowering")&&t.irDump("afterLowering:"+u.name,G_(c))}),t.phaseEnd("lowering",performance.now()-i),a}_runTirPasses(e){let t=new j_;for(let r of E2(this.config))t.addPass(r);if(t.setTrace(e.trace),this.config.verifyMode==="full"){let r=new Gd;t.setVerifyHook(s=>r.verify(s))}t.run(e.primFuncs,{trace:e.trace,errors:e.errors,failed:e.failed,resilient:e.resilient})}_verifyGraph(e,t,r,s,o,i){if(i){for(let u of e){if(o.has(u.name))continue;let p=jd?jd(u):[];if(p.length>0){let c=p.map(f=>f.toString()).join("; ");s.push(new Yn("verification",u.name,c)),o.add(u.name),r.errorEvent("verification",u.name,c)}}return}let a=Qv(e);if(a.length>0)throw new Error("Graph verification failed ("+t+"): "+a.map(u=>u.toString()).join("; "))}_verifyAll(e,t,r,s){let o=new Gd;for(let i of e){if(r.has(i.name))continue;let a=o.verify(i);if(a.length>0){let u=a.join("; ");if(s)t.push(new Yn("verification",i.name,u)),r.add(i.name);else throw new Error("TensorIR verification failed for "+i.name+": "+u)}}}_lowerToLIR(e,t,r,s,o){t.phaseStart("lirLowering");let i=performance.now(),a=[];return this._eachFunc(e,"lirLowering",t,r,s,o,u=>{let p=performance.now(),c=J2(u,this.config.target);if(u.cublasInfo&&(c.cublasInfo=u.cublasInfo),u.gpuRegisterBlocked&&(c.gpuRegisterBlocked=!0),this.config.verifyMode==="full"){let f=Q2(c);if(f.length>0)throw new Error("LIR verification failed: "+f.map(d=>d.toString()).join("; "))}t.functionEvent("lirLowering",u.name,{durationMs:performance.now()-p}),a.push(c)}),t.phaseEnd("lirLowering",performance.now()-i),a}_codegen(e,t,r,s,o){t.phaseStart("codegen");let i=performance.now(),a=new jl("compiled"),u=this.config.usePartition,p={matmulBackend:this.config.matmulBackend,context:this.context},c=new Map,f=l(h=>(c.has(h.name)||c.set(h.name,new bo(h,p)),c.get(h.name)),"getBackend"),d=u?null:new bo(this.config.target,p);return this._eachFunc(e,"codegen",t,r,s,o,h=>{let m=performance.now(),g;if(u){let _=h._partitionTarget,b=_?this.config.partition.targets.find(w=>w.name===_):this.config.target;g=f(b||this.config.target)}else g=d;let y=g.compile(h);a.addCompiledKernel(y),h.shapeParamMap&&h.shapeParamMap.size>0&&a.setShapeParamMap(h.name,h.shapeParamMap,h.bufferMap),t.codegenStats(h.name,{durationMs:performance.now()-m,sourceSize:y.source.length,targetName:y.target.name})}),t.phaseEnd("codegen",performance.now()-i),a}};l(eC,"compileGraph")});function Fj(n){return typeof n=="object"&&n!==null}function zj(n){return Fj(n)&&"_impl"in n}function Vj(n){return(e,...t)=>{let r=As();if(!r)throw new Error(`TRACING dispatch key active but no tracer is set for op '${n}'`);if($j.has(n)){let c=Lt.findOp(n);return Lt.redispatch(c,e,...t)}let s=[],o={},i=Ly(n),a=0,u=l(c=>c instanceof bt||zj(c),"isTensor"),p=l(c=>{c instanceof bt||c.isSymbolic?s.push(c):s.push(r.captureConstant(c))},"pushTensor");for(let c of t)if(Array.isArray(c)&&c.length>0&&u(c[0]))for(let f of c)p(f);else u(c)?p(c):i&&(c!=null&&a<i.length&&(o[i[a]]=c),a++);return r.recordOp(n,s,o)}}function nC(){let n=Lt.listOps();for(let e of n){let t=Lt.findOp(e);if(!t)continue;let r=t.name,s=Mr.fromUnboxed(Vj(r));t.entry.registerKernel(48,s)}}var $j,rC=M(()=>{"use strict";An();sp();Ma();_u();yu();Cy();$j=new Set(["scatter","repeat","tile","split","chunk","roll","flip","cumsum","sort","argsort","topk"]);l(Fj,"_isObject");l(zj,"_hasImpl");l(Vj,"_tracingKernel");l(nC,"registerTracingDispatch")});function jj(n){return n&&n.shape&&n.shape.length>=2&&Gj.has(n.dtype)}function sC(n,e,t=jj){let r=n.graph.functions().next().value;if(!r)return n;let s=r.entryBlock,o=n.numUserInputs,i=n.capturedParams,a=[];for(let f=0;f<i.length;f++){let d=o+f,h=s.getArgument(d);h&&t(i[f],f,h)&&a.push({j:f,argIndex:d,arg:h,param:i[f]})}if(a.length===0)return n;for(let f of a){let d=e(f.param),h=f.arg.type,m=Me,g=new m("constant",[],[h],{value:d,tensor_type:h}),y=s.firstOp;y?s.insertBefore(g,y):s.pushOp(g),f.arg.replaceAllUsesWith(g.getResult(0))}let u=new Set(a.map(f=>f.argIndex));s.removeArguments(u),r.inputTypes=Object.freeze(r.inputTypes.filter((f,d)=>!u.has(d)));let p=new Set(a.map(f=>f.j)),c=i.filter((f,d)=>!p.has(d));return{...n,capturedParams:c}}var Gj,oC=M(()=>{"use strict";yr();Gj=new Set(["f16","f32","f64"]);l(jj,"defaultWeightPredicate");l(sC,"foldWeightParams")});function Hd(n,e,t){let r=t.get(e.id);if(r)return r;let s=n.scalarConstant(0,e.type.dtype).getResult(0);return n.broadcast(s,e.type.shape,[]).getResult(0)}var jo,Yd=M(()=>{"use strict";l(Hd,"gradOrZero");jo=class{static{l(this,"GradAccumulator")}constructor(e){this._builder=e,this._pending=new Map,this._reduced=new Map}accumulate(e,t){if(!t)return;let r=this._pending.get(e);r||(r=[],this._pending.set(e,r)),r.push(t),this._reduced.delete(e)}_treeReduce(e){let t=e;for(;t.length>1;){let r=[];for(let s=0;s<t.length;s+=2)s+1<t.length?r.push(this._builder.add(t[s],t[s+1]).getResult(0)):r.push(t[s]);t=r}return t[0]}get(e){if(this._reduced.has(e))return this._reduced.get(e);let t=this._pending.get(e);if(!t||t.length===0)return null;let r=this._treeReduce(t);return this._reduced.set(e,r),r}has(e){let t=this._pending.get(e);return!!t&&t.length>0}}});function pe(n,e){uk.set(n,e)}function Eu(n){return uk.get(n)||null}function pw(n){let e=uk.get(n);if(e)return e;if(lk.has(n))return null;throw new Error(`autodiff: op '${n}' is on the gradient path but has no VJP rule and is not a registered gradient barrier. Register one with registerVJPRule('${n}', ...) or registerGradientBarrier('${n}').`)}function ck(n,e){iC.set(n,e)}function fw(n){return iC.get(n)||null}function pk(n){lk.add(n)}function dw(n){return lk.has(n)}var uk,lk,iC,Is=M(()=>{"use strict";uk=new Map,lk=new Set;l(pe,"registerVJPRule");l(Eu,"getVJPRule");l(pw,"requireVJPRuleOrBarrier");iC=new Map;l(ck,"registerRegionVJP");l(fw,"getRegionVJP");l(pk,"registerGradientBarrier");l(dw,"isGradientBarrier")});var vi,fk=M(()=>{"use strict";vi=new Set(["scan","if"])});function Jd(n){let e=new Set(n.arguments.map(i=>i.id)),t=l(i=>{for(let a of i.ops()){for(let u of a.results)e.add(u.id);for(let u of a.regions||[])for(let p of u.blocks){for(let c of p.arguments)e.add(c.id);t(p)}}},"addLocals");t(n);let r=new Set,s=[],o=l(i=>{for(let a of i.ops()){for(let u of a.operands)e.has(u.id)||r.has(u.id)||u.definingOp&&u.definingOp.opName==="constant"||(r.add(u.id),s.push(u));for(let u of a.regions||[])for(let p of u.blocks)o(p)}},"scan");return o(n),s}function hw(n,e){let t=n.scalarConstant(0,e.type.dtype).getResult(0);return n.broadcast(t,e.type.shape,[]).getResult(0)}function aC(n,e,t){let r=e.type.shape,s=r.map((a,u)=>u===0?t:0),o=r.map((a,u)=>u===0?t+1:a),i=n.slice(e,s,o).getResult(0);return n.reshape(i,r.slice(1)).getResult(0)}function qj(n,e,t){let r=t.slice(1),s=e.map(o=>n.reshape(o,[1,...r]).getResult(0));return s.length===1?n.reshape(s[0],t).getResult(0):n.concat(s,0).getResult(0)}function mw(n,e,t,r,s,o,i=new Map,a=[]){let u=new Map,p=new Map;for(let _=0;_<e.arguments.length;_++)u.set(e.arguments[_].id,t[_]),p.set(e.arguments[_],t[_]);for(let[_,b]of r)u.set(_,b);for(let _ of a)r.has(_.id)&&p.set(_,r.get(_.id));let c=[],f=null;for(let _ of e.ops())_.opName==="yield"?f=_:c.push(_);let d=l(_=>{if(u.has(_.id))return u.get(_.id);let b=_.definingOp;if(b&&b.opName==="constant"){let w=i.get(_.id);return w===void 0&&(w=n._buildOp("constant",[],[_.type],new Map(b.attributes),null).getResult(0),i.set(_.id,w)),u.set(_.id,w),p.set(_,w),w}return _},"matOperand");for(let _ of c){if(vi.has(_.opName)){for(let S of _.operands)d(S),p.set(S,u.get(S.id)??S);let x=_.clone(p);n.block.pushOp(x);for(let S=0;S<_.numResults;S++)u.set(_.getResult(S).id,x.getResult(S)),p.set(_.getResult(S),x.getResult(S));continue}let b=_.operands.map(d),w=n._buildOp(_.opName,b,_.results.map(x=>x.type),new Map(_.attributes),null);for(let x=0;x<_.numResults;x++)u.set(_.getResult(x).id,w.getResult(x)),p.set(_.getResult(x),w.getResult(x))}let h=f.operands.map(_=>u.get(_.id));if(o)return{forwardYields:h};let m=new jo(n);for(let _=0;_<f.operands.length;_++)s[_]&&m.accumulate(f.operands[_].id,s[_]);for(let _=c.length-1;_>=0;_--){let b=c[_];if(b.opName==="constant")continue;let w=b.results.map(v=>m.get(v.id));if(w.every(v=>v===null))continue;if(vi.has(b.opName)){let v=fw(b.opName);v&&v(b,{accumulator:m,builder:n,materialize:d,needsGrad:Uj,scanCheckpoint:null});continue}let x=pw(b.opName);if(!x)continue;let S={builder:n,op:b,operands:b.operands.map(d),results:b.results.map(v=>u.get(v.id)),gradOutputs:w,attrs:b.attributes,full:l((v,A)=>n.broadcast(n.scalarConstant(v,A.dtype).getResult(0),A.shape,[]).getResult(0),"full")},T=x(S);if(T)for(let v=0;v<b.numOperands;v++)T[v]&&m.accumulate(b.getOperand(v).id,dk(n,T[v],b.getOperand(v).type.shape))}let g=e.arguments.map(_=>m.get(_.id)),y=new Map;for(let _ of r.keys())y.set(_,m.get(_));return{forwardYields:h,gradArgs:g,gradFree:y}}function Wj(n,e,t,r,s){let o=n.regions[0].blocks[0],i=n.regions[1].blocks[0],a=r(n.getOperand(0)),u=[];for(let y=0;y<n.numResults;y++)u.push(e.get(n.getResult(y).id));let p=Jd(o),c=Jd(i),f=new Map(p.map(y=>[y.id,r(y)])),d=new Map(c.map(y=>[y.id,r(y)])),{gradFree:h}=mw(t,o,[],f,u,!1,new Map,p),{gradFree:m}=mw(t,i,[],d,u,!1,new Map,c),g=new Map;for(let y of p)g.set(y.id,y);for(let y of c)g.set(y.id,y);for(let[y,_]of g){if(!s.has(y))continue;let b=h.get(y),w=m.get(y);if(!b&&!w)continue;let x=hw(t,_),S=t.broadcast(a,_.type.shape,[]).getResult(0);e.accumulate(y,t.select(S,b??x,w??x).getResult(0))}}function Kj(n,e){if(!n||e<=1)return null;if(n==="sqrt"||n===!0)return Math.max(1,Math.ceil(Math.sqrt(e)));if(typeof n=="number"&&n>=1){let t=Math.floor(n);return t>=e?null:t}return null}function Hj(n,e,t,r,s,o=null){let i=n.regions[0].blocks[0],a=n.getAttr("num_carry"),u=n.getAttr("num_xs"),p=n.numResults-a,c=[];for(let k=0;k<u;k++)c.push(n.getOperand(k));let f=[];for(let k=0;k<a;k++)f.push(n.getOperand(u+k));let d=c[0].type.shape[0],h=Jd(i),m=c.map(r),g=f.map(r),y=new Map(h.map(k=>[k.id,r(k)])),_=new Map,b=l(k=>m.map(O=>aC(t,O,k)),"sliceX"),w=l((k,O)=>mw(t,i,[...k,...O],y,null,!0,_,h).forwardYields.slice(0,a),"stepForward"),x=[];for(let k=0;k<p;k++)x.push(e.get(n.getResult(a+k).id));let S=[];for(let k=0;k<a;k++){let O=e.get(n.getResult(k).id);S.push(O??hw(t,g[k]))}let T=new Map,v=m.map(()=>new Array(d)),A=l((k,O,R)=>{let B=[...O,...R],E=x.map(G=>G===null?null:aC(t,G,k)),F=[...S,...E],{gradArgs:C,gradFree:H}=mw(t,i,B,y,F,!1,_,h);for(let G=0;G<u;G++)v[G][k]=C[G]??hw(t,O[G]);S=[];for(let G=0;G<a;G++)S.push(C[u+G]??hw(t,R[G]));for(let[G,U]of H){if(!U)continue;let D=T.get(G);T.set(G,D?t.add(D,U).getResult(0):U)}},"backwardStep"),N=Kj(o,d);if(N){let k=Math.ceil(d/N),O=new Array(k),R=g;O[0]=R;for(let B=0;B<d;B++){R=w(b(B),R);let E=(B+1)/N;Number.isInteger(E)&&E<k&&(O[E]=R)}for(let B=k-1;B>=0;B--){let E=B*N,F=Math.min(E+N,d),C=[],H=[O[B]],G=O[B];for(let U=E;U<F;U++){let D=b(U);C.push(D),G=w(D,G),H.push(G)}for(let U=F-1;U>=E;U--)A(U,C[U-E],H[U-E])}}else{let k=g,O=[k],R=[];for(let B=0;B<d;B++){let E=b(B);R.push(E),k=w(E,k),O.push(k)}for(let B=d-1;B>=0;B--)A(B,R[B],O[B])}for(let k=0;k<u;k++)s.has(c[k].id)&&e.accumulate(c[k].id,qj(t,v[k],m[k].type.shape));for(let k=0;k<a;k++)s.has(f[k].id)&&e.accumulate(f[k].id,S[k]);for(let[k,O]of T)O&&s.has(k)&&e.accumulate(k,O)}var Uj,uC=M(()=>{"use strict";Yd();Is();Zd();fk();Uj={has:l(()=>!0,"has")};ck("scan",(n,e)=>Hj(n,e.accumulator,e.builder,e.materialize,e.needsGrad,e.scanCheckpoint));ck("if",(n,e)=>Wj(n,e.accumulator,e.builder,e.materialize,e.needsGrad));l(Jd,"regionFreeVars");l(hw,"zeroLike");l(aC,"sliceStep");l(qj,"stackSteps");l(mw,"diffBodyStep");l(Wj,"buildCondBackward");l(Kj,"resolveSegmentLength");l(Hj,"buildScanBackward")});function Qd(n,{accumulator:e,builder:t,needsGrad:r,resolveValue:s,handleRegionOp:o=null}){for(let i=n.length-1;i>=0;i--){let a=n[i];if(a.opName==="return"||a.opName==="constant"||!a.results.some(g=>r.has(g.id)))continue;let p=[];for(let g=0;g<a.numResults;g++)p.push(e.get(a.getResult(g).id));if(p.every(g=>g===null)||o&&o(a))continue;let c=pw(a.opName);if(!c)continue;let f=new Array(a.numOperands);for(let g=0;g<a.numOperands;g++)f[g]=s(a.getOperand(g));let d=new Array(a.numResults);for(let g=0;g<a.numResults;g++)d[g]=s(a.getResult(g));let h=l((g,y)=>t.broadcast(t.scalarConstant(g,y.dtype).getResult(0),y.shape,[]).getResult(0),"full"),m=c({builder:t,op:a,operands:f,results:d,gradOutputs:p,attrs:a.attributes,full:h});if(m)for(let g=0;g<a.numOperands;g++){if(g>=m.length||!m[g])continue;let y=a.getOperand(g);r.has(y.id)&&e.accumulate(y.id,dk(t,m[g],y.type.shape))}}}function lC(n){let e=[];for(let t of n.regions)t.blocks[0]&&e.push(...Jd(t.blocks[0]));return e}function dk(n,e,t){let r=e.type.shape;if(r.length===t.length&&r.every((u,p)=>u===t[p]))return e;let s=r.length-t.length,o=[];for(let u=0;u<s;u++)o.push(u);for(let u=0;u<t.length;u++)t[u]===1&&r[s+u]!==1&&o.push(s+u);let i=e;if(o.length>0){let u=n.scalarConstant(0,e.type.dtype).getResult(0);i=n.reduce(e,u,o,"sum").getResult(0)}let a=i.type.shape;return a.length===t.length&&a.every((u,p)=>u===t[p])||(i=n.reshape(i,t).getResult(0)),i}var Yj,Xd,Zd=M(()=>{"use strict";Us();_s();ro();Yd();Is();uC();fk();Yj=new Set(["neg","abs","sign","floor","ceil"]);l(Qd,"backpropOps");l(lC,"regionControlFlowFreeVars");l(dk,"reduceGradToOperandShape");Xd=class{static{l(this,"BackwardGraphBuilder")}constructor(e={}){this._rematPolicy=e.rematPolicy||null,this._checkpointPolicy=e.checkpointPolicy||null,this._scanCheckpoint=e.scanCheckpoint||null}build(e){if(this._checkpointPolicy)return this._buildCheckpointed(e);let r=ft.compute(e).topologicalOrder,s=e.getReturnOp();if(!s)throw new Error("Forward function has no return op");let o=s.operands,i=e.args,a=this._computeGradReachability(e,r),{savedValues:u,savedValueIndices:p}=this._identifySavedValues(r,a,i),c=o.map(T=>T.type),f=u.map(T=>T.type),d=[...c,...f],h=[];for(let T=0;T<i.length;T++)a.has(i[T].id)&&h.push(i[T].type);let m=new yn(`backward_${e.name}`,d,h),g=new $n(m),y=m.args,_=y.slice(0,c.length),b=y.slice(c.length),w=new Map;for(let T=0;T<u.length;T++)w.set(u[T].id,b[T]);for(let T=0;T<i.length;T++)p.has(i[T].id)&&w.set(i[T].id,b[p.get(i[T].id)]);let x=new jo(g);for(let T=0;T<o.length;T++){let v=o[T];x.accumulate(v.id,_[T])}Qd(r,{accumulator:x,builder:g,needsGrad:a,resolveValue:l(T=>this._materialize(T,w,g),"resolveValue"),handleRegionOp:l(T=>{let v=fw(T.opName);return v?(v(T,{accumulator:x,builder:g,materialize:l(A=>this._materialize(A,w,g),"materialize"),needsGrad:a,scanCheckpoint:this._scanCheckpoint}),!0):!1},"handleRegionOp")});let S=[];for(let T=0;T<i.length;T++)a.has(i[T].id)&&S.push(Hd(g,i[T],x));return g.returnOp(S),{backwardFunc:m,savedValues:u,gradInputIndices:this._getGradInputIndices(i,a)}}_materialize(e,t,r){if(t.has(e.id))return t.get(e.id);if(!e.definingOp)return e;let s=new Set([e.id]),o=[{val:e,i:0}];for(;o.length>0;){let i=o[o.length-1],a=i.val,u=a.definingOp;if(t.has(a.id)||!u){s.delete(a.id),o.pop();continue}if(i.i<u.numOperands){let d=u.getOperand(i.i);i.i++,d.definingOp&&!t.has(d.id)&&!s.has(d.id)&&(s.add(d.id),o.push({val:d,i:0}));continue}let p=new Array(u.numOperands);for(let d=0;d<u.numOperands;d++){let h=u.getOperand(d);p[d]=t.has(h.id)?t.get(h.id):h}let c=u.results.map(d=>d.type),f=r._buildOp(u.opName,p,c,new Map(u.attributes),null);for(let d=0;d<u.numResults;d++)t.set(u.getResult(d).id,f.getResult(d));s.delete(a.id),o.pop()}return t.has(e.id)?t.get(e.id):e}_computeGradReachability(e,t){let r=new Set,s=e.getReturnOp();for(let o of s.operands)r.add(o.id);for(let o=t.length-1;o>=0;o--){let i=t[o];if(!(i.opName==="return"||!i.results.some(u=>r.has(u.id)))){if(vi.has(i.opName)){for(let u=0;u<i.numOperands;u++)r.add(i.getOperand(u).id);for(let u of lC(i))r.add(u.id);continue}if(Eu(i.opName)&&!dw(i.opName))for(let u=0;u<i.numOperands;u++)r.add(i.getOperand(u).id)}}return r}_identifySavedValues(e,t,r){let s=[],o=new Map,i=new Set(r.map(f=>f.id));for(let f of e)if(!(f.opName==="return"||f.opName==="constant"||!Eu(f.opName)||!f.results.some(m=>t.has(m.id)))){for(let m=0;m<f.numOperands;m++){let g=f.getOperand(m);i.has(g.id)&&!o.has(g.id)&&(o.set(g.id,s.length),s.push(g))}for(let m=0;m<f.numResults;m++){let g=f.getResult(m);t.has(g.id)&&!o.has(g.id)&&this._shouldSaveResult(f)&&(o.set(g.id,s.length),s.push(g))}}let a=new Set(o.keys()),u=new Map(r.map(f=>[f.id,f])),p=new Set,c=l(f=>{if(a.has(f.id)||p.has(f.id))return;p.add(f.id);let d=f.definingOp;if(!d){i.has(f.id)&&!o.has(f.id)&&(o.set(f.id,s.length),s.push(u.get(f.id)));return}for(let h=0;h<d.numOperands;h++)c(d.getOperand(h))},"collect");for(let f of e)if(!(f.opName==="return"||f.opName==="constant")&&f.results.some(d=>t.has(d.id))){if(vi.has(f.opName)){for(let d of f.operands)c(d);for(let d of lC(f))c(d);continue}if(Eu(f.opName)){for(let d=0;d<f.numOperands;d++)c(f.getOperand(d));for(let d=0;d<f.numResults;d++)c(f.getResult(d))}}return{savedValues:s,savedValueIndices:o}}_shouldSaveResult(e){return this._rematPolicy?!this._rematPolicy.shouldRematerialize(e):!Yj.has(e.opName)}_getGradInputIndices(e,t){let r=[];for(let s=0;s<e.length;s++)t.has(e[s].id)&&r.push(s);return r}_buildCheckpointed(e){let r=ft.compute(e).topologicalOrder,s=e.getReturnOp();if(!s)throw new Error("Forward function has no return op");let o=s.operands,i=e.args,a=this._computeGradReachability(e,r),u=this._checkpointPolicy.segment(r,e);for(let O of u)for(let R of O.ops)if(vi.has(R.opName))throw new Error(`Checkpointed backward does not support region control-flow op '${R.opName}'; build the backward without a checkpointPolicy, which differentiates scan/if via buildScanBackward/buildCondBackward.`);let p=new Set,c=[],f=new Map,d=new Map;for(let O of r)for(let R=0;R<O.numResults;R++){let B=O.getResult(R);d.set(B.id,B)}let h=new Set(i.map(O=>O.id));for(let O of i)a.has(O.id)&&!p.has(O.id)&&(p.add(O.id),f.set(O.id,c.length),c.push(O));for(let O of u){for(let R of O.boundaryInputs)if(!h.has(R)&&!p.has(R)){p.add(R);let B=d.get(R)||null;B&&(f.set(R,c.length),c.push(B))}for(let R of O.boundaryOutputs)if(!p.has(R)){p.add(R);let B=d.get(R)||null;B&&(f.set(R,c.length),c.push(B))}}for(let O of o)p.has(O.id)||(p.add(O.id),f.set(O.id,c.length),c.push(O));let m=o.map(O=>O.type),g=c.map(O=>O.type),y=[...m,...g],_=[];for(let O=0;O<i.length;O++)a.has(i[O].id)&&_.push(i[O].type);let b=new yn(`backward_${e.name}`,y,_),w=new $n(b),x=b.args,S=x.slice(0,m.length),T=x.slice(m.length),v=new Map;for(let O=0;O<c.length;O++)v.set(c[O].id,T[O]);let A=new jo(w);for(let O=0;O<o.length;O++)A.accumulate(o[O].id,S[O]);let N=new Map;for(let O of r)if(O.opName==="constant"){let R=O.getResult(0).type,B=w._buildOp("constant",[],[R],new Map(O.attributes),null);N.set(O.getResult(0).id,B.getResult(0))}for(let O=u.length-1;O>=0;O--){let R=u[O],B=new Map;for(let E of R.ops){let F=new Array(E.numOperands);for(let G=0;G<E.numOperands;G++){let U=E.getOperand(G),D=B.get(U.id)||v.get(U.id)||N.get(U.id);F[G]=D||U}let C=E.results.map(G=>G.type),H=w._buildOp(E.opName,F,C,new Map(E.attributes),null);for(let G=0;G<E.numResults;G++)B.set(E.getResult(G).id,H.getResult(G))}Qd(R.ops,{accumulator:A,builder:w,needsGrad:a,resolveValue:l(E=>B.get(E.id)||v.get(E.id)||N.get(E.id)||E,"resolveValue")})}let k=[];for(let O=0;O<i.length;O++)a.has(i[O].id)&&k.push(Hd(w,i[O],A));return w.returnOp(k),{backwardFunc:b,savedValues:c,gradInputIndices:this._getGradInputIndices(i,a)}}}});var Lu,gw=M(()=>{"use strict";Lu=class{static{l(this,"RematPolicy")}constructor(e={}){this._maxRematDepth=e.maxRematDepth||1,this._sizeThreshold=e.sizeThreshold||1024*1024,this._alwaysRemat=new Set(e.alwaysRemat||["neg","abs","sign","floor","ceil","exp","log","sqrt","rsqrt","sin","cos","tanh"]),this._neverRemat=new Set(e.neverRemat||["matmul","dot","conv","reduce","custom_call","pool2d"])}shouldRematerialize(e){if(this._alwaysRemat.has(e.opName))return!0;if(this._neverRemat.has(e.opName))return!1;let t=e.numResults>0?e.getResult(0).type:null;return!t||!t.shape||t.numel()>this._sizeThreshold?!1:this._isElementwise(e)}_isElementwise(e){if(e.numOperands===0||e.numResults===0)return!1;let t=e.getResult(0).type.shape;for(let r=0;r<e.numOperands;r++){let s=e.getOperand(r).type;if(!s||!s.shape||s.shape.length!==t.length)return!1;for(let o=0;o<t.length;o++)if(s.shape[o]!==t[o])return!1}return!0}}});var eh,hk=M(()=>{"use strict";Us();_s();ro();Yd();Is();gw();Zd();eh=class{static{l(this,"JointGraphBuilder")}constructor(e={}){this._rematPolicy=e.rematPolicy||new Lu(e.remat||{}),this._checkpointPolicy=e.checkpointPolicy||null}build(e){if(this._checkpointPolicy)return this._buildCheckpointed(e);let t=this._buildScaffold(e);return Qd(t.topoOrder,{accumulator:t.accumulator,builder:t.builder,needsGrad:t.needsGrad,resolveValue:l(r=>t.valueMap.get(r.id)||r,"resolveValue")}),this._finish(t)}_buildCheckpointed(e){let t=this._buildScaffold(e),r=this._checkpointPolicy.segment(t.topoOrder,e);for(let s=r.length-1;s>=0;s--){let o=r[s],i=new Map;for(let a of o.ops){let u=new Array(a.numOperands);for(let f=0;f<a.numOperands;f++){let d=a.getOperand(f);u[f]=i.get(d.id)||t.valueMap.get(d.id)||d}let p=a.results.map(f=>f.type),c=t.builder._buildOp(a.opName,u,p,new Map(a.attributes),null);for(let f=0;f<a.numResults;f++)i.set(a.getResult(f).id,c.getResult(f))}Qd(o.ops,{accumulator:t.accumulator,builder:t.builder,needsGrad:t.needsGrad,resolveValue:l(a=>i.get(a.id)||t.valueMap.get(a.id)||a,"resolveValue")})}return this._finish(t)}_buildScaffold(e){let r=ft.compute(e).topologicalOrder;this._assertNoRegionControlFlow(r);let s=e.getReturnOp();if(!s)throw new Error("Forward function has no return op");let o=s.operands,i=e.args,a=o.map(w=>w.type),u=[...e.inputTypes,...a],p=[...e.outputTypes,...e.inputTypes],c=new yn(`joint_${e.name}`,u,p),f=new $n(c),d=c.args,h=d.slice(0,e.inputTypes.length),m=d.slice(e.inputTypes.length),g=new Map;for(let w=0;w<i.length;w++)g.set(i[w].id,h[w]);for(let w of r){if(w.opName==="return")continue;let x=new Array(w.numOperands);for(let v=0;v<w.numOperands;v++){let A=w.getOperand(v);x[v]=g.get(A.id)||A}let S=w.results.map(v=>v.type),T=f._buildOp(w.opName,x,S,new Map(w.attributes),null);for(let v=0;v<w.numResults;v++)g.set(w.getResult(v).id,T.getResult(v))}let y=o.map(w=>g.get(w.id)),_=this._computeGradReachability(e,r),b=new jo(f);for(let w=0;w<o.length;w++)b.accumulate(o[w].id,m[w]);return{topoOrder:r,forwardInputs:i,forwardOutputs:o,fwdOutputValues:y,valueMap:g,builder:f,needsGrad:_,accumulator:b,jointFunc:c}}_finish(e){let t=[];for(let r=0;r<e.forwardInputs.length;r++)t.push(Hd(e.builder,e.forwardInputs[r],e.accumulator));return e.builder.returnOp([...e.fwdOutputValues,...t]),{jointFunc:e.jointFunc,numForwardOutputs:e.forwardOutputs.length,numGradInputs:t.length}}_computeGradReachability(e,t){let r=new Set,s=e.getReturnOp();for(let o of s.operands)r.add(o.id);for(let o=t.length-1;o>=0;o--){let i=t[o];if(!(i.opName==="return"||!i.results.some(u=>r.has(u.id)))&&Eu(i.opName)&&!dw(i.opName))for(let u=0;u<i.numOperands;u++)r.add(i.getOperand(u).id)}return r}_assertNoRegionControlFlow(e){for(let t of e)if(vi.has(t.opName))throw new Error(`JointGraphBuilder does not support region control-flow op '${t.opName}'; use BackwardGraphBuilder (separate mode) without a checkpointPolicy, which differentiates scan/if.`)}}});var cC=M(()=>{"use strict"});function pC(n,e){let t=n.gradOutputs[0],[r,s]=n.operands,o=n.full(0,r.type),i=n.builder.compare(r,s,e).getResult(0),a=n.builder.select(i,t,o).getResult(0),u=n.builder.select(i,o,t).getResult(0);return[a,u]}function fC(n){let e=n.gradOutputs[0],[t]=n.operands,r=n.full(0,e.type),s=n.builder.where(t,e,r).getResult(0),o=n.builder.where(t,r,e).getResult(0);return[null,s,o]}var dC=M(()=>{"use strict";Is();pe("add",n=>{let e=n.gradOutputs[0];return[e,e]});pe("sub",n=>{let e=n.gradOutputs[0],t=n.builder.neg(e).getResult(0);return[e,t]});l(pC,"_minMaxVJP");pe("maximum",n=>pC(n,"ge"));pe("minimum",n=>pC(n,"le"));pe("clamp",n=>{let e=n.gradOutputs[0],[t,r,s]=n.operands,o=n.full(0,r.type),i=n.builder.compare(r,t,"ge").getResult(0),a=n.builder.where(i,e,o).getResult(0),u=n.builder.compare(r,s,"le").getResult(0);return[null,n.builder.where(u,a,o).getResult(0),null]});l(fC,"_whereVJP");pe("where",fC);pe("select",fC);pe("mul",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder.mul(e,r).getResult(0),o=n.builder.mul(e,t).getResult(0);return[s,o]});pe("div",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder.div(e,r).getResult(0),o=n.builder.mul(r,r).getResult(0),i=n.builder.neg(t).getResult(0),a=n.builder.mul(e,i).getResult(0),u=n.builder.div(a,o).getResult(0);return[s,u]});pe("neg",n=>{let e=n.gradOutputs[0];return[n.builder.neg(e).getResult(0)]});pe("pow",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.full(1,t.type),o=n.builder.sub(r,s).getResult(0),i=n.builder.pow(t,o).getResult(0),a=n.builder.mul(r,i).getResult(0),u=n.builder.mul(e,a).getResult(0),p=n.builder.pow(t,r).getResult(0),c=n.builder.log(t).getResult(0),f=n.builder.mul(p,c).getResult(0),d=n.builder.mul(e,f).getResult(0);return[u,d]})});function hC(n,e){let t=n.full(2/Math.sqrt(Math.PI),e.type),r=n.builder.mul(e,e).getResult(0),s=n.builder.neg(r).getResult(0),o=n.builder.exp(s).getResult(0);return n.builder.mul(t,o).getResult(0)}function mC(n,e){let t=n.builder,r=n.full(1,e.type),s=t.add(e,n.full(ii,e.type)).getResult(0),o=t.div(r,s).getResult(0),i=t.log(s).getResult(0),a=t.sub(i,t.mul(n.full(.5,e.type),o).getResult(0)).getResult(0),u=t.mul(o,o).getResult(0),p=u;for(let c of Ga)a=t.add(a,t.mul(n.full(c,e.type),p).getResult(0)).getResult(0),p=t.mul(p,u).getResult(0);for(let c=0;c<ii;c++){let f=t.add(e,n.full(c,e.type)).getResult(0);a=t.sub(a,t.div(r,f).getResult(0)).getResult(0)}return a}var gC=M(()=>{"use strict";Is();Yi();pe("exp",n=>{let e=n.gradOutputs[0],t=n.results[0];return[n.builder.mul(e,t).getResult(0)]});pe("log",n=>{let e=n.gradOutputs[0],[t]=n.operands;return[n.builder.div(e,t).getResult(0)]});pe("sqrt",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.full(2,t.type),s=n.builder.mul(r,t).getResult(0);return[n.builder.div(e,s).getResult(0)]});pe("tanh",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.mul(t,t).getResult(0),s=n.builder.sub(n.full(1,t.type),r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("sigmoid",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.sub(n.full(1,t.type),t).getResult(0),s=n.builder.mul(t,r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("relu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(0,t.type),s=n.builder.compare(t,r,"gt").getResult(0);return[n.builder.select(s,e,r).getResult(0)]});pe("gelu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(1.702,t.type),s=n.full(1,t.type),o=n.builder.mul(r,t).getResult(0),i=n.builder.sigmoid(o).getResult(0),a=n.builder.sub(s,i).getResult(0),u=n.builder.mul(o,a).getResult(0),p=n.builder.add(s,u).getResult(0),c=n.builder.mul(i,p).getResult(0);return[n.builder.mul(e,c).getResult(0)]});pe("silu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.sigmoid(t).getResult(0),s=n.builder.sub(n.full(1,t.type),r).getResult(0),o=n.builder.mul(t,s).getResult(0),i=n.builder.add(r,n.builder.mul(r,o).getResult(0)).getResult(0);return[n.builder.mul(e,i).getResult(0)]});pe("sin",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.cos(t).getResult(0);return[n.builder.mul(e,r).getResult(0)]});pe("cos",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.sin(t).getResult(0),s=n.builder.neg(r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("abs",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.sign(t).getResult(0);return[n.builder.mul(e,r).getResult(0)]});l(hC,"_erfDerivIR");l(mC,"_digammaIR");pe("erf",n=>{let e=n.gradOutputs[0],[t]=n.operands;return[n.builder.mul(e,hC(n,t)).getResult(0)]});pe("erfc",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.neg(hC(n,t)).getResult(0);return[n.builder.mul(e,r).getResult(0)]});pe("lgamma",n=>{let e=n.gradOutputs[0],[t]=n.operands;return[n.builder.mul(e,mC(n,t)).getResult(0)]});pe("gamma",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.results[0],s=n.builder.mul(r,mC(n,t)).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("log2",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(Math.LN2,t.type),s=n.builder.mul(t,r).getResult(0);return[n.builder.div(e,s).getResult(0)]});pe("log10",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(Math.LN10,t.type),s=n.builder.mul(t,r).getResult(0);return[n.builder.div(e,s).getResult(0)]});pe("exp2",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.full(Math.LN2,t.type),s=n.builder.mul(t,r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("square",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(2,t.type),s=n.builder.mul(r,t).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("reciprocal",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.mul(t,t).getResult(0),s=n.builder.neg(r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("rsqrt",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.mul(n.builder.mul(t,t).getResult(0),t).getResult(0),s=n.full(-.5,t.type),o=n.builder.mul(s,r).getResult(0);return[n.builder.mul(e,o).getResult(0)]})});var yC=M(()=>{"use strict";Is();pe("dot",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.op.getAttr("lhs_contracting"),o=n.op.getAttr("rhs_contracting"),i=n.op.getAttr("lhs_batch")||[],a=n.op.getAttr("rhs_batch")||[],u=t.type.rank,p=r.type.rank,c=e.type.rank,f=[];for(let _=0;_<u;_++)!s.includes(_)&&!i.includes(_)&&f.push(_);let d=[];for(let _=0;_<p;_++)!o.includes(_)&&!a.includes(_)&&d.push(_);let h=[];for(let _=i.length;_<i.length+d.length;_++)h.push(_+f.length);let m=[];for(let _=i.length;_<i.length+f.length;_++)m.push(_);let g=n.builder.dot(e,r,h,d,Array.from({length:i.length},(_,b)=>b),a).getResult(0),y=n.builder.dot(t,e,f,m,i,Array.from({length:i.length},(_,b)=>b)).getResult(0);return[g,y]});pe("matmul",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=l(p=>{let c=Array.from({length:p},(f,d)=>d);return c[p-2]=p-1,c[p-1]=p-2,c},"swapLastTwo"),o=n.builder.transpose(r,s(r.type.rank)).getResult(0),i=n.builder.matmul(e,o).getResult(0),a=n.builder.transpose(t,s(t.type.rank)).getResult(0),u=n.builder.matmul(a,e).getResult(0);return[i,u]});pe("conv",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder,o=n.op.getAttr("strides"),i=n.op.getAttr("padding"),a=n.op.getAttr("dilation")||o.map(()=>1),u=n.op.getAttr("groups")||1,p=n.op.getAttr("input_layout"),c=n.op.getAttr("kernel_layout");if(!(o.length===2&&o.every(A=>A===1)&&a.every(A=>A===1)&&u===1&&p==="NCHW"&&c==="OIHW"))throw new Error("conv VJP supports only 2D stride-1 dilation-1 groups-1 NCHW/OIHW conv");let d=r.type.shape,h=d[2],m=d[3],[g,y]=i,_=s.reverse(s.transpose(r,[1,0,2,3]).getResult(0),[2,3]).getResult(0),b=[[h-1-g[0],h-1-g[1]],[m-1-y[0],m-1-y[1]]],w=s.conv(e,_,[1,1],b).getResult(0),x=s.transpose(t,[1,0,2,3]).getResult(0),S=s.transpose(e,[1,0,2,3]).getResult(0),T=s.conv(x,S,[1,1],[g,y]).getResult(0),v=s.transpose(T,[1,0,2,3]).getResult(0);return[w,v]})});var _C=M(()=>{"use strict";Is();pe("reduce",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("reduce_type"),s=n.op.getAttr("dimensions"),o=t.type.shape,i=t.type.dtype,a=new Set(s),u=o.map((f,d)=>a.has(d)?1:f),p=o.map((f,d)=>d),c=n.builder.reshape(e,u).getResult(0);if(r==="sum")return[n.builder.broadcast(c,o,p).getResult(0),null];if(r==="mean"){let f=1;for(let y=0;y<o.length;y++)a.has(y)&&(f*=o[y]);let d=n.builder.broadcast(c,o,p).getResult(0),h=n.builder.scalarConstant(f,i).getResult(0),m=n.builder.broadcast(h,o,[]).getResult(0);return[n.builder.div(d,m).getResult(0),null]}if(r==="max"||r==="min"){let f=n.builder.reshape(n.results[0],u).getResult(0),d=n.builder.broadcast(f,o,p).getResult(0),h=n.builder.compare(t,d,"eq").getResult(0),m=n.builder.convert(h,i).getResult(0),g=n.builder.broadcast(c,o,p).getResult(0);return[n.builder.mul(g,m).getResult(0),null]}throw r==="prod"?new Error("reduce VJP for reduce_type='prod' is not implemented; provide a gradient rule or avoid differentiating reduce_prod"):new Error(`reduce VJP: unsupported reduce_type '${r}' on the gradient path (would silently drop the gradient)`)})});var bC=M(()=>{"use strict";Is();pe("reshape",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=t.type.shape;return[n.builder.reshape(e,r).getResult(0)]});pe("reverse",n=>[n.builder.reverse(n.gradOutputs[0],n.op.getAttr("dimensions")).getResult(0)]);pe("transpose",n=>{let e=n.gradOutputs[0],t=n.op.getAttr("permutation"),r=new Array(t.length);for(let s=0;s<t.length;s++)r[t[s]]=s;return[n.builder.transpose(e,r).getResult(0)]});pe("broadcast_in_dim",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("broadcast_dimensions"),s=t.type.shape,o=n.results[0].type.shape,i=t.type.dtype,a=[];for(let f=0;f<o.length;f++)if(!r.includes(f))a.push(f);else{let d=r.indexOf(f);s[d]===1&&o[f]!==1&&a.push(f)}if(a.length===0)return[e];let u=n.builder.scalarConstant(0,i).getResult(0),p=n.builder.reduce(e,u,a,"sum").getResult(0),c=p.type.shape;return c.length!==s.length||!c.every((f,d)=>f===s[d])?[n.builder.reshape(p,s).getResult(0)]:[p]});pe("slice",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("starts"),s=t.type.shape,o=e.type.shape,i=n.op.getAttr("strides")||s.map(()=>1),a=t.type.dtype,u=[...r],p=new Array(s.length),c=new Array(s.length);for(let d=0;d<s.length;d++)c[d]=i[d]-1,p[d]=s[d]-r[d]-(o[d]-1)*i[d]-1;let f=n.builder.scalarConstant(0,a).getResult(0);return[n.builder.pad(e,f,u,p,c).getResult(0)]});pe("concat",n=>{let e=n.gradOutputs[0],t=n.op.getAttr("dimension"),r=n.operands,s=[],o=0;for(let i=0;i<r.length;i++){let a=r[i].type.shape,u=new Array(a.length).fill(0),p=[...e.type.shape];u[t]=o,p[t]=o+a[t],s.push(n.builder.slice(e,u,p).getResult(0)),o+=a[t]}return s});pe("gather",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder.scalarConstant(0,t.type.dtype).getResult(0),o=n.builder.broadcast(s,t.type.shape,[]).getResult(0);return[n.builder.scatterAdd(o,r,e,{updateWindowDims:n.op.getAttr("offset_dims"),insertedWindowDims:n.op.getAttr("collapsed_slice_dims"),scatterDimsToOperandDims:n.op.getAttr("start_index_map"),indexVectorDim:n.op.getAttr("index_vector_dim")}).getResult(0),null]});pe("scatter",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.op.getAttr("inserted_window_dims"),o=t.type.shape.map((a,u)=>s.includes(u)?1:a),i=n.builder.gather(e,r,{offsetDims:n.op.getAttr("update_window_dims"),collapsedSliceDims:s,startIndexMap:n.op.getAttr("scatter_dims_to_operand_dims"),indexVectorDim:n.op.getAttr("index_vector_dim"),sliceSizes:o}).getResult(0);return[e,null,i]});pe("pad",n=>{let e=n.gradOutputs[0],t=n.op.getAttr("low"),r=n.op.getAttr("high"),[s]=n.operands,o=s.type.shape,i=[...t],a=new Array(o.length);for(let u=0;u<o.length;u++)a[u]=t[u]+o[u];return[n.builder.slice(e,i,a).getResult(0),null]})});var wC=M(()=>{"use strict";Is();_s();pe("softmax",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.op.getAttr("axis"),s=t.type.dtype,o=t.type.shape,i=n.builder.mul(e,t).getResult(0),a=n.builder.scalarConstant(0,s).getResult(0),u=n.builder.reduce(i,a,[r],"sum").getResult(0),p=Xo(o.length,r),c=n.builder.broadcast(u,o,p).getResult(0),f=n.builder.sub(e,c).getResult(0);return[n.builder.mul(t,f).getResult(0)]});pe("log_softmax",n=>{let e=n.gradOutputs[0],t=n.results[0],[r]=n.operands,s=n.op.getAttr("axis"),o=t.type.dtype,i=t.type.shape,a=n.builder.exp(t).getResult(0),u=n.builder.scalarConstant(0,o).getResult(0),p=n.builder.reduce(e,u,[s],"sum").getResult(0),c=Xo(i.length,s),f=n.builder.broadcast(p,i,c).getResult(0),d=n.builder.mul(a,f).getResult(0);return[n.builder.sub(e,d).getResult(0)]});pe("layer_norm",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.op.getAttr("axis"),o=n.op.getAttr("epsilon"),i=t.type.dtype,a=t.type.shape,u=[s<0?a.length+s:s],p=a[u[0]],c=Xo(a.length,u[0]),f=n.builder.scalarConstant(0,i).getResult(0),d=n.builder.reduce(t,f,u,"mean").getResult(0),h=n.builder.broadcast(d,a,c).getResult(0),m=n.builder.sub(t,h).getResult(0),g=n.builder.mul(m,m).getResult(0),y=n.builder.reduce(g,f,u,"mean").getResult(0),_=n.full(o,y.type),b=n.builder.add(y,_).getResult(0),w=n.builder.rsqrt(b).getResult(0),x=n.builder.broadcast(w,a,c).getResult(0),S=[];for(let ue=0;ue<a.length;ue++)u.includes(ue)&&S.push(ue);let T=n.builder.broadcast(r,a,S).getResult(0),v=n.builder.mul(e,T).getResult(0),A=n.full(p,t.type),N=n.builder.mul(A,v).getResult(0),k=n.builder.reduce(v,f,u,"sum").getResult(0),O=n.builder.broadcast(k,a,c).getResult(0),R=n.builder.mul(m,x).getResult(0),B=n.builder.mul(v,R).getResult(0),E=n.builder.reduce(B,f,u,"sum").getResult(0),F=n.builder.broadcast(E,a,c).getResult(0),C=n.builder.mul(R,F).getResult(0),H=n.builder.sub(N,n.builder.add(O,C).getResult(0)).getResult(0),G=n.builder.div(x,A).getResult(0),U=n.builder.mul(G,H).getResult(0),D=n.builder.reduce(n.builder.mul(e,R).getResult(0),f,c,"sum").getResult(0),V=n.builder.reduce(e,f,c,"sum").getResult(0);return[U,D,V]});pe("scaled_dot_product_attention",n=>{let e=n.gradOutputs[0],[t,r,s]=n.operands,o=n.op.getAttr("scale");if(n.op.getAttr("causal"))throw new Error("causal scaled_dot_product_attention VJP not supported");let i=n.builder,a=t.type.dtype,u=t.type.rank,p=[];for(let N=0;N<u;N++)p.push(N);p[u-2]=u-1,p[u-1]=u-2;let c=l(N=>i.transpose(N,p).getResult(0),"lastT"),f=i.matmul(t,c(r)).getResult(0),d=i.mul(f,n.full(o,f.type)).getResult(0),h=i.softmax(d,u-1).getResult(0),m=i.matmul(c(h),e).getResult(0),g=i.matmul(e,c(s)).getResult(0),y=i.scalarConstant(0,a).getResult(0),_=i.mul(g,h).getResult(0),b=i.reduce(_,y,[u-1],"sum").getResult(0),w=[];for(let N=0;N<u-1;N++)w.push(N);let x=i.broadcast(b,h.type.shape,w).getResult(0),S=i.mul(h,i.sub(g,x).getResult(0)).getResult(0),T=i.mul(S,n.full(o,S.type)).getResult(0),v=i.matmul(T,r).getResult(0),A=i.matmul(c(T),t).getResult(0);return[v,A,m]});pe("pool2d",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder,s=n.op.getAttr("pool_type"),o=n.op.getAttr("kernel_size"),i=n.op.getAttr("strides"),a=n.op.getAttr("padding"),u=n.op.getAttr("layout")||"NCHW",p=a.every(v=>v[0]===0&&v[1]===0),c=i[0]===o[0]&&i[1]===o[1];if(u!=="NCHW"||!p||!c||s!=="avg"&&s!=="max")throw new Error("pool2d VJP supports only non-overlapping (stride=kernel) avg/max pooling without padding, NCHW");let[f,d,h,m]=e.type.shape,[g,y]=o,_=t.type.shape,b=l(v=>r.reshape(r.broadcast(v,[f,d,h,g,m,y],[0,1,2,4]).getResult(0),_).getResult(0),"upsample"),w=b(e);if(s==="avg"){let v=n.full(g*y,t.type);return[r.div(w,v).getResult(0)]}let x=b(n.results[0]),S=r.compare(t,x,"eq").getResult(0),T=n.full(0,t.type);return[r.select(S,w,T).getResult(0)]});pe("batch_norm",n=>{let e=n.gradOutputs[0],[t,r,,s,o]=n.operands,i=n.op.getAttr("axis"),a=n.op.getAttr("epsilon"),u=t.type.dtype,p=t.type.shape,c=n.builder,f=Xo(p.length,i),d=c.scalarConstant(0,u).getResult(0),h=n.full(a,o.type),m=c.rsqrt(c.add(o,h).getResult(0)).getResult(0),g=c.broadcast(m,p,[i]).getResult(0),y=c.broadcast(s,p,[i]).getResult(0),_=c.broadcast(r,p,[i]).getResult(0),b=c.sub(t,y).getResult(0),w=c.mul(b,g).getResult(0),x=c.mul(e,_).getResult(0),S=c.mul(x,g).getResult(0),T=c.reduce(c.mul(e,w).getResult(0),d,f,"sum").getResult(0),v=c.reduce(e,d,f,"sum").getResult(0),A=c.neg(c.reduce(S,d,f,"sum").getResult(0)).getResult(0),N=c.mul(c.mul(g,g).getResult(0),g).getResult(0),k=n.full(-.5,t.type),O=c.mul(c.mul(c.mul(x,b).getResult(0),N).getResult(0),k).getResult(0),R=c.reduce(O,d,f,"sum").getResult(0);return[S,T,v,A,R]});pe("elu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.results[0],s=n.op.getAttr("alpha")??1,o=n.full(0,t.type),i=n.full(1,t.type),a=n.full(s,t.type),u=n.builder.compare(t,o,"gt").getResult(0),p=n.builder.add(r,a).getResult(0),c=n.builder.select(u,i,p).getResult(0);return[n.builder.mul(e,c).getResult(0)]});pe("leaky_relu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("negative_slope")??.01,s=n.full(0,t.type),o=n.full(1,t.type),i=n.full(r,t.type),a=n.builder.compare(t,s,"gt").getResult(0),u=n.builder.select(a,o,i).getResult(0);return[n.builder.mul(e,u).getResult(0)]});pe("celu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("alpha")??1,s=n.full(0,t.type),o=n.full(1,t.type),i=n.builder.compare(t,s,"gt").getResult(0),a=n.full(r,t.type),u=n.builder.div(t,a).getResult(0),p=n.builder.exp(u).getResult(0),c=n.builder.select(i,o,p).getResult(0);return[n.builder.mul(e,c).getResult(0)]});pe("selu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=1.0507009873554805,s=1.6732632423543772,o=n.full(0,t.type),i=n.full(r,t.type),a=n.builder.compare(t,o,"gt").getResult(0),u=n.full(s,t.type),p=n.builder.exp(t).getResult(0),c=n.builder.mul(u,p).getResult(0),f=n.builder.select(a,i,n.builder.mul(i,c).getResult(0)).getResult(0);return[n.builder.mul(e,f).getResult(0)]});pe("hardswish",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(-3,t.type),s=n.full(3,t.type),o=n.full(0,t.type),i=n.full(1,t.type),a=n.full(2,t.type),u=n.full(6,t.type),p=n.builder.compare(t,r,"le").getResult(0),c=n.builder.compare(t,s,"ge").getResult(0),f=n.builder.add(n.builder.mul(a,t).getResult(0),s).getResult(0),d=n.builder.div(f,u).getResult(0),h=n.builder.select(p,o,n.builder.select(c,i,d).getResult(0)).getResult(0);return[n.builder.mul(e,h).getResult(0)]});pe("hardsigmoid",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(-3,t.type),s=n.full(3,t.type),o=n.full(0,t.type),i=n.full(1/6,t.type),a=n.builder.compare(t,r,"le").getResult(0),u=n.builder.compare(t,s,"ge").getResult(0),p=n.builder.select(a,o,n.builder.select(u,o,i).getResult(0)).getResult(0);return[n.builder.mul(e,p).getResult(0)]});pe("embedding",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=r.type.rank,o=n.full(0,t.type);return[n.builder.scatter(o,r,e,{updateWindowDims:[s],insertedWindowDims:[0],scatterDimsToOperandDims:[0],indexVectorDim:s}).getResult(0),null]})});var TC=M(()=>{"use strict";Is();pk("stop_gradient");pe("stop_gradient",()=>[null]);for(let n of["compare","logical_not","argmax","argmin","iota"])pk(n)});var xC=M(()=>{"use strict";Is();Yd();Zd();hk();gw();cC();dC();gC();yC();_C();bC();wC();TC()});var SC={};at(SC,{compileWithBackward:()=>_w});function yw(n){return typeof n.then=="function"}function _w(n,e,t={}){let r=t.target??go(),s=t.mode||"separate",o=t.rematPolicy||new Lu(t.remat||{}),i={target:r,verify:!1,...t,backward:void 0,mode:void 0,rematPolicy:void 0,remat:void 0},a=t.dynamic_shapes||null,u=[],p=null,c=null,f=null;function d(k){let O=ww((...B)=>n.forward(...B),k,{name:n.constructor.name||"compiled",dynamicShapes:a}),R=l(B=>{let E=B.graph.functions().next().value,F=s==="joint"?m(E,B,o):h(E,B,o);return F.shapeEnv=B.shapeEnv,F.outputSymShapes=B.outputSymShapes,F},"finish");return yw(O)?O.then(R):R(O)}l(d,"_compile");function h(k,O,R){let B=new Xd({rematPolicy:R}),{backwardFunc:E,savedValues:F,gradInputIndices:C}=B.build(k),H=k.getReturnOp(),G=[...H.operands],U=G.length,D=new Map(k.args.map((ke,rt)=>[ke.id,rt])),V=new Map(G.map((ke,rt)=>[ke.id,rt])),ue=[],ce=new Map;for(let ke of F)D.has(ke.id)||V.has(ke.id)||ce.has(ke.id)||(ce.set(ke.id,U+ue.length),ue.push(ke));ue.length>0&&(H.erase(),new $n(k).returnOp([...G,...ue]),k.outputTypes=Object.freeze([...G.map(ke=>ke.type),...ue.map(ke=>ke.type)]));let Le=F.map(ke=>D.has(ke.id)?{kind:"arg",index:D.get(ke.id)}:V.has(ke.id)?{kind:"output",index:V.get(ke.id)}:{kind:"output",index:ce.get(ke.id)}),j=new Mn("forward");j.addFunction(k);let W=new Si(i).compile(j),ie=new Mn("backward");ie.addFunction(E);let je=new Si(i).compile(ie);return{mode:"separate",fwdResult:W,bwdResult:je,forwardFunc:k,backwardFunc:E,savedValues:F,savedSources:Le,numRealOutputs:U,gradInputIndices:C,capturedParams:O.capturedParams,numUserInputs:O.numUserInputs,outputTypes:O.outputTypes,shapeEnv:O.shapeEnv,outputSymShapes:O.outputSymShapes}}l(h,"_compileSeparate");function m(k,O,R){let B=new eh({rematPolicy:R}),{jointFunc:E,numForwardOutputs:F,numGradInputs:C}=B.build(k),H=new Mn("joint");return H.addFunction(E),{mode:"joint",result:new Si(i).compile(H),jointFunc:E,numForwardOutputs:F,numGradInputs:C,capturedParams:O.capturedParams,numUserInputs:O.numUserInputs,outputTypes:O.outputTypes,inputTypes:k.inputTypes,shapeEnv:O.shapeEnv,outputSymShapes:O.outputSymShapes}}l(m,"_compileJoint");function g(k,O){return k.outputSymShapes&&k.shapeEnv?k.shapeEnv.resolveSymbolicShape(k.outputSymShapes[O]):[...k.outputTypes[O].shape]}l(g,"_resolveOutputShape");function y(k,O,R){let B=k.module||k;return B.executionPlan?B.runPlanAsync(B.executionPlan,R):k.isAsync(O)?k.runAsync(O,...R):(k.run(O,...R),null)}l(y,"_runK");function _(k,O){let B=k.fwdResult.listKernels()[0],E=O.length>0?O[0].device:"cpu",F=O.map(j=>Qe(j)),H=k.capturedParams.map(j=>Qe(j)),G=k.forwardFunc.outputTypes,U=k.numRealOutputs,D=new Array(G.length),V=new Array(G.length);for(let j=0;j<G.length;j++){let W=j<U?g(k,j):[...G[j].shape],ie=G[j].dtype,je=bn(W),ke=St(ie);D[j]=new ke(Math.max(je,1)),V[j]=W}let ue=[...F,...H,...D],ce=l(()=>({results:U===1?Ee(D[0],V[0],G[0].dtype,E):Array.from({length:U},(W,ie)=>Ee(D[ie],V[ie],G[ie].dtype,E)),inputArrays:F,paramArrays:H,outputArrays:D,device:E}),"build"),Le=y(k.fwdResult,B,ue);return Le?Le.then(ce):ce()}l(_,"_executeSeparateForward");function b(k,O,R){let E=k.bwdResult.listKernels()[0],F=O.map(ie=>Qe(ie)),C=k.savedValues,H=k.savedSources,G=[...R.inputArrays,...R.paramArrays],U=new Array(C.length);for(let ie=0;ie<C.length;ie++){let je=H[ie];U[ie]=je.kind==="arg"?G[je.index]:R.outputArrays[je.index]}let D=k.backwardFunc,V=D.outputTypes.length,ue=new Array(V),ce=new Array(V);for(let ie=0;ie<V;ie++){let je=[...D.outputTypes[ie].shape],ke=D.outputTypes[ie].dtype,rt=bn(je),zt=St(ke);ue[ie]=new zt(Math.max(rt,1)),ce[ie]=je}let Le=[...F,...U,...ue],j=l(()=>ue.map((ie,je)=>Ee(ie,ce[je],D.outputTypes[je].dtype,R.device)),"build"),W=y(k.bwdResult,E,Le);return W?W.then(j):j()}l(b,"_executeSeparateBackward");function w(k){for(let O=0;O<u.length;O++){let R=u[O];R.shapeEnv.bindInputShapes(k);let{passed:B}=R.shapeEnv.evaluateGuards();if(B)return R}return null}l(w,"_findCachedEntry");function x(k,O){if(c=k,k.mode==="joint")return v(k,O);let R=_(k,O);return yw(R)?R.then(B=>(p=B,B.results)):(p=R,R.results)}l(x,"_forwardWith");function S(k){let O=w(k);if(O)return x(O,k);let R=d(k);return yw(R)?R.then(B=>(u.push(B),B.shapeEnv.bindInputShapes(k),x(B,k))):(u.push(R),R.shapeEnv.bindInputShapes(k),x(R,k))}l(S,"_runForward");function T(...k){return f?f.then(()=>S(k)):S(k)}l(T,"compiledForward");function v(k,O){let B=k.result.listKernels()[0],E=O.length>0?O[0].device:"cpu",F=O.map(W=>Qe(W)),H=k.capturedParams.map(W=>Qe(W)),G=k.jointFunc,U=G.outputTypes.length,D=new Array(U),V=new Array(U);for(let W=0;W<U;W++){let ie=[...G.outputTypes[W].shape],je=G.outputTypes[W].dtype,ke=bn(ie),rt=St(je);D[W]=new rt(Math.max(ke,1)),V[W]=ie}let ue=new Array(k.numForwardOutputs);for(let W=0;W<k.numForwardOutputs;W++){let ie=k.outputTypes[W],je=bn(ie.shape),ke=St(ie.dtype);ue[W]=new ke(Math.max(je,1))}p={inputArrays:F,paramArrays:H,gradOutputArrays:ue,outputArrays:D,outputShapes:V,device:E,compiled:k};let ce=[...F,...H,...ue,...D],Le=l(()=>{let W=[];for(let ie=0;ie<k.numForwardOutputs;ie++)W.push(Ee(D[ie],V[ie],G.outputTypes[ie].dtype,E));return W.length===1?W[0]:W},"build"),j=y(k.result,B,ce);return j?j.then(Le):Le()}l(v,"_executeJointForward");let A=T;A.backward=function(...k){if(!c||!p)throw new Error("Must run forward before backward");return c.mode==="joint"?N(c,k,p):b(c,k,p)};function N(k,O,R){let{inputArrays:B,paramArrays:E,outputArrays:F,outputShapes:C,device:H}=R,G=O.map(W=>Qe(W));for(let W=0;W<G.length;W++)R.gradOutputArrays[W].set(G[W]);let U=k.jointFunc,D=U.outputTypes.length,V=new Array(D),ue=new Array(D);for(let W=0;W<D;W++){let ie=[...U.outputTypes[W].shape],je=U.outputTypes[W].dtype,ke=bn(ie),rt=St(je);V[W]=new rt(Math.max(ke,1)),ue[W]=ie}let ce=[...B,...E,...G,...V],Le=l(()=>{let W=[];for(let ie=k.numForwardOutputs;ie<D;ie++)W.push(Ee(V[ie],ue[ie],U.outputTypes[ie].dtype,H));return W},"build"),j=y(k.result,k.result.listKernels()[0],ce);return j?j.then(Le):Le()}if(l(N,"_executeJointBackward"),A.original=n,A.backwardGraph=()=>{if(u.length===0)return null;let k=u[0];return k.mode==="joint"?k.jointFunc:k.backwardFunc},A.forwardGraph=()=>{if(u.length===0)return null;let k=u[0];return k.mode==="joint"?k.jointFunc:k.forwardFunc},A.capturedParams=()=>u.length?u[0].capturedParams:[],e){let k=d(e);yw(k)?f=k.then(O=>(u.push(O),f=null,O)):u.push(k)}return A}var bw=M(()=>{"use strict";mk();aw();yo();Di();Zd();_s();hk();gw();xC();wn();rs();ss();l(yw,"_isThenable");l(_w,"compileWithBackward")});function Cu(n){return typeof n.then=="function"}function Jj(){vC||(nC(),vC=!0)}function Zj(n,e){if(!n)return new Array(e.length).fill(null);let t=new Array(e.length);for(let r=0;r<e.length;r++){let s=n[r];if(s===!0){let o=new Set;for(let i=0;i<e[r].shape.length;i++)o.add(i);t[r]=o}else s instanceof Set?t[r]=s:t[r]=null}return t}function ww(n,e,t){Jj();let r=t?.name||n.name||"traced",s=new I_(r),o=Zj(t?.dynamicShapes,e);for(let f=0;f<e.length;f++)s.createInput(e[f].shape,e[f].dtype,o[f]);let i=e.length,a=s._initGraph();function u(f){Array.isArray(f)?s.markOutputs(f):s.markOutput(f),s.deactivate();let d=s.getGraphModule(),h=d.functions().next().value;return{graph:d,capturedParams:[...s.capturedParams],numUserInputs:i,outputTypes:h.outputTypes,shapeEnv:s.shapeEnv,outputSymShapes:s.outputSymShapes}}l(u,"_finalize"),s.activate();let p=Jr.fromKey(48),c=NN(p,()=>n(...a));if(Cu(c))return c.then(f=>u(f),f=>{throw s.deactivate(),f});try{return u(c)}catch(f){throw s.deactivate(),f}}function kC(n,e,t){let r=ww(n,e,t);return Cu(r)?r.then(s=>s.graph):r.graph}function Qj(n,e,t){let r=n.result.listKernels();if(r.length===0)throw new Error("No kernels compiled");let s=r[0],o=e.length>0?e[0].device:"cpu",i=new Array(e.length);for(let g=0;g<e.length;g++)i[g]=Qe(e[g]);let a=n.capturedParams,u=new Array(a.length);for(let g=0;g<a.length;g++)u[g]=Qe(a[g]);let p=n.outputTypes,c=n.outputSymShapes,f=new Array(p.length),d=new Array(p.length);for(let g=0;g<p.length;g++){let y=c&&t?t.resolveSymbolicShape(c[g]):p[g].shape,_=p[g].dtype,b=bn(y),w=St(_);f[g]=new w(Math.max(b,1)),d[g]=[...y]}let h=new Array(i.length+u.length+f.length),m=0;for(let g=0;g<i.length;g++)h[m++]=new Lo(i[g],e[g].shape,e[g].dtype);for(let g=0;g<u.length;g++){let y=new Lo(u[g],a[g].shape,a[g].dtype),_=a[g]._impl;_&&(y.resident={key:_.storage.rawData,version:_.version}),h[m++]=y}for(let g=0;g<f.length;g++)h[m++]=new Lo(f[g],d[g],p[g].dtype);return{funcName:s,device:o,outputTypes:p,outputArrays:f,outputShapes:d,allArgs:h}}function gk(n,e,t,r){if(e.length===1)return Ee(t[0],r[0],e[0].dtype,n);let s=new Array(e.length);for(let o=0;o<e.length;o++)s[o]=Ee(t[o],r[o],e[o].dtype,n);return s}function Xj(n,e,t){let{funcName:r,device:s,outputTypes:o,outputArrays:i,outputShapes:a,allArgs:u}=Qj(n,e,t),p=n.result.module.executionPlan;return p?n.result.module.runPlanAsync(p,u,{resident:!0}).then(()=>gk(s,o,i,a)):n.result.isAsync(r)?n.result.runAsync(r,...u).then(()=>gk(s,o,i,a)):(n.result.run(r,...u),gk(s,o,i,a))}function eU(n,e,t={}){if(t?.backward)return _w(n,e,t);let r=t?.target??go(),s={target:r,verify:!1,...t},o=t?.dynamic_shapes||null,i=t?.shapeBuckets||null,a=t?.foldWeights??t?.quantization?.foldWeights??!1,u=[];function p(w,x,S){if(!w||typeof w!="object"||w.repro)return w;try{w.repro={name:n.constructor?.name||"compiled",phase:S,target:r?.name,inputs:(x||[]).map(T=>({shape:T.shape,dtype:T.dtype})),config:{fusion:s.fusion,scheduling:s.scheduling,optimization:s.optimization,quantization:s.quantization,dynamicShapes:!!o}}}catch{return w}return w}l(p,"_attachRepro");function c(w){let x=a?sC(w,Qe):w;return{result:new Si(s).compile(x.graph),graph:x.graph,capturedParams:x.capturedParams,numUserInputs:x.numUserInputs,outputTypes:x.outputTypes,shapeEnv:x.shapeEnv,outputSymShapes:x.outputSymShapes}}l(c,"_finalize");function f(w,x){try{let S=ww((...T)=>n.forward(...T),w,{name:n.constructor.name||"compiled",dynamicShapes:x});return Cu(S)?S.then(c,T=>{throw p(T,w,"compile")}):c(S)}catch(S){throw p(S,w,"compile")}}l(f,"_compileWith");function d(w){return f(w,o)}l(d,"_compile");function h(w){return w.map((x,S)=>({shape:x,dtype:e[S].dtype}))}l(h,"_bucketInputs");function m(w){for(let x=0;x<u.length;x++){let S=u[x];S.shapeEnv.bindInputShapes(w);let{passed:T}=S.shapeEnv.evaluateGuards();if(T)return S}return null}l(m,"_findCachedEntry");function g(w,x){try{let S=Xj(w,x,w.shapeEnv);return Cu(S)?S.then(void 0,T=>{throw p(T,x,"run")}):S}catch(S){throw p(S,x,"run")}}l(g,"_execute");function y(...w){let x=m(w);if(!x){let S=d(w);if(Cu(S))return S.then(T=>(u.push(T),T.shapeEnv.bindInputShapes(w),g(T,w)));x=S,u.push(x),x.shapeEnv.bindInputShapes(w)}return g(x,w)}l(y,"compiledForward");let _=null;if(e){let w=[];if(i)for(let x of i)w.push(f(h(x),null));if(w.push(d(e)),w.some(x=>Cu(x)))_=Promise.all(w).then(x=>{for(let S of x)u.push(S)});else for(let x of w)Cu(x)||u.push(x)}let b=y;return b.original=n,b.graph=w=>kC((...S)=>n.forward(...S),w||e,{name:n.constructor.name||"compiled",dynamicShapes:o}),b.source=()=>{if(u.length===0)return null;let w=u[0],x=w.result.listKernels();return x.length>0?w.result.getSource(x[0]):null},b.kernels=()=>u.length===0?[]:u[0].result.listKernels(),b.snippet=()=>{if(u.length===0)return null;let w=u[0],x=w.result.listKernels();return x.length>0?w.result.getSnippet(x[0]):null},b.result=()=>u.length>0?u[0].result:null,b._ready=_,b}var vC,mk=M(()=>{"use strict";_u();rC();An();o0();aw();yo();wn();dy();rs();ss();oC();bw();vC=!1;l(Cu,"_isThenable");l(Jj,"_ensureTracing");l(Zj,"_normalizeDynamicShapes");l(ww,"_traceCore");l(kC,"trace");l(Qj,"_prepareExecution");l(gk,"_wrapOutputs");l(Xj,"executeCompiled");l(eU,"compile")});ys();An();wn();Cy();sr();di();Xa();qn();ss();rs();function nc(n,e){let t=En(n),r=Wn.allocate(0,e,Bl),s=new Pt(r,0,n,t,e,Bl);return new Ze(s)}l(nc,"_metaTensor");function aV(n,e,t){let r=Rl(e.shape,t.shape);if(!r)throw new Error(`Incompatible shapes: [${e.shape}] vs [${t.shape}]`);let s=_g(e.dtype,t.dtype);return nc(r,s)}l(aV,"_metaBinary");function uV(n,e){return nc([...e.shape],e.dtype)}l(uV,"_metaUnary");function lV(n,e,t,r){let s=[...e.shape],o=t!=null?Array.isArray(t)?t:[t]:Array.from({length:s.length},(u,p)=>p);if(o.length===s.length||t===void 0)return nc(r?s.map(()=>1):[],e.dtype);let i=[],a=new Set(o.map(u=>u<0?s.length+u:u));for(let u=0;u<s.length;u++)a.has(u)?r&&i.push(1):i.push(s[u]);return nc(i,e.dtype)}l(lV,"_metaReduction");function cV(n,e,t){let r=xg(e.shape,t.shape);if(r===null)throw new Error("metaMatmul: unsupported shapes");return nc(r,e.dtype)}l(cV,"metaMatmul");function pV(n,e){return nc([...e.shape],e.dtype)}l(pV,"metaClone");function fS(n,e){return Object.fromEntries(n.map(t=>[t,e]))}l(fS,"mapKernel");var FM=Object.freeze({...fS(["add","sub","mul","div","pow","rem","maximum","minimum"],aV),...fS(["neg","exp","log","sqrt","rsqrt","abs","sin","cos","tanh","erf","erfc","lgamma","gamma","sigmoid","relu","gelu","silu","sign","floor","ceil"],uV),...fS(["sum","mean","max","min","prod"],lV),matmul:cV,clone:pV});GT();be();kt();os();ss();function fV(n,e,t,r){let s=Ve(n.shape,{dtype:n.dtype}),o=nu(s,e,t,r),i=nu(s,e,t,tn(r.shape,{dtype:n.dtype}));return or(Il(i,Ve(i.shape,{dtype:i.dtype})),o,n)}l(fV,"scatterKernel");function dV(n,e){let t=n.shape,r=t.length;if(e.length<r)throw new Error("repeat: reps length must be >= tensor rank");let s=e.length-r,o=s>0?[...Array(s).fill(1),...t]:t.slice(),i=[],a=[],u=[];for(let p=0;p<o.length;p++)i.push(1,o[p]),a.push(e[p],o[p]),u.push(e[p]*o[p]);return Hn(Ll(Hn(n,i),a),u)}l(dV,"repeatKernel");function hV(n,e){let t=n.shape.length,r=e.length<t?[...Array(t-e.length).fill(1),...e]:e;return Dl(n,r)}l(hV,"tileKernel");function mV(n,e,t){let r=[],s=n.shape.length,o=t<0?s+t:t,i=0;for(let a of e)r.push(ir(n,o,i,a)),i+=a;return r}l(mV,"splitKernel");function gV(n,e,t){let r=n.shape.length,s=t<0?r+t:t,o=n.shape[s],i=Math.ceil(o/e);return hi(n,i,s)}l(gV,"chunkKernel");function yV(n,e,t=0){let r=n.shape.length,s=Ks(t,r),o=n.shape[s],i=(e%o+o)%o;return i===0?n:ru([ir(n,s,o-i,i),ir(n,s,0,o-i)],s)}l(yV,"rollKernel");function _V(n,e){let t=Array.isArray(e)?e:[e],r=n.shape.length,s=n;for(let o of t){let i=Ks(o,r),a=s.shape[i],u=new Array(a);for(let p=0;p<a;p++)u[p]=a-1-p;s=ra(s,i,Wt(u,{dtype:"i32"}))}return s}l(_V,"flipKernel");function bV(n,e=0){let t=n.shape.length,r=Ks(e,t),s=n.shape[r],o=n;for(let i=1;i<s;i*=2){let a=[...o.shape];a[r]=i;let u=ru([Ve(a,{dtype:o.dtype}),ir(o,r,0,s-i)],r);o=ae(o,u)}return o}l(bV,"cumsumKernel");function wV(n){let e=1;for(;e<n;)e<<=1;return e}l(wV,"nextPow2");function $y(n,e,t){let r=n.shape.length,s=r-1,o=n.shape[s],i=wV(o),a=e?-1/0:1/0,u=n;if(i>o){let f=new Array(r).fill(0),d=new Array(r).fill(0);d[s]=i-o,u=tu(n,f,d,a)}let p=new Array(r).fill(1);p[s]=i;let c=null;if(t){let f=new Array(i);for(let d=0;d<i;d++)f[d]=d;c=Hn(Wt(f,{dtype:"i32"}),p)}for(let f=2;f<=i;f<<=1)for(let d=f>>1;d>=1;d>>=1){let h=new Array(i),m=new Array(i);for(let x=0;x<i;x++){let S=x^d;h[x]=S;let T=(x&f)===0,v=e?!T:T;S>x?m[x]=v?1:0:m[x]=v?0:1}let g=Wt(h,{dtype:"i32"}),y=ra(u,s,g),_=vg(u,y),b=ta(u,y),w=Hn(Wt(m,{dtype:"f32"}),p);if(t){let x=ra(c,s,g),S=or(w,Yp(_,u),Yp(b,u));c=or(S,c,x)}u=or(w,_,b)}return i>o&&(u=ir(u,s,0,o),t&&(c=ir(c,s,0,o))),t?{values:u,indices:c}:u}l($y,"bitonicLastDim");function TV(n,e=-1,t=!1){let r=n.shape.length,s=Ks(e,r);if(s===r-1)return $y(n,t,!1);let o=vt(n,s,r-1),i=$y(o,t,!1);return vt(i,s,r-1)}l(TV,"sortKernel");function zM(n,e,t){let r=n.shape.length,s=Ks(e,r);if(s===r-1)return $y(n,t,!0);let o=vt(n,s,r-1),i=$y(o,t,!0);return{values:vt(i.values,s,r-1),indices:vt(i.indices,s,r-1)}}l(zM,"sortWithIndices");function xV(n,e=-1,t=!1){return zM(n,e,t).indices}l(xV,"argsortKernel");function SV(n,e,t=-1,r=!0){let s=n.shape.length,o=Ks(t,s),{values:i,indices:a}=zM(n,o,r);return[ir(i,o,0,e),ir(a,o,0,e)]}l(SV,"topkKernel");var VM=Object.freeze({scatter:l((n,e,t,r,s)=>fV(e,t,r,s),"scatter"),repeat:l((n,e,t)=>dV(e,t),"repeat"),tile:l((n,e,t)=>hV(e,t),"tile"),split:l((n,e,t,r)=>mV(e,t,r),"split"),chunk:l((n,e,t,r)=>gV(e,t,r),"chunk"),roll:l((n,e,t,r)=>yV(e,t,r),"roll"),flip:l((n,e,t)=>_V(e,t),"flip"),cumsum:l((n,e,t)=>bV(e,t),"cumsum"),sort:l((n,e,t,r)=>TV(e,t,r),"sort"),argsort:l((n,e,t,r)=>xV(e,t,r),"argsort"),topk:l((n,e,t,r,s)=>SV(e,t,r,s),"topk")});var vV=3,GM=!1,kV=[0,1,2,3,5];function jM(){if(GM)return;GM=!0;let n=new Vt("mlc","DEF");for(let t of BM())n.def(t);let e=new Vt("mlc","IMPL");for(let[t,r]of Object.entries(FM))e.impl(t,vV,r);for(let[t,r]of Object.entries({...BB,...VM}))for(let s of kV)e.impl(t,s,r);pS()}l(jM,"registerNativeOps");ys();An();ty();os();wn();qn();var NV=new Set(["cpu","wasm"]);function dS(n){let e=new Float64Array(n.length);for(let t=0;t<n.length;t++)e[t]=Number(n[t]);return e}l(dS,"float64From");function UM(n){if(!NV.has(n.device.type))throw new Error(`linalg/ml: host-readable device required (cpu or wasm), got '${n.device.type}'`)}l(UM,"requireHost");function Ye(n){if(n.ndim!==2)throw new Error(`linalg/ml: expected a 2-D matrix, got ${n.ndim}-D`);UM(n);let[e,t]=n.shape;return{data:dS(Qe(n)),rows:e,cols:t}}l(Ye,"hostMatrix");function $t(n){if(UM(n),n.ndim===1)return{data:dS(Qe(n)),rows:n.shape[0],cols:1,wasVector:!0};if(n.ndim===2)return{data:dS(Qe(n)),rows:n.shape[0],cols:n.shape[1],wasVector:!1};throw new Error(`linalg/ml: expected a 1-D or 2-D right-hand side, got ${n.ndim}-D`)}l($t,"hostColumns");function Q(n,e,t,r=Ws){return Wt(n,{shape:e,dtype:t,device:r})}l(Q,"toHostTensor");os();function qM(n,e){let t=Float64Array.from(n),r=new Int32Array(e);for(let o=0;o<e;o++)r[o]=o;let s=1;for(let o=0;o<e;o++){let i=o,a=Math.abs(t[o*e+o]);for(let p=o+1;p<e;p++){let c=Math.abs(t[p*e+o]);c>a&&(a=c,i=p)}if(a===0)throw new Error("linalg: matrix is singular");if(i!==o){for(let c=0;c<e;c++){let f=t[o*e+c];t[o*e+c]=t[i*e+c],t[i*e+c]=f}let p=r[o];r[o]=r[i],r[i]=p,s=-s}let u=t[o*e+o];for(let p=o+1;p<e;p++){let c=t[p*e+o]/u;t[p*e+o]=c;for(let f=o+1;f<e;f++)t[p*e+f]-=c*t[o*e+f]}}return{lu:t,piv:r,sign:s}}l(qM,"luFactor");function AV(n,e,t,r,s){let o=new Float64Array(t*s);for(let i=0;i<t;i++)for(let a=0;a<s;a++)o[i*s+a]=r[e[i]*s+a];for(let i=0;i<t;i++)for(let a=0;a<s;a++){let u=o[i*s+a];for(let p=0;p<i;p++)u-=n[i*t+p]*o[p*s+a];o[i*s+a]=u}for(let i=t-1;i>=0;i--){let a=n[i*t+i];for(let u=0;u<s;u++){let p=o[i*s+u];for(let c=i+1;c<t;c++)p-=n[i*t+c]*o[c*s+u];o[i*s+u]=p/a}}return o}l(AV,"luSolve");function hS(n,e,t,r){let{lu:s,piv:o}=qM(n,e);return AV(s,o,e,t,r)}l(hS,"solveHost");function WM(n,e){let t;try{t=qM(n,e)}catch{return 0}let r=t.sign;for(let s=0;s<e;s++)r*=t.lu[s*e+s];return r}l(WM,"detHost");function KM(n,e){let t=new Float64Array(e*e);for(let r=0;r<e;r++)for(let s=0;s<=r;s++){let o=n[r*e+s];for(let i=0;i<s;i++)o-=t[r*e+i]*t[s*e+i];if(r===s){if(o<=0)throw new Error("linalg.cholesky: matrix is not positive definite");t[r*e+s]=Math.sqrt(o)}else t[r*e+s]=o/t[s*e+s]}return t}l(KM,"choleskyHost");ef();function vf(n,e,t){let r=t?.tol??1e-12,s=t?.maxSweeps??100,o=Float64Array.from(n),i=new Float64Array(e*e);for(let c=0;c<e;c++)i[c*e+c]=1;for(let c=0;c<s;c++){let f=0;for(let d=0;d<e;d++)for(let h=d+1;h<e;h++)f+=o[d*e+h]*o[d*e+h];if(Math.sqrt(f)<r)break;for(let d=0;d<e;d++)for(let h=d+1;h<e;h++){let m=o[d*e+h];if(Math.abs(m)<1e-300)continue;let g=(o[h*e+h]-o[d*e+d])/(2*m),y=Math.sign(g||1)/(Math.abs(g)+Math.sqrt(g*g+1)),_=1/Math.sqrt(y*y+1),b=y*_;for(let w=0;w<e;w++){let x=o[w*e+d],S=o[w*e+h];o[w*e+d]=_*x-b*S,o[w*e+h]=b*x+_*S}for(let w=0;w<e;w++){let x=o[d*e+w],S=o[h*e+w];o[d*e+w]=_*x-b*S,o[h*e+w]=b*x+_*S}for(let w=0;w<e;w++){let x=i[w*e+d],S=i[w*e+h];i[w*e+d]=_*x-b*S,i[w*e+h]=b*x+_*S}}}let a=Array.from({length:e},(c,f)=>f).sort((c,f)=>o[c*e+c]-o[f*e+f]),u=new Float64Array(e),p=new Float64Array(e*e);for(let c=0;c<e;c++){let f=a[c];u[c]=o[f*e+f];for(let d=0;d<e;d++)p[d*e+c]=i[d*e+f]}return{values:u,vectors:p}}l(vf,"eighHost");ef();function RV(n,e,t,r){if(r){let o=new Float64Array(t*t);for(let i=0;i<t;i++)for(let a=i;a<t;a++){let u=0;for(let p=0;p<e;p++)u+=n[p*t+i]*n[p*t+a];o[i*t+a]=u,o[a*t+i]=u}return o}let s=new Float64Array(e*e);for(let o=0;o<e;o++)for(let i=o;i<e;i++){let a=0;for(let u=0;u<t;u++)a+=n[o*t+u]*n[i*t+u];s[o*e+i]=a,s[i*e+o]=a}return s}l(RV,"gram");function MV(n,e,t,r,s){let o=new Float64Array(e*s);for(let i=0;i<e;i++)for(let a=0;a<s;a++){let u=0;for(let p=0;p<t;p++)u+=n[i*t+p]*r[a*t+p];o[i*s+a]=u}return o}l(MV,"matmulRows");function mu(n,e,t,r,s=RV,o=MV){let i=r?.tol??1e-12,a=Math.min(e,t),u=new Float64Array(e*a),p=new Float64Array(a),c=new Float64Array(t*a);if(t<=e){let{values:f,vectors:d}=vf(s(n,e,t,!0),t,r),h=new Float64Array(a*t);for(let g=0;g<a;g++){let y=t-1-g;p[g]=Math.sqrt(Math.max(f[y],0));for(let _=0;_<t;_++){let b=d[_*t+y];c[_*a+g]=b,h[g*t+_]=b}}let m=o(n,e,t,h,a);for(let g=0;g<a;g++)if(p[g]>i)for(let y=0;y<e;y++)u[y*a+g]=m[y*a+g]/p[g]}else{let{values:f,vectors:d}=vf(s(n,e,t,!1),e,r),h=new Float64Array(a*e);for(let y=0;y<a;y++){let _=e-1-y;p[y]=Math.sqrt(Math.max(f[_],0));for(let b=0;b<e;b++){let w=d[b*e+_];u[b*a+y]=w,h[y*e+b]=w}}let m=new Float64Array(t*e);for(let y=0;y<e;y++)for(let _=0;_<t;_++)m[_*e+y]=n[y*t+_];let g=o(m,t,e,h,a);for(let y=0;y<a;y++)if(p[y]>i)for(let _=0;_<t;_++)c[_*a+y]=g[_*a+y]/p[y]}return{U:u,S:p,V:c,k:a}}l(mu,"svdHost");ef();function HM(n,e,t,r,s,o){let i=o?.rcond??1e-12,{U:a,S:u,V:p,k:c}=mu(n,e,t,o),f=i*(u.length?u[0]:0),d=new Float64Array(c*s);for(let m=0;m<c;m++){let g=u[m];for(let y=0;y<s;y++){let _=0;for(let b=0;b<e;b++)_+=a[b*c+m]*r[b*s+y];d[m*s+y]=g>f?_/g:0}}let h=new Float64Array(t*s);for(let m=0;m<t;m++)for(let g=0;g<s;g++){let y=0;for(let _=0;_<c;_++)y+=p[m*c+_]*d[_*s+g];h[m*s+g]=y}return h}l(HM,"lstsqHost");function YM(n,e,t,r){let s=r?.rcond??1e-12,{U:o,S:i,V:a,k:u}=mu(n,e,t,r),p=s*(i.length?i[0]:0),c=new Float64Array(t*e);for(let f=0;f<t;f++)for(let d=0;d<e;d++){let h=0;for(let m=0;m<u;m++){let g=i[m];g>p&&(h+=a[f*u+m]*(1/g)*o[d*u+m])}c[f*e+d]=h}return c}l(YM,"pinvHost");function JM(n,e,t){let r=new Float64Array(t);for(let i=0;i<t;i++){let a=0;for(let u=0;u<e;u++)a+=n[u*t+i];r[i]=a/e}let s=e>1?e-1:1,o=new Float64Array(t*t);for(let i=0;i<t;i++)for(let a=i;a<t;a++){let u=0;for(let c=0;c<e;c++)u+=(n[c*t+i]-r[i])*(n[c*t+a]-r[a]);let p=u/s;o[i*t+a]=p,o[a*t+i]=p}return o}l(JM,"covHost");function kf(n,e){let t=Ye(n);if(t.rows!==t.cols)throw new Error(`linalg.${e}: matrix must be square`);return t}l(kf,"squareMatrix");function ZM(n,e){let{data:t,rows:r}=kf(e,"cholesky");return Q(KM(t,r),[r,r],e.dtype,e.device)}l(ZM,"cpuCholesky");function QM(n,e,t){let{data:r,rows:s}=kf(e,"solve"),o=$t(t);if(o.rows!==s)throw new Error("linalg.solve: right-hand side rows must match matrix");let i=hS(r,s,o.data,o.cols);return Q(i,o.wasVector?[s]:[s,o.cols],e.dtype,e.device)}l(QM,"cpuSolve");function XM(n,e,t){let{data:r,rows:s,cols:o}=Ye(e),i=$t(t);if(i.rows!==s)throw new Error("linalg.lstsq: right-hand side rows must match matrix");let a=HM(r,s,o,i.data,i.cols);return Q(a,i.wasVector?[o]:[o,i.cols],e.dtype,e.device)}l(XM,"cpuLstsq");function eI(n,e){let{data:t,rows:r}=kf(e,"inv"),s=new Float64Array(r*r);for(let i=0;i<r;i++)s[i*r+i]=1;let o=hS(t,r,s,r);return Q(o,[r,r],e.dtype,e.device)}l(eI,"cpuInv");function tI(n,e){let{data:t,rows:r,cols:s}=Ye(e);return Q(YM(t,r,s),[s,r],e.dtype,e.device)}l(tI,"cpuPinv");function nI(n,e){let{data:t,rows:r}=kf(e,"det");return Kp(WM(t,r),{dtype:e.dtype})}l(nI,"cpuDet");function rI(n,e){let{data:t,rows:r,cols:s}=Ye(e);return Q(JM(t,r,s),[s,s],e.dtype,e.device)}l(rI,"cpuCov");function sI(n,e){let{data:t,rows:r}=kf(e,"eigh"),{values:s,vectors:o}=vf(t,r);return[Q(s,[r],e.dtype,e.device),Q(o,[r,r],e.dtype,e.device)]}l(sI,"cpuEigh");function Fy(n,e){let{data:t,rows:r,cols:s}=Ye(e),{U:o,S:i,V:a,k:u}=mu(t,r,s);return[Q(o,[r,u],e.dtype,e.device),Q(i,[u],e.dtype,e.device),Q(a,[s,u],e.dtype,e.device)]}l(Fy,"cpuSvd");var IV={svd:Fy,eigh:sI,cholesky:ZM,inv:eI,pinv:tI,det:nI,cov:rI,solve:QM,lstsq:XM},EV=[0,2],oI=!1;function iI(){if(oI)return;oI=!0,$l();let n=new Vt("mlc","IMPL");for(let[e,t]of Object.entries(IV))for(let r of EV)n.impl(e,r,t)}l(iI,"registerCpuLinalg");ys();An();ys();var LV=["kmeans(Tensor x, int n_clusters, int max_iter, int n_init, int seed) -> (Tensor, Tensor, Tensor)","kmeans_predict(Tensor x, Tensor centers) -> Tensor","knn_predict(Tensor x_train, Tensor y_train, Tensor x_query, int n_neighbors, bool classify) -> Tensor","gaussian_nb_fit(Tensor x, Tensor y) -> (Tensor, Tensor, Tensor, Tensor)","gaussian_nb_predict(Tensor x, Tensor means, Tensor variances, Tensor priors, Tensor classes) -> Tensor","elastic_net(Tensor x, Tensor y, float alpha, float l1_ratio, int max_iter, float tol, bool fit_intercept) -> (Tensor, Tensor)","decision_tree_fit(Tensor x, Tensor y, int max_depth, int min_split, int min_leaf, int max_features, bool classify, int seed) -> (Tensor, Tensor, Tensor, Tensor, Tensor)","decision_tree_predict(Tensor x, Tensor feature, Tensor threshold, Tensor left, Tensor right, Tensor value) -> Tensor"],aI=!1;function zy(){if(aI)return;aI=!0;let n=new Vt("mlc","DEF");for(let e of LV)n.def(e)}l(zy,"ensureMlSchemas");function xr(n){let e=(n??2654435769)>>>0;return l(function(){e|=0,e=e+1831565813|0;let r=Math.imul(e^e>>>15,1|e);return r=r+Math.imul(r^r>>>7,61|r)^r,((r^r>>>14)>>>0)/4294967296},"next")}l(xr,"makeRng");function rc(n,e){return Math.floor(n()*e)}l(rc,"randInt");function gS(n,e){let t=Array.from({length:n},(r,s)=>s);for(let r=n-1;r>0;r--){let s=rc(e,r+1),o=t[r];t[r]=t[s],t[s]=o}return t}l(gS,"shuffledIndices");function yS(n,e,t,r,s){let o=0;for(let i=0;i<s;i++){let a=n[e*s+i]-t[r*s+i];o+=a*a}return o}l(yS,"sqDist");function _S(n,e,t,r,s){let o=new Float64Array(r*t),i=rc(s,e);for(let u=0;u<t;u++)o[u]=n[i*t+u];let a=new Float64Array(e).fill(1/0);for(let u=1;u<r;u++){let p=0;for(let d=0;d<e;d++){let h=yS(n,d,o,u-1,t);h<a[d]&&(a[d]=h),p+=a[d]}let c=s()*p,f=e-1;for(let d=0;d<e;d++)if(c-=a[d],c<=0){f=d;break}for(let d=0;d<t;d++)o[u*t+d]=n[f*t+d]}return o}l(_S,"kmeansPlusPlus");function uI(n,e,t,r,s,o){let i=0;for(let a=0;a<e;a++){let u=0,p=1/0;for(let c=0;c<s;c++){let f=yS(n,a,r,c,t);f<p&&(p=f,u=c)}o[a]=u,i+=p}return i}l(uI,"assign");function bS(n,e,t,r,s,o){let i=new Float64Array(r*t),a=new Int32Array(r);for(let p=0;p<e;p++){let c=s[p];a[c]++;for(let f=0;f<t;f++)i[c*t+f]+=n[p*t+f]}let u=!1;for(let p=0;p<r;p++)if(a[p]!==0)for(let c=0;c<t;c++){let f=i[p*t+c]/a[p];f!==o[p*t+c]&&(u=!0),o[p*t+c]=f}return u}l(bS,"updateCenters");function CV(n,e,t,r,s,o){let i=_S(n,e,t,r,o),a=new Int32Array(e),u=1/0;for(let p=0;p<s&&(u=uI(n,e,t,i,r,a),!!bS(n,e,t,r,a,i));p++);return u=uI(n,e,t,i,r,a),{centers:i,labels:a,inertia:u}}l(CV,"lloyd");function Vy(n,e,t,r,s,o){let{data:i,rows:a,cols:u}=Ye(e),p=null;for(let f=0;f<s;f++){let d=xr(o+f*2654435769),h=CV(i,a,u,t,r,d);(!p||h.inertia<p.inertia)&&(p=h)}let c=new Float64Array(a);for(let f=0;f<a;f++)c[f]=p.labels[f];return[Q(p.centers,[t,u],e.dtype,e.device),Q(c,[a],e.dtype,e.device),Q(new Float64Array([p.inertia]),[1],e.dtype,e.device)]}l(Vy,"cpuKmeans");function Gy(n,e,t){let{data:r,rows:s,cols:o}=Ye(e),i=Ye(t),a=new Float64Array(s);for(let u=0;u<s;u++){let p=0,c=1/0;for(let f=0;f<i.rows;f++){let d=yS(r,u,i.data,f,o);d<c&&(c=d,p=f)}a[u]=p}return Q(a,[s],e.dtype,e.device)}l(Gy,"cpuKmeansPredict");function wS(n,e,t,r){for(let s=0;s<t;s++)e[s]=s;for(let s=0;s<r;s++){let o=s;for(let a=s+1;a<t;a++)n[e[a]]<n[e[o]]&&(o=a);let i=e[s];e[s]=e[o],e[o]=i}}l(wS,"topKSelect");function TS(n,e,t,r){if(r){let o=new Map,i=n[e[0]],a=0;for(let u=0;u<t;u++){let p=n[e[u]],c=(o.get(p)||0)+1;o.set(p,c),c>a&&(a=c,i=p)}return i}let s=0;for(let o=0;o<t;o++)s+=n[e[o]];return s/t}l(TS,"knnVote");function jy(n,e,t,r,s,o){let i=Ye(e),a=Ye(r),u=$t(t),p=i.cols,c=Math.min(s,i.rows),f=new Float64Array(a.rows),d=new Float64Array(i.rows),h=new Int32Array(i.rows);for(let m=0;m<a.rows;m++){for(let g=0;g<i.rows;g++){let y=0;for(let _=0;_<p;_++){let b=a.data[m*p+_]-i.data[g*p+_];y+=b*b}d[g]=y}wS(d,h,i.rows,c),f[m]=TS(u.data,h,c,o)}return Q(f,[a.rows],r.dtype,r.device)}l(jy,"cpuKnnPredict");be();os();function ks(n,e){let t=Wt(Int32Array.from(e),{shape:[e.length],dtype:"i32",device:n.device});return ct("index_select",n,t,0)}l(ks,"takeRows");function sc(n){return Ye(n)}l(sc,"matrixOf");function Yt(n){let e=$t(n);if(!e.wasVector&&e.cols!==1)throw new Error("ml: expected a 1-D target or single-column matrix");return{data:e.data,n:e.rows}}l(Yt,"vectorOf");function Nf(n,e,t,r){return Q(n,[e,t],r??"f32")}l(Nf,"matrix");function xS(n,e,t){return Q(n,[e],t??"f32")}l(xS,"vector");function Ns(n,e){let t=[],r=new Map,s=new Int32Array(e);for(let o=0;o<e;o++){let i=n[o],a=r.get(i);a===void 0&&(a=t.length,r.set(i,a),t.push(i)),s[o]=a}return{y:s,classes:t}}l(Ns,"encodeLabels");var DV=1e-9;function lI(n,e,t){let{data:r,rows:s,cols:o}=Ye(e),i=$t(t),{y:a,classes:u}=Ns(i.data,i.rows),p=u.length,c=new Float64Array(p*o),f=new Float64Array(p*o),d=new Float64Array(p),h=new Int32Array(p);for(let _=0;_<s;_++){let b=a[_];h[b]++;for(let w=0;w<o;w++)c[b*o+w]+=r[_*o+w]}for(let _=0;_<p;_++){for(let b=0;b<o;b++)c[_*o+b]/=h[_];d[_]=h[_]/s}for(let _=0;_<s;_++){let b=a[_];for(let w=0;w<o;w++){let x=r[_*o+w]-c[b*o+w];f[b*o+w]+=x*x}}let m=0;for(let _=0;_<p;_++)for(let b=0;b<o;b++)f[_*o+b]/=h[_],f[_*o+b]>m&&(m=f[_*o+b]);let g=DV*m;for(let _=0;_<p*o;_++)f[_]+=g;let y=new Float64Array(p);for(let _=0;_<p;_++)y[_]=u[_];return[Q(c,[p,o],e.dtype,e.device),Q(f,[p,o],e.dtype,e.device),Q(d,[p],e.dtype,e.device),Q(y,[p],e.dtype,e.device)]}l(lI,"cpuGaussianNbFit");function cI(n,e,t,r,s,o){let{data:i,rows:a,cols:u}=Ye(e),p=Ye(t),c=Ye(r),f=$t(s),d=$t(o),h=p.rows,m=new Float64Array(a);for(let g=0;g<a;g++){let y=0,_=-1/0;for(let b=0;b<h;b++){let w=Math.log(f.data[b]);for(let x=0;x<u;x++){let S=c.data[b*u+x],T=i[g*u+x]-p.data[b*u+x];w+=-.5*(Math.log(2*Math.PI*S)+T*T/S)}w>_&&(_=w,y=b)}m[g]=d.data[y]}return Q(m,[a],e.dtype,e.device)}l(cI,"cpuGaussianNbPredict");function PV(n,e){return n>e?n-e:n<-e?n+e:0}l(PV,"softThreshold");function SS(n,e,t,r,s){let o=new Float64Array(t),i=0;if(s){for(let c=0;c<t;c++){let f=0;for(let d=0;d<e;d++)f+=n[d*t+c];o[c]=f/e}for(let c=0;c<e;c++)i+=r[c];i/=e}let a=new Float64Array(e*t);for(let c=0;c<e;c++)for(let f=0;f<t;f++)a[c*t+f]=n[c*t+f]-o[f];let u=new Float64Array(e);for(let c=0;c<e;c++)u[c]=r[c]-i;let p=new Float64Array(t);for(let c=0;c<t;c++){let f=0;for(let d=0;d<e;d++)f+=a[d*t+c]*a[d*t+c];p[c]=f/e}return{xc:a,r:u,z:p,meanX:o,meanY:i}}l(SS,"elasticNetPrep");function vS(n,e,t,r){let s=e;for(let o=0;o<r;o++)s-=n[o]*t[o];return s}l(vS,"elasticNetIntercept");function $V(n,e,t,r,s,o,i,a,u,p){for(let c=0;c<u;c++){let f=0;for(let d=0;d<t;d++){if(s[d]===0)continue;let h=0;for(let _=0;_<e;_++)h+=n[_*t+d]*r[_];let m=h/e+o[d]*s[d],g=PV(m,i)/(s[d]+a),y=g-o[d];if(y!==0){for(let _=0;_<e;_++)r[_]-=y*n[_*t+d];o[d]=g,Math.abs(y)>f&&(f=Math.abs(y))}}if(f<p)break}}l($V,"coordinateDescent");function Uy(n,e,t,r,s,o,i,a){let{data:u,rows:p,cols:c}=Ye(e),f=$t(t),{xc:d,r:h,z:m,meanX:g,meanY:y}=SS(u,p,c,f.data,a),_=new Float64Array(c),b=r*s,w=r*(1-s);$V(d,p,c,h,m,_,b,w,o,i);let x=vS(g,y,_,c);return[Q(_,[c],e.dtype,e.device),Q(new Float64Array([x]),[1],e.dtype,e.device)]}l(Uy,"cpuElasticNet");function FV(n,e,t){if(e<=0||e>=n)return Array.from({length:n},(s,o)=>o);let r=Array.from({length:n},(s,o)=>o);for(let s=n-1;s>n-1-e;s--){let o=Math.floor(t()*(s+1)),i=r[s];r[s]=r[o],r[o]=i}return r.slice(n-e)}l(FV,"chooseFeatures");function zV(n,e,t,r,s,o,i){let a=t.length,u=null;for(let p of r){let c=t.slice().sort((m,g)=>n[m*e+p]-n[g*e+p]),f=new Float64Array(s),d=new Float64Array(s);for(let m of c)d[o[m]]++;let h=0;for(let m=0;m<a-1;m++){let g=o[c[m]];f[g]++,d[g]--,h++;let y=a-h,_=n[c[m]*e+p],b=n[c[m+1]*e+p];if(_===b||h<i||y<i)continue;let w=1,x=1;for(let T=0;T<s;T++){let v=f[T]/h,A=d[T]/y;w-=v*v,x-=A*A}let S=(h*w+y*x)/a;(!u||S<u.impurity)&&(u={feature:p,threshold:(_+b)/2,impurity:S})}}return u}l(zV,"bestSplitClassify");function VV(n,e,t,r,s,o){let i=t.length,a=null;for(let u of r){let p=t.slice().sort((g,y)=>n[g*e+u]-n[y*e+u]),c=0,f=0;for(let g of p)c+=s[g],f+=s[g]*s[g];let d=0,h=0,m=0;for(let g=0;g<i-1;g++){let y=s[p[g]];d+=y,h+=y*y,m++;let _=i-m,b=n[p[g]*e+u],w=n[p[g+1]*e+u];if(b===w||m<o||_<o)continue;let x=h-d*d/m,S=c-d,v=f-h-S*S/_,A=x+v;(!a||A<a.impurity)&&(a={feature:u,threshold:(b+w)/2,impurity:A})}}return a}l(VV,"bestSplitRegress");function pI(n,e,t,r,s,o,i,a,u){let p=Ye(e),c=p.data,f=p.cols,d=$t(t),h=xr(u),m=null,g=null,y=0;if(a){let N=Ns(d.data,d.rows);m=N.y,g=N.classes,y=g.length}let _=[],b=[],w=[],x=[],S=[],T=l(N=>{if(a){let O=new Float64Array(y);for(let B of N)O[m[B]]++;let R=0;for(let B=1;B<y;B++)O[B]>O[R]&&(R=B);return g[R]}let k=0;for(let O of N)k+=d.data[O];return k/N.length},"leafValue"),v=l(N=>{let k=a?m:d.data,O=k[N[0]];for(let R of N)if(k[R]!==O)return!1;return!0},"isPure"),A=l((N,k)=>{let O=_.length;if(_.push(-1),b.push(0),w.push(-1),x.push(-1),S.push(0),k>=r||N.length<s||v(N))return S[O]=T(N),O;let R=FV(f,i,h),B=a?zV(c,f,N,R,y,m,o):VV(c,f,N,R,d.data,o);if(!B)return S[O]=T(N),O;let E=[],F=[];for(let C of N)c[C*f+B.feature]<=B.threshold?E.push(C):F.push(C);return _[O]=B.feature,b[O]=B.threshold,w[O]=A(E,k+1),x[O]=A(F,k+1),O},"build");return A(Array.from({length:p.rows},(N,k)=>k),0),[Q(Float64Array.from(_),[_.length],e.dtype,e.device),Q(Float64Array.from(b),[b.length],e.dtype,e.device),Q(Float64Array.from(w),[w.length],e.dtype,e.device),Q(Float64Array.from(x),[x.length],e.dtype,e.device),Q(Float64Array.from(S),[S.length],e.dtype,e.device)]}l(pI,"cpuDecisionTreeFit");function fI(n,e,t,r,s,o,i){let a=Ye(e),u=$t(t).data,p=$t(r).data,c=$t(s).data,f=$t(o).data,d=$t(i).data,h=new Float64Array(a.rows),m=a.cols;for(let g=0;g<a.rows;g++){let y=0;for(;u[y]!==-1;)a.data[g*m+u[y]]<=p[y]?y=c[y]:y=f[y];h[g]=d[y]}return Q(h,[a.rows],e.dtype,e.device)}l(fI,"cpuDecisionTreePredict");var GV={kmeans:Vy,kmeans_predict:Gy,knn_predict:jy,gaussian_nb_fit:lI,gaussian_nb_predict:cI,elastic_net:Uy,decision_tree_fit:pI,decision_tree_predict:fI},jV=[0,2],UV=[1,5];function qV(n){return()=>{throw new Error(`ml.${n}: scalar-iterative algorithm runs on CPU/WASM only; GPU/WebGPU not supported (no performance benefit)`)}}l(qV,"unsupported");var dI=!1;function hI(){if(dI)return;dI=!0,zy();let n=new Vt("mlc","IMPL");for(let[e,t]of Object.entries(GV)){for(let r of jV)n.impl(e,r,t);for(let r of UV)n.impl(e,r,qV(e))}}l(hI,"registerCpuMl");ys();An();ys();var WV=["qr(Tensor input) -> (Tensor, Tensor)","fft(Tensor input) -> Tensor","ifft(Tensor input) -> Tensor"],mI=!1;function gI(){if(mI)return;mI=!0;let n=new Vt("mlc","DEF");for(let e of WV)n.def(e)}l(gI,"ensureNumericSchemas");wn();function yI(n,e,t){let r=Math.min(e,t),s=Float64Array.from(n),o=[];for(let u=0;u<r;u++){let p=0;for(let h=u;h<e;h++)p+=s[h*t+u]*s[h*t+u];if(p=Math.sqrt(p),p===0){o.push(null);continue}let c=s[u*t+u]>=0?-p:p,f=new Float64Array(e-u);f[0]=s[u*t+u]-c;for(let h=u+1;h<e;h++)f[h-u]=s[h*t+u];let d=0;for(let h=0;h<f.length;h++)d+=f[h]*f[h];if(d===0){o.push(null);continue}o.push(f);for(let h=u;h<t;h++){let m=0;for(let y=0;y<f.length;y++)m+=f[y]*s[(u+y)*t+h];let g=2*m/d;for(let y=0;y<f.length;y++)s[(u+y)*t+h]-=g*f[y]}}let i=new Float64Array(e*r);for(let u=0;u<r;u++)i[u*r+u]=1;for(let u=r-1;u>=0;u--){let p=o[u];if(!p)continue;let c=0;for(let f=0;f<p.length;f++)c+=p[f]*p[f];for(let f=0;f<r;f++){let d=0;for(let m=0;m<p.length;m++)d+=p[m]*i[(u+m)*r+f];let h=2*d/c;for(let m=0;m<p.length;m++)i[(u+m)*r+f]-=h*p[m]}}let a=new Float64Array(r*t);for(let u=0;u<r;u++)for(let p=u;p<t;p++)a[u*t+p]=s[u*t+p];return{Q:i,R:a,k:r}}l(yI,"qrHost");function KV(n,e){let t=n.length;for(let r=1,s=0;r<t;r++){let o=t>>1;for(;s&o;o>>=1)s^=o;s^=o,r<s&&([n[r],n[s]]=[n[s],n[r]],[e[r],e[s]]=[e[s],e[r]])}}l(KV,"bitReversePermute");function qy(n,e,t){let r=n.length;KV(n,e);for(let s=2;s<=r;s<<=1){let o=(t?1:-1)*2*Math.PI/s,i=Math.cos(o),a=Math.sin(o);for(let u=0;u<r;u+=s){let p=1,c=0,f=s>>1;for(let d=0;d<f;d++){let h=n[u+d],m=e[u+d],g=n[u+d+f]*p-e[u+d+f]*c,y=n[u+d+f]*c+e[u+d+f]*p;n[u+d]=h+g,e[u+d]=m+y,n[u+d+f]=h-g,e[u+d+f]=m-y;let _=p*i-c*a;c=p*a+c*i,p=_}}}if(t)for(let s=0;s<r;s++)n[s]/=r,e[s]/=r}l(qy,"fftRadix2");function HV(n){let e=1;for(;e<n;)e<<=1;return e}l(HV,"nextPow2");function YV(n,e,t){let r=n.length,s=HV(2*r-1),o=t?1:-1,i=new Float64Array(r),a=new Float64Array(r);for(let d=0;d<r;d++){let h=d*d%(2*r),m=o*Math.PI*h/r;i[d]=Math.cos(m),a[d]=Math.sin(m)}let u=new Float64Array(s),p=new Float64Array(s);for(let d=0;d<r;d++)u[d]=n[d]*i[d]-e[d]*a[d],p[d]=n[d]*a[d]+e[d]*i[d];let c=new Float64Array(s),f=new Float64Array(s);c[0]=i[0],f[0]=-a[0];for(let d=1;d<r;d++)c[d]=i[d],f[d]=-a[d],c[s-d]=i[d],f[s-d]=-a[d];qy(u,p,!1),qy(c,f,!1);for(let d=0;d<s;d++){let h=u[d]*c[d]-p[d]*f[d];p[d]=u[d]*f[d]+p[d]*c[d],u[d]=h}qy(u,p,!0);for(let d=0;d<r;d++)n[d]=u[d]*i[d]-p[d]*a[d],e[d]=u[d]*a[d]+p[d]*i[d];if(t)for(let d=0;d<r;d++)n[d]/=r,e[d]/=r}l(YV,"bluestein");function _I(n,e,t){let r=n.length;r!==0&&((r&r-1)===0?qy(n,e,t):YV(n,e,t))}l(_I,"fftHost");function bI(n,e){let{data:t,rows:r,cols:s}=Ye(e),{Q:o,R:i,k:a}=yI(t,r,s);return[Q(o,[r,a],e.dtype,e.device),Q(i,[a,s],e.dtype,e.device)]}l(bI,"cpuQr");function JV(n){let e=Qe(n);if(n.ndim===1){let t=n.shape[0];return{re:Float64Array.from(e),im:new Float64Array(t),n:t}}if(n.ndim===2&&n.shape[1]===2){let t=n.shape[0],r=new Float64Array(t),s=new Float64Array(t);for(let o=0;o<t;o++)r[o]=e[2*o],s[o]=e[2*o+1];return{re:r,im:s,n:t}}throw new Error(`fft: expected a 1-D real signal or [n, 2] complex tensor, got shape [${n.shape}]`)}l(JV,"splitComplex");function ZV(n,e,t){let r=new Float64Array(2*t);for(let s=0;s<t;s++)r[2*s]=n[s],r[2*s+1]=e[s];return r}l(ZV,"interleave");function wI(n,e){let{re:t,im:r,n:s}=JV(n);return _I(t,r,e),Q(ZV(t,r,s),[s,2],n.dtype,n.device)}l(wI,"transform");function TI(n,e){return wI(e,!1)}l(TI,"cpuFft");function xI(n,e){return wI(e,!0)}l(xI,"cpuIfft");var QV={qr:bI,fft:TI,ifft:xI},XV=[0,2],SI=!1;function vI(){if(SI)return;SI=!0,gI();let n=new Vt("mlc","IMPL");for(let[e,t]of Object.entries(QV))for(let r of XV)n.impl(e,r,t)}l(vI,"registerCpuNumeric");ys();An();ty();Wm();var pt=8,NS=4,oc=2,eG=oc*pt,gu=Math.log2(oc),tG=65536,kI=eG,nG=64*1024*1024;function NI(n,e){return Math.ceil(n/e)*e}l(NI,"alignUp");var kS=class{static{l(this,"SimdModule")}constructor(e,t){this._module=e,this._exportName=t,this._instantiate()}_instantiate(){this._instance=new WebAssembly.Instance(this._module,{}),this._memory=this._instance.exports.memory,this._fn=this._instance.exports[this._exportName],this._top=0}reset(){this._top=0}reclaim(){this._memory.buffer.byteLength>nG?this._instantiate():this._top=0}_ensure(e){let t=this._memory.buffer.byteLength;e<=t||this._memory.grow(Math.ceil((e-t)/tG))}alloc(e){let t=NI(this._top,kI);return this._top=t+NI(e,kI),this._ensure(this._top),t}allocF64(e){return this.alloc(e*pt)}allocI32(e){return this.alloc(e*NS)}f64(e,t){return new Float64Array(this._memory.buffer,e,t)}i32(e,t){return new Int32Array(this._memory.buffer,e,t)}writeF64(e,t){this.f64(e,t.length).set(t)}run(...e){this._fn(...e)}},AI=new Map;function Po(n,e,t){let r=AI.get(n);return r||(r=new kS(new WebAssembly.Module(Sl(e())),t),AI.set(n,r)),r.reclaim(),r}l(Po,"simdModule");var rG=1e308,sG=1,oG=32768;function Af(n,e,t,r){let s=e.map(([i,a])=>`(param $${i} ${a})`).join(" "),o=t.map(([i,a])=>`(local $${i} ${a})`).join(" ");return`(module (memory (export "memory") ${sG} ${oG})
|
|
252
|
+
(func (export "${n}") ${s}
|
|
253
|
+
${o}
|
|
254
|
+
${r}))`}l(Af,"moduleWrap");function OI(n,e,t,r,s,o){return`
|
|
255
|
+
(local.get $${r}) (i32.const ${gu}) i32.shr_s (i32.const ${gu}) i32.shl local.set $${o.nmain}
|
|
256
|
+
(f64.const 0) f64x2.splat local.set $${o.acc}
|
|
257
|
+
(i32.const 0) local.set $${o.j}
|
|
258
|
+
(block $${n}_ve (loop $${n}_vl
|
|
259
|
+
(local.get $${o.j}) (local.get $${o.nmain}) i32.ge_s br_if $${n}_ve
|
|
260
|
+
(local.get $${e}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add v128.load local.set $${o.va}
|
|
261
|
+
(local.get $${t}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add v128.load local.set $${o.vb}
|
|
262
|
+
(local.get $${o.acc})
|
|
263
|
+
(local.get $${o.va}) (local.get $${o.vb}) f64x2.sub local.set $${o.va}
|
|
264
|
+
(local.get $${o.va}) (local.get $${o.va}) f64x2.mul
|
|
265
|
+
f64x2.add local.set $${o.acc}
|
|
266
|
+
(local.get $${o.j}) (i32.const ${oc}) i32.add local.set $${o.j}
|
|
267
|
+
br $${n}_vl))
|
|
268
|
+
(local.get $${o.acc}) f64x2.extract_lane 0
|
|
269
|
+
(local.get $${o.acc}) f64x2.extract_lane 1
|
|
270
|
+
f64.add local.set $${s}
|
|
271
|
+
(block $${n}_te (loop $${n}_tl
|
|
272
|
+
(local.get $${o.j}) (local.get $${r}) i32.ge_s br_if $${n}_te
|
|
273
|
+
(local.get $${e}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add f64.load
|
|
274
|
+
(local.get $${t}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add f64.load
|
|
275
|
+
f64.sub local.set $${o.diff}
|
|
276
|
+
(local.get $${s}) (local.get $${o.diff}) (local.get $${o.diff}) f64.mul f64.add local.set $${s}
|
|
277
|
+
(local.get $${o.j}) (i32.const 1) i32.add local.set $${o.j}
|
|
278
|
+
br $${n}_tl))`}l(OI,"sqDistSnippet");var BI={j:"j",nmain:"nmain",acc:"acc",va:"va",vb:"vb",diff:"diff"},RI=[["j","i32"],["nmain","i32"],["acc","v128"],["va","v128"],["vb","v128"],["diff","f64"]];function AS(){let n=`
|
|
279
|
+
(f64.const 0) local.set $inertiaAcc
|
|
280
|
+
(i32.const 0) local.set $i
|
|
281
|
+
(block $iend (loop $iloop
|
|
282
|
+
(local.get $i) (local.get $n) i32.ge_s br_if $iend
|
|
283
|
+
(local.get $x) (local.get $i) (local.get $d) i32.mul (i32.const ${pt}) i32.mul i32.add local.set $xrow
|
|
284
|
+
(i32.const 0) local.set $best
|
|
285
|
+
(f64.const ${rG}) local.set $bestd
|
|
286
|
+
(i32.const 0) local.set $cc
|
|
287
|
+
(block $cend (loop $cloop
|
|
288
|
+
(local.get $cc) (local.get $k) i32.ge_s br_if $cend
|
|
289
|
+
(local.get $c) (local.get $cc) (local.get $d) i32.mul (i32.const ${pt}) i32.mul i32.add local.set $crow
|
|
290
|
+
${OI("nc","xrow","crow","d","dist",BI)}
|
|
291
|
+
(local.get $dist) (local.get $bestd) f64.lt
|
|
292
|
+
(if (then
|
|
293
|
+
(local.get $dist) local.set $bestd
|
|
294
|
+
(local.get $cc) local.set $best))
|
|
295
|
+
(local.get $cc) (i32.const 1) i32.add local.set $cc
|
|
296
|
+
br $cloop))
|
|
297
|
+
(local.get $labels) (local.get $i) (i32.const ${NS}) i32.mul i32.add (local.get $best) i32.store
|
|
298
|
+
(local.get $inertiaAcc) (local.get $bestd) f64.add local.set $inertiaAcc
|
|
299
|
+
(local.get $i) (i32.const 1) i32.add local.set $i
|
|
300
|
+
br $iloop))
|
|
301
|
+
(local.get $inertia) (local.get $inertiaAcc) f64.store`;return Af("nearest_centroid",[["x","i32"],["n","i32"],["d","i32"],["c","i32"],["k","i32"],["labels","i32"],["inertia","i32"]],[["i","i32"],["cc","i32"],["best","i32"],["bestd","f64"],["dist","f64"],["xrow","i32"],["crow","i32"],["inertiaAcc","f64"],...RI],n)}l(AS,"nearestCentroidWat");function MI(){let n=`
|
|
302
|
+
(i32.const 0) local.set $t
|
|
303
|
+
(block $tend (loop $tloop
|
|
304
|
+
(local.get $t) (local.get $ntr) i32.ge_s br_if $tend
|
|
305
|
+
(local.get $tr) (local.get $t) (local.get $d) i32.mul (i32.const ${pt}) i32.mul i32.add local.set $trow
|
|
306
|
+
${OI("dr","q","trow","d","dist",BI)}
|
|
307
|
+
(local.get $out) (local.get $t) (i32.const ${pt}) i32.mul i32.add (local.get $dist) f64.store
|
|
308
|
+
(local.get $t) (i32.const 1) i32.add local.set $t
|
|
309
|
+
br $tloop))`;return Af("dist_row",[["q","i32"],["tr","i32"],["ntr","i32"],["d","i32"],["out","i32"]],[["t","i32"],["trow","i32"],["dist","f64"],...RI],n)}l(MI,"distRowWat");function OS(n,e,t,r,s,o){return`
|
|
310
|
+
(local.get $${r}) (i32.const ${gu}) i32.shr_s (i32.const ${gu}) i32.shl local.set $${o.nmain}
|
|
311
|
+
(f64.const 0) f64x2.splat local.set $${o.acc}
|
|
312
|
+
(i32.const 0) local.set $${o.j}
|
|
313
|
+
(block $${n}_ve (loop $${n}_vl
|
|
314
|
+
(local.get $${o.j}) (local.get $${o.nmain}) i32.ge_s br_if $${n}_ve
|
|
315
|
+
(local.get $${o.acc})
|
|
316
|
+
(local.get $${e}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add v128.load
|
|
317
|
+
(local.get $${t}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add v128.load
|
|
318
|
+
f64x2.mul f64x2.add local.set $${o.acc}
|
|
319
|
+
(local.get $${o.j}) (i32.const ${oc}) i32.add local.set $${o.j}
|
|
320
|
+
br $${n}_vl))
|
|
321
|
+
(local.get $${o.acc}) f64x2.extract_lane 0
|
|
322
|
+
(local.get $${o.acc}) f64x2.extract_lane 1
|
|
323
|
+
f64.add local.set $${s}
|
|
324
|
+
(block $${n}_te (loop $${n}_tl
|
|
325
|
+
(local.get $${o.j}) (local.get $${r}) i32.ge_s br_if $${n}_te
|
|
326
|
+
(local.get $${s})
|
|
327
|
+
(local.get $${e}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add f64.load
|
|
328
|
+
(local.get $${t}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add f64.load
|
|
329
|
+
f64.mul f64.add local.set $${s}
|
|
330
|
+
(local.get $${o.j}) (i32.const 1) i32.add local.set $${o.j}
|
|
331
|
+
br $${n}_tl))`}l(OS,"reduceDotSnippet");function iG(n,e,t,r,s,o){return`
|
|
332
|
+
(local.get $${r}) f64x2.splat local.set $${o.sv}
|
|
333
|
+
(local.get $${s}) (i32.const ${gu}) i32.shr_s (i32.const ${gu}) i32.shl local.set $${o.nmain}
|
|
334
|
+
(i32.const 0) local.set $${o.j}
|
|
335
|
+
(block $${n}_ve (loop $${n}_vl
|
|
336
|
+
(local.get $${o.j}) (local.get $${o.nmain}) i32.ge_s br_if $${n}_ve
|
|
337
|
+
(local.get $${e}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add local.set $${o.ya}
|
|
338
|
+
(local.get $${o.ya}) v128.load
|
|
339
|
+
(local.get $${o.sv})
|
|
340
|
+
(local.get $${t}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add v128.load
|
|
341
|
+
f64x2.mul f64x2.sub local.set $${o.vy}
|
|
342
|
+
(local.get $${o.ya}) (local.get $${o.vy}) v128.store
|
|
343
|
+
(local.get $${o.j}) (i32.const ${oc}) i32.add local.set $${o.j}
|
|
344
|
+
br $${n}_vl))
|
|
345
|
+
(block $${n}_te (loop $${n}_tl
|
|
346
|
+
(local.get $${o.j}) (local.get $${s}) i32.ge_s br_if $${n}_te
|
|
347
|
+
(local.get $${e}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add local.set $${o.ya}
|
|
348
|
+
(local.get $${o.ya})
|
|
349
|
+
(local.get $${o.ya}) f64.load
|
|
350
|
+
(local.get $${r})
|
|
351
|
+
(local.get $${t}) (local.get $${o.j}) (i32.const ${pt}) i32.mul i32.add f64.load
|
|
352
|
+
f64.mul f64.sub f64.store
|
|
353
|
+
(local.get $${o.j}) (i32.const 1) i32.add local.set $${o.j}
|
|
354
|
+
br $${n}_tl))`}l(iG,"axpySnippet");function II(){let e=`
|
|
355
|
+
(i32.const 0) local.set $i
|
|
356
|
+
(block $ie (loop $il
|
|
357
|
+
(local.get $i) (local.get $m) i32.ge_s br_if $ie
|
|
358
|
+
(local.get $mat) (local.get $i) (local.get $len) i32.mul (i32.const ${pt}) i32.mul i32.add local.set $rowI
|
|
359
|
+
(local.get $i) local.set $j
|
|
360
|
+
(block $je (loop $jl
|
|
361
|
+
(local.get $j) (local.get $m) i32.ge_s br_if $je
|
|
362
|
+
(local.get $mat) (local.get $j) (local.get $len) i32.mul (i32.const ${pt}) i32.mul i32.add local.set $rowJ
|
|
363
|
+
${OS("gs","rowI","rowJ","len","d",{j:"kk",nmain:"nm",acc:"acc"})}
|
|
364
|
+
(local.get $g) (local.get $i) (local.get $m) i32.mul (local.get $j) i32.add (i32.const ${pt}) i32.mul i32.add (local.get $d) f64.store
|
|
365
|
+
(local.get $g) (local.get $j) (local.get $m) i32.mul (local.get $i) i32.add (i32.const ${pt}) i32.mul i32.add (local.get $d) f64.store
|
|
366
|
+
(local.get $j) (i32.const 1) i32.add local.set $j
|
|
367
|
+
br $jl))
|
|
368
|
+
(local.get $i) (i32.const 1) i32.add local.set $i
|
|
369
|
+
br $il))`;return Af("gram_sym",[["mat","i32"],["m","i32"],["len","i32"],["g","i32"]],[["i","i32"],["j","i32"],["rowI","i32"],["rowJ","i32"],["d","f64"],["kk","i32"],["nm","i32"],["acc","v128"]],e)}l(II,"gramSymWat");function EI(){let e=`
|
|
370
|
+
(i32.const 0) local.set $i
|
|
371
|
+
(block $ie (loop $il
|
|
372
|
+
(local.get $i) (local.get $m) i32.ge_s br_if $ie
|
|
373
|
+
(local.get $a) (local.get $i) (local.get $len) i32.mul (i32.const ${pt}) i32.mul i32.add local.set $rowA
|
|
374
|
+
(i32.const 0) local.set $c
|
|
375
|
+
(block $ce (loop $cl
|
|
376
|
+
(local.get $c) (local.get $p) i32.ge_s br_if $ce
|
|
377
|
+
(local.get $b) (local.get $c) (local.get $len) i32.mul (i32.const ${pt}) i32.mul i32.add local.set $rowB
|
|
378
|
+
${OS("mm","rowA","rowB","len","d",{j:"kk",nmain:"nm",acc:"acc"})}
|
|
379
|
+
(local.get $out) (local.get $i) (local.get $p) i32.mul (local.get $c) i32.add (i32.const ${pt}) i32.mul i32.add (local.get $d) f64.store
|
|
380
|
+
(local.get $c) (i32.const 1) i32.add local.set $c
|
|
381
|
+
br $cl))
|
|
382
|
+
(local.get $i) (i32.const 1) i32.add local.set $i
|
|
383
|
+
br $il))`;return Af("matmul_rows",[["a","i32"],["m","i32"],["len","i32"],["b","i32"],["p","i32"],["out","i32"]],[["i","i32"],["c","i32"],["rowA","i32"],["rowB","i32"],["d","f64"],["kk","i32"],["nm","i32"],["acc","v128"]],e)}l(EI,"matmulRowsWat");function LI(){let n={j:"j",nmain:"nmain",acc:"acc"},e={j:"j2",nmain:"nmain2",sv:"sv",ya:"ya",vy:"vy"},t=`
|
|
384
|
+
(local.get $n) f64.convert_i32_s local.set $nf
|
|
385
|
+
(i32.const 0) local.set $iter
|
|
386
|
+
(block $ie (loop $il
|
|
387
|
+
(local.get $iter) (local.get $maxIter) i32.ge_s br_if $ie
|
|
388
|
+
(f64.const 0) local.set $maxChange
|
|
389
|
+
(i32.const 0) local.set $jc
|
|
390
|
+
(block $je (loop $jl
|
|
391
|
+
(local.get $jc) (local.get $d) i32.ge_s br_if $je
|
|
392
|
+
(local.get $z) (local.get $jc) (i32.const ${pt}) i32.mul i32.add f64.load local.set $zj
|
|
393
|
+
(local.get $zj) (f64.const 0) f64.ne
|
|
394
|
+
(if (then
|
|
395
|
+
(local.get $xc) (local.get $jc) (local.get $n) i32.mul (i32.const ${pt}) i32.mul i32.add local.set $col
|
|
396
|
+
${OS("cd","col","r","n","dot",n)}
|
|
397
|
+
(local.get $w) (local.get $jc) (i32.const ${pt}) i32.mul i32.add f64.load local.set $wold
|
|
398
|
+
(local.get $dot) (local.get $nf) f64.div (local.get $wold) (local.get $zj) f64.mul f64.add local.set $rho
|
|
399
|
+
(local.get $rho) (local.get $l1) f64.gt
|
|
400
|
+
(if (then
|
|
401
|
+
(local.get $rho) (local.get $l1) f64.sub local.set $thr
|
|
402
|
+
) (else
|
|
403
|
+
(local.get $rho) (f64.const 0) (local.get $l1) f64.sub f64.lt
|
|
404
|
+
(if (then
|
|
405
|
+
(local.get $rho) (local.get $l1) f64.add local.set $thr
|
|
406
|
+
) (else
|
|
407
|
+
(f64.const 0) local.set $thr))))
|
|
408
|
+
(local.get $thr) (local.get $zj) (local.get $l2) f64.add f64.div local.set $wj
|
|
409
|
+
(local.get $wj) (local.get $wold) f64.sub local.set $delta
|
|
410
|
+
(local.get $delta) (f64.const 0) f64.ne
|
|
411
|
+
(if (then
|
|
412
|
+
${iG("cd","r","col","delta","n",e)}
|
|
413
|
+
(local.get $w) (local.get $jc) (i32.const ${pt}) i32.mul i32.add (local.get $wj) f64.store
|
|
414
|
+
(local.get $delta) f64.abs local.set $absd
|
|
415
|
+
(local.get $absd) (local.get $maxChange) f64.gt
|
|
416
|
+
(if (then (local.get $absd) local.set $maxChange))))))
|
|
417
|
+
(local.get $jc) (i32.const 1) i32.add local.set $jc
|
|
418
|
+
br $jl))
|
|
419
|
+
(local.get $maxChange) (local.get $tol) f64.lt br_if $ie
|
|
420
|
+
(local.get $iter) (i32.const 1) i32.add local.set $iter
|
|
421
|
+
br $il))`;return Af("coord_descent",[["xc","i32"],["n","i32"],["d","i32"],["r","i32"],["w","i32"],["z","i32"],["l1","f64"],["l2","f64"],["maxIter","i32"],["tol","f64"]],[["iter","i32"],["jc","i32"],["nf","f64"],["maxChange","f64"],["zj","f64"],["col","i32"],["dot","f64"],["wold","f64"],["rho","f64"],["thr","f64"],["wj","f64"],["delta","f64"],["absd","f64"],["j","i32"],["nmain","i32"],["acc","v128"],["j2","i32"],["nmain2","i32"],["sv","v128"],["ya","i32"],["vy","v128"]],t)}l(LI,"coordDescentWat");var aG=5e5;function CI(n,e,t){let r=Po("gram_sym",II,"gram_sym"),s=r.allocF64(e*t),o=r.allocF64(e*e);return r.writeF64(s,n),r.run(s,e,t,o),Float64Array.from(r.f64(o,e*e))}l(CI,"gramSym");function uG(n,e,t,r){if(r){let s=new Float64Array(t*e);for(let o=0;o<e;o++)for(let i=0;i<t;i++)s[i*e+o]=n[o*t+i];return CI(s,t,e)}return CI(n,e,t)}l(uG,"wasmGram");function lG(n,e,t,r,s){let o=Po("matmul_rows",EI,"matmul_rows"),i=o.allocF64(e*t),a=o.allocF64(s*t),u=o.allocF64(e*s);return o.writeF64(i,n),o.writeF64(a,r),o.run(i,e,t,a,s,u),Float64Array.from(o.f64(u,e*s))}l(lG,"wasmMatmulRows");function DI(n,e){let{data:t,rows:r,cols:s}=Ye(e);if(Math.max(r,s)*Math.min(r,s)*Math.min(r,s)<aG)return Fy(n,e);let{U:i,S:a,V:u,k:p}=mu(t,r,s,void 0,uG,lG);return[Q(i,[r,p],e.dtype,e.device),Q(a,[p],e.dtype,e.device),Q(u,[s,p],e.dtype,e.device)]}l(DI,"wasmSvd");var PI={svd:DI},cG={eigh:"Jacobi rotation sweeps use strided column access; not SIMD-friendly",cholesky:"sequential dependent updates; not vectorizable at f64x2 width",solve:"triangular back-substitution is inherently sequential",cov:"single O(n*d) pass dominated by the eigen-free reduction"},$I=!1;function FI(){let n={enabled:Object.keys(PI),skipped:Object.keys(cG)};if($I)return n;$I=!0,$l();let e=new Vt("mlc","IMPL");for(let[t,r]of Object.entries(PI))e.impl(t,2,r);return n}l(FI,"registerWasmLinalg");ys();An();var pG=8e3,fG=5e4,dG=2654435769;function zI(n,e,t,r,s,o,i,a,u,p){return n.writeF64(t,u),n.run(e,o,i,t,a,r,s),p.set(n.i32(r,o)),n.f64(s,1)[0]}l(zI,"assignWasm");function VI(n,e,t,r,s,o){let{data:i,rows:a,cols:u}=Ye(e);if(a*u*t<pG)return Vy(n,e,t,r,s,o);let p=Po("kmeans_assign",AS,"nearest_centroid"),c=null;for(let d=0;d<s;d++){p.reset();let h=p.allocF64(a*u),m=p.allocF64(t*u),g=p.allocI32(a),y=p.allocF64(1);p.writeF64(h,i);let _=xr(o+d*dG),b=_S(i,a,u,t,_),w=new Int32Array(a),x=1/0;for(let S=0;S<r&&(x=zI(p,h,m,g,y,a,u,t,b,w),!!bS(i,a,u,t,w,b));S++);x=zI(p,h,m,g,y,a,u,t,b,w),(!c||x<c.inertia)&&(c={centers:Float64Array.from(b),labels:Int32Array.from(w),inertia:x})}let f=new Float64Array(a);for(let d=0;d<a;d++)f[d]=c.labels[d];return[Q(c.centers,[t,u],e.dtype,e.device),Q(f,[a],e.dtype,e.device),Q(new Float64Array([c.inertia]),[1],e.dtype,e.device)]}l(VI,"wasmKmeans");function GI(n,e,t){let{data:r,rows:s,cols:o}=Ye(e),i=Ye(t);if(s*o*i.rows<fG)return Gy(n,e,t);let a=Po("kmeans_assign",AS,"nearest_centroid");a.reset();let u=a.allocF64(s*o),p=a.allocF64(i.rows*o),c=a.allocI32(s),f=a.allocF64(1);a.writeF64(u,r),a.writeF64(p,i.data),a.run(u,s,o,p,i.rows,c,f);let d=new Float64Array(s),h=a.i32(c,s);for(let m=0;m<s;m++)d[m]=h[m];return Q(d,[s],e.dtype,e.device)}l(GI,"wasmKmeansPredict");var hG=2e4;function jI(n,e,t,r,s,o){let i=Ye(e),a=Ye(r),u=$t(t),p=i.cols;if(a.rows*i.rows*p<hG)return jy(n,e,t,r,s,o);let c=Po("knn_dist",MI,"dist_row");c.reset();let f=c.allocF64(a.rows*p),d=c.allocF64(i.rows*p),h=c.allocF64(i.rows);c.writeF64(f,a.data),c.writeF64(d,i.data);let m=Math.min(s,i.rows),g=new Float64Array(a.rows),y=new Int32Array(i.rows),_=p*pt;for(let b=0;b<a.rows;b++){c.run(f+b*_,d,i.rows,p,h);let w=c.f64(h,i.rows);wS(w,y,i.rows,m),g[b]=TS(u.data,y,m,o)}return Q(g,[a.rows],r.dtype,r.device)}l(jI,"wasmKnnPredict");var mG=3e6;function UI(n,e,t,r,s,o,i,a){let{data:u,rows:p,cols:c}=Ye(e);if(p*c*o<mG)return Uy(n,e,t,r,s,o,i,a);let f=$t(t),{xc:d,r:h,z:m,meanX:g,meanY:y}=SS(u,p,c,f.data,a),_=new Float64Array(p*c);for(let O=0;O<c;O++)for(let R=0;R<p;R++)_[O*p+R]=d[R*c+O];let b=r*s,w=r*(1-s),x=Po("coord_descent",LI,"coord_descent");x.reset();let S=x.allocF64(p*c),T=x.allocF64(p),v=x.allocF64(c),A=x.allocF64(c);x.writeF64(S,_),x.writeF64(T,h),x.writeF64(A,m),x.f64(v,c).fill(0),x.run(S,p,c,T,v,A,b,w,o,i);let N=Float64Array.from(x.f64(v,c)),k=vS(g,y,N,c);return[Q(N,[c],e.dtype,e.device),Q(new Float64Array([k]),[1],e.dtype,e.device)]}l(UI,"wasmElasticNet");var qI={kmeans:VI,kmeans_predict:GI,knn_predict:jI,elastic_net:UI},WI=!1;function KI(){let n={enabled:Object.keys(qI)};if(WI)return n;WI=!0,zy();let e=new Vt("mlc","IMPL");for(let[t,r]of Object.entries(qI))e.impl(t,2,r);return n}l(KI,"registerWasmMl");An();sp();Ma();ou();Ao();be();kt();var Wy=class extends ve{static{l(this,"AddBackward")}constructor(){super(2)}apply(e){let t=e[0];return[t,t]}},Ky=class extends ve{static{l(this,"SubBackward")}constructor(){super(2)}apply(e){let t=e[0];return[t,is(t)]}},Hy=class extends ve{static{l(this,"MulBackward")}constructor(){super(2)}apply(e){let t=e[0],[r,s]=this.savedTensors();return[ee(t,s.detach()),ee(t,r.detach())]}},Yy=class extends ve{static{l(this,"DivBackward")}constructor(){super(2)}apply(e){let t=e[0],[r,s]=this.savedTensors(),o=Kt(t,s.detach()),i=is(Kt(ee(t,r.detach()),ee(s.detach(),s.detach())));return[o,i]}},Jy=class extends ve{static{l(this,"NegBackward")}constructor(){super(1)}apply(e){let t=e[0];return[is(t)]}},Zy=class extends ve{static{l(this,"PowBackward")}constructor(){super(2)}apply(e){let t=e[0],[r,s]=this.savedTensors(),o=tn(s.shape,{dtype:s.dtype,device:s.device}),i=ee(t,ee(s.detach(),vo(r.detach(),ze(s.detach(),o)))),a=ee(t,ee(vo(r.detach(),s.detach()),ko(r.detach())));return[i,a]}};Ao();be();kt();Yi();var gG=2/Math.sqrt(Math.PI);function HI(n){let e=ot(n.shape,gG,{dtype:n.dtype,device:n.device});return ee(e,as(is(ee(n,n))))}l(HI,"_erfDeriv");function YI(n){let e=ae(n,ii),t=vo(e,-1),r=ze(ko(e),ee(t,.5)),s=ee(t,t),o=s;for(let i of Ga)r=ae(r,ee(o,i)),o=ee(o,s);for(let i=0;i<ii;i++)r=ze(r,vo(ae(n,i),-1));return r}l(YI,"_digammaTensor");var Qy=class extends ve{static{l(this,"ErfBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors();return[ee(e[0],HI(t.detach()))]}},Xy=class extends ve{static{l(this,"ErfcBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors();return[is(ee(e[0],HI(t.detach())))]}},e_=class extends ve{static{l(this,"LgammaBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors();return[ee(e[0],YI(t.detach()))]}},t_=class extends ve{static{l(this,"GammaBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),r=t.detach();return[ee(e[0],ee(kg(r),YI(r)))]}},n_=class extends ve{static{l(this,"ExpBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors();return[ee(e[0],as(t.detach()))]}},r_=class extends ve{static{l(this,"LogBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors();return[Kt(e[0],t.detach())]}},s_=class extends ve{static{l(this,"SqrtBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),r=t.detach(),s=na(r),o=ot(r.shape,2,{dtype:r.dtype,device:r.device});return[Kt(e[0],ee(o,s))]}},o_=class extends ve{static{l(this,"TanhBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),r=t.detach(),s=Hs(r),o=tn(r.shape,{dtype:r.dtype,device:r.device});return[ee(e[0],ze(o,ee(s,s)))]}},i_=class extends ve{static{l(this,"SigmoidBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),r=t.detach(),s=wr(r),o=tn(r.shape,{dtype:r.dtype,device:r.device});return[ee(e[0],ee(s,ze(o,s)))]}},a_=class extends ve{static{l(this,"SoftmaxBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),r=this.opArgs(),s=r&&r.length>1&&r[1]!=null?r[1]:-1,o=e[0],i=mi(t.detach(),s),a=wt(ee(o,i),s,!0);return[ee(i,ze(o,a))]}},u_=class extends ve{static{l(this,"LogSoftmaxBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),r=this.opArgs(),s=r&&r.length>1&&r[1]!=null?r[1]:-1,o=e[0],i=mi(t.detach(),s),a=wt(o,s,!0);return[ze(o,ee(i,a))]}},l_=class extends ve{static{l(this,"ReluBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),r=t.detach();return[ee(e[0],Og(Ml(r)))]}},c_=class extends ve{static{l(this,"GeluBackward")}constructor(){super(1)}apply(e){let t=e[0],[r]=this.savedTensors(),s=r.detach(),o=s.shape,i=s.dtype,a=s.device,u=ot(o,1.702,{dtype:i,device:a}),p=tn(o,{dtype:i,device:a}),c=ee(u,s),f=wr(c),d=ee(f,ae(p,ee(c,ze(p,f))));return[ee(t,d)]}},p_=class extends ve{static{l(this,"SiluBackward")}constructor(){super(1)}apply(e){let t=e[0],[r]=this.savedTensors(),s=r.detach(),o=wr(s),i=tn(s.shape,{dtype:s.dtype,device:s.device}),a=ee(o,ae(i,ee(s,ze(i,o))));return[ee(t,a)]}};Ao();be();kt();be();function JI(n,e){if(n==null){let r=[];for(let s=0;s<e;s++)r.push(s);return r}return(Array.isArray(n)?n:[n]).map(r=>r<0?r+e:r).sort((r,s)=>r-s)}l(JI,"_normalizeDims");function ZI(n,e,t,r){let s=n;if(!r)for(let i of t)s=Lr(s,i);let o=Ve(e,{dtype:s.dtype,device:s.device});return ae(o,s)}l(ZI,"_unreduce");var f_=class extends ve{static{l(this,"SumBackward")}constructor(){super(1)}apply(e){let t=e[0],s=this.inputMetadata(0).shape,o=this.opArgs(),i=o?o[1]:void 0,a=o?o[2]:!1,u=JI(i,s.length);return[ZI(t,s,u,a)]}},d_=class extends ve{static{l(this,"MeanBackward")}constructor(){super(1)}apply(e){let t=e[0],s=this.inputMetadata(0).shape,o=this.opArgs(),i=o?o[1]:void 0,a=o?o[2]:!1,u=JI(i,s.length),p=1;for(let d of u)p*=s[d];let c=ZI(t,s,u,a),f=ot(s,p,{dtype:t.dtype,device:t.device});return[Kt(c,f)]}};Ao();be();be();function QI(n,e){let t=n;for(;t.ndim>e.length;)t=wt(t,0,!1);for(let r=0;r<e.length;r++)e[r]===1&&t.shape[r]!==1&&(t=wt(t,r,!0));return t}l(QI,"_sumToShape");var h_=class extends ve{static{l(this,"MatmulBackward")}constructor(){super(2)}apply(e){let t=e[0],[r,s]=this.savedTensors(),o=r.detach(),i=s.detach(),a=o.ndim,u=i.ndim;if(a===1&&u===1)return[ee(t,i),ee(t,o)];if(a===2&&u===1){let f=Lr(t,1),d=Lr(i,0);return[tt(f,d),Cl(tt(vt(o,0,1),f),1)]}if(a===1&&u===2){let f=tt(t,vt(i,0,1)),d=tt(Lr(o,1),Lr(t,0));return[f,d]}let p=tt(t,vt(i,u-2,u-1)),c=tt(vt(o,a-2,a-1),t);return p=QI(p,o.shape),c=QI(c,i.shape),[p,c]}},m_=class extends ve{static{l(this,"DotBackward")}constructor(){super(2)}apply(e){let t=e[0],[r,s]=this.savedTensors();return[ee(t,s.detach()),ee(t,r.detach())]}};UT();Ao();be();kt();be();function yG(n,e,t){n[e]+=t}l(yG,"_addAt");var ic=class extends ve{static{l(this,"ReshapeBackward")}constructor(){super(1)}apply(e){let t=this.inputMetadata(0);return[Hn(e[0],t.shape)]}},g_=class extends ve{static{l(this,"TransposeBackward")}_dim0;_dim1;constructor(e,t){super(1),this._dim0=e,this._dim1=t}apply(e){return[vt(e[0],this._dim0,this._dim1)]}},Of=class extends ve{static{l(this,"SliceBackward")}_dim;_start;_end;_step;constructor(e,t,r,s){super(1),this._dim=e,this._start=t,this._end=r,this._step=s}apply(e){let t=e[0],r=this.inputMetadata(0),s=this._dim,o=r.shape[s],i=this._step||1,a=this._start<0?this._start+o:this._start,u=this._end<0?this._end+o:this._end;if(a=Math.max(0,Math.min(a,o)),u=Math.max(0,Math.min(u,o)),i===1){let w=r.shape.map(()=>0),x=r.shape.map(()=>0);return w[s]=a,x[s]=o-u,[tu(t,w,x,0)]}let p=Ve(r.shape,{dtype:t.dtype,device:t.device}),c=p._impl.storage.data,f=t._impl.storage.data,d=t._impl.storageOffset,h=t.shape,m=t.strides,g=p.strides,y=h.length,_=new Int32Array(y),b=d;for(let w=0;w<t.numel;w++){let x=0;for(let S=0;S<y;S++){let T=S===s?a+_[S]*i:_[S];x+=T*g[S]}yG(c,x,f[b]);for(let S=y-1;S>=0;S--){if(_[S]++,_[S]<h[S]){b+=m[S];break}b-=(h[S]-1)*m[S],_[S]=0}}return[p]}},y_=class extends ve{static{l(this,"SelectBackward")}_dim;_index;constructor(e,t){super(1),this._dim=e,this._index=t}apply(e){let t=e[0],r=this.inputMetadata(0),s=this._dim,o=r.shape[s],i=this._index<0?this._index+o:this._index,a=Lr(t,s),u=r.shape.map(()=>0),p=r.shape.map(()=>0);return u[s]=i,p[s]=o-1-i,[tu(a,u,p,0)]}},Bf=class extends ve{static{l(this,"ExpandBackward")}constructor(){super(1)}apply(e){let t=e[0],s=this.inputMetadata(0).shape,o=t.shape,i=[],a=o.length-s.length;for(let p=0;p<a;p++)i.push(p);for(let p=0;p<s.length;p++)s[p]===1&&o[p+a]!==1&&i.push(p+a);let u=t;return i.length>0&&(u=wt(t,i,!0)),[Hn(u,s)]}},__=class extends ve{static{l(this,"PermuteBackward")}_dims;constructor(e){super(1),this._dims=e}apply(e){let t=this._dims.length,r=new Array(t);for(let s=0;s<t;s++){let o=this._dims[s]<0?t+this._dims[s]:this._dims[s];r[o]=s}return[El(e[0],r)]}};var BS=new Map;function Ge(n,e){BS.set(n,e)}l(Ge,"_register");Ge("add",()=>new Wy);Ge("sub",()=>new Ky);Ge("mul",()=>new Hy);Ge("div",()=>new Yy);Ge("neg",()=>new Jy);Ge("pow",()=>new Zy);Ge("exp",()=>new n_);Ge("log",()=>new r_);Ge("sqrt",()=>new s_);Ge("tanh",()=>new o_);Ge("erf",()=>new Qy);Ge("erfc",()=>new Xy);Ge("lgamma",()=>new e_);Ge("gamma",()=>new t_);Ge("sigmoid",()=>new i_);Ge("relu",()=>new l_);Ge("softmax",()=>new a_);Ge("log_softmax",()=>new u_);Ge("gelu",()=>new c_);Ge("silu",()=>new p_);Ge("sum",()=>new f_);Ge("mean",()=>new d_);Ge("matmul",()=>new h_);Ge("dot",()=>new m_);Ge("cat",()=>new Ug);Ge("stack",()=>new qg);Ge("clamp",()=>new Wg);Ge("pad",()=>new Kg);Ge("index_select",()=>new Pl);Ge("where",()=>new Hg);Ge("reshape",()=>new ic);Ge("transpose",n=>new g_(n[1],n[2]));Ge("permute",n=>new __(n[1]));Ge("broadcast_in_dim",()=>new Bf);Ge("expand",()=>new Bf);Ge("slice",n=>new Of(n[1],n[2],n[3],n[4]));Ge("narrow",n=>new Of(n[1],n[2],n[2]+n[3],1));Ge("select",n=>new y_(n[1],n[2]));Ge("unsqueeze",()=>new ic);Ge("squeeze",()=>new ic);function XI(n,e=null){let t=BS.get(n);return t?t(e):null}l(XI,"getGradFn");function eE(n){return BS.has(n)}l(eE,"hasGradFn");Xg();Wp();di();sr();qn();fi();sr();kt();be();function tE(n,e){let t=n.gradFn;if(!t)throw new Error("Cannot call backward on a tensor that does not require grad");let r=e;if(!r){if(n.numel!==1)throw new Error("grad must be specified for non-scalar tensors");r=tn(n.shape,{dtype:n.dtype,device:n.device})}let s=new Map;bG(t,s,new Set);let i=new Map;i.set(t.id,[r]);let a=[];for(let[p,c]of s)c===0&&a.push(p);let u=[];for(;a.length>0;){let p=a.pop();if(p){u.push(p);for(let c of p.nextEdges){if(!c||!c.node)continue;let f=c.node,d=s.get(f)-1;s.set(f,d),d===0&&a.push(f)}}}for(let p of u){let c=i.get(p.id);if(!c)continue;let f=p.apply(c),d=f;if(f&&(d=f.map((m,g)=>{if(!m)return m;let y=p.inputMetadata(g);return y?_G(m,y.shape):m})),p.releaseVariables(),!d)continue;let h=p.nextEdges;for(let m=0;m<h.length;m++){let g=h[m];if(!g||!g.node||m>=d.length||!d[m])continue;let y=d[m],_=g.node.id,b=i.get(_);if(b){let w=b[g.inputNr];b[g.inputNr]=b[g.inputNr]?ae(w,y):y}else{let w=[];w[g.inputNr]=y,i.set(_,w)}}}}l(tE,"backward");function _G(n,e){let t=n.shape;if(t.length===e.length&&t.every((o,i)=>o===e[i]))return n;let r=n,s=t.length-e.length;for(let o=0;o<s;o++)r=wt(r,0,!1);for(let o=e.length-1;o>=0;o--)e[o]===1&&r.shape[o]!==1&&(r=wt(r,o,!0));return r}l(_G,"_reduceBroadcastGrad");function bG(n,e,t){let r=[n];for(e.set(n,0);r.length>0;){let s=r.shift();if(!t.has(s.id)){t.add(s.id);for(let o of s.nextEdges){if(!o||!o.node)continue;let i=o.node;e.has(i)||e.set(i,0),e.set(i,e.get(i)+1),t.has(i.id)||r.push(i)}}}}l(bG,"_countDeps");WO({backward:tE});function wG(n){let e=n._impl;if(In()&&e.device&&e.device.type==="gpu"){e.storage.retain();let s=new Pt(e.storage,e.storageOffset,e.sizes(),e.strides(),e.dtype,e.device);return new Ze(s)}let t=e.storage.clone(),r=new Pt(t,e.storageOffset,e.sizes(),e.strides(),e.dtype,e.device);return new Ze(r)}l(wG,"_snapshotTensor");function Rf(n){return typeof n=="object"&&n!==null&&"_impl"in n}l(Rf,"_isTensor");function TG(n){for(let e=0;e<n.length;e++){let t=n[e];if(Rf(t)&&t.requiresGrad)return!0;if(Array.isArray(t)){for(let r=0;r<t.length;r++)if(Rf(t[r])&&t[r].requiresGrad)return!0}}return!1}l(TG,"_anyRequiresGrad");function xG(n){let e=[];for(let t=0;t<n.length;t++){let r=n[t];if(Rf(r))e.push(r);else if(Array.isArray(r))for(let s=0;s<r.length;s++)Rf(r[s])&&e.push(r[s])}return e}l(xG,"_extractTensors");function SG(n){let e=n._impl.autogradMeta;if(!e)return null;let t=e.getGradAccumulator();return t||(t=new Xp(n),e.setGradAccumulator(t)),t}l(SG,"_getOrCreateAccumulator");function vG(n,e){let t=Lt.dispatch.bind(Lt);return(r,...s)=>{let o=r;if(!Oo.isEnabled()||!TG(s)){let c=o.subtract(ip);return Lt.redispatch(e,c,...s)}let i=XI(n,s);if(!i){let c=o.subtract(ip);return Lt.redispatch(e,c,...s)}let a=xG(s);i.setOpArgs(s);for(let c=0;c<a.length;c++)i.saveTensor(wG(a[c])),i.saveInputMetadata(c,[...a[c].shape],a[c].dtype);for(let c=0;c<a.length;c++){let f=a[c];if(f.requiresGrad){let d=f.gradFn;if(d){let h=f._impl.autogradMeta?f._impl.autogradMeta.outputNr:0;i.setNextEdge(c,d,h)}else{let h=SG(f);h&&i.setNextEdge(c,h,0)}}}let u=o.subtract(ip),p=Lt.redispatch(e,u,...s);if(Rf(p)){p._impl.setAutogradMeta(new So);let c=p._impl.autogradMeta;c.setGradFn(i,0),c.requiresGrad=!0,p._impl._updateKeySet()}return p}}l(vG,"wrapWithAutograd");function kG(n){return(e,...t)=>{let r=e.subtract(ip);return Lt.redispatch(n,r,...t)}}l(kG,"_makePassthrough");function nE(){let n=Lt.listOps(),e=[40,41,42,43];for(let t of n){let r=Lt.findOp(t);if(!r)continue;let s=r.name,o=eE(s)?vG(s,r):kG(r),i=Mr.fromUnboxed(o);for(let a of e)r.entry.registerKernel(a,i)}}l(nE,"registerAutogradKernels");be();os();var If=Ht,NG=["neg","exp","log","sqrt","rsqrt","abs","sin","cos","tanh","erf","erfc","lgamma","gamma","sigmoid","relu","gelu","silu","sign","floor","ceil","clone","contiguous"],AG=["add","sub","mul","div","pow","remainder","maximum","minimum","eq","ne","lt","le","gt","ge","matmul","dot","flip","unsqueeze"],OG=["sum","mean","max","min","argmax","argmin","prod"],BG={softmax:[-1],log_softmax:[-1],roll:[void 0,0],cumsum:[0],sort:[-1,!1],argsort:[-1,!1],topk:[void 0,-1,!0],split:[void 0,0],chunk:[void 0,0],squeeze:[null]};function RG(n,e){let t=[];for(let r=0;r<e.length;r++)t[r]=n[r]===void 0?e[r]:n[r];for(let r=e.length;r<n.length;r++)t[r]=n[r];return t}l(RG,"withDefaults");function MG(n){for(let e of NG)n[e]=function(){return If[e](this)}}l(MG,"installSelfMethods");function IG(n){for(let e of AG)n[e]=function(t){return If[e](this,t)}}l(IG,"installOneArgMethods");function EG(n){for(let[e,t]of Object.entries(BG))n[e]=function(...r){return If[e](this,...RG(r,t))}}l(EG,"installDefaultedMethods");function LG(n){for(let e of OG)n[e]=function(t,r){return If[e](this,t,r)}}l(LG,"installReductionMethods");function Mf(n){return n.length===1&&Array.isArray(n[0])?n[0]:n}l(Mf,"arrayArg");function rE(n){let e=n.prototype;e.to=function(t){if(this.device.equals(t))return this;let s=If.contiguous(this).data.slice(0,this.numel);return eu(s,this.shape,this.dtype,{device:t})},MG(e),IG(e),EG(e),LG(e),e.mm=function(t){return tt(this,t)},e.requires_grad=function(t=!0){return this.requiresGrad_(t)},e.gather=function(t,r){return Eg(this,t,r)},e.scatter_add=function(t,r,s){return nu(this,t,r,s)},e.scatter=function(t,r,s){return Lg(this,t,r,s)},e.transpose=function(t,r){return vt(this,t,r)},e.slice=function(t,r,s,o){return Dg(this,t,r,s,o)},e.narrow=function(t,r,s){return ir(this,t,r,s)},e.select=function(t,r){return en(this,t,r)},e.reshape=function(...t){return Hn(this,Mf(t))},e.permute=function(...t){return El(this,Mf(t))},e.expand=function(...t){return Ll(this,Mf(t))},e.repeat=function(...t){return Dl(this,Mf(t))},e.tile=function(...t){return Pg(this,Mf(t))},e.t=function(){if(this.ndim!==2)throw new Error("t() expects a 2D tensor");return vt(this,0,1)}}l(rE,"installOps");sr();sr();qn();ou();yu();nn();RS();_f();yx();Ma();kt();kt();function Lf(n,e){return{dtype:e?.dtype??n.dtype,device:e?.device??n.device,requiresGrad:e?.requiresGrad??!1}}l(Lf,"_likeOpts");function $G(n,e){return Tn(n.shape,Lf(n,e))}l($G,"emptyLike");function FG(n,e){return Ve(n.shape,Lf(n,e))}l(FG,"zerosLike");function zG(n,e){return tn(n.shape,Lf(n,e))}l(zG,"onesLike");function VG(n,e,t){return ot(n.shape,e,Lf(n,t))}l(VG,"fullLike");function GG(n,e){return Gg(n.shape,Lf(n,e))}l(GG,"randnLike");os();be();var dE={};at(dE,{AdaptiveAvgPool2d:()=>Xf,AvgPool2d:()=>Qf,BCELoss:()=>rd,BatchNorm1d:()=>cc,BatchNorm2d:()=>Jf,Conv1d:()=>zf,Conv2d:()=>Ff,CrossEntropyLoss:()=>td,Dropout:()=>Sr,ELU:()=>Kf,Embedding:()=>sd,F:()=>fE,Flatten:()=>cd,GELU:()=>Gf,GRU:()=>id,GRUCell:()=>pc,GroupNorm:()=>E_,LSTM:()=>ad,LSTMCell:()=>fc,LayerNorm:()=>no,LeakyReLU:()=>Wf,Linear:()=>Sn,LogSoftmax:()=>Yf,MSELoss:()=>ed,MaxPool2d:()=>Zf,Module:()=>ge,ModuleDict:()=>ld,ModuleList:()=>aa,MultiheadAttention:()=>ua,NLLLoss:()=>nd,Parameter:()=>At,PositionalEncoding:()=>fd,ReLU:()=>Vf,Sequential:()=>ud,SiLU:()=>jf,Sigmoid:()=>Uf,Softmax:()=>Hf,Tanh:()=>qf,Transformer:()=>pd,TransformerDecoder:()=>hc,TransformerDecoderLayer:()=>wu,TransformerEncoder:()=>dc,TransformerEncoderLayer:()=>bu,functional:()=>lc,init:()=>T_});sr();var At=class extends Ze{static{l(this,"Parameter")}constructor(e,t=!0){e instanceof Ze?super(e._impl):super(e),t&&this.requiresGrad_(!0)}get isParameter(){return!0}};var ge=class n{static{l(this,"Module")}_parameters;_buffers;_modules;_training;_detected;constructor(){this._parameters=new Map,this._buffers=new Map,this._modules=new Map,this._training=!0}forward(...e){throw new Error(`${this.constructor.name}.forward() not implemented`)}call(...e){return this.forward(...e)}registerParameter(e,t){if(t!==null&&!(t instanceof At))throw new Error("Expected Parameter instance");this._parameters.set(e,t)}registerBuffer(e,t){this._buffers.set(e,t)}registerModule(e,t){if(t!==null&&!(t instanceof n))throw new Error("Expected Module instance");this._modules.set(e,t)}*parameters(e=!0){this._autoDetect();for(let[,t]of this._parameters)t!==null&&(yield t);if(e)for(let[,t]of this._modules)t!==null&&(yield*t.parameters(!0))}*namedParameters(e="",t=!0){this._autoDetect();let r=e?e+".":"";for(let[s,o]of this._parameters)o!==null&&(yield[r+s,o]);if(t)for(let[s,o]of this._modules)o!==null&&(yield*o.namedParameters(r+s,!0))}*buffers(e=!0){for(let[,t]of this._buffers)t!==null&&(yield t);if(e)for(let[,t]of this._modules)t!==null&&(yield*t.buffers(!0))}*children(){this._autoDetect();for(let[,e]of this._modules)e!==null&&(yield e)}*namedChildren(){this._autoDetect();for(let[e,t]of this._modules)t!==null&&(yield[e,t])}*modules(){yield this,this._autoDetect();for(let[,e]of this._modules)e!==null&&(yield*e.modules())}*namedModules(e=""){yield[e,this],this._autoDetect();let t=e?e+".":"";for(let[r,s]of this._modules)s!==null&&(yield*s.namedModules(t+r))}stateDict(e=""){this._autoDetect();let t=new Map,r=e?e+".":"";for(let[s,o]of this._parameters)o!==null&&t.set(r+s,o);for(let[s,o]of this._buffers)o!==null&&t.set(r+s,o);for(let[s,o]of this._modules)if(o!==null)for(let[i,a]of o.stateDict(r+s))t.set(i,a);return t}loadStateDict(e){this._autoDetect();for(let[t,r]of this._parameters){let s=t;if(e.has(s)&&r!==null){let o=e.get(s),i=r._impl.storage.data,a=o._impl?o._impl.storage.data:o.data;for(let u=0;u<i.length;u++)i[u]=a[u]}}for(let[t,r]of this._modules)if(r!==null){let s=new Map,o=t+".";for(let[i,a]of e)i.startsWith(o)&&s.set(i.substring(o.length),a);s.size>0&&r.loadStateDict(s)}}train(e=!0){this._autoDetect(),this._training=e;for(let[,t]of this._modules)t!==null&&t.train(e);return this}eval(){return this.train(!1)}get training(){return this._training}to(e){this._autoDetect();for(let[t,r]of this._parameters){let s=r;r!==null&&typeof s?.to=="function"&&(this._parameters.set(t,new At(s.to(e),r.requiresGrad)),this[t]=this._parameters.get(t))}for(let[t,r]of this._buffers){let s=r;r!==null&&typeof s?.to=="function"&&(this._buffers.set(t,s.to(e)),this[t]=this._buffers.get(t))}for(let[,t]of this._modules)t!==null&&t.to(e);return this}apply(e){this._autoDetect();for(let[,t]of this._modules)t!==null&&t.apply(e);return e(this),this}zeroGrad(){for(let e of this.parameters())if(e.grad){let t=e.grad._impl.storage.data;t&&t.fill(0)}return this}_autoDetect(){if(this._detected)return;this._detected=!0;let e=Object.keys(this);for(let t of e){if(t.startsWith("_"))continue;let r=this[t];r instanceof At&&!this._parameters.has(t)?this._parameters.set(t,r):r instanceof n&&!this._modules.has(t)&&this._modules.set(t,r)}}toString(){return this._buildRepr("")}_buildRepr(e){this._autoDetect();let t=this.constructor.name;if(this._modules.size===0)return t+"()";let r=[t+"("];for(let[s,o]of this._modules)o!==null&&r.push(e+" ("+s+"): "+o._buildRepr(e+" "));return r.push(e+")"),r.join(`
|
|
422
|
+
`)}};var T_={};at(T_,{_calculateFanInFanOut:()=>ac,constant_:()=>qG,kaiming_normal_:()=>HG,kaiming_uniform_:()=>sE,normal_:()=>Cf,ones_:()=>UG,resetLinearParameters:()=>uc,uniform_:()=>w_,xavier_normal_:()=>KG,xavier_uniform_:()=>WG,zeros_:()=>jG});function ac(n){let e=n.shape,t=e.length;if(t<2)throw new Error("Fan in/out requires at least 2D tensor");let r=e[1],s=e[0],o=1;for(let i=2;i<t;i++)o*=e[i];return{fanIn:r*o,fanOut:s*o}}l(ac,"_calculateFanInFanOut");function uc(n,e){if(sE(n,Math.sqrt(5)),e){let{fanIn:t}=ac(n),r=1/Math.sqrt(t);w_(e,-r,r)}}l(uc,"resetLinearParameters");function w_(n,e=0,t=1){let r=n._impl.storage.data;if(!r)return n;let s=t-e;for(let o=0;o<r.length;o++)r[o]=e+Math.random()*s;return n._impl.bumpVersion(),n}l(w_,"uniform_");function Cf(n,e=0,t=1){let r=n._impl.storage.data;if(!r)return n;for(let s=0;s<r.length;s+=2){let o=Math.random()||1e-10,i=Math.random(),a=Math.sqrt(-2*Math.log(o)),u=6.283185307179586*i;r[s]=e+t*a*Math.cos(u),s+1<r.length&&(r[s+1]=e+t*a*Math.sin(u))}return n._impl.bumpVersion(),n}l(Cf,"normal_");function jG(n){let e=n._impl.storage.data;return e&&e.fill(0),n._impl.bumpVersion(),n}l(jG,"zeros_");function UG(n){let e=n._impl.storage.data;return e&&e.fill(1),n._impl.bumpVersion(),n}l(UG,"ones_");function qG(n,e){let t=n._impl.storage.data;return t&&t.fill(e),n._impl.bumpVersion(),n}l(qG,"constant_");function WG(n,e=1){let{fanIn:t,fanOut:r}=ac(n),s=e*Math.sqrt(2/(t+r)),o=Math.sqrt(3)*s;return w_(n,-o,o)}l(WG,"xavier_uniform_");function KG(n,e=1){let{fanIn:t,fanOut:r}=ac(n),s=e*Math.sqrt(2/(t+r));return Cf(n,0,s)}l(KG,"xavier_normal_");function sE(n,e=0,t="fan_in",r="leaky_relu"){let{fanIn:s,fanOut:o}=ac(n),i=t==="fan_in"?s:o,u=oE(r,e)/Math.sqrt(i),p=Math.sqrt(3)*u;return w_(n,-p,p)}l(sE,"kaiming_uniform_");function HG(n,e=0,t="fan_in",r="leaky_relu"){let{fanIn:s,fanOut:o}=ac(n),i=t==="fan_in"?s:o,u=oE(r,e)/Math.sqrt(i);return Cf(n,0,u)}l(HG,"kaiming_normal_");function oE(n,e=.01){switch(n){case"linear":case"sigmoid":return 1;case"tanh":return 5/3;case"relu":return Math.sqrt(2);case"leaky_relu":return Math.sqrt(2/(1+e*e));default:return 1}}l(oE,"_calculateGain");var lc={};at(lc,{elu:()=>PS,gelu:()=>Pf,leaky_relu:()=>DS,log_softmax:()=>CS,relu:()=>Df,sigmoid:()=>IS,silu:()=>MS,softmax:()=>LS,tanh:()=>ES});be();kt();function Df(n){return Ml(n)}l(Df,"relu");function Pf(n){return Ng(n)}l(Pf,"gelu");function MS(n){return Ag(n)}l(MS,"silu");function IS(n){return wr(n)}l(IS,"sigmoid");function ES(n){return Hs(n)}l(ES,"tanh");function LS(n,e=-1){return mi(n,e)}l(LS,"softmax");function CS(n,e=-1){return $g(n,e)}l(CS,"log_softmax");function DS(n,e=.01){let t=ee(n,ot(n.shape,e,{dtype:n.dtype,device:n.device}));return ta(n,t)}l(DS,"leaky_relu");function PS(n,e=1){let t=ot(n.shape,0,{dtype:n.dtype,device:n.device}),r=ot(n.shape,1,{dtype:n.dtype,device:n.device}),s=ot(n.shape,e,{dtype:n.dtype,device:n.device}),o=Il(n,t),i=ee(s,ze(as(n),r));return or(o,n,i)}l(PS,"elu");var S_={};at(S_,{batch_norm:()=>zS,group_norm:()=>FS,layer_norm:()=>$S});be();kt();yu();function $S(n,e,t,r,s=1e-5){if((n instanceof bt||n.isSymbolic)&&t&&r){let m=n.ndim-e.length;return PT(n,t,r,m,s)}let o=n.ndim-e.length,i=[];for(let m=o;m<n.ndim;m++)i.push(m);let a=n;for(let m=i.length-1;m>=0;m--)a=Tr(a,i[m],!0);let u=ze(n,a),c=ee(u,u);for(let m=i.length-1;m>=0;m--)c=Tr(c,i[m],!0);let f=ot([],s),d=Kt(ot([],1),na(ae(c,f))),h=ee(u,d);return t&&(h=ee(h,t)),r&&(h=ae(h,r)),h}l($S,"layer_norm");function FS(n,e,t,r,s=1e-5){let o=n.shape,i=o[0],a=o[1],u=o.slice(2),p=n.reshape([i,e,a/e,...u]),c=[];for(let w=2;w<p.ndim;w++)c.push(w);let f=p;for(let w=c.length-1;w>=0;w--)f=Tr(f,c[w],!0);let d=ze(p,f),m=ee(d,d);for(let w=c.length-1;w>=0;w--)m=Tr(m,c[w],!0);let g=ot([],s),y=Kt(ot([],1),na(ae(m,g))),_=ee(d,y).reshape(o),b=[1,a,...u.map(()=>1)];return t&&(_=ee(_,t.reshape(b))),r&&(_=ae(_,r.reshape(b))),_}l(FS,"group_norm");var x_=1;function YG(n,e){let t=new Array(n).fill(1);return t[x_]=e,t}l(YG,"channelShape");function iE(n,e){let t=n;for(let r=e.length-1;r>=0;r--)t=Tr(t,e[r],!0);return t}l(iE,"reduceMeanOver");function aE(n,e,t){let r=n.data;if(!r)return;let s=e.reshape([n.shape[0]]).toArray();for(let o=0;o<r.length;o++)r[o]=r[o]*(1-t)+s[o]*t}l(aE,"blendRunning");function zS(n,e,t,r,s,o=!0,i=1e-5,a=.1){let u=n instanceof bt||n.isSymbolic;if(!o)return $T(n,r,s,e,t,x_,i);let p=[];for(let y=0;y<n.ndim;y++)y!==x_&&p.push(y);let c=iE(n,p),f=ze(n,c),d=iE(ee(f,f),p),h=Kt(ot([],1),na(ae(d,ot([],i)))),m=ee(f,h),g=YG(n.ndim,n.shape[x_]);return r&&(m=ee(m,r.reshape(g))),s&&(m=ae(m,s.reshape(g))),u||(e&&aE(e,c,a),t&&aE(t,d,a)),m}l(zS,"batch_norm");var GS={};at(GS,{linear:()=>VS});be();be();yu();function VS(n,e,t){let r;n instanceof bt||n.isSymbolic?r=vt(e,0,1):r=vt(e,0,1);let s=tt(n,r);return t?ae(s,t):s}l(VS,"linear");var k_={};at(k_,{conv1d:()=>jS,conv2d:()=>v_});be();be();function v_(n,e,t,r=[1,1],s=[[0,0],[0,0]],o=[1,1],i=1){let a=Array.isArray(r)?r:[r,r],u=JG(s),p=Array.isArray(o)?o:[o,o],c=FT(n,e,a,u,p,i);if(t){let f=Hn(t,[1,t.shape[0],1,1]);return ae(c,f)}return c}l(v_,"conv2d");function jS(n,e,t,r=1,s=0,o=1,i=1){let a=Lr(n,2),u=Lr(e,2),p=[1,Array.isArray(r)?r[0]:r],c=[[0,0],ZG(s)],f=[1,Array.isArray(o)?o[0]:o],d=v_(a,u,null,p,c,f,i),h=Cl(d,2);if(t){let m=Hn(t,[1,t.shape[0],1]);return ae(h,m)}return h}l(jS,"conv1d");function JG(n){return typeof n=="number"?[[n,n],[n,n]]:Array.isArray(n)&&typeof n[0]=="number"?n.map(e=>[e,e]):n}l(JG,"_normalizePadding");function ZG(n){return typeof n=="number"?[n,n]:n}l(ZG,"_normalizePadding1d");var A_={};at(A_,{adaptive_avg_pool2d:()=>qS,avg_pool2d:()=>N_,max_pool2d:()=>US});be();function US(n,e,t,r=[[0,0],[0,0]]){let s=Array.isArray(e)?e:[e,e],o=t?Array.isArray(t)?t:[t,t]:s,i=uE(r);return Fg(n,"max",s,o,i)}l(US,"max_pool2d");function N_(n,e,t,r=[[0,0],[0,0]]){let s=Array.isArray(e)?e:[e,e],o=t?Array.isArray(t)?t:[t,t]:s,i=uE(r);return Fg(n,"avg",s,o,i)}l(N_,"avg_pool2d");function qS(n,e){let[t,r]=Array.isArray(e)?e:[e,e],s=n.shape[2],o=n.shape[3],i=Math.floor(s/t),a=Math.floor(o/r);return N_(n,[i,a],[i,a],[[0,0],[0,0]])}l(qS,"adaptive_avg_pool2d");function uE(n){if(typeof n=="number")return[[n,n],[n,n]];if(Array.isArray(n)&&typeof n[0]=="number"){let e=n.map(t=>[t,t]);return e.length===1?[e[0],e[0]]:e}return n}l(uE,"_normalizePadding");var WS={};at(WS,{dropout:()=>$f});be();kt();function $f(n,e=.5,t=!0){if(!t||e===0)return n;if(e===1)return ot(n.shape,0,{dtype:n.dtype,device:n.device});let r=QG(n.shape,1-e,n.dtype,n.device),s=ot(n.shape,1/(1-e),{dtype:n.dtype,device:n.device});return ee(ee(n,r),s)}l($f,"dropout");function QG(n,e,t,r){let s=Tn(n,{dtype:t,device:r}),o=s._impl.storage.data;if(o)for(let i=0;i<o.length;i++)o[i]=Math.random()<e?1:0;return s}l(QG,"_bernoulliMask");var B_={};at(B_,{binary_cross_entropy:()=>YS,cross_entropy:()=>HS,mse_loss:()=>KS,nll_loss:()=>O_});be();kt();be();ss();function KS(n,e,t="mean"){let r=ze(n,e),s=ee(r,r);return lE(s,t)}l(KS,"mse_loss");function XG(n,e){let t=e6(n,e),r=ze(n,t),s=as(r),o=t6(s,e),i=ko(o);return ze(r,i)}l(XG,"_logSoftmaxAutograd");function e6(n,e){let t=e<0?n.ndim+e:e;return No(n,t,!0)}l(e6,"_dimMax");function t6(n,e){let t=e<0?n.ndim+e:e;return wt(n,t,!0)}l(t6,"_dimSum");function O_(n,e,t="mean",r=null){let s=n.ndim-1,o=n.shape[s],i=Ig(e,o),a=wt(ee(n,i),s),u=a,p=null;if(r!==null){let f=en(i,s,r),d=ae(is(f),1);u=ee(a,d),p=wt(d)}let c=is(wt(u));if(t==="sum")return c;if(t==="mean")return p!==null?Kt(c,p):Kt(c,bn(e.shape));throw new Error(`nll_loss: unknown reduction '${t}'`)}l(O_,"nll_loss");function HS(n,e,t="mean",r=null){let s=XG(n,-1);return O_(s,e,t,r)}l(HS,"cross_entropy");function YS(n,e,t="mean"){let r=ot(n.shape,1e-7,{dtype:n.dtype,device:n.device}),s=ot(n.shape,1,{dtype:n.dtype,device:n.device}),o=ko(ae(n,r)),i=ko(ae(ze(s,n),r)),a=is(ae(ee(e,o),ee(ze(s,e),i)));return lE(a,t)}l(YS,"binary_cross_entropy");function lE(n,e){if(e==="mean")return Tr(n);if(e==="sum")return wt(n);if(e==="none")return n;throw new Error(`Unknown reduction: ${e}`)}l(lE,"_reduce");var ZS={};at(ZS,{embedding:()=>JS});be();function JS(n,e){let t=n.shape[n.shape.length-1],r=e.shape.reduce((o,i)=>o*i,1);return ra(n,0,e.reshape([r])).reshape([...e.shape,t])}l(JS,"embedding");var XS={};at(XS,{scaled_dot_product_attention:()=>QS});be();be();yu();_u();kt();function r6(n,e){if(n instanceof bt||n.isSymbolic)return mi(n,e);let t=e<0?n.ndim+e:e,r=No(n,t,!0).detach(),s=ze(n,r),o=as(s),i=wt(o,t,!0);return Kt(o,i)}l(r6,"_softmax");function s6(n){let e=n.ndim;return n instanceof bt||n.isSymbolic?vt(n,e-2,e-1):vt(n,e-2,e-1)}l(s6,"_transposeLastTwo");function o6(n,e){let t=Tn([n,e]),r=t._impl.storage.data,s=e-n;for(let o=0;o<n;o++)for(let i=0;i<e;i++)r[o*e+i]=i<=o+s?0:-1/0;return t}l(o6,"_generateCausalMask");function QS(n,e,t,r=null,s=0,o=!1,i=!1){let a=n.shape[n.ndim-1],u=n.shape[n.ndim-2],p=e.shape[e.ndim-2],c=As();if(c&&!r&&!(s>0&&i)&&n instanceof bt&&e instanceof bt&&t instanceof bt&&n.ndim===4)return c.recordOp("scaled_dot_product_attention",[n,e,t],{scale:1/Math.sqrt(a),causal:o});let d=ot([],1/Math.sqrt(a)),h=s6(e),m=tt(n,h);m=ee(m,d),o&&(m=ae(m,o6(u,p))),r&&(m=ae(m,r));let g=r6(m,-1);return s>0&&i&&(g=$f(g,s,!0)),tt(g,t)}l(QS,"scaled_dot_product_attention");kt();var Sn=class extends ge{static{l(this,"Linear")}inFeatures;outFeatures;weight;bias;constructor(e,t,r=!0){super(),this.inFeatures=e,this.outFeatures=t,this.weight=new At(Tn([t,e])),this.bias=r?new At(Ve([t])):null,this._resetParameters()}_resetParameters(){uc(this.weight,this.bias)}forward(e){return VS(e,this.weight,this.bias)}};kt();var Ff=class extends ge{static{l(this,"Conv2d")}inChannels;outChannels;kernelSize;stride;padding;dilation;groups;weight;bias;constructor(e,t,r,s={}){super(),this.inChannels=e,this.outChannels=t,this.kernelSize=Array.isArray(r)?r:[r,r],this.stride=s.stride?Array.isArray(s.stride)?s.stride:[s.stride,s.stride]:[1,1],this.padding=s.padding??0,this.dilation=s.dilation?Array.isArray(s.dilation)?s.dilation:[s.dilation,s.dilation]:[1,1],this.groups=s.groups??1;let o=this.kernelSize[0],i=this.kernelSize[1];this.weight=new At(Tn([t,e/this.groups,o,i])),this.bias=s.bias!==!1?new At(Ve([t])):null,this._resetParameters()}_resetParameters(){uc(this.weight,this.bias)}forward(e){return v_(e,this.weight,this.bias,this.stride,this.padding,this.dilation,this.groups)}},zf=class extends ge{static{l(this,"Conv1d")}inChannels;outChannels;kernelSize;stride;padding;dilation;groups;weight;bias;constructor(e,t,r,s={}){super(),this.inChannels=e,this.outChannels=t,this.kernelSize=Array.isArray(r)?r[0]:r,this.stride=s.stride??1,this.padding=s.padding??0,this.dilation=s.dilation??1,this.groups=s.groups??1,this.weight=new At(Tn([t,e/this.groups,this.kernelSize])),this.bias=s.bias!==!1?new At(Ve([t])):null,uc(this.weight,this.bias)}forward(e){return jS(e,this.weight,this.bias,this.stride,this.padding,this.dilation,this.groups)}};var Vf=class extends ge{static{l(this,"ReLU")}forward(e){return Df(e)}},Gf=class extends ge{static{l(this,"GELU")}forward(e){return Pf(e)}},jf=class extends ge{static{l(this,"SiLU")}forward(e){return MS(e)}},Uf=class extends ge{static{l(this,"Sigmoid")}forward(e){return IS(e)}},qf=class extends ge{static{l(this,"Tanh")}forward(e){return ES(e)}},Wf=class extends ge{static{l(this,"LeakyReLU")}negativeSlope;constructor(e=.01){super(),this.negativeSlope=e}forward(e){return DS(e,this.negativeSlope)}},Kf=class extends ge{static{l(this,"ELU")}alpha;constructor(e=1){super(),this.alpha=e}forward(e){return PS(e,this.alpha)}},Hf=class extends ge{static{l(this,"Softmax")}dim;constructor(e=-1){super(),this.dim=e}forward(e){return LS(e,this.dim)}},Yf=class extends ge{static{l(this,"LogSoftmax")}dim;constructor(e=-1){super(),this.dim=e}forward(e){return CS(e,this.dim)}};kt();var no=class extends ge{static{l(this,"LayerNorm")}normalizedShape;eps;weight;bias;constructor(e,t=1e-5,r=!0){super(),this.normalizedShape=Array.isArray(e)?e:[e],this.eps=t,this.weight=r?new At(tn(this.normalizedShape)):null,this.bias=r?new At(Ve(this.normalizedShape)):null}forward(e){return $S(e,this.normalizedShape,this.weight,this.bias,this.eps)}},E_=class extends ge{static{l(this,"GroupNorm")}numGroups;numChannels;eps;weight;bias;constructor(e,t,r=1e-5,s=!0){super(),this.numGroups=e,this.numChannels=t,this.eps=r,this.weight=s?new At(tn([t])):null,this.bias=s?new At(Ve([t])):null}forward(e){return FS(e,this.numGroups,this.weight,this.bias,this.eps)}},cc=class extends ge{static{l(this,"BatchNorm1d")}numFeatures;eps;weight;bias;runningMean;runningVar;constructor(e,t=1e-5,r=!0){super(),this.numFeatures=e,this.eps=t,this.weight=r?new At(tn([e])):null,this.bias=r?new At(Ve([e])):null,this.runningMean=Ve([e]),this.runningVar=tn([e]),this.registerBuffer("runningMean",this.runningMean),this.registerBuffer("runningVar",this.runningVar)}forward(e){return zS(e,this.runningMean,this.runningVar,this.weight,this.bias,this.training,this.eps)}},Jf=class extends cc{static{l(this,"BatchNorm2d")}constructor(e,t=1e-5,r=!0){super(e,t,r)}};var Zf=class extends ge{static{l(this,"MaxPool2d")}kernelSize;stride;padding;constructor(e,t,r=0){super(),this.kernelSize=Array.isArray(e)?e:[e,e],this.stride=t?Array.isArray(t)?t:[t,t]:this.kernelSize,this.padding=r}forward(e){return US(e,this.kernelSize,this.stride,this.padding)}},Qf=class extends ge{static{l(this,"AvgPool2d")}kernelSize;stride;padding;constructor(e,t,r=0){super(),this.kernelSize=Array.isArray(e)?e:[e,e],this.stride=t?Array.isArray(t)?t:[t,t]:this.kernelSize,this.padding=r}forward(e){return N_(e,this.kernelSize,this.stride,this.padding)}},Xf=class extends ge{static{l(this,"AdaptiveAvgPool2d")}outputSize;constructor(e){super(),this.outputSize=Array.isArray(e)?e:[e,e]}forward(e){return qS(e,this.outputSize)}};var Sr=class extends ge{static{l(this,"Dropout")}p;constructor(e=.5){super(),this.p=e}forward(e){return $f(e,this.p,this.training)}};var ed=class extends ge{static{l(this,"MSELoss")}reduction;constructor(e="mean"){super(),this.reduction=e}forward(e,t){return KS(e,t,this.reduction)}},td=class extends ge{static{l(this,"CrossEntropyLoss")}reduction;ignoreIndex;constructor(e="mean",t=-100){super(),this.reduction=e,this.ignoreIndex=t}forward(e,t){return HS(e,t,this.reduction,this.ignoreIndex===-100?null:this.ignoreIndex)}},nd=class extends ge{static{l(this,"NLLLoss")}reduction;ignoreIndex;constructor(e="mean",t=-100){super(),this.reduction=e,this.ignoreIndex=t}forward(e,t){return O_(e,t,this.reduction,this.ignoreIndex===-100?null:this.ignoreIndex)}},rd=class extends ge{static{l(this,"BCELoss")}reduction;constructor(e="mean"){super(),this.reduction=e}forward(e,t){return YS(e,t,this.reduction)}};kt();var sd=class extends ge{static{l(this,"Embedding")}numEmbeddings;embeddingDim;weight;constructor(e,t){super(),this.numEmbeddings=e,this.embeddingDim=t,this.weight=new At(Tn([e,t])),Cf(this.weight)}forward(e){return JS(this.weight,e)}};kt();be();be();_u();be();be();function i6(n){if(Array.isArray(n[0])){let e=n[0],t=new Array(e.length);for(let r=0;r<t.length;r++)t[r]=us(n.map(s=>s[r]),0);return t}return us(n,0)}l(i6,"stackSteps");function od(n,e,t){let r=Array.isArray(e),s=Array.isArray(t),o=s?t:[t],i=r?e:[e],a=As();if(!a){let d=e,h=[],m=o[0].shape[0];for(let g=0;g<m;g++){let y=s?o.map(w=>en(w,0,g)):en(o[0],0,g),[_,b]=n(d,y);d=_,h.push(b)}return[d,i6(h)]}let u=!1,p=l((d,h)=>{let[m,g]=n(r?d:d[0],s?h:h[0]);return u=Array.isArray(g),[r?m:[m],u?g:[g]]},"step"),[c,f]=a.scan(o,i,p);return[r?c:c[0],u?f:f[0]]}l(od,"scan");_u();wn();qn();var pc=class extends ge{static{l(this,"GRUCell")}inputSize;hiddenSize;x2h;h2h;constructor(e,t,r=!0){super(),this.inputSize=e,this.hiddenSize=t,this.x2h=new Sn(e,3*t,r),this.h2h=new Sn(t,3*t,r)}forward(e,t=null){let r=t!==null?t:Ve([e.shape[0],this.hiddenSize],{device:e.device}),s=this.x2h.forward(e),o=this.h2h.forward(r),[i,a,u]=hi(s,this.hiddenSize,-1),[p,c,f]=hi(o,this.hiddenSize,-1),d=wr(ae(i,p)),h=wr(ae(a,c)),m=Hs(ae(u,ee(d,f)));return ae(m,ee(h,ze(r,m)))}},id=class extends ge{static{l(this,"GRU")}inputSize;hiddenSize;numLayers;batchFirst;cells;constructor(e,t,r=1,s=!1,o=!0){super(),this.inputSize=e,this.hiddenSize=t,this.numLayers=r,this.batchFirst=s,this.cells=[];for(let i=0;i<r;i++){let a=new pc(i===0?e:t,t,o);this.cells.push(a),this.registerModule("cell_"+i,a)}}forward(e,t=null){let r=uS();if(r&&e.device.type==="gpu"&&!As()){let c=this.batchFirst?e.transpose(0,1):e,f={inputSize:this.inputSize,hiddenSize:this.hiddenSize,seqLen:c.shape[0],batch:c.shape[1]},[d,h]=r(c,this.cells,f,t);return[this.batchFirst?d.transpose(0,1):d,h]}let s=Sf();if(s&&e.device.type==="webgpu"&&!As()){let c=this.batchFirst?e.transpose(0,1):e,f={kind:"gru",inputSize:this.inputSize,hiddenSize:this.hiddenSize,seqLen:c.shape[0],batch:c.shape[1]},d=s(c,this.cells,f,t,null);if(d){let[h,m]=d;return[this.batchFirst?h.transpose(0,1):h,m]}}let o=this.batchFirst?e.transpose(0,1):e,i=o.shape[1],a=o,u=[];for(let c=0;c<this.numLayers;c++){let f=t!==null?en(t,0,c):Ve([i,this.hiddenSize],{device:o.device}),d=this.cells[c],[h,m]=od((g,y)=>{let _=d.forward(y,g);return[_,_]},f,a);u.push(h),a=m}let p=a;return this.batchFirst&&(p=p.transpose(0,1)),[p,us(u,0)]}};kt();be();be();_u();wn();qn();var fc=class extends ge{static{l(this,"LSTMCell")}inputSize;hiddenSize;x2h;h2h;constructor(e,t,r=!0){super(),this.inputSize=e,this.hiddenSize=t,this.x2h=new Sn(e,4*t,r),this.h2h=new Sn(t,4*t,r)}forward(e,t=null){let r=t!==null?t[0]:Ve([e.shape[0],this.hiddenSize],{device:e.device}),s=t!==null?t[1]:Ve([e.shape[0],this.hiddenSize],{device:e.device}),o=ae(this.x2h.forward(e),this.h2h.forward(r)),[i,a,u,p]=hi(o,this.hiddenSize,-1),c=wr(i),f=wr(a),d=Hs(u),h=wr(p),m=ae(ee(f,s),ee(c,d));return[ee(h,Hs(m)),m]}},ad=class extends ge{static{l(this,"LSTM")}inputSize;hiddenSize;numLayers;batchFirst;cells;constructor(e,t,r=1,s=!1,o=!0){super(),this.inputSize=e,this.hiddenSize=t,this.numLayers=r,this.batchFirst=s,this.cells=[];for(let i=0;i<r;i++){let a=new fc(i===0?e:t,t,o);this.cells.push(a),this.registerModule("cell_"+i,a)}}forward(e,t=null){let r=aS();if(r&&e.device.type==="gpu"&&!As()){let f=this.batchFirst?e.transpose(0,1):e,d={inputSize:this.inputSize,hiddenSize:this.hiddenSize,seqLen:f.shape[0],batch:f.shape[1]},[h,m,g]=r(f,this.cells,d,t?t[0]:null,t?t[1]:null);return[this.batchFirst?h.transpose(0,1):h,[m,g]]}let s=Sf();if(s&&e.device.type==="webgpu"&&!As()){let f=this.batchFirst?e.transpose(0,1):e,d={inputSize:this.inputSize,hiddenSize:this.hiddenSize,seqLen:f.shape[0],batch:f.shape[1]},h=s(f,this.cells,d,t?t[0]:null,t?t[1]:null);if(h){let[m,g,y]=h;return[this.batchFirst?m.transpose(0,1):m,[g,y]]}}let o=this.batchFirst?e.transpose(0,1):e,i=o.shape[1],a=o,u=[],p=[];for(let f=0;f<this.numLayers;f++){let d=t!==null?en(t[0],0,f):Ve([i,this.hiddenSize],{device:o.device}),h=t!==null?en(t[1],0,f):Ve([i,this.hiddenSize],{device:o.device}),m=this.cells[f],[[g,y],_]=od((b,w)=>{let[x,S]=m.forward(w,b);return[[x,S],x]},[d,h],a);u.push(g),p.push(y),a=_}let c=a;return this.batchFirst&&(c=c.transpose(0,1)),[c,[us(u,0),us(p,0)]]}};var ud=class extends ge{static{l(this,"Sequential")}_length;constructor(...e){super();for(let t=0;t<e.length;t++)this[String(t)]=e[t],this.registerModule(String(t),e[t]);this._length=e.length}forward(e){let t=e;for(let r=0;r<this._length;r++)t=this[String(r)].forward(t);return t}get length(){return this._length}*[Symbol.iterator](){for(let e=0;e<this._length;e++)yield this[String(e)]}push(e){let t=this._length;return this[String(t)]=e,this.registerModule(String(t),e),this._length++,this}},aa=class extends ge{static{l(this,"ModuleList")}_list;constructor(e){if(super(),this._list=[],e)for(let t=0;t<e.length;t++)this._list.push(e[t]),this.registerModule(String(t),e[t])}get length(){return this._list.length}get(e){return this._list[e]}push(e){let t=this._list.length;return this._list.push(e),this.registerModule(String(t),e),this}*[Symbol.iterator](){for(let e of this._list)yield e}forward(){throw new Error("ModuleList does not implement forward()")}},ld=class extends ge{static{l(this,"ModuleDict")}_dict;constructor(e){if(super(),this._dict=new Map,e)for(let[t,r]of Object.entries(e))this._dict.set(t,r),this.registerModule(t,r)}get(e){return this._dict.get(e)}set(e,t){return this._dict.set(e,t),this.registerModule(e,t),this}has(e){return this._dict.has(e)}get size(){return this._dict.size}*keys(){yield*this._dict.keys()}*values(){yield*this._dict.values()}*[Symbol.iterator](){yield*this._dict.entries()}forward(){throw new Error("ModuleDict does not implement forward()")}};be();var cd=class extends ge{static{l(this,"Flatten")}startDim;endDim;constructor(e=1,t=-1){super(),this.startDim=e,this.endDim=t}forward(e){let t=e.shape,r=t.length,s=this.startDim<0?r+this.startDim:this.startDim,o=this.endDim<0?r+this.endDim:this.endDim,i=1;for(let u=s;u<=o;u++)i*=t[u];let a=[];for(let u=0;u<s;u++)a.push(t[u]);a.push(i);for(let u=o+1;u<r;u++)a.push(t[u]);return Hn(e,a)}};be();kt();kt();function pE(n){return n==="gelu"?Pf:Df}l(pE,"_getActivation");var ua=class extends ge{static{l(this,"MultiheadAttention")}embedDim;numHeads;headDim;batchFirst;dropout;qProj;kProj;vProj;outProj;constructor(e,t,r=0,s=!0,o=null,i=null,a=!0){super(),this.embedDim=e,this.numHeads=t,this.headDim=Math.floor(e/t),this.batchFirst=a,this.dropout=r,this.qProj=new Sn(e,e,s),this.kProj=new Sn(o??e,e,s),this.vProj=new Sn(i??e,e,s),this.outProj=new Sn(e,e,s)}forward(e,t,r,s=null,o=null,i=!1){this.batchFirst||(e=e.transpose(0,1),t=t.transpose(0,1),r=r.transpose(0,1));let a=e.shape[0],u=e.shape[1],p=t.shape[1],c=this.qProj.forward(e),f=this.kProj.forward(t),d=this.vProj.forward(r);if(c=c.reshape([a,u,this.numHeads,this.headDim]).permute([0,2,1,3]),f=f.reshape([a,p,this.numHeads,this.headDim]).permute([0,2,1,3]),d=d.reshape([a,p,this.numHeads,this.headDim]).permute([0,2,1,3]),o){let g=ot(o.shape,-1/0),y=ot(o.shape,0),_=or(o,g,y);_=_.unsqueeze(1).unsqueeze(2),s=s?ae(s,_):_}let h=QS(c,f,d,s,this.dropout,i,this.training);h=h.permute([0,2,1,3]).reshape([a,u,this.embedDim]);let m=this.outProj.forward(h);return this.batchFirst||(m=m.transpose(0,1)),m}},bu=class extends ge{static{l(this,"TransformerEncoderLayer")}selfAttn;linear1;linear2;norm1;norm2;dropout1;dropout2;dropoutFFN;_activation;_activationName;normFirst;_dModel;_nhead;_dimFeedforward;_dropout;_layerNormEps;_batchFirst;constructor(e,t,r=2048,s=.1,o="relu",i=1e-5,a=!0,u=!1){super(),this.selfAttn=new ua(e,t,s,!0,null,null,a),this.linear1=new Sn(e,r),this.linear2=new Sn(r,e),this.norm1=new no(e,i),this.norm2=new no(e,i),this.dropout1=new Sr(s),this.dropout2=new Sr(s),this.dropoutFFN=new Sr(s),this._activation=pE(o),this._activationName=o,this.normFirst=u,this._dModel=e,this._nhead=t,this._dimFeedforward=r,this._dropout=s,this._layerNormEps=i,this._batchFirst=a}forward(e,t=null,r=null,s=!1){return this.normFirst?this._forwardPreNorm(e,t,r,s):this._forwardPostNorm(e,t,r,s)}_forwardPostNorm(e,t,r,s){let o=this.selfAttn.forward(e,e,e,t,r,s);o=this.norm1.forward(ae(e,this.dropout1.forward(o)));let i=this._activation(this.linear1.forward(o));return i=this.linear2.forward(this.dropoutFFN.forward(i)),o=this.norm2.forward(ae(o,this.dropout2.forward(i))),o}_forwardPreNorm(e,t,r,s){let o=this.norm1.forward(e),i=this.selfAttn.forward(o,o,o,t,r,s);i=ae(e,this.dropout1.forward(i));let a=this._activation(this.linear1.forward(this.norm2.forward(i)));return a=this.linear2.forward(this.dropoutFFN.forward(a)),i=ae(i,this.dropout2.forward(a)),i}},wu=class extends ge{static{l(this,"TransformerDecoderLayer")}selfAttn;crossAttn;linear1;linear2;norm1;norm2;norm3;dropout1;dropout2;dropout3;dropoutFFN;_activation;_activationName;normFirst;_dModel;_nhead;_dimFeedforward;_dropout;_layerNormEps;_batchFirst;constructor(e,t,r=2048,s=.1,o="relu",i=1e-5,a=!0,u=!1){super(),this.selfAttn=new ua(e,t,s,!0,null,null,a),this.crossAttn=new ua(e,t,s,!0,null,null,a),this.linear1=new Sn(e,r),this.linear2=new Sn(r,e),this.norm1=new no(e,i),this.norm2=new no(e,i),this.norm3=new no(e,i),this.dropout1=new Sr(s),this.dropout2=new Sr(s),this.dropout3=new Sr(s),this.dropoutFFN=new Sr(s),this._activation=pE(o),this._activationName=o,this.normFirst=u,this._dModel=e,this._nhead=t,this._dimFeedforward=r,this._dropout=s,this._layerNormEps=i,this._batchFirst=a}forward(e,t,r=null,s=null,o=null,i=null,a=!1){return this.normFirst?this._forwardPreNorm(e,t,r,s,o,i,a):this._forwardPostNorm(e,t,r,s,o,i,a)}_forwardPostNorm(e,t,r,s,o,i,a){let u=this.selfAttn.forward(e,e,e,r,o,a);u=this.norm1.forward(ae(e,this.dropout1.forward(u)));let p=this.crossAttn.forward(u,t,t,s,i);u=this.norm2.forward(ae(u,this.dropout2.forward(p)));let c=this._activation(this.linear1.forward(u));return c=this.linear2.forward(this.dropoutFFN.forward(c)),u=this.norm3.forward(ae(u,this.dropout3.forward(c))),u}_forwardPreNorm(e,t,r,s,o,i,a){let u=this.norm1.forward(e),p=this.selfAttn.forward(u,u,u,r,o,a);p=ae(e,this.dropout1.forward(p));let c=this.norm2.forward(p),f=this.crossAttn.forward(c,t,t,s,i);p=ae(p,this.dropout2.forward(f));let d=this._activation(this.linear1.forward(this.norm3.forward(p)));return d=this.linear2.forward(this.dropoutFFN.forward(d)),p=ae(p,this.dropout3.forward(d)),p}};function a6(n){return new bu(n._dModel,n._nhead,n._dimFeedforward,n._dropout,n._activationName,n._layerNormEps,n._batchFirst,n.normFirst)}l(a6,"_cloneEncoderLayer");function u6(n){return new wu(n._dModel,n._nhead,n._dimFeedforward,n._dropout,n._activationName,n._layerNormEps,n._batchFirst,n.normFirst)}l(u6,"_cloneDecoderLayer");var dc=class extends ge{static{l(this,"TransformerEncoder")}layers;norm;constructor(e,t,r=null){super(),this.layers=new aa(Array.from({length:t},()=>a6(e))),this.norm=r}forward(e,t=null,r=null,s=!1){let o=e;for(let i of this.layers)o=i.forward(o,t,r,s);return this.norm&&(o=this.norm.forward(o)),o}},hc=class extends ge{static{l(this,"TransformerDecoder")}layers;norm;constructor(e,t,r=null){super(),this.layers=new aa(Array.from({length:t},()=>u6(e))),this.norm=r}forward(e,t,r=null,s=null,o=null,i=null,a=!1){let u=e;for(let p of this.layers)u=p.forward(u,t,r,s,o,i,a);return this.norm&&(u=this.norm.forward(u)),u}},pd=class extends ge{static{l(this,"Transformer")}encoder;decoder;dModel;constructor({dModel:e=512,nhead:t=8,numEncoderLayers:r=6,numDecoderLayers:s=6,dimFeedforward:o=2048,dropout:i=.1,activation:a="relu",batchFirst:u=!0,normFirst:p=!1,layerNormEps:c=1e-5}={}){super();let f=new bu(e,t,o,i,a,c,u,p),d=new wu(e,t,o,i,a,c,u,p);this.encoder=new dc(f,r),this.decoder=new hc(d,s),this.dModel=e}forward(e,t,r=null,s=null,o=null,i=null,a=null,u=null){let p=this.encoder.forward(e,r,i);return this.decoder.forward(t,p,s,o,a,u)}static generateSquareSubsequentMask(e){let t=Tn([e,e]),r=t._impl.storage.data;for(let s=0;s<e;s++)for(let o=0;o<e;o++)r[s*e+o]=o<=s?0:-1/0;return t}};be();kt();var fd=class extends ge{static{l(this,"PositionalEncoding")}dropoutLayer;pe;constructor(e,t=5e3,r=.1){super(),this.dropoutLayer=new Sr(r);let s=Ve([1,t,e]),o=s._impl.storage.data;for(let i=0;i<t;i++)for(let a=0;a<e;a+=2){let u=i*Math.exp(-(a*Math.log(1e4))/e);o[i*e+a]=Math.sin(u),a+1<e&&(o[i*e+a+1]=Math.cos(u))}this.pe=s,this.registerBuffer("pe",s)}forward(e){let t=e.shape[1],r=this.pe.narrow(1,0,t);return this.dropoutLayer.forward(ae(e,r))}};var fE={...lc,...S_,...GS,...k_,...A_,...WS,...B_,...ZS,...XS};var hE={};at(hE,{BatchSampler:()=>vu,DataLoader:()=>md,Dataset:()=>Tu,MapDataset:()=>hd,RandomSampler:()=>Su,Sampler:()=>la,SequentialSampler:()=>xu,TensorDataset:()=>dd,defaultCollate:()=>ku});be();var Tu=class{static{l(this,"Dataset")}get length(){throw new Error("Subclass must implement get length()")}get(e){throw new Error("Subclass must implement get(index)")}*[Symbol.iterator](){let e=this.length;for(let t=0;t<e;t++)yield this.get(t)}},dd=class extends Tu{static{l(this,"TensorDataset")}_tensors;_length;constructor(...e){if(super(),e.length===0)throw new Error("TensorDataset requires at least one tensor");let t=e[0].shape[0];for(let r=1;r<e.length;r++)if(e[r].shape[0]!==t)throw new Error(`Size mismatch at dim 0: tensor 0 has ${t}, tensor ${r} has ${e[r].shape[0]}`);this._tensors=e,this._length=t}get length(){return this._length}get(e){let t=new Array(this._tensors.length);for(let r=0;r<this._tensors.length;r++)t[r]=en(this._tensors[r],0,e);return t}},hd=class extends Tu{static{l(this,"MapDataset")}_dataset;_transform;constructor(e,t){super(),this._dataset=e,this._transform=t}get length(){return this._dataset.length}get(e){return this._transform(this._dataset.get(e))}};var la=class{static{l(this,"Sampler")}*[Symbol.iterator](){throw new Error("Subclass must implement [Symbol.iterator]()")}},xu=class extends la{static{l(this,"SequentialSampler")}_dataSource;constructor(e){super(),this._dataSource=e}*[Symbol.iterator](){let e=this._dataSource.length;for(let t=0;t<e;t++)yield t}},Su=class extends la{static{l(this,"RandomSampler")}_dataSource;constructor(e){super(),this._dataSource=e}*[Symbol.iterator](){let e=this._dataSource.length,t=new Int32Array(e);for(let r=0;r<e;r++)t[r]=r;for(let r=e-1;r>0;r--){let s=Math.floor(Math.random()*(r+1)),o=t[r];t[r]=t[s],t[s]=o}for(let r=0;r<e;r++)yield t[r]}},vu=class extends la{static{l(this,"BatchSampler")}_dropLast;_sampler;_batchSize;constructor(e,t,r=!1){super(),this._sampler=e,this._batchSize=t,this._dropLast=r}*[Symbol.iterator](){let e=[];for(let t of this._sampler)e.push(t),e.length===this._batchSize&&(yield e,e=[]);e.length>0&&!this._dropLast&&(yield e)}};sr();os();rs();function l6(n,e=0){let t=n.length,r=n[0].shape,s=n[0].dtype,o=[...r];o.splice(e,0,t);let i=r.reduce((c,f)=>c*f,1),a=St(s),u=new a(t*i),p=u;for(let c=0;c<t;c++){let f=n[c],d=f.data,h=f._impl?f._impl.storageOffset:0;if(f.isContiguous)p.set(d.subarray(h,h+i),c*i);else{let m=f.shape,g=f.strides,y=f._impl.storage.data,_=m.length,b=new Int32Array(_),w=h;for(let x=0;x<i;x++){p[c*i+x]=y[w];for(let S=_-1;S>=0;S--){if(b[S]++,b[S]<m[S]){w+=g[S];break}w-=(m[S]-1)*g[S],b[S]=0}}}}return eu(u,o,s)}l(l6,"stackTensors");function ku(n){let e=n[0];if(e instanceof Ze)return l6(n,0);if(typeof e=="number")return Wt(n);if(Array.isArray(e)){let t=new Array(e.length);for(let r=0;r<e.length;r++){let s=new Array(n.length);for(let o=0;o<n.length;o++)s[o]=n[o][r];t[r]=ku(s)}return t}if(e!==null&&typeof e=="object"&&e.constructor===Object){let t=Object.keys(e),r={};for(let s=0;s<t.length;s++){let o=t[s],i=new Array(n.length);for(let a=0;a<n.length;a++)i[a]=n[a][o];r[o]=ku(i)}return r}throw new Error(`defaultCollate: unsupported element type ${typeof e}`)}l(ku,"defaultCollate");var md=class{static{l(this,"DataLoader")}_dataset;_collate;_batchSampler;_batchSize;_dropLast;constructor(e,t={}){if(this._dataset=e,this._collate=t.collate??(r=>ku(r)),t.batchSampler!=null){if(t.batchSize!=null||t.shuffle||t.sampler!=null||t.dropLast)throw new Error("batchSampler is mutually exclusive with batchSize, shuffle, sampler, and dropLast");this._batchSampler=t.batchSampler,this._batchSize=null,this._dropLast=null}else{let r=t.batchSize??1,s=t.dropLast??!1;this._batchSize=r,this._dropLast=s;let o;if(t.sampler!=null){if(t.shuffle)throw new Error("sampler and shuffle are mutually exclusive");o=t.sampler}else o=t.shuffle?new Su(e):new xu(e);this._batchSampler=new vu(o,r,s)}}get dataset(){return this._dataset}get length(){let e=this._dataset.length;if(this._batchSize==null){let t=0;for(let r of this._batchSampler)t++;return t}return this._dropLast?Math.floor(e/this._batchSize):Math.ceil(e/this._batchSize)}*[Symbol.iterator](){for(let e of this._batchSampler){let t=new Array(e.length);for(let r=0;r<e.length;r++)t[r]=this._dataset.get(e[r]);yield this._collate(t)}}};var TE={};at(TE,{DEFAULT_SPECIALS:()=>F_,TOKENIZER_FORMAT:()=>yc,TOKENIZER_VERSION:()=>_c,Tokenizer:()=>yd,Vocab:()=>ca});var ca=class n{static{l(this,"Vocab")}_tokenToId;_idToToken;constructor(e=[]){this._tokenToId=new Map,this._idToToken=[];for(let t of e)this.add(t)}add(e){let t=this._tokenToId.get(e);return t===void 0&&(t=this._idToToken.length,this._tokenToId.set(e,t),this._idToToken.push(e)),t}getId(e,t=-1){let r=this._tokenToId.get(e);return r===void 0?t:r}getToken(e){return e>=0&&e<this._idToToken.length?this._idToToken[e]:void 0}has(e){return this._tokenToId.has(e)}get size(){return this._idToToken.length}tokens(){return this._idToToken.slice()}static fromTokens(e){if(!Array.isArray(e))throw new Error("mlfw tokenizer: vocab must be an array");let t=new n,r=new Set;for(let s of e){if(typeof s!="string")throw new Error("mlfw tokenizer: vocab entries must be strings");if(r.has(s))throw new Error(`mlfw tokenizer: duplicate vocab token '${s}'`);r.add(s),t.add(s)}return t}};var L_=class n{static{l(this,"WordStrategy")}_lowercase;constructor({lowercase:e=!1}={}){this._lowercase=e}fit(e,t){}segment(e){return(this._lowercase?String(e).toLowerCase():String(e)).split(/\s+/).filter(Boolean)}detokenize(e){return e.join(" ")}toJSON(){return{lowercase:this._lowercase}}static fromJSON(e={}){return new n({lowercase:e.lowercase??!1})}};var C_=class n{static{l(this,"CharStrategy")}_lowercase;constructor({lowercase:e=!1}={}){this._lowercase=e}fit(e,t){}segment(e){let t=this._lowercase?String(e).toLowerCase():String(e);return Array.from(t)}detokenize(e){return e.join("")}toJSON(){return{lowercase:this._lowercase}}static fromJSON(e={}){return new n({lowercase:e.lowercase??!1})}};function D_(n,e){return n+""+e}l(D_,"pairKey");var P_=class n{static{l(this,"BpeStrategy")}_numMerges;_lowercase;_eow;_ranks;_encodeCache;constructor({numMerges:e=1e3,lowercase:t=!1,endOfWord:r="</w>"}={}){this._numMerges=e,this._lowercase=t,this._eow=r,this._ranks=new Map,this._encodeCache=new Map}_pretokenize(e){return(this._lowercase?String(e).toLowerCase():String(e)).split(/\s+/).filter(Boolean)}_baseSymbols(e){let t=Array.from(e);return t.push(this._eow),t}fit(e,t){this._encodeCache=new Map;let r=new Map;for(let h of e)for(let m of this._pretokenize(h))r.set(m,(r.get(m)||0)+1);let s=new Map;for(let h of r.keys())s.set(h,this._baseSymbols(h));let o=new Map,i=new Map,a=[],u=l((h,m)=>h[0]>m[0]||h[0]===m[0]&&h[1]<m[1],"higher"),p=l((h,m)=>{a.push([h,m]);let g=a.length-1;for(;g>0;){let y=g-1>>1;if(u(a[g],a[y])){let _=a[y];a[y]=a[g],a[g]=_,g=y}else break}},"pushHeap"),c=l(()=>{let h=a[0],m=a.pop();if(a.length>0){a[0]=m;let g=0,y=a.length;for(;;){let _=g,b=2*g+1,w=2*g+2;if(b<y&&u(a[b],a[_])&&(_=b),w<y&&u(a[w],a[_])&&(_=w),_===g)break;let x=a[_];a[_]=a[g],a[g]=x,g=_}}return h},"popHeap"),f=l(h=>{let m=s.get(h),g=r.get(h);for(let y=0;y+1<m.length;y++){let _=D_(m[y],m[y+1]),b=(o.get(_)||0)+g;o.set(_,b),p(b,_);let w=i.get(_);w||(w=new Set,i.set(_,w)),w.add(h)}},"addWordPairs"),d=l(h=>{let m=s.get(h),g=r.get(h);for(let y=0;y+1<m.length;y++){let _=D_(m[y],m[y+1]),b=(o.get(_)||0)-g;b<=0?o.delete(_):(o.set(_,b),p(b,_));let w=i.get(_);w&&w.delete(h)}},"removeWordPairs");for(let h of r.keys())f(h);this._ranks=new Map;for(let h=0;h<this._numMerges;h++){let m=null,g=0;for(;a.length>0;){let[S,T]=c(),v=o.get(T);if(v===S&&v>0){m=T,g=v;break}}if(m===null||g<=0)break;let y=m.indexOf(""),_=m.slice(0,y),b=m.slice(y+1),w=_+b;this._ranks.set(m,h);let x=i.get(m);if(!x||x.size===0)break;for(let S of[...x]){d(S);let T=s.get(S),v=[];for(let A=0;A<T.length;A++)A+1<T.length&&T[A]===_&&T[A+1]===b?(v.push(w),A++):v.push(T[A]);s.set(S,v),f(S)}}}_encodeWord(e){let t=this._encodeCache.get(e);if(t!==void 0)return t;let r=this._baseSymbols(e);for(;r.length>1;){let s=1/0,o=-1;for(let i=0;i+1<r.length;i++){let a=this._ranks.get(D_(r[i],r[i+1]));a!==void 0&&a<s&&(s=a,o=i)}if(o<0)break;r=r.slice(0,o).concat(r[o]+r[o+1],r.slice(o+2))}return this._encodeCache.set(e,r),r}segment(e){let t=[];for(let r of this._pretokenize(e))for(let s of this._encodeWord(r))t.push(s);return t}detokenize(e){let t="";for(let r of e)t+=r;return t.split(this._eow).join(" ").trim()}toJSON(){let e=[...this._ranks.entries()].map(([t])=>{let r=t.indexOf("");return[t.slice(0,r),t.slice(r+1)]});return{numMerges:this._numMerges,lowercase:this._lowercase,endOfWord:this._eow,merges:e}}static fromJSON(e={}){if(!Array.isArray(e.merges))throw new Error("mlfw tokenizer: bpe strategy merges must be an array");let t=new n({numMerges:e.numMerges??e.merges.length,lowercase:e.lowercase??!1,endOfWord:e.endOfWord??"</w>"});t._ranks=new Map;for(let r=0;r<e.merges.length;r++){let s=e.merges[r];if(!Array.isArray(s)||s.length!==2||s.some(o=>typeof o!="string"))throw new Error("mlfw tokenizer: bpe merges must be string pairs");t._ranks.set(D_(s[0],s[1]),r)}return t}};os();import{writeFileSync as mE,appendFileSync as c6,readFileSync as gE,existsSync as p6,mkdirSync as f6,readdirSync as d6,unlinkSync as h6,renameSync as m6}from"node:fs";import{resolve as gd}from"node:path";var vn={readFile(n){return gE(gd(n),"utf8")},readBinary(n){return gE(gd(n))},writeFile(n,e){mE(n,e)},writeBinary(n,e){mE(gd(n),e)},appendFile(n,e){c6(n,e)},exists(n){return p6(n)},mkdir(n){f6(n,{recursive:!0})},readdir(n){return d6(n)},remove(n){h6(n)},rename(n,e){m6(gd(n),gd(e))}};var yc="mlfw-tokenizer",_c=1,F_=Object.freeze({pad:"<pad>",unk:"<unk>",bos:"<bos>",eos:"<eos>"}),yE=`${yc}-v${_c}`,$_={word:L_,char:C_,bpe:P_},gc=Object.freeze(["pad","unk","bos","eos"]),yd=class n{static{l(this,"Tokenizer")}_mode;_specials;_maxVocab;_strategy;_vocab;constructor(e={}){let t=e.mode??"word",r=$_[t];if(!r)throw new Error(`Unknown tokenizer mode '${t}'. Available: ${Object.keys($_).join(", ")}`);this._mode=t,this._specials=_E(e.specialTokens),bE(this._specials),this._maxVocab=e.vocabSize??null,wE(this._maxVocab,this._specials),this._strategy=new r(e),this._vocab=null}get mode(){return this._mode}get vocabSize(){return this._fitVocab().size}get padId(){return this._specialId("pad")}get unkId(){return this._specialId("unk")}get bosId(){return this._specialId("bos")}get eosId(){return this._specialId("eos")}_ensureFit(){if(!this._vocab)throw new Error("Tokenizer must be fit() on a corpus before use")}_fitVocab(){return this._ensureFit(),this._vocab}_specialId(e){return this._fitVocab().getId(this._specials[e])}fit(e){let t=Array.isArray(e)?e:[e];this._strategy.fit(t,{vocabSize:this._maxVocab});let r=new ca(Object.values(this._specials));if(this._maxVocab==null)for(let s of t)for(let o of this._strategy.segment(s))r.add(o);else{let s=new Map;for(let a of t)for(let u of this._strategy.segment(a))s.set(u,(s.get(u)||0)+1);let o=[...s.entries()].sort((a,u)=>u[1]-a[1]),i=this._maxVocab-r.size;for(let a=0;a<o.length&&a<i;a++)r.add(o[a][0])}return this._vocab=r,this}toJSON(){let e=this._fitVocab(),t=this._strategy.toJSON();return{format:yc,version:_c,mode:this._mode,config:{vocabSize:this._maxVocab,...t},specialTokens:{...this._specials},vocab:e.tokens(),strategy:t}}save(e){if(typeof e!="string")throw new Error("mlfw tokenizer: save(path) requires a file path string");let t=e+".tmp";vn.writeFile(t,y6(this.toJSON())),vn.rename(t,e)}static load(e){if(typeof e!="string")throw new Error("mlfw tokenizer: load(path) requires a file path string");let t=vn.readFile(e),r=typeof t=="string"?t:new TextDecoder().decode(t);return n.fromJSON(_6(r))}static fromJSON(e){g6(e);let t=$_[e.mode],r=new n({mode:e.mode,vocabSize:e.config.vocabSize,specialTokens:e.specialTokens});r._strategy=t.fromJSON(e.strategy),r._vocab=ca.fromTokens(e.vocab);for(let s of Object.values(r._specials))if(!r._vocab.has(s))throw new Error(`mlfw tokenizer: special token '${s}' is missing from vocab`);return r}encode(e,t={}){let r=this._fitVocab(),s=[];t.addBos&&s.push(this.bosId);let o=this.unkId;for(let i of this._strategy.segment(e))s.push(r.getId(i,o));return t.addEos&&s.push(this.eosId),s}decode(e,t={}){let r=this._fitVocab(),s=t.skipSpecial??!0,o=new Set(Object.values(this._specials).map(a=>r.getId(a))),i=[];for(let a of e){if(s&&o.has(a))continue;let u=r.getToken(a);u!==void 0&&i.push(u)}return this._strategy.detokenize(i)}encodeBatch(e,t={}){this._ensureFit();let s=(Array.isArray(e)?e:[e]).map(p=>this.encode(p,t)),o=t.maxLen??s.reduce((p,c)=>Math.max(p,c.length),0),i=t.padId??this.padId,a=s.length,u=new Int32Array(a*o).fill(i);for(let p=0;p<a;p++){let c=s[p],f=Math.min(c.length,o);for(let d=0;d<f;d++)u[p*o+d]=c[d]}return Wt(u,{shape:[a,o],dtype:"i32"})}};function _E(n){if(Array.isArray(n)){let e={...F_};for(let t=0;t<n.length&&t<gc.length;t++)e[gc[t]]=n[t];return e}return{...F_,...n??{}}}l(_E,"normalizeSpecials");function bE(n){let e=new Set;for(let t of gc){let r=n[t];if(typeof r!="string"||r.length===0)throw new Error(`mlfw tokenizer: special token '${t}' must be a non-empty string`);if(e.has(r))throw new Error(`mlfw tokenizer: duplicate special token '${r}'`);e.add(r)}}l(bE,"validateSpecials");function wE(n,e){if(n!=null&&(!Number.isInteger(n)||n<Object.keys(e).length))throw new Error(`mlfw tokenizer: vocabSize must be an integer >= ${Object.keys(e).length}`)}l(wE,"validateVocabSize");function g6(n){if(!n||typeof n!="object")throw new Error("mlfw tokenizer: artifact must be an object");let e=n;if(e.format!==yc)throw new Error("mlfw tokenizer: unrecognized tokenizer format");if(e.version!==_c)throw new Error(`mlfw tokenizer: unsupported tokenizer version ${e.version}`);if(typeof e.mode!="string"||!(e.mode in $_))throw new Error(`mlfw tokenizer: unknown tokenizer mode '${e.mode}'`);if(!e.config||typeof e.config!="object")throw new Error("mlfw tokenizer: config must be an object");let t=e.config,r=_E(x6(e.specialTokens));if(bE(r),wE(S6(t.vocabSize),r),!Array.isArray(e.vocab))throw new Error("mlfw tokenizer: vocab must be an array");if(!e.strategy||typeof e.strategy!="object")throw new Error("mlfw tokenizer: strategy must be an object")}l(g6,"validateTokenizerData");function y6(n){let e=[yE,Nu("m",[n.mode]),Nu("z",[n.config.vocabSize==null?"":String(n.config.vocabSize)]),Nu("s",gc.map(t=>n.specialTokens[t]))];if(n.mode==="bpe"){let t=[];for(let r of n.strategy.merges)t.push(r[0],r[1]);e.push(Nu("g",[String(n.strategy.lowercase?1:0),String(n.strategy.numMerges),n.strategy.endOfWord])),e.push(Nu("r",t))}else e.push(Nu("g",[String(n.strategy.lowercase?1:0)]));return e.push(Nu("v",n.vocab)),e.join(`
|
|
423
|
+
`)}l(y6,"serializeTokenizer");function _6(n){if(!n.startsWith(yE))throw new Error("mlfw tokenizer: unrecognized tokenizer format");let e=new Map;for(let p of n.split(/\r?\n/).slice(1)){if(!p)continue;let c=p.indexOf(" ");if(c<0)throw new Error("mlfw tokenizer: malformed tokenizer artifact");e.set(p.slice(0,c),b6(p.slice(c+1)))}let t=mc(e,"m")[0],r=mc(e,"z")[0],s=mc(e,"s"),o=mc(e,"g"),i=mc(e,"v"),a=Object.fromEntries(gc.map((p,c)=>[p,s[c]])),u={lowercase:o[0]==="1"};if(t==="bpe"){let p=mc(e,"r");if(p.length%2!==0)throw new Error("mlfw tokenizer: bpe merges must be string pairs");u.numMerges=Number(o[1]),u.endOfWord=o[2],u.merges=[];for(let c=0;c<p.length;c+=2)u.merges.push([p[c],p[c+1]])}return{format:yc,version:_c,mode:t,config:{vocabSize:r===""?null:Number(r),...u},specialTokens:a,vocab:i,strategy:u}}l(_6,"parseTokenizer");function Nu(n,e){return`${n} ${e.map(w6).join(" ")}`}l(Nu,"record");function b6(n){return n.split(" ").map(T6)}l(b6,"parseRecord");function w6(n){return String(n).replace(/\\/g,"\\\\").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r")}l(w6,"escapeField");function T6(n){let e="";for(let t=0;t<n.length;t++){let r=n[t];if(r!=="\\"){e+=r;continue}let s=n[++t];s==="t"?e+=" ":s==="n"?e+=`
|
|
424
|
+
`:s==="r"?e+="\r":s==="\\"?e+="\\":e+=s??""}return e}l(T6,"unescapeField");function mc(n,e){let t=n.get(e);if(!t)throw new Error(`mlfw tokenizer: missing '${e}' record`);return t}l(mc,"requiredField");function x6(n){if(n==null)return;if(Array.isArray(n))return n.map(String);if(typeof n!="object")return;let e=n,t={};for(let r of gc){let s=e[r];typeof s=="string"&&(t[r]=s)}return t}l(x6,"asSpecialTokensInput");function S6(n){return n==null||typeof n=="number"?n:Number(n)}l(S6,"asNullableNumber");var tC={};at(tC,{Adam:()=>bd,AdamW:()=>wd,CosineAnnealingLR:()=>xd,FusedAdam:()=>lw,FusedOptimizer:()=>Fc,FusedSGD:()=>uw,GradScaler:()=>cw,LRScheduler:()=>Au,Optimizer:()=>fs,ReduceLROnPlateau:()=>Sd,SGD:()=>_d,StepLR:()=>Td,clipGradNorm_:()=>vd,clipGradValue_:()=>kd});var fs=class{static{l(this,"Optimizer")}_defaults;_paramGroups;_state;_paramIndex;_nextId;constructor(e,t){this._defaults={...t},this._paramGroups=[],this._state=new Map,this._paramIndex=new WeakMap,this._nextId=0;let r=v6(e);for(let s of r)this._addParamGroup(s)}get paramGroups(){return this._paramGroups}get defaults(){return this._defaults}step(){throw new Error(`${this.constructor.name}.step() not implemented`)}zeroGrad(e=!0){for(let t of this._paramGroups)for(let r of t.params)r.grad!==null&&(e?r.grad=null:r.grad._impl.storage.data.fill(0))}stateDict(){let e=new Map;for(let[t,r]of this._state){let s={};for(let o of Object.keys(r)){let i=r[o];s[o]=xE(i)?SE(i):i}e.set(t,s)}return{state:e,paramGroups:this._paramGroups.map(t=>{let r={};for(let s of Object.keys(t)){if(s==="params")continue;let o=t[s];r[s]=Array.isArray(o)?[...o]:o}return r})}}loadStateDict(e){for(let t=0;t<this._paramGroups.length;t++){let r=e.paramGroups[t];for(let s of Object.keys(r))this._paramGroups[t][s]=Array.isArray(r[s])?[...r[s]]:r[s]}this._state=new Map;for(let[t,r]of e.state){let s={};for(let o of Object.keys(r)){let i=r[o];s[o]=xE(i)?SE(i):i}this._state.set(t,s)}}_addParamGroup(e){let t={...this._defaults,params:[]};for(let s of Object.keys(e))s!=="params"&&(t[s]=e[s]);let r=Array.isArray(e.params)?e.params:[...e.params];for(let s of r){if(this._paramIndex.has(s))throw new Error("Parameter appears in more than one parameter group");this._paramIndex.set(s,this._nextId++)}t.params=r,this._paramGroups.push(t)}_getParamId(e){return this._paramIndex.get(e)}_getState(e){let t=this._paramIndex.get(e),r=this._state.get(t);return r||(r={},this._state.set(t,r)),r}};function v6(n){if(n==null)throw new Error("Optimizer requires at least one parameter");let e=Array.isArray(n)?n:[...n];if(e.length===0)throw new Error("Optimizer got an empty parameter list");return k6(e[0])?e:[{params:e}]}l(v6,"normalizeParamGroups");function xE(n){return n instanceof Float32Array||n instanceof Float64Array||n instanceof Int32Array||n instanceof Int16Array||n instanceof Int8Array||n instanceof Uint8Array||n instanceof Uint16Array||n instanceof Uint32Array}l(xE,"isTypedArray");function SE(n){return n.slice()}l(SE,"cloneTypedArray");function k6(n){return typeof n=="object"&&n!==null&&"params"in n}l(k6,"isParamGroupInput");var _d=class extends fs{static{l(this,"SGD")}constructor(e,{lr:t=.01,momentum:r=0,dampening:s=0,weightDecay:o=0,nesterov:i=!1}={}){if(i&&(r===0||s!==0))throw new Error("Nesterov momentum requires momentum > 0 and dampening = 0");super(e,{lr:t,momentum:r,dampening:s,weightDecay:o,nesterov:i})}step(){for(let e of this._paramGroups){let t=e.lr,r=e.momentum,s=e.dampening,o=e.weightDecay,i=e.nesterov;for(let a of e.params){if(a.grad===null)continue;let u=a._impl.storage.data,p=a.grad._impl.storage.data,c=u.length,f=this._getState(a);if(r===0)if(o===0)for(let d=0;d<c;d++)u[d]-=t*p[d];else for(let d=0;d<c;d++)u[d]-=t*(p[d]+o*u[d]);else{let d=f.momentumBuffer;if(d){d=d;for(let h=0;h<c;h++){let m=o!==0?p[h]+o*u[h]:p[h];d[h]=r*d[h]+(1-s)*m}}else{d=new u.constructor(c);for(let h=0;h<c;h++)d[h]=o!==0?p[h]+o*u[h]:p[h];f.momentumBuffer=d}if(i)for(let h=0;h<c;h++){let m=o!==0?p[h]+o*u[h]:p[h];u[h]-=t*(m+r*d[h])}else for(let h=0;h<c;h++)u[h]-=t*d[h]}a._impl.bumpVersion()}}}};wn();qn();var bd=class extends fs{static{l(this,"Adam")}constructor(e,{lr:t=.001,betas:r=[.9,.999],eps:s=1e-8,weightDecay:o=0,amsgrad:i=!1}={}){super(e,{lr:t,betas:[...r],eps:s,weightDecay:o,amsgrad:i})}step(){let e=lS();for(let t of this._paramGroups){let r=t.lr,s=t.betas,o=t.eps,i=t.weightDecay,a=t.amsgrad,[u,p]=s;for(let c of t.params){if(c.grad===null)continue;let f=this._getState(c);if(e&&!a&&c.device&&c.device.type==="gpu"){f.step=(f.step||0)+1;let v=f.step,A=1-Math.pow(u,v),N=1-Math.pow(p,v);if(e(c,f,{beta1:u,beta2:p,omb1:1-u,omb2:1-p,eps:o,stepSize:r/A,bc2sqrt:Math.sqrt(N),wd:i,lr:r})){c._impl.bumpVersion();continue}}let d=c._impl.storage.data,h=c.grad._impl.storage.data,m=d.length,g=d.constructor;f.expAvg===void 0&&(f.step===void 0&&(f.step=0),f.expAvg=new g(m),f.expAvgSq=new g(m),a&&(f.maxExpAvgSq=new g(m))),f.step=f.step+1;let y=f.step,_=f.expAvg,b=f.expAvgSq,w=1-Math.pow(u,y),x=1-Math.pow(p,y),S=r/w,T=Math.sqrt(x);if(a){let v=f.maxExpAvgSq;for(let A=0;A<m;A++){let N=i!==0?h[A]+i*d[A]:h[A];_[A]=u*_[A]+(1-u)*N,b[A]=p*b[A]+(1-p)*N*N,b[A]>v[A]&&(v[A]=b[A]),d[A]-=S*_[A]/(Math.sqrt(v[A])/T+o)}}else for(let v=0;v<m;v++){let A=i!==0?h[v]+i*d[v]:h[v];_[v]=u*_[v]+(1-u)*A,b[v]=p*b[v]+(1-p)*A*A,d[v]-=S*_[v]/(Math.sqrt(b[v])/T+o)}c._impl.bumpVersion()}}}};var wd=class extends fs{static{l(this,"AdamW")}constructor(e,{lr:t=.001,betas:r=[.9,.999],eps:s=1e-8,weightDecay:o=.01,amsgrad:i=!1}={}){super(e,{lr:t,betas:[...r],eps:s,weightDecay:o,amsgrad:i})}step(){for(let e of this._paramGroups){let t=e.lr,r=e.betas,s=e.eps,o=e.weightDecay,i=e.amsgrad,[a,u]=r;for(let p of e.params){if(p.grad===null)continue;let c=p._impl.storage.data,f=p.grad._impl.storage.data,d=c.length,h=this._getState(p),m=c.constructor;h.step===void 0&&(h.step=0,h.expAvg=new m(d),h.expAvgSq=new m(d),i&&(h.maxExpAvgSq=new m(d))),h.step=h.step+1;let g=h.step,y=h.expAvg,_=h.expAvgSq,b=1-Math.pow(a,g),w=1-Math.pow(u,g),x=t/b,S=Math.sqrt(w);if(o!==0){let T=1-t*o;for(let v=0;v<d;v++)c[v]*=T}if(i){let T=h.maxExpAvgSq;for(let v=0;v<d;v++)y[v]=a*y[v]+(1-a)*f[v],_[v]=u*_[v]+(1-u)*f[v]*f[v],_[v]>T[v]&&(T[v]=_[v]),c[v]-=x*y[v]/(Math.sqrt(T[v])/S+s)}else for(let T=0;T<d;T++)y[T]=a*y[T]+(1-a)*f[T],_[T]=u*_[T]+(1-u)*f[T]*f[T],c[T]-=x*y[T]/(Math.sqrt(_[T])/S+s);p._impl.bumpVersion()}}}};var Au=class{static{l(this,"LRScheduler")}_optimizer;_baseLRs;_lastEpoch;_lastLR;constructor(e,t=-1){this._optimizer=e,this._baseLRs=e.paramGroups.map(r=>r.lr),this._lastEpoch=t,this._lastLR=null}_init(){this.step()}getLR(){throw new Error(`${this.constructor.name}.getLR() not implemented`)}getLastLR(){return this._lastLR}step(){this._lastEpoch++;let e=this.getLR();this._lastLR=e;let t=this._optimizer.paramGroups;for(let r=0;r<t.length;r++)t[r].lr=e[r]}},Td=class extends Au{static{l(this,"StepLR")}_stepSize;_gamma;constructor(e,t,r=.1,s=-1){super(e,s),this._stepSize=t,this._gamma=r,this._init()}getLR(){let e=Math.pow(this._gamma,Math.floor(this._lastEpoch/this._stepSize));return this._baseLRs.map(t=>t*e)}},xd=class extends Au{static{l(this,"CosineAnnealingLR")}_tMax;_etaMin;constructor(e,t,r=0,s=-1){super(e,s),this._tMax=t,this._etaMin=r,this._init()}getLR(){let e=(1+Math.cos(Math.PI*this._lastEpoch/this._tMax))/2;return this._baseLRs.map(t=>this._etaMin+(t-this._etaMin)*e)}},Sd=class{static{l(this,"ReduceLROnPlateau")}_optimizer;_mode;_factor;_patience;_threshold;_thresholdMode;_cooldown;_minLR;_eps;_best;_numBadEpochs;_cooldownCounter;constructor(e,{mode:t="min",factor:r=.1,patience:s=10,threshold:o=1e-4,thresholdMode:i="rel",cooldown:a=0,minLR:u=0,eps:p=1e-8}={}){this._optimizer=e,this._mode=t,this._factor=r,this._patience=s,this._threshold=o,this._thresholdMode=i,this._cooldown=a,this._minLR=u,this._eps=p,this._best=t==="min"?1/0:-1/0,this._numBadEpochs=0,this._cooldownCounter=0}step(e){if(e===void 0)throw new Error("ReduceLROnPlateau.step() requires a metric value");this._cooldownCounter>0&&(this._cooldownCounter--,this._numBadEpochs=0),this._isBetter(e)?(this._best=e,this._numBadEpochs=0):this._numBadEpochs++,this._numBadEpochs>this._patience&&(this._reduceAllLRs(),this._cooldownCounter=this._cooldown,this._numBadEpochs=0)}_isBetter(e){return this._mode==="min"?this._thresholdMode==="rel"?e<this._best*(1-this._threshold):e<this._best-this._threshold:this._thresholdMode==="rel"?e>this._best*(1+this._threshold):e>this._best+this._threshold}_reduceAllLRs(){for(let e of this._optimizer.paramGroups){let t=e.lr,r=Math.max(t*this._factor,this._minLR);t-r>this._eps&&(e.lr=r)}}};be();function vE(n){return(Array.isArray(n)?n:[...n]).filter(t=>t.grad!==null)}l(vE,"_gradParams");function vd(n,e,t=2){let r=vE(n);if(r.length===0)return 0;let s;if(t===2){let i=null;for(let a of r){let u=wt(ee(a.grad,a.grad));i=i===null?u:ae(i,u)}s=Math.sqrt(Number(i.item()))}else if(t===1/0){let i=null;for(let a of r){let u=No(Hp(a.grad));i=i===null?u:ta(i,u)}s=Number(i.item())}else{let i=null;for(let a of r){let u=wt(vo(Hp(a.grad),t));i=i===null?u:ae(i,u)}s=Math.pow(Number(i.item()),1/t)}let o=e/(s+1e-6);if(o<1)for(let i of r)i.grad=ee(i.grad,o);return s}l(vd,"clipGradNorm_");function kd(n,e){for(let t of vE(n))t.grad=Mg(t.grad,-e,e)}l(kd,"clipGradValue_");_s();Re();aw();yo();var oo="f32",Ft=l(n=>new I([n],oo),"VEC"),$c=new I([],oo);function Ms(n,e,t){return n.broadcast(e,[t],[]).getResult(0)}l(Ms,"bcast");var Fc=class extends fs{static{l(this,"FusedOptimizer")}_target;_kernels;constructor(e,t,r=null){super(e,t),this._target=r||go(),this._kernels=new Map}_kernel(e){let t=this._kernels.get(e);if(!t){let r=this._buildGraph(e);t=eC(r,this._target,{fusion:{enabled:!0}}),this._kernels.set(e,t)}return t}_buildGraph(e){throw new Error(`${this.constructor.name}._buildGraph() not implemented`)}},uw=class extends Fc{static{l(this,"FusedSGD")}constructor(e,{lr:t=.01,momentum:r=0,dampening:s=0,weightDecay:o=0,nesterov:i=!1}={},a=null){if(i&&(r===0||s!==0))throw new Error("Nesterov momentum requires momentum > 0 and dampening = 0");super(e,{lr:t,momentum:r,dampening:s,weightDecay:o,nesterov:i},a)}_buildGraph(e){let t=this._defaults.momentum,r=this._defaults.dampening,s=this._defaults.weightDecay,o=this._defaults.nesterov,i=t!==0,a=i?[Ft(e),Ft(e),Ft(e),$c]:[Ft(e),Ft(e),$c],u=i?[Ft(e),Ft(e)]:[Ft(e)];return mp("sgd_update",a,u,(c,f)=>{let d=f[0],h=f[1],m=i?f[3]:f[2],g=Ms(c,m,e),y=h;if(s!==0){let v=c.scalarConstant(s,oo).getResult(0);y=c.add(h,c.mul(Ms(c,v,e),d).getResult(0)).getResult(0)}if(!i){let v=c.sub(d,c.mul(g,y).getResult(0)).getResult(0);c.returnOp([v]);return}let _=f[2],b=Ms(c,c.scalarConstant(t,oo).getResult(0),e),w=Ms(c,c.scalarConstant(1-r,oo).getResult(0),e),x=c.add(c.mul(b,_).getResult(0),c.mul(w,y).getResult(0)).getResult(0),S=x;o&&(S=c.add(y,c.mul(b,x).getResult(0)).getResult(0));let T=c.sub(d,c.mul(g,S).getResult(0)).getResult(0);c.returnOp([T,x])})}step(){for(let e of this._paramGroups){let t=e.lr,s=e.momentum!==0,o=new Float32Array([t]);for(let i of e.params){if(i.grad===null)continue;let a=i._impl.storage.data,u=i.grad._impl.storage.data,p=a.length,c=this._kernel(p);if(s){let f=this._getState(i),d=f.momentumBuffer;d||(d=new a.constructor(p),f.momentumBuffer=d),d=d,c.run("sgd_update",a,u,d,o,a,d)}else c.run("sgd_update",a,u,o,a);i._impl.bumpVersion()}}}},lw=class extends Fc{static{l(this,"FusedAdam")}constructor(e,{lr:t=.001,betas:r=[.9,.999],eps:s=1e-8,weightDecay:o=0,amsgrad:i=!1}={},a=null){super(e,{lr:t,betas:[...r],eps:s,weightDecay:o,amsgrad:i},a)}_buildGraph(e){let t=this._defaults.betas,r=this._defaults.eps,s=this._defaults.weightDecay,o=this._defaults.amsgrad,[i,a]=t,u=o?[Ft(e),Ft(e),Ft(e),Ft(e),Ft(e),$c,$c]:[Ft(e),Ft(e),Ft(e),Ft(e),$c,$c],p=o?[Ft(e),Ft(e),Ft(e),Ft(e)]:[Ft(e),Ft(e),Ft(e)];return mp("adam_update",u,p,(c,f)=>{let d=f[0],h=f[1],m=f[2],g=f[3],y=o?f[4]:null,_=o?f[5]:f[4],b=o?f[6]:f[5],w=h;if(s!==0){let H=Ms(c,c.scalarConstant(s,oo).getResult(0),e);w=c.add(h,c.mul(H,d).getResult(0)).getResult(0)}let x=Ms(c,c.scalarConstant(i,oo).getResult(0),e),S=Ms(c,c.scalarConstant(a,oo).getResult(0),e),T=Ms(c,c.scalarConstant(1-i,oo).getResult(0),e),v=Ms(c,c.scalarConstant(1-a,oo).getResult(0),e),A=Ms(c,c.scalarConstant(r,oo).getResult(0),e),N=c.add(c.mul(x,m).getResult(0),c.mul(T,w).getResult(0)).getResult(0),k=c.mul(w,w).getResult(0),O=c.add(c.mul(S,g).getResult(0),c.mul(v,k).getResult(0)).getResult(0),R=O,B=null;o&&(B=c.maximum(y,O).getResult(0),R=B);let E=c.add(c.div(c.sqrt(R).getResult(0),Ms(c,b,e)).getResult(0),A).getResult(0),F=c.mul(Ms(c,_,e),c.div(N,E).getResult(0)).getResult(0),C=c.sub(d,F).getResult(0);c.returnOp(o?[C,N,O,B]:[C,N,O])})}step(){for(let e of this._paramGroups){let t=e.lr,r=e.betas,s=e.amsgrad,[o,i]=r;for(let a of e.params){if(a.grad===null)continue;let u=a._impl.storage.data,p=a.grad._impl.storage.data,c=u.length,f=this._getState(a);f.step===void 0&&(f.step=0,f.expAvg=new u.constructor(c),f.expAvgSq=new u.constructor(c),s&&(f.maxExpAvgSq=new u.constructor(c))),f.step=f.step+1;let d=f.step,h=1-Math.pow(o,d),m=1-Math.pow(i,d),g=new Float32Array([t/h]),y=new Float32Array([Math.sqrt(m)]),_=this._kernel(c),b=f.expAvg,w=f.expAvgSq;if(s){let x=f.maxExpAvgSq;_.run("adam_update",u,p,b,w,x,g,y,u,b,w,x)}else _.run("adam_update",u,p,b,w,g,y,u,b,w);a._impl.bumpVersion()}}}};var cw=class{static{l(this,"GradScaler")}enabled;_scale;_growthFactor;_backoffFactor;_growthInterval;_growthTracker;_foundInf;_unscaled;constructor(e={}){this.enabled=e.enabled!==!1,this._scale=e.initScale??65536,this._growthFactor=e.growthFactor??2,this._backoffFactor=e.backoffFactor??.5,this._growthInterval=e.growthInterval??2e3,this._growthTracker=0,this._foundInf=!1,this._unscaled=new WeakSet}getScale(){return this.enabled?this._scale:1}get growthTracker(){return this._growthTracker}scale(e){if(!this.enabled)return e;let t=e._impl.storage.data;for(let r=0;r<t.length;r++)t[r]*=this._scale;return e._impl.bumpVersion&&e._impl.bumpVersion(),e}unscale_(e){if(!this.enabled)return!1;let t=1/this._scale,r=!1;for(let s of e.paramGroups)for(let o of s.params){if(o.grad===null||o.grad===void 0)continue;let i=o.grad._impl.storage.data;for(let a=0;a<i.length;a++){let u=i[a]*t;Number.isFinite(u)||(r=!0),i[a]=u}}return this._unscaled.add(e),r&&(this._foundInf=!0),r}step(e){return this.enabled?(this._unscaled.has(e)||this.unscale_(e),this._foundInf?!1:(e.step(),!0)):(e.step(),!0)}update(e){this.enabled&&(e!==void 0?(this._scale=e,this._growthTracker=0):this._foundInf?(this._scale*=this._backoffFactor,this._growthTracker=0):(this._growthTracker++,this._growthTracker>=this._growthInterval&&(this._scale*=this._growthFactor,this._growthTracker=0)),this._foundInf=!1,this._unscaled=new WeakSet)}};mk();bw();yo();var $C={};at($C,{Accuracy:()=>dh,CSVLogger:()=>lh,Callback:()=>Zn,CallbackConnector:()=>jc,ConfusionMatrix:()=>yh,ConsoleLogger:()=>Du,EarlyStopping:()=>oh,EvaluationLoop:()=>Yc,F1Score:()=>gh,FitLoop:()=>Hc,GradientAccumulationScheduler:()=>uh,HOOKS:()=>yk,LearningRateMonitor:()=>ih,LightningModule:()=>th,Logger:()=>ki,LoggerConnector:()=>Uc,MeanMetric:()=>ch,Metric:()=>lr,MetricAccumulator:()=>zc,MetricCollection:()=>fh,ModelCheckpoint:()=>Ta,Precision:()=>hh,PredictionLoop:()=>Jc,ProgressCallback:()=>wa,Recall:()=>mh,SingleDeviceStrategy:()=>Gc,Stage:()=>_a,SumMetric:()=>ph,Timer:()=>ah,Trainer:()=>sh,TrainerState:()=>Vc,TrainingLoop:()=>Wc,ValidationLoop:()=>Kc,applyCheckpoint:()=>Sk,deserializeCheckpoint:()=>Sw,loadCheckpoint:()=>xk,serializeCheckpoint:()=>rh});var th=class extends ge{static{l(this,"LightningModule")}_trainer;_logBuffer;_automaticOptimization;_currentOptimizers;_device;constructor(){super(),this._trainer=null,this._logBuffer=new Map,this._automaticOptimization=!0,this._currentOptimizers=[],this._device=null}get trainer(){return this._trainer}get currentEpoch(){return this._trainer?this._trainer.state.epoch:0}get globalStep(){return this._trainer?this._trainer.state.globalStep:0}get device(){return this._device}get logger(){return this._trainer?this._trainer.logger:null}get loggers(){return this._trainer?this._trainer.loggers:[]}get automaticOptimization(){return this._automaticOptimization}set automaticOptimization(e){this._automaticOptimization=e}get optimizers(){return this._currentOptimizers}trainingStep(e,t){throw new Error(`${this.constructor.name}.trainingStep() not implemented`)}validationStep(e,t){}testStep(e,t){}predictStep(e,t){return this.forward(Array.isArray(e)&&e.length===1?e[0]:e)}configureOptimizers(){throw new Error(`${this.constructor.name}.configureOptimizers() not implemented`)}onTrainEpochStart(){}onTrainEpochEnd(){}onValidationEpochStart(){}onValidationEpochEnd(){}onTestEpochStart(){}onTestEpochEnd(){}log(e,t,{onStep:r=null,onEpoch:s=null,reduceFx:o="mean",progBar:i=!1}={}){let a=this._trainer?this._trainer.state.stage:"training",u=a==="training",p=a!=="training";this._logBuffer.set(e,{value:t,onStep:r!==null?r:u,onEpoch:s!==null?s:p,reduceFx:o,progBar:i})}logDict(e,t={}){for(let r of Object.keys(e))this.log(r,e[r],t)}manualBackward(e){this._trainer&&this._trainer.strategy?this._trainer.strategy.backward(e):e.backward()}};function AC(n){if(!n)throw new Error("configureOptimizers() returned null/undefined");if(Array.isArray(n)){let t=[],r=[];for(let s=0;s<n.length;s++){let o=n[s];nU(o)?(t.push(o.optimizer),r.push(NC(o.lrScheduler))):(t.push(o),r.push(null))}return{optimizers:t,schedulerConfigs:r}}let e=n;if(e.optimizer)return{optimizers:[e.optimizer],schedulerConfigs:[NC(e.lrScheduler)]};if(tU(n))return{optimizers:[n],schedulerConfigs:[null]};throw new Error("configureOptimizers() returned an unrecognized format")}l(AC,"parseOptimizersConfig");function NC(n){return n?"scheduler"in n&&n.scheduler?{scheduler:n.scheduler,interval:n.interval||"epoch",frequency:n.frequency||1,monitor:n.monitor||null}:OC(n)?{scheduler:n,interval:"epoch",frequency:1,monitor:null}:null:null}l(NC,"normalizeSchedulerConfig");function OC(n){return typeof n=="object"&&n!==null&&typeof n.step=="function"}l(OC,"isSchedulerLike");function tU(n){return OC(n)}l(tU,"isOptimizerLike");function nU(n){return typeof n=="object"&&n!==null&&"optimizer"in n}l(nU,"isOptimizerConfig");var _a=(o=>(o.IDLE="idle",o.TRAINING="training",o.VALIDATING="validating",o.TESTING="testing",o.PREDICTING="predicting",o))(_a||{}),rU={mean:l(n=>n.count===0?0:n.sum/n.count,"mean"),sum:l(n=>n.sum,"sum"),min:l(n=>n.min,"min"),max:l(n=>n.max,"max"),last:l(n=>n.last,"last")};function sU(){return{sum:0,count:0,min:1/0,max:-1/0,last:0}}l(sU,"createAccumulator");function oU(n,e){let t=typeof e=="number"?e:e.item();n.sum+=t,n.count+=1,t<n.min&&(n.min=t),t>n.max&&(n.max=t),n.last=t}l(oU,"updateAccumulator");var zc=class{static{l(this,"MetricAccumulator")}_accumulators;_reduceFns;constructor(){this._accumulators=new Map,this._reduceFns=new Map}update(e,t,r="mean"){this._accumulators.has(e)||(this._accumulators.set(e,sU()),this._reduceFns.set(e,r)),oU(this._accumulators.get(e),t)}compute(e){let t=this._accumulators.get(e);if(!t)return;let r=rU[this._reduceFns.get(e)];return r(t)}computeAll(){let e={};for(let[t]of this._accumulators)e[t]=this.compute(t);return e}reset(){this._accumulators.clear(),this._reduceFns.clear()}has(e){return this._accumulators.has(e)}get size(){return this._accumulators.size}},Vc=class{static{l(this,"TrainerState")}stage;epoch;globalStep;maxEpochs;maxSteps;shouldStop;stepMetrics;epochMetrics;numTrainingBatches;numValBatches;_progBarMetrics;constructor(){this.stage="idle",this.epoch=0,this.globalStep=0,this.maxEpochs=0,this.maxSteps=-1,this.shouldStop=!1,this.stepMetrics=new zc,this.epochMetrics=new zc}resetEpochMetrics(){this.epochMetrics.reset()}resetStepMetrics(){this.stepMetrics.reset()}},Gc=class{static{l(this,"SingleDeviceStrategy")}device;constructor(){this.device=null}setup(e,t){this.device=t,(t.type==="gpu"||t.type==="webgpu")&&iU(e,"to")&&e.to(t)}toDevice(e){if(!this.device||this.device.type!=="gpu"&&this.device.type!=="webgpu")return e;if(aU(e))return e.to(this.device);if(Array.isArray(e)){let t=new Array(e.length);for(let r=0;r<e.length;r++)t[r]=this.toDevice(e[r]);return t}return e}backward(e){e.backward()}optimizerStep(e){e.step()}};function iU(n,e){return typeof n=="object"&&n!==null&&typeof n[e]=="function"}l(iU,"hasMethod");function aU(n){return typeof n=="object"&&n!==null&&"device"in n&&typeof n.to=="function"}l(aU,"hasDeviceTo");var yk=(E=>(E.ON_FIT_START="onFitStart",E.ON_FIT_END="onFitEnd",E.ON_TRAIN_START="onTrainStart",E.ON_TRAIN_END="onTrainEnd",E.ON_TRAIN_EPOCH_START="onTrainEpochStart",E.ON_TRAIN_EPOCH_END="onTrainEpochEnd",E.ON_TRAIN_BATCH_START="onTrainBatchStart",E.ON_TRAIN_BATCH_END="onTrainBatchEnd",E.ON_VALIDATION_START="onValidationStart",E.ON_VALIDATION_END="onValidationEnd",E.ON_VALIDATION_EPOCH_START="onValidationEpochStart",E.ON_VALIDATION_EPOCH_END="onValidationEpochEnd",E.ON_VALIDATION_BATCH_START="onValidationBatchStart",E.ON_VALIDATION_BATCH_END="onValidationBatchEnd",E.ON_TEST_START="onTestStart",E.ON_TEST_END="onTestEnd",E.ON_TEST_BATCH_START="onTestBatchStart",E.ON_TEST_BATCH_END="onTestBatchEnd",E.ON_PREDICT_START="onPredictStart",E.ON_PREDICT_END="onPredictEnd",E.ON_PREDICT_BATCH_START="onPredictBatchStart",E.ON_PREDICT_BATCH_END="onPredictBatchEnd",E.SETUP="setup",E.TEARDOWN="teardown",E.ON_BEFORE_BACKWARD="onBeforeBackward",E.ON_AFTER_BACKWARD="onAfterBackward",E.ON_BEFORE_OPTIMIZER_STEP="onBeforeOptimizerStep",E.ON_BEFORE_ZERO_GRAD="onBeforeZeroGrad",E.ON_SAVE_CHECKPOINT="onSaveCheckpoint",E.ON_LOAD_CHECKPOINT="onLoadCheckpoint",E))(yk||{}),jc=class{static{l(this,"CallbackConnector")}_callbacks;constructor(e=[]){this._callbacks=e}get callbacks(){return this._callbacks}add(e){this._callbacks.push(e)}dispatch(e,...t){for(let r=0;r<this._callbacks.length;r++){let s=this._callbacks[r][e];typeof s=="function"&&s.apply(this._callbacks[r],t)}}remove(e){let t=this._callbacks.indexOf(e);t!==-1&&this._callbacks.splice(t,1)}},Uc=class{static{l(this,"LoggerConnector")}_loggers;_state;constructor(e=[],t){this._loggers=Array.isArray(e)?e:[e],this._state=t}drain(e){let t=e._logBuffer;if(t.size!==0){for(let[r,s]of t){let{value:o,onStep:i,onEpoch:a,reduceFx:u,progBar:p}=s;if(a&&this._state.epochMetrics.update(r,o,u),i&&this._state.stepMetrics.update(r,o,u),p){this._state._progBarMetrics||(this._state._progBarMetrics=new Map);let c=typeof o=="number"?o:o.item();this._state._progBarMetrics.set(r,c)}}t.clear()}}flushStepMetrics(e){let t=this._state.stepMetrics.computeAll();if(Object.keys(t).length===0)return t;for(let r=0;r<this._loggers.length;r++)this._loggers[r].logMetrics(t,e);return this._state.stepMetrics.reset(),t}flushEpochMetrics(e){let t=this._state.epochMetrics.computeAll();if(Object.keys(t).length===0)return t;for(let r=0;r<this._loggers.length;r++)this._loggers[r].logMetrics(t,e);return this._state.epochMetrics.reset(),t}logHyperparams(e){for(let t=0;t<this._loggers.length;t++)this._loggers[t].logHyperparams(e)}};be();fi();ou();function ba(n,e){return n==null?e:typeof n=="number"?n>0&&n<=1?Math.max(1,Math.round(n*e)):Math.min(n,e):e}l(ba,"resolveLimit");async function qc(n){let{GradMode:e}=await Promise.resolve().then(()=>(ou(),HB)),t=e.isEnabled();e.setEnabled(!1);try{await n()}finally{e.setEnabled(t)}}l(qc,"noGradAsync");var Wc=class{static{l(this,"TrainingLoop")}async run(e,t,r,s,o){let i=r.state,a=r.callbackConnector,u=r.loggerConnector,p=r.strategy,c=r.accumulateGradBatches,f=ba(r.limitTrainBatches,t.length);i.numTrainingBatches=f,i.stage="training",e.train(),Oo.setEnabled(!0),e.onTrainEpochStart(),a.dispatch("onTrainEpochStart",r,e);let d=0;for(let m of t){if(d>=f||i.shouldStop)break;if(i.maxSteps>0&&i.globalStep>=i.maxSteps){i.shouldStop=!0;break}let g=p.toDevice(m);a.dispatch("onTrainBatchStart",r,e,g,d);let y;e.automaticOptimization?y=await this._automaticStep(e,g,d,r,s,o,p,c,a):y=await Promise.resolve(e.trainingStep(g,d)),u.drain(e),i.globalStep%r.logEveryNSteps===0&&u.flushStepMetrics(i.globalStep),a.dispatch("onTrainBatchEnd",r,e,y,g,d),iT(),i.globalStep++,d++}iT();let h=u.flushEpochMetrics(i.globalStep);return this._stepEpochSchedulers(o,i.epoch),e.onTrainEpochEnd(),a.dispatch("onTrainEpochEnd",r,e),h}async _automaticStep(e,t,r,s,o,i,a,u,p){if(s.compile)return this._compiledStep(e,t,r,s,o,i,u);if(s.cudaGraph)return this._graphedStep(e,t,s,o,i,a);let c=await Promise.resolve(e.trainingStep(t,r)),f=c,d=c;if(BC(c)&&(f=c.loss,d=c),u>1&&(f=Kt(f,u)),p.dispatch("onBeforeBackward",s,e,f),a.backward(f),p.dispatch("onAfterBackward",s,e),(r+1)%u===0){for(let m=0;m<o.length;m++)this._clipGradients(e,s),p.dispatch("onBeforeOptimizerStep",s,e,o[m]),a.optimizerStep(o[m]),p.dispatch("onBeforeZeroGrad",s,e,o[m]),o[m].zeroGrad();this._stepStepSchedulers(i,s.state.globalStep)}return d}async _compiledStep(e,t,r,s,o,i,a){let u=Array.isArray(t)?t:[t],p=l((..._)=>e.trainingStep(Array.isArray(t)?_:_[0],0),"callForward"),c;if(e.__compiledTrainStep)c=e.__compiledTrainStep(...u),_k(c)&&(c=await c);else{let{compileWithBackward:_}=await Promise.resolve().then(()=>(bw(),SC)),{CPUTarget:b,CUDATarget:w,WebGPUTarget:x}=await Promise.resolve().then(()=>(yo(),t1)),S=e._device&&e._device.type,T=S==="webgpu"?x():S==="gpu"?w():b();e.__compiledTrainStep=_({forward:p},u,{target:T,mode:s.compileMode});let v=e.log;e.log=()=>{};try{c=e.__compiledTrainStep(...u),_k(c)&&(c=await c)}finally{e.log=v}}let f=e.__compiledTrainStep,d=f.capturedParams(),{ones:h}=await Promise.resolve().then(()=>(kt(),EB)),m=f.backward(h(c.shape));_k(m)&&(m=await m);let g=m,y=g.length-d.length;for(let _=0;_<d.length;_++){let b=g[y+_];b&&(d[_].grad=b)}if((r+1)%a===0){for(let _=0;_<o.length;_++)this._clipGradients(e,s),o[_].step(),o[_].zeroGrad();this._stepStepSchedulers(i,s.state.globalStep)}return e.log&&e.log("train_loss",c),c}async _eagerTrainStepCore(e,t,r,s,o){let i=await Promise.resolve(e.trainingStep(t,0)),a=i;if(BC(i)&&(a=i.loss),s.backward(a),o&&o.gradientClipVal){let{deviceClipGradNorm:u}=await Promise.resolve().then(()=>(iy(),oy));u([...e.parameters()],o.gradientClipVal)}for(let u=0;u<r.length;u++)s.optimizerStep(r[u]),r[u].zeroGrad();return a}async _graphedStep(e,t,r,s,o,i){let a=await Promise.resolve().then(()=>(ug(),gO)),u=await Promise.resolve().then(()=>(qp(),EO)),p=await Promise.resolve().then(()=>(pi(),iO)),c=e.__eagerGraphRunner;if(c||(c=e.__eagerGraphRunner={phase:"warmup",seen:0},u.setEagerDeferred(!0),Nl(!0)),c.phase==="disabled")return this._eagerTrainStepCore(e,t,s,i,r);if(c.phase==="warmup"&&c.seen<r.cudaGraphWarmupSteps)return c.seen++,this._eagerTrainStepCore(e,t,s,i,r);let f=bk(t);if(c.phase==="warmup"){c.inputs=f.map(y=>{let _=y._impl.storage.rawData;return u.deviceBufferForInput(_),u.pinResident(_),{dptr:u.deviceBufferDptr(_)}});let h=e.log,m;try{a.beginEagerCapture(),e.log=()=>{};try{m=await this._eagerTrainStepCore(e,t,s,i,r)}finally{e.log=h}c.captured=a.endEagerCapture()}catch(y){try{a.endEagerCapture()}catch{}return e.log=h,u.clearCapturePins(),c.phase="disabled",c.captureError=y&&typeof y=="object"&&"message"in y?y.message:y,this._eagerTrainStepCore(e,t,s,i,r)}c.exec=c.captured.exec,c.lossDptr=u.deviceBufferDptr(m._impl.storage.rawData),c.lossScratch=new Float32Array(1),c.phase="replay",a.replay(c.exec),a.syncStream(),p.copyDeviceToHost(c.lossScratch,c.lossDptr);let g=c.lossScratch[0];return this._logGraphLoss(r,g),this._stepStepSchedulers(o,r.state.globalStep),g}for(let h=0;h<c.inputs.length&&h<f.length;h++)c.inputs[h].dptr&&p.copyHostToDeviceAsync(c.inputs[h].dptr,f[h]._impl.storage.rawData);a.replay(c.exec),a.syncStream(),p.copyDeviceToHost(c.lossScratch,c.lossDptr);let d=c.lossScratch[0];return this._logGraphLoss(r,d),this._stepStepSchedulers(o,r.state.globalStep),d}_logGraphLoss(e,t){e.state.stepMetrics.update("train_loss",t),e.state.epochMetrics.update("train_loss",t),e.state._progBarMetrics||(e.state._progBarMetrics=new Map),e.state._progBarMetrics.set("train_loss",t)}_clipGradients(e,t){if(!t.gradientClipVal)return;let r=[...e.parameters()];t.gradientClipAlgorithm==="norm"?vd(r,t.gradientClipVal):kd(r,t.gradientClipVal)}_stepStepSchedulers(e,t){if(e)for(let r=0;r<e.length;r++){let s=e[r];!s||s.interval!=="step"||s.monitor||(t+1)%s.frequency===0&&s.scheduler.step()}}_stepEpochSchedulers(e,t){if(e)for(let r=0;r<e.length;r++){let s=e[r];!s||s.interval!=="epoch"||s.monitor||(t+1)%s.frequency===0&&s.scheduler.step()}}};function bk(n,e=[]){if(n==null)return e;if(uU(n))return e.push(n),e;if(Array.isArray(n)){for(let t of n)bk(t,e);return e}if(typeof n=="object"){let t=n;for(let r of Object.keys(t))bk(t[r],e);return e}return e}l(bk,"_flattenTensors");function _k(n){return typeof n=="object"&&n!==null&&typeof n.then=="function"}l(_k,"isThenable");function BC(n){return typeof n=="object"&&n!==null&&!("backward"in n)&&"loss"in n}l(BC,"isLossObject");function uU(n){return typeof n=="object"&&n!==null&&"shape"in n&&typeof n.contiguous=="function"}l(uU,"isFlattenTensor");var Kc=class{static{l(this,"ValidationLoop")}async run(e,t,r,s){let o=r.state,i=r.callbackConnector,a=r.loggerConnector,u=o.stage;o.stage="validating",e.eval(),e.onValidationEpochStart(),i.dispatch("onValidationStart",r,e),i.dispatch("onValidationEpochStart",r,e);let p=ba(r.limitValBatches,t.length);o.numValBatches=p;let c=0;await qc(async()=>{for(let d of t){if(c>=p)break;i.dispatch("onValidationBatchStart",r,e,d,c);let h=await Promise.resolve(e.validationStep(d,c));a.drain(e),i.dispatch("onValidationBatchEnd",r,e,h,d,c),c++}});let f=a.flushEpochMetrics(o.globalStep);return this._stepPlateauSchedulers(s,f),e.onValidationEpochEnd(),i.dispatch("onValidationEpochEnd",r,e),i.dispatch("onValidationEnd",r,e),e.train(),o.stage=u,f}_stepPlateauSchedulers(e,t){if(e)for(let r=0;r<e.length;r++){let s=e[r];if(!s||!s.monitor)continue;let o=s.scheduler;if(typeof o.step=="function"&&o.step.length>0){let i=t[s.monitor];i!==void 0&&o.step(i)}}}};var Hc=class{static{l(this,"FitLoop")}_trainingLoop;_validationLoop;constructor(){this._trainingLoop=new Wc,this._validationLoop=new Kc}get trainingLoop(){return this._trainingLoop}get validationLoop(){return this._validationLoop}async run(e,t,r,s,o,i){let a=s.state,u=s.callbackConnector;u.dispatch("onTrainStart",s,e);for(let p=0;p<a.maxEpochs&&!(a.shouldStop||a.maxSteps>0&&a.globalStep>=a.maxSteps||(a.epoch=p,a.resetEpochMetrics(),await this._trainingLoop.run(e,t,s,o,i),a.shouldStop));p++)r&&this._shouldRunValidation(p,s)&&await this._validationLoop.run(e,r,s,i);u.dispatch("onTrainEnd",s,e)}_shouldRunValidation(e,t){let r=t.checkValEveryNEpoch;return(e+1)%r===0}};var Yc=class{static{l(this,"EvaluationLoop")}async run(e,t,r){let s=r.state,o=r.callbackConnector,i=r.loggerConnector,a=s.stage;s.stage="testing",e.eval(),e.onTestEpochStart(),o.dispatch("onTestStart",r,e),o.dispatch("onTestEpochStart",r,e);let u=ba(r.limitTestBatches,t.length),p=0;await qc(async()=>{for(let f of t){if(p>=u)break;o.dispatch("onTestBatchStart",r,e,f,p);let d=await Promise.resolve(e.testStep(f,p));i.drain(e),o.dispatch("onTestBatchEnd",r,e,d,f,p),p++}});let c=i.flushEpochMetrics(s.globalStep);return e.onTestEpochEnd(),o.dispatch("onTestEpochEnd",r,e),o.dispatch("onTestEnd",r,e),e.train(),s.stage=a,c}};var Jc=class{static{l(this,"PredictionLoop")}async run(e,t,r){let s=r.state,o=r.callbackConnector,i=r.strategy,a=s.stage;s.stage="predicting",e.eval(),o.dispatch("onPredictStart",r,e);let u=[],p=ba(r.limitTestBatches,t.length),c=0;return await qc(async()=>{for(let f of t){if(c>=p)break;let d=i.toDevice(f);o.dispatch("onPredictBatchStart",r,e,d,c);let h=await Promise.resolve(e.predictStep(d,c));await r._flushEagerInference(),u.push(h),o.dispatch("onPredictBatchEnd",r,e,h,d,c),c++}}),o.dispatch("onPredictEnd",r,e),e.train(),s.stage=a,u}};var ki=class{static{l(this,"Logger")}_name;_version;constructor({name:e="default",version:t=0}={}){this._name=e,this._version=t}get name(){return this._name}get version(){return this._version}logMetrics(e,t){throw new Error(`${this.constructor.name}.logMetrics() not implemented`)}logHyperparams(e){throw new Error(`${this.constructor.name}.logHyperparams() not implemented`)}finalize(){}};var Du=class extends ki{static{l(this,"ConsoleLogger")}_logFrequency;_callCount;constructor(e={}){super(e),this._logFrequency=e.logFrequency||1,this._callCount=0}logMetrics(e,t){if(this._callCount++,this._callCount%this._logFrequency!==0)return;let r=[`[step ${t}]`],s=Object.keys(e).sort();for(let o=0;o<s.length;o++){let i=e[s[o]];r.push(`${s[o]}: ${lU(i)}`)}console.log(r.join(" | "))}logHyperparams(e){let t=["[hyperparams]"],r=Object.keys(e).sort();for(let s=0;s<r.length;s++)t.push(`${r[s]}: ${e[r[s]]}`);console.log(t.join(" | "))}};function lU(n){return typeof n!="number"||Number.isInteger(n)?String(n):Math.abs(n)<.001&&n!==0?n.toExponential(3):n.toFixed(4)}l(lU,"formatNumber");var Zn=class{static{l(this,"Callback")}setup(e,t,r){}teardown(e,t,r){}onFitStart(e,t){}onFitEnd(e,t){}onTrainStart(e,t){}onTrainEnd(e,t){}onTrainEpochStart(e,t){}onTrainEpochEnd(e,t){}onTrainBatchStart(e,t,r,s){}onTrainBatchEnd(e,t,r,s,o){}onValidationStart(e,t){}onValidationEnd(e,t){}onValidationEpochStart(e,t){}onValidationEpochEnd(e,t){}onValidationBatchStart(e,t,r,s){}onValidationBatchEnd(e,t,r,s,o){}onTestStart(e,t){}onTestEnd(e,t){}onTestBatchStart(e,t,r,s){}onTestBatchEnd(e,t,r,s,o){}onPredictStart(e,t){}onPredictEnd(e,t){}onPredictBatchStart(e,t,r,s){}onPredictBatchEnd(e,t,r,s,o){}onBeforeBackward(e,t,r){}onAfterBackward(e,t){}onBeforeOptimizerStep(e,t,r){}onBeforeZeroGrad(e,t,r){}onSaveCheckpoint(e,t,r){}onLoadCheckpoint(e,t,r){}};var cU=["\u258F","\u258E","\u258D","\u258C","\u258B","\u258A","\u2589"];function RC(n){globalThis.process.stdout.write(n)}l(RC,"stdoutWrite");var wa=class extends Zn{static{l(this,"ProgressCallback")}_barLength;_trainBatchCount;_valBatchCount;_epochStartTime;_lastLen;_active;constructor({barLength:e=24}={}){super(),this._barLength=e,this._trainBatchCount=0,this._valBatchCount=0,this._epochStartTime=0,this._lastLen=0,this._active=!1}onTrainEpochStart(e,t){this._trainBatchCount=0,this._epochStartTime=Date.now();let r=this._trainTotal(e);r&&this._render("Epoch",e.state.epoch+1,e.state.maxEpochs,0,r,e.state)}onTrainBatchEnd(e,t,r,s,o){this._trainBatchCount++;let i=this._trainTotal(e);i&&this._render("Epoch",e.state.epoch+1,e.state.maxEpochs,this._trainBatchCount,i,e.state)}onTrainEpochEnd(e,t){let r=this._trainTotal(e);r&&this._render("Epoch",e.state.epoch+1,e.state.maxEpochs,r,r,e.state)}onTrainEnd(e,t){this._active&&RC(`
|
|
425
|
+
`),this._active=!1,this._lastLen=0}onValidationEpochStart(e,t){this._valBatchCount=0,this._epochStartTime=Date.now()}onValidationBatchEnd(e,t,r,s,o){this._valBatchCount++;let i=this._valTotal(e);i&&this._render("Validation",null,null,this._valBatchCount,i,e.state)}onValidationEnd(e,t){let r=this._valTotal(e);r&&this._render("Validation",null,null,r,r,e.state)}_trainTotal(e){return e.state.numTrainingBatches??MC(e.limitTrainBatches)}_valTotal(e){return e.state.numValBatches??MC(e.limitValBatches)}_render(e,t,r,s,o,i){let a=o>0?Math.min(s/o,1):0,u=String(Math.round(a*100)).padStart(3," "),p=this._bar(a),c=t!==null?`${e} ${t}/${r}`:e,f=(Date.now()-this._epochStartTime)/1e3,d=f>0?s/f:0,h=d>0?(o-s)/d:0,m=`${IC(f)}<${IC(h)}, ${d.toFixed(2)}it/s`,g=this._formatProgBarMetrics(i),y=`${c}: ${u}%|${p}| ${s}/${o} [${m}${g}]`,_=Math.max(0,this._lastLen-y.length);RC("\r"+y+" ".repeat(_)),this._lastLen=y.length,this._active=!0}_bar(e){let t=this._barLength,r=e*t,s=Math.floor(r),o=Math.round((r-s)*8);if(o===8&&(s+=1,o=0),s>=t)return"\u2588".repeat(t);let i=o>0?cU[o-1]:"",a=t-s-(i?1:0);return"\u2588".repeat(s)+i+" ".repeat(a)}_formatProgBarMetrics(e){let t=e._progBarMetrics;if(!t||t.size===0)return"";let r=[];for(let[s,o]of t)r.push(`${s}=${pU(o)}`);return", "+r.join(", ")}};function MC(n){return n==null?null:typeof n=="number"&&n>1?n:null}l(MC,"resolveTotal");function IC(n){(!Number.isFinite(n)||n<0)&&(n=0);let e=Math.round(n),t=Math.floor(e/60),r=e%60;return`${String(t).padStart(2,"0")}:${String(r).padStart(2,"0")}`}l(IC,"fmtTime");function pU(n){return typeof n!="number"||Number.isInteger(n)?String(n):n.toFixed(4)}l(pU,"formatNum");function Ni(...n){return n.filter(e=>e!=null&&e!=="").join("/").replace(/\/+/g,"/")}l(Ni,"joinPath");sr();os();rs();var LC="mlfw-ckpt-v1",nh=8,fU={Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array};function dU(n){return ArrayBuffer.isView(n)&&!(n instanceof DataView)}l(dU,"isTypedArray");function wk(n){return new Uint8Array(n.buffer,n.byteOffset,n.byteLength)}l(wk,"byteView");function hU(n){return n.constructor.name}l(hU,"typedArrayName");function mU(n){return"$buf"in n&&typeof n.$buf=="number"}l(mU,"isBufferNode");function gU(n){return"$map"in n&&Array.isArray(n.$map)}l(gU,"isMapNode");function rh(n){let e=[],t=Tw(n,e),r=0;for(let u of e)u.meta.offset=r,u.meta.length=u.bytes.length,r+=u.bytes.length;let s={format:LC,tree:t,buffers:e.map(u=>u.meta)},o=new TextEncoder().encode(JSON.stringify(s)),i=new Uint8Array(nh+o.length+r);new DataView(i.buffer).setBigUint64(0,BigInt(o.length),!0),i.set(o,nh);let a=nh+o.length;for(let u of e)i.set(u.bytes,a),a+=u.bytes.length;return i}l(rh,"serializeCheckpoint");function Sw(n){let e=n instanceof Uint8Array?n:new Uint8Array(n),t=new DataView(e.buffer,e.byteOffset,e.byteLength),r=Number(t.getBigUint64(0,!0)),s=nh+r,o=JSON.parse(new TextDecoder().decode(e.subarray(nh,s)));if(o.format!==LC)throw new Error("mlfw: unrecognized checkpoint format: "+o.format);let i=o.buffers.map(a=>yU(a,e,s));return xw(o.tree,i)}l(Sw,"deserializeCheckpoint");function Tw(n,e){if(n instanceof Ze)return EC(e,{kind:"tensor",dtype:n.dtype,shape:n.shape},wk(n._impl.storage.data));if(dU(n))return EC(e,{kind:"array",arrayType:hU(n)},wk(n));if(n instanceof Map){let t=[];for(let[r,s]of n)t.push([r,Tw(s,e)]);return{$map:t}}if(Array.isArray(n))return n.map(t=>Tw(t,e));if(n&&typeof n=="object"){let t={},r=n;for(let s of Object.keys(r))t[s]=Tw(r[s],e);return t}return n}l(Tw,"encodeNode");function EC(n,e,t){let r=n.length;return n.push({meta:e,bytes:t}),{$buf:r}}l(EC,"pushBuffer");function yU(n,e,t){let r=t+n.offset,s=e.subarray(r,r+n.length),o=n.kind==="tensor"?St(n.dtype):fU[n.arrayType],i=n.kind==="tensor"?void 0:n.arrayType;if(!o)throw new Error("mlfw: unknown typed array in checkpoint: "+i);let a=new o(n.length/o.BYTES_PER_ELEMENT);return wk(a).set(s),n.kind==="tensor"?eu(a,n.shape,n.dtype):a}l(yU,"decodeBuffer");function xw(n,e){if(n===null||typeof n!="object")return n;if(Array.isArray(n))return n.map(r=>xw(r,e));if(mU(n))return e[n.$buf];if(gU(n)){let r=new Map;for(let[s,o]of n.$map)r.set(s,xw(o,e));return r}let t={};for(let r of Object.keys(n))t[r]=xw(n[r],e);return t}l(xw,"decodeNode");var Tk=".ckpt",Ta=class extends Zn{static{l(this,"ModelCheckpoint")}_dirpath;_filename;_monitor;_mode;_saveTopK;_saveLast;_everyNEpochs;_bestK;_recent;_compareFn;_bestModelPath;_lastModelPath;constructor({dirpath:e="./lightning_logs/checkpoints",filename:t="epoch={epoch}-step={step}",monitor:r=null,mode:s="min",saveTopK:o=1,saveLast:i=!0,everyNEpochs:a=1}={}){super(),this._dirpath=e,this._filename=t,this._monitor=r,this._mode=s,this._saveTopK=o,this._saveLast=i,this._everyNEpochs=a,this._bestK=[],this._recent=[],this._compareFn=s==="min"?(u,p)=>u-p:(u,p)=>p-u,this._bestModelPath=null,this._lastModelPath=null}get bestModelPath(){return this._bestModelPath}get lastModelPath(){return this._lastModelPath}get bestKModels(){return this._bestK}onTrainEpochEnd(e,t){let r=e.state;if((r.epoch+1)%this._everyNEpochs!==0)return;this._ensureDir();let s=this._fillTemplate(r);if(this._saveLast){let c=Ni(this._dirpath,"last"+Tk);this._saveCheckpoint(t,e,c),this._lastModelPath=c}if(!this._monitor){let c=Ni(this._dirpath,s+Tk);if(this._saveCheckpoint(t,e,c),this._saveTopK>=0)for(this._recent.push(c);this._recent.length>this._saveTopK;)this._tryDelete(this._recent.shift());return}let i=r.epochMetrics.computeAll()[this._monitor];if(i===void 0)return;let a=Ni(this._dirpath,s+Tk),u={score:i,path:a};if(this._saveTopK<0){this._saveCheckpoint(t,e,a),this._bestK.push(u),this._updateBest();return}let p=this._findInsertIndex(i);if(p<this._saveTopK){if(this._saveCheckpoint(t,e,a),this._bestK.splice(p,0,u),this._bestK.length>this._saveTopK){let c=this._bestK.pop();this._tryDelete(c.path)}this._updateBest()}}_findInsertIndex(e){let t=0,r=this._bestK.length;for(;t<r;){let s=t+r>>>1;this._compareFn(e,this._bestK[s].score)<0?r=s:t=s+1}return t}_updateBest(){this._bestK.length>0&&(this._bestModelPath=this._bestK[0].path)}_saveCheckpoint(e,t,r){let s={epoch:t.state.epoch,globalStep:t.state.globalStep,modelState:e.stateDict()},o=e._currentOptimizers;o&&o.length>0&&(s.optimizerStates=o.map(a=>a.stateDict())),t.callbackConnector.dispatch("onSaveCheckpoint",t,e,s);let i=r+".tmp";vn.writeBinary(i,rh(s)),vn.rename(i,r)}_fillTemplate(e){return this._filename.replace("{epoch}",String(e.epoch)).replace("{step}",String(e.globalStep))}_ensureDir(){vn.exists(this._dirpath)||vn.mkdir(this._dirpath)}_tryDelete(e){try{vn.remove(e)}catch{}}};function xk(n){return Sw(vn.readBinary(n))}l(xk,"loadCheckpoint");function Sk(n,e,t=[]){let r=n;if(r.modelState&&e.loadStateDict(r.modelState),r.optimizerStates){let s=Math.min(t.length,r.optimizerStates.length);for(let o=0;o<s;o++)t[o].loadStateDict(r.optimizerStates[o])}return n}l(Sk,"applyCheckpoint");qn();var sh=class{static{l(this,"Trainer")}_state;_compile;_compileMode;_cudaGraph;_cudaGraphWarmupSteps;_accelerator;_precision;_gradientClipVal;_gradientClipAlgorithm;_accumulateGradBatches;_limitTrainBatches;_limitValBatches;_limitTestBatches;_valCheckInterval;_checkValEveryNEpoch;_logEveryNSteps;_deterministic;_defaultRootDir;_loggers;_strategy;_fitLoop;_evaluationLoop;_predictionLoop;_callbackConnector;_loggerConnector;_model;_webgpuMod;constructor({maxEpochs:e=10,maxSteps:t=-1,accelerator:r="auto",precision:s="f32",callbacks:o=[],logger:i=!0,enableCheckpointing:a=!1,enableProgress:u=!0,gradientClipVal:p=null,gradientClipAlgorithm:c="norm",accumulateGradBatches:f=1,limitTrainBatches:d=null,limitValBatches:h=null,limitTestBatches:m=null,valCheckInterval:g=1,checkValEveryNEpoch:y=1,logEveryNSteps:_=50,deterministic:b=!1,fastDevRun:w=!1,defaultRootDir:x="./lightning_logs",compile:S=!1,compileMode:T="separate",cudaGraph:v=!1,cudaGraphWarmupSteps:A=3}={}){if(this._state=new Vc,this._state.maxEpochs=e,this._state.maxSteps=t,this._compile=S,this._compileMode=T,this._cudaGraph=v,this._cudaGraphWarmupSteps=A,this._accelerator=r,this._precision=s,this._gradientClipVal=p,this._gradientClipAlgorithm=c,this._accumulateGradBatches=f,this._limitTrainBatches=d,this._limitValBatches=h,this._limitTestBatches=m,this._valCheckInterval=g,this._checkValEveryNEpoch=y,this._logEveryNSteps=_,this._deterministic=b,this._defaultRootDir=x,w!==!1){let k=typeof w=="number"?w:1;this._limitTrainBatches=k,this._limitValBatches=k,this._limitTestBatches=k,this._state.maxEpochs=1}this._loggers=this._resolveLoggers(i),this._strategy=new Gc,this._fitLoop=new Hc,this._evaluationLoop=new Yc,this._predictionLoop=new Jc;let N=[...o];u&&!N.some(k=>k instanceof wa)&&N.push(new wa),a&&!N.some(k=>k instanceof Ta)&&N.push(new Ta({dirpath:x})),this._callbackConnector=new jc(N),this._loggerConnector=new Uc(this._loggers,this._state),this._model=null,this._webgpuMod=null}get state(){return this._state}get strategy(){return this._strategy}get callbackConnector(){return this._callbackConnector}get loggerConnector(){return this._loggerConnector}get fitLoop(){return this._fitLoop}get gradientClipVal(){return this._gradientClipVal}get gradientClipAlgorithm(){return this._gradientClipAlgorithm}get compile(){return this._compile}get compileMode(){return this._compileMode}get cudaGraph(){return this._cudaGraph}get cudaGraphWarmupSteps(){return this._cudaGraphWarmupSteps}get accumulateGradBatches(){return this._accumulateGradBatches}set accumulateGradBatches(e){this._accumulateGradBatches=e}get limitTrainBatches(){return this._limitTrainBatches}get limitValBatches(){return this._limitValBatches}get limitTestBatches(){return this._limitTestBatches}get checkValEveryNEpoch(){return this._checkValEveryNEpoch}get logEveryNSteps(){return this._logEveryNSteps}get shouldStop(){return this._state.shouldStop}set shouldStop(e){this._state.shouldStop=e}get currentEpoch(){return this._state.epoch}get globalStep(){return this._state.globalStep}get logger(){return this._loggers[0]||null}get loggers(){return this._loggers}get callbacks(){return this._callbackConnector.callbacks}get model(){return this._model}get defaultRootDir(){return this._defaultRootDir}async fit(e,t,r=null){this._model=e,e._trainer=this;let s=this._resolveDevice();this._guardEagerWebGPU(s,"fit",r!=null),this._guardCudaGraph(s,r!=null),e._device=s,await this._prepareDevice(s),this._strategy.setup(e,s);let{optimizers:o,schedulerConfigs:i}=AC(await Promise.resolve(e.configureOptimizers()));if(this._cudaGraph&&i&&i.some(a=>a&&a.scheduler))throw new Error("Trainer(cudaGraph=true) v1 requires a constant learning rate: LR schedulers change lr, but lr is baked into the captured graph. Remove the scheduler or disable cudaGraph.");if(e._currentOptimizers=o,this._loggerConnector.logHyperparams(this._extractHyperparams(e,o)),this._callbackConnector.dispatch("setup",this,e,"training"),this._callbackConnector.dispatch("onFitStart",this,e),this._state.shouldStop=!1,await this._fitLoop.run(e,t,r,this,o,i),s===bg){let{teardownAfterFit:a}=await Promise.resolve().then(()=>(iy(),oy));a(e,o)}this._callbackConnector.dispatch("onFitEnd",this,e),this._callbackConnector.dispatch("teardown",this,e,"training");for(let a=0;a<this._loggers.length;a++)this._loggers[a].finalize()}async validate(e,t){e._trainer=this,this._model=e;let r=this._resolveDevice();this._guardEagerWebGPU(r,"validate"),e._device=r,await this._prepareDevice(r),this._strategy.setup(e,r),this._callbackConnector.dispatch("setup",this,e,"validating");let s=await this._fitLoop.validationLoop.run(e,t,this,null);this._callbackConnector.dispatch("teardown",this,e,"validating");for(let o=0;o<this._loggers.length;o++)this._loggers[o].finalize();return s}async test(e,t){e._trainer=this,this._model=e;let r=this._resolveDevice();this._guardEagerWebGPU(r,"test"),e._device=r,await this._prepareDevice(r),this._strategy.setup(e,r),this._callbackConnector.dispatch("setup",this,e,"testing");let s=await this._evaluationLoop.run(e,t,this);this._callbackConnector.dispatch("teardown",this,e,"testing");for(let o=0;o<this._loggers.length;o++)this._loggers[o].finalize();return s}async predict(e,t){e._trainer=this,this._model=e;let r=this._resolveDevice();return e._device=r,await this._prepareDevice(r),this._strategy.setup(e,r),await this._predictionLoop.run(e,t,this)}_resolveDevice(){return this._accelerator==="gpu"?bg:this._accelerator==="wasm"?RT:this._accelerator==="webgpu"?xo:this._accelerator==="cpu"?Ws:Ws}_guardEagerWebGPU(e,t,r=!1){if(e.type==="webgpu"){if(t==="fit"){if(!this._compile)throw new Error('Trainer(accelerator="webgpu"): eager WebGPU is inference-only (CUSTOM_0 dispatch has no autograd key). Pass compile=true to train on WebGPU, or use predict() for eager inference.');if(r)throw new Error(`Trainer(accelerator="webgpu"): in-fit validation is unsupported \u2014 validationStep runs eagerly and reads scalar metrics via .item(), which WebGPU's asynchronous readback cannot serve, and there is no compiled validation path. Call fit() without a valLoader on WebGPU.`);return}throw new Error(`Trainer(accelerator="webgpu"): ${t}() reads scalar metrics synchronously via .item(), which WebGPU's asynchronous readback cannot serve eagerly. Use predict() for eager WebGPU inference, or train via compile=true.`)}}_guardCudaGraph(e,t=!1){if(this._cudaGraph){if(e.type!=="gpu")throw new Error('Trainer(cudaGraph=true) requires accelerator="gpu" (eager CUDA whole-step capture/replay).');if(this._compile)throw new Error("Trainer(cudaGraph=true) is incompatible with compile=true: CUDA graph capture targets the eager training step, not the compiled path.");if(this._gradientClipVal!=null&&this._gradientClipAlgorithm!=="norm")throw new Error('Trainer(cudaGraph=true) supports gradient_clip_algorithm="norm" only; "value" clipping is not yet device-side.');if(this._accumulateGradBatches!==1)throw new Error("Trainer(cudaGraph=true) v1 requires accumulateGradBatches=1.");if(t)throw new Error("Trainer(cudaGraph=true) v1 does not support in-fit validation. Call fit() without a valLoader.")}}async _prepareDevice(e){if(e.type==="gpu"){let{preloadCudaRuntime:t}=await Promise.resolve().then(()=>(_f(),vx));await t()}else if(e.type==="webgpu"){let{preloadWebGPU:t}=await Promise.resolve().then(()=>(_f(),vx));this._webgpuMod=await t()}}async _flushEagerInference(){this._webgpuMod&&await this._webgpuMod.flushWebGPUEager()}_resolveLoggers(e){return e===!1||e===null?[]:e===!0?[new Du]:Array.isArray(e)?e:[e]}_extractHyperparams(e,t){let r={maxEpochs:this._state.maxEpochs,maxSteps:this._state.maxSteps,accelerator:this._accelerator,precision:this._precision,accumulateGradBatches:this._accumulateGradBatches};for(let s=0;s<t.length;s++){let o=t[s],i=o.defaults||{},a=t.length>1?`optimizer_${s}_`:"";r[a+"optimizer"]=o.constructor.name,i.lr!==void 0&&(r[a+"lr"]=i.lr),i.weightDecay!==void 0&&(r[a+"weight_decay"]=i.weightDecay)}return r}};var oh=class extends Zn{static{l(this,"EarlyStopping")}_monitor;_patience;_mode;_minDelta;_checkOnTrainEpochEnd;_waitCount;_bestScore;_compareFn;constructor({monitor:e="val_loss",patience:t=3,mode:r="min",minDelta:s=0,checkOnTrainEpochEnd:o=!1}={}){super(),this._monitor=e,this._patience=t,this._mode=r,this._minDelta=s,this._checkOnTrainEpochEnd=o,this._waitCount=0,this._bestScore=null,this._compareFn=r==="min"?(i,a)=>i<a-s:(i,a)=>i>a+s}get monitor(){return this._monitor}get patience(){return this._patience}get bestScore(){return this._bestScore}get waitCount(){return this._waitCount}onValidationEnd(e,t){this._checkOnTrainEpochEnd||this._check(e)}onTrainEpochEnd(e,t){this._checkOnTrainEpochEnd&&this._check(e)}_check(e){let r=e.state.epochMetrics.computeAll()[this._monitor];if(r!==void 0){if(this._bestScore===null||this._compareFn(r,this._bestScore)){this._bestScore=r,this._waitCount=0;return}this._waitCount++,this._waitCount>=this._patience&&(e.shouldStop=!0)}}reset(){this._waitCount=0,this._bestScore=null}};var ih=class extends Zn{static{l(this,"LearningRateMonitor")}_logMomentum;_lrHistory;constructor({logMomentum:e=!1}={}){super(),this._logMomentum=e,this._lrHistory={}}get lrHistory(){return this._lrHistory}onTrainBatchStart(e,t,r,s){let o=t._currentOptimizers;if(o)for(let i=0;i<o.length;i++){let a=o[i].paramGroups;for(let u=0;u<a.length;u++){let p=o.length>1||a.length>1?`lr_opt${i}_group${u}`:"lr",c=a[u].lr;if(t.log(p,c,{onStep:!0,onEpoch:!1,progBar:!1}),this._lrHistory[p]||(this._lrHistory[p]=[]),this._lrHistory[p].push({step:e.state.globalStep,lr:c}),this._logMomentum&&a[u].momentum!==void 0){let f=p.replace("lr","momentum");t.log(f,a[u].momentum,{onStep:!0,onEpoch:!1})}}}}};var ah=class extends Zn{static{l(this,"Timer")}_fitStartTime;_epochStartTime;_epochDurations;_validationDurations;_totalTrainingTime;_valStartTime;constructor(){super(),this._fitStartTime=0,this._epochStartTime=0,this._epochDurations=[],this._validationDurations=[],this._totalTrainingTime=0,this._valStartTime=0}get epochDurations(){return this._epochDurations}get validationDurations(){return this._validationDurations}get totalTrainingTime(){return this._totalTrainingTime}onFitStart(e,t){this._fitStartTime=performance.now()}onFitEnd(e,t){this._totalTrainingTime=(performance.now()-this._fitStartTime)/1e3}onTrainEpochStart(e,t){this._epochStartTime=performance.now()}onTrainEpochEnd(e,t){this._epochDurations.push((performance.now()-this._epochStartTime)/1e3)}onValidationStart(e,t){this._valStartTime=performance.now()}onValidationEnd(e,t){this._validationDurations.push((performance.now()-this._valStartTime)/1e3)}};var uh=class extends Zn{static{l(this,"GradientAccumulationScheduler")}_scheduling;_sortedEpochs;constructor({scheduling:e}){super(),this._scheduling=new Map;let t=Object.entries(e);for(let r=0;r<t.length;r++)this._scheduling.set(Number(t[r][0]),t[r][1]);this._sortedEpochs=[...this._scheduling.keys()].sort((r,s)=>r-s)}onTrainEpochStart(e,t){let r=e.state.epoch;this._scheduling.has(r)&&(e.accumulateGradBatches=this._scheduling.get(r))}getCurrentAccumulation(e){let t=1;for(let r=0;r<this._sortedEpochs.length&&this._sortedEpochs[r]<=e;r++)t=this._scheduling.get(this._sortedEpochs[r]);return t}};var lh=class extends ki{static{l(this,"CSVLogger")}_saveDir;_flushInterval;_columns;_columnSet;_buffer;_filePath;_headerWritten;_version;constructor({saveDir:e="./lightning_logs",name:t="default",version:r=null,flushInterval:s=10}={}){super({name:t,version:r||0}),this._saveDir=e,this._flushInterval=s,this._columns=[],this._columnSet=new Set,this._buffer=[],this._filePath=null,this._headerWritten=!1,this._version=r}get logDir(){let e=this._version!==null?this._version:this._resolveVersion();return Ni(this._saveDir,this._name,`version_${e}`)}logMetrics(e,t){let r={step:t},s=Object.keys(e);for(let o=0;o<s.length;o++){let i=s[o];r[i]=e[i],this._columnSet.has(i)||(this._columnSet.add(i),this._columns.push(i),this._headerWritten=!1)}this._buffer.push(r),this._buffer.length>=this._flushInterval&&this._flush()}logHyperparams(e){this._ensureDir();let t=Ni(this.logDir,"hparams.json");vn.writeFile(t,JSON.stringify(e,null,2))}finalize(){this._buffer.length>0&&this._flush()}_flush(){this._ensureDir();let e=this._getFilePath();if(!this._headerWritten){let s=["step",...this._columns].join(",");vn.writeFile(e,s+`
|
|
426
|
+
`),this._headerWritten=!0}let t=["step",...this._columns],r=[];for(let s=0;s<this._buffer.length;s++){let o=this._buffer[s],i=[];for(let a=0;a<t.length;a++){let u=o[t[a]];i.push(u!==void 0?String(u):"")}r.push(i.join(","))}vn.appendFile(e,r.join(`
|
|
427
|
+
`)+`
|
|
428
|
+
`),this._buffer.length=0}_getFilePath(){return this._filePath||(this._filePath=Ni(this.logDir,"metrics.csv")),this._filePath}_ensureDir(){let e=this.logDir;vn.exists(e)||vn.mkdir(e)}_resolveVersion(){if(this._version!==null)return this._version;let e=Ni(this._saveDir,this._name);if(!vn.exists(e))return this._version=0,0;let t=-1;try{let r=vn.readdir(e);for(let s=0;s<r.length;s++){let o=r[s].match(/^version_(\d+)$/);if(o){let i=parseInt(o[1],10);i>t&&(t=i)}}}catch{}return this._version=t+1,this._version}};var lr=class{static{l(this,"Metric")}_computed;constructor(){this._computed=null}update(e,t){throw new Error(`${this.constructor.name}.update() not implemented`)}compute(){throw new Error(`${this.constructor.name}.compute() not implemented`)}reset(){this._computed=null}forward(e,t){return this.update(e,t),this._computed=this.compute(),this._computed}get value(){return this._computed}};var ch=class extends lr{static{l(this,"MeanMetric")}_sum;_count;constructor(){super(),this._sum=0,this._count=0}update(e,t=1){let r=typeof e=="number"?e:e.item();this._sum+=r*t,this._count+=t}compute(){return this._count===0?0:this._sum/this._count}reset(){super.reset(),this._sum=0,this._count=0}},ph=class extends lr{static{l(this,"SumMetric")}_sum;constructor(){super(),this._sum=0}update(e){this._sum+=typeof e=="number"?e:e.item()}compute(){return this._sum}reset(){super.reset(),this._sum=0}};var fh=class{static{l(this,"MetricCollection")}_metrics;constructor(e={}){this._metrics=new Map;let t=Object.keys(e);for(let r=0;r<t.length;r++)this._metrics.set(t[r],e[t[r]])}add(e,t){return this._metrics.set(e,t),this}update(e,t){for(let[,r]of this._metrics)r.update(e,t)}compute(){let e={};for(let[t,r]of this._metrics)e[t]=r.compute();return e}reset(){for(let[,e]of this._metrics)e.reset()}forward(e,t){return this.update(e,t),this.compute()}get(e){return this._metrics.get(e)}has(e){return this._metrics.has(e)}get size(){return this._metrics.size}[Symbol.iterator](){return this._metrics.entries()}};var dh=class extends lr{static{l(this,"Accuracy")}_task;_numClasses;_topK;_threshold;_correct;_total;constructor({task:e="multiclass",numClasses:t=null,topK:r=1,threshold:s=.5}={}){super(),this._task=e,this._numClasses=t,this._topK=r,this._threshold=s,this._correct=0,this._total=0}update(e,t){let r=e._impl.storage.data,s=t._impl.storage.data;this._task==="binary"?this._updateBinary(r,s):this._task==="multiclass"?this._updateMulticlass(e,t):this._task==="multilabel"&&this._updateMultilabel(r,s)}compute(){return this._total===0?0:this._correct/this._total}reset(){super.reset(),this._correct=0,this._total=0}_updateBinary(e,t){let r=t.length;for(let s=0;s<r;s++)(e[s]>=this._threshold?1:0)===t[s]&&this._correct++;this._total+=r}_updateMulticlass(e,t){let r=e.shape;if(r.length<2){let u=e._impl.storage.data,p=t._impl.storage.data,c=p.length;for(let f=0;f<c;f++)Math.round(u[f])===p[f]&&this._correct++;this._total+=c;return}let s=r[0],o=r[1],i=e._impl.storage.data,a=t._impl.storage.data;if(this._topK===1)for(let u=0;u<s;u++){let p=0,c=i[u*o];for(let f=1;f<o;f++){let d=i[u*o+f];d>c&&(c=d,p=f)}p===a[u]&&this._correct++}else for(let u=0;u<s;u++){let p=_U(i,u*o,o,this._topK);for(let c=0;c<p.length;c++)if(p[c]===a[u]){this._correct++;break}}this._total+=s}_updateMultilabel(e,t){let r=t.length;for(let s=0;s<r;s++)(e[s]>=this._threshold?1:0)===t[s]&&this._correct++;this._total+=r}};function _U(n,e,t,r){let s=[];for(let o=0;o<t;o++){let i=n[e+o];s.length<r?(s.push({val:i,idx:o}),s.length===r&&bU(s)):i>s[0].val&&(s[0]={val:i,idx:o},CC(s,0))}return s.map(o=>o.idx)}l(_U,"topKIndices");function bU(n){for(let e=(n.length>>>1)-1;e>=0;e--)CC(n,e)}l(bU,"heapify");function CC(n,e){let t=n.length;for(;;){let r=e,s=2*e+1,o=2*e+2;if(s<t&&n[s].val<n[r].val&&(r=s),o<t&&n[o].val<n[r].val&&(r=o),r===e)break;let i=n[e];n[e]=n[r],n[r]=i,e=r}}l(CC,"siftDown");var hh=class extends lr{static{l(this,"Precision")}_task;_numClasses;_average;_tp;_fp;_support;constructor({task:e="multiclass",numClasses:t=2,average:r="macro"}={}){super(),this._task=e,this._numClasses=t,this._average=r,this._tp=new Int32Array(t),this._fp=new Int32Array(t),this._support=new Int32Array(t)}update(e,t){vk(e,t,this._tp,this._fp,null,this._support,this._task,this._numClasses)}compute(){return TU(this._tp,this._fp,this._support,this._numClasses,this._average)}reset(){super.reset(),this._tp.fill(0),this._fp.fill(0),this._support.fill(0)}},mh=class extends lr{static{l(this,"Recall")}_task;_numClasses;_average;_tp;_fn;_support;constructor({task:e="multiclass",numClasses:t=2,average:r="macro"}={}){super(),this._task=e,this._numClasses=t,this._average=r,this._tp=new Int32Array(t),this._fn=new Int32Array(t),this._support=new Int32Array(t)}update(e,t){vk(e,t,this._tp,null,this._fn,this._support,this._task,this._numClasses)}compute(){return xU(this._tp,this._fn,this._support,this._numClasses,this._average)}reset(){super.reset(),this._tp.fill(0),this._fn.fill(0),this._support.fill(0)}},gh=class extends lr{static{l(this,"F1Score")}_task;_numClasses;_average;_tp;_fp;_fn;_support;constructor({task:e="multiclass",numClasses:t=2,average:r="macro"}={}){super(),this._task=e,this._numClasses=t,this._average=r,this._tp=new Int32Array(t),this._fp=new Int32Array(t),this._fn=new Int32Array(t),this._support=new Int32Array(t)}update(e,t){vk(e,t,this._tp,this._fp,this._fn,this._support,this._task,this._numClasses)}compute(){let e=DC(this._tp,this._fp,this._numClasses),t=PC(this._tp,this._fn,this._numClasses),r=new Float64Array(this._numClasses);for(let s=0;s<this._numClasses;s++){let o=e[s]+t[s];r[s]=o>0?2*e[s]*t[s]/o:0}return kk(r,this._support,this._numClasses,this._average)}reset(){super.reset(),this._tp.fill(0),this._fp.fill(0),this._fn.fill(0),this._support.fill(0)}};function vk(n,e,t,r,s,o,i,a){let u=n._impl.storage.data,p=e._impl.storage.data,c=p.length,f=n.shape.length>=2&&n.shape[1]>1;for(let d=0;d<c;d++){let h=p[d]|0,m;i==="binary"?m=u[d]>=.5?1:0:f?m=wU(u,d,a):m=u[d]|0,o[h]++,m===h?t&&t[h]++:(r&&r[m]++,s&&s[h]++)}}l(vk,"updateConfusionCounters");function wU(n,e,t){let r=0,s=n[e*t];for(let o=1;o<t;o++){let i=n[e*t+o];i>s&&(s=i,r=o)}return r}l(wU,"argmaxRow");function DC(n,e,t){let r=new Float64Array(t);for(let s=0;s<t;s++){let o=n[s]+e[s];r[s]=o>0?n[s]/o:0}return r}l(DC,"computePrecisionPerClass");function PC(n,e,t){let r=new Float64Array(t);for(let s=0;s<t;s++){let o=n[s]+e[s];r[s]=o>0?n[s]/o:0}return r}l(PC,"computeRecallPerClass");function TU(n,e,t,r,s){let o=DC(n,e,r);return kk(o,t,r,s)}l(TU,"computePrecision");function xU(n,e,t,r,s){let o=PC(n,e,r);return kk(o,t,r,s)}l(xU,"computeRecall");function kk(n,e,t,r){if(r==="none")return[...n];if(r==="micro"){let i=0,a=0;for(let u=0;u<t;u++)i+=n[u]*e[u],a+=e[u];return a>0?i/a:0}if(r==="weighted"){let i=0,a=0;for(let u=0;u<t;u++)a+=n[u]*e[u],i+=e[u];return i>0?a/i:0}let s=0,o=0;for(let i=0;i<t;i++)e[i]>0&&(o+=n[i],s++);return s>0?o/s:0}l(kk,"aggregate");var yh=class extends lr{static{l(this,"ConfusionMatrix")}_numClasses;_matrix;constructor({numClasses:e}){super(),this._numClasses=e,this._matrix=new Int32Array(e*e)}update(e,t){let r=e._impl.storage.data,s=t._impl.storage.data,o=s.length,i=this._numClasses,a=e.shape.length>=2&&e.shape[1]>1;for(let u=0;u<o;u++){let p=s[u]|0,c;a?c=SU(r,u,i):c=r[u]|0,this._matrix[p*i+c]++}}compute(){let e=this._numClasses,t=[];for(let r=0;r<e;r++){let s=new Array(e);for(let o=0;o<e;o++)s[o]=this._matrix[r*e+o];t.push(s)}return t}reset(){super.reset(),this._matrix.fill(0)}};function SU(n,e,t){let r=0,s=n[e*t];for(let o=1;o<t;o++){let i=n[e*t+o];i>s&&(s=i,r=o)}return r}l(SU,"argmaxRow");be();var FC={};at(FC,{cholesky:()=>Ok,cov:()=>AU,det:()=>OU,eigh:()=>vU,inv:()=>kU,lstsq:()=>bh,pinv:()=>NU,qr:()=>Ak,solve:()=>_h,svd:()=>Nk});be();function Nk(n){let[e,t,r]=ct("svd",n);return{U:e,S:t,V:r}}l(Nk,"svd");function vU(n){let[e,t]=ct("eigh",n);return{values:e,vectors:t}}l(vU,"eigh");function Ak(n){let[e,t]=ct("qr",n);return{Q:e,R:t}}l(Ak,"qr");var Ok=l(n=>ct("cholesky",n),"cholesky"),kU=l(n=>ct("inv",n),"inv"),NU=l(n=>ct("pinv",n),"pinv"),AU=l(n=>ct("cov",n),"cov"),_h=l((n,e)=>ct("solve",n,e),"solve"),bh=l((n,e)=>ct("lstsq",n,e),"lstsq"),OU=l(n=>ct("det",n).item(),"det");var jC={};at(jC,{DecisionTreeClassifier:()=>Uk,DecisionTreeRegressor:()=>jk,ElasticNet:()=>vw,GaussianNB:()=>Gk,GradientBoostingClassifier:()=>Hk,GradientBoostingRegressor:()=>Kk,GridSearchCV:()=>Jk,KFold:()=>Bw,KMeans:()=>Fk,KNeighborsClassifier:()=>zk,KNeighborsRegressor:()=>Vk,LabelEncoder:()=>Rk,Lasso:()=>Ck,LinearRegression:()=>Ek,LogisticRegression:()=>Dk,MinMaxScaler:()=>Ik,OneHotEncoder:()=>Mk,PCA:()=>$k,RandomForestClassifier:()=>Wk,RandomForestRegressor:()=>qk,Ridge:()=>Lk,StandardScaler:()=>Bk,TimeSeriesSplit:()=>Yk,accuracy_score:()=>ao,confusion_matrix:()=>MU,cross_val_score:()=>GC,mean_absolute_error:()=>RU,mean_squared_error:()=>BU,r2_score:()=>io,train_test_split:()=>LU});var Bk=class{static{l(this,"StandardScaler")}withMean;withStd;mean_;scale_;_cols;constructor({withMean:e=!0,withStd:t=!0}={}){this.withMean=e,this.withStd=t,this.mean_=null,this.scale_=null}fit(e){let t=sc(e);this.mean_=new Float64Array(t.cols),this.scale_=new Float64Array(t.cols);for(let r=0;r<t.cols;r++){let s=0;for(let a=0;a<t.rows;a++)s+=t.data[a*t.cols+r];s/=t.rows;let o=0;for(let a=0;a<t.rows;a++){let u=t.data[a*t.cols+r]-s;o+=u*u}o/=t.rows;let i=Math.sqrt(o);this.mean_[r]=this.withMean?s:0,this.scale_[r]=this.withStd&&i>0?i:1}return this._cols=t.cols,this}transform(e){let t=sc(e),r=this.mean_,s=this.scale_,o=new Float64Array(t.rows*t.cols);for(let i=0;i<t.rows;i++)for(let a=0;a<t.cols;a++)o[i*t.cols+a]=(t.data[i*t.cols+a]-r[a])/s[a];return Nf(o,t.rows,t.cols,e.dtype)}fit_transform(e){return this.fit(e).transform(e)}inverse_transform(e){let t=sc(e),r=this.mean_,s=this.scale_,o=new Float64Array(t.rows*t.cols);for(let i=0;i<t.rows;i++)for(let a=0;a<t.cols;a++)o[i*t.cols+a]=t.data[i*t.cols+a]*s[a]+r[a];return Nf(o,t.rows,t.cols,e.dtype)}},Rk=class{static{l(this,"LabelEncoder")}classes_;_lookup;constructor(){this.classes_=null,this._lookup=null}fit(e){let t=Yt(e),{classes:r}=Ns(t.data,t.n);return this.classes_=r,this._lookup=new Map(r.map((s,o)=>[s,o])),this}transform(e){let t=Yt(e),r=this._lookup,s=new Float64Array(t.n);for(let o=0;o<t.n;o++){let i=r.get(t.data[o]);if(i===void 0)throw new Error(`LabelEncoder: unseen label ${t.data[o]}`);s[o]=i}return xS(s,t.n,e.dtype)}fit_transform(e){let t=Yt(e),{y:r,classes:s}=Ns(t.data,t.n);this.classes_=s,this._lookup=new Map(s.map((i,a)=>[i,a]));let o=new Float64Array(t.n);for(let i=0;i<t.n;i++)o[i]=r[i];return xS(o,t.n,e.dtype)}inverse_transform(e){let t=Yt(e),r=this.classes_;return Array.from({length:t.n},(s,o)=>r[Math.round(t.data[o])])}},Mk=class{static{l(this,"OneHotEncoder")}classes_;constructor(){this.classes_=null}fit(e){let t=Yt(e);return this.classes_=Ns(t.data,t.n).classes,this}transform(e){let t=Yt(e),r=this.classes_,s=new Map(r.map((a,u)=>[a,u])),o=r.length,i=new Float64Array(t.n*o);for(let a=0;a<t.n;a++){let u=s.get(t.data[a]);if(u===void 0)throw new Error(`OneHotEncoder: unseen label ${t.data[a]}`);i[a*o+u]=1}return Nf(i,t.n,o,e.dtype)}fit_transform(e){return this.fit(e).transform(e)}},Ik=class{static{l(this,"MinMaxScaler")}featureRange;min_;dataMin_;dataRange_;constructor({featureRange:e=[0,1]}={}){this.featureRange=e,this.min_=null,this.dataMin_=null,this.dataRange_=null}fit(e){let t=sc(e);this.dataMin_=new Float64Array(t.cols),this.dataRange_=new Float64Array(t.cols);for(let r=0;r<t.cols;r++){let s=1/0,o=-1/0;for(let i=0;i<t.rows;i++){let a=t.data[i*t.cols+r];a<s&&(s=a),a>o&&(o=a)}this.dataMin_[r]=s,this.dataRange_[r]=o>s?o-s:1}return this}transform(e){let t=sc(e),[r,s]=this.featureRange,o=this.dataMin_,i=this.dataRange_,a=s-r,u=new Float64Array(t.rows*t.cols);for(let p=0;p<t.rows;p++)for(let c=0;c<t.cols;c++){let f=(t.data[p*t.cols+c]-o[c])/i[c];u[p*t.cols+c]=f*a+r}return Nf(u,t.rows,t.cols,e.dtype)}fit_transform(e){return this.fit(e).transform(e)}};function BU(n,e){let t=Yt(n),r=Yt(e),s=0;for(let o=0;o<t.n;o++){let i=t.data[o]-r.data[o];s+=i*i}return s/t.n}l(BU,"mean_squared_error");function RU(n,e){let t=Yt(n),r=Yt(e),s=0;for(let o=0;o<t.n;o++)s+=Math.abs(t.data[o]-r.data[o]);return s/t.n}l(RU,"mean_absolute_error");function io(n,e){let t=Yt(n),r=Yt(e),s=0;for(let a=0;a<t.n;a++)s+=t.data[a];s/=t.n;let o=0,i=0;for(let a=0;a<t.n;a++){let u=t.data[a]-r.data[a],p=t.data[a]-s;o+=u*u,i+=p*p}return i===0?0:1-o/i}l(io,"r2_score");function ao(n,e){let t=Yt(n),r=Yt(e),s=0;for(let o=0;o<t.n;o++)t.data[o]===r.data[o]&&s++;return s/t.n}l(ao,"accuracy_score");function MU(n,e){let t=Yt(n),r=Yt(e),s=0;for(let a=0;a<t.n;a++)s=Math.max(s,t.data[a],r.data[a]);let o=s+1,i=Array.from({length:o},()=>new Array(o).fill(0));for(let a=0;a<t.n;a++)i[t.data[a]][r.data[a]]++;return i}l(MU,"confusion_matrix");be();kt();os();function zC(n,e){return e?ru([n,tn([n.shape[0],1],{device:n.device,dtype:n.dtype})],1):n}l(zC,"addBias");function Pk(n){return n.ndim===1?n.reshape([n.shape[0],1]):n}l(Pk,"asColumn");var Ek=class{static{l(this,"LinearRegression")}fitIntercept;weight_;constructor({fitIntercept:e=!0}={}){this.fitIntercept=e,this.weight_=null}fit(e,t){let r=zC(e,this.fitIntercept);return this.weight_=bh(r,Pk(t)),this}predict(e){let t=zC(e,this.fitIntercept);return tt(t,this.weight_).reshape([e.shape[0]])}score(e,t){return io(t,this.predict(e))}},Lk=class{static{l(this,"Ridge")}alpha;fitIntercept;coef_;intercept_;constructor({alpha:e=1,fitIntercept:t=!0}={}){this.alpha=e,this.fitIntercept=t,this.coef_=null,this.intercept_=null}fit(e,t){let r=e.shape[1],s=Pk(t),o=e,i=s,a=null,u=null;this.fitIntercept&&(a=Tr(e,[0],!0),u=Tr(s,[0],!0),o=ze(e,a),i=ze(s,u));let p=o.transpose(0,1),c=ae(tt(p,o),ee(jg(r,r,{device:e.device,dtype:e.dtype}),this.alpha));return this.coef_=_h(c,tt(p,i)),this.intercept_=this.fitIntercept?ze(u,tt(a,this.coef_)):Ve([1,1],{device:e.device,dtype:e.dtype}),this}predict(e){return ae(tt(e,this.coef_),this.intercept_).reshape([e.shape[0]])}score(e,t){return io(t,this.predict(e))}},vw=class{static{l(this,"ElasticNet")}alpha;l1Ratio;fitIntercept;maxIter;tol;coef_;intercept_;constructor({alpha:e=1,l1Ratio:t=.5,fitIntercept:r=!0,maxIter:s=1e3,tol:o=1e-6}={}){this.alpha=e,this.l1Ratio=t,this.fitIntercept=r,this.maxIter=s,this.tol=o,this.coef_=null,this.intercept_=null}fit(e,t){let[r,s]=ct("elastic_net",e,Pk(t).reshape([e.shape[0]]),this.alpha,this.l1Ratio,this.maxIter,this.tol,this.fitIntercept);return this.coef_=r,this.intercept_=s,this}predict(e){let t=this.coef_.reshape([e.shape[1],1]);return ae(tt(e,t).reshape([e.shape[0]]),this.intercept_)}score(e,t){return io(t,this.predict(e))}},Ck=class extends vw{static{l(this,"Lasso")}constructor({alpha:e=1,fitIntercept:t=!0,maxIter:r=1e3,tol:s=1e-6}={}){super({alpha:e,l1Ratio:1,fitIntercept:t,maxIter:r,tol:s})}},Dk=class{static{l(this,"LogisticRegression")}C;lr;maxIter;W_;b_;classes_;constructor({C:e=1,lr:t=.5,maxIter:r=1e3}={}){this.C=e,this.lr=t,this.maxIter=r,this.W_=null,this.b_=null,this.classes_=null}fit(e,t){let r=e.shape[0],s=e.shape[1],o=Yt(t),{y:i,classes:a}=Ns(o.data,o.n);this.classes_=a;let u=a.length,p=new Float64Array(r*u);for(let y=0;y<r;y++)p[y*u+i[y]]=1;let c=Wt(p,{shape:[r,u],dtype:e.dtype,device:e.device}),f=Ve([s,u],{device:e.device,dtype:e.dtype}),d=Ve([1,u],{device:e.device,dtype:e.dtype}),h=1/this.C,m=e.transpose(0,1),g=this.lr/r;for(let y=0;y<this.maxIter;y++){let _=ae(tt(e,f),d),b=ze(_,No(_,1,!0)),w=as(b),x=Kt(w,wt(w,[1],!0)),S=ze(x,c),T=ae(tt(m,S),ee(f,h)),v=wt(S,[0],!0);f=ze(f,ee(T,g)),d=ze(d,ee(v,g))}return this.W_=f,this.b_=d,this}decisionLogits(e){return ae(tt(e,this.W_),this.b_)}predict(e){let t=Cg(this.decisionLogits(e),1,!1).toArray(),r=new Float64Array(t.length),s=this.classes_;for(let o=0;o<t.length;o++)r[o]=s[t[o]];return Wt(r,{shape:[t.length],dtype:e.dtype,device:e.device})}score(e,t){return ao(t,this.predict(e))}};be();var $k=class{static{l(this,"PCA")}nComponents;components_;mean_;explainedVariance_;explainedVarianceRatio_;_nc;constructor({nComponents:e=null}={}){this.nComponents=e,this.components_=null,this.mean_=null,this.explainedVariance_=null,this.explainedVarianceRatio_=null}fit(e){let t=e.shape[0],r=e.shape[1];this.mean_=Tr(e,[0],!0);let s=ze(e,this.mean_),{S:o,V:i}=Nk(s),a=i.shape[1],u=Math.min(this.nComponents??Math.min(t,r),a);this.components_=i.narrow(1,0,u),this._nc=u;let p=o.toArray(),c=t>1?t-1:1,f=0;for(let d=0;d<p.length;d++)f+=p[d]*p[d]/c;this.explainedVariance_=new Array(u),this.explainedVarianceRatio_=new Array(u);for(let d=0;d<u;d++){let h=p[d]*p[d]/c;this.explainedVariance_[d]=h,this.explainedVarianceRatio_[d]=f>0?h/f:0}return this}transform(e){return tt(ze(e,this.mean_),this.components_)}fit_transform(e){return this.fit(e).transform(e)}inverse_transform(e){return ae(tt(e,this.components_.transpose(0,1)),this.mean_)}};be();var Fk=class{static{l(this,"KMeans")}nClusters;maxIter;nInit;randomState;clusterCenters_;labels_;inertia_;constructor({nClusters:e=8,maxIter:t=300,nInit:r=10,randomState:s=0}={}){this.nClusters=e,this.maxIter=t,this.nInit=r,this.randomState=s,this.clusterCenters_=null,this.labels_=null,this.inertia_=null}fit(e){let[t,r,s]=ct("kmeans",e,this.nClusters,this.maxIter,this.nInit,this.randomState);return this.clusterCenters_=t,this.labels_=r,this.inertia_=Number(s.item()),this}predict(e){return ct("kmeans_predict",e,this.clusterCenters_)}fit_predict(e){return this.fit(e),this.labels_}};be();var kw=class{static{l(this,"BaseKNN")}nNeighbors;_classify;_X;_y;constructor(e,t){this.nNeighbors=e,this._classify=t,this._X=null,this._y=null}fit(e,t){return this._X=e,this._y=t,this}predict(e){return ct("knn_predict",this._X,this._y,e,this.nNeighbors,this._classify)}},zk=class extends kw{static{l(this,"KNeighborsClassifier")}constructor({nNeighbors:e=5}={}){super(e,!0)}score(e,t){return ao(t,this.predict(e))}},Vk=class extends kw{static{l(this,"KNeighborsRegressor")}constructor({nNeighbors:e=5}={}){super(e,!1)}score(e,t){return io(t,this.predict(e))}};be();var Gk=class{static{l(this,"GaussianNB")}means_;variances_;priors_;classes_;constructor(){this.means_=null,this.variances_=null,this.priors_=null,this.classes_=null}fit(e,t){let[r,s,o,i]=ct("gaussian_nb_fit",e,t);return this.means_=r,this.variances_=s,this.priors_=o,this.classes_=i,this}predict(e){return ct("gaussian_nb_predict",e,this.means_,this.variances_,this.priors_,this.classes_)}score(e,t){return ao(t,this.predict(e))}};be();os();var VC=1<<30;function Ow(n,e,t,r,s){return ct("decision_tree_fit",n,e,t.maxDepth,t.minSamplesSplit,t.minSamplesLeaf,t.maxFeatures,r,s)}l(Ow,"fitTree");function Zc(n,e){return ct("decision_tree_predict",n,e[0],e[1],e[2],e[3],e[4])}l(Zc,"treePredict");var Nw=class{static{l(this,"BaseTree")}maxDepth;minSamplesSplit;minSamplesLeaf;maxFeatures;randomState;_classify;_nodes;constructor(e,t){this.maxDepth=e.maxDepth??VC,this.minSamplesSplit=e.minSamplesSplit??2,this.minSamplesLeaf=e.minSamplesLeaf??1,this.maxFeatures=e.maxFeatures??0,this.randomState=e.randomState??0,this._classify=t,this._nodes=null}fit(e,t){return this._nodes=Ow(e,t,this,this._classify,this.randomState),this}predict(e){return Zc(e,this._nodes)}},jk=class extends Nw{static{l(this,"DecisionTreeRegressor")}constructor(e={}){super(e,!1)}score(e,t){return io(t,this.predict(e))}},Uk=class extends Nw{static{l(this,"DecisionTreeClassifier")}constructor(e={}){super(e,!0)}score(e,t){return ao(t,this.predict(e))}};function IU(n,e,t){return n>0?n:t?Math.max(1,Math.floor(Math.sqrt(e))):Math.max(1,Math.floor(e/3))}l(IU,"defaultMaxFeatures");var Aw=class{static{l(this,"BaseForest")}nEstimators;maxDepth;minSamplesSplit;minSamplesLeaf;maxFeatures;randomState;_classify;_trees;constructor(e,t){this.nEstimators=e.nEstimators??100,this.maxDepth=e.maxDepth??VC,this.minSamplesSplit=e.minSamplesSplit??2,this.minSamplesLeaf=e.minSamplesLeaf??1,this.maxFeatures=e.maxFeatures??0,this.randomState=e.randomState??0,this._classify=t,this._trees=[]}fit(e,t){let r=e.shape[0],s=e.shape[1],o=xr(this.randomState),i=IU(this.maxFeatures,s,this._classify),a={maxDepth:this.maxDepth,minSamplesSplit:this.minSamplesSplit,minSamplesLeaf:this.minSamplesLeaf,maxFeatures:i};this._trees=[];for(let u=0;u<this.nEstimators;u++){let p=new Array(r);for(let d=0;d<r;d++)p[d]=Math.floor(o()*r);let c=ks(e,p),f=ks(t,p);this._trees.push(Ow(c,f,a,this._classify,this.randomState+u+1))}return this}predict(e){let t=e.shape[0],r=this._trees.map(o=>Zc(e,o).toArray()),s=new Float64Array(t);if(this._classify)for(let o=0;o<t;o++){let i=new Map,a=r[0][o],u=0;for(let p=0;p<r.length;p++){let c=r[p][o],f=(i.get(c)||0)+1;i.set(c,f),f>u&&(u=f,a=c)}s[o]=a}else for(let o=0;o<t;o++){let i=0;for(let a=0;a<r.length;a++)i+=r[a][o];s[o]=i/r.length}return Wt(s,{shape:[t],dtype:e.dtype,device:e.device})}},qk=class extends Aw{static{l(this,"RandomForestRegressor")}constructor(e={}){super(e,!1)}score(e,t){return io(t,this.predict(e))}},Wk=class extends Aw{static{l(this,"RandomForestClassifier")}constructor(e={}){super(e,!0)}score(e,t){return ao(t,this.predict(e))}},Kk=class{static{l(this,"GradientBoostingRegressor")}nEstimators;learningRate;params;randomState;init_;_trees;constructor({nEstimators:e=100,learningRate:t=.1,maxDepth:r=3,minSamplesSplit:s=2,minSamplesLeaf:o=1,randomState:i=0}={}){this.nEstimators=e,this.learningRate=t,this.params={maxDepth:r,minSamplesSplit:s,minSamplesLeaf:o,maxFeatures:0},this.randomState=i,this.init_=0,this._trees=[]}fit(e,t){let r=Yt(t),s=r.n,o=0;for(let a=0;a<s;a++)o+=r.data[a];o/=s,this.init_=o;let i=new Float64Array(s).fill(o);this._trees=[];for(let a=0;a<this.nEstimators;a++){let u=new Float64Array(s);for(let d=0;d<s;d++)u[d]=r.data[d]-i[d];let p=Wt(u,{shape:[s],dtype:e.dtype,device:e.device}),c=Ow(e,p,this.params,!1,this.randomState+a+1);this._trees.push(c);let f=Zc(e,c).toArray();for(let d=0;d<s;d++)i[d]+=this.learningRate*f[d]}return this}predict(e){let t=e.shape[0],r=new Float64Array(t).fill(this.init_);for(let s of this._trees){let o=Zc(e,s).toArray();for(let i=0;i<t;i++)r[i]+=this.learningRate*o[i]}return Wt(r,{shape:[t],dtype:e.dtype,device:e.device})}score(e,t){return io(t,this.predict(e))}},Hk=class{static{l(this,"GradientBoostingClassifier")}nEstimators;learningRate;params;randomState;classes_;_stages;constructor({nEstimators:e=100,learningRate:t=.1,maxDepth:r=3,minSamplesSplit:s=2,minSamplesLeaf:o=1,randomState:i=0}={}){this.nEstimators=e,this.learningRate=t,this.params={maxDepth:r,minSamplesSplit:s,minSamplesLeaf:o,maxFeatures:0},this.randomState=i,this.classes_=null,this._stages=[]}fit(e,t){let r=Yt(t),s=r.n,{y:o,classes:i}=Ns(r.data,r.n);this.classes_=i;let a=i.length,u=new Float64Array(s*a);this._stages=[];for(let p=0;p<this.nEstimators;p++){let c=EU(u,s,a),f=[];for(let d=0;d<a;d++){let h=new Float64Array(s);for(let _=0;_<s;_++)h[_]=(o[_]===d?1:0)-c[_*a+d];let m=Wt(h,{shape:[s],dtype:e.dtype,device:e.device}),g=Ow(e,m,this.params,!1,this.randomState+p*a+d+1);f.push(g);let y=Zc(e,g).toArray();for(let _=0;_<s;_++)u[_*a+d]+=this.learningRate*y[_]}this._stages.push(f)}return this}predict(e){let t=e.shape[0],r=this.classes_,s=r.length,o=new Float64Array(t*s);for(let a of this._stages)for(let u=0;u<s;u++){let p=Zc(e,a[u]).toArray();for(let c=0;c<t;c++)o[c*s+u]+=this.learningRate*p[c]}let i=new Float64Array(t);for(let a=0;a<t;a++){let u=0;for(let p=1;p<s;p++)o[a*s+p]>o[a*s+u]&&(u=p);i[a]=r[u]}return Wt(i,{shape:[t],dtype:e.dtype,device:e.device})}score(e,t){return ao(t,this.predict(e))}};function EU(n,e,t){let r=new Float64Array(e*t);for(let s=0;s<e;s++){let o=-1/0;for(let a=0;a<t;a++)n[s*t+a]>o&&(o=n[s*t+a]);let i=0;for(let a=0;a<t;a++){let u=Math.exp(n[s*t+a]-o);r[s*t+a]=u,i+=u}for(let a=0;a<t;a++)r[s*t+a]/=i}return r}l(EU,"softmaxRows");function LU(n,e,{testSize:t=.25,shuffle:r=!0,randomState:s=0}={}){let o=n.shape[0],i=r?gS(o,xr(s)):Array.from({length:o},(c,f)=>f),a=Math.max(1,Math.round(o*t)),u=i.slice(0,a),p=i.slice(a);return[ks(n,p),ks(n,u),ks(e,p),ks(e,u)]}l(LU,"train_test_split");var Bw=class{static{l(this,"KFold")}nSplits;shuffle;randomState;constructor({nSplits:e=5,shuffle:t=!1,randomState:r=0}={}){this.nSplits=e,this.shuffle=t,this.randomState=r}split(e){let t=this.shuffle?gS(e,xr(this.randomState)):Array.from({length:e},(a,u)=>u),r=[],s=Math.floor(e/this.nSplits),o=e%this.nSplits,i=0;for(let a=0;a<this.nSplits;a++){let u=s+(o>0?1:0);o>0&&o--;let p=t.slice(i,i+u),c=t.slice(0,i).concat(t.slice(i+u));r.push({train:c,test:p}),i+=u}return r}},Yk=class{static{l(this,"TimeSeriesSplit")}nSplits;constructor({nSplits:e=5}={}){this.nSplits=e}split(e){let t=Math.floor(e/(this.nSplits+1)),r=[];for(let s=1;s<=this.nSplits;s++){let o=t*s,i=s===this.nSplits?e:t*(s+1),a=Array.from({length:o},(p,c)=>c),u=Array.from({length:i-o},(p,c)=>o+c);r.push({train:a,test:u})}return r}};function GC(n,e,t,{cv:r=5,scoring:s=null,shuffle:o=!0,randomState:i=0}={}){let a=new Bw({nSplits:r,shuffle:o,randomState:i}).split(e.shape[0]),u=[];for(let{train:p,test:c}of a){let f=ks(e,p),d=ks(t,p),h=ks(e,c),m=ks(t,c),g=n().fit(f,d);u.push(s?s(m,g.predict(h)):g.score(h,m))}return u}l(GC,"cross_val_score");function CU(n){let e=Object.keys(n),t=[{}];for(let r of e){let s=[];for(let o of t)for(let i of n[r])s.push({...o,[r]:i});t=s}return t}l(CU,"cartesian");var Jk=class{static{l(this,"GridSearchCV")}makeEstimator;paramGrid;cv;scoring;bestParams_;bestScore_;bestEstimator_;constructor(e,t,{cv:r=5,scoring:s=null}={}){this.makeEstimator=e,this.paramGrid=t,this.cv=r,this.scoring=s,this.bestParams_=null,this.bestScore_=-1/0,this.bestEstimator_=null}fit(e,t){for(let r of CU(this.paramGrid)){let s=GC(()=>this.makeEstimator(r),e,t,{cv:this.cv,scoring:this.scoring}),o=s.reduce((i,a)=>i+a,0)/s.length;o>this.bestScore_&&(this.bestScore_=o,this.bestParams_=r)}return this.bestEstimator_=this.makeEstimator(this.bestParams_).fit(e,t),this}predict(e){return this.bestEstimator_.predict(e)}};var ZD={};at(ZD,{Generator:()=>Vw,acf:()=>RD,andersonDarling:()=>AD,betaRegularized:()=>Th,bisect:()=>UC,brentq:()=>Rw,chi2:()=>ep,chi2Gof:()=>wD,chi2Independence:()=>TD,constrainedMinimize:()=>iN,convolve:()=>zD,correlate:()=>VD,cubicSpline:()=>mD,dagostinoK2:()=>ND,differentialEvolution:()=>nN,digammaScalar:()=>ja,durbinWatson:()=>ED,erfScalar:()=>Wi,erfcScalar:()=>Ki,fft:()=>Fu,fisherF:()=>nD,gammaScalar:()=>Hi,ifft:()=>tp,jarqueBera:()=>kD,ksTest1Samp:()=>SD,ksTest2Samp:()=>vD,lbfgs:()=>Sh,lbfgsB:()=>sN,levenbergMarquardt:()=>oN,lgammaScalar:()=>cn,linearInterp:()=>hD,ljungBox:()=>ID,lowerGammaRegularized:()=>Iw,mannWhitneyU:()=>OD,nelderMead:()=>eN,newton:()=>qC,normal:()=>tD,normalCdfScalar:()=>Uo,normalPdfScalar:()=>xh,normalPpfScalar:()=>xa,pacf:()=>MD,periodogram:()=>LD,polyfit:()=>HD,polyroots:()=>JD,polyval:()=>YD,qr:()=>Ak,quadrature:()=>pD,rollingMax:()=>KD,rollingMean:()=>jD,rollingMin:()=>WD,rollingStd:()=>UD,rollingSum:()=>GD,simpson:()=>cD,studentT:()=>Ew,tTest1Samp:()=>fN,tTestInd:()=>_D,tTestPaired:()=>bD,trapezoid:()=>lD});sr();wn();function Es(n,e,t){return{root:n,iterations:e,converged:t}}l(Es,"result");function UC(n,e,t,r={}){let s=r.tol??1e-12,o=r.maxIter??100,i=n(e),a=n(t);if(i===0)return Es(e,0,!0);if(a===0)return Es(t,0,!0);if(i*a>0)throw new Error("bisect: f(a) and f(b) must have opposite signs");let u=e,p=t,c=i;for(let f=1;f<=o;f++){let d=.5*(u+p),h=n(d);if(h===0||.5*(p-u)<s)return Es(d,f,!0);c*h<0?p=d:(u=d,c=h)}return Es(.5*(u+p),o,!1)}l(UC,"bisect");function qC(n,e,t={}){let r=t.tol??1e-12,s=t.maxIter??100,o=t.step??1e-7,i=t.derivative??(u=>(n(u+o)-n(u-o))/(2*o)),a=e;for(let u=1;u<=s;u++){let p=n(a);if(Math.abs(p)<r)return Es(a,u,!0);let c=i(a);if(c===0)break;let f=a-p/c;if(Math.abs(f-a)<r)return Es(f,u,!0);a=f}return Es(a,s,!1)}l(qC,"newton");function Rw(n,e,t,r={}){let s=r.tol??1e-12,o=r.maxIter??100,i=n(e),a=n(t);if(i===0)return Es(e,0,!0);if(a===0)return Es(t,0,!0);if(i*a>0)throw new Error("brentq: f(a) and f(b) must have opposite signs");Math.abs(i)<Math.abs(a)&&([e,t]=[t,e],[i,a]=[a,i]);let u=e,p=i,c=u,f=!0;for(let d=1;d<=o;d++){let h;i!==p&&a!==p?h=e*a*p/((i-a)*(i-p))+t*i*p/((a-i)*(a-p))+u*i*a/((p-i)*(p-a)):h=t-a*(t-e)/(a-i);let m=.25*(3*e+t),g=!(h>Math.min(m,t)&&h<Math.max(m,t)),y=f&&Math.abs(h-t)>=.5*Math.abs(t-u),_=!f&&Math.abs(h-t)>=.5*Math.abs(u-c),b=f&&Math.abs(t-u)<s,w=!f&&Math.abs(u-c)<s;g||y||_||b||w?(h=.5*(e+t),f=!0):f=!1;let x=n(h);if(c=u,u=t,p=a,i*x<0?(t=h,a=x):(e=h,i=x),Math.abs(i)<Math.abs(a)&&([e,t]=[t,e],[i,a]=[a,i]),x===0)return Es(h,d,!0);if(Math.abs(a)<s||Math.abs(t-e)<s)return Es(t,d,!0)}return Es(t,o,!1)}l(Rw,"brentq");Yi();var Qc=[-39.69683028665376,220.9460984245205,-275.9285104469687,138.357751867269,-30.66479806614716,2.506628277459239],wh=[-54.47609879822406,161.5858368580409,-155.6989798598866,66.80131188771972,-13.28068155288572],Xc=[-.007784894002430293,-.3223964580411365,-2.400758277161838,-2.549732539343734,4.374664141464968,2.938163982698783],Mw=[.007784695709041462,.3224671290700398,2.445134137142996,3.754408661907416],WC=.02425,Zk=300,Qk=3e-14,Ar=1e-300,DU=2,PU=Math.SQRT2,$U=Math.sqrt(2*Math.PI),aSe=1/Math.sqrt(Math.PI);function Iw(n,e){if(e<=0)return 0;if(e<n+1){let i=1/n,a=i;for(let u=1;u<Zk&&(i*=e/(n+u),a+=i,!(Math.abs(i)<Math.abs(a)*Qk));u++);return a*Math.exp(-e+n*Math.log(e)-cn(n))}let t=e+1-n,r=1/Ar,s=1/t,o=s;for(let i=1;i<Zk;i++){let a=-i*(i-n);t+=2,s=a*s+t,Math.abs(s)<Ar&&(s=Ar),r=t+a/r,Math.abs(r)<Ar&&(r=Ar),s=1/s;let u=s*r;if(o*=u,Math.abs(u-1)<Qk)break}return 1-o*Math.exp(-e+n*Math.log(e)-cn(n))}l(Iw,"lowerGammaRegularized");function KC(n,e,t){let r=n+e,s=n+1,o=n-1,i=1,a=1-r*t/s;Math.abs(a)<Ar&&(a=Ar),a=1/a;let u=a;for(let p=1;p<Zk;p++){let c=2*p,f=p*(e-p)*t/((o+c)*(n+c));a=1+f*a,Math.abs(a)<Ar&&(a=Ar),i=1+f/i,Math.abs(i)<Ar&&(i=Ar),a=1/a,u*=a*i,f=-(n+p)*(r+p)*t/((n+c)*(s+c)),a=1+f*a,Math.abs(a)<Ar&&(a=Ar),i=1+f/i,Math.abs(i)<Ar&&(i=Ar),a=1/a;let d=a*i;if(u*=d,Math.abs(d-1)<Qk)break}return u}l(KC,"betacf");function Th(n,e,t){if(t<=0)return 0;if(t>=1)return 1;let r=Math.exp(cn(n+e)-cn(n)-cn(e)+n*Math.log(t)+e*Math.log(1-t));return t<(n+1)/(n+e+2)?r*KC(n,e,t)/n:1-r*KC(e,n,1-t)/e}l(Th,"betaRegularized");function Uo(n){return .5*Ki(-n/PU)}l(Uo,"normalCdfScalar");function xh(n){return Math.exp(-.5*n*n)/$U}l(xh,"normalPdfScalar");function HC(n){if(n<WC){let r=Math.sqrt(-2*Math.log(n));return(((((Xc[0]*r+Xc[1])*r+Xc[2])*r+Xc[3])*r+Xc[4])*r+Xc[5])/((((Mw[0]*r+Mw[1])*r+Mw[2])*r+Mw[3])*r+1)}if(n>1-WC)return-HC(1-n);let e=n-.5,t=e*e;return(((((Qc[0]*t+Qc[1])*t+Qc[2])*t+Qc[3])*t+Qc[4])*t+Qc[5])*e/(((((wh[0]*t+wh[1])*t+wh[2])*t+wh[3])*t+wh[4])*t+1)}l(HC,"ppfSeed");function xa(n,e={}){if(n<=0)return-1/0;if(n>=1)return 1/0;let t=e.refineSteps??DU,r=HC(n);for(let s=0;s<t;s++){let i=(Uo(r)-n)/xh(r);r-=i/(1+r*i/2)}return r}l(xa,"normalPpfScalar");var FU=3,zU=2,VU=60,GU=1e-11;function Ls(n,e){if(n instanceof Ze){let t=Qe(n),r=new Float64Array(t.length);for(let s=0;s<t.length;s++)r[s]=e(Number(t[s]));return Q(r,n.shape,n.dtype,n.device)}return e(n)}l(Ls,"mapElementwise");function Xk(n,e,t,r,s={}){if(t<=0||t>=1)return t===0?s.lowerLimit??-1/0:t===1?1/0:NaN;let o=s.refineSteps??FU,i=s.tol??GU,a=r;for(let f=0;f<o;f++){let d=n(a)-t,h=e(a);if(!Number.isFinite(a)||h<=0)break;let m=a-d/h;if(a=s.lowerLimit!==void 0&&m<=s.lowerLimit?(a+s.lowerLimit)/2:m,Math.abs(d)<i)return a}if(Math.abs(n(a)-t)<i)return a;let u=s.lowerLimit!==void 0?s.lowerLimit+Number.EPSILON:a,p=a,c=Math.max(1,Math.abs(a));for(let f=0;f<VU;f++){if(s.lowerLimit===void 0&&(u=a-c),p=a+c,n(u)-t<0&&n(p)-t>0)return Rw(d=>n(d)-t,u,p,{tol:i}).root;c*=zU}return a}l(Xk,"invertCdf");var tD={cdf:l((n,e={})=>{let t=e.loc??0,r=e.scale??1;return Ls(n,s=>Uo((s-t)/r))},"cdf"),pdf:l((n,e={})=>{let t=e.loc??0,r=e.scale??1;return Ls(n,s=>xh((s-t)/r)/r)},"pdf"),ppf:l((n,e={})=>{let t=e.loc??0,r=e.scale??1;return Ls(n,s=>t+r*xa(s,e))},"ppf")};function YC(n,e){if(n===0)return .5;let t=Th(e/2,.5,e/(e+n*n));return n>0?1-t/2:t/2}l(YC,"tCdfScalar");function JC(n,e){let t=cn((e+1)/2)-cn(e/2)-.5*Math.log(e*Math.PI);return Math.exp(t-(e+1)/2*Math.log(1+n*n/e))}l(JC,"tPdfScalar");var Ew={cdf:l((n,e,t={})=>Ls(n,r=>YC(r,e??t.df)),"cdf"),pdf:l((n,e,t={})=>Ls(n,r=>JC(r,e??t.df)),"pdf"),ppf:l((n,e,t={})=>{let r=e??t.df;return Ls(n,s=>Xk(o=>YC(o,r),o=>JC(o,r),s,xa(s),t))},"ppf")};function ZC(n,e){return n<=0?0:Iw(e/2,n/2)}l(ZC,"chi2CdfScalar");function QC(n,e){if(n<=0)return 0;let t=e/2;return Math.exp((t-1)*Math.log(n)-n/2-t*Math.log(2)-cn(t))}l(QC,"chi2PdfScalar");function jU(n,e){let t=xa(n),r=2/(9*e),s=1-r+t*Math.sqrt(r);return Math.max(e*s*s*s,Number.EPSILON)}l(jU,"chi2Seed");var ep={cdf:l((n,e,t={})=>Ls(n,r=>ZC(r,e??t.df)),"cdf"),pdf:l((n,e,t={})=>Ls(n,r=>QC(r,e??t.df)),"pdf"),ppf:l((n,e,t={})=>{let r=e??t.df;return Ls(n,s=>Xk(o=>ZC(o,r),o=>QC(o,r),s,jU(s,r),{...t,lowerLimit:0}))},"ppf")};function XC(n,e,t){return n<=0?0:Th(e/2,t/2,e*n/(e*n+t))}l(XC,"fCdfScalar");function eD(n,e,t){if(n<=0)return 0;let r=cn(e/2)+cn(t/2)-cn((e+t)/2);return Math.exp(e/2*Math.log(e/t)+(e/2-1)*Math.log(n)-(e+t)/2*Math.log(1+e/t*n)-r)}l(eD,"fPdfScalar");var nD={cdf:l((n,e,t,r={})=>Ls(n,s=>XC(s,e??r.d1,t??r.d2)),"cdf"),pdf:l((n,e,t,r={})=>Ls(n,s=>eD(s,e??r.d1,t??r.d2)),"pdf"),ppf:l((n,e,t,r={})=>{let s=e??r.d1,o=t??r.d2;return Ls(n,i=>Xk(a=>XC(a,s,o),a=>eD(a,s,o),i,1,{...r,lowerLimit:0}))},"ppf")};function eN(n,e,t={}){let r=e.length,s=t.maxIter??200*r,o=t.tol??1e-10,i=t.initialStep??.05,a=t.zeroStep??25e-5,u=t.alpha??1,p=t.gamma??2,c=t.rho??.5,f=t.sigma??.5,d=[e.slice()];for(let _=0;_<r;_++){let b=e.slice();b[_]=b[_]!==0?b[_]*(1+i):a,d.push(b)}let h=d.map(n),m=Array.from({length:r+1},(_,b)=>b),g=0;for(;g<s;g++){m.sort((v,A)=>h[v]-h[A]);let _=m.map(v=>d[v]),b=m.map(v=>h[v]);for(let v=0;v<=r;v++)d[v]=_[v],h[v]=b[v];if(Math.abs(h[r]-h[0])<o)break;let w=new Array(r).fill(0);for(let v=0;v<r;v++)for(let A=0;A<r;A++)w[A]+=d[v][A];for(let v=0;v<r;v++)w[v]/=r;let x=d[r],S=w.map((v,A)=>v+u*(v-x[A])),T=n(S);if(T<h[0]){let v=w.map((N,k)=>N+p*(S[k]-N)),A=n(v);A<T?(d[r]=v,h[r]=A):(d[r]=S,h[r]=T)}else if(T<h[r-1])d[r]=S,h[r]=T;else{let v=w.map((N,k)=>N+c*(x[k]-N)),A=n(v);if(A<h[r])d[r]=v,h[r]=A;else{let N=d[0];for(let k=1;k<=r;k++)d[k]=N.map((O,R)=>O+f*(d[k][R]-O)),h[k]=n(d[k])}}}let y=0;for(let _=1;_<=r;_++)h[_]<h[y]&&(y=_);return{point:d[y],value:h[y],iterations:g,converged:g<s}}l(eN,"nelderMead");function Cs(n,e){let t=0;for(let r=0;r<n.length;r++)t+=n[r]*e[r];return t}l(Cs,"dot");function Pu(n){return Math.sqrt(Cs(n,n))}l(Pu,"norm");function Lw(n,e){let t=new Array(n.length);for(let r=0;r<n.length;r++)t[r]=n[r]-e[r];return t}l(Lw,"sub");function rD(n,e){let t=new Array(n.length);for(let r=0;r<n.length;r++)t[r]=n[r]*e;return t}l(rD,"scale");function sD(n){let e=0;for(let t=1;t<n.length;t++)n[t]<n[e]&&(e=t);return e}l(sD,"argmin");function oD(n,e,t){let r=e.length,s=new Array(r),o=e.slice();for(let i=0;i<r;i++){let a=o[i];o[i]=a+t;let u=n(o);o[i]=a-t;let p=n(o);o[i]=a,s[i]=(u-p)/(2*t)}return s}l(oD,"numericGradient");function $u(n,e){return e?n.map((t,r)=>{let s=e[r];if(!s)return t;let[o,i]=s;return t<o?o:t>i?i:t}):n.slice()}l($u,"clampToBounds");var UU=10,qU=5,WU=.8,KU=.9,HU=1e3,YU=1e-10;function tN(n,e,t){let r;do r=rc(n,e);while(t.includes(r));return r}l(tN,"pickDistinct");function nN(n,e,t={}){let r=e.length,s=xr(t.seed),o=t.populationSize??Math.max(qU,UU*r),i=t.mutation??WU,a=t.recombination??KU,u=t.maxIter??HU,p=t.tol??YU,c=[];for(let m=0;m<o;m++){let g=new Array(r);for(let y=0;y<r;y++){let[_,b]=e[y];g[y]=_+s()*(b-_)}c.push(g)}let f=c.map(n),d=sD(f),h=0;for(;h<u;h++){for(let y=0;y<o;y++){let _=tN(s,o,[y]),b=tN(s,o,[y,_]),w=tN(s,o,[y,_,b]),x=rc(s,r),S=c[y].slice();for(let v=0;v<r;v++)if(s()<a||v===x){let[A,N]=e[v],k=c[_][v]+i*(c[b][v]-c[w][v]);k<A?k=A:k>N&&(k=N),S[v]=k}let T=n(S);T<f[y]&&(c[y]=S,f[y]=T,T<f[d]&&(d=y))}let m=f[0],g=f[0];for(let y=1;y<o;y++)f[y]<m&&(m=f[y]),f[y]>g&&(g=f[y]);if(g-m<p){h++;break}}return{point:c[d].slice(),value:f[d],iterations:h,converged:h<u}}l(nN,"differentialEvolution");var JU=10,ZU=1e3,QU=1e-8,XU=1e-12,eq=1e-4,tq=.1,iD=.5,nq=40;function rq(n,e,t,r){let s=n.slice(),o=e.length,i=new Array(o);for(let a=o-1;a>=0;a--){i[a]=r[a]*Cs(e[a],s);for(let u=0;u<s.length;u++)s[u]-=i[a]*t[a][u]}if(o>0){let a=e[o-1],u=t[o-1],p=Cs(a,u)/Cs(u,u);for(let c=0;c<s.length;c++)s[c]*=p}for(let a=0;a<o;a++){let u=r[a]*Cs(t[a],s);for(let p=0;p<s.length;p++)s[p]+=(i[a]-u)*e[a][p]}return s}l(rq,"twoLoop");function aD(n,e,t,r){let s=e.length,o=t.memory??JU,i=t.maxIter??ZU,a=t.gtol??QU,u=t.ftol??XU,p=t.step??1e-7,c=t.gradient??(x=>oD(n,x,p)),f=l((x,S)=>r?x.map((T,v)=>{let A=r[v];if(!A)return T;let[N,k]=A;return S[v]<=N&&T>0||S[v]>=k&&T<0?0:T}):x,"projectGrad"),d=$u(e,r),h=n(d),m=c(d),g=f(m,d),y=[],_=[],b=[],w=0;for(;w<i;w++){if(Pu(g)<a)return{point:d,value:h,iterations:w,converged:!0};let x=rD(rq(g,y,_,b),-1);if(Cs(x,g)>=0)for(let H=0;H<s;H++)x[H]=-g[H];let S=1,T=d,v=h,A=!1,N=!1,k=Cs(g,x);for(let H=0;H<nq;H++){let G=d.map((j,W)=>j+S*x[W]),U=$u(G,r),D=n(U);if(D<=h+eq*S*k){T=U,v=D,A=!0,N=r!=null&&U.some((j,W)=>j!==G[W]);break}let V=2*(D-h-k*S),ue=V>0?-k*S*S/V:S*iD,ce=tq*S,Le=iD*S;S=ue<ce?ce:ue>Le?Le:ue}if(!A)return{point:d,value:h,iterations:w,converged:!1};let O=c(T),R=f(O,T),B=Lw(T,d),E=N?Lw(R,g):Lw(O,m),F=Cs(B,E);F>Number.EPSILON*Pu(B)*Pu(E)&&(y.push(B),_.push(E),b.push(1/F),y.length>o&&(y.shift(),_.shift(),b.shift()));let C=Math.abs(v-h);if(d=T,h=v,m=O,g=R,C<u*(1+Math.abs(h))&&Pu(B)<u*(1+Pu(d)))return w++,{point:d,value:h,iterations:w,converged:!0}}return{point:d,value:h,iterations:w,converged:Pu(g)<a}}l(aD,"minimize");function Sh(n,e,t={}){return aD(n,e,t,null)}l(Sh,"lbfgs");function sN(n,e,t,r={}){return aD(n,e,r,t)}l(sN,"lbfgsB");var sq=200,oq=1e-10,iq=.001,uD=10,aq=10,uq=1e12;function lq(n,e,t,r){let s=e.length,o=new Array(t);for(let a=0;a<t;a++)o[a]=new Array(s);let i=e.slice();for(let a=0;a<s;a++){let u=i[a];i[a]=u+r;let p=n(i);i[a]=u-r;let c=n(i);i[a]=u;for(let f=0;f<t;f++)o[f][a]=(p[f]-c[f])/(2*r)}return o}l(lq,"numericJacobian");function cq(n,e,t,r){let s=new Float64Array(r*r);for(let a=0;a<r;a++){for(let u=0;u<r;u++)s[a*r+u]=n[a][u];s[a*r+a]+=t*n[a][a]}let o=Float64Array.from(e),i=_h(Q(s,[r,r],"f64"),Q(o,[r],"f64"));return Array.from(i.toArray())}l(cq,"solveNormal");function oN(n,e,t={}){let r=t.maxIter??sq,s=t.tol??oq,o=t.step??1e-7,i=t.jacobian??((m,g)=>lq(n,m,g,o)),a=e.slice(),u=n(a),p=u.length,c=a.length,f=Cs(u,u),d=t.lambda??iq,h=0;for(;h<r;h++){let m=i(a,p),g=new Array(c),y=new Array(c).fill(0);for(let b=0;b<c;b++){g[b]=new Array(c).fill(0);for(let w=0;w<p;w++)y[b]+=m[w][b]*u[w]}for(let b=0;b<c;b++)for(let w=b;w<c;w++){let x=0;for(let S=0;S<p;S++)x+=m[S][b]*m[S][w];g[b][w]=x,g[w][b]=x}let _=!1;for(;d<uq;){let b;try{b=cq(g,y,d,c)}catch{d*=uD;continue}let w=a.map((T,v)=>T-b[v]),x=n(w),S=Cs(x,x);if(S<f){a=w,u=x;let T=f-S;if(f=S,d/=aq,_=!0,T<s*(1+f))return{point:a,value:f,iterations:h+1,converged:!0};break}d*=uD}if(!_)return{point:a,value:f,iterations:h,converged:f<s}}return{point:a,value:f,iterations:h,converged:!1}}l(oN,"levenbergMarquardt");var pq=10,fq=10,dq=20,hq=1e-8;function iN(n,e,t={}){let r=t.bounds??null,s=t.inequalities??[],o=t.equalities??[],i=t.outerIter??dq,a=t.ctol??hq,u=t.penaltyGrowth??fq,p=t.inner??Sh,c=t.innerOpts??{},f=t.penalty??pq,d=$u(e,r),h=0,m=null;for(let g=0;g<i;g++){m=p(l(b=>{let w=$u(b,r),x=n(w);for(let S of s){let T=S(w);T>0&&(x+=f*T*T)}for(let S of o){let T=S(w);x+=f*T*T}return x},"penalized"),d,c),d=$u(m.point,r),h+=m.iterations;let _=0;for(let b of s)_=Math.max(_,b(d));for(let b of o)_=Math.max(_,Math.abs(b(d)));if(_<a)return{point:d,value:n(d),iterations:h,converged:!0};f*=u}return{point:d,value:n(d),iterations:h,converged:!1}}l(iN,"constrainedMinimize");function lD(n,e,t,r={}){let s=r.n??1024,o=(t-e)/s,i=.5*(n(e)+n(t));for(let a=1;a<s;a++)i+=n(e+a*o);return i*o}l(lD,"trapezoid");function cD(n,e,t,r={}){let s=r.n??1024;s%2===1&&(s+=1);let o=(t-e)/s,i=n(e)+n(t);for(let a=1;a<s;a++)i+=(a%2===0?2:4)*n(e+a*o);return i*o/3}l(cD,"simpson");function aN(n,e,t,r,s){return(s-r)/6*(n+4*t+e)}l(aN,"simpsonCell");function uN(n,e,t,r,s,o,i,a,u,p){let c=.5*(e+t),f=.5*(e+c),d=.5*(c+t),h=n(f),m=n(d),g=aN(r,o,h,e,c),y=aN(o,s,m,c,t),_=g+y-i;return u>=p||Math.abs(_)<=15*a?g+y+_/15:uN(n,e,c,r,o,h,g,.5*a,u+1,p)+uN(n,c,t,o,s,m,y,.5*a,u+1,p)}l(uN,"adaptiveStep");function pD(n,e,t,r={}){let s=r.tol??1e-10,o=r.maxDepth??50,i=.5*(e+t),a=n(e),u=n(t),p=n(i),c=aN(a,u,p,e,t);return uN(n,e,t,a,u,p,c,s,0,o)}l(pD,"quadrature");function dD(n,e){let t=1,r=n.length-1;for(;t<r;){let s=t+r>>1;n[s]<e?t=s+1:r=s}return t}l(dD,"upperInterval");function fD(n,e,t){let r=n.length;if(t<=n[0])return e[0];if(t>=n[r-1])return e[r-1];let s=dD(n,t),o=(t-n[s-1])/(n[s]-n[s-1]);return e[s-1]+o*(e[s]-e[s-1])}l(fD,"evalLinear");function hD(n,e,t){return typeof t!="number"?t.map(r=>fD(n,e,r)):fD(n,e,t)}l(hD,"linearInterp");function mq(n,e,t,r){let s=e.length,o=new Array(s),i=new Array(s);o[0]=t[0]/e[0],i[0]=r[0]/e[0];for(let u=1;u<s;u++){let p=e[u]-n[u]*o[u-1];o[u]=t[u]/p,i[u]=(r[u]-n[u]*i[u-1])/p}let a=new Array(s);a[s-1]=i[s-1];for(let u=s-2;u>=0;u--)a[u]=i[u]-o[u]*a[u+1];return a}l(mq,"thomas");function mD(n,e){let t=n.length,r=new Array(t-1);for(let c=0;c<t-1;c++)r[c]=n[c+1]-n[c];let s=new Array(t).fill(0),o=new Array(t).fill(1),i=new Array(t).fill(0),a=new Array(t).fill(0);for(let c=1;c<t-1;c++)s[c]=r[c-1],o[c]=2*(r[c-1]+r[c]),i[c]=r[c],a[c]=6*((e[c+1]-e[c])/r[c]-(e[c]-e[c-1])/r[c-1]);let u=mq(s,o,i,a),p=l(c=>{if(c<=n[0])return e[0];if(c>=n[t-1])return e[t-1];let f=dD(n,c),d=r[f-1],h=(n[f]-c)/d,m=(c-n[f-1])/d;return h*e[f-1]+m*e[f]+((h*h*h-h)*u[f-1]+(m*m*m-m)*u[f])*d*d/6},"evalOne");return{xs:n,ys:e,coefficients:u,evaluate:l((c=>typeof c!="number"?c.map(p):p(c)),"evaluate")}}l(mD,"cubicSpline");be();var Fu=l(n=>ct("fft",n),"fft"),tp=l(n=>ct("ifft",n),"ifft");sr();wn();qn();function gD(n){let e=new Float64Array(n.length);for(let t=0;t<n.length;t++)e[t]=Number(n[t]);return e}l(gD,"float64From");function Je(n){return n instanceof Ze?{data:gD(Qe(n)),dtype:n.dtype,device:n.device}:typeof n=="number"?{data:Float64Array.of(n),dtype:"f64",device:Ws}:{data:Float64Array.from(n),dtype:"f64",device:Ws}}l(Je,"hostVector");function Cw(n){if(n instanceof Ze){if(n.ndim!==2)throw new Error(`numeric: expected a 2-D table, got ${n.ndim}-D`);let[s,o]=n.shape;return{data:gD(Qe(n)),rows:s,cols:o,dtype:n.dtype,device:n.device}}let e=n.length,t=n[0].length,r=new Float64Array(e*t);for(let s=0;s<e;s++)for(let o=0;o<t;o++)r[s*t+o]=n[s][o];return{data:r,rows:e,cols:t,dtype:"f64",device:Ws}}l(Cw,"hostGrid");function Dw(n){let e=1;for(;e<n;)e<<=1;return e}l(Dw,"nextPow2");var gq=100,yq=1e-16,_q=.12,bq=.11,yD=[.75,2.25],lN=[.2,.34,.6],wq=[-13.436,101.14,-223.73],Tq=[-8.318,42.796,-59.938],xq=[.9177,-4.279,-1.38],Sq=[1.2937,-5.709,.0186];function $w(n){let e=n.length,t=0;for(let o=0;o<e;o++)t+=n[o];let r=t/e,s=0;for(let o=0;o<e;o++){let i=n[o]-r;s+=i*i}return{n:e,mean:r,variance:s/(e-1)}}l($w,"summary");function pN(n){let e=n.length,t=0;for(let a=0;a<e;a++)t+=n[a];let r=t/e,s=0,o=0,i=0;for(let a=0;a<e;a++){let u=n[a]-r,p=u*u;s+=p,o+=p*u,i+=p*p}return s/=e,o/=e,i/=e,{n:e,mean:r,m2:s,skew:o/Math.pow(s,1.5),kurt:i/(s*s)}}l(pN,"centralMoments");function Sa(n){return Math.min(Math.max(n,0),1)}l(Sa,"clampP");function cN(n,e){return Sa(2*(1-Ew.cdf(Math.abs(n),e)))}l(cN,"tPvalue");function Fw(n,e){return Sa(1-ep.cdf(n,e))}l(Fw,"chi2Pvalue");function fN(n,e={}){let{data:t}=Je(n),r=e.popmean??0,{n:s,mean:o,variance:i}=$w(t),a=(o-r)/Math.sqrt(i/s),u=s-1;return{statistic:a,pvalue:cN(a,u),df:u}}l(fN,"tTest1Samp");function _D(n,e,t={}){let r=$w(Je(n).data),s=$w(Je(e).data);if(t.equalVar??!0){let p=r.n+s.n-2,c=((r.n-1)*r.variance+(s.n-1)*s.variance)/p,f=(r.mean-s.mean)/Math.sqrt(c*(1/r.n+1/s.n));return{statistic:f,pvalue:cN(f,p),df:p}}let o=r.variance/r.n,i=s.variance/s.n,a=(r.mean-s.mean)/Math.sqrt(o+i),u=(o+i)*(o+i)/(o*o/(r.n-1)+i*i/(s.n-1));return{statistic:a,pvalue:cN(a,u),df:u}}l(_D,"tTestInd");function bD(n,e,t={}){let r=Je(n).data,s=Je(e).data;if(r.length!==s.length)throw new Error(`tTestPaired: samples must have equal length, got ${r.length} and ${s.length}`);let o=new Float64Array(r.length);for(let i=0;i<r.length;i++)o[i]=r[i]-s[i];return fN(o,{popmean:t.popmean??0})}l(bD,"tTestPaired");function wD(n,e,t={}){let r=Je(n).data,s=r.length,o;if(e==null){let u=0;for(let p=0;p<s;p++)u+=r[p];o=new Float64Array(s).fill(u/s)}else o=Je(e).data;let i=0;for(let u=0;u<s;u++){let p=r[u]-o[u];i+=p*p/o[u]}let a=s-1-(t.ddof??0);return{statistic:i,pvalue:Fw(i,a),df:a}}l(wD,"chi2Gof");function TD(n){let{data:e,rows:t,cols:r}=Cw(n),s=new Float64Array(t),o=new Float64Array(r),i=0;for(let p=0;p<t;p++)for(let c=0;c<r;c++){let f=e[p*r+c];s[p]+=f,o[c]+=f,i+=f}let a=0;for(let p=0;p<t;p++)for(let c=0;c<r;c++){let f=s[p]*o[c]/i,d=e[p*r+c]-f;a+=d*d/f}let u=(t-1)*(r-1);return{statistic:a,pvalue:Fw(a,u),df:u}}l(TD,"chi2Independence");function vq(n){if(n<=0)return 1;let e=0;for(let t=1;t<=gq;t++){let r=Math.exp(-2*t*t*n*n);if(e+=t%2===1?r:-r,r<yq)break}return Sa(2*e)}l(vq,"kolmogorovSf");function xD(n,e){let t=Math.sqrt(e);return vq(n*(t+_q+bq/t))}l(xD,"ksPvalue");function SD(n,e,t={}){let{data:r}=Je(n),s=Float64Array.from(r).sort(),o=s.length,i=t.loc??0,a=t.scale??1,u=typeof e=="function"?e:c=>Uo((c-i)/a),p=0;for(let c=0;c<o;c++){let f=u(s[c]);p=Math.max(p,(c+1)/o-f,f-c/o)}return{statistic:p,pvalue:xD(p,o)}}l(SD,"ksTest1Samp");function vD(n,e){let t=Float64Array.from(Je(n).data).sort(),r=Float64Array.from(Je(e).data).sort(),s=t.length,o=r.length,i=0,a=0,u=0;for(;i<s&&a<o;){let c=Math.min(t[i],r[a]);for(;i<s&&t[i]<=c;)i++;for(;a<o&&r[a]<=c;)a++;u=Math.max(u,Math.abs(i/s-a/o))}let p=s*o/(s+o);return{statistic:u,pvalue:xD(u,p)}}l(vD,"ksTest2Samp");function kD(n){let{n:e,skew:t,kurt:r}=pN(Je(n).data),s=e/6*(t*t+(r-3)*(r-3)/4);return{statistic:s,pvalue:Fw(s,2),df:2}}l(kD,"jarqueBera");function kq(n){let{n:e,skew:t}=pN(n),r=t*Math.sqrt((e+1)*(e+3)/(6*(e-2))),s=3*(e*e+27*e-70)*(e+1)*(e+3)/((e-2)*(e+5)*(e+7)*(e+9)),o=-1+Math.sqrt(2*(s-1)),i=1/Math.sqrt(.5*Math.log(o)),a=Math.sqrt(2/(o-1)),u=r/a;return i*Math.log(u+Math.sqrt(u*u+1))}l(kq,"skewZ");function Nq(n){let{n:e,kurt:t}=pN(n),r=3*(e-1)/(e+1),s=24*e*(e-2)*(e-3)/((e+1)*(e+1)*(e+3)*(e+5)),o=(t-r)/Math.sqrt(s),i=6*(e*e-5*e+2)/((e+7)*(e+9))*Math.sqrt(6*(e+3)*(e+5)/(e*(e-2)*(e-3))),a=6+8/i*(2/i+Math.sqrt(1+4/(i*i))),u=1-2/(9*a),p=1+o*Math.sqrt(2/(a-4));return(u-Math.cbrt((1-2/a)/p))/Math.sqrt(2/(9*a))}l(Nq,"kurtosisZ");function ND(n){let{data:e}=Je(n),t=kq(e),r=Nq(e),s=t*t+r*r;return{statistic:s,pvalue:Fw(s,2),df:2}}l(ND,"dagostinoK2");function Pw(n,e){return n[0]+n[1]*e+n[2]*e*e}l(Pw,"poly2");function Aq(n){return n>=lN[2]?Sa(Math.exp(Pw(Sq,n))):n>lN[1]?Sa(Math.exp(Pw(xq,n))):n>lN[0]?Sa(1-Math.exp(Pw(Tq,n))):Sa(1-Math.exp(Pw(wq,n)))}l(Aq,"adPvalue");function AD(n){let{data:e}=Je(n),t=Float64Array.from(e).sort(),r=t.length,{mean:s,variance:o}=$w(t),i=Math.sqrt(o),a=0;for(let c=0;c<r;c++){let f=Uo((t[c]-s)/i),d=Uo(-(t[r-1-c]-s)/i);a+=(2*c+1)*(Math.log(f)+Math.log(d))}let u=-r-a/r,p=u*(1+yD[0]/r+yD[1]/(r*r));return{statistic:u,pvalue:Aq(p)}}l(AD,"andersonDarling");function Oq(n){let e=n.length,t=Array.from({length:e},(i,a)=>a);t.sort((i,a)=>n[i]-n[a]);let r=new Float64Array(e),s=0,o=0;for(;o<e;){let i=o;for(;i+1<e&&n[t[i+1]]===n[t[o]];)i++;let a=(o+i+2)/2;for(let p=o;p<=i;p++)r[t[p]]=a;let u=i-o+1;s+=u*u*u-u,o=i+1}return{ranks:r,tieSum:s}}l(Oq,"rankData");function OD(n,e){let t=Je(n).data,r=Je(e).data,s=t.length,o=r.length,i=new Float64Array(s+o);i.set(t),i.set(r,s);let{ranks:a,tieSum:u}=Oq(i),p=0;for(let y=0;y<s;y++)p+=a[y];let c=p-s*(s+1)/2,f=s+o,d=s*o/2,h=Math.sqrt(s*o/12*(f+1-u/(f*(f-1)))),m=c-d,g=(m-.5*Math.sign(m))/h;return{statistic:c,pvalue:Sa(2*(1-Uo(Math.abs(g))))}}l(OD,"mannWhitneyU");wn();function Ai(n,e){return Number(n[e])}l(Ai,"f64");function BD(n){return Math.max(1,Math.min(n-1,Math.floor(10*Math.log10(n))))}l(BD,"defaultNlags");function Bq(n,e,t){let r=n.length,s=0;for(let d=0;d<r;d++)s+=n[d];let o=s/r,i=Dw(2*r),a=new Float64Array(i);for(let d=0;d<r;d++)a[d]=n[d]-o;let u=Qe(Fu(Q(a,[i],"f64",e))),p=new Float64Array(2*i);for(let d=0;d<i;d++)p[2*d]=Ai(u,2*d)*Ai(u,2*d)+Ai(u,2*d+1)*Ai(u,2*d+1);let c=Qe(tp(Q(p,[i,2],"f64",e))),f=new Float64Array(t+1);for(let d=0;d<=t;d++)f[d]=Ai(c,2*d)/r;return f}l(Bq,"autocovariance");function dN(n,e,t){let r=Bq(n,e,t),s=new Float64Array(t+1);for(let o=0;o<=t;o++)s[o]=r[o]/r[0];return s}l(dN,"normalizedAcf");function RD(n,e={}){let{data:t,device:r,dtype:s}=Je(n),o=e.nlags??BD(t.length),i=dN(t,r,o);return Q(i,[o+1],s,r)}l(RD,"acf");function MD(n,e={}){let{data:t,device:r,dtype:s}=Je(n),o=e.nlags??BD(t.length),i=dN(t,r,o),a=new Float64Array(o+1);a[0]=1;let u=new Float64Array(o+1),p=new Float64Array(o+1);for(let c=1;c<=o;c++){let f=i[c],d=1;for(let g=1;g<c;g++)f-=u[g]*i[c-g],d-=u[g]*i[g];let h=f/d;p[c]=h;for(let g=1;g<c;g++)p[g]=u[g]-h*u[c-g];a[c]=h;let m=u;u=p,p=m}return Q(a,[o+1],s,r)}l(MD,"pacf");function ID(n,e={}){let{data:t,device:r}=Je(n),s=t.length,o=e.lags??Math.min(10,s-1),i=dN(t,r,o),a=0;for(let p=1;p<=o;p++)a+=i[p]*i[p]/(s-p);a*=s*(s+2);let u=o-(e.modelDf??0);return{statistic:a,pvalue:Math.min(Math.max(1-ep.cdf(a,u),0),1),df:u}}l(ID,"ljungBox");function ED(n){let{data:e}=Je(n),t=0,r=0;for(let s=0;s<e.length;s++)if(r+=e[s]*e[s],s>0){let o=e[s]-e[s-1];t+=o*o}return t/r}l(ED,"durbinWatson");function LD(n,e={}){let{data:t,device:r,dtype:s}=Je(n),o=t.length,i=t;if(e.detrend!==!1){let c=0;for(let d=0;d<o;d++)c+=t[d];let f=c/o;i=new Float64Array(o);for(let d=0;d<o;d++)i[d]=t[d]-f}let a=Qe(Fu(Q(i,[o],"f64",r))),u=Math.floor(o/2),p=new Float64Array(u+1);for(let c=0;c<=u;c++)p[c]=(Ai(a,2*c)*Ai(a,2*c)+Ai(a,2*c+1)*Ai(a,2*c+1))/o;return Q(p,[u+1],s,r)}l(LD,"periodogram");wn();var Rq=200,Mq=1e-13,CD=.4,DD=.9;function vh(n,e){return Number(n[e])}l(vh,"f64");function Iq(n){let e=new Float64Array(n.length);for(let t=0;t<n.length;t++)e[t]=Number(n[t]);return e}l(Iq,"float64From");function PD(n,e,t){let r=new Float64Array(e);return r.set(n),Qe(Fu(Q(r,[e],"f64",t)))}l(PD,"fftInterleaved");function Eq(n,e,t){let r=n+e-1;if(t==="full")return{offset:0,length:r};let s=Math.max(n,e),o=Math.min(n,e);if(t==="same")return{offset:o-1>>1,length:s};if(t==="valid")return{offset:o-1,length:s-o+1};throw new Error(`convolve: unknown mode '${t}', expected 'full', 'same', or 'valid'`)}l(Eq,"modeSlice");function FD(n,e,t,r,s){let o=n.length,i=e.length,a=Dw(o+i-1),u=PD(n,a,r),p=PD(e,a,r),c=new Float64Array(2*a);for(let g=0;g<a;g++){let y=vh(u,2*g),_=vh(u,2*g+1),b=vh(p,2*g),w=vh(p,2*g+1);c[2*g]=y*b-_*w,c[2*g+1]=y*w+_*b}let f=Qe(tp(Q(c,[a,2],"f64",r))),{offset:d,length:h}=Eq(o,i,t),m=new Float64Array(h);for(let g=0;g<h;g++)m[g]=vh(f,2*(d+g));return Q(m,[h],s,r)}l(FD,"fftConvolve");function zD(n,e,t={}){let r=Je(n),s=Je(e);return FD(r.data,s.data,t.mode??"full",r.device,r.dtype)}l(zD,"convolve");function VD(n,e,t={}){let r=Je(n),s=Je(e),o=new Float64Array(s.data.length);for(let i=0;i<s.data.length;i++)o[i]=s.data[s.data.length-1-i];return FD(r.data,o,t.mode??"full",r.device,r.dtype)}l(VD,"correlate");function zw(n,e){let t=Math.floor(n);if(!(t>=1&&t<=e))throw new Error(`rolling: window must be in [1, ${e}], got ${n}`);return t}l(zw,"checkWindow");function GD(n,e){let{data:t,device:r,dtype:s}=Je(n),o=t.length,i=zw(e,o),a=new Float64Array(o+1);for(let p=0;p<o;p++)a[p+1]=a[p]+t[p];let u=new Float64Array(o-i+1);for(let p=0;p<u.length;p++)u[p]=a[p+i]-a[p];return Q(u,[u.length],s,r)}l(GD,"rollingSum");function jD(n,e){let{data:t,device:r,dtype:s}=Je(n),o=t.length,i=zw(e,o),a=new Float64Array(o+1);for(let p=0;p<o;p++)a[p+1]=a[p]+t[p];let u=new Float64Array(o-i+1);for(let p=0;p<u.length;p++)u[p]=(a[p+i]-a[p])/i;return Q(u,[u.length],s,r)}l(jD,"rollingMean");function UD(n,e,t={}){let{data:r,device:s,dtype:o}=Je(n),i=r.length,a=zw(e,i),u=t.ddof??1,p=0;for(let m=0;m<i;m++)p+=r[m];let c=p/i,f=new Float64Array(i+1),d=new Float64Array(i+1);for(let m=0;m<i;m++){let g=r[m]-c;f[m+1]=f[m]+g,d[m+1]=d[m]+g*g}let h=new Float64Array(i-a+1);for(let m=0;m<h.length;m++){let g=f[m+a]-f[m],y=d[m+a]-d[m];h[m]=Math.sqrt(Math.max((y-g*g/a)/(a-u),0))}return Q(h,[h.length],o,s)}l(UD,"rollingStd");function qD(n,e,t){let{data:r,device:s,dtype:o}=Je(n),i=r.length,a=zw(e,i),u=new Float64Array(i-a+1),p=new Int32Array(i),c=0,f=0;for(let d=0;d<i;d++){for(;f>c&&t(r[d],r[p[f-1]]);)f--;p[f++]=d,p[c]<=d-a&&c++,d>=a-1&&(u[d-a+1]=r[p[c]])}return Q(u,[u.length],o,s)}l(qD,"rollingExtreme");function WD(n,e){return qD(n,e,(t,r)=>t<=r)}l(WD,"rollingMin");function KD(n,e){return qD(n,e,(t,r)=>t>=r)}l(KD,"rollingMax");function HD(n,e,t){let r=Je(n),s=Je(e),o=r.data.length;if(s.data.length!==o)throw new Error(`polyfit: x and y must have equal length, got ${o} and ${s.data.length}`);let i=t+1,a=new Float64Array(o*i);for(let p=0;p<o;p++){a[p*i+t]=1;for(let c=t-1;c>=0;c--)a[p*i+c]=a[p*i+c+1]*r.data[p]}let u=bh(Q(a,[o,i],"f64",r.device),Q(s.data,[o,1],"f64",r.device));return Q(Iq(Qe(u)),[i],"f64",r.device)}l(HD,"polyfit");function $D(n,e){let t=0;for(let r=0;r<n.length;r++)t=t*e+n[r];return t}l($D,"horner");function YD(n,e){let t=Je(n).data;if(typeof e=="number")return $D(t,e);let r=Je(e),s=new Float64Array(r.data.length);for(let o=0;o<s.length;o++)s[o]=$D(t,r.data[o]);return Q(s,[s.length],r.dtype,r.device)}l(YD,"polyval");function JD(n){let{data:e,device:t}=Je(n),r=0;for(;r<e.length-1&&e[r]===0;)r++;let s=e.length-1-r;if(s<1)return Q(new Float64Array(0),[0,2],"f64",t);let o=new Float64Array(s+1);for(let f=0;f<=s;f++)o[f]=e[r+f]/e[r];let i=new Float64Array(s),a=new Float64Array(s),u=1,p=0;for(let f=0;f<s;f++){let d=u*CD-p*DD;p=u*DD+p*CD,u=d,i[f]=u,a[f]=p}for(let f=0;f<Rq;f++){let d=0;for(let h=0;h<s;h++){let m=1,g=0;for(let S=1;S<=s;S++){let T=m*i[h]-g*a[h]+o[S];g=m*a[h]+g*i[h],m=T}let y=1,_=0;for(let S=0;S<s;S++){if(S===h)continue;let T=i[h]-i[S],v=a[h]-a[S],A=y*T-_*v;_=y*v+_*T,y=A}let b=y*y+_*_,w=(m*y+g*_)/b,x=(g*y-m*_)/b;i[h]-=w,a[h]-=x,d=Math.max(d,Math.hypot(w,x))}if(d<Mq)break}let c=new Float64Array(2*s);for(let f=0;f<s;f++)c[2*f]=i[f],c[2*f+1]=a[f];return Q(c,[s,2],"f64",t)}l(JD,"polyroots");be();var Lq=.0331;function Cq(n){let e=Array.isArray(n)?n:[n],t=1;for(let r of e)t*=r;return{shp:e,n:t}}l(Cq,"normalizeShape");var Vw=class{static{l(this,"Generator")}_next;constructor(e){this._next=xr(e)}_uniformPositive(){let e=this._next();for(;e<=0;)e=this._next();return e}_normalDraw(){return xa(this._uniformPositive())}_gammaDraw(e){if(e<1)return this._gammaDraw(e+1)*Math.pow(this._uniformPositive(),1/e);let t=e-1/3,r=1/Math.sqrt(9*t);for(;;){let s,o;do s=this._normalDraw(),o=1+r*s;while(o<=0);o=o*o*o;let i=this._uniformPositive();if(i<1-Lq*s*s*s*s||Math.log(i)<.5*s*s+t*(1-o+Math.log(o)))return t*o}}_fill(e,t,r){let{shp:s,n:o}=Cq(e),i=new Float64Array(o);for(let a=0;a<o;a++)i[a]=r();return Q(i,s,t.dtype??"f64",t.device)}uniform(e,t={}){let r=t.low??0,s=t.high??1;return this._fill(e,t,()=>r+(s-r)*this._next())}normal(e,t={}){let r=t.loc??0,s=t.scale??1;return this._fill(e,t,()=>r+s*this._normalDraw())}standardT(e,t={}){let r=t.df;return this._fill(e,t,()=>{let s=this._normalDraw(),o=2*this._gammaDraw(r/2);return s/Math.sqrt(o/r)})}chi2(e,t={}){let r=t.df;return this._fill(e,t,()=>2*this._gammaDraw(r/2))}exponential(e,t={}){let r=t.scale??1;return this._fill(e,t,()=>-r*Math.log(this._uniformPositive()))}multivariateNormal(e,t,r=1,s={}){let o=Je(e),i=Cw(t);if(i.rows!==i.cols||i.rows!==o.data.length)throw new Error(`multivariateNormal: mean of length ${o.data.length} incompatible with ${i.rows}x${i.cols} covariance`);let a=i.rows,u=s.device??i.device,p=s.dtype??"f64",c=Ok(Q(i.data,[a,a],p,u)),f=this.normal([r,a],{device:u,dtype:p}),d=Q(o.data,[a],p,u);return ae(tt(f,c.transpose(0,1)),d)}};jM();iI();hI();vI();FI();KI();nE();rE(Ze);export{dh as Accuracy,bd as Adam,wd as AdamW,Xf as AdaptiveAvgPool2d,Qf as AvgPool2d,rd as BCELoss,cc as BatchNorm1d,Jf as BatchNorm2d,vu as BatchSampler,go as CPUTarget,Ws as CPU_DEVICE,lh as CSVLogger,xm as CUDATarget,Zn as Callback,yh as ConfusionMatrix,Du as ConsoleLogger,zf as Conv1d,Ff as Conv2d,xd as CosineAnnealingLR,td as CrossEntropyLoss,md as DataLoader,Tu as Dataset,Sr as Dropout,Kf as ELU,oh as EarlyStopping,sd as Embedding,fE as F,gh as F1Score,cd as Flatten,Gf as GELU,bg as GPU_DEVICE,id as GRU,pc as GRUCell,Oo as GradMode,uh as GradientAccumulationScheduler,Au as LRScheduler,ad as LSTM,fc as LSTMCell,no as LayerNorm,Wf as LeakyReLU,ih as LearningRateMonitor,th as LightningModule,Sn as Linear,Yf as LogSoftmax,ki as Logger,ed as MSELoss,hd as MapDataset,Zf as MaxPool2d,ch as MeanMetric,lr as Metric,fh as MetricCollection,Ta as ModelCheckpoint,ge as Module,ld as ModuleDict,aa as ModuleList,ua as MultiheadAttention,nd as NLLLoss,fs as Optimizer,At as Parameter,fd as PositionalEncoding,hh as Precision,wa as ProgressCallback,Su as RandomSampler,Vf as ReLU,mh as Recall,Sd as ReduceLROnPlateau,_d as SGD,la as Sampler,ud as Sequential,xu as SequentialSampler,jf as SiLU,Uf as Sigmoid,Hf as Softmax,Td as StepLR,ph as SumMetric,bt as SymbolicTensor,qf as Tanh,Ze as Tensor,dd as TensorDataset,ah as Timer,yd as Tokenizer,le as TraceLevel,sh as Trainer,pd as Transformer,hc as TransformerDecoder,wu as TransformerDecoderLayer,dc as TransformerEncoder,bu as TransformerEncoderLayer,ca as Vocab,RT as WASM_DEVICE,xo as WEBGPU_DEVICE,Sm as WasmTarget,vm as WebGPUTarget,Hp as abs,ae as add,Sk as applyCheckpoint,RB as arange,Cg as argmax,iB as argmin,gB as argsort,cB as broadcast_in_dim,ru as cat,nB as ceil,pB as chunk,Mg as clamp,vd as clipGradNorm_,kd as clipGradValue_,lB as clone,eU as compile,_w as compileWithBackward,ls as contiguous,ZO as cos,hB as cumsum,hE as data,ku as defaultCollate,Lt as dispatcher,Kt as div,uB as dot,Tn as empty,$G as emptyLike,KB as enableGrad,Yp as eq,QO as erf,XO as erfc,as as exp,Ll as expand,jg as eye,dB as flip,tB as floor,HR as flushWebGPUEager,eu as fromBuffer,ot as full,VG as fullLike,kg as gamma,Eg as gather,Rg as ge,Ng as gelu,ea as getDefaultDevice,Il as gt,ra as index_select,T_ as init,Bg as le,eB as lgamma,$C as lightning,FC as linalg,IB as linspace,xk as loadCheckpoint,ko as log,$g as log_softmax,sB as lt,tt as matmul,No as max,ta as maximum,Tr as mean,vn as memfs,oB as min,vg as minimum,jC as ml,ee as mul,ir as narrow,rB as ne,is as neg,dE as nn,WB as noGrad,ZD as numeric,Ig as one_hot,tn as ones,zG as onesLike,Ht as ops,tC as optim,tu as pad,El as permute,vo as pow,ZR as preloadCudaRuntime,gf as preloadWebGPU,PG as printModule,aB as prod,Gg as randn,GG as randnLike,MB as randperm,Ml as relu,Dl as repeat,Hn as reshape,fB as roll,YO as rsqrt,Kp as scalar,od as scan,Lg as scatter,nu as scatter_add,en as select,rh as serializeCheckpoint,GF as setDefaultDevice,wr as sigmoid,Og as sign,Ag as silu,JO as sin,Dg as slice,mi as softmax,mB as sort,hi as split,na as sqrt,Cl as squeeze,us as stack,ze as sub,wt as sum,Hs as tanh,Wt as tensor,Pg as tile,TE as tokenizer,yB as topk,kC as trace,vt as transpose,Lr as unsqueeze,or as where,Ve as zeros,FG as zerosLike};
|