@slexisvn/mlfw 0.1.1 → 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.
@@ -0,0 +1,284 @@
1
+ var SS=Object.defineProperty;var l=(r,e)=>SS(r,"name",{value:e,configurable:!0});var M=(r,e)=>()=>(r&&(e=r(r=0)),e);var nt=(r,e)=>{for(var t in e)SS(r,t,{get:e[t],enumerable:!0})};function oh(r,e){let t=e||"mlc",n=r.indexOf("->"),s=n>=0?r.substring(0,n).trim():r.trim(),o=n>=0?r.substring(n+2).trim():"",i=s.indexOf("("),a=s.lastIndexOf(")"),u,p="",c=s.substring(0,i).trim(),h=c.indexOf(".");h>=0?(u=c.substring(0,h),p=c.substring(h+1)):u=c;let f=s.substring(i+1,a).trim(),d=f.length>0?ME(f):[],m=o.length>0?IE(o):[{kind:"Tensor"}];return new sb(t,u,p,d,m)}function ME(r){return kS(r,",").map(t=>{let n=t.trim(),s=n.indexOf("="),o=n,i;s>=0&&(o=n.substring(0,s).trim(),i=n.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 h=ob.get(p)||"Scalar";return new rb(c,h,i,a)})}function IE(r){let e=r.trim();if(e.startsWith("(")&&e.endsWith(")")){let n=e.substring(1,e.length-1);return kS(n,",").map(o=>{let i=o.trim();return{kind:ob.get(i)||"Tensor"}})}return[{kind:ob.get(e)||"Tensor"}]}function kS(r,e){let t=[],n=0,s=0;for(let o=0;o<r.length;o++){let i=r[o];i==="("||i==="["?n++:i===")"||i==="]"?n--:i===e&&n===0&&(t.push(r.substring(s,o)),s=o+1)}return t.push(r.substring(s)),t}var RE,rb,sb,ob,ib=M(()=>{"use strict";RE=new Set(["Tensor","Tensor[]"]),rb=class{static{l(this,"SchemaArg")}name;kind;defaultValue;isOut;constructor(e,t,n,s){this.name=e,this.kind=t,this.defaultValue=n??null,this.isOut=s??!1}get isTensor(){return RE.has(this.kind)}},sb=class{static{l(this,"OperatorSchema")}namespace;name;overload;args;returns;_key;_tensorArgIndices;constructor(e,t,n,s,o){this.namespace=e,this.name=t,this.overload=n||"",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}},ob=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(oh,"parseSchema");l(ME,"_parseArgs");l(IE,"_parseReturns");l(kS,"_splitTopLevel")});function vS(r){return typeof r=="object"&&r!==null&&"_impl"in r}function NS(r){return r instanceof Dr?r:vS(r)?Dr.tensor(r):typeof r=="number"?Number.isInteger(r)?Dr.int(r):Dr.float(r):typeof r=="boolean"?Dr.bool(r):typeof r=="string"?Dr.string(r):Array.isArray(r)?r.length>0&&vS(r[0])?Dr.tensorList(r):Dr.intList(r):(r==null,Dr.none())}var Dr,wr,nc=M(()=>{"use strict";Dr=class r{static{l(this,"IValue")}tag;value;constructor(e,t){this.tag=e,this.value=t}static tensor(e){return new r(0,e)}static int(e){return new r(1,e)}static float(e){return new r(2,e)}static bool(e){return new r(3,e)}static intList(e){return new r(4,e)}static tensorList(e){return new r(5,e)}static string(e){return new r(6,e)}static none(){return new r(7,null)}static device(e){return new r(8,e)}static dtype(e){return new r(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}},wr=class r{static{l(this,"KernelFunction")}_boxed;_unboxed;constructor(e,t){this._boxed=e||null,this._unboxed=t||null}static fromBoxed(e){return new r(e)}static fromUnboxed(e){return new r(void 0,e)}static fromBoth(e,t){return new r(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 n=t.map(i=>NS(i)),s=this._boxed;if(!s)return;let o=s(e,n);return Array.isArray(o)?o.length===1?o[0].value:o.map(i=>i.value):o instanceof Dr?o.value:o}_callUnboxedAsBoxed(e,t){let n=t.map(i=>i.value),s=this._unboxed;if(!s)return[];let o=s(e,...n);return o==null?[]:[NS(o)]}};l(vS,"hasImpl");l(NS,"_toIValue")});function AS(r){_o=r}var _o,Xt,gs=M(()=>{"use strict";ib();nc();_o=null;l(AS,"_setDispatcher");Xt=class{static{l(this,"Library")}_namespace;_kind;_registrations;constructor(e,t){this._namespace=e,this._kind=t,this._registrations=[]}def(e){let t=oh(e,this._namespace);return _o&&_o.registerOp(t),this._registrations.push({type:"def",schema:t}),this}impl(e,t,n){let s=n instanceof wr?n:wr.fromUnboxed(n);return _o&&_o.registerKernel(`${this._namespace}::${e}`,t,s),this._registrations.push({type:"impl",name:e,key:t,kernelFn:s}),this}implBoxed(e,t,n){let s=wr.fromBoxed(n);return _o&&_o.registerKernel(`${this._namespace}::${e}`,t,s),this._registrations.push({type:"impl",name:e,key:t,kernelFn:s}),this}fallback(e,t){let n=t instanceof wr?t:wr.fromBoxed(t);return _o&&_o.registerFallback(e,n),this._registrations.push({type:"fallback",key:e,kernelFn:n}),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 ja(r){let e=EE[r];if(e===void 0)throw new Error(`No backend key for device: ${r}`);return e}function RS(r){return LE[r]??40}function OS(r){return r=r-(r>>1&1431655765),r=(r&858993459)+(r>>2&858993459),(r+(r>>4)&252645135)*16843009>>24}var tr,BS,EE,LE,Pr,rc,cG,sc,pG,Nn=M(()=>{"use strict";tr=(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))(tr||{}),BS=new Array(49).fill(null);for(let[r,e]of Object.entries(tr))r!=="NUM_KEYS"&&typeof e=="number"&&e<49&&(BS[e]=r);EE=Object.freeze({cpu:0,gpu:1,wasm:2,webgpu:5,meta:3,lazy:4}),LE=Object.freeze({0:41,1:42,2:43});l(ja,"backendKeyForDevice");l(RS,"autogradKeyForBackend");Pr=class r{static{l(this,"DispatchKeySet")}_lo;_hi;constructor(e,t){this._lo=e|0,this._hi=t|0}static fromKey(e){return e<32?new r(1<<e,0):new r(0,1<<e-32)}static fromKeys(...e){let t=0,n=0;for(let s=0;s<e.length;s++){let o=e[s];o<32?t|=1<<o:n|=1<<o-32}return new r(t,n)}add(e){return e<32?new r(this._lo|1<<e,this._hi):new r(this._lo,this._hi|1<<e-32)}remove(e){return e<32?new r(this._lo&~(1<<e),this._hi):new r(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 r(this._lo|e._lo,this._hi|e._hi)}intersect(e){return new r(this._lo&e._lo,this._hi&e._hi)}subtract(e){return new r(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 OS(this._lo)+OS(this._hi)}*[Symbol.iterator](){let e=this._hi;for(;e!==0;){let n=31-Math.clz32(e);yield n+32,e&=~(1<<n)}let t=this._lo;for(;t!==0;){let n=31-Math.clz32(t);yield n,t&=~(1<<n)}}toString(){let e=[];for(let t of this)e.push(BS[t]||String(t));return`DispatchKeySet(${e.join(", ")})`}};l(OS,"_popcount32");rc=new Pr(0,0),cG=Pr.fromKeys(0,1,2,3,4,5,6,7,8),sc=Pr.fromKeys(40,41,42,43),pG=Pr.fromKeys(20,24,28,32,40,41,42,43,48)});var ih,MS=M(()=>{"use strict";Nn();ih=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 n=this._kernels[t];if(n)return{key:t,kernel:n}}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 ah,IS=M(()=>{"use strict";ah=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 uh,ES=M(()=>{"use strict";Nn();uh=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 CE(r){return typeof r=="object"&&r!==null&&"then"in r&&typeof r.then=="function"}function LS(r,e){Pi.push(void 0,r);let t;try{t=e()}catch(n){throw Pi.pop(),n}return CE(t)?t.then(n=>(Pi.pop(),n),n=>{throw Pi.pop(),n}):(Pi.pop(),t)}var ab,Pi,ub=M(()=>{"use strict";Nn();ab=class{static{l(this,"GuardStack")}_frames;constructor(){this._frames=[]}push(e,t){this._frames.push({exclude:e||rc,include:t||rc})}pop(){return this._frames.pop()}apply(e){let t=e;for(let n=this._frames.length-1;n>=0;n--){let s=this._frames[n];t=t.subtract(s.exclude),t=t.union(s.include)}return t}get depth(){return this._frames.length}clear(){this._frames.length=0}},Pi=new ab;l(CE,"isThenable");l(LS,"withIncludedKeys")});function DE(r){return typeof r=="object"&&r!==null&&"dispatchKeySet"in r}function cb(r,e){if(!e)return r;if(DE(e)&&e.dispatchKeySet)return r.union(e.dispatchKeySet);if(Array.isArray(e))for(let t=0;t<e.length;t++)r=cb(r,e[t]);return r}function pb(r,e){let t=rc;if(e){let n=e.tensorArgIndices;for(let s=0;s<n.length;s++){let o=n[s];o<r.length&&(t=cb(t,r[o]))}}else for(let n=0;n<r.length;n++)t=cb(t,r[n]);return t}var lb,Nt,$i=M(()=>{"use strict";Nn();MS();IS();ES();ub();gs();ib();l(DE,"hasDispatchKeySet");lb=class{static{l(this,"Dispatcher")}_entries;_handles;_fallbacks;constructor(){this._entries=new Map,this._handles=new Map,this._fallbacks=new uh}registerOp(e){let t=e.key(),n=this._handles.get(t);if(n)return n;let s=new ih(e);this._entries.set(t,s);let o=new ah(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 n=oh(`${e}() -> Tensor`,"mlc");t=this.registerOp(n)}return t}registerKernel(e,t,n){let s=e.includes("::")?e:`mlc::${e}`,o=this._entries.get(s);if(!o)throw new Error(`Op '${s}' not registered`);o.registerKernel(t,n)}registerFallback(e,t){this._fallbacks.register(e,t)}dispatch(e,t,...n){let s=Pi.apply(t);return this._dispatchInternal(e,s,n)}redispatch(e,t,...n){return this._dispatchInternal(e,t,n)}_dispatchInternal(e,t,n){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,...n)}callOp(e,...t){let n=this.findOp(e);if(!n)throw new Error(`Op '${e}' not found`);let s=pb(t,n.schema);return this.dispatch(n,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(cb,"_unionArg");l(pb,"computeKeySet");Nt=new lb;AS(Nt)});function qa(r){return $E+r}var PE,$E,Ce,Vs=M(()=>{"use strict";PE={ceildiv:"ceilDiv"},$E="_sym_";l(qa,"symVarName");Ce=class r{static{l(this,"SymInt")}constructor(e,t=null,n=[]){this.type=e,this.name=t,this.args=n}static var(e){return new r("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:r.equals(e,t)?r.mul(2,e):new r("add",null,[e,t])}static sub(e,t){return typeof e=="number"&&typeof t=="number"?e-t:t===0?e:r.equals(e,t)?0:new r("sub",null,[e,t])}static neg(e){return typeof e=="number"?-e:e instanceof r&&e.type==="neg"?e.args[0]:new r("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 r("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:r.equals(e,t)?1:new r("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||r.equals(e,t)?0:new r("mod",null,[e,t])}static max(e,t){return typeof e=="number"&&typeof t=="number"?Math.max(e,t):e===t||r.equals(e,t)?e:new r("max",null,[e,t])}static min(e,t){return typeof e=="number"&&typeof t=="number"?Math.min(e,t):e===t||r.equals(e,t)?e:new r("min",null,[e,t])}static ceilDiv(e,t){return typeof e=="number"&&typeof t=="number"?Math.ceil(e/t):new r("ceildiv",null,[e,t])}static equals(e,t){if(e===t)return!0;if(typeof e=="number"||typeof t=="number"||!(e instanceof r)||!(t instanceof r)||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 n=r.equals(e.args[0],t.args[0])&&r.equals(e.args[1],t.args[1]),s=r.equals(e.args[0],t.args[1])&&r.equals(e.args[1],t.args[0]);return n||s}for(let n=0;n<e.args.length;n++)if(!r.equals(e.args[n],t.args[n]))return!1;return!0}static substitute(e,t,n){if(typeof e=="number"||!(e instanceof r))return e;if(e.type==="var")return e.name===t?n:e;let s=e.args.map(i=>r.substitute(i,t,n)),o=r[PE[e.type]||e.type];return o&&s.length===2?o(s[0],s[1]):o&&s.length===1?o(s[0]):new r(e.type,e.name,s)}static evaluate(e,t){if(typeof e=="number"||!(e instanceof r))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 n=e.args.map(s=>r.evaluate(s,t));switch(e.type){case"add":return n[0]+n[1];case"sub":return n[0]-n[1];case"mul":return n[0]*n[1];case"div":return Math.floor(n[0]/n[1]);case"mod":return(n[0]%n[1]+n[1])%n[1];case"max":return Math.max(n[0],n[1]);case"min":return Math.min(n[0],n[1]);case"neg":return-n[0];case"ceildiv":return Math.ceil(n[0]/n[1]);default:throw new Error(`Unknown SymInt op: ${e.type}`)}}static freeVars(e,t=new Set){if(typeof e=="number"||!(e instanceof r))return t;if(e.type==="var")return t.add(e.name),t;for(let n of e.args)r.freeVars(n,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 lh(r,e){return r===e?!0:r instanceof Ce&&e instanceof Ce?Ce.equals(r,e):!1}function oc(r,e){let t=1;for(let n=0;n<r.length;n++){let s=r[n];if(typeof s!="number"||s<0)return e;t*=s}return t}function mb(r){let e=1;for(let t=0;t<r.length;t++){let n=r[t];if(typeof n=="number"){if(n<0)return X;e=Ce.mul(e,n)}else if(n instanceof Ce)e=Ce.mul(e,n);else return X}return e}function Gs(r){let e=CS[r];if(e===void 0)throw new Error(`Unknown dtype: ${r}`);return e}function Lt(r){return FE.has(r)}function gb(r){return zE.has(r)}function VE(r,e){return lh(r,e)?r:r===1?e:e===1?r:r===X?e===X?X:e:e===X?r:r instanceof Ce||e instanceof Ce?X:null}function db(r,e){return r===e?!0:r&&typeof r.equals=="function"?r.equals(e):!1}function Rn(r){if(r instanceof I)return`tensor<${r.shape.map(t=>t===X?"?":String(t)).join("x")}x${r.dtype}>`;if(r instanceof Fi)return`tuple<${r.elements.map(Rn).join(", ")}>`;if(r instanceof fb)return"token";if(r instanceof hb){let e=r.inputs.map(Rn).join(", "),t=r.outputs.map(Rn).join(", ");return`(${e}) -> (${t})`}return"unknown"}var zn,CS,FE,zE,X,Et,I,Fi,fb,hb,Oe=M(()=>{"use strict";Vs();l(lh,"dimEquals");zn=(h=>(h.F16="f16",h.BF16="bf16",h.F32="f32",h.F64="f64",h.I8="i8",h.I16="i16",h.I32="i32",h.I64="i64",h.UI8="ui8",h.BOOL="bool",h.INDEX="index",h))(zn||{}),CS=Object.freeze({f16:2,bf16:2,f32:4,f64:8,i8:1,i16:2,i32:4,i64:8,ui8:1,bool:1,index:4}),FE=new Set(["f16","bf16","f32","f64"]),zE=new Set(["i8","i16","i32","i64","ui8"]),X=-1;l(oc,"shapeProduct");l(mb,"symbolicShapeProduct");l(Gs,"scalarBytes");l(Lt,"isFloatType");l(gb,"isIntType");Et=class r{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 n=0;n<e;n++)t[n]=n;return new r(t)}static columnMajor(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=e-1-n;return new r(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 r(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 n=0;n<this.order.length;n++)t[n]=this.order[e.order[n]];return new r(t)}computeStrides(e){let t=e.length,n=new Array(t),s=1;for(let o=t-1;o>=0;o--){let i=this.order[o];n[i]=s,e[i]===X||e[i]instanceof Ce?s=X:s!==X&&(s*=e[i])}return n}equals(e){if(this===e)return!0;if(!(e instanceof r)||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(VE,"broadcastDim");I=class r{static{l(this,"TensorType")}shape;dtype;layout;_hash;constructor(e,t,n=null){this.shape=Object.freeze([...e]),this.dtype=t,this.layout=n||Et.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 Ce)}numel(){return oc(this.shape,X)}symbolicNumel(){return mb(this.shape)}sizeInBytes(){let e=this.numel();return e===X?X:e*CS[this.dtype]}strides(){return this.layout.computeStrides(this.shape)}withShape(e){return new r(e,this.dtype,this.layout)}withDtype(e){return new r(this.shape,e,this.layout)}withLayout(e){return new r(this.shape,this.dtype,e)}equals(e){if(this===e)return!0;if(!(e instanceof r)||this.dtype!==e.dtype||this.shape.length!==e.shape.length)return!1;for(let t=0;t<this.shape.length;t++)if(!lh(this.shape[t],e.shape[t]))return!1;return this.layout.equals(e.layout)}shapeEquals(e){if(!(e instanceof r)||this.dtype!==e.dtype||this.shape.length!==e.shape.length)return!1;for(let t=0;t<this.shape.length;t++)if(!lh(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 n=this.shape[t],s=e.shape[t];if(!(n===X||s===X)&&!lh(n,s)&&typeof n=="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 n=this.shape[t],s=typeof n=="number"?n&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 n=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=VE(o,a[u]);if(p===null)return null;o=p}n[s]=o}return n}static broadcastCompatible(...e){return r.broadcastShape(...e)!==null}},Fi=class r{static{l(this,"TupleType")}elements;constructor(e){this.elements=Object.freeze([...e])}equals(e){if(this===e)return!0;if(!(e instanceof r)||this.elements.length!==e.elements.length)return!1;for(let t=0;t<this.elements.length;t++)if(!db(this.elements[t],e.elements[t]))return!1;return!0}},fb=class r{static{l(this,"TokenType")}equals(e){return e instanceof r}},hb=class r{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 r)||this.inputs.length!==e.inputs.length||this.outputs.length!==e.outputs.length)return!1;for(let t=0;t<this.inputs.length;t++)if(!db(this.inputs[t],e.inputs[t]))return!1;for(let t=0;t<this.outputs.length;t++)if(!db(this.outputs[t],e.outputs[t]))return!1;return!0}};l(db,"typeEquals");l(Rn,"typeToString")});var GE,ic,Wo,ac,ch=M(()=>{"use strict";GE=0,ic=class{static{l(this,"UseLink")}constructor(e,t){this.user=e,this.operandIndex=t,this.prev=null,this.next=null}},Wo=class{static{l(this,"Value")}constructor(e,t=null,n=0){this.type=e,this.definingOp=t,this.resultIndex=n,this.id=GE++,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,n=this._useHead;for(;n;)n.user.operands[n.operandIndex]=e,n=n.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}},ac=class extends Wo{static{l(this,"BlockArgument")}constructor(e,t,n){super(e,null,0),this.ownerBlock=t,this.argIndex=n}_owningFunction(){return this.ownerBlock?this.ownerBlock._owningFunction():null}isBlockArgument(){return!0}}});var yn,Ct,zi=M(()=>{"use strict";ch();yn=class{static{l(this,"Block")}constructor(e=[]){this.parentRegion=null,this._head=null,this._tail=null,this._size=0,this.arguments=e.map((t,n)=>new ac(t,this,n))}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 ac(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 n=[];for(let s=0;s<this.arguments.length;s++)t.has(s)||n.push(this.arguments[s]);this.arguments=n;for(let s=0;s<n.length;s++)n[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()}},Ct=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 an,H,Z,ph,_n=M(()=>{"use strict";an=Object.freeze({NONE:0,READ:1,WRITE:2,ALLOCATE:4,CONTROL:8}),H=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||an.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(H.COMMUTATIVE)}get isAssociative(){return this.traits.has(H.ASSOCIATIVE)}get isElementwise(){return this.traits.has(H.ELEMENTWISE)}get isTerminator(){return this.traits.has(H.TERMINATOR)}get isConstant(){return this.traits.has(H.CONSTANT)}get isReduction(){return this.traits.has(H.REDUCTION)}get isBroadcast(){return this.traits.has(H.BROADCAST)}get isInjective(){return this.traits.has(H.INJECTIVE)}get isOutEWiseFusable(){return this.traits.has(H.OUT_EWISE_FUSABLE)}get isOpaque(){return this.traits.has(H.OPAQUE)}get hasSideEffects(){return this.sideEffects!==an.NONE}},ph=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,n){let s=this._defs.get(e);if(!s)throw new Error(`registerOpAttr: op '${e}' not registered`);return s.setAttr(t,n),s}registerTrait(e,t){let n=this._defs.get(e);if(!n)throw new Error(`registerTrait: op '${e}' not registered`);return n.addTrait(t),n}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 ut,Ua,uc=M(()=>{"use strict";ut=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}},Ua=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,n)=>n.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 n=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?n[i++]=t[s++]:n[i++]=this._generic[o++];for(;s<t.length;)n[i++]=t[s++];for(;o<this._generic.length;)n[i++]=this._generic[o++];return n}hasPatterns(){return this.patterns.length>0}}});function ys(r){return(lc[r]||hh).wasm}function dh(r){return(lc[r]||hh).load}function $S(r){return(lc[r]||hh).store}function cc(r){return(lc[r]||hh).bytes}function yb(r){return PS[r]||null}function _s(r,e){let t=PS[r];return t&&t[e]||null}function Ho(r){return DS[r]||jE}function mh(r){return Ho(r).js}function $r(r){return Ho(r).c}function _b(r){return Ho(r).cPtr}function bb(r){return Ho(r).suffix}function qE(r){return Ho(r).mathSuffix}function bn(r){return Ho(r).bytes}function rr(r){return Ho(r).isFloat}function nr(r){return Ho(r).isInt}function FS(r,e){return r==="sum"||r==="mean"?0:r==="prod"?1:r==="max"?nr(e)?(fh[e]||fh.i32)[0]:-1/0:r==="min"?nr(e)?(fh[e]||fh.i32)[1]:1/0:0}function gh(r,e){let t=UE[r];return t?t+qE(e):r}function Ka(r){let e=KE[r];if(!e)throw new Error(`unsupported compare direction '${r}'`);return e}function zS(r){let e=WE[r];if(!e)throw new Error(`unsupported compare direction '${r}'`);return e}function VS(r){return HE.has(r)}function Mn(r){return(wb[r]||GS).wgsl}function bs(r){return(wb[r]||GS).bytes}function qS(r){return jS[r]||r}function US(r){return r in jS}var DS,lc,hh,PS,jE,fh,UE,KE,WE,HE,wb,GS,jS,Vn=M(()=>{"use strict";DS={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}},lc={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}},hh=lc.f32,PS={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(ys,"wasmType");l(dh,"wasmLoad");l($S,"wasmStore");l(cc,"wasmBytes");l(yb,"wasmSimdEntry");l(_s,"wasmVecOp");jE=DS.f32;l(Ho,"dtypeInfo");l(mh,"jsTypedArray");l($r,"cType");l(_b,"cPtrType");l(bb,"cLiteralSuffix");l(qE,"cMathFuncSuffix");l(bn,"dtypeBytes");l(rr,"isDtypeFloat");l(nr,"isDtypeInt");fh={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(FS,"reduceInitValue");UE={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(gh,"cMathFunc");KE={eq:"==",ne:"!=",lt:"<",le:"<=",gt:">",ge:">="},WE={eq:"===",ne:"!==",lt:"<",le:"<=",gt:">",ge:">="};l(Ka,"cCompareOp");l(zS,"jsCompareOp");HE=new Set(["exp","log","sqrt","tanh","abs","ceil","floor","sin","cos","max","min","pow","round","sign","log2","log10"]);l(VS,"isJSMathFunc");wb={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}},GS=wb.f32;l(Mn,"wgslType");l(bs,"wgslBytes");jS={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(qS,"wgslMathFunc");l(US,"hasWgslMathFunc")});function Ha(r,e){let t={};return r.match(e,t)?t:null}var yh,Tb,xb,Wa,ws,KS=M(()=>{"use strict";yh=class{static{l(this,"DFPattern")}match(e,t){return!1}},Tb=class extends yh{static{l(this,"AnyPattern")}match(){return!0}},xb=class extends yh{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 n=0;n<this.operandPatterns.length;n++){let s=e.getOperand(n).definingOp;if(!this.operandPatterns[n].match(s,t))return!1}return!0}},Wa=l(()=>new Tb,"wildcard"),ws=l((r,...e)=>new xb(r,e),"isOp");l(Ha,"matchPattern")});function js(r,e){return r&&r.opName==="constant"&&r.getAttr("value")===e}function kb(r){return new Sb(r)}var YE,JE,ZE,QE,XE,_h,Ya,bh,wh,Th,xh,Sb,Ja,Za,Qa,Xa,eu,tu,nu,Sh,kh,vh,Nh,Ah,Oh,Bh,Rh,Mh,Ih,ru=M(()=>{"use strict";uc();Oe();Vn();KS();YE=ws("transpose",ws("transpose",Wa())),JE=ws("reshape",ws("reshape",Wa())),ZE=ws("neg",ws("neg",Wa())),QE=ws("exp",ws("log",Wa())),XE=ws("log",ws("exp",Wa()));l(js,"isConstantVal");_h=class extends ut{static{l(this,"FoldTrivialReshape")}constructor(){super("fold_trivial_reshape",10),this.rootOpName="reshape"}match(e){let t=e.getOperand(0).type,n=e.getResult(0).type;return t instanceof I&&n instanceof I&&t.shapeEquals(n)}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0)]),e.erase(),!0}},Ya=class extends ut{static{l(this,"ReshapeReshape")}constructor(){super("reshape_reshape",10),this.rootOpName="reshape"}match(e){return Ha(JE,e)!==null}rewrite(e,t){let n=e.getOperand(0).definingOp.getOperand(0),s=e.getAttr("new_shape"),o=t.reshape(n,s);return e.replaceAllResultsWith([o.getResult(0)]),e.erase(),!0}},bh=class extends ut{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 n=0;n<t.length;n++)if(t[n]!==n)return!1;return!0}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0)]),e.erase(),!0}},wh=class extends ut{static{l(this,"TransposeTranspose")}constructor(){super("transpose_transpose",10),this.rootOpName="transpose"}match(e){return Ha(YE,e)!==null}rewrite(e,t){let n=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(n,i);return e.replaceAllResultsWith([a.getResult(0)]),e.erase(),!0}},Th=class extends ut{static{l(this,"FoldTrivialPad")}constructor(){super("fold_trivial_pad",10),this.rootOpName="pad"}match(e){let t=e.getAttr("low"),n=e.getAttr("high"),s=e.getAttr("interior")||[];return!(t&&t.some(o=>o!==0)||n&&n.some(o=>o!==0)||s&&s.some(o=>o!==0))}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0)]),e.erase(),!0}},xh=class extends ut{static{l(this,"FoldTrivialSlice")}constructor(){super("fold_trivial_slice",10),this.rootOpName="slice"}match(e){let t=e.getOperand(0).type,n=e.getResult(0).type;if(!(t instanceof I)||!(n 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(n)}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0)]),e.erase(),!0}},Sb=class extends ut{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,n=e.getOperand(1).definingOp;return t&&t.opName==="constant"&&!(n&&n.opName==="constant")}rewrite(e,t){let n=e.getOperand(0),s=e.getOperand(1);return e.replaceOperand(0,s),e.replaceOperand(1,n),!0}};l(kb,"commutativeConstantRightFor");Ja=class extends ut{static{l(this,"AddZero")}constructor(){super("add_zero",5),this.rootOpName="add"}match(e){return js(e.getOperand(1).definingOp,0)||js(e.getOperand(0).definingOp,0)}rewrite(e,t){let n=js(e.getOperand(1).definingOp,0)?e.getOperand(0):e.getOperand(1);return n.type.equals(e.getResult(0).type)?(e.replaceAllResultsWith([n]),e.erase(),!0):!1}},Za=class extends ut{static{l(this,"SubZero")}constructor(){super("sub_zero",5),this.rootOpName="sub"}match(e){return js(e.getOperand(1).definingOp,0)}rewrite(e,t){let n=e.getOperand(0);return n.type.equals(e.getResult(0).type)?(e.replaceAllResultsWith([n]),e.erase(),!0):!1}},Qa=class extends ut{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:nr(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}},Xa=class extends ut{static{l(this,"MulOne")}constructor(){super("mul_one",5),this.rootOpName="mul"}match(e){return js(e.getOperand(1).definingOp,1)||js(e.getOperand(0).definingOp,1)}rewrite(e,t){let n=js(e.getOperand(1).definingOp,1)?e.getOperand(0):e.getOperand(1);return n.type.equals(e.getResult(0).type)?(e.replaceAllResultsWith([n]),e.erase(),!0):!1}},eu=class extends ut{static{l(this,"MulZero")}constructor(e=!1){super("mul_zero",5),this.rootOpName="mul",this.fastMath=e}match(e){return!nr(e.getResult(0).type.dtype)&&!this.fastMath?!1:js(e.getOperand(1).definingOp,0)||js(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}},tu=class extends ut{static{l(this,"DivOne")}constructor(){super("div_one",5),this.rootOpName="div"}match(e){return js(e.getOperand(1).definingOp,1)}rewrite(e,t){let n=e.getOperand(0);return n.type.equals(e.getResult(0).type)?(e.replaceAllResultsWith([n]),e.erase(),!0):!1}},nu=class extends ut{static{l(this,"DoubleNeg")}constructor(){super("double_neg",5),this.rootOpName="neg"}match(e){return Ha(ZE,e)!==null}rewrite(e,t){let n=e.getOperand(0).definingOp.getOperand(0);return e.replaceAllResultsWith([n]),e.erase(),!0}},Sh=class extends ut{static{l(this,"ExpLog")}constructor(e=!1){super("exp_log",5),this.rootOpName="exp",this.fastMath=e}match(e){return this.fastMath&&Ha(QE,e)!==null}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0).definingOp.getOperand(0)]),e.erase(),!0}},kh=class extends ut{static{l(this,"LogExp")}constructor(e=!1){super("log_exp",5),this.rootOpName="log",this.fastMath=e}match(e){return this.fastMath&&Ha(XE,e)!==null}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0).definingOp.getOperand(0)]),e.erase(),!0}},vh=class extends ut{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}},Nh=class extends ut{static{l(this,"MulNegNeg")}constructor(){super("mul_neg_neg",4),this.rootOpName="mul"}match(e){let t=e.getOperand(0).definingOp,n=e.getOperand(1).definingOp;return t&&t.opName==="neg"&&n&&n.opName==="neg"}rewrite(e,t){let n=e.getOperand(0).definingOp.getOperand(0),s=e.getOperand(1).definingOp.getOperand(0),o=t.mul(n,s);return e.replaceAllResultsWith([o.getResult(0)]),e.erase(),!0}},Ah=class extends ut{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 n=e.getOperand(0),s=e.getOperand(1).definingOp.getOperand(0),o=t.sub(n,s);return e.replaceAllResultsWith([o.getResult(0)]),e.erase(),!0}},Oh=class extends ut{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 n=e.getOperand(0),s=e.getOperand(1).definingOp.getOperand(0),o=t.add(n,s);return e.replaceAllResultsWith([o.getResult(0)]),e.erase(),!0}},Bh=class extends ut{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 n=t.getOperand(0).type.dtype,s=e.getAttr("target_dtype");return n===s}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0).definingOp.getOperand(0)]),e.erase(),!0}},Rh=class extends ut{static{l(this,"LayoutTransformIdentity")}constructor(){super("layout_transform_identity",10),this.rootOpName="layout_transform"}match(e){let t=e.getAttr("src_layout"),n=e.getAttr("dst_layout");if(!t||!n||t.length!==n.length)return!1;for(let s=0;s<t.length;s++)if(t[s]!==n[s])return!1;return!0}rewrite(e,t){return e.replaceAllResultsWith([e.getOperand(0)]),e.erase(),!0}},Mh=class extends ut{static{l(this,"FoldTransposeIntoDot")}constructor(){super("fold_transpose_into_dot",10),this.rootOpName="dot"}match(e){for(let t=0;t<2;t++){let n=e.getOperand(t).definingOp;if(!n||n.opName!=="transpose")continue;let s=n.getAttr("permutation");if(!(!s||s.length!==2)&&!(s[0]!==1||s[1]!==0))return!0}return!1}rewrite(e,t){let n=[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=n[p].definingOp;if(!c||c.opName!=="transpose")continue;let h=c.getAttr("permutation");if(!h||h.length!==2||h[0]!==1||h[1]!==0)continue;n[p]=c.getOperand(0);let f=p===0?s:o,d=p===0?i:a;for(let m=0;m<f.length;m++)f[m]=f[m]===0?1:0;for(let m=0;m<d.length;m++)d[m]=d[m]===0?1:0}let u=t.dot(n[0],n[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}},Ih=class extends ut{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 n=e.getOperand(0).definingOp,s=n.getAttr("src_layout"),o=n.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=n.getOperand(0),p=t._inferAndBuild("layout_transform",[u],{src_layout:s,dst_layout:a});return e.replaceAllResultsWith([p.getResult(0)]),e.erase(),!0}}});function Vi(r){if(r.length!==2)return null;let e=r[0],t=r[1];if(!(e instanceof I)||!(t instanceof I)||e.dtype!==t.dtype)return null;let n=I.broadcastShape(e.shape,t.shape);return n?[new I(n,e.dtype)]:null}function Eh(r){if(r.length!==1)return null;let e=r[0];return e instanceof I?[new I(e.shape,e.dtype)]:null}function WS(r){if(r.length!==2)return null;let e=r[0],t=r[1];if(!(e instanceof I)||!(t instanceof I))return null;let n=I.broadcastShape(e.shape,t.shape);return n?[new I(n,"bool")]:null}function vb(r){if(r.length!==1)return null;let e=r[0];return!(e instanceof I)||!Lt(e.dtype)?null:[new I(e.shape,e.dtype)]}function Gi(r){let e=[];if(r.numOperands!==2)return e.push(`${r.opName} expects 2 operands, got ${r.numOperands}`),e;let t=r.getOperand(0).type,n=r.getOperand(1).type;return t instanceof I||e.push(`${r.opName} operand 0 is not tensor`),n instanceof I||e.push(`${r.opName} operand 1 is not tensor`),t instanceof I&&n instanceof I&&t.dtype!==n.dtype&&e.push(`${r.opName} dtype mismatch: ${t.dtype} vs ${n.dtype}`),e}function fc(r){let e=[];return r.numOperands!==1?(e.push(`${r.opName} expects 1 operand, got ${r.numOperands}`),e):(r.getOperand(0).type instanceof I||e.push(`${r.opName} operand is not tensor`),e)}function Nb(r){let e=fc(r);if(e.length===0){let t=r.getOperand(0).type.dtype;Lt(t)||e.push(`${r.opName} requires float type, got ${t}`)}return e}var su,Ab,Lh=M(()=>{"use strict";Oe();_n();l(Vi,"inferBinaryElementwise");l(Eh,"inferUnaryElementwise");l(WS,"inferCompare");l(vb,"inferUnaryFloat");l(Gi,"verifyBinaryElementwise");l(fc,"verifyUnaryElementwise");l(Nb,"verifyUnaryFloat");su=[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],Ab=[...su,H.COMMUTATIVE,H.ASSOCIATIVE]});function Ch(r){return e=>{if(!(typeof e[0]!="number"||typeof e[1]!="number"))return r(e[0],e[1])}}function eL(r){return e=>typeof e[0]=="number"?r(e[0]):void 0}function HS(r){r.register(new Z({name:"add",numOperands:2,numResults:1,traits:Ab,inferResultTypes:Vi,verify:Gi,getCanonicalizationPatterns(){return[kb("add"),new Ja]},fold:Ch((e,t)=>e+t)})),r.register(new Z({name:"mul",numOperands:2,numResults:1,traits:Ab,inferResultTypes:Vi,verify:Gi,getCanonicalizationPatterns(){return[kb("mul"),new Xa,new eu]},fold:Ch((e,t)=>e*t)})),r.register(new Z({name:"sub",numOperands:2,numResults:1,traits:su,inferResultTypes:Vi,verify:Gi,getCanonicalizationPatterns(){return[new Za,new Qa]},fold:Ch((e,t)=>e-t)})),r.register(new Z({name:"div",numOperands:2,numResults:1,traits:su,inferResultTypes:Vi,verify:Gi,getCanonicalizationPatterns(){return[new tu]},fold:Ch((e,t)=>e/t)}));for(let e of["rem","pow"])r.register(new Z({name:e,numOperands:2,numResults:1,traits:su,inferResultTypes:Vi,verify:Gi}));r.register(new Z({name:"neg",numOperands:1,numResults:1,traits:[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Eh,verify:fc,getCanonicalizationPatterns(){return[new nu]},fold:eL(e=>-e)}));for(let e of["maximum","minimum"])r.register(new Z({name:e,numOperands:2,numResults:1,traits:[...su,H.COMMUTATIVE],inferResultTypes:Vi,verify:Gi}))}var YS=M(()=>{"use strict";_n();ru();Lh();l(Ch,"scalarBinaryFold");l(eL,"scalarUnaryFold");l(HS,"register")});function JS(r){for(let e of["abs","floor","ceil","round","sign","square","reciprocal"])r.register(new Z({name:e,numOperands:1,numResults:1,traits:[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Eh,verify:fc}));r.register(new Z({name:"exp",numOperands:1,numResults:1,traits:[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:vb,fold(e){return typeof e[0]=="number"?Math.exp(e[0]):void 0},verify:Nb}));for(let e of["log","sqrt","rsqrt","tanh","sin","cos","erf","erfc","lgamma","gamma","log2","log10","exp2"])r.register(new Z({name:e,numOperands:1,numResults:1,traits:[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:vb,verify:Nb}))}var ZS=M(()=>{"use strict";_n();Lh();l(JS,"register")});function tL(r){if(r.length!==1)return null;let e=r[0];return!(e instanceof I)||e.dtype!=="bool"?null:[new I(e.shape,"bool")]}function nL(r){if(r.length!==2)return null;let[e,t]=r;if(!(e instanceof I)||e.dtype!=="bool"||!(t instanceof I)||t.dtype!=="bool")return null;let n=I.broadcastShape(e.shape,t.shape);return n?[new I(n,"bool")]:null}function QS(r){r.register(new Z({name:"compare",numOperands:2,numResults:1,attrs:[{name:"direction",type:"string",required:!0}],traits:[H.ELEMENTWISE],inferResultTypes:WS,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 n=e.getAttr("direction");rL.has(n)||t.push(`compare invalid direction: ${n}`)}return t}})),r.register(new Z({name:"select",numOperands:3,numResults:1,traits:[H.ELEMENTWISE],inferResultTypes(e){if(e.length!==3)return null;let[t,n,s]=e;if(!(t instanceof I)||t.dtype!=="bool"||!(n instanceof I)||!(s instanceof I)||n.dtype!==s.dtype)return null;let o=I.broadcastShape(t.shape,n.shape,s.shape);return o?[new I(o,n.dtype)]:null}})),r.register(new Z({name:"where",numOperands:3,numResults:1,traits:[H.ELEMENTWISE],inferResultTypes(e){if(e.length!==3)return null;let[t,n,s]=e;if(!(t instanceof I)||!(n instanceof I)||!(s instanceof I)||n.dtype!==s.dtype)return null;let o=I.broadcastShape(t.shape,n.shape,s.shape);return o?[new I(o,n.dtype)]:null}})),r.register(new Z({name:"logical_not",numOperands:1,numResults:1,traits:[H.ELEMENTWISE],inferResultTypes:tL}));for(let e of["logical_and","logical_or"])r.register(new Z({name:e,numOperands:2,numResults:1,traits:[H.ELEMENTWISE],inferResultTypes:nL}));r.register(new Z({name:"clamp",numOperands:3,numResults:1,traits:[H.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 rL,XS=M(()=>{"use strict";_n();Oe();Lh();l(tL,"inferUnaryBool");l(nL,"inferBinaryBool");rL=new Set(["eq","ne","lt","le","gt","ge"]);l(QS,"register")});function ek(r){r.register(new Z({name:"stop_gradient",numOperands:1,numResults:1,traits:[H.VIEW],inferResultTypes(e){return e.length!==1?null:[e[0]]},propagateSymbolicShapes(e,t){let n=t.get(e.getOperand(0));return n?[n]:null}})),r.register(new Z({name:"reverse",numOperands:1,numResults:1,traits:[H.INJECTIVE],attrs:[{name:"dimensions",type:"array",required:!0}],inferResultTypes(e){return e.length!==1?null:[e[0]]},propagateSymbolicShapes(e,t){let n=t.get(e.getOperand(0));return n?[n]:null}})),r.register(new Z({name:"broadcast_in_dim",numOperands:1,numResults:1,traits:[H.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 n=e[0];if(!(n instanceof I))return null;let s=t.get?t.get("result_shape"):t.result_shape;return s?[new I(s,n.dtype)]:null},propagateSymbolicShapes(e,t){let n=t.get(e.getOperand(0));if(!n)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 n[a]!="number"||i[s[a]]===null)&&(i[s[a]]=n[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 n=e.getAttr("broadcast_dimensions"),s=e.getAttr("result_shape"),o=e.getOperand(0).type;n.length!==o.rank&&t.push(`broadcast_dimensions length ${n.length} != input rank ${o.rank}`);for(let a=0;a<n.length;a++)n[a]<0||n[a]>=s.length?t.push(`broadcast_dimensions[${a}]=${n[a]} out of range for result rank ${s.length}`):o instanceof I&&o.shape[a]!==X&&o.shape[a]!==1&&s[n[a]]!==X&&o.shape[a]!==s[n[a]]&&t.push(`broadcast_in_dim: input dim ${a} size ${o.shape[a]} incompatible with result dim ${n[a]} size ${s[n[a]]}`);let i=new Set;for(let a of n)i.has(a)&&t.push(`broadcast_dimensions has duplicate: ${a}`),i.add(a)}return t}})),r.register(new Z({name:"reshape",numOperands:1,numResults:1,traits:[H.VIEW],attrs:[{name:"new_shape",type:"array",required:!0}],inferResultTypes(e,t){if(e.length!==1)return null;let n=e[0];if(!(n instanceof I))return null;let s=t.get?t.get("new_shape"):t.new_shape;return s?[new I(s,n.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 n=e.getOperand(0).type,s=e.getAttr("new_shape");if(n instanceof I&&n.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=n.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 n=t.get(e.getOperand(0));if(!n)return null;let s=e.getAttr("new_shape"),o=[];for(let i of s)if(i===-1){let a=n.find(u=>typeof u!="number");o.push(a||-1)}else o.push(i);return[o]},getCanonicalizationPatterns(){return[new _h,new Ya]},fold(e){return e[0]}})),r.register(new Z({name:"transpose",numOperands:1,numResults:1,traits:[H.VIEW],attrs:[{name:"permutation",type:"array",required:!0}],inferResultTypes(e,t){if(e.length!==1)return null;let n=e[0];if(!(n instanceof I))return null;let s=t.get?t.get("permutation"):t.permutation;if(!s)return null;let o=s.map(i=>n.shape[i]);return[new I(o,n.dtype)]},propagateSymbolicShapes(e,t){let n=t.get(e.getOperand(0));if(!n)return null;let s=e.getAttr("permutation");return s?[s.map(o=>n[o])]:null},getCanonicalizationPatterns(){return[new bh]},fold(e,t,n){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=n&&n[0]?n[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]),h=s.length,f=new Array(h),d=new Array(o.length).fill(0);for(let m=0;m<h;m++){let g=0;for(let y=0;y<o.length;y++)g+=d[y]*u[o[y]];f[m]=s[g];for(let y=o.length-1;y>=0&&!(++d[y]<c[y]);y--)d[y]=0}return f},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 n=e.getAttr("permutation"),s=e.getOperand(0).type;s instanceof I&&n.length!==s.rank&&t.push(`transpose permutation length ${n.length} != input rank ${s.rank}`);let o=new Set;for(let i of n)o.has(i)&&t.push(`transpose duplicate in permutation: ${i}`),o.add(i);return t}})),r.register(new Z({name:"slice",numOperands:1,numResults:1,traits:[H.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 n=e[0];if(!(n 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,n.dtype)]},getCanonicalizationPatterns(){return[new xh]}})),r.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 n=e[0];if(!(n instanceof I))return null;let s=t.get?t.get("dimension"):t.dimension;if(s===void 0)return null;let o=[...n.shape];for(let i=1;i<e.length;i++){let a=e[i];if(!(a instanceof I)||a.dtype!==n.dtype||a.rank!==n.rank)return null;o[s]===X||a.shape[s]===X?o[s]=X:o[s]+=a.shape[s]}return[new I(o,n.dtype)]}})),r.register(new Z({name:"pad",numOperands:2,numResults:1,traits:[H.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 n=e[0];if(!(n 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<n.rank;u++)n.shape[u]===X?a.push(X):a.push(s[u]+n.shape[u]+(n.shape[u]-1)*i[u]+o[u]);return[new I(a,n.dtype)]},getCanonicalizationPatterns(){return[new Th]}})),r.register(new Z({name:"gather",numOperands:2,numResults:1,traits:[H.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 n=e[0],s=e[1];if(!(n 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 h=new Set(o),f=[],d=0,m=0,g=p.length+c.length;for(let y=0;y<g;y++)h.has(y)?f.push(c[m++]):f.push(p[d++]);return[new I(f,n.dtype)]}})),r.register(new Z({name:"scatter",numOperands:3,numResults:1,traits:[H.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}})),r.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 n=e[0];if(!(n 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=[...n.shape];return a[s]=i,new I(a,n.dtype)})}}))}var tk=M(()=>{"use strict";_n();Oe();ru();l(ek,"register")});function nk(r){r.register(new Z({name:"reduce",numOperands:2,numResults:1,attrs:[{name:"dimensions",type:"array",required:!0},{name:"reduce_type",type:"string",required:!0}],traits:[H.REDUCTION],hasRegions:!0,numRegions:1,inferResultTypes(t,n){if(t.length<1)return null;let s=t[0];if(!(s instanceof I))return null;let o=n.get?n.get("dimensions"):n.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,n){let s=n.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 n=[];if(t.hasAttr("dimensions")||n.push("reduce missing dimensions"),!t.hasAttr("reduce_type"))n.push("reduce missing reduce_type");else{let s=t.getAttr("reduce_type");sL.has(s)||n.push(`reduce invalid reduce_type: ${s}`)}return n}}));function e(t,n){if(t.length<1)return null;let s=t[0];if(!(s instanceof I))return null;let o=n.get?n.get("axis"):n.axis;if(o===void 0)return null;let i=(n.get?n.get("keep_dims"):n.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"),r.register(new Z({name:"argmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"keep_dims",type:"boolean",required:!1}],traits:[H.REDUCTION],inferResultTypes:e})),r.register(new Z({name:"argmin",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"keep_dims",type:"boolean",required:!1}],traits:[H.REDUCTION],inferResultTypes:e}))}var sL,rk=M(()=>{"use strict";_n();Oe();sL=new Set(["sum","max","min","prod","mean","and","or"]);l(nk,"register")});function oL(r,e){let t=r.getAttr("scale"),n=e.getAttr("scale"),s=r.getAttr("zero_point"),o=e.getAttr("zero_point"),i=r.getAttr("scheme"),a=e.getAttr("scheme");if(i!==a)return!1;if(typeof t=="number"&&typeof n=="number"){if(t!==n)return!1}else return!1;if(typeof s=="number"&&typeof o=="number"){if(s!==o)return!1}else return!1;return!0}var Dh,Ph,$h,Ob=M(()=>{"use strict";uc();Oe();l(oL,"quantParamsMatch");Dh=class extends ut{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:oL(e,t)}rewrite(e,t){let n=e.getOperand(0).definingOp.getOperand(0);if(!n.type||!e.getResult(0).type)return!1;let s=n.type,o=e.getResult(0).type;return s instanceof I&&o instanceof I&&s.dtype===o.dtype?(e.replaceAllResultsWith([n]),e.erase(),!0):!1}},Ph=class extends ut{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=Gs(a)*8,p=a==="ui8",c=p?0:-(1<<u-1),h=p?(1<<u)-1:(1<<u-1)-1,f=Math.max(c,Math.min(h,Math.round(s/o+i))),d=e.getResult(0).type,m=t.constant(f,d);return e.replaceAllResultsWith([m.getResult(0)]),e.erase(),!0}},$h=class extends ut{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,n=e.getOperand(1).definingOp;return t&&t.opName==="dequantize"&&n&&n.opName==="dequantize"}rewrite(e,t){let n=e.getOperand(0).definingOp,s=e.getOperand(1).definingOp,o=n.getOperand(0),i=s.getOperand(0),a=n.getAttr("scale"),u=s.getAttr("scale"),p=n.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 h=a*u,f=e.getAttr("lhs_contracting"),d=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:f,rhs_contracting:d,lhs_batch:m,rhs_batch:g,lhs_scale:a,lhs_zero_point:p,rhs_scale:u,rhs_zero_point:c,output_scale:h,output_zero_point:0}),b=t._inferAndBuild("dequantize",[_.getResult(0)],{scale:h,zero_point:0,scheme:n.getAttr("scheme"),target_dtype:e.getResult(0).type.dtype});return e.replaceAllResultsWith([b.getResult(0)]),e.erase(),!0}}});function ok(r){r.register(new Z({name:"dot",numOperands:2,numResults:1,opAttrs:{gpuCapable:!0},traits:[H.OPAQUE,H.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,n=e.getOperand(1).type;if(!(t instanceof I)||!(n 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:Fh,getCanonicalizationPatterns(){return[new Mh,new $h]},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 n=e.getOperand(0).type,s=e.getOperand(1).type;if(n instanceof I&&s instanceof I){n.dtype!==s.dtype&&t.push(`dot dtype mismatch: ${n.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>=n.rank&&t.push(`dot lhs_contracting[${a}]=${u} out of range (rank ${n.rank})`),p>=s.rank&&t.push(`dot rhs_contracting[${a}]=${p} out of range (rank ${s.rank})`);let c=n.shape[u],h=s.shape[p];u<n.rank&&p<s.rank&&typeof c=="number"&&c!==X&&typeof h=="number"&&h!==X&&c!==h&&t.push(`dot contracting dim size mismatch at [${a}]: lhs dim ${u} size ${c} vs rhs dim ${p} size ${h}`)}}return t}})),r.register(new Z({name:"cublas_gemm",numOperands:2,numResults:1,traits:[H.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:Fh,getFlops(e){let t=e.getOperand(0).type,n=e.getResult(0).type;if(!(t instanceof I)||!(n 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=n.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)}})),r.register(new Z({name:"conv",numOperands:2,numResults:1,opAttrs:{gpuCapable:!0},traits:[H.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,n=e.getOperand(1).type;if(!(t instanceof I)||!(n instanceof I))return 0;let s=t.numel(),o=n.numel();return s===X||o===X?0:2*s*o/(n.shape[0]||1)},inferResultTypes:Bb,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 Fh(r,e,t){if(r.length!==2)return null;let n=r[0],s=r[1];if(!(n instanceof I)||!(s instanceof I))return null;let o=t&&!Array.isArray(t)?t:{};if(!o.allowMixedDtype&&n.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 h=0;h<n.rank;h++)u.has(h)&&c.push(n.shape[h]);for(let h=0;h<n.rank;h++)!i.has(h)&&!u.has(h)&&c.push(n.shape[h]);for(let h=0;h<s.rank;h++)!a.has(h)&&!p.has(h)&&c.push(s.shape[h]);return[new I(c,o.outputDtype||n.dtype)]}function Bb(r,e,t){if(r.length!==2)return null;let n=r[0],s=r[1];if(!(n instanceof I)||!(s instanceof I))return null;let o=t&&!Array.isArray(t)?t:{};if(!o.allowMixedDtype&&n.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=n.shape[0],h=s.shape[0],f=[];for(let d=0;d<p;d++){let m=n.shape[d+2],g=s.shape[d+2],y=a[d][0]+a[d][1];if(m===X||g===X)f.push(X);else{let _=(g-1)*u[d]+1;f.push(Math.floor((m+y-_)/i[d])+1)}}return[new I([c,h,...f],o.outputDtype||n.dtype)]}var Rb=M(()=>{"use strict";_n();Oe();ru();Ob();l(ok,"register");l(Fh,"inferDotResultTypes");l(Bb,"inferConvResultTypes")});function ik(r){let e={numOperands:0,numResults:1,attrs:[{name:"value",type:"any",required:!0},{name:"tensor_type",type:"object",required:!0}],traits:[H.CONSTANT],inferResultTypes(t,n){let s=n.get?n.get("tensor_type"):n.tensor_type;return s?[s]:null}};for(let t of["constant","scalar_constant"])r.register(new Z({name:t,...e}));r.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,n){let s=n.get?n.get("tensor_type"):n.tensor_type;return s?[s]:null}})),r.register(new Z({name:"tuple",numOperands:-1,numResults:1,inferResultTypes(t){return[new Fi(t)]}})),r.register(new Z({name:"get_tuple_element",numOperands:1,numResults:1,attrs:[{name:"index",type:"number",required:!0}],inferResultTypes(t,n){if(t.length!==1)return null;let s=t[0];if(!s||!s.elements)return null;let o=n.get?n.get("index"):n.index;return o===void 0||o<0||o>=s.elements.length?null:[s.elements[o]]}})),r.register(new Z({name:"convert",numOperands:1,numResults:1,attrs:[{name:"target_dtype",type:"string",required:!0}],traits:[H.ELEMENTWISE],inferResultTypes(t,n){if(t.length!==1)return null;let s=t[0];if(!(s instanceof I))return null;let o=n.get?n.get("target_dtype"):n.target_dtype;return[new I(s.shape,o)]}})),r.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,n){if(t.length<1)return null;let s=t[0];if(!(s instanceof I))return null;let o=n.get?n.get("depth"):n.depth,i=(n.get?n.get("axis"):n.axis)??-1,a=(n.get?n.get("dtype"):n.dtype)||"f32",u=[...s.shape],p=i<0?u.length+1+i:i;return u.splice(p,0,o),[new I(u,a)]}}))}var ak=M(()=>{"use strict";_n();Oe();l(ik,"register")});function uk(r){r.register(new Z({name:"return",numOperands:-1,numResults:0,traits:[H.TERMINATOR]})),r.register(new Z({name:"yield",numOperands:-1,numResults:0,traits:[H.TERMINATOR]})),r.register(new Z({name:"if",numOperands:1,numResults:-1,hasRegions:!0,numRegions:2,sideEffects:an.CONTROL,inferResultTypes(e,t,n){return n||null}})),r.register(new Z({name:"while",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:2,sideEffects:an.CONTROL,inferResultTypes(e){return[...e]}})),r.register(new Z({name:"scan",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:1,sideEffects:an.CONTROL,attrs:[{name:"num_carry",type:"number",required:!0},{name:"num_xs",type:"number",required:!0}],inferResultTypes(e,t,n){return n||null}})),r.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:an.WRITE,inferResultTypes(e,t,n){return n||null}})),r.register(new Z({name:"fused_dot_epilogue",numOperands:-1,numResults:1,traits:[H.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,n){return n||null}})),r.register(new Z({name:"fusion",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:1,attrs:[{name:"fusion_kind",type:"string",required:!1}],inferResultTypes(e,t,n){return n||null}}))}var lk=M(()=>{"use strict";_n();l(uk,"register")});function ck(r){r.register(new Z({name:"layout_transform",numOperands:1,numResults:1,traits:[H.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 n=e[0];if(!(n instanceof I))return null;let s=t.get?t.get("dst_layout"):t.dst_layout;return s?[new I(n.shape,n.dtype,new Et(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 n=e.getOperand(0).type,s=e.getAttr("src_layout"),o=e.getAttr("dst_layout");n instanceof I&&(s.length!==n.rank&&t.push(`src_layout length ${s.length} != input rank ${n.rank}`),o.length!==n.rank&&t.push(`dst_layout length ${o.length} != input rank ${n.rank}`))}return t},getCanonicalizationPatterns(){return[new Rh,new Ih]}}))}var pk=M(()=>{"use strict";_n();Oe();ru();l(ck,"register")});function fk(r){r.register(new Z({name:"quantize",numOperands:1,numResults:1,traits:[H.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 n=e[0];if(!(n instanceof I))return null;let s=t.get?t.get("target_dtype"):t.target_dtype;return s?[new I(n.shape,s)]:null},verify(e){let t=[];if(e.numOperands!==1)return t.push("quantize expects 1 operand"),t;let n=e.getOperand(0).type;n instanceof I&&!Lt(n.dtype)&&t.push(`quantize input must be float, got ${n.dtype}`);let s=e.getAttr("target_dtype");s&&!Mb.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 n=t.get?t.get("scale"):t.scale,s=t.get?t.get("zero_point"):t.zero_point;if(!(typeof n!="number"||typeof s!="number"))return Math.round(e[0]/n+s)},getCanonicalizationPatterns(){return[new Dh,new Ph]}})),r.register(new Z({name:"dequantize",numOperands:1,numResults:1,traits:[H.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 n=e[0];if(!(n instanceof I))return null;let s=t.get?t.get("target_dtype"):t.target_dtype;return s?[new I(n.shape,s)]:null},verify(e){let t=[];if(e.numOperands!==1)return t.push("dequantize expects 1 operand"),t;let n=e.getOperand(0).type;n instanceof I&&!iL.has(n.dtype)&&t.push("dequantize input must be i8, ui8, or i32, got "+n.dtype);let s=e.getAttr("target_dtype");return s&&!Lt(s)&&t.push(`dequantize target_dtype must be float, got ${s}`),t},fold(e,t){if(typeof e[0]!="number")return;let n=t.get?t.get("scale"):t.scale,s=t.get?t.get("zero_point"):t.zero_point;if(!(typeof n!="number"||typeof s!="number"))return(e[0]-s)*n}})),r.register(new Z({name:"quantized_dot",numOperands:2,numResults:1,traits:[H.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,n=e.getOperand(1).type;if(!(t instanceof I)||!(n 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 Fh(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 n=e.getOperand(0).type,s=e.getOperand(1).type;return n instanceof I&&!Mb.has(n.dtype)&&t.push(`quantized_dot lhs must be i8/ui8, got ${n.dtype}`),s instanceof I&&!Mb.has(s.dtype)&&t.push(`quantized_dot rhs must be i8/ui8, got ${s.dtype}`),t}})),r.register(new Z({name:"quantized_conv",numOperands:2,numResults:1,traits:[H.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 Bb(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 Mb,iL,hk=M(()=>{"use strict";_n();Oe();Ob();Rb();Mb=new Set(["i8","ui8"]),iL=new Set(["i8","ui8","i32"]);l(fk,"register")});function aL(r){if(r.length<1)return null;let e=r[0];return e instanceof I?[new I(e.shape,e.dtype)]:null}function Ts(r){if(r.length<1)return null;let e=r[0];return!(e instanceof I)||!Lt(e.dtype)?null:[new I(e.shape,e.dtype)]}function bo(r){let e=[];if(r.numOperands<1)return e.push(`${r.opName} expects at least 1 operand`),e;let t=r.getOperand(0).type;return t instanceof I&&!Lt(t.dtype)&&e.push(`${r.opName} requires float input, got ${t.dtype}`),e}function dk(r){r.register(new Z({name:"all_reduce",numOperands:1,numResults:1,traits:[H.OPAQUE],attrs:[{name:"reduce_op",type:"string",required:!1},{name:"mesh_axis",type:"number",required:!1}],inferResultTypes:aL})),r.register(new Z({name:"all_gather",numOperands:1,numResults:1,traits:[H.OPAQUE],attrs:[{name:"mesh_axis",type:"number",required:!1},{name:"gather_dim",type:"number",required:!1}],inferResultTypes(e,t){let n=e[0];if(!(n 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=[...n.shape];return i[o]=i[o]*i[s],[new I(i,n.dtype)]}})),r.register(new Z({name:"scaled_dot_product_attention",numOperands:3,numResults:1,traits:[H.OPAQUE],attrs:[{name:"scale",type:"number",required:!0},{name:"causal",type:"boolean",required:!1}],inferResultTypes(e){let t=e[0],n=e[2];return!(t instanceof I)||!(n instanceof I)?null:[new I([...t.shape.slice(0,t.rank-1),n.shape[n.rank-1]],t.dtype)]}})),r.register(new Z({name:"softmax",numOperands:1,numResults:1,opAttrs:{gpuCapable:!0},attrs:[{name:"axis",type:"number",required:!0}],inferResultTypes:Ts,verify:bo})),r.register(new Z({name:"log_softmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0}],inferResultTypes:Ts,verify:bo})),r.register(new Z({name:"gelu",numOperands:1,numResults:1,traits:[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Ts,verify:bo})),r.register(new Z({name:"sigmoid",numOperands:1,numResults:1,traits:[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Ts,verify:bo})),r.register(new Z({name:"silu",numOperands:1,numResults:1,traits:[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Ts,verify:bo})),r.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:Ts,verify(e){let t=[];if(e.numOperands!==3)return t.push("layer_norm expects 3 operands (input, gamma, beta)"),t;let n=e.getOperand(0).type;return n instanceof I&&!Lt(n.dtype)&&t.push(`layer_norm requires float input, got ${n.dtype}`),t}})),r.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:Ts,verify(e){let t=[];if(e.numOperands!==5)return t.push("batch_norm expects 5 operands (input, gamma, beta, mean, var)"),t;let n=e.getOperand(0).type;return n instanceof I&&!Lt(n.dtype)&&t.push(`batch_norm requires float input, got ${n.dtype}`),t}}));for(let e of["selu","mish","hardswish","hardsigmoid"])r.register(new Z({name:e,numOperands:1,numResults:1,traits:[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Ts,verify:bo}));r.register(new Z({name:"elu",numOperands:1,numResults:1,attrs:[{name:"alpha",type:"number"}],traits:[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Ts,verify:bo})),r.register(new Z({name:"leaky_relu",numOperands:1,numResults:1,attrs:[{name:"negative_slope",type:"number"}],traits:[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Ts,verify:bo})),r.register(new Z({name:"celu",numOperands:1,numResults:1,attrs:[{name:"alpha",type:"number"}],traits:[H.ELEMENTWISE,H.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Ts,verify:bo})),r.register(new Z({name:"embedding",numOperands:2,numResults:1,inferResultTypes(e){if(e.length<2)return null;let t=e[0],n=e[1];if(!(t instanceof I)||!(n instanceof I))return null;let s=[...n.shape,t.shape[t.rank-1]];return[new I(s,t.dtype)]}}))}var mk=M(()=>{"use strict";_n();Oe();l(aL,"inferSameAsInput");l(Ts,"inferSameAsInputFloat");l(bo,"verifyUnaryFloat");l(dk,"register")});function gk(r){r.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:an.READ|an.WRITE,traits:[H.INJECTIVE],inferResultTypes(e){return e.length!==1?null:[e[0]]},getFlops(){return 0}}))}var yk=M(()=>{"use strict";_n();l(gk,"register")});function _k(r,e,t,n,s,o){let i=r+n+s-e;return(o?Math.ceil(i/t):Math.floor(i/t))+1}function bk(r){r.register(new Z({name:"pool2d",numOperands:1,numResults:1,traits:[H.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 n=e[0];if(!(n instanceof I)||n.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=_k(n.shape[2],s[0],o[0],i[0][0],i[0][1],a),p=_k(n.shape[3],s[1],o[1],i[1][0],i[1][1],a);return[new I([n.shape[0],n.shape[1],u,p],n.dtype)]}}))}var wk=M(()=>{"use strict";_n();Oe();l(_k,"computePoolOutputDim");l(bk,"register")});function Tk(r){r.register(new Z({name:"resize",numOperands:1,numResults:1,traits:[H.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 n=e[0];if(!(n instanceof I)||n.rank!==4)return null;let s=t.get?t.get("output_size"):t.output_size;return[new I([n.shape[0],n.shape[1],s[0],s[1]],n.dtype)]}}))}var xk=M(()=>{"use strict";_n();Oe();l(Tk,"register")});function lL(r=uL){let e=new ph;for(let t of r)t(e);return e}var uL,ne,At=M(()=>{"use strict";_n();YS();ZS();XS();tk();rk();Rb();ak();lk();pk();hk();mk();yk();wk();xk();uL=[HS,JS,QS,ek,nk,ok,ik,uk,ck,fk,dk,gk,bk,Tk];l(lL,"buildRegistry");ne=lL()});function wo(r,e,t="ignore"){let n=[],s=new Map;for(let o of r){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),n.push(u),i.pop()}}return n}function zh(r,e="throw"){let t=Array.isArray(r)?r:[...r],n=new Set(t);return wo(t,s=>n.has(s),e)}var ji=M(()=>{"use strict";l(wo,"topoSortByOperands");l(zh,"topoSortOpSet")});function Sk(r){return Array.isArray(r)?r.map(Sk):r}function kk(r){if(typeof r=="number")return r*2654435761&2147483647;if(typeof r=="string"){let e=0;for(let t=0;t<r.length;t++)e=(e<<5)-e+r.charCodeAt(t)&2147483647;return e}if(typeof r=="boolean")return r?1:0;if(Array.isArray(r)){let e=2654435769;for(let t=0;t<r.length;t++)e=(e^kk(r[t]))*16777619&2147483647;return e}if(ArrayBuffer.isView(r)&&r.buffer instanceof ArrayBuffer){let e=new Uint8Array(r.buffer,r.byteOffset,r.byteLength),t=2654435769;t=(t^e.length)*16777619&2147483647;let n=e.length>256?Math.ceil(e.length/256):1;for(let s=0;s<e.length;s+=n)t=(t^e[s])*16777619&2147483647;return t}return typeof r=="object"&&r!==null&&typeof r.hash=="function"?r.hash():0}function vk(r,e){if(r===e)return!0;if(typeof r!=typeof e)return!1;if(Array.isArray(r)){if(!Array.isArray(e)||r.length!==e.length)return!1;for(let t=0;t<r.length;t++)if(!vk(r[t],e[t]))return!1;return!0}return typeof r=="object"&&r!==null&&typeof r.equals=="function"?r.equals(e):!1}function pL(r,e=new Map){let t=new Ct;for(let n of r.blocks){let s=n.arguments.map(p=>p.type),o=new n.constructor(s);for(let p=0;p<n.arguments.length;p++)e.set(n.arguments[p],o.arguments[p]);let i=n.opsArray(),a=new Set(i),u=new Map;for(let p of wo(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 cL,Re,sr=M(()=>{"use strict";ch();zi();At();ji();cL=0,Re=class r{static{l(this,"Operation")}constructor(e,t,n,s=null,o=null){if(this.opName=e,this.id=cL++,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 ic(this,i);t[i].addUse(a),this._operandLinks[i]=a}this.results=new Array(n.length);for(let i=0;i<n.length;i++)this.results[i]=new Wo(n[i],this,i);if(this.regions=[],o)for(let i=0;i<o.length;i++){let a=o[i]instanceof Ct?o[i]:new Ct;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 Wo))throw new Error(`replaceOperand: new operand for '${this.opName}' is not a Value`);let n=this.operands[e];if(n===t)return;n.removeUse(this._operandLinks[e]),this.operands[e]=t;let s=new ic(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),n=this.regions.map(i=>pL(i,e)),s=new Map;for(let[i,a]of this.attributes)s.set(i,Sk(a));let o=new r(this.opName,t,this.results.map(i=>i.type),s,n);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,n]of this.attributes){for(let s=0;s<t.length;s++)e=(e^t.charCodeAt(s))*16777619&2147483647;e=(e^kk(n))*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,n]of this.attributes)if(!e.attributes.has(t)||!vk(n,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(Sk,"cloneAttrValue");l(kk,"hashAttrValue");l(vk,"attrValueEquals");l(pL,"cloneRegion")});function*Nk(r){if(!(!r.regions||r.regions.length===0))for(let e of r.regions)for(let t of e.blocks)for(let n of t.ops())yield n,yield*Nk(n)}function*Ak(r){if(!(!r.regions||r.regions.length===0))for(let e of r.regions)for(let t of e.blocks){yield t;for(let n of t.ops())yield*Ak(n)}}function fL(r){let e=r.opsArray(),t=new Set(e);return wo(e,n=>t.has(n),"ignore")}function Vh(r){let e=new un(r.name,r.inputTypes,r.outputTypes),t=new Map,n=r.entryBlock,s=e.entryBlock;for(let i=0;i<n.arguments.length;i++)t.set(n.arguments[i],s.arguments[i]);let o=new Map;for(let i of fL(n))o.set(i,i.clone(t));for(let i of n)s.pushOp(o.get(i));return e._version=r._version,e}var un,xs=M(()=>{"use strict";zi();ji();l(Nk,"opsInRegions");l(Ak,"blocksInRegions");un=class{static{l(this,"GraphFunction")}constructor(e,t,n){this.name=e,this.inputTypes=Object.freeze([...t]),this.outputTypes=Object.freeze([...n]),this.body=new Ct;let s=new yn(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*Nk(e)}*blocksRecursive(){for(let e of this.body){yield e;for(let t of e)yield*Ak(t)}}opsArray(){let e=[];for(let t of this.body)for(let n of t)e.push(n);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 n of this.ops())e(n)&&t.push(n);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(fL,"topoOrderTopLevel");l(Vh,"cloneGraphFunction")});function Ok(r){let e=new wn(r.name);for(let t of r)e.addFunction(Vh(t));return e._version=r._version,e}var wn,Yo=M(()=>{"use strict";xs();wn=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 n=t.verify();for(let s=0;s<n.length;s++)e.push(`${t.name}: ${n[s]}`)}return e}};l(Ok,"cloneGraphModule")});function Bk(r){return r?r instanceof Fi?`tuple(${r.types.map(Bk).join(", ")})`:r.shape!==void 0&&r.dtype!==void 0?`[${r.shape.join(",")}]:${r.dtype}`:String(r):"?"}function Rk(r,e,t){let n=r.rank,s=e<0?n+e:e,o=r.shape.map((a,u)=>u===s?1:a),i=[];for(let a=0;a<n;a++)a!==s&&i.push(a<s?a:a-1+t);return{offsetDims:i,collapsedSliceDims:[s],startIndexMap:[s],indexVectorDim:t,sliceSizes:o}}function hL(r,e){let t=Math.max(r.length,e.length),n=new Array(t);for(let s=0;s<t;s++){let o=s<r.length?r[r.length-1-s]:1,i=s<e.length?e[e.length-1-s]:1;n[t-1-s]=o===1?i:o}return n}function To(r,e){let t=[];for(let n=0;n<r;n++)n!==e&&t.push(n);return t}function dL(r){if(!r)return new Map;if(r instanceof Map)return r;let e=new Map;for(let t of Object.keys(r))e.set(t,r[t]);return e}function hc(r,e,t,n){let s=new un(r,e,t),o=new An(s);return n(o,s.args),s}var An,es=M(()=>{"use strict";Oe();sr();zi();xs();Yo();At();l(Bk,"describeType");l(Rk,"indexSelectGatherOpts");l(hL,"bcastBatchDims");An=class r{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,n,s=null,o=null){let i=new Re(e,t,n,s,o);return this._insert(i)}_inferAndBuild(e,t,n=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=n instanceof Map?n:dL(n);a=i.inferResultTypes(u,p,o)}if(!a){let u=t.map(c=>Bk(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,n,s)}constant(e,t){return this._buildOp("constant",[],[t],{value:e,tensor_type:t})}scalarConstant(e,t="f32"){let n=new I([],t);return this.constant(e,n)}tensorConstant(e,t,n="f32"){let s=new I(t,n);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,n,s,o=!1){return this._inferAndBuild("scaled_dot_product_attention",[e,t,n],{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,n){return this._inferAndBuild("compare",[e,t],{direction:n})}select(e,t,n){return this._inferAndBuild("select",[e,t,n])}clamp(e,t,n){return this._inferAndBuild("clamp",[e,t,n])}broadcast(e,t,n){return this._inferAndBuild("broadcast_in_dim",[e],{result_shape:t,broadcast_dimensions:n})}reshape(e,t){return this._inferAndBuild("reshape",[e],{new_shape:t})}transpose(e,t){return this._inferAndBuild("transpose",[e],{permutation:t})}slice(e,t,n,s=null){let o={starts:t,limits:n};return s&&(o.strides=s),this._inferAndBuild("slice",[e],o)}concat(e,t){return this._inferAndBuild("concat",e,{dimension:t})}pad(e,t,n,s,o=null){let i={low:n,high:s};return o&&(i.interior=o),this._inferAndBuild("pad",[e,t],i)}reduce(e,t,n,s){let o=new I([],e.type.dtype),i=new Ct,a=new yn([o,o]);return i.addBlock(a),this._inferAndBuild("reduce",[e,t],{dimensions:n,reduce_type:s},[i])}dot(e,t,n,s,o=[],i=[]){return this._inferAndBuild("dot",[e,t],{lhs_contracting:n,rhs_contracting:s,lhs_batch:o,rhs_batch:i})}matmul(e,t){let n=e.type.rank,s=t.type.rank;if(n===1&&s===1)return this.dot(e,t,[0],[0]);let o=e,i=t,a=!1,u=!1;n===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,h=i.type.shape.slice(0,c-2),f;if(h.length===0)f=this.dot(o,i,[p-1],[0]);else{let g=o.type.shape.slice(0,p-2),y=hL(g,h),_=y.length,b=this._broadcastBatch(o,g,y),w=this._broadcastBatch(i,h,y),T=Array.from({length:_},(S,x)=>x);f=this.dot(b,w,[_+1],[_],T,T)}if(!a&&!u)return f;let d=f.getResult(0).type.shape,m=new Set;return u&&m.add(d.length-1),a&&m.add(d.length-2),this.reshape(f.getResult(0),d.filter((g,y)=>!m.has(y)))}_broadcastBatch(e,t,n){let s=e.type.shape.slice(e.type.rank-2),o=[...n,...s];if(t.length===n.length&&t.every((u,p)=>u===n[p]))return e;let i=n.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,n,s,o={}){return this._inferAndBuild("conv",[e,t],{strides:n,padding:s,dilation:o.dilation||n.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,n,s=null){let o={call_target_name:e};return s&&(o.backend_config=s),this._buildOp("custom_call",t,n,o)}fusion(e,t,n,s){let o=new Ct,i=new yn(e.map(u=>u.type));o.addBlock(i);let a=this._buildOp("fusion",e,t,{fusion_kind:n},[o]);if(s){let u=new r(this.func);u.block=i,s(u,i.arguments)}return a}ifOp(e,t,n,s){let o=new Ct,i=new yn([]);o.addBlock(i);let a=new Ct,u=new yn([]);a.addBlock(u);let p=this._buildOp("if",[e],t,null,[o,a]);if(n){let c=new r(this.func);c.block=i,n(c)}if(s){let c=new r(this.func);c.block=u,s(c)}return p}whileOp(e,t,n){let s=e.map(c=>c.type),o=new Ct,i=new yn(s);o.addBlock(i);let a=new Ct,u=new yn(s);a.addBlock(u);let p=this._buildOp("while",e,s,null,[o,a]);if(t){let c=new r(this.func);c.block=i,t(c,i.arguments)}if(n){let c=new r(this.func);c.block=u,n(c,u.arguments)}return p}scanOp(e,t,n){let s=e.map(g=>g.type.withShape(g.type.shape.slice(1))),o=t.map(g=>g.type),i=new Ct,a=new yn([...s,...o]);i.addBlock(a);let u=new r(this.func);u.block=a;let p=a.arguments.slice(0,s.length),c=a.arguments.slice(s.length),[h,f]=n(u,p,c);if(u.yieldOp([...h,...f]),e.length===0)throw new Error("scanOp requires at least one xs input");let d=e[0].type.shape[0];if(typeof d!="number"||d<0)throw new Error(`scanOp requires a static, non-negative leading dim on xs, got ${d}`);for(let g=1;g<e.length;g++)if(e[g].type.shape[0]!==d)throw new Error("scanOp requires all xs inputs to share the same leading length");let m=f.map(g=>g.type.withShape([d,...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),n=this.broadcast(t.getResult(0),e.type.shape,[]);return this.maximum(e,n.getResult(0))}softmax(e,t=-1){let n=t<0?e.type.rank+t:t;return this._inferAndBuild("softmax",[e],{axis:n})}logSoftmax(e,t=-1){let n=t<0?e.type.rank+t:t;return this._inferAndBuild("log_softmax",[e],{axis:n})}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,n,s=-1,o=1e-5){let i=s<0?e.type.rank+s:s;return this._inferAndBuild("layer_norm",[e,t,n],{axis:i,epsilon:o})}batchnorm(e,t,n,s,o,i=1,a=1e-5){return this._inferAndBuild("batch_norm",[e,t,n,s,o],{axis:i,epsilon:a})}where(e,t,n){return this._inferAndBuild("where",[e,t,n])}split(e,t,n){return this._inferAndBuild("split",[e],{dimension:t,split_sizes:n})}oneHot(e,t,n={}){return this._inferAndBuild("one_hot",[e],{depth:t,axis:n.axis??-1,on_value:n.onValue??1,off_value:n.offValue??0,dtype:n.dtype||e.type.dtype})}embedding(e,t){return this._inferAndBuild("embedding",[e,t])}gather(e,t,n){return this._inferAndBuild("gather",[e,t],{offset_dims:n.offsetDims,collapsed_slice_dims:n.collapsedSliceDims,start_index_map:n.startIndexMap,index_vector_dim:n.indexVectorDim,slice_sizes:n.sliceSizes})}scatter(e,t,n,s){let o=new I([],e.type.dtype),i=new Ct;return i.addBlock(new yn([o,o])),this._inferAndBuild("scatter",[e,t,n],{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,n,s){return this.scatter(e,t,n,s)}_dimCoordIndices(e,t,n){let s=n.length,o=e.type.dtype==="i32"?e:this.convert(e,"i32").getResult(0),i=[...n,1],a=[];for(let u=0;u<s;u++){let p=u===t?o:this.iota(u,new I(n,"i32")).getResult(0);a.push(this.reshape(p,i).getResult(0))}return this.concat(a,s).getResult(0)}gatherDim(e,t,n){let s=e.type.rank,o=n<0?s+n:n,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,n,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,n,{updateWindowDims:[],insertedWindowDims:a,scatterDimsToOperandDims:a,indexVectorDim:o})}argmax(e,t,n=!1){let s=t<0?e.type.rank+t:t;return this._inferAndBuild("argmax",[e],{axis:s,keep_dims:n})}argmin(e,t,n=!1){let s=t<0?e.type.rank+t:t;return this._inferAndBuild("argmin",[e],{axis:s,keep_dims:n})}pool2d(e,t,n,s,o,i={}){return this._inferAndBuild("pool2d",[e],{pool_type:t,kernel_size:n,strides:s,padding:o,ceil_mode:i.ceilMode||!1,count_include_pad:i.countIncludePad||!1,layout:i.layout||"NCHW"})}resize(e,t,n,s={}){return this._inferAndBuild("resize",[e],{output_size:t,method:n,coordinate_mode:s.coordinateMode||"asymmetric",layout:s.layout||"NCHW"})}};l(To,"broadcastDimsExcluding");l(dL,"toMap");l(hc,"buildFunction")});function In(r,e,t){if(t==null)return new C(r,e,t);let n=e instanceof L,s=t instanceof L;if(n&&s){let o=e.value,i=t.value;switch(r){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((r==="+"||r==="-")&&o===0||r==="*"&&o===1)return e;if(r==="*"&&o===0)return new L(0);if(r==="//"&&o===1)return e;if(r==="%"&&o===1)return new L(0)}if(n){let o=e.value;if(r==="+"&&o===0||r==="*"&&o===1)return t;if(r==="*"&&o===0)return new L(0)}return new C(r,e,t)}var F,Tt,Jo,de,pe,Ib,qi,Y,q,xe,Ye,Ve,te,Zo,ou,On,iu,ke,C,qe,rt,Ne,L,fe,De=M(()=>{"use strict";F=Object.freeze({SERIAL:"serial",PARALLEL:"parallel",VECTORIZED:"vectorized",UNROLLED:"unrolled",THREAD_BINDING:"thread_binding",RECURRENCE:"recurrence"}),Tt=class r{static{l(this,"TensorNode")}constructor(){this.type=this.constructor.name.replace(/^_+/,""),this._parent=null,this._parentKey=null,this._parentIdx=-1}_setChild(e,t,n=-1){t instanceof r&&(t._parent=this,t._parentKey=e,t._parentIdx=n)}_setChildren(e,t){if(t)for(let n=0;n<t.length;n++)t[n]instanceof r&&(t[n]._parent=this,t[n]._parentKey=e,t[n]._parentIdx=n)}replaceWith(e){let t=this._parent;return t?(this._parentIdx>=0?t[this._parentKey][this._parentIdx]=e:t[this._parentKey]=e,e instanceof r&&(e._parent=t,e._parentKey=this._parentKey,e._parentIdx=this._parentIdx),this._parent=null,this._parentKey=null,this._parentIdx=-1,!0):!1}},Jo=class extends Tt{static{l(this,"PrimFunc")}constructor(e,t,n,s=new Map,o=[],i=null){super(),this.name=e,this.params=t,this.body=n,this.bufferMap=s,this.shapeParams=o,this.shapeParamMap=i||new Map,this._setChild("body",n)}},de=class extends Tt{static{l(this,"ForNode")}constructor(e,t,n,s,o,i=null){super(),this.loopVar=e,this.min=t,this.extent=n,this.kind=s,this.body=o,this.threadTag=i,this._setChild("body",o)}},pe=class extends Tt{static{l(this,"BlockNode")}constructor(e,t,n,s,o,i=null){super(),this.name=e,this.iterVars=t,this.reads=n,this.writes=s,this.body=o,this.initBody=i,this._setChild("body",o),this._setChild("initBody",i)}},Ib=Object.freeze({DATA_PAR:"DataPar",COMM_REDUCE:"CommReduce"}),qi=class extends Tt{static{l(this,"BlockRealizeNode")}constructor(e,t,n=Ib.DATA_PAR){super(),this.iterVar=e,this.binding=t,this.kind=n}},Y=class extends Tt{static{l(this,"BufferStoreNode")}constructor(e,t,n){super(),this.buffer=e,this.indices=t,this.value=n}},q=class extends Tt{static{l(this,"BufferLoadNode")}constructor(e,t){super(),this.buffer=e,this.indices=t}},xe=class extends Tt{static{l(this,"IfThenElseNode")}constructor(e,t,n=null){super(),this.condition=e,this.thenBody=t,this.elseBody=n,this._setChild("thenBody",t),this._setChild("elseBody",n)}},Ye=class extends Tt{static{l(this,"LetStmtNode")}constructor(e,t,n){super(),this.variable=e,this.value=t,this.body=n,this._setChild("body",n)}},Ve=class extends Tt{static{l(this,"AllocateNode")}constructor(e,t,n){super(),this.buffer=e,this.scope=t,this.body=n,this._setChild("body",n)}},te=class extends Tt{static{l(this,"SeqNode")}constructor(e){super(),this.stmts=e,this._setChildren("stmts",e)}},Zo=class extends Tt{static{l(this,"WhileNode")}constructor(e,t,n){super(),this.condVar=e,this.condBody=t,this.loopBody=n,this._setChild("condBody",t),this._setChild("loopBody",n)}},ou=class extends Tt{static{l(this,"EvaluateNode")}constructor(e){super(),this.value=e}},On=class extends Tt{static{l(this,"SyncThreadsNode")}constructor(){super()}},iu=class extends Tt{static{l(this,"VecCopyNode")}constructor(e,t,n,s,o){super(),this.dstBuffer=e,this.dstIndex=t,this.srcBuffer=n,this.srcIndex=s,this.width=o}},ke=class extends Tt{static{l(this,"CallExternNode")}constructor(e,t,n){super(),this.externName=e,this.args=t,this.dtype=n}},C=class extends Tt{static{l(this,"MathOpNode")}constructor(e,t,n=null){super(),this.op=e,this.a=t,this.b=n}},qe=class extends Tt{static{l(this,"CompareNode")}constructor(e,t,n){super(),this.direction=e,this.a=t,this.b=n}},rt=class extends Tt{static{l(this,"CastNode")}constructor(e,t,n){super(),this.expr=e,this.fromDtype=t,this.toDtype=n}},Ne=class extends Tt{static{l(this,"VariableNode")}constructor(e,t){super(),this.name=e,this.dtype=t}},L=class extends Tt{static{l(this,"IntImmNode")}constructor(e){super(),this.value=e}},fe=class extends Tt{static{l(this,"FloatImmNode")}constructor(e){super(),this.value=e}};l(In,"mathOp")});var au,Eb=M(()=>{"use strict";au=Object.freeze({GLOBAL:"global",SHARED:"shared",LOCAL:"local",REGISTER:"register"})});var me,xo=M(()=>{"use strict";Vn();Oe();me=class{static{l(this,"Buffer")}constructor(e,t,n,s,o=null,i=0,a=64){if(this.name=e,this.shape=t,this.dtype=n,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 oc(this.shape,-1)}symbolicNumel(){return mb(this.shape)}sizeInBytes(){let e=this.numel();return e<0?-1:e*bn(this.dtype)}}});function mL(r,e,t){switch(r){case"add":return new C("+",e,t);case"sub":return new C("-",e,t);case"mul":return new C("*",e,t);case"div":return new C("//",e,t);case"mod":return new C("%",e,t);case"neg":return new C("-",e);case"ceildiv":return new C("//",new C("-",new C("+",e,t),new L(1)),t);default:throw new Error(`symIntToNode: unsupported op '${r}' in extent/index context`)}}function uu(r,e){if(typeof r=="number")return new L(r);if(!(r instanceof Ce))return new L(r);if(r.type==="var")return e(r.name);let t=uu(r.args[0],e),n=r.args.length>1?uu(r.args[1],e):null;return mL(r.type,t,n)}var Lb=M(()=>{"use strict";Vs();De();l(mL,"symOpToNode");l(uu,"symIntToNode")});function ln(r){let e=ne.get(r);return e!==null&&e.isConstant}function qt(r){let e=ne.get(r);return e!==null&&e.isTerminator}function lu(r){let e=ne.get(r);return e!==null&&e.isBroadcast}var Tr=M(()=>{"use strict";At();l(ln,"isConstantOp");l(qt,"isTerminatorOp");l(lu,"isBroadcastOp")});function qh(r,{name:e,compute:t,plevel:n=10,targetKind:s=null}){let o=Cb.get(r);return o||(o=new dc(r),Cb.set(r,o)),o.addImplementation(e,t,n,s),Gh.clear(),o}function Uh(r,e=null){let t=Cb.get(r);if(!t)return null;let n=e?e.kind:null,s=`${r}|${n===null?"":n}`;if(Gh.has(s))return Gh.get(s);let o=new dc(r);for(let a of t.implementations)(a.targetKind===null||a.targetKind===n)&&o.implementations.push(a);let i=o.implementations.length>0?o:null;return Gh.set(s,i),i}function Db(r,e=null){let t=Uh(r,e);return t?t.best():null}var jh,dc,Cb,Gh,Pb=M(()=>{"use strict";jh=class{static{l(this,"OpImplementation")}constructor(e,t,n=10,s=null){this.name=e,this.compute=t,this.plevel=n,this.targetKind=s}},dc=class{static{l(this,"OpStrategy")}constructor(e){this.opName=e,this.implementations=[]}addImplementation(e,t,n=10,s=null){let o=new jh(e,t,n,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}},Cb=new Map,Gh=new Map;l(qh,"registerOpStrategy");l(Uh,"getOpStrategy");l(Db,"selectImplementation")});function Fb(r,e){return Uh(r,e)?!0:ln(r)}function Me(r,e,t=gL){qh(r,{name:`${r}.generic`,compute:e,plevel:t,targetKind:null})}function Ik(r,e,t,n=yL){qh(r,{name:`${r}.${e}`,compute:t,plevel:n,targetKind:e})}function Qo(r,e,t=null){if(t){let s=t.getLoweringRule(r);if(s)return s}let n=Db(r,e);return n?n.compute:void 0}function xt(r,e,t){let n=e.length,s=r.allocVarArray("i",n),o=r.allocBindArray("v",s),i=new Array(n);for(let u=0;u<n;u++)i[u]=o[u].iterVar;let a=t?r.extentNodes(e,t):null;return{loopVars:s,loopBinds:o,indices:i,extentNodes:a}}function gc(r,e,t){let n=r;for(let s=e.length-1;s>=0;s--)n=new de(e[s],new L(0),t[s],F.SERIAL,n);return n}function _L(r,e,t){let n=r;for(let s=e.length-1;s>=0;s--)n=new de(e[s],new L(0),new L(t[s]),F.SERIAL,n);return n}function St(r,e,t,n){return n?gc(r,e,n):_L(r,e,t)}function yc(r,e,t,n,s,{prefix:o,initBlockName:i,accBlockName:a,initVal:u,accLeaf:p}){let c=bL(r,e,t,n),h=or(r,o,Array.from({length:s.shape.length},(A,N)=>N),s.shape,s),f=new Y(s,h.indices,u()),d=new pe(r.blockName(i),h.ivs,[],[{buffer:s}],f),m=h.wrap(d),g=new q(t,c.lhsIdx),y=new q(n,c.rhsIdx),_=e.getAttr("lhs_prologue_cast"),b=e.getAttr("rhs_prologue_cast");_&&(g=new rt(g,t.dtype,_)),b&&(y=new rt(y,n.dtype,b));let w=p(g,y),T=new C("+",new q(s,c.outIdx),w),S=new Y(s,c.outIdx,T),x=new pe(r.blockName(a),c.allIvs,[{buffer:t},{buffer:n}],[{buffer:s}],S),k=c.wrapAccBody(x);return{geo:c,initBody:m,accBody:k}}function Hh(r,e,t,n,s,{prefix:o,blockPrefix:i,initVal:a,guardFill:u,leafBuilder:p}){let c=e.getAttr("strides"),h=e.getAttr("padding"),f=e.getAttr("dilation")||c.map(()=>1),d=e.getAttr("groups")||1,m=Wh(e.getAttr("input_layout")),g=Wh(e.getAttr("kernel_layout")),y=c.length,_=t.shape[m.N],b=n.shape[g.O],w=n.shape[g.I],T=s.shape,S=or(r,o+"i",Array.from({length:T.length},(ye,He)=>He),T,s),x=new Y(s,S.indices,a()),k=new pe(r.blockName(i+"_init"),S.ivs,[],[{buffer:s}],x),A=S.wrap(k),N=r.allocVar(o+"n"),v=r.allocVar(o+"oc"),O=r.allocVar(o+"ic"),R=r.allocVarArray(o+"o",y),B=r.allocVarArray(o+"k",y),E=[N,v,...R,O,...B],z=r.allocBindArray(o+"v",E),D=z[0].iterVar,J=z[1].iterVar,j=z.slice(2,2+y),U=z[2+y].iterVar,P=z.slice(3+y),V=new Array(T.length);V[m.N]=D,V[m.C]=J;let he=Object.keys(m).filter(ye=>ye!=="N"&&ye!=="C").sort();for(let ye=0;ye<y;ye++)V[m[he[ye]]]=j[ye].iterVar;let le=new Array(t.shape.length);le[m.N]=D;let Ie=Math.floor(b/d);d>1?le[m.C]=new C("+",new C("*",new C("//",J,new L(Ie)),new L(w)),U):le[m.C]=U;let G=new Array(n.shape.length);G[g.O]=J,G[g.I]=U;let K=null;for(let ye=0;ye<y;ye++){let He=he[ye],gn=He.toUpperCase(),jt=In("+",In("*",j[ye].iterVar,new L(c[ye])),In("+",In("*",P[ye].iterVar,new L(f[ye])),new L(-h[ye][0])));if(le[m[He]]=jt,G[g[gn]]=P[ye].iterVar,h[ye][0]!==0||h[ye][1]!==0){let Zt=new qe("ge",jt,new L(0)),Qt=new qe("lt",jt,new L(t.shape[m[He]])),on=new C("*",Zt,Qt);K=K?new C("*",K,on):on}}let ie=p(le,G),ze=K?new xe(K,ie,u()):ie,Se=new q(s,V),Qe=new C("+",Se,ze),It=new Y(s,V,Qe),Jt=new pe(r.blockName(i+"_acc"),z,[{buffer:t},{buffer:n}],[{buffer:s}],It),Gt=new Array(y);for(let ye=0;ye<y;ye++){let He=he[ye].toUpperCase();Gt[ye]=n.shape[g[He]]}let tt=Jt;for(let ye=y-1;ye>=0;ye--){let He=he[ye].toUpperCase();tt=new de(B[ye],new L(0),r.extentNode(Gt[ye],n,g[He]),F.SERIAL,tt)}tt=new de(O,new L(0),r.extentNode(w,n,g.I),F.SERIAL,tt);for(let ye=y-1;ye>=0;ye--){let He=m[he[ye]];tt=new de(R[ye],new L(0),r.extentNode(T[He],s,He),F.SERIAL,tt)}return tt=new de(v,new L(0),r.extentNode(b,n,g.O),F.SERIAL,tt),tt=new de(N,new L(0),r.extentNode(_,t,m.N),F.SERIAL,tt),new te([A,tt])}function or(r,e,t,n,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]=r.allocVar(`${e}${t[c]}`),a[c]=new qi(r.allocVar(`${e}v${t[c]}`),i[c]),u[c]=a[c].iterVar,p[c]=r.extentNode(n[t[c]],s,t[c]);return{vars:i,ivs:a,indices:u,extentNodes:p,wrap(c){return gc(c,i,p)}}}function So(r,e,t){let n=r.shape.length;if(r.broadcastDims){let a=r.broadcastDims,u=new Array(n);for(let p=0;p<n;p++)u[p]=r.shape[p]===1?new L(0):t[a[p]];return u}let o=e.shape.length-n,i=new Array(n);for(let a=0;a<n;a++)i[a]=r.shape[a]===1?new L(0):t[o+a];return i}function cu(r){let e=new Array(r.length);for(let t=0;t<r.length;t++)e[t]={buffer:r[t]};return e}function mc(){let r=0;for(let n=0;n<arguments.length;n++)r+=arguments[n].length;let e=new Array(r),t=0;for(let n=0;n<arguments.length;n++){let s=arguments[n];for(let o=0;o<s.length;o++)e[t++]=s[o]}return e}function $b(r){let e=new Array(r.length);for(let t=0;t<r.length;t++)e[t]=r[t].iterVar;return e}function qs(r,e,t,n,s){let o=n[0],{loopVars:i,loopBinds:a,indices:u,extentNodes:p}=xt(r,o.shape,o),c=new Array(t.length);for(let m=0;m<t.length;m++){let g=So(t[m],o,u);c[m]=new q(t[m],g)}let h=s(e,c,o.dtype),f=new Y(o,u,h),d=new pe(r.blockName(`${e.opName}_block`),a,cu(t),[{buffer:o}],f);return St(d,i,o.shape,p)}function pu(r,e){let t=e.getResult(0),n=e.getAttr("value"),s=t.type&&t.type.shape||[];if(typeof n=="number"&&s.length>0&&!r.bufferMap.has(t)){let g=t.type&&t.type.dtype||"f32",y=new Array(s.length).fill(1),_=new me(`buf_${r.varCounter++}`,y,g,au.GLOBAL);_.broadcastDims=Array.from({length:s.length},(w,T)=>T),r.bufferMap.set(t,_);let b=nr(g)?new L(n):new fe(n);return new Y(_,y.map(()=>new L(0)),b)}let o=r.getOrAllocBuffer(t),i=nr(o.dtype),a=l(g=>i?new L(g):new fe(g),"imm");if(n&&typeof n!="number"&&typeof n.length=="number"){if(o.shape.length===0)return new Y(o,[],a(n[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<n.length;b++){let w=new Array(o.shape.length);for(let T=0;T<o.shape.length;T++)w[T]=new L(Math.floor(b/g[T])%o.shape[T]);_.push(new Y(o,w,a(n[b])))}return new te(_)}let u=a(typeof n=="number"?n:0);if(o.shape.length===0)return new Y(o,[],u);let{loopVars:p,loopBinds:c,indices:h,extentNodes:f}=xt(r,o.shape,o),d=new Y(o,h,u),m=new pe(r.blockName(`${e.opName}_block`),c,[],[{buffer:o}],d);return St(m,p,o.shape,f)}function Wh(r){let e={};for(let t=0;t<r.length;t++)e[r[t]]=t;return e}function Mk(r,e){if(!r.broadcastDims)return e;let t=r.broadcastDims,n=new Array(r.shape.length);for(let s=0;s<r.shape.length;s++)n[s]=r.shape[s]===1?new L(0):e[t[s]];return n}function bL(r,e,t,n){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),h=new Set(i),f=new Set(o),d=new Set(a),m=[];for(let D=0;D<u.length;D++)!c.has(D)&&!h.has(D)&&m.push(D);let g=[];for(let D=0;D<p.length;D++)!f.has(D)&&!d.has(D)&&g.push(D);let y=r.allocVarArray("b",i.length),_=r.allocVarArray("ls",m.length),b=r.allocVarArray("rs",g.length),w=r.allocVarArray("c",s.length),T=r.allocBindArray("vb",y),S=r.allocBindArray("vls",_),x=r.allocBindArray("vrs",b),k=r.allocBindArray("vc",w),A=mc($b(T),$b(S),$b(x)),N=new Array(u.length);for(let D=0;D<i.length;D++)N[i[D]]=T[D].iterVar;for(let D=0;D<m.length;D++)N[m[D]]=S[D].iterVar;for(let D=0;D<s.length;D++)N[s[D]]=k[D].iterVar;let v=new Array(p.length);for(let D=0;D<a.length;D++)v[a[D]]=T[D].iterVar;for(let D=0;D<g.length;D++)v[g[D]]=x[D].iterVar;for(let D=0;D<o.length;D++)v[o[D]]=k[D].iterVar;let O=Mk(t,N),R=Mk(n,v),B=mc(T,S,x,k),E=[{vars:y,dims:i,shape:u,buf:t},{vars:_,dims:m,shape:u,buf:t},{vars:b,dims:g,shape:p,buf:n},{vars:w,dims:s,shape:u,buf:t}];function z(D){let J=D;for(let j=E.length-1;j>=0;j--){let{vars:U,dims:P,shape:V,buf:he}=E[j];for(let le=U.length-1;le>=0;le--){let Ie=he.broadcastDims?new L(V[P[le]]):r.extentNode(V[P[le]],he,P[le]);J=new de(U[le],new L(0),Ie,F.SERIAL,J)}}return J}return l(z,"wrapAccBody"),{outIdx:A,lhsIdx:O,rhsIdx:R,allIvs:B,wrapAccBody:z}}var gL,yL,Kh,Fr=M(()=>{"use strict";Oe();Vs();Eb();xo();Vn();De();Lb();Tr();Pb();Pb();gL=10,yL=20;l(Fb,"hasLoweringRule");l(Me,"registerLoweringRule");l(Ik,"registerTargetLoweringRule");l(Qo,"getLoweringRule");Kh=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 Ne(`${e}_${this.varCounter++}`,t)}getOrAllocBuffer(e){let t=this.bufferMap.get(e);if(t)return t;let n=e.type,s=n.shape||[],o=n.dtype||"f32",i=n.layout?n.layout.computeStrides(s):null;return t=new me(`buf_${this.varCounter++}`,s,o,au.GLOBAL,i),e.symbolicShape&&(t.symbolicShape=e.symbolicShape),this.bufferMap.set(e,t),this._registerDynamicDims(t),t}allocFreshBuffer(e){let t=e.type,n=t.shape||[],s=t.dtype||"f32",o=t.layout?t.layout.computeStrides(n):null,i=new me(`buf_${this.varCounter++}`,n,s,au.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 n=e.shape[t];n===X?this.extentNode(X,e,t):n instanceof Ce&&this._registerSymIntDim(e,t,n)}}_symVarNode(e){let t=this.symVars.get(e);return t||(t=new Ne(qa(e),"int32"),this.symVars.set(e,t)),t}_registerSymIntDim(e,t,n){for(let s of Ce.freeVars(n))this._symVarNode(s);if(n.type==="var"){let s=`${e.name}:${t}`;this.shapeParams.has(s)||this.shapeParams.set(s,this._symVarNode(n.name))}}symIntToExtentNode(e){return uu(e,t=>this._symVarNode(t))}_shapeParamVar(e,t){let n=t>=0?`${e.name}:${t}`:`${e.name}:dyn`,s=this.shapeParams.get(n);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(n,s),s}extentNode(e,t,n=-1){return e instanceof Ce?this.symIntToExtentNode(e):e!==X?new L(e):this._shapeParamVar(t,n)}extentNodes(e,t){let n=new Array(e.length);for(let s=0;s<e.length;s++)n[s]=this.extentNode(e[s],t,s);return n}allocVarArray(e,t){let n=new Array(t);for(let s=0;s<t;s++)n[s]=this.allocVar(`${e}${s}`);return n}allocBindArray(e,t){let n=new Array(t.length);for(let s=0;s<t.length;s++)n[s]=new qi(this.allocVar(`${e}${s}`),t[s]);return n}};l(xt,"makeLoopNest");l(gc,"wrapLoopsWithNodes");l(_L,"wrapLoops");l(St,"wrapInLoops");l(yc,"emitMatmulInitAcc");l(Hh,"buildConvNest");l(or,"buildSpatialNest");l(So,"computeBroadcastIndices");l(cu,"bufRefs");l(mc,"concatIterVars");l($b,"extractIterVars");l(qs,"lowerPointwise");l(pu,"lowerConstant");l(Wh,"parseLayout");l(Mk,"physicalDotIndices");l(bL,"buildDotGeometry")});function kL(r){let e=ne.get(r);return e?e.getAttr(zb):null}function _c(){let r=[];for(let e of ne.allOps())e.hasAttr(zb)&&r.push(e.name);return r}function Vb(r,e,t){let n=kL(r);return n?Ek[r]?Ek[r](e):e.length===2&&wL.has(n)?new C(n,e[0],e[1]):e.length===2&&TL.has(n)?new C(n,e[0],e[1]):e.length===1&&xL.has(n)?new C(n,e[0]):e.length===1&&n==="-"?new C("-",e[0]):new ke(n,e,t):null}function Lk(){for(let r of _c())Me(r,(e,t,n,s)=>qs(e,t,n,s,(o,i,a)=>Vb(o.opName,i,a)));Me("compare",(r,e,t,n)=>qs(r,e,t,n,(s,o)=>new qe(s.getAttr("direction")||"eq",o[0],o[1]))),Me("select",(r,e,t,n)=>qs(r,e,t,n,(s,o)=>new xe(o[0],o[1],o[2]))),Me("clamp",(r,e,t,n)=>qs(r,e,t,n,(s,o,i)=>new ke("min",[new ke("max",[o[1],o[0]],i),o[2]],i))),Me("convert",(r,e,t,n)=>qs(r,e,t,n,(s,o)=>new rt(o[0],t[0].dtype,n[0].dtype))),Me("copy_to_device",(r,e,t,n)=>qs(r,e,t,n,(s,o)=>o[0]))}var wL,TL,xL,Ek,zb,SL,Yh=M(()=>{"use strict";De();At();Fr();wL=new Set(["+","-","*","/"]),TL=new Set(["&&","||"]),xL=new Set(["!"]),Ek={square:l(r=>new C("*",r[0],r[0]),"square"),reciprocal:l(r=>new C("/",new fe(1),r[0]),"reciprocal")},zb="elementwiseScalarOp",SL={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[r,e]of Object.entries(SL))ne.has(r)&&ne.registerOpAttr(r,zb,e);l(kL,"elementwiseScalarOp");l(_c,"elementwiseOpNames");l(Vb,"buildElementwiseExpr");l(Lk,"register")});function Dk(){Me("broadcast_in_dim",Ck),Me("broadcast",Ck),Me("transpose",(r,e,t,n)=>{let s=e.getAttr("permutation"),o=t[0],i=n[0],{loopVars:a,loopBinds:u,indices:p,extentNodes:c}=xt(r,i.shape,i),h=new Array(o.shape.length);for(let m=0;m<s.length;m++)h[s[m]]=p[m];let f=new Y(i,p,new q(o,h)),d=new pe(r.blockName("transpose_block"),u,[{buffer:o}],[{buffer:i}],f);return St(d,a,i.shape,c)}),Me("reverse",(r,e,t,n)=>{let s=new Set(e.getAttr("dimensions")),o=t[0],i=n[0],{loopVars:a,loopBinds:u,indices:p,extentNodes:c}=xt(r,i.shape,i),h=new Array(o.shape.length);for(let m=0;m<o.shape.length;m++)h[m]=s.has(m)?new C("-",new C("-",c[m],new L(1)),p[m]):p[m];let f=new Y(i,p,new q(o,h)),d=new pe(r.blockName("reverse_block"),u,[{buffer:o}],[{buffer:i}],f);return St(d,a,i.shape,c)}),Me("reshape",(r,e,t,n)=>{let s=t[0],o=n[0],{loopVars:i,loopBinds:a,indices:u,extentNodes:p}=xt(r,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=In("+",m,In("*",u[_],new L(g)));c=new Array(s.shape.length);let y=m;for(let _=s.shape.length-1;_>=0;_--)_===0?c[_]=y:(c[_]=In("%",y,new L(s.shape[_])),y=In("//",y,new L(s.shape[_])))}let f=new Y(o,u,new q(s,c)),d=new pe(r.blockName("reshape_block"),a,[{buffer:s}],[{buffer:o}],f);return St(d,i,o.shape,p)}),Me("slice",(r,e,t,n)=>{let s=t[0],o=n[0],i=e.getAttr("starts"),a=e.getAttr("strides")||i.map(()=>1),{loopVars:u,loopBinds:p,indices:c,extentNodes:h}=xt(r,o.shape,o),f=new Array(s.shape.length);for(let g=0;g<s.shape.length;g++){let y=new L(i[g]);a[g]===1?f[g]=new C("+",y,c[g]):f[g]=new C("+",y,new C("*",c[g],new L(a[g])))}let d=new Y(o,c,new q(s,f)),m=new pe(r.blockName("slice_block"),p,[{buffer:s}],[{buffer:o}],d);return St(m,u,o.shape,h)}),Me("pad",(r,e,t,n)=>{let s=t[0],o=t[1],i=n[0],a=e.getAttr("low"),u=e.getAttr("interior")||a.map(()=>0),{loopVars:p,loopBinds:c,indices:h,extentNodes:f}=xt(r,i.shape,i),d=new Array(s.shape.length),m=new L(1);for(let T=0;T<s.shape.length;T++){let S=new C("+",h[T],new L(-a[T]));if(u[T]>0){let A=u[T]+1,N=new C("%",S,new L(A)),v=new qe("eq",N,new L(0));m=new C("*",m,v),d[T]=new C("//",S,new L(A))}else d[T]=S;let x=new qe("ge",d[T],new L(0)),k=new qe("lt",d[T],new L(s.shape[T]));m=new C("*",m,new C("*",x,k))}let g=new q(s,d),y=new q(o,[]),_=new xe(m,g,y),b=new Y(i,h,_),w=new pe(r.blockName("pad_block"),c,[{buffer:s},{buffer:o}],[{buffer:i}],b);return St(w,p,i.shape,f)}),Me("concat",(r,e,t,n)=>{let s=n[0],o=e.getAttr("dimension"),i=[],a=0;for(let u=0;u<t.length;u++){let p=t[u],{loopVars:c,loopBinds:h,indices:f,extentNodes:d}=xt(r,p.shape,p),m=new Array(p.shape.length);for(let _=0;_<p.shape.length;_++)m[_]=_===o&&a>0?new C("+",f[_],new L(a)):f[_];let g=new Y(s,m,new q(p,f)),y=new pe(r.blockName("concat"),h,[{buffer:p}],[{buffer:s}],g);i.push(St(y,c,p.shape,d)),a+=p.shape[o]}return i.length===1?i[0]:new te(i)}),Me("iota",(r,e,t,n)=>{let s=n[0],o=e.getAttr("iota_dimension"),{loopVars:i,loopBinds:a,indices:u,extentNodes:p}=xt(r,s.shape,s),c=new rt(u[o],"index",s.dtype),h=new Y(s,u,c),f=new pe(r.blockName("iota_block"),a,[],[{buffer:s}],h);return St(f,i,s.shape,p)})}function Ck(r,e,t,n){let s=t[0],o=n[0],i=e.getAttr("broadcast_dimensions")||[],{loopVars:a,loopBinds:u,indices:p,extentNodes:c}=xt(r,o.shape,o),h=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);h[m]=s.shape[m]===1?new L(0):p[g]}let f=new Y(o,p,new q(s,h)),d=new pe(r.blockName("broadcast_block"),u,[{buffer:s}],[{buffer:o}],f);return St(d,a,o.shape,c)}var Pk=M(()=>{"use strict";De();Fr();l(Dk,"register");l(Ck,"lowerBroadcast")});function AL(r,e){if(nr(r)){let t=e?vL[r]??-2147483648:NL[r]??2147483647;return new L(t)}return new fe(e?-1/0:1/0)}function $k(){Me("reduce",(e,t,n,s)=>{let o=n[0],i=n[1],a=s[0],u=t.getAttr("dimensions")||[],p=t.getAttr("reduce_type")||"sum",c=new Set(u),h=[],f=[];for(let B=0;B<o.shape.length;B++)(c.has(B)?f:h).push(B);let d=or(e,"si",h,o.shape,o),m=new Y(a,d.indices,new q(i,[])),g=new pe(e.blockName("reduce_init"),d.ivs,[{buffer:i}],[{buffer:a}],m),y=h.length>0?d.wrap(g):g,_=or(e,"sa",h,o.shape,o),b=e.allocVarArray("r",f.length),w=e.allocBindArray("rv",b);for(let B of w)B.kind=Ib.COMM_REDUCE;let T=new Array(o.shape.length);for(let B=0;B<h.length;B++)T[h[B]]=_.ivs[B].iterVar;for(let B=0;B<f.length;B++)T[f[B]]=w[B].iterVar;let S=new q(a,_.indices),x=new q(o,T),k=OL[p];if(!k)throw new Error(`reduction lowering: unsupported reduce_type '${p}'`);let A=new Y(a,_.indices,k(S,x,a.dtype)),N=new Array(f.length);for(let B=0;B<f.length;B++)N[B]=e.extentNode(o.shape[f[B]],o,f[B]);let v=new pe(e.blockName("reduce_acc"),mc(_.ivs,w),[{buffer:o}],[{buffer:a}],A),O=gc(v,b,N);O=_.wrap(O);let R=[y,O];if(p==="mean"){let B=1,E=[];for(let P=0;P<f.length;P++){let V=o.shape[f[P]];V===X?E.push(e.extentNode(X,o,f[P])):B*=V}let z=or(e,"sm",h,o.shape,o),D=new q(a,z.indices),J;if(E.length===0)J=new C("*",D,new fe(1/B));else{let P=new L(B);for(let V of E)P=new C("*",P,V);J=new C("/",D,P)}let j=new Y(a,z.indices,J),U=new pe(e.blockName("mean_div"),z.ivs,[{buffer:a}],[{buffer:a}],j);R.push(h.length>0?z.wrap(U):U)}return new te(R)});function r(e,t){Me(e,(n,s,o,i)=>{let a=o[0],u=i[0],p=s.getAttr("axis"),c=s.getAttr("keep_dims")||!1,h=new Set([p]),f=[],d=p;for(let V=0;V<a.shape.length;V++)h.has(V)||f.push(V);let m=l(V=>{if(!c)return V.indices;let he=new Array(a.shape.length);for(let le=0;le<f.length;le++)he[f[le]]=V.indices[le];return he[d]=new L(0),he},"outIndicesFor"),g=new me("_argval_"+n.varCounter,f.map(V=>a.shape[V]),a.dtype,"global");n.varCounter++;let y=or(n,"ai",f,a.shape,a),_=new Y(g,y.indices,AL(a.dtype,t==="gt")),b=new Y(u,m(y),new L(0)),w=new pe(n.blockName("arg_init"),y.ivs,[],[{buffer:g},{buffer:u}],new te([_,b])),T=f.length>0?y.wrap(w):w,S=or(n,"as",f,a.shape,a),x=n.allocVar("ar"),k=n.allocBindArray("arv",[x]),A=new Array(a.shape.length);for(let V=0;V<f.length;V++)A[f[V]]=S.ivs[V].iterVar;A[d]=k[0].iterVar;let N=new q(a,A),v=new q(g,S.indices),O=new qe(t,N,v),R=new xe(O,N,v),B=m(S),E=new q(u,B),z=new xe(O,k[0].iterVar,E),D=new Y(u,B,z),J=new Y(g,S.indices,R),j=new pe(n.blockName("arg_acc"),mc(S.ivs,k),[{buffer:a},{buffer:g}],[{buffer:g},{buffer:u}],new te([D,J])),U=n.extentNode(a.shape[d],a,d),P=gc(j,[x],[U]);return P=S.wrap(P),new te([T,P])})}l(r,"registerArgReduce"),r("argmax","gt"),r("argmin","lt")}var vL,NL,OL,Fk=M(()=>{"use strict";De();xo();Oe();Fr();Vn();vL={i8:-128,i16:-32768,i32:-2147483648,i64:-2147483648,ui8:0,ui16:0,ui32:0,bool:0},NL={i8:127,i16:32767,i32:2147483647,i64:2147483647,ui8:255,ui16:65535,ui32:4294967295,bool:1};l(AL,"argReduceSentinel");OL={sum:l((r,e)=>new C("+",r,e),"sum"),mean:l((r,e)=>new C("+",r,e),"mean"),prod:l((r,e)=>new C("*",r,e),"prod"),max:l((r,e,t)=>new ke("max",[r,e],t),"max"),min:l((r,e,t)=>new ke("min",[r,e],t),"min"),and:l((r,e)=>new C("*",r,e),"and"),or:l((r,e,t)=>new ke("max",[r,e],t),"or")};l($k,"register")});function zk(r,e){return Lt(e)?new rt(r,e,"i32"):r}function Vk(){Me("dot",(r,e,t,n)=>{let{initBody:s,accBody:o}=yc(r,e,t[0],t[1],n[0],{prefix:"di",initBlockName:"matmul_init",accBlockName:"matmul",initVal:l(()=>new fe(0),"initVal"),accLeaf:l((i,a)=>new C("*",i,a),"accLeaf")});return new te([s,o])}),Me("conv",(r,e,t,n)=>{let s=t[0],o=t[1];return Hh(r,e,s,o,n[0],{prefix:"c",blockPrefix:"conv",initVal:l(()=>new fe(0),"initVal"),guardFill:l(()=>new fe(0),"guardFill"),leafBuilder:l((i,a)=>new C("*",new q(s,i),new q(o,a)),"leafBuilder")})}),Me("gather",(r,e,t,n)=>{let s=t[0],o=t[1],i=n[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:h,loopBinds:f,indices:d,extentNodes:m}=xt(r,i.shape,i),g=[],y=[];for(let S=0;S<i.shape.length;S++)a.has(S)?y.push(d[S]):g.push(d[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 x=new Array(o.shape.length),k=0;for(let v=0;v<o.shape.length;v++)v===c?x[v]=new L(S):x[v]=g[k++];let A=zk(new q(o,x),o.dtype),N=p[S];_[N]=new C("+",_[N],A)}let w=new Y(i,d,new q(s,_)),T=new pe(r.blockName("gather_block"),f,[{buffer:s},{buffer:o}],[{buffer:i}],w);return St(T,h,i.shape,m)}),Me("scatter",(r,e,t,n)=>{let s=t[0],o=t[1],i=t[2],a=n[0],u=new Set(e.getAttr("inserted_window_dims")),p=e.getAttr("scatter_dims_to_operand_dims"),c=e.getAttr("index_vector_dim"),h=new Set(e.getAttr("update_window_dims")),f=xt(r,s.shape,s),d=new Y(a,f.indices,new q(s,f.indices)),m=new pe(r.blockName("scatter_copy"),f.loopBinds,[{buffer:s}],[{buffer:a}],d),g=St(m,f.loopVars,s.shape,f.extentNodes),{loopVars:y,loopBinds:_,indices:b,extentNodes:w}=xt(r,i.shape,i),T=[],S=[];for(let E=0;E<i.shape.length;E++)h.has(E)?S.push(b[E]):T.push(b[E]);let x=new Array(s.shape.length),k=0;for(let E=0;E<s.shape.length;E++)u.has(E)?x[E]=new L(0):x[E]=S[k++];for(let E=0;E<p.length;E++){let z=new Array(o.shape.length),D=0;for(let U=0;U<o.shape.length;U++)U===c?z[U]=new L(E):z[U]=T[D++];let J=zk(new q(o,z),o.dtype),j=p[E];x[j]=new C("+",x[j],J)}let A=new q(i,b),N=new q(a,x),v=new C("+",N,A),O=new Y(a,x,v),R=new pe(r.blockName("scatter_update"),_,[{buffer:i},{buffer:o}],[{buffer:a}],O),B=St(R,y,i.shape,w);return new te([g,B])}),Me("fused_dot_epilogue",(r,e,t,n)=>{let s=e.getAttr("num_dot_operands")||2,o=t[0],i=t[1],a=t.slice(s),u=n[0],p=e.getAttr("epilogue_tags")||[],{initBody:c,accBody:h}=yc(r,e,o,i,u,{prefix:"ei",initBlockName:"matmul_init",accBlockName:"matmul_acc",initVal:l(()=>new fe(0),"initVal"),accLeaf:l((T,S)=>new C("*",T,S),"accLeaf")});if(p.length===0)return new te([c,h]);let f=or(r,"ep",Array.from({length:u.shape.length},(T,S)=>S),u.shape,u),d=f.indices,m=new q(u,d),g={v:0};for(let T of p){let S=zr.get(T);S&&(m=S(m,a,g,u,d))}let y=cu([u,...a]),_=new Y(u,d,m),b=new pe(r.blockName("epilogue"),f.ivs,y,[{buffer:u}],_),w=f.wrap(b);return new te([c,h,w])}),Me("cublas_gemm",Qo("dot"))}var zr,Gk=M(()=>{"use strict";De();Oe();Fr();l(zk,"asIndexValue");zr=new Map;zr.set("bias",(r,e,t,n,s)=>{let o=e[t.v++];return new C("+",r,new q(o,So(o,n,s)))});zr.set("residual_add",(r,e,t,n,s)=>{let o=e[t.v++];return new C("+",r,new q(o,So(o,n,s)))});zr.set("scale",(r,e,t,n,s)=>{let o=e[t.v++];return new C("*",r,new q(o,So(o,n,s)))});zr.set("relu",(r,e,t,n)=>new ke("max",[r,new fe(0)],n.dtype));zr.set("clamp",(r,e,t,n,s)=>{let o=e[t.v++],i=e[t.v++];return new ke("min",[new ke("max",[r,new q(o,So(o,n,s))],n.dtype),new q(i,So(i,n,s))],n.dtype)});zr.set("neg",r=>new C("-",r));zr.set("exp",(r,e,t,n)=>new ke("exp",[r],n.dtype));zr.set("tanh",(r,e,t,n)=>new ke("tanh",[r],n.dtype));zr.set("sqrt",(r,e,t,n)=>new ke("sqrt",[r],n.dtype));zr.set("abs",(r,e,t,n)=>new ke("abs",[r],n.dtype));zr.set("log",(r,e,t,n)=>new ke("log",[r],n.dtype));zr.set("activation",r=>r);l(Vk,"register")});function fu(r,e,t){let{loopVars:n,loopBinds:s,indices:o,extentNodes:i}=xt(r,t.shape,t),a=new Y(t,o,new q(e,o)),u=new pe(r.blockName("cf_copy"),s,[{buffer:e}],[{buffer:t}],a);return St(u,n,t.shape,i)}function BL(r,e,t,n){let{loopVars:s,loopBinds:o,indices:i,extentNodes:a}=xt(r,t.shape,t),u=new Y(t,i,new q(e,[n,...i])),p=new pe(r.blockName("scan_in"),o,[{buffer:e}],[{buffer:t}],u);return St(p,s,t.shape,a)}function RL(r,e,t,n){let{loopVars:s,loopBinds:o,indices:i,extentNodes:a}=xt(r,e.shape,e),u=new Y(t,[n,...i],new q(e,i)),p=new pe(r.blockName("scan_out"),o,[{buffer:e}],[{buffer:t}],u);return St(p,s,e.shape,a)}function bc(r,e,t){let n=e.entryBlock,s=new Map;for(let i=0;i<n.arguments.length;i++)s.set(n.arguments[i],t[i]),t[i]&&r.bufferMap.set(n.arguments[i],t[i]);let o=[];for(let i of n.ops()){if(i.opName==="yield"){let f=new Array(i.numOperands);for(let d=0;d<i.numOperands;d++)f[d]=s.get(i.getOperand(d))||r.getOrAllocBuffer(i.getOperand(d));return{stmts:o,yieldBuffers:f}}if(ln(i.opName)){o.push(pu(r,i));continue}let a=new Array(i.numOperands);for(let f=0;f<i.numOperands;f++)a[f]=s.get(i.getOperand(f))||i.getOperand(f);let u=new Array(a.length);for(let f=0;f<a.length;f++)u[f]=a[f]instanceof me?a[f]:r.getOrAllocBuffer(a[f]);let p=new Array(i.numResults);for(let f=0;f<i.numResults;f++){let d={type:i.getResult(f).type};p[f]=r.getOrAllocBuffer(d),s.set(i.getResult(f),p[f]),r.bufferMap.set(i.getResult(f),p[f])}let c=Qo(i.opName);if(!c)throw new Error(`No lowering rule for op '${i.opName}' inside region`);let h=c(r,i,u,p);h&&o.push(h)}return{stmts:o,yieldBuffers:[]}}function jk(){Me("if",(r,e,t,n)=>{let s=t[0],o=new q(s,[]),i=e.regions[0],a=e.regions[1],u=new Array(e.numResults);for(let d=0;d<e.numResults;d++)u[d]=n[d]||r.getOrAllocBuffer(e.getResult(d)),r.bufferMap.set(e.getResult(d),u[d]);let p=bc(r,i,[]),c=p.stmts.slice();for(let d=0;d<u.length&&d<p.yieldBuffers.length;d++){let m=p.yieldBuffers[d];m&&m!==u[d]&&c.push(fu(r,m,u[d]))}let h=c.length===1?c[0]:new te(c),f=null;if(a&&a.entryBlock){let d=bc(r,a,[]),m=d.stmts.slice();for(let g=0;g<u.length&&g<d.yieldBuffers.length;g++){let y=d.yieldBuffers[g];y&&y!==u[g]&&m.push(fu(r,y,u[g]))}m.length>0&&(f=m.length===1?m[0]:new te(m))}return new xe(o,h,f)}),Me("while",(r,e,t,n)=>{let s=e.regions[0],o=e.regions[1],i=new Array(t.length),a=[];for(let y=0;y<t.length;y++){let _=n[y]||r.getOrAllocBuffer({type:e.getResult(y).type});i[y]=_,a.push(fu(r,t[y],_))}for(let y=0;y<e.numResults;y++)r.bufferMap.set(e.getResult(y),i[y]);let u=new me(`_wcond_${r.varCounter++}`,[],"bool",au.GLOBAL),p=bc(r,s,i),c=p.stmts.slice(),h=p.yieldBuffers[0];c.push(new Y(u,[],new q(h,[])));let f=c.length===1?c[0]:new te(c),d=bc(r,o,i),m=d.stmts.slice();for(let y=0;y<i.length&&y<d.yieldBuffers.length;y++){let _=d.yieldBuffers[y];_&&_!==i[y]&&m.push(fu(r,_,i[y]))}let g=m.length===1?m[0]:new te(m);return new te([...a,new Zo(u,f,g)])}),Me("scan",(r,e,t,n)=>{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=n[b]||r.getOrAllocBuffer({type:e.getResult(b).type});u[b]=w,p.push(fu(r,a[b],w)),r.bufferMap.set(e.getResult(b),w)}let c=n.length-o,h=new Array(c);for(let b=0;b<c;b++){let w=n[o+b]||r.getOrAllocBuffer({type:e.getResult(o+b).type});h[b]=w,r.bufferMap.set(e.getResult(o+b),w)}let f=r.allocVar("t"),d=r.extentNode(i[0].shape[0],i[0],0),m=[],g=new Array(s);for(let b=0;b<s;b++){let w=r.getOrAllocBuffer({type:{shape:i[b].shape.slice(1),dtype:i[b].dtype}});g[b]=w,m.push(BL(r,i[b],w,f))}let y=bc(r,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(fu(r,w,u[b]))}for(let b=0;b<c;b++)m.push(RL(r,y.yieldBuffers[o+b],h[b],f));m.push(new On);let _=m.length===1?m[0]:new te(m);return new te([...p,new de(f,new L(0),d,F.RECURRENCE,_)])})}var qk=M(()=>{"use strict";De();xo();Eb();Fr();l(fu,"copyBuffer");l(BL,"sliceCopyIn");l(RL,"sliceCopyOut");l(bc,"lowerRegionBody");l(jk,"register")});function Uk(){Me("layout_transform",(r,e,t,n)=>{let s=t[0],o=n[0],i=e.getAttr("src_layout"),a=e.getAttr("dst_layout"),{loopVars:u,loopBinds:p,indices:c,extentNodes:h}=xt(r,o.shape,o),f=new Y(o,c,new q(s,c)),d=new pe(r.blockName("layout_transform_block"),p,[{buffer:s}],[{buffer:o}],f);return St(d,u,o.shape,h)})}var Kk=M(()=>{"use strict";De();Fr();l(Uk,"register")});function ML(r){let e=r==="ui8",t=8;return{cMin:e?0:-(1<<t-1),cMax:e?(1<<t)-1:(1<<t-1)-1}}function Jh(r,{scale:e,zeroPoint:t,targetDtype:n}){let s=n||"i8",{cMin:o,cMax:i}=ML(s),a=new C("/",r,new fe(e)),u=t!==0?new C("+",a,new fe(t)):a,p=new ke("round",[u],"f32"),c=new ke("min",[new ke("max",[p,new fe(o)],"f32"),new fe(i)],"f32");return new rt(c,"f32",s)}function Zh(r,{scale:e,zeroPoint:t,srcDtype:n,targetDtype:s}){let o=s||"f32",i=new rt(r,n||"i8",o),a=t!==0?new C("-",i,new fe(t)):i;return new C("*",a,new fe(e))}var Gb=M(()=>{"use strict";De();l(ML,"clampRange");l(Jh,"buildQuantizeExpr");l(Zh,"buildDequantizeExpr")});function Wk(){Me("quantize",(r,e,t,n)=>{let s=e.getAttr("scale"),o=e.getAttr("zero_point"),i=e.getAttr("target_dtype")||"i8";return qs(r,e,t,n,(a,u)=>Jh(u[0],{scale:s,zeroPoint:o,targetDtype:i}))}),Me("dequantize",(r,e,t,n)=>{let s=e.getAttr("scale"),o=e.getAttr("zero_point"),i=t[0].dtype||"i8",a=e.getAttr("target_dtype")||"f32";return qs(r,e,t,n,(u,p)=>Zh(p[0],{scale:s,zeroPoint:o,srcDtype:i,targetDtype:a}))}),Me("quantized_dot",(r,e,t,n)=>{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}=yc(r,e,s,o,n[0],{prefix:"qdi",initBlockName:"qmatmul_init",accBlockName:"qmatmul_acc",initVal:l(()=>new L(0),"initVal"),accLeaf:l((c,h)=>{let f=new rt(c,s.dtype,"i32"),d=new rt(h,o.dtype,"i32"),m=i!==0?new C("-",f,new L(i)):f,g=a!==0?new C("-",d,new L(a)):d;return new C("*",m,g)},"accLeaf")});return new te([u,p])}),Me("quantized_conv",(r,e,t,n)=>{let s=t[0],o=t[1],i=e.getAttr("input_zero_point")||0,a=e.getAttr("kernel_zero_point")||0;return Hh(r,e,s,o,n[0],{prefix:"qc",blockPrefix:"qconv",initVal:l(()=>new L(0),"initVal"),guardFill:l(()=>new L(0),"guardFill"),leafBuilder:l((u,p)=>{let c=new rt(new q(s,u),s.dtype,"i32"),h=new rt(new q(o,p),o.dtype,"i32"),f=i!==0?new C("-",c,new L(i)):c,d=a!==0?new C("-",h,new L(a)):h;return new C("*",f,d)},"leafBuilder")})})}var Hk=M(()=>{"use strict";De();Fr();Gb();l(Wk,"register")});function ko(r){return Vr.has(r)}function IL(){for(let r of _c())Vr.set(r,(e,t,n)=>Vb(e.opName,t,n));Vr.set("compare",(r,e)=>new qe(r.getAttr("direction")||"eq",e[0],e[1])),Vr.set("select",(r,e)=>new xe(e[0],e[1],e[2])),Vr.set("clamp",(r,e,t)=>new ke("min",[new ke("max",[e[1],e[0]],t),e[2]],t)),Vr.set("convert",(r,e)=>new rt(e[0],r.getOperand(0).type.dtype,r.getAttr("target_dtype")||r.getResult(0).type.dtype)),Vr.set("broadcast_in_dim",(r,e)=>e[0]),Vr.set("broadcast",(r,e)=>e[0]),Vr.set("iota",()=>{throw new Error("iota fusion must be handled by the index-aware path in lowerFusion")}),Vr.set("quantize",(r,e)=>Jh(e[0],{scale:r.getAttr("scale"),zeroPoint:r.getAttr("zero_point"),targetDtype:r.getAttr("target_dtype")||"i8"})),Vr.set("dequantize",(r,e)=>Zh(e[0],{scale:r.getAttr("scale"),zeroPoint:r.getAttr("zero_point"),srcDtype:r.getOperand(0).type?.dtype||"i8",targetDtype:r.getAttr("target_dtype")||"f32"}))}function Yk(r,e){let t=e.numOperands,n=e.numResults,s=new Array(t);for(let N=0;N<t;N++)s[N]=r.getOrAllocBuffer(e.getOperand(N));let o=new Array(n);for(let N=0;N<n;N++)o[N]=r.getOrAllocBuffer(e.getResult(N));let i=o[0],{loopVars:a,loopBinds:u,indices:p,extentNodes:c}=xt(r,i.shape,i),h=new Map,f=e.regions[0].entryBlock,d=new Map,m=[...f.ops()];for(let N=m.length-1;N>=0;N--){let v=m[N];if(lu(v.opName)){let O=v.getAttr("broadcast_dimensions");O&&O.length>0&&d.set(v.getOperand(0),O);continue}if(!(v.opName==="yield"||ln(v.opName)))for(let O=0;O<v.numResults;O++){let R=d.get(v.getResult(O));if(R)for(let B=0;B<v.numOperands;B++)d.has(v.getOperand(B))||d.set(v.getOperand(B),R)}}let g=new Map,y=f.arguments;for(let N=0;N<y.length;N++){let v=d.get(y[N]);v&&g.set(N,v)}for(let N=0;N<f.arguments.length;N++){let v=g.get(N),O;if(v){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[v[B]]}else O=So(s[N],i,p);h.set(f.arguments[N],new q(s[N],O))}let _=new Map;for(let N of f.ops())for(let v=0;v<N.numOperands;v++){let O=N.getOperand(v);_.set(O,(_.get(O)||0)+1)}let b=new Map,w=0,T=[];function S(N){let v=h.get(N);if(v===void 0)throw new Error(`Fusion lowering: unmapped operand from '${N.definingOp?N.definingOp.opName:"unknown"}'`);if((_.get(N)||0)>1&&!EL.has(v.type)){if(!b.has(N)){let O=v.type==="CompareNode"?"i32":v.type==="CastNode"?v.toDtype:N.type&&N.type.dtype?N.type.dtype:i.dtype,R=r.allocVar(`cse${w++}`,O);b.set(N,R),T.push({variable:R,value:v}),h.set(N,R)}return b.get(N)}return v}l(S,"getExpr");let x=[];for(let N of f.ops()){if(N.opName==="yield"){for(let B=0;B<N.numOperands;B++)x.push(new Y(o[B],p,S(N.getOperand(B))));break}if(ln(N.opName)){let B=N.getAttr("value");h.set(N.getResult(0),new fe(typeof B=="number"?B:0));continue}if(N.opName==="iota"){let B=N.getAttr("iota_dimension")??N.getAttr("dimension")??0;h.set(N.getResult(0),p[B]);continue}let v=Vr.get(N.opName);if(!v)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;h.set(N.getResult(0),v(N,O,R))}let k=x.length===1?x[0]:new te(x);for(let N=T.length-1;N>=0;N--)k=new Ye(T[N].variable,T[N].value,k);let A=new pe(r.blockName("fusion_block"),u,cu(s),cu(o),k);return St(A,a,i.shape,c)}function LL(r,e){if(r.length!==e.length)return!1;for(let t=0;t<r.length;t++)if(r[t]!==e[t])return!1;return!0}function Jk(r){let e=r.regions[0];if(!e)return!1;for(let t of e.entryBlock.ops())if(t.opName!=="yield"){if(ln(t.opName)){if(typeof t.getAttr("value")=="number")continue;return!1}if(!Vr.has(t.opName))return!1}if(r.numResults>1){let t=r.getResult(0).type.shape;for(let n=1;n<r.numResults;n++)if(!LL(r.getResult(n).type.shape,t))return!1}return!0}function Zk(r,e,t){let n=e.regions[0].entryBlock,s=new Map;for(let i=0;i<n.arguments.length;i++)s.set(n.arguments[i],e.getOperand(i));let o=new Map;for(let i of n.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 n.ops()){if(i.opName==="yield")continue;let a=new Array(i.numOperands);for(let f=0;f<i.numOperands;f++)a[f]=s.get(i.getOperand(f))||i.getOperand(f);let u=new Array(a.length);for(let f=0;f<a.length;f++)u[f]=r.getOrAllocBuffer(a[f]);let p=new Array(i.numResults);for(let f=0;f<i.numResults;f++){let d=i.getResult(f),m=o.get(d);if(m){let g=r.getOrAllocBuffer(m);p[f]=g,s.set(d,m)}else{let g={type:d.type};p[f]=r.getOrAllocBuffer(g),s.set(d,g)}}if(ln(i.opName)){t.push(pu(r,i));continue}let c=Qo(i.opName);if(!c)throw new Error(`Fusion lowering: no lowering rule for op '${i.opName}' inside fusion body`);let h=c(r,i,u,p);h&&t.push(h)}}function Qk(){IL()}var Vr,EL,Xk=M(()=>{"use strict";De();Fr();Yh();Gb();Tr();Vr=new Map;l(ko,"canInlineFuse");l(IL,"initBuiltinFusionBuilders");EL=new Set(["BufferLoadNode","VariableNode","IntImmNode","FloatImmNode"]);l(Yk,"lowerFusion");l(LL,"shapesEqual");l(Jk,"canLowerAsElementwiseFusion");l(Zk,"lowerFusionAsIndividualOps");l(Qk,"register")});function ev(r,e,t,n,s){let o=new C("-",new C("*",r,new L(e)),new L(n)),i=new C("+",o,new L(t)),a=new ke("max",[o,new L(0)],"index"),u=new ke("min",[i,new L(s)],"index"),p=new C("-",u,a);return new ke("max",[p,new L(0)],"index")}function CL(r,e,t,n,s,o,i,a,u,p,c){let h=ev(r,t,s,i,u),f=ev(e,n,o,a,p),d=new C("*",h,f);return new rt(d,"index",c)}function tv(){Me("pool2d",(r,e,t,n)=>{let s=t[0],o=n[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,h=Wh(e.getAttr("layout")||"NCHW"),f=h.N,d=h.C,m=h.H,g=h.W,y=s.shape[f],_=s.shape[d],b=s.shape[m],w=s.shape[g],T=o.shape[m],S=o.shape[g],x=a[0],k=a[1],A=u[0],N=u[1],v=p[0][0],O=p[1][0],R=i==="max",B=R?new fe(-1/0):new fe(0),E=or(r,"pi",[0,1,2,3],o.shape,o),z=new Y(o,E.indices,B),D=new pe(r.blockName("pool_init"),E.ivs,[],[{buffer:o}],z),J=E.wrap(D),j=r.allocVar("pn"),U=r.allocVar("pc"),P=r.allocVar("poh"),V=r.allocVar("pow"),he=r.allocVar("pkh"),le=r.allocVar("pkw"),Ie=[j,U,P,V,he,le],G=r.allocBindArray("pv",Ie),K=G[0].iterVar,ie=G[1].iterVar,ze=G[2].iterVar,Se=G[3].iterVar,Qe=G[4].iterVar,It=G[5].iterVar,Jt=new C("+",new C("*",ze,new L(A)),new C("-",Qe,new L(v))),Gt=new C("+",new C("*",Se,new L(N)),new C("-",It,new L(O))),tt=new qe("ge",Jt,new L(0)),ye=new qe("lt",Jt,new L(b)),He=new qe("ge",Gt,new L(0)),gn=new qe("lt",Gt,new L(w)),jt=new C("*",new C("*",tt,ye),new C("*",He,gn)),Zt=new Array(4);Zt[f]=K,Zt[d]=ie,Zt[m]=ze,Zt[g]=Se;let Qt=new Array(4);Qt[f]=K,Qt[d]=ie,Qt[m]=Jt,Qt[g]=Gt;let on=new q(s,Qt),Jn=new q(o,Zt),Bn;if(R){let mt=new xe(jt,on,new fe(-1/0));Bn=new ke("max",[Jn,mt],o.dtype)}else{let mt=new xe(jt,on,new fe(0));Bn=new C("+",Jn,mt)}let yr=new Y(o,Zt,Bn),pt=new pe(r.blockName("pool_acc"),G,[{buffer:s}],[{buffer:o}],yr);pt=new de(le,new L(0),new L(k),F.SERIAL,pt),pt=new de(he,new L(0),new L(x),F.SERIAL,pt),pt=new de(V,new L(0),new L(S),F.SERIAL,pt),pt=new de(P,new L(0),new L(T),F.SERIAL,pt),pt=new de(U,new L(0),new L(_),F.SERIAL,pt),pt=new de(j,new L(0),new L(y),F.SERIAL,pt);let Qn=[J,pt];if(!R){let mt=or(r,"pd",[0,1,2,3],o.shape,o),Or=mt.indices[m],hs=mt.indices[g],$s=c?new fe(x*k):CL(Or,hs,A,N,x,k,v,O,b,w,o.dtype),Br=new q(o,mt.indices),Xn=c?new C("*",Br,new fe(1/(x*k))):new C("/",Br,$s),_r=new Y(o,mt.indices,Xn),fo=new pe(r.blockName("pool_div"),mt.ivs,[{buffer:o}],[{buffer:o}],_r);Qn.push(mt.wrap(fo))}return new te(Qn)})}var nv=M(()=>{"use strict";De();Fr();l(ev,"spatialCount");l(CL,"avgPoolDivisorExpr");l(tv,"register")});function rv(){Me("resize",(r,e,t,n)=>{let s=t[0],o=n[0],i=e.getAttr("method"),a=s.shape[2],u=s.shape[3],p=o.shape[2],c=o.shape[3],h=or(r,"rz",[0,1,2,3],o.shape,o),[f,d,m,g]=h.indices,y=a/p,_=u/c,b;if(i==="nearest"){let S=new ke("floor",[new C("*",m,new fe(y))],"f32"),x=new ke("floor",[new C("*",g,new fe(_))],"f32"),k=new rt(new ke("min",[new ke("max",[S,new fe(0)],"f32"),new fe(a-1)],"f32"),"f32","i32"),A=new rt(new ke("min",[new ke("max",[x,new fe(0)],"f32"),new fe(u-1)],"f32"),"f32","i32");b=new q(s,[f,d,k,A])}else{let S=new C("*",m,new fe(y)),x=new C("*",g,new fe(_)),k=new ke("floor",[S],"f32"),A=new ke("floor",[x],"f32"),N=new ke("min",[new C("+",k,new fe(1)),new fe(a-1)],"f32"),v=new ke("min",[new C("+",A,new fe(1)),new fe(u-1)],"f32"),O=new C("-",S,k),R=new C("-",x,A),B=new ke("max",[k,new fe(0)],"f32"),E=new ke("max",[A,new fe(0)],"f32"),z=new rt(B,"f32","i32"),D=new rt(E,"f32","i32"),J=new rt(N,"f32","i32"),j=new rt(v,"f32","i32"),U=new q(s,[f,d,z,D]),P=new q(s,[f,d,z,j]),V=new q(s,[f,d,J,D]),he=new q(s,[f,d,J,j]),le=new C("-",new fe(1),R),Ie=new C("-",new fe(1),O),G=new C("+",new C("*",U,le),new C("*",P,R)),K=new C("+",new C("*",V,le),new C("*",he,R));b=new C("+",new C("*",G,Ie),new C("*",K,O))}let w=new Y(o,h.indices,b),T=new pe(r.blockName("resize_block"),h.ivs,[{buffer:s}],[{buffer:o}],w);return h.wrap(T)})}var sv=M(()=>{"use strict";De();Fr();l(rv,"register")});function jb(r,e){let[t,n,s]=e;return{Q:t,K:n,V:s,O:null,scale:r.getAttr("scale"),causal:!!r.getAttr("causal"),dtype:t.dtype,B:t.shape[0],H:t.shape[1],Lq:t.shape[2],Dk:t.shape[3],Lk:n.shape[2],Dv:s.shape[3]}}function ov(r,e,t,n){let{Q:s,K:o,V:i,scale:a,causal:u,dtype:p,B:c,H:h,Lq:f,Dk:d,Lk:m,Dv:g}=jb(e,t),y=n[0],_=new me(r.blockName("fa_m"),[1],p,"local"),b=new me(r.blockName("fa_l"),[1],p,"local"),w=new me(r.blockName("fa_s"),[1],p,"local"),T=new me(r.blockName("fa_mn"),[1],p,"local"),S=new me(r.blockName("fa_p"),[1],p,"local"),x=new me(r.blockName("fa_cr"),[1],p,"local"),k=new me(r.blockName("fa_o"),[g],p,"local"),A=new Ne(r.blockName("fa_b"),"int32"),N=new Ne(r.blockName("fa_h"),"int32"),v=new Ne(r.blockName("fa_i"),"i32"),O=new Ne(r.blockName("fa_j"),"int32"),R=new Ne(r.blockName("fa_d"),"int32"),B=new Ne(r.blockName("fa_di"),"int32"),E=new Ne(r.blockName("fa_do"),"int32"),z=new Ne(r.blockName("fa_dw"),"int32"),D=new pe(r.blockName("fa_initm"),[],[],[{buffer:_}],ct(_,[_e],new fe(-1/0))),J=new pe(r.blockName("fa_initl"),[],[],[{buffer:b}],ct(b,[_e],new fe(0))),j=xr(B,g,new pe(r.blockName("fa_inito"),[],[],[{buffer:k}],ct(k,[B],new fe(0)))),U=new pe(r.blockName("fa_sinit"),[],[],[{buffer:w}],ct(w,[_e],new fe(0))),P=xr(R,d,new pe(r.blockName("fa_sred"),[],[{buffer:s},{buffer:o},{buffer:w}],[{buffer:w}],ct(w,[_e],st("+",Ee(w,[_e]),st("*",Ee(s,[A,N,v,R]),Ee(o,[A,N,O,R])))))),V=new te([U,P]),he=new pe(r.blockName("fa_supd"),[],[{buffer:w},{buffer:_},{buffer:b}],[{buffer:w},{buffer:T},{buffer:S},{buffer:x},{buffer:b}],new te([ct(w,[_e],st("*",Ee(w,[_e]),new fe(a))),ct(T,[_e],hu("max",Ee(_,[_e]),Ee(w,[_e]))),ct(S,[_e],hu("exp",st("-",Ee(w,[_e]),Ee(T,[_e])))),ct(x,[_e],hu("exp",st("-",Ee(_,[_e]),Ee(T,[_e])))),ct(b,[_e],st("+",st("*",Ee(b,[_e]),Ee(x,[_e])),Ee(S,[_e])))])),le=xr(E,g,new pe(r.blockName("fa_oupd"),[],[{buffer:k},{buffer:x},{buffer:S},{buffer:i}],[{buffer:k}],ct(k,[E],st("+",st("*",Ee(k,[E]),Ee(x,[_e])),st("*",Ee(S,[_e]),Ee(i,[A,N,O,E])))))),Ie=new pe(r.blockName("fa_mupd"),[],[{buffer:T}],[{buffer:_}],ct(_,[_e],Ee(T,[_e]))),G=new te([V,he,le,Ie]),K=m-f,ie=u?new xe(new qe("le",O,st("+",v,Xo(K))),G):G,ze=xr(O,m,ie),Se=xr(z,g,new pe(r.blockName("fa_finw"),[],[{buffer:k},{buffer:b}],[{buffer:y}],ct(y,[A,N,v,z],st("/",Ee(k,[z]),Ee(b,[_e]))))),Qe=new te([D,J,j,ze,Se]);for(let ye of[k,x,S,T,w,b,_])Qe=new Ve(ye,ye.scope,Qe);let It=Math.min(f,256),Jt=Math.ceil(f/It),Gt;if(Jt===1)Gt=new de(v,_e,new L(f),F.THREAD_BINDING,Qe,"threadIdx.x");else{let ye=new Ne(r.blockName("fa_ii"),"i32"),He=new Ne(r.blockName("fa_io"),"i32"),gn=st("+",st("*",He,new L(It)),ye),jt=Jt*It===f?Qe:new xe(new qe("lt",v,new L(f)),Qe),Zt=new de(ye,_e,new L(It),F.THREAD_BINDING,new Ye(v,gn,jt),"threadIdx.x");Gt=new de(He,_e,new L(Jt),F.THREAD_BINDING,Zt,"blockIdx.z")}let tt=new de(N,_e,new L(h),F.THREAD_BINDING,Gt,"blockIdx.y");return new de(A,_e,new L(c),F.THREAD_BINDING,tt,"blockIdx.x")}function DL(r,e,t,n,s){let{Q:o,K:i,V:a,scale:u,causal:p,dtype:c,B:h,H:f,Lq:d,Dk:m,Lk:g,Dv:y}=jb(e,t),_=n[0],b=Math.ceil(g/s),w=Math.ceil(d/s),T=g-d,S=new me(r.blockName("fa_Ks"),[s,m],c,"shared"),x=new me(r.blockName("fa_Vs"),[s,y],c,"shared"),k=new me(r.blockName("fa_q"),[m],c,"local"),A=new me(r.blockName("fa_m"),[1],c,"local"),N=new me(r.blockName("fa_l"),[1],c,"local"),v=new me(r.blockName("fa_s"),[1],c,"local"),O=new me(r.blockName("fa_mn"),[1],c,"local"),R=new me(r.blockName("fa_p"),[1],c,"local"),B=new me(r.blockName("fa_cr"),[1],c,"local"),E=new me(r.blockName("fa_o"),[y],c,"local"),z=new Ne(r.blockName("fa_b"),"int32"),D=new Ne(r.blockName("fa_h"),"int32"),J=new Ne(r.blockName("fa_qb"),"int32"),j=new Ne(r.blockName("fa_t"),"int32"),U=new Ne(r.blockName("fa_i"),"i32"),P=new Ne(r.blockName("fa_kt"),"int32"),V=new Ne(r.blockName("fa_jj"),"int32"),he=new Ne(r.blockName("fa_dq"),"int32"),le=new Ne(r.blockName("fa_dk"),"int32"),Ie=new Ne(r.blockName("fa_dv"),"int32"),G=new Ne(r.blockName("fa_ds"),"int32"),K=new Ne(r.blockName("fa_doo"),"int32"),ie=new Ne(r.blockName("fa_dw"),"int32"),ze=new Ne(r.blockName("fa_din"),"int32"),Se=st("+",st("*",J,Xo(s)),j),Qe=st("+",st("*",P,Xo(s)),j),It=st("+",st("*",P,Xo(s)),V),Jt=new qe("lt",U,Xo(d)),Gt=new qe("lt",Qe,Xo(g)),tt=xr(he,m,ct(k,[he],Ee(o,[z,D,U,he]))),ye=ct(A,[_e],new fe(-1/0)),He=ct(N,[_e],new fe(0)),gn=xr(ze,y,ct(E,[ze],new fe(0))),jt=new xe(Jt,new te([tt,ye,He,gn])),Zt=new xe(Gt,xr(le,m,ct(S,[j,le],Ee(i,[z,D,Qe,le])))),Qt=new xe(Gt,xr(Ie,y,ct(x,[j,Ie],Ee(a,[z,D,Qe,Ie])))),on=ct(v,[_e],new fe(0)),Jn=xr(G,m,ct(v,[_e],st("+",Ee(v,[_e]),st("*",Ee(k,[G]),Ee(S,[V,G]))))),Bn=new te([ct(v,[_e],st("*",Ee(v,[_e]),new fe(u))),ct(O,[_e],hu("max",Ee(A,[_e]),Ee(v,[_e]))),ct(R,[_e],hu("exp",st("-",Ee(v,[_e]),Ee(O,[_e])))),ct(B,[_e],hu("exp",st("-",Ee(A,[_e]),Ee(O,[_e])))),ct(N,[_e],st("+",st("*",Ee(N,[_e]),Ee(B,[_e])),Ee(R,[_e])))]),yr=xr(K,y,ct(E,[K],st("+",st("*",Ee(E,[K]),Ee(B,[_e])),st("*",Ee(R,[_e]),Ee(x,[V,K]))))),Zn=ct(A,[_e],Ee(O,[_e])),pt=new te([on,Jn,Bn,yr,Zn]),Qn=new qe("lt",It,Xo(g)),mt=p?new xe(new qe("le",It,st("+",U,Xo(T))),pt):pt,Or=xr(V,s,new xe(Qn,mt)),hs=new xe(Jt,Or),$s=new te([Zt,Qt,new On,hs,new On]),Br=xr(P,b,$s),Xn=new xe(Jt,xr(ie,y,ct(_,[z,D,U,ie],st("/",Ee(E,[ie]),Ee(N,[_e]))))),_r=new Ye(U,Se,new te([jt,Br,Xn]));for(let Qr of[E,B,R,O,v,N,A,k])_r=new Ve(Qr,Qr.scope,_r);let fo=Qh(j,"threadIdx.x",s,_r),Rr=new Ve(S,"shared",new Ve(x,"shared",fo)),Mr=Qh(J,"blockIdx.z",w,Rr),ds=Qh(D,"blockIdx.y",f,Mr);return Qh(z,"blockIdx.x",h,ds)}function iv(){Me("scaled_dot_product_attention",ov),Ik("scaled_dot_product_attention","cuda",(r,e,t,n)=>{let{Lq:s,Dk:o,Dv:i,dtype:a}=jb(e,t),u=r.target,p=u&&u.sharedMemoryBytes||0,c=u&&u.maxThreadsPerBlock||1024,h=Math.min(c,s,Math.floor(p/((o+i)*bn(a)*2)));return h<8?ov(r,e,t,n):DL(r,e,t,n,h)})}var _e,Xo,Ee,ct,st,hu,xr,Qh,av=M(()=>{"use strict";De();xo();Fr();Vn();_e=new L(0),Xo=l(r=>new L(r),"I"),Ee=l((r,e)=>new q(r,e),"ld"),ct=l((r,e,t)=>new Y(r,e,t),"st"),st=l((r,e,t)=>new C(r,e,t),"mop"),hu=l((r,...e)=>new ke(r,e),"ext"),xr=l((r,e,t,n=F.SERIAL)=>new de(r,_e,new L(e),n,t),"forL"),Qh=l((r,e,t,n)=>new de(r,_e,new L(t),F.THREAD_BINDING,n,e),"forT");l(jb,"dims");l(ov,"buildNaive");l(DL,"buildTiled");l(iv,"register")});function uv(r,e,t=new Set){let n=ne.get(e.opName);if(!n||!n.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)!==r)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),!uv(a,u,t)))return!1}}return!0}function $L(r){return zh([...r.ops()],"ignore")}function Xh(r,e=null,t=null){let n=new Kh;n.target=e;let s=[],o=new Map;for(let g of r.args){let y=n.allocVar("arg");s.push(y),o.set(y,n.getOrAllocBuffer(g))}let i=r.getReturnOp(),a=new Set;for(let g of r.args)a.add(n.getOrAllocBuffer(g));let u=[],p=new Set;for(let g=0;g<i.numOperands;g++){let y=n.allocVar("ret");s.push(y);let _=n.getOrAllocBuffer(i.getOperand(g));if(a.has(_)||p.has(_)){let b=n.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 h=[];for(let g of r.ops())ln(g.opName)&&h.push(pu(n,g));for(let g of $L(r)){if(qt(g.opName)||ln(g.opName))continue;if(g.opName==="fusion"){Jk(g)?h.push(Yk(n,g)):Zk(n,g,h);continue}if(lu(g.opName)&&!c.has(g.getResult(0))&&g.getOperand(0).getUsers().length===1&&g.getResult(0).getUsers().every(T=>uv(g.getResult(0),T))){let T=n.getOrAllocBuffer(g.getOperand(0)),S=g.getResult(0).type.shape,x=g.getAttr("broadcast_dimensions"),k;if(x&&x.length>0)k=x;else{let A=S.length-T.shape.length;k=Array.from({length:T.shape.length},(N,v)=>v+A)}T.broadcastDims=k,n.bufferMap.set(g.getResult(0),T);continue}let y=Qo(g.opName,e,t);if(!y)throw new Error(`No lowering rule defined for op: ${g.opName}`);let _=new Array(g.numOperands);for(let T=0;T<g.numOperands;T++)_[T]=n.getOrAllocBuffer(g.getOperand(T));let b=new Array(g.numResults);for(let T=0;T<g.numResults;T++)b[T]=n.getOrAllocBuffer(g.getResult(T));let w=y(n,g,_,b);w&&h.push(w)}for(let{src:g,dst:y}of u){let{loopVars:_,loopBinds:b,indices:w,extentNodes:T}=xt(n,g.shape,g),S=new q(g,w),x=new Y(y,w,S),k=new pe(n.blockName("copy_block"),b,[{buffer:g}],[{buffer:y}],x);h.push(St(k,_,g.shape,T))}let f=[],d=new Set;for(let g of n.shapeParams.values())d.has(g.name)||(d.add(g.name),f.push(g));for(let[g,y]of n.symVars)if(!d.has(y.name))throw new Error(`Symbolic dimension '${g}' has no input dimension to bind it to at runtime`);for(let g of f)s.push(g);let m=new Jo(r.name,s,h.length===1?h[0]:new te(h),o,f,new Map(n.shapeParams));return r._partitionTarget&&(m._partitionTarget=r._partitionTarget),m}var PL,Ui=M(()=>{"use strict";De();ji();At();Fr();Tr();Yh();Pk();Fk();Gk();qk();Kk();Hk();Xk();nv();sv();av();Yh();PL=["compare","select","clamp","convert","copy_to_device","dot","fusion"];for(let r of[..._c(),...PL])ne.has(r)&&ne.registerOpAttr(r,"broadcastViewSafe",!0);l(uv,"broadcastViewSafeForUser");Lk();Dk();$k();Vk();jk();Uk();Wk();Qk();tv();rv();iv();l($L,"topologicalOps");l(Xh,"lowerGraphToPrimFunc")});var lv={};nt(lv,{CPUTarget:()=>Us,CUDATarget:()=>ed,TargetFeatures:()=>Ki,TargetKind:()=>Ot,WasmTarget:()=>td,WebGPUTarget:()=>nd});var Ot,Ki,Us,ed,td,nd,Ks=M(()=>{"use strict";Ot=Object.freeze({CPU:"cpu",CUDA:"cuda",WEBGPU:"webgpu",WASM:"wasm",ACCELERATOR:"accelerator"}),Ki=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===Ot.CUDA||this.kind===Ot.WEBGPU}isWebGPU(){return this.kind===Ot.WEBGPU}isCPU(){return this.kind===Ot.CPU}isWasm(){return this.kind===Ot.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)}},Us=l((r={})=>new Ki({kind:Ot.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,...r}),"CPUTarget"),ed=l((r={})=>new Ki({kind:Ot.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,...r}),"CUDATarget"),td=l((r={})=>new Ki({kind:Ot.WASM,name:"wasm_generic",vectorWidth:4,numCores:1,cacheLineSizeBytes:64,memoryBandwidthGBs:10,computeTFLOPs:.1,supportsInt8:!0,simd:!0,...r}),"WasmTarget"),nd=l((r={})=>new Ki({kind:Ot.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,...r}),"WebGPUTarget")});function du(r,e,t,n,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=r.strides[i];u===1?o.push(a):typeof u=="number"&&u>=0?o.push(`${a} * ${u}`):o.push(`${a} * ${n(r,i)}`)}return o.length===0?"0":o.join(" + ")}var rd=M(()=>{"use strict";l(du,"flattenRowMajorIndex")});function ei(r){return zL[r]||"f32"}function ir(r){if(!r)return"f32";if(r._dtype)return r._dtype;switch(r.type){case"IntImmNode":return"i32";case"FloatImmNode":return"f32";case"LIRFlatLoadNode":return ei(r.dtype);case"BufferLoadNode":return ei(r.buffer.dtype);case"CastNode":return ei(r.toDtype);case"CallExternNode":return ei(r.dtype||"f32");case"CompareNode":return"i32";case"VariableNode":return ei(r.dtype||"i32");case"MathOpNode":{let e=ir(r.a);if(rr(e))return e;if(r.b){let t=ir(r.b);if(rr(t))return t}return e}case"IfThenElseNode":return ir(r.thenBody);default:return"f32"}}function No(r){!r||typeof r!="object"||!r.type||(r._dtype=ir(r))}function cv(r){return FL.has(r)}var vo,sd,wc,Tc,od,mu,id,FL,zL,xc=M(()=>{"use strict";Vn();vo=class r{static{l(this,"LIRNode")}constructor(){this.type=this.constructor.name.replace(/^_+/,""),this._parent=null,this._parentKey=null,this._parentIdx=-1}_setChild(e,t,n=-1){(t instanceof r||t&&t._parent!==void 0)&&(t._parent=this,t._parentKey=e,t._parentIdx=n)}_setChildren(e,t){if(t)for(let n=0;n<t.length;n++){let s=t[n];(s instanceof r||s&&s._parent!==void 0)&&(s._parent=this,s._parentKey=e,s._parentIdx=n)}}replaceWith(e){let t=this._parent;return t?(this._parentIdx>=0?t[this._parentKey][this._parentIdx]=e:t[this._parentKey]=e,e&&(e instanceof r||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}},sd=class extends vo{static{l(this,"LIRFunc")}constructor(e,t,n,s,o,i,a){super(),this.name=e,this.params=t,this.body=n,this.bufferMap=s,this.shapeParams=o,this.shapeParamMap=i,this.metadata=a,this._setChild("body",n)}},wc=class extends vo{static{l(this,"LIRFlatLoadNode")}constructor(e,t,n){super(),this.buffer=e,this.offsetExpr=t,this.dtype=n,this._setChild("offsetExpr",t)}},Tc=class extends vo{static{l(this,"LIRFlatStoreNode")}constructor(e,t,n,s){super(),this.buffer=e,this.offsetExpr=t,this.value=n,this.dtype=s,this._setChild("offsetExpr",t),this._setChild("value",n)}},od=class extends vo{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)}},mu=class extends vo{static{l(this,"LIRBindingsNode")}constructor(e,t){super(),this.bindings=e,this.body=t,this._setChild("body",t)}},id=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}},FL=new Set(["sqrt","abs","ceil","floor","min","max"]),zL={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(ei,"normalizeDtype");l(ir,"inferDtype");l(No,"annotateDtype");l(cv,"isWasmNativeOp")});function Gn(r){return r instanceof Tt||r instanceof vo}function ve(r,e,t={}){return{key:r,kind:e,array:!!t.array,poly:!!t.poly,bind:!!t.bind,param:!!t.param,region:!!t.region,iterVarBinding:!!t.iterVarBinding,bindingsExpr:!!t.bindingsExpr}}function Ub(r){return{kinds:r.kinds||"both",descendParams:r.descendParams===!0,bindVars:r.bindVars!==!1}}function VL(r,e){return e.iterVarBinding?{read(){return(r.iterVars||[]).map(t=>t&&t.binding).filter(Gn)},write(t){let n=0;for(let s of r.iterVars||[])s&&Gn(s.binding)&&(s.binding=t[n++])}}:e.bindingsExpr?{read(){return(r.bindings||[]).map(t=>t&&t.expr).filter(Gn)},write(t){let n=0;for(let s of r.bindings||[])s&&Gn(s.expr)&&(s.expr=t[n++])}}:e.poly?{read(){let t=r[e.key];return Array.isArray(t)?t.filter(Gn):Gn(t)?[t]:[]},write(t){let n=r[e.key];if(Array.isArray(n)){let s=0;r[e.key]=n.map(o=>Gn(o)?t[s++]:o)}else t.length&&(r[e.key]=t[0])}}:e.array?{read(){let t=r[e.key];return Array.isArray(t)?t.filter(Gn):[]},write(t){let n=r[e.key];if(!Array.isArray(n))return;let s=0;r[e.key]=n.map(o=>Gn(o)?t[s++]:o)}}:{read(){return Gn(r[e.key])?[r[e.key]]:[]},write(t){t.length&&Gn(r[e.key])&&(r[e.key]=t[0])}}}function Kb(r,e={}){let t=Ub(e),n=qb[r.type];if(n===void 0)throw new Error(`ir_visitor: no child schema for node type '${r.type}'`);let s=[];for(let o of n)o.region||o.param&&!t.descendParams||o.bind&&!t.bindVars||t.kinds!=="both"&&o.kind!==t.kinds||s.push(VL(r,o));return s}function en(r,e={}){if(!Gn(r)||qb[r.type]===void 0)return[];let t=[];for(let n of Kb(r,e)){let s=n.read();for(let o=0;o<s.length;o++)t.push(s[o])}return t}function pv(r,e,t,n){r._parent=e,r._parentKey=t,r._parentIdx=n}function GL(r){let e=qb[r.type];if(e)for(let t of e){if(t.iterVarBinding||t.bindingsExpr||t.region)continue;let n=r[t.key];if(Array.isArray(n))for(let s=0;s<n.length;s++)Gn(n[s])&&pv(n[s],r,t.key,s);else Gn(n)&&pv(n,r,t.key,-1)}}function fv(r,e,t,n,s,o){if(o.stop||!Gn(r))return;let i={parent:n,depth:s},a=!0;if(e.pre){let u=e.pre(r,i);if(u===ad){o.stop=!0;return}u===!1&&(a=!1)}if(a)for(let u of Kb(r,t)){let p=u.read();for(let c=0;c<p.length;c++)if(fv(p[c],e,t,r,s+1,o),o.stop)return}e.post&&e.post(r,i)}function Ss(r,e,t={}){fv(r,typeof e=="function"?{pre:e}:e||{},Ub(t),null,0,{stop:!1})}function gu(r,e,t={}){let n=[];return Ss(r,s=>{e(s)&&n.push(s)},t),n}function Sc(r,e,t={}){let n=!1;return Ss(r,s=>{if(e(s))return n=!0,ad},t),n}function ti(r,e,t={}){let n=Ub(t);return hv(r,e,n)}function hv(r,e,t){if(!Gn(r))return r;let n=!1;for(let o of Kb(r,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=hv(i[p],e,t);c!==i[p]&&(a=!0),u[p]=c}a&&(o.write(u),n=!0)}n&&GL(r);let s=e(r);return s??r}var ad,qb,Gr=M(()=>{"use strict";De();xc();ad=Symbol("ir-visitor-stop");l(Gn,"isIRNode");l(ve,"field");qb={VariableNode:[],IntImmNode:[],FloatImmNode:[],SyncThreadsNode:[],MathOpNode:[ve("a","expr"),ve("b","expr")],CompareNode:[ve("a","expr"),ve("b","expr")],CastNode:[ve("expr","expr")],CallExternNode:[ve("args","expr",{array:!0})],BufferLoadNode:[ve("indices","expr",{array:!0})],IfThenElseNode:[ve("condition","expr"),ve("thenBody","stmt"),ve("elseBody","stmt")],BlockRealizeNode:[ve("binding","expr",{poly:!0}),ve("iterVar","expr",{bind:!0})],LIRFlatLoadNode:[ve("offsetExpr","expr")],PrimFunc:[ve("params","expr",{array:!0,param:!0}),ve("body","stmt")],ForNode:[ve("min","expr"),ve("extent","expr"),ve("loopVar","expr",{bind:!0}),ve("body","stmt")],BlockNode:[ve("iterVars","expr",{iterVarBinding:!0}),ve("reads","expr",{region:!0,array:!0}),ve("writes","expr",{region:!0,array:!0}),ve("initBody","stmt"),ve("body","stmt")],SeqNode:[ve("stmts","stmt",{array:!0})],VecCopyNode:[ve("dstIndex","expr"),ve("srcIndex","expr")],LetStmtNode:[ve("value","expr"),ve("variable","expr",{bind:!0}),ve("body","stmt")],AllocateNode:[ve("body","stmt")],WhileNode:[ve("condBody","stmt"),ve("condVar","expr",{bind:!0}),ve("loopBody","stmt")],EvaluateNode:[ve("value","expr")],BufferStoreNode:[ve("indices","expr",{array:!0}),ve("value","expr")],LIRFunc:[ve("body","stmt")],LIRFlatStoreNode:[ve("offsetExpr","expr"),ve("value","expr")],LIRAccumulatorNode:[ve("loopVar","expr",{bind:!0}),ve("extent","expr"),ve("initLoad","stmt"),ve("initBody","stmt"),ve("body","expr"),ve("flushStore","stmt")],LIRBindingsNode:[ve("bindings","expr",{bindingsExpr:!0}),ve("body","stmt")]};l(Ub,"normOpts");l(VL,"buildAccessor");l(Kb,"childAccessors");l(en,"irChildNodes");l(pv,"setParent");l(GL,"relink");l(fv,"walkInner");l(Ss,"walk");l(gu,"collect");l(Sc,"some");l(ti,"transform");l(hv,"transformInner")});function jL(r,e,t,n){switch(r){case"add":return`(${e} + ${t})`;case"sub":return`(${e} - ${t})`;case"mul":return`(${e} * ${t})`;case"neg":return`(-${e})`;case"div":return n==="js"?`((${e} / ${t}) | 0)`:`(${e} / ${t})`;case"mod":return n==="js"?`((${e} % ${t} + ${t}) % ${t})`:`(${e} % ${t})`;case"ceildiv":return n==="js"?`(((${e} + ${t} - 1) / ${t}) | 0)`:`((${e} + ${t} - 1) / ${t})`;case"max":return n==="js"?`Math.max(${e}, ${t})`:`max(${e}, ${t})`;case"min":return n==="js"?`Math.min(${e}, ${t})`:`min(${e}, ${t})`;default:throw new Error(`emitSymInt: unsupported op '${r}'`)}}function Wb(r,e,t="c"){if(typeof r=="number"||!(r instanceof Ce))return String(r);if(r.type==="var")return e({name:qa(r.name)});if(t==="wat")throw new Error("emitSymInt: compound symbolic expressions are not supported on the WASM backend");let n=Wb(r.args[0],e,t),s=r.args.length>1?Wb(r.args[1],e,t):null;return jL(r.type,n,s,t)}function Wi(r){let e=r.indexOf(".");if(e<0)return null;let t=r.charCodeAt(e+1)-120;if(t<0||t>2)return null;let n=r.substring(0,e);return n==="threadIdx"?{space:"thread",axis:t}:n==="blockIdx"?{space:"block",axis:t}:null}function ud(r,e){let t=[e];for(;t.length>0;){let n=t.pop();if(n)switch(n.type){case"SeqNode":for(let s=n.stmts.length-1;s>=0;s--)t.push(n.stmts[s]);continue;case"AllocateNode":r._visitAllocateNode(n),t.push(n.body);continue;case"ForNode":r._visitForNode(n);continue;case"BlockNode":r._visitBlockNode(n);continue;case"IfThenElseNode":r._visitIfStmt(n);continue;case"LetStmtNode":r._visitLetStmtNode(n);continue;case"BufferStoreNode":r._visitBufferStoreNode(n);continue;case"LIRFlatStoreNode":r._visitLIRFlatStore(n);continue;case"LIRBindingsNode":r._visitLIRBindings(n);continue;case"LIRAccumulatorNode":r._visitLIRAccumulator(n);continue;case"WhileNode":r._visitWhileNode(n);continue;case"SyncThreadsNode":r._emitSync();continue;case"VecCopyNode":r._visitVecCopyNode(n);continue;case"EvaluateNode":continue;default:throw new Error(`${r.constructor.name}: unhandled statement node '${n.type}'`)}}}function yu(r,e){let t=[r];for(;t.length>0;){let n=t.pop();if(!(!n||typeof n!="object")&&e(n)!==!1)for(let s of qL){let o=n[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 ld(r){let e=r;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 ni(r,e,t,n,s,o="c"){let i=e.shape[t];if(i instanceof Ce)return Wb(i,n,o);if(r&&r.shapeParamMap){let a=r.shapeParamMap.get(`${e.name}:${t}`);if(a)return n(a)}throw new Error(`${s} codegen: missing shape param for ${e.name}:${t}`)}function cd(r){let e=1;for(let t of r.shape)typeof t=="number"&&t>0&&(e*=t);return e}function ri(r,e,t){let n=[];for(let s=e;s<r.shape.length;s++){let o=r.shape[s];typeof o=="number"&&o>=0?n.push(String(o)):n.push(t(r,s))}return n.length===0?"1":n.join(" * ")}function pd(r,e){let t=r.get(e);if(!t)return 0;let n=0;for(let s of t)s.extent>n&&(n=s.extent);return n}var qL,kc=M(()=>{"use strict";Vs();l(jL,"symOpToString");l(Wb,"emitSymInt");l(Wi,"parseThreadAxis");l(ud,"visitStatements");qL=["body","stmts","thenBody","elseBody","loopBody","condBody","initBody"];l(yu,"walkStmtTree");l(ld,"isZeroFillBody");l(ni,"resolveShapeParam");l(cd,"estimateBufferSize");l(ri,"dynamicDimProduct");l(pd,"maxBindingExtent")});function bu(r){let e=r>=0?1:-1,t=Math.abs(r),n=1/(1+vc*t),s=0;for(let o=_u.length-1;o>=0;o--)s=s*n+_u[o];return e*(1-s*n*Math.exp(-t*t))}function wu(r){return 1-bu(r)}function Tn(r){if(r<.5)return Math.log(Math.PI/Math.abs(Math.sin(Math.PI*r)))-Tn(1-r);let e=r-1,t=Hi[0];for(let s=1;s<Hi.length;s++)t+=Hi[s]/(e+s);let n=e+7+.5;return .5*Math.log(2*Math.PI)+(e+.5)*Math.log(n)-n+Math.log(t)}function Tu(r){return r<.5?Math.PI/(Math.sin(Math.PI*r)*Tu(1-r)):Math.exp(Tn(r))}function Ac(r){let e=0,t=r;for(;t<Yi;)e-=1/t,t+=1;let n=1/t,s=n*n;e+=Math.log(t)-.5*n;let o=s;for(let i of Nc)e+=i*o,o*=s;return e}var Hi,_u,vc,Yi,Nc,Ji=M(()=>{"use strict";Hi=[.9999999999998099,676.5203681218851,-1259.1392167224028,771.3234287776531,-176.6150291621406,12.507343278686905,-.13857109526572012,9984369578019572e-21,15056327351493116e-23],_u=[.254829592,-.284496736,1.421413741,-1.453152027,1.061405429],vc=.3275911,Yi=6,Nc=[-1/12,1/120,-1/252,1/240,-1/132];l(bu,"erfScalar");l(wu,"erfcScalar");l(Tn,"lgammaScalar");l(Tu,"gammaScalar");l(Ac,"digammaScalar")});function gv(r){return Ao[0]=(r&32767)<<13,si[0]*=mv,si[0]>=65536&&(Ao[0]|=2139095040),Ao[0]|=(r&32768)<<16,si[0]}function yv(r){si[0]=r;let e=Ao[0]&2147483648,t=(Ao[0]^e)>>>0,n;if(t>=1199570944)n=t>2139095040?32256:31744;else if(t<947912704)Ao[0]=t,si[0]+=.5,n=Ao[0]-1056964608&65535;else{let s=t>>13&1;t=t+dv>>>0,t=t+s>>>0,n=t>>13&65535}return(n|e>>>16)&65535}function fd(r){return Ao[0]=r<<16>>>0,si[0]}function hd(r){si[0]=r;let e=Ao[0];if((e>>>23&255)===255)return e>>>16&65535;let t=32767+(e>>>16&1)>>>0;return e+t>>>0>>>16&65535}function dd(r,e){return r==="f16"?yv(Number(e)):r==="bf16"?hd(Number(e)):r==="i64"?typeof e=="bigint"?e:BigInt(Math.trunc(e)):e}function md(r,e){return r==="f16"?gv(Number(e)):r==="bf16"?fd(Number(e)):r==="i64"&&typeof e=="bigint"?Number(e):e}var si,Ao,dv,mv,Hb,gd,xu=M(()=>{"use strict";si=new Float32Array(1),Ao=new Uint32Array(si.buffer),dv=(-112<<23>>>0)+4095>>>0,mv=5192296858534828e18;l(gv,"f16ToF32");l(yv,"f32ToF16");l(fd,"bf16ToF32");l(hd,"f32ToBf16");l(dd,"coerceForStorage");l(md,"readFromStorage");Hb={F16_ADD_BIAS:dv|0,F16_MAGIC_MUL:mv},gd=globalThis;gd.__mlfw_f16_to_f32=gv;gd.__mlfw_f32_to_f16=yv;gd.__mlfw_bf16_to_f32=fd;gd.__mlfw_f32_to_bf16=hd});function bv(r){return`((x_erf => { ${KL} })(${r}))`}function HL(r){return`((x_lg => { const lg = ${wv}; return x_lg < 0.5 ? Math.log(Math.PI / Math.abs(Math.sin(Math.PI * x_lg))) - lg(1 - x_lg) : lg(x_lg); })(${r}))`}function YL(r){return`((x_g => { const lg = ${wv}; return x_g < 0.5 ? Math.PI / (Math.sin(Math.PI * x_g) * Math.exp(lg(1 - x_g))) : Math.exp(lg(x_g)); })(${r}))`}var Yb,_v,UL,KL,WL,wv,yd,Tv=M(()=>{"use strict";De();Vn();rd();Gr();kc();Ji();xu();Yb={f16:"__mlfw_f16_to_f32",bf16:"__mlfw_bf16_to_f32"},_v={f16:"__mlfw_f32_to_f16",bf16:"__mlfw_f32_to_bf16"},UL=_u.slice().reverse().reduce((r,e)=>`(${e} + t * ${r})`,"0"),KL=`const t = 1.0 / (1.0 + ${vc} * Math.abs(x_erf)); const p = t * ${UL}; return (x_erf >= 0 ? 1 : -1) * (1.0 - p * Math.exp(-x_erf * x_erf));`;l(bv,"_erfExpr");WL=Hi.map((r,e)=>e===0?`${r}`:`${r} / (zz + ${e})`).join(" + "),wv=`(zg => { const zz = zg - 1; const s = ${WL}; const t = zz + ${7} + 0.5; return ${.5*Math.log(2*Math.PI)} + (zz + 0.5) * Math.log(t) - t + Math.log(s); })`;l(HL,"_lgammaExpr");l(YL,"_gammaExpr");yd=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",n=new Set,s=[];for(let[,c]of e.bufferMap)s.push(c.name),n.add(c.name);for(let c of e.shapeParams)s.push(c.name);this._paramBuffers=n,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,n),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 h=c.numel();h>0&&(p=Math.max(p,c.poolByteOffset+h*bn(c.dtype)))}p>0&&this._emit(`const _mem_pool = new ArrayBuffer(${p});`);for(let[c,h]of o)if(!a.has(c)&&!u.has(c)&&!n.has(c)&&!i.has(c)){let f=h.numel();if(f>0)this._emit(`const ${c} = ${this._allocRhs(h,f)};`);else if(f<0){let d=[];for(let m=0;m<h.shape.length;m++)d.push(typeof h.shape[m]=="number"&&h.shape[m]>=0?String(h.shape[m]):this._resolveShapeParam(h,m));this._emit(`const ${c} = new ${mh(h.dtype)}(${d.join(" * ")});`)}}for(let[,c]of o)n.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 n=mh(e.dtype);return e.poolByteOffset!==void 0&&e.poolByteOffset!==null&&t>0?`new ${n}(_mem_pool, ${e.poolByteOffset}, ${t})`:`new ${n}(${t})`}_wrapLoad(e,t){let n=Yb[e];return n?`${n}(${t})`:t}_wrapStoreVal(e,t){let n=_v[e];return n?`${n}(${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 n=0;n<t.stmts.length-1;n++)this._visitNode(t.stmts[n]);t=t.stmts[t.stmts.length-1];continue;case"AllocateNode":{let n=t.buffer,s=n.numel();s>0?this._emit(`const ${n.name} = ${this._allocRhs(n,s)};`):s<0&&this._emit(`const ${n.name} = new ${mh(n.dtype)}(${this._dynamicNumel(n)});`),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 n=this._exprToJS(e.extent);if(e.kind===F.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+" < "+n+"; "+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+" < "+n+"; "+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 n=this._exprToJS(t.expr);this._aliases.set(t.name,n)}this._visitNode(e.body)}_visitLIRAccumulator(e){let t=e.localName,n=this._exprToJS(e.initLoad);this._emit("let "+t+" = "+n+";");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 n=t.body;if(!n||n.type!=="BufferStoreNode")return null;let s=n,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 n=this._exprToJS(t.binding);this._aliases.set(t.iterVar.name,n)}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=[],n=[{node:e,phase:0}];for(;n.length>0;){let s=n[n.length-1],o=s.node;if(!o){n.pop(),t.push("0");continue}switch(o.type){case"IntImmNode":n.pop(),t.push(String(o.value));continue;case"FloatImmNode":n.pop(),t.push(String(o.value));continue;case"VariableNode":n.pop(),t.push(this._aliases.get(o.name)||o.name);continue;case"BufferLoadNode":{if(n.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(n.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,n.push({node:o.a,phase:0});else if(s.phase===1&&o.b)s.phase=2,n.push({node:o.b,phase:0});else if(n.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,n.push({node:o.a,phase:0});else if(s.phase===1)s.phase=2,n.push({node:o.b,phase:0});else{n.pop();let i=t.pop(),a=t.pop();t.push(`(${a} ${zS(o.direction)} ${i})`)}continue;case"IfThenElseNode":if(s.phase===0)s.phase=1,n.push({node:o.condition,phase:0});else if(s.phase===1)s.phase=2,n.push({node:o.thenBody,phase:0});else if(s.phase===2)s.phase=3,n.push({node:o.elseBody,phase:0});else{n.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,n.push({node:o.expr,phase:0});else{n.pop();let i=t.pop();o.toDtype==="bool"?t.push(`(${i} ? 1 : 0)`):nr(o.toDtype)?t.push(`(${i} | 0)`):Yb[o.toDtype]?t.push(`${Yb[o.toDtype]}(${_v[o.toDtype]}(${i}))`):t.push(`(+${i})`)}continue;case"CallExternNode":if(s.phase<o.args.length){let i=s.phase;s.phase++,n.push({node:o.args[i],phase:0})}else{n.pop();let i=[];for(let u=0;u<o.args.length;u++)i.unshift(t.pop());let a=i.join(", ");if(VS(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(bv(a));else if(o.externName==="erfc")t.push(`(1.0 - ${bv(a)})`);else if(o.externName==="lgamma")t.push(HL(a));else if(o.externName==="gamma")t.push(YL(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 ri(e,0,(t,n)=>this._resolveShapeParam(t,n))}_flatIndex(e,t){return du(e,t,n=>this._exprToJS(n),(n,s)=>this._computeDynamicStride(n,s),!0)}_computeDynamicStride(e,t){return ri(e,t+1,(n,s)=>this._resolveShapeParam(n,s))}_resolveShapeParam(e,t){return ni(this._primFunc,e,t,n=>n.name,"CPU","js")}_cleanupSource(e){let t=e.split(`
3
+ `),n=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&&(n[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(n[i]!==null&&s.get(n[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 n=t.value;if(!(n.type==="FloatImmNode"&&n.value===0||n.type==="IntImmNode"&&n.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 ld(e)}_scanTree(e,t,n,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&&n.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 en(i))o.push(a)}}}_findZeroOnlyBuffers(e,t){let n=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)||(n.has(a)||n.set(a,[]),n.get(a).push(i.value))}for(let a of en(i))s.push(a)}}let o=new Set;this._constantBuffers=new Map;for(let[i,a]of n){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 Sv(r,e){xv.set(r,e)}function kv(r){return xv.get(r)||null}var xv,vv=M(()=>{"use strict";xv=new Map;l(Sv,"registerCudaIntrin");l(kv,"getCudaIntrin");Sv("wmma_16x16x16_f16f16f32",(r,e)=>{let{M:t,N:n,K:s,a:o,b:i,c:a}=e;r._blockDim=[32,1,1],r._gridDim=[Math.ceil(t/16),Math.ceil(n/16),1],r._emit("const int warpM = blockIdx.x;"),r._emit("const int warpN = blockIdx.y;"),r._emit("fragment<accumulator, 16, 16, 16, float> cf;"),r._emit("fill_fragment(cf, 0.0f);"),r._emit(`for (int kk = 0; kk < ${s}; kk += 16) {`),r._indent++,r._emit("fragment<matrix_a, 16, 16, 16, half, row_major> af;"),r._emit("fragment<matrix_b, 16, 16, 16, half, row_major> bf;"),r._emit(`load_matrix_sync(af, ${o} + warpM * 16 * ${s} + kk, ${s});`),r._emit(`load_matrix_sync(bf, ${i} + kk * ${n} + warpN * 16, ${n});`),r._emit("mma_sync(cf, af, bf, cf);"),r._indent--,r._emit("}"),r._emit(`store_matrix_sync(${a} + warpM * 16 * ${n} + warpN * 16, cf, ${n}, mem_row_major);`)});Sv("gemm_pipelined_f32",(r,e)=>{let{M:t,N:n,K:s,a:o,b:i,c:a,tile:u=16}=e;r._blockDim=[u,u,1],r._gridDim=[Math.ceil(n/u),Math.ceil(t/u),1];let p=u;r._emit(`__shared__ float As[2][${p}][${p}];`),r._emit(`__shared__ float Bs[2][${p}][${p}];`),r._emit(`const int row = blockIdx.y * ${p} + threadIdx.y;`),r._emit(`const int col = blockIdx.x * ${p} + threadIdx.x;`),r._emit("float acc = 0.0f;"),r._emit(`const int nTiles = ${s} / ${p};`),r._emit(`__pipeline_memcpy_async(&As[0][threadIdx.y][threadIdx.x], &${o}[row * ${s} + threadIdx.x], sizeof(float));`),r._emit(`__pipeline_memcpy_async(&Bs[0][threadIdx.y][threadIdx.x], &${i}[threadIdx.y * ${n} + col], sizeof(float));`),r._emit("__pipeline_commit();"),r._emit("for (int t = 0; t < nTiles; t++) {"),r._indent++,r._emit("const int cur = t & 1, nxt = (t + 1) & 1;"),r._emit("if (t + 1 < nTiles) {"),r._indent++,r._emit(`__pipeline_memcpy_async(&As[nxt][threadIdx.y][threadIdx.x], &${o}[row * ${s} + (t + 1) * ${p} + threadIdx.x], sizeof(float));`),r._emit(`__pipeline_memcpy_async(&Bs[nxt][threadIdx.y][threadIdx.x], &${i}[((t + 1) * ${p} + threadIdx.y) * ${n} + col], sizeof(float));`),r._emit("__pipeline_commit();"),r._indent--,r._emit("}"),r._emit("__pipeline_wait_prior(t + 1 < nTiles ? 1 : 0);"),r._emit("__syncthreads();"),r._emit(`for (int kk = 0; kk < ${p}; kk++) acc += As[cur][threadIdx.y][kk] * Bs[cur][kk][threadIdx.x];`),r._emit("__syncthreads();"),r._indent--,r._emit("}"),r._emit(`${a}[row * ${n} + col] = acc;`)})});var Nv,Zb,_d,Av=M(()=>{"use strict";De();Vn();rd();Gr();kc();vv();Nv=256*1024,Zb=class{static{l(this,"CUDAKernel")}constructor(e,t,n,s,o,i,a,u){this.name=e,this.source=t,this.blockDim=n,this.gridDim=s,this.sharedMemBytes=o,this.params=i,this.outputIndices=a,this.scratch=u||[]}},_d=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 n=[],s=[],o=[],i=0;for(let[,m]of e.bufferMap)s.push(m.name),n.push(`${_b(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),n.push(`${_b(m.dtype)} ${m.name}`);for(let m of e.shapeParams)s.push(m.name),n.push(`int ${m.name}`);this._emit(`__global__ void ${e.name}(${n.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}${$r(m.dtype)} ${m.name}[${g>0?g:1}];`)}for(let m of this._promotedBufferDecls)this._emit(`__shared__ ${$r(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=kv(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)],h=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)],f=c[0]*c[1]*c[2];if(f>p.maxThreadsPerBlock)throw new Error(`[codegen] kernel '${e.name}' block ${c.join("x")} = ${f} threads exceeds maxThreadsPerBlock ${p.maxThreadsPerBlock}`);let d=this._sharedBuffers.reduce((m,g)=>m+Math.max(g.sizeInBytes(),0),0)+this._promotedBufferDecls.reduce((m,g)=>m+Math.max(g.size,0)*bn(g.dtype),0);if(d>p.sharedMemoryBytes)throw new Error(`[codegen] kernel '${e.name}' shared memory ${d} bytes exceeds device limit ${p.sharedMemoryBytes}`);return new Zb(e.name,this._lines.join(`
5
+ `),c,h,d,s,o,this._globalScratch)}_scanBindings(e){let t=[e];for(;t.length>0;){let n=t.pop();if(n){if(n.type==="ForNode"&&n.kind===F.THREAD_BINDING&&n.threadTag){let s=n.extent.type==="IntImmNode"?n.extent.value:0,o=n.extent.type!=="IntImmNode",i={varName:n.loopVar.name,extent:s,isDynamic:o,extentNode:n.extent};this._threadBindings.has(n.threadTag)?this._threadBindings.get(n.threadTag).push(i):this._threadBindings.set(n.threadTag,[i]),o||this._applyBindingDim(n.threadTag,s)}if(n.type==="AllocateNode"&&n.scope==="shared"&&this._sharedBuffers.push(n.buffer),n.body&&t.push(n.body),n.stmts)for(let s of n.stmts)t.push(s);n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody)}}}_applyBindingDim(e,t){let n=Wi(e);n&&(n.space==="thread"?this._blockDim[n.axis]=Math.max(this._blockDim[n.axis],t):this._gridDim[n.axis]=Math.max(this._gridDim[n.axis],t))}_emit(e){this._lines.push(" ".repeat(this._indent)+e)}_visitNode(e){ud(this,e)}_emitSync(){this._emit("__syncthreads();")}_matchFullReduction(e){let t=[],n=e;for(;n&&n.type==="ForNode";){if(n.kind!==F.SERIAL)return null;let f=n.extent.type==="IntImmNode"?n.extent.value:0;if(f<=0)return null;t.push({extC:this._exprToC(n.extent),extVal:f,varName:n.loopVar.name}),n=n.body}if(!n||n.type!=="BlockNode")return null;let s=n,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(f=>this._exprToC(f)).join(","),u=l(f=>f&&f.type==="BufferLoadNode"&&f.buffer===o.buffer&&f.indices.map(d=>this._exprToC(d)).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(f=>f.varName));if(a.split(/[^A-Za-z0-9_]/).some(f=>c.has(f)))return null;let h=t.reduce((f,d)=>f*d.extVal,1);return h<2048?null:{loops:t,block:s,store:o,valExpr:p,total:h}}_emitParallelReduction(e,t){this._blockDim=[256,1,1],this._didParallelReduce=!0;let s=$r(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===F.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===F.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,n=this._exprToC(e.extent);(e.kind===F.UNROLLED||e.kind===F.VECTORIZED)&&this._emit("#pragma unroll"),this._emit(`for (int ${t} = 0; ${t} < ${n}; ${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 n=e.buffer.align16?"__align__(16) ":"";this._emit(`${n}${$r(e.buffer.dtype)} ${e.buffer.name}[${t}];`)}else this._emit(`${$r(e.buffer.dtype)}* ${e.buffer.name} = (${$r(e.buffer.dtype)}*)alloca(${this._dynamicNumel(e.buffer)} * sizeof(${$r(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(`${$r(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=`${$r(e.dstBuffer.dtype)}${e.width}`,n=`${e.dstBuffer.name}[${this._flatIndex(e.dstBuffer,[e.dstIndex])}]`,s=`${e.srcBuffer.name}[${this._flatIndex(e.srcBuffer,[e.srcIndex])}]`;this._emit(`*reinterpret_cast<${t}*>(&${n}) = *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,n=e.dtype||this._defaultDtype;this._emit(`${$r(n)} ${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"?nr(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 n=this._exprToC(e.b);return e.op==="//"?`(${t} / ${n})`:`(${t} ${e.op} ${n})`}case"CompareNode":return`(${this._exprToC(e.a)} ${Ka(e.direction)} ${this._exprToC(e.b)})`;case"IfThenElseNode":return`(${this._exprToC(e.condition)} ? ${this._exprToC(e.thenBody)} : ${this._exprToC(e.elseBody)})`;case"CastNode":return`((${$r(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=bb(this._defaultDtype),n=String(e);return`${/[.eEnN]/.test(n)?n:n+".0"}${t}`}_emitExternCall(e){let t=e.args.length,n=new Array(t);for(let a=0;a<t;a++)n[a]=this._exprToC(e.args[a]);let s=n.join(", "),o=e.dtype||this._defaultDtype;if(e.externName==="rsqrt")return`${gh("rsqrt",o)}(${s})`;if(e.externName==="sign"){let a=n[0],u=`0.0${bb(o)}`;return`((${a} > ${u}) - (${a} < ${u}))`}if(e.externName==="min"||e.externName==="max"){if(nr(o)){let a=e.externName==="min"?"<":">";return`((${n[0]}) ${a} (${n[1]}) ? (${n[0]}) : (${n[1]}))`}return`${gh(e.externName,o)}(${s})`}return`${gh(e.externName,o)}(${s})`}_flatIndex(e,t){return du(e,t,n=>this._exprToC(n),(n,s)=>this._computeDynamicStride(n,s),!1)}_computeDynamicStride(e,t){return ri(e,t+1,(n,s)=>this._resolveShapeParam(n,s))}_dynamicNumel(e){return ri(e,0,(t,n)=>this._resolveShapeParam(t,n))}_getMaxBindingExtent(e){return pd(this._threadBindings,e)}_scanStoreTargets(e){let t=[e];for(;t.length>0;){let n=t.pop();if(n){(n.type==="BufferStoreNode"||n.type==="LIRFlatStoreNode")&&this._storeBuffers.add(n.buffer.name),n.type==="VecCopyNode"&&this._storeBuffers.add(n.dstBuffer.name),n.type==="LIRAccumulatorNode"&&n.flushStore&&this._storeBuffers.add(n.flushStore.buffer.name);for(let s of en(n))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 n=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===F.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(n,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 en(a))i(c,p)},"walk");i(e.body,[]);for(let[a,u]of n){let p=s.get(a);if(!p)continue;let c=new Set(u);for(let h of p)c.add(h);if(c.size>1)return!0}return!1}_findThreadPrivateAllocs(e,t){let n=l((s,o)=>{if(!s||typeof s!="object")return;let i=o;if(s.type==="ForNode"&&s.kind===F.THREAD_BINDING&&s.threadTag){let a=Wi(s.threadTag);a&&a.space==="thread"&&(i=!0)}i&&s.type==="AllocateNode"&&s.buffer&&t.add(s.buffer.name);for(let a of en(s))n(a,i)},"walk");n(e,!1)}_findCrossThreadBuffers(e){let t=new Set;for(let[,d]of e.bufferMap)t.add(d.name);let n=new Set(this._sharedBuffers.map(d=>d.name)),s=new Set;this._findThreadPrivateAllocs(e.body,s);let o=l(d=>d&&!t.has(d.name)&&!n.has(d.name)&&!s.has(d.name),"isIntermediate"),i=l(d=>typeof d.numel!="function"||d.numel()!==1,"isMulti"),a=new Set,u=new Set,p=new Set,c=new Set,h=l((d,m)=>{if(!d||typeof d!="object")return;let g=m;if(d.type==="ForNode"&&d.kind===F.THREAD_BINDING&&d.threadTag){let y=d.extent&&d.extent.type==="IntImmNode"?d.extent.value:0,_=this._getMaxBindingExtent(d.threadTag);y>0&&_>0&&y<_&&(g=!0)}(d.type==="BufferStoreNode"||d.type==="LIRFlatStoreNode")&&o(d.buffer)&&(i(d.buffer)?a.add(d.buffer.name):g&&p.add(d.buffer.name)),(d.type==="BufferLoadNode"||d.type==="LIRFlatLoadNode")&&o(d.buffer)&&(i(d.buffer)?u.add(d.buffer.name):c.add(d.buffer.name));for(let y of en(d))h(y,g)},"walk");h(e.body,!1);let f=new Set;for(let d of a)u.has(d)&&f.add(d);for(let d of p)c.has(d)&&f.add(d);return f}_crossThreadBuffersAreBlockLocal(e,t){let n=!0,s=l((o,i)=>{if(!o||typeof o!="object")return;let a=i;if(o.type==="ForNode"&&o.kind===F.THREAD_BINDING&&o.threadTag){let u=Wi(o.threadTag);u&&u.space==="block"&&(a=!0)}a&&(o.type==="BufferStoreNode"||o.type==="LIRFlatStoreNode")&&o.buffer&&t.has(o.buffer.name)&&(n=!1);for(let u of en(o))s(u,a)},"walk");return s(e.body,!1),n}_promoteCrossThreadToShared(e,t){let n=new Set;for(let[,p]of e.bufferMap)n.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(n.has(p)||this._promotedBuffers.has(p)||i.has(p))continue;let c=s.get(p);if(!c)return!1;let h=c.numel(),f=h>0?h:this._estimateBufferSize(c);if(f<=0||(a+=f*bn(c.dtype),a>o))return!1;u.push({name:p,dtype:c.dtype,size:f})}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 n=!1;for(let[u,p]of this._threadBindings){let c=new Set;for(let f of p)f.extent>0&&c.add(f.extent);if(c.size<=1)continue;let h=Wi(u);if(h&&h.space==="block"){this._serializeThreads=!0,this._needsBarriers=!1;return}n=!0}if(!n)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 en(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(),h=c>0?c:this._estimateBufferSize(p);h>0&&(this._promotedBuffers.add(u),this._promotedBufferDecls.push({name:u,dtype:p.dtype,size:h}))}}_emitMissingLocalDecls(e){let t=new Set;for(let[,o]of e.bufferMap)t.add(o.name);let n=new Set;this._scanAllocateNodes(e.body,n);let s=new Map;this._scanBufferRefs(e.body,s);for(let[o,i]of s){if(t.has(o)||n.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(`${$r(i.dtype)} ${o}[${u}];`))}}_collectGlobalScratch(e){let t=this._serializeThreads;if(this._threadBindings.size>0&&!t)return;let n=32768,s=new Set;for(let[,f]of e.bufferMap)s.add(f.name);let o=[],i=new Set,a=l((f,d)=>{if(!d||s.has(f)||this._promotedBuffers.has(f)||this._scratchNames.has(f)||i.has(f))return;let m=typeof d.numel=="function"?d.numel():0,g=m>0?m:this._estimateBufferSize(d);g<=0||(i.add(f),o.push({name:f,dtype:d.dtype,size:g,bytes:g*bn(d.dtype)}))},"consider"),u=[e.body];for(;u.length>0;){let f=u.pop();if(f){f.type==="AllocateNode"&&f.scope!=="shared"&&a(f.buffer.name,f.buffer);for(let d of en(f))u.push(d)}}let p=new Map;this._scanBufferRefs(e.body,p);for(let[f,d]of p)a(f,d);let c=l(f=>{this._scratchNames.add(f.name),this._globalScratch.push({name:f.name,dtype:f.dtype,size:f.size})},"offload");if(!t){for(let f of o)f.size>n&&c(f);return}let h=o.reduce((f,d)=>f+d.bytes,0);if(!(h<=Nv))for(let f of[...o].sort((d,m)=>m.bytes-d.bytes)){if(h<=Nv)break;c(f),h-=f.bytes}}_scanAllocateNodes(e,t){let n=[e];for(;n.length>0;){let s=n.pop();if(s){s.type==="AllocateNode"&&t.add(s.buffer.name);for(let o of en(s))n.push(o)}}}_scanBufferRefs(e,t){let n=[e];for(;n.length>0;){let s=n.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 en(s))n.push(o)}}}_estimateBufferSize(e){return cd(e)}_resolveShapeParam(e,t){return ni(this._primFunc,e,t,n=>n.name,"CUDA","c")}}});var Su,bd,Ov=M(()=>{"use strict";De();Vn();xc();xu();Gr();kc();Su=new Set(["f16","bf16"]),bd=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,h]of e.metadata.memoryLayout.bufferOffsets)this._bufferOffsets.set(c,h);this._totalMemBytes=e.metadata.memoryLayout.totalBytes;for(let[c,h]of e.metadata.externCalls){let f=this._mathImportSig(c,h.argCount);this._imports.set(c,f)}for(let[c,h]of e.metadata.locals)this._ensureLocal(c,ys(h))}else this._layoutBuffers(e),this._scanMathImports(e.body);this._scanParallel(e.body);let n=[];for(let[,c]of e.bufferMap)n.push(c.name);for(let c of e.shapeParams)n.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,h]of this._imports)this._emit(`(import "math" "${c}" (func $math_${c} ${h}))`);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"),n.push("_par_start"),n.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,h]of this._locals)u.push("(local $"+c+" "+h+")");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:n,parallel:this._hasParallel?{extent:this._parallelExtent,outputIndices:this._findOutputIndices(e),poolSafe:this._isParallelSafe(e)}:null}}_isParallelSafe(e){let t=[],n=[e.body];for(;n.length>0;){let p=n.pop();if(p){if(p.type==="ForNode"&&p.kind===F.PARALLEL&&t.push(p),p.body&&n.push(p.body),p.stmts)for(let c of p.stmts)n.push(c);p.thenBody&&n.push(p.thenBody),p.elseBody&&n.push(p.elseBody),p.loopBody&&n.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 h=[p];for(;h.length>0;){let f=h.pop();if(f){if((f.type==="BufferStoreNode"||f.type==="LIRFlatStoreNode")&&c.add(f),f.type==="LIRAccumulatorNode"&&f.flushStore&&c.add(f.flushStore),f.body&&h.push(f.body),f.stmts)for(let d of f.stmts)h.push(d);f.thenBody&&h.push(f.thenBody),f.elseBody&&h.push(f.elseBody),f.loopBody&&h.push(f.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(Su.has(e)){this._emitHalfDecode(e);return}this._emit(dh(e))}_emitStoreOp(e){if(Su.has(e)){this._emitHalfEncode(e);return}this._emit($S(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 "+Hb.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 "+Hb.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[,n]of e.bufferMap)if(Su.has(n.dtype)){t=!0;break}if(!t&&e.metadata&&e.metadata.locals){for(let[,n]of e.metadata.locals)if(Su.has(n)){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 n=t.pop();if(!(!n||typeof n!="object")){if((n.type==="BufferLoadNode"||n.type==="BufferStoreNode")&&n.buffer&&Su.has(n.buffer.dtype)||(n.type==="LIRFlatLoadNode"||n.type==="LIRFlatStoreNode")&&Su.has(n.dtype))return!0;if(n.body&&t.push(n.body),n.value&&typeof n.value=="object"&&t.push(n.value),n.stmts)for(let s of n.stmts)t.push(s);if(n.a&&t.push(n.a),n.b&&t.push(n.b),n.expr&&t.push(n.expr),n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody),n.initBody&&t.push(n.initBody),n.condition&&t.push(n.condition),n.offsetExpr&&t.push(n.offsetExpr),n.args)for(let s of n.args)t.push(s);if(n.indices)for(let s of n.indices)t.push(s)}}return!1}_visitNode(e){let t=e;for(;t;)switch(t.type){case"SeqNode":for(let n=0;n<t.stmts.length-1;n++)this._visitNode(t.stmts[n]);t=t.stmts[t.stmts.length-1];continue;case"AllocateNode":t=t.body;continue;case"LetStmtNode":{let n=this._vectorMode;if(n&&n.simd&&this._dependsOnVecVar(t.value)){let o=t.variable.name+"_vlet";this._ensureLocal(o,"v128"),n.vecLets||(n.vecLets=new Set),n.vecLets.add(t.variable.name),this._emitVecExpr(t.value),this._emit("local.set $"+o),t=t.body;continue}let s=ir(t.value)||t.variable.dtype||this._defaultDtype;this._locals.set(t.variable.name,ys(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,n=[e.body];for(;n.length>0;){let i=n.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&&n.push(i.body),i.stmts)for(let a of i.stmts)n.push(a);i.thenBody&&n.push(i.thenBody),i.elseBody&&n.push(i.elseBody),i.loopBody&&n.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 n=t.pop();if(n){if(n.type==="ForNode"&&n.kind===F.PARALLEL){this._hasParallel=!0,this._parallelExtent=n.extent&&n.extent.type==="IntImmNode"?n.extent.value:0;return}if(n.body&&t.push(n.body),n.stmts)for(let s of n.stmts)t.push(s);n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody)}}}_visitFor(e){let t=e.loopVar.name,n=this._constExtent(e.extent);if(e.kind===F.PARALLEL){if(n!==null&&this._parallelExtent&&n!==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===F.VECTORIZED&&this.target.supportsSimd()&&n!==null){this._visitVectorizedFor(e);return}if((e.kind===F.UNROLLED||e.kind===F.VECTORIZED)&&n!==null&&n<=32&&!this._isZeroFillBody(e.body)){for(let o=0;o<n;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,ys(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 n=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==="*"&&(n(u.a)||n(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(n(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(rr(t))return e;throw new Error(`wasm accumulator: integer ${e} reduction not supported (dtype ${t})`)}return"add"}_visitLIRAccumulator(e){let t=e.localName,n=e.dtype;this._ensureLocal(t,ys(n));let s=this._constExtent(e.extent),o=e.op||"+",i=o==="+"&&s!==null&&e.loopKind===F.VECTORIZED&&this.target.supportsSimd()?yb(n):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(n)),this._emit(this._numPrefix(n)+"."+this._accumInstr(o,n)),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,n,s){let o=e.localName,i=e.dtype,a=e.loopVar.name,u=rr(i),p=u?"f32.add":"i32.add",c=_s(i,"add"),h=Math.floor(s/n)*n,f=h,d=o+"_vec";this._ensureLocal(d,"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 $"+d);let m=this._wasmAcc;this._wasmAcc={local:o,bufName:e.flushStore.buffer.name},this._vectorMode={dtype:i,lanes:n,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 "+h+")"),this._emit("i32.ge_s"),this._emit("br_if $vbreak_"+a),this._emit("(local.get $"+d+")"),this._emitVecExpr(e.body),this._emit(c),this._emit("local.set $"+d),this._emit("(local.get $"+a+")"),this._emit("(i32.const "+n+")"),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<n;g++)this._emit("(local.get $"+d+")"),this._emit(t.extractLane+" "+g),this._emit(p);this._emit("local.set $"+o),f<s&&(this._emit("(i32.const "+f+")"),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 n=this._bufferOffsets.get(e.name)||0,s=cc(e.dtype||"f32");if(!t||t.type==="IntImmNode"&&t.value===0){this._emit(`(i32.const ${n})`);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+")"),n>0&&(this._emit(`(i32.const ${n})`),this._emit("i32.add"));return}this._emitExpr(t),this._emit(`(i32.const ${s})`),this._emit("i32.mul"),n>0&&(this._emit(`(i32.const ${n})`),this._emit("i32.add"))}_emitForLoop(e,t,n){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(n),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 n=this._vectorMode;n&&n.laneVars&&this._dependsOnVecVar(t.binding)&&n.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 n=this._bufferOffsets.get(e.name)||0,s=cc(e.dtype);if(t.length===0){this._emit(`(i32.const ${n})`);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+")"),n>0&&(this._emit(`(i32.const ${n})`),this._emit("i32.add"));return}this._emitFlatIndex(e,t),this._emit(`(i32.const ${s})`),this._emit("i32.mul"),n>0&&(this._emit(`(i32.const ${n})`),this._emit("i32.add"))}_emitFlatIndex(e,t){if(t.length===1){this._emitExpr(t[0]);return}let n=!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")),n||this._emit("i32.add"),n=!1}}_emitDynamicStride(e,t){let n=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})`)}n>0&&this._emit("i32.mul"),n++}n===0&&this._emit("(i32.const 1)")}_resolveShapeParam(e,t){return ni(this._primFunc,e,t,n=>n.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||ir(e.thenBody),n=this._numPrefix(t);this._emitExpr(e.condition),rr(this._wasmExprDtype(e.condition))&&(this._emit("(f32.const 0)"),this._emit("f32.ne")),this._emit("(if (result "+n+")"),this._indent++,this._emit("(then"),this._indent++,this._emitCoercedTo(e.thenBody,n),this._indent--,this._emit(")"),this._emit("(else"),this._indent++,this._emitCoercedTo(e.elseBody,n),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":rr(e)?"f32":"i32"}_exprPrefix(e){return this._numPrefix(e&&e._dtype||ir(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),n=e.b?this._exprPrefix(e.b):t,s=this._joinPrefix(t,n);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),n=this._exprPrefix(e.b),s=this._joinPrefix(t,n),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")&&!rr(e.dtype)){this._emitIntMinMax(e);return}if(e.externName==="abs"&&!rr(e.dtype)){this._emitIntAbs(e);return}let t=new Set(["sqrt","abs","ceil","floor","min","max","rsqrt"]),n=e.externName==="abs"||t.has(e.externName)?this._numPrefix(e.dtype)==="i32"?"f32":this._numPrefix(e.dtype):"f32";if(e.externName==="rsqrt"){this._emit("("+n+".const 1)");for(let s of e.args)this._emitCoercedTo(s,n);this._emit(n+".sqrt"),this._emit(n+".div");return}for(let s of e.args)this._emitCoercedTo(s,n);switch(e.externName){case"sqrt":this._emit(n+".sqrt");break;case"abs":this._emit(n+".abs");break;case"ceil":this._emit(n+".ceil");break;case"floor":this._emit(n+".floor");break;case"min":this._emit(n+".min");break;case"max":this._emit(n+".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,n="_iabs"+t;this._intMinMaxEmitDepth=t+1,this._emitExpr(e.args[0]),this._emit("local.set $"+n),this._intMinMaxEmitDepth=t,this._emit("(i32.const 0)"),this._emit("(local.get $"+n+")"),this._emit("i32.sub"),this._emit("(local.get $"+n+")"),this._emit("(local.get $"+n+")"),this._emit("(i32.const 0)"),this._emit("i32.lt_s"),this._emit("select")}_emitIntMinMax(e){let t=this._intMinMaxEmitDepth||0,n="_immm_a"+t,s="_immm_b"+t;this._intMinMaxEmitDepth=t+1,this._emitExpr(e.args[0]),this._emit("local.set $"+n),this._emitExpr(e.args[1]),this._emit("local.set $"+s),this._intMinMaxEmitDepth=t,this._emit("(local.get $"+n+")"),this._emit("(local.get $"+s+")"),this._emit("(local.get $"+n+")"),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 n=this._intDivEmitDepth||0,s="_idiv_a"+n,o="_idiv_b"+n;this._intDivEmitDepth=n+1,this._emitCoercedTo(e.a,t),this._emit("local.set $"+s),this._emitCoercedTo(e.b,t),this._emit("local.set $"+o),this._intDivEmitDepth=n;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 n=this._intDivEmitDepth||0,s="_idiv_a"+n,o="_idiv_b"+n;this._intDivEmitDepth=n+1,this._emitCoercedTo(e.a,t),this._emit("local.set $"+s),this._emitCoercedTo(e.b,t),this._emit("local.set $"+o),this._intDivEmitDepth=n,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((n,s,o)=>{if(!n||typeof n!="object")return;let i=s,a=o;if(n.type==="CallExternNode"&&(n.externName==="min"||n.externName==="max")&&!rr(n.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(n.type==="CallExternNode"&&n.externName==="abs"&&!rr(n.dtype))this._ensureLocal("_iabs"+s,"i32"),s+1>this._intMinMaxDepth&&(this._intMinMaxDepth=s+1),i=s+1;else if(this._isIntDivNode(n)){let u=ys(this._joinPrefix(this._exprPrefix(n.a),this._exprPrefix(n.b)));this._ensureLocal("_idiv_a"+o,u),this._ensureLocal("_idiv_b"+o,u),a=o+1}if(n.body&&t(n.body,i,a),n.value&&typeof n.value=="object"&&t(n.value,i,a),n.a&&t(n.a,i,a),n.b&&t(n.b,i,a),n.expr&&t(n.expr,i,a),n.condition&&t(n.condition,i,a),n.offsetExpr&&t(n.offsetExpr,i,a),n.thenBody&&t(n.thenBody,i,a),n.elseBody&&t(n.elseBody,i,a),n.initBody&&t(n.initBody,i,a),n.stmts)for(let u of n.stmts)t(u,i,a);if(n.args)for(let u of n.args)t(u,i,a);if(n.indices)for(let u of n.indices)t(u,i,a);if(n.bindings)for(let u of n.bindings)t(u.expr,i,a);if(n.iterVars)for(let u of n.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 ld(e)}_collectBindings(e,t){let n=[e];for(;n.length>0;){let s=n.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&&n.push(s.body),s.stmts)for(let o of s.stmts)n.push(o);s.thenBody&&n.push(s.thenBody),s.elseBody&&n.push(s.elseBody),s.initBody&&n.push(s.initBody),s.loopBody&&n.push(s.loopBody)}}}_computeLaneVars(e){let t=new Set([e.loopVar.name]),n=[];this._collectBindings(e.body,n);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 n)t.has(i.name)||s(i.expr).some(a=>t.has(a))&&(t.add(i.name),o=!0)}return t}_vecLoadsContiguous(e,t){let n=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==="*"&&(n(u.a)||n(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(n(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 n=[e];for(;n.length>0;){let s=n.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&&n.push(s.body),s.stmts)for(let i of s.stmts)n.push(i);s.thenBody&&n.push(s.thenBody),s.elseBody&&n.push(s.elseBody),s.loopBody&&n.push(s.loopBody)}return!0}_visitVectorizedFor(e){let t=e.loopVar.name,n=this._constExtent(e.extent),s=this._inferBodyDtype(e.body)||this._defaultDtype,o=this.target.vectorWidth,i=yb(s);if(!i||n<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(n/o)*o,p=n-u,c=cc(s),d=this._countBufAccesses(e.body)>=2?"_vaddr_"+t:null;if(u>0&&(this._vectorMode={dtype:s,lanes:o,loopVar:t,simd:i,addrLocal:d,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<n;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,n=[e];for(;n.length>0;){let s=n.pop();if(!(!s||typeof s!="object")){if((s.type==="BufferLoadNode"||s.type==="BufferStoreNode"||s.type==="LIRFlatLoadNode"||s.type==="LIRFlatStoreNode")&&t++,s.body&&n.push(s.body),s.value&&typeof s.value=="object"&&n.push(s.value),s.a&&n.push(s.a),s.b&&n.push(s.b),s.stmts)for(let o of s.stmts)n.push(o);if(s.args)for(let o of s.args)n.push(o);if(s.indices)for(let o of s.indices)n.push(o);s.thenBody&&n.push(s.thenBody),s.elseBody&&n.push(s.elseBody),s.expr&&n.push(s.expr),s.condition&&n.push(s.condition),s.offsetExpr&&n.push(s.offsetExpr)}}return t}_inferBodyDtype(e){let t=[e];for(;t.length>0;){let n=t.pop();if(n){if(n.type==="BufferStoreNode"&&n.buffer)return n.buffer.dtype;if(n.type==="LIRFlatStoreNode")return n.dtype||this._defaultDtype;if(n.type==="BufferLoadNode"&&n.buffer)return n.buffer.dtype;if(n.type==="LIRFlatLoadNode")return n.dtype||this._defaultDtype;if(n.body&&t.push(n.body),n.stmts)for(let s of n.stmts)t.push(s);n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody),n.value&&typeof n.value=="object"&&t.push(n.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 n=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||n&&n.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,n=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":rr(n)?(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+")"),rr(n)&&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),rr(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,n=t.dtype;if(!e.b){if(e.op==="-"){let u=_s(n,"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(_s(n,"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=_s(n,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=_s(n,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,n=t.dtype,s=_s(n,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,n=t.dtype,s=_s(n,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=_s(n,"sqrt");if(o){this._emit("(f32.const 1)"),this._emit(t.simd.splat),this._emitVecExpr(e.args[0]),this._emit(o),this._emit(_s(n,"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,n=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<n;p++){let c="_vl_"+a+"_"+p;this._ensureLocal(c,ys(t.dtype)),u.push(c)}this._emitVecExpr(e.args[0]),this._emit("local.set $"+a);for(let p=0;p<n;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 h=this._vectorMode;this._vectorMode=t,this._emitVecExpr(e.args[1]),this._vectorMode=h,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[n-1]+")"),this._emit(i);for(let p=n-2;p>=0;p--)this._emit("(local.get $"+u[p]+")"),this._emit(o+" "+p)}_prescanVecLocalsAll(e){let t=[e];for(;t.length>0;){let n=t.pop();if(n){if(n.type==="ForNode"&&n.kind===F.VECTORIZED&&(this._prescanVecLocals(n.body),this._countBufAccesses(n.body)>=2&&this._ensureLocal("_vaddr_"+n.loopVar.name,"i32"),this._prescanVecLets(n)),n.type==="LIRAccumulatorNode"&&n.loopKind===F.VECTORIZED&&(this._ensureLocal(n.localName+"_vec","v128"),this._prescanVecLocals(n.body)),n.body&&t.push(n.body),n.stmts)for(let s of n.stmts)t.push(s);n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody),n.initBody&&t.push(n.initBody),n.loopBody&&t.push(n.loopBody)}}}_prescanVecLets(e){let t=this._computeLaneVars(e),n=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&&n(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 n=t.pop();if(n){if(n.type==="CallExternNode"&&n.externName&&!_s(this._defaultDtype,n.externName)&&n.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,ys(this._defaultDtype));n.args.length>1&&this._ensureLocal("_vtmp2_"+o,"v128"),this._vecTmpCounter++}if(n.body&&t.push(n.body),n.stmts)for(let s of n.stmts)t.push(s);if(n.value&&typeof n.value=="object"&&n.value.type&&t.push(n.value),n.a&&typeof n.a=="object"&&t.push(n.a),n.b&&typeof n.b=="object"&&t.push(n.b),n.args)for(let s of n.args)typeof s=="object"&&t.push(s);n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody)}}}_layoutBuffers(e){let t=0,n=16;this._dynamicBuffers=new Set;let s=l(i=>{t=Math.ceil(t/n)*n,this._bufferOffsets.set(i.name,t);let a=i.numel();!i.shape.some(p=>typeof p!="number"||p<0)&&a>0?t+=a*cc(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 n=[e];for(;n.length>0;){let s=n.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 en(s))n.push(o)}}}_scanMathImports(e){let t=[e];for(;t.length>0;){let n=t.pop();if(!(!n||typeof n!="object")){if(n.type==="CallExternNode"&&n.externName){let s=n.externName;if(s!=="sqrt"&&s!=="min"&&s!=="max"){let o=this._mathImportSig(s,n.args.length);this._imports.set(s,o)}}if(n.body&&t.push(n.body),n.stmts)for(let s of n.stmts)t.push(s);if(n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody),n.initBody&&t.push(n.initBody),n.value&&typeof n.value=="object"&&n.value.type&&t.push(n.value),n.a&&typeof n.a=="object"&&t.push(n.a),n.b&&typeof n.b=="object"&&t.push(n.b),n.expr&&typeof n.expr=="object"&&t.push(n.expr),n.args)for(let s of n.args)typeof s=="object"&&t.push(s);if(n.indices)for(let s of n.indices)typeof s=="object"&&t.push(s);n.condition&&typeof n.condition=="object"&&t.push(n.condition)}}}_prescanLocals(e){this._waccCounter=0;let t=[e];for(;t.length>0;){let n=t.pop();if(n){if(n.type==="ForNode"){this._ensureLocal(n.loopVar.name,"i32");let s=this._accPatternDtype(n);s&&this._ensureLocal("_wacc_"+ ++this._waccCounter,ys(s)),n.kind===F.VECTORIZED&&this.target.supportsSimd()&&this._prescanVecLocals(n.body)}if(n.type==="BlockNode")for(let s of n.iterVars)s.iterVar&&this._ensureLocal(s.iterVar.name,"i32");if(n.type==="LetStmtNode"&&n.variable){let s=ir(n.value)||n.variable.dtype||this._defaultDtype;this._ensureLocal(n.variable.name,ys(s))}if(n.body&&t.push(n.body),n.stmts)for(let s of n.stmts)t.push(s);n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody),n.initBody&&t.push(n.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 ir(e)}_fixLetStmtLocals(e){let t=[e];for(;t.length>0;){let n=t.pop();if(n){if(n.type==="LetStmtNode"&&n.variable&&n.value){let s=ir(n.value);s&&this._locals.set(n.variable.name,ys(s))}if(n.body&&t.push(n.body),n.stmts)for(let s of n.stmts)t.push(s);n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody),n.initBody&&t.push(n.initBody)}}}_accPatternDtype(e){let t=e.body;if(!t||t.type!=="BlockNode")return null;let n=t.body;if(!n||n.type!=="BufferStoreNode")return null;let s=n.value;return!s||s.type!=="MathOpNode"||s.op!=="+"?null:s.a&&s.a.type==="BufferLoadNode"&&s.a.buffer.name===n.buffer.name||s.b&&s.b.type==="BufferLoadNode"&&s.b.buffer.name===n.buffer.name?n.buffer.dtype:null}_inferDtype(e){return e&&e._dtype||ir(e)}_detectWasmAcc(e){let t=e.body;if(!t||t.type!=="BlockNode")return null;let n=t.body;if(!n||n.type!=="BufferStoreNode")return null;let s=n,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 h of t.iterVars)if(h.iterVar&&h.iterVar.name===c.name&&h.binding)return h.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 n=[];for(let s=0;s<t.length;s++)n.push(this._exprKey(t[s]));return e.name+":"+n.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 ku,Qb=M(()=>{"use strict";ku=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 n=t.length-1;for(;n>0;){let s=n-1>>1;if(this._compare(t[n],t[s])>=0)break;[t[n],t[s]]=[t[s],t[n]],n=s}}pop(){let e=this._items;if(e.length===0)return null;let t=e[0],n=e.pop();if(e.length>0){e[0]=n;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 oi(r){let e=String(r);return/[.e]/.test(e)?e:`${e}.0`}function Bv(r){let e=`(1.0 / (1.0 + ${oi(vc)} * abs(${r})))`,t=_u.slice().reverse().reduce((n,s)=>`(${oi(s)} + ${e} * ${n})`,"0.0");return`((select(-1.0, 1.0, ${r} >= 0.0)) * (1.0 - ${e} * ${t} * exp(-${r} * ${r})))`}function wd(r){let e=`(${r} - 1.0)`,t=Hi.map((s,o)=>o===0?oi(s):`${oi(s)} / (${e} + ${oi(o)})`).join(" + "),n=`(${e} + ${oi(7+.5)})`;return`(${oi(.5*Math.log(2*Math.PI))} + (${e} + 0.5) * log(${n}) - ${n} + log(${t}))`}function QL(r){let e=`(log(${Td} / abs(sin(${Td} * ${r}))) - ${wd(`(1.0 - ${r})`)})`;return`(select(${wd(r)}, ${e}, ${r} < 0.5))`}function XL(r){let e=`(${Td} / (sin(${Td} * ${r}) * exp(${wd(`(1.0 - ${r})`)})))`;return`(select(exp(${wd(r)}), ${e}, ${r} < 0.5))`}function Rv(r,e){for(let t of e2)r[t]&&e(r[t]);if(r.stmts)for(let t of r.stmts)e(t);if(r.indices)for(let t of r.indices)e(t);if(r.args)for(let t of r.args)e(t);r.initLoad&&e(r.initLoad),r.flushStore&&e(r.flushStore)}var JL,ZL,Td,e2,t2,Xb,xd,Mv=M(()=>{"use strict";De();Vn();rd();Qb();Gr();kc();Ji();JL=new Set(["!","&&","||"]),ZL=new Set(["<",">","<=",">=","==","!="]);l(oi,"_wgslFloat");l(Bv,"_wgslErf");l(wd,"_wgslLanczosCore");Td=oi(Math.PI);l(QL,"_wgslLgamma");l(XL,"_wgslGamma");e2=["body","loopBody","condBody","initBody","thenBody","elseBody","value","a","b","condition","expr","offsetExpr"];l(Rv,"walkFullChildren");t2={"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"},Xb=class{static{l(this,"WebGPUKernel")}constructor(e,t,n,s,o,i,a){this.name=e,this.source=t,this.workgroupSize=n,this.dispatchSize=s,this.sharedMemBytes=o,this.params=i,this.bindings=a}},xd=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[x,k]of e.metadata.threadBindings){this._threadBindings.set(x,k);for(let A of k)A.isDynamic||this._applyBindingDim(x,A.extent)}this._sharedBuffers=e.metadata.sharedBuffers}else this._scanBindings(e.body);this._scanStoreTargets(e.body),this._analyzeSharing(e);let n=[],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(x=>x.numel()>0);this._packedMode=p,this._packedOffsets=null;let c=new Map;{let x=0;for(let[,k]of e.bufferMap)c.set(k.name,x++)}{let x=null;for(let[,k]of e.bufferMap)if(this._storeBuffers.has(k.name)){x=k.dtype;break}this._defaultDtype=x||(u.length?u[0].dtype:"f32")}let h={f32:"f32",i32:"i32",u32:"u32",f16:"f16"};if(p){this._packedOffsets=new Map;let x=l(N=>Math.ceil(N/4)*4,"align4"),k=new Map;for(let[,N]of e.bufferMap){n.push(N.name);let v=this._storeBuffers.has(N.name),O=Mn(N.dtype),R=(v?"w:":"r:")+O,B=k.get(R);B||(B={isWrite:v,wt:O,dtype:h[O]||N.dtype,bufs:[],off:0},k.set(R,B));let E=N.numel(),z=(v?"_pw_":"_pr_")+O;this._packedOffsets.set(N.name,{storage:z,offset:B.off}),B.bufs.push({name:N.name,offset:B.off,size:E,dtype:N.dtype,argIndex:c.get(N.name)}),B.off+=x(E)}let A=[...k.values()].sort((N,v)=>N.isWrite===v.isWrite?N.wt.localeCompare(v.wt):N.isWrite?1:-1);for(let N of A){let v=(N.isWrite?"_pw_":"_pr_")+N.wt,O=N.isWrite?"storage, read_write":"storage, read";s.push({index:o,name:v,mode:N.isWrite?"read_write":"read",packed:N.bufs,packedSize:N.off,dtype:N.dtype}),this._emit(`@group(0) @binding(${o}) var<${O}> ${v}: array<${N.wt}>;`),o++}}else for(let[,x]of e.bufferMap){n.push(x.name);let k=this._storeBuffers.has(x.name)?"read_write":"read",A=k==="read_write"?"storage, read_write":"storage, read";s.push({index:o,name:x.name,mode:k,dtype:x.dtype,argIndex:c.get(x.name)}),this._emit(`@group(0) @binding(${o}) var<${A}> ${x.name}: array<${Mn(x.dtype)}>;`),o++}if(e.shapeParams.length>0){this._emit(""),this._emit("struct ShapeParams {"),this._indent++;for(let x of e.shapeParams)n.push(x.name),this._emit(`${x.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 x of this._sharedBuffers){let k=x.numel();this._emit(`var<workgroup> ${x.name}: array<${Mn(x.dtype)}, ${k>0?k:1}>;`)}for(let x of this._promotedBufferDecls)this._emit(`var<workgroup> ${x.name}: array<${Mn(x.dtype)}, ${x.size}>;`);for(let x of this._wgPoolDecls)this._emit(`var<workgroup> ${x.pool}: array<${Mn(x.dtype)}, ${x.size>0?x.size:1}>;`);(this._sharedBuffers.length>0||this._promotedBufferDecls.length>0||this._wgPoolDecls.length>0)&&this._emit("");let f=[],d=!this._serializeThreads&&this._hasBindingPrefix("threadIdx"),m=!this._serializeThreads&&this._hasBindingPrefix("blockIdx");d&&f.push("@builtin(local_invocation_id) _lid: vec3u"),m&&f.push("@builtin(workgroup_id) _wid: vec3u"),!d&&!m&&f.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 x=0;this._workgroupSize[1]>this._workgroupSize[x]&&(x=1),this._workgroupSize[2]>this._workgroupSize[x]&&(x=2),this._workgroupSize[x]=Math.max(1,this._workgroupSize[x]>>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}(${f.join(", ")}) {`),this._indent++,!this._serializeThreads){let x=new Set;for(let[k,A]of this._threadBindings)if(t2[k]){for(let v of A)if(!x.has(v.varName)){let O=this._wgslBuiltinAccess(k);this._emit(`let ${v.varName}: i32 = i32(${O});`),x.add(v.varName)}}}this._assignLocalSlots(e),this._emitMissingLocalDecls(e),this._visitNode(e.body),this._indent--,this._emit("}");let w=this.target,T=[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 Xb(e.name,this._lines.join(`
7
+ `),T,S,this._sharedBuffers.reduce((x,k)=>x+Math.max(k.sizeInBytes(),0),0),n,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 n=e.substring(0,t),s=e.substring(t+1);return n==="threadIdx"?`_lid.${s}`:n==="blockIdx"?`_wid.${s}`:`_gid.${s}`}_scanStoreTargets(e){let t=[e];for(;t.length>0;){let n=t.pop();if(n){(n.type==="BufferStoreNode"||n.type==="LIRFlatStoreNode")&&this._storeBuffers.add(n.buffer.name),n.type==="LIRAccumulatorNode"&&n.flushStore&&this._storeBuffers.add(n.flushStore.buffer.name);for(let s of en(n))t.push(s)}}}_scanBindings(e){yu(e,t=>{if(t.type==="ForNode"&&t.kind===F.THREAD_BINDING&&t.threadTag){let n=t.extent.type==="IntImmNode"?t.extent.value:0,s=t.extent.type!=="IntImmNode",o={varName:t.loopVar.name,extent:n,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,n)}t.type==="AllocateNode"&&t.scope==="shared"&&this._sharedBuffers.push(t.buffer)})}_applyBindingDim(e,t){let n=Wi(e);n&&(n.space==="thread"?this._workgroupSize[n.axis]=Math.max(this._workgroupSize[n.axis],t):this._dispatchSize[n.axis]=Math.max(this._dispatchSize[n.axis],t))}_getMaxBindingExtent(e){return pd(this._threadBindings,e)}_hasRecurrence(e){let t=!1;return yu(e.body,n=>{if(n.type==="SyncThreadsNode")return t=!0,!1}),t}_analyzeSharing(e){let t=new Set;for(let[,h]of this._threadBindings)for(let f of h)f.extent>0&&t.add(f.extent);let n=this._threadBindings.size>0?this._findCrossThreadBuffers(e):new Set,s=this._threadBindings.size>0?this._findCrossExtentBuffers(e):new Set;this._crossThread=n,this._crossExtent=s;let o=this._hasRecurrence(e);if(!o&&t.size<=1&&n.size===0&&s.size===0)return;let i=this.target.sharedMemoryBytes||16384,a=this._sharedBuffers.reduce((h,f)=>h+Math.max(f.sizeInBytes(),0),0),u=new Set;for(let[,h]of e.bufferMap)u.add(h.name);let p=this._collectPromotionCandidates(e,u);p.sort((h,f)=>(n.has(f.name)?1:0)-(n.has(h.name)?1:0));let c=this._dispatchSize[0]*this._dispatchSize[1]*this._dispatchSize[2];if(o){let h=this.target.maxThreadsPerBlock||256,f=this._workgroupSize[0]*this._workgroupSize[1]*this._workgroupSize[2],d=this._packWorkgroupPool(e,p),m=a+d.bytes<=i;if(c===1&&f<=h&&m){this._needsBarriers=!0,this._wgPoolOffsets=d.offsets,this._wgPoolDecls=d.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=(n.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 h of p){let f=h.size*(bs(h.dtype)||4);a+f<=i&&(this._promotedBuffers.add(h.name),this._promotedBufferDecls.push(h),a+=f)}}_collectPromotionCandidates(e,t){let n=[];yu(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&&n.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(n.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&&(n.push({name:a,dtype:u.dtype,size:c}),i.add(a))}return n}_findRecurrenceBody(e){let t=null;return yu(e,n=>{if(!t&&n.type==="ForNode"&&n.kind===F.RECURRENCE)return t=n.body,!1}),t}_namesTouchedOutside(e,t,n){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&&n.has(i.buffer.name)&&s.add(i.buffer.name),i.type==="LIRAccumulatorNode"&&(i.flushStore&&i.flushStore.buffer&&n.has(i.flushStore.buffer.name)&&s.add(i.flushStore.buffer.name),i.initLoad&&i.initLoad.buffer&&n.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 n=new Set(t.map(f=>f.name)),s=this._findRecurrenceBody(e.body),{minPos:o,maxPos:i}=s?this._livenessWalk(s,n):{minPos:new Map,maxPos:new Map},a=s?this._namesTouchedOutside(e.body,s,n):n,u=new Map;for(let f of t){u.has(f.dtype)||u.set(f.dtype,[]);let d=a.has(f.name)||!o.has(f.name),m=d?0:o.get(f.name),g=d?Number.MAX_SAFE_INTEGER:i.get(f.name);u.get(f.dtype).push({name:f.name,size:f.size,first:m,last:g})}let p=new Map,c=[],h=0;for(let[f,d]of u){d.sort((_,b)=>_.first-b.first||b.size-_.size);let m=[],g=`_wg_${Mn(f)}`,y=0;for(let _ of d){let b=m.filter(T=>T.first<=_.last&&_.first<=T.last).map(T=>[T.offset,T.offset+T.size]).sort((T,S)=>T[0]-S[0]),w=0;for(let[T,S]of b){if(w+_.size<=T)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:f,size:y}),h+=y*(bs(f)||4)}return{offsets:p,decls:c,bytes:h}}_findCrossThreadBuffers(e){let t=new Set;for(let[,a]of e.bufferMap)t.add(a.name);let n=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!==F.THREAD_BINDING||a.type==="WhileNode"||a.type==="LIRAccumulatorNode")&&a.loopVar&&(p=[...u,a.loopVar.name]),a.type==="LIRBindingsNode"&&a.bindings)for(let h of a.bindings)!p.includes(h.name)&&o(h.expr,p)&&(p=[...p,h.name]);if(a.type==="BlockNode"&&a.iterVars)for(let h of a.iterVars)h.iterVar&&h.binding&&!p.includes(h.iterVar.name)&&o(h.binding,p)&&(p=[...p,h.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)&&n.add(a.buffer.name),a.type==="LIRFlatLoadNode"&&a.buffer&&!t.has(a.buffer.name)&&a.offsetExpr&&s([a.offsetExpr],p)&&n.add(a.buffer.name),Rv(a,h=>i(h,p))},"walk");return i(e.body,[]),n}_findCrossExtentBuffers(e){let t=new Set;for(let[,c]of e.bufferMap)t.add(c.name);let n=new Map,s=new Map,o=new Set,i=l(c=>c&&(c.type==="FloatImmNode"||c.type==="IntImmNode"),"isConst"),a=l((c,h,f)=>{if(t.has(h))return;let d=c.get(h);d||(d=new Set,c.set(h,d)),d.add(f)},"record"),u=l((c,h)=>{if(!c)return;let f=h;if(c.type==="ForNode"&&c.kind===F.THREAD_BINDING){let d=c.extent&&c.extent.type==="IntImmNode"?c.extent.value:0;d>0&&(f=h*d)}c.type==="BufferStoreNode"&&c.buffer&&(a(n,c.buffer.name,f),i(c.value)||o.add(c.buffer.name)),c.type==="LIRFlatStoreNode"&&c.buffer&&(a(n,c.buffer.name,f),i(c.value)||o.add(c.buffer.name)),c.type==="LIRAccumulatorNode"&&c.flushStore&&c.flushStore.buffer&&(a(n,c.flushStore.buffer.name,f),o.add(c.flushStore.buffer.name)),c.type==="BufferLoadNode"&&c.buffer&&a(s,c.buffer.name,f),c.type==="LIRFlatLoadNode"&&c.buffer&&a(s,c.buffer.name,f),Rv(c,d=>u(d,f))},"walk");u(e.body,1);let p=new Set;for(let[c,h]of s){if(!o.has(c))continue;let f=n.get(c);if(f){for(let d of h)if(!f.has(d)){p.add(c);break}}}return p}_collectVarNames(e,t){let n=[e];for(;n.length>0;){let s=n.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]&&n.push(s[o]);if(s.indices)for(let o of s.indices)n.push(o);if(s.args)for(let o of s.args)n.push(o)}}}_emit(e){this._lines.push(" ".repeat(this._indent)+e)}_visitNode(e){ud(this,e)}_emitSync(){this._needsBarriers&&(this._emit("storageBarrier();"),this._emit("workgroupBarrier();"))}_visitForNode(e){if(e.kind===F.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,n=this._exprToWGSL(e.extent);this._emit(`for (var ${t}: i32 = 0; ${t} < ${n}; ${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}: ${Mn(e.dtype)};`):this._emit(`var ${e.name}: array<${Mn(e.dtype)}, ${e.size}>;`)}_collectLocalBuffers(e){let t=new Set;for(let[,a]of e.bufferMap)t.add(a.name);let n=new Set;for(let a of this._sharedBuffers)n.add(a.name);let s=new Map,o=l(a=>!t.has(a)&&!n.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 yu(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:n,maxPos:s}=this._computeBufferLiveness(e,t),o=[...t.keys()].sort((d,m)=>n.get(d)-n.get(m)||s.get(d)-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,h=this._crossExtent||new Set,f=l((d,m)=>!this._serializeThreads&&this._threadBindings.size>0&&!c.has(d)&&!h.has(d)&&m.numel()>1&&m.numel()<=p,"scalarEligible");for(let d of o){let m=t.get(d),g=m.numel()>0?m.numel():this._estimateBufferSize(m),y=Math.max(g,1),_=n.get(d),b=s.get(d);if(f(d,m)){let x=`_s${u++}`;this._slotDecls.push({name:x,dtype:m.dtype,size:1,scalar:!0}),this._localSlots.set(d,x),this._scalarSlotNames.add(x);continue}let w=i.get(m.dtype);w||(w=new ku((x,k)=>x.freeAt-k.freeAt),i.set(m.dtype,w));let T=null,S=w.peek();if(S&&S.freeAt<_&&(T=w.pop()),T)y>T.decl.size&&(T.decl.size=y);else{let x={name:`_lt${a++}`,dtype:m.dtype,size:y};this._slotDecls.push(x),T={decl:x}}T.freeAt=b,this._localSlots.set(d,T.decl.name),w.push(T)}}_computeBufferLiveness(e,t){return this._livenessWalk(e.body,t)}_livenessWalk(e,t){let n=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,h=l(d=>{t.has(d)&&(n.has(d)||n.set(d,a),s.set(d,a),u.length>0&&p.add(d))},"touch"),f=l(d=>{if(!d)return;a++;let m=o.has(d.type),g=!1;m&&(u.length===0&&(p=new Set,c=a,g=!0),u.push(d)),(d.type==="BufferLoadNode"||d.type==="BufferStoreNode"||d.type==="LIRFlatLoadNode"||d.type==="LIRFlatStoreNode")&&d.buffer&&h(d.buffer.name),d.type==="LIRAccumulatorNode"&&(d.flushStore&&d.flushStore.buffer&&h(d.flushStore.buffer.name),d.initLoad&&d.initLoad.buffer&&h(d.initLoad.buffer.name));for(let y of i){let _=d[y];if(_)if(Array.isArray(_))for(let b of _)f(b);else typeof _=="object"&&f(_)}if(m&&(u.pop(),g)){let y=a;for(let _ of p)n.set(_,Math.min(n.get(_),c)),s.set(_,Math.max(s.get(_),y));p=null}},"walk");return f(e),{minPos:n,maxPos:s}}_scanAllocateNodes(e,t){let n=[e];for(;n.length>0;){let s=n.pop();if(s){s.type==="AllocateNode"&&t.add(s.buffer.name);for(let o of en(s))n.push(o)}}}_scanBufferRefs(e,t){let n=[e];for(;n.length>0;){let s=n.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 en(s))n.push(o)}}}_estimateBufferSize(e){return cd(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(),n=t>0?t:this._estimateBufferSize(e.buffer);n>0&&this._emit(`var ${e.buffer.name}: array<${Mn(e.buffer.dtype)}, ${n}>;`)}}_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,n=Mn(t),s=this._numericExpr(e.value,t);this._emit(`var ${e.variable.name}: ${n} = ${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),n=this._packedBufAccess(e.buffer.name,t),s=this._numericExpr(e.value,e.buffer.dtype);this._emit(`${n} = ${s};`)}_visitLIRFlatStore(e){let t=this._exprToWGSL(e.offsetExpr),n=this._packedBufAccess(e.buffer.name,t),s=this._numericExpr(e.value,e.buffer.dtype);this._emit(`${n} = ${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,n=Mn(e.dtype||this._defaultDtype);this._emit(`var ${t}: ${n} = ${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"&&(JL.has(e.op)||ZL.has(e.op))):!1}_numericExpr(e,t){if(this._isBoolExpr(e)){let n=t?Mn(t):"i32";return n==="f32"?`select(0.0, 1.0, ${this._exprToWGSL(e)})`:`select(${n}(0), ${n}(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),n=this._numericExpr(e.b);return e.op==="//"?`(${t} / ${n})`:e.op==="%"?`(${t} % ${n})`:`(${t} ${e.op} ${n})`}case"CompareNode":return`(${this._numericExpr(e.a)} ${Ka(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`${Mn(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,n=new Array(t);for(let i=0;i<t;i++)n[i]=this._exprToWGSL(e.args[i]);let s=n.join(", ");if(e.externName==="fmod")return`(${n[0]} % ${n[1]})`;if(e.externName==="erf")return Bv(n[0]);if(e.externName==="erfc")return`(1.0 - ${Bv(n[0])})`;if(e.externName==="lgamma")return QL(n[0]);if(e.externName==="gamma")return XL(n[0]);if(e.externName==="log10")return`(log(${n[0]}) * ${1/Math.LN10})`;let o=qS(e.externName);if(o===e.externName&&!US(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 n=this._wgPoolOffsets.get(e);return n.offset===0?`${n.pool}[${t}]`:`${n.pool}[${n.offset}u + u32(${t})]`}if(this._packedMode&&this._packedOffsets&&this._packedOffsets.has(e)){let n=this._packedOffsets.get(e);return n.offset===0?`${n.storage}[${t}]`:`${n.storage}[${n.offset}u + u32(${t})]`}if(this._localSlots&&this._localSlots.has(e)){let n=this._localSlots.get(e);return this._scalarSlotNames.has(n)?n:`${n}[${t}]`}return`${e}[${t}]`}_flatIndex(e,t){return du(e,t,n=>this._exprToWGSL(n),(n,s)=>this._computeDynamicStride(n,s),!1)}_computeDynamicStride(e,t){return ri(e,t+1,(n,s)=>this._resolveShapeParam(n,s))}_resolveShapeParam(e,t){return ni(this._primFunc,e,t,n=>`i32(_shapes.${n.name})`,"WebGPU","wgsl")}}});function Iv(r){let{source:e,name:t,metadata:n}=r,s=[];for(let i=0;i<n.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 Ev=M(()=>{"use strict";l(Iv,"buildSnippet")});function re(r){let e=[];do{let t=r&127;r>>>=7,r&&(t|=128),e.push(t)}while(r);return e}function p2(r){let e=[],t=!0;for(;t;){let n=r&127;r>>=7,r===0&&!(n&64)||r===-1&&n&64?t=!1:n|=128,e.push(n)}return e}function f2(r){r=BigInt(r);let e=[],t=!0;for(;t;){let n=Number(r&0x7fn);r>>=7n,r===0n&&!(n&64)||r===-1n&&n&64?t=!1:n|=128,e.push(n)}return e}function Sd(r){let e=new TextEncoder().encode(r);return[...re(e.length),...e]}function h2(r){let e=new ArrayBuffer(4);return new Float32Array(e)[0]=r,[...new Uint8Array(e)]}function d2(r){let e=new ArrayBuffer(8);return new Float64Array(e)[0]=r,[...new Uint8Array(e)]}function Ws(r,e){for(let t=0;t<e.length;t++)r.push(e[t]);return r}function vu(r,e){let t=[r];return Ws(t,re(e.length)),Ws(t,e),t}function Zi(r){let e=[];Ws(e,re(r.length));for(let t of r)Ws(e,t);return e}function m2(r){let e=[],t=0;for(;t<r.length;){let n=r[t];if(n<=" "){t++;continue}if(n===";"&&r[t+1]===";"){t=r.indexOf(`
9
+ `,t),t<0&&(t=r.length);continue}if(n==="("&&r[t+1]===";"){t=r.indexOf(";)",t+2),t=t<0?r.length:t+2;continue}if(n==="("||n===")"){e.push(n),t++;continue}if(n==='"'){let o=t+1;for(;o<r.length&&r[o]!=='"';)o++;e.push(r.substring(t,o+1)),t=o+1;continue}let s=t;for(;s<r.length&&r[s]>" "&&r[s]!=="("&&r[s]!==")";)s++;e.push(r.substring(t,s)),t=s}return e}function g2(r){let e=0,t=l(()=>r[e++],"eat"),n=l(()=>r[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=[],h=[],f=[],d=-1,m=-1;for(;n()!==")";){if(n()!=="("){e++;continue}e++;let y=t();if(y==="memory"){for(;n()==="(";)g();n()!==")"&&(i=parseInt(t())||1),n()!==")"&&(a=parseInt(t())||256),s(")")}else if(y==="import"){let _=t().replace(/"/g,""),b=t().replace(/"/g,"");s("("),s("func"),n().startsWith("$")&&e++;let w=[],T=[];for(;n()==="(";){e++;let S=t();if(S==="param")for(;n()!==")";){let x=t();x==="f32"?w.push(Rc):x==="f64"?w.push(Bc):x==="i32"?w.push(Oc):x==="v128"&&w.push(Mc)}else if(S==="result")for(;n()!==")";){let x=t();x==="f32"?T.push(Rc):x==="f64"?T.push(Bc):x==="i32"?T.push(Oc):x==="v128"&&T.push(Mc)}s(")")}s(")"),s(")"),o.push({module:_,name:b,params:w,results:T})}else if(y==="func"){for(;n()==="(";){let b=e;e++;let w=t();if(w==="export")u=t().replace(/"/g,""),s(")");else if(w==="param"){let T="";for(;n()!==")";){let S=t();if(S.startsWith("$")){T=S.replace("$","");continue}let x=null;S==="i32"?x=Oc:S==="i64"?x=nw:S==="f32"?x=Rc:S==="f64"?x=Bc:S==="v128"&&(x=Mc),x!==null&&(p.push(x),c.push(T),T="")}s(")")}else if(w==="result"){for(;n()!==")";)t();s(")")}else if(w==="local"){for(;n()!==")";){let T=t();T.startsWith("$")?f.push(T.replace("$","")):T==="i32"?h.push(Oc):T==="i64"?h.push(nw):T==="f32"?h.push(Rc):T==="f64"?h.push(Bc):T==="v128"&&h.push(Mc)}s(")")}else{e=b;break}}d=e;let _=1;for(;_>0;){let b=t();b==="("?_++:b===")"&&_--}m=e-1}else g()}function g(){if(n()!=="("){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:h,funcLocalNames:f,bodyTokens:r.slice(d,m)}}function y2(r,e,t){let n=[],s=0,o=l(()=>r[s],"peek"),i=l(()=>r[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 h(y,_){let b=y.indexOf(_);return b>=0?y.substring(b+_.length):""}l(h,"extractName");function f(){for(;s<r.length&&o()!==")";){let y=o();if(y==="("){s++;let _=i();if(_==="i32.const")n.push(65),n.push(...p2(parseInt(i()))),m(")");else if(_==="i64.const")n.push(66),n.push(...f2(i())),m(")");else if(_==="f32.const")n.push(67),n.push(...h2(parseFloat(i()))),m(")");else if(_==="f64.const")n.push(68),n.push(...d2(parseFloat(i()))),m(")");else if(_==="local.get")n.push(32),n.push(...re(p(i()))),m(")");else if(_==="local.set")n.push(33),n.push(...re(p(i()))),m(")");else if(_==="block"){let b=i().replace("$","");a.push(b),n.push(2,ew),f(),n.push(11),a.pop(),m(")")}else if(_==="loop"){let b=i().replace("$","");a.push(b),n.push(3,ew),f(),n.push(11),a.pop(),m(")")}else if(_==="if"){let b=ew;if(o()==="("){let w=s;if(s++,o()==="result"){i();let T=i();b=T==="f32"?Rc:T==="f64"?Bc:T==="v128"?Mc:T==="i64"?nw:Oc,m(")")}else s=w}for(n.push(4,b),a.push("_if");s<r.length&&o()!==")"&&o()==="(";){let w=s;s++;let T=i();if(T==="then")f(),m(")");else if(T==="else")n.push(5),f(),m(")");else{s=w;break}}n.push(11),a.pop(),m(")")}else s-=2,g();continue}if(s++,y==="br_if")n.push(13),n.push(...re(d(c())));else if(y==="br")n.push(12),n.push(...re(d(c())));else if(y.startsWith("call")){n.push(16);let _=h(y,"math_")||(o()&&o().startsWith("$")?h(i(),"math_"):""),b=t.get(_);if(b===void 0)throw new Error(`WAT encoder: call to unresolved import '${_}'`);n.push(...re(b))}else y==="local.get"?(n.push(32),n.push(...re(p(c())))):y==="local.set"?(n.push(33),n.push(...re(p(c())))):y.endsWith(".extract_lane")||y.endsWith(".replace_lane")?(n.push(...tw.get(y)),n.push(parseInt(i(),10))):tw.has(y)&&n.push(...tw.get(y))}}l(f,"emitBlock");function d(y){for(let _=a.length-1;_>=0;_--)if(a[_]===y)return a.length-1-_;return 0}l(d,"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"),f(),n}function Nu(r){let e=m2(r),t=g2(e),n=new Map,s=t.funcParams.length;for(let x=0;x<t.funcParamNames.length;x++)t.funcParamNames[x]&&n.set(t.funcParamNames[x],x);for(let x=0;x<t.funcLocalNames.length;x++)n.set(t.funcLocalNames[x],s+x);let o=new Map;for(let x=0;x<t.imports.length;x++)o.set(t.imports[x].name,x);let i=[],a=new Map;function u(x,k){let A=x.join(",")+">"+k.join(",");if(a.has(A))return a.get(A);let N=i.length;return i.push({params:x,results:k}),a.set(A,N),N}l(u,"getType");for(let x of t.imports)x.typeIdx=u(x.params,x.results);let p=u(t.funcParams,[]),c=vu(s2,Zi(i.map(x=>[c2,...re(x.params.length),...x.params,...re(x.results.length),...x.results]))),h=[];t.imports.length>0&&(h=vu(o2,Zi(t.imports.map(x=>[...Sd(x.module),...Sd(x.name),0,...re(x.typeIdx)]))));let f=vu(i2,Zi([[...re(p)]])),d=vu(a2,Zi([[1,...re(t.memMin),...re(t.memMax)]])),m=t.imports.length,g=vu(u2,Zi([[...Sd("memory"),2,...re(0)],[...Sd(t.funcExportName),0,...re(m)]])),y=[];if(t.funcLocals.length>0){let x=[],k=t.funcLocals[0],A=1;for(let N=1;N<t.funcLocals.length;N++)t.funcLocals[N]===k?A++:(x.push([...re(A),k]),k=t.funcLocals[N],A=1);x.push([...re(A),k]),Ws(y,Zi(x))}else y.push(0);let _=y2(t.bodyTokens,n,o),b=[];Ws(b,y),Ws(b,_),b.push(11);let w=[];Ws(w,re(b.length)),Ws(w,b);let T=vu(l2,Zi([w])),S=[];for(let x of[n2,r2,c,h,f,d,g,T])Ws(S,x);return new Uint8Array(S)}var n2,r2,s2,o2,i2,a2,u2,l2,Oc,nw,Bc,Rc,Mc,c2,ew,tw,kd=M(()=>{"use strict";n2=[0,97,115,109],r2=[1,0,0,0],s2=1,o2=2,i2=3,a2=5,u2=7,l2=10,Oc=127,nw=126,Bc=124,Rc=125,Mc=123,c2=96,ew=64;l(re,"uleb");l(p2,"sleb");l(f2,"slebBig");l(Sd,"encStr");l(h2,"encF32");l(d2,"encF64");l(Ws,"pushAll");l(vu,"section");l(Zi,"vec");tw=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(m2,"tokenize");l(g2,"parseModule");l(y2,"encodeBody");l(Nu,"encodeWat")});function Lv(r){let{source:e,name:t,metadata:n}=r,{bufferOffsets:s,imports:o}=n,i=[...s.entries()],a=Nu(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,h]of i)u.push(`// ${c}: new Float32Array(memory.buffer, ${h}, 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 Cv=M(()=>{"use strict";kd();l(Lv,"buildSnippet")});function Dv(r){let{source:e,name:t,metadata:n}=r,{bindings:s,dispatchSize:o}=n,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"?_2(i,a):a.packed?b2(i,a):a.mode==="read_write"?w2(i,a):T2(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(""),x2(i,s),i.push(""),i.push("gpuBuffers.forEach(b => b.destroy());"),i.push("device.destroy();"),i.push("})();"),i.join(`
11
+ `)}function _2(r,e){r.push(`// binding ${e.index}: uniform (_shapes)`),r.push("{"),r.push(" const shapeData = new Uint32Array([/* shape values */]);"),r.push(" const size = Math.max(Math.ceil(shapeData.byteLength / 16) * 16, 16);"),r.push(" const buf = device.createBuffer({ size, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });"),r.push(" device.queue.writeBuffer(buf, 0, shapeData);"),r.push(" entries.push({ binding: "+e.index+", resource: { buffer: buf } });"),r.push(" gpuBuffers.push(buf);"),r.push("}")}function b2(r,e){let t=e.mode==="read_write",n=t?"GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST":"GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST";if(r.push(`// binding ${e.index}: packed ${e.name} (${e.packed.length} buffers, ${e.packedSize} elems)`),r.push("{"),r.push(` const buf = device.createBuffer({ size: ${e.packedSize*4}, usage: ${n}, mappedAtCreation: true });`),r.push(" const mapped = new Float32Array(buf.getMappedRange());"),t)r.push(" mapped.fill(0);");else for(let s of e.packed)r.push(` // mapped.set(${s.name}_data, ${s.offset}); // size=${s.size}`);r.push(" buf.unmap();"),r.push(" entries.push({ binding: "+e.index+", resource: { buffer: buf } });"),r.push(" gpuBuffers.push(buf);"),r.push("}")}function w2(r,e){r.push(`// binding ${e.index}: output ${e.name}`),r.push("{"),r.push(" const size = N * 4; // N = output element count"),r.push(" const buf = device.createBuffer({ size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, mappedAtCreation: true });"),r.push(" new Float32Array(buf.getMappedRange()).fill(0);"),r.push(" buf.unmap();"),r.push(" entries.push({ binding: "+e.index+", resource: { buffer: buf } });"),r.push(" gpuBuffers.push(buf);"),r.push("}")}function T2(r,e){r.push(`// binding ${e.index}: input ${e.name}`),r.push("{"),r.push(` const data = ${e.name}_data; // Float32Array`),r.push(" const buf = device.createBuffer({ size: Math.max(data.byteLength, 4), usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST, mappedAtCreation: true });"),r.push(" new Float32Array(buf.getMappedRange()).set(data);"),r.push(" buf.unmap();"),r.push(" entries.push({ binding: "+e.index+", resource: { buffer: buf } });"),r.push(" gpuBuffers.push(buf);"),r.push("}")}function x2(r,e){if(e.filter(n=>n.mode==="read_write").length===0){r.push("device.queue.submit([encoder.finish()]);");return}for(let n=0;n<e.length;n++){let s=e[n];if(s.mode==="read_write")if(s.packed)for(let o of s.packed)r.push("{"),r.push(` const rb = device.createBuffer({ size: ${o.size*4}, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST });`),r.push(` encoder.copyBufferToBuffer(gpuBuffers[${n}], ${o.offset*4}, rb, 0, ${o.size*4});`),r.push(" device.queue.submit([encoder.finish()]);"),r.push(" await rb.mapAsync(GPUMapMode.READ);"),r.push(` console.log("${o.name}:", new Float32Array(rb.getMappedRange()));`),r.push(" rb.unmap(); rb.destroy();"),r.push("}");else r.push("{"),r.push(` const src = gpuBuffers[${n}];`),r.push(" const rb = device.createBuffer({ size: src.size, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST });"),r.push(" encoder.copyBufferToBuffer(src, 0, rb, 0, src.size);"),r.push(" device.queue.submit([encoder.finish()]);"),r.push(" await rb.mapAsync(GPUMapMode.READ);"),r.push(` console.log("${s.name}:", new Float32Array(rb.getMappedRange()));`),r.push(" rb.unmap(); rb.destroy();"),r.push("}")}}var Pv=M(()=>{"use strict";l(Dv,"buildSnippet");l(_2,"emitShapeBinding");l(b2,"emitPackedBinding");l(w2,"emitOutputBinding");l(T2,"emitInputBinding");l(x2,"emitReadback")});function $v(r){let{source:e,name:t,metadata:n}=r,{blockDim:s,gridDim:o,sharedMemBytes:i,params:a}=n,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 Fv=M(()=>{"use strict";l($v,"buildSnippet")});function vd(r,e){zv.set(r,e),e.runtimeKind&&e.snippet&&Vv.set(e.runtimeKind,e.snippet)}function Gv(r){return zv.get(r)||null}function jv(r){return Vv.get(r)||null}var zv,Vv,qv=M(()=>{"use strict";Ks();Tv();Av();Ov();Mv();Ev();Cv();Pv();Fv();zv=new Map,Vv=new Map;l(vd,"registerCodegen");l(Gv,"getCodegenEntry");l(jv,"getSnippetBuilder");vd(Ot.CPU,{runtimeKind:"js",snippet:Iv,compile(r,e){return{source:new yd(e).generate(r),metadata:{kind:"js",paramCount:r.params.length}}}});vd(Ot.WASM,{runtimeKind:"wasm",snippet:Lv,compile(r,e){let t=new bd(e).generate(r),n={kind:"wasm",memoryPages:t.memoryPages,bufferOffsets:t.bufferOffsets,imports:t.imports,params:t.params,bufferMap:r.bufferMap};return t.parallel&&(n.parallel=t.parallel),{source:t.wat,metadata:n}}});vd(Ot.WEBGPU,{runtimeKind:"webgpu",snippet:Dv,compile(r,e){let t=new xd(e).generate(r);return{source:t.source,metadata:{kind:"webgpu",workgroupSize:t.workgroupSize,dispatchSize:t.dispatchSize,sharedMemBytes:t.sharedMemBytes,params:t.params,bindings:t.bindings}}}});vd(Ot.CUDA,{runtimeKind:"cuda",snippet:$v,compile(r,e,t){if(t&&t.matmulBackend==="cublas"&&r.cublasInfo)return{source:"",metadata:{kind:"cuda",cublas:r.cublasInfo,outputIndices:[r.cublasInfo.cIdx]}};let n=new _d(e).generate(r);return{source:n.source,metadata:{kind:"cuda",blockDim:n.blockDim,gridDim:n.gridDim,sharedMemBytes:n.sharedMemBytes,params:n.params,outputIndices:n.outputIndices,scratch:n.scratch}}}})});function Uv(r){let e=[],t=[r.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 n=null;for(let m of e)if(m.name.includes("matmul"))m.reads.length>=2&&m.writes.length>=1&&(n=m);else return null;if(!n)return null;let s=n.reads[0].buffer,o=n.reads[1].buffer,i=n.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 r.bufferMap)c.push(m.name);let h=c.indexOf(s.name),f=c.indexOf(o.name),d=c.indexOf(i.name);return h<0||f<0||d<0?null:{M:a,N:u,K:p,aIdx:h,bIdx:f,cIdx:d}}var Ic,Hs,Ec=M(()=>{"use strict";qv();l(Uv,"detectPureMatmul");Ic=class{static{l(this,"CompiledKernel")}constructor(e,t,n,s={}){this.name=e,this.source=t,this.target=n,this.metadata=s}snippet(){let e=jv(this.metadata.kind);if(!e)throw new Error(`No snippet for kind: ${this.metadata.kind}`);return e(this)}},Hs=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)||Gv(this.target.kind);if(!t)throw new Error(`Unsupported target kind: ${this.target.kind}`);let{source:n,metadata:s}=t.compile(e,this.target,this);return new Ic(e.name,n,this.target,s)}compileAll(e){return e.map(t=>this.compile(t))}}});function Wv(r,e){Kv.set(r,e)}function Hv(r){return Kv.get(r)||null}var Kv,rw=M(()=>{"use strict";Kv=new Map;l(Wv,"registerMeasurer");l(Hv,"getMeasurer")});var Yv={};nt(Yv,{runWasmParallel:()=>S2});async function S2(r,e,t,n,s){let{exports:o,memory:i,bufferOffsets:a}=r,u=o[e],p=[...a.values()],c=Math.min(p.length,t.length);for(let f=0;f<c;f++){let d=t[f];ArrayBuffer.isView(d)&&new d.constructor(i.buffer,p[f],d.length).set(d)}let h=p.slice(0,c);if(n)for(let f of n)h.push(f);h.push(0,s.extent),u(...h);for(let f=0;f<c;f++){let d=t[f];ArrayBuffer.isView(d)&&d.set(new d.constructor(i.buffer,p[f],d.length))}}var Jv=M(()=>{"use strict";l(S2,"runWasmParallel")});var Nd={};nt(Nd,{deviceClipGradNorm:()=>A2,measureCudaKernel:()=>v2,runCudaKernel:()=>k2,runCudaPlan:()=>N2});function k2(){throw new Error("CUDA runtime is not available in the browser")}function v2(){throw new Error("CUDA runtime is not available in the browser")}function N2(){throw new Error("CUDA runtime is not available in the browser")}function A2(){throw new Error("CUDA runtime is not available in the browser")}var Ad=M(()=>{"use strict";l(k2,"runCudaKernel");l(v2,"measureCudaKernel");l(N2,"runCudaPlan");l(A2,"deviceClipGradNorm")});function ii(){return Zv}function O2(r){Zv=r}var Qi,ks,Od,sw,Ys,Au,Zv,ar=M(()=>{"use strict";Nn();Qi=class{static{l(this,"Device")}type;index;constructor(e,t=0){this.type=e,this.index=t}dispatchKey(){return ja(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}`}},ks=new Qi("cpu"),Od=new Qi("gpu"),sw=new Qi("wasm"),Ys=new Qi("webgpu"),Au=new Qi("meta"),Zv=ks;l(ii,"getDefaultDevice");l(O2,"setDefaultDevice")});function Xv(r){let e=Qv.get(r)??null;return e||(e=Nt.findOp(r),e&&Qv.set(r,e)),e||null}var Qv,eN=M(()=>{"use strict";$i();Qv=new Map;l(Xv,"getHandle")});function vs(r,e){return r<0?e+r:r}function xn(r){let e=r.length,t=new Array(e),n=1;for(let s=e-1;s>=0;s--)t[s]=n,n*=r[s];return t}function cn(r){let e=1;for(let t=0;t<r.length;t++)e*=r[t];return e}function ow(r,e){let t=r.length;if(t===0)return!0;let n=1;for(let s=t-1;s>=0;s--){let o=r[s];if(o===0)return!0;if(o!==1&&e[s]!==n)return!1;n*=o}return!0}function Ou(r,e){let t=r.length,n=e.length,s=Math.max(t,n),o=new Array(s);for(let i=0;i<s;i++){let a=i<t?r[t-1-i]:1,u=i<n?e[n-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 Bd(r,e){let t=r.length,n=e.length;if(t===1&&n===1)return[];let s=t===1?[1,r[0]]:[...r],o=n===1?[e[0],1]:[...e],i=s.length,a=o.length,u=Ou(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 n===1&&c.add(p.length-1),t===1&&c.add(p.length-2),p.filter((h,f)=>!c.has(f))}function tN(r,e,t){let n=cn(r),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]=n/o|0,i[s]*o!==n))return null}else if(cn(i)!==n)return null;return ow(r,e)?{sizes:i,strides:xn(i),needsCopy:!1}:{sizes:i,strides:xn(i),needsCopy:!0}}var jr=M(()=>{"use strict";l(vs,"normalizeAxis");l(xn,"computeStrides");l(cn,"computeNumel");l(ow,"isContiguous");l(Ou,"broadcastShapes");l(Bd,"matmulOutputShape");l(tN,"inferReshape")});function nN(r,e){if(r.length<4)return!1;let t=r.length,n=1,s=t===4?B2:R2;if(!s)return!1;for(let o=t-1;o>=0;o--){let i=s[o];if(i===void 0||r[i]===0||e[i]!==n)return!1;n*=r[i]}return!0}var B2,R2,iw=M(()=>{"use strict";B2=Object.freeze([0,2,3,1]),R2=Object.freeze([0,2,3,4,1]);l(nN,"isChannelsLast")});var Bt,Oo=M(()=>{"use strict";Nn();jr();iw();iw();Bt=class{static{l(this,"TensorImpl")}_storage;_storageOffset;_sizes;_strides;_dtype;_device;_numel;_keySet;_autogradMeta;_version;_contiguousCache;constructor(e,t,n,s,o,i){this._storage=e,this._storageOffset=t,this._sizes=Object.freeze([...n]),this._strides=Object.freeze(s?[...s]:xn(n)),this._dtype=o,this._device=i,this._numel=cn(n),this._keySet=Pr.fromKey(ja(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=ow(this._sizes,this._strides)),this._contiguousCache):e==="channels_last"?nN(this._sizes,this._strides):!1}setSizesAndStrides(e,t){this._sizes=Object.freeze([...e]),this._strides=Object.freeze(t?[...t]:xn(e)),this._numel=cn(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=Pr.fromKey(ja(this._device.type));if(this._autogradMeta&&this._autogradMeta.requiresGrad){let t=ja(this._device.type);e=e.add(RS(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 Bu,aw=M(()=>{"use strict";Bu=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 sN(r,e,t,n,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]=md(o,r[n+p*t[s]]);else for(let p=0;p<i;p++)u[p]=r[n+p*t[s]];return u}let a=new Array(i);for(let u=0;u<i;u++)a[u]=sN(r,e,t,n+u*t[s],s+1,o);return a}function M2(){if(!uw)throw new Error("Autograd engine not initialized. Import autograd/engine.js first.");return uw}function oN(r){uw=r}var rN,We,uw,jn=M(()=>{"use strict";Oo();aw();xu();rN=new Set(["f16","bf16","i64"]),We=class r{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,n=this._impl.storageOffset;return t&&n===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 Bt(this._impl.storage,this._impl.storageOffset,this._impl.sizes(),this._impl.strides(),this._impl.dtype,this._impl.device);return new r(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 rN.has(this._impl.dtype)?md(this._impl.dtype,t):t}toArray(){let e=this.shape,t=this.strides,n=this._impl.storage.data,s=this._impl.storageOffset,o=rN.has(this._impl.dtype)?this._impl.dtype:null;return e.length===0?o?md(o,n[s]):n[s]:sN(n,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 n=this._impl.sizes(),s=this._impl.strides(),o=[],i=[];for(let p=0;p<n.length;p++)p!==e&&(o.push(n[p]),i.push(s[p]));let a=this._impl.storageOffset+t*s[e],u=new Bt(this._impl.storage,a,o,i,this._impl.dtype,this._impl.device);return new r(u)}_ensureAutogradMeta(){if(!this._impl.autogradMeta){let e=new Bu;e.versionAtCreation=this._impl.version,this._impl.setAutogradMeta(e)}return this._impl.autogradMeta}backward(e){let{backward:t}=M2();t(this,e)}};l(sN,"_toNestedArray");uw=null;l(M2,"require_autograd_engine");l(oN,"setAutogradEngine")});function Dt(r){return E2[r]||Float32Array}function Rd(r,e){if(r===e)return r;let t=Ru.get(r)??0,n=Ru.get(e)??0;return t>=n?r:e}function Xe(r,e){let t=uN.get(r);t||(t=new Set,uN.set(r,t)),t.add(e)}function Xi(r){return I2(r)}var W,I2,E2,iN,aN,Ru,uN,ts=M(()=>{"use strict";Oe();W=zn,I2=Gs,E2=Object.freeze({[W.F16]:Uint16Array,[W.BF16]:Uint16Array,[W.F32]:Float32Array,[W.F64]:Float64Array,[W.I8]:Int8Array,[W.I16]:Int16Array,[W.I32]:Int32Array,[W.I64]:BigInt64Array,[W.UI8]:Uint8Array,[W.BOOL]:Uint8Array,[W.INDEX]:Int32Array});l(Dt,"typedArrayCtor");iN=[W.F16,W.BF16,W.F32,W.F64],aN=[W.UI8,W.I8,W.I16,W.I32,W.I64],Ru=new Map;for(let r=0;r<iN.length;r++)Ru.set(iN[r],100+r);for(let r=0;r<aN.length;r++)Ru.set(aN[r],r);Ru.set(W.BOOL,-1);Ru.set(W.INDEX,50);l(Rd,"resultDtype");uN=new Map;l(Xe,"_addCastPair");Xe(W.BOOL,W.I8);Xe(W.BOOL,W.I16);Xe(W.BOOL,W.I32);Xe(W.BOOL,W.I64);Xe(W.BOOL,W.F16);Xe(W.BOOL,W.F32);Xe(W.BOOL,W.F64);Xe(W.UI8,W.I16);Xe(W.UI8,W.I32);Xe(W.UI8,W.I64);Xe(W.UI8,W.F16);Xe(W.UI8,W.F32);Xe(W.UI8,W.F64);Xe(W.I8,W.I16);Xe(W.I8,W.I32);Xe(W.I8,W.I64);Xe(W.I8,W.F16);Xe(W.I8,W.F32);Xe(W.I8,W.F64);Xe(W.I16,W.I32);Xe(W.I16,W.I64);Xe(W.I16,W.F32);Xe(W.I16,W.F64);Xe(W.I32,W.I64);Xe(W.I32,W.F64);Xe(W.F16,W.F32);Xe(W.F16,W.F64);Xe(W.BF16,W.F32);Xe(W.BF16,W.F64);Xe(W.BOOL,W.BF16);Xe(W.UI8,W.BF16);Xe(W.I8,W.BF16);Xe(W.F32,W.F64);l(Xi,"dtypeSize")});function lw(r){return lN.get(r)||lN.get("cpu")}var Md,Id,lN,Lc,cN=M(()=>{"use strict";ts();ar();Md=class{static{l(this,"CPUAllocator")}allocate(e,t){let n=Dt(t);return new n(e)}free(e){}},Id=class{static{l(this,"MetaAllocator")}allocate(){return null}free(e){}},lN=new Map([["cpu",new Md],["wasm",new Md],["meta",new Id],["lazy",new Id]]);l(lw,"getAllocator");Lc=class r{static{l(this,"StorageImpl")}static#e=null;_data;_nbytes;_device;_allocator;_refCount;static setHostReadHook(e){r.#e=e}constructor(e,t,n,s){this._data=e,this._nbytes=t,this._device=n,this._allocator=s,this._refCount=1}static allocate(e,t,n){let s=lw(n.type),o=Xi(t),i=Math.max(Math.ceil(e/o),1),a=s.allocate(i,t);return new r(a,e,n,s)}static fromData(e,t){let n=e?e.byteLength:0,s=lw(t.type);return new r(e,n,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 r.#e&&this._data&&r.#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 n=Xi(t),s=Math.max(Math.ceil(e/n),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=lw(this._device.type),t=null;if(this._data){r.#e&&r.#e(this._data);let n=this._data.constructor;t=new n(this._data.length),t.set(this._data)}return new r(t,this._nbytes,this._device,e)}}});var En,ea=M(()=>{"use strict";cN();En=class r{static{l(this,"Storage")}_impl;constructor(e){this._impl=e}static allocate(e,t,n){return new r(Lc.allocate(e,t,n))}static fromData(e,t){return new r(Lc.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 r(this._impl.clone())}resize(e,t){this._impl.resize(e,t)}}});function Pt(r,e){let t=e?.dtype??W.F32,n=e?.device??ii(),s=e?.requiresGrad??!1;if(ArrayBuffer.isView(r)&&"length"in r)return L2(r,e?.shape,t,n,s);if(Array.isArray(r)){let{flat:o,shape:i}=D2(r),a=e?.shape??i;return C2(o,a,t,n,s)}if(typeof r=="number")return pN(r,t,n,s);throw new Error("Unsupported data type for tensor()")}function ta(r,e,t,n){let s=n?.device??ii(),o=n?.requiresGrad??!1,i=xn(e),a=En.fromData(r,s),u=n?.offset??0,p=new Bt(a,u,e,i,t,s),c=new We(p);return o&&c.requiresGrad_(!0),c}function Dc(r,e){let t=e?.dtype??W.F32,n=e?.device??ii();return pN(r,t,n,e?.requiresGrad??!1)}function pN(r,e,t,n){let s=Dt(e),o=new s(1),i=o;i[0]=cw.has(e)?dd(e,r):r;let a=En.fromData(o,t),u=new Bt(a,0,[],[],e,t),p=new We(u);return n&&p.requiresGrad_(!0),p}function L2(r,e,t,n,s){let o=Dt(t),i=new o(r.length),a=i;if(cw.has(t))for(let d=0;d<r.length;d++)a[d]=dd(t,r[d]);else for(let d=0;d<r.length;d++)a[d]=r[d];let u=e??[r.length],p=xn(u),c=En.fromData(i,n),h=new Bt(c,0,u,p,t,n),f=new We(h);return s&&f.requiresGrad_(!0),f}function C2(r,e,t,n,s){let o=Dt(t),i=new o(r.length),a=i;if(cw.has(t))for(let f=0;f<r.length;f++)a[f]=dd(t,r[f]);else for(let f=0;f<r.length;f++)a[f]=r[f];let u=xn(e),p=En.fromData(i,n),c=new Bt(p,0,e,u,t,n),h=new We(c);return s&&h.requiresGrad_(!0),h}function D2(r){let e=[],t=r;for(;Array.isArray(t);)e.push(t.length),t=t[0];let n=[];return fN(r,n,e,0),{flat:n,shape:e}}function fN(r,e,t,n){if(n===t.length-1){for(let s=0;s<r.length;s++)e.push(r[s]);return}for(let s=0;s<r.length;s++)fN(r[s],e,t,n+1)}var cw,qr=M(()=>{"use strict";jn();Oo();ea();ts();ar();jr();xu();l(Pt,"tensor");l(ta,"fromBuffer");l(Dc,"scalar");cw=new Set(["f16","bf16","i64"]);l(pN,"_fromScalar");l(L2,"_fromTypedArray");l(C2,"_fromFlatArray");l(D2,"_flattenNested");l(fN,"_recurFlatten")});var Ft={};nt(Ft,{_dispatch:()=>ot,abs:()=>Pc,add:()=>ae,argmax:()=>Ud,argmin:()=>kN,argsort:()=>LN,batch_norm:()=>fw,broadcast_in_dim:()=>ON,cat:()=>sa,ceil:()=>wN,chunk:()=>BN,clamp:()=>Vd,clone:()=>AN,contiguous:()=>ci,conv2d:()=>hw,cos:()=>mN,cumsum:()=>IN,div:()=>$t,dot:()=>NN,embedding:()=>z2,eq:()=>$c,erf:()=>gN,erfc:()=>yN,exp:()=>Kr,expand:()=>Lu,fill:()=>F2,flip:()=>MN,floor:()=>bN,gamma:()=>Cd,gather:()=>jd,ge:()=>zd,gelu:()=>Dd,gt:()=>Iu,index_select:()=>li,layer_norm:()=>pw,le:()=>Fd,lgamma:()=>_N,log:()=>Zs,log_softmax:()=>Hd,lt:()=>xN,matmul:()=>lt,max:()=>Qs,maximum:()=>ai,mean:()=>lr,min:()=>SN,minimum:()=>Ld,mul:()=>ee,narrow:()=>Un,ne:()=>TN,neg:()=>Ur,one_hot:()=>Gd,pad:()=>na,permute:()=>Eu,pool2d:()=>Yd,pow:()=>Js,prod:()=>vN,relu:()=>Mu,remainder:()=>$2,repeat:()=>Du,reshape:()=>Cn,roll:()=>RN,rsqrt:()=>hN,scatter:()=>qd,scatter_add:()=>ra,select:()=>Ut,sigmoid:()=>ur,sign:()=>$d,silu:()=>Pd,sin:()=>dN,slice:()=>Kd,softmax:()=>Ro,sort:()=>EN,split:()=>Bo,sqrt:()=>ui,squeeze:()=>Cu,stack:()=>Wr,sub:()=>Pe,sum:()=>ht,tanh:()=>Ns,tile:()=>Wd,topk:()=>CN,transpose:()=>gt,unsqueeze:()=>Sr,where:()=>qn});function P2(r){return typeof r=="object"&&r!==null&&"_impl"in r}function Ln(r,e){return P2(r)?r:Dc(r,{dtype:e.dtype,device:e.device})}function ot(r,...e){let t=Xv(r);if(!t)return Nt.callOp(r,...e);let n=pb(e,t.schema);return Nt.dispatch(t,n,...e)}function se(r,...e){return ot(r,...e)}function Ed(r,...e){return ot(r,...e)}function ae(r,e){return se("add",r,Ln(e,r))}function Pe(r,e){return se("sub",r,Ln(e,r))}function ee(r,e){return se("mul",r,Ln(e,r))}function $t(r,e){return se("div",r,Ln(e,r))}function Ur(r){return se("neg",r)}function Js(r,e){return se("pow",r,Ln(e,r))}function $2(r,e){return se("rem",r,Ln(e,r))}function ai(r,e){return se("maximum",r,Ln(e,r))}function Ld(r,e){return se("minimum",r,Ln(e,r))}function Kr(r){return se("exp",r)}function Zs(r){return se("log",r)}function ui(r){return se("sqrt",r)}function hN(r){return se("rsqrt",r)}function Pc(r){return se("abs",r)}function dN(r){return se("sin",r)}function mN(r){return se("cos",r)}function Ns(r){return se("tanh",r)}function gN(r){return se("erf",r)}function yN(r){return se("erfc",r)}function _N(r){return se("lgamma",r)}function Cd(r){return se("gamma",r)}function ur(r){return se("sigmoid",r)}function Mu(r){return se("relu",r)}function Dd(r){return se("gelu",r)}function Pd(r){return se("silu",r)}function $d(r){return se("sign",r)}function bN(r){return se("floor",r)}function wN(r){return se("ceil",r)}function $c(r,e){return se("eq",r,Ln(e,r))}function TN(r,e){return se("ne",r,Ln(e,r))}function xN(r,e){return se("lt",r,Ln(e,r))}function Fd(r,e){return se("le",r,Ln(e,r))}function Iu(r,e){return se("gt",r,Ln(e,r))}function zd(r,e){return se("ge",r,Ln(e,r))}function qn(r,e,t){return se("where",r,e,t)}function Vd(r,e,t){return se("clamp",r,Ln(e,r),Ln(t,r))}function na(r,e,t,n=0){return se("pad",r,Ln(n,r),e,t)}function Gd(r,e){return se("one_hot",r,e)}function li(r,e,t){return se("index_select",r,t,e)}function jd(r,e,t){return se("gather",r,t,e)}function ra(r,e,t,n){return se("scatter_add",r,t,n,e)}function qd(r,e,t,n){return se("scatter",r,e,t,n)}function ht(r,e,t){return se("sum",r,e,t)}function lr(r,e,t){return se("mean",r,e,t)}function Qs(r,e,t){return se("max",r,e,t)}function SN(r,e,t){return se("min",r,e,t)}function Ud(r,e,t){return se("argmax",r,e,t)}function kN(r,e,t){return se("argmin",r,e,t)}function vN(r,e,t){return se("prod",r,e,t)}function lt(r,e){let t=dw();if(t){let n=t(r,e);if(n!==null)return n}return se("matmul",r,e)}function NN(r,e){return se("dot",r,e)}function sa(r,e){return se("cat",r,e)}function Wr(r,e){return se("stack",r,e)}function AN(r){return se("clone",r)}function F2(r,e){return se("fill",r,e)}function Cn(r,e){return se("reshape",r,e)}function gt(r,e,t){return se("transpose",r,e,t)}function Eu(r,e){return se("permute",r,e)}function ON(r,e,t){return se("broadcast_in_dim",r,e,t)}function Lu(r,e){return se("expand",r,e)}function Kd(r,e,t,n=null,s=1){return se("slice",r,e,t,n,s)}function Sr(r,e){return se("unsqueeze",r,e)}function Cu(r,e=null){return se("squeeze",r,e)}function Un(r,e,t,n){return se("narrow",r,e,t,n)}function Ut(r,e,t){return se("select",r,e,t)}function ci(r){return se("contiguous",r)}function Du(r,e){return se("repeat",r,e)}function Wd(r,e){return se("tile",r,e)}function Bo(r,e,t=0){if(typeof e!="number")return Ed("split",r,e,t);let n=r.shape.length,s=t<0?n+t:t,o=r.shape[s],i=[];for(let a=0;a<o;a+=e)i.push(Math.min(e,o-a));return Ed("split",r,i,t)}function BN(r,e,t=0){return Ed("chunk",r,e,t)}function RN(r,e,t=0){return se("roll",r,e,t)}function MN(r,e){return se("flip",r,Array.isArray(e)?e:[e])}function IN(r,e=0){return se("cumsum",r,e)}function EN(r,e=-1,t=!1){return se("sort",r,e,t)}function LN(r,e=-1,t=!1){return se("argsort",r,e,t)}function CN(r,e,t=-1,n=!0){return Ed("topk",r,e,t,n)}function Ro(r,e){return se("softmax",r,e)}function Hd(r,e){return se("log_softmax",r,e)}function pw(r,e,t,n,s){return se("layer_norm",r,e,t,n,s)}function fw(r,e,t,n,s,o,i){return se("batch_norm",r,e,t,n,s,o,i)}function hw(r,e,t,n,s,o){return se("conv2d",r,e,t,n,s,o)}function Yd(r,e,t,n,s){return se("pool2d",r,e,t,n,s)}function z2(r,e){return se("embedding",r,e)}var be=M(()=>{"use strict";$i();eN();qr();Kn();l(P2,"hasTensorImpl");l(Ln,"_asTensor");l(ot,"_dispatch");l(se,"_dispatchTensor");l(Ed,"_dispatchTensorArray");l(ae,"add");l(Pe,"sub");l(ee,"mul");l($t,"div");l(Ur,"neg");l(Js,"pow");l($2,"remainder");l(ai,"maximum");l(Ld,"minimum");l(Kr,"exp");l(Zs,"log");l(ui,"sqrt");l(hN,"rsqrt");l(Pc,"abs");l(dN,"sin");l(mN,"cos");l(Ns,"tanh");l(gN,"erf");l(yN,"erfc");l(_N,"lgamma");l(Cd,"gamma");l(ur,"sigmoid");l(Mu,"relu");l(Dd,"gelu");l(Pd,"silu");l($d,"sign");l(bN,"floor");l(wN,"ceil");l($c,"eq");l(TN,"ne");l(xN,"lt");l(Fd,"le");l(Iu,"gt");l(zd,"ge");l(qn,"where");l(Vd,"clamp");l(na,"pad");l(Gd,"one_hot");l(li,"index_select");l(jd,"gather");l(ra,"scatter_add");l(qd,"scatter");l(ht,"sum");l(lr,"mean");l(Qs,"max");l(SN,"min");l(Ud,"argmax");l(kN,"argmin");l(vN,"prod");l(lt,"matmul");l(NN,"dot");l(sa,"cat");l(Wr,"stack");l(AN,"clone");l(F2,"fill");l(Cn,"reshape");l(gt,"transpose");l(Eu,"permute");l(ON,"broadcast_in_dim");l(Lu,"expand");l(Kd,"slice");l(Sr,"unsqueeze");l(Cu,"squeeze");l(Un,"narrow");l(Ut,"select");l(ci,"contiguous");l(Du,"repeat");l(Wd,"tile");l(Bo,"split");l(BN,"chunk");l(RN,"roll");l(MN,"flip");l(IN,"cumsum");l(EN,"sort");l(LN,"argsort");l(CN,"topk");l(Ro,"softmax");l(Hd,"log_softmax");l(pw,"layer_norm");l(fw,"batch_norm");l(hw,"conv2d");l(Yd,"pool2d");l(z2,"embedding")});var $N={};nt($N,{create:()=>DN,default:()=>V2,globals:()=>PN});var DN,PN,V2,FN=M(()=>{DN=l(()=>{throw new Error('mlfw: the "webgpu" npm package is not available in the browser; use navigator.gpu instead')},"create"),PN=void 0,V2={create:DN,globals:PN}});var XN={};nt(XN,{ensureWebGPUEager:()=>H2,flushWebGPUEager:()=>ZN,instantiateWebGPU:()=>q2,prewarmPipelines:()=>YN,recordWebGPUEager:()=>Gc,resetDevice:()=>G2,runWebGPUKernel:()=>U2,runWebGPUPlan:()=>K2,webgpuEagerOp:()=>Z2,webgpuEagerReady:()=>Y2,webgpuRNN:()=>tC});function $u(r){switch(Mn(r)){case"i32":return Int32Array;case"u32":return Uint32Array;case"f16":return Uint16Array;default:return Float32Array}}function Qd(r,e,t,n){if(t==="bf16")for(let s=0;s<e.length;s++)r[n+s]=fd(e[s]);else if(t==="i64")for(let s=0;s<e.length;s++)r[n+s]=Number(BigInt.asIntN(32,e[s]));else r.set(e,n)}function bw(r,e,t,n,s){if(t==="bf16")for(let o=0;o<s;o++)r[o]=hd(e[n+o]);else if(t==="i64")for(let o=0;o<s;o++)r[o]=BigInt(e[n+o]);else r.set(e.subarray(n,n+s))}function eo(r){return Math.ceil(r/4)*4}async function ww(){return kt||Fc||(Fc=(async()=>{let r=typeof navigator<"u"&&navigator.gpu;if(!r)try{let i=await Promise.resolve().then(()=>(FN(),$N));qN=i,r=i.create([]),i.globals&&(zc=i.globals.GPUBufferUsage,Jd=i.globals.GPUMapMode,Zd=i.globals.GPUShaderStage)}catch{throw new Error('WebGPU not available: install the "webgpu" npm package or run in a browser with WebGPU support')}!zc&&typeof GPUBufferUsage<"u"&&(zc=GPUBufferUsage,Jd=GPUMapMode,Zd=GPUShaderStage);let e=await r.requestAdapter();if(!e)throw new Error("WebGPU: no adapter found");let t=e.limits||{},n={},s=["maxStorageBuffersPerShaderStage","maxStorageBufferBindingSize","maxBufferSize","maxBindingsPerBindGroup"];for(let i of s)t[i]!==void 0&&(n[i]=t[i]);let o=[];return e.features&&e.features.has("shader-f16")&&o.push("shader-f16"),kt=await e.requestDevice({requiredLimits:n,requiredFeatures:o}),!zN&&typeof process<"u"&&process.on&&(zN=!0,process.on("exit",()=>{kt&&(kt.destroy(),kt=null)})),kt})(),Fc)}function Fu(){return zc}function Tw(){return Jd.READ}function G2(){kt&&(kt.destroy(),kt=null),Fc=null,zc=null,Jd=null,Zd=null,qN=null}function j2(r){return r.name==="_shapes"?"uniform":r.mode==="read_write"?"storage":"read-only-storage"}function UN(r,e){let t=r.createShaderModule({code:e.source}),n=[];for(let i of e.metadata.bindings)n.push({binding:i.index,visibility:Zd.COMPUTE,buffer:{type:j2(i)}});let s=r.createBindGroupLayout({entries:n}),o=r.createPipelineLayout({bindGroupLayouts:[s]});return{shaderModule:t,bindGroupLayout:s,pipelineLayout:o}}function KN(r,e){let{shaderModule:t,bindGroupLayout:n,pipelineLayout:s}=UN(r,e);return{pipeline:r.createComputePipeline({layout:s,compute:{module:t,entryPoint:e.name}}),bindGroupLayout:n}}function WN(r,e){let t=Xd.get(e);return t||(t=KN(r,e),Xd.set(e,t)),t}async function q2(r){let e=await ww(),{pipeline:t,bindGroupLayout:n}=KN(e,r);return{device:e,pipeline:t,bindGroupLayout:n,kernel:r,workgroupSize:r.metadata.workgroupSize,dispatchSize:r.metadata.dispatchSize,bindings:r.metadata.bindings}}function HN(r){let e=new Map,t=0;for(let n of r)if(n.name!=="_shapes")if(n.packed)for(let s of n.packed)e.set(s.name,s.argIndex!==void 0?s.argIndex:t++);else e.set(n.name,n.argIndex!==void 0?n.argIndex:t++);return e}async function U2(r,e,t){let{device:n,pipeline:s,bindGroupLayout:o,bindings:i,dispatchSize:a}=r,u=Fu(),p=[],c=[],h=HN(i);for(let y=0;y<i.length;y++){let _=i[y];if(_.name==="_shapes"){let v=new Uint32Array(t||[]),O=Math.max(Math.ceil(v.byteLength/16)*16,16),R=n.createBuffer({size:O,usage:u.UNIFORM|u.COPY_DST});n.queue.writeBuffer(R,0,v),p.push(R),c.push({binding:_.index,resource:{buffer:R}});continue}let b=bs(_.dtype),w=$u(_.dtype);if(_.packed){let v=eo(_.packedSize*b),R=_.mode==="read_write"?u.STORAGE|u.COPY_SRC|u.COPY_DST:u.STORAGE|u.COPY_DST,B=n.createBuffer({size:Math.max(v,4),usage:R,mappedAtCreation:!0}),E=new w(B.getMappedRange());for(let z of _.packed){let D=h.get(z.name),J=e[D];J&&Qd(E,J,z.dtype,z.offset)}B.unmap(),p.push(B),c.push({binding:_.index,resource:{buffer:B}});continue}let T=h.get(_.name),S=e[T],x=eo(S.length*b),A=_.mode==="read_write"?u.STORAGE|u.COPY_SRC|u.COPY_DST:u.STORAGE|u.COPY_DST,N=n.createBuffer({size:Math.max(x,4),usage:A,mappedAtCreation:!0});Qd(new w(N.getMappedRange()),S,_.dtype,0),N.unmap(),p.push(N),c.push({binding:_.index,resource:{buffer:N}})}let f=n.createBindGroup({layout:o,entries:c}),d=n.createCommandEncoder(),m=d.beginComputePass();m.setPipeline(s),m.setBindGroup(0,f),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=bs(_.dtype),T=$u(_.dtype);if(_.packed)for(let S of _.packed){let x=S.offset*w,k=eo(S.size*w),A=n.createBuffer({size:k,usage:u.MAP_READ|u.COPY_DST});d.copyBufferToBuffer(b,x,A,0,k),g.push({readBuf:A,tensorIdx:h.get(S.name),size:S.size,dtype:S.dtype,ViewCtor:T})}else{let S=b.size,x=n.createBuffer({size:S,usage:u.MAP_READ|u.COPY_DST});d.copyBufferToBuffer(b,0,x,0,S),g.push({readBuf:x,tensorIdx:h.get(_.name),size:e[h.get(_.name)].length,dtype:_.dtype,ViewCtor:T})}}n.queue.submit([d.finish()]);for(let y of g){await y.readBuf.mapAsync(Tw());let _=new y.ViewCtor(y.readBuf.getMappedRange());bw(e[y.tensorIdx],_,y.dtype,0,y.size),y.readBuf.unmap(),y.readBuf.destroy()}for(let y of p)y.destroy()}async function YN(r,e){if(typeof r.createComputePipelineAsync!="function")return;let t=[],n=new Set;for(let s of e){if(!s||n.has(s)||Xd.has(s))continue;n.add(s);let{shaderModule:o,bindGroupLayout:i,pipelineLayout:a}=UN(r,s);t.push(r.createComputePipelineAsync({layout:a,compute:{module:o,entryPoint:s.name}}).then(u=>Xd.set(s,{pipeline:u,bindGroupLayout:i}),()=>{}))}t.length>0&&await Promise.all(t)}async function K2(r,e,t){let n=await ww(),s=Fu();await YN(n,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(r.numSlots).fill(null),a=new Array(r.numSlots).fill("f32");for(let b=0;b<r.numSlots;b++){let w=e[b];if(!w)continue;let T=w.dtype||"f32";a[b]=T;let S=Math.max(eo(w.data.length*bs(T)),4),x=!o.has(b),k=n.createBuffer({size:S,usage:s.STORAGE|s.COPY_DST|s.COPY_SRC,mappedAtCreation:x});x&&(Qd(new($u(T))(k.getMappedRange()),w.data,T,0),k.unmap()),i[b]=k}let u=32,p=[],c=[],h={encoder:n.createCommandEncoder(),pending:0},f=l(()=>{++h.pending>=u&&(n.queue.submit([h.encoder.finish()]),h.encoder=n.createCommandEncoder(),h.pending=0)},"maybeFlush"),d=l(b=>{let w=h.encoder,{pipeline:T,bindGroupLayout:S}=WN(n,b.kernel),x=b.inputSlots.concat(b.outputSlots),k=[],A=[],N=0;for(let B of b.kernel.metadata.bindings)if(B.name==="_shapes"){let E=new Uint32Array(b.shapeValues||[]),z=Math.max(Math.ceil(E.byteLength/16)*16,16),D=n.createBuffer({size:z,usage:s.UNIFORM|s.COPY_DST});n.queue.writeBuffer(D,0,E),p.push(D),k.push({binding:B.index,resource:{buffer:D}})}else if(B.packed){let E=bs(B.dtype),z=n.createBuffer({size:Math.max(eo(B.packedSize*E),4),usage:s.STORAGE|s.COPY_DST|s.COPY_SRC});c.push(z);let D=B.mode==="read_write";for(let J of B.packed){let j=x[J.argIndex],U=eo(J.size*E);D?A.push({slot:j,src:z,srcOff:J.offset*E,bytes:U}):w.copyBufferToBuffer(i[j],0,z,J.offset*E,U)}k.push({binding:B.index,resource:{buffer:z}})}else k.push({binding:B.index,resource:{buffer:i[x[N++]]}});let v=n.createBindGroup({layout:S,entries:k}),O=w.beginComputePass();O.setPipeline(T),O.setBindGroup(0,v);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);f()},"encodeStep"),m=r.scanLoops?[...r.scanLoops].sort((b,w)=>b.loopStart-w.loopStart):r.scanLoop?[r.scanLoop]:null;if(m&&m.length){let b=0;for(let w of m){for(;b<w.loopStart;b++)d(t[b]);for(let T of w.carry)h.encoder.copyBufferToBuffer(i[T.initSlot],0,i[T.a],0,T.bytes);for(let T=0;T<w.T;T++){for(let S of w.xs)h.encoder.copyBufferToBuffer(i[S.xsSlot],T*S.stepBytes,i[S.xtSlot],0,S.stepBytes);for(let S=w.loopStart;S<w.loopEnd;S++)d(t[S]);for(let S of w.ys)h.encoder.copyBufferToBuffer(i[S.ytSlot],0,i[S.ysSlot],T*S.stepBytes,S.stepBytes);for(let S of w.carry){let x=i[S.a];i[S.a]=i[S.b],i[S.b]=x}}for(let T of w.carry)h.encoder.copyBufferToBuffer(i[T.a],0,i[T.finalSlot],0,T.bytes);b=w.loopEnd}for(;b<t.length;b++)d(t[b])}else for(let b of t)d(b);let g=h.encoder,y=new Set(r.argSlots),_=[];for(let b=0;b<r.numSlots;b++){if(!i[b]||!o.has(b)||!y.has(b))continue;let w=e[b],T=Math.max(eo(w.data.length*bs(a[b])),4),S=n.createBuffer({size:T,usage:s.MAP_READ|s.COPY_DST});g.copyBufferToBuffer(i[b],0,S,0,T),_.push({rb:S,dtype:a[b],size:w.data.length,dst:w.data})}n.queue.submit([g.finish()]);for(let b of _)await b.rb.mapAsync(Tw()),bw(b.dst,new($u(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 JN(r){let e=_w.get(r);if(e&&e.length)return e.pop();let t=Fu();return kt.createBuffer({size:r,usage:t.STORAGE|t.COPY_SRC|t.COPY_DST})}function VN(r,e){let t=_w.get(e);t||(t=[],_w.set(e,t)),t.push(r)}async function H2(){return await ww(),As||(As=kt.createCommandEncoder()),kt}function Y2(){return!!kt&&!!As}function GN(r,e,t){let n=Vc.get(r);if(n)return n.buf;let s=Fu(),o=Math.max(eo(r.length*bs(e)),4);if(t){let a=kt.createBuffer({size:o,usage:s.STORAGE|s.COPY_SRC|s.COPY_DST,mappedAtCreation:!0});return Qd(new($u(e))(a.getMappedRange()),r,e,0),a.unmap(),Vc.set(r,{buf:a,bytes:o,dtype:e,persistent:t}),a}let i=JN(o);return Vc.set(r,{buf:i,bytes:o,dtype:e,persistent:!1}),i}function Gc(r,e,t){let{pipeline:n,bindGroupLayout:s}=WN(kt,r),{bindings:o,dispatchSize:i}=r.metadata,a=HN(o),u=Fu(),p=[],c=[];for(let d of o){if(d.name==="_shapes"){let y=new Uint32Array(t||[]),_=Math.max(Math.ceil(y.byteLength/16)*16,16),b=kt.createBuffer({size:_,usage:u.UNIFORM|u.COPY_DST});kt.queue.writeBuffer(b,0,y),gw.push(b),p.push({binding:d.index,resource:{buffer:b}});continue}if(d.packed){let y=bs(d.dtype),_=Math.max(eo(d.packedSize*y),4),b=JN(_);yw.push({buf:b,bytes:_});let w=d.mode==="read_write";for(let T of d.packed){let S=GN(e[a.get(T.name)],T.dtype,!w),x=eo(T.size*y);w?c.push({pbuf:b,off:T.offset*y,sub:S,bytes:x}):As.copyBufferToBuffer(S,0,b,T.offset*y,x)}p.push({binding:d.index,resource:{buffer:b}});continue}let m=e[a.get(d.name)],g=GN(m,d.dtype,d.mode!=="read_write");p.push({binding:d.index,resource:{buffer:g}})}let h=kt.createBindGroup({layout:s,entries:p}),f=As.beginComputePass();f.setPipeline(n),f.setBindGroup(0,h),f.dispatchWorkgroups(i[0],i[1],i[2]),f.end();for(let d of c)As.copyBufferToBuffer(d.pbuf,d.off,d.sub,0,d.bytes);++mw>=W2&&(kt.queue.submit([As.finish()]),As=kt.createCommandEncoder(),mw=0)}async function ZN(){if(!kt)return;let r=Fu(),e=[];for(let[t,n]of Vc){if(n.persistent)continue;let s=kt.createBuffer({size:n.bytes,usage:r.MAP_READ|r.COPY_DST});As.copyBufferToBuffer(n.buf,0,s,0,n.bytes),e.push({rb:s,ha:t,buf:n.buf,bytes:n.bytes,dtype:n.dtype})}kt.queue.submit([As.finish()]),As=kt.createCommandEncoder(),mw=0;for(let t of gw)t.destroy();gw.length=0,await Promise.all(e.map(t=>t.rb.mapAsync(Tw())));for(let t of e)bw(t.ha,new($u(t.dtype))(t.rb.getMappedRange()),t.dtype,0,t.ha.length),t.rb.unmap(),t.rb.destroy();for(let t of yw)VN(t.buf,t.bytes);yw.length=0;for(let t of e)VN(t.buf,t.bytes),Vc.delete(t.ha)}function J2(r,e,t,n,s){let o=`${r.join(",")}|${e.join(",")}|${t}|${n}`,i=jN.get(o);if(i)return i;let a=Mn(n),u="";for(let c=r.length-1;c>=0;c--)u+=` let idx${c} = rem % ${r[c]}u; rem = rem / ${r[c]}u; src = src + idx${c} * ${e[c]}u;
13
+ `;return i={name:"contig",source:`@group(0) @binding(0) var<storage, read> inp : array<${a}>;
14
+ @group(0) @binding(1) var<storage, read_write> outp : array<${a}>;
15
+ @compute @workgroup_size(64)
16
+ fn contig(@builtin(global_invocation_id) gid : vec3<u32>) {
17
+ let i = gid.x;
18
+ if (i >= ${s}u) { return; }
19
+ var rem = i;
20
+ var src = ${t}u;
21
+ `+u+` outp[i] = inp[src];
22
+ }`,metadata:{bindings:[{index:0,name:"inp",dtype:n,mode:"read"},{index:1,name:"outp",dtype:n,mode:"read_write"}],dispatchSize:[Math.ceil(s/64),1,1]}},jN.set(o,i),i}function Pu(r){let e=r._impl.storage.rawData;if(r.isContiguous&&r._impl.storageOffset===0&&e.length===r.numel)return e;let t=new e.constructor(r.numel);return Gc(J2(r.shape,r.strides,r._impl.storageOffset,r.dtype,r.numel),[e,t],void 0),t}function Z2(r,e,t){let n=e.map(Pu);n.push(t),Gc(r,n,void 0)}function Q2(r,e,t){let n=`lstm|${r}|${e}|${t}`,s=em.get(n);if(s)return s;let o=4*e,i=o*r,a=o*r+o*e,u=a+o;return s={name:"rnn",source:`fn sig(x: f32) -> f32 { return 1.0 / (1.0 + exp(-x)); }
23
+ @group(0) @binding(0) var<storage, read> xin : array<f32>;
24
+ @group(0) @binding(1) var<storage, read> w : array<f32>;
25
+ @group(0) @binding(2) var<storage, read> h0 : array<f32>;
26
+ @group(0) @binding(3) var<storage, read> c0 : array<f32>;
27
+ @group(0) @binding(4) var<storage, read_write> ys : array<f32>;
28
+ @group(0) @binding(5) var<storage, read_write> hn : array<f32>;
29
+ @group(0) @binding(6) var<storage, read_write> cn : array<f32>;
30
+ var<workgroup> wh : array<f32, ${e}>;
31
+ var<workgroup> wc : array<f32, ${e}>;
32
+ var<workgroup> wg : array<f32, ${o}>;
33
+ @compute @workgroup_size(${Xs})
34
+ fn rnn(@builtin(local_invocation_id) lid : vec3<u32>) {
35
+ let tid = lid.x;
36
+ for (var p = tid; p < ${e}u; p = p + ${Xs}u) { wh[p] = h0[p]; wc[p] = c0[p]; }
37
+ workgroupBarrier();
38
+ for (var t = 0u; t < ${t}u; t = t + 1u) {
39
+ for (var j = tid; j < ${o}u; j = j + ${Xs}u) {
40
+ var acc = w[${a}u + j] + w[${u}u + j];
41
+ let xb = t * ${r}u;
42
+ let wxb = j * ${r}u;
43
+ for (var e = 0u; e < ${r}u; e = e + 1u) { acc = acc + xin[xb + e] * w[wxb + e]; }
44
+ let whb = ${i}u + j * ${e}u;
45
+ for (var q = 0u; q < ${e}u; q = q + 1u) { acc = acc + wh[q] * w[whb + q]; }
46
+ wg[j] = acc;
47
+ }
48
+ workgroupBarrier();
49
+ for (var j = tid; j < ${e}u; j = j + ${Xs}u) {
50
+ let ii = sig(wg[j]);
51
+ let ff = sig(wg[${e}u + j]);
52
+ let gg = tanh(wg[${2*e}u + j]);
53
+ let oo = sig(wg[${3*e}u + j]);
54
+ let cv = ff * wc[j] + ii * gg;
55
+ wc[j] = cv;
56
+ let nh = oo * tanh(cv);
57
+ wh[j] = nh;
58
+ ys[t * ${e}u + j] = nh;
59
+ }
60
+ workgroupBarrier();
61
+ }
62
+ for (var p = tid; p < ${e}u; p = p + ${Xs}u) { hn[p] = wh[p]; cn[p] = wc[p]; }
63
+ }`,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]}},em.set(n,s),s}function QN(r,e,t,n){let s=`${e}|${t}|${n}`;if(r._webgpuPacked&&r._webgpuPackedTag===s)return r._webgpuPacked;let o=l(h=>h.contiguous()._impl.storage.rawData,"raw"),i=o(r.x2h.weight),a=o(r.h2h.weight),u=r.x2h.bias?o(r.x2h.bias):new Float32Array(n),p=r.h2h.bias?o(r.h2h.bias):new Float32Array(n),c=new Float32Array(n*e+n*t+n+n);return c.set(i,0),c.set(a,n*e),c.set(u,n*e+n*t),c.set(p,n*e+n*t+n),r._webgpuPacked=c,r._webgpuPackedTag=s,c}function X2(r,e,t){return QN(r,e,t,4*t)}function eC(r,e,t){let n=`gru|${r}|${e}|${t}`,s=em.get(n);if(s)return s;let o=3*e,i=o*r,a=o*r+o*e,u=a+o;return s={name:"rnn",source:`fn sig(x: f32) -> f32 { return 1.0 / (1.0 + exp(-x)); }
64
+ @group(0) @binding(0) var<storage, read> xin : array<f32>;
65
+ @group(0) @binding(1) var<storage, read> w : array<f32>;
66
+ @group(0) @binding(2) var<storage, read> h0 : array<f32>;
67
+ @group(0) @binding(3) var<storage, read_write> ys : array<f32>;
68
+ @group(0) @binding(4) var<storage, read_write> hn : array<f32>;
69
+ var<workgroup> wh : array<f32, ${e}>;
70
+ var<workgroup> wgx : array<f32, ${o}>;
71
+ var<workgroup> wgh : array<f32, ${o}>;
72
+ @compute @workgroup_size(${Xs})
73
+ fn rnn(@builtin(local_invocation_id) lid : vec3<u32>) {
74
+ let tid = lid.x;
75
+ for (var p = tid; p < ${e}u; p = p + ${Xs}u) { wh[p] = h0[p]; }
76
+ workgroupBarrier();
77
+ for (var t = 0u; t < ${t}u; t = t + 1u) {
78
+ for (var j = tid; j < ${o}u; j = j + ${Xs}u) {
79
+ var ax = w[${a}u + j];
80
+ var ah = w[${u}u + j];
81
+ let xb = t * ${r}u;
82
+ let wxb = j * ${r}u;
83
+ for (var e = 0u; e < ${r}u; e = e + 1u) { ax = ax + xin[xb + e] * w[wxb + e]; }
84
+ let whb = ${i}u + j * ${e}u;
85
+ for (var q = 0u; q < ${e}u; q = q + 1u) { ah = ah + wh[q] * w[whb + q]; }
86
+ wgx[j] = ax;
87
+ wgh[j] = ah;
88
+ }
89
+ workgroupBarrier();
90
+ for (var j = tid; j < ${e}u; j = j + ${Xs}u) {
91
+ let rr = sig(wgx[j] + wgh[j]);
92
+ let zz = sig(wgx[${e}u + j] + wgh[${e}u + j]);
93
+ let nn = tanh(wgx[${2*e}u + j] + rr * wgh[${2*e}u + j]);
94
+ let nh = (1.0 - zz) * nn + zz * wh[j];
95
+ wh[j] = nh;
96
+ ys[t * ${e}u + j] = nh;
97
+ }
98
+ workgroupBarrier();
99
+ }
100
+ for (var p = tid; p < ${e}u; p = p + ${Xs}u) { hn[p] = wh[p]; }
101
+ }`,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]}},em.set(n,s),s}function tC(r,e,t,n,s){if(t.kind==="gru")return nC(r,e,t,n);if(t.batch!==1)return null;let o=t.hiddenSize,i=t.seqLen,a=e.length,u=Pu(r),p=[],c=[];for(let d=0;d<a;d++){let m=d===0?t.inputSize:o,g=X2(e[d],m,o),y,_;n!=null&&s!=null?(y=Pu(Ut(n,0,d)),_=Pu(Ut(s,0,d))):(y=new Float32Array(o),_=new Float32Array(o));let b=new Float32Array(i*o),w=new Float32Array(o),T=new Float32Array(o);Gc(Q2(m,o,i),[u,g,y,_,b,w,T],void 0),u=b,p.push(w),c.push(T)}let h=tn(u,[i,1,o],"f32",Ys),f=l(d=>a===1?tn(d[0],[1,1,o],"f32",Ys):Wr(d.map(m=>tn(m,[1,o],"f32",Ys)),0),"wrapState");return[h,f(p),f(c)]}function nC(r,e,t,n){if(t.batch!==1)return null;let s=t.hiddenSize,o=t.seqLen,i=e.length,a=Pu(r),u=[];for(let h=0;h<i;h++){let f=h===0?t.inputSize:s,d=QN(e[h],f,s,3*s),m=n!=null?Pu(Ut(n,0,h)):new Float32Array(s),g=new Float32Array(o*s),y=new Float32Array(s);Gc(eC(f,s,o),[a,d,m,g,y],void 0),a=g,u.push(y)}let p=tn(a,[o,1,s],"f32",Ys),c=i===1?tn(u[0],[1,1,s],"f32",Ys):Wr(u.map(h=>tn(h,[1,s],"f32",Ys)),0);return[p,c]}var kt,Fc,zc,Jd,Zd,qN,zN,Xd,As,mw,W2,Vc,gw,yw,_w,jN,Xs,em,xw=M(()=>{"use strict";Vn();xu();Kn();ar();be();be();kt=null,Fc=null,zc=null,Jd=null,Zd=null,qN=null,zN=!1;l($u,"wgslViewCtor");l(Qd,"packTensorInto");l(bw,"unpackTensorFrom");l(eo,"align4");l(ww,"ensureDevice");l(Fu,"bufUsage");l(Tw,"mapModeRead");l(G2,"resetDevice");l(j2,"bindingBufferType");l(UN,"pipelineParts");l(KN,"createPipeline");Xd=new WeakMap;l(WN,"pipelineFor");l(q2,"instantiateWebGPU");l(HN,"buildParamIndex");l(U2,"runWebGPUKernel");l(YN,"prewarmPipelines");l(K2,"runWebGPUPlan");As=null,mw=0,W2=64,Vc=new Map,gw=[],yw=[],_w=new Map;l(JN,"acquireStorage");l(VN,"releaseStorage");l(H2,"ensureWebGPUEager");l(Y2,"webgpuEagerReady");l(GN,"eagerBufferFor");l(Gc,"recordWebGPUEager");l(ZN,"flushWebGPUEager");jN=new Map;l(J2,"contigKernel");l(Pu,"webgpuEagerInput");l(Z2,"webgpuEagerOp");Xs=256,em=new Map;l(Q2,"lstmKernelWGSL");l(QN,"rnnPackedWeights");l(X2,"lstmPackedWeights");l(eC,"gruKernelWGSL");l(tC,"webgpuRNN");l(nC,"webgpuGRU")});var Bw={};nt(Bw,{getBackend:()=>Ow,hasBackend:()=>rC,preloadCudaRuntime:()=>eA,preloadWebGPU:()=>qc,registerBackend:()=>Uc});function Uc(r,e){Aw.set(r,e)}function Ow(r){return Aw.get(r)||null}function rC(r){return Aw.has(r)}function sC(){return Sw||(Sw=Promise.resolve().then(()=>(Jv(),Yv))),Sw}function Nw(){return kw||(kw=Promise.resolve().then(()=>(Ad(),Nd))),kw}async function eA(){if(jc)return jc;let r=await Nw();return r.preloadCublas&&await r.preloadCublas(),jc=r,r}async function qc(){if(vw)return vw;let r=await Promise.resolve().then(()=>(xw(),XN)),{setWebGPUEagerFn:e,setWebgpuRNN:t}=await Promise.resolve().then(()=>(Kn(),nA));return await r.ensureWebGPUEager(),e(r.webgpuEagerOp),t(r.webgpuRNN),vw=r,r}function tA(r){let e;try{e=Nu(r.source)}catch(o){throw new Error("encodeWat: "+o.message+`
102
+ `+r.source)}let t;try{t=new WebAssembly.Module(e)}catch(o){throw new Error("WASM: "+o.message+`
103
+ `+r.source)}let n={};if(r.metadata.imports)for(let[o]of r.metadata.imports)n[o]=oC[o]||Math[o]||(i=>i);let s=new WebAssembly.Instance(t,{math:n});return{exports:s.exports,memory:s.exports.memory,bufferOffsets:r.metadata.bufferOffsets,funcName:r.name,binary:e,parallel:r.metadata.parallel||null,mathNames:r.metadata.imports?[...r.metadata.imports.keys()]:[]}}function tm(r,e,t,n,s){let{exports:o,memory:i,bufferOffsets:a,funcName:u}=r,p=o[u],c=[...a.values()],h=Math.min(c.length,e.length);for(let d=0;d<h;d++){let m=e[d];ArrayBuffer.isView(m)&&new m.constructor(i.buffer,c[d],m.length).set(m)}let f=c.slice(0,h);if(t)for(let d of t)f.push(d);n!==void 0&&s!==void 0&&f.push(n,s),p(...f);for(let d=0;d<h;d++){let m=e[d];ArrayBuffer.isView(m)&&m.set(new m.constructor(i.buffer,c[d],m.length))}}function iC(r,e,t=[],n={}){let s=n.warmup??5,o=n.repeat??30,i=tA(r),a=e.map(h=>new Float32Array(Math.max(1,Math.ceil(h/4)))),u=t||[],p=l(()=>{i.parallel?tm(i,a,u,0,i.parallel.extent):tm(i,a,u)},"once");for(let h=0;h<s;h++)p();let c=[];for(let h=0;h<o;h++){let f=performance.now();p(),c.push(performance.now()-f)}return c}var Aw,Sw,kw,jc,vw,oC,Kc=M(()=>{"use strict";kd();Ji();rw();Aw=new Map;l(Uc,"registerBackend");l(Ow,"getBackend");l(rC,"hasBackend");Sw=null;l(sC,"getWasmPool");kw=null;l(Nw,"getCudaRuntime");jc=null;l(eA,"preloadCudaRuntime");vw=null;l(qc,"preloadWebGPU");oC={exp:Math.exp,log:Math.log,sin:Math.sin,cos:Math.cos,tan:Math.tan,tanh:Math.tanh,pow:Math.pow,fmod:l((r,e)=>r%e,"fmod"),rsqrt:l(r=>1/Math.sqrt(r),"rsqrt"),sign:Math.sign,round:Math.round,erf:bu,erfc:wu,lgamma:Tn,gamma:Tu,digamma:Ac};l(tA,"instantiateWasm");l(tm,"runWasmKernel");Uc("js",{instantiate(r){return new Function("return "+r.source)()},runSync(r,e,t){let n=t?[...e,...t]:e;return r(...n)},runAsync(r,e,t){let n=t?[...e,...t]:e;return r(...n)},isAsync(){return!1}});Uc("wasm",{instantiate(r){return tA(r)},runSync(r,e,t){r.parallel?tm(r,e,t,0,r.parallel.extent):tm(r,e,t)},async runAsync(r,e,t){if(r.parallel&&r.parallel.poolSafe){let{runWasmParallel:n}=await sC();await n(r,r.funcName,e,t,r.parallel,r.mathNames);return}this.runSync(r,e,t)},isAsync(r){return!!(r&&r.parallel&&r.parallel.poolSafe)}});l(iC,"measureWasm");Wv("wasm",iC);Uc("webgpu",{instantiate(r){return qc().then(e=>e.instantiateWebGPU(r))},runSync(){throw new Error("WebGPU kernel requires async execution \u2014 use runAsync()")},async runAsync(r,e,t){let{runWebGPUKernel:n}=await qc();await n(r,e,t)},async runPlan(r,e,t){let{runWebGPUPlan:n}=await qc();await n(r,e,t)},isAsync(){return!0}});Uc("cuda",{instantiate(r){return{kernel:r}},runSync(r,e,t){if(!jc)throw new Error("CUDA sync runtime not preloaded \u2014 call preloadCudaRuntime() before synchronous execution");jc.runCudaKernelResident(r.kernel,e,t)},async runAsync(r,e,t){let{runCudaKernel:n}=await Nw();await n(r.kernel,e,t)},async runPlan(r,e,t,n){let{runCudaPlan:s}=await Nw();await s(r,e,t,n)},isAsync(){return!0}})});function Rw(r){return aC[r]||Float32Array}function uC(r){return r instanceof Float64Array?"f64":r instanceof Int32Array?"i32":r instanceof Int16Array?"i16":r instanceof Int8Array?"i8":r instanceof Uint16Array?"ui16":r instanceof Uint8Array?"ui8":r instanceof BigInt64Array?"i64":"f32"}function rA(r,e){if(!(!r.returnFixups||r.returnFixups.length===0))for(let t of r.returnFixups){let n=e[r.argSlots[t.pos]];if(!(!n||!n.data))if(t.kind==="copy"){let s=e[t.srcSlot];s&&s.data&&n.data.set(s.data.subarray(0,n.data.length))}else t.kind==="const"&&n.data.fill(t.value)}}var aC,to,Mw,zu,nm=M(()=>{"use strict";Kc();Ec();aC={f16:Uint16Array,bf16:Uint16Array,f32:Float32Array,f64:Float64Array,i8:Int8Array,i16:Int16Array,i32:Int32Array,i64:BigInt64Array,ui8:Uint8Array,bool:Uint8Array,index:Int32Array};l(Rw,"typedArrayCtor");l(uC,"dtypeOfTypedArray");l(rA,"_applyReturnFixups");to=class r{static{l(this,"RuntimeTensor")}data;shape;dtype;strides;resident;constructor(e,t,n,s=null){this.data=e,this.shape=[...t],this.dtype=n,this.strides=s?[...s]:r.defaultStrides(t)}static defaultStrides(e){let t=new Array(e.length),n=1;for(let s=e.length-1;s>=0;s--)t[s]=n,n*=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 n=1;for(let s=0;s<e.length;s++)n*=e[s];return n=Math.max(n,1),new r(new(Rw(t))(n),e,t)}static fromArray(e,t,n="f32"){let s=Rw(n);return new r(new s(e),t,n)}get(e){let t=0;for(let n=0;n<e.length;n++)t+=e[n]*this.strides[n];return this.data[t]}set(e,t){let n=0;for(let s=0;s<e.length;s++)n+=e[s]*this.strides[s];this.data[n]=t}},Mw=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()]}},zu=class r{static{l(this,"RuntimeModule")}name;kernels;_instances;_shapeParamMaps;_bufferMaps;constructor(e){this.name=e,this.kernels=new Mw,this._instances=new Map}addCompiledKernel(e){this.kernels.register(e.name,e);let t=Ow(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,n){this._shapeParamMaps||(this._shapeParamMaps=new Map),this._shapeParamMaps.set(e,t),n&&(this._bufferMaps||(this._bufferMaps=new Map),this._bufferMaps.set(e,n))}_prepareArgs(e,t){let n=[],s=new Map;for(let a=0;a<t.length;a++){let u=t[a];u instanceof to?(n.push(u.data),s.set(a,u.shape)):n.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=r._extractShapeParams(o,s,t,a)}return{tensorArgs:n,shapeValues:i}}run(e,...t){let n=this._instances.get(e);if(!n)throw new Error("Kernel '"+e+"' not found or not executable");if(n.instance instanceof Promise)throw new Error("Kernel '"+e+"' requires async execution \u2014 use runAsync()");let{tensorArgs:s,shapeValues:o}=this._prepareArgs(e,t);return n.backend.runSync(n.instance,s,o)}async runAsync(e,...t){let n=this._instances.get(e);if(!n)throw new Error("Kernel '"+e+"' not found or not executable");let{tensorArgs:s,shapeValues:o}=this._prepareArgs(e,t),i=await n.instance;return n.backend.runAsync(i,s,o)}isAsync(e){let t=this._instances.get(e);if(!t)return!1;let n=t.instance instanceof Promise?null:t.instance;return t.backend.isAsync(n)}async runPlanAsync(e,t,n){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 to?u:new to(u,[u.length],uC(u))}for(let a of e.intermediates){let u=1;for(let p of a.shape)u*=p;s[a.slot]=new to(new(Rw(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 h of u.inputSlots)p.push(s[h]);for(let h of u.outputSlots)p.push(s[h]);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,n),rA(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)}rA(e,s)}_uniformPlanBackend(e){let t=null;for(let n of e.steps){let s=this._instances.get(n.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,n,s){let o=new Map;if(s){let u=0;for(let[p,c]of s){let h=typeof p=="string"?p:c&&c.name;typeof h=="string"&&o.set(h,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(":"),h=u.substring(0,c),f=parseInt(u.substring(c+1),10),d=null;if(o.has(h)){let m=t.get(o.get(h));m&&f<m.length&&m[f]>0&&(d=m[f])}if(d===null){for(let[,m]of t)if(f<m.length&&m[f]>0){d=m[f];break}}a.push(d!==null?d: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 n=this.kernels.get(t);e.push({name:n.name,source:n.source,target:n.target.name,metadata:n.metadata})}return{name:this.name,kernels:e}}static deserialize(e){let t=new r(e.name);for(let n of e.kernels)t.addCompiledKernel(new Ic(n.name,n.source,{name:n.target},n.metadata));return t}}});var rm,sA=M(()=>{"use strict";rm=class{static{l(this,"AnalysisManager")}constructor(){this._cache=new WeakMap}getAnalysis(e,t){let n=this._cache.get(t);n||(n=new Map,this._cache.set(t,n));let s=n.get(e);if(!s||s.version!==t.version){let o=this._resolveDeps(e,t,n);s={data:e.compute(t,o),version:t.version},n.set(e,s)}return s.data}_resolveDeps(e,t,n){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 n=this._cache.get(e);if(!n)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,h=!1;if(c){for(let f of c)if(!s(f)||i(f)){h=!0;break}}return o.set(u,h),h},"isStale"),a=[];for(let u of n.keys())if(!s(u)||i(u))a.push(u);else{let p=n.get(u);p&&(p.version=e.version)}for(let u of a)n.delete(u)}invalidateFunctions(e,t=null){for(let n of e)this.invalidate(n,t)}invalidateAll(){this._cache=new WeakMap}}});var oe,sm,om,Ge,im,nn=M(()=>{"use strict";oe=Object.freeze({UNCHANGED:0,CHANGED:1,FAILED:2}),sm=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")}},om=class{static{l(this,"PassContext")}constructor({optLevel:e=1/0,disabledPasses:t=[],requiredPasses:n=[],config:s={}}={}){this.optLevel=e,this.disabledPasses=t instanceof Set?t:new Set(t),this.requiredPasses=n instanceof Set?n:new Set(n),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)}},Ge=class extends sm{static{l(this,"FunctionPass")}run(e,t){throw new Error("Not implemented")}},im=class extends sm{static{l(this,"ModulePass")}run(e,t){throw new Error("Not implemented")}}});var ue,Dn,lC,am,Kt=M(()=>{"use strict";nn();ue=Object.freeze({SILENT:0,INFO:1,VERBOSE:2,DEBUG:3}),Dn=class{static{l(this,"CompilationError")}constructor(e,t,n,s){this.phase=e,this.funcName=t,this.message=n,this.passName=s||null}toString(){let e="["+this.phase+"]";return this.funcName&&(e+=" "+this.funcName),this.passName&&(e+=" ("+this.passName+")"),e+=": "+this.message,e}},lC=l(()=>{},"NOOP"),am=class{static{l(this,"TraceLog")}constructor(e={}){this.level=e.level??ue.SILENT,this.sink=typeof e.sink=="function"?e.sink:lC,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:ue.INFO})}phaseEnd(e,t){this.emit({type:"phase",action:"end",phase:e,durationMs:t,level:ue.INFO})}passRun(e,t,n,s,o){this.emit({type:"pass",passName:e,changed:t===oe.CHANGED,durationMs:n,opCountBefore:s,opCountAfter:o,level:ue.VERBOSE})}functionEvent(e,t,n){this.emit({type:"function",phase:e,funcName:t,...n,level:ue.INFO})}irDump(e,t){this.emit({type:"ir_snapshot",label:e,text:t,level:ue.DEBUG})}memoryStats(e,t){this.emit({type:"memory",funcName:e,...t,level:ue.VERBOSE})}autotuneStats(e,t){this.emit({type:"autotune",funcName:e,...t,level:ue.VERBOSE})}codegenStats(e,t){this.emit({type:"codegen",funcName:e,...t,level:ue.VERBOSE})}errorEvent(e,t,n,s){this.emit({type:"error",phase:e,funcName:t,message:n,passName:s||null,level:ue.INFO})}warn(e,t,n,s){this.emit({type:"warning",phase:e,funcName:t,message:n,detail:s||null,level:ue.INFO})}explain(e,t,n,s,o){this.emit({type:"explain",category:e,subject:t,decision:n,reason:s,...o||{},level:ue.DEBUG})}get explainsEnabled(){return this.level>=ue.DEBUG}shouldSnapshot(e){return this.level>=ue.DEBUG&&!!this.irSnapshot[e]}}});function Wc(r){if(typeof r.numOps=="function")return r.numOps();if(typeof r[Symbol.iterator]=="function"){let e=0;for(let t of r)typeof t.numOps=="function"&&(e+=t.numOps());return e}return-1}var Vu,oa,um=M(()=>{"use strict";sA();nn();Kt();Kt();l(Wc,"countOps");Vu=class{static{l(this,"FixedPointGroup")}constructor(e,t,n=8){this.name=e,this.passes=t,this.maxIterations=n}},oa=class{static{l(this,"PassManager")}constructor(){this.passes=[],this.analysisManager=new rm,this.trace=null,this.verifyHook=null,this.instruments=[]}addPass(e){this.passes.push(e)}addInstrument(e){this.instruments.push(e)}_notify(e,t,n,s){for(let o of this.instruments)typeof o[e]=="function"&&o[e](t,n,s)}setTrace(e){this.trace=e}setVerifyHook(e){this.verifyHook=e}_verifyAfter(e,t,n){if(!this.verifyHook)return null;let s=this.verifyHook(t,n);if(!s||s.length===0)return null;let o=n?t.name||"<module>":t.name;return new Dn("verification",o,`pass '${e.name}' produced invalid IR: ${s.join("; ")}`,e.name)}_applyPass(e,t,n,s){this.trace&&(e.trace=this.trace),this.instruments.length&&this._notify("runBeforePass",e,t,null);let o=n.verbose,i=n.resilient,a=!1,u=!1;if(e instanceof im){let p=o?Wc(t):-1,c=o?performance.now():0,h;try{h=e.run(t,this.analysisManager)}catch(f){if(!i)throw f;return this.analysisManager.invalidateAll(),s.push(oe.FAILED),n.errors.push(new Dn("graphPasses",t.name||"<module>",f.message,e.name)),{changed:a,fatal:!1}}if(s.push(h),o&&this.trace.passRun(e.name,h,performance.now()-c,p,Wc(t)),h===oe.CHANGED){a=!0,n.anyChanged=!0,this.analysisManager.invalidateFunctions(t,e.preservedAnalyses);let f=this._verifyAfter(e,t,!0);f&&(n.errors.push(f),i||(u=!0))}else h===oe.FAILED&&(this.analysisManager.invalidateAll(),n.errors.push(new Dn("graphPasses",t.name||"<module>",`pass '${e.name}' failed`,e.name)),i||(u=!0))}else if(e instanceof Ge){let p=!1;for(let c of t){if(n.failedFunctions.has(c.name))continue;let h=o?Wc(c):-1,f=o?performance.now():0;if(i)try{for(let m of e.requiredAnalyses)this.analysisManager.getAnalysis(m,c);let d=e.run(c,this.analysisManager);if(o&&this.trace.passRun(e.name,d,performance.now()-f,h,Wc(c)),d===oe.CHANGED){p=!0,n.anyChanged=!0,c.bumpVersion(),this.analysisManager.invalidate(c,e.preservedAnalyses);let m=this._verifyAfter(e,c,!1);m&&(n.errors.push(m),n.failedFunctions.add(c.name))}else d===oe.FAILED&&(this.analysisManager.invalidate(c),n.errors.push(new Dn("graphPasses",c.name,`pass '${e.name}' failed`,e.name)),n.failedFunctions.add(c.name))}catch(d){n.errors.push(new Dn("graphPasses",c.name,d.message,e.name)),n.failedFunctions.add(c.name)}else{for(let m of e.requiredAnalyses)this.analysisManager.getAnalysis(m,c);let d=e.run(c,this.analysisManager);if(o&&this.trace.passRun(e.name,d,performance.now()-f,h,Wc(c)),d===oe.CHANGED){p=!0,n.anyChanged=!0,c.bumpVersion(),this.analysisManager.invalidate(c,e.preservedAnalyses);let m=this._verifyAfter(e,c,!1);if(m){n.errors.push(m),n.failedFunctions.add(c.name),u=!0;break}}else if(d===oe.FAILED){this.analysisManager.invalidate(c),n.errors.push(new Dn("graphPasses",c.name,`pass '${e.name}' failed`,e.name)),n.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,n,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(!n.passContext.shouldRun(u))continue;let{changed:p,fatal:c}=this._applyPass(u,t,n,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 n={verbose:this.trace&&this.trace.level>=ue.VERBOSE,resilient:t.errorMode==="resilient",errors:[],failedFunctions:new Set,anyChanged:!1,passContext:t.passContext||new om},s=[];for(let o of this.passes){if(!(o instanceof Vu)&&!n.passContext.shouldRun(o))continue;if(o instanceof Vu?this._runGroup(o,e,n,s):this._applyPass(o,e,n,s).fatal)return{changed:n.anyChanged,results:s,errors:n.errors,failedFunctions:n.failedFunctions.size>0?n.failedFunctions:null}}return{changed:n.anyChanged,results:s,errors:n.errors.length>0?n.errors:null,failedFunctions:n.failedFunctions.size>0?n.failedFunctions:null}}}});function rn(r,e){Iw.set(r,e)}function Ew(r,e,t,n){let s=r.neg(e).getResult(0),o=r.exp(s).getResult(0),i=r.broadcast(r.scalarConstant(1,t).getResult(0),n,[]).getResult(0),a=r.add(i,o).getResult(0);return r.div(i,a).getResult(0)}function pn(r,e,t,n){return r.broadcast(r.scalarConstant(e,t).getResult(0),n,[]).getResult(0)}var Iw,Gu,Lw=M(()=>{"use strict";nn();es();Oe();Kt();Iw=new Map;l(rn,"registerDecomposition");Gu=class extends Ge{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())Iw.has(o.opName)&&this._shouldDecompose(o)&&t.push(o);if(t.length===0)return oe.UNCHANGED;let n=new An(e),s=[];for(let o of t){if(!o.parentBlock)continue;let i=Iw.get(o.opName);n.block=o.parentBlock,n.setInsertionPoint(o),s.push(o.opName),i(o,n)}if(this.trace&&this.trace.level>=ue.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:ue.DEBUG})}return oe.CHANGED}};rn("stop_gradient",r=>{r.replaceAllResultsWith([r.getOperand(0)]),r.erase()});rn("all_reduce",(r,e)=>{let t=r.getOperand(0),n=r.getAttr("mesh_axis")??0,s=r.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),[n],s).getResult(0),p=[];for(let h=0;h<o.length;h++)h!==n&&p.push(h);let c=e.broadcast(u,o,p).getResult(0);r.replaceAllResultsWith([c]),r.erase()});rn("all_gather",(r,e)=>{let t=r.getOperand(0),n=t.type.shape,s=r.getAttr("mesh_axis")??0,o=r.getAttr("gather_dim")??1,i=n[s],a=n.filter((m,g)=>g!==s),u=o<s?o:o-1,p=[];for(let m=0;m<i;m++){let g=n.map((b,w)=>w===s?m:0),y=n.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),h=[...n];h[o]=i*n[o];let f=[];for(let m=0;m<h.length;m++)m!==s&&f.push(m);let d=e.broadcast(c,h,f).getResult(0);r.replaceAllResultsWith([d]),r.erase()});rn("softmax",(r,e)=>{let t=r.getOperand(0),n=r.getAttr("axis"),s=t.type.rank,o=t.type.dtype,i=t.type.shape,a=To(s,n),u=e.reduce(t,e.scalarConstant(-1/0,o).getResult(0),[n],"max"),p=e.broadcast(u.getResult(0),i,a),c=e.sub(t,p.getResult(0)),h=e.exp(c.getResult(0)),f=e.reduce(h.getResult(0),e.scalarConstant(0,o).getResult(0),[n],"sum"),d=e.broadcast(f.getResult(0),i,a),m=e.div(h.getResult(0),d.getResult(0));r.replaceAllResultsWith([m.getResult(0)]),r.erase()});rn("log_softmax",(r,e)=>{let t=r.getOperand(0),n=r.getAttr("axis"),s=t.type.rank,o=t.type.dtype,i=t.type.shape,a=To(s,n),u=e.reduce(t,e.scalarConstant(-1/0,o).getResult(0),[n],"max"),p=e.broadcast(u.getResult(0),i,a),c=e.sub(t,p.getResult(0)),h=e.exp(c.getResult(0)),f=e.reduce(h.getResult(0),e.scalarConstant(0,o).getResult(0),[n],"sum"),d=e.log(f.getResult(0)),m=e.broadcast(d.getResult(0),i,a),g=e.sub(c.getResult(0),m.getResult(0));r.replaceAllResultsWith([g.getResult(0)]),r.erase()});l(Ew,"emitSigmoid");rn("sigmoid",(r,e)=>{let t=r.getOperand(0),n=Ew(e,t,t.type.dtype,t.type.shape);r.replaceAllResultsWith([n]),r.erase()});rn("gelu",(r,e)=>{let t=r.getOperand(0),n=t.type.dtype,s=t.type.shape,o=e.broadcast(e.scalarConstant(1.702,n).getResult(0),s,[]).getResult(0),i=e.mul(o,t).getResult(0),a=Ew(e,i,n,s),u=e.mul(t,a).getResult(0);r.replaceAllResultsWith([u]),r.erase()});rn("silu",(r,e)=>{let t=r.getOperand(0),n=Ew(e,t,t.type.dtype,t.type.shape),s=e.mul(t,n).getResult(0);r.replaceAllResultsWith([s]),r.erase()});rn("layer_norm",(r,e)=>{let t=r.getOperand(0),n=r.getOperand(1),s=r.getOperand(2),o=r.getAttr("axis"),i=r.getAttr("epsilon"),a=t.type.rank,u=t.type.dtype,p=t.type.shape,c=To(a,o),h=e.reduce(t,e.scalarConstant(0,u).getResult(0),[o],"mean"),f=e.broadcast(h.getResult(0),p,c),d=e.sub(t,f.getResult(0)),m=e.mul(d.getResult(0),d.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),T=e.mul(d.getResult(0),w.getResult(0)),S=e.broadcast(n,p,[o]),x=e.mul(T.getResult(0),S.getResult(0)),k=e.broadcast(s,p,[o]),A=e.add(x.getResult(0),k.getResult(0));r.replaceAllResultsWith([A.getResult(0)]),r.erase()});rn("batch_norm",(r,e)=>{let t=r.getOperand(0),n=r.getOperand(1),s=r.getOperand(2),o=r.getOperand(3),i=r.getOperand(4),a=r.getAttr("axis"),u=r.getAttr("epsilon"),p=t.type.rank,c=t.type.dtype,h=t.type.shape,f=e.broadcast(e.scalarConstant(u,c).getResult(0),i.type.shape,[]),d=e.add(i,f.getResult(0)),m=e.rsqrt(d.getResult(0)),g=e.broadcast(o,h,[a]),y=e.sub(t,g.getResult(0)),_=e.broadcast(m.getResult(0),h,[a]),b=e.mul(y.getResult(0),_.getResult(0)),w=e.broadcast(n,h,[a]),T=e.mul(b.getResult(0),w.getResult(0)),S=e.broadcast(s,h,[a]),x=e.add(T.getResult(0),S.getResult(0));r.replaceAllResultsWith([x.getResult(0)]),r.erase()});rn("where",(r,e)=>{let t=r.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 n=e.select(t,r.getOperand(1),r.getOperand(2));r.replaceAllResultsWith([n.getResult(0)]),r.erase()});rn("split",(r,e)=>{let t=r.getOperand(0),n=r.getAttr("dimension"),s=r.getAttr("split_sizes"),o=t.type.shape,i=[],a=0;for(let u of s){let p=o.map((h,f)=>f===n?a:0),c=o.map((h,f)=>f===n?a+u:h);i.push(e.slice(t,p,c).getResult(0)),a+=u}r.replaceAllResultsWith(i),r.erase()});rn("one_hot",(r,e)=>{let t=r.getOperand(0),n=r.getAttr("depth"),s=r.getAttr("axis")??-1,o=r.getAttr("on_value")??1,i=r.getAttr("off_value")??0,a=r.getResult(0).type,u=a.dtype,p=a.shape,c=s<0?t.type.rank+1+s:s,h=new I(p,"i32"),f=e._inferAndBuild("iota",[],{iota_dimension:c,tensor_type:h}),d=t.type.rank,m=[];for(let S=0;S<d;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),f.getResult(0),"eq"),b=e.broadcast(e.scalarConstant(o,u).getResult(0),p,[]),w=e.broadcast(e.scalarConstant(i,u).getResult(0),p,[]),T=e.select(_.getResult(0),b.getResult(0),w.getResult(0));r.replaceAllResultsWith([T.getResult(0)]),r.erase()});l(pn,"bcast");rn("elu",(r,e)=>{let t=r.getOperand(0),n=r.getAttr("alpha")??1,s=t.type.dtype,o=t.type.shape,i=pn(e,0,s,o),a=e.compare(t,i,"gt").getResult(0),u=pn(e,1,s,o),p=e.exp(t).getResult(0),c=e.sub(p,u).getResult(0),h=pn(e,n,s,o),f=e.mul(h,c).getResult(0),d=e.select(a,t,f);r.replaceAllResultsWith([d.getResult(0)]),r.erase()});rn("leaky_relu",(r,e)=>{let t=r.getOperand(0),n=r.getAttr("negative_slope")??.01,s=t.type.dtype,o=t.type.shape,i=pn(e,0,s,o),a=e.compare(t,i,"gt").getResult(0),u=pn(e,n,s,o),p=e.mul(u,t).getResult(0),c=e.select(a,t,p);r.replaceAllResultsWith([c.getResult(0)]),r.erase()});rn("celu",(r,e)=>{let t=r.getOperand(0),n=r.getAttr("alpha")??1,s=t.type.dtype,o=t.type.shape,i=pn(e,0,s,o),a=e.maximum(t,i).getResult(0),u=pn(e,n,s,o),p=e.div(t,u).getResult(0),c=e.exp(p).getResult(0),h=pn(e,1,s,o),f=e.sub(c,h).getResult(0),d=e.mul(u,f).getResult(0),m=e.minimum(i,d).getResult(0),g=e.add(a,m);r.replaceAllResultsWith([g.getResult(0)]),r.erase()});rn("selu",(r,e)=>{let t=r.getOperand(0),n=t.type.dtype,s=t.type.shape,o=1.0507009873554805,i=1.6732632423543772,a=pn(e,0,n,s),u=e.compare(t,a,"gt").getResult(0),p=pn(e,1,n,s),c=e.exp(t).getResult(0),h=e.sub(c,p).getResult(0),f=pn(e,i,n,s),d=e.mul(f,h).getResult(0),m=e.select(u,t,d).getResult(0),g=pn(e,o,n,s),y=e.mul(g,m);r.replaceAllResultsWith([y.getResult(0)]),r.erase()});rn("mish",(r,e)=>{let t=r.getOperand(0),n=t.type.dtype,s=t.type.shape,o=pn(e,1,n,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);r.replaceAllResultsWith([c.getResult(0)]),r.erase()});rn("hardswish",(r,e)=>{let t=r.getOperand(0),n=t.type.dtype,s=t.type.shape,o=pn(e,3,n,s),i=pn(e,0,n,s),a=pn(e,6,n,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),h=e.mul(t,c);r.replaceAllResultsWith([h.getResult(0)]),r.erase()});rn("hardsigmoid",(r,e)=>{let t=r.getOperand(0),n=t.type.dtype,s=t.type.shape,o=pn(e,6,n,s),i=pn(e,.5,n,s),a=pn(e,0,n,s),u=pn(e,1,n,s),p=e.div(t,o).getResult(0),c=e.add(p,i).getResult(0),h=e.minimum(e.maximum(c,a).getResult(0),u);r.replaceAllResultsWith([h.getResult(0)]),r.erase()});rn("embedding",(r,e)=>{let t=r.getOperand(0),n=r.getOperand(1),s=t.type.shape[t.type.rank-1],o=n.type.rank,i=e._inferAndBuild("gather",[t,n],{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});r.replaceAllResultsWith([i.getResult(0)]),r.erase()})});var ju,Cw=M(()=>{"use strict";nn();es();Kt();ju=class{static{l(this,"PatternApplicator")}constructor(e){this.patternSet=e}applyPatterns(e,t=10,n=null){let s=new An(e),o=0,i=[...e.opsRecursive()],a=0,u=new Set(i),p=l(d=>{!d||!d.parentBlock||u.has(d)||(u.add(d),i.push(d))},"enqueue"),c=Math.max(t,1)*Math.max(i.length,1)*4+1e3,h=0,f=!1;for(;a<i.length;){if(++h>c){f=!0;break}let d=i[a++];if(u.delete(d),!d.parentBlock)continue;let m=this.patternSet.getForOp(d.opName);for(let g of m){if(!g.match(d))continue;let y=d.parentBlock,_=d._prev,b=d._next,w=[];for(let x=0;x<d.numResults;x++)for(let k of d.getResult(x).getUsers())w.push(k);for(let x=0;x<d.numOperands;x++){let k=d.getOperand(x).definingOp;k&&w.push(k)}if(s.block=y,s.setInsertionPoint(d),!g.rewrite(d,s))continue;o++;for(let x of w)p(x);let T=_?_._next:y._head,S=y._size+2;for(;T&&T!==b&&S-- >0;)p(T),T=T._next;p(d);break}}return n&&(f&&n.level>=ue.INFO&&n.emit({type:"pass_detail",passName:"PatternApplicator",message:`pattern rewriting hit safety budget (${c}) without converging`,totalRewrites:o,level:ue.INFO}),n.level>=ue.DEBUG&&o>0&&n.emit({type:"pass_detail",passName:"PatternApplicator",totalRewrites:o,patternCount:this.patternSet.patterns.length,level:ue.DEBUG})),o>0?oe.CHANGED:oe.UNCHANGED}}});function cC(){if(Hc)return Hc;Hc=new Ua;for(let r of ne.allOps())if(r.getCanonicalizationPatterns){let e=r.getCanonicalizationPatterns();if(e)for(let t of e)Hc.add(t)}return Hc}var Hc,ia,Dw=M(()=>{"use strict";nn();uc();Cw();At();Hc=null;l(cC,"getCanonicalizationPatterns");ia=class extends Ge{static{l(this,"CanonicalizePass")}constructor(){super("canonicalize")}run(e,t){let n=cC();return new ju(n).applyPatterns(e,10,this.trace)}}});var lm,Pw,qu,oA=M(()=>{"use strict";At();_n();lm=class{static{l(this,"MemoryEffect")}constructor(e,t){this.kind=e,this.value=t}},Pw=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 n=[];for(let s=0;s<t.length;s++)t[s].effect.kind===an.READ&&n.push(t[s].op);return n}getWritersOf(e){let t=this._valueEffects.get(e);if(!t)return[];let n=[];for(let s=0;s<t.length;s++)t[s].effect.kind===an.WRITE&&n.push(t[s].op);return n}},qu=class{static get name(){return"memory_effect"}static get depKey(){return"memoryEffect"}static get dependencies(){return[]}static compute(e){let t=new Map,n=new Map,s=l((i,a,u)=>{let p=n.get(i);p||(p=[],n.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=an.NONE;if(a&&a.sideEffects?p=a.sideEffects:i.hasSideEffects&&i.hasSideEffects()&&(p=an.WRITE),p!==an.NONE)if(a&&a.getMemoryEffects){let c=a.getMemoryEffects(i);for(let h of c)u.push(h),s(h.value,i,h)}else{if(p&an.READ)for(let c=0;c<i.numOperands;c++){let h=new lm(an.READ,i.getOperand(c));u.push(h),s(i.getOperand(c),i,h)}if(p&an.WRITE)for(let c=0;c<i.numResults;c++){let h=new lm(an.WRITE,i.getResult(c));u.push(h),s(i.getResult(c),i,h)}}t.set(i,u)}return new Pw(t,n)}}});var no,$w=M(()=>{"use strict";nn();oA();Tr();Kt();no=class extends Ge{static{l(this,"DCEPass")}constructor(){super("dce"),this.preservedAnalyses=new Set,this.requiredAnalyses=[qu]}run(e,t){let n=!1,s=t?t.getAnalysis(qu,e):qu.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(),n=!0,i++;for(let p of u)p.parentBlock&&this._isDead(p,s)&&o.push(p)}return this.trace&&this.trace.level>=ue.DEBUG&&i>0&&this.trace.emit({type:"pass_detail",passName:this.name,erasedCount:i,level:ue.DEBUG}),n?oe.CHANGED:oe.UNCHANGED}_isDead(e,t){if(qt(e.opName)||e.regions&&e.regions.length>0)return!1;for(let n=0;n<e.numResults;n++)if(e.getResult(n).hasUses)return!1;return!t.hasSideEffect(e)}}});var cm,pm,iA=M(()=>{"use strict";cm=class r{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 r(e.primitive,e.args)}},pm=class r{static{l(this,"ScheduleTrace")}constructor(){this.steps=[]}record(e,t){let n=new cm(e,t);return this.steps.push(n),n}serialize(){return this.steps.map(e=>e.serialize())}static deserialize(e){let t=new r;for(let n of e)t.steps.push(cm.deserialize(n));return t}replay(e){for(let t of this.steps){let n=e[t.primitive];if(typeof n!="function")throw new Error(`Unknown schedule primitive: ${t.primitive}`);e._replaying=!0;try{n.call(e,...t.args)}finally{e._replaying=!1}}}get length(){return this.steps.length}clear(){this.steps.length=0}}});function aa(r){return Number.isFinite(r)}function aA(r){return Number.isNaN(r)?lA:r}function uA(r){return Number.isNaN(r)?cA:r}var lA,cA,Hr,Uu,Os,Ku=M(()=>{"use strict";Vs();lA=-1/0,cA=1/0;l(aa,"isFinite2");l(aA,"boundLo");l(uA,"boundHi");Hr=class{static{l(this,"IntBound")}constructor(e,t){this.min=e,this.max=t}isConst(){return this.min===this.max&&aa(this.min)}},Uu=new Hr(lA,cA),Os=class{static{l(this,"Analyzer")}constructor(){this._varBounds=new Map}bind(e,t,n){return this._varBounds.set(e,new Hr(t,n)),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,n]of e)typeof n=="number"&&n>0&&this.bind(t,0,n-1);return this}constIntBound(e){if(typeof e=="number")return new Hr(e,e);if(!(e instanceof Ce))return Uu;if(e.type==="var")return this._varBounds.get(e.name)||Uu;let t=e.args.length>0?this.constIntBound(e.args[0]):null,n=e.args.length>1?this.constIntBound(e.args[1]):null;switch(e.type){case"add":return new Hr(aA(t.min+n.min),uA(t.max+n.max));case"sub":return new Hr(aA(t.min-n.max),uA(t.max-n.min));case"neg":return new Hr(-t.max,-t.min);case"mul":return this._mulBound(t,n);case"max":return new Hr(Math.max(t.min,n.min),Math.max(t.max,n.max));case"min":return new Hr(Math.min(t.min,n.min),Math.min(t.max,n.max));case"div":return this._divBound(t,n,Math.floor);case"ceildiv":return this._divBound(t,n,Math.ceil);case"mod":return this._modBound(t,n);default:return Uu}}_mulBound(e,t){if(!aa(e.min)||!aa(e.max)||!aa(t.min)||!aa(t.max))return Uu;let n=[e.min*t.min,e.min*t.max,e.max*t.min,e.max*t.max];return new Hr(Math.min(...n),Math.max(...n))}_divBound(e,t,n){if(t.isConst()&&t.min>0){let s=aa(e.min)?n(e.min/t.min):e.min,o=aa(e.max)?n(e.max/t.min):e.max;return new Hr(s,o)}return Uu}_modBound(e,t){return t.isConst()&&t.min>0?new Hr(0,t.min-1):Uu}canProveGreaterEqual(e,t){return this.constIntBound(Ce.sub(e,t)).min>=0}canProveLess(e,t){return this.constIntBound(Ce.sub(e,t)).max<0}canProveNonNegative(e){return this.canProveGreaterEqual(e,0)}canProveEqual(e,t){if(Ce.equals(e,t))return!0;let n=this.constIntBound(Ce.sub(e,t));return n.min===0&&n.max===0}}});function pi(r){if(r==null)return null;if(typeof r=="number")return r;switch(r.type){case"IntImmNode":return r.value;case"VariableNode":return Ce.var(r.name);case"MathOpNode":{if(r.b===null||r.b===void 0){if(r.op==="-"){let s=pi(r.a);return s===null?null:Ce.neg(s)}return null}let e=pC[r.op];if(!e)return null;let t=pi(r.a);if(t===null)return null;let n=pi(r.b);return n===null||(r.op==="//"||r.op==="%")&&(typeof n!="number"||n<=0)?null:Ce[e](t,n)}case"CallExternNode":{if(r.args.length!==2||r.externName!=="max"&&r.externName!=="min")return null;let e=pi(r.args[0]);if(e===null)return null;let t=pi(r.args[1]);return t===null?null:r.externName==="max"?Ce.max(e,t):Ce.min(e,t)}default:return null}}function la(r,e){let t=pi(e);return t===null?null:r.constIntBound(t)}function fC(r,e,t){let n=pi(e);if(n===null)return null;let s=pi(t);return s===null?null:r.constIntBound(Ce.sub(n,s))}function fm(r,e,t,n,s){let o=s[e];if(!o)return!1;let i=fC(r,t,n);return i===null?!1:o(i)}function Mo(r,e){if(e==null)return!1;if(e.type==="IntImmNode")return e.value!==0;if(e.type==="CompareNode")return fm(r,e.direction,e.a,e.b,pA);if(e.type==="MathOpNode"){if(e.op==="*"&&e.b)return Mo(r,e.a)&&Mo(r,e.b);if(Fw.has(e.op))return fm(r,mA[e.op],e.a,e.b,pA)}return!1}function ua(r,e){if(e==null)return!1;if(e.type==="IntImmNode")return e.value===0;if(e.type==="CompareNode")return fm(r,e.direction,e.a,e.b,fA);if(e.type==="MathOpNode"){if(e.op==="*"&&e.b)return ua(r,e.a)||ua(r,e.b);if(Fw.has(e.op))return fm(r,mA[e.op],e.a,e.b,fA)}return!1}function hA(r){return!r||r.type!=="MathOpNode"||r.op!=="*"?null:r.b&&r.b.type==="IntImmNode"&&r.b.value>0?{factor:r.a,c:r.b.value}:r.a&&r.a.type==="IntImmNode"&&r.a.value>0?{factor:r.b,c:r.a.value}:null}function dA(r,e,t,n){let s=la(r,e);return s===null?!1:s.min>=t&&s.max<=n}var pC,Fw,mA,pA,fA,hm,Yc=M(()=>{"use strict";Ku();Vs();De();pC={"+":"add","-":"sub","*":"mul","//":"div","%":"mod"},Fw=new Set(["<","<=",">",">=","==","!="]),mA={"<":"lt","<=":"le",">":"gt",">=":"ge","==":"eq","!=":"ne"};l(pi,"irToSymInt");l(la,"irBound");l(fC,"diffBound");pA={lt:l(r=>r.max<0,"lt"),le:l(r=>r.max<=0,"le"),gt:l(r=>r.min>0,"gt"),ge:l(r=>r.min>=0,"ge"),eq:l(r=>r.min===0&&r.max===0,"eq"),ne:l(r=>r.min>0||r.max<0,"ne")},fA={lt:l(r=>r.min>=0,"lt"),le:l(r=>r.min>0,"le"),gt:l(r=>r.max<=0,"gt"),ge:l(r=>r.max<0,"ge"),eq:l(r=>r.min>0||r.max<0,"eq"),ne:l(r=>r.min===0&&r.max===0,"ne")};l(fm,"proveCompare");l(Mo,"proveTrue");l(ua,"proveFalse");l(hA,"asScaledVar");l(dA,"boundWithin");hm=class{static{l(this,"RewriteSimplify")}constructor(e=new Os){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),n=this.simplify(e.b);return Mo(this.analyzer,new qe(e.direction,t,n))?new L(1):ua(this.analyzer,new qe(e.direction,t,n))?new L(0):t===e.a&&n===e.b?e:new qe(e.direction,t,n)}_simplifyMathOp(e){if(e.b===null||e.b===void 0){let o=this.simplify(e.a);return o===e.a?e:new C(e.op,o)}let t=this.simplify(e.a),n=this.simplify(e.b),s=In(e.op,t,n);if(!s||s.type!=="MathOpNode")return s;if(s.op==="//"&&s.b.type==="IntImmNode"&&s.b.value>0){let o=s.b.value,i=hA(s.a);if(i&&i.c===o)return i.factor;if(dA(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=hA(s.a);if(i&&i.c===o)return new L(0);if(dA(this.analyzer,s.a,0,o-1))return s.a}if(Fw.has(s.op)){if(Mo(this.analyzer,s))return new L(1);if(ua(this.analyzer,s))return new L(0)}return s}}});function gA(r,e,t){if(typeof t!="number"||t<0)return"unknown";let n=la(r,e);return n===null?"unknown":n.min>=0&&n.max<=t-1?"in":n.min>t-1||n.max<0?"oob":"unknown"}function yt(r,e){if(!(!r||typeof r!="object"))switch(r.type){case"VariableNode":r.name&&e.add(r.name);return;case"BufferLoadNode":if(r.indices)for(let t of r.indices)yt(t,e);return;case"BufferStoreNode":if(r.indices)for(let t of r.indices)yt(t,e);yt(r.value,e);return;case"MathOpNode":yt(r.a,e),r.b&&yt(r.b,e);return;case"CompareNode":yt(r.a,e),yt(r.b,e);return;case"CastNode":yt(r.expr,e);return;case"CallExternNode":for(let t of r.args)yt(t,e);return;case"IfThenElseNode":yt(r.condition,e),yt(r.thenBody,e),r.elseBody&&yt(r.elseBody,e);return;case"SeqNode":for(let t of r.stmts)yt(t,e);return;case"LetStmtNode":yt(r.value,e),yt(r.body,e);return;default:return}}function dm(r,e){let t=new Set((r.writes||[]).map(s=>s.buffer&&s.buffer.name)),n=[r.body,r.initBody];for(;n.length>0;){let s=n.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)yt(o,e);if(s.type==="BufferStoreNode"){s.value&&n.push(s.value);continue}if(s.type==="SeqNode"){for(let o of s.stmts)n.push(o);continue}if(s.type==="IfThenElseNode"){n.push(s.thenBody),s.elseBody&&n.push(s.elseBody);continue}if(s.type==="LetStmtNode"){n.push(s.body);continue}}}}function hC(r,e){let t=new Set;if(yt(e.body,t),e.initBody&&yt(e.initBody,t),!t.has(r))return!1;let n=new Set;return dm(e,n),!n.has(r)}function ca(r,e){if(!(!r||typeof r!="object"))switch(r.type){case"BlockNode":e.push(r),ca(r.body,e),r.initBody&&ca(r.initBody,e);return;case"ForNode":case"AllocateNode":case"LetStmtNode":ca(r.body,e);return;case"SeqNode":for(let t of r.stmts)ca(t,e);return;case"IfThenElseNode":ca(r.thenBody,e),r.elseBody&&ca(r.elseBody,e);return;default:return}}function zw(r){let e=r.loopVar.name,t=[];ca(r.body,t);for(let n of t)if(hC(e,n))return n.name;return null}var Vw=M(()=>{"use strict";Ku();Vs();Yc();l(gA,"classifyBufferIndex");l(yt,"collectVarsUsed");l(dm,"collectWriteIndexVars");l(hC,"loopVarIsReductionOf");l(ca,"collectBlocksUnder");l(zw,"loopCarriesReduction")});var Wu,Gw=M(()=>{"use strict";De();Vw();Ku();Yc();Wu=class r{static{l(this,"ScheduleValidator")}static validate(e){let t=[],n={boundVars:new Set,threadBindings:new Map,parallelExtents:new Map,parLoops:[],innermostLoopVar:null,loopStack:[],analyzer:new Os,condDepth:0,errors:t};for(let s of e.params)n.boundVars.add(s.name);return r._visitNode(e.body,n),r._checkPartitionConsistency(n),t}static _checkPartitionConsistency(e){if(e.parallelExtents.size<=1)return;let t=[...e.parallelExtents.entries()].map(([n,s])=>`'${s}'(extent ${n})`);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":r._visitFor(e,t);break;case"BlockNode":r._visitBlock(e,t);break;case"SeqNode":for(let n of e.stmts)r._visitNode(n,t);break;case"IfThenElseNode":r._visitExpr(e.condition,t),t.condDepth++,r._visitNode(e.thenBody,t),e.elseBody&&r._visitNode(e.elseBody,t),t.condDepth--;break;case"AllocateNode":r._visitNode(e.body,t);break;case"LetStmtNode":t.boundVars.add(e.variable.name),r._visitNode(e.body,t),t.boundVars.delete(e.variable.name);break;case"BufferStoreNode":r._validateBufferAccess(e,t);break;case"EvaluateNode":break;default:break}}static _visitFor(e,t){let n=e.loopVar.name;if(t.boundVars.has(n)&&t.errors.push(`Duplicate loop variable: ${n}`),e.kind===F.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,n)):t.errors.push(`Thread-bound loop '${n}' missing threadTag`)),e.kind===F.VECTORIZED){let a=e.extent;a.type==="IntImmNode"&&a.value<=0&&t.errors.push(`Vectorized loop '${n}' has non-positive extent ${a.value}`)}if(e.kind===F.PARALLEL){let a=e.extent;a&&a.type==="IntImmNode"&&!t.parallelExtents.has(a.value)&&t.parallelExtents.set(a.value,n)}(e.kind===F.PARALLEL||e.kind===F.THREAD_BINDING)&&r._checkNoNestedParallel(e.body,n,t);let s=e.kind===F.PARALLEL||e.kind===F.VECTORIZED;s&&t.parLoops.push({varName:n,kind:e.kind}),t.boundVars.add(n);let o=t.analyzer.getVarBound(n);e.extent&&e.extent.type==="IntImmNode"&&e.extent.value>0?t.analyzer.bind(n,0,e.extent.value-1):t.analyzer.setVarBound(n,null);let i=t.innermostLoopVar;t.innermostLoopVar=n,t.loopStack.push(n),r._visitNode(e.body,t),t.loopStack.pop(),t.innermostLoopVar=i,t.boundVars.delete(n),t.analyzer.setVarBound(n,o),s&&t.parLoops.pop(),e.kind===F.THREAD_BINDING&&e.threadTag&&t.threadBindings.delete(e.threadTag)}static _checkNoNestedParallel(e,t,n){e&&e.type==="ForNode"&&e.kind===F.PARALLEL&&n.errors.push(`Parallel loop '${e.loopVar.name}' nested inside parallel/thread-bound loop '${t}'`)}static _visitBlock(e,t){let n=[];for(let s of e.iterVars)s.iterVar&&(t.boundVars.add(s.iterVar.name),n.push([s.iterVar.name,t.analyzer.getVarBound(s.iterVar.name)]),t.analyzer.setVarBound(s.iterVar.name,s.binding?la(t.analyzer,s.binding):null));if(e.initBody&&t.innermostLoopVar){let s=new Set;dm(e,s);let o=new Set;yt(e.body,o),yt(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;yt(e.body,s),e.initBody&&yt(e.initBody,s);let o=new Set;dm(e,o);for(let i of t.parLoops)if(s.has(i.varName)&&!o.has(i.varName)){let a=i.kind===F.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`)}}r._visitNode(e.body,t),e.initBody&&r._visitNode(e.initBody,t);for(let s of e.iterVars)s.iterVar&&t.boundVars.delete(s.iterVar.name);for(let[s,o]of n)t.analyzer.setVarBound(s,o)}static _validateBufferAccess(e,t){if(e.buffer?(r._checkRank(e,t),r._checkBounds(e,t)):t.errors.push("BufferStore with null buffer"),e.indices)for(let n of e.indices)r._visitExpr(n,t);e.value&&r._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 n=0;n<e.indices.length;n++){let s=e.buffer.shape[n];gA(t.analyzer,e.indices[n],s)==="oob"&&t.errors.push(`Buffer '${e.buffer.name}' access is out of bounds on axis ${n}: index is provably outside [0, ${s-1}]`)}}static _visitExpr(e,t){if(!(!e||typeof e!="object"))switch(e.type){case"BufferLoadNode":if(e.buffer?(r._checkRank(e,t),r._checkBounds(e,t)):t.errors.push("BufferLoad with null buffer"),e.indices)for(let n of e.indices)r._visitExpr(n,t);break;case"BufferStoreNode":r._validateBufferAccess(e,t);break;case"MathOpNode":case"CompareNode":r._visitExpr(e.a,t),r._visitExpr(e.b,t);break;case"CastNode":r._visitExpr(e.expr,t);break;case"CallExternNode":for(let n of e.args)r._visitExpr(n,t);break;case"IfThenElseNode":r._visitExpr(e.condition,t),t.condDepth++,r._visitNode(e.thenBody,t),e.elseBody&&r._visitNode(e.elseBody,t),t.condDepth--;break}}}});var jw,Hu,qw=M(()=>{"use strict";De();jw=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 n=t.pop();if(n.isBlock)e.push(n);else for(let s of n.children)t.push(s)}return e}childLoops(){return this.children.filter(e=>e.isLoop)}},Hu=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 n=t.pop();this._nodeToSRef.delete(n.node),n.isLoop?this._loopSRefs.delete(n):n.isBlock&&(this._blockSRefs.delete(n),this._blockNameToSRef.get(n.node.name)===n&&this._blockNameToSRef.delete(n.node.name));for(let s of n.children)t.push(s)}}_buildSubtree(e,t){let n=[],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 jw(o,i);this._register(u),a?n.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 n}_build(e,t){let n=this._buildSubtree(e,t);if(t)for(let s of n)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(n=>n.node.kind!==F.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 n=this._nodeToSRef.get(e);if(!n)return!1;let s=n.parent,o=this.root===n;this._unregisterSubtree(n);let i=this._buildSubtree(t,s);if(s){let a=s.children.indexOf(n);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 n=t.parent,s=this.root===t;if(this._unregisterSubtree(t),n){let o=n.children.indexOf(t);o>=0&&n.children.splice(o,1)}else s&&(this.root=null);return!0}}});var Uw,Kw,mm,yA=M(()=>{"use strict";qw();De();Uw=class{static{l(this,"LoopBinding")}constructor(e,t,n,s,o){this.loopSRef=e,this.iterVar=t,this.extent=n,this.kind=s,this.threadTag=o}},Kw=class{static{l(this,"BlockBinding")}constructor(e,t,n,s){this.blockSRef=e,this.iterVars=t,this.readBuffers=n,this.writeBuffers=s}},mm=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 Hu(this.primFunc),this._loopBindings=new Map,this._blockBindings=new Map;for(let e of this._tree.allLoops()){let t=e.node,n=t.extent&&t.extent.type==="IntImmNode"?t.extent.value:null;this._loopBindings.set(t.loopVar.name,new Uw(e,t.loopVar.name,n,t.kind,t.threadTag))}for(let e of this._tree.allBlocks()){let t=e.node,n=[];for(let i of t.iterVars)i.iterVar&&n.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 Kw(e,n,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===F.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 gm,_A=M(()=>{"use strict";Gr();gm=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 n=t.stmts.indexOf(e);if(n>=0){t.stmts.splice(n,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,n){e&&Ss(e,s=>{s.type==="BufferLoadNode"&&s.buffer===t&&(s.buffer=n)})}redirectBuffer(e,t,n){e&&Ss(e,s=>{(s.type==="BufferLoadNode"||s.type==="BufferStoreNode")&&s.buffer===t&&(s.buffer=n)})}}});function ym(r,e,t){if(!r||typeof r!="object")return r;if(Array.isArray(r))return r.map(e);let n=Object.create(Object.getPrototypeOf(r));switch(n.type=r.type,n._parent=null,n._parentKey=null,n._parentIdx=-1,r.type){case"ForNode":return n.loopVar=r.loopVar,n.min=e(r.min),n.extent=e(r.extent),n.kind=r.kind,n.body=e(r.body),n.threadTag=r.threadTag,n._setChild("body",n.body),n;case"BlockNode":return n.name=r.name,n.iterVars=r.iterVars.map(e),n.reads=r.reads,n.writes=r.writes,n.body=e(r.body),n.initBody=r.initBody?e(r.initBody):null,n._setChild("body",n.body),n._setChild("initBody",n.initBody),n;case"SeqNode":return n.stmts=r.stmts.map(e),n._setChildren("stmts",n.stmts),n;case"IfThenElseNode":return n.condition=e(r.condition),n.thenBody=e(r.thenBody),n.elseBody=r.elseBody?e(r.elseBody):null,n._setChild("thenBody",n.thenBody),n._setChild("elseBody",n.elseBody),n;case"BufferStoreNode":return n.buffer=r.buffer,n.indices=r.indices.map(e),n.value=e(r.value),n;case"BufferLoadNode":return n.buffer=r.buffer,n.indices=r.indices.map(e),n;case"MathOpNode":return n.op=r.op,n.a=e(r.a),n.b=e(r.b),n;case"CompareNode":return n.direction=r.direction,n.a=e(r.a),n.b=e(r.b),n;case"CastNode":return n.expr=e(r.expr),n.fromDtype=r.fromDtype,n.toDtype=r.toDtype,n;case"CallExternNode":return n.externName=r.externName,n.args=r.args.map(e),n.dtype=r.dtype,n;default:return t(r,n,e)}}var Ww=M(()=>{"use strict";l(ym,"cloneIRShared")});function fi(r,e,t){return ti(r,n=>n.type==="VariableNode"&&n.name===e?t():n,{bindVars:!1})}function mC(r,e,t,n){return ti(r,s=>s.type==="BufferLoadNode"&&s.buffer&&s.buffer.name===e?(n.n++,t(s)):s,{bindVars:!1})}function Hw(r,e){return Sc(r,t=>t.type==="BufferLoadNode"&&t.buffer&&t.buffer.name===e)}function Ju(r,e){if(!r||typeof r!="object")return!1;if((r.type==="BufferLoadNode"||r.type==="BufferStoreNode")&&r.indices){for(let t of r.indices)if(Hw(t,e))return!0}for(let t of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])if(r[t]&&Ju(r[t],e))return!0;if(r.args){for(let t of r.args)if(Ju(t,e))return!0}if(r.indices){for(let t of r.indices)if(Ju(t,e))return!0}if(r.stmts){for(let t of r.stmts)if(Ju(t,e))return!0}return!1}function Wt(r){return ym(r,Wt,(e,t,n)=>{if(e.type==="BlockRealizeNode")return t.iterVar=e.iterVar,t.binding=n(e.binding),t;for(let s of Object.keys(e))s==="_parent"||s==="_parentKey"||s==="_parentIdx"||(t[s]=e[s]);return t})}function _m(r,e){if(r){if(r.type==="BufferStoreNode"){e.push(r);return}if(r.type==="SeqNode"){for(let t of r.stmts)_m(t,e);return}(r.type==="ForNode"||r.type==="BlockNode")&&_m(r.body,e)}}function bA(r){return r.indices.map(e=>e&&e.type==="VariableNode"?e.name:null)}function wA(r,e,t){let n=e.indices.map(i=>i.name),s=n.map((i,a)=>`__inl_${t}_${a}`),o={n:0};return mC(r,e.buffer.name,i=>{let a=Wt(e.value);for(let u=0;u<n.length;u++)a=fi(a,n[u],()=>new Ne(s[u],"int32"));for(let u=0;u<n.length;u++)a=fi(a,s[u],()=>Wt(i.indices[u]));return a},o),o.n}function TA(r,e){let t=[r];for(;t.length>0;){let n=t.pop();if(!(!n||typeof n!="object")){if(n.type==="BlockNode"&&n.reads){let s=n.reads.filter(o=>!(o.buffer&&o.buffer.name===e));s.length!==n.reads.length&&(n.reads=s)}if(n.body&&t.push(n.body),n.stmts)for(let s of n.stmts)t.push(s);n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody),n.initBody&&t.push(n.initBody)}}}function pa(r){return r.type==="IntImmNode"?r.value:null}function Yu(r,e="int32"){return new Ne(`${r}_${gC++}`,e)}function Jc(r,e,t){if(!(!r||typeof r!="object")){r.type==="BufferLoadNode"&&r.buffer&&e.set(r.buffer.name,r.buffer),r.type==="BufferStoreNode"&&r.buffer&&t.set(r.buffer.name,r.buffer);for(let n of["a","b","value","expr","condition","thenBody","elseBody","body","initBody"])r[n]&&Jc(r[n],e,t);if(r.indices)for(let n of r.indices)Jc(n,e,t);if(r.args)for(let n of r.args)Jc(n,e,t);if(r.stmts)for(let n of r.stmts)Jc(n,e,t)}}var dC,gC,Sn,fa=M(()=>{"use strict";De();xo();iA();Gw();yA();_A();qw();Vw();Ww();Gr();dC=new Set(["+","*","min","max"]);l(fi,"substituteVar");l(mC,"replaceBufferLoads");l(Hw,"loadsBuffer");l(Ju,"usesBufferInIndex");l(Wt,"cloneExprTree");l(_m,"collectStores");l(bA,"storeIndexNames");l(wA,"inlineStoreValue");l(TA,"dropBufferReads");l(pa,"getConstExtent");gC=0;l(Yu,"freshVar");Sn=class{static{l(this,"Schedule")}constructor(e){this.func=e,this.trace=new pm,this.state=new mm(e),this._replaying=!1,this._srefTree=new Hu(e),this.mutator=new gm(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,n=l(s=>{if(!(!s||typeof s!="object"||t)){if(s.type==="ForNode"&&s.loopVar&&s.loopVar.name===e){t=s;return}if(s.body&&n(s.body),s.initBody&&n(s.initBody),s.stmts)for(let o of s.stmts)n(o);s.thenBody&&n(s.thenBody),s.elseBody&&n(s.elseBody)}},"walk");return n(this.func.body),t||e}split(e,t){e=this._resolveLoop(e);let n=pa(e.extent);if(n===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(n/t),o=Yu(`${e.loopVar.name}_o`),i=Yu(`${e.loopVar.name}_i`),a=e.loopVar.name,u=Wt(e.body),p=new de(i,new L(0),new L(t),e.kind,u,e.threadTag);if(n%t!==0){let f=new C("+",new C("*",o,new L(t)),i),d=new C("<",f,new L(n)),m=new xe(d,p.body);p.body=m,p._setChild("body",m)}let h=new de(o,new L(0),new L(s),e.kind,p,e.threadTag);return fi(p.body,a,()=>new C("+",new C("*",o,new L(t)),i)),this.mutator.replaceNode(e,h),this._replaceInTree(e,h),this._replaying||this.trace.record("split",[e.loopVar.name,t]),[h,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 n=null,s=1/0,o=new Map,i=l((c,h)=>{if(c){if(c.type==="ForNode"){t.has(c)&&(o.set(c,h),h<s&&(s=h,n=c)),i(c.body,h+1);return}if(c.type==="SeqNode")for(let f of c.stmts)i(f,h);else c.type==="IfThenElseNode"?i(c.thenBody,h):(c.type==="BlockNode"||c.type==="AllocateNode"||c.type==="LetStmtNode")&&i(c.body,h)}},"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,n);this.mutator.replaceNode(n,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 h=c<e.length-1?e[c+1]:p;e[c].body=h,e[c]._setChild("body",h)}this._replaceInTree(n,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 n=new Set(e),s=[],o=t,i=!1,a=null;for(;o;)if(o.type==="ForNode"){if(e.has(o)){if(n.delete(o),i=!0,n.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(n.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 n=pa(e.extent),s=pa(t.extent);if(n===null||s===null)throw new Error("Cannot fuse loops with non-constant extents");let o=n*s,i=Yu(`${e.loopVar.name}_${t.loopVar.name}_fused`),a=e.loopVar.name,u=t.loopVar.name,p=new de(i,new L(0),new L(o),e.kind,t.body);return fi(p.body,a,()=>new C("//",i,new L(s))),fi(p.body,u,()=>new C("%",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,n){if(t.length!==n.length)throw new Error("tile: loopIndices and tileSizes must have same length");let s=this.getLoops(e),o=t.map(h=>{if(h>=s.length)throw new Error(`tile: loop index ${h} out of range`);return s[h]}),i=[],a=[];for(let h=0;h<o.length;h++){let d=this.getLoops(e).find(y=>y.loopVar.name===o[h].loopVar.name||y===o[h]);if(!d)throw new Error(`tile: lost track of loop at index ${h}`);let[m,g]=this.split(d,n[h]);i.push(m),a.push(g)}let u=this.getLoops(e),p=[],c=[];for(let h of u)i.some(f=>f.loopVar.name===h.loopVar.name)?p.push(h):a.some(f=>f.loopVar.name===h.loopVar.name)&&c.push(h);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(pa(e.extent)===null)throw new Error("Cannot vectorize loop with non-constant extent");let n=zw(e);if(n!==null)throw new Error(`Cannot vectorize reduction loop '${e.loopVar.name}' (loop-carried dependency in block '${n}')`);e.kind=F.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=F.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=zw(e);if(t!==null)throw new Error(`Cannot parallelize reduction loop '${e.loopVar.name}' (loop-carried dependency in block '${t}')`);e.kind=F.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 n=["blockIdx.x","blockIdx.y","blockIdx.z","threadIdx.x","threadIdx.y","threadIdx.z"];if(!n.includes(t))throw new Error(`Invalid thread tag: ${t}. Must be one of: ${n.join(", ")}`);e.kind=F.THREAD_BINDING,e.threadTag=t,this.state.invalidate(),this._replaying||this.trace.record("bindThread",[e.loopVar.name,t])}rfactor(e,t,n){let s=this.getBlock(e),o=this.getLoops(e),i=o.find(D=>D.loopVar.name===t);if(!i)throw new Error(`rfactor: reduction loop '${t}' not found for block '${e}'`);let a=pa(i.extent);if(a===null)throw new Error(`rfactor: reduction loop '${t}' has non-constant extent`);if(!Number.isInteger(n)||n<=1||n>=a||a%n!==0)throw new Error(`rfactor: factor ${n} 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,h=u.value.op,f=l(D=>D&&D.type==="BufferLoadNode"&&D.buffer===p,"isAccLoad"),d;if(f(u.value.a))d=u.value.b;else if(f(u.value.b))d=u.value.a;else throw new Error(`rfactor: accumulator load not found in block '${e}' body`);if(!dC.has(h))throw new Error(`rfactor: op '${h}' 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(D=>D.loopVar.name!==t),y=a/n,_=new me(`${p.name}_rf`,[n,...p.shape],p.dtype,p.scope),b=Yu(`${t}_rfi`),w=Yu(`${t}_rfo`),T=Yu(`${t}_rfp`),S=l(D=>[D,...c.map(Wt)],"cfIdx"),x=fi(Wt(d),t,()=>new C("+",new C("*",w,new L(n)),b)),k=new Y(_,S(b),new C(h,new q(_,S(b)),x)),A=new Y(_,S(b),Wt(m)),N=new pe(`${e}_rf_p`,[],s.reads.map(D=>({buffer:D.buffer})),[{buffer:_}],k,A),v=new de(w,new L(0),new L(y),F.SERIAL,N);v=new de(b,new L(0),new L(n),F.SERIAL,v);for(let D=g.length-1;D>=0;D--)v=new de(g[D].loopVar,new L(0),Wt(g[D].extent),F.SERIAL,v);let O=new Y(p,c.map(Wt),new C(h,new q(p,c.map(Wt)),new q(_,S(T)))),R=new Y(p,c.map(Wt),Wt(m)),B=new pe(`${e}_rf_c`,[],[{buffer:_}],[{buffer:p}],O,R),E=new de(T,new L(0),new L(n),F.SERIAL,B);for(let D=g.length-1;D>=0;D--)E=new de(g[D].loopVar,new L(0),Wt(g[D].extent),F.SERIAL,E);let z=new te([v,E]);return this.mutator.replaceNode(o[0],z),this._replaceInTree(o[0],z),this._replaying||this.trace.record("rfactor",[e,t,n]),_}decomposeReduction(e){let t=this.getBlock(e);if(!t.initBody)throw new Error(`decomposeReduction: block '${e}' has no initBody`);let n=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)yt(g,i);let a=n.filter(g=>i.has(g.loopVar.name));if(n.filter(g=>!i.has(g.loopVar.name)).length===0)throw new Error(`decomposeReduction: block '${e}' has no reduction loop`);let p=new Y(o,s.indices.map(Wt),Wt(t.initBody.value)),h=new pe(`${e}_init`,[],[],[{buffer:o}],p);for(let g=a.length-1;g>=0;g--)h=new de(a[g].loopVar,new L(0),Wt(a[g].extent),F.SERIAL,h);let d=new pe(`${e}_upd`,[],t.reads.map(g=>({buffer:g.buffer})),[{buffer:o}],Wt(s));for(let g=n.length-1;g>=0;g--)d=new de(n[g].loopVar,new L(0),Wt(n[g].extent),F.SERIAL,d);let m=new te([h,d]);this.mutator.replaceNode(n[0],m),this._replaceInTree(n[0],m),this._replaying||this.trace.record("decomposeReduction",[e])}cacheWrite(e,t,n="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,n);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 Ne(`${u.name}_o${_}`,"int32")),c=new Y(a,p,new q(u,p)),f=new pe(`${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--)f=new de(p[y],new L(0),new L(a.shape[y]),F.SERIAL,f);let d=o[0],m=new te([]),g=new Ve(u,n,m);this.mutator.replaceNode(d,g),m.stmts.push(d,f),this._replaceInTree(d,g),this._replaying||this.trace.record("cacheWrite",[e,t,n])}setScope(e,t,n){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=n,this.state.invalidate(),this._replaying||this.trace.record("setScope",[e,t,n])}storageAlign(e,t,n,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:n,factor:s,offset:o||0},this.state.invalidate(),this._replaying||this.trace.record("storageAlign",[e,t,n,s,o||0])}_removeBlockNest(e,t){let n=this.getLoops(e),s=n.length>0?n[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 n=[];_m(t.body,n);let s=n[0];if(!s)throw new Error("computeInline: producer has no single store to inline");let o=s.buffer;if(bA(s).some(i=>i===null))throw new Error("computeInline: producer indices must be simple loop variables");if(Hw(s.value,o.name))throw new Error("computeInline: producer is self-referential (recurrence), cannot inline");if(Ju(this.func.body,o.name))throw new Error(`computeInline: buffer '${o.name}' is used inside an index expression (indirect), cannot safely inline`);if(wA(this.func.body,s,e)===0)throw new Error(`computeInline: buffer '${o.name}' has no consumers to inline into`);TA(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 n=[];if(_m(t.body,n),n.length===0)throw new Error("computeInlineBlock: producer has no stores to inline");let s=new Set(n.map(i=>i.buffer.name));if(s.size!==n.length)throw new Error("computeInlineBlock: buffer written more than once in block");for(let i of n){if(bA(i).some(a=>a===null))throw new Error("computeInlineBlock: producer indices must be simple loop variables");for(let a of s)if(Hw(i.value,a))throw new Error("computeInlineBlock: producer store depends on a co-produced buffer");if(Ju(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 n)o+=wA(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)TA(this.func.body,i);this._removeBlockNest(e,t),this._replaying||this.trace.record("computeInlineBlock",[e])}_relocateBlockToLoop(e,t,n){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=pa(a.extent),p=pa(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=Wt(a.body);fi(c,a.loopVar.name,()=>o.loopVar);let h=new te([]);this.mutator.replaceNode(a,h);let f=o.body;if(f&&f.type==="SeqNode")n?f.stmts.unshift(c):f.stmts.push(c);else{let d=n?new te([c,f]):new te([f,c]);o.body=d,o._setChild("body",d)}return this._replaceInTree(a,h),this._replaceInTree(o,o),o.loopVar.name}computeAt(e,t){let n=this._relocateBlockToLoop(e,t,!0);this._replaying||this.trace.record("computeAt",[e,n])}reverseComputeAt(e,t){let n=this._relocateBlockToLoop(e,t,!1);this._replaying||this.trace.record("reverseComputeAt",[e,n])}cacheRead(e,t,n="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,n),p=a.shape.map((y,_)=>new Ne(`${u.name}_i${_}`,"int32")),c=new Y(u,p,new q(a,p)),f=new pe(`${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--)f=new de(p[y],new L(0),new L(a.shape[y]),F.SERIAL,f);this.mutator.redirectReads(s.body,a,u),s.initBody&&this.mutator.redirectReads(s.initBody,a,u),i.buffer=u;let d=o[0],m=new te([f]),g=new Ve(u,n,m);this.mutator.replaceNode(d,g),m.stmts.push(d),this._replaceInTree(d,g),this._replaying||this.trace.record("cacheRead",[e,t,n])}fuseConsumer(e,t){let n=this.getBlock(e),s=this.getBlock(t),o=this.getLoops(e),i=this.getLoops(t);if(!n.body||n.body.type!=="BufferStoreNode")throw new Error(`fuseConsumer: producer '${e}' body is not a store`);let a=new Set;for(let g of n.body.indices)yt(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=Wt(p.body),h=Wt(s.body);for(let g=0;g<i.length;g++){let y=u[g].loopVar.name;h=fi(h,i[g].loopVar.name,()=>new Ne(y,"int32"))}let f=new pe(`${t}_fused`,[],s.reads.map(g=>({buffer:g.buffer})),s.writes.map(g=>({buffer:g.buffer})),h),d=new te([c,f]);for(let g=u.length-1;g>=0;g--){let y=u[g];d=new de(y.loopVar,new L(0),Wt(y.extent),y.kind,d,y.threadTag)}let m=i[0];this.mutator.replaceNode(o[0],d),this.mutator.removeNode(m),this._replaceInTree(o[0],d),this._removeFromTree(m),this._replaying||this.trace.record("fuseConsumer",[e,t])}annotate(e,t,n){if(e=this._resolveLoop(e),e.type!=="ForNode")throw new Error("annotate expects ForNode");e.annotations||(e.annotations={}),e.annotations[t]=n,this.state.invalidate(),this._replaying||this.trace.record("annotate",[e.loopVar.name,t,n])}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 n=this._resolveLoop(e);if(!n||n.type!=="ForNode")throw new Error("blockize expects a loop");let s=new Map,o=new Map;Jc(n,s,o);let i=t||`blockized_${n.loopVar.name}`,a=new pe(i,[],[...s.values()].map(u=>({buffer:u})),[...o.values()].map(u=>({buffer:u})),new te([]));return this.mutator.replaceNode(n,a),a.body=n,a._setChild("body",n),this._replaceInTree(n,a),this._replaying||this.trace.record("blockize",[n.loopVar.name]),a}getTrace(){return this.trace}verify(){return Wu.validate(this.func)}};l(Jc,"collectBufferAccess")});function dt(r,e){let t=bm.get(r);return t&&t.has(e)?t.get(e):(t||(t=new Map,bm.set(r,t)),_C(r.body,t,[]),t.has(e)?t.get(e):null)}function s0(r){r&&bm.delete(r)}function yC(r,e){let t=bm.get(r);t&&t.delete(e)}function xA(r,e){let t=[r];for(;t.length>0;){let n=t.pop();if(!(!n||typeof n!="object")){if(n.type==="VariableNode"){e.add(n.name);continue}for(let s of["a","b","condition","thenBody","elseBody","expr","value","offsetExpr","extent"])n[s]&&t.push(n[s]);if(n.indices)for(let s of n.indices)t.push(s);if(n.args)for(let s of n.args)t.push(s)}}}function SA(r){let e=new Set((r.writes||[]).map(o=>o.buffer&&o.buffer.name)),t=new Set,n=[r.body,r.initBody];for(;n.length>0;){let o=n.pop();if(o){if(o.type==="BufferStoreNode"&&o.buffer&&e.has(o.buffer.name))for(let i of o.indices)xA(i,t);if(o.body&&n.push(o.body),o.stmts)for(let i of o.stmts)n.push(i);o.thenBody&&n.push(o.thenBody),o.elseBody&&n.push(o.elseBody),o.value&&n.push(o.value)}}let s=new Set;for(let o of r.iterVars||[]){let i=new Set;if(xA(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 _C(r,e,t){let n=[{node:r,loops:[...t]}];for(;n.length>0;){let{node:s,loops:o}=n.pop();if(s)if(s.type==="ForNode")n.push({node:s.body,loops:[...o,s]});else if(s.type==="BlockNode"){if(!e.has(s.name)){let i=SA(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]})}n.push({node:s.body,loops:o})}else if(s.type==="SeqNode")for(let i=s.stmts.length-1;i>=0;i--)n.push({node:s.stmts[i],loops:o});else s.type==="IfThenElseNode"?(s.elseBody&&n.push({node:s.elseBody,loops:o}),n.push({node:s.thenBody,loops:o})):s.type==="AllocateNode"?n.push({node:s.body,loops:o}):s.type==="LetStmtNode"&&n.push({node:s.body,loops:o})}}function kA(r){return r.hasReduction&&r.readBuffers.length===2&&r.writeBuffers.length===1&&r.loopCount>=3}function bC(r,e){let t=dt(r,e);return t?t.loops.some(n=>n.extent&&n.extent.type!=="IntImmNode"):!1}function vA(r){let e=0,t=[r.body];for(;t.length>0;){let n=t.pop();if(n){if(n.type==="BlockNode"&&(e++,e>1))return!0;if(n.type==="ForNode"&&t.push(n.body),n.type==="SeqNode")for(let s of n.stmts)t.push(s)}}return!1}function o0(r){return Sc(r.body,e=>e.type==="BlockNode"&&(e.initBody!==null||SA(e).size>0),{kinds:"stmt"})}function i0(r){return Sc(r.body,e=>e.type==="ForNode"&&e.kind===F.RECURRENCE,{kinds:"stmt"})}function a0(r,e,t){let n=Math.min(t.maxThreadsPerBlock,256),s=e.extent;if(s.type==="IntImmNode"&&s.value>n){let[o,i]=r.split(e,n);r.bindThread(i,"threadIdx.x"),i0(r.func)||r.bindThread(o,"blockIdx.x")}else r.bindThread(e,"threadIdx.x")}function Zu(r,e){return e.reductionLoopVars?e.reductionLoopVars.has(r.loopVar.name):!1}function u0(r,e){return r.body===e}function wC(r){return gu(r,e=>e.type==="BlockNode",{kinds:"stmt"}).map(e=>e.name)}var Bs,bm,Yw,Jw,Zw,Qw,Xw,e0,t0,n0,r0,Rs,ro=M(()=>{"use strict";Ks();De();Gr();Bs=class{static{l(this,"ScheduleRule")}constructor(e){this.name=e}matches(e,t,n){throw new Error("ScheduleRule.matches must be implemented")}apply(e,t,n){throw new Error("ScheduleRule.apply must be implemented")}},bm=new WeakMap;l(dt,"classifyBlock");l(s0,"invalidateClassifyCache");l(yC,"invalidateClassifyBlock");l(xA,"collectVarNames");l(SA,"computeReductionLoopVars");l(_C,"collectBlockInfo");l(kA,"isMatmulShape");l(bC,"blockHasNonConstExtent");l(vA,"hasMultipleBlocks");Yw=class extends Bs{static{l(this,"ElementwiseCPURule")}constructor(){super("elementwise_cpu")}matches(e,t,n){if(n.kind!==Ot.CPU)return!1;let s=dt(e,t);if(!s||s.hasReduction||s.loopCount<1||s.loops.length>0&&vA(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>=n.numCores*n.vectorWidth}apply(e,t,n){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>=n.vectorWidth*2){let[u,p]=e.split(s[0],n.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>=n.vectorWidth&&i.value%n.vectorWidth===0){let[a,u]=e.split(o,n.vectorWidth);e.vectorize(u)}}};l(o0,"primFuncHasReduction");l(i0,"primFuncHasRecurrence");l(a0,"bindFusedSpatialGPU");Jw=class extends Bs{static{l(this,"ElementwiseGPURule")}constructor(){super("elementwise_gpu")}matches(e,t,n){if(!n.isGPU())return!1;let s=dt(e,t);return s?!s.hasReduction&&s.loopCount>=1:!1}apply(e,t,n){let s=e.getLoops(t);if(s.length===0)return;let o=s[0];for(let p=1;p<s.length;p++){let h=e.getLoops(t).find(f=>f.loopVar.name===s[p].loopVar.name);h&&u0(o,h)&&(o=e.fuseLoops(o,h))}let i=o.extent;if(i.type!=="IntImmNode"){e.bindThread(o,"threadIdx.x");return}let a=i.value,u=Math.min(n.maxThreadsPerBlock,1024);if(o0(e.func)&&a<=u){e.bindThread(o,"threadIdx.x");return}a0(e,o,n)}},Zw=class extends Bs{static{l(this,"ReductionCPURule")}constructor(){super("reduction_cpu")}matches(e,t,n){if(n.kind!==Ot.CPU)return!1;let s=dt(e,t);return s?s.hasReduction:!1}apply(e,t,n){let s=e.getLoops(t);if(s.length===0)return;let o=[],i=[],a=dt(e.func,t);for(let u of s)a&&Zu(u,a)?i.push(u):o.push(u);o.length>0&&e.parallelize(o[0])}},Qw=class extends Bs{static{l(this,"ReductionGPURule")}constructor(){super("reduction_gpu")}matches(e,t,n){if(!n.isGPU())return!1;let s=dt(e,t);return s?s.hasReduction:!1}apply(e,t,n){let s=e.getLoops(t);if(s.length===0)return;let o=[],i=dt(e.func,t);for(let u of s)(!i||!Zu(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(h=>h.loopVar.name===o[u].loopVar.name);c&&u0(a,c)&&(a=e.fuseLoops(a,c))}a0(e,a,n)}},Xw=class extends Bs{static{l(this,"MatmulTiledCPURule")}constructor(){super("matmul_tiled_cpu")}matches(e,t,n){if(n.kind!==Ot.CPU)return!1;let s=dt(e,t);if(!s||!kA(s))return!1;let o=n.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,n){let s=e.getLoops(t);if(s.length<3)return;let o=n.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 h=s[c].extent.type==="IntImmNode"?s[c].extent.value:null;h&&h>=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])}},e0=class extends Bs{static{l(this,"MatmulTiledGPURule")}constructor(){super("matmul_tiled_gpu")}matches(e,t,n){if(!n.isGPU())return!1;let s=dt(e,t);if(!s||!kA(s))return!1;let o=n.sharedMemoryBytes||49152,a=Math.max(16,Math.min(128,Math.floor(Math.sqrt(o/8))));return s.loops.reduce((p,c)=>{let h=c.extent&&c.extent.type==="IntImmNode"?c.extent.value:0;return h>p?h:p},0)>=a}apply(e,t,n){let s=e.getLoops(t);if(s.length<3)return;let o=dt(e.func,t),i=s.filter(u=>!o||!Zu(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(h=>h.loopVar.name===i[u].loopVar.name);c&&u0(a,c)&&(a=e.fuseLoops(a,c))}a0(e,a,n)}},t0=class extends Bs{static{l(this,"ElementwiseWasmRule")}constructor(){super("elementwise_wasm")}matches(e,t,n){if(n.kind!==Ot.WASM)return!1;let s=dt(e,t);return!(!s||s.hasReduction||s.loopCount<1||s.loops.length>0&&vA(s.loops[0]))}apply(e,t,n){let s=e.getLoops(t);if(s.length===0)return;let o=n.numCores||1,i=n.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&&n.supportsSimd&&n.supportsSimd()&&c>=i*2&&c%i===0){let[h,f]=e.split(s[0],i);e.parallelize(h),e.vectorize(f);return}if(e.parallelize(s[0]),s.length>1){let h=s[s.length-1],f=h.extent;if(f.type==="IntImmNode"&&f.value>=i&&f.value%i===0){let[,d]=e.split(h,i);e.vectorize(d)}}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)}},n0=class extends Bs{static{l(this,"ReductionWasmRule")}constructor(){super("reduction_wasm")}matches(e,t,n){if(n.kind!==Ot.WASM)return!1;let s=n.numCores>1,o=n.supportsSimd&&n.supportsSimd();if(!s&&!o)return!1;let i=dt(e,t);return i?i.hasReduction&&i.loopCount>=2:!1}apply(e,t,n){let s=e.getLoops(t);if(s.length<2)return;let o=dt(e.func,t),i=[],a=[];for(let u of s)!o||!Zu(u,o)?i.push(u):a.push(u);if(i.length>0){let u=i[0].extent;(u&&u.type==="IntImmNode"?u.value:0)>=(n.numCores||1)*4&&e.parallelize(i[0])}if(n.supportsSimd&&n.supportsSimd()&&a.length>0){let u=a[a.length-1];(u.extent&&u.extent.type==="IntImmNode"?u.extent.value:0)>=n.vectorWidth*2&&e.vectorize(u)}}},r0=class extends Bs{static{l(this,"FallbackRule")}constructor(){super("fallback")}matches(){return!0}apply(e,t,n){let s=e.getLoops(t);s.length!==0&&n.isCPU()&&s.length>=1&&e.parallelize(s[0])}};l(Zu,"isReductionLoop");l(u0,"findDirectChild");Rs=class r{static{l(this,"SchedulePolicy")}constructor(e,t=null,n=null){this.target=e,this.rules=t||r.defaultRules(),this.trace=n}static defaultRules(){return[new Xw,new e0,new Zw,new Qw,new n0,new Yw,new Jw,new t0,new r0]}selectRule(e,t){for(let n of this.rules)if(n.matches(e,t,this.target))return n;return null}applyToBlock(e,t){if(this.target.isGPU()&&bC(e.func,t))return this._explain(t,"none","block has dynamic loop extents; runs sequentially (no dynamic grid)"),null;let n=this.selectRule(e.func,t);return n?(n.apply(e,t,this.target),yC(e.func,t),this._explain(t,n.name,`matched rule '${n.name}' for ${this.target.name}`),n.name):(this._explain(t,"none","no schedule rule matched; runs sequentially"),null)}_explain(e,t,n){this.trace&&this.trace.explainsEnabled&&this.trace.explain("schedule",e,t,n,{target:this.target.name})}applyToAllBlocks(e){s0(e.func);let t=wC(e.func.body),n=new Set,s=new Map;for(let o of t){if(n.has(o))continue;n.add(o);let i=this.applyToBlock(e,o);i&&s.set(o,i)}return s}};l(wC,"collectAllBlockNames")});function wm(r,e,t,n){if(TC[e])return xC(r,e,t,n);let s=kC[e];return s?s(r,t,n||{}):typeof r[e]=="function"?SC(r,e,t,n):r._inferAndBuild(e,t,n||null)}function xC(r,e,t,n){let s=t[0].type.rank,o=n?.dim,i=o!=null?(Array.isArray(o)?o:[o]).map(h=>h<0?s+h:h):Array.from({length:s},(h,f)=>f),a=r.scalarConstant(FS(e,t[0].type.dtype),t[0].type.dtype),u=r.reduce(t[0],a.getResult(0),i,e);if(!n?.keepdim)return u;let p=new Set(i),c=t[0].type.shape.map((h,f)=>p.has(f)?1:h);return r.reshape(u.getResult(0),c)}function SC(r,e,t,n){return t.length===1?Le(r,e,t[0]):t.length===2?Le(r,e,t[0],t[1]):t.length===3?Le(r,e,t[0],t[1],t[2]):r._inferAndBuild(e,t,n||null)}function Le(r,e,...t){return r[e].call(r,...t)}function Io(r,e){return r<0?e+r:r}function vC(r,e){let t=1,n=-1,s=1;for(let i of e)s*=i;let o=[...r];for(let i=0;i<o.length;i++)o[i]===-1?n=i:t*=o[i];return n>=0&&(o[n]=t===0?0:s/t),o}function NA(r,e){return r<0?e+1+r:r}function NC(r,e,t,n,s){let o=r.length,i=Io(e,o),a=r[i],u=t??0,p=n??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 h=new Array(o).fill(0),f=[...r],d=new Array(o).fill(1);return h[i]=u,f[i]=p,d[i]=c,{starts:h,limits:f,strides:d}}var TC,kC,l0=M(()=>{"use strict";es();Oe();Vn();TC=Object.freeze({sum:"sum",mean:"mean",max:"max",min:"min",prod:"prod"});l(wm,"buildMappedOp");l(xC,"buildReduce");l(SC,"callBuilder");l(Le,"callMethod");kC=Object.freeze({matmul:l((r,e)=>Le(r,"matmul",e[0],e[1]),"matmul"),dot:l((r,e)=>Le(r,"dot",e[0],e[1],[e[0].type.rank-1],[0]),"dot"),clone:l((r,e)=>r._inferAndBuild("add",[e[0],r.scalarConstant(0,e[0].type.dtype).getResult(0)],null),"clone"),relu:l((r,e)=>Le(r,"relu",e[0]),"relu"),sigmoid:l((r,e)=>Le(r,"sigmoid",e[0]),"sigmoid"),gelu:l((r,e)=>Le(r,"gelu",e[0]),"gelu"),silu:l((r,e)=>Le(r,"silu",e[0]),"silu"),softmax:l((r,e,t)=>Le(r,"softmax",e[0],t?.dim??-1),"softmax"),log_softmax:l((r,e,t)=>Le(r,"logSoftmax",e[0],t?.dim??-1),"log_softmax"),layer_norm:l((r,e,t)=>Le(r,"layernorm",e[0],e[1],e[2],t?.axis??-1,t?.eps??1e-5),"layer_norm"),batch_norm:l((r,e,t)=>Le(r,"batchnorm",e[0],e[1],e[2],e[3],e[4],t?.axis??1,t?.eps??1e-5),"batch_norm"),conv2d:l((r,e,t)=>Le(r,"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((r,e,t)=>Le(r,"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((r,e)=>Le(r,"embedding",e[0],e[1]),"embedding"),argmax:l((r,e,t)=>Le(r,"argmax",e[0],t?.dim??0,t?.keepdim??!1),"argmax"),argmin:l((r,e,t)=>Le(r,"argmin",e[0],t?.dim??0,t?.keepdim??!1),"argmin"),maximum:l((r,e)=>Le(r,"maximum",e[0],e[1]),"maximum"),minimum:l((r,e)=>Le(r,"minimum",e[0],e[1]),"minimum"),eq:l((r,e)=>Le(r,"compare",e[0],e[1],"eq"),"eq"),ne:l((r,e)=>Le(r,"compare",e[0],e[1],"ne"),"ne"),lt:l((r,e)=>Le(r,"compare",e[0],e[1],"lt"),"lt"),le:l((r,e)=>Le(r,"compare",e[0],e[1],"le"),"le"),gt:l((r,e)=>Le(r,"compare",e[0],e[1],"gt"),"gt"),ge:l((r,e)=>Le(r,"compare",e[0],e[1],"ge"),"ge"),clamp:l((r,e)=>Le(r,"clamp",e[1],e[0],e[2]),"clamp"),pad:l((r,e,t)=>Le(r,"pad",e[0],e[1],t.low,t.high),"pad"),one_hot:l((r,e,t)=>Le(r,"oneHot",e[0],t.depth,{dtype:"f32"}),"one_hot"),index_select:l((r,e,t)=>Le(r,"gather",e[0],e[1],Rk(e[0].type,t?.dim??0,e[1].type.rank)),"index_select"),gather:l((r,e,t)=>Le(r,"gatherDim",e[0],e[1],t?.dim??0),"gather"),scatter_add:l((r,e,t)=>Le(r,"scatterAddDim",e[0],e[1],e[2],t?.dim??0),"scatter_add"),cat:l((r,e,t)=>Le(r,"concat",e,Io(t?.dim??0,e[0].type.rank)),"cat"),stack:l((r,e,t)=>{let n=NA(t?.dim??0,e[0].type.rank),s=e.map(o=>{let i=[...o.type.shape];return i.splice(n,0,1),r.reshape(o,i).getResult(0)});return Le(r,"concat",s,n)},"stack"),reshape:l((r,e,t)=>r.reshape(e[0],vC(t.shape??t.new_shape,e[0].type.shape)),"reshape"),transpose:l((r,e,t)=>{let n=e[0].type.rank,s=Io(t?.dim0??0,n),o=Io(t?.dim1??1,n),i=Array.from({length:n},(a,u)=>u);return i[s]=o,i[o]=s,r.transpose(e[0],i)},"transpose"),permute:l((r,e,t)=>Le(r,"transpose",e[0],t.dims),"permute"),broadcast_in_dim:l((r,e,t)=>Le(r,"broadcast",e[0],t.result_shape,t.broadcast_dimensions),"broadcast_in_dim"),expand:l((r,e,t)=>{let n=e[0].type.rank,s=t.shape,o=s.length-n,i=s.map((u,p)=>u===-1?e[0].type.shape[p-o]:u),a=Array.from({length:n},(u,p)=>p+o);return Le(r,"broadcast",e[0],i,a)},"expand"),slice:l((r,e,t)=>{let n=NC(e[0].type.shape,t.dim,t.start,t.end,t.step);return Le(r,"slice",e[0],n.starts,n.limits,n.strides)},"slice"),unsqueeze:l((r,e,t)=>{let n=[...e[0].type.shape];return n.splice(NA(t.dim,n.length),0,1),r.reshape(e[0],n)},"unsqueeze"),squeeze:l((r,e,t)=>{let n=[...e[0].type.shape];if(t.dim===void 0||t.dim===null)return r.reshape(e[0],n.filter(o=>o!==1));let s=Io(t.dim,n.length);return n[s]===1&&n.splice(s,1),r.reshape(e[0],n)},"squeeze"),narrow:l((r,e,t)=>{let n=Io(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[n]=t.start,o[n]=t.start+t.length,Le(r,"slice",e[0],s,o,i)},"narrow"),select:l((r,e,t)=>{let n=e[0].type.rank,s=Io(t.dim,n),o=t.index,i=o<0?e[0].type.shape[s]+o:o,a=new Array(n).fill(0),u=[...e[0].type.shape];a[s]=i,u[s]=i+1;let p=Le(r,"slice",e[0],a,u,new Array(n).fill(1)).getResult(0);return r.reshape(p,e[0].type.shape.filter((c,h)=>h!==s))},"select"),split:l((r,e,t)=>Le(r,"split",e[0],Io(t.dim??0,e[0].type.rank),t.sizes),"split"),chunk:l((r,e,t)=>{let n=Io(t.dim??0,e[0].type.rank),s=e[0].type.shape[n],o=Math.ceil(s/t.chunks),i=[];for(let a=0;a<s;a+=o)i.push(Math.min(o,s-a));return Le(r,"split",e[0],n,i)},"chunk")});l(Io,"normalizeDim");l(vC,"resolveShape");l(NA,"normalizeInsertDim");l(NC,"sliceAttrs")});function OC(r,e,t,n){let s=r;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+="|"+n.name,s}function BC(r){let e=OA.get(r);return e||(e=new zu("jit_"+r),OA.set(r,e)),e}function RC(r){let e=1;for(let t of r.shape)e*=typeof t=="number"&&t>0?t:1;return Math.max(e,1)}function MC(r,e,t){let n=[];for(let[,s]of t.bufferMap)n.push(new(Dt(s.dtype))(RC(s)));r.run(e.name,...n)}function IC(r,e,t,n){try{let s=BA(r,e);if(s.shapeParams&&s.shapeParams.length>0)return null;new Rs(e).applyToAllBlocks(new Sn(s));let o=t.compile(s);return n.addCompiledKernel(o),e.isWebGPU()||MC(n,o,s),o}catch{return null}}function LC(r,e,t){let n=e.map(a=>new I(a.shape,a.dtype)),s=r+"_jit_"+EC++,o=hc(s,n,[],(a,u)=>{let p;p=wm(a,r,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 RA(r,e,t=null,n){let s=OC(r,e,t,n),o=AA.get(s);if(o)return o;let i=LC(r,e,t),a=new wn(r+"_jit_mod");a.addFunction(i);let u=new oa;u.addPass(new Gu),u.addPass(new ia),u.addPass(new no),u.run(a);let p=BC(n.name),c=new AC(n),f=typeof n.isGPU=="function"&&n.isGPU()?IC(i,n,c,p):null;f||(f=c.compile(BA(i,n)),p.addCompiledKernel(f));let d=i.getReturnOp(),m=d&&d.operands.length>0?d.operands[0].type.dtype:null;return o={funcName:f.name,runtime:p,numInputs:e.length,outDtype:m,compiled:f},AA.set(s,o),o}var AA,OA,BA,AC,EC,MA=M(()=>{"use strict";es();Yo();Oe();Ui();Ec();nm();um();Lw();Dw();$w();fa();ro();ts();l0();AA=new Map,OA=new Map,BA=Xh,AC=Hs;l(OC,"_cacheKey");l(BC,"_getRuntime");l(RC,"_bufferNumel");l(MC,"_trialLaunch");l(IC,"_compileScheduledGPU");EC=0;l(LC,"_buildGraphFunc");l(RA,"jitCompile")});function Zc(){return CC}function EA(r){DC=r}function c0(){IA&&IA()}var CC,IA,DC,Tm=M(()=>{"use strict";CC=!1,IA=null,DC=!1;l(Zc,"isEagerDeferred");l(EA,"setCudaGraphArmed");l(c0,"eagerFlush")});function xm(r){return r.slice(0,r.indexOf("(")).split(".")[0].trim()}function LA(){return Sm.map(r=>r.schema)}function km(r){return $C.get(r)||null}function FC(r){let e=r.indexOf("(")+1,t=r.lastIndexOf(")"),n=r.slice(e,t).trim();if(!n)return[];let s=[];for(let o of CA(n)){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 CA(r){let e=[],t=0,n=0;for(let s=0;s<r.length;s++){let o=r[s];o==="("||o==="["?t++:o===")"||o==="]"?t--:o===","&&t===0&&(e.push(r.slice(n,s)),n=s+1)}return e.push(r.slice(n)),e}function zC(r){let t=r.lastIndexOf("->"),n=t>=0?r.slice(t+2).trim():"Tensor";return n==="Tensor"?{kind:"tensor",arity:1}:n==="Tensor[]"?{kind:"tensor_list"}:n.startsWith("(")&&n.endsWith(")")?{kind:"tuple",arity:CA(n.slice(1,-1)).length}:{kind:"value",arity:1}}var Sm,PC,$C,h5,vm=M(()=>{"use strict";Sm=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"}]),PC=new Map(Sm.filter(r=>r.scalarArgs).map(r=>[xm(r.schema),r.scalarArgs])),$C=new Map(Sm.map(r=>[xm(r.schema),PC.get(xm(r.schema))||FC(r.schema)])),h5=new Map(Sm.map(r=>{let e=xm(r.schema);return[e,Object.freeze({...r,name:e,returns:r.returns||zC(r.schema)})]}));l(xm,"opNameFromSchema");l(LA,"tensorOpSchemas");l(km,"scalarArgNames");l(FC,"scalarArgNamesFromSchema");l(CA,"splitTopLevel");l(zC,"returnsFromSchema")});var nA={};nt(nA,{getCudnnGRU:()=>d0,getCudnnLSTM:()=>h0,getGpuAdamFn:()=>m0,getGpuContiguousFn:()=>QC,getGpuMatmul:()=>dw,getWebgpuRNN:()=>Qc,gpuContiguousArray:()=>oD,registerJITKernels:()=>y0,setCudnnGRU:()=>tD,setCudnnLSTM:()=>eD,setGpuAdamFn:()=>rD,setGpuConcatFn:()=>XC,setGpuContiguousFn:()=>ZC,setGpuMatmul:()=>sD,setWebGPUEagerFn:()=>KC,setWebgpuRNN:()=>nD,tensorToContiguous:()=>Je,wrapResult:()=>tn});function KC(r){PA=r}function p0(r){return typeof r=="object"&&r!==null&&"_impl"in r}function WC(r,e){let t=[],n={},s=km(r),o=0;for(let i=0;i<e.length;i++){let a=e[i];if(p0(a))t.push(a);else if(Array.isArray(a)&&a.length>0&&p0(a[0]))for(let u of a)p0(u)&&t.push(u);else a!=null&&(s&&o<s.length&&(n[s[o]]=a),o++)}return{tensors:t,scalars:n}}function ha(r,e){return typeof r=="number"?r:e}function Qu(r,e){return Array.isArray(r)?r:e}function DA(r){return Array.isArray(r)?r:[[0,0],[0,0]]}function $A(r,e,t){if(e.length===0)return[];if(new Set(["sum","mean","max","min","prod","argmax","argmin"]).has(r)){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(r==="matmul")return Bd(e[0].shape,e[1].shape)||[];if(r==="dot")return[];if(r==="transpose"){let o=[...e[0].shape],i=ha(t.dim0,0),a=ha(t.dim1,1),u=o[i];return o[i]=o[a],o[a]=u,o}if(r==="conv2d"){let o=e[0].shape,i=e[1].shape,a=Qu(t.strides,[1,1]),u=DA(t.padding),p=Qu(t.dilation,[1,1]),c=[];for(let h=0;h<2;h++){let f=u[h][0]+u[h][1],d=(i[h+2]-1)*p[h]+1;c.push(Math.floor((o[h+2]+f-d)/a[h])+1)}return[o[0],i[0],...c]}if(r==="pool2d"){let o=e[0].shape,i=Qu(t.kernel_size,[2,2]),a=Qu(t.strides,i),u=DA(t.padding),p=[];for(let c=0;c<2;c++){let h=u[c][0]+u[c][1];p.push(Math.floor((o[c+2]+h-i[c])/a[c])+1)}return[o[0],o[1],...p]}if(r==="clamp"){let o=[...e[0].shape];for(let i=1;i<e.length;i++)o=Ou(o,e[i].shape)||o;return o}if(r==="pad"){let o=e[0].shape,i=Qu(t.low,[]),a=Qu(t.high,[]);return o.map((u,p)=>u+(i[p]||0)+(a[p]||0))}if(r==="one_hot")return[...e[0].shape,ha(t.depth,0)];if(r==="cat"){let o=e[0].shape.length,i=ha(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(r==="stack"){let o=e[0].shape.length,i=ha(t.dim,0),a=i<0?o+1+i:i,u=[...e[0].shape];return u.splice(a,0,e.length),u}if(r==="index_select"){let o=[...e[0].shape],i=o.length,a=ha(t.dim,0),u=a<0?i+a:a;return o[u]=e[1].shape.reduce((p,c)=>p*c,1),o}if(r==="gather")return[...e[1].shape];if(r==="scatter_add")return[...e[0].shape];if(r==="softmax"||r==="log_softmax")return[...e[0].shape];if(r==="layer_norm"||r==="batch_norm")return[...e[0].shape];if(r==="embedding"){let o=e[0].shape;return[...e[1].shape,o[1]]}return e.length===1?[...e[0].shape]:Ou(e[0].shape,e[1].shape)||[...e[0].shape]}function Je(r){let e=r._impl.storage.data,t=r._impl.storageOffset,n=r.numel;if(r.isContiguous&&t===0&&e.length===n)return r.data||e;let s=r.shape,o=r.strides,i=e.constructor,a=new i(n),u=a,p=s.length,c=new Int32Array(p),h=t;for(let f=0;f<n;f++){u[f]=e[h];for(let d=p-1;d>=0;d--){if(c[d]++,c[d]<s[d]){h+=o[d];break}h-=(s[d]-1)*o[d],c[d]=0}}return a}function tn(r,e,t,n){let s=xn(e),o=En.fromData(r,n),i=new Bt(o,0,e,s,t,n);return new We(i)}function Nm(r,e,t){let n=1;for(let s=e;s<t;s++)n*=r[s];return n}function HC(r,e,t,n){let s=e.length,o=t<0?s+1+t:t,i=Nm(e,0,o),a=Nm(e,o,s),u=r.length;for(let p=0;p<i;p++)for(let c=0;c<u;c++)n.set(r[c].subarray(p*a,(p+1)*a),(p*u+c)*a)}function YC(r,e,t,n){let s=e[0].length,o=t<0?s+t:t,i=Nm(e[0],0,o),a=Nm(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 h=0;h<r.length;h++){let f=e[h][o]*a;n.set(r[h].subarray(p*f,(p+1)*f),c),c+=f}}}function JC(r,e,t){let n=$A(r,e,t),s=e[0].dtype,o=Dt(s),i=new o(Math.max(cn(n),1)),a=ha(t.dim,0);if(Zc()&&f0){let p=e.map(c=>g0(c));return f0(r,p,e.map(c=>[...c.shape]),a,n,i,s),tn(i,n,s,e[0].device)}let u=e.map(p=>Je(p));return r==="stack"?HC(u,e[0].shape,a,i):YC(u,e.map(p=>p.shape),a,i),tn(i,n,s,e[0].device)}function ZC(r){Am=r}function QC(){return Am}function XC(r){f0=r}function eD(r){FA=r}function h0(){return FA}function tD(r){zA=r}function d0(){return zA}function nD(r){VA=r}function Qc(){return VA}function rD(r){GA=r}function m0(){return GA}function sD(r){jA=r}function dw(){return jA}function g0(r){if(r.isContiguous&&r._impl.storageOffset===0){let e=r._impl.storage.rawData;if(e&&e.length===r.numel)return e}return Zc()&&Am?Am(r._impl.storage.rawData,r.shape,r.strides,r._impl.storageOffset,r.dtype):Je(r)}function oD(r){return g0(r)}function iD(r,e){let t=UC[e];if(!t)return null;let n=e===1,s=e===5,o=n&&(r==="stack"||r==="cat");return(i,...a)=>{let{tensors:u,scalars:p}=WC(r,a);if(u.length===0)throw new Error(`JIT dispatch: no tensor args for op '${r}'`);if(o)return JC(r,u,p);let c=t(),h=RA(r,u,p,c),f=$A(r,u,p),d=h.outDtype||Rd(u[0].dtype,u.length>1?u[1].dtype:u[0].dtype),m=cn(f),g=Dt(d),y=new g(Math.max(m,1));if(s)return PA(h.compiled,u,y),tn(y,f,d,u[0].device);let _=u.map(b=>n?g0(b):Je(b));return _.push(y),h.runtime.run(h.funcName,..._),tn(y,f,d,u[0].device)}}function y0(){let r=Nt.listOps(),e=[0,1,2,5];for(let t of r){let n=Nt.findOp(t);if(!n)continue;let s=n.name;for(let o of e){if(n.entry.hasKernel(o))continue;let i=iD(s,o);i&&n.entry.registerKernel(o,wr.fromUnboxed(i))}}}var VC,GC,jC,qC,UC,PA,Am,f0,FA,zA,VA,GA,jA,Kn=M(()=>{"use strict";Nn();nc();$i();MA();Tm();Ks();jn();Oo();ea();jr();ts();vm();UC={0:()=>VC??=Us(),1:()=>GC??=ed(),2:()=>jC??=td(),5:()=>qC??=nd()},PA=null;l(KC,"setWebGPUEagerFn");l(p0,"hasTensorImpl");l(WC,"_extractTensorsAndScalars");l(ha,"numberScalar");l(Qu,"numberArrayScalar");l(DA,"paddingScalar");l($A,"_inferOutputShape");l(Je,"tensorToContiguous");l(tn,"wrapResult");l(Nm,"_prod");l(HC,"_hostStack");l(YC,"_hostCat");l(JC,"_runHostConcatLike");Am=null;l(ZC,"setGpuContiguousFn");l(QC,"getGpuContiguousFn");f0=null;l(XC,"setGpuConcatFn");FA=null;l(eD,"setCudnnLSTM");l(h0,"getCudnnLSTM");zA=null;l(tD,"setCudnnGRU");l(d0,"getCudnnGRU");VA=null;l(nD,"setWebgpuRNN");l(Qc,"getWebgpuRNN");GA=null;l(rD,"setGpuAdamFn");l(m0,"getGpuAdamFn");jA=null;l(sD,"setGpuMatmul");l(dw,"getGpuMatmul");l(g0,"_gpuInputArray");l(oD,"gpuContiguousArray");l(iD,"_wrapOpForJIT");l(y0,"registerJITKernels")});var o1={};nt(o1,{arange:()=>n1,empty:()=>fn,eye:()=>Rm,full:()=>et,linspace:()=>s1,ones:()=>Ht,randn:()=>Bm,randperm:()=>r1,zeros:()=>$e});function Om(r){return{dtype:r?.dtype??W.F32,device:r?.device??ii(),requiresGrad:r?.requiresGrad??!1}}function Xc(r,e,t,n){let s=xn(r),i=cn(r)*Xi(e),a=En.allocate(i,e,t),u=new Bt(a,0,r,s,e,t),p=new We(u);return n&&p.requiresGrad_(!0),p}function fn(r,e){let{dtype:t,device:n,requiresGrad:s}=Om(e);return Xc(r,t,n,s)}function b0(r,e,t){let n=fn(r,e),s=n.data;if(s)return s.fill(t),n;if(n.device&&n.device.type==="meta"){let o=fn(r,{dtype:e?.dtype});return o.data.fill(t),o}return n}function $e(r,e){return b0(r,e,0)}function Ht(r,e){return b0(r,e,1)}function et(r,e,t){return b0(r,t,e)}function Bm(r,e){let t=fn(r,e),n=t.data;if(n){let s=n.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;n[o]=u*Math.cos(p),o+1<s&&(n[o+1]=u*Math.sin(p))}}return t}function n1(r,e,t,n){let s,o,i;e===void 0&&t===void 0?(s=0,o=r,i=1):t===void 0?(s=r,o=e,i=1):(s=r,o=e,i=t);let a=Math.max(0,Math.ceil((o-s)/i)),{dtype:u,device:p,requiresGrad:c}=Om(n),h=Xc([a],u,p,c),f=h.data;if(f)for(let d=0;d<a;d++)f[d]=s+d*i;return h}function Rm(r,e,t){let n=e??r,{dtype:s,device:o,requiresGrad:i}=Om(t),a=Xc([r,n],s,o,i),u=a.data;if(u){u.fill(0);let p=Math.min(r,n);for(let c=0;c<p;c++)u[c*n+c]=1}return a}function r1(r,e){let t=e?.dtype??W.I32,n=e?.device??ii(),s=Xc([r],t,n,e?.requiresGrad??!1),o=s.data;for(let i=0;i<r;i++)o[i]=i;for(let i=r-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 s1(r,e,t,n){let{dtype:s,device:o,requiresGrad:i}=Om(n),a=Xc([t],s,o,i),u=a.data;if(u&&t>0)if(t===1)u[0]=r;else{let p=(e-r)/(t-1);for(let c=0;c<t;c++)u[c]=r+c*p}return a}var _t=M(()=>{"use strict";jn();Oo();ea();ts();ar();jr();l(Om,"_defaultOpts");l(Xc,"_makeTensor");l(fn,"empty");l(b0,"_filled");l($e,"zeros");l(Ht,"ones");l(et,"full");l(Bm,"randn");l(n1,"arange");l(Rm,"eye");l(r1,"randperm");l(s1,"linspace")});var _O={};nt(_O,{GradMode:()=>rl,enableGrad:()=>yO,noGrad:()=>gO});function gO(r){let e=hi;hi=!1;try{return r()}finally{hi=e}}function yO(r){let e=hi;hi=!0;try{return r()}finally{hi=e}}var hi,rl,sp=M(()=>{"use strict";hi=!0,rl={isEnabled(){return hi},setEnabled(r){hi=r}};l(gO,"noGrad");l(yO,"enableGrad")});var YP,ft,ga=M(()=>{"use strict";jn();Oo();ea();Nn();ar();jr();YP=Pr.fromKey(48),ft=class extends We{static{l(this,"SymbolicTensor")}_irValue;_tracer;_symbolicShape;constructor(e,t,n,s,o){let i=xn(t),a=En.allocate(0,n,Au),u=new Bt(a,0,t,i,n,Au);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(YP)}get isSymbolic(){return!0}}});function JP(r){let e=[...r.entries()];return e.sort((t,n)=>t[0]<n[0]?-1:t[0]>n[0]?1:0),e}function Tg(r){return r==null?"null":typeof r=="number"?Number.isFinite(r)?String(r):r===1/0?"inf":r===-1/0?"-inf":"nan":typeof r=="boolean"?String(r):typeof r=="string"?`"${r}"`:r instanceof I?Rn(r):Array.isArray(r)?r.length===0?"[]":Array.isArray(r[0])?"["+r.map(e=>Tg(e)).join(", ")+"]":"["+r.map(e=>Tg(e)).join(", ")+"]":typeof r=="object"&&r.constructor===Object?"{"+Object.entries(r).sort((t,n)=>t[0].localeCompare(n[0])).map(([t,n])=>`${t}: ${Tg(n)}`).join(", ")+"}":String(r)}function ZP(r){return new cp().printModule(r)}var cp,P0=M(()=>{"use strict";Oe();cp=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 n=!0;for(let s of e)n||t.push(""),n=!1,this.printFunction(s,t);return this.indent--,t.push("}"),t.join(`
104
+ `)}printFunction(e,t=null){let n=t===null;n&&(t=[]),this.valueNames.clear(),this._nextValueId=0;let s=e.args.map((i,a)=>`${this._nameValue(i)}: ${Rn(i.type)}`),o=e.outputTypes.map(i=>Rn(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()}}`),n?t.join(`
105
+ `):void 0}printBlock(e,t,n=!1){if(!n&&e.arguments.length>0){let s=e.arguments.map(o=>`${this._nameValue(o)}: ${Rn(o.type)}`);t.push(`${this._indentPrefix()}^bb(${s.join(", ")}):`)}for(let s of e)this.printOperation(s,t)}printOperation(e,t=null){let n=t===null;n&&(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 JP(e.attributes))i.push(`${a} = ${Tg(u)}`);o+=" {"+i.join(", ")+"}"}if(e.numResults>0){let i=[];for(let a=0;a<e.numResults;a++)i.push(Rn(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)}: ${Rn(c.type)}`);t.push(`${this._indentPrefix()}^bb(${p.join(", ")}):`)}for(let p of u)this.printOperation(p,t)}this.indent--,t.push(`${s}}`)}return n?t.join(`
106
+ `):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(JP,"sortedEntries");l(Tg,"formatAttrValue");l(ZP,"printModule")});var g$,Ig,CO=M(()=>{"use strict";Oe();Vs();g$={eq:l((r,e)=>r===e,"eq"),ne:l((r,e)=>r!==e,"ne"),gt:l((r,e)=>r>e,"gt"),ge:l((r,e)=>r>=e,"ge"),lt:l((r,e)=>r<e,"lt"),le:l((r,e)=>r<=e,"le")},Ig=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,n){let s=`s${this._nextId++}`;return this._symbols.set(s,{hint:n,inputIdx:e,dimIdx:t}),s}produceShapeSpec(e,t,n){let s=new Array(t.length),o=new Array(t.length);for(let i=0;i<t.length;i++)if(n&&n.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,n){this._guards.push({lhs:e,op:t,rhs:n})}guardDivisible(e,t){this._guards.push({type:"divisible",sym:e,divisor:t})}bindInputShapes(e){this._bindings.clear();for(let[t,n]of this._symbols)this._bindings.set(t,e[n.inputIdx].shape[n.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 n=this._resolve(t.lhs),s=this._resolve(t.rhs);if(!g$[t.op](n,s))return{passed:!1,failedGuard:t}}return{passed:!0,failedGuard:null}}resolveSymbolicShape(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=this._resolve(e[n]);return t}_resolve(e){return typeof e=="number"?e:typeof e=="string"?this._bindings.get(e):e instanceof Ce?Ce.evaluate(e,this._bindings):e}get symbols(){return this._symbols}get guards(){return this._guards}get bindings(){return this._bindings}}});function ss(){return Eg}var Eg,Lg,ya=M(()=>{"use strict";es();Yo();xs();Oe();At();ga();CO();l0();Eg=null;l(ss,"getActiveTracer");Lg=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 Ig,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,n){let s=this._inputTypes.length,{irShape:o,symShape:i}=this._shapeEnv.produceShapeSpec(s,e,n);if(n)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 un(this._name,this._inputTypes,[]),this._func.inputTypes=[...this._func.inputTypes],this._builder=new An(this._func),this._module=new wn(this._name);let e=[],t=this._func.args;for(let n=0;n<t.length;n++){let s=t[n],o=this._inputTypes[n];s.symbolicShape=this._inputSymShapes[n];let i=new ft(s,o.shape,o.dtype,this,this._inputSymShapes[n]);e.push(i)}return this._inputs=e,e}recordOp(e,t,n){let s=[];for(let u of t)u instanceof ft&&s.push(u.irValue);let o=this._requireBuilder(),i=wm(o,e,s,n),a=[];for(let u=0;u<i.numResults;u++){let p=i.getResult(u),c=p.type,h=this._propagateSymbolicShape(e,i,t,c,u);p.symbolicShape=h,a.push(new ft(p,c.shape,c.dtype,this,h))}return a.length===1?a[0]:a}_propagateSymbolicShape(e,t,n,s,o=0){let i=n.filter(c=>c instanceof ft),a=ne.get(t.opName||e);if(a&&a.propagateSymbolicShapes){let c=new Map;for(let f of i)c.set(f.irValue,f.symbolicShape);let h=a.propagateSymbolicShapes(t,c);if(h&&h[o])return h[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 h=null;for(let f of i){let d=f.symbolicShape;if(!d)continue;let m=u.length-d.length,g=c-m;if(g>=0&&g<d.length&&typeof d[g]=="string"){h=d[g];break}}p[c]=h!==null?h:X}return p}scan(e,t,n){let s=l(f=>f instanceof ft?f.irValue:this.captureConstant(f).irValue,"toIr"),o=e.map(s),i=t.map(s),u=this._requireBuilder().scanOp(o,i,(f,d,m)=>{let g=this._requireBuilder();this._builder=f;try{let y=l(w=>new ft(w,w.type.shape,w.type.dtype,this,[...w.type.shape]),"wrap"),[_,b]=n(m.map(y),d.map(y));return[_.map(w=>w.irValue),b.map(w=>w.irValue)]}finally{this._builder=g}}),p=t.length,c=[],h=[];for(let f=0;f<u.numResults;f++){let d=u.getResult(f),m=new ft(d,d.type.shape,d.type.dtype,this,[...d.type.shape]);f<p?c.push(m):h.push(m)}return[c,h]}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),h=new ft(c,[],e.dtype,this,[]);return this._capturedParams.set(e,h),h}let n=new I(e.shape,e.dtype),s=this._requireFunc();s.inputTypes.push(n);let i=s.entryBlock.addArgument(n),a=new ft(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 ft&&(this._requireBuilder().returnOp([e.irValue]),this._outputSymShapes=[e.symbolicShape]),this._outputTypes=[new I(e.shape,e.dtype)]}markOutputs(e){let t=e.map(n=>n.irValue);this._requireBuilder().returnOp(t),this._outputTypes=e.map(n=>new I(n.shape,n.dtype)),this._outputSymShapes=e.map(n=>n.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(){Eg=this}deactivate(){Eg===this&&(Eg=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 YO(){return[...HO]}function Gg(r,e,t,n=HO){return n.filter(s=>s.phase===r).sort((s,o)=>s.priority-o.priority).map(s=>s.factory(e,t)).filter(Boolean)}var HO,uT=M(()=>{"use strict";HO=[];l(YO,"snapshotGraphPasses");l(Gg,"graphPassesForPhase")});function JO(r){return r instanceof Map?r:r&&typeof r=="object"?new Map(Object.entries(r)):new Map}var jg,ZO=M(()=>{"use strict";uT();l(JO,"toMap");jg=class{static{l(this,"CompilerContext")}constructor({loweringRules:e=null,codegenEntries:t=null,graphPasses:n=null}={}){this.loweringRules=JO(e),this.codegenEntries=JO(t),this.graphPasses=n||YO()}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,n){return Gg(e,t,n,this.graphPasses)}}});function qg(r){return new lT().print(r)}var lT,cT=M(()=>{"use strict";Vn();lT=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(`
107
+ `+" ".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,n]of e.bufferMap)this.push(`${n.name} = buffer_map(${t.name}, shape=[${n.shape.join(",")}], dtype=${n.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} `,n=e.threadTag?`[${e.threadTag}] `:"";this.push(`for ${e.loopVar.name} in 0..`),this.visit(e.extent),this.push(` ${t}${n}{`),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(` ${Ka(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(qg,"printTensorIR")});var Ug,QO=M(()=>{"use strict";Kt();cT();Ug=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 n of this.passes)this._runPass(n,e,t);return e}_runPass(e,t,n){let s=n.trace;e.trace=s,s.phaseStart(e.phase);let o=performance.now();e.begin(n);for(let i=0;i<t.length;i++){let a=t[i];if(!n.failed.has(a.name))try{let u=e.run(a,n);u&&u!==a&&(t[i]=u)}catch(u){if(n.errors.push(new Dn(e.phase,a.name,u.message)),n.failed.add(a.name),s.errorEvent(e.phase,a.name,u.message),!n.resilient)break}}if(e.end(n),s.phaseEnd(e.phase,performance.now()-o),e.snapshotPoint&&s.shouldSnapshot(e.snapshotPoint))for(let i of t)n.failed.has(i.name)||s.irDump(e.snapshotPoint+":"+i.name,qg(i));this.verifyHook&&this._verifyFuncs(t,n),e.trace=null}_verifyFuncs(e,t){for(let n of e){if(t.failed.has(n.name))continue;let s=this.verifyHook(n);if(s&&s.length>0){let o=s.join("; ");if(t.resilient)t.errors.push(new Dn("verification",n.name,o)),t.failed.add(n.name);else throw new Error("TensorIR verification failed for "+n.name+": "+o)}}}}});function XO(r){let e=new Ua;return e.add(new Ja),e.add(new Za),e.add(new Qa(r)),e.add(new Xa),e.add(new eu(r)),e.add(new tu),e.add(new nu),e.add(new wh),e.add(new Ya),e.add(new Nh),e.add(new Ah),e.add(new Oh),e.add(new Bh),r&&(e.add(new vh(r)),e.add(new Sh(r)),e.add(new kh(r))),e}var L$,C$,Kg,eB=M(()=>{"use strict";nn();uc();Cw();ru();l(XO,"buildAlgebraicPatterns");L$=XO(!1),C$=XO(!0),Kg=class extends Ge{static{l(this,"AlgebraicSimplificationPass")}constructor(e={}){super("algebraic_simplify"),this.preservedAnalyses=new Set,this.patterns=e.fastMath?C$:L$}run(e,t){return new ju(this.patterns).applyPatterns(e,10,this.trace)}}});function tB(r){let e=ne.get(r);return!!(e&&e.isConstant)}function D$(r,e){return!gb(e)||typeof r!="number"?!0:Number.isInteger(r)&&Number.isSafeInteger(r)}function nB(r,e,t){if(t.has(r))return t.get(r);let n=P$(r,e,t);return t.set(r,n),n}function P$(r,e,t){let n=r.definingOp;if(!n)return;if(tB(n.opName))return n.getAttr("value");if(e.has(n))return;e.add(n);let s=ne.get(n.opName);if(!s||!s.fold||s.hasSideEffects||n.regions.length>0)return;let o=new Array(n.numOperands),i=new Array(n.numOperands);for(let a=0;a<n.numOperands;a++){let u=nB(n.getOperand(a),e,t);if(u===void 0)return;o[a]=u,i[a]=n.getOperand(a).definingOp}try{return s.fold(o,n.attributes,i)}catch{return}}var Wg,rB=M(()=>{"use strict";nn();es();At();Kt();Oe();l(tB,"isConstantProducer");l(D$,"isFoldResultRepresentable");l(nB,"resolveConstantValue");l(P$,"computeConstantValue");Wg=class extends Ge{static{l(this,"ConstantFoldPass")}constructor(){super("constant_fold")}run(e,t){let n=!1,s=0,o=new An(e),i=new Map;for(let a of[...e.opsRecursive()]){if(!a.parentBlock||tB(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),h=!0;for(let f=0;f<a.numOperands;f++){let d=nB(a.getOperand(f),new Set,i);if(d===void 0){h=!1;break}p[f]=d,c[f]=a.getOperand(f).definingOp}if(h)try{let f=u.fold(p,a.attributes,c);if(f===void 0||!D$(f,a.getResult(0).type.dtype))continue;o.block=a.parentBlock,o.setInsertionPoint(a);let d=o.constant(f,a.getResult(0).type);a.replaceAllResultsWith([d.getResult(0)]),a.erase(),n=!0,s++}catch(f){this.trace&&this.trace.level>=ue.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,foldError:a.opName,message:f.message,level:ue.DEBUG})}}return this.trace&&this.trace.level>=ue.DEBUG&&s>0&&this.trace.emit({type:"pass_detail",passName:this.name,foldedCount:s,level:ue.DEBUG}),n?oe.CHANGED:oe.UNCHANGED}}});var Hg,sB=M(()=>{"use strict";nn();At();Kt();Hg=class extends Ge{static{l(this,"CSEPass")}constructor(){super("cse"),this.preservedAnalyses=new Set}run(e,t){let n=!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 h=a.get(c),f=!1;for(let d of h)if(d.parentBlock&&d.structuralEquals(u)){let m=[];for(let g=0;g<d.numResults;g++)m.push(d.getResult(g));u.replaceAllResultsWith(m),u.erase(),n=!0,s++,f=!0;break}f||h.push(u)}}return this.trace&&this.trace.level>=ue.DEBUG&&s>0&&this.trace.emit({type:"pass_detail",passName:this.name,eliminated:s,level:ue.DEBUG}),n?oe.CHANGED:oe.UNCHANGED}}});function fT(r,e){let t=pT[r],n=pT[e];return t===void 0||n===void 0?!1:r===je.REDUCTION?e===je.ELEMENTWISE:e===je.REDUCTION?t<=pT[je.INJECTIVE]:!0}function bl(r){let e=!1,t=!1,n=!1,s=!1;for(let o of r){let i=ne.get(o.opName);if(!i||i.isOpaque){n=!0;continue}i.isReduction?e=!0:i.isInjective?t=!0:i.isBroadcast?s=!0:i.isElementwise||(n=!0)}return n?je.OPAQUE:e?je.REDUCTION:t?je.INJECTIVE:s?je.BROADCAST:je.ELEMENTWISE}function Aa(r){let e=ne.get(r.opName);return!e||e.isOpaque?je.OPAQUE:e.isReduction?je.REDUCTION:e.isInjective?je.INJECTIVE:e.isBroadcast?je.BROADCAST:e.isElementwise?je.ELEMENTWISE:je.OPAQUE}function oB(r,e){if(r.length!==e.length)return!1;for(let t=0;t<r.length;t++)if(!(r[t]===X||e[t]===X)&&r[t]!==e[t])return!1;return!0}var je,pT,yl,_l,Oa=M(()=>{"use strict";Oe();At();Ui();Tr();je=Object.freeze({ELEMENTWISE:"kElementwise",BROADCAST:"kBroadcast",REDUCTION:"kReduction",INJECTIVE:"kInjective",OPAQUE:"kOpaque",HORIZONTAL:"kHorizontal"}),pT={[je.ELEMENTWISE]:0,[je.BROADCAST]:1,[je.INJECTIVE]:2,[je.REDUCTION]:3};l(fT,"canFusePatterns");l(bl,"classifyFusionKind");l(Aa,"classifyOpPattern");yl=Object.freeze({legal:!0}),_l=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=qt(e)||ko(e)||Fb(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 n=ne.get(e.opName),s=ne.get(t.opName);if(!n)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(n.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=Aa(e),i=Aa(t);return fT(o,i)?(o===je.REDUCTION||i===je.REDUCTION)&&!this.allowReductionFusion?{legal:!1,reason:"reduction fusion disabled by target"}:o===je.ELEMENTWISE&&i===je.ELEMENTWISE?this._checkElementwisePair(e,t):(o===je.BROADCAST||o===je.REDUCTION)&&i===je.ELEMENTWISE?yl: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 n=0;for(let s of e.ops){let o=ne.get(s.opName);if(o&&o.isReduction&&n++,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&&n++,o&&o.isOpaque)return{legal:!1,reason:"opaque op in merge (use EpilogueFusionPass)"}}return n>1?{legal:!1,reason:"merged group would contain multiple reductions"}:yl}_checkElementwisePair(e,t){let n=this._getOutputShape(e),s=this._getOutputShape(t);return n&&s&&!oB(n,s)?{legal:!1,reason:`elementwise shape mismatch: [${n}] vs [${s}]`}:yl}_checkProducerConsumerShapes(e,t){let n=this._getOutputShape(e);if(!n)return yl;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 oB(n,o.shape)?yl:{legal:!1,reason:`shape mismatch on data edge: [${n}] vs [${o.shape}]`}}return yl}_getOutputShape(e){for(let t=0;t<e.numResults;t++){let n=e.getResult(t).type;if(n instanceof I)return n.shape}return null}};l(oB,"shapesCompatible")});function Ba(r){return zh(r,"throw")}function nf(r,{labelOf:e,sameLabel:t=l((i,a)=>i===a,"sameLabel"),canMerge:n=l(()=>!0,"canMerge"),onAttach:s=l(()=>{},"onAttach"),sort:o=Ba}){let i=o(r),a=new Map,u=new Map,p=[],c=0,h=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 T of w)y.push(T)}return!1},"isUpstreamOf"),f=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"),d=l((m,g)=>{for(let y of f(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)||!n(w,m,g))continue;let T=!1;for(let S of f(m))if(S!==w&&h(w,S)){T=!0;break}if(!T){w.ops.push(m),w.opSet.add(m),s(w,m),a.set(m,w),d(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,_),d(m,_)}}return{partitions:p,opToPart:a,preds:u}}function _i(r,e,{pullConstants:t=!1,isConstant:n=l(()=>!1,"isConstant")}={}){let s=[],o=new Set,i=[],a=new Set,u=[],p=new Set;for(let c of e){for(let h=0;h<c.numOperands;h++){let f=c.getOperand(h),d=f.definingOp;if(!(d&&r.has(d))){if(t&&d&&n(d)){p.has(d)||(p.add(d),u.push(d));continue}o.has(f)||(o.add(f),s.push(f))}}for(let h=0;h<c.numResults;h++){let f=c.getResult(h);if(a.has(f))continue;let d=!1;for(let m of f.uses())if(!r.has(m.user)){d=!0;break}d&&(a.add(f),i.push(f))}}return{inputs:s,outputs:i,constDefs:u}}function hT(r,e){let t=new Map,n=new Map;for(let a of r)t.set(a,0),n.set(a,[]);for(let a of r){let u=e.get(a);if(u)for(let p of u)n.has(p)&&(n.get(p).push(a),t.set(a,t.get(a)+1))}let s=[];for(let a of r)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 n.get(a)){let p=t.get(u)-1;t.set(u,p),p===0&&s.push(u)}}return o.length===r.length?o:null}var rf=M(()=>{"use strict";ji();l(Ba,"topoSortOps");l(nf,"buildPartitions");l(_i,"computePartitionIO");l(hT,"topoSortPartitions")});function $$(r){for(let e=0;e<r.numResults;e++){let t=r.getResult(e).type;if(t instanceof I)return t.shape.join(",")}return null}var Eo,Yg,Jg=M(()=>{"use strict";Oe();At();Oa();Ui();rf();Eo=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}=_i(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=bl(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)&&!ko(e.opName))return!1}return!0}};l($$,"outputShapeKey");Yg=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 n=[],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 h=ne.get(c.opName);if(!h||h.isConstant||h.isReduction)continue;let f=s.get(i),d=s.get(c);if(f&&f===d||!this.legality.canFuse(c,i).legal)continue;let g=this._topoIndex.get(c);if(!f&&!d){let y=new Eo(this._nextId++);y.addOp(c,g),y.addOp(i,u),s.set(c,y),s.set(i,y)}else if(f&&!d)f.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(f,c)&&(f.addOp(c,g),s.set(c,f));else if(!f&&d)d.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(d,i)&&(d.addOp(i,u),s.set(i,d));else if(this.legality.canMergeGroups(f,d).legal&&!this._mergeWouldCreateCycle(f,d)){f.merge(d);for(let _ of d.ops)s.set(_,f)}}}let o=new Set;for(let i of s.values())o.has(i)||i.size<2||(o.add(i),i.classifyKind(),n.push(i));return n}_wouldCreateCycle(e,t){for(let n=0;n<t.numOperands;n++){let s=t.getOperand(n).definingOp;if(!(!s||e.hasOp(s)||this._topoIndex.get(s)<e.minTopoPos)&&this._dependsOnGroup(s,e))return!0}return!1}_dependsOnOps(e,t,n){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)<n||(s.add(u),o.push(u))}}return!1}_dependsOnGroup(e,t){return this._dependsOnOps(e,t.opSet,t.minTopoPos)}_mergeWouldCreateCycle(e,t){let n=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<n||p>s)&&this._dependsOnOps(u,o,n))return!0}return!1}_bucketable(e,t){return t&&!t.isConstant&&!t.isTerminator&&!t.isOpaque&&e.numResults>0&&$$(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 n=[],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 h=p.getResult(0).type;a++,i.set(p,a);let f=new Eo(this._nextId++);f.addOp(p,u),s.set(p,f);let d=Math.min(t.length,u+1+o);for(let m=u+1;m<d;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(f.size>=o||s.has(g))continue;let _=ne.get(g.opName);this._bucketable(g,_)&&h.equals(g.getResult(0).type)&&(this._sharesInput(p,g)||c.isElementwise&&_.isElementwise)&&(f.addOp(g,m),s.set(g,f),i.set(g,a))}f.size>=2&&(f.kind=je.HORIZONTAL,n.push(f))}return n}buildAllGroups(e){let t=this.buildProducerConsumerGroups(e),n=this.buildHorizontalGroups(e),s=new Map;for(let c of t)for(let h of c.ops)s.set(h,c);let o=new Set(s.keys()),i=[];for(let c of n){let h=!1;for(let f of c.ops)if(o.has(f)){h=!0;break}h||i.push(c)}let a=[...e.ops()],u=[];for(let c of a)for(let h=0;h<c.numOperands;h++){let f=c.getOperand(h).definingOp;f&&u.push(c,f)}for(let c of i)for(let h of c.ops)s.set(h,c);if(!this._condensedHasCycle(a,u,s))return[...t,...i];for(let c of i)for(let h of c.ops)s.delete(h);let p=[...t];for(let c of i){for(let h of c.ops)s.set(h,c);if(this._condensedHasCycle(a,u,s)){for(let h of c.ops)s.delete(h);continue}p.push(c)}return p}_condensedHasCycle(e,t,n){let s=l(h=>n.get(h)||h,"repOf"),o=new Map,i=new Set;for(let h of e)i.add(s(h));for(let h=0;h<t.length;h+=2){let f=s(t[h]),d=s(t[h+1]);if(d===f)continue;i.add(d),i.add(f);let m=o.get(d);m||(m=new Set,o.set(d,m)),m.add(f)}let a=0,u=1,p=2,c=new Map;for(let h of i)c.set(h,a);for(let h of i){if(c.get(h)!==a)continue;let f=[h];for(;f.length>0;){let d=f[f.length-1],m=c.get(d);if(m===a){c.set(d,u);let g=o.get(d);if(g)for(let y of g){let _=c.get(y);if(_===u)return!0;_===a&&f.push(y)}}else m===u&&c.set(d,p),f.pop()}}return!1}_sharesInput(e,t){if(e.numOperands===0||t.numOperands===0)return!1;let n=new Set;for(let s=0;s<e.numOperands;s++)n.add(e.getOperand(s));for(let s=0;s<t.numOperands;s++)if(n.has(t.getOperand(s)))return!0;return!1}_hasDependency(e,t){for(let n=0;n<e.numResults;n++){let s=e.getResult(n);for(let o=0;o<t.numOperands;o++)if(t.getOperand(o)===s)return!0}return!1}}});var F$,bi,Zg=M(()=>{"use strict";Oe();At();F$=Object.freeze({memory:1,launch:1e3}),bi=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={...F$,...e.benefitWeights||{}}}edgeBenefit(e){let t=this.benefitWeights;return t.launch*this.launchOverheadUs+t.memory*e}estimateOpCost(e){let t=this.estimateFLOPs(e),n=this.estimateBytes(e);return{flops:t,bytes:n,arithmeticIntensity:n>0?t/n:0}}estimateFLOPs(e){let t=ne.get(e.opName);if(t&&t.getFlops)return t.getFlops(e);let n=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&&(n=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 n}estimateBytes(e){let t=0;for(let n=0;n<e.numOperands;n++){let s=e.getOperand(n).type;if(s instanceof I){let o=s.sizeInBytes();o!==X&&(t+=o)}}for(let n=0;n<e.numResults;n++){let s=e.getResult(n).type;if(s instanceof I){let o=s.sizeInBytes();o!==X&&(t+=o)}}return t}estimateGroupCost(e){let t=e.opSet,n=new Map,s=0,o=0,i=0,a=0,u=!1,p=!1,c=0,h=0,f=0,d=0,m=new Set;for(let T of e.ops){let S=this.estimateFLOPs(T);n.set(T,S),s+=S,o+=this.estimateBytes(T),this.libraryOps.has(T.opName)&&a++;let x=ne.get(T.opName);if(x){let k=this._outputElements(T);x.isReduction&&(u=!0,c+=k),x.isElementwise&&(p=!0,h+=k)}for(let k=0;k<T.numResults;k++){let A=T.getResult(k);m.add(A);let N=0,v=!1;for(let O of A.uses())t.has(O.user)?N++:v=!0;N>1&&(i+=(N-1)*S),(N>0||v)&&f++}for(let k=0;k<T.numOperands;k++){let A=T.getOperand(k);if(!m.has(A))continue;let N=!1;for(let v of A.uses())if(t.has(v.user)&&v.user!==T){N=!0;break}N||f--}f>d&&(d=f)}let g=e.getInputValues(),y=e.getOutputValues(),_=0;for(let T of g)if(T.type instanceof I){let S=T.type.sizeInBytes();S!==X&&(_+=S)}for(let T of y)if(T.type instanceof I){let S=T.type.sizeInBytes();S!==X&&(_+=S)}let b=0;for(let T of e.ops)for(let S=0;S<T.numResults;S++){let x=T.getResult(S),k=0;for(let A of x.uses())if(t.has(A.user)&&(k++,k>1))break;if(k>1){let A=x.type;if(A instanceof I){let N=A.sizeInBytes();N!==X&&(b+=N)}}}let w=0;return u&&p&&h>0&&c>0&&(w=Math.abs(h-c)),{unfusedFLOPs:s,unfusedBytes:o,fusedFLOPs:s+i,fusedBytes:_,recomputeCost:i,memorySaved:o-_,launchSaved:(e.size-1)*this.launchOverheadUs,registerPressure:d*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 n=this.policy.shouldFuse(e,this);if(n)return n}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 n=t.memorySaved+t.launchSaved*1e3;if(t.parallelismLoss>n*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 n=e.getResult(t).type;if(n instanceof I){let s=n.numel();if(s!==X)return s}}return 0}}});function wl(r){let e=null;for(let t of r.ops())e=t;return e&&e.opName==="yield"?e:null}function Tl(r){let e=0,t=r.regions[0]?.entryBlock;if(!t)return 0;for(let n of t.ops())n.opName!=="yield"&&e++;return e}function xl(r){let e=0,t=r.regions[0]?.entryBlock;if(!t)return 0;for(let n of t.ops()){let s=ne.get(n.opName);s&&s.isReduction&&e++}return e}function Sl(r){let e=r.regions[0]?.entryBlock;if(!e)return!1;for(let t of e.ops())if(t.opName!=="yield"&&!ko(t.opName))return!1;return!0}function z$(r){let e=new Map,t=0;for(let n=r.firstOp;n;n=n._next)e.set(n,t++);return e}function dT(r){let e=z$(r);return(t,n)=>{let s=e.get(t),o=e.get(n);return s!==void 0&&o!==void 0&&s<o}}function kl(r,e){let t=wo(r.ops,g=>r.hasOp(g),"null");if(t===null||t.length===0)return null;r._inputValues=null,r._outputValues=null;let n=r.getInputValues(),s=r.getOutputValues(),o=n.map(g=>g.type),i=s.map(g=>g.type),a=new Ct,u=new yn(o);a.addBlock(u);let p=new Map;for(let g=0;g<n.length;g++)p.set(n[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 Re("yield",c,[]));let h=new Re("fusion",n,i,{fusion_kind:r.kind||e},[a]),f=t[0].parentBlock;if(!f)return null;let d=dT(f),m=null;for(let g of n){let y=g.definingOp;!y||r.hasOp(y)||(!m||!d(y,m))&&(m=y)}m&&m.parentBlock===f?f.insertAfter(h,m):f.insertBefore(h,t[0]);for(let g=0;g<s.length;g++)s[g].replaceAllUsesWith(h.getResult(g));for(let g of t)g.dropAllOperands(),g.parentBlock&&g.parentBlock.removeOp(g);return h}var vl=M(()=>{"use strict";At();Ui();sr();zi();ji();l(wl,"getYieldOp");l(Tl,"countInnerOps");l(xl,"countReductions");l(Sl,"allInnerOpsFusable");l(z$,"blockPositionIndex");l(dT,"makeComesBefore");l(kl,"materializeFusionGroup")});var Qg,iB=M(()=>{"use strict";nn();Oa();Jg();Zg();vl();Kt();Qg=class extends Ge{static{l(this,"FusionPass")}constructor(e={}){super("FusionPass");let t=e.target||{};this.legality=new _l({maxFusionSize:t.maxFusionSize||e.maxFusionSize,maxSharedMemory:t.sharedMemoryBytes||e.maxSharedMemory,libraryOps:t.libraryOps||e.libraryOps,allowReductionFusion:e.allowReductionFusion}),this.costModel=new bi({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 Yg(this.legality)}run(e,t){let n=this.groupBuilder.buildAllGroups(e),s=[];for(let i of n)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)kl(i,je.ELEMENTWISE);return oe.CHANGED}_traceDecision(e,t,n){if(!this.trace||this.trace.level<ue.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:n||null,level:ue.DEBUG}),this.trace.explain("fusion",s.join("+"),t?"fused":"not-fused",n||null,{groupSize:s.length})}_createsCycle(e,t){let n=t.opSet,s=t.getOutputValues(),o=new Set,i=[];for(let u of s)for(let p of u.uses())n.has(p.user)||(i.push(p.user),o.add(p.user));let a=0;for(;a<i.length;){let u=i[a++];if(n.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 Xg(r){return lu(r.opName)||ln(r.opName)}function aB(r){if(Xg(r))return!0;let e=ne.get(r.opName);return e!==null&&e.isElementwise}function G$(r,e){let t=r.getOperand(0).definingOp;return t&&e.has(t)&&!Xg(t)?r.getOperand(1).definingOp:t}function j$(r,e){if(!r.parentBlock||r.parentBlock!==e.parentBlock)return!1;let t=r.parentBlock.firstOp;for(;t;){if(t===r)return!0;if(t===e)return!1;t=t._next}return!1}function q$(r,e){for(let t of r)if(t!==e){for(let n=0;n<t.numResults;n++)for(let s of t.getResult(n).uses())if(!r.has(s.user))return!0}return!1}function U$(r,e){let t=ne.get(r.opName),n=t&&t.getAttr("epilogueTag");return n?n(r,e):"activation"}function K$(r){let e=[],t=new Set,n=new Set;function s(f){if(!(n.has(f)||f===r)){n.add(f);for(let d=0;d<f.numOperands;d++){let m=f.getOperand(d).definingOp;m&&m!==r&&!n.has(m)&&Xg(m)&&s(m)}e.push(f),t.add(f)}}l(s,"absorb");let o=[],i=r.getResult(0);for(let f of i.uses())aB(f.user)&&o.push(f.user);for(;o.length>0;){let f=o.pop();if(!n.has(f)){s(f);for(let d=0;d<f.numResults;d++)for(let m of f.getResult(d).uses())!n.has(m.user)&&aB(m.user)&&o.push(m.user)}}let a=[];for(let f of e)Xg(f)||a.push(U$(f,t));let u=e[e.length-1];for(let f=e.length-1;f>=0;f--){let d=e[f];for(let m=0;m<d.numResults;m++){for(let g of d.getResult(m).uses())if(!t.has(g.user)){u=d,f=-1;break}if(f<0)break}}let p=new Set;for(let f=0;f<r.numOperands;f++)p.add(r.getOperand(f));let c=[],h=new Set;for(let f of e)for(let d=0;d<f.numOperands;d++){let m=f.getOperand(d);if(h.has(m))continue;h.add(m);let g=m.definingOp;g!==r&&(g&&t.has(g)||p.has(m)||c.push(m))}return{chain:e,chainSet:t,tags:a,lastOp:u,extras:c}}function W$(r){let e=null,t=null,n=null,s=null,o=new Set;for(let i=0;i<2;i++){let a=r.getOperand(i),u=a.definingOp;if(!u||u.opName!=="convert")continue;let p=!1;for(let h of a.uses())if(h.user!==r){p=!0;break}if(p)continue;let c=u.getAttr("target_dtype")||u.getResult(0).type.dtype;i===0?(e=c,n=u.getOperand(0)):(t=c,s=u.getOperand(0)),o.add(u)}return{lhsCast:e,rhsCast:t,lhsInput:n,rhsInput:s,removed:o}}var V$,ey,uB=M(()=>{"use strict";nn();sr();At();Tr();Kt();l(Xg,"isPassthrough");l(aB,"isEpilogueCandidate");V$=new Map([["add",(r,e)=>e.has(G$(r,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(G$,"resolveOtherOperand");for(let[r,e]of V$)ne.has(r)&&ne.registerOpAttr(r,"epilogueTag",e);l(j$,"comesBefore");l(q$,"hasEscapingUse");l(U$,"classifyTag");l(K$,"collectChainAndAnalyze");l(W$,"collectPrologue");ey=class extends Ge{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,n=[];for(let s of e.ops()){let o=ne.get(s.opName);o&&o.isOutEWiseFusable&&n.push(s)}for(let s of n){let o=K$(s);if(o.chain.length>this.maxEpilogueOps)continue;let i=W$(s),a=o.chain.length>0,u=i.lhsCast!==null||i.rhsCast!==null;if(!a&&!u)continue;let{chain:p,tags:c,extras:h}=o,f=0;for(let k of c)k==="bias"||k==="residual_add"||k==="scale"?f++:k==="clamp"&&(f+=2);if(f!==h.length)continue;let d=i.lhsInput||s.getOperand(0),m=i.rhsInput||s.getOperand(1),g=[d,m,...h],y=a?o.lastOp:s,_=y.getResult(0).type,b=new Map(s.attributes);b.set("epilogue_ops",p.map(k=>k.opName)),b.set("epilogue_tags",c),b.set("num_dot_operands",2),b.set("num_extra_inputs",h.length),i.lhsCast&&b.set("lhs_prologue_cast",i.lhsCast),i.rhsCast&&b.set("rhs_prologue_cast",i.rhsCast);let w=new Re("fused_dot_epilogue",g,[_],b),T=s.parentBlock;if(!T)continue;let S=new Set(p);S.add(s);for(let k of i.removed)S.add(k);if(q$(S,y))continue;let x=null;for(let k of g){let A=k.definingOp;!A||S.has(A)||(!x||!j$(A,x))&&(x=A)}x&&x.parentBlock===T?T.insertAfter(w,x):T.insertBefore(w,s),y.getResult(0).replaceAllUsesWith(w.getResult(0));for(let k=p.length-1;k>=0;k--)p[k].dropAllOperands(),p[k].parentBlock&&p[k].parentBlock.removeOp(p[k]);s.dropAllOperands(),s.parentBlock&&s.parentBlock.removeOp(s);for(let k of i.removed)k.dropAllOperands(),k.parentBlock&&k.parentBlock.removeOp(k);t=!0}return this.trace&&this.trace.level>=ue.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,dotsFound:n.length,changed:t,level:ue.DEBUG}),t?oe.CHANGED:oe.UNCHANGED}}});var ty,lB=M(()=>{"use strict";nn();sr();zi();Oe();At();Kt();Oa();vl();ty=class extends Ge{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 n=new Set(t),s=this._buildProducerConsumerEdges(t,n);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,n)||(this._merge(u,p,c),a.add(u),a.add(p),o=!0,i++));return this.trace&&this.trace.level>=ue.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:t.length,edges:s.length,mergeCount:i,level:ue.DEBUG}),o?oe.CHANGED:oe.UNCHANGED}_buildProducerConsumerEdges(e,t){let n=[],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}`,h=s.get(c);h||(h={producer:o,consumer:p,sharedResults:new Map},s.set(c,h),n.push(h)),h.sharedResults.set(i,u.operandIndex)}}return n}_canMerge(e,t){let n=Tl(e),s=Tl(t);if(n+s>this.maxFusionSize)return!1;let o=0;return o+=xl(e),o+=xl(t),!(o>this.maxReductions||!Sl(e)||!Sl(t))}_shouldMerge(e,t,n){let s=0;for(let[u]of n){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,n){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,n){let s=e.regions[0].entryBlock,o=t.regions[0].entryBlock,i=wl(s),a=wl(o);if(!i||!a)return;let u=new Map;for(let[N,v]of n)u.set(v,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 h=new Map;for(let N=0;N<t.numOperands;N++){if(u.has(N))continue;let v=t.getOperand(N),O=-1;for(let R=0;R<p.length;R++)if(p[R]===v){O=R;break}O>=0?h.set(N,O):(h.set(N,p.length),p.push(v))}let f=p.map(N=>N.type),d=new Ct,m=new yn(f);d.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 v=i.getOperand(N),O=g.get(v)||v;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 v=h.get(N);v!==void 0&&g.set(o.arguments[N],m.arguments[v])}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 v=!1;for(let O of e.getResult(N).uses())if(O.user!==t){v=!0;break}if(v){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 v=a.getOperand(N),O=g.get(v)||v;y.push(O),_.push(t.getResult(N).type)}let w=new Re("yield",y,[]);m.pushOp(w);let T=[];for(let N of m.ops())N.opName!=="yield"&&T.push(N);let S=bl(T),x=new Re("fusion",p,_,{fusion_kind:S},[d]),k=t;if(!k.parentBlock)return;k.parentBlock.insertBefore(x,k);let A=0;for(let{resultIdx:N}of b)e.getResult(N).replaceAllUsesWith(x.getResult(A)),A++;for(let N=0;N<t.numResults;N++)t.getResult(N).replaceAllUsesWith(x.getResult(A)),A++;e.dropAllOperands(),e.parentBlock&&e.parentBlock.removeOp(e),t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t)}_estimateRecomputeCost(e){let t=0,n=e.regions[0]?.entryBlock;if(!n)return 0;for(let s of n.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 H$(r,e){let t=Math.min(r.id,e.id),n=Math.max(r.id,e.id);return`${t}|${n}`}var sf,cB=M(()=>{"use strict";nn();sr();zi();Oe();Oa();Kt();vl();sf=class extends Ge{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 n=this._findCandidates(t);if(n.length===0)return oe.UNCHANGED;let s=!1,o=0,i=new Set;for(let{left:a,right:u,sharedInputs:p,sharedBytes:c}of n)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>=ue.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:t.length,candidates:n.length,mergeCount:o,level:ue.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 n=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],h=i[p];if(c===h)continue;let f=H$(c,h),d=n.get(f);d||(d={left:c,right:h,sharedInputs:new Set,sharedBytes:0},n.set(f,d)),d.sharedInputs.add(o)}}let s=[];for(let o of n.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 h=p.getOperand(c);if(h.id===a&&h.type instanceof I){let f=h.type.sizeInBytes();f!==X&&(i+=f);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 n=Tl(e),s=Tl(t);if(n+s>this.maxFusionSize||e.numResults+t.numResults>this.maxOutputs)return!1;let i=0;return i+=xl(e),i+=xl(t),!(i>this.maxReductions||!Sl(e)||!Sl(t))}_mergeCreatesCycle(e,t){let n=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&&!n.has(u.user)&&(n.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())n.has(u.user)||(n.add(u.user),s.push(u.user))}n.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&&!n.has(u.user)&&(n.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())n.has(u.user)||(n.add(u.user),s.push(u.user))}return!1}_mergeMultiOutput(e,t,n){let s=e.regions[0].entryBlock,o=t.regions[0].entryBlock,i=wl(s),a=wl(o);if(!i||!a)return;let u=[],p=new Map,c=new Map;for(let v=0;v<e.numOperands;v++){let O=e.getOperand(v),R=O.id,B=p.get(R);B===void 0&&(B=u.length,p.set(R,B),u.push(O)),c.set(v,B)}let h=new Map;for(let v=0;v<t.numOperands;v++){let O=t.getOperand(v),R=O.id,B=p.get(R);B===void 0&&(B=u.length,p.set(R,B),u.push(O)),h.set(v,B)}let f=u.map(v=>v.type),d=new Ct,m=new yn(f);d.addBlock(m);let g=new Map;for(let v=0;v<s.arguments.length;v++)g.set(s.arguments[v],m.arguments[c.get(v)]);for(let v of s.ops())v.opName!=="yield"&&m.pushOp(v.clone(g));for(let v=0;v<o.arguments.length;v++)g.set(o.arguments[v],m.arguments[h.get(v)]);for(let v of o.ops())v.opName!=="yield"&&m.pushOp(v.clone(g));let y=[],_=[];for(let v=0;v<i.numOperands;v++){let O=i.getOperand(v);y.push(g.get(O)||O),_.push(e.getResult(v).type)}for(let v=0;v<a.numOperands;v++){let O=a.getOperand(v);y.push(g.get(O)||O),_.push(t.getResult(v).type)}let b=new Re("yield",y,[]);m.pushOp(b);let w=[];for(let v of m.ops())v.opName!=="yield"&&w.push(v);let T=bl(w),S=new Re("fusion",u,_,{fusion_kind:T},[d]),x=e.parentBlock;if(!x)return;let k=dT(x),A=null;for(let v of u){let O=v.definingOp;!O||O===e||O===t||(!A||!k(O,A))&&(A=O)}A&&A.parentBlock===x?x.insertAfter(S,A):x.insertBefore(S,e);for(let v=0;v<e.numResults;v++)e.getResult(v).replaceAllUsesWith(S.getResult(v));let N=e.numResults;for(let v=0;v<t.numResults;v++)t.getResult(v).replaceAllUsesWith(S.getResult(N+v));e.dropAllOperands(),e.parentBlock&&e.parentBlock.removeOp(e),t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t)}_hasProducerConsumerEdge(e,t){for(let n=0;n<e.numResults;n++)for(let s of e.getResult(n).uses())if(s.user===t)return!0;return!1}};l(H$,"pairKey")});var mT,at,Es=M(()=>{"use strict";mT=class{static{l(this,"UseDefResult")}constructor(e,t,n,s,o){this.topologicalOrder=e,this.valueToOp=t,this.opUsers=n,this.depth=s,this.height=o}},at=class{static get name(){return"use_def"}static get depKey(){return"useDef"}static get dependencies(){return[]}static compute(e){let t=[],n=new Map,s=new Map,o=new Set,i=new Set,a=l(c=>{if(o.has(c))return;i.add(c);let h=[{op:c,i:0}];for(;h.length>0;){let f=h[h.length-1],d=f.op;if(f.i<d.numOperands){let m=d.getOperand(f.i).definingOp;if(f.i++,m&&!o.has(m)){if(i.has(m))throw new Error("Cycle detected in UseDefAnalysis");i.add(m),h.push({op:m,i:0})}continue}i.delete(d),o.add(d),t.push(d),h.pop()}},"visit");for(let c of e.ops()){for(let h=0;h<c.numResults;h++)n.set(c.getResult(h),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 h=0;for(let f=0;f<c.numOperands;f++){let d=c.getOperand(f).definingOp;if(d){s.get(d).add(c);let m=u.get(d)||0;m+1>h&&(h=m+1)}}u.set(c,h)}for(let c=t.length-1;c>=0;c--){let h=t[c],f=0,d=s.get(h)||new Set;for(let m of d){let g=p.get(m)||0;g+1>f&&(f=g+1)}p.set(h,f)}return new mT(t,n,s,u,p)}}});var gT,ny,pB=M(()=>{"use strict";Es();Tr();gT=class{static{l(this,"DominanceResult")}constructor(e){this.idom=e}postDominates(e,t){let n=t;for(;n;){if(n===e)return!0;n=this.idom.get(n)}return!1}immediatePDom(e){return this.idom.get(e)||null}pathToPDom(e){let t=[],n=this.idom.get(e);for(;n&&(t.push(n),n!==this.idom.get(n));)n=this.idom.get(n);return t}},ny=class{static get name(){return"post_dominance"}static get depKey(){return"postDominance"}static get dependencies(){return[at]}static compute(e,t={}){let s=(t.useDef||at.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)qt(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,h=new Map;c.set(a,0),h.set(a,new Array(u).fill(a));let f=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[_]=h.get(y[_-1])[_-1];h.set(m,y)},"link"),d=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=h.get(m)[_]);if(m===g)return m;for(let _=u-1;_>=0;_--)h.get(m)[_]!==h.get(g)[_]&&(m=h.get(m)[_],g=h.get(g)[_]);return h.get(m)[0]},"lca");for(let m=s.length-1;m>=0;m--){let g=s[m];if(i.has(g)){f(g,a);continue}let y=null;for(let _ of o.get(g))p.has(_)&&(y=y===null?_:d(y,_));y!==null&&f(g,y)}for(let[m,g]of p)g===a&&p.delete(m);return new gT(p,s)}}});function fB(r){return qt(r)||ln(r)}var ry,hB=M(()=>{"use strict";nn();At();Oa();Jg();Zg();pB();Kt();Es();vl();Tr();l(fB,"isSkipOp");ry=class extends Ge{static{l(this,"DominatorFusionPass")}constructor(e={}){super("DominatorFusionPass"),this.requiredAnalyses=[at];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 bi({memoryBandwidthGBs:t.memoryBandwidthGBs,computeTFLOPs:t.computeTFLOPs,maxRegistersPerThread:t.registersPerThread,maxSharedMemory:t.sharedMemoryBytes,libraryOps:t.libraryOps,...e.cost})}run(e,t){let n=t?t.getAnalysis(at,e):at.compute(e),s=ny.compute(e,{useDef:n}),o=n.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)kl(u,je.ELEMENTWISE);return this.trace&&this.trace.level>=ue.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,groupsBuilt:i.length,groupsFused:a.length,level:ue.DEBUG}),oe.CHANGED}_explain(e,t,n){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",n||null,{groupSize:s.length,strategy:"dominator"})}_buildGroups(e,t){let n=new Map,s=[],o=0;for(let p=e.length-1;p>=0;p--){let c=e[p];if(fB(c.opName))continue;let h=Aa(c);if(h===je.OPAQUE||this.libraryOps.has(c.opName))continue;let f=t.immediatePDom(c);if(!f||fB(f.opName))continue;let d=Aa(f);if(d===je.OPAQUE||this.libraryOps.has(f.opName)||!this._canFusePatterns(h,d)||!this._pathAllFusable(c,f,e,t))continue;let m=n.get(f);if(m){if(m.size+1>this.maxFusionSize||!this._checkReductionLimit(m,c))continue;m.addOp(c),n.set(c,m),this._absorbIntermediates(c,f,m,n,e,t)}else{let g=n.get(c);if(g){if(g.size+1>this.maxFusionSize||!this._checkReductionLimit(g,f))continue;g.addOp(f),n.set(f,g),this._absorbIntermediates(c,f,g,n,e,t)}else{let y=new Eo(o++);if(y.addOp(c),y.addOp(f),!this._checkGroupReductions(y))continue;n.set(c,y),n.set(f,y),s.push(y),this._absorbIntermediates(c,f,y,n,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 n.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 n=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(!n.has(o)){if(n.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 fT(e,t)}_pathAllFusable(e,t,n,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),Aa(c)===je.OPAQUE||this.libraryOps.has(c.opName))return!1;i.push(c)}}return!0}_absorbIntermediates(e,t,n,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 h of p.getResult(c).uses()){let f=h.user;f===t||a.has(f)||!i.postDominates(t,f)||(a.add(f),Aa(f)===je.OPAQUE||this.libraryOps.has(f.opName))||(n.size<this.maxFusionSize&&this._checkReductionLimit(n,f)&&(n.addOp(f),s.set(f,n)),u.push(f))}}}_checkGroupReductions(e){let t=0;for(let n of e.ops){let s=ne.get(n.opName);s&&s.isReduction&&t++}return t<=this.maxReductions}_checkReductionLimit(e,t){let n=ne.get(t.opName);if(!n||!n.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 sy,dB=M(()=>{"use strict";sy=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 n=0;n<e;n++)this._parent[n]=n,this._rank[n]=n,this._nodeAtRank[n]=n,this._out[n]=new Set,this._in[n]=new Set;if(t)for(let[n,s]of t)n!==s&&(this._out[n].add(s),this._in[s].add(n))}find(e){let t=this._parent,n=e;for(;t[n]!==n;)n=t[n];for(;t[e]!==n;){let s=t[e];t[e]=n,e=s}return n}wouldCreateCycle(e,t){let n=this.find(e),s=this.find(t);if(n===s)return!1;let o=this._rank[n]<this._rank[s]?n:s,i=o===n?s:n;return this._hasIntermediatePath(o,i)}_hasIntermediatePath(e,t){let n=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]>=n||s.has(u)||(s.add(u),o.push(u))}}}return!1}merge(e,t){let n=this.find(e),s=this.find(t);if(n===s)return n;let o=this._rank[n]<this._rank[s]?n:s,i=o===n?s:n,a=this._rank[o],u=this._rank[i],p=this._out[n].size+this._in[n].size,c=this._out[s].size+this._in[s].size,h=p>=c?n:s,f=h===n?s:n,d=this._out[h],m=this._in[h];for(let g of this._in[f]){let y=this.find(g);y===h||y===f||(this._out[y].delete(f),this._out[y].add(h),m.add(y))}for(let g of this._out[f]){let y=this.find(g);y===h||y===f||(this._in[y].delete(f),this._in[y].add(h),d.add(y))}return d.delete(f),m.delete(f),d.delete(h),m.delete(h),this._parent[f]=h,this._nodeAtRank[this._rank[f]]=-1,this._reorder(a,u),h}_reorder(e,t){let n=[],s=[];for(let c=e;c<=t;c++){let h=this._nodeAtRank[c];if(!(h<0)){if(this.find(h)!==h){this._nodeAtRank[c]=-1;continue}n.push(c),s.push(h)}}if(s.length<=1){for(let c=0;c<s.length;c++)this._rank[s[c]]=n[c],this._nodeAtRank[n[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 h of this._out[c]){let f=this.find(h);f!==c&&o.has(f)&&i.set(f,i.get(f)+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 h of this._out[c]){let f=this.find(h);if(f!==c&&o.has(f)){let d=i.get(f)-1;i.set(f,d),d===0&&a.push(f)}}}for(let c=0;c<u.length;c++){let h=u[c],f=n[c];this._rank[h]=f,this._nodeAtRank[f]=h}}}});var oy,mB=M(()=>{"use strict";oy=class{static{l(this,"MaxHeap")}constructor(){this._items=[]}get size(){return this._items.length}isEmpty(){return this._items.length===0}push(e,t){let n=this._items;n.push({priority:e,value:t});let s=n.length-1;for(;s>0;){let o=s-1>>1;if(n[o].priority>=n[s].priority)break;let i=n[o];n[o]=n[s],n[s]=i,s=o}}pop(){let e=this._items,t=e.length;if(t===0)return;let n=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 n.value}}});function Y$(r){let e=ne.get(r.opName);return!e||e.isConstant||e.isTerminator||e.isOpaque?!1:e.isReduction?!0:ko(r.opName)}var iy,gB=M(()=>{"use strict";nn();At();Oe();Oa();Jg();Zg();Es();dB();mB();vl();Ui();Kt();l(Y$,"isFusibleOp");iy=class extends Ge{static{l(this,"PriorityFusionPass")}constructor(e={}){super("PriorityFusionPass"),this.requiredAnalyses=[at];let t=e.target||{};this.maxReductions=e.maxReductions??1,this.legality=new _l({maxFusionSize:t.maxFusionSize||e.maxFusionSize,maxSharedMemory:t.sharedMemoryBytes||e.maxSharedMemory,libraryOps:t.libraryOps||e.libraryOps,allowReductionFusion:e.allowReductionFusion});let n=t.getAttr&&t.getAttr("fusionBenefitWeights")||e.benefitWeights;this.costModel=new bi({memoryBandwidthGBs:t.memoryBandwidthGBs,computeTFLOPs:t.computeTFLOPs,maxRegistersPerThread:t.registersPerThread,maxSharedMemory:t.sharedMemoryBytes,libraryOps:t.libraryOps,policy:t.getAttr?t.getAttr("fusionPolicy"):null,benefitWeights:n,...e.cost})}run(e,t){let s=(t?t.getAnalysis(at,e):at.compute(e)).topologicalOrder,o=s.length;if(o===0)return oe.UNCHANGED;let i=new Map;for(let x=0;x<o;x++)i.set(s[x],x);let a=[];for(let x=0;x<o;x++){let k=s[x];for(let A=0;A<k.numOperands;A++){let N=k.getOperand(A).definingOp;N&&i.has(N)&&a.push([i.get(N),x])}}let u=new sy(o,a),p=new Int32Array(o),c=new Map;for(let x=0;x<o;x++)if(Y$(s[x])){let k=new Eo(x);k.addOp(s[x]),c.set(x,k)}let h=new oy,f=l((x,k)=>{let A=new Eo(-1);for(let N of x.ops)A.addOp(N);for(let N of k.ops)A.addOp(N);return A},"mergedGroup"),d=l(x=>{if(x.size<2||!x.allOpsInlineFusable())return!1;let k=0;for(let A of x.ops){let N=ne.get(A.opName);N&&N.isReduction&&k++}return k>this.maxReductions||(x.classifyKind(),x.kind===je.OPAQUE)?!1:this.costModel.shouldFuse(x).fuse===!0},"legalMerge"),m=l((x,k)=>{let A=0;for(let N=0;N<k.numOperands;N++){let v=k.getOperand(N);if(v.definingOp===x&&v.type instanceof I){let O=v.type.sizeInBytes();O!==X&&(A+=O)}}return A},"edgeBytes"),g=l((x,k)=>{let A=u.find(i.get(x)),N=u.find(i.get(k));if(A===N||!c.has(A)||!c.has(N)||!this.legality.canFuse(x,k).legal)return;let v=this.costModel.edgeBenefit(m(x,k));h.push(v,{a:A,b:N,va:p[A],vb:p[N],prodOp:x,consOp:k})},"pushCandidate"),y=l((x,k)=>{let A=new Set;for(let N of x.ops){for(let v=0;v<N.numResults;v++)for(let O of N.getResult(v).uses()){let R=i.get(O.user);if(R===void 0)continue;let B=u.find(R);if(B===k||!c.has(B))continue;let E=k*o+B;A.has(E)||(A.add(E),g(N,O.user))}for(let v=0;v<N.numOperands;v++){let O=N.getOperand(v).definingOp;if(!O)continue;let R=i.get(O);if(R===void 0)continue;let B=u.find(R);if(B===k||!c.has(B))continue;let E=B*o+k;A.has(E)||(A.add(E),g(O,N))}}},"reEval"),_=new Set;for(let x=0;x<o;x++){if(!c.has(x))continue;let k=s[x];for(let A=0;A<k.numOperands;A++){let N=k.getOperand(A).definingOp;if(!N)continue;let v=i.get(N);if(v===void 0||!c.has(v))continue;let O=v*o+x;_.has(O)||(_.add(O),g(N,k))}}let b=0;for(;!h.isEmpty();){let x=h.pop(),k=u.find(x.a),A=u.find(x.b);if(k===A||p[k]!==x.va||p[A]!==x.vb)continue;let N=c.get(k),v=c.get(A);if(!N||!v||!this.legality.canFuse(x.prodOp,x.consOp).legal)continue;let O=f(N,v);if(!d(O)||u.wouldCreateCycle(k,A))continue;let R=u.merge(k,A),B=N.size>=v.size?N:v,E=B===N?v:N;B.merge(E),c.delete(k),c.delete(A),c.set(R,B),p[R]=(p[k]>p[A]?p[k]:p[A])+1,b++,y(B,R)}if(b===0)return oe.UNCHANGED;let w=[],T=new Set;for(let x of c.values())T.has(x)||x.size<2||(T.add(x),w.push(x));let S=0;for(let x of w)x.classifyKind(),this._explain(x),kl(x,je.ELEMENTWISE)&&S++;return this.trace&&this.trace.level>=ue.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,ops:o,merges:b,fusions:S,level:ue.DEBUG}),S>0?oe.CHANGED:oe.UNCHANGED}_explain(e){if(!this.trace||!this.trace.explainsEnabled)return;let t=e.ops.map(n=>n.opName);this.trace.explain("fusion",t.join("+"),"fused",null,{groupSize:t.length,strategy:"priority"})}}});function J$(r,e){return r===e?!0:!r||!e?!1:typeof r.equals=="function"?r.equals(e):!1}var wi,ay,yB=M(()=>{"use strict";Oe();wi=class{static{l(this,"LayoutPreference")}constructor(e,t,n=0){this.inputs=e,this.outputs=t,this.cost=n}},ay=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,n){if(!(n instanceof I)||J$(e,t))return 0;let s=n.numel();return s<0?1024:s*2}estimateBenefit(e,t,n){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*n;if(o==="reduce")return s*2*n;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 wi([t.preferredConvLayout,null],[t.preferredConvLayout]);if(t.isGPU()&&s===4){let o=new Et([0,2,3,1]);return new wi([o,null],[o])}if(t.isCPU()&&s===4){let o=new Et([0,2,3,1]);return new wi([o,null],[o])}return null}),this._rules.set("dot",(e,t)=>{let n=e.getOperand(0).type,s=e.getOperand(1).type;if(!n||!s)return null;let o=Et.rowMajor(n.rank);if(t.isCPU()&&s.rank===2){let a=Et.columnMajor(s.rank);return new wi([o,a],[o])}let i=Et.rowMajor(s.rank);return new wi([o,i],[o])}),this._rules.set("reduce",(e,t)=>{let n=e.getResult(0).type;return n?new wi([null],[Et.rowMajor(n.rank)]):null})}};l(J$,"layoutEquals")});function _B(r,e){let t=new Map,n=null,s=0;for(let i=0;i<r.numOperands;i++){let a=r.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,n=u)}if(n)return n;let o=r.getResult(0);return o&&o.type instanceof I?Et.rowMajor(o.type.rank):Et.rowMajor(1)}function Z$(r,e){return r===e?!0:!r||!e?!1:typeof r.equals=="function"?r.equals(e):!1}function bB(r){if(r instanceof Et)return r;if(r&&typeof r.toLayout=="function")try{return r.toLayout()}catch{}return r&&r.baseOrder&&!r.isBlocked?.()?new Et(r.baseOrder):r&&r.order?new Et(r.order):null}var yT,uy,wB=M(()=>{"use strict";Es();Oe();At();_n();Tr();yT=class{static{l(this,"LayoutAnalysisResult")}constructor(e,t,n){this.assignments=e,this.conversions=t,this.totalCost=n}},uy=class{static get name(){return"layout"}static get depKey(){return"layout"}static get dependencies(){return[at]}static compute(e,t,n){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||Et.rowMajor(p.type.rank));for(let p=0;p<i.length;p++){let c=i[p];if(qt(c.opName))continue;let h=n?n.getPreference(c):null;if(h&&h.outputs.length>0){for(let m=0;m<c.numResults;m++){let g=c.getResult(m);if(!(g.type instanceof I))continue;let y=h.outputs[m]||null;y?o.set(g,y):o.set(g,_B(c,o))}continue}let f=ne.get(c.opName),d=f&&f.hasTrait(H.ELEMENTWISE);for(let m=0;m<c.numResults;m++){let g=c.getResult(m);g.type instanceof I&&(d?o.set(g,_B(c,o)):o.set(g,Et.rowMajor(g.type.rank)))}}let a=[],u=0;for(let p=0;p<i.length;p++){let c=i[p],h=n?n.getPreference(c):null;if(h)for(let f=0;f<c.numOperands;f++){let d=c.getOperand(f);if(!(d.type instanceof I))continue;let m=o.get(d);if(!m)continue;let g=h.inputs[f];if(!g||Z$(m,g))continue;let y=bB(m),_=bB(g);!y||!_||y.equals(_)||(a.push({value:d,consumer:c,operandIdx:f,from:y,to:_}),n&&(u+=n.estimateConversionCost(y,_,d.type)))}}return new yT(o,a,u)}};l(_B,"resolveFromInputs");l(Z$,"layoutEquals");l(bB,"toPlainLayout")});function Q$(r,e,t){let n=r.id,s=e.hash?e.hash():0,o=t.hash?t.hash():0;return`${n}:${s}:${o}`}var ly,TB=M(()=>{"use strict";nn();sr();Oe();yB();wB();Es();Kt();ly=class extends Ge{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 ay(this.target));let n=t?t.getAnalysis(at,e):at.compute(e),s=uy.compute(e,{useDef:n},this._policy);if(s.conversions.length===0)return oe.UNCHANGED;let o=new Map;for(let p of s.conversions){let{value:c,consumer:h,operandIdx:f,from:d,to:m}=p,g=Q$(c,d,m),y=o.get(g);y||(y={value:c,from:d,to:m,consumers:[],cost:this._policy.estimateConversionCost(d,m,c.type),benefit:0},o.set(g,y)),y.consumers.push({consumer:h,operandIdx:f});let _=this.target.layoutAwareOps&&this.target.layoutAwareOps.has(h.opName);y.benefit+=_?this._policy.estimateBenefit(h,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 Et?p.from.order:Array.from({length:p.value.type.rank},(y,_)=>_),h=p.to instanceof Et?p.to.order:Array.from({length:p.value.type.rank},(y,_)=>_),f=new I(p.value.type.shape,p.value.type.dtype,p.to),d=new Re("layout_transform",[p.value],[f],{src_layout:[...c],dst_layout:[...h]}),m=p.value.definingOp;m&&m.parentBlock?m.parentBlock.insertAfter(d,m):p.consumers[0].consumer.parentBlock&&p.consumers[0].consumer.parentBlock.insertBefore(d,p.consumers[0].consumer);let g=d.getResult(0);for(let y of p.consumers)y.consumer.replaceOperand(y.operandIdx,g)}return this.trace&&this.trace.level>=ue.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,conversions:s.conversions.length,uniqueTransforms:u.length,level:ue.DEBUG}),oe.CHANGED}};l(Q$,"valueLayoutKey")});function xB(r,e){if(r===e)return!0;if(typeof r=="number"&&typeof e=="number")return r===e;if(typeof r!=typeof e||r.length!==e.length)return!1;for(let t=0;t<r.length;t++)if(r[t]!==e[t])return!1;return!0}function SB(r){let e=0;for(let t=0;t<r.length;t++)e=(e<<5)-e+r.charCodeAt(t)&2147483647;return e}function tF(r){if(typeof r=="number"){let t=new Float64Array([r]),n=new Uint32Array(t.buffer);return(n[0]^n[1])&2147483647}let e=r.length;for(let t=0;t<Math.min(r.length,8);t++){let n=new Float64Array([r[t]]),s=new Uint32Array(n.buffer);e=(e^(s[0]^s[1]))*16777619&2147483647}return e}var kr,X$,eF,hr,_T=M(()=>{"use strict";Oe();kr=Object.freeze({PER_TENSOR_SYMMETRIC:"per_tensor_symmetric",PER_TENSOR_ASYMMETRIC:"per_tensor_asymmetric",PER_CHANNEL:"per_channel",PER_GROUP:"per_group"}),X$=new Set(Object.values(kr)),eF=new Set(["i8","ui8"]),hr=class r{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||Gs(this.dtype)*8,this._hash=null}clampRange(){if(this.isSymmetric()){let n=2**(this.numBits-1)-1;return[-n,n]}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,n]=this.clampRange(),s=Math.round(e/this.getScalarScale()+this.getScalarZeroPoint());return Math.max(t,Math.min(n,s))}dequantize(e){return(e-this.getScalarZeroPoint())*this.getScalarScale()}quantizeArray(e){let[t,n]=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(n,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 n=this.getScalarScale(),s=this.getScalarZeroPoint();for(let o=0;o<e.length;o++)t[o]=(e[o]-s)*n;return t}_channelStride(e){let t=1;for(let n=this.axis+1;n<e.length;n++)t*=e[n];return t}quantizeArrayPerChannel(e,t){let[n,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),h=this.getZeroPointForChannel(p);a[u]=Math.max(n,Math.min(s,Math.round(e[u]/c+h)))}return a}dequantizeArrayPerChannel(e,t){let n=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/n)%s;o[i]=(e[i]-this.getZeroPointForChannel(a))*this.getScaleForChannel(a)}return o}quantizeArrayPerGroup(e){let[t,n]=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(n,Math.round(e[o]/a+u)))}return s}dequantizeArrayPerGroup(e){let t=new Array(e.length);for(let n=0;n<e.length;n++){let s=Math.floor(n/this.groupSize);t[n]=(e[n]-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===kr.PER_CHANNEL}isPerGroup(){return this.scheme===kr.PER_GROUP}isSymmetric(){return this.scheme===kr.PER_TENSOR_SYMMETRIC}equals(e){return this===e?!0:!(!(e instanceof r)||this.scheme!==e.scheme||this.dtype!==e.dtype||this.numBits!==e.numBits||this.axis!==e.axis||this.groupSize!==e.groupSize||!xB(this.scale,e.scale)||!xB(this.zeroPoint,e.zeroPoint))}hash(){if(this._hash!==null)return this._hash;let e=2166136261;return e=(e^SB(this.scheme))*16777619&2147483647,e=(e^SB(this.dtype))*16777619&2147483647,e=(e^this.numBits)*16777619&2147483647,e=(e^tF(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 r(e)}static fromRange(e,t,n,s="i8",o=8){if(n===kr.PER_TENSOR_SYMMETRIC){let f=Math.max(Math.abs(e),Math.abs(t)),d=2**(o-1)-1,m=f/d||1e-10;return new r({scheme:n,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),h=Math.max(i,Math.min(a,c));return new r({scheme:n,scale:p,zeroPoint:h,dtype:s,numBits:o})}static fromRangePerChannel(e,t,n,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 h=Math.max(Math.abs(e[c]),Math.abs(t[c]));a[c]=h/p||1e-10,u[c]=0}return new r({scheme:kr.PER_CHANNEL,scale:a,zeroPoint:u,axis:n,dtype:s,numBits:o})}static defaultForActivation(e,t="i8",n=8){return r.fromRange(-6,6,e,t,n)}static fromConstantArray(e,t,n="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),r.fromRange(o,i,t,n,s)}static fromConstantArrayPerChannel(e,t,n,s="i8",o=8){let i=t[n],a=1;for(let c=n+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 h=Math.floor(c/a)%i,f=e[c];f<u[h]&&(u[h]=f),f>p[h]&&(p[h]=f)}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 r.fromRangePerChannel(u,p,n,s,o)}static fromConstantArrayPerGroup(e,t,n="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,h=Math.min(c+t,e.length),f=0;for(let d=c;d<h;d++){let m=Math.abs(e[d]);m>f&&(f=m)}i[p]=f/u||1e-10,a[p]=0}return new r({scheme:kr.PER_GROUP,scale:i,zeroPoint:a,groupSize:t,dtype:n,numBits:s})}static isQuantizableDtype(e){return eF.has(e)}static isValidScheme(e){return X$.has(e)}};l(xB,"scaleEquals");l(SB,"hashStr");l(tF,"hashScaleValue")});function oF(r){for(let e=0;e<r.numOperands;e++){let t=r.getOperand(e).definingOp;if(t&&t.opName==="constant")return!0}return!1}function iF(r,e,t){for(let n=0;n<r.numOperands;n++){let s=r.getOperand(n);if(!e.has(s)&&(!(s.type instanceof I)||!Lt(s.type.dtype)))return!1}return r.numOperands>0}var nF,rF,sF,cy,py,kB=M(()=>{"use strict";nn();sr();Oe();At();Es();_T();Kt();Tr();nF=new Set(["softmax","sqrt","div","rsqrt","log","exp","tanh"]),rF=new Set(["dot","conv","add","mul","sub"]),sF=new Map([["dot","quantized_dot"],["conv","quantized_conv"]]);for(let[r,e]of sF)ne.has(r)&&ne.registerOpAttr(r,"quantizedVariant",e);cy=class{static{l(this,"QuantizationConfig")}constructor(e={}){this.scheme=e.scheme||kr.PER_TENSOR_SYMMETRIC,this.calibration=e.calibration||null,this.targetDtype=e.targetDtype||"i8",this.excludeOps=e.excludeOps||nF,this.quantizableOps=e.quantizableOps||rF,this.sensitivityThreshold=e.sensitivityThreshold||0,this.sensitivityResult=e.sensitivityResult||null,this.weightOnly=e.weightOnly||!1,this.target=e.target||null}},py=class extends Ge{static{l(this,"QuantizationPass")}constructor(e={}){super("QuantizationPass"),this.requiredAnalyses=[at],this.config=e instanceof cy?e:new cy(e)}run(e,t){let s=(t?t.getAnalysis(at,e):at.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(qt(c.opName))continue;if(i.excludeOps.has(c.opName)||!i.quantizableOps.has(c.opName)){for(let d=0;d<c.numOperands;d++)o.has(c.getOperand(d))&&(a=this._insertDequantBefore(c,d,c.getOperand(d),i)||a);continue}if(i.sensitivityResult&&i.sensitivityThreshold>0&&i.sensitivityResult.isSensitive(c,i.sensitivityThreshold)||i.weightOnly&&!oF(c))continue;let h=ne.get(c.opName),f=h?h.getAttr("quantizedVariant"):null;if(f&&iF(c,o,i)){i.scheme===kr.PER_CHANNEL&&this._canPerChannelDot(c,o)?a=this._replacePerChannelDot(c,i)||a:a=this._replaceWithNativeQuantized(c,f,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>=ue.DEBUG&&a&&this.trace.emit({type:"pass_detail",passName:this.name,opsProcessed:s.length,changed:a,level:ue.DEBUG}),a?oe.CHANGED:oe.UNCHANGED}_getQuantParams(e,t){let n=Gs(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 hr.fromRange(-Math.abs(o)||-1,Math.abs(o)||1,t.scheme,t.targetDtype,n);if(o&&typeof o.length=="number")return hr.fromConstantArray(o,t.scheme,t.targetDtype,n)}return e.type instanceof I&&Lt(e.type.dtype)?hr.defaultForActivation(t.scheme,t.targetDtype,n):null}_resolveQuantParams(e,t){let n=this._paramsByValue.get(e);if(n)return n;let s=e.definingOp;return s&&s.opName==="quantize"?new hr({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,n){let s=e.getResult(t),o=this._getQuantParams(s,n);if(!o)return null;let i=new I(s.type.shape,n.targetDtype),a=new Re("quantize",[s],[i],{scale:o.getScalarScale(),zero_point:o.getScalarZeroPoint(),scheme:n.scheme,target_dtype:n.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,n,s){let o=this._resolveQuantParams(n,s);if(!o)return!1;let i="f32",a=new I(n.type.shape,i),u=new Re("dequantize",[n],[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,n){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,n)||s)}for(let o=0;o<e.numResults;o++){let i=e.getResult(o);if(!(i.type instanceof I)||!Lt(i.type.dtype))continue;let a=this._insertQuantizeAfter(e,o,n);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,n,s){let o=[],i={};for(let[h,f]of e.attributes||[])i[h]=f;for(let h=0;h<e.numOperands;h++){let f=e.getOperand(h);if(n.has(f)){o.push(f);let _=this._resolveQuantParams(f,s);if(_){let b=h===0?"lhs":"rhs";if(t==="quantized_dot")i[`${b}_scale`]=_.getScalarScale(),i[`${b}_zero_point`]=_.getScalarZeroPoint();else{let w=h===0?"input":"kernel";i[`${w}_scale`]=_.getScalarScale(),i[`${w}_zero_point`]=_.getScalarZeroPoint()}}continue}let d=this._getQuantParams(f,s);if(!d)return!1;let m=new I(f.type.shape,s.targetDtype),g=new Re("quantize",[f],[m],{scale:d.getScalarScale(),zero_point:d.getScalarZeroPoint(),scheme:s.scheme,target_dtype:s.targetDtype});e.parentBlock&&e.parentBlock.insertBefore(g,e),o.push(g.getResult(0));let y=h===0?t==="quantized_dot"?"lhs":"input":t==="quantized_dot"?"rhs":"kernel";i[`${y}_scale`]=d.getScalarScale(),i[`${y}_zero_point`]=d.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 Re(t,o,[p],i);if(e.parentBlock){e.parentBlock.insertBefore(c,e);let h=new I(e.getResult(0).type.shape,e.getResult(0).type.dtype),f=i.output_scale||1,d=new Re("dequantize",[c.getResult(0)],[h],{scale:f,zero_point:i.output_zero_point||0,scheme:s.scheme,target_dtype:e.getResult(0).type.dtype});e.parentBlock.insertBefore(d,e),e.replaceAllResultsWith([d.getResult(0)]),e.erase()}return!0}_canPerChannelDot(e,t){if(e.opName!=="dot")return!1;let n=e.getOperand(0),s=e.getOperand(1);if(t.has(n)||t.has(s)||!(n.type instanceof I)||n.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 n=Gs(t.targetDtype)*8,s=kr.PER_TENSOR_SYMMETRIC;return t.calibration&&t.calibration.hasData(e)?t.calibration.getQuantParams(e,s,t.targetDtype):hr.defaultForActivation(s,t.targetDtype,n)}_replacePerChannelDot(e,t){let n=e.getOperand(0),s=e.getOperand(1),o=s.type.shape,i=s.definingOp.getAttr("value"),a=Gs(t.targetDtype)*8,p=e.getAttr("rhs_contracting")[0]===0?1:0,c=hr.fromConstantArrayPerChannel([...i],o,p,t.targetDtype,a),h=c.quantizeArrayPerChannel([...i],o),f=this._activationParams(n,t),d=f.getScalarScale(),m=f.getScalarZeroPoint(),g=e.parentBlock;if(!g)return!1;let y=new I(o,t.targetDtype),_=new Re("constant",[],[y],{value:h,tensor_type:y});g.insertBefore(_,e);let b=new I(n.type.shape,t.targetDtype),w=new Re("quantize",[n],[b],{scale:d,zero_point:m,scheme:kr.PER_TENSOR_SYMMETRIC,target_dtype:t.targetDtype});g.insertBefore(w,e);let T=e.getResult(0).type.shape,S={};for(let[U,P]of e.attributes||[])S[U]=P;S.lhs_scale=d,S.lhs_zero_point=m,S.rhs_scale=1,S.rhs_zero_point=0,S.output_scale=1,S.output_zero_point=0;let x=new I(T,"i32"),k=new Re("quantized_dot",[w.getResult(0),_.getResult(0)],[x],S);g.insertBefore(k,e);let A=new I(T,"f32"),N=new Re("convert",[k.getResult(0)],[A],{target_dtype:"f32"});g.insertBefore(N,e);let v=o[p],O=new Array(v);for(let U=0;U<v;U++)O[U]=d*c.getScaleForChannel(U);let R=new Set(e.getAttr("lhs_contracting")||[]),B=0;for(let U=0;U<n.type.shape.length;U++)R.has(U)||B++;let E=B,z=new I([v],"f32"),D=new Re("constant",[],[z],{value:O,tensor_type:z});g.insertBefore(D,e);let J=new Re("broadcast_in_dim",[D.getResult(0)],[A],{broadcast_dimensions:[E],result_shape:T});g.insertBefore(J,e);let j=new Re("mul",[N.getResult(0),J.getResult(0)],[A],{});return g.insertBefore(j,e),e.replaceAllResultsWith([j.getResult(0)]),e.erase(),!0}};l(oF,"hasConstantOperand");l(iF,"allOperandsCanQuantize")});var bT,fy,vB=M(()=>{"use strict";Es();Oe();bT=class{static{l(this,"LivenessResult")}constructor(e,t,n,s,o,i,a){this.liveIn=e,this.liveOut=t,this.intervals=n,this.opIndex=s,this.peakPressure=o,this.peakOp=i,this.pressureAtOp=a}interfere(e,t){if(e===t)return!0;let n=this.intervals.get(e),s=this.intervals.get(t);return n&&s?n.start<=s.end&&s.start<=n.end:!1}liveAtOp(e){return this.liveIn.get(e)||new Set}intervalOf(e){return this.intervals.get(e)||null}},fy=class r{static get name(){return"liveness"}static get depKey(){return"liveness"}static get dependencies(){return[at]}static buildIntervals(e,t){let n=new Map;for(let o=0;o<t.length;o++)n.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:n}}static compute(e,t={}){let n=t.useDef||at.compute(e),s=n.topologicalOrder,{intervals:o,opIndex:i}=r.buildIntervals(e,s),a=new Map,u=new Map;for(let f of s)a.set(f,new Set),u.set(f,new Set);for(let f=s.length-1;f>=0;f--){let d=s[f],m=u.get(d),g=n.opUsers.get(d);if(g)for(let _ of g){let b=a.get(_);if(b)for(let w of b)m.add(w)}let y=a.get(d);for(let _ of m)y.add(_);for(let _=0;_<d.numResults;_++)y.delete(d.getResult(_));for(let _=0;_<d.numOperands;_++)y.add(d.getOperand(_))}for(let[f,d]of i){let m=a.get(f);for(let g of m){let y=o.get(g);y&&y.end<d&&(y.end=d)}}let p=0,c=null,h=new Map;for(let f=0;f<s.length;f++){let d=s[f],m=a.get(d),g=0;for(let y of m)if(y.type instanceof I){let _=y.type.sizeInBytes();_!==X&&(g+=_)}h.set(d,g),g>p&&(p=g,c=d)}return new bT(a,u,o,i,p,c,h)}}});var hy,dy,NB=M(()=>{"use strict";nn();sr();Oe();At();Kt();Es();vB();Tr();hy=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}},dy=class extends Ge{static{l(this,"RematerializationPass")}constructor(e={}){super("RematerializationPass"),this.config=e instanceof hy?e:new hy(e)}run(e,t){if(this.config.memoryBudget===1/0)return oe.UNCHANGED;let n=!1,s=0,o=0;for(;s<this.config.maxIterations;){let i=at.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,h)=>h.score-c.score),this._rematerialize(e,u[0],p),n=!0,s++}return this.trace&&this.trace.level>=ue.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,iterations:s,peakPressure:o,budget:this.config.memoryBudget,changed:n,level:ue.DEBUG}),n?oe.CHANGED:oe.UNCHANGED}_analyzeIntervalPressure(e,t){let n=t.topologicalOrder,{intervals:s,opIndex:o}=fy.buildIntervals(e,n),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,h=new Set,f=0;for(let m=-1;m<=n.length;m++){for(;f<i.length&&i[f].idx<=m;)a+=i[f].delta,i[f].delta>0&&i[f].value&&h.add(i[f].value),i[f].delta<0&&i[f].value&&h.delete(i[f].value),f++;if(a>u){u=a,p=m,c.clear();for(let g of h)c.add(g)}}let d=[];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&&d.push({value:m,definingOp:g,memorySaved:y,recomputeCost:_,score:y/_})}return{peakPressure:u,peakIdx:p,candidates:d,opIndex:o}}_canRematerialize(e){if(e.isBlockArgument())return!1;let t=e.definingOp;if(!t||qt(t.opName)||ln(t.opName)||this.config.excludeOps.has(t.opName)||t.regions.length>0||t.hasSideEffects()||e.useCount<=1)return!1;for(let n=0;n<t.numOperands;n++){let s=t.getOperand(n);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,n){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=n.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 n=t.getFlops(e);if(n>0)return n}if(t.isElementwise||t.isBroadcast||t.isView){let n=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){n=i;break}}}return n||1}if(t.isReduction)for(let n=0;n<e.numOperands;n++){let s=e.getOperand(n).type;if(s instanceof I){let o=s.numel();if(o!==X)return o}}return 1/0}_rematerialize(e,t,n){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,h)=>(n.get(c.user)||0)-(n.get(h.user)||0));let a=n.get(i[i.length-1].user)||0,u=this._operandExtensionCost(o,a,n),p=this._computeMemorySaved(s);if(!(u>=p))for(let c=1;c<i.length;c++){let h=i[c],f=h.user;if(!f.parentBlock)continue;let d=new Re(o.opName,[...o.operands],o.results.map(m=>m.type),new Map(o.attributes));f.parentBlock.insertBefore(d,f),f.replaceOperand(h.operandIndex,d.getResult(s.resultIndex))}}}});function oo(r){return ln(r.opName)}function AB(r){let e=0;for(let t=0;t<r.numResults;t++){let n=r.getResult(t).type;if(!n||!n.isFullyStatic)continue;let s=n.sizeInBytes();s>e&&(e=s)}return e}function uF(r){let e=r.definingOp;if(e&&e.opName==="broadcast"){let t=e.getOperand(0);e=t&&t.definingOp}if(e&&oo(e)){let t=e.getAttr("value");if(typeof t=="number")return t}return 0}function wT(r){if(r.opName!=="dot"&&r.opName!=="cublas_gemm")return null;let e=r.getOperand(0).type,t=r.getOperand(1).type,n=r.getResult(0).type;if(!e||!t||!n||e.dtype!=="f32"||t.dtype!=="f32"||n.dtype!=="f32")return null;let s=r.getAttr("lhs_batch")||[],o=r.getAttr("rhs_batch")||[];if(s.length>0||o.length>0)return null;let i=r.getAttr("lhs_contracting")||[],a=r.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||!n.isFullyStatic)return null;let u=r.getOperand(0).definingOp,p=r.getOperand(1).definingOp;if(u&&oo(u)||p&&oo(p))return null;let c=a[0]===1,h=e.shape[e.rank-1];if((c?t.shape[1]:t.shape[0])!==h)return null;let d=1;for(let g=0;g<e.rank-1;g++)d*=e.shape[g];let m=c?t.shape[0]:t.shape[1];return d<=0||m<=0||h<=0?null:{M:d,N:m,K:h,transB:c}}function OB(r){return{labelOf:l(e=>r.get(e),"labelOf"),canMerge:l((e,t)=>Math.max(e.maxBuf||0,AB(t))<=aF,"canMerge"),onAttach:l((e,t)=>{e.maxBuf=Math.max(e.maxBuf||0,AB(t))},"onAttach")}}function my(r,e,t){let n=r.opSet,s=Ba(r.ops),{inputs:o,outputs:i,constDefs:a}=_i(n,s,{pullConstants:!0,isConstant:oo});for(let f of o)if(!f.type||!f.type.isFullyStatic)return null;for(let f of i)if(!f.type||!f.type.isFullyStatic)return null;let u=new un(e,o.map(f=>f.type),i.map(f=>f.type)),p=new Map;for(let f=0;f<o.length;f++)p.set(o[f],u.args[f]);for(let f of a)u.entryBlock.pushOp(f.clone(p));for(let f of s)u.entryBlock.pushOp(f.clone(p));let c=i.map(f=>p.get(f));if(c.some(f=>f===void 0))return null;u.entryBlock.pushOp(new Re("return",c,[]));let h=r.ops.length===1&&t.has(r.ops[0])?r.ops[0]:null;return{part:r,subFunc:u,inputs:o,outputs:i,dotOp:h}}function of(r){if(lF.has(r.opName))return!0;if(r.regions)for(let e of r.regions){let t=e.entryBlock;if(t){for(let n of t.ops())if(of(n))return!0}}return!1}function cF(r){let e=0;for(let t=0;t<r.numResults;t++){let n=r.getResult(t).type;if(!n||!n.isFullyStatic)continue;let s=1;for(let o of n.shape)s*=o;s>e&&(e=s)}return e}function BB(r,e=256){if(r.functionCount!==1)return!1;let t=r.functions().next().value,n=new Map,s=l(o=>{if(!o)return 0;let i=n.get(o);if(i!==void 0)return i;n.set(o,0);let a=of(o)?cF(o):0;for(let u=0;u<o.numOperands;u++){let p=s(o.getOperand(u).definingOp);p>a&&(a=p)}return n.set(o,a),a},"maxBoundaryInSubtree");for(let o of t.ops())if(!(qt(o.opName)||!of(o))){for(let i=0;i<o.numOperands;i++)if(s(o.getOperand(i).definingOp)>e)return!0}return!1}function RB(r,e,t){let n=new Map,s=0,o=l(d=>{let m=n.get(d);return m===void 0&&(m=s++,n.set(d,m)),m},"getSlot");for(let d of r.args)o(d);for(let d of t)for(let m of d.outputs)o(m);let i=[];for(let d of r.args)i.push(o(d));let a=[],u=new Set;for(let d=0;d<e.numOperands;d++){let m=e.getOperand(d),g=i.length,y=m.isBlockArgument&&m.isBlockArgument(),_=m.definingOp&&oo(m.definingOp);if(!y&&!_&&n.has(m)){let b=n.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:uF(m)});continue}return null}let p=[];for(let d of t){let m=[];for(let y of d.inputs){let _=n.get(y);if(_===void 0)return null;m.push(_)}let g=d.outputs.map(y=>n.get(y));p.push({name:d.subFunc.name,inputSlots:m,outputSlots:g})}let c=new Set(i),h=[],f=new Set;for(let[d,m]of n)if(!(c.has(m)||f.has(m))){if(f.add(m),!d.type||!d.type.isFullyStatic)return null;h.push({slot:m,shape:[...d.type.shape],dtype:d.type.dtype})}return{plan:{numSlots:s,argSlots:i,intermediates:h,steps:p,returnFixups:a}}}function Ti(r,e=2){if(r.functionCount!==1)return null;let t=r.functions().next().value,n=t.getReturnOp();if(!n)return null;let s=[],o=new Map,i=0;for(let g of t.ops())qt(g.opName)||oo(g)||(of(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}=nf(s,OB(o));if(a.length<2)return null;let p=hT(a,u);if(!p)return null;let c=t.name,h=[],f=new Map,d=0;for(let g of p){let y=my(g,c+"_p"+d++,f);if(!y)return null;h.push(y)}let m=RB(t,n,h);if(!m)return null;r.removeFunction(t.name);for(let g of h)r.addFunction(g.subFunc);return{plan:m.plan}}function MB(r){if(r.functionCount!==1)return null;let e=r.functions().next().value,t=e.getReturnOp();if(!t)return null;let n=[],s=new Map,o=new Map,i=0,a=0;for(let y of e.ops()){if(qt(y.opName)||oo(y))continue;let _=wT(y);_?(s.set(y,"cublas#"+i),o.set(y,_),i++):of(y)?s.set(y,"boundary#"+a++):s.set(y,"native"),n.push(y)}if(i+a===0||n.length===0)return null;let{partitions:u,preds:p}=nf(n,OB(s));if(u.length<2)return null;let c=hT(u,p);if(!c)return null;let h=e.name,f=[],d=0;for(let y of c){let _=my(y,h+"_p"+d++,o);if(!_)return null;f.push(_)}let m=RB(e,t,f);if(!m)return null;let g=new Map;for(let y of f){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)),T=y.outputs.indexOf(y.dotOp.getResult(0));b<0||w<0||T<0||g.set(y.subFunc.name,{M:_.M,N:_.N,K:_.K,transB:_.transB,aIdx:b,bIdx:w,cIdx:y.inputs.length+T})}if(g.size===0)return null;r.removeFunction(e.name);for(let y of f)r.addFunction(y.subFunc);return{plan:m.plan,cublasInfos:g}}var aF,lF,gy=M(()=>{"use strict";xs();sr();rf();Tr();l(oo,"isConstantOp");aF=32*1024;l(AB,"maxResultBytes");l(uF,"constScalarOf");l(wT,"cublasDotInfo");l(OB,"bufferLimitedConfig");l(my,"materializePartition");lF=new Set(["dot","cublas_gemm","reduce","conv","quantized_conv","scaled_dot_product_attention"]);l(of,"containsBoundaryOp");l(cF,"maxBoundaryResultBytes");l(BB,"hasDependentBoundaries");l(RB,"buildExecutionPlan");l(Ti,"splitGraphForNative");l(MB,"splitGraphForCublas")});function EB(r){return IB.push(r),r}var IB,yy,LB=M(()=>{"use strict";nn();IB=[];l(EB,"registerBackendOpRewrite");yy=class extends Ge{static{l(this,"BackendOpRewritePass")}constructor(e={}){super(e.name||"BackendOpRewritePass"),this.config=e,this.rewrites=e.rewrites||IB}run(e){let t=!1;for(let n of[...e.ops()]){let s=n.parentBlock;if(s){for(let o of this.rewrites)if(o.match(n,this.config)){o.build(n,s,this.config),t=!0;break}}}return t?oe.CHANGED:oe.UNCHANGED}}});var pF,_y,CB=M(()=>{"use strict";sr();gy();LB();pF=EB({name:"dot->cublas_gemm",match:l(r=>r.opName==="dot"&&r.numOperands===2&&wT(r)!==null,"match"),build:l((r,e)=>{let t=r.results.map(s=>s.type),n=new Re("cublas_gemm",[r.getOperand(0),r.getOperand(1)],t,new Map(r.attributes),null);e.insertBefore(n,r),r.replaceAllResultsWith(n.results),r.erase()},"build")}),_y=class extends yy{static{l(this,"CublasRewritePass")}constructor(e={}){super({...e,name:"CublasRewritePass",rewrites:[pF]})}}});function DB(r,e,{cudaMatmulChain:t=!1,context:n=null}={}){let s=n?a=>n.passesForPhase(a,r,e):a=>Gg(a,r,e),o=[];for(let a of s("pre"))o.push(a);if(o.push(new Gu(e)),o.push(new Vu("canonicalize",[new ia,new Kg({fastMath:r.optimization.fastMath}),new Wg,new Hg,new no],r.optimization.maxSimplifyIterations)),r.optimization.layout&&e&&(o.push(new ly({target:e})),o.push(new no)),r.quantization.enabled&&(o.push(new py({...r.quantization,target:e})),o.push(new ia),o.push(new no)),r.matmulBackend!=="cublas"&&!t&&(r.fusion.epilogue!==void 0?r.fusion.epilogue:e&&e.enableEpilogueFusion)&&(o.push(new ey({target:e})),o.push(new no)),r.fusion.enabled){let a=r.fusion,u=a.launchOverheadUs??fF;a.strategy==="dominator"?o.push(new ry({target:e,...a})):a.strategy==="priority"?(o.push(new iy({target:e,cost:{launchOverheadUs:u},...a})),o.push(new sf({maxFusionSize:e?.maxFusionSize,...a}))):(o.push(new Qg({target:e,cost:{launchOverheadUs:u},...a})),o.push(new ty({maxFusionSize:e?.maxFusionSize,...a})),o.push(new sf({maxFusionSize:e?.maxFusionSize,...a}))),o.push(new no)}if(r.matmulBackend==="cublas"&&o.push(new _y),r.optimization.rematerialization){let a={...r.optimization.rematConfig};a.memoryBudget===void 0&&e&&e.memoryBudgetBytes>0&&(a.memoryBudget=e.memoryBudgetBytes),o.push(new dy(a))}for(let a of s("post"))o.push(a);return o}var fF,PB=M(()=>{"use strict";um();Dw();eB();rB();sB();$w();iB();uB();lB();cB();hB();gB();TB();kB();Lw();NB();CB();uT();fF=5;l(DB,"buildGraphPipeline")});var Wn,xi=M(()=>{"use strict";Wn=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 by,TT,Ra,xT=M(()=>{"use strict";De();by=["iterCount","depth","parallelLoops","vectorizedLoops","unrolledLoops","threadBoundLoops","serialLoops","threadBlockSize","gridSize","underReduction","numMathOps","numExternCalls","numReads","numWrites","stride1Accesses","stridedAccesses","reuseCount","touchedBytes","arithmeticIntensity","vectorized","parallelized","innermostExtent"],TT=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}},Ra=class r{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};r._visitIterative(e.body,t);let n=t.totalBufferBytes,s=t.numMathOps+t.numExternCalls;return t.arithmeticIntensity=n>0?s/n:0,new TT(t)}static extractStatements(e){let t=[],n=[],s=0,o=[{node:e.body,action:"enter"}];for(;o.length>0;){let{node:i,action:a}=o.pop();if(i){if(a==="leaveFor"){n.pop();continue}if(a==="leaveBlock"){s--;continue}switch(i.type){case"ForNode":n.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(r._statementVector(i,n,s));break}}}return t}static _statementVector(e,t,n){let s=1,o=0,i=0,a=0,u=0,p=0,c=1,h=1;for(let x of t){let k=x.extent&&x.extent.type==="IntImmNode"?x.extent.value:1;switch(s*=k,x.kind){case F.PARALLEL:o++;break;case F.VECTORIZED:i++;break;case F.UNROLLED:a++;break;case F.THREAD_BINDING:u++,x.threadTag&&x.threadTag.startsWith("threadIdx")?c*=k:x.threadTag&&x.threadTag.startsWith("blockIdx")&&(h*=k);break;default:p++;break}}let f={math:0,extern:0};r._countExpr(e.value,f);let d=t.map(x=>x.loopVar.name),m=[{buffer:e.buffer,indices:e.indices}];r._collectLoads(e.value,m);let g=0,y=0,_=0,b=0;for(let x of m){let k=x.buffer&&x.buffer.sizeInBytes?x.buffer.sizeInBytes():0;k>0&&(b+=k);let A=x.indices&&x.indices.length>0?x.indices[x.indices.length-1]:null;A&&A.type==="VariableNode"?g++:y++;let N=new Set;if(x.indices)for(let v of x.indices)r._collectVars(v,N);for(let v of d)N.has(v)||_++}let w=t.length>0?t[t.length-1]:null,T=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:h,underReduction:n>0?1:0,numMathOps:f.math,numExternCalls:f.extern,numReads:m.length-1,numWrites:1,stride1Accesses:g,stridedAccesses:y,reuseCount:_,touchedBytes:b,arithmeticIntensity:b>0?(f.math+f.extern)/b:0,vectorized:i>0?1:0,parallelized:o+u>0?1:0,innermostExtent:T};return by.map(x=>S[x]||0)}static _countExpr(e,t){if(!(!e||typeof e!="object"))switch(e.type){case"MathOpNode":t.math++,r._countExpr(e.a,t),e.b&&r._countExpr(e.b,t);break;case"CompareNode":t.math++,r._countExpr(e.a,t),r._countExpr(e.b,t);break;case"CallExternNode":t.extern++;for(let n of e.args)r._countExpr(n,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&&r._collectLoads(e.a,t),e.b&&r._collectLoads(e.b,t),e.args)for(let n of e.args)r._collectLoads(n,t)}}static _collectVars(e,t){if(!(!e||typeof e!="object")){if(e.type==="VariableNode"){t.add(e.name);return}if(e.a&&r._collectVars(e.a,t),e.b&&r._collectVars(e.b,t),e.args)for(let n of e.args)r._collectVars(n,t);if(e.indices)for(let n of e.indices)r._collectVars(n,t)}}static _visitIterative(e,t){let n=[{node:e,action:"enter"}];for(;n.length>0;){let{node:s,action:o}=n.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 F.PARALLEL:t.numParallelLoops++;break;case F.VECTORIZED:t.numVectorizedLoops++;break;case F.UNROLLED:t.numUnrolledLoops++;break;case F.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,n.push({node:null,action:"leave_for"}),n.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)r._visitBuffer(i.buffer,t);for(let i of s.writes)r._visitBuffer(i.buffer,t);n.push({node:s.body,action:"enter"}),s.initBody&&n.push({node:s.initBody,action:"enter"});break;case"SeqNode":for(let i=s.stmts.length-1;i>=0;i--)n.push({node:s.stmts[i],action:"enter"});break;case"AllocateNode":r._visitBuffer(s.buffer,t),n.push({node:s.body,action:"enter"});break;case"IfThenElseNode":s.elseBody&&n.push({node:s.elseBody,action:"enter"}),n.push({node:s.thenBody,action:"enter"});break;case"LetStmtNode":n.push({node:s.body,action:"enter"});break;case"BufferStoreNode":t.numBufferWrites++,r._visitBuffer(s.buffer,t),r._checkStride(s.buffer,s.indices,t),r._visitExpr(s.value,t);break}}}}static _visitBuffer(e,t){if(!e||t.buffersSeen.has(e))return;t.buffersSeen.add(e);let n=e.sizeInBytes();n>0&&(t.totalBufferBytes+=n)}static _visitExpr(e,t){if(e)switch(e.type){case"MathOpNode":t.numMathOps++,r._visitExpr(e.a,t),e.b&&r._visitExpr(e.b,t);break;case"CallExternNode":t.numExternCalls++;for(let n of e.args)r._visitExpr(n,t);break;case"BufferLoadNode":t.numBufferReads++,r._checkStride(e.buffer,e.indices,t);break;case"CompareNode":t.numMathOps++,r._visitExpr(e.a,t),r._visitExpr(e.b,t);break;default:break}}static _checkStride(e,t,n){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"?n.strideOneAccesses++:n.nonStrideOneAccesses++}}});function $B(r,e){if(r.length===0)return 0;let t=0;for(let n of r)t+=e[n];return t/r.length}function ST(r,e,t,n,s,o,i){let a=t.length;if(n>=s||a<o*2)return{leaf:$B(t,e)};let u=r[0].length,p=0,c=0,h=new Set(t);for(let g of t){let y=e[g];p+=y,c+=y*y}let f=null,d=null;for(let g=0;g<u;g++){let y=i[g].filter(w=>h.has(w)),_=0,b=0;for(let w=0;w<y.length-1;w++){let T=e[y[w]];_+=T,b+=T*T;let S=r[y[w]][g],x=r[y[w+1]][g];if(S===x)continue;let k=w+1,A=a-k;if(k<o||A<o)continue;let N=p-_,v=c-b,O=b-_*_/k+(v-N*N/A);(!f||O<f.cost)&&(f={f:g,thr:(S+x)/2,sIdx:w,cost:O},d=y)}}if(!f)return{leaf:$B(t,e)};let m=d;return{f:f.f,thr:f.thr,left:ST(r,e,m.slice(0,f.sIdx+1),n+1,s,o,i),right:ST(r,e,m.slice(f.sIdx+1),n+1,s,o,i)}}function hF(r){let e=r.length,t=e>0?r[0].length:0,n=new Array(t),s=[];for(let o=0;o<e;o++)s.push(o);for(let o=0;o<t;o++)n[o]=s.slice().sort((i,a)=>r[i][o]-r[a][o]);return n}function FB(r,e){for(;r&&r.leaf===void 0;)r=e[r.f]<=r.thr?r.left:r.right;return r?r.leaf:0}var af,zB=M(()=>{"use strict";l($B,"leafValue");l(ST,"buildTree");l(hF,"buildFeatureOrder");l(FB,"predictTree");af=class r{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 n=e.length;if(n===0)return;let s=0;for(let p of t)s+=p;s/=n;let o=new Array(n).fill(s),i=[];for(let p=0;p<n;p++)i.push(p);let a=hF(e),u=[];for(let p=0;p<this.numTrees;p++){let c=new Array(n);for(let f=0;f<n;f++)c[f]=t[f]-o[f];let h=ST(e,c,i,0,this.maxDepth,this.minSamples,a);for(let f=0;f<n;f++)o[f]+=this.lr*FB(h,e[f]);u.push(h)}this.trees=u,this.base=s}predict(e){if(!this.trees)return 0;let t=this.base;for(let n of this.trees)t+=this.lr*FB(n,e);return t}serialize(){return{trees:this.trees,base:this.base,lr:this.lr}}static deserialize(e){let t=new r({lr:e.lr});return t.trees=e.trees,t.base=e.base,t}}});function VB(r){let e=r[0].length,t=new Array(e+1).fill(0);for(let n of r)for(let s=0;s<e;s++){let o=n[s]||0;gF.has(s)?o>t[s]&&(t[s]=o):t[s]+=o}for(let n of yF)n<e&&(t[n]/=r.length);return t[e]=r.length,t}var dF,mF,gF,yF,kT,_F,wy,Nl,Al,vT=M(()=>{"use strict";xT();zB();dF=new Set(["depth","threadBlockSize","gridSize","underReduction","vectorized","parallelized","innermostExtent"]),mF=new Set(["arithmeticIntensity"]),gF=new Set(by.map((r,e)=>dF.has(r)?e:-1).filter(r=>r>=0)),yF=new Set(by.map((r,e)=>mF.has(r)?e:-1).filter(r=>r>=0));l(VB,"aggregateStatements");kT=class{static{l(this,"CostEstimate")}constructor(e,t){this.score=e,this.breakdown=t}},_F={parallelism:2,vectorization:1.5,memoryCoalescing:2,occupancy:1,arithmeticIntensity:1,loopOverhead:-.5,codeSize:-.3},wy=class{static{l(this,"AnalyticalCostModel")}constructor(e,t={}){this.target=e,this._weights={..._F,...e&&e.costModelWeights?e.costModelWeights:{},...t.weights||{}}}estimate(e){let t=Ra.extract(e);return this.estimateFromFeatures(t)}score(e){return this.estimate(e).score}estimateFromFeatures(e){let t=this._scoreParallelism(e),n=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*n+p.memoryCoalescing*s+p.occupancy*o+p.arithmeticIntensity*i+p.loopOverhead*a+p.codeSize*u;return new kT(c,{parallelism:t,vectorization:n,memoryCoalescing:s,occupancy:o,arithmeticIntensity:i,loopOverhead:a,codeSize:u})}_scoreParallelism(e){if(this.target.isGPU()){let n=e.threadBlockSize*e.gridSize,s=this.target.maxParallelism();return Math.min(1,n/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,n=Math.ceil(e.threadBlockSize/t),s=Math.floor(this.target.maxThreadsPerBlock/t);return Math.min(1,n/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}},Nl=class r{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?af.deserialize(e):null,this._X=[],this._Y=[]}addSample(e,t){!e||e.length===0||Number.isFinite(t)&&(this._X.push(VB(e)),this._Y.push(t))}train(){if(this._X.length===0)return;let e=new af(this.opts);e.fit(this._X,this._Y),this._gbt=e}predict(e){return!this._gbt||!e||e.length===0?0:this._gbt.predict(VB(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 r(e&&e.gbt?e.gbt:null)}},Al=class{static{l(this,"GuidedCostModel")}constructor(e,t,n={}){this.analytical=e,this.learned=t,this.confidenceSamples=n.confidenceSamples??8}_learnedConfident(){return this.learned&&this.learned.trained&&this.learned.sampleCount>=this.confidenceSamples}score(e){return this._learnedConfident()?this.learned.predict(Ra.extractStatements(e)):this.analytical.score(e)}}});function*GB(r){let e=[r];for(;e.length>0;){let t=e.pop();if(t){if(yield t,t.body&&e.push(t.body),t.stmts)for(let n of t.stmts)e.push(n);t.thenBody&&e.push(t.thenBody),t.elseBody&&e.push(t.elseBody),t.initBody&&e.push(t.initBody)}}}function uf(r,e){for(let t of GB(r))if(t.type==="BlockNode"&&t.name===e)return t;return null}function io(r){let e=[];for(let t of GB(r))t.type==="BlockNode"&&e.push(t.name);return e}function lf(r,e){let t=dt(r,e);if(!t)return{spatial:0,reduction:0,reads:0,hasReduction:!1};let n=0,s=0;for(let o of t.loops)t.reductionLoopVars.has(o.loopVar.name)?s++:n++;return{spatial:n,reduction:s,reads:t.readBuffers.length,hasReduction:t.hasReduction}}var Ol=M(()=>{"use strict";ro();l(GB,"walkStmts");l(uf,"findBlock");l(io,"collectAllBlockNames");l(lf,"analyzeBlockStructure")});function qB(r){return r.isGPU()?wF:bF}function NT(r){let e=0,t=0;for(let[n,s]of r.order)n==="S"?e=Math.max(e,s+1):t=Math.max(t,s+1);return{spatialLevels:e,reductionLevels:t}}var bF,wF,jB,AT=M(()=>{"use strict";bF={name:"mlt_cpu",order:[["S",0],["S",1],["S",2],["S",3],["R",0]],roles:{S0:"parallelize",S3:"vectorize"}},wF={name:"mlt_gpu",order:[["S",0],["S",1],["S",2],["R",0]],roles:{S0:"blockIdx",S1:"threadIdx",S2:"unroll"}},jB={name:"ssrsrs_cpu",order:[["S",0],["S",1],["R",0],["S",2],["R",1],["S",3]],roles:{S0:"parallelize",S3:"vectorize",R1:"unroll"}};l(qB,"getTileStructure");l(NT,"levelCounts")});var os,vr,Ty=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)]}},vr=class{static{l(this,"ScheduleSketch")}constructor(e,t,n){this.name=e,this.variables=t,this._apply=n}instantiate(e){return(t,n,s)=>{this._apply(t,n,s,e)}}sampleParams(e){let t={};for(let n of this.variables)t[n.name]=n.sample(e);return t}}});function TF(r){let e=[];for(let t=1;t*t<=r;t++){if(r%t!==0)continue;e.push(t);let n=r/t;n!==t&&e.push(n)}return e.sort((t,n)=>t-n),e}function xF(r,e){let t=Math.min(r.length,e.length);for(let n=0;n<t;n++)if(r[n]!==e[n])return r[n]-e[n];return r.length-e.length}function SF(r,e){let t=new Set,n=[];r.sort(xF);for(let i of r){let a=i.join(",");t.has(a)||(t.add(a),n.push(i))}if(n.length<=e)return n;let s=new Set,o=[];for(let i=0;i<e;i++){let a=Math.floor(i*(n.length-1)/(e-1));s.has(a)||(s.add(a),o.push(n[a]))}return o}function OT(r,e,t=48){if(!Number.isFinite(r)||r<1||e<=1)return[[r]];let n=[],s=t*8,o=l((i,a,u)=>{if(!(n.length>=s)){if(a===e-1){n.push([...u,i]);return}for(let p of TF(i))if(u.push(p),o(i/p,a+1,u),u.pop(),n.length>=s)return}},"rec");return o(r,0,[]),SF(n,t)}var UB=M(()=>{"use strict";l(TF,"divisorsOf");l(xF,"cmpTuple");l(SF,"selectDiverse");l(OT,"enumerateFactorizations")});function xy(r){return r.extent&&r.extent.type==="IntImmNode"?r.extent.value:null}function WB(r,e,t,n){let s=[],o=t;for(let i=0;i<n.length-1;i++){let a=r.getLoops(e).find(h=>h.loopVar.name===o);if(!a)return null;let u=1;for(let h=i+1;h<n.length;h++)u*=n[h];let[p,c]=r.split(a,u);s.push(p.loopVar.name),o=c.loopVar.name}return s.push(o),s}function kF(r,e,t,n,s){let o=l(i=>r.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"?n:s;if(u==="parallelize"){let c=p[0]&&a<p[0].length?o(p[0][a]):null;c&&r.parallelize(c)}else if(u==="vectorize"){let c=p[p.length-1],h=c&&a<c.length?o(c[a]):null;h&&r.vectorize(h)}else if(u==="unroll")for(let c of p){let h=a<c.length?o(c[a]):null;h&&r.unroll(h)}else(u==="blockIdx"||u==="threadIdx")&&p.forEach((c,h)=>{if(h>=KB.length)return;let f=a<c.length?o(c[a]):null;f&&r.bindThread(f,`${u}.${KB[h]}`)})}}function HB(r,e,t,n,s,o){let i=[];for(let h=0;h<n.length;h++){let f=WB(r,e,n[h],o[`s${h}`]);if(!f)return;i.push(f)}let a=[];for(let h=0;h<s.length;h++){let f=WB(r,e,s[h],o[`r${h}`]);if(!f)return;a.push(f)}let u=[];for(let[h,f]of t.order){let d=h==="S"?i:a;for(let m of d)f<m.length&&u.push(m[f])}let p=new Map(r.getLoops(e).map(h=>[h.loopVar.name,h])),c=u.map(h=>p.get(h)).filter(Boolean);c.length>=2&&r.reorder(...c),kF(r,e,t,i,a)}function YB(r){let e=r.loops.filter(n=>!r.reductionLoopVars.has(n.loopVar.name)),t=r.loops.filter(n=>r.reductionLoopVars.has(n.loopVar.name));return{spatialLoops:e,reductionLoops:t}}function JB(r,e,t,n){let s=[];return r.forEach((o,i)=>s.push(new os(`s${i}`,OT(xy(o),t)))),e.forEach((o,i)=>s.push(new os(`r${i}`,OT(xy(o),n)))),s}function ZB(r,e){let{spatialLevels:t,reductionLevels:n}=NT(e),{spatialLoops:s,reductionLoops:o}=YB(r);if(s.length===0)return null;for(let p of[...s,...o])if(xy(p)===null)return null;let i=JB(s,o,t,n),a=s.map(p=>p.loopVar.name),u=o.map(p=>p.loopVar.name);return new vr(e.name,i,(p,c,h,f)=>{HB(p,c,e,a,u,f)})}function QB(r,e){let{spatialLevels:t,reductionLevels:n}=NT(e),{spatialLoops:s,reductionLoops:o}=YB(r);if(s.length===0||o.length===0)return null;for(let p of[...s,...o])if(xy(p)===null)return null;let i=JB(s,o,t,n),a=s.map(p=>p.loopVar.name),u=o.map(p=>p.loopVar.name);return new vr(e.name,i,(p,c,h,f)=>{p.decomposeReduction(c),HB(p,`${c}_upd`,e,a,u,f)})}var KB,XB=M(()=>{"use strict";Ty();UB();AT();KB=["x","y","z"];l(xy,"staticExtent");l(WB,"multiLevelSplit");l(kF,"applyRoles");l(HB,"tileBlock");l(YB,"splitLoops");l(JB,"tilingVariables");l(ZB,"createMultiLevelTilingSketch");l(QB,"createSSRSRSTilingSketch")});function tR(r){return Math.min(r&&r.maxThreadsPerBlock||256,256)}function NF(r){let e=new Set;for(let t=2;t*t<=r;t++){if(r%t!==0)continue;e.add(t);let n=r/t;n>1&&n<r&&e.add(n)}return[...e].sort((t,n)=>t-n)}function nR(r){return new vr("fused",[],(e,t,n,s)=>{e.fuseConsumer(t,r);let o=e.getLoops(t);o.length>0&&e.parallelize(o[0])})}function rR(r){let e=r.loops.filter(i=>r.reductionLoopVars.has(i.loopVar.name));if(e.length!==1)return null;let t=e[0],n=t.extent&&t.extent.type==="IntImmNode"?t.extent.value:null;if(n===null)return null;let s=NF(n);if(s.length===0)return null;let o=t.loopVar.name;return new vr("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 sR(){return new vr("elementwise_cpu",[new os("vector_width",vF)],(r,e,t,n)=>{let s=r.getLoops(e);if(s.length===0)return;if(s.length===1){let a=s[0].extent;if(a.type==="IntImmNode"&&a.value>=n.vector_width*2){let[u,p]=r.split(s[0],n.vector_width);r.parallelize(u),r.vectorize(p)}else r.parallelize(s[0]);return}r.parallelize(s[0]);let o=s[s.length-1],i=o.extent;if(i.type==="IntImmNode"&&i.value>=n.vector_width){let[,a]=r.split(o,n.vector_width);r.vectorize(a)}})}function oR(){return new vr("elementwise_gpu",[new os("block_size",eR)],(r,e,t,n)=>{let s=r.getLoops(e);if(s.length===0)return;let o=s[0];for(let c=1;c<s.length;c++){let f=r.getLoops(e).find(d=>d.loopVar.name===s[c].loopVar.name);f&&o.body===f&&(o=r.fuseLoops(o,f))}let i=o.extent;if(i.type!=="IntImmNode"){r.bindThread(o,"threadIdx.x");return}let a=i.value,u=Math.min(t.maxThreadsPerBlock,1024);if(o0(r.func)&&a<=u){r.bindThread(o,"threadIdx.x");return}let p=Math.min(n.block_size,tR(t));if(a>p){let[c,h]=r.split(o,p);r.bindThread(c,"blockIdx.x"),r.bindThread(h,"threadIdx.x")}else r.bindThread(o,"threadIdx.x")})}function iR(){return new vr("reduction_cpu",[],(r,e,t,n)=>{let s=r.getLoops(e);s.length>0&&r.parallelize(s[0])})}function aR(){return new vr("reduction_gpu",[new os("block_size",eR)],(r,e,t,n)=>{let s=r.getLoops(e);if(s.length===0)return;let o=dt(r.func,e),i=s.filter(c=>!o||!Zu(c,o));if(i.length===0)return;let a=i[0];for(let c=1;c<i.length;c++){let f=r.getLoops(e).find(d=>d.loopVar.name===i[c].loopVar.name);f&&a.body===f&&(a=r.fuseLoops(a,f))}let u=a.extent;if(u.type!=="IntImmNode"){r.bindThread(a,"threadIdx.x");return}let p=Math.min(n.block_size,tR(t));if(u.value>p){let[c,h]=r.split(a,p);r.bindThread(c,"blockIdx.x"),r.bindThread(h,"threadIdx.x")}else r.bindThread(a,"threadIdx.x")})}var eR,vF,uR=M(()=>{"use strict";ro();Ty();eR=[32,64,128,256,512,1024],vF=[1,2,4,8,16];l(tR,"gpuThreadCap");l(NF,"reductionDivisors");l(nR,"createFusedTilingSketch");l(rR,"createRfactorSketch");l(sR,"createElementwiseCPUSketch");l(oR,"createElementwiseGPUSketch");l(iR,"createReductionCPUSketch");l(aR,"createReductionGPUSketch")});function Sy(r){return ym(r,Sy,(e,t,n)=>{switch(e.type){case"PrimFunc":return t.name=e.name,t.params=e.params,t.body=n(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=n(e.body),t._setChild("body",t.body),t;case"LetStmtNode":return t.variable=e.variable,t.value=n(e.value),t.body=n(e.body),t._setChild("body",t.body),t;case"WhileNode":return t.condVar=e.condVar,t.condBody=n(e.condBody),t.loopBody=n(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(n):typeof o=="object"&&o!==null&&o.type?t[s]=n(o):t[s]=o}return t}})}function Lo(r){return Sy(r)}function Ma(r){return Sy(r)}function BT(r){return Sy(r)}function lR(r,e,t){let n=t.get(e);if(!n)return null;let s=[],o=n._parent;for(;o&&o!==r;)o.type==="ForNode"&&s.push(o),o=o._parent;s.reverse();let i=BT(n);for(let p=s.length-1;p>=0;p--){let c=s[p],h=new de(c.loopVar,BT(c.min),BT(c.extent),c.kind,i,c.threadTag);h._setChild("body",i),i=h}let a=new Map;for(let p of n.reads)a.set(p.buffer.name,p.buffer);for(let p of n.writes)a.set(p.buffer.name,p.buffer);let u=[];for(let p of r.params)a.has(p.name)&&u.push(p);return new Jo("__tune_"+e,u,i,a,[])}var cf=M(()=>{"use strict";De();Ww();l(Sy,"deepCloneIR");l(Lo,"clonePrimFunc");l(Ma,"cloneTensorIR");l(BT,"cloneBlockSubtree");l(lR,"extractBlockMini")});function ET(r,e){let t=[];for(let n=1;n<=e;n*=2)n>=r&&t.push(n);return t}function hf(r){if(!r)return null;let e=[];for(let t of r){if(!t||t.type!=="VariableNode")return null;e.push(t.name)}return e}function AF(r,e){let t=[r];for(;t.length>0;){let n=t.pop();if(!(!n||typeof n!="object")){if(n.type==="BufferStoreNode"&&n.buffer&&n.buffer.name===e&&n.value&&n.value.type==="MathOpNode"&&n.value.op==="+")return n;if(n.body&&t.push(n.body),n.stmts)for(let s of n.stmts)t.push(s);n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody)}}return null}function ff(r){if(!r||r.broadcastDims||r.offset&&r.offset!==0)return!1;let e=r.shape,t=r.strides;if(!t||t.length!==e.length)return!1;let n=1;for(let s=e.length-1;s>=0;s--){if(typeof e[s]!="number"||e[s]<=0||t[s]!==n)return!1;n*=e[s]}return!0}function LT(r,e){if(!dt(r,e))return null;let n=uf(r.body,e);if(!n||n.reads.length<2||n.writes.length<1)return null;let s=n.writes[0].buffer;if(!s||s.shape.length<2)return null;let o=s.shape.length,i=AF(n.body,s.name);if(!i)return null;let a=hf(i.indices);if(!a||a.length!==o)return null;let u=i.value,p=l(x=>x&&x.type==="BufferLoadNode"&&x.buffer&&x.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 h=[c.a,c.b];if(!h.every(x=>x&&x.type==="BufferLoadNode"&&x.buffer))return null;let f=a[o-1],d=a.slice(0,o-1),m=null,g=null;for(let x of h){let k=hf(x.indices);if(!k||k.length!==o)continue;let A=!0;for(let N=0;N<o-1;N++)if(k[N]!==d[N]){A=!1;break}if(A){m=x,g=k[o-1];break}}if(!m||g==null)return null;let y=h[0]===m?h[1]:h[0],_=hf(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 x=0;x<o-1;x++)if(b.shape[x]!==s.shape[x])return null;let T=s.shape[o-1],S=b.shape[o-1];if(![T,S].every(x=>typeof x=="number"&&x>0))return null;if(_.length===2&&w.shape.length===2){let x;if(_[0]===g&&_[1]===f)x=!1;else if(_[0]===f&&_[1]===g)x=!0;else return null;if(x){if(w.shape[0]!==T||w.shape[1]!==S)return null}else if(w.shape[0]!==S||w.shape[1]!==T)return null;let k=1;for(let A=0;A<o-1;A++){let N=s.shape[A];if(typeof N!="number"||N<=0)return null;k*=N}return o===2?{A:b,B:w,C:s,M:k,N:T,K:S,transB:x,batch:1}:!ff(b)||!ff(s)?null:{A:new me(b.name,[k,S],b.dtype,b.scope),B:w,C:new me(s.name,[k,T],s.dtype,s.scope),M:k,N:T,K:S,transB:x,batch:1}}if(o>=3&&_.length===o&&w.shape.length===o){let x=a.slice(0,o-2);for(let B=0;B<o-2;B++)if(_[B]!==x[B])return null;let k;if(_[o-2]===g&&_[o-1]===f)k=!1;else if(_[o-2]===f&&_[o-1]===g)k=!0;else return null;for(let B=0;B<o-2;B++)if(w.shape[B]!==s.shape[B])return null;if(k){if(w.shape[o-2]!==T||w.shape[o-1]!==S)return null}else if(w.shape[o-2]!==S||w.shape[o-1]!==T)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(!ff(b)||!ff(w)||!ff(s))return null;let v=new me(b.name,[N,A,S],b.dtype,b.scope),O=new me(w.name,k?[N,T,S]:[N,S,T],w.dtype,w.scope),R=new me(s.name,[N,A,T],s.dtype,s.scope);return{A:v,B:O,C:R,M:A,N:T,K:S,transB:k,batch:N}}return null}function dR(r,e,t=32){let n=r.maxThreadsPerBlock||1024,s=r.warpSize||32,o=r.sharedMemoryBytes||49152,i=r.registersPerThread||255,a=4,{M:u,N:p,K:c}=e,h=ET(2,8),f=ET(2,s/2),d=ET(s/4,s/2),m=2*s,g=Math.min(n,8*s),y=4*s,_=new Set,b=[];for(let w of h)for(let T of h)if(!(w*T>y)&&!(w*T+w+T+s>i))for(let S of f)for(let x of f){let k=x*S;if(k<m||k>g||k%s!==0)continue;let A=w*S,N=T*x;if(!(A>u||N>p||A>y||N>y))for(let v of d){if(v>c||(A*v+v*N)*a>o)continue;let R=`${A}_${N}_${v}_${w}_${T}`;_.has(R)||(_.add(R),b.push({BM:A,BN:N,BK:v,TM:w,TN:T,threads:k}))}}return b.sort((w,T)=>fR(T,s)-fR(w,s)),b.slice(0,t)}function fR(r,e){let t=r.TM*r.TN,n=-Math.abs(r.TM-r.TN),s=-Math.abs(Math.log2(r.BM)-Math.log2(r.BN)),o=r.threads>=4*e&&r.threads<=8*e?1:0,i=r.BK===e/4?1:0;return o*100+t*4+n*6+s*4+i}function ky(r,e){if(!e)return null;let t=dR(r,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 OF(r,e,t,n){let s=Ma(r.storeValue);return ti(s,o=>o.type==="BufferLoadNode"&&o.buffer?o.buffer.name===r.inputName?e:r.scalarConsts.has(o.buffer.name)?Ma(r.scalarConsts.get(o.buffer.name)):o:o.type==="VariableNode"?o.name===r.iv0?Ma(t):o.name===r.iv1?Ma(n):o:o)}function CT(r,e,t=null){let{A:n,B:s,C:o,M:i,N:a,K:u,transB:p}=r,c=r.batch||1,{BM:h,BN:f,BK:d,TM:m,TN:g}=e,y=f/g,_=h/m,b=y*_,w=Math.ceil(u/d),T=h*d,S=d*f,x=Math.ceil(T/b),k=Math.ceil(S/b),A=i%h!==0,N=a%f!==0,v=u%d!==0,O=T%b!==0,R=S%b!==0,B=new me("rb_As",[T],"f32","shared"),E=new me("rb_Bs",[S],"f32","shared"),z=new me("rb_acc",[m*g],"f32","local"),D=new me("rb_af",[m],"f32","local"),J=new me("rb_bf",[g],"f32","local"),j=Vt("rb_bx"),U=Vt("rb_by"),P=Vt("rb_tx"),V=Vt("rb_ty"),he=Vt("rb_tid"),le=Vt("rb_brow"),Ie=Vt("rb_bcol"),G=Vt("rb_k0"),K=Vt("rb_bz"),ie=c>1,ze=l((wt,Fs)=>ie?[K,wt,Fs]:[wt,Fs],"gIdx"),Se=l((wt,Fs)=>Pn(Nr(wt,Ze(g)),Fs),"accIdx"),Qe=Vt("rb_im"),It=Vt("rb_in"),Jt=Co(Qe,m,Co(It,g,new Y(z,[Se(Qe,It)],RT()))),Gt=Vt("rb_la"),tt=Vt("rb_aidx"),ye=Pn(le,cR(tt,Ze(d))),He=Pn(G,pR(tt,Ze(d))),gn=new q(n,ze(ye,He));if(A||v){let wt=A?Ls(ye,Ze(i)):null;v&&(wt=wt?MT(wt,Ls(He,Ze(u))):Ls(He,Ze(u))),gn=new xe(wt,gn,RT())}let jt=new Y(B,[tt],gn);O&&(jt=new xe(Ls(tt,Ze(T)),jt));let Zt=IT(Gt,x,new Ye(tt,Pn(he,Nr(Gt,Ze(b))),jt)),Qt=Vt("rb_lb"),on=Vt("rb_bidx"),Jn=Pn(G,cR(on,Ze(f))),Bn=Pn(Ie,pR(on,Ze(f))),yr=new q(s,p?ze(Bn,Jn):ze(Jn,Bn));if(v||N){let wt=v?Ls(Jn,Ze(u)):null;N&&(wt=wt?MT(wt,Ls(Bn,Ze(a))):Ls(Bn,Ze(a))),yr=new xe(wt,yr,RT())}let Zn=new Y(E,[on],yr);R&&(Zn=new xe(Ls(on,Ze(S)),Zn));let pt=IT(Qt,k,new Ye(on,Pn(he,Nr(Qt,Ze(b))),Zn)),Qn=Vt("rb_kk"),mt=Vt("rb_fi"),Or=Vt("rb_fj"),hs=Co(mt,m,new Y(D,[mt],new q(B,[Pn(Nr(Pn(Nr(V,Ze(m)),mt),Ze(d)),Qn)]))),$s=Co(Or,g,new Y(J,[Or],new q(E,[Pn(Nr(Qn,Ze(f)),Pn(Nr(P,Ze(g)),Or))]))),Br=Vt("rb_mi"),Xn=Vt("rb_ni"),_r=Co(Br,m,Co(Xn,g,new Y(z,[Se(Br,Xn)],Pn(new q(z,[Se(Br,Xn)]),Nr(new q(D,[Br]),new q(J,[Xn])))))),fo=Co(Qn,d,new te([hs,$s,_r])),Rr=Vt("rb_kt"),Mr=new Ye(G,Nr(Rr,Ze(d)),new te([Zt,pt,new On,fo,new On])),ds=IT(Rr,w,Mr),Qr=Vt("rb_wm"),Mi=Vt("rb_wn"),Vo=Pn(Pn(le,Nr(V,Ze(m))),Qr),Go=Pn(Pn(Ie,Nr(P,Ze(g))),Mi),Ii=new q(z,[Se(Qr,Mi)]),ec=t?t.outBuffer:o,Fa=t?OF(t,Ii,Vo,Go):Ii,Ei=new Y(ec,ze(Vo,Go),Fa);if(A||N){let wt=A?Ls(Vo,Ze(i)):null;N&&(wt=wt?MT(wt,Ls(Go,Ze(a))):Ls(Go,Ze(a))),Ei=new xe(wt,Ei)}let Li=Co(Qr,m,Co(Mi,g,Ei)),ho=new te([Jt,ds,Li]),Ci=new Ve(z,"local",new Ve(D,"local",new Ve(J,"local",ho))),jo=new Ye(he,Pn(Nr(V,Ze(y)),P),new Ye(le,Nr(U,Ze(h)),new Ye(Ie,Nr(j,Ze(f)),Ci))),mo=Math.ceil(a/f),qo=Math.ceil(i/h),Di=pf(U,"blockIdx.y",qo,pf(j,"blockIdx.x",mo,pf(V,"threadIdx.y",_,pf(P,"threadIdx.x",y,jo))));return ie&&(Di=pf(K,"blockIdx.z",c,Di)),new Ve(B,"shared",new Ve(E,"shared",Di))}function BF(r){let e=new os("config_index",r.map((n,s)=>s)),t=new vr("matmul_register_block_gpu",[e],(n,s,o,i)=>{let a=LT(n.func,s);if(!a)return;let u=r[i.config_index];if(!u)return;let p=CT(a,u);n.func.body=p,n.func._setChild&&n.func._setChild("body",p),n.func.gpuRegisterBlocked=!0});return t.configs=r,t.enumerate=()=>r.map((n,s)=>({config_index:s})),t}function RF(r){let e=io(r.body),t=null;for(let s of e){let o=lf(r,s);if(o.hasReduction&&o.spatial>=2&&o.reads>=2){if(t)return null;t=s}}if(!t)return null;let n=LT(r,t);if(!n)return null;for(let s of e){if(s===t)continue;let o=dt(r,s);if(!o||o.hasReduction||o.readBuffers.length>0)return null;for(let i of o.writeBuffers)if(i!==n.C.name)return null}return{reductionBlock:t,dims:n}}function MF(r){let e=new Map,t=new Map;Ss(r,n=>{if(n.type==="BufferStoreNode"&&n.buffer){let s=n.buffer.name;t.set(s,(t.get(s)||0)+1),(!n.indices||n.indices.length===0)&&n.value&&(n.value.type==="FloatImmNode"||n.value.type==="IntImmNode")&&e.set(s,n.value)}});for(let n of[...e.keys()])(t.get(n)||0)!==1&&e.delete(n);return e}function IF(r,e){let t=null;return Ss(r,n=>{if(n.type==="BufferStoreNode"&&n.buffer&&n.buffer.name===e)return t=n,ad}),t}function EF(r,e){let t=null,n=0;return Ss(r,s=>{s.type==="BufferLoadNode"&&s.buffer&&s.buffer.name===e&&(n++,t=s)}),n===1?t:null}function mR(r){let e=io(r.body),t=null;for(let y of e){let _=lf(r,y);if(_.hasReduction&&_.spatial>=2&&_.reads>=2){if(t)return null;t=y}}if(!t)return null;let n=LT(r,t);if(!n)return null;let s=n.C.name,o=new Set;for(let y of e){let _=dt(r,y);if(!_)return null;for(let b of _.writeBuffers)o.add(b)}let i=[];for(let y of e){if(y===t)continue;let _=dt(r,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:n,epilogue:null};if(i.length!==1||(n.batch||1)!==1)return null;let a=i[0];if(a.info.writeBuffers.length!==1)return null;let u=a.info.writeBuffers[0],p=MF(r.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 h=uf(r.body,a.name);if(!h)return null;let f=IF(h.body,u);if(!f)return null;let d=hf(f.indices);if(!d||d.length!==2)return null;let m=EF(f.value,s);if(!m)return null;let g=hf(m.indices);return!g||g.length!==d.length||g.some((y,_)=>y!==d[_])?null:{reductionBlock:t,dims:n,epilogue:{outBuffer:f.buffer,storeValue:f.value,inputName:s,iv0:d[0],iv1:d[1],scalarConsts:p}}}function gR(r,e,t){let n=RF(r);if(!n)return null;let s=hR.get(r);if(s===void 0){let o=dR(t,n.dims);s=o.length>0?BF(o):null,hR.set(r,s)}return s?e===n.reductionBlock?[s]:[]:null}var Ze,RT,Vt,Pn,Nr,cR,pR,Ls,MT,IT,Co,pf,hR,vy=M(()=>{"use strict";De();xo();ro();Ty();Ol();Gr();cf();Ze=l(r=>new L(r),"I"),RT=l(()=>new fe(0),"FZERO"),Vt=l(r=>new Ne(r,"i32"),"IV"),Pn=l((r,e)=>In("+",r,e),"ADD"),Nr=l((r,e)=>In("*",r,e),"MUL"),cR=l((r,e)=>In("//",r,e),"DIV"),pR=l((r,e)=>In("%",r,e),"MOD"),Ls=l((r,e)=>new qe("lt",r,e),"LT"),MT=l((r,e)=>new C("&&",r,e),"AND"),IT=l((r,e,t)=>new de(r,Ze(0),Ze(e),F.SERIAL,t),"forS"),Co=l((r,e,t)=>new de(r,Ze(0),Ze(e),F.UNROLLED,t),"forU"),pf=l((r,e,t,n)=>new de(r,Ze(0),Ze(t),F.THREAD_BINDING,n,e),"forT");l(ET,"pow2Range");l(hf,"plainVars");l(AF,"findAccStore");l(ff,"isContiguousRowMajor");l(LT,"matmulTileDims");l(dR,"enumerateRegisterBlockConfigs");l(fR,"goodness");l(ky,"pickFixedConfig");l(OF,"foldEpilogue");l(CT,"buildRegisterBlockedMatmul");l(BF,"createMatmulRegisterBlockGPUSketch");l(RF,"analyzePureMatmul");l(MF,"collectScalarConstBuffers");l(IF,"findEpilogueStore");l(EF,"singleBufferLoad");l(mR,"analyzeMatmulEpilogue");hR=new WeakMap;l(gR,"richMatmulSketches")});function LF(r,e){let t=[r];for(;t.length;){let n=t.pop();if(!(!n||typeof n!="object")&&(n.type==="BlockNode"&&e(n),n.body&&t.push(n.body),n.initBody&&t.push(n.initBody),n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody),Array.isArray(n.stmts)))for(let s of n.stmts)t.push(s)}}function mf(r){let e=[];LF(r.body,n=>{e.push({name:n.name,node:n,reads:(n.reads||[]).map(s=>s.buffer&&s.buffer.name).filter(Boolean),writes:(n.writes||[]).map(s=>s.buffer&&s.buffer.name).filter(Boolean)})});let t=new Map;for(let n of e)for(let s of n.reads)t.has(s)||t.set(s,[]),t.get(s).push(n.name);return{blocks:e,consumersByBuf:t,byName:new Map(e.map(n=>[n.name,n]))}}function DT(r){let e=[];for(let t of r){if(!t||t.type!=="VariableNode")return null;e.push(t.name)}return e}function df(r,e,t){if(!r||typeof r!="object")return t;r.type==="BufferLoadNode"&&r.buffer&&r.buffer.name===e&&t.push(r);for(let n of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])r[n]&&df(r[n],e,t);if(Array.isArray(r.args))for(let n of r.args)df(n,e,t);if(Array.isArray(r.indices))for(let n of r.indices)df(n,e,t);if(Array.isArray(r.stmts))for(let n of r.stmts)df(n,e,t);return t}function Bl(r,e){if(!r||typeof r!="object")return e;r.type==="BufferLoadNode"&&r.buffer&&r.buffer.name&&e.add(r.buffer.name);for(let t of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])r[t]&&Bl(r[t],e);if(Array.isArray(r.args))for(let t of r.args)Bl(t,e);if(Array.isArray(r.indices))for(let t of r.indices)Bl(t,e);if(Array.isArray(r.stmts))for(let t of r.stmts)Bl(t,e);return e}function Ny(r,e,t,n){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=n(r,t),p=n(r,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 h=new Set(s.reads);if(Bl(s.node.body,h),Bl(s.node.initBody,h),h.has(c.writes[0]))return null;let f=s.node.body&&s.node.body.type==="BufferStoreNode"?s.node.body:null,d=c.node.body&&c.node.body.type==="BufferStoreNode"?c.node.body:null;if(!f||!d)return null;let m=u.loops.filter(w=>!u.reductionLoopVars.has(w.loopVar.name)).map(w=>w.loopVar.name),g=DT(f.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 _=DT(d.indices);if(!_||_.join(",")!==y.join(","))return null;let b=df(d.value,o,[]);if(b.length===0)return null;for(let w of b){let T=DT(w.indices);if(!T||T.join(",")!==_.join(","))return null}return a}var PT=M(()=>{"use strict";l(LF,"walkBlocks");l(mf,"buildBlockDAG");l(DT,"indexVarNames");l(df,"collectLoads");l(Bl,"collectLoadedBuffers");l(Ny,"findFusibleConsumer")});function $T(r){return r.isGPU()?aR():iR()}function CF(r){return r.isGPU()?oR():sR()}function DF(r,e,t,n){let s=dt(r,e);if(!s)return[$T(t)];let o=[],i=ZB(s,qB(t));if(i&&o.push(i),t.kind===Ot.CPU){let a=QB(s,jB);a&&o.push(a);let u=rR(s);u&&o.push(u);let p=n?Ny(r,n,e,dt):null;p&&o.push(nR(p))}return o.push($T(t)),o}function FT(r,{priority:e=100}={}){Ay.some(t=>t.derive===r.derive&&t.matches===r.matches)||(Ay.push({matches:r.matches,derive:r.derive,priority:e}),Ay.sort((t,n)=>t.priority-n.priority))}function yR(r,e,t,n={}){if(n.richGpu&&t.isGPU()){let i=gR(r,e,t);if(i!==null)return i}if(t.kind!==Ot.CPU&&!t.isGPU())return[];let s=lf(r,e),o=n.dag||mf(r);for(let i of Ay)if(i.matches(s,t))return i.derive(r,e,t,o);return[]}var Ay,_R=M(()=>{"use strict";Ks();ro();Ol();AT();XB();uR();vy();PT();l($T,"reductionSketch");l(CF,"elementwiseSketch");l(DF,"deriveMultiLevel");Ay=[];l(FT,"registerSketchRule");FT({matches:l(r=>r.hasReduction&&r.spatial>=1&&r.reads>=2,"matches"),derive:DF},{priority:10});FT({matches:l(r=>r.hasReduction,"matches"),derive:l((r,e,t)=>[$T(t)],"derive")},{priority:20});FT({matches:l(()=>!0,"matches"),derive:l((r,e,t)=>[CF(t)],"derive")},{priority:30});l(yR,"deriveSketches")});function zT(r,e,t,n,s={}){return yR(r,e,t,s)}var bR=M(()=>{"use strict";_R();l(zT,"getSketchesForBlock")});function Oy(r){let e=new Map,t=[r];for(;t.length>0;){let n=t.pop();if(n){if(n.type==="BlockNode"&&e.set(n.name,n),n.body&&t.push(n.body),n.stmts)for(let s of n.stmts)t.push(s);n.thenBody&&t.push(n.thenBody),n.elseBody&&t.push(n.elseBody),n.initBody&&t.push(n.initBody)}}return e}function By(r,e,t,n=null){let s=n||Oy(r.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(Yt(o.body,u),o.initBody&&Yt(o.initBody,u),i.push(u.join(";")),o.writes.length===1){let p=o.writes[0].buffer.name,c=[];for(let h of PF(s).get(p)||[]){if(h===o)continue;let f=[];Yt(h.body,f),h.initBody&&Yt(h.initBody,f),c.push(f.join(";"))}c.length>0&&(c.sort(),i.push(`consumers:${c.join("|")}`))}}return i.push(t.name),i.push(t.kind),$F(i.join("|"))}function PF(r){if(r.__readersByBuffer)return r.__readersByBuffer;let e=new Map;for(let t of r.values()){if(!t.reads)continue;let n=new Set;for(let s of t.reads)s.buffer&&n.add(s.buffer.name);for(let s of n){let o=e.get(s);o||(o=[],e.set(s,o)),o.push(t)}}return r.__readersByBuffer=e,e}function Yt(r,e){if(!(!r||typeof r!="object"))switch(r.type){case"BufferStoreNode":if(e.push("store"),r.indices)for(let t of r.indices)Yt(t,e);Yt(r.value,e);return;case"BufferLoadNode":if(e.push(`load:${r.buffer.shape.join("x")}:${r.buffer.dtype}`),r.indices)for(let t of r.indices)Yt(t,e);return;case"MathOpNode":e.push(`math:${r.op}`),Yt(r.a,e),r.b&&Yt(r.b,e);return;case"CallExternNode":e.push(`call:${r.externName}`);for(let t of r.args)Yt(t,e);return;case"CompareNode":e.push(`cmp:${r.direction}`),Yt(r.a,e),Yt(r.b,e);return;case"CastNode":e.push(`cast:${r.fromDtype}->${r.toDtype}`),Yt(r.expr,e);return;case"IfThenElseNode":Yt(r.condition,e),Yt(r.thenBody,e),r.elseBody&&Yt(r.elseBody,e);return;case"SeqNode":for(let t of r.stmts)Yt(t,e);return;case"ForNode":Yt(r.body,e);return;case"BlockNode":r.initBody&&Yt(r.initBody,e),Yt(r.body,e);return;case"LetStmtNode":Yt(r.value,e),Yt(r.body,e);return;default:return}}function $F(r){let e=2166136261;for(let t=0;t<r.length;t++)e^=r.charCodeAt(t),e=e*16777619>>>0;return e.toString(16).padStart(8,"0")}var VT=M(()=>{"use strict";l(Oy,"buildBlockMap");l(By,"computeWorkloadKey");l(PF,"readersByBuffer");l(Yt,"collectBlockOps");l($F,"fnv1a")});function FF(r,e){let t=r.medianMs!=null?1:0,n=e.medianMs!=null?1:0;return t!==n?n-t:t===1?r.medianMs-e.medianMs:e.score-r.score}var wR,gf,yf,TR=M(()=>{"use strict";VT();wR="mlfw-codegen-1",gf=class{static{l(this,"TuningRecord")}constructor(e,t,n,s,o,i){this.workloadKey=e,this.sketchName=t,this.params=n,this.score=s,this.traceData=o,this.version=i,this.timestamp=Date.now(),this.medianMs=null,this.minMs=null}};l(FF,"rankRecords");yf=class r{static{l(this,"TuningDatabase")}constructor(e=1){this.version=e,this._records=new Map}computeWorkloadKey(e,t,n,s=null){return By(e,t,n,s)}store(e,t){let n=this._records.get(e);n||(n=[],this._records.set(e,n)),n.push(t),n.sort(FF),n.length>10&&(n.length=10)}lookup(e){let t=this._records.get(e);return!t||t.length===0?null:t[0]}lookupTopK(e,t=5){let n=this._records.get(e);return n?n.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,n]of this._records)for(let s of n)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:wR,entries:e}}static deserialize(e){let t=new r(e.version);if(e.codegenVersion!==void 0&&e.codegenVersion!==wR)return t;for(let n of e.entries){let s=new gf(n.workloadKey,n.sketchName,n.params,n.score,n.traceData,n.version);s.timestamp=n.timestamp,s.medianMs=n.medianMs??null,s.minMs=n.minMs??null,t.store(n.workloadKey,s)}return t}saveToFile(e,t){return t.writeFile(e,JSON.stringify(this.serialize())),e}static loadFromFile(e,t){return t.exists(e)?r.deserialize(JSON.parse(t.readFile(e))):new r}clear(){this._records.clear()}}});function Si(r,e){if(!r)return 0;if(r.type==="ForNode"){let t=r.extent&&r.extent.type==="IntImmNode"?r.extent.value:1,n=r.threadTag?e:e*t;return Si(r.body,n)}if(r.type==="BlockNode")return Math.max(e,Si(r.body,e),r.initBody?Si(r.initBody,e):0);if(r.type==="SeqNode"){let t=e;for(let n of r.stmts)t=Math.max(t,Si(n,e));return t}return r.type==="IfThenElseNode"?Math.max(Si(r.thenBody,e),r.elseBody?Si(r.elseBody,e):e):r.type==="AllocateNode"||r.type==="LetStmtNode"?Si(r.body,e):e}function xR(r,e=.1){let t=r.slice().sort((g,y)=>g-y),n=t.length;if(n===0)return{median:0,min:0,trimmedMean:0,cv:0};let s=t[n>>1],o=t[0],i=Math.floor(n*e),a=i,u=n-i>a?n-i:n,p=0,c=0;for(let g=a;g<u;g++)p+=t[g],c++;let h=c>0?p/c:s,f=0;for(let g=a;g<u;g++){let y=t[g]-h;f+=y*y}let d=c>1?Math.sqrt(f/(c-1)):0,m=h>0?d/h:0;return{median:s,min:o,trimmedMean:h,cv:m}}var zF,Ry,My,SR=M(()=>{"use strict";Ec();zF=1e6;l(Si,"maxSerialTripCount");Ry=class{static{l(this,"BenchmarkResult")}constructor(e,t,n,s,o=null,i=0){this.medianMs=e,this.minMs=t,this.trimmedMeanMs=o??e,this.cv=i,this.samples=n,this._totalBytes=s}get throughputGBs(){return this.minMs<=0||!this._totalBytes?0:this._totalBytes/(this.minMs*1e6)}};l(xR,"robustStats");My=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,n=[];for(let[,i]of e.bufferMap){let a=Math.max(i.numel(),1);n.push(a);let u=i.sizeInBytes();u>0&&(t+=u)}let s=n.join(","),o=this._bufferCache.get(s);o||(o=n.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 Hs(this.target),n;try{n=t.compile(e)}catch(p){return this._record("benchmark-compile",p),null}if(n.metadata.kind!=="js")return null;let s;try{s=new Function("return "+n.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=xR(a),!(this.maxCv<=0||u.cv<=this.maxCv));p++);return new Ry(u.median,u.min,a,i,u.trimmedMean,u.cv)}_runMeasured(e){if(e.shapeParams&&e.shapeParams.length>0||Si(e.body,1)>zF)return null;let t;try{t=new Hs(this.target).compile(e)}catch(a){return this._record("measured-compile",a),null}let n=[],s=0;for(let[,a]of e.bufferMap){let u=Math.max(a.sizeInBytes(),1);n.push(u),s+=u}let o;try{o=this.measurer(t,n,[],{warmup:this.warmup,repeat:this.repeat})}catch(a){return this._record("measurer",a),null}if(!o||o.length===0)return null;let i=xR(o);return new Ry(i.median,i.min,o,s,i.trimmedMean,i.cv)}_collect(e,t,n){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;n.push(u),s+=u}}}});var Iy,kR=M(()=>{"use strict";Iy=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 vR(r){let e=null;for(let w of r.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"),n=e.getAttr("kernel_layout");if(t&&t!=="NCHW"||n&&n!=="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],h=s.shape[1],f=s.shape[2],d=s.shape[3],m=o.shape[0],g=o.shape[2],y=o.shape[3],_=i.shape[2],b=i.shape[3];return![c,h,f,d,m,g,y,_,b].every(w=>typeof w=="number"&&w>0)||o.shape[1]!==h||i.shape[1]!==m||i.shape[0]!==c?null:{N:c,Cin:h,H:f,W:d,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 VF(r,e,t){let{weight:n,input:s,output:o}=r,{N:i,Cin:a,H:u,W:p,O:c,Kh:h,Kw:f,Oh:d,Ow:m,sH:g,sW:y,pH:_,pW:b,dH:w,dW:T}=e,{BM:S,BN:x,BK:k,TM:A,TN:N}=t,v=c,O=i*d*m,R=a*h*f,B=h*f,E=d*m,z=x/N,D=S/A,J=z*D,j=Math.ceil(R/k),U=S*k,P=k*x,V=Math.ceil(U/J),he=Math.ceil(P/J),le=new me("ig_As",[U],"f32","shared"),Ie=new me("ig_Bs",[P],"f32","shared"),G=new me("ig_acc",[A*N],"f32","local"),K=new me("ig_af",[A],"f32","local"),ie=new me("ig_bf",[N],"f32","local"),ze=Ae("ig_bx"),Se=Ae("ig_by"),Qe=Ae("ig_tx"),It=Ae("ig_ty"),Jt=Ae("ig_tid"),Gt=Ae("ig_brow"),tt=Ae("ig_bcol"),ye=Ae("ig_k0"),He=l((Cr,X_)=>we(Te(Cr,$(N)),X_),"accIdx"),gn=Ae("ig_im"),jt=Ae("ig_in"),Zt=mn(gn,A,mn(jt,N,new Y(G,[He(gn,jt)],_f()))),Qt=v%S!==0,on=R%k!==0,Jn=O%x!==0,Bn=U%J!==0,yr=P%J!==0,Zn=Ae("ig_la"),pt=Ae("ig_aidx"),Qn=we(Gt,kn(pt,$(k))),mt=we(ye,sn(pt,$(k))),Or=kn(mt,$(B)),hs=sn(mt,$(B)),$s=kn(hs,$(f)),Br=sn(hs,$(f)),Xn=new q(n,[Qn,Or,$s,Br]);if(Qt||on){let Cr=Qt?dr(Qn,$(v)):null;on&&(Cr=Cr?ao(Cr,dr(mt,$(R))):dr(mt,$(R))),Xn=new xe(Cr,Xn,_f())}let _r=new Y(le,[pt],Xn);Bn&&(_r=new xe(dr(pt,$(U)),_r));let fo=Ey(Zn,V,new Ye(pt,we(Jt,Te(Zn,$(J))),_r)),Rr=Ae("ig_lb"),Mr=Ae("ig_bidx"),ds=we(ye,kn(Mr,$(x))),Qr=we(tt,sn(Mr,$(x))),Mi=kn(ds,$(B)),Vo=sn(ds,$(B)),Go=kn(Vo,$(f)),Ii=sn(Vo,$(f)),ec=kn(Qr,$(E)),Fa=sn(Qr,$(E)),Ei=kn(Fa,$(m)),Li=sn(Fa,$(m)),ho=Ly(we(Te(Ei,$(g)),Te(Go,$(w))),$(_)),Ci=Ly(we(Te(Li,$(y)),Te(Ii,$(T))),$(b)),jo=new q(s,[ec,Mi,ho,Ci]),mo=ao(ao(Cy(ho,$(0)),dr(ho,$(u))),ao(Cy(Ci,$(0)),dr(Ci,$(p))));on&&(mo=ao(mo,dr(ds,$(R)))),Jn&&(mo=ao(mo,dr(Qr,$(O)))),jo=new xe(mo,jo,_f());let qo=new Y(Ie,[Mr],jo);yr&&(qo=new xe(dr(Mr,$(P)),qo));let Di=Ey(Rr,he,new Ye(Mr,we(Jt,Te(Rr,$(J))),qo)),wt=Ae("ig_kk"),Fs=Ae("ig_fi"),tc=Ae("ig_fj"),U_=mn(Fs,A,new Y(K,[Fs],new q(le,[we(Te(we(Te(It,$(A)),Fs),$(k)),wt)]))),K_=mn(tc,N,new Y(ie,[tc],new q(Ie,[we(Te(wt,$(x)),we(Te(Qe,$(N)),tc))]))),za=Ae("ig_mi"),Va=Ae("ig_ni"),W_=mn(za,A,mn(Va,N,new Y(G,[He(za,Va)],we(new q(G,[He(za,Va)]),Te(new q(K,[za]),new q(ie,[Va])))))),H_=mn(wt,k,new te([U_,K_,W_])),ms=Ae("ig_kt"),Xr=new Ye(ye,Te(ms,$(k)),new te([fo,Di,new On,H_,new On])),br=Ey(ms,j,Xr),er=Ae("ig_wm"),Ir=Ae("ig_wn"),Uo=we(we(Gt,Te(It,$(A))),er),zs=we(we(tt,Te(Qe,$(N))),Ir),Er=kn(zs,$(E)),Lr=sn(zs,$(E)),go=kn(Lr,$(m)),Fn=sn(Lr,$(m)),Ko=new Y(o,[Er,Uo,go,Fn],new q(G,[He(er,Ir)]));if(Qt||Jn){let Cr=Qt?dr(Uo,$(v)):null;Jn&&(Cr=Cr?ao(Cr,dr(zs,$(O))):dr(zs,$(O))),Ko=new xe(Cr,Ko)}let yo=mn(er,A,mn(Ir,N,Ko)),Y_=new te([Zt,br,yo]),rh=new Ve(G,"local",new Ve(K,"local",new Ve(ie,"local",Y_))),J_=new Ye(Jt,we(Te(It,$(z)),Qe),new Ye(Gt,Te(Se,$(S)),new Ye(tt,Te(ze,$(x)),rh))),Z_=Math.ceil(O/x),Q_=Math.ceil(v/S),sh=ki(Se,"blockIdx.y",Q_,ki(ze,"blockIdx.x",Z_,ki(It,"threadIdx.y",D,ki(Qe,"threadIdx.x",z,J_))));return new Ve(le,"shared",new Ve(Ie,"shared",sh))}function NR(r,e,t){if(!AR(r,{M:e.O,N:e.N*e.Oh*e.Ow,K:e.Cin*e.Kh*e.Kw},t))return!1;let{BM:n,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%n!==0||p%s!==0||c%o!==0||[n,s,o,i,a].some(f=>f%GT!==0))return!1;let h=n/i*(s/a);return!(n*o%(GT*h)!==0||o*s%h!==0)}function GF(r,e,t){let{weight:n,input:s,output:o}=r,{N:i,Cin:a,H:u,W:p,O:c,Kh:h,Kw:f,Oh:d,Ow:m,sH:g,sW:y,pH:_,pW:b,dH:w,dW:T}=e,{BM:S,BN:x,BK:k,TM:A,TN:N}=t,v=GT,O=c,R=i*d*m,B=a*h*f,E=h*f,z=d*m,D=u*p,J=a*u*p,j=x/N,U=S/A,P=j*U,V=B/k,he=S*k,le=k*x,Ie=he/P,G=le/P,K=Ie/v,ie=k/v,ze=(d-1)*g+(h-1)*w-_,Se=(m-1)*y+(f-1)*T-b,Qe=_>0||ze>=u,It=b>0||Se>=p,Jt=new me(n.name,[O*B>0?O*B:1],n.dtype,n.scope),Gt=new me(s.name,[i*J>0?i*J:1],s.dtype,s.scope),tt=new me("iv_As",[2*he],"f32","shared");tt.align16=!0;let ye=new me("iv_Bs",[2*le],"f32","shared");ye.align16=!0;let He=new me("iv_acc",[A*N],"f32","local"),gn=new me("iv_af",[A],"f32","local");gn.align16=!0;let jt=new me("iv_bf",[N],"f32","local");jt.align16=!0;let Zt=new me("iv_ra",[Ie],"f32","local");Zt.align16=!0;let Qt=new me("iv_rb",[G],"f32","local"),on=Ae("iv_bx"),Jn=Ae("iv_by"),Bn=Ae("iv_tx"),yr=Ae("iv_ty"),Zn=Ae("iv_tid"),pt=Ae("iv_brow"),Qn=Ae("iv_bcol"),mt=l((ms,Xr)=>we(Te(ms,$(N)),Xr),"accIdx"),Or=0,hs=Ae("iv_im"),$s=Ae("iv_in"),Br=mn(hs,A,mn($s,N,new Y(He,[mt(hs,$s)],_f()))),Xn=l(ms=>{let Xr=Or++,br=Ae("iv_la"+Xr),er=Ae("iv_c"+Xr),Ir=kn(er,$(ie)),Uo=Te(sn(er,$(ie)),$(v)),zs=mn(br,K,new Ye(er,we(Zn,Te(br,$(P))),new iu(Zt,Te(br,$(v)),Jt,we(Te(we(pt,Ir),$(B)),we(ms,Uo)),v))),Er=Ae("iv_lb"+Xr),Lr=Ae("iv_e"+Xr),go=kn(Lr,$(x)),Fn=sn(Lr,$(x)),Ko=we(Qn,Fn),yo=we(ms,go),Y_=kn(yo,$(E)),rh=sn(yo,$(E)),J_=kn(rh,$(f)),Z_=sn(rh,$(f)),Q_=kn(Ko,$(z)),sh=sn(Ko,$(z)),Cr=kn(sh,$(m)),X_=sn(sh,$(m)),eb=Ly(we(Te(Cr,$(g)),Te(J_,$(w))),$(_)),tb=Ly(we(Te(X_,$(y)),Te(Z_,$(T))),$(b)),OE=we(we(we(Te(Q_,$(J)),Te(Y_,$(D))),Te(eb,$(p))),tb),nb=new q(Gt,[OE]),Ga=null;if(Qe&&(Ga=ao(Cy(eb,$(0)),dr(eb,$(u)))),It){let xS=ao(Cy(tb,$(0)),dr(tb,$(p)));Ga=Ga?ao(Ga,xS):xS}Ga&&(nb=new xe(Ga,nb,_f()));let BE=mn(Er,G,new Ye(Lr,we(Zn,Te(Er,$(P))),new Y(Qt,[Er],nb)));return new te([zs,BE])},"prefetch"),_r=l((ms,Xr)=>{let br=Or++,er=Ae("iv_la"+br),Ir=Ae("iv_c"+br),Uo=kn(Ir,$(ie)),zs=Te(sn(Ir,$(ie)),$(v)),Er=[];for(let yo=0;yo<v;yo++)Er.push(new Y(tt,[we(ms,we(Te(we(zs,$(yo)),$(S)),Uo))],new q(Zt,[we(Te(er,$(v)),$(yo))])));let Lr=mn(er,K,new Ye(Ir,we(Zn,Te(er,$(P))),new te(Er))),go=Ae("iv_lb"+br),Fn=Ae("iv_e"+br),Ko=mn(go,G,new Ye(Fn,we(Zn,Te(go,$(P))),new Y(ye,[we(Xr,Fn)],new q(Qt,[go]))));return new te([Lr,Ko])},"commit"),fo=l((ms,Xr)=>{let br=Or++,er=Ae("iv_kk"+br),Ir=[],Uo=we(ms,we(Te(er,$(S)),Te(yr,$(A))));for(let Fn=0;Fn<A/v;Fn++)Ir.push(new iu(gn,$(Fn*v),tt,we(Uo,$(Fn*v)),v));let zs=we(Xr,we(Te(er,$(x)),Te(Bn,$(N))));for(let Fn=0;Fn<N/v;Fn++)Ir.push(new iu(jt,$(Fn*v),ye,we(zs,$(Fn*v)),v));let Er=Ae("iv_mi"+br),Lr=Ae("iv_ni"+br),go=mn(Er,A,mn(Lr,N,new Y(He,[mt(Er,Lr)],we(new q(He,[mt(Er,Lr)]),Te(new q(gn,[Er]),new q(jt,[Lr]))))));return mn(er,k,new te([...Ir,go]))},"computeMMA"),Rr=Ae("iv_kt"),Mr=Ae("iv_p"),ds=Ae("iv_pN"),Qr=Te(Mr,$(he)),Mi=Te(Mr,$(le)),Vo=Te(ds,$(he)),Go=Te(ds,$(le)),Ii=dr(Rr,$(V-1)),ec=new te([Xn($(0)),_r($(0),$(0)),new On]),Fa=new Ye(Mr,sn(Rr,$(2)),new Ye(ds,sn(we(Rr,$(1)),$(2)),new te([new xe(Ii,Xn(Te(we(Rr,$(1)),$(k)))),fo(Qr,Mi),new xe(Ii,new te([_r(Vo,Go),new On]))]))),Ei=Ey(Rr,V,Fa),Li=Ae("iv_wm"),ho=Ae("iv_wn"),Ci=we(we(pt,Te(yr,$(A))),Li),jo=we(we(Qn,Te(Bn,$(N))),ho),mo=kn(jo,$(z)),qo=sn(jo,$(z)),Di=kn(qo,$(m)),wt=sn(qo,$(m)),Fs=new Y(o,[mo,Ci,Di,wt],new q(He,[mt(Li,ho)])),tc=mn(Li,A,mn(ho,N,Fs)),U_=new te([Br,ec,Ei,tc]),K_=new Ve(He,"local",new Ve(gn,"local",new Ve(jt,"local",new Ve(Zt,"local",new Ve(Qt,"local",U_))))),za=new Ye(Zn,we(Te(yr,$(j)),Bn),new Ye(pt,Te(Jn,$(S)),new Ye(Qn,Te(on,$(x)),K_))),Va=R/x,W_=O/S,H_=ki(Jn,"blockIdx.y",W_,ki(on,"blockIdx.x",Va,ki(yr,"threadIdx.y",U,ki(Bn,"threadIdx.x",j,za))));return new Ve(tt,"shared",new Ve(ye,"shared",H_))}function AR(r,e,t){let{BM:n,BN:s,BK:o,TM:i,TN:a}=t;if(![n,s,o,i,a].every(m=>typeof m=="number"&&m>0)||n%i!==0||s%a!==0)return!1;let u=s/a,p=n/i,c=u*p,h=r.warpSize||32;return!(c%h!==0||c>(r.maxThreadsPerBlock||1024)||(n*o+o*s)*4*2>(r.sharedMemoryBytes||49152)||i*a+i+a+h>(r.registersPerThread||255))}function qF(r,e){for(let t of jF)if(NR(r,e,t))return t;return null}function OR(r,e,t){let n=r.func,s=n.convInfo;if(!s)return!1;let i=io(n.body).find(_=>/^conv_acc_/.test(_));if(!i)return!1;let a=uf(n.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 h=s.O,f=s.N*s.Oh*s.Ow,d=s.Cin*s.Kh*s.Kw;if(d<128||f<64)return!1;let m=t&&t.convConfig,g=t&&t.convNoVec,y=null;if(!g){let _=m?NR(e,s,m)?m:null:qF(e,s);_&&(y=GF({weight:p,input:u,output:c},s,_))}if(!y){let _=m?AR(e,{M:h,N:f,K:d},m)?m:null:ky(e,{M:h,N:f,K:d});if(!_)return!1;y=VF({weight:p,input:u,output:c},s,_)}return r.func.body=y,r.func._setChild&&r.func._setChild("body",y),r.func.gpuRegisterBlocked=!0,!0}var $,_f,Ae,we,Ly,Te,kn,sn,dr,Cy,ao,Ey,mn,ki,GT,jF,jT=M(()=>{"use strict";De();xo();Ol();vy();$=l(r=>new L(r),"I"),_f=l(()=>new fe(0),"FZERO"),Ae=l(r=>new Ne(r,"i32"),"IV"),we=l((r,e)=>new C("+",r,e),"ADD"),Ly=l((r,e)=>new C("-",r,e),"SUB"),Te=l((r,e)=>new C("*",r,e),"MUL"),kn=l((r,e)=>new C("//",r,e),"DIV"),sn=l((r,e)=>new C("%",r,e),"MOD"),dr=l((r,e)=>new qe("lt",r,e),"LT"),Cy=l((r,e)=>new qe("ge",r,e),"GE"),ao=l((r,e)=>new C("&&",r,e),"AND"),Ey=l((r,e,t)=>new de(r,$(0),$(e),F.SERIAL,t),"forS"),mn=l((r,e,t)=>new de(r,$(0),$(e),F.UNROLLED,t),"forU"),ki=l((r,e,t,n)=>new de(r,$(0),$(t),F.THREAD_BINDING,n,e),"forT");l(vR,"detectPureConv");l(VF,"buildImplicitGemmConv");GT=4;l(NR,"vectorizableConvConfig");l(GF,"buildVectorizedImplicitGemmConv");l(AR,"validConvConfig");jF=[{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(qF,"pickVectorizedConvConfig");l(OR,"applyImplicitGemmConv")});function BR(r,e,t,n,s,o,i,a){let u=new me(r,[t,n],e.dtype,"shared"),p=new Y(u,[s,o],new q(e,[i,a]));return{tile:u,fill:p}}function WF(r,e,t){let{A:n,B:s,C:o,M:i,N:a,K:u,transB:p}=r,c=u/t,h=Rl("ts_by"),f=Rl("ts_bx"),d=Rl("ts_ty"),m=Rl("ts_tx"),g=Rl("ts_ko"),y=Rl("ts_ki"),_=new me("ts_acc",[1],"f32","local"),b=Ml(Dy(h,Jr(e)),d),w=Ml(Dy(f,Jr(e)),m),T=Dy(g,Jr(t)),S=BR("ts_As",n,e,t,d,m,b,Ml(T,m)),x=BR("ts_Bs",s,t,e,d,m,p?w:Ml(T,d),p?Ml(T,d):w),k=KF(y,t,new Y(_,[Jr(0)],Ml(new q(_,[Jr(0)]),Dy(new q(S.tile,[d,y]),new q(x.tile,[y,m]))))),A=new te([S.fill,x.fill,new On,k,new On]),N=UF(g,c,A),v=new Y(_,[Jr(0)],new fe(0)),O=new Y(o,[b,w],new q(_,[Jr(0)])),R=new Ve(_,"local",new te([v,N,O]));return Py(h,"blockIdx.y",i/e,Py(f,"blockIdx.x",a/e,new Ve(S.tile,"shared",new Ve(x.tile,"shared",Py(d,"threadIdx.y",e,Py(m,"threadIdx.x",e,R))))))}function HF(r,e){let{M:t,N:n,K:s}=e,o=16,i=16;return o*o>(r.maxThreadsPerBlock||1024)||(o*i+i*o)*4>(r.sharedMemoryBytes||49152)||t%o!==0||n%o!==0||s%i!==0||t<o||n<o||s<i?null:{BS:o,BK:i}}function YF(r,e){let n=io(r.func.body).filter(o=>/^q?conv_(init|acc)_/.test(o));if(n.length===0)return!1;let s=new Rs(e);for(let o of n)s.applyToBlock(r,o);return!0}function JF(r,e,t={}){if(!e.isGPU())return!1;let n=mR(r.func);if(!n)return!1;let s=n.dims;if(t&&t.primitiveMatmul&&(s.batch||1)===1&&!n.epilogue){let a=HF(e,s);if(a){let u=WF(s,a.BS,a.BK);return r.func.body=u,r.func._setChild&&r.func._setChild("body",u),r.func.gpuRegisterBlocked=!0,!0}}let o=ky(e,s);if(!o)return!1;let i=CT(s,o,n.epilogue);return r.func.body=i,r.func._setChild&&r.func._setChild("body",i),r.func.gpuRegisterBlocked=!0,!0}function $y(r,e,t={}){if(!e.isGPU()||e.isWebGPU&&e.isWebGPU())return!1;let n=JF(r,e,t);return n||(n=OR(r,e,t)),n||(n=YF(r,e)),n}var Jr,Rl,Ml,Dy,UF,KF,Py,qT=M(()=>{"use strict";vy();Ol();jT();ro();De();xo();Jr=l(r=>new L(r),"I"),Rl=l(r=>new Ne(r,"i32"),"IV"),Ml=l((r,e)=>new C("+",r,e),"ADD"),Dy=l((r,e)=>new C("*",r,e),"MUL"),UF=l((r,e,t)=>new de(r,Jr(0),Jr(e),F.SERIAL,t),"forS"),KF=l((r,e,t)=>new de(r,Jr(0),Jr(e),F.UNROLLED,t),"forU"),Py=l((r,e,t,n)=>new de(r,Jr(0),Jr(t),F.THREAD_BINDING,n,e),"forT");l(BR,"stageTileToShared");l(WF,"buildTiledSharedMatmul");l(HF,"pickTiledConfig");l(YF,"applyDeterministicGpuConv");l(JF,"applyDeterministicGpuMatmul");l($y,"applyDeterministicGpuSchedule")});function UT(r){return r*1664525+1013904223&2147483647}function QF(r,e,t){let n={...r};for(let s of e.variables)t.rngFloat()<t.mutationRate&&(n[s.name]=s.sample(o=>t.rng(o)));return n}function RR(r={}){return r.strategy==="random"?new KT({numTrials:r.numTrials,seed:r.seed,deadline:r.deadline}):new WT({populationSize:r.populationSize,numGenerations:r.numGenerations,mutationRate:r.mutationRate,eliteRatio:r.eliteRatio,seed:r.seed,deadline:r.deadline})}var Fy,KT,WT,ZF,MR=M(()=>{"use strict";l(UT,"nextLcg");Fy=class{static{l(this,"SearchCandidate")}constructor(e,t,n){this.sketchName=e,this.params=t,this.score=n}},KT=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=UT(this._rngState),this._rngState%e}_expired(){return this.deadline?this.deadline.expired:!1}search(e,t){let n=[];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&&n.push(new Fy(s.name,i,a.score))}}return n.sort((s,o)=>o.score-s.score),{candidates:n,population:null}}},WT=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=UT(this._rngState),this._rngState%e}_rngFloat(){return this._rngState=UT(this._rngState),this._rngState/2147483647}_expired(){return this.deadline?this.deadline.expired:!1}search(e,t,n=null){let s=n&&n.length?n: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 h=t(u,p);return o.set(c,h),h},"evalMemo");for(let u=0;u<this.numGenerations&&!this._expired();u++){let p=[];for(let d of s){let m=i(d.sketch,d.params);m&&p.push({...d,score:m.score})}if(p.sort((d,m)=>m.score-d.score),p.length===0)break;let c=Math.max(1,Math.floor(p.length*this.eliteRatio)),h=p.slice(0,c),f=h.map(d=>({sketch:d.sketch,params:{...d.params}}));for(;f.length<this.populationSize;){let d=h[this._rng(h.length)],m=h[this._rng(h.length)];if(d.sketch!==m.sketch){let g=this._rngFloat()<.5?d:m;f.push(this._mutate(g.sketch,{...g.params}))}else f.push(this._mutate(d.sketch,this._crossover(d,m)))}s=f}let a=[];for(let u of s){let p=i(u.sketch,u.params);p&&a.push(new Fy(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 n=0;n<this.populationSize;n++){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 n={};for(let s of e.sketch.variables)n[s.name]=this._rngFloat()<.5?e.params[s.name]:t.params[s.name];return n}_mutate(e,t){let n={rngFloat:l(()=>this._rngFloat(),"rngFloat"),rng:l(i=>this._rng(i),"rng"),mutationRate:this.mutationRate},s=[QF,...ZF],o=t;for(let i of s)o=i(o,e,n);return{sketch:e,params:o}}},ZF=[];l(QF,"defaultResampleMutator");l(RR,"createSearchStrategy")});function Vy(r){let e={"threadIdx.x":1,"threadIdx.y":1,"threadIdx.z":1},t=new Set,n=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(n):i&&typeof i=="object"&&n(i)}}},"visit");return n(r.body),XF.reduce((s,o)=>s*e[o],1)}var XF,zy,IR=M(()=>{"use strict";fa();Gw();xT();cf();MR();vT();XF=["threadIdx.x","threadIdx.y","threadIdx.z"];l(Vy,"gpuThreadBlockSize");zy=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 Nl,this.costModel=new Al(e.costModel.analytical,this.learnedModel)):(this.costModel=e.costModel,this.learnedModel=e.learnedModel);let n=t?null:lR(e.primFunc,e.blockName,e.blockMap);this.evalFunc=n||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=RR({...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 n=this._best?this._best.measuredScore:-1/0;return Math.max(0,n-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 Sn(Lo(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:n}=this.strategy.search(this.sketches,e,this.population);return this.population=n,t}_evaluate(e,t){try{let n=Lo(this.evalFunc),s=new Sn(n);if(e.instantiate(t)(s,this.evalBlockName,this.target),Wu.validate(n).length>0)return null;let i=this.target.maxThreadsPerBlock;return this.target.isGPU&&this.target.isGPU()&&i&&Vy(n)>i?null:{score:this.costModel.score(n)}}catch(n){return this._warnedEvalSketches.has(e.name)||(this._warnedEvalSketches.add(e.name),this._warn("evaluate-candidate",this.blockName,n)),null}}_measureAndLearn(e){let t=e.slice(0,this.config.topKForBenchmark);for(let n of t){if(this.deadline&&this.deadline.expired)break;let s=this._measure(n);if(!s)continue;let o=-s.result.medianMs;this.learnedModel.addSample(s.features,o),this._consider({sketchName:n.sketchName,params:n.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 n,s;try{n=Lo(this.primFunc),t.instantiate(e.params)(new Sn(n),this.blockName,this.target),s=Lo(this.evalFunc),t.instantiate(e.params)(new Sn(s),this.evalBlockName,this.target)}catch(i){return this._warn("measure-candidate",this.blockName,i),null}let o=this.benchmarkRunner.run(n);return o?{result:o,features:Ra.extractStatements(s)}:null}_consider(e){(!this._best||e.measuredScore>this._best.measuredScore)&&(this._best=e)}}});var HT,Gy,ER=M(()=>{"use strict";HT=class{static{l(this,"GradientSchedulerPolicy")}pick(e){let t=e.filter(i=>!i.plateaued);if(t.length===0)return null;let n=t.filter(i=>i.rounds===0);if(n.length>0)return n[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}},Gy=class{static{l(this,"TaskScheduler")}constructor(e=null){this.policy=e||new HT}run(e,t,n={}){let s=n.maxRoundsPerTask??8,o=n.plateauPatience??2,i=n.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 ez(r){if(r.isCPU())return null;let e=Hv(r.kind);if(!e)throw new Error("hardwareMeasure requested for target '"+r.kind+"' but no measurer is registered for it; the corresponding runtime must be loaded (Node: import '#io/cuda_runtime') before compiling");return e}var jy,qy,LR=M(()=>{"use strict";fa();vT();bR();TR();SR();kR();VT();Ol();PT();ro();qT();IR();cf();De();ER();rw();l(ez,"resolveMeasurer");jy=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}},qy=class{static{l(this,"Autotuner")}constructor(e,t={},n=null){this.target=e,this.config=t instanceof jy?t:new jy(t),this.trace=n,this._funcName=null,this.config.hardwareMeasure&&(this.config.measurer=ez(e)),this.analyticalModel=new wy(e),this.learnedModel=new Nl,this.costModel=new Al(this.analyticalModel,this.learnedModel),this.db=this.config.tuningDB instanceof yf?this.config.tuningDB:new yf;let s=l((o,i,a)=>this._warn(o,i,a),"warn");this.benchmarkRunner=this.config.enableBenchmark?new My(e,{warmup:this.config.benchmarkWarmup,repeat:this.config.benchmarkRepeat,maxCv:this.config.benchmarkMaxCv,measurer:this.config.measurer,warn:s}):null,this.scheduler=new Gy(this.config.schedulerPolicy)}_warn(e,t,n){let s=n&&n.message?n.message:String(n);if(this.config.onWarning)try{this.config.onWarning({stage:e,func:this._funcName,block:t||null,message:s,error:n})}catch(o){}this.trace&&this.trace.warn("autotune",this._funcName,`${e}${t?" ["+t+"]":""}: ${s}`)}tune(e,t=null){this._funcName=e.name;let n=t?[t]:io(e.body),s=Oy(e.body),o=mf(e),i=new Iy(this.config.timeBudgetMs,this.config.clock),a=new Map,u=new Map;for(let h of n){let f=By(e,h,this.target,s);u.set(h,f);let d=a.get(f);if(d){d.weight++;continue}if(this.config.useTuningDB&&this.db.has(f)){a.set(f,{key:f,kind:"cache",cached:this.db.lookup(f),weight:1});continue}let m=zT(e,h,this.target,s,{richGpu:this.config.richGpu??!!this.config.measurer,dag:o});if(m.length===0){a.set(f,{key:f,kind:"empty",weight:1});continue}let g=new zy({target:this.target,primFunc:e,blockName:h,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(f,{key:f,kind:"session",session:g,weight:1})}let p=[...a.values()].filter(h=>h.kind==="session");p.length>0&&this.scheduler.run(p,i,this.config);let c=new Map;for(let h of n){let f=a.get(u.get(h));if(f.kind==="cache"){c.set(h,{sketchName:f.cached.sketchName,params:f.cached.params,score:f.cached.score,fromCache:!0});continue}if(f.kind==="empty")continue;let d=f.session.best();if(d){if(this.config.useTuningDB&&!f.stored){let m=new gf(f.key,d.sketchName,d.params,d.score,f.session.bestTrace(),this.db.version);m.medianMs=d.medianMs||null,m.minMs=d.minMs||null,this.db.store(f.key,m),f.stored=!0}c.set(h,{sketchName:d.sketchName,params:d.params,score:d.score,fromCache:!1,medianMs:d.medianMs,minMs:d.minMs})}}return c}tuneAndApply(e,t=null){let n=this.tune(e,t);if(n.size>0){let s=this._applyBestSchedule(e,n);if(s)return{func:s.func,results:n,applied:!0}}return{func:e,results:n,applied:!1}}_applyBestSchedule(e,t){let n=this._buildDefaultSchedule(e),s=!!n&&this._scheduleIsValid(n),o=s&&this._isStrongBackendSchedule(n),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,n),{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 n=Lo(e),s=new Sn(n),o=Oy(n.body),i=mf(n),a=new Set,u=[];for(let c of t)if(c[1].sketchName==="fused"){let h=Ny(n,i,c[0],dt);h&&a.add(h),u.unshift(c)}else u.push(c);let p=new Set;for(let[c,h]of u)if(!(a.has(c)||p.has(h))&&(p.add(h),!(!h.sketchName||!h.params)))try{let d=zT(n,c,this.target,o,{richGpu:this.config.richGpu??!!this.config.measurer,dag:i}).find(m=>m.name===h.sketchName);d&&this._fitsThreadBlock(n,c,d,h.params)&&d.instantiate(h.params)(s,c,this.target)}catch(f){this._warn("apply-tuned-block",c,f);continue}return this._scheduleResidualBlocks(s,a),n}catch(n){return this._warn("build-tuned-schedule",null,n),null}}_scheduleResidualBlocks(e,t){let n=null;for(let s of io(e.func.body))if(!(t.has(s)||this._blockIsParallelized(e,s))){n||(n=new Rs(this.target));try{n.applyToBlock(e,s)}catch(o){this._warn("residual-block",s,o)}}}_blockIsParallelized(e,t){let n;try{n=e.getLoops(t)}catch(s){return this._warn("block-loops",t,s),!0}for(let s of n)if(s.kind===F.THREAD_BINDING||s.kind===F.PARALLEL||s.kind===F.VECTORIZED)return!0;return!1}_scheduleIsValid(e){return!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock?!0:Vy(e)<=this.target.maxThreadsPerBlock}_buildDefaultSchedule(e){try{let t=Lo(e),n=new Sn(t);return $y(n,this.target,this.config)||new Rs(this.target).applyToAllBlocks(n),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,n,s){if(!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock)return!0;try{let o=Lo(e);return n.instantiate(s)(new Sn(o),t,this.target),Vy(o)<=this.target.maxThreadsPerBlock}catch(o){return this._warn("fits-thread-block",t,o),!1}}}});var Uy,CR=M(()=>{"use strict";xi();fa();ro();LR();qT();Uy=class extends Wn{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 qy(this.target,t,e.trace):t.enabled&&(this._policy=new Rs(this.target,null,e.trace))}run(e,t){let n=this.config.scheduling,s=t.trace;if(n.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,h]of i.results)h.fromCache&&u++,s.explainsEnabled&&s.explain("schedule",c,h.sketchName,`autotuned: best of search${h.fromCache?" (cached)":""}, score ${h.score!=null?h.score.toFixed(3):"n/a"}`,{target:this.target.name,params:h.params})}s.autotuneStats(e.name,{durationMs:a,blockCount:p,applied:!!(i&&i.applied),cacheHits:u})}else if(n.enabled||n.gpuTiling){if(e.cublasInfo||e._tensorIntrin)return;let o=performance.now(),i=new Sn(e);!$y(i,this.target,n)&&n.enabled&&this._policy.applyToAllBlocks(i),s.functionEvent("scheduling",e.name,{durationMs:performance.now()-o})}}}});function DR(r){let e={analyzer:new Os,simp:null};e.simp=new hm(e.analyzer);let t=Zr(r.body,e);return r.body=t,r._setChild("body",t),r}function tz(r,e,t){let n=r.analyzer.getVarBound(e);return t&&t.type==="IntImmNode"&&t.value>0?r.analyzer.bind(e,0,t.value-1):r.analyzer.setVarBound(e,null),n}function Zr(r,e){if(!r||typeof r!="object")return r;switch(r.type){case"ForNode":{let t=tz(e,r.loopVar.name,r.extent),n=Zr(r.body,e);e.analyzer.setVarBound(r.loopVar.name,t);let s=new de(r.loopVar,r.min,r.extent,r.kind,n,r.threadTag);return r.annotations&&(s.annotations=r.annotations),s}case"BlockNode":{let t=[];for(let i of r.iterVars)i.iterVar&&(t.push([i.iterVar.name,e.analyzer.getVarBound(i.iterVar.name)]),e.analyzer.setVarBound(i.iterVar.name,i.binding?la(e.analyzer,i.binding):null));let n=r.iterVars.map(nz(e)),s=Zr(r.body,e),o=r.initBody?Zr(r.initBody,e):null;for(let[i,a]of t)e.analyzer.setVarBound(i,a);return new pe(r.name,n,r.reads,r.writes,s,o)}case"SeqNode":return new te(r.stmts.map(t=>Zr(t,e)));case"IfThenElseNode":{let t=Hn(r.condition,e);return Mo(e.analyzer,t)?Zr(r.thenBody,e):ua(e.analyzer,t)?r.elseBody?Zr(r.elseBody,e):new te([]):new xe(t,Zr(r.thenBody,e),r.elseBody?Zr(r.elseBody,e):null)}case"BufferStoreNode":return new Y(r.buffer,r.indices.map(t=>Hn(t,e)),Hn(r.value,e));case"LetStmtNode":return new Ye(r.variable,Hn(r.value,e),Zr(r.body,e));case"AllocateNode":return new Ve(r.buffer,r.scope,Zr(r.body,e));case"WhileNode":return new Zo(r.condVar,Zr(r.condBody,e),Zr(r.loopBody,e));case"EvaluateNode":return new ou(Hn(r.value,e));default:return r}}function nz(r){return e=>{if(!e.iterVar||!e.binding)return e;let t=Hn(e.binding,r);return new qi(e.iterVar,t,e.kind)}}function Hn(r,e){if(!r||typeof r!="object"||!r.type)return r;switch(r.type){case"IntImmNode":case"FloatImmNode":case"VariableNode":return r;case"BufferLoadNode":return new q(r.buffer,r.indices.map(t=>Hn(t,e)));case"MathOpNode":{let t=Hn(r.a,e),n=r.b?Hn(r.b,e):null;return e.simp.simplify(new C(r.op,t,n))}case"CompareNode":{let t=Hn(r.a,e),n=Hn(r.b,e);return e.simp.simplify(new qe(r.direction,t,n))}case"CastNode":return new rt(Hn(r.expr,e),r.fromDtype,r.toDtype);case"CallExternNode":return new ke(r.externName,r.args.map(t=>Hn(t,e)),r.dtype);case"IfThenElseNode":{let t=Hn(r.condition,e),n=Hn(r.thenBody,e),s=r.elseBody?Hn(r.elseBody,e):null;return Mo(e.analyzer,t)?n:s!==null&&ua(e.analyzer,t)?s:new xe(t,n,s)}default:return r}}var PR=M(()=>{"use strict";Ku();Yc();De();l(DR,"simplifyPrimFunc");l(tz,"bindLoopVar");l(Zr,"simplifyStmt");l(nz,"simplifyIterVar");l(Hn,"simplifyExpr")});var Ky,$R=M(()=>{"use strict";xi();PR();Ky=class extends Wn{static{l(this,"SimplifyPass")}constructor(){super("SimplifyPass","simplify")}run(e,t){let n=performance.now();return DR(e),t.trace.functionEvent("simplify",e.name,{durationMs:performance.now()-n}),e}}});function FR(r){return!!r&&typeof r=="object"&&typeof r.name=="string"&&r.dtype!==void 0&&r.shape!==void 0&&r.type===void 0}var YT,JT,rz,Wy,zR=M(()=>{"use strict";YT=class{static{l(this,"BufferInterval")}constructor(e,t,n,s){this.buffer=e,this.firstUse=t,this.lastUse=n,this.scope=s}get size(){return this.buffer.sizeInBytes()}overlaps(e){return this.firstUse<=e.lastUse&&e.firstUse<=this.lastUse}},JT=class{static{l(this,"BufferLivenessResult")}constructor(e,t,n){this.intervals=e,this.stmtOrder=t,this.paramBuffers=n}isParam(e){return this.paramBuffers.has(e)}getTemporaries(){let e=[];for(let[t,n]of this.intervals)this.paramBuffers.has(t)||e.push(n);return e}interfere(e,t){let n=this.intervals.get(e),s=this.intervals.get(t);return!n||!s?!1:n.overlaps(s)}},rz=new Set(["_parent","_parentKey","_parentIdx"]);l(FR,"isBuffer");Wy=class{static{l(this,"BufferLiveness")}static analyze(e){let t=new Map,n=[],s=0,o=new Set;for(let[,h]of e.bufferMap)o.add(h);let i=[],a=l(h=>{if(!h)return;let f=t.get(h);f?(s<f.firstUse&&(f.firstUse=s),s>f.lastUse&&(f.lastUse=s)):(f=new YT(h,s,s,h.scope),t.set(h,f)),i.push(h)},"touch"),u=l((h,f)=>{if(!(!h||typeof h!="object"||f.has(h))){if(f.add(h),FR(h)){a(h);return}FR(h.buffer)&&a(h.buffer);for(let d of Object.keys(h)){if(rz.has(d)||d==="buffer")continue;let m=h[d];if(!(!m||typeof m!="object"))if(Array.isArray(m))for(let g of m)u(g,f);else u(m,f)}}},"touchAll"),p=l((h,f)=>{for(let d=h;d<i.length;d++){let m=t.get(i[d]);m&&f>m.lastUse&&(m.lastUse=f)}},"extendRegion"),c=l(h=>{if(h)switch(h.type){case"SeqNode":for(let f of h.stmts)c(f);break;case"ForNode":{let f=s,d=i.length;u(h.min,new Set),u(h.extent,new Set),c(h.body);let m=s>f?s-1:f;p(d,m);break}case"WhileNode":{let f=s,d=i.length;u(h.condVar,new Set),c(h.condBody),c(h.loopBody);let m=s>f?s-1:f;p(d,m);break}case"BlockNode":n.push({idx:s,node:h});for(let f of h.reads)a(f.buffer);for(let f of h.writes)a(f.buffer);u(h.body,new Set),h.initBody&&u(h.initBody,new Set),s++;break;case"AllocateNode":a(h.buffer),c(h.body);break;case"IfThenElseNode":{let f=s,d=i.length;u(h.condition,new Set),c(h.thenBody),h.elseBody&&c(h.elseBody);let m=s>f?s-1:f;p(d,m);break}case"LetStmtNode":u(h.value,new Set),c(h.body);break;case"EvaluateNode":u(h.value,new Set);break;default:u(h,new Set);break}},"walk");return c(e.body),new JT(t,n,o)}}});function sz(r,e){if(r.shape.length!==e.shape.length)return!1;for(let t=0;t<r.shape.length;t++)if(r.shape[t]!==e.shape[t])return!1;return!0}function Il(r,e){if(r===e)return!0;if(!r||!e||typeof r!="object"||typeof e!="object"||r.type!==e.type)return!1;switch(r.type){case"VariableNode":return r.name===e.name;case"IntImmNode":return r.value===e.value;case"FloatImmNode":return r.value===e.value;case"MathOpNode":return r.op===e.op&&Il(r.a,e.a)&&Il(r.b,e.b);case"CompareNode":return r.direction===e.direction&&Il(r.a,e.a)&&Il(r.b,e.b);case"CastNode":return r.toDtype===e.toDtype&&Il(r.expr,e.expr);case"BufferLoadNode":return r.buffer===e.buffer&&QT(r.indices,e.indices);default:return!1}}function QT(r,e){if(r.length!==e.length)return!1;for(let t=0;t<r.length;t++)if(!Il(r[t],e[t]))return!1;return!0}function VR(r,e){r&&Ss(r,e)}function oz(r,e,t){let n=[],s=[];for(let p of[r.body,r.initBody])p&&VR(p,c=>{c.type==="BufferStoreNode"&&c.buffer===t?n.push(c):c.type==="BufferLoadNode"&&c.buffer===e&&s.push(c)});if(n.length!==1||s.length===0)return!1;let o=n[0],i=o.indices;for(let p of s)if(!QT(p.indices,i))return!1;let a=new Set;for(let p of[o.value,...o.indices])p&&VR(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&&QT(p.indices,i)))return!1}return!0}function iz(r,e){if(r)for(let t of gu(r,n=>n.type==="BlockNode",{kinds:"stmt"}))e.push(t)}var ZT,Hy,GR=M(()=>{"use strict";Gr();ZT=class{static{l(this,"InplaceCandidate")}constructor(e,t,n){this.srcBuffer=e,this.dstBuffer=t,this.reason=n}},Hy=class{static{l(this,"InplaceAnalysis")}static analyze(e,t,n=new Set){let s=[],o=[];iz(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 h=a.get(c.buffer);(h===void 0||p.idx>h)&&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 h of p.writes){let f=h.buffer;if(!(t.isParam(f)&&!n.has(f)))for(let d of p.reads){let m=d.buffer;if(m===f||u.has(m)||t.isParam(m)&&!n.has(m)||!sz(m,f)||m.dtype!==f.dtype||m.scope!==f.scope)continue;let g=t.intervals.get(m),y=t.intervals.get(f);if(!(!g||!y)&&g.lastUse<=y.firstUse){let _=a.get(m);if(_===void 0||_<=c){if(!oz(p,m,f))continue;s.push(new ZT(m,f,`${m.name} last used at ${g.lastUse}, ${f.name} first used at ${y.firstUse}`)),u.add(m);break}}}}}return s}};l(sz,"shapesMatch");l(Il,"exprEqual");l(QT,"indexListEqual");l(VR,"walkNodes");l(oz,"isInplaceComputeSafe");l(iz,"collectBlocks")});function jR(r,e){let t=0,n=r.length;for(;t<n;){let s=t+n>>1;r[s].offset<=e.offset?t=s+1:n=s}r.splice(t,0,e)}function qR(r){return{best:null,consider(e,t,n){return t<n?null:r==="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 XT,ex,Yy,UR=M(()=>{"use strict";l(jR,"insertByOffset");l(qR,"gapSelector");XT=class{static{l(this,"MemoryBlock")}constructor(e,t,n){this.offset=e,this.size=t,this.buffer=n}get end(){return this.offset+this.size}overlaps(e){return this.offset<e.end&&e.offset<this.end}},ex=class{static{l(this,"MemoryPool")}constructor(e,t=64,n="best-fit"){this.scope=e,this.alignment=t,this.strategy=n,this.blocks=[],this.peakUsage=0}allocate(e,t){let n=this._align(e),s=this._findFreeOffset(n);return this.placeAt(s,n,t)}placeAt(e,t,n){let s=this._align(t),o=new XT(e,s,n);jR(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,n=0,s=qR(this.strategy);for(let o of t){let i=this._align(n),a=o.offset-i,u=s.consider(i,a,e);if(u!==null)return u;o.end>n&&(n=o.end)}return s.result(this._align(n))}fragmentation(){if(this.peakUsage===0)return 0;let e=this.blocks.reduce((t,n)=>t+n.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)}},Yy=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=[],n=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,h]of this.inplaceMap)!o.has(c)||!o.has(h)||(a.has(h)||a.set(h,[]),a.get(h).push(c));if(a.size>0){let c=new Map;for(let h of e){let f=h.buffer;if(c.get(f)===1)continue;let d=[f];for(;d.length;){let m=d[d.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&&d.push(y);continue}if(d.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,h)=>{let f=c.size,d=h.size,m=f>0,g=d>0;if(m&&g){let y=d-f;if(y!==0)return y}else if(m!==g)return m?-1:1;return c.firstUse-h.firstUse}),p=new Map;for(let c of u){let h=c.buffer;if(this.inplaceMap.has(h)){let w=this.inplaceMap.get(h),T=this.assignments.get(w);if(T){this.assignments.set(h,{offset:T.offset,size:c.size,scope:c.scope,pool:T.pool,inplaceOf:w});continue}}let f=c.size;if(f===0)continue;if(f<0){this.assignments.set(h,{offset:0,size:0,scope:c.scope,pool:c.scope,inplaceOf:null,isDynamic:!0});continue}let d=c.scope,m=this.pools.get(d);m||(m=new ex(d,n),this.pools.set(d,m)),p.has(d)||p.set(d,[]);let g=p.get(d),y=i.get(h)??c.lastUse,_=this._interferenceOffset(g,c.firstUse,y,m._align(f),n,s),b=m.placeAt(_,f,h);jR(g,{firstUse:c.firstUse,lastUseEff:y,offset:b.offset,size:b.size}),this.assignments.set(h,{offset:b.offset,size:b.size,scope:d,pool:d,inplaceOf:null})}return this}_interferenceOffset(e,t,n,s,o,i){let a=[];for(let c of e)c.firstUse<=n&&t<=c.lastUseEff&&a.push([c.offset,c.offset+c.size]);let u=0,p=qR(i);for(let[c,h]of a){let f=Math.ceil(u/o)*o,d=c-f,m=p.consider(f,d,s);if(m!==null)return m;h>u&&(u=h)}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 n=this.pools.get(e);return n?n.peakUsage:0}let t=0;for(let[,n]of this.pools)t+=n.peakUsage;return t}}});function Zy(r,e,t){if(!r||typeof r!="object"||t.has(r))return!1;if(t.add(r),r.type==="BufferLoadNode"&&(!e||r.buffer===e))return!0;for(let n of Object.keys(r)){if(n==="_parent"||n==="_parentKey"||n==="_parentIdx")continue;let s=r[n];if(!(!s||typeof s!="object")){if(Array.isArray(s)){for(let o of s)if(Zy(o,e,t))return!0}else if(Zy(s,e,t))return!0}}return!1}function KR(r){let e=new Set,t=new Map,n=new Set,s=[r.body];for(;s.length>0;){let o=s.pop();if(!(!o||typeof o!="object"||n.has(o))){if(n.add(o),o.type==="BufferStoreNode"&&o.buffer){let i=o.buffer;for(let u of o.indices)if(Zy(u,null,new Set)){e.add(i);break}Zy(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 az(r,e){let t=new Set,n=[r];for(;n.length>0;){let s=n.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"&&n.push(u)}continue}i&&typeof i=="object"&&n.push(i)}}}}var tx,Jy,WR=M(()=>{"use strict";zR();GR();UR();De();Qb();tx=class{static{l(this,"MemoryPlan")}constructor(e,t,n){this.assignment=e,this.liveness=t,this.inplaceCandidates=n,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,n=this.inplaceCandidates.length;return{peakMemory:this.assignment.peakMemory(),scopeBreakdown:e,totalTemporaries:t,totalInplace:n,materializedReuse:this.aliasMap.size,assignments:this.assignment.assignments}}},Jy=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=Wy.analyze(e),n=t.getTemporaries(),s=[];this.enableInplace&&(s=Hy.analyze(e,t));let o=new Yy;return o.assign(n,s,this.alignment,this.allocStrategy),new tx(o,t,s)}planAndRewrite(e){let t=this.plan(e);return{func:this._insertAllocations(e,t),plan:t}}_insertAllocations(e,t){let n=t.liveness.getTemporaries();if(n.length===0)return e;let s=new Map;this.poolAllocation?this._assignPoolOffsets(e,t,n):(s=this._buildReuseAliases(n,t,e),s.size>0&&az(e.body,s)),t.aliasMap=s;let o=[...n].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 Ve(p,c.isDynamic?"dynamic":c.scope,i)))}return e.body=i,e._setChild("body",i),e}_assignPoolOffsets(e,t,n){let s=KR(e);for(let o of n){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,n){let s=KR(n),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 h=c.buffer,f=t.assignment.getAssignment(h);if(!f||f.inplaceOf||f.isDynamic||o.has(h)||h.numel()<=0||s.has(h))continue;let d=`${h.scope}|${h.dtype}|${h.shape.join(",")}|${h.strides.join(",")}`,m=u.get(d);m||(m=[],u.set(d,m)),m.push(c)}let p=new Map;for(let c of u.values()){if(c.length<2)continue;c.sort((f,d)=>f.firstUse-d.firstUse||a(f)-a(d));let h=new ku((f,d)=>f.lastUse-d.lastUse);for(let f of c){let d=h.peek();d&&d.lastUse<f.firstUse?(h.pop(),d.lastUse=a(f),h.push(d),p.set(f.buffer,d.rep)):h.push({rep:f.buffer,lastUse:a(f)})}}return p}};l(Zy,"exprLoadsBuffer");l(KR,"collectFreshZeroDependent");l(az,"rewriteBufferAliases")});var Qy,HR=M(()=>{"use strict";xi();WR();Qy=class extends Wn{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 Jy({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 n=performance.now(),{plan:s}=this._planner.planAndRewrite(e),o=s.getReport();t.trace.memoryStats(e.name,{durationMs:performance.now()-n,peakMemory:o.peakMemory,totalTemporaries:o.totalTemporaries,totalInplace:o.totalInplace})}}});function El(r){return r&&r.type==="IntImmNode"?r.value:null}function uz(r){if(!r||r.type!=="ForNode"||!YR.has(r.kind)||El(r.min)!==0)return null;let e=El(r.extent);if(e===null)return null;let t=r.body;if(!t||t.type!=="ForNode"||!YR.has(t.kind)||El(t.min)!==0)return null;let n=El(t.extent);if(n===null||n<=0)return null;let s=t.body;if(!s||s.type!=="IfThenElseNode"||s.elseBody)return null;let o=lz(s.condition,r.loopVar.name,t.loopVar.name,n);if(o===null||o<=0)return null;let i=Math.floor(o/n),a=o-i*n;if(a===0||i<1||e!==i+1)return null;let u=new Os;if(u.bind(r.loopVar.name,0,i-1),u.bind(t.loopVar.name,0,n-1),!Mo(u,s.condition))return null;let p=s.thenBody,c=new de(t.loopVar,new L(0),new L(n),t.kind,p,t.threadTag),h=new de(r.loopVar,new L(0),new L(i),r.kind,c,r.threadTag),f=ti(Ma(p),g=>g.type==="VariableNode"&&g.name===r.loopVar.name?new L(i):g),d=new Ne(t.loopVar.name,t.loopVar.dtype),m=new de(d,new L(0),new L(a),t.kind,f,t.threadTag);return new te([h,m])}function lz(r,e,t,n){if(!r)return null;let s,o;if(r.type==="MathOpNode"&&r.op==="<")s=r.a,o=r.b;else if(r.type==="CompareNode"&&r.direction==="lt")s=r.a,o=r.b;else return null;let i=El(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||El(a.b)!==n?null:i}var YR,Xy,JR=M(()=>{"use strict";xi();De();Gr();cf();Ku();Yc();YR=new Set([F.SERIAL,F.PARALLEL,F.VECTORIZED,F.UNROLLED]),Xy=class extends Wn{static{l(this,"LoopPartitionPass")}constructor(){super("LoopPartitionPass","loopPartition")}run(e,t){let n=ti(e.body,s=>uz(s)||s);return n&&n!==e.body&&(e.body=n,e._setChild("body",n)),e}};l(El,"intImm");l(uz,"partitionLoop");l(lz,"matchFlatGuard")});function e_(r){let e=r.body;if(!e||e.type!=="BlockNode")return null;let t=e.body;if(!t||t.type!=="BufferStoreNode")return null;let n=t,s=n.value;if(!s||s.type!=="MathOpNode"||!cz.has(s.op))return null;let o=null,i=null;if(s.a&&s.a.type==="BufferLoadNode"&&s.a.buffer===n.buffer?(o=s.a,i=s.b):s.b&&s.b.type==="BufferLoadNode"&&s.b.buffer===n.buffer&&(o=s.b,i=s.a),!o)return null;let a=nx(n.indices),u=nx(o.indices);if(a!==u||a.includes("?"))return null;let p=n.indices.map(f=>{if(f.type!=="VariableNode")return f;for(let d of e.iterVars)if(d.iterVar&&d.iterVar.name===f.name&&d.binding)return d.binding;return f}),c=r.loopVar.name,h=nx(p);return h.includes("?")||h.includes("$"+c)?null:{store:n,loadSide:o,valueSide:i,outerIndices:p,block:e,op:n.value.op}}function nx(r){return r.map(rx).join(",")}function rx(r){return r?r.type==="VariableNode"?"$"+r.name:r.type==="IntImmNode"?String(r.value):r.type==="MathOpNode"?"("+rx(r.a)+r.op+(r.b?rx(r.b):"")+")":"?":"?"}var cz,sx=M(()=>{"use strict";cz=new Set(["+","*","max","min"]);l(e_,"detectAccumulator");l(nx,"indicesKey");l(rx,"exprKey")});function uo(r){if(!(!r||typeof r!="object"||!r.type))switch(r.type){case"ForNode":{let e=e_(r);r.accumulator=e,e||uo(r.body);return}case"BlockNode":uo(r.body),r.initBody&&uo(r.initBody);return;case"SeqNode":for(let e of r.stmts)uo(e);return;case"LetStmtNode":case"AllocateNode":uo(r.body);return;case"IfThenElseNode":uo(r.thenBody),r.elseBody&&uo(r.elseBody);return;case"WhileNode":uo(r.condBody),uo(r.loopBody);return;default:return}}var t_,ZR=M(()=>{"use strict";xi();sx();t_=class extends Wn{static{l(this,"AccumulatorDetectionPass")}constructor(){super("AccumulatorDetectionPass","accumulatorDetect")}run(e,t){return uo(e.body),e}};l(uo,"annotateStmt")});function fz(r){let e=[],t=[r.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 n=null;for(let s of e){if(!s.name.includes("matmul"))return null;s.reads.length>=2&&s.writes.length>=1&&(n=s)}return n}function hz(r){let e=fz(r);if(!e)return null;let t=e.reads[0].buffer,n=e.reads[1].buffer,s=e.writes[0].buffer;if(!QR.has(t.dtype)||!QR.has(n.dtype)||s.dtype!=="f32"||t.shape.length!==2||n.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%pz===0))return null;let u=new Set;for(let[,p]of r.bufferMap)u.add(p.name);return!u.has(t.name)||!u.has(n.name)||!u.has(s.name)?null:{M:o,N:i,K:a,a:t.name,b:n.name,c:s.name}}var pz,QR,n_,XR=M(()=>{"use strict";xi();fa();pz=16,QR=new Set(["f16","bf16"]);l(fz,"findMatmulBlock");l(hz,"detectWmmaMatmul");n_=class extends Wn{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 n=hz(e);n&&(new Sn(e).tensorize("wmma_16x16x16_f16f16f32",n),t&&t.trace&&t.trace.explainsEnabled&&t.trace.explain("tensorize",e.name,"wmma_16x16x16_f16f16f32",`auto-tensorized ${n.M}x${n.N}x${n.K} f16 GEMM`,{target:this.target.name}))}}});function dz(r){let e=[],t=new Map,n=new Map,s=[{node:r,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=n.get(o.buffer.name);u||(u=new Set,n.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 en(o))s.push({node:u,block:a})}return{blocks:e,loadCount:t,storeWriters:n}}var r_,eM=M(()=>{"use strict";xi();fa();ro();Gr();l(dz,"analyzeFunc");r_=class extends Wn{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||i0(e))return;let n=this.config.scheduling;if(!(n.enabled||n.gpuTiling||n.autotune))return;let s=new Set;for(let[,c]of e.bufferMap)s.add(c.name);let{blocks:o,loadCount:i,storeWriters:a}=dz(e.body),u=new Sn(e),p=!1;for(let c of o){if(c.hasInit||c.writes.size===0)continue;let h=[...c.writes];if(h.every(f=>!s.has(f)&&a.get(f).size===1)&&h.some(f=>(i.get(f)||0)>0)&&[...c.reads].every(f=>(a.get(f)?a.get(f).size:0)<=1))try{u.computeInlineBlock(c.name),p=!0}catch{}}p&&s0(e)}}});function ox(r,e,t=mz){return t.filter(n=>n.phase===r).sort((n,s)=>n.priority-s.priority).map(n=>n.factory(e)).filter(Boolean)}var mz,tM=M(()=>{"use strict";mz=[];l(ox,"tirPassesForPhase")});function nM(r){let e=[];for(let t of ox("pre",r))e.push(t);e.push(new r_(r)),r.optimization.tensorize&&e.push(new n_(r)),e.push(new Uy(r)),r.optimization.loopPartition&&e.push(new Xy),e.push(new Ky),e.push(new Qy(r)),r.optimization.detectAccumulators&&e.push(new t_);for(let t of ox("post",r))e.push(t);return e}var rM=M(()=>{"use strict";CR();$R();HR();JR();ZR();XR();eM();tM();l(nM,"buildTirPipeline")});var bf,sM=M(()=>{"use strict";De();bf=class{static{l(this,"TensorVerifier")}constructor(){this.errors=[],this.boundVars=new Set}verify(e){if(this.errors=[],this.boundVars.clear(),!(e instanceof Jo))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 n=t.pop();if(n)switch(n.type){case"SeqNode":for(let s=n.stmts.length-1;s>=0;s--)t.push(n.stmts[s]);break;case"ForNode":this.boundVars.has(n.loopVar.name)&&this.errors.push(`Loop variable ${n.loopVar.name} already bound`),this.boundVars.add(n.loopVar.name),t.push({type:"_unbind",name:n.loopVar.name}),t.push(n.body);break;case"BlockNode":for(let s of n.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=n.iterVars.length-1;s>=0;s--)n.iterVars[s].iterVar&&t.push({type:"_unbind",name:n.iterVars[s].iterVar.name});t.push(n.body),n.initBody&&t.push(n.initBody);break;case"AllocateNode":n.buffer||this.errors.push("Allocate missing buffer"),t.push(n.body);break;case"LetStmtNode":this._visitExpr(n.value),this.boundVars.add(n.variable.name),t.push({type:"_unbind",name:n.variable.name}),t.push(n.body);break;case"IfThenElseNode":this._visitExpr(n.condition),n.elseBody&&t.push(n.elseBody),t.push(n.thenBody);break;case"WhileNode":n.condVar||this.errors.push("WhileNode missing condition variable"),t.push(n.loopBody),t.push(n.condBody);break;case"BufferStoreNode":if(n.buffer||this.errors.push("BufferStore missing buffer"),(!n.indices||n.indices.length!==n.buffer.shape.length)&&this.errors.push(`BufferStore rank mismatch for ${n.buffer?n.buffer.name:"unknown"}`),n.indices)for(let s of n.indices)this._visitExpr(s);this._visitExpr(n.value);break;case"EvaluateNode":this._visitExpr(n.value);break;case"_unbind":this.boundVars.delete(n.name);break;default:this._visitExpr(n);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 ix(r){let e=[];r.functionCount===0&&e.push(new vt("Module has no functions"));for(let t of r)wf(t,e);return e}function wf(r,e=[]){if(!r.entryBlock)return e.push(new vt("Function has no entry block",null,r)),e;r.entryBlock.arguments.length!==r.inputTypes.length&&e.push(new vt(`Entry block has ${r.entryBlock.arguments.length} args, expected ${r.inputTypes.length}`,null,r));for(let s=0;s<r.entryBlock.arguments.length;s++){let o=r.entryBlock.arguments[s],i=r.inputTypes[s];i&&!o.type.equals(i)&&e.push(new vt(`Block arg ${s} type ${Rn(o.type)} != expected ${Rn(i)}`,null,r))}let t=new Set;for(let s of r.entryBlock.arguments)t.add(s);for(let s of r.body)oM(s,t);for(let s of r.body)iM(s,r,t,e);let n=r.getReturnOp();if(!n)e.push(new vt("Missing return op",null,r));else{n.numOperands!==r.outputTypes.length&&e.push(new vt(`Return has ${n.numOperands} operands, function declares ${r.outputTypes.length} outputs`,n,r));for(let s=0;s<Math.min(n.numOperands,r.outputTypes.length);s++){let o=n.getOperand(s).type,i=r.outputTypes[s];i instanceof I&&o instanceof I&&(o.dtype!==i.dtype&&e.push(new vt(`Return operand ${s} dtype ${o.dtype} != expected ${i.dtype}`,n,r)),o.shapeCompatible(i)||e.push(new vt(`Return operand ${s} shape incompatible: ${Rn(o)} vs ${Rn(i)}`,n,r)))}}return e}function oM(r,e){for(let t of r.arguments)e.add(t);for(let t of r)for(let n=0;n<t.numResults;n++){let s=t.getResult(n);s&&e.add(s)}}function gz(r,e,t){let n=new Set;for(let u of r)n.add(u);let s=1,o=2,i=new Map,a=new Set;for(let u of r){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],h=c.op;if(c.i<h.numOperands){let f=h.getOperand(c.i);c.i++;let d=f&&f.definingOp;if(!d||!n.has(d))continue;let m=i.get(d);m===s?a.has(d)||(a.add(d),t.push(new vt("participates in a value dependency cycle",d,e))):m===void 0&&(i.set(d,s),p.push({op:d,i:0}));continue}i.set(h,o),p.pop()}}}function iM(r,e,t,n){gz(r,e,n);for(let s of r)yz(s,e,t,n);if(r.size>0){let s=r.lastOp;if(r.parentRegion&&r.parentRegion.parentOp){let o=r.parentRegion.parentOp.opName,i=ne.get(o);i&&i.hasRegions&&(s.isTerminator()||n.push(new vt(`Block in ${o} region must end with terminator, got '${s.opName}'`,s,e)))}}}function yz(r,e,t,n){for(let o=0;o<r.numOperands;o++){let i=r.getOperand(o);if(!i){n.push(new vt(`Operand ${o} is null`,r,e));continue}if(!(i instanceof Wo)){n.push(new vt(`Operand ${o} is not a Value`,r,e));continue}t.has(i)||n.push(new vt(`Operand ${o} used before definition`,r,e))}for(let o=0;o<r.numResults;o++){let i=r.getResult(o);i||n.push(new vt(`Result ${o} is null`,r,e)),i&&i.definingOp!==r&&n.push(new vt(`Result ${o} definingOp mismatch`,r,e))}let s=ne.get(r.opName);if(!s){!["return","yield"].includes(r.opName)&&!ne.has(r.opName)&&n.push(new vt(`Unknown op '${r.opName}'`,r,e));return}s.numOperands>=0&&r.numOperands!==s.numOperands&&n.push(new vt(`'${r.opName}' expects ${s.numOperands} operands, got ${r.numOperands}`,r,e)),s.numResults>=0&&r.numResults!==s.numResults&&n.push(new vt(`'${r.opName}' expects ${s.numResults} results, got ${r.numResults}`,r,e));for(let o of s.attrs)o.required&&!r.hasAttr(o.name)&&n.push(new vt(`'${r.opName}' missing required attribute '${o.name}'`,r,e));if(s.hasRegions&&s.numRegions>0&&r.numRegions!==s.numRegions&&n.push(new vt(`'${r.opName}' expects ${s.numRegions} regions, got ${r.numRegions}`,r,e)),s.verify){let o=s.verify(r);if(o)for(let i of o)n.push(new vt(i,r,e))}if(s.inferResultTypes&&r.numResults>0){let o=[];for(let a=0;a<r.numOperands;a++)o.push(r.getOperand(a).type);let i=s.inferResultTypes(o,r.attributes,r.results.map(a=>a.type));if(i)for(let a=0;a<Math.min(i.length,r.numResults);a++){let u=r.getResult(a).type,p=i[a];p instanceof I&&u instanceof I&&(u.dtype!==p.dtype&&n.push(new vt(`Result ${a} dtype ${u.dtype} != inferred ${p.dtype}`,r,e)),u.shapeCompatible(p)||n.push(new vt(`Result ${a} shape ${Rn(u)} incompatible with inferred ${Rn(p)}`,r,e)))}}for(let o of r.regions){let i=new Set(t);for(let a of o)oM(a,i);for(let a of o)iM(a,e,i,n)}}var vt,aM=M(()=>{"use strict";Oe();ch();At();vt=class{static{l(this,"VerificationError")}constructor(e,t=null,n=null){this.message=e,this.op=t,this.func=n}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(ix,"verifyModule");l(wf,"verifyFunction");l(oM,"collectScopeDefs");l(gz,"detectCycles");l(iM,"verifyBlock");l(yz,"verifyOperation")});var s_,ax,Ll,ux,lx=M(()=>{"use strict";_T();Oe();s_=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 n=e[t];n<this.min&&(this.min=n),n>this.max&&(this.max=n)}this.count+=e.length,this.histogram&&this.histogram.update(e)}enableHistogram(e=2048){this.histogram=new ax(e)}},ax=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 n=this.numBins/t;for(let s=0;s<e.length;s++){let o=Math.min(this.numBins-1,Math.floor((e[s]-this.rangeMin)*n));this.bins[o]++}this.totalCount+=e.length}_initRange(e){let t=1/0,n=-1/0;for(let s=0;s<e.length;s++)e[s]<t&&(t=e[s]),e[s]>n&&(n=e[s]);t===n&&(n=t+1),this.rangeMin=t,this.rangeMax=n,this.initialized=!0}_expandAndRebucket(e){let t=this.rangeMin,n=this.rangeMax;for(let c=0;c<e.length;c++)e[c]<t&&(t=e[c]),e[c]>n&&(n=e[c]);let s=new Float64Array(this.bins),o=this.rangeMin,a=this.rangeMax-o;this.rangeMin=t,this.rangeMax=n;let u=n-t,p=this.numBins/u;if(this.bins.fill(0),a>0){let c=a/this.numBins;for(let h=0;h<this.numBins;h++){if(s[h]===0)continue;let f=o+(h+.5)*c,d=Math.min(this.numBins-1,Math.floor((f-t)*p));this.bins[d]+=s[h]}}for(let c=0;c<e.length;c++){let h=Math.min(this.numBins-1,Math.floor((e[c]-t)*p));this.bins[h]++}this.totalCount+=e.length}computePercentileThreshold(e){let t=this.totalCount*e,n=0,s=(this.rangeMax-this.rangeMin)/this.numBins;for(let o=0;o<this.numBins;o++)if(n+=this.bins[o],n>=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 n=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 h=Math.floor(c*a),f=Math.min(i,Math.floor((c+1)*a)),d=0;for(let g=h;g<f;g++)d+=t[g];let m=f-h;if(m>0){let g=d/m;for(let y=h;y<f;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,n=i)}let o=(this.rangeMax-this.rangeMin)/this.numBins;return this.rangeMin+n*o}},Ll=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 n=0;n<t.numResults;n++){let s=t.getResult(n);if(s.type instanceof I&&Lt(s.type.dtype)){let o=new s_;(this.mode==="entropy"||this.mode==="percentile")&&o.enableHistogram(),this.observers.set(s,o)}}for(let t of e.args)if(t.type instanceof I&&Lt(t.type.dtype)){let n=new s_;(this.mode==="entropy"||this.mode==="percentile")&&n.enableHistogram(),this.observers.set(t,n)}}observe(e,t){let n=this.observers.get(e);n&&n.update(t)}getResult(){return new ux(this.observers,this.mode)}},ux=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,n){let s=this.getRange(e);if(!s)return null;let o=t===kr.PER_TENSOR_ASYMMETRIC;if(this._mode==="percentile"&&this._observers.get(e).histogram){if(o)return hr.fromRange(s.min,s.max,t,n);let a=this._observers.get(e).histogram.computePercentileThreshold(.999);return hr.fromRange(-a,a,t,n)}if(this._mode==="entropy"&&this._observers.get(e).histogram){if(o)return hr.fromRange(s.min,s.max,t,n);let i=this._observers.get(e).histogram,a=n==="ui8"?256:255,u=i.computeEntropyThreshold(a);return hr.fromRange(-u,u,t,n)}return hr.fromRange(s.min,s.max,t,n)}hasData(e){let t=this._observers.get(e);return t&&t.count>0}values(){return this._observers.keys()}}});function bz(r,e){let t=[],n=new Set;for(let s of r.ops())if(e.has(s.opName))for(let o=0;o<s.numOperands;o++){let i=s.getOperand(o);if(n.has(i)||!(i.type instanceof I)||!Lt(i.type.dtype))continue;let a=i.definingOp;a&&a.opName==="constant"||(n.add(i),t.push(i))}return t}function wz(r,e){let t=e.map(d=>d.type),n=new un(r.name,r.inputTypes,[...r.outputTypes,...t]),s=new Map,o=r.entryBlock.arguments,i=n.entryBlock.arguments;for(let d=0;d<o.length;d++)s.set(o[d],i[d]);let a=r.entryBlock.opsArray(),u=new Set(a),p=wo(a,d=>u.has(d),"ignore"),c=new Map;for(let d of p)d.opName!=="return"&&c.set(d,d.clone(s));for(let d of a)d.opName!=="return"&&n.entryBlock.pushOp(c.get(d));let h=r.getReturnOp(),f=[];if(h)for(let d of h.operands)f.push(s.get(d)||d);for(let d of e)f.push(s.get(d));return n.entryBlock.pushOp(new Re("return",f,[],{})),n}function uM(r,e,t,n={}){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=n.quantizableOps||_z,o=n.mode||"minmax",i=bz(r,s),a=new Ll(o);a.attach(r);let u=a.getResult();if(i.length===0)return u;let p=wz(r,i),c=new wn("__calibrate__");c.addFunction(p);let h=r.outputTypes.length,d=(n.compileFn||Tz)(c,e);for(let m of t){let g=Array.isArray(m)?m:[m],y=p.outputTypes.map(_=>new Float32Array(Math.max(1,_.numel())));d.run(r.name,...g,...y);for(let _=0;_<i.length;_++)a.observe(i[_],y[h+_])}return u}function Tz(r,e){throw new Error("collectCalibration: opts.compileFn is required (pass the compileModule function to avoid a circular import)")}var _z,lM=M(()=>{"use strict";xs();Yo();sr();Oe();ji();lx();_z=new Set(["dot","conv","add","mul","sub"]);l(bz,"activationTargets");l(wz,"buildCaptureFunction");l(uM,"collectCalibration");l(Tz,"defaultCompileFn")});function px(r){let e=0;for(let t=0;t<r.numResults;t++){let n=r.getResult(t).type;n instanceof I&&n.isFullyStatic&&(e+=n.sizeInBytes())}return e}var cx,Cl,fx,o_,cM=M(()=>{"use strict";At();Oe();rf();cx=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+=px(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}=_i(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(px,"estimateOpMemory");Cl=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||{}}}},fx=class{static{l(this,"PartitionResult")}constructor(e,t,n){this.partitions=e,this.opToPartition=t,this.transferEdges=n}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}},o_=class{static{l(this,"GraphPartitioner")}constructor(e){this.config=e instanceof Cl?e:new Cl(e),this._supportCache=new Map,this._buildSupportMap()}partition(e){let t=this._collectPartitionableOps(e),n=this._assignTargets(t),s=this._buildPartitions(t,n),o=this._mergeSmallPartitions(s,n),i=this._computeTransferEdges(o),a=new Map;for(let u of o)for(let p of u.ops)a.set(p,u);return new fx(o,a,i)}_buildSupportMap(){for(let e of this.config.targets){let t=new Set;for(let n of ne.names())this._targetSupportsOp(e,n)&&t.add(n);this._supportCache.set(e,t)}}_targetSupportsOp(e,t){if(e.hasLibraryOp(t))return!0;let n=ne.get(t);return n?n.isConstant||n.isTerminator?!0:e.isGPU()?n.isElementwise||n.isReduction||n.isBroadcast||n.isInjective||n.getAttr("gpuCapable")===!0:e.isCPU()?!0:e.isWasm()?!n.isOpaque&&t!=="custom_call":!1:!1}_collectPartitionableOps(e){let t=[];for(let n of e.ops()){let s=ne.get(n.opName);!s||s.isTerminator||t.push(n)}return t}_assignTargets(e){let t=new Map;for(let n of e){let s=n.getAttr("device");if(s){let a=this._resolveDeviceAttr(s);if(a){t.set(n,a);continue}}let o=this.config.opTargetOverrides.get(n.opName);if(o){t.set(n,o);continue}let i=this._selectBestTarget(n);t.set(n,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],n=-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>n&&(n=i,t=s)}return t}_scoreTargetForOp(e,t){let n=ne.get(t.opName);if(!n)return 0;let s=0;if(e.hasLibraryOp(t.opName)&&(s+=100),n.isReduction||n.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:n}=nf(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)+px(a)},"onAttach")}),s=new Map;for(let i of n){let a=new cx(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,n){let s=this.config.memoryLimits.get(n.name);if(!s)return!0;let o=px(t);return e.memoryBytes+o<=s}_mergeSmallPartitions(e,t){if(e.length<=1)return e;let n=new Map;for(let d of e)for(let m of d.ops)n.set(m,d);let s=new Set,o=new Map,i=new Map;for(let d of e)s.has(d)||o.set(d,new Set);for(let d of e){if(s.has(d))continue;let m=o.get(d);for(let g of d.ops)for(let y=0;y<g.numResults;y++)for(let _ of g.getResult(y).uses()){let b=n.get(_.user);b&&b!==d&&m.add(b)}}let a=l((d,m)=>{let g=o.get(d);for(let y of o.get(m))y!==d&&g.add(y);g.delete(m);for(let[y,_]of o)_.has(m)&&(_.delete(m),y!==d&&_.add(d));o.delete(m),i=new Map},"mergeSucc"),u=l(d=>{let m=i.get(d);if(m)return m;m=new Set;let g=[...o.get(d)];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(d,m),m},"reachOf"),p=l((d,m)=>{for(let g of o.get(d))if(g!==m&&u(g).has(m))return!0;return!1},"pathThroughIntermediate"),c=l((d,m)=>p(d,m)||p(m,d),"mergeCreatesCycle"),h=[];for(let d=0;d<e.length;d++){let m=e[d];if(s.has(m))continue;if(m.size>=this.config.minPartitionSize){h.push(m);continue}let g=-1,y=-1/0;for(let _=0;_<e.length;_++){if(d===_)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)n.set(b,_);s.add(m)}else h.push(m)}let f=new Set;for(let d of h)for(let m of d.ops)f.add(m);for(let d of e){let m=!1;for(let g of d.ops)if(!f.has(g)){m=!0;break}if(m){h.push(d);for(let g of d.ops)f.add(g)}}return h}_mergeScore(e,t){let n=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))&&n++;return n}_computeTransferEdges(e){let t=[],n=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),h=`${p.id}|${o.id}|${c.id}`;n.has(h)||(n.add(h),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),n=new Map,s=new Map;for(let u of e)n.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),n.set(u,n.get(u)+1))}let o=[];for(let u of e)n.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=n.get(p)-1;n.set(p,c),c===0&&o.push(p)}}return i}}});var i_,a_,pM=M(()=>{"use strict";nn();sr();xs();cM();rf();Kt();i_=class extends Ge{static{l(this,"GraphPartitionPass")}constructor(e={}){super("GraphPartitionPass"),this.partitionerConfig=new Cl(e),this.partitionResult=null}run(e,t){if(this.partitionerConfig.targets.length<2)return oe.UNCHANGED;let n=new o_(this.partitionerConfig);return this.partitionResult=n.partition(e),this.partitionResult.numPartitions<=1?oe.UNCHANGED:(this._annotateOps(),this._insertTransferOps(e),this.trace&&this.trace.level>=ue.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,numPartitions:this.partitionResult.numPartitions,transferEdges:this.partitionResult.transferEdges.length,level:ue.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,n=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 Re("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 h=s.get(i.dst),f=h&&h.get(a)||o.get(i.dst)||null,d=a.definingOp;if(f&&d&&n.has(d)&&n.has(f)&&n.get(d)>=n.get(f))t.insertAfter(c,d);else if(f)t.insertBefore(c,f);else if(d&&n.has(d))t.insertAfter(c,d);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))],n=new Map,s=new Map;for(let o of t)n.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=n.get(i);for(let u=0;u<o.numOperands;u++){let p=o.getOperand(u);a.has(p)||a.set(p,o)}}return{useMap:n,firstInPart:s}}_buildOrderIndex(e){let t=new Map,n=0;for(let s of e.ops())t.set(s,n++);return t}},a_=class extends Ge{static{l(this,"PartitionMaterializationPass")}constructor(e={}){super("PartitionMaterializationPass"),this.targets=e.targets||[]}run(e,t){let n=this._collectPartitions(e);if(n.size<=1)return oe.UNCHANGED;let s=this._getModule(e);if(!s)return oe.UNCHANGED;let o=this._materializePartitions(e,n);for(let i of o)s.addFunction(i);return this._rewriteOriginalFunction(e,o,n),this.trace&&this.trace.level>=ue.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,partitions:n.size,subFunctions:o.length,level:ue.DEBUG}),e.bumpVersion(),oe.CHANGED}_collectPartitions(e){let t=new Map;for(let n of e.ops()){let s=n.getAttr("partition_id");s!==void 0&&(t.has(s)||t.set(s,{id:s,target:n.getAttr("partition_target"),ops:[]}),t.get(s).ops.push(n))}return t}_getModule(e){return e._module||null}_materializePartitions(e,t){let n=[];for(let[s,o]of t){let i=new Set(o.ops),{inputs:a,outputs:u}=_i(i,o.ops),p=a.map(_=>_.type),c=u.map(_=>_.type),h=`${e.name}_partition_${s}`,f=new un(h,p,c);f._partitionTarget=o.target;let d=new Map;for(let _=0;_<a.length;_++)d.set(a[_],f.args[_]);let m=Ba(o.ops);for(let _ of m)f.entryBlock.pushOp(_.clone(d));let g=u.map(_=>d.get(_)||_),y=new Re("return",g,[]);f.entryBlock.pushOp(y),n.push(f)}return n}_rewriteOriginalFunction(e,t,n){for(let s of t)e.setAttr?.(`sub_${s.name}`,s._partitionTarget)}}});function Tf(r){return oc(r,-1)}function xz(r,e,t){let n=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 h=Tf(c.shape);if(h<0)return!0;let f=h*bn(c.dtype);f>s&&(s=f)}let o=r.getAttr("num_carry"),i=r.getAttr("num_xs"),a=0;for(let u=0;u<o;u++){let p=r.getOperand(i+u).type,c=p&&p.shape?Tf(p.shape):-1;c>0&&(a+=c*bn(p.dtype))}return 3*s+2*a>n}function Sz(r,e){let n=r.regions[0].entryBlock,s=r.getAttr("num_carry"),o=[],i=null;for(let b of n.ops()){if(b.opName==="yield"){i=b;continue}o.push(b)}if(!i)return null;let a=new Set(o),u=new Set(n.arguments),p=[],c=new Set,h=[],f=new Set;for(let b of o)for(let w=0;w<b.numOperands;w++){let T=b.getOperand(w),S=T.definingOp;if(!u.has(T)&&!(S&&a.has(S))){if(S&&oo(S)){f.has(S)||(f.add(S),h.push(S));continue}c.has(T)||(c.add(T),p.push(T))}}let d=[...n.arguments,...p];for(let b of d)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 un(e,d.map(b=>b.type),m.map(b=>b.type)),y=new Map;for(let b=0;b<d.length;b++)y.set(d[b],g.args[b]);for(let b of h)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 Re("return",_,[])),{bodyFunc:g,captured:p,numCarry:s,numYs:m.length-s})}function kz(r,e,t,n,s){let{getSlot:o,newSlot:i,steps:a,addedFuncs:u}=n,p=new wn(r.name+"_mod");p.addFunction(r);let c=Ti(p,s);if(c){let h=c.plan,f=e.length,d=new Map,m=!0;for(let g of h.returnFixups||[]){if(g.kind!=="copy"){m=!1;break}d.set(g.pos,g.srcSlot)}if(m){let g=l(T=>{let S=f+T;return d.has(S)?d.get(S):h.argSlots[S]},"retSlot"),y=new Map;for(let T=0;T<f;T++)y.set(h.argSlots[T],o(e[T]));for(let T of h.intermediates)y.set(T.slot,i(T.shape,T.dtype));for(let T=0;T<t.length;T++){let S=g(T);y.has(S)||y.set(S,o(t[T]))}let _=l(T=>{let S=y.get(T);return S===void 0?null:S},"mapSlot"),b=[],w=!0;for(let T of h.steps){let S=T.inputSlots.map(_),x=T.outputSlots.map(_);if(S.includes(null)||x.includes(null)){w=!1;break}b.push({name:T.name,inputSlots:S,outputSlots:x})}if(w){for(let T of b)a.push(T);for(let T of p.functions())u.push(T);return!0}}}return a.push({name:r.name,inputSlots:e.map(o),outputSlots:t.map(o)}),u.push(r),!0}function fM(r,e,t){let n=my({ops:r,opSet:new Set(r)},e,new Map);return n?kz(n.subFunc,n.inputs,n.outputs,t,1):!1}function vz(r,e,t){let{getSlot:n,newSlot:s,steps:o,scanLoops:i,addedFuncs:a}=t,u=r.regions[0];if(!u||!u.entryBlock)return!1;let p=Sz(r,e);if(!p)return!1;let{bodyFunc:c,captured:h,numCarry:f,numYs:d}=p,m=r.getAttr("num_xs"),g=new wn(e+"_mod");g.addFunction(c);let y=Ti(g,2),_=y?y.plan:null,b=[],w=[];for(let P=0;P<f;P++){let V=r.getOperand(m+P).type;b.push(V.shape),w.push(V.dtype)}let T=[],S=[];for(let P=0;P<m;P++){let V=r.getOperand(P).type;T.push(V.shape.slice(1)),S.push(V.dtype)}let x=[],k=[];for(let P=0;P<d;P++){let V=r.getResult(f+P).type;x.push(V.shape.slice(1)),k.push(V.dtype)}let A=b.map((P,V)=>s(P,w[V])),N=b.map((P,V)=>s(P,w[V])),v=T.map((P,V)=>s(P,S[V])),O=[],R=[],B=[],E=[];for(let P=0;P<f;P++)R.push(n(r.getOperand(m+P)));for(let P=0;P<f;P++)B.push(n(r.getResult(P)));for(let P of h)E.push(n(P));let z=[],D=[];for(let P=0;P<m;P++)z.push(n(r.getOperand(P)));for(let P=0;P<d;P++)D.push(n(r.getResult(f+P)));let J=o.length;if(!_)O=x.map((P,V)=>s(P,k[V])),o.push({name:c.name,inputSlots:[...v,...A,...E],outputSlots:[...N,...O]}),a.push(c);else{let P=m+f+h.length,V=new Map;for(let G of _.returnFixups||[]){if(G.kind!=="copy")return!1;V.set(G.pos,G.srcSlot)}let he=l(G=>{let K=P+G;return V.has(K)?V.get(K):_.argSlots[K]},"bodyReturnSlot"),le=new Map;for(let G=0;G<m;G++)le.set(_.argSlots[G],v[G]);for(let G=0;G<f;G++)le.set(_.argSlots[m+G],A[G]);for(let G=0;G<h.length;G++)le.set(_.argSlots[m+f+G],E[G]);for(let G of _.intermediates)le.set(G.slot,s(G.shape,G.dtype));for(let G=0;G<f;G++){let K=he(G);le.has(K)||le.set(K,N[G])}for(let G=0;G<d;G++){let K=he(f+G),ie=le.get(K);ie===void 0&&(ie=s(x[G],k[G]),le.set(K,ie)),O.push(ie)}let Ie=l(G=>{let K=le.get(G);return K===void 0?null:K},"mapSlot");for(let G of _.steps){let K=G.inputSlots.map(Ie),ie=G.outputSlots.map(Ie);if(K.includes(null)||ie.includes(null))return!1;o.push({name:G.name,inputSlots:K,outputSlots:ie})}for(let G of g.functions())a.push(G)}let j=o.length,U=r.getOperand(0).type.shape[0];return typeof U!="number"||U<0?!1:(i.push({T:U,loopStart:J,loopEnd:j,carry:b.map((P,V)=>({a:A[V],b:N[V],initSlot:R[V],finalSlot:B[V],bytes:Tf(P)*bn(w[V])})),xs:v.map((P,V)=>({xtSlot:P,xsSlot:z[V],stepBytes:Tf(T[V])*bn(S[V])})),ys:O.map((P,V)=>({ytSlot:P,ysSlot:D[V],stepBytes:Tf(x[V])*bn(k[V])}))}),!0)}function hx(r,e,t=!1){if(!e||typeof e.isWebGPU!="function"||!e.isWebGPU()||r.functionCount!==1)return null;let n=r.functions().next().value,s=n.getReturnOp();if(!s)return null;let o=[];for(let R of n.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&&xz(B,E,e)){R=!0;break}}if(!R)return null}let u=[];for(let R of i)oo(R)||qt(R.opName)||u.push(R);let p=Ba(u),c=new Map,h=0,f=l(R=>{let B=c.get(R);return B===void 0&&(B=h++,c.set(R,B)),B},"getSlot");for(let R of n.args)f(R);let d=[],m=l((R,B)=>{let E=h++;return d.push({slot:E,shape:[...R],dtype:B}),E},"newSlot"),g=[],y=[],_=[],b={getSlot:f,newSlot:m,steps:g,scanLoops:y,addedFuncs:_},w=new Set(o),T=[],S=0,x=0;for(let R of p)if(w.has(R)){if(T.length&&!fM(T,n.name+"_seg"+S++,b)||(T=[],!vz(R,n.name+"_scan"+x++,b)))return null}else T.push(R);if(T.length&&!fM(T,n.name+"_seg"+S++,b))return null;let k=n.args.map(f),A=[],N=new Set;for(let R=0;R<s.numOperands;R++){let B=s.getOperand(R),E=k.length;if(c.has(B)&&!(B.isBlockArgument&&B.isBlockArgument())){let z=f(B);N.has(z)?(k.push(h++),A.push({pos:E,kind:"copy",srcSlot:z})):(N.add(z),k.push(z))}else if(B.isBlockArgument&&B.isBlockArgument())k.push(h++),A.push({pos:E,kind:"copy",srcSlot:f(B)});else return null}let v=new Set(k),O=new Set;for(let[R,B]of c)if(!(v.has(B)||O.has(B))){if(O.add(B),!R.type||!R.type.isFullyStatic)return null;d.some(E=>E.slot===B)||d.push({slot:B,shape:[...R.type.shape],dtype:R.type.dtype})}r.removeFunction(n.name);for(let R of _)r.addFunction(R);return{plan:{numSlots:h,argSlots:k,intermediates:d,steps:g,returnFixups:A,scanLoops:y}}}var hM=M(()=>{"use strict";sr();xs();Yo();gy();Tr();Vn();Oe();l(Tf,"numel");l(xz,"isScanOversized");l(Sz,"buildScanBodyFunction");l(kz,"inlineNativeSplit");l(fM,"emitSegment");l(vz,"emitScanLoop");l(hx,"splitGraphForScan")});function xf(r){dx.push(r),dx.sort((e,t)=>(e.priority??100)-(t.priority??100))}function Nz(r){for(let e of dx)if(e.applies(r))return e;return null}function dM(r,e){let t=Nz(e);return t?t.run(r,e):null}var dx,mM=M(()=>{"use strict";gy();hM();dx=[];l(xf,"registerGraphSplitStrategy");l(Nz,"selectGraphSplitStrategy");l(dM,"splitGraph");xf({name:"cublas",priority:10,applies:l(r=>r.config.matmulBackend==="cublas","applies"),run:l(r=>MB(r),"run")});xf({name:"cuda-attention",priority:15,applies:l(r=>r.cudaAttention,"applies"),run:l(r=>Ti(r,1),"run")});xf({name:"cuda-native-matmul-chain",priority:20,applies:l(r=>r.cudaMatmulChain,"applies"),run:l(r=>Ti(r),"run")});xf({name:"cuda-native-conv-chain",priority:25,applies:l(r=>r.cudaConvChain,"applies"),run:l(r=>Ti(r),"run")});xf({name:"webgpu",priority:30,applies:l(r=>r.isWebGPU,"applies"),run:l((r,e)=>{let t=hx(r,e.target);return!t&&BB(r,e.target.maxThreadsPerBlock||256)&&(t=hx(r,e.target,!0),t||(t=Ti(r,2))),t},"run")})});function gM(r,e){let t=new id;for(let[,n]of r.bufferMap)t.paramBuffers.add(n.name);return Az(r.body,t,e),Bz(r,t,e),Rz(r.body,t),t}function Az(r,e,t){let n=[r];for(;n.length>0;){let s=n.pop();if(!(!s||typeof s!="object")){switch(s.type){case"ForNode":if(s.loopVar&&e.locals.set(s.loopVar.name,"i32"),s.kind===F.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,ei(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&&!cv(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,ei(o.iterVar.dtype)),o.binding&&typeof o.binding=="object"&&o.binding.type&&n.push(o.binding);break}if(s.body&&n.push(s.body),s.value&&typeof s.value=="object"&&s.value.type&&n.push(s.value),s.stmts)for(let o of s.stmts)n.push(o);if(s.thenBody&&n.push(s.thenBody),s.elseBody&&n.push(s.elseBody),s.initBody&&n.push(s.initBody),s.condBody&&n.push(s.condBody),s.loopBody&&n.push(s.loopBody),s.condition&&typeof s.condition=="object"&&s.condition.type&&n.push(s.condition),s.a&&typeof s.a=="object"&&s.a.type&&n.push(s.a),s.b&&typeof s.b=="object"&&s.b.type&&n.push(s.b),s.expr&&typeof s.expr=="object"&&s.expr.type&&n.push(s.expr),s.args)for(let o of s.args)typeof o=="object"&&o!==null&&o.type&&n.push(o);if(s.indices)for(let o of s.indices)typeof o=="object"&&o!==null&&o.type&&n.push(o)}}}function Bz(r,e,t){let n=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*bn(i.dtype);let p=1;for(let c of i.shape)p*=typeof c=="number"&&c>0?c:1;return Math.max(Oz,p*bn(i.dtype))},"bufBytes");for(let[,i]of r.bufferMap)s=Math.ceil(s/n)*n,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/n)*n,e.memoryLayout.bufferOffsets.set(i,s),s+=o(a));e.memoryLayout.totalBytes=s}function Rz(r,e){let t=new Map;for(let n of gu(r,s=>s.type==="BufferStoreNode"&&s.buffer&&!e.paramBuffers.has(s.buffer.name))){let s=n.buffer.name;t.has(s)||t.set(s,[]),t.get(s).push(n.value)}for(let[n,s]of t)if(s.every(i=>i&&i.type==="IntImmNode"&&i.value===0||i&&i.type==="FloatImmNode"&&i.value===0)&&e.zeroBuffers.add(n),s.length===1&&s[0]){let i=s[0];(i.type==="IntImmNode"||i.type==="FloatImmNode")&&e.constantBuffers.set(n,i.value)}}var Oz,yM=M(()=>{"use strict";xc();Vn();De();Gr();l(gM,"scanMetadata");l(Az,"walkTree");Oz=65536;l(Bz,"computeMemoryLayout");l(Rz,"detectZeroBuffers")});function Sf(r,e,t){let n=typeof r.offset=="number"?r.offset:0;if(e.length===0)return new L(n);if(e.length===1)return n===0?e[0]:new C("+",e[0],new L(n));let s=[];n!==0&&s.push(new L(n));for(let o=0;o<e.length;o++){let i=e[o];if(i.type==="IntImmNode"&&i.value===0)continue;let a=r.strides[o];if(a===1)s.push(i);else if(typeof a=="number"&&a>=0)s.push(new C("*",i,new L(a)));else{let u=Mz(r,o,t);s.push(new C("*",i,u))}}return s.length===0?new L(0):s.reduce((o,i)=>new C("+",o,i))}function Mz(r,e,t){let n=[];for(let s=e+1;s<r.shape.length;s++){let o=r.shape[s];typeof o=="number"&&o>=0?n.push(new L(o)):n.push(Iz(r,s,t))}return n.length===0?new L(1):n.reduce((s,o)=>new C("*",s,o))}function Iz(r,e,t){let n=r.shape[e];if(n instanceof Ce)return uu(n,s=>new Ne(qa(s),"index"));if(t){let s=`${r.name}:${e}`,o=t.get(s);if(o)return new Ne(o.name,"index")}return new L(1)}var _M=M(()=>{"use strict";De();Vs();Lb();l(Sf,"flattenIndex");l(Mz,"computeDynamicStride");l(Iz,"resolveShapeParam")});function bM(r,e){let t=gM(r,e),n={shapeParamMap:r.shapeParamMap,accCounter:0,metadata:t},s=is(r.body,n);return new sd(r.name,r.params,s,r.bufferMap,r.shapeParams,r.shapeParamMap,t)}function is(r,e){if(!r||typeof r!="object")return r;switch(r.type){case"ForNode":return Ez(r,e);case"BlockNode":return Lz(r,e);case"SeqNode":return Cz(r,e);case"BufferStoreNode":return Dz(r,e);case"LetStmtNode":return Pz(r,e);case"AllocateNode":return $z(r,e);case"IfThenElseNode":return Fz(r,e);case"WhileNode":return zz(r,e);case"EvaluateNode":return new ou(mr(r.value,e));case"SyncThreadsNode":return r;default:return r}}function Ez(r,e){let t=r.accumulator!==void 0?r.accumulator:e_(r);if(t)return Vz(r,t,e);let n=is(r.body,e);return new de(r.loopVar,r.min,r.extent,r.kind,n,r.threadTag)}function Lz(r,e){let t=[];for(let o of r.iterVars)o.iterVar&&o.binding&&t.push({name:o.iterVar.name,dtype:o.iterVar.dtype,expr:mr(o.binding,e)});let n=is(r.body,e),s=r.initBody?is(r.initBody,e):null;return t.length>0?s?(n=new te([new mu(t,s),new mu(t,n)]),s=null):n=new mu(t,n):s&&(n=new te([s,n])),n}function Cz(r,e){let t=[];for(let n of r.stmts)t.push(is(n,e));return new te(t)}function Dz(r,e){let t=Sf(r.buffer,r.indices,e.shapeParamMap),n=mr(r.value,e),s=r.buffer.dtype||ir(r.value);return new Tc(r.buffer,t,n,s)}function Pz(r,e){return new Ye(r.variable,mr(r.value,e),is(r.body,e))}function $z(r,e){return new Ve(r.buffer,r.scope,is(r.body,e))}function Fz(r,e){return new xe(mr(r.condition,e),is(r.thenBody,e),r.elseBody?is(r.elseBody,e):null)}function zz(r,e){return new Zo(r.condVar,is(r.condBody,e),is(r.loopBody,e))}function mr(r,e){if(!r||typeof r!="object"||!r.type)return r;switch(r.type){case"BufferLoadNode":{let t=Sf(r.buffer,r.indices,e.shapeParamMap),n=new wc(r.buffer,t,r.buffer.dtype);return No(n),n}case"MathOpNode":{let t=mr(r.a,e),n=r.b?mr(r.b,e):null,s={...r,a:t,b:n};return Object.setPrototypeOf(s,Object.getPrototypeOf(r)),No(s),s}case"CompareNode":{let t=mr(r.a,e),n=mr(r.b,e),s={...r,a:t,b:n};return Object.setPrototypeOf(s,Object.getPrototypeOf(r)),No(s),s}case"CastNode":{let t=mr(r.expr,e),n={...r,expr:t};return Object.setPrototypeOf(n,Object.getPrototypeOf(r)),No(n),n}case"CallExternNode":{let t=r.args.map(s=>mr(s,e)),n={...r,args:t};return Object.setPrototypeOf(n,Object.getPrototypeOf(r)),No(n),n}case"IfThenElseNode":{let t=mr(r.condition,e),n=mr(r.thenBody,e),s=r.elseBody?mr(r.elseBody,e):null,o={...r,condition:t,thenBody:n,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(r)),No(o),o}default:return No(r),r}}function Vz(r,e,t){let n=`_acc_${t.accCounter++}`,s=ir(e.loadSide);t.metadata.locals.set(n,s);let o=new Map;for(let d of e.block.iterVars)d.iterVar&&d.binding&&o.set(d.iterVar.name,d.binding);let i=o.size>0?Ar(e.valueSide,o):e.valueSide,a=Sf(e.store.buffer,e.outerIndices,t.shapeParamMap),u=new wc(e.store.buffer,a,e.store.buffer.dtype);No(u);let p=mr(i,t),c=Sf(e.store.buffer,e.outerIndices,t.shapeParamMap),h=new Tc(e.store.buffer,c,null,e.store.buffer.dtype),f=e.block.initBody?o.size>0?kf(e.block.initBody,o):e.block.initBody:null;return new od({localName:n,dtype:s,op:e.op,initLoad:u,loopVar:r.loopVar,extent:r.extent,loopKind:r.kind,body:p,flushStore:h,initBody:f?is(f,t):null})}function Ar(r,e){if(!r||typeof r!="object"||!r.type)return r;if(r.type==="VariableNode"&&e.has(r.name))return e.get(r.name);if(r.type==="BufferLoadNode"){let t=r.indices.map(o=>Ar(o,e));if(!t.some((o,i)=>o!==r.indices[i]))return r;let s={...r,indices:t};return Object.setPrototypeOf(s,Object.getPrototypeOf(r)),s}if(r.type==="MathOpNode"){let t=Ar(r.a,e),n=r.b?Ar(r.b,e):null;if(t===r.a&&n===r.b)return r;let s={...r,a:t,b:n};return Object.setPrototypeOf(s,Object.getPrototypeOf(r)),s}if(r.type==="CompareNode"){let t=Ar(r.a,e),n=Ar(r.b,e);if(t===r.a&&n===r.b)return r;let s={...r,a:t,b:n};return Object.setPrototypeOf(s,Object.getPrototypeOf(r)),s}if(r.type==="CastNode"){let t=Ar(r.expr,e);if(t===r.expr)return r;let n={...r,expr:t};return Object.setPrototypeOf(n,Object.getPrototypeOf(r)),n}if(r.type==="CallExternNode"){let t=r.args.map(o=>Ar(o,e));if(!t.some((o,i)=>o!==r.args[i]))return r;let s={...r,args:t};return Object.setPrototypeOf(s,Object.getPrototypeOf(r)),s}if(r.type==="IfThenElseNode"){let t=Ar(r.condition,e),n=Ar(r.thenBody,e),s=r.elseBody?Ar(r.elseBody,e):null;if(t===r.condition&&n===r.thenBody&&s===r.elseBody)return r;let o={...r,condition:t,thenBody:n,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(r)),o}return r}function kf(r,e){if(!r||typeof r!="object"||!r.type)return r;switch(r.type){case"BufferStoreNode":{let t=r.indices.map(o=>Ar(o,e)),n=Ar(r.value,e);if(t.every((o,i)=>o===r.indices[i])&&n===r.value)return r;let s={...r,indices:t,value:n};return Object.setPrototypeOf(s,Object.getPrototypeOf(r)),s}case"SeqNode":{let t=r.stmts.map(s=>kf(s,e)),n={...r,stmts:t};return Object.setPrototypeOf(n,Object.getPrototypeOf(r)),n}case"ForNode":{let t=kf(r.body,e);if(t===r.body)return r;let n={...r,body:t};return Object.setPrototypeOf(n,Object.getPrototypeOf(r)),n}case"IfThenElseNode":{let t=Ar(r.condition,e),n=kf(r.thenBody,e),s=r.elseBody?kf(r.elseBody,e):null,o={...r,condition:t,thenBody:n,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(r)),o}default:return r}}var wM=M(()=>{"use strict";xc();yM();_M();De();sx();l(bM,"lowerToLIR");l(is,"lowerStmt");l(Ez,"lowerForNode");l(Lz,"lowerBlockNode");l(Cz,"lowerSeqNode");l(Dz,"lowerBufferStore");l(Pz,"lowerLetStmt");l($z,"lowerAllocate");l(Fz,"lowerIfThenElse");l(zz,"lowerWhile");l(mr,"lowerExpr");l(Vz,"lowerAccumulator");l(Ar,"substituteVars");l(kf,"substituteVarsStmt")});function TM(r){let e=[],t={errors:e,boundVars:new Set,path:[],bufferNames:new Set};if(!r||r.type!=="LIRFunc")return e.push(new lo("root must be LIRFunc")),e;for(let[,n]of r.bufferMap)t.bufferNames.add(n.name);for(let n of r.shapeParams)t.boundVars.add(n.name);if(r.metadata)for(let[n]of r.metadata.locals)t.boundVars.add(n);return as(r.body,t),e}function as(r,e){if(!(!r||typeof r!="object")){switch(e.path.push(r.type),r.type){case"ForNode":Gz(r,e);break;case"SeqNode":for(let t of r.stmts)as(t,e);break;case"LIRFlatStoreNode":jz(r,e);break;case"LIRAccumulatorNode":qz(r,e);break;case"LIRBindingsNode":Uz(r,e);break;case"LetStmtNode":{vn(r.value,e);let t=e.boundVars.has(r.variable.name);e.boundVars.add(r.variable.name),as(r.body,e),t||e.boundVars.delete(r.variable.name);break}case"AllocateNode":{let t=r.buffer?e.bufferNames.has(r.buffer.name):!0;r.buffer&&e.bufferNames.add(r.buffer.name),as(r.body,e),r.buffer&&!t&&e.bufferNames.delete(r.buffer.name);break}case"IfThenElseNode":vn(r.condition,e),as(r.thenBody,e),r.elseBody&&as(r.elseBody,e);break;case"WhileNode":as(r.condBody,e),as(r.loopBody,e);break;case"EvaluateNode":vn(r.value,e);break;default:break}e.path.pop()}}function Gz(r,e){r.extent?vn(r.extent,e):e.errors.push(new lo("ForNode missing extent",[...e.path]));let t=!0;r.loopVar?(t=e.boundVars.has(r.loopVar.name),e.boundVars.add(r.loopVar.name)):e.errors.push(new lo("ForNode missing loopVar",[...e.path])),as(r.body,e),r.loopVar&&!t&&e.boundVars.delete(r.loopVar.name)}function jz(r,e){r.buffer||e.errors.push(new lo("LIRFlatStoreNode missing buffer",[...e.path])),vn(r.offsetExpr,e),vn(r.value,e)}function qz(r,e){r.localName||e.errors.push(new lo("LIRAccumulatorNode missing localName",[...e.path])),r.dtype||e.errors.push(new lo("LIRAccumulatorNode missing dtype",[...e.path]));let t=r.localName?e.boundVars.has(r.localName):!0;r.localName&&e.boundVars.add(r.localName),vn(r.initLoad,e),vn(r.body,e),as(r.flushStore,e),r.initBody&&as(r.initBody,e),r.localName&&!t&&e.boundVars.delete(r.localName)}function Uz(r,e){let t=[];for(let n of r.bindings)vn(n.expr,e),e.boundVars.has(n.name)||t.push(n.name),e.boundVars.add(n.name);as(r.body,e);for(let n of t)e.boundVars.delete(n)}function vn(r,e){if(!(!r||typeof r!="object"||!r.type))switch(r.type){case"LIRFlatLoadNode":r.buffer||e.errors.push(new lo("LIRFlatLoadNode missing buffer",[...e.path])),vn(r.offsetExpr,e);break;case"MathOpNode":vn(r.a,e),r.b&&vn(r.b,e);break;case"CompareNode":vn(r.a,e),vn(r.b,e);break;case"CastNode":vn(r.expr,e);break;case"CallExternNode":if(r.args)for(let t of r.args)vn(t,e);break;case"IfThenElseNode":vn(r.condition,e),vn(r.thenBody,e),r.elseBody&&vn(r.elseBody,e);break;case"VariableNode":r.name!==void 0&&!e.boundVars.has(r.name)&&e.errors.push(new lo(`unbound variable '${r.name}'`,[...e.path]));break;case"IntImmNode":case"FloatImmNode":break;default:break}}var lo,xM=M(()=>{"use strict";lo=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(TM,"verifyLIR");l(as,"verifyStmt");l(Gz,"verifyForNode");l(jz,"verifyFlatStore");l(qz,"verifyAccumulator");l(Uz,"verifyBindings");l(vn,"verifyExpr")});function SM(r,e,t={}){return new Do({target:e,...t}).compileFunction(r)}var u_,mx,Do,l_=M(()=>{"use strict";Yo();xs();ZO();um();QO();PB();rM();Ui();Ec();nm();sM();aM();lx();lM();pM();mM();jT();Kt();P0();cT();wM();xM();Kt();u_=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??ue.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}},mx=class{static{l(this,"CompilationResult")}constructor(e,t,n){this.module=e,this.trace=t,this.errors=n||[]}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()}},Do=class r{static{l(this,"Compiler")}constructor(e){if(this.config=e instanceof u_?e:new u_(e),!this.config.target)throw new Error("Compiler requires a target");this.context=new jg({loweringRules:this.config.loweringRules,codegenEntries:this.config.codegenEntries})}compile(e){let t=new am(this.config.trace),n=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:n,original:e,working:n?Ok(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),!n&&s.length>0)throw new Error(s[0].toString());return new mx(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,n=typeof t.target.isWebGPU=="function"&&t.target.isWebGPU(),s=typeof t.target.isGPU=="function"&&t.target.isGPU()&&!n,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=dM(e.working,{config:t,target:t.target,cudaMatmulChain:e.cudaMatmulChain,cudaConvChain:a,cudaAttention:u,isWebGPU:n})},"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:Uv(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 wn("single");return t.addFunction(e),this.compile(t)}calibrate(e,t="minmax"){let n=new Ll(t);for(let s of e)n.attach(s);return n}_runCalibration(e,t){let n=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,h)=>new r({target:h,verify:this.config.verify}).compile(c),"compileFn"),p=uM(o,a,n.calibrationData,{mode:n.calibrationMode||"minmax",quantizableOps:n.quantizableOps,compileFn:u});this.config.quantization={...n,calibration:p},t.phaseEnd("calibrate",performance.now()-i)}_runGraphPasses(e,t,n,s,o,i){let a=new oa,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),h=u>=c;for(let m of DB(this.config,this.config.target,{cudaMatmulChain:h,context:this.context}))a.addPass(m);a.setTrace(n),this.config.verifyMode==="full"&&a.setVerifyHook((m,g)=>(g?ix(m):wf(m)).map(_=>_.toString())),n.phaseStart("graphPasses");let f=performance.now(),d=a.run(e,{errorMode:i?"resilient":"strict",passContext:this.config.passContext});if(d.errors){for(let m of d.errors)s.push(m),n.errorEvent(m.phase,m.funcName,m.message,m.passName);if(d.failedFunctions){for(let m of d.failedFunctions)if(o.add(m),i&&t&&t!==e){let g=t.getFunction(m);g&&e.addFunction(Vh(g))}}}if(n.phaseEnd("graphPasses",performance.now()-f),n.shouldSnapshot("afterGraphPasses")){let m=new cp;n.irDump("afterGraphPasses",m.printModule(e))}return h}_runPartitioning(e,t){let n=new oa;n.addPass(new i_(this.config.partition)),n.addPass(new a_({targets:this.config.partition.targets})),n.setTrace(t),t.phaseStart("partition");let s=performance.now();n.run(e),t.phaseEnd("partition",performance.now()-s)}_eachFunc(e,t,n,s,o,i,a){for(let u of e)if(!o.has(u.name))try{a(u)}catch(p){if(s.push(new Dn(t,u.name,p.message)),o.add(u.name),n.errorEvent(t,u.name,p.message),!i)break}}_lowerAll(e,t,n,s,o){t.phaseStart("lowering");let i=performance.now(),a=[];return this._eachFunc(e,"lowering",t,n,s,o,u=>{let p=performance.now(),c=Xh(u,this.config.target,this.context);this.config.target.isGPU&&this.config.target.isGPU()&&!(this.config.target.isWebGPU&&this.config.target.isWebGPU())&&(c.convInfo=vR(u)),t.functionEvent("lowering",u.name,{durationMs:performance.now()-p}),a.push(c),t.shouldSnapshot("afterLowering")&&t.irDump("afterLowering:"+u.name,qg(c))}),t.phaseEnd("lowering",performance.now()-i),a}_runTirPasses(e){let t=new Ug;for(let n of nM(this.config))t.addPass(n);if(t.setTrace(e.trace),this.config.verifyMode==="full"){let n=new bf;t.setVerifyHook(s=>n.verify(s))}t.run(e.primFuncs,{trace:e.trace,errors:e.errors,failed:e.failed,resilient:e.resilient})}_verifyGraph(e,t,n,s,o,i){if(i){for(let u of e){if(o.has(u.name))continue;let p=wf?wf(u):[];if(p.length>0){let c=p.map(h=>h.toString()).join("; ");s.push(new Dn("verification",u.name,c)),o.add(u.name),n.errorEvent("verification",u.name,c)}}return}let a=ix(e);if(a.length>0)throw new Error("Graph verification failed ("+t+"): "+a.map(u=>u.toString()).join("; "))}_verifyAll(e,t,n,s){let o=new bf;for(let i of e){if(n.has(i.name))continue;let a=o.verify(i);if(a.length>0){let u=a.join("; ");if(s)t.push(new Dn("verification",i.name,u)),n.add(i.name);else throw new Error("TensorIR verification failed for "+i.name+": "+u)}}}_lowerToLIR(e,t,n,s,o){t.phaseStart("lirLowering");let i=performance.now(),a=[];return this._eachFunc(e,"lirLowering",t,n,s,o,u=>{let p=performance.now(),c=bM(u,this.config.target);if(u.cublasInfo&&(c.cublasInfo=u.cublasInfo),u.gpuRegisterBlocked&&(c.gpuRegisterBlocked=!0),this.config.verifyMode==="full"){let h=TM(c);if(h.length>0)throw new Error("LIR verification failed: "+h.map(f=>f.toString()).join("; "))}t.functionEvent("lirLowering",u.name,{durationMs:performance.now()-p}),a.push(c)}),t.phaseEnd("lirLowering",performance.now()-i),a}_codegen(e,t,n,s,o){t.phaseStart("codegen");let i=performance.now(),a=new zu("compiled"),u=this.config.usePartition,p={matmulBackend:this.config.matmulBackend,context:this.context},c=new Map,h=l(d=>(c.has(d.name)||c.set(d.name,new Hs(d,p)),c.get(d.name)),"getBackend"),f=u?null:new Hs(this.config.target,p);return this._eachFunc(e,"codegen",t,n,s,o,d=>{let m=performance.now(),g;if(u){let _=d._partitionTarget,b=_?this.config.partition.targets.find(w=>w.name===_):this.config.target;g=h(b||this.config.target)}else g=f;let y=g.compile(d);a.addCompiledKernel(y),d.shapeParamMap&&d.shapeParamMap.size>0&&a.setShapeParamMap(d.name,d.shapeParamMap,d.bufferMap),t.codegenStats(d.name,{durationMs:performance.now()-m,sourceSize:y.source.length,targetName:y.target.name})}),t.phaseEnd("codegen",performance.now()-i),a}};l(SM,"compileGraph")});function Wz(r){return typeof r=="object"&&r!==null}function Hz(r){return Wz(r)&&"_impl"in r}function Yz(r){return(e,...t)=>{let n=ss();if(!n)throw new Error(`TRACING dispatch key active but no tracer is set for op '${r}'`);if(Kz.has(r)){let c=Nt.findOp(r);return Nt.redispatch(c,e,...t)}let s=[],o={},i=km(r),a=0,u=l(c=>c instanceof ft||Hz(c),"isTensor"),p=l(c=>{c instanceof ft||c.isSymbolic?s.push(c):s.push(n.captureConstant(c))},"pushTensor");for(let c of t)if(Array.isArray(c)&&c.length>0&&u(c[0]))for(let h of c)p(h);else u(c)?p(c):i&&(c!=null&&a<i.length&&(o[i[a]]=c),a++);return n.recordOp(r,s,o)}}function vM(){let r=Nt.listOps();for(let e of r){let t=Nt.findOp(e);if(!t)continue;let n=t.name,s=wr.fromUnboxed(Yz(n));t.entry.registerKernel(48,s)}}var Kz,NM=M(()=>{"use strict";Nn();nc();$i();ya();ga();vm();Kz=new Set(["scatter","repeat","tile","split","chunk","roll","flip","cumsum","sort","argsort","topk"]);l(Wz,"_isObject");l(Hz,"_hasImpl");l(Yz,"_tracingKernel");l(vM,"registerTracingDispatch")});function Zz(r){return r&&r.shape&&r.shape.length>=2&&Jz.has(r.dtype)}function AM(r,e,t=Zz){let n=r.graph.functions().next().value;if(!n)return r;let s=n.entryBlock,o=r.numUserInputs,i=r.capturedParams,a=[];for(let h=0;h<i.length;h++){let f=o+h,d=s.getArgument(f);d&&t(i[h],h,d)&&a.push({j:h,argIndex:f,arg:d,param:i[h]})}if(a.length===0)return r;for(let h of a){let f=e(h.param),d=h.arg.type,m=Re,g=new m("constant",[],[d],{value:f,tensor_type:d}),y=s.firstOp;y?s.insertBefore(g,y):s.pushOp(g),h.arg.replaceAllUsesWith(g.getResult(0))}let u=new Set(a.map(h=>h.argIndex));s.removeArguments(u),n.inputTypes=Object.freeze(n.inputTypes.filter((h,f)=>!u.has(f)));let p=new Set(a.map(h=>h.j)),c=i.filter((h,f)=>!p.has(f));return{...r,capturedParams:c}}var Jz,OM=M(()=>{"use strict";sr();Jz=new Set(["f16","f32","f64"]);l(Zz,"defaultWeightPredicate");l(AM,"foldWeightParams")});function vf(r,e,t){let n=t.get(e.id);if(n)return n;let s=r.scalarConstant(0,e.type.dtype).getResult(0);return r.broadcast(s,e.type.shape,[]).getResult(0)}var co,Nf=M(()=>{"use strict";l(vf,"gradOrZero");co=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 n=this._pending.get(e);n||(n=[],this._pending.set(e,n)),n.push(t),this._reduced.delete(e)}_treeReduce(e){let t=e;for(;t.length>1;){let n=[];for(let s=0;s<t.length;s+=2)s+1<t.length?n.push(this._builder.add(t[s],t[s+1]).getResult(0)):n.push(t[s]);t=n}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 n=this._treeReduce(t);return this._reduced.set(e,n),n}has(e){let t=this._pending.get(e);return!!t&&t.length>0}}});function ce(r,e){gx.set(r,e)}function Ia(r){return gx.get(r)||null}function h_(r){let e=gx.get(r);if(e)return e;if(yx.has(r))return null;throw new Error(`autodiff: op '${r}' is on the gradient path but has no VJP rule and is not a registered gradient barrier. Register one with registerVJPRule('${r}', ...) or registerGradientBarrier('${r}').`)}function _x(r,e){BM.set(r,e)}function d_(r){return BM.get(r)||null}function bx(r){yx.add(r)}function m_(r){return yx.has(r)}var gx,yx,BM,ls=M(()=>{"use strict";gx=new Map,yx=new Set;l(ce,"registerVJPRule");l(Ia,"getVJPRule");l(h_,"requireVJPRuleOrBarrier");BM=new Map;l(_x,"registerRegionVJP");l(d_,"getRegionVJP");l(bx,"registerGradientBarrier");l(m_,"isGradientBarrier")});var Po,wx=M(()=>{"use strict";Po=new Set(["scan","if"])});function Af(r){let e=new Set(r.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(r);let n=new Set,s=[],o=l(i=>{for(let a of i.ops()){for(let u of a.operands)e.has(u.id)||n.has(u.id)||u.definingOp&&u.definingOp.opName==="constant"||(n.add(u.id),s.push(u));for(let u of a.regions||[])for(let p of u.blocks)o(p)}},"scan");return o(r),s}function g_(r,e){let t=r.scalarConstant(0,e.type.dtype).getResult(0);return r.broadcast(t,e.type.shape,[]).getResult(0)}function RM(r,e,t){let n=e.type.shape,s=n.map((a,u)=>u===0?t:0),o=n.map((a,u)=>u===0?t+1:a),i=r.slice(e,s,o).getResult(0);return r.reshape(i,n.slice(1)).getResult(0)}function Xz(r,e,t){let n=t.slice(1),s=e.map(o=>r.reshape(o,[1,...n]).getResult(0));return s.length===1?r.reshape(s[0],t).getResult(0):r.concat(s,0).getResult(0)}function y_(r,e,t,n,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 n)u.set(_,b);for(let _ of a)n.has(_.id)&&p.set(_,n.get(_.id));let c=[],h=null;for(let _ of e.ops())_.opName==="yield"?h=_:c.push(_);let f=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=r._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(Po.has(_.opName)){for(let S of _.operands)f(S),p.set(S,u.get(S.id)??S);let T=_.clone(p);r.block.pushOp(T);for(let S=0;S<_.numResults;S++)u.set(_.getResult(S).id,T.getResult(S)),p.set(_.getResult(S),T.getResult(S));continue}let b=_.operands.map(f),w=r._buildOp(_.opName,b,_.results.map(T=>T.type),new Map(_.attributes),null);for(let T=0;T<_.numResults;T++)u.set(_.getResult(T).id,w.getResult(T)),p.set(_.getResult(T),w.getResult(T))}let d=h.operands.map(_=>u.get(_.id));if(o)return{forwardYields:d};let m=new co(r);for(let _=0;_<h.operands.length;_++)s[_]&&m.accumulate(h.operands[_].id,s[_]);for(let _=c.length-1;_>=0;_--){let b=c[_];if(b.opName==="constant")continue;let w=b.results.map(k=>m.get(k.id));if(w.every(k=>k===null))continue;if(Po.has(b.opName)){let k=d_(b.opName);k&&k(b,{accumulator:m,builder:r,materialize:f,needsGrad:Qz,scanCheckpoint:null});continue}let T=h_(b.opName);if(!T)continue;let S={builder:r,op:b,operands:b.operands.map(f),results:b.results.map(k=>u.get(k.id)),gradOutputs:w,attrs:b.attributes,full:l((k,A)=>r.broadcast(r.scalarConstant(k,A.dtype).getResult(0),A.shape,[]).getResult(0),"full")},x=T(S);if(x)for(let k=0;k<b.numOperands;k++)x[k]&&m.accumulate(b.getOperand(k).id,Tx(r,x[k],b.getOperand(k).type.shape))}let g=e.arguments.map(_=>m.get(_.id)),y=new Map;for(let _ of n.keys())y.set(_,m.get(_));return{forwardYields:d,gradArgs:g,gradFree:y}}function e3(r,e,t,n,s){let o=r.regions[0].blocks[0],i=r.regions[1].blocks[0],a=n(r.getOperand(0)),u=[];for(let y=0;y<r.numResults;y++)u.push(e.get(r.getResult(y).id));let p=Af(o),c=Af(i),h=new Map(p.map(y=>[y.id,n(y)])),f=new Map(c.map(y=>[y.id,n(y)])),{gradFree:d}=y_(t,o,[],h,u,!1,new Map,p),{gradFree:m}=y_(t,i,[],f,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=d.get(y),w=m.get(y);if(!b&&!w)continue;let T=g_(t,_),S=t.broadcast(a,_.type.shape,[]).getResult(0);e.accumulate(y,t.select(S,b??T,w??T).getResult(0))}}function t3(r,e){if(!r||e<=1)return null;if(r==="sqrt"||r===!0)return Math.max(1,Math.ceil(Math.sqrt(e)));if(typeof r=="number"&&r>=1){let t=Math.floor(r);return t>=e?null:t}return null}function n3(r,e,t,n,s,o=null){let i=r.regions[0].blocks[0],a=r.getAttr("num_carry"),u=r.getAttr("num_xs"),p=r.numResults-a,c=[];for(let v=0;v<u;v++)c.push(r.getOperand(v));let h=[];for(let v=0;v<a;v++)h.push(r.getOperand(u+v));let f=c[0].type.shape[0],d=Af(i),m=c.map(n),g=h.map(n),y=new Map(d.map(v=>[v.id,n(v)])),_=new Map,b=l(v=>m.map(O=>RM(t,O,v)),"sliceX"),w=l((v,O)=>y_(t,i,[...v,...O],y,null,!0,_,d).forwardYields.slice(0,a),"stepForward"),T=[];for(let v=0;v<p;v++)T.push(e.get(r.getResult(a+v).id));let S=[];for(let v=0;v<a;v++){let O=e.get(r.getResult(v).id);S.push(O??g_(t,g[v]))}let x=new Map,k=m.map(()=>new Array(f)),A=l((v,O,R)=>{let B=[...O,...R],E=T.map(j=>j===null?null:RM(t,j,v)),z=[...S,...E],{gradArgs:D,gradFree:J}=y_(t,i,B,y,z,!1,_,d);for(let j=0;j<u;j++)k[j][v]=D[j]??g_(t,O[j]);S=[];for(let j=0;j<a;j++)S.push(D[u+j]??g_(t,R[j]));for(let[j,U]of J){if(!U)continue;let P=x.get(j);x.set(j,P?t.add(P,U).getResult(0):U)}},"backwardStep"),N=t3(o,f);if(N){let v=Math.ceil(f/N),O=new Array(v),R=g;O[0]=R;for(let B=0;B<f;B++){R=w(b(B),R);let E=(B+1)/N;Number.isInteger(E)&&E<v&&(O[E]=R)}for(let B=v-1;B>=0;B--){let E=B*N,z=Math.min(E+N,f),D=[],J=[O[B]],j=O[B];for(let U=E;U<z;U++){let P=b(U);D.push(P),j=w(P,j),J.push(j)}for(let U=z-1;U>=E;U--)A(U,D[U-E],J[U-E])}}else{let v=g,O=[v],R=[];for(let B=0;B<f;B++){let E=b(B);R.push(E),v=w(E,v),O.push(v)}for(let B=f-1;B>=0;B--)A(B,R[B],O[B])}for(let v=0;v<u;v++)s.has(c[v].id)&&e.accumulate(c[v].id,Xz(t,k[v],m[v].type.shape));for(let v=0;v<a;v++)s.has(h[v].id)&&e.accumulate(h[v].id,S[v]);for(let[v,O]of x)O&&s.has(v)&&e.accumulate(v,O)}var Qz,MM=M(()=>{"use strict";Nf();ls();Of();wx();Qz={has:l(()=>!0,"has")};_x("scan",(r,e)=>n3(r,e.accumulator,e.builder,e.materialize,e.needsGrad,e.scanCheckpoint));_x("if",(r,e)=>e3(r,e.accumulator,e.builder,e.materialize,e.needsGrad));l(Af,"regionFreeVars");l(g_,"zeroLike");l(RM,"sliceStep");l(Xz,"stackSteps");l(y_,"diffBodyStep");l(e3,"buildCondBackward");l(t3,"resolveSegmentLength");l(n3,"buildScanBackward")});function Bf(r,{accumulator:e,builder:t,needsGrad:n,resolveValue:s,handleRegionOp:o=null}){for(let i=r.length-1;i>=0;i--){let a=r[i];if(a.opName==="return"||a.opName==="constant"||!a.results.some(g=>n.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=h_(a.opName);if(!c)continue;let h=new Array(a.numOperands);for(let g=0;g<a.numOperands;g++)h[g]=s(a.getOperand(g));let f=new Array(a.numResults);for(let g=0;g<a.numResults;g++)f[g]=s(a.getResult(g));let d=l((g,y)=>t.broadcast(t.scalarConstant(g,y.dtype).getResult(0),y.shape,[]).getResult(0),"full"),m=c({builder:t,op:a,operands:h,results:f,gradOutputs:p,attrs:a.attributes,full:d});if(m)for(let g=0;g<a.numOperands;g++){if(g>=m.length||!m[g])continue;let y=a.getOperand(g);n.has(y.id)&&e.accumulate(y.id,Tx(t,m[g],y.type.shape))}}}function IM(r){let e=[];for(let t of r.regions)t.blocks[0]&&e.push(...Af(t.blocks[0]));return e}function Tx(r,e,t){let n=e.type.shape;if(n.length===t.length&&n.every((u,p)=>u===t[p]))return e;let s=n.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&&n[s+u]!==1&&o.push(s+u);let i=e;if(o.length>0){let u=r.scalarConstant(0,e.type.dtype).getResult(0);i=r.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=r.reshape(i,t).getResult(0)),i}var r3,Rf,Of=M(()=>{"use strict";xs();es();Es();Nf();ls();MM();wx();r3=new Set(["neg","abs","sign","floor","ceil"]);l(Bf,"backpropOps");l(IM,"regionControlFlowFreeVars");l(Tx,"reduceGradToOperandShape");Rf=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 n=at.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,n),{savedValues:u,savedValueIndices:p}=this._identifySavedValues(n,a,i),c=o.map(x=>x.type),h=u.map(x=>x.type),f=[...c,...h],d=[];for(let x=0;x<i.length;x++)a.has(i[x].id)&&d.push(i[x].type);let m=new un(`backward_${e.name}`,f,d),g=new An(m),y=m.args,_=y.slice(0,c.length),b=y.slice(c.length),w=new Map;for(let x=0;x<u.length;x++)w.set(u[x].id,b[x]);for(let x=0;x<i.length;x++)p.has(i[x].id)&&w.set(i[x].id,b[p.get(i[x].id)]);let T=new co(g);for(let x=0;x<o.length;x++){let k=o[x];T.accumulate(k.id,_[x])}Bf(n,{accumulator:T,builder:g,needsGrad:a,resolveValue:l(x=>this._materialize(x,w,g),"resolveValue"),handleRegionOp:l(x=>{let k=d_(x.opName);return k?(k(x,{accumulator:T,builder:g,materialize:l(A=>this._materialize(A,w,g),"materialize"),needsGrad:a,scanCheckpoint:this._scanCheckpoint}),!0):!1},"handleRegionOp")});let S=[];for(let x=0;x<i.length;x++)a.has(i[x].id)&&S.push(vf(g,i[x],T));return g.returnOp(S),{backwardFunc:m,savedValues:u,gradInputIndices:this._getGradInputIndices(i,a)}}_materialize(e,t,n){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 f=u.getOperand(i.i);i.i++,f.definingOp&&!t.has(f.id)&&!s.has(f.id)&&(s.add(f.id),o.push({val:f,i:0}));continue}let p=new Array(u.numOperands);for(let f=0;f<u.numOperands;f++){let d=u.getOperand(f);p[f]=t.has(d.id)?t.get(d.id):d}let c=u.results.map(f=>f.type),h=n._buildOp(u.opName,p,c,new Map(u.attributes),null);for(let f=0;f<u.numResults;f++)t.set(u.getResult(f).id,h.getResult(f));s.delete(a.id),o.pop()}return t.has(e.id)?t.get(e.id):e}_computeGradReachability(e,t){let n=new Set,s=e.getReturnOp();for(let o of s.operands)n.add(o.id);for(let o=t.length-1;o>=0;o--){let i=t[o];if(!(i.opName==="return"||!i.results.some(u=>n.has(u.id)))){if(Po.has(i.opName)){for(let u=0;u<i.numOperands;u++)n.add(i.getOperand(u).id);for(let u of IM(i))n.add(u.id);continue}if(Ia(i.opName)&&!m_(i.opName))for(let u=0;u<i.numOperands;u++)n.add(i.getOperand(u).id)}}return n}_identifySavedValues(e,t,n){let s=[],o=new Map,i=new Set(n.map(h=>h.id));for(let h of e)if(!(h.opName==="return"||h.opName==="constant"||!Ia(h.opName)||!h.results.some(m=>t.has(m.id)))){for(let m=0;m<h.numOperands;m++){let g=h.getOperand(m);i.has(g.id)&&!o.has(g.id)&&(o.set(g.id,s.length),s.push(g))}for(let m=0;m<h.numResults;m++){let g=h.getResult(m);t.has(g.id)&&!o.has(g.id)&&this._shouldSaveResult(h)&&(o.set(g.id,s.length),s.push(g))}}let a=new Set(o.keys()),u=new Map(n.map(h=>[h.id,h])),p=new Set,c=l(h=>{if(a.has(h.id)||p.has(h.id))return;p.add(h.id);let f=h.definingOp;if(!f){i.has(h.id)&&!o.has(h.id)&&(o.set(h.id,s.length),s.push(u.get(h.id)));return}for(let d=0;d<f.numOperands;d++)c(f.getOperand(d))},"collect");for(let h of e)if(!(h.opName==="return"||h.opName==="constant")&&h.results.some(f=>t.has(f.id))){if(Po.has(h.opName)){for(let f of h.operands)c(f);for(let f of IM(h))c(f);continue}if(Ia(h.opName)){for(let f=0;f<h.numOperands;f++)c(h.getOperand(f));for(let f=0;f<h.numResults;f++)c(h.getResult(f))}}return{savedValues:s,savedValueIndices:o}}_shouldSaveResult(e){return this._rematPolicy?!this._rematPolicy.shouldRematerialize(e):!r3.has(e.opName)}_getGradInputIndices(e,t){let n=[];for(let s=0;s<e.length;s++)t.has(e[s].id)&&n.push(s);return n}_buildCheckpointed(e){let n=at.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,n),u=this._checkpointPolicy.segment(n,e);for(let O of u)for(let R of O.ops)if(Po.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=[],h=new Map,f=new Map;for(let O of n)for(let R=0;R<O.numResults;R++){let B=O.getResult(R);f.set(B.id,B)}let d=new Set(i.map(O=>O.id));for(let O of i)a.has(O.id)&&!p.has(O.id)&&(p.add(O.id),h.set(O.id,c.length),c.push(O));for(let O of u){for(let R of O.boundaryInputs)if(!d.has(R)&&!p.has(R)){p.add(R);let B=f.get(R)||null;B&&(h.set(R,c.length),c.push(B))}for(let R of O.boundaryOutputs)if(!p.has(R)){p.add(R);let B=f.get(R)||null;B&&(h.set(R,c.length),c.push(B))}}for(let O of o)p.has(O.id)||(p.add(O.id),h.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 un(`backward_${e.name}`,y,_),w=new An(b),T=b.args,S=T.slice(0,m.length),x=T.slice(m.length),k=new Map;for(let O=0;O<c.length;O++)k.set(c[O].id,x[O]);let A=new co(w);for(let O=0;O<o.length;O++)A.accumulate(o[O].id,S[O]);let N=new Map;for(let O of n)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 z=new Array(E.numOperands);for(let j=0;j<E.numOperands;j++){let U=E.getOperand(j),P=B.get(U.id)||k.get(U.id)||N.get(U.id);z[j]=P||U}let D=E.results.map(j=>j.type),J=w._buildOp(E.opName,z,D,new Map(E.attributes),null);for(let j=0;j<E.numResults;j++)B.set(E.getResult(j).id,J.getResult(j))}Bf(R.ops,{accumulator:A,builder:w,needsGrad:a,resolveValue:l(E=>B.get(E.id)||k.get(E.id)||N.get(E.id)||E,"resolveValue")})}let v=[];for(let O=0;O<i.length;O++)a.has(i[O].id)&&v.push(vf(w,i[O],A));return w.returnOp(v),{backwardFunc:b,savedValues:c,gradInputIndices:this._getGradInputIndices(i,a)}}}});var Ea,__=M(()=>{"use strict";Ea=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 n=0;n<e.numOperands;n++){let s=e.getOperand(n).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 Mf,xx=M(()=>{"use strict";xs();es();Es();Nf();ls();__();Of();Mf=class{static{l(this,"JointGraphBuilder")}constructor(e={}){this._rematPolicy=e.rematPolicy||new Ea(e.remat||{}),this._checkpointPolicy=e.checkpointPolicy||null}build(e){if(this._checkpointPolicy)return this._buildCheckpointed(e);let t=this._buildScaffold(e);return Bf(t.topoOrder,{accumulator:t.accumulator,builder:t.builder,needsGrad:t.needsGrad,resolveValue:l(n=>t.valueMap.get(n.id)||n,"resolveValue")}),this._finish(t)}_buildCheckpointed(e){let t=this._buildScaffold(e),n=this._checkpointPolicy.segment(t.topoOrder,e);for(let s=n.length-1;s>=0;s--){let o=n[s],i=new Map;for(let a of o.ops){let u=new Array(a.numOperands);for(let h=0;h<a.numOperands;h++){let f=a.getOperand(h);u[h]=i.get(f.id)||t.valueMap.get(f.id)||f}let p=a.results.map(h=>h.type),c=t.builder._buildOp(a.opName,u,p,new Map(a.attributes),null);for(let h=0;h<a.numResults;h++)i.set(a.getResult(h).id,c.getResult(h))}Bf(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 n=at.compute(e).topologicalOrder;this._assertNoRegionControlFlow(n);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 un(`joint_${e.name}`,u,p),h=new An(c),f=c.args,d=f.slice(0,e.inputTypes.length),m=f.slice(e.inputTypes.length),g=new Map;for(let w=0;w<i.length;w++)g.set(i[w].id,d[w]);for(let w of n){if(w.opName==="return")continue;let T=new Array(w.numOperands);for(let k=0;k<w.numOperands;k++){let A=w.getOperand(k);T[k]=g.get(A.id)||A}let S=w.results.map(k=>k.type),x=h._buildOp(w.opName,T,S,new Map(w.attributes),null);for(let k=0;k<w.numResults;k++)g.set(w.getResult(k).id,x.getResult(k))}let y=o.map(w=>g.get(w.id)),_=this._computeGradReachability(e,n),b=new co(h);for(let w=0;w<o.length;w++)b.accumulate(o[w].id,m[w]);return{topoOrder:n,forwardInputs:i,forwardOutputs:o,fwdOutputValues:y,valueMap:g,builder:h,needsGrad:_,accumulator:b,jointFunc:c}}_finish(e){let t=[];for(let n=0;n<e.forwardInputs.length;n++)t.push(vf(e.builder,e.forwardInputs[n],e.accumulator));return e.builder.returnOp([...e.fwdOutputValues,...t]),{jointFunc:e.jointFunc,numForwardOutputs:e.forwardOutputs.length,numGradInputs:t.length}}_computeGradReachability(e,t){let n=new Set,s=e.getReturnOp();for(let o of s.operands)n.add(o.id);for(let o=t.length-1;o>=0;o--){let i=t[o];if(!(i.opName==="return"||!i.results.some(u=>n.has(u.id)))&&Ia(i.opName)&&!m_(i.opName))for(let u=0;u<i.numOperands;u++)n.add(i.getOperand(u).id)}return n}_assertNoRegionControlFlow(e){for(let t of e)if(Po.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 EM=M(()=>{"use strict"});function LM(r,e){let t=r.gradOutputs[0],[n,s]=r.operands,o=r.full(0,n.type),i=r.builder.compare(n,s,e).getResult(0),a=r.builder.select(i,t,o).getResult(0),u=r.builder.select(i,o,t).getResult(0);return[a,u]}function CM(r){let e=r.gradOutputs[0],[t]=r.operands,n=r.full(0,e.type),s=r.builder.where(t,e,n).getResult(0),o=r.builder.where(t,n,e).getResult(0);return[null,s,o]}var DM=M(()=>{"use strict";ls();ce("add",r=>{let e=r.gradOutputs[0];return[e,e]});ce("sub",r=>{let e=r.gradOutputs[0],t=r.builder.neg(e).getResult(0);return[e,t]});l(LM,"_minMaxVJP");ce("maximum",r=>LM(r,"ge"));ce("minimum",r=>LM(r,"le"));ce("clamp",r=>{let e=r.gradOutputs[0],[t,n,s]=r.operands,o=r.full(0,n.type),i=r.builder.compare(n,t,"ge").getResult(0),a=r.builder.where(i,e,o).getResult(0),u=r.builder.compare(n,s,"le").getResult(0);return[null,r.builder.where(u,a,o).getResult(0),null]});l(CM,"_whereVJP");ce("where",CM);ce("select",CM);ce("mul",r=>{let e=r.gradOutputs[0],[t,n]=r.operands,s=r.builder.mul(e,n).getResult(0),o=r.builder.mul(e,t).getResult(0);return[s,o]});ce("div",r=>{let e=r.gradOutputs[0],[t,n]=r.operands,s=r.builder.div(e,n).getResult(0),o=r.builder.mul(n,n).getResult(0),i=r.builder.neg(t).getResult(0),a=r.builder.mul(e,i).getResult(0),u=r.builder.div(a,o).getResult(0);return[s,u]});ce("neg",r=>{let e=r.gradOutputs[0];return[r.builder.neg(e).getResult(0)]});ce("pow",r=>{let e=r.gradOutputs[0],[t,n]=r.operands,s=r.full(1,t.type),o=r.builder.sub(n,s).getResult(0),i=r.builder.pow(t,o).getResult(0),a=r.builder.mul(n,i).getResult(0),u=r.builder.mul(e,a).getResult(0),p=r.builder.pow(t,n).getResult(0),c=r.builder.log(t).getResult(0),h=r.builder.mul(p,c).getResult(0),f=r.builder.mul(e,h).getResult(0);return[u,f]})});function PM(r,e){let t=r.full(2/Math.sqrt(Math.PI),e.type),n=r.builder.mul(e,e).getResult(0),s=r.builder.neg(n).getResult(0),o=r.builder.exp(s).getResult(0);return r.builder.mul(t,o).getResult(0)}function $M(r,e){let t=r.builder,n=r.full(1,e.type),s=t.add(e,r.full(Yi,e.type)).getResult(0),o=t.div(n,s).getResult(0),i=t.log(s).getResult(0),a=t.sub(i,t.mul(r.full(.5,e.type),o).getResult(0)).getResult(0),u=t.mul(o,o).getResult(0),p=u;for(let c of Nc)a=t.add(a,t.mul(r.full(c,e.type),p).getResult(0)).getResult(0),p=t.mul(p,u).getResult(0);for(let c=0;c<Yi;c++){let h=t.add(e,r.full(c,e.type)).getResult(0);a=t.sub(a,t.div(n,h).getResult(0)).getResult(0)}return a}var FM=M(()=>{"use strict";ls();Ji();ce("exp",r=>{let e=r.gradOutputs[0],t=r.results[0];return[r.builder.mul(e,t).getResult(0)]});ce("log",r=>{let e=r.gradOutputs[0],[t]=r.operands;return[r.builder.div(e,t).getResult(0)]});ce("sqrt",r=>{let e=r.gradOutputs[0],t=r.results[0],n=r.full(2,t.type),s=r.builder.mul(n,t).getResult(0);return[r.builder.div(e,s).getResult(0)]});ce("tanh",r=>{let e=r.gradOutputs[0],t=r.results[0],n=r.builder.mul(t,t).getResult(0),s=r.builder.sub(r.full(1,t.type),n).getResult(0);return[r.builder.mul(e,s).getResult(0)]});ce("sigmoid",r=>{let e=r.gradOutputs[0],t=r.results[0],n=r.builder.sub(r.full(1,t.type),t).getResult(0),s=r.builder.mul(t,n).getResult(0);return[r.builder.mul(e,s).getResult(0)]});ce("relu",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.full(0,t.type),s=r.builder.compare(t,n,"gt").getResult(0);return[r.builder.select(s,e,n).getResult(0)]});ce("gelu",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.full(1.702,t.type),s=r.full(1,t.type),o=r.builder.mul(n,t).getResult(0),i=r.builder.sigmoid(o).getResult(0),a=r.builder.sub(s,i).getResult(0),u=r.builder.mul(o,a).getResult(0),p=r.builder.add(s,u).getResult(0),c=r.builder.mul(i,p).getResult(0);return[r.builder.mul(e,c).getResult(0)]});ce("silu",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.builder.sigmoid(t).getResult(0),s=r.builder.sub(r.full(1,t.type),n).getResult(0),o=r.builder.mul(t,s).getResult(0),i=r.builder.add(n,r.builder.mul(n,o).getResult(0)).getResult(0);return[r.builder.mul(e,i).getResult(0)]});ce("sin",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.builder.cos(t).getResult(0);return[r.builder.mul(e,n).getResult(0)]});ce("cos",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.builder.sin(t).getResult(0),s=r.builder.neg(n).getResult(0);return[r.builder.mul(e,s).getResult(0)]});ce("abs",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.builder.sign(t).getResult(0);return[r.builder.mul(e,n).getResult(0)]});l(PM,"_erfDerivIR");l($M,"_digammaIR");ce("erf",r=>{let e=r.gradOutputs[0],[t]=r.operands;return[r.builder.mul(e,PM(r,t)).getResult(0)]});ce("erfc",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.builder.neg(PM(r,t)).getResult(0);return[r.builder.mul(e,n).getResult(0)]});ce("lgamma",r=>{let e=r.gradOutputs[0],[t]=r.operands;return[r.builder.mul(e,$M(r,t)).getResult(0)]});ce("gamma",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.results[0],s=r.builder.mul(n,$M(r,t)).getResult(0);return[r.builder.mul(e,s).getResult(0)]});ce("log2",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.full(Math.LN2,t.type),s=r.builder.mul(t,n).getResult(0);return[r.builder.div(e,s).getResult(0)]});ce("log10",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.full(Math.LN10,t.type),s=r.builder.mul(t,n).getResult(0);return[r.builder.div(e,s).getResult(0)]});ce("exp2",r=>{let e=r.gradOutputs[0],t=r.results[0],n=r.full(Math.LN2,t.type),s=r.builder.mul(t,n).getResult(0);return[r.builder.mul(e,s).getResult(0)]});ce("square",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.full(2,t.type),s=r.builder.mul(n,t).getResult(0);return[r.builder.mul(e,s).getResult(0)]});ce("reciprocal",r=>{let e=r.gradOutputs[0],t=r.results[0],n=r.builder.mul(t,t).getResult(0),s=r.builder.neg(n).getResult(0);return[r.builder.mul(e,s).getResult(0)]});ce("rsqrt",r=>{let e=r.gradOutputs[0],t=r.results[0],n=r.builder.mul(r.builder.mul(t,t).getResult(0),t).getResult(0),s=r.full(-.5,t.type),o=r.builder.mul(s,n).getResult(0);return[r.builder.mul(e,o).getResult(0)]})});var zM=M(()=>{"use strict";ls();ce("dot",r=>{let e=r.gradOutputs[0],[t,n]=r.operands,s=r.op.getAttr("lhs_contracting"),o=r.op.getAttr("rhs_contracting"),i=r.op.getAttr("lhs_batch")||[],a=r.op.getAttr("rhs_batch")||[],u=t.type.rank,p=n.type.rank,c=e.type.rank,h=[];for(let _=0;_<u;_++)!s.includes(_)&&!i.includes(_)&&h.push(_);let f=[];for(let _=0;_<p;_++)!o.includes(_)&&!a.includes(_)&&f.push(_);let d=[];for(let _=i.length;_<i.length+f.length;_++)d.push(_+h.length);let m=[];for(let _=i.length;_<i.length+h.length;_++)m.push(_);let g=r.builder.dot(e,n,d,f,Array.from({length:i.length},(_,b)=>b),a).getResult(0),y=r.builder.dot(t,e,h,m,i,Array.from({length:i.length},(_,b)=>b)).getResult(0);return[g,y]});ce("matmul",r=>{let e=r.gradOutputs[0],[t,n]=r.operands,s=l(p=>{let c=Array.from({length:p},(h,f)=>f);return c[p-2]=p-1,c[p-1]=p-2,c},"swapLastTwo"),o=r.builder.transpose(n,s(n.type.rank)).getResult(0),i=r.builder.matmul(e,o).getResult(0),a=r.builder.transpose(t,s(t.type.rank)).getResult(0),u=r.builder.matmul(a,e).getResult(0);return[i,u]});ce("conv",r=>{let e=r.gradOutputs[0],[t,n]=r.operands,s=r.builder,o=r.op.getAttr("strides"),i=r.op.getAttr("padding"),a=r.op.getAttr("dilation")||o.map(()=>1),u=r.op.getAttr("groups")||1,p=r.op.getAttr("input_layout"),c=r.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 f=n.type.shape,d=f[2],m=f[3],[g,y]=i,_=s.reverse(s.transpose(n,[1,0,2,3]).getResult(0),[2,3]).getResult(0),b=[[d-1-g[0],d-1-g[1]],[m-1-y[0],m-1-y[1]]],w=s.conv(e,_,[1,1],b).getResult(0),T=s.transpose(t,[1,0,2,3]).getResult(0),S=s.transpose(e,[1,0,2,3]).getResult(0),x=s.conv(T,S,[1,1],[g,y]).getResult(0),k=s.transpose(x,[1,0,2,3]).getResult(0);return[w,k]})});var VM=M(()=>{"use strict";ls();ce("reduce",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.op.getAttr("reduce_type"),s=r.op.getAttr("dimensions"),o=t.type.shape,i=t.type.dtype,a=new Set(s),u=o.map((h,f)=>a.has(f)?1:h),p=o.map((h,f)=>f),c=r.builder.reshape(e,u).getResult(0);if(n==="sum")return[r.builder.broadcast(c,o,p).getResult(0),null];if(n==="mean"){let h=1;for(let y=0;y<o.length;y++)a.has(y)&&(h*=o[y]);let f=r.builder.broadcast(c,o,p).getResult(0),d=r.builder.scalarConstant(h,i).getResult(0),m=r.builder.broadcast(d,o,[]).getResult(0);return[r.builder.div(f,m).getResult(0),null]}if(n==="max"||n==="min"){let h=r.builder.reshape(r.results[0],u).getResult(0),f=r.builder.broadcast(h,o,p).getResult(0),d=r.builder.compare(t,f,"eq").getResult(0),m=r.builder.convert(d,i).getResult(0),g=r.builder.broadcast(c,o,p).getResult(0);return[r.builder.mul(g,m).getResult(0),null]}throw n==="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 '${n}' on the gradient path (would silently drop the gradient)`)})});var GM=M(()=>{"use strict";ls();ce("reshape",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=t.type.shape;return[r.builder.reshape(e,n).getResult(0)]});ce("reverse",r=>[r.builder.reverse(r.gradOutputs[0],r.op.getAttr("dimensions")).getResult(0)]);ce("transpose",r=>{let e=r.gradOutputs[0],t=r.op.getAttr("permutation"),n=new Array(t.length);for(let s=0;s<t.length;s++)n[t[s]]=s;return[r.builder.transpose(e,n).getResult(0)]});ce("broadcast_in_dim",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.op.getAttr("broadcast_dimensions"),s=t.type.shape,o=r.results[0].type.shape,i=t.type.dtype,a=[];for(let h=0;h<o.length;h++)if(!n.includes(h))a.push(h);else{let f=n.indexOf(h);s[f]===1&&o[h]!==1&&a.push(h)}if(a.length===0)return[e];let u=r.builder.scalarConstant(0,i).getResult(0),p=r.builder.reduce(e,u,a,"sum").getResult(0),c=p.type.shape;return c.length!==s.length||!c.every((h,f)=>h===s[f])?[r.builder.reshape(p,s).getResult(0)]:[p]});ce("slice",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.op.getAttr("starts"),s=t.type.shape,o=e.type.shape,i=r.op.getAttr("strides")||s.map(()=>1),a=t.type.dtype,u=[...n],p=new Array(s.length),c=new Array(s.length);for(let f=0;f<s.length;f++)c[f]=i[f]-1,p[f]=s[f]-n[f]-(o[f]-1)*i[f]-1;let h=r.builder.scalarConstant(0,a).getResult(0);return[r.builder.pad(e,h,u,p,c).getResult(0)]});ce("concat",r=>{let e=r.gradOutputs[0],t=r.op.getAttr("dimension"),n=r.operands,s=[],o=0;for(let i=0;i<n.length;i++){let a=n[i].type.shape,u=new Array(a.length).fill(0),p=[...e.type.shape];u[t]=o,p[t]=o+a[t],s.push(r.builder.slice(e,u,p).getResult(0)),o+=a[t]}return s});ce("gather",r=>{let e=r.gradOutputs[0],[t,n]=r.operands,s=r.builder.scalarConstant(0,t.type.dtype).getResult(0),o=r.builder.broadcast(s,t.type.shape,[]).getResult(0);return[r.builder.scatterAdd(o,n,e,{updateWindowDims:r.op.getAttr("offset_dims"),insertedWindowDims:r.op.getAttr("collapsed_slice_dims"),scatterDimsToOperandDims:r.op.getAttr("start_index_map"),indexVectorDim:r.op.getAttr("index_vector_dim")}).getResult(0),null]});ce("scatter",r=>{let e=r.gradOutputs[0],[t,n]=r.operands,s=r.op.getAttr("inserted_window_dims"),o=t.type.shape.map((a,u)=>s.includes(u)?1:a),i=r.builder.gather(e,n,{offsetDims:r.op.getAttr("update_window_dims"),collapsedSliceDims:s,startIndexMap:r.op.getAttr("scatter_dims_to_operand_dims"),indexVectorDim:r.op.getAttr("index_vector_dim"),sliceSizes:o}).getResult(0);return[e,null,i]});ce("pad",r=>{let e=r.gradOutputs[0],t=r.op.getAttr("low"),n=r.op.getAttr("high"),[s]=r.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[r.builder.slice(e,i,a).getResult(0),null]})});var jM=M(()=>{"use strict";ls();es();ce("softmax",r=>{let e=r.gradOutputs[0],t=r.results[0],n=r.op.getAttr("axis"),s=t.type.dtype,o=t.type.shape,i=r.builder.mul(e,t).getResult(0),a=r.builder.scalarConstant(0,s).getResult(0),u=r.builder.reduce(i,a,[n],"sum").getResult(0),p=To(o.length,n),c=r.builder.broadcast(u,o,p).getResult(0),h=r.builder.sub(e,c).getResult(0);return[r.builder.mul(t,h).getResult(0)]});ce("log_softmax",r=>{let e=r.gradOutputs[0],t=r.results[0],[n]=r.operands,s=r.op.getAttr("axis"),o=t.type.dtype,i=t.type.shape,a=r.builder.exp(t).getResult(0),u=r.builder.scalarConstant(0,o).getResult(0),p=r.builder.reduce(e,u,[s],"sum").getResult(0),c=To(i.length,s),h=r.builder.broadcast(p,i,c).getResult(0),f=r.builder.mul(a,h).getResult(0);return[r.builder.sub(e,f).getResult(0)]});ce("layer_norm",r=>{let e=r.gradOutputs[0],[t,n]=r.operands,s=r.op.getAttr("axis"),o=r.op.getAttr("epsilon"),i=t.type.dtype,a=t.type.shape,u=[s<0?a.length+s:s],p=a[u[0]],c=To(a.length,u[0]),h=r.builder.scalarConstant(0,i).getResult(0),f=r.builder.reduce(t,h,u,"mean").getResult(0),d=r.builder.broadcast(f,a,c).getResult(0),m=r.builder.sub(t,d).getResult(0),g=r.builder.mul(m,m).getResult(0),y=r.builder.reduce(g,h,u,"mean").getResult(0),_=r.full(o,y.type),b=r.builder.add(y,_).getResult(0),w=r.builder.rsqrt(b).getResult(0),T=r.builder.broadcast(w,a,c).getResult(0),S=[];for(let he=0;he<a.length;he++)u.includes(he)&&S.push(he);let x=r.builder.broadcast(n,a,S).getResult(0),k=r.builder.mul(e,x).getResult(0),A=r.full(p,t.type),N=r.builder.mul(A,k).getResult(0),v=r.builder.reduce(k,h,u,"sum").getResult(0),O=r.builder.broadcast(v,a,c).getResult(0),R=r.builder.mul(m,T).getResult(0),B=r.builder.mul(k,R).getResult(0),E=r.builder.reduce(B,h,u,"sum").getResult(0),z=r.builder.broadcast(E,a,c).getResult(0),D=r.builder.mul(R,z).getResult(0),J=r.builder.sub(N,r.builder.add(O,D).getResult(0)).getResult(0),j=r.builder.div(T,A).getResult(0),U=r.builder.mul(j,J).getResult(0),P=r.builder.reduce(r.builder.mul(e,R).getResult(0),h,c,"sum").getResult(0),V=r.builder.reduce(e,h,c,"sum").getResult(0);return[U,P,V]});ce("scaled_dot_product_attention",r=>{let e=r.gradOutputs[0],[t,n,s]=r.operands,o=r.op.getAttr("scale");if(r.op.getAttr("causal"))throw new Error("causal scaled_dot_product_attention VJP not supported");let i=r.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"),h=i.matmul(t,c(n)).getResult(0),f=i.mul(h,r.full(o,h.type)).getResult(0),d=i.softmax(f,u-1).getResult(0),m=i.matmul(c(d),e).getResult(0),g=i.matmul(e,c(s)).getResult(0),y=i.scalarConstant(0,a).getResult(0),_=i.mul(g,d).getResult(0),b=i.reduce(_,y,[u-1],"sum").getResult(0),w=[];for(let N=0;N<u-1;N++)w.push(N);let T=i.broadcast(b,d.type.shape,w).getResult(0),S=i.mul(d,i.sub(g,T).getResult(0)).getResult(0),x=i.mul(S,r.full(o,S.type)).getResult(0),k=i.matmul(x,n).getResult(0),A=i.matmul(c(x),t).getResult(0);return[k,A,m]});ce("pool2d",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.builder,s=r.op.getAttr("pool_type"),o=r.op.getAttr("kernel_size"),i=r.op.getAttr("strides"),a=r.op.getAttr("padding"),u=r.op.getAttr("layout")||"NCHW",p=a.every(k=>k[0]===0&&k[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[h,f,d,m]=e.type.shape,[g,y]=o,_=t.type.shape,b=l(k=>n.reshape(n.broadcast(k,[h,f,d,g,m,y],[0,1,2,4]).getResult(0),_).getResult(0),"upsample"),w=b(e);if(s==="avg"){let k=r.full(g*y,t.type);return[n.div(w,k).getResult(0)]}let T=b(r.results[0]),S=n.compare(t,T,"eq").getResult(0),x=r.full(0,t.type);return[n.select(S,w,x).getResult(0)]});ce("batch_norm",r=>{let e=r.gradOutputs[0],[t,n,,s,o]=r.operands,i=r.op.getAttr("axis"),a=r.op.getAttr("epsilon"),u=t.type.dtype,p=t.type.shape,c=r.builder,h=To(p.length,i),f=c.scalarConstant(0,u).getResult(0),d=r.full(a,o.type),m=c.rsqrt(c.add(o,d).getResult(0)).getResult(0),g=c.broadcast(m,p,[i]).getResult(0),y=c.broadcast(s,p,[i]).getResult(0),_=c.broadcast(n,p,[i]).getResult(0),b=c.sub(t,y).getResult(0),w=c.mul(b,g).getResult(0),T=c.mul(e,_).getResult(0),S=c.mul(T,g).getResult(0),x=c.reduce(c.mul(e,w).getResult(0),f,h,"sum").getResult(0),k=c.reduce(e,f,h,"sum").getResult(0),A=c.neg(c.reduce(S,f,h,"sum").getResult(0)).getResult(0),N=c.mul(c.mul(g,g).getResult(0),g).getResult(0),v=r.full(-.5,t.type),O=c.mul(c.mul(c.mul(T,b).getResult(0),N).getResult(0),v).getResult(0),R=c.reduce(O,f,h,"sum").getResult(0);return[S,x,k,A,R]});ce("elu",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.results[0],s=r.op.getAttr("alpha")??1,o=r.full(0,t.type),i=r.full(1,t.type),a=r.full(s,t.type),u=r.builder.compare(t,o,"gt").getResult(0),p=r.builder.add(n,a).getResult(0),c=r.builder.select(u,i,p).getResult(0);return[r.builder.mul(e,c).getResult(0)]});ce("leaky_relu",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.op.getAttr("negative_slope")??.01,s=r.full(0,t.type),o=r.full(1,t.type),i=r.full(n,t.type),a=r.builder.compare(t,s,"gt").getResult(0),u=r.builder.select(a,o,i).getResult(0);return[r.builder.mul(e,u).getResult(0)]});ce("celu",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.op.getAttr("alpha")??1,s=r.full(0,t.type),o=r.full(1,t.type),i=r.builder.compare(t,s,"gt").getResult(0),a=r.full(n,t.type),u=r.builder.div(t,a).getResult(0),p=r.builder.exp(u).getResult(0),c=r.builder.select(i,o,p).getResult(0);return[r.builder.mul(e,c).getResult(0)]});ce("selu",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=1.0507009873554805,s=1.6732632423543772,o=r.full(0,t.type),i=r.full(n,t.type),a=r.builder.compare(t,o,"gt").getResult(0),u=r.full(s,t.type),p=r.builder.exp(t).getResult(0),c=r.builder.mul(u,p).getResult(0),h=r.builder.select(a,i,r.builder.mul(i,c).getResult(0)).getResult(0);return[r.builder.mul(e,h).getResult(0)]});ce("hardswish",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.full(-3,t.type),s=r.full(3,t.type),o=r.full(0,t.type),i=r.full(1,t.type),a=r.full(2,t.type),u=r.full(6,t.type),p=r.builder.compare(t,n,"le").getResult(0),c=r.builder.compare(t,s,"ge").getResult(0),h=r.builder.add(r.builder.mul(a,t).getResult(0),s).getResult(0),f=r.builder.div(h,u).getResult(0),d=r.builder.select(p,o,r.builder.select(c,i,f).getResult(0)).getResult(0);return[r.builder.mul(e,d).getResult(0)]});ce("hardsigmoid",r=>{let e=r.gradOutputs[0],[t]=r.operands,n=r.full(-3,t.type),s=r.full(3,t.type),o=r.full(0,t.type),i=r.full(1/6,t.type),a=r.builder.compare(t,n,"le").getResult(0),u=r.builder.compare(t,s,"ge").getResult(0),p=r.builder.select(a,o,r.builder.select(u,o,i).getResult(0)).getResult(0);return[r.builder.mul(e,p).getResult(0)]});ce("embedding",r=>{let e=r.gradOutputs[0],[t,n]=r.operands,s=n.type.rank,o=r.full(0,t.type);return[r.builder.scatter(o,n,e,{updateWindowDims:[s],insertedWindowDims:[0],scatterDimsToOperandDims:[0],indexVectorDim:s}).getResult(0),null]})});var qM=M(()=>{"use strict";ls();bx("stop_gradient");ce("stop_gradient",()=>[null]);for(let r of["compare","logical_not","argmax","argmin","iota"])bx(r)});var UM=M(()=>{"use strict";ls();Nf();Of();xx();__();EM();DM();FM();zM();VM();GM();jM();qM()});var KM={};nt(KM,{compileWithBackward:()=>w_});function b_(r){return typeof r.then=="function"}function w_(r,e,t={}){let n=t.target??Us(),s=t.mode||"separate",o=t.rematPolicy||new Ea(t.remat||{}),i={target:n,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,h=null;function f(v){let O=x_((...B)=>r.forward(...B),v,{name:r.constructor.name||"compiled",dynamicShapes:a}),R=l(B=>{let E=B.graph.functions().next().value,z=s==="joint"?m(E,B,o):d(E,B,o);return z.shapeEnv=B.shapeEnv,z.outputSymShapes=B.outputSymShapes,z},"finish");return b_(O)?O.then(R):R(O)}l(f,"_compile");function d(v,O,R){let B=new Rf({rematPolicy:R}),{backwardFunc:E,savedValues:z,gradInputIndices:D}=B.build(v),J=v.getReturnOp(),j=[...J.operands],U=j.length,P=new Map(v.args.map((Se,Qe)=>[Se.id,Qe])),V=new Map(j.map((Se,Qe)=>[Se.id,Qe])),he=[],le=new Map;for(let Se of z)P.has(Se.id)||V.has(Se.id)||le.has(Se.id)||(le.set(Se.id,U+he.length),he.push(Se));he.length>0&&(J.erase(),new An(v).returnOp([...j,...he]),v.outputTypes=Object.freeze([...j.map(Se=>Se.type),...he.map(Se=>Se.type)]));let Ie=z.map(Se=>P.has(Se.id)?{kind:"arg",index:P.get(Se.id)}:V.has(Se.id)?{kind:"output",index:V.get(Se.id)}:{kind:"output",index:le.get(Se.id)}),G=new wn("forward");G.addFunction(v);let K=new Do(i).compile(G),ie=new wn("backward");ie.addFunction(E);let ze=new Do(i).compile(ie);return{mode:"separate",fwdResult:K,bwdResult:ze,forwardFunc:v,backwardFunc:E,savedValues:z,savedSources:Ie,numRealOutputs:U,gradInputIndices:D,capturedParams:O.capturedParams,numUserInputs:O.numUserInputs,outputTypes:O.outputTypes,shapeEnv:O.shapeEnv,outputSymShapes:O.outputSymShapes}}l(d,"_compileSeparate");function m(v,O,R){let B=new Mf({rematPolicy:R}),{jointFunc:E,numForwardOutputs:z,numGradInputs:D}=B.build(v),J=new wn("joint");return J.addFunction(E),{mode:"joint",result:new Do(i).compile(J),jointFunc:E,numForwardOutputs:z,numGradInputs:D,capturedParams:O.capturedParams,numUserInputs:O.numUserInputs,outputTypes:O.outputTypes,inputTypes:v.inputTypes,shapeEnv:O.shapeEnv,outputSymShapes:O.outputSymShapes}}l(m,"_compileJoint");function g(v,O){return v.outputSymShapes&&v.shapeEnv?v.shapeEnv.resolveSymbolicShape(v.outputSymShapes[O]):[...v.outputTypes[O].shape]}l(g,"_resolveOutputShape");function y(v,O,R){let B=v.module||v;return B.executionPlan?B.runPlanAsync(B.executionPlan,R):v.isAsync(O)?v.runAsync(O,...R):(v.run(O,...R),null)}l(y,"_runK");function _(v,O){let B=v.fwdResult.listKernels()[0],E=O.length>0?O[0].device:"cpu",z=O.map(G=>Je(G)),J=v.capturedParams.map(G=>Je(G)),j=v.forwardFunc.outputTypes,U=v.numRealOutputs,P=new Array(j.length),V=new Array(j.length);for(let G=0;G<j.length;G++){let K=G<U?g(v,G):[...j[G].shape],ie=j[G].dtype,ze=cn(K),Se=Dt(ie);P[G]=new Se(Math.max(ze,1)),V[G]=K}let he=[...z,...J,...P],le=l(()=>({results:U===1?tn(P[0],V[0],j[0].dtype,E):Array.from({length:U},(K,ie)=>tn(P[ie],V[ie],j[ie].dtype,E)),inputArrays:z,paramArrays:J,outputArrays:P,device:E}),"build"),Ie=y(v.fwdResult,B,he);return Ie?Ie.then(le):le()}l(_,"_executeSeparateForward");function b(v,O,R){let E=v.bwdResult.listKernels()[0],z=O.map(ie=>Je(ie)),D=v.savedValues,J=v.savedSources,j=[...R.inputArrays,...R.paramArrays],U=new Array(D.length);for(let ie=0;ie<D.length;ie++){let ze=J[ie];U[ie]=ze.kind==="arg"?j[ze.index]:R.outputArrays[ze.index]}let P=v.backwardFunc,V=P.outputTypes.length,he=new Array(V),le=new Array(V);for(let ie=0;ie<V;ie++){let ze=[...P.outputTypes[ie].shape],Se=P.outputTypes[ie].dtype,Qe=cn(ze),It=Dt(Se);he[ie]=new It(Math.max(Qe,1)),le[ie]=ze}let Ie=[...z,...U,...he],G=l(()=>he.map((ie,ze)=>tn(ie,le[ze],P.outputTypes[ze].dtype,R.device)),"build"),K=y(v.bwdResult,E,Ie);return K?K.then(G):G()}l(b,"_executeSeparateBackward");function w(v){for(let O=0;O<u.length;O++){let R=u[O];R.shapeEnv.bindInputShapes(v);let{passed:B}=R.shapeEnv.evaluateGuards();if(B)return R}return null}l(w,"_findCachedEntry");function T(v,O){if(c=v,v.mode==="joint")return k(v,O);let R=_(v,O);return b_(R)?R.then(B=>(p=B,B.results)):(p=R,R.results)}l(T,"_forwardWith");function S(v){let O=w(v);if(O)return T(O,v);let R=f(v);return b_(R)?R.then(B=>(u.push(B),B.shapeEnv.bindInputShapes(v),T(B,v))):(u.push(R),R.shapeEnv.bindInputShapes(v),T(R,v))}l(S,"_runForward");function x(...v){return h?h.then(()=>S(v)):S(v)}l(x,"compiledForward");function k(v,O){let B=v.result.listKernels()[0],E=O.length>0?O[0].device:"cpu",z=O.map(K=>Je(K)),J=v.capturedParams.map(K=>Je(K)),j=v.jointFunc,U=j.outputTypes.length,P=new Array(U),V=new Array(U);for(let K=0;K<U;K++){let ie=[...j.outputTypes[K].shape],ze=j.outputTypes[K].dtype,Se=cn(ie),Qe=Dt(ze);P[K]=new Qe(Math.max(Se,1)),V[K]=ie}let he=new Array(v.numForwardOutputs);for(let K=0;K<v.numForwardOutputs;K++){let ie=v.outputTypes[K],ze=cn(ie.shape),Se=Dt(ie.dtype);he[K]=new Se(Math.max(ze,1))}p={inputArrays:z,paramArrays:J,gradOutputArrays:he,outputArrays:P,outputShapes:V,device:E,compiled:v};let le=[...z,...J,...he,...P],Ie=l(()=>{let K=[];for(let ie=0;ie<v.numForwardOutputs;ie++)K.push(tn(P[ie],V[ie],j.outputTypes[ie].dtype,E));return K.length===1?K[0]:K},"build"),G=y(v.result,B,le);return G?G.then(Ie):Ie()}l(k,"_executeJointForward");let A=x;A.backward=function(...v){if(!c||!p)throw new Error("Must run forward before backward");return c.mode==="joint"?N(c,v,p):b(c,v,p)};function N(v,O,R){let{inputArrays:B,paramArrays:E,outputArrays:z,outputShapes:D,device:J}=R,j=O.map(K=>Je(K));for(let K=0;K<j.length;K++)R.gradOutputArrays[K].set(j[K]);let U=v.jointFunc,P=U.outputTypes.length,V=new Array(P),he=new Array(P);for(let K=0;K<P;K++){let ie=[...U.outputTypes[K].shape],ze=U.outputTypes[K].dtype,Se=cn(ie),Qe=Dt(ze);V[K]=new Qe(Math.max(Se,1)),he[K]=ie}let le=[...B,...E,...j,...V],Ie=l(()=>{let K=[];for(let ie=v.numForwardOutputs;ie<P;ie++)K.push(tn(V[ie],he[ie],U.outputTypes[ie].dtype,J));return K},"build"),G=y(v.result,v.result.listKernels()[0],le);return G?G.then(Ie):Ie()}if(l(N,"_executeJointBackward"),A.original=r,A.backwardGraph=()=>{if(u.length===0)return null;let v=u[0];return v.mode==="joint"?v.jointFunc:v.backwardFunc},A.forwardGraph=()=>{if(u.length===0)return null;let v=u[0];return v.mode==="joint"?v.jointFunc:v.forwardFunc},A.capturedParams=()=>u.length?u[0].capturedParams:[],e){let v=f(e);b_(v)?h=v.then(O=>(u.push(O),h=null,O)):u.push(v)}return A}var T_=M(()=>{"use strict";Sx();l_();Ks();Yo();Of();es();xx();__();UM();Kn();ts();jr();l(b_,"_isThenable");l(w_,"compileWithBackward")});function La(r){return typeof r.then=="function"}function s3(){WM||(vM(),WM=!0)}function o3(r,e){if(!r)return new Array(e.length).fill(null);let t=new Array(e.length);for(let n=0;n<e.length;n++){let s=r[n];if(s===!0){let o=new Set;for(let i=0;i<e[n].shape.length;i++)o.add(i);t[n]=o}else s instanceof Set?t[n]=s:t[n]=null}return t}function x_(r,e,t){s3();let n=t?.name||r.name||"traced",s=new Lg(n),o=o3(t?.dynamicShapes,e);for(let h=0;h<e.length;h++)s.createInput(e[h].shape,e[h].dtype,o[h]);let i=e.length,a=s._initGraph();function u(h){Array.isArray(h)?s.markOutputs(h):s.markOutput(h),s.deactivate();let f=s.getGraphModule(),d=f.functions().next().value;return{graph:f,capturedParams:[...s.capturedParams],numUserInputs:i,outputTypes:d.outputTypes,shapeEnv:s.shapeEnv,outputSymShapes:s.outputSymShapes}}l(u,"_finalize"),s.activate();let p=Pr.fromKey(48),c=LS(p,()=>r(...a));if(La(c))return c.then(h=>u(h),h=>{throw s.deactivate(),h});try{return u(c)}catch(h){throw s.deactivate(),h}}function HM(r,e,t){let n=x_(r,e,t);return La(n)?n.then(s=>s.graph):n.graph}function i3(r,e,t){let n=r.result.listKernels();if(n.length===0)throw new Error("No kernels compiled");let s=n[0],o=e.length>0?e[0].device:"cpu",i=new Array(e.length);for(let g=0;g<e.length;g++)i[g]=Je(e[g]);let a=r.capturedParams,u=new Array(a.length);for(let g=0;g<a.length;g++)u[g]=Je(a[g]);let p=r.outputTypes,c=r.outputSymShapes,h=new Array(p.length),f=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=cn(y),w=Dt(_);h[g]=new w(Math.max(b,1)),f[g]=[...y]}let d=new Array(i.length+u.length+h.length),m=0;for(let g=0;g<i.length;g++)d[m++]=new to(i[g],e[g].shape,e[g].dtype);for(let g=0;g<u.length;g++){let y=new to(u[g],a[g].shape,a[g].dtype),_=a[g]._impl;_&&(y.resident={key:_.storage.rawData,version:_.version}),d[m++]=y}for(let g=0;g<h.length;g++)d[m++]=new to(h[g],f[g],p[g].dtype);return{funcName:s,device:o,outputTypes:p,outputArrays:h,outputShapes:f,allArgs:d}}function kx(r,e,t,n){if(e.length===1)return tn(t[0],n[0],e[0].dtype,r);let s=new Array(e.length);for(let o=0;o<e.length;o++)s[o]=tn(t[o],n[o],e[o].dtype,r);return s}function a3(r,e,t){let{funcName:n,device:s,outputTypes:o,outputArrays:i,outputShapes:a,allArgs:u}=i3(r,e,t),p=r.result.module.executionPlan;return p?r.result.module.runPlanAsync(p,u,{resident:!0}).then(()=>kx(s,o,i,a)):r.result.isAsync(n)?r.result.runAsync(n,...u).then(()=>kx(s,o,i,a)):(r.result.run(n,...u),kx(s,o,i,a))}function u3(r,e,t={}){if(t?.backward)return w_(r,e,t);let n=t?.target??Us(),s={target:n,verify:!1,...t},o=t?.dynamic_shapes||null,i=t?.shapeBuckets||null,a=t?.foldWeights??t?.quantization?.foldWeights??!1,u=[];function p(w,T,S){if(!w||typeof w!="object"||w.repro)return w;try{w.repro={name:r.constructor?.name||"compiled",phase:S,target:n?.name,inputs:(T||[]).map(x=>({shape:x.shape,dtype:x.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 T=a?AM(w,Je):w;return{result:new Do(s).compile(T.graph),graph:T.graph,capturedParams:T.capturedParams,numUserInputs:T.numUserInputs,outputTypes:T.outputTypes,shapeEnv:T.shapeEnv,outputSymShapes:T.outputSymShapes}}l(c,"_finalize");function h(w,T){try{let S=x_((...x)=>r.forward(...x),w,{name:r.constructor.name||"compiled",dynamicShapes:T});return La(S)?S.then(c,x=>{throw p(x,w,"compile")}):c(S)}catch(S){throw p(S,w,"compile")}}l(h,"_compileWith");function f(w){return h(w,o)}l(f,"_compile");function d(w){return w.map((T,S)=>({shape:T,dtype:e[S].dtype}))}l(d,"_bucketInputs");function m(w){for(let T=0;T<u.length;T++){let S=u[T];S.shapeEnv.bindInputShapes(w);let{passed:x}=S.shapeEnv.evaluateGuards();if(x)return S}return null}l(m,"_findCachedEntry");function g(w,T){try{let S=a3(w,T,w.shapeEnv);return La(S)?S.then(void 0,x=>{throw p(x,T,"run")}):S}catch(S){throw p(S,T,"run")}}l(g,"_execute");function y(...w){let T=m(w);if(!T){let S=f(w);if(La(S))return S.then(x=>(u.push(x),x.shapeEnv.bindInputShapes(w),g(x,w)));T=S,u.push(T),T.shapeEnv.bindInputShapes(w)}return g(T,w)}l(y,"compiledForward");let _=null;if(e){let w=[];if(i)for(let T of i)w.push(h(d(T),null));if(w.push(f(e)),w.some(T=>La(T)))_=Promise.all(w).then(T=>{for(let S of T)u.push(S)});else for(let T of w)La(T)||u.push(T)}let b=y;return b.original=r,b.graph=w=>HM((...S)=>r.forward(...S),w||e,{name:r.constructor.name||"compiled",dynamicShapes:o}),b.source=()=>{if(u.length===0)return null;let w=u[0],T=w.result.listKernels();return T.length>0?w.result.getSource(T[0]):null},b.kernels=()=>u.length===0?[]:u[0].result.listKernels(),b.snippet=()=>{if(u.length===0)return null;let w=u[0],T=w.result.listKernels();return T.length>0?w.result.getSnippet(T[0]):null},b.result=()=>u.length>0?u[0].result:null,b._ready=_,b}var WM,Sx=M(()=>{"use strict";ya();NM();Nn();ub();l_();Ks();Kn();nm();ts();jr();OM();T_();WM=!1;l(La,"_isThenable");l(s3,"_ensureTracing");l(o3,"_normalizeDynamicShapes");l(x_,"_traceCore");l(HM,"trace");l(i3,"_prepareExecution");l(kx,"_wrapOutputs");l(a3,"executeCompiled");l(u3,"compile")});var QM={};nt(QM,{beginEagerCapture:()=>g3,endEagerCapture:()=>y3,replay:()=>_3,syncStream:()=>b3});var S_,g3,y3,_3,b3,XM=M(()=>{"use strict";S_=l(r=>{throw new Error(`mlfw: CUDA eager-graph (${r}) is not available in the browser`)},"unavailable"),g3=l(()=>S_("beginEagerCapture"),"beginEagerCapture"),y3=l(()=>S_("endEagerCapture"),"endEagerCapture"),_3=l(()=>S_("replay"),"replay"),b3=l(()=>S_("syncStream"),"syncStream")});var eI={};nt(eI,{clearCapturePins:()=>k3,deviceBufferDptr:()=>x3,deviceBufferForInput:()=>T3,pinResident:()=>S3,setEagerDeferred:()=>w3});var Ef,w3,T3,x3,S3,k3,tI=M(()=>{"use strict";Ef=l(r=>{throw new Error(`mlfw: CUDA resident memory (${r}) is not available in the browser`)},"unavailable"),w3=l(()=>Ef("setEagerDeferred"),"setEagerDeferred"),T3=l(()=>Ef("deviceBufferForInput"),"deviceBufferForInput"),x3=l(()=>Ef("deviceBufferDptr"),"deviceBufferDptr"),S3=l(()=>Ef("pinResident"),"pinResident"),k3=l(()=>Ef("clearCapturePins"),"clearCapturePins")});var rI={};nt(rI,{copyDeviceToHost:()=>v3,copyHostToDeviceAsync:()=>N3});var nI,v3,N3,sI=M(()=>{"use strict";nI=l(r=>{throw new Error(`mlfw: CUDA memory (${r}) is not available in the browser`)},"unavailable"),v3=l(()=>nI("copyDeviceToHost"),"copyDeviceToHost"),N3=l(()=>nI("copyHostToDeviceAsync"),"copyHostToDeviceAsync")});gs();Nn();Kn();vm();jn();Oo();ea();ar();jr();ts();function Xu(r,e){let t=xn(r),n=En.allocate(0,e,Au),s=new Bt(n,0,r,t,e,Au);return new We(s)}l(Xu,"_metaTensor");function aD(r,e,t){let n=Ou(e.shape,t.shape);if(!n)throw new Error(`Incompatible shapes: [${e.shape}] vs [${t.shape}]`);let s=Rd(e.dtype,t.dtype);return Xu(n,s)}l(aD,"_metaBinary");function uD(r,e){return Xu([...e.shape],e.dtype)}l(uD,"_metaUnary");function lD(r,e,t,n){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 Xu(n?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)?n&&i.push(1):i.push(s[u]);return Xu(i,e.dtype)}l(lD,"_metaReduction");function cD(r,e,t){let n=Bd(e.shape,t.shape);if(n===null)throw new Error("metaMatmul: unsupported shapes");return Xu(n,e.dtype)}l(cD,"metaMatmul");function pD(r,e){return Xu([...e.shape],e.dtype)}l(pD,"metaClone");function _0(r,e){return Object.fromEntries(r.map(t=>[t,e]))}l(_0,"mapKernel");var qA=Object.freeze({..._0(["add","sub","mul","div","pow","rem","maximum","minimum"],aD),..._0(["neg","exp","log","sqrt","rsqrt","abs","sin","cos","tanh","erf","erfc","lgamma","gamma","sigmoid","relu","gelu","silu","sign","floor","ceil"],uD),..._0(["sum","mean","max","min","prod"],lD),matmul:cD,clone:pD});jn();Oo();ea();ts();jr();function UA(r,e,t,n){let s=r.length,o=t<0?s+t:t,i=n<0?s+n:n,a=[...r],u=[...e];return a[o]=r[i],a[i]=r[o],u[o]=e[i],u[i]=e[o],{sizes:a,strides:u}}l(UA,"computeTranspose");function KA(r,e,t){let n=r.length,s=new Array(n),o=new Array(n);for(let i=0;i<n;i++){let a=t[i],u=a<0?n+a:a;s[i]=r[u],o[i]=e[u]}return{sizes:s,strides:o}}l(KA,"computePermute");function WA(r,e,t){let n=r.length,s=t.length,o=s-n,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(r[p]===1&&t[u]!==1)a[u]=t[u],i[u]=0;else if(r[p]===t[u]||t[u]===-1)a[u]=r[p],i[u]=e[p];else throw new Error(`Cannot expand size ${r[p]} to ${t[u]} at dim ${u}`)}return{sizes:a,strides:i}}l(WA,"computeExpand");function HA(r,e,t,n,s,o){let i=r.length,a=t<0?i+t:t,u=r[a],p=n??0,c=s??u,h=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 f=Math.max(0,Math.ceil((c-p)/h)),d=p*e[a],m=[...r],g=[...e];return m[a]=f,g[a]=e[a]*h,{sizes:m,strides:g,offsetDelta:d}}l(HA,"computeSlice");function YA(r,e,t){let n=r.length,s=t<0?n+1+t:t,o=[...r],i=[...e],a=s<n?r[s]*e[s]:1;return o.splice(s,0,1),i.splice(s,0,a),{sizes:o,strides:i}}l(YA,"computeUnsqueeze");function JA(r,e,t){if(t!=null){let o=r.length,i=t<0?o+t:t;if(r[i]!==1)return{sizes:[...r],strides:[...e]};let a=[...r],u=[...e];return a.splice(i,1),u.splice(i,1),{sizes:a,strides:u}}let n=[],s=[];for(let o=0;o<r.length;o++)r[o]!==1&&(n.push(r[o]),s.push(e[o]));return{sizes:n,strides:s}}l(JA,"computeSqueeze");function ZA(r,e,t,n,s){let o=r.length,i=t<0?o+t:t,a=[...r];a[i]=s;let u=n*e[i];return{sizes:a,strides:[...e],offsetDelta:u}}l(ZA,"computeNarrow");function QA(r,e,t,n){let s=r.length,o=t<0?s+t:t,i=n<0?r[o]+n:n,a=[],u=[];for(let c=0;c<s;c++)c!==o&&(a.push(r[c]),u.push(e[c]));let p=i*e[o];return{sizes:a,strides:u,offsetDelta:p}}l(QA,"computeSelect");function Ms(r,e,t,n){let s=new Bt(r._impl.storage,r._impl.storageOffset+(n||0),e,t,r._impl.dtype,r._impl.device),o=r._impl.autogradMeta;return o&&s.setAutogradMeta(o),new We(s)}l(Ms,"makeView");function fD(r,e){let t=tN(r.shape,r.strides,e);if(!t)throw new Error(`Cannot reshape tensor of shape [${r.shape}] to [${e}]`);if(!t.needsCopy)return Ms(r,t.sizes,t.strides,0);let n=r.isContiguous?r:e1(r);return Ms(n,t.sizes,t.strides,0)}l(fD,"reshapeKernel");function hD(r,e,t){let{sizes:n,strides:s}=UA(r.shape,r.strides,e,t);return Ms(r,n,s,0)}l(hD,"transposeKernel");function dD(r,e){let{sizes:t,strides:n}=KA(r.shape,r.strides,e);return Ms(r,t,n,0)}l(dD,"permuteKernel");function mD(r,e){let{sizes:t,strides:n}=WA(r.shape,r.strides,e);return Ms(r,t,n,0)}l(mD,"expandKernel");function gD(r,e,t){let n=r.shape,s=r.strides,o=[...e],i=new Array(e.length).fill(0);for(let a=0;a<t.length;a++){let u=t[a];i[u]=n[a]===1&&e[u]!==1?0:s[a]}return Ms(r,o,i,0)}l(gD,"broadcastInDimKernel");function yD(r,e,t,n,s){let{sizes:o,strides:i,offsetDelta:a}=HA(r.shape,r.strides,e,t,n,s);return Ms(r,o,i,a)}l(yD,"sliceKernel");function _D(r,e){let{sizes:t,strides:n}=YA(r.shape,r.strides,e);return Ms(r,t,n,0)}l(_D,"unsqueezeKernel");function bD(r,e){let{sizes:t,strides:n}=JA(r.shape,r.strides,e);return Ms(r,t,n,0)}l(bD,"squeezeKernel");function wD(r,e,t,n){let{sizes:s,strides:o,offsetDelta:i}=ZA(r.shape,r.strides,e,t,n);return Ms(r,s,o,i)}l(wD,"narrowKernel");function TD(r,e,t){let{sizes:n,strides:s,offsetDelta:o}=QA(r.shape,r.strides,e,t);return Ms(r,n,s,o)}l(TD,"selectKernel");var XA=null;function e1(r){let e=r._impl;return r.isContiguous&&e.storageOffset===0&&e.storage.rawData.length===r.numel?r:xD(r)}l(e1,"contiguousKernel");var t1=Object.freeze({reshape:l((r,e,t)=>fD(e,t),"reshape"),transpose:l((r,e,t,n)=>hD(e,t,n),"transpose"),permute:l((r,e,t)=>dD(e,t),"permute"),broadcast_in_dim:l((r,e,t,n)=>gD(e,t,n),"broadcast_in_dim"),expand:l((r,e,t)=>mD(e,t),"expand"),slice:l((r,e,t,n,s,o)=>yD(e,t,n,s,o),"slice"),unsqueeze:l((r,e,t)=>_D(e,t),"unsqueeze"),squeeze:l((r,e,t)=>bD(e,t),"squeeze"),narrow:l((r,e,t,n,s)=>wD(e,t,n,s),"narrow"),select:l((r,e,t,n)=>TD(e,t,n),"select"),contiguous:l((r,e)=>e1(e),"contiguous")});function xD(r){if(XA){let g=XA(r);if(g)return g}let e=r.shape,t=r.strides,n=r._impl.storage.data,s=r._impl.storageOffset,o=r.numel,i=xn(e),a=r._impl.dtype,u=En.allocate(o*Xi(a),a,r._impl.device),p=u.data,c=e.length,h=new Int32Array(c),f=s;for(let g=0;g<o;g++){p[g]=n[f];for(let y=c-1;y>=0&&(h[y]++,f+=t[y],!(h[y]<e[y]));y--)f-=h[y]*t[y],h[y]=0}let d=new Bt(u,0,e,i,r._impl.dtype,r._impl.device),m=r._impl.autogradMeta;return m&&d.setAutogradMeta(m),new We(d)}l(xD,"copyContiguous");be();_t();qr();jr();function SD(r,e,t,n){let s=$e(r.shape,{dtype:r.dtype}),o=ra(s,e,t,n),i=ra(s,e,t,Ht(n.shape,{dtype:r.dtype}));return qn(Iu(i,$e(i.shape,{dtype:i.dtype})),o,r)}l(SD,"scatterKernel");function kD(r,e){let t=r.shape,n=t.length;if(e.length<n)throw new Error("repeat: reps length must be >= tensor rank");let s=e.length-n,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 Cn(Lu(Cn(r,i),a),u)}l(kD,"repeatKernel");function vD(r,e){let t=r.shape.length,n=e.length<t?[...Array(t-e.length).fill(1),...e]:e;return Du(r,n)}l(vD,"tileKernel");function ND(r,e,t){let n=[],s=r.shape.length,o=t<0?s+t:t,i=0;for(let a of e)n.push(Un(r,o,i,a)),i+=a;return n}l(ND,"splitKernel");function AD(r,e,t){let n=r.shape.length,s=t<0?n+t:t,o=r.shape[s],i=Math.ceil(o/e);return Bo(r,i,s)}l(AD,"chunkKernel");function OD(r,e,t=0){let n=r.shape.length,s=vs(t,n),o=r.shape[s],i=(e%o+o)%o;return i===0?r:sa([Un(r,s,o-i,i),Un(r,s,0,o-i)],s)}l(OD,"rollKernel");function BD(r,e){let t=Array.isArray(e)?e:[e],n=r.shape.length,s=r;for(let o of t){let i=vs(o,n),a=s.shape[i],u=new Array(a);for(let p=0;p<a;p++)u[p]=a-1-p;s=li(s,i,Pt(u,{dtype:"i32"}))}return s}l(BD,"flipKernel");function RD(r,e=0){let t=r.shape.length,n=vs(e,t),s=r.shape[n],o=r;for(let i=1;i<s;i*=2){let a=[...o.shape];a[n]=i;let u=sa([$e(a,{dtype:o.dtype}),Un(o,n,0,s-i)],n);o=ae(o,u)}return o}l(RD,"cumsumKernel");function MD(r){let e=1;for(;e<r;)e<<=1;return e}l(MD,"nextPow2");function Mm(r,e,t){let n=r.shape.length,s=n-1,o=r.shape[s],i=MD(o),a=e?-1/0:1/0,u=r;if(i>o){let h=new Array(n).fill(0),f=new Array(n).fill(0);f[s]=i-o,u=na(r,h,f,a)}let p=new Array(n).fill(1);p[s]=i;let c=null;if(t){let h=new Array(i);for(let f=0;f<i;f++)h[f]=f;c=Cn(Pt(h,{dtype:"i32"}),p)}for(let h=2;h<=i;h<<=1)for(let f=h>>1;f>=1;f>>=1){let d=new Array(i),m=new Array(i);for(let T=0;T<i;T++){let S=T^f;d[T]=S;let x=(T&h)===0,k=e?!x:x;S>T?m[T]=k?1:0:m[T]=k?0:1}let g=Pt(d,{dtype:"i32"}),y=li(u,s,g),_=Ld(u,y),b=ai(u,y),w=Cn(Pt(m,{dtype:"f32"}),p);if(t){let T=li(c,s,g),S=qn(w,$c(_,u),$c(b,u));c=qn(S,c,T)}u=qn(w,_,b)}return i>o&&(u=Un(u,s,0,o),t&&(c=Un(c,s,0,o))),t?{values:u,indices:c}:u}l(Mm,"bitonicLastDim");function ID(r,e=-1,t=!1){let n=r.shape.length,s=vs(e,n);if(s===n-1)return Mm(r,t,!1);let o=gt(r,s,n-1),i=Mm(o,t,!1);return gt(i,s,n-1)}l(ID,"sortKernel");function i1(r,e,t){let n=r.shape.length,s=vs(e,n);if(s===n-1)return Mm(r,t,!0);let o=gt(r,s,n-1),i=Mm(o,t,!0);return{values:gt(i.values,s,n-1),indices:gt(i.indices,s,n-1)}}l(i1,"sortWithIndices");function ED(r,e=-1,t=!1){return i1(r,e,t).indices}l(ED,"argsortKernel");function LD(r,e,t=-1,n=!0){let s=r.shape.length,o=vs(t,s),{values:i,indices:a}=i1(r,o,n);return[Un(i,o,0,e),Un(a,o,0,e)]}l(LD,"topkKernel");var a1=Object.freeze({scatter:l((r,e,t,n,s)=>SD(e,t,n,s),"scatter"),repeat:l((r,e,t)=>kD(e,t),"repeat"),tile:l((r,e,t)=>vD(e,t),"tile"),split:l((r,e,t,n)=>ND(e,t,n),"split"),chunk:l((r,e,t,n)=>AD(e,t,n),"chunk"),roll:l((r,e,t,n)=>OD(e,t,n),"roll"),flip:l((r,e,t)=>BD(e,t),"flip"),cumsum:l((r,e,t)=>RD(e,t),"cumsum"),sort:l((r,e,t,n)=>ID(e,t,n),"sort"),argsort:l((r,e,t,n)=>ED(e,t,n),"argsort"),topk:l((r,e,t,n,s)=>LD(e,t,n,s),"topk")});var CD=3,u1=!1,DD=[0,1,2,3,5];function l1(){if(u1)return;u1=!0;let r=new Xt("mlc","DEF");for(let t of LA())r.def(t);let e=new Xt("mlc","IMPL");for(let[t,n]of Object.entries(qA))e.impl(t,CD,n);for(let[t,n]of Object.entries({...t1,...a1}))for(let s of DD)e.impl(t,s,n);y0()}l(l1,"registerNativeOps");gs();Nn();gs();var PD=["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"],c1=!1;function Im(){if(c1)return;c1=!0;let r=new Xt("mlc","DEF");for(let e of PD)r.def(e)}l(Im,"ensureLinalgSchemas");qr();Kn();ar();var $D=new Set(["cpu","wasm"]);function w0(r){let e=new Float64Array(r.length);for(let t=0;t<r.length;t++)e[t]=Number(r[t]);return e}l(w0,"float64From");function p1(r){if(!$D.has(r.device.type))throw new Error(`linalg/ml: host-readable device required (cpu or wasm), got '${r.device.type}'`)}l(p1,"requireHost");function Ue(r){if(r.ndim!==2)throw new Error(`linalg/ml: expected a 2-D matrix, got ${r.ndim}-D`);p1(r);let[e,t]=r.shape;return{data:w0(Je(r)),rows:e,cols:t}}l(Ue,"hostMatrix");function Rt(r){if(p1(r),r.ndim===1)return{data:w0(Je(r)),rows:r.shape[0],cols:1,wasVector:!0};if(r.ndim===2)return{data:w0(Je(r)),rows:r.shape[0],cols:r.shape[1],wasVector:!1};throw new Error(`linalg/ml: expected a 1-D or 2-D right-hand side, got ${r.ndim}-D`)}l(Rt,"hostColumns");function Q(r,e,t,n=ks){return Pt(r,{shape:e,dtype:t,device:n})}l(Q,"toHostTensor");qr();function f1(r,e){let t=Float64Array.from(r),n=new Int32Array(e);for(let o=0;o<e;o++)n[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 h=t[o*e+c];t[o*e+c]=t[i*e+c],t[i*e+c]=h}let p=n[o];n[o]=n[i],n[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 h=o+1;h<e;h++)t[p*e+h]-=c*t[o*e+h]}}return{lu:t,piv:n,sign:s}}l(f1,"luFactor");function FD(r,e,t,n,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]=n[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-=r[i*t+p]*o[p*s+a];o[i*s+a]=u}for(let i=t-1;i>=0;i--){let a=r[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-=r[i*t+c]*o[c*s+u];o[i*s+u]=p/a}}return o}l(FD,"luSolve");function T0(r,e,t,n){let{lu:s,piv:o}=f1(r,e);return FD(s,o,e,t,n)}l(T0,"solveHost");function h1(r,e){let t;try{t=f1(r,e)}catch{return 0}let n=t.sign;for(let s=0;s<e;s++)n*=t.lu[s*e+s];return n}l(h1,"detHost");function d1(r,e){let t=new Float64Array(e*e);for(let n=0;n<e;n++)for(let s=0;s<=n;s++){let o=r[n*e+s];for(let i=0;i<s;i++)o-=t[n*e+i]*t[s*e+i];if(n===s){if(o<=0)throw new Error("linalg.cholesky: matrix is not positive definite");t[n*e+s]=Math.sqrt(o)}else t[n*e+s]=o/t[s*e+s]}return t}l(d1,"choleskyHost");function ep(r,e,t){let n=t?.tol??1e-12,s=t?.maxSweeps??100,o=Float64Array.from(r),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 h=0;for(let f=0;f<e;f++)for(let d=f+1;d<e;d++)h+=o[f*e+d]*o[f*e+d];if(Math.sqrt(h)<n)break;for(let f=0;f<e;f++)for(let d=f+1;d<e;d++){let m=o[f*e+d];if(Math.abs(m)<1e-300)continue;let g=(o[d*e+d]-o[f*e+f])/(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 T=o[w*e+f],S=o[w*e+d];o[w*e+f]=_*T-b*S,o[w*e+d]=b*T+_*S}for(let w=0;w<e;w++){let T=o[f*e+w],S=o[d*e+w];o[f*e+w]=_*T-b*S,o[d*e+w]=b*T+_*S}for(let w=0;w<e;w++){let T=i[w*e+f],S=i[w*e+d];i[w*e+f]=_*T-b*S,i[w*e+d]=b*T+_*S}}}let a=Array.from({length:e},(c,h)=>h).sort((c,h)=>o[c*e+c]-o[h*e+h]),u=new Float64Array(e),p=new Float64Array(e*e);for(let c=0;c<e;c++){let h=a[c];u[c]=o[h*e+h];for(let f=0;f<e;f++)p[f*e+c]=i[f*e+h]}return{values:u,vectors:p}}l(ep,"eighHost");function GD(r,e,t,n){if(n){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+=r[p*t+i]*r[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+=r[o*t+u]*r[i*t+u];s[o*e+i]=a,s[i*e+o]=a}return s}l(GD,"gram");function jD(r,e,t,n,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+=r[i*t+p]*n[a*t+p];o[i*s+a]=u}return o}l(jD,"matmulRows");function da(r,e,t,n,s=GD,o=jD){let i=n?.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:h,vectors:f}=ep(s(r,e,t,!0),t,n),d=new Float64Array(a*t);for(let g=0;g<a;g++){let y=t-1-g;p[g]=Math.sqrt(Math.max(h[y],0));for(let _=0;_<t;_++){let b=f[_*t+y];c[_*a+g]=b,d[g*t+_]=b}}let m=o(r,e,t,d,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:h,vectors:f}=ep(s(r,e,t,!1),e,n),d=new Float64Array(a*e);for(let y=0;y<a;y++){let _=e-1-y;p[y]=Math.sqrt(Math.max(h[_],0));for(let b=0;b<e;b++){let w=f[b*e+_];u[b*a+y]=w,d[y*e+b]=w}}let m=new Float64Array(t*e);for(let y=0;y<e;y++)for(let _=0;_<t;_++)m[_*e+y]=r[y*t+_];let g=o(m,t,e,d,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(da,"svdHost");function g1(r,e,t,n,s,o){let i=o?.rcond??1e-12,{U:a,S:u,V:p,k:c}=da(r,e,t,o),h=i*(u.length?u[0]:0),f=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]*n[b*s+y];f[m*s+y]=g>h?_/g:0}}let d=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+_]*f[_*s+g];d[m*s+g]=y}return d}l(g1,"lstsqHost");function y1(r,e,t,n){let s=n?.rcond??1e-12,{U:o,S:i,V:a,k:u}=da(r,e,t,n),p=s*(i.length?i[0]:0),c=new Float64Array(t*e);for(let h=0;h<t;h++)for(let f=0;f<e;f++){let d=0;for(let m=0;m<u;m++){let g=i[m];g>p&&(d+=a[h*u+m]*(1/g)*o[f*u+m])}c[h*e+f]=d}return c}l(y1,"pinvHost");function _1(r,e,t){let n=new Float64Array(t);for(let i=0;i<t;i++){let a=0;for(let u=0;u<e;u++)a+=r[u*t+i];n[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+=(r[c*t+i]-n[i])*(r[c*t+a]-n[a]);let p=u/s;o[i*t+a]=p,o[a*t+i]=p}return o}l(_1,"covHost");function tp(r,e){let t=Ue(r);if(t.rows!==t.cols)throw new Error(`linalg.${e}: matrix must be square`);return t}l(tp,"squareMatrix");function b1(r,e){let{data:t,rows:n}=tp(e,"cholesky");return Q(d1(t,n),[n,n],e.dtype,e.device)}l(b1,"cpuCholesky");function w1(r,e,t){let{data:n,rows:s}=tp(e,"solve"),o=Rt(t);if(o.rows!==s)throw new Error("linalg.solve: right-hand side rows must match matrix");let i=T0(n,s,o.data,o.cols);return Q(i,o.wasVector?[s]:[s,o.cols],e.dtype,e.device)}l(w1,"cpuSolve");function T1(r,e,t){let{data:n,rows:s,cols:o}=Ue(e),i=Rt(t);if(i.rows!==s)throw new Error("linalg.lstsq: right-hand side rows must match matrix");let a=g1(n,s,o,i.data,i.cols);return Q(a,i.wasVector?[o]:[o,i.cols],e.dtype,e.device)}l(T1,"cpuLstsq");function x1(r,e){let{data:t,rows:n}=tp(e,"inv"),s=new Float64Array(n*n);for(let i=0;i<n;i++)s[i*n+i]=1;let o=T0(t,n,s,n);return Q(o,[n,n],e.dtype,e.device)}l(x1,"cpuInv");function S1(r,e){let{data:t,rows:n,cols:s}=Ue(e);return Q(y1(t,n,s),[s,n],e.dtype,e.device)}l(S1,"cpuPinv");function k1(r,e){let{data:t,rows:n}=tp(e,"det");return Dc(h1(t,n),{dtype:e.dtype})}l(k1,"cpuDet");function v1(r,e){let{data:t,rows:n,cols:s}=Ue(e);return Q(_1(t,n,s),[s,s],e.dtype,e.device)}l(v1,"cpuCov");function N1(r,e){let{data:t,rows:n}=tp(e,"eigh"),{values:s,vectors:o}=ep(t,n);return[Q(s,[n],e.dtype,e.device),Q(o,[n,n],e.dtype,e.device)]}l(N1,"cpuEigh");function Em(r,e){let{data:t,rows:n,cols:s}=Ue(e),{U:o,S:i,V:a,k:u}=da(t,n,s);return[Q(o,[n,u],e.dtype,e.device),Q(i,[u],e.dtype,e.device),Q(a,[s,u],e.dtype,e.device)]}l(Em,"cpuSvd");var qD={svd:Em,eigh:N1,cholesky:b1,inv:x1,pinv:S1,det:k1,cov:v1,solve:w1,lstsq:T1},UD=[0,2],A1=!1;function O1(){if(A1)return;A1=!0,Im();let r=new Xt("mlc","IMPL");for(let[e,t]of Object.entries(qD))for(let n of UD)r.impl(e,n,t)}l(O1,"registerCpuLinalg");gs();Nn();gs();var KD=["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"],B1=!1;function Lm(){if(B1)return;B1=!0;let r=new Xt("mlc","DEF");for(let e of KD)r.def(e)}l(Lm,"ensureMlSchemas");function cr(r){let e=(r??2654435769)>>>0;return l(function(){e|=0,e=e+1831565813|0;let n=Math.imul(e^e>>>15,1|e);return n=n+Math.imul(n^n>>>7,61|n)^n,((n^n>>>14)>>>0)/4294967296},"next")}l(cr,"makeRng");function el(r,e){return Math.floor(r()*e)}l(el,"randInt");function S0(r,e){let t=Array.from({length:r},(n,s)=>s);for(let n=r-1;n>0;n--){let s=el(e,n+1),o=t[n];t[n]=t[s],t[s]=o}return t}l(S0,"shuffledIndices");function k0(r,e,t,n,s){let o=0;for(let i=0;i<s;i++){let a=r[e*s+i]-t[n*s+i];o+=a*a}return o}l(k0,"sqDist");function v0(r,e,t,n,s){let o=new Float64Array(n*t),i=el(s,e);for(let u=0;u<t;u++)o[u]=r[i*t+u];let a=new Float64Array(e).fill(1/0);for(let u=1;u<n;u++){let p=0;for(let f=0;f<e;f++){let d=k0(r,f,o,u-1,t);d<a[f]&&(a[f]=d),p+=a[f]}let c=s()*p,h=e-1;for(let f=0;f<e;f++)if(c-=a[f],c<=0){h=f;break}for(let f=0;f<t;f++)o[u*t+f]=r[h*t+f]}return o}l(v0,"kmeansPlusPlus");function R1(r,e,t,n,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 h=k0(r,a,n,c,t);h<p&&(p=h,u=c)}o[a]=u,i+=p}return i}l(R1,"assign");function N0(r,e,t,n,s,o){let i=new Float64Array(n*t),a=new Int32Array(n);for(let p=0;p<e;p++){let c=s[p];a[c]++;for(let h=0;h<t;h++)i[c*t+h]+=r[p*t+h]}let u=!1;for(let p=0;p<n;p++)if(a[p]!==0)for(let c=0;c<t;c++){let h=i[p*t+c]/a[p];h!==o[p*t+c]&&(u=!0),o[p*t+c]=h}return u}l(N0,"updateCenters");function WD(r,e,t,n,s,o){let i=v0(r,e,t,n,o),a=new Int32Array(e),u=1/0;for(let p=0;p<s&&(u=R1(r,e,t,i,n,a),!!N0(r,e,t,n,a,i));p++);return u=R1(r,e,t,i,n,a),{centers:i,labels:a,inertia:u}}l(WD,"lloyd");function Cm(r,e,t,n,s,o){let{data:i,rows:a,cols:u}=Ue(e),p=null;for(let h=0;h<s;h++){let f=cr(o+h*2654435769),d=WD(i,a,u,t,n,f);(!p||d.inertia<p.inertia)&&(p=d)}let c=new Float64Array(a);for(let h=0;h<a;h++)c[h]=p.labels[h];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(Cm,"cpuKmeans");function Dm(r,e,t){let{data:n,rows:s,cols:o}=Ue(e),i=Ue(t),a=new Float64Array(s);for(let u=0;u<s;u++){let p=0,c=1/0;for(let h=0;h<i.rows;h++){let f=k0(n,u,i.data,h,o);f<c&&(c=f,p=h)}a[u]=p}return Q(a,[s],e.dtype,e.device)}l(Dm,"cpuKmeansPredict");function A0(r,e,t,n){for(let s=0;s<t;s++)e[s]=s;for(let s=0;s<n;s++){let o=s;for(let a=s+1;a<t;a++)r[e[a]]<r[e[o]]&&(o=a);let i=e[s];e[s]=e[o],e[o]=i}}l(A0,"topKSelect");function O0(r,e,t,n){if(n){let o=new Map,i=r[e[0]],a=0;for(let u=0;u<t;u++){let p=r[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+=r[e[o]];return s/t}l(O0,"knnVote");function Pm(r,e,t,n,s,o){let i=Ue(e),a=Ue(n),u=Rt(t),p=i.cols,c=Math.min(s,i.rows),h=new Float64Array(a.rows),f=new Float64Array(i.rows),d=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}f[g]=y}A0(f,d,i.rows,c),h[m]=O0(u.data,d,c,o)}return Q(h,[a.rows],n.dtype,n.device)}l(Pm,"cpuKnnPredict");be();qr();function ns(r,e){let t=Pt(Int32Array.from(e),{shape:[e.length],dtype:"i32",device:r.device});return ot("index_select",r,t,0)}l(ns,"takeRows");function tl(r){return Ue(r)}l(tl,"matrixOf");function zt(r){let e=Rt(r);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(zt,"vectorOf");function np(r,e,t,n){return Q(r,[e,t],n??"f32")}l(np,"matrix");function B0(r,e,t){return Q(r,[e],t??"f32")}l(B0,"vector");function rs(r,e){let t=[],n=new Map,s=new Int32Array(e);for(let o=0;o<e;o++){let i=r[o],a=n.get(i);a===void 0&&(a=t.length,n.set(i,a),t.push(i)),s[o]=a}return{y:s,classes:t}}l(rs,"encodeLabels");var HD=1e-9;function M1(r,e,t){let{data:n,rows:s,cols:o}=Ue(e),i=Rt(t),{y:a,classes:u}=rs(i.data,i.rows),p=u.length,c=new Float64Array(p*o),h=new Float64Array(p*o),f=new Float64Array(p),d=new Int32Array(p);for(let _=0;_<s;_++){let b=a[_];d[b]++;for(let w=0;w<o;w++)c[b*o+w]+=n[_*o+w]}for(let _=0;_<p;_++){for(let b=0;b<o;b++)c[_*o+b]/=d[_];f[_]=d[_]/s}for(let _=0;_<s;_++){let b=a[_];for(let w=0;w<o;w++){let T=n[_*o+w]-c[b*o+w];h[b*o+w]+=T*T}}let m=0;for(let _=0;_<p;_++)for(let b=0;b<o;b++)h[_*o+b]/=d[_],h[_*o+b]>m&&(m=h[_*o+b]);let g=HD*m;for(let _=0;_<p*o;_++)h[_]+=g;let y=new Float64Array(p);for(let _=0;_<p;_++)y[_]=u[_];return[Q(c,[p,o],e.dtype,e.device),Q(h,[p,o],e.dtype,e.device),Q(f,[p],e.dtype,e.device),Q(y,[p],e.dtype,e.device)]}l(M1,"cpuGaussianNbFit");function I1(r,e,t,n,s,o){let{data:i,rows:a,cols:u}=Ue(e),p=Ue(t),c=Ue(n),h=Rt(s),f=Rt(o),d=p.rows,m=new Float64Array(a);for(let g=0;g<a;g++){let y=0,_=-1/0;for(let b=0;b<d;b++){let w=Math.log(h.data[b]);for(let T=0;T<u;T++){let S=c.data[b*u+T],x=i[g*u+T]-p.data[b*u+T];w+=-.5*(Math.log(2*Math.PI*S)+x*x/S)}w>_&&(_=w,y=b)}m[g]=f.data[y]}return Q(m,[a],e.dtype,e.device)}l(I1,"cpuGaussianNbPredict");function YD(r,e){return r>e?r-e:r<-e?r+e:0}l(YD,"softThreshold");function R0(r,e,t,n,s){let o=new Float64Array(t),i=0;if(s){for(let c=0;c<t;c++){let h=0;for(let f=0;f<e;f++)h+=r[f*t+c];o[c]=h/e}for(let c=0;c<e;c++)i+=n[c];i/=e}let a=new Float64Array(e*t);for(let c=0;c<e;c++)for(let h=0;h<t;h++)a[c*t+h]=r[c*t+h]-o[h];let u=new Float64Array(e);for(let c=0;c<e;c++)u[c]=n[c]-i;let p=new Float64Array(t);for(let c=0;c<t;c++){let h=0;for(let f=0;f<e;f++)h+=a[f*t+c]*a[f*t+c];p[c]=h/e}return{xc:a,r:u,z:p,meanX:o,meanY:i}}l(R0,"elasticNetPrep");function M0(r,e,t,n){let s=e;for(let o=0;o<n;o++)s-=r[o]*t[o];return s}l(M0,"elasticNetIntercept");function JD(r,e,t,n,s,o,i,a,u,p){for(let c=0;c<u;c++){let h=0;for(let f=0;f<t;f++){if(s[f]===0)continue;let d=0;for(let _=0;_<e;_++)d+=r[_*t+f]*n[_];let m=d/e+o[f]*s[f],g=YD(m,i)/(s[f]+a),y=g-o[f];if(y!==0){for(let _=0;_<e;_++)n[_]-=y*r[_*t+f];o[f]=g,Math.abs(y)>h&&(h=Math.abs(y))}}if(h<p)break}}l(JD,"coordinateDescent");function $m(r,e,t,n,s,o,i,a){let{data:u,rows:p,cols:c}=Ue(e),h=Rt(t),{xc:f,r:d,z:m,meanX:g,meanY:y}=R0(u,p,c,h.data,a),_=new Float64Array(c),b=n*s,w=n*(1-s);JD(f,p,c,d,m,_,b,w,o,i);let T=M0(g,y,_,c);return[Q(_,[c],e.dtype,e.device),Q(new Float64Array([T]),[1],e.dtype,e.device)]}l($m,"cpuElasticNet");function ZD(r,e,t){if(e<=0||e>=r)return Array.from({length:r},(s,o)=>o);let n=Array.from({length:r},(s,o)=>o);for(let s=r-1;s>r-1-e;s--){let o=Math.floor(t()*(s+1)),i=n[s];n[s]=n[o],n[o]=i}return n.slice(r-e)}l(ZD,"chooseFeatures");function QD(r,e,t,n,s,o,i){let a=t.length,u=null;for(let p of n){let c=t.slice().sort((m,g)=>r[m*e+p]-r[g*e+p]),h=new Float64Array(s),f=new Float64Array(s);for(let m of c)f[o[m]]++;let d=0;for(let m=0;m<a-1;m++){let g=o[c[m]];h[g]++,f[g]--,d++;let y=a-d,_=r[c[m]*e+p],b=r[c[m+1]*e+p];if(_===b||d<i||y<i)continue;let w=1,T=1;for(let x=0;x<s;x++){let k=h[x]/d,A=f[x]/y;w-=k*k,T-=A*A}let S=(d*w+y*T)/a;(!u||S<u.impurity)&&(u={feature:p,threshold:(_+b)/2,impurity:S})}}return u}l(QD,"bestSplitClassify");function XD(r,e,t,n,s,o){let i=t.length,a=null;for(let u of n){let p=t.slice().sort((g,y)=>r[g*e+u]-r[y*e+u]),c=0,h=0;for(let g of p)c+=s[g],h+=s[g]*s[g];let f=0,d=0,m=0;for(let g=0;g<i-1;g++){let y=s[p[g]];f+=y,d+=y*y,m++;let _=i-m,b=r[p[g]*e+u],w=r[p[g+1]*e+u];if(b===w||m<o||_<o)continue;let T=d-f*f/m,S=c-f,k=h-d-S*S/_,A=T+k;(!a||A<a.impurity)&&(a={feature:u,threshold:(b+w)/2,impurity:A})}}return a}l(XD,"bestSplitRegress");function E1(r,e,t,n,s,o,i,a,u){let p=Ue(e),c=p.data,h=p.cols,f=Rt(t),d=cr(u),m=null,g=null,y=0;if(a){let N=rs(f.data,f.rows);m=N.y,g=N.classes,y=g.length}let _=[],b=[],w=[],T=[],S=[],x=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 v=0;for(let O of N)v+=f.data[O];return v/N.length},"leafValue"),k=l(N=>{let v=a?m:f.data,O=v[N[0]];for(let R of N)if(v[R]!==O)return!1;return!0},"isPure"),A=l((N,v)=>{let O=_.length;if(_.push(-1),b.push(0),w.push(-1),T.push(-1),S.push(0),v>=n||N.length<s||k(N))return S[O]=x(N),O;let R=ZD(h,i,d),B=a?QD(c,h,N,R,y,m,o):XD(c,h,N,R,f.data,o);if(!B)return S[O]=x(N),O;let E=[],z=[];for(let D of N)c[D*h+B.feature]<=B.threshold?E.push(D):z.push(D);return _[O]=B.feature,b[O]=B.threshold,w[O]=A(E,v+1),T[O]=A(z,v+1),O},"build");return A(Array.from({length:p.rows},(N,v)=>v),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(T),[T.length],e.dtype,e.device),Q(Float64Array.from(S),[S.length],e.dtype,e.device)]}l(E1,"cpuDecisionTreeFit");function L1(r,e,t,n,s,o,i){let a=Ue(e),u=Rt(t).data,p=Rt(n).data,c=Rt(s).data,h=Rt(o).data,f=Rt(i).data,d=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=h[y];d[g]=f[y]}return Q(d,[a.rows],e.dtype,e.device)}l(L1,"cpuDecisionTreePredict");var eP={kmeans:Cm,kmeans_predict:Dm,knn_predict:Pm,gaussian_nb_fit:M1,gaussian_nb_predict:I1,elastic_net:$m,decision_tree_fit:E1,decision_tree_predict:L1},tP=[0,2],nP=[1,5];function rP(r){return()=>{throw new Error(`ml.${r}: scalar-iterative algorithm runs on CPU/WASM only; GPU/WebGPU not supported (no performance benefit)`)}}l(rP,"unsupported");var C1=!1;function D1(){if(C1)return;C1=!0,Lm();let r=new Xt("mlc","IMPL");for(let[e,t]of Object.entries(eP)){for(let n of tP)r.impl(e,n,t);for(let n of nP)r.impl(e,n,rP(e))}}l(D1,"registerCpuMl");gs();Nn();gs();var sP=["qr(Tensor input) -> (Tensor, Tensor)","fft(Tensor input) -> Tensor","ifft(Tensor input) -> Tensor"],P1=!1;function $1(){if(P1)return;P1=!0;let r=new Xt("mlc","DEF");for(let e of sP)r.def(e)}l($1,"ensureNumericSchemas");Kn();function F1(r,e,t){let n=Math.min(e,t),s=Float64Array.from(r),o=[];for(let u=0;u<n;u++){let p=0;for(let d=u;d<e;d++)p+=s[d*t+u]*s[d*t+u];if(p=Math.sqrt(p),p===0){o.push(null);continue}let c=s[u*t+u]>=0?-p:p,h=new Float64Array(e-u);h[0]=s[u*t+u]-c;for(let d=u+1;d<e;d++)h[d-u]=s[d*t+u];let f=0;for(let d=0;d<h.length;d++)f+=h[d]*h[d];if(f===0){o.push(null);continue}o.push(h);for(let d=u;d<t;d++){let m=0;for(let y=0;y<h.length;y++)m+=h[y]*s[(u+y)*t+d];let g=2*m/f;for(let y=0;y<h.length;y++)s[(u+y)*t+d]-=g*h[y]}}let i=new Float64Array(e*n);for(let u=0;u<n;u++)i[u*n+u]=1;for(let u=n-1;u>=0;u--){let p=o[u];if(!p)continue;let c=0;for(let h=0;h<p.length;h++)c+=p[h]*p[h];for(let h=0;h<n;h++){let f=0;for(let m=0;m<p.length;m++)f+=p[m]*i[(u+m)*n+h];let d=2*f/c;for(let m=0;m<p.length;m++)i[(u+m)*n+h]-=d*p[m]}}let a=new Float64Array(n*t);for(let u=0;u<n;u++)for(let p=u;p<t;p++)a[u*t+p]=s[u*t+p];return{Q:i,R:a,k:n}}l(F1,"qrHost");function oP(r,e){let t=r.length;for(let n=1,s=0;n<t;n++){let o=t>>1;for(;s&o;o>>=1)s^=o;s^=o,n<s&&([r[n],r[s]]=[r[s],r[n]],[e[n],e[s]]=[e[s],e[n]])}}l(oP,"bitReversePermute");function Fm(r,e,t){let n=r.length;oP(r,e);for(let s=2;s<=n;s<<=1){let o=(t?1:-1)*2*Math.PI/s,i=Math.cos(o),a=Math.sin(o);for(let u=0;u<n;u+=s){let p=1,c=0,h=s>>1;for(let f=0;f<h;f++){let d=r[u+f],m=e[u+f],g=r[u+f+h]*p-e[u+f+h]*c,y=r[u+f+h]*c+e[u+f+h]*p;r[u+f]=d+g,e[u+f]=m+y,r[u+f+h]=d-g,e[u+f+h]=m-y;let _=p*i-c*a;c=p*a+c*i,p=_}}}if(t)for(let s=0;s<n;s++)r[s]/=n,e[s]/=n}l(Fm,"fftRadix2");function iP(r){let e=1;for(;e<r;)e<<=1;return e}l(iP,"nextPow2");function aP(r,e,t){let n=r.length,s=iP(2*n-1),o=t?1:-1,i=new Float64Array(n),a=new Float64Array(n);for(let f=0;f<n;f++){let d=f*f%(2*n),m=o*Math.PI*d/n;i[f]=Math.cos(m),a[f]=Math.sin(m)}let u=new Float64Array(s),p=new Float64Array(s);for(let f=0;f<n;f++)u[f]=r[f]*i[f]-e[f]*a[f],p[f]=r[f]*a[f]+e[f]*i[f];let c=new Float64Array(s),h=new Float64Array(s);c[0]=i[0],h[0]=-a[0];for(let f=1;f<n;f++)c[f]=i[f],h[f]=-a[f],c[s-f]=i[f],h[s-f]=-a[f];Fm(u,p,!1),Fm(c,h,!1);for(let f=0;f<s;f++){let d=u[f]*c[f]-p[f]*h[f];p[f]=u[f]*h[f]+p[f]*c[f],u[f]=d}Fm(u,p,!0);for(let f=0;f<n;f++)r[f]=u[f]*i[f]-p[f]*a[f],e[f]=u[f]*a[f]+p[f]*i[f];if(t)for(let f=0;f<n;f++)r[f]/=n,e[f]/=n}l(aP,"bluestein");function z1(r,e,t){let n=r.length;n!==0&&((n&n-1)===0?Fm(r,e,t):aP(r,e,t))}l(z1,"fftHost");function V1(r,e){let{data:t,rows:n,cols:s}=Ue(e),{Q:o,R:i,k:a}=F1(t,n,s);return[Q(o,[n,a],e.dtype,e.device),Q(i,[a,s],e.dtype,e.device)]}l(V1,"cpuQr");function uP(r){let e=Je(r);if(r.ndim===1){let t=r.shape[0];return{re:Float64Array.from(e),im:new Float64Array(t),n:t}}if(r.ndim===2&&r.shape[1]===2){let t=r.shape[0],n=new Float64Array(t),s=new Float64Array(t);for(let o=0;o<t;o++)n[o]=e[2*o],s[o]=e[2*o+1];return{re:n,im:s,n:t}}throw new Error(`fft: expected a 1-D real signal or [n, 2] complex tensor, got shape [${r.shape}]`)}l(uP,"splitComplex");function lP(r,e,t){let n=new Float64Array(2*t);for(let s=0;s<t;s++)n[2*s]=r[s],n[2*s+1]=e[s];return n}l(lP,"interleave");function G1(r,e){let{re:t,im:n,n:s}=uP(r);return z1(t,n,e),Q(lP(t,n,s),[s,2],r.dtype,r.device)}l(G1,"transform");function j1(r,e){return G1(e,!1)}l(j1,"cpuFft");function q1(r,e){return G1(e,!0)}l(q1,"cpuIfft");var cP={qr:V1,fft:j1,ifft:q1},pP=[0,2],U1=!1;function K1(){if(U1)return;U1=!0,$1();let r=new Xt("mlc","IMPL");for(let[e,t]of Object.entries(cP))for(let n of pP)r.impl(e,n,t)}l(K1,"registerCpuNumeric");gs();Nn();kd();var it=8,E0=4,nl=2,fP=nl*it,ma=Math.log2(nl),hP=65536,W1=fP,dP=64*1024*1024;function H1(r,e){return Math.ceil(r/e)*e}l(H1,"alignUp");var I0=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>dP?this._instantiate():this._top=0}_ensure(e){let t=this._memory.buffer.byteLength;e<=t||this._memory.grow(Math.ceil((e-t)/hP))}alloc(e){let t=H1(this._top,W1);return this._top=t+H1(e,W1),this._ensure(this._top),t}allocF64(e){return this.alloc(e*it)}allocI32(e){return this.alloc(e*E0)}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)}},Y1=new Map;function so(r,e,t){let n=Y1.get(r);return n||(n=new I0(new WebAssembly.Module(Nu(e())),t),Y1.set(r,n)),n.reclaim(),n}l(so,"simdModule");var mP=1e308,gP=1,yP=32768;function rp(r,e,t,n){let s=e.map(([i,a])=>`(param $${i} ${a})`).join(" "),o=t.map(([i,a])=>`(local $${i} ${a})`).join(" ");return`(module (memory (export "memory") ${gP} ${yP})
108
+ (func (export "${r}") ${s}
109
+ ${o}
110
+ ${n}))`}l(rp,"moduleWrap");function J1(r,e,t,n,s,o){return`
111
+ (local.get $${n}) (i32.const ${ma}) i32.shr_s (i32.const ${ma}) i32.shl local.set $${o.nmain}
112
+ (f64.const 0) f64x2.splat local.set $${o.acc}
113
+ (i32.const 0) local.set $${o.j}
114
+ (block $${r}_ve (loop $${r}_vl
115
+ (local.get $${o.j}) (local.get $${o.nmain}) i32.ge_s br_if $${r}_ve
116
+ (local.get $${e}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add v128.load local.set $${o.va}
117
+ (local.get $${t}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add v128.load local.set $${o.vb}
118
+ (local.get $${o.acc})
119
+ (local.get $${o.va}) (local.get $${o.vb}) f64x2.sub local.set $${o.va}
120
+ (local.get $${o.va}) (local.get $${o.va}) f64x2.mul
121
+ f64x2.add local.set $${o.acc}
122
+ (local.get $${o.j}) (i32.const ${nl}) i32.add local.set $${o.j}
123
+ br $${r}_vl))
124
+ (local.get $${o.acc}) f64x2.extract_lane 0
125
+ (local.get $${o.acc}) f64x2.extract_lane 1
126
+ f64.add local.set $${s}
127
+ (block $${r}_te (loop $${r}_tl
128
+ (local.get $${o.j}) (local.get $${n}) i32.ge_s br_if $${r}_te
129
+ (local.get $${e}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add f64.load
130
+ (local.get $${t}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add f64.load
131
+ f64.sub local.set $${o.diff}
132
+ (local.get $${s}) (local.get $${o.diff}) (local.get $${o.diff}) f64.mul f64.add local.set $${s}
133
+ (local.get $${o.j}) (i32.const 1) i32.add local.set $${o.j}
134
+ br $${r}_tl))`}l(J1,"sqDistSnippet");var Z1={j:"j",nmain:"nmain",acc:"acc",va:"va",vb:"vb",diff:"diff"},Q1=[["j","i32"],["nmain","i32"],["acc","v128"],["va","v128"],["vb","v128"],["diff","f64"]];function L0(){let r=`
135
+ (f64.const 0) local.set $inertiaAcc
136
+ (i32.const 0) local.set $i
137
+ (block $iend (loop $iloop
138
+ (local.get $i) (local.get $n) i32.ge_s br_if $iend
139
+ (local.get $x) (local.get $i) (local.get $d) i32.mul (i32.const ${it}) i32.mul i32.add local.set $xrow
140
+ (i32.const 0) local.set $best
141
+ (f64.const ${mP}) local.set $bestd
142
+ (i32.const 0) local.set $cc
143
+ (block $cend (loop $cloop
144
+ (local.get $cc) (local.get $k) i32.ge_s br_if $cend
145
+ (local.get $c) (local.get $cc) (local.get $d) i32.mul (i32.const ${it}) i32.mul i32.add local.set $crow
146
+ ${J1("nc","xrow","crow","d","dist",Z1)}
147
+ (local.get $dist) (local.get $bestd) f64.lt
148
+ (if (then
149
+ (local.get $dist) local.set $bestd
150
+ (local.get $cc) local.set $best))
151
+ (local.get $cc) (i32.const 1) i32.add local.set $cc
152
+ br $cloop))
153
+ (local.get $labels) (local.get $i) (i32.const ${E0}) i32.mul i32.add (local.get $best) i32.store
154
+ (local.get $inertiaAcc) (local.get $bestd) f64.add local.set $inertiaAcc
155
+ (local.get $i) (i32.const 1) i32.add local.set $i
156
+ br $iloop))
157
+ (local.get $inertia) (local.get $inertiaAcc) f64.store`;return rp("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"],...Q1],r)}l(L0,"nearestCentroidWat");function X1(){let r=`
158
+ (i32.const 0) local.set $t
159
+ (block $tend (loop $tloop
160
+ (local.get $t) (local.get $ntr) i32.ge_s br_if $tend
161
+ (local.get $tr) (local.get $t) (local.get $d) i32.mul (i32.const ${it}) i32.mul i32.add local.set $trow
162
+ ${J1("dr","q","trow","d","dist",Z1)}
163
+ (local.get $out) (local.get $t) (i32.const ${it}) i32.mul i32.add (local.get $dist) f64.store
164
+ (local.get $t) (i32.const 1) i32.add local.set $t
165
+ br $tloop))`;return rp("dist_row",[["q","i32"],["tr","i32"],["ntr","i32"],["d","i32"],["out","i32"]],[["t","i32"],["trow","i32"],["dist","f64"],...Q1],r)}l(X1,"distRowWat");function C0(r,e,t,n,s,o){return`
166
+ (local.get $${n}) (i32.const ${ma}) i32.shr_s (i32.const ${ma}) i32.shl local.set $${o.nmain}
167
+ (f64.const 0) f64x2.splat local.set $${o.acc}
168
+ (i32.const 0) local.set $${o.j}
169
+ (block $${r}_ve (loop $${r}_vl
170
+ (local.get $${o.j}) (local.get $${o.nmain}) i32.ge_s br_if $${r}_ve
171
+ (local.get $${o.acc})
172
+ (local.get $${e}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add v128.load
173
+ (local.get $${t}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add v128.load
174
+ f64x2.mul f64x2.add local.set $${o.acc}
175
+ (local.get $${o.j}) (i32.const ${nl}) i32.add local.set $${o.j}
176
+ br $${r}_vl))
177
+ (local.get $${o.acc}) f64x2.extract_lane 0
178
+ (local.get $${o.acc}) f64x2.extract_lane 1
179
+ f64.add local.set $${s}
180
+ (block $${r}_te (loop $${r}_tl
181
+ (local.get $${o.j}) (local.get $${n}) i32.ge_s br_if $${r}_te
182
+ (local.get $${s})
183
+ (local.get $${e}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add f64.load
184
+ (local.get $${t}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add f64.load
185
+ f64.mul f64.add local.set $${s}
186
+ (local.get $${o.j}) (i32.const 1) i32.add local.set $${o.j}
187
+ br $${r}_tl))`}l(C0,"reduceDotSnippet");function _P(r,e,t,n,s,o){return`
188
+ (local.get $${n}) f64x2.splat local.set $${o.sv}
189
+ (local.get $${s}) (i32.const ${ma}) i32.shr_s (i32.const ${ma}) i32.shl local.set $${o.nmain}
190
+ (i32.const 0) local.set $${o.j}
191
+ (block $${r}_ve (loop $${r}_vl
192
+ (local.get $${o.j}) (local.get $${o.nmain}) i32.ge_s br_if $${r}_ve
193
+ (local.get $${e}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add local.set $${o.ya}
194
+ (local.get $${o.ya}) v128.load
195
+ (local.get $${o.sv})
196
+ (local.get $${t}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add v128.load
197
+ f64x2.mul f64x2.sub local.set $${o.vy}
198
+ (local.get $${o.ya}) (local.get $${o.vy}) v128.store
199
+ (local.get $${o.j}) (i32.const ${nl}) i32.add local.set $${o.j}
200
+ br $${r}_vl))
201
+ (block $${r}_te (loop $${r}_tl
202
+ (local.get $${o.j}) (local.get $${s}) i32.ge_s br_if $${r}_te
203
+ (local.get $${e}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add local.set $${o.ya}
204
+ (local.get $${o.ya})
205
+ (local.get $${o.ya}) f64.load
206
+ (local.get $${n})
207
+ (local.get $${t}) (local.get $${o.j}) (i32.const ${it}) i32.mul i32.add f64.load
208
+ f64.mul f64.sub f64.store
209
+ (local.get $${o.j}) (i32.const 1) i32.add local.set $${o.j}
210
+ br $${r}_tl))`}l(_P,"axpySnippet");function eO(){let e=`
211
+ (i32.const 0) local.set $i
212
+ (block $ie (loop $il
213
+ (local.get $i) (local.get $m) i32.ge_s br_if $ie
214
+ (local.get $mat) (local.get $i) (local.get $len) i32.mul (i32.const ${it}) i32.mul i32.add local.set $rowI
215
+ (local.get $i) local.set $j
216
+ (block $je (loop $jl
217
+ (local.get $j) (local.get $m) i32.ge_s br_if $je
218
+ (local.get $mat) (local.get $j) (local.get $len) i32.mul (i32.const ${it}) i32.mul i32.add local.set $rowJ
219
+ ${C0("gs","rowI","rowJ","len","d",{j:"kk",nmain:"nm",acc:"acc"})}
220
+ (local.get $g) (local.get $i) (local.get $m) i32.mul (local.get $j) i32.add (i32.const ${it}) i32.mul i32.add (local.get $d) f64.store
221
+ (local.get $g) (local.get $j) (local.get $m) i32.mul (local.get $i) i32.add (i32.const ${it}) i32.mul i32.add (local.get $d) f64.store
222
+ (local.get $j) (i32.const 1) i32.add local.set $j
223
+ br $jl))
224
+ (local.get $i) (i32.const 1) i32.add local.set $i
225
+ br $il))`;return rp("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(eO,"gramSymWat");function tO(){let e=`
226
+ (i32.const 0) local.set $i
227
+ (block $ie (loop $il
228
+ (local.get $i) (local.get $m) i32.ge_s br_if $ie
229
+ (local.get $a) (local.get $i) (local.get $len) i32.mul (i32.const ${it}) i32.mul i32.add local.set $rowA
230
+ (i32.const 0) local.set $c
231
+ (block $ce (loop $cl
232
+ (local.get $c) (local.get $p) i32.ge_s br_if $ce
233
+ (local.get $b) (local.get $c) (local.get $len) i32.mul (i32.const ${it}) i32.mul i32.add local.set $rowB
234
+ ${C0("mm","rowA","rowB","len","d",{j:"kk",nmain:"nm",acc:"acc"})}
235
+ (local.get $out) (local.get $i) (local.get $p) i32.mul (local.get $c) i32.add (i32.const ${it}) i32.mul i32.add (local.get $d) f64.store
236
+ (local.get $c) (i32.const 1) i32.add local.set $c
237
+ br $cl))
238
+ (local.get $i) (i32.const 1) i32.add local.set $i
239
+ br $il))`;return rp("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(tO,"matmulRowsWat");function nO(){let r={j:"j",nmain:"nmain",acc:"acc"},e={j:"j2",nmain:"nmain2",sv:"sv",ya:"ya",vy:"vy"},t=`
240
+ (local.get $n) f64.convert_i32_s local.set $nf
241
+ (i32.const 0) local.set $iter
242
+ (block $ie (loop $il
243
+ (local.get $iter) (local.get $maxIter) i32.ge_s br_if $ie
244
+ (f64.const 0) local.set $maxChange
245
+ (i32.const 0) local.set $jc
246
+ (block $je (loop $jl
247
+ (local.get $jc) (local.get $d) i32.ge_s br_if $je
248
+ (local.get $z) (local.get $jc) (i32.const ${it}) i32.mul i32.add f64.load local.set $zj
249
+ (local.get $zj) (f64.const 0) f64.ne
250
+ (if (then
251
+ (local.get $xc) (local.get $jc) (local.get $n) i32.mul (i32.const ${it}) i32.mul i32.add local.set $col
252
+ ${C0("cd","col","r","n","dot",r)}
253
+ (local.get $w) (local.get $jc) (i32.const ${it}) i32.mul i32.add f64.load local.set $wold
254
+ (local.get $dot) (local.get $nf) f64.div (local.get $wold) (local.get $zj) f64.mul f64.add local.set $rho
255
+ (local.get $rho) (local.get $l1) f64.gt
256
+ (if (then
257
+ (local.get $rho) (local.get $l1) f64.sub local.set $thr
258
+ ) (else
259
+ (local.get $rho) (f64.const 0) (local.get $l1) f64.sub f64.lt
260
+ (if (then
261
+ (local.get $rho) (local.get $l1) f64.add local.set $thr
262
+ ) (else
263
+ (f64.const 0) local.set $thr))))
264
+ (local.get $thr) (local.get $zj) (local.get $l2) f64.add f64.div local.set $wj
265
+ (local.get $wj) (local.get $wold) f64.sub local.set $delta
266
+ (local.get $delta) (f64.const 0) f64.ne
267
+ (if (then
268
+ ${_P("cd","r","col","delta","n",e)}
269
+ (local.get $w) (local.get $jc) (i32.const ${it}) i32.mul i32.add (local.get $wj) f64.store
270
+ (local.get $delta) f64.abs local.set $absd
271
+ (local.get $absd) (local.get $maxChange) f64.gt
272
+ (if (then (local.get $absd) local.set $maxChange))))))
273
+ (local.get $jc) (i32.const 1) i32.add local.set $jc
274
+ br $jl))
275
+ (local.get $maxChange) (local.get $tol) f64.lt br_if $ie
276
+ (local.get $iter) (i32.const 1) i32.add local.set $iter
277
+ br $il))`;return rp("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(nO,"coordDescentWat");var bP=5e5;function rO(r,e,t){let n=so("gram_sym",eO,"gram_sym"),s=n.allocF64(e*t),o=n.allocF64(e*e);return n.writeF64(s,r),n.run(s,e,t,o),Float64Array.from(n.f64(o,e*e))}l(rO,"gramSym");function wP(r,e,t,n){if(n){let s=new Float64Array(t*e);for(let o=0;o<e;o++)for(let i=0;i<t;i++)s[i*e+o]=r[o*t+i];return rO(s,t,e)}return rO(r,e,t)}l(wP,"wasmGram");function TP(r,e,t,n,s){let o=so("matmul_rows",tO,"matmul_rows"),i=o.allocF64(e*t),a=o.allocF64(s*t),u=o.allocF64(e*s);return o.writeF64(i,r),o.writeF64(a,n),o.run(i,e,t,a,s,u),Float64Array.from(o.f64(u,e*s))}l(TP,"wasmMatmulRows");function sO(r,e){let{data:t,rows:n,cols:s}=Ue(e);if(Math.max(n,s)*Math.min(n,s)*Math.min(n,s)<bP)return Em(r,e);let{U:i,S:a,V:u,k:p}=da(t,n,s,void 0,wP,TP);return[Q(i,[n,p],e.dtype,e.device),Q(a,[p],e.dtype,e.device),Q(u,[s,p],e.dtype,e.device)]}l(sO,"wasmSvd");var oO={svd:sO},xP={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"},iO=!1;function aO(){let r={enabled:Object.keys(oO),skipped:Object.keys(xP)};if(iO)return r;iO=!0,Im();let e=new Xt("mlc","IMPL");for(let[t,n]of Object.entries(oO))e.impl(t,2,n);return r}l(aO,"registerWasmLinalg");gs();Nn();var SP=8e3,kP=5e4,vP=2654435769;function uO(r,e,t,n,s,o,i,a,u,p){return r.writeF64(t,u),r.run(e,o,i,t,a,n,s),p.set(r.i32(n,o)),r.f64(s,1)[0]}l(uO,"assignWasm");function lO(r,e,t,n,s,o){let{data:i,rows:a,cols:u}=Ue(e);if(a*u*t<SP)return Cm(r,e,t,n,s,o);let p=so("kmeans_assign",L0,"nearest_centroid"),c=null;for(let f=0;f<s;f++){p.reset();let d=p.allocF64(a*u),m=p.allocF64(t*u),g=p.allocI32(a),y=p.allocF64(1);p.writeF64(d,i);let _=cr(o+f*vP),b=v0(i,a,u,t,_),w=new Int32Array(a),T=1/0;for(let S=0;S<n&&(T=uO(p,d,m,g,y,a,u,t,b,w),!!N0(i,a,u,t,w,b));S++);T=uO(p,d,m,g,y,a,u,t,b,w),(!c||T<c.inertia)&&(c={centers:Float64Array.from(b),labels:Int32Array.from(w),inertia:T})}let h=new Float64Array(a);for(let f=0;f<a;f++)h[f]=c.labels[f];return[Q(c.centers,[t,u],e.dtype,e.device),Q(h,[a],e.dtype,e.device),Q(new Float64Array([c.inertia]),[1],e.dtype,e.device)]}l(lO,"wasmKmeans");function cO(r,e,t){let{data:n,rows:s,cols:o}=Ue(e),i=Ue(t);if(s*o*i.rows<kP)return Dm(r,e,t);let a=so("kmeans_assign",L0,"nearest_centroid");a.reset();let u=a.allocF64(s*o),p=a.allocF64(i.rows*o),c=a.allocI32(s),h=a.allocF64(1);a.writeF64(u,n),a.writeF64(p,i.data),a.run(u,s,o,p,i.rows,c,h);let f=new Float64Array(s),d=a.i32(c,s);for(let m=0;m<s;m++)f[m]=d[m];return Q(f,[s],e.dtype,e.device)}l(cO,"wasmKmeansPredict");var NP=2e4;function pO(r,e,t,n,s,o){let i=Ue(e),a=Ue(n),u=Rt(t),p=i.cols;if(a.rows*i.rows*p<NP)return Pm(r,e,t,n,s,o);let c=so("knn_dist",X1,"dist_row");c.reset();let h=c.allocF64(a.rows*p),f=c.allocF64(i.rows*p),d=c.allocF64(i.rows);c.writeF64(h,a.data),c.writeF64(f,i.data);let m=Math.min(s,i.rows),g=new Float64Array(a.rows),y=new Int32Array(i.rows),_=p*it;for(let b=0;b<a.rows;b++){c.run(h+b*_,f,i.rows,p,d);let w=c.f64(d,i.rows);A0(w,y,i.rows,m),g[b]=O0(u.data,y,m,o)}return Q(g,[a.rows],n.dtype,n.device)}l(pO,"wasmKnnPredict");var AP=3e6;function fO(r,e,t,n,s,o,i,a){let{data:u,rows:p,cols:c}=Ue(e);if(p*c*o<AP)return $m(r,e,t,n,s,o,i,a);let h=Rt(t),{xc:f,r:d,z:m,meanX:g,meanY:y}=R0(u,p,c,h.data,a),_=new Float64Array(p*c);for(let O=0;O<c;O++)for(let R=0;R<p;R++)_[O*p+R]=f[R*c+O];let b=n*s,w=n*(1-s),T=so("coord_descent",nO,"coord_descent");T.reset();let S=T.allocF64(p*c),x=T.allocF64(p),k=T.allocF64(c),A=T.allocF64(c);T.writeF64(S,_),T.writeF64(x,d),T.writeF64(A,m),T.f64(k,c).fill(0),T.run(S,p,c,x,k,A,b,w,o,i);let N=Float64Array.from(T.f64(k,c)),v=M0(g,y,N,c);return[Q(N,[c],e.dtype,e.device),Q(new Float64Array([v]),[1],e.dtype,e.device)]}l(fO,"wasmElasticNet");var hO={kmeans:lO,kmeans_predict:cO,knn_predict:pO,elastic_net:fO},dO=!1;function mO(){let r={enabled:Object.keys(hO)};if(dO)return r;dO=!0,Lm();let e=new Xt("mlc","IMPL");for(let[t,n]of Object.entries(hO))e.impl(t,2,n);return r}l(mO,"registerWasmMl");Nn();nc();$i();sp();var OP=0,zm=class{static{l(this,"Edge")}node;inputNr;constructor(e,t){this.node=e,this.inputNr=t}},Be=class{static{l(this,"AutogradNode")}_id;_numInputs;_nextEdges;_savedTensors;_inputMetadata;_opArgs;constructor(e){this._id=OP++,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 zm(e,t))}setNextEdge(e,t,n){for(;this._nextEdges.length<=e;)this._nextEdges.push(null);this._nextEdges[e]=new zm(t,n)}saveTensor(e){this._savedTensors.push(e)}savedTensors(){return this._savedTensors}saveInputMetadata(e,t,n){this._inputMetadata[e]={shape:t,dtype:n}}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=[]}};be();_t();var Vm=class extends Be{static{l(this,"AddBackward")}constructor(){super(2)}apply(e){let t=e[0];return[t,t]}},Gm=class extends Be{static{l(this,"SubBackward")}constructor(){super(2)}apply(e){let t=e[0];return[t,Ur(t)]}},jm=class extends Be{static{l(this,"MulBackward")}constructor(){super(2)}apply(e){let t=e[0],[n,s]=this.savedTensors();return[ee(t,s.detach()),ee(t,n.detach())]}},qm=class extends Be{static{l(this,"DivBackward")}constructor(){super(2)}apply(e){let t=e[0],[n,s]=this.savedTensors(),o=$t(t,s.detach()),i=Ur($t(ee(t,n.detach()),ee(s.detach(),s.detach())));return[o,i]}},Um=class extends Be{static{l(this,"NegBackward")}constructor(){super(1)}apply(e){let t=e[0];return[Ur(t)]}},Km=class extends Be{static{l(this,"PowBackward")}constructor(){super(2)}apply(e){let t=e[0],[n,s]=this.savedTensors(),o=Ht(s.shape,{dtype:s.dtype,device:s.device}),i=ee(t,ee(s.detach(),Js(n.detach(),Pe(s.detach(),o)))),a=ee(t,ee(Js(n.detach(),s.detach()),Zs(n.detach())));return[i,a]}};be();_t();Ji();var BP=2/Math.sqrt(Math.PI);function bO(r){let e=et(r.shape,BP,{dtype:r.dtype,device:r.device});return ee(e,Kr(Ur(ee(r,r))))}l(bO,"_erfDeriv");function wO(r){let e=ae(r,Yi),t=Js(e,-1),n=Pe(Zs(e),ee(t,.5)),s=ee(t,t),o=s;for(let i of Nc)n=ae(n,ee(o,i)),o=ee(o,s);for(let i=0;i<Yi;i++)n=Pe(n,Js(ae(r,i),-1));return n}l(wO,"_digammaTensor");var Wm=class extends Be{static{l(this,"ErfBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors();return[ee(e[0],bO(t.detach()))]}},Hm=class extends Be{static{l(this,"ErfcBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors();return[Ur(ee(e[0],bO(t.detach())))]}},Ym=class extends Be{static{l(this,"LgammaBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors();return[ee(e[0],wO(t.detach()))]}},Jm=class extends Be{static{l(this,"GammaBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),n=t.detach();return[ee(e[0],ee(Cd(n),wO(n)))]}},Zm=class extends Be{static{l(this,"ExpBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors();return[ee(e[0],Kr(t.detach()))]}},Qm=class extends Be{static{l(this,"LogBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors();return[$t(e[0],t.detach())]}},Xm=class extends Be{static{l(this,"SqrtBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),n=t.detach(),s=ui(n),o=et(n.shape,2,{dtype:n.dtype,device:n.device});return[$t(e[0],ee(o,s))]}},eg=class extends Be{static{l(this,"TanhBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),n=t.detach(),s=Ns(n),o=Ht(n.shape,{dtype:n.dtype,device:n.device});return[ee(e[0],Pe(o,ee(s,s)))]}},tg=class extends Be{static{l(this,"SigmoidBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),n=t.detach(),s=ur(n),o=Ht(n.shape,{dtype:n.dtype,device:n.device});return[ee(e[0],ee(s,Pe(o,s)))]}},ng=class extends Be{static{l(this,"SoftmaxBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),n=this.opArgs(),s=n&&n.length>1&&n[1]!=null?n[1]:-1,o=e[0],i=Ro(t.detach(),s),a=ht(ee(o,i),s,!0);return[ee(i,Pe(o,a))]}},rg=class extends Be{static{l(this,"LogSoftmaxBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),n=this.opArgs(),s=n&&n.length>1&&n[1]!=null?n[1]:-1,o=e[0],i=Ro(t.detach(),s),a=ht(o,s,!0);return[Pe(o,ee(i,a))]}},sg=class extends Be{static{l(this,"ReluBackward")}constructor(){super(1)}apply(e){let[t]=this.savedTensors(),n=t.detach();return[ee(e[0],$d(Mu(n)))]}},og=class extends Be{static{l(this,"GeluBackward")}constructor(){super(1)}apply(e){let t=e[0],[n]=this.savedTensors(),s=n.detach(),o=s.shape,i=s.dtype,a=s.device,u=et(o,1.702,{dtype:i,device:a}),p=Ht(o,{dtype:i,device:a}),c=ee(u,s),h=ur(c),f=ee(h,ae(p,ee(c,Pe(p,h))));return[ee(t,f)]}},ig=class extends Be{static{l(this,"SiluBackward")}constructor(){super(1)}apply(e){let t=e[0],[n]=this.savedTensors(),s=n.detach(),o=ur(s),i=Ht(s.shape,{dtype:s.dtype,device:s.device}),a=ee(o,ae(i,ee(s,Pe(i,o))));return[ee(t,a)]}};be();_t();be();function TO(r,e){if(r==null){let n=[];for(let s=0;s<e;s++)n.push(s);return n}return(Array.isArray(r)?r:[r]).map(n=>n<0?n+e:n).sort((n,s)=>n-s)}l(TO,"_normalizeDims");function xO(r,e,t,n){let s=r;if(!n)for(let i of t)s=Sr(s,i);let o=$e(e,{dtype:s.dtype,device:s.device});return ae(o,s)}l(xO,"_unreduce");var ag=class extends Be{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=TO(i,s.length);return[xO(t,s,u,a)]}},ug=class extends Be{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=TO(i,s.length),p=1;for(let f of u)p*=s[f];let c=xO(t,s,u,a),h=et(s,p,{dtype:t.dtype,device:t.device});return[$t(c,h)]}};be();be();function SO(r,e){let t=r;for(;t.ndim>e.length;)t=ht(t,0,!1);for(let n=0;n<e.length;n++)e[n]===1&&t.shape[n]!==1&&(t=ht(t,n,!0));return t}l(SO,"_sumToShape");var lg=class extends Be{static{l(this,"MatmulBackward")}constructor(){super(2)}apply(e){let t=e[0],[n,s]=this.savedTensors(),o=n.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 h=Sr(t,1),f=Sr(i,0);return[lt(h,f),Cu(lt(gt(o,0,1),h),1)]}if(a===1&&u===2){let h=lt(t,gt(i,0,1)),f=lt(Sr(o,1),Sr(t,0));return[h,f]}let p=lt(t,gt(i,u-2,u-1)),c=lt(gt(o,a-2,a-1),t);return p=SO(p,o.shape),c=SO(c,i.shape),[p,c]}},cg=class extends Be{static{l(this,"DotBackward")}constructor(){super(2)}apply(e){let t=e[0],[n,s]=this.savedTensors();return[ee(t,s.detach()),ee(t,n.detach())]}};be();_t();be();jr();function RP(r,e,t){r[e]+=t}l(RP,"_addAt");var pg=class extends Be{static{l(this,"CatBackward")}constructor(){super(0)}apply(e){let t=e[0],n=this.opArgs(),s=t.shape.length,o=vs(n&&n.length>1?n[1]??0:0,s),i=[],a=0,u=0;for(;this.inputMetadata(u);){let c=this.inputMetadata(u).shape[o];i.push(ci(Un(t,o,a,c))),a+=c,u++}return i}},fg=class extends Be{static{l(this,"StackBackward")}constructor(){super(0)}apply(e){let t=e[0],n=this.opArgs(),s=t.shape.length,o=vs(n&&n.length>1?n[1]??0:0,s),i=[],a=0;for(;this.inputMetadata(a);)i.push(ci(Ut(t,o,a))),a++;return i}},hg=class extends Be{static{l(this,"ClampBackward")}constructor(){super(3)}apply(e){let t=e[0],[n,s,o]=this.savedTensors(),i=$e(t.shape,{dtype:t.dtype,device:t.device}),a=zd(n.detach(),s.detach()),u=qn(a,t,i),p=Fd(n.detach(),o.detach());return[qn(p,u,i),null,null]}},dg=class extends Be{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=Un(a,u,p,i[u])}return[ci(a),null]}},mg=class r extends Be{static{l(this,"IndexSelectBackward")}static#e=null;static setGpuBackward(e){r.#e=e}constructor(){super(2)}apply(e){let t=e[0],[,n]=this.savedTensors(),o=this.inputMetadata(0).shape,i=o.length,a=this.opArgs(),u=vs(a&&a.length>2?a[2]??0:0,i);if(r.#e){let k=r.#e(t,n,o,u);if(k)return[k]}let p=$e(o,{dtype:t.dtype,device:t.device}),c=p._impl.storage.data,h=p.strides,f=ci(t),d=f._impl.storage.data,m=f._impl.storageOffset,g=f.shape,y=f.strides,_=ci(n),b=_._impl.storage.data,w=_._impl.storageOffset,T=g.length,S=new Int32Array(T),x=m;for(let k=0;k<f.numel;k++){let A=0;for(let N=0;N<T;N++){let v=N===u?Number(b[w+S[N]]):S[N];A+=v*h[N]}RP(c,A,d[x]);for(let N=T-1;N>=0;N--){if(S[N]++,S[N]<g[N]){x+=y[N];break}x-=(g[N]-1)*y[N],S[N]=0}}return[p,null]}},gg=class extends Be{static{l(this,"WhereBackward")}constructor(){super(3)}apply(e){let t=e[0],[n]=this.savedTensors(),s=$e(t.shape,{dtype:t.dtype,device:t.device}),o=qn(n.detach(),t,s),i=qn(n.detach(),s,t);return[null,o,i]}};be();_t();be();function MP(r,e,t){r[e]+=t}l(MP,"_addAt");var sl=class extends Be{static{l(this,"ReshapeBackward")}constructor(){super(1)}apply(e){let t=this.inputMetadata(0);return[Cn(e[0],t.shape)]}},yg=class extends Be{static{l(this,"TransposeBackward")}_dim0;_dim1;constructor(e,t){super(1),this._dim0=e,this._dim1=t}apply(e){return[gt(e[0],this._dim0,this._dim1)]}},op=class extends Be{static{l(this,"SliceBackward")}_dim;_start;_end;_step;constructor(e,t,n,s){super(1),this._dim=e,this._start=t,this._end=n,this._step=s}apply(e){let t=e[0],n=this.inputMetadata(0),s=this._dim,o=n.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=n.shape.map(()=>0),T=n.shape.map(()=>0);return w[s]=a,T[s]=o-u,[na(t,w,T,0)]}let p=$e(n.shape,{dtype:t.dtype,device:t.device}),c=p._impl.storage.data,h=t._impl.storage.data,f=t._impl.storageOffset,d=t.shape,m=t.strides,g=p.strides,y=d.length,_=new Int32Array(y),b=f;for(let w=0;w<t.numel;w++){let T=0;for(let S=0;S<y;S++){let x=S===s?a+_[S]*i:_[S];T+=x*g[S]}MP(c,T,h[b]);for(let S=y-1;S>=0;S--){if(_[S]++,_[S]<d[S]){b+=m[S];break}b-=(d[S]-1)*m[S],_[S]=0}}return[p]}},_g=class extends Be{static{l(this,"SelectBackward")}_dim;_index;constructor(e,t){super(1),this._dim=e,this._index=t}apply(e){let t=e[0],n=this.inputMetadata(0),s=this._dim,o=n.shape[s],i=this._index<0?this._index+o:this._index,a=Sr(t,s),u=n.shape.map(()=>0),p=n.shape.map(()=>0);return u[s]=i,p[s]=o-1-i,[na(a,u,p,0)]}},ip=class extends Be{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=ht(t,i,!0)),[Cn(u,s)]}},bg=class extends Be{static{l(this,"PermuteBackward")}_dims;constructor(e){super(1),this._dims=e}apply(e){let t=this._dims.length,n=new Array(t);for(let s=0;s<t;s++){let o=this._dims[s]<0?t+this._dims[s]:this._dims[s];n[o]=s}return[Eu(e[0],n)]}};var D0=new Map;function Fe(r,e){D0.set(r,e)}l(Fe,"_register");Fe("add",()=>new Vm);Fe("sub",()=>new Gm);Fe("mul",()=>new jm);Fe("div",()=>new qm);Fe("neg",()=>new Um);Fe("pow",()=>new Km);Fe("exp",()=>new Zm);Fe("log",()=>new Qm);Fe("sqrt",()=>new Xm);Fe("tanh",()=>new eg);Fe("erf",()=>new Wm);Fe("erfc",()=>new Hm);Fe("lgamma",()=>new Ym);Fe("gamma",()=>new Jm);Fe("sigmoid",()=>new tg);Fe("relu",()=>new sg);Fe("softmax",()=>new ng);Fe("log_softmax",()=>new rg);Fe("gelu",()=>new og);Fe("silu",()=>new ig);Fe("sum",()=>new ag);Fe("mean",()=>new ug);Fe("matmul",()=>new lg);Fe("dot",()=>new cg);Fe("cat",()=>new pg);Fe("stack",()=>new fg);Fe("clamp",()=>new hg);Fe("pad",()=>new dg);Fe("index_select",()=>new mg);Fe("where",()=>new gg);Fe("reshape",()=>new sl);Fe("transpose",r=>new yg(r[1],r[2]));Fe("permute",r=>new bg(r[1]));Fe("broadcast_in_dim",()=>new ip);Fe("expand",()=>new ip);Fe("slice",r=>new op(r[1],r[2],r[3],r[4]));Fe("narrow",r=>new op(r[1],r[2],r[2]+r[3],1));Fe("select",r=>new _g(r[1],r[2]));Fe("unsqueeze",()=>new sl);Fe("squeeze",()=>new sl);function kO(r,e=null){let t=D0.get(r);return t?t(e):null}l(kO,"getGradFn");function vO(r){return D0.has(r)}l(vO,"hasGradFn");be();var wg=class extends Be{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],n=this._variable.deref();if(!n)return[];let s=n._impl.autogradMeta;return s?(s.grad===null?s.grad=t:s.grad=ae(s.grad,t),[]):[]}name(){return"GradAccumulator"}};aw();Oo();jn();ar();Tm();jn();_t();be();function NO(r,e){let t=r.gradFn;if(!t)throw new Error("Cannot call backward on a tensor that does not require grad");let n=e;if(!n){if(r.numel!==1)throw new Error("grad must be specified for non-scalar tensors");n=Ht(r.shape,{dtype:r.dtype,device:r.device})}let s=new Map;EP(t,s,new Set);let i=new Map;i.set(t.id,[n]);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 h=c.node,f=s.get(h)-1;s.set(h,f),f===0&&a.push(h)}}}for(let p of u){let c=i.get(p.id);if(!c)continue;let h=p.apply(c),f=h;if(h&&(f=h.map((m,g)=>{if(!m)return m;let y=p.inputMetadata(g);return y?IP(m,y.shape):m})),p.releaseVariables(),!f)continue;let d=p.nextEdges;for(let m=0;m<d.length;m++){let g=d[m];if(!g||!g.node||m>=f.length||!f[m])continue;let y=f[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(NO,"backward");function IP(r,e){let t=r.shape;if(t.length===e.length&&t.every((o,i)=>o===e[i]))return r;let n=r,s=t.length-e.length;for(let o=0;o<s;o++)n=ht(n,0,!1);for(let o=e.length-1;o>=0;o--)e[o]===1&&n.shape[o]!==1&&(n=ht(n,o,!0));return n}l(IP,"_reduceBroadcastGrad");function EP(r,e,t){let n=[r];for(e.set(r,0);n.length>0;){let s=n.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)||n.push(i)}}}}l(EP,"_countDeps");oN({backward:NO});function LP(r){let e=r._impl;if(Zc()&&e.device&&e.device.type==="gpu"){e.storage.retain();let s=new Bt(e.storage,e.storageOffset,e.sizes(),e.strides(),e.dtype,e.device);return new We(s)}let t=e.storage.clone(),n=new Bt(t,e.storageOffset,e.sizes(),e.strides(),e.dtype,e.device);return new We(n)}l(LP,"_snapshotTensor");function ap(r){return typeof r=="object"&&r!==null&&"_impl"in r}l(ap,"_isTensor");function CP(r){for(let e=0;e<r.length;e++){let t=r[e];if(ap(t)&&t.requiresGrad)return!0;if(Array.isArray(t)){for(let n=0;n<t.length;n++)if(ap(t[n])&&t[n].requiresGrad)return!0}}return!1}l(CP,"_anyRequiresGrad");function DP(r){let e=[];for(let t=0;t<r.length;t++){let n=r[t];if(ap(n))e.push(n);else if(Array.isArray(n))for(let s=0;s<n.length;s++)ap(n[s])&&e.push(n[s])}return e}l(DP,"_extractTensors");function PP(r){let e=r._impl.autogradMeta;if(!e)return null;let t=e.getGradAccumulator();return t||(t=new wg(r),e.setGradAccumulator(t)),t}l(PP,"_getOrCreateAccumulator");function $P(r,e){let t=Nt.dispatch.bind(Nt);return(n,...s)=>{let o=n;if(!rl.isEnabled()||!CP(s)){let c=o.subtract(sc);return Nt.redispatch(e,c,...s)}let i=kO(r,s);if(!i){let c=o.subtract(sc);return Nt.redispatch(e,c,...s)}let a=DP(s);i.setOpArgs(s);for(let c=0;c<a.length;c++)i.saveTensor(LP(a[c])),i.saveInputMetadata(c,[...a[c].shape],a[c].dtype);for(let c=0;c<a.length;c++){let h=a[c];if(h.requiresGrad){let f=h.gradFn;if(f){let d=h._impl.autogradMeta?h._impl.autogradMeta.outputNr:0;i.setNextEdge(c,f,d)}else{let d=PP(h);d&&i.setNextEdge(c,d,0)}}}let u=o.subtract(sc),p=Nt.redispatch(e,u,...s);if(ap(p)){p._impl.setAutogradMeta(new Bu);let c=p._impl.autogradMeta;c.setGradFn(i,0),c.requiresGrad=!0,p._impl._updateKeySet()}return p}}l($P,"wrapWithAutograd");function FP(r){return(e,...t)=>{let n=e.subtract(sc);return Nt.redispatch(r,n,...t)}}l(FP,"_makePassthrough");function AO(){let r=Nt.listOps(),e=[40,41,42,43];for(let t of r){let n=Nt.findOp(t);if(!n)continue;let s=n.name,o=vO(s)?$P(s,n):FP(n),i=wr.fromUnboxed(o);for(let a of e)n.entry.registerKernel(a,i)}}l(AO,"registerAutogradKernels");be();qr();var lp=Ft,zP=["neg","exp","log","sqrt","rsqrt","abs","sin","cos","tanh","erf","erfc","lgamma","gamma","sigmoid","relu","gelu","silu","sign","floor","ceil","clone","contiguous"],VP=["add","sub","mul","div","pow","remainder","maximum","minimum","eq","ne","lt","le","gt","ge","matmul","dot","flip","unsqueeze"],GP=["sum","mean","max","min","argmax","argmin","prod"],jP={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 qP(r,e){let t=[];for(let n=0;n<e.length;n++)t[n]=r[n]===void 0?e[n]:r[n];for(let n=e.length;n<r.length;n++)t[n]=r[n];return t}l(qP,"withDefaults");function UP(r){for(let e of zP)r[e]=function(){return lp[e](this)}}l(UP,"installSelfMethods");function KP(r){for(let e of VP)r[e]=function(t){return lp[e](this,t)}}l(KP,"installOneArgMethods");function WP(r){for(let[e,t]of Object.entries(jP))r[e]=function(...n){return lp[e](this,...qP(n,t))}}l(WP,"installDefaultedMethods");function HP(r){for(let e of GP)r[e]=function(t,n){return lp[e](this,t,n)}}l(HP,"installReductionMethods");function up(r){return r.length===1&&Array.isArray(r[0])?r[0]:r}l(up,"arrayArg");function OO(r){let e=r.prototype;e.to=function(t){if(this.device.equals(t))return this;let s=lp.contiguous(this).data.slice(0,this.numel);return ta(s,this.shape,this.dtype,{device:t})},UP(e),KP(e),WP(e),HP(e),e.mm=function(t){return lt(this,t)},e.requires_grad=function(t=!0){return this.requiresGrad_(t)},e.gather=function(t,n){return jd(this,t,n)},e.scatter_add=function(t,n,s){return ra(this,t,n,s)},e.scatter=function(t,n,s){return qd(this,t,n,s)},e.transpose=function(t,n){return gt(this,t,n)},e.slice=function(t,n,s,o){return Kd(this,t,n,s,o)},e.narrow=function(t,n,s){return Un(this,t,n,s)},e.select=function(t,n){return Ut(this,t,n)},e.reshape=function(...t){return Cn(this,up(t))},e.permute=function(...t){return Eu(this,up(t))},e.expand=function(...t){return Lu(this,up(t))},e.repeat=function(...t){return Du(this,up(t))},e.tile=function(...t){return Wd(this,up(t))},e.t=function(){if(this.ndim!==2)throw new Error("t() expects a 2D tensor");return gt(this,0,1)}}l(OO,"installOps");jn();jn();ar();sp();ga();Kt();P0();Kc();xw();$i();_t();_t();function pp(r,e){return{dtype:e?.dtype??r.dtype,device:e?.device??r.device,requiresGrad:e?.requiresGrad??!1}}l(pp,"_likeOpts");function QP(r,e){return fn(r.shape,pp(r,e))}l(QP,"emptyLike");function XP(r,e){return $e(r.shape,pp(r,e))}l(XP,"zerosLike");function e$(r,e){return Ht(r.shape,pp(r,e))}l(e$,"onesLike");function t$(r,e,t){return et(r.shape,e,pp(r,t))}l(t$,"fullLike");function n$(r,e){return Bm(r.shape,pp(r,e))}l(n$,"randnLike");qr();be();var $O={};nt($O,{AdaptiveAvgPool2d:()=>Rp,AvgPool2d:()=>Bp,BCELoss:()=>Lp,BatchNorm1d:()=>ul,BatchNorm2d:()=>Ap,Conv1d:()=>yp,Conv2d:()=>gp,CrossEntropyLoss:()=>Ip,Dropout:()=>pr,ELU:()=>kp,Embedding:()=>Cp,F:()=>PO,Flatten:()=>Vp,GELU:()=>bp,GRU:()=>Pp,GRUCell:()=>ll,GroupNorm:()=>Cg,LSTM:()=>$p,LSTMCell:()=>cl,LayerNorm:()=>Is,LeakyReLU:()=>Sp,Linear:()=>hn,LogSoftmax:()=>Np,MSELoss:()=>Mp,MaxPool2d:()=>Op,Module:()=>ge,ModuleDict:()=>zp,ModuleList:()=>di,MultiheadAttention:()=>mi,NLLLoss:()=>Ep,Parameter:()=>bt,PositionalEncoding:()=>jp,ReLU:()=>_p,Sequential:()=>Fp,SiLU:()=>wp,Sigmoid:()=>Tp,Softmax:()=>vp,Tanh:()=>xp,Transformer:()=>Gp,TransformerDecoder:()=>fl,TransformerDecoderLayer:()=>ba,TransformerEncoder:()=>pl,TransformerEncoderLayer:()=>_a,functional:()=>al,init:()=>Sg});jn();var bt=class extends We{static{l(this,"Parameter")}constructor(e,t=!0){e instanceof We?super(e._impl):super(e),t&&this.requiresGrad_(!0)}get isParameter(){return!0}};var ge=class r{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 bt))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 r))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 n=e?e+".":"";for(let[s,o]of this._parameters)o!==null&&(yield[n+s,o]);if(t)for(let[s,o]of this._modules)o!==null&&(yield*o.namedParameters(n+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[n,s]of this._modules)s!==null&&(yield*s.namedModules(t+n))}stateDict(e=""){this._autoDetect();let t=new Map,n=e?e+".":"";for(let[s,o]of this._parameters)o!==null&&t.set(n+s,o);for(let[s,o]of this._buffers)o!==null&&t.set(n+s,o);for(let[s,o]of this._modules)if(o!==null)for(let[i,a]of o.stateDict(n+s))t.set(i,a);return t}loadStateDict(e){this._autoDetect();for(let[t,n]of this._parameters){let s=t;if(e.has(s)&&n!==null){let o=e.get(s),i=n._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,n]of this._modules)if(n!==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&&n.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,n]of this._parameters){let s=n;n!==null&&typeof s?.to=="function"&&(this._parameters.set(t,new bt(s.to(e),n.requiresGrad)),this[t]=this._parameters.get(t))}for(let[t,n]of this._buffers){let s=n;n!==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 n=this[t];n instanceof bt&&!this._parameters.has(t)?this._parameters.set(t,n):n instanceof r&&!this._modules.has(t)&&this._modules.set(t,n)}}toString(){return this._buildRepr("")}_buildRepr(e){this._autoDetect();let t=this.constructor.name;if(this._modules.size===0)return t+"()";let n=[t+"("];for(let[s,o]of this._modules)o!==null&&n.push(e+" ("+s+"): "+o._buildRepr(e+" "));return n.push(e+")"),n.join(`
278
+ `)}};var Sg={};nt(Sg,{_calculateFanInFanOut:()=>ol,constant_:()=>o$,kaiming_normal_:()=>u$,kaiming_uniform_:()=>BO,normal_:()=>fp,ones_:()=>s$,resetLinearParameters:()=>il,uniform_:()=>xg,xavier_normal_:()=>a$,xavier_uniform_:()=>i$,zeros_:()=>r$});function ol(r){let e=r.shape,t=e.length;if(t<2)throw new Error("Fan in/out requires at least 2D tensor");let n=e[1],s=e[0],o=1;for(let i=2;i<t;i++)o*=e[i];return{fanIn:n*o,fanOut:s*o}}l(ol,"_calculateFanInFanOut");function il(r,e){if(BO(r,Math.sqrt(5)),e){let{fanIn:t}=ol(r),n=1/Math.sqrt(t);xg(e,-n,n)}}l(il,"resetLinearParameters");function xg(r,e=0,t=1){let n=r._impl.storage.data;if(!n)return r;let s=t-e;for(let o=0;o<n.length;o++)n[o]=e+Math.random()*s;return r._impl.bumpVersion(),r}l(xg,"uniform_");function fp(r,e=0,t=1){let n=r._impl.storage.data;if(!n)return r;for(let s=0;s<n.length;s+=2){let o=Math.random()||1e-10,i=Math.random(),a=Math.sqrt(-2*Math.log(o)),u=6.283185307179586*i;n[s]=e+t*a*Math.cos(u),s+1<n.length&&(n[s+1]=e+t*a*Math.sin(u))}return r._impl.bumpVersion(),r}l(fp,"normal_");function r$(r){let e=r._impl.storage.data;return e&&e.fill(0),r._impl.bumpVersion(),r}l(r$,"zeros_");function s$(r){let e=r._impl.storage.data;return e&&e.fill(1),r._impl.bumpVersion(),r}l(s$,"ones_");function o$(r,e){let t=r._impl.storage.data;return t&&t.fill(e),r._impl.bumpVersion(),r}l(o$,"constant_");function i$(r,e=1){let{fanIn:t,fanOut:n}=ol(r),s=e*Math.sqrt(2/(t+n)),o=Math.sqrt(3)*s;return xg(r,-o,o)}l(i$,"xavier_uniform_");function a$(r,e=1){let{fanIn:t,fanOut:n}=ol(r),s=e*Math.sqrt(2/(t+n));return fp(r,0,s)}l(a$,"xavier_normal_");function BO(r,e=0,t="fan_in",n="leaky_relu"){let{fanIn:s,fanOut:o}=ol(r),i=t==="fan_in"?s:o,u=RO(n,e)/Math.sqrt(i),p=Math.sqrt(3)*u;return xg(r,-p,p)}l(BO,"kaiming_uniform_");function u$(r,e=0,t="fan_in",n="leaky_relu"){let{fanIn:s,fanOut:o}=ol(r),i=t==="fan_in"?s:o,u=RO(n,e)/Math.sqrt(i);return fp(r,0,u)}l(u$,"kaiming_normal_");function RO(r,e=.01){switch(r){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(RO,"_calculateGain");var al={};nt(al,{elu:()=>q0,gelu:()=>dp,leaky_relu:()=>j0,log_softmax:()=>G0,relu:()=>hp,sigmoid:()=>F0,silu:()=>$0,softmax:()=>V0,tanh:()=>z0});be();_t();function hp(r){return Mu(r)}l(hp,"relu");function dp(r){return Dd(r)}l(dp,"gelu");function $0(r){return Pd(r)}l($0,"silu");function F0(r){return ur(r)}l(F0,"sigmoid");function z0(r){return Ns(r)}l(z0,"tanh");function V0(r,e=-1){return Ro(r,e)}l(V0,"softmax");function G0(r,e=-1){return Hd(r,e)}l(G0,"log_softmax");function j0(r,e=.01){let t=ee(r,et(r.shape,e,{dtype:r.dtype,device:r.device}));return ai(r,t)}l(j0,"leaky_relu");function q0(r,e=1){let t=et(r.shape,0,{dtype:r.dtype,device:r.device}),n=et(r.shape,1,{dtype:r.dtype,device:r.device}),s=et(r.shape,e,{dtype:r.dtype,device:r.device}),o=Iu(r,t),i=ee(s,Pe(Kr(r),n));return qn(o,r,i)}l(q0,"elu");var vg={};nt(vg,{batch_norm:()=>W0,group_norm:()=>K0,layer_norm:()=>U0});be();_t();ga();function U0(r,e,t,n,s=1e-5){if((r instanceof ft||r.isSymbolic)&&t&&n){let m=r.ndim-e.length;return pw(r,t,n,m,s)}let o=r.ndim-e.length,i=[];for(let m=o;m<r.ndim;m++)i.push(m);let a=r;for(let m=i.length-1;m>=0;m--)a=lr(a,i[m],!0);let u=Pe(r,a),c=ee(u,u);for(let m=i.length-1;m>=0;m--)c=lr(c,i[m],!0);let h=et([],s),f=$t(et([],1),ui(ae(c,h))),d=ee(u,f);return t&&(d=ee(d,t)),n&&(d=ae(d,n)),d}l(U0,"layer_norm");function K0(r,e,t,n,s=1e-5){let o=r.shape,i=o[0],a=o[1],u=o.slice(2),p=r.reshape([i,e,a/e,...u]),c=[];for(let w=2;w<p.ndim;w++)c.push(w);let h=p;for(let w=c.length-1;w>=0;w--)h=lr(h,c[w],!0);let f=Pe(p,h),m=ee(f,f);for(let w=c.length-1;w>=0;w--)m=lr(m,c[w],!0);let g=et([],s),y=$t(et([],1),ui(ae(m,g))),_=ee(f,y).reshape(o),b=[1,a,...u.map(()=>1)];return t&&(_=ee(_,t.reshape(b))),n&&(_=ae(_,n.reshape(b))),_}l(K0,"group_norm");var kg=1;function l$(r,e){let t=new Array(r).fill(1);return t[kg]=e,t}l(l$,"channelShape");function MO(r,e){let t=r;for(let n=e.length-1;n>=0;n--)t=lr(t,e[n],!0);return t}l(MO,"reduceMeanOver");function IO(r,e,t){let n=r.data;if(!n)return;let s=e.reshape([r.shape[0]]).toArray();for(let o=0;o<n.length;o++)n[o]=n[o]*(1-t)+s[o]*t}l(IO,"blendRunning");function W0(r,e,t,n,s,o=!0,i=1e-5,a=.1){let u=r instanceof ft||r.isSymbolic;if(!o)return fw(r,n,s,e,t,kg,i);let p=[];for(let y=0;y<r.ndim;y++)y!==kg&&p.push(y);let c=MO(r,p),h=Pe(r,c),f=MO(ee(h,h),p),d=$t(et([],1),ui(ae(f,et([],i)))),m=ee(h,d),g=l$(r.ndim,r.shape[kg]);return n&&(m=ee(m,n.reshape(g))),s&&(m=ae(m,s.reshape(g))),u||(e&&IO(e,c,a),t&&IO(t,f,a)),m}l(W0,"batch_norm");var Y0={};nt(Y0,{linear:()=>H0});be();be();ga();function H0(r,e,t){let n;r instanceof ft||r.isSymbolic?n=gt(e,0,1):n=gt(e,0,1);let s=lt(r,n);return t?ae(s,t):s}l(H0,"linear");var Ag={};nt(Ag,{conv1d:()=>J0,conv2d:()=>Ng});be();be();function Ng(r,e,t,n=[1,1],s=[[0,0],[0,0]],o=[1,1],i=1){let a=Array.isArray(n)?n:[n,n],u=c$(s),p=Array.isArray(o)?o:[o,o],c=hw(r,e,a,u,p,i);if(t){let h=Cn(t,[1,t.shape[0],1,1]);return ae(c,h)}return c}l(Ng,"conv2d");function J0(r,e,t,n=1,s=0,o=1,i=1){let a=Sr(r,2),u=Sr(e,2),p=[1,Array.isArray(n)?n[0]:n],c=[[0,0],p$(s)],h=[1,Array.isArray(o)?o[0]:o],f=Ng(a,u,null,p,c,h,i),d=Cu(f,2);if(t){let m=Cn(t,[1,t.shape[0],1]);return ae(d,m)}return d}l(J0,"conv1d");function c$(r){return typeof r=="number"?[[r,r],[r,r]]:Array.isArray(r)&&typeof r[0]=="number"?r.map(e=>[e,e]):r}l(c$,"_normalizePadding");function p$(r){return typeof r=="number"?[r,r]:r}l(p$,"_normalizePadding1d");var Bg={};nt(Bg,{adaptive_avg_pool2d:()=>Q0,avg_pool2d:()=>Og,max_pool2d:()=>Z0});be();function Z0(r,e,t,n=[[0,0],[0,0]]){let s=Array.isArray(e)?e:[e,e],o=t?Array.isArray(t)?t:[t,t]:s,i=EO(n);return Yd(r,"max",s,o,i)}l(Z0,"max_pool2d");function Og(r,e,t,n=[[0,0],[0,0]]){let s=Array.isArray(e)?e:[e,e],o=t?Array.isArray(t)?t:[t,t]:s,i=EO(n);return Yd(r,"avg",s,o,i)}l(Og,"avg_pool2d");function Q0(r,e){let[t,n]=Array.isArray(e)?e:[e,e],s=r.shape[2],o=r.shape[3],i=Math.floor(s/t),a=Math.floor(o/n);return Og(r,[i,a],[i,a],[[0,0],[0,0]])}l(Q0,"adaptive_avg_pool2d");function EO(r){if(typeof r=="number")return[[r,r],[r,r]];if(Array.isArray(r)&&typeof r[0]=="number"){let e=r.map(t=>[t,t]);return e.length===1?[e[0],e[0]]:e}return r}l(EO,"_normalizePadding");var X0={};nt(X0,{dropout:()=>mp});be();_t();function mp(r,e=.5,t=!0){if(!t||e===0)return r;if(e===1)return et(r.shape,0,{dtype:r.dtype,device:r.device});let n=f$(r.shape,1-e,r.dtype,r.device),s=et(r.shape,1/(1-e),{dtype:r.dtype,device:r.device});return ee(ee(r,n),s)}l(mp,"dropout");function f$(r,e,t,n){let s=fn(r,{dtype:t,device:n}),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(f$,"_bernoulliMask");var Mg={};nt(Mg,{binary_cross_entropy:()=>nT,cross_entropy:()=>tT,mse_loss:()=>eT,nll_loss:()=>Rg});be();_t();be();jr();function eT(r,e,t="mean"){let n=Pe(r,e),s=ee(n,n);return LO(s,t)}l(eT,"mse_loss");function h$(r,e){let t=d$(r,e),n=Pe(r,t),s=Kr(n),o=m$(s,e),i=Zs(o);return Pe(n,i)}l(h$,"_logSoftmaxAutograd");function d$(r,e){let t=e<0?r.ndim+e:e;return Qs(r,t,!0)}l(d$,"_dimMax");function m$(r,e){let t=e<0?r.ndim+e:e;return ht(r,t,!0)}l(m$,"_dimSum");function Rg(r,e,t="mean",n=null){let s=r.ndim-1,o=r.shape[s],i=Gd(e,o),a=ht(ee(r,i),s),u=a,p=null;if(n!==null){let h=Ut(i,s,n),f=ae(Ur(h),1);u=ee(a,f),p=ht(f)}let c=Ur(ht(u));if(t==="sum")return c;if(t==="mean")return p!==null?$t(c,p):$t(c,cn(e.shape));throw new Error(`nll_loss: unknown reduction '${t}'`)}l(Rg,"nll_loss");function tT(r,e,t="mean",n=null){let s=h$(r,-1);return Rg(s,e,t,n)}l(tT,"cross_entropy");function nT(r,e,t="mean"){let n=et(r.shape,1e-7,{dtype:r.dtype,device:r.device}),s=et(r.shape,1,{dtype:r.dtype,device:r.device}),o=Zs(ae(r,n)),i=Zs(ae(Pe(s,r),n)),a=Ur(ae(ee(e,o),ee(Pe(s,e),i)));return LO(a,t)}l(nT,"binary_cross_entropy");function LO(r,e){if(e==="mean")return lr(r);if(e==="sum")return ht(r);if(e==="none")return r;throw new Error(`Unknown reduction: ${e}`)}l(LO,"_reduce");var sT={};nt(sT,{embedding:()=>rT});be();function rT(r,e){let t=r.shape[r.shape.length-1],n=e.shape.reduce((o,i)=>o*i,1);return li(r,0,e.reshape([n])).reshape([...e.shape,t])}l(rT,"embedding");var iT={};nt(iT,{scaled_dot_product_attention:()=>oT});be();be();ga();ya();_t();function y$(r,e){if(r instanceof ft||r.isSymbolic)return Ro(r,e);let t=e<0?r.ndim+e:e,n=Qs(r,t,!0).detach(),s=Pe(r,n),o=Kr(s),i=ht(o,t,!0);return $t(o,i)}l(y$,"_softmax");function _$(r){let e=r.ndim;return r instanceof ft||r.isSymbolic?gt(r,e-2,e-1):gt(r,e-2,e-1)}l(_$,"_transposeLastTwo");function b$(r,e){let t=fn([r,e]),n=t._impl.storage.data,s=e-r;for(let o=0;o<r;o++)for(let i=0;i<e;i++)n[o*e+i]=i<=o+s?0:-1/0;return t}l(b$,"_generateCausalMask");function oT(r,e,t,n=null,s=0,o=!1,i=!1){let a=r.shape[r.ndim-1],u=r.shape[r.ndim-2],p=e.shape[e.ndim-2],c=ss();if(c&&!n&&!(s>0&&i)&&r instanceof ft&&e instanceof ft&&t instanceof ft&&r.ndim===4)return c.recordOp("scaled_dot_product_attention",[r,e,t],{scale:1/Math.sqrt(a),causal:o});let f=et([],1/Math.sqrt(a)),d=_$(e),m=lt(r,d);m=ee(m,f),o&&(m=ae(m,b$(u,p))),n&&(m=ae(m,n));let g=y$(m,-1);return s>0&&i&&(g=mp(g,s,!0)),lt(g,t)}l(oT,"scaled_dot_product_attention");_t();var hn=class extends ge{static{l(this,"Linear")}inFeatures;outFeatures;weight;bias;constructor(e,t,n=!0){super(),this.inFeatures=e,this.outFeatures=t,this.weight=new bt(fn([t,e])),this.bias=n?new bt($e([t])):null,this._resetParameters()}_resetParameters(){il(this.weight,this.bias)}forward(e){return H0(e,this.weight,this.bias)}};_t();var gp=class extends ge{static{l(this,"Conv2d")}inChannels;outChannels;kernelSize;stride;padding;dilation;groups;weight;bias;constructor(e,t,n,s={}){super(),this.inChannels=e,this.outChannels=t,this.kernelSize=Array.isArray(n)?n:[n,n],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 bt(fn([t,e/this.groups,o,i])),this.bias=s.bias!==!1?new bt($e([t])):null,this._resetParameters()}_resetParameters(){il(this.weight,this.bias)}forward(e){return Ng(e,this.weight,this.bias,this.stride,this.padding,this.dilation,this.groups)}},yp=class extends ge{static{l(this,"Conv1d")}inChannels;outChannels;kernelSize;stride;padding;dilation;groups;weight;bias;constructor(e,t,n,s={}){super(),this.inChannels=e,this.outChannels=t,this.kernelSize=Array.isArray(n)?n[0]:n,this.stride=s.stride??1,this.padding=s.padding??0,this.dilation=s.dilation??1,this.groups=s.groups??1,this.weight=new bt(fn([t,e/this.groups,this.kernelSize])),this.bias=s.bias!==!1?new bt($e([t])):null,il(this.weight,this.bias)}forward(e){return J0(e,this.weight,this.bias,this.stride,this.padding,this.dilation,this.groups)}};var _p=class extends ge{static{l(this,"ReLU")}forward(e){return hp(e)}},bp=class extends ge{static{l(this,"GELU")}forward(e){return dp(e)}},wp=class extends ge{static{l(this,"SiLU")}forward(e){return $0(e)}},Tp=class extends ge{static{l(this,"Sigmoid")}forward(e){return F0(e)}},xp=class extends ge{static{l(this,"Tanh")}forward(e){return z0(e)}},Sp=class extends ge{static{l(this,"LeakyReLU")}negativeSlope;constructor(e=.01){super(),this.negativeSlope=e}forward(e){return j0(e,this.negativeSlope)}},kp=class extends ge{static{l(this,"ELU")}alpha;constructor(e=1){super(),this.alpha=e}forward(e){return q0(e,this.alpha)}},vp=class extends ge{static{l(this,"Softmax")}dim;constructor(e=-1){super(),this.dim=e}forward(e){return V0(e,this.dim)}},Np=class extends ge{static{l(this,"LogSoftmax")}dim;constructor(e=-1){super(),this.dim=e}forward(e){return G0(e,this.dim)}};_t();var Is=class extends ge{static{l(this,"LayerNorm")}normalizedShape;eps;weight;bias;constructor(e,t=1e-5,n=!0){super(),this.normalizedShape=Array.isArray(e)?e:[e],this.eps=t,this.weight=n?new bt(Ht(this.normalizedShape)):null,this.bias=n?new bt($e(this.normalizedShape)):null}forward(e){return U0(e,this.normalizedShape,this.weight,this.bias,this.eps)}},Cg=class extends ge{static{l(this,"GroupNorm")}numGroups;numChannels;eps;weight;bias;constructor(e,t,n=1e-5,s=!0){super(),this.numGroups=e,this.numChannels=t,this.eps=n,this.weight=s?new bt(Ht([t])):null,this.bias=s?new bt($e([t])):null}forward(e){return K0(e,this.numGroups,this.weight,this.bias,this.eps)}},ul=class extends ge{static{l(this,"BatchNorm1d")}numFeatures;eps;weight;bias;runningMean;runningVar;constructor(e,t=1e-5,n=!0){super(),this.numFeatures=e,this.eps=t,this.weight=n?new bt(Ht([e])):null,this.bias=n?new bt($e([e])):null,this.runningMean=$e([e]),this.runningVar=Ht([e]),this.registerBuffer("runningMean",this.runningMean),this.registerBuffer("runningVar",this.runningVar)}forward(e){return W0(e,this.runningMean,this.runningVar,this.weight,this.bias,this.training,this.eps)}},Ap=class extends ul{static{l(this,"BatchNorm2d")}constructor(e,t=1e-5,n=!0){super(e,t,n)}};var Op=class extends ge{static{l(this,"MaxPool2d")}kernelSize;stride;padding;constructor(e,t,n=0){super(),this.kernelSize=Array.isArray(e)?e:[e,e],this.stride=t?Array.isArray(t)?t:[t,t]:this.kernelSize,this.padding=n}forward(e){return Z0(e,this.kernelSize,this.stride,this.padding)}},Bp=class extends ge{static{l(this,"AvgPool2d")}kernelSize;stride;padding;constructor(e,t,n=0){super(),this.kernelSize=Array.isArray(e)?e:[e,e],this.stride=t?Array.isArray(t)?t:[t,t]:this.kernelSize,this.padding=n}forward(e){return Og(e,this.kernelSize,this.stride,this.padding)}},Rp=class extends ge{static{l(this,"AdaptiveAvgPool2d")}outputSize;constructor(e){super(),this.outputSize=Array.isArray(e)?e:[e,e]}forward(e){return Q0(e,this.outputSize)}};var pr=class extends ge{static{l(this,"Dropout")}p;constructor(e=.5){super(),this.p=e}forward(e){return mp(e,this.p,this.training)}};var Mp=class extends ge{static{l(this,"MSELoss")}reduction;constructor(e="mean"){super(),this.reduction=e}forward(e,t){return eT(e,t,this.reduction)}},Ip=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 tT(e,t,this.reduction,this.ignoreIndex===-100?null:this.ignoreIndex)}},Ep=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 Rg(e,t,this.reduction,this.ignoreIndex===-100?null:this.ignoreIndex)}},Lp=class extends ge{static{l(this,"BCELoss")}reduction;constructor(e="mean"){super(),this.reduction=e}forward(e,t){return nT(e,t,this.reduction)}};_t();var Cp=class extends ge{static{l(this,"Embedding")}numEmbeddings;embeddingDim;weight;constructor(e,t){super(),this.numEmbeddings=e,this.embeddingDim=t,this.weight=new bt(fn([e,t])),fp(this.weight)}forward(e){return rT(this.weight,e)}};_t();be();be();ya();be();be();function w$(r){if(Array.isArray(r[0])){let e=r[0],t=new Array(e.length);for(let n=0;n<t.length;n++)t[n]=Wr(r.map(s=>s[n]),0);return t}return Wr(r,0)}l(w$,"stackSteps");function Dp(r,e,t){let n=Array.isArray(e),s=Array.isArray(t),o=s?t:[t],i=n?e:[e],a=ss();if(!a){let f=e,d=[],m=o[0].shape[0];for(let g=0;g<m;g++){let y=s?o.map(w=>Ut(w,0,g)):Ut(o[0],0,g),[_,b]=r(f,y);f=_,d.push(b)}return[f,w$(d)]}let u=!1,p=l((f,d)=>{let[m,g]=r(n?f:f[0],s?d:d[0]);return u=Array.isArray(g),[n?m:[m],u?g:[g]]},"step"),[c,h]=a.scan(o,i,p);return[n?c:c[0],u?h:h[0]]}l(Dp,"scan");ya();Kn();ar();var ll=class extends ge{static{l(this,"GRUCell")}inputSize;hiddenSize;x2h;h2h;constructor(e,t,n=!0){super(),this.inputSize=e,this.hiddenSize=t,this.x2h=new hn(e,3*t,n),this.h2h=new hn(t,3*t,n)}forward(e,t=null){let n=t!==null?t:$e([e.shape[0],this.hiddenSize],{device:e.device}),s=this.x2h.forward(e),o=this.h2h.forward(n),[i,a,u]=Bo(s,this.hiddenSize,-1),[p,c,h]=Bo(o,this.hiddenSize,-1),f=ur(ae(i,p)),d=ur(ae(a,c)),m=Ns(ae(u,ee(f,h)));return ae(m,ee(d,Pe(n,m)))}},Pp=class extends ge{static{l(this,"GRU")}inputSize;hiddenSize;numLayers;batchFirst;cells;constructor(e,t,n=1,s=!1,o=!0){super(),this.inputSize=e,this.hiddenSize=t,this.numLayers=n,this.batchFirst=s,this.cells=[];for(let i=0;i<n;i++){let a=new ll(i===0?e:t,t,o);this.cells.push(a),this.registerModule("cell_"+i,a)}}forward(e,t=null){let n=d0();if(n&&e.device.type==="gpu"&&!ss()){let c=this.batchFirst?e.transpose(0,1):e,h={inputSize:this.inputSize,hiddenSize:this.hiddenSize,seqLen:c.shape[0],batch:c.shape[1]},[f,d]=n(c,this.cells,h,t);return[this.batchFirst?f.transpose(0,1):f,d]}let s=Qc();if(s&&e.device.type==="webgpu"&&!ss()){let c=this.batchFirst?e.transpose(0,1):e,h={kind:"gru",inputSize:this.inputSize,hiddenSize:this.hiddenSize,seqLen:c.shape[0],batch:c.shape[1]},f=s(c,this.cells,h,t,null);if(f){let[d,m]=f;return[this.batchFirst?d.transpose(0,1):d,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 h=t!==null?Ut(t,0,c):$e([i,this.hiddenSize],{device:o.device}),f=this.cells[c],[d,m]=Dp((g,y)=>{let _=f.forward(y,g);return[_,_]},h,a);u.push(d),a=m}let p=a;return this.batchFirst&&(p=p.transpose(0,1)),[p,Wr(u,0)]}};_t();be();be();ya();Kn();ar();var cl=class extends ge{static{l(this,"LSTMCell")}inputSize;hiddenSize;x2h;h2h;constructor(e,t,n=!0){super(),this.inputSize=e,this.hiddenSize=t,this.x2h=new hn(e,4*t,n),this.h2h=new hn(t,4*t,n)}forward(e,t=null){let n=t!==null?t[0]:$e([e.shape[0],this.hiddenSize],{device:e.device}),s=t!==null?t[1]:$e([e.shape[0],this.hiddenSize],{device:e.device}),o=ae(this.x2h.forward(e),this.h2h.forward(n)),[i,a,u,p]=Bo(o,this.hiddenSize,-1),c=ur(i),h=ur(a),f=Ns(u),d=ur(p),m=ae(ee(h,s),ee(c,f));return[ee(d,Ns(m)),m]}},$p=class extends ge{static{l(this,"LSTM")}inputSize;hiddenSize;numLayers;batchFirst;cells;constructor(e,t,n=1,s=!1,o=!0){super(),this.inputSize=e,this.hiddenSize=t,this.numLayers=n,this.batchFirst=s,this.cells=[];for(let i=0;i<n;i++){let a=new cl(i===0?e:t,t,o);this.cells.push(a),this.registerModule("cell_"+i,a)}}forward(e,t=null){let n=h0();if(n&&e.device.type==="gpu"&&!ss()){let h=this.batchFirst?e.transpose(0,1):e,f={inputSize:this.inputSize,hiddenSize:this.hiddenSize,seqLen:h.shape[0],batch:h.shape[1]},[d,m,g]=n(h,this.cells,f,t?t[0]:null,t?t[1]:null);return[this.batchFirst?d.transpose(0,1):d,[m,g]]}let s=Qc();if(s&&e.device.type==="webgpu"&&!ss()){let h=this.batchFirst?e.transpose(0,1):e,f={inputSize:this.inputSize,hiddenSize:this.hiddenSize,seqLen:h.shape[0],batch:h.shape[1]},d=s(h,this.cells,f,t?t[0]:null,t?t[1]:null);if(d){let[m,g,y]=d;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 h=0;h<this.numLayers;h++){let f=t!==null?Ut(t[0],0,h):$e([i,this.hiddenSize],{device:o.device}),d=t!==null?Ut(t[1],0,h):$e([i,this.hiddenSize],{device:o.device}),m=this.cells[h],[[g,y],_]=Dp((b,w)=>{let[T,S]=m.forward(w,b);return[[T,S],T]},[f,d],a);u.push(g),p.push(y),a=_}let c=a;return this.batchFirst&&(c=c.transpose(0,1)),[c,[Wr(u,0),Wr(p,0)]]}};var Fp=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 n=0;n<this._length;n++)t=this[String(n)].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}},di=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()")}},zp=class extends ge{static{l(this,"ModuleDict")}_dict;constructor(e){if(super(),this._dict=new Map,e)for(let[t,n]of Object.entries(e))this._dict.set(t,n),this.registerModule(t,n)}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 Vp=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,n=t.length,s=this.startDim<0?n+this.startDim:this.startDim,o=this.endDim<0?n+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<n;u++)a.push(t[u]);return Cn(e,a)}};be();_t();_t();function DO(r){return r==="gelu"?dp:hp}l(DO,"_getActivation");var mi=class extends ge{static{l(this,"MultiheadAttention")}embedDim;numHeads;headDim;batchFirst;dropout;qProj;kProj;vProj;outProj;constructor(e,t,n=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=n,this.qProj=new hn(e,e,s),this.kProj=new hn(o??e,e,s),this.vProj=new hn(i??e,e,s),this.outProj=new hn(e,e,s)}forward(e,t,n,s=null,o=null,i=!1){this.batchFirst||(e=e.transpose(0,1),t=t.transpose(0,1),n=n.transpose(0,1));let a=e.shape[0],u=e.shape[1],p=t.shape[1],c=this.qProj.forward(e),h=this.kProj.forward(t),f=this.vProj.forward(n);if(c=c.reshape([a,u,this.numHeads,this.headDim]).permute([0,2,1,3]),h=h.reshape([a,p,this.numHeads,this.headDim]).permute([0,2,1,3]),f=f.reshape([a,p,this.numHeads,this.headDim]).permute([0,2,1,3]),o){let g=et(o.shape,-1/0),y=et(o.shape,0),_=qn(o,g,y);_=_.unsqueeze(1).unsqueeze(2),s=s?ae(s,_):_}let d=oT(c,h,f,s,this.dropout,i,this.training);d=d.permute([0,2,1,3]).reshape([a,u,this.embedDim]);let m=this.outProj.forward(d);return this.batchFirst||(m=m.transpose(0,1)),m}},_a=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,n=2048,s=.1,o="relu",i=1e-5,a=!0,u=!1){super(),this.selfAttn=new mi(e,t,s,!0,null,null,a),this.linear1=new hn(e,n),this.linear2=new hn(n,e),this.norm1=new Is(e,i),this.norm2=new Is(e,i),this.dropout1=new pr(s),this.dropout2=new pr(s),this.dropoutFFN=new pr(s),this._activation=DO(o),this._activationName=o,this.normFirst=u,this._dModel=e,this._nhead=t,this._dimFeedforward=n,this._dropout=s,this._layerNormEps=i,this._batchFirst=a}forward(e,t=null,n=null,s=!1){return this.normFirst?this._forwardPreNorm(e,t,n,s):this._forwardPostNorm(e,t,n,s)}_forwardPostNorm(e,t,n,s){let o=this.selfAttn.forward(e,e,e,t,n,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,n,s){let o=this.norm1.forward(e),i=this.selfAttn.forward(o,o,o,t,n,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}},ba=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,n=2048,s=.1,o="relu",i=1e-5,a=!0,u=!1){super(),this.selfAttn=new mi(e,t,s,!0,null,null,a),this.crossAttn=new mi(e,t,s,!0,null,null,a),this.linear1=new hn(e,n),this.linear2=new hn(n,e),this.norm1=new Is(e,i),this.norm2=new Is(e,i),this.norm3=new Is(e,i),this.dropout1=new pr(s),this.dropout2=new pr(s),this.dropout3=new pr(s),this.dropoutFFN=new pr(s),this._activation=DO(o),this._activationName=o,this.normFirst=u,this._dModel=e,this._nhead=t,this._dimFeedforward=n,this._dropout=s,this._layerNormEps=i,this._batchFirst=a}forward(e,t,n=null,s=null,o=null,i=null,a=!1){return this.normFirst?this._forwardPreNorm(e,t,n,s,o,i,a):this._forwardPostNorm(e,t,n,s,o,i,a)}_forwardPostNorm(e,t,n,s,o,i,a){let u=this.selfAttn.forward(e,e,e,n,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,n,s,o,i,a){let u=this.norm1.forward(e),p=this.selfAttn.forward(u,u,u,n,o,a);p=ae(e,this.dropout1.forward(p));let c=this.norm2.forward(p),h=this.crossAttn.forward(c,t,t,s,i);p=ae(p,this.dropout2.forward(h));let f=this._activation(this.linear1.forward(this.norm3.forward(p)));return f=this.linear2.forward(this.dropoutFFN.forward(f)),p=ae(p,this.dropout3.forward(f)),p}};function T$(r){return new _a(r._dModel,r._nhead,r._dimFeedforward,r._dropout,r._activationName,r._layerNormEps,r._batchFirst,r.normFirst)}l(T$,"_cloneEncoderLayer");function x$(r){return new ba(r._dModel,r._nhead,r._dimFeedforward,r._dropout,r._activationName,r._layerNormEps,r._batchFirst,r.normFirst)}l(x$,"_cloneDecoderLayer");var pl=class extends ge{static{l(this,"TransformerEncoder")}layers;norm;constructor(e,t,n=null){super(),this.layers=new di(Array.from({length:t},()=>T$(e))),this.norm=n}forward(e,t=null,n=null,s=!1){let o=e;for(let i of this.layers)o=i.forward(o,t,n,s);return this.norm&&(o=this.norm.forward(o)),o}},fl=class extends ge{static{l(this,"TransformerDecoder")}layers;norm;constructor(e,t,n=null){super(),this.layers=new di(Array.from({length:t},()=>x$(e))),this.norm=n}forward(e,t,n=null,s=null,o=null,i=null,a=!1){let u=e;for(let p of this.layers)u=p.forward(u,t,n,s,o,i,a);return this.norm&&(u=this.norm.forward(u)),u}},Gp=class extends ge{static{l(this,"Transformer")}encoder;decoder;dModel;constructor({dModel:e=512,nhead:t=8,numEncoderLayers:n=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 h=new _a(e,t,o,i,a,c,u,p),f=new ba(e,t,o,i,a,c,u,p);this.encoder=new pl(h,n),this.decoder=new fl(f,s),this.dModel=e}forward(e,t,n=null,s=null,o=null,i=null,a=null,u=null){let p=this.encoder.forward(e,n,i);return this.decoder.forward(t,p,s,o,a,u)}static generateSquareSubsequentMask(e){let t=fn([e,e]),n=t._impl.storage.data;for(let s=0;s<e;s++)for(let o=0;o<e;o++)n[s*e+o]=o<=s?0:-1/0;return t}};be();_t();var jp=class extends ge{static{l(this,"PositionalEncoding")}dropoutLayer;pe;constructor(e,t=5e3,n=.1){super(),this.dropoutLayer=new pr(n);let s=$e([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],n=this.pe.narrow(1,0,t);return this.dropoutLayer.forward(ae(e,n))}};var PO={...al,...vg,...Y0,...Ag,...Bg,...X0,...Mg,...sT,...iT};var FO={};nt(FO,{BatchSampler:()=>Sa,DataLoader:()=>Kp,Dataset:()=>wa,MapDataset:()=>Up,RandomSampler:()=>xa,Sampler:()=>gi,SequentialSampler:()=>Ta,TensorDataset:()=>qp,defaultCollate:()=>ka});be();var wa=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)}},qp=class extends wa{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 n=1;n<e.length;n++)if(e[n].shape[0]!==t)throw new Error(`Size mismatch at dim 0: tensor 0 has ${t}, tensor ${n} has ${e[n].shape[0]}`);this._tensors=e,this._length=t}get length(){return this._length}get(e){let t=new Array(this._tensors.length);for(let n=0;n<this._tensors.length;n++)t[n]=Ut(this._tensors[n],0,e);return t}},Up=class extends wa{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 gi=class{static{l(this,"Sampler")}*[Symbol.iterator](){throw new Error("Subclass must implement [Symbol.iterator]()")}},Ta=class extends gi{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}},xa=class extends gi{static{l(this,"RandomSampler")}_dataSource;constructor(e){super(),this._dataSource=e}*[Symbol.iterator](){let e=this._dataSource.length,t=new Int32Array(e);for(let n=0;n<e;n++)t[n]=n;for(let n=e-1;n>0;n--){let s=Math.floor(Math.random()*(n+1)),o=t[n];t[n]=t[s],t[s]=o}for(let n=0;n<e;n++)yield t[n]}},Sa=class extends gi{static{l(this,"BatchSampler")}_dropLast;_sampler;_batchSize;constructor(e,t,n=!1){super(),this._sampler=e,this._batchSize=t,this._dropLast=n}*[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)}};jn();qr();ts();function S$(r,e=0){let t=r.length,n=r[0].shape,s=r[0].dtype,o=[...n];o.splice(e,0,t);let i=n.reduce((c,h)=>c*h,1),a=Dt(s),u=new a(t*i),p=u;for(let c=0;c<t;c++){let h=r[c],f=h.data,d=h._impl?h._impl.storageOffset:0;if(h.isContiguous)p.set(f.subarray(d,d+i),c*i);else{let m=h.shape,g=h.strides,y=h._impl.storage.data,_=m.length,b=new Int32Array(_),w=d;for(let T=0;T<i;T++){p[c*i+T]=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 ta(u,o,s)}l(S$,"stackTensors");function ka(r){let e=r[0];if(e instanceof We)return S$(r,0);if(typeof e=="number")return Pt(r);if(Array.isArray(e)){let t=new Array(e.length);for(let n=0;n<e.length;n++){let s=new Array(r.length);for(let o=0;o<r.length;o++)s[o]=r[o][n];t[n]=ka(s)}return t}if(e!==null&&typeof e=="object"&&e.constructor===Object){let t=Object.keys(e),n={};for(let s=0;s<t.length;s++){let o=t[s],i=new Array(r.length);for(let a=0;a<r.length;a++)i[a]=r[a][o];n[o]=ka(i)}return n}throw new Error(`defaultCollate: unsupported element type ${typeof e}`)}l(ka,"defaultCollate");var Kp=class{static{l(this,"DataLoader")}_dataset;_collate;_batchSampler;_batchSize;_dropLast;constructor(e,t={}){if(this._dataset=e,this._collate=t.collate??(n=>ka(n)),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 n=t.batchSize??1,s=t.dropLast??!1;this._batchSize=n,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 xa(e):new Ta(e);this._batchSampler=new Sa(o,n,s)}}get dataset(){return this._dataset}get length(){let e=this._dataset.length;if(this._batchSize==null){let t=0;for(let n 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 n=0;n<e.length;n++)t[n]=this._dataset.get(e[n]);yield this._collate(t)}}};var qO={};nt(qO,{DEFAULT_SPECIALS:()=>Vg,TOKENIZER_FORMAT:()=>ml,TOKENIZER_VERSION:()=>gl,Tokenizer:()=>Wp,Vocab:()=>yi});var yi=class r{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 n=this._tokenToId.get(e);return n===void 0?t:n}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 r,n=new Set;for(let s of e){if(typeof s!="string")throw new Error("mlfw tokenizer: vocab entries must be strings");if(n.has(s))throw new Error(`mlfw tokenizer: duplicate vocab token '${s}'`);n.add(s),t.add(s)}return t}};var Dg=class r{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 r({lowercase:e.lowercase??!1})}};var Pg=class r{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 r({lowercase:e.lowercase??!1})}};function $g(r,e){return r+""+e}l($g,"pairKey");var Fg=class r{static{l(this,"BpeStrategy")}_numMerges;_lowercase;_eow;_ranks;_encodeCache;constructor({numMerges:e=1e3,lowercase:t=!1,endOfWord:n="</w>"}={}){this._numMerges=e,this._lowercase=t,this._eow=n,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 n=new Map;for(let d of e)for(let m of this._pretokenize(d))n.set(m,(n.get(m)||0)+1);let s=new Map;for(let d of n.keys())s.set(d,this._baseSymbols(d));let o=new Map,i=new Map,a=[],u=l((d,m)=>d[0]>m[0]||d[0]===m[0]&&d[1]<m[1],"higher"),p=l((d,m)=>{a.push([d,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 d=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 T=a[_];a[_]=a[g],a[g]=T,g=_}}return d},"popHeap"),h=l(d=>{let m=s.get(d),g=n.get(d);for(let y=0;y+1<m.length;y++){let _=$g(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(d)}},"addWordPairs"),f=l(d=>{let m=s.get(d),g=n.get(d);for(let y=0;y+1<m.length;y++){let _=$g(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(d)}},"removeWordPairs");for(let d of n.keys())h(d);this._ranks=new Map;for(let d=0;d<this._numMerges;d++){let m=null,g=0;for(;a.length>0;){let[S,x]=c(),k=o.get(x);if(k===S&&k>0){m=x,g=k;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,d);let T=i.get(m);if(!T||T.size===0)break;for(let S of[...T]){f(S);let x=s.get(S),k=[];for(let A=0;A<x.length;A++)A+1<x.length&&x[A]===_&&x[A+1]===b?(k.push(w),A++):k.push(x[A]);s.set(S,k),h(S)}}}_encodeWord(e){let t=this._encodeCache.get(e);if(t!==void 0)return t;let n=this._baseSymbols(e);for(;n.length>1;){let s=1/0,o=-1;for(let i=0;i+1<n.length;i++){let a=this._ranks.get($g(n[i],n[i+1]));a!==void 0&&a<s&&(s=a,o=i)}if(o<0)break;n=n.slice(0,o).concat(n[o]+n[o+1],n.slice(o+2))}return this._encodeCache.set(e,n),n}segment(e){let t=[];for(let n of this._pretokenize(e))for(let s of this._encodeWord(n))t.push(s);return t}detokenize(e){let t="";for(let n of e)t+=n;return t.split(this._eow).join(" ").trim()}toJSON(){let e=[...this._ranks.entries()].map(([t])=>{let n=t.indexOf("");return[t.slice(0,n),t.slice(n+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 r({numMerges:e.numMerges??e.merges.length,lowercase:e.lowercase??!1,endOfWord:e.endOfWord??"</w>"});t._ranks=new Map;for(let n=0;n<e.merges.length;n++){let s=e.merges[n];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($g(s[0],s[1]),n)}return t}};qr();var fr=new Map,aT=new Set,dn={readFile(r){if(!fr.has(r))throw new Error("mlfw: file not found in browser memfs: "+r);return fr.get(r)},readBinary(r){if(!fr.has(r))throw new Error("mlfw: file not found in browser memfs: "+r);return fr.get(r)},writeFile(r,e){fr.set(r,e)},writeBinary(r,e){fr.set(r,e)},appendFile(r,e){fr.set(r,(fr.get(r)||"")+e)},exists(r){return fr.has(r)||aT.has(r)},mkdir(r){aT.add(r)},rename(r,e){if(!fr.has(r))throw new Error("mlfw: file not found in browser memfs: "+r);fr.set(e,fr.get(r)),fr.delete(r)},readdir(r){let e=r.endsWith("/")?r:r+"/",t=new Set;for(let n of[...fr.keys(),...aT])if(n.startsWith(e)){let s=n.slice(e.length).split("/")[0];s&&t.add(s)}return[...t]},remove(r){fr.delete(r)}};var ml="mlfw-tokenizer",gl=1,Vg=Object.freeze({pad:"<pad>",unk:"<unk>",bos:"<bos>",eos:"<eos>"}),zO=`${ml}-v${gl}`,zg={word:Dg,char:Pg,bpe:Fg},dl=Object.freeze(["pad","unk","bos","eos"]),Wp=class r{static{l(this,"Tokenizer")}_mode;_specials;_maxVocab;_strategy;_vocab;constructor(e={}){let t=e.mode??"word",n=zg[t];if(!n)throw new Error(`Unknown tokenizer mode '${t}'. Available: ${Object.keys(zg).join(", ")}`);this._mode=t,this._specials=VO(e.specialTokens),GO(this._specials),this._maxVocab=e.vocabSize??null,jO(this._maxVocab,this._specials),this._strategy=new n(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 n=new yi(Object.values(this._specials));if(this._maxVocab==null)for(let s of t)for(let o of this._strategy.segment(s))n.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-n.size;for(let a=0;a<o.length&&a<i;a++)n.add(o[a][0])}return this._vocab=n,this}toJSON(){let e=this._fitVocab(),t=this._strategy.toJSON();return{format:ml,version:gl,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";dn.writeFile(t,v$(this.toJSON())),dn.rename(t,e)}static load(e){if(typeof e!="string")throw new Error("mlfw tokenizer: load(path) requires a file path string");let t=dn.readFile(e),n=typeof t=="string"?t:new TextDecoder().decode(t);return r.fromJSON(N$(n))}static fromJSON(e){k$(e);let t=zg[e.mode],n=new r({mode:e.mode,vocabSize:e.config.vocabSize,specialTokens:e.specialTokens});n._strategy=t.fromJSON(e.strategy),n._vocab=yi.fromTokens(e.vocab);for(let s of Object.values(n._specials))if(!n._vocab.has(s))throw new Error(`mlfw tokenizer: special token '${s}' is missing from vocab`);return n}encode(e,t={}){let n=this._fitVocab(),s=[];t.addBos&&s.push(this.bosId);let o=this.unkId;for(let i of this._strategy.segment(e))s.push(n.getId(i,o));return t.addEos&&s.push(this.eosId),s}decode(e,t={}){let n=this._fitVocab(),s=t.skipSpecial??!0,o=new Set(Object.values(this._specials).map(a=>n.getId(a))),i=[];for(let a of e){if(s&&o.has(a))continue;let u=n.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],h=Math.min(c.length,o);for(let f=0;f<h;f++)u[p*o+f]=c[f]}return Pt(u,{shape:[a,o],dtype:"i32"})}};function VO(r){if(Array.isArray(r)){let e={...Vg};for(let t=0;t<r.length&&t<dl.length;t++)e[dl[t]]=r[t];return e}return{...Vg,...r??{}}}l(VO,"normalizeSpecials");function GO(r){let e=new Set;for(let t of dl){let n=r[t];if(typeof n!="string"||n.length===0)throw new Error(`mlfw tokenizer: special token '${t}' must be a non-empty string`);if(e.has(n))throw new Error(`mlfw tokenizer: duplicate special token '${n}'`);e.add(n)}}l(GO,"validateSpecials");function jO(r,e){if(r!=null&&(!Number.isInteger(r)||r<Object.keys(e).length))throw new Error(`mlfw tokenizer: vocabSize must be an integer >= ${Object.keys(e).length}`)}l(jO,"validateVocabSize");function k$(r){if(!r||typeof r!="object")throw new Error("mlfw tokenizer: artifact must be an object");let e=r;if(e.format!==ml)throw new Error("mlfw tokenizer: unrecognized tokenizer format");if(e.version!==gl)throw new Error(`mlfw tokenizer: unsupported tokenizer version ${e.version}`);if(typeof e.mode!="string"||!(e.mode in zg))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,n=VO(R$(e.specialTokens));if(GO(n),jO(M$(t.vocabSize),n),!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(k$,"validateTokenizerData");function v$(r){let e=[zO,va("m",[r.mode]),va("z",[r.config.vocabSize==null?"":String(r.config.vocabSize)]),va("s",dl.map(t=>r.specialTokens[t]))];if(r.mode==="bpe"){let t=[];for(let n of r.strategy.merges)t.push(n[0],n[1]);e.push(va("g",[String(r.strategy.lowercase?1:0),String(r.strategy.numMerges),r.strategy.endOfWord])),e.push(va("r",t))}else e.push(va("g",[String(r.strategy.lowercase?1:0)]));return e.push(va("v",r.vocab)),e.join(`
279
+ `)}l(v$,"serializeTokenizer");function N$(r){if(!r.startsWith(zO))throw new Error("mlfw tokenizer: unrecognized tokenizer format");let e=new Map;for(let p of r.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),A$(p.slice(c+1)))}let t=hl(e,"m")[0],n=hl(e,"z")[0],s=hl(e,"s"),o=hl(e,"g"),i=hl(e,"v"),a=Object.fromEntries(dl.map((p,c)=>[p,s[c]])),u={lowercase:o[0]==="1"};if(t==="bpe"){let p=hl(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:ml,version:gl,mode:t,config:{vocabSize:n===""?null:Number(n),...u},specialTokens:a,vocab:i,strategy:u}}l(N$,"parseTokenizer");function va(r,e){return`${r} ${e.map(O$).join(" ")}`}l(va,"record");function A$(r){return r.split(" ").map(B$)}l(A$,"parseRecord");function O$(r){return String(r).replace(/\\/g,"\\\\").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r")}l(O$,"escapeField");function B$(r){let e="";for(let t=0;t<r.length;t++){let n=r[t];if(n!=="\\"){e+=n;continue}let s=r[++t];s==="t"?e+=" ":s==="n"?e+=`
280
+ `:s==="r"?e+="\r":s==="\\"?e+="\\":e+=s??""}return e}l(B$,"unescapeField");function hl(r,e){let t=r.get(e);if(!t)throw new Error(`mlfw tokenizer: missing '${e}' record`);return t}l(hl,"requiredField");function R$(r){if(r==null)return;if(Array.isArray(r))return r.map(String);if(typeof r!="object")return;let e=r,t={};for(let n of dl){let s=e[n];typeof s=="string"&&(t[n]=s)}return t}l(R$,"asSpecialTokensInput");function M$(r){return r==null||typeof r=="number"?r:Number(r)}l(M$,"asNullableNumber");var kM={};nt(kM,{Adam:()=>Yp,AdamW:()=>Jp,CosineAnnealingLR:()=>Qp,FusedAdam:()=>p_,FusedOptimizer:()=>Pl,FusedSGD:()=>c_,GradScaler:()=>f_,LRScheduler:()=>Na,Optimizer:()=>Yr,ReduceLROnPlateau:()=>Xp,SGD:()=>Hp,StepLR:()=>Zp,clipGradNorm_:()=>ef,clipGradValue_:()=>tf});var Yr=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 n=I$(e);for(let s of n)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 n of t.params)n.grad!==null&&(e?n.grad=null:n.grad._impl.storage.data.fill(0))}stateDict(){let e=new Map;for(let[t,n]of this._state){let s={};for(let o of Object.keys(n)){let i=n[o];s[o]=UO(i)?KO(i):i}e.set(t,s)}return{state:e,paramGroups:this._paramGroups.map(t=>{let n={};for(let s of Object.keys(t)){if(s==="params")continue;let o=t[s];n[s]=Array.isArray(o)?[...o]:o}return n})}}loadStateDict(e){for(let t=0;t<this._paramGroups.length;t++){let n=e.paramGroups[t];for(let s of Object.keys(n))this._paramGroups[t][s]=Array.isArray(n[s])?[...n[s]]:n[s]}this._state=new Map;for(let[t,n]of e.state){let s={};for(let o of Object.keys(n)){let i=n[o];s[o]=UO(i)?KO(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 n=Array.isArray(e.params)?e.params:[...e.params];for(let s of n){if(this._paramIndex.has(s))throw new Error("Parameter appears in more than one parameter group");this._paramIndex.set(s,this._nextId++)}t.params=n,this._paramGroups.push(t)}_getParamId(e){return this._paramIndex.get(e)}_getState(e){let t=this._paramIndex.get(e),n=this._state.get(t);return n||(n={},this._state.set(t,n)),n}};function I$(r){if(r==null)throw new Error("Optimizer requires at least one parameter");let e=Array.isArray(r)?r:[...r];if(e.length===0)throw new Error("Optimizer got an empty parameter list");return E$(e[0])?e:[{params:e}]}l(I$,"normalizeParamGroups");function UO(r){return r instanceof Float32Array||r instanceof Float64Array||r instanceof Int32Array||r instanceof Int16Array||r instanceof Int8Array||r instanceof Uint8Array||r instanceof Uint16Array||r instanceof Uint32Array}l(UO,"isTypedArray");function KO(r){return r.slice()}l(KO,"cloneTypedArray");function E$(r){return typeof r=="object"&&r!==null&&"params"in r}l(E$,"isParamGroupInput");var Hp=class extends Yr{static{l(this,"SGD")}constructor(e,{lr:t=.01,momentum:n=0,dampening:s=0,weightDecay:o=0,nesterov:i=!1}={}){if(i&&(n===0||s!==0))throw new Error("Nesterov momentum requires momentum > 0 and dampening = 0");super(e,{lr:t,momentum:n,dampening:s,weightDecay:o,nesterov:i})}step(){for(let e of this._paramGroups){let t=e.lr,n=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,h=this._getState(a);if(n===0)if(o===0)for(let f=0;f<c;f++)u[f]-=t*p[f];else for(let f=0;f<c;f++)u[f]-=t*(p[f]+o*u[f]);else{let f=h.momentumBuffer;if(f){f=f;for(let d=0;d<c;d++){let m=o!==0?p[d]+o*u[d]:p[d];f[d]=n*f[d]+(1-s)*m}}else{f=new u.constructor(c);for(let d=0;d<c;d++)f[d]=o!==0?p[d]+o*u[d]:p[d];h.momentumBuffer=f}if(i)for(let d=0;d<c;d++){let m=o!==0?p[d]+o*u[d]:p[d];u[d]-=t*(m+n*f[d])}else for(let d=0;d<c;d++)u[d]-=t*f[d]}a._impl.bumpVersion()}}}};Kn();ar();var Yp=class extends Yr{static{l(this,"Adam")}constructor(e,{lr:t=.001,betas:n=[.9,.999],eps:s=1e-8,weightDecay:o=0,amsgrad:i=!1}={}){super(e,{lr:t,betas:[...n],eps:s,weightDecay:o,amsgrad:i})}step(){let e=m0();for(let t of this._paramGroups){let n=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 h=this._getState(c);if(e&&!a&&c.device&&c.device.type==="gpu"){h.step=(h.step||0)+1;let k=h.step,A=1-Math.pow(u,k),N=1-Math.pow(p,k);if(e(c,h,{beta1:u,beta2:p,omb1:1-u,omb2:1-p,eps:o,stepSize:n/A,bc2sqrt:Math.sqrt(N),wd:i,lr:n})){c._impl.bumpVersion();continue}}let f=c._impl.storage.data,d=c.grad._impl.storage.data,m=f.length,g=f.constructor;h.expAvg===void 0&&(h.step===void 0&&(h.step=0),h.expAvg=new g(m),h.expAvgSq=new g(m),a&&(h.maxExpAvgSq=new g(m))),h.step=h.step+1;let y=h.step,_=h.expAvg,b=h.expAvgSq,w=1-Math.pow(u,y),T=1-Math.pow(p,y),S=n/w,x=Math.sqrt(T);if(a){let k=h.maxExpAvgSq;for(let A=0;A<m;A++){let N=i!==0?d[A]+i*f[A]:d[A];_[A]=u*_[A]+(1-u)*N,b[A]=p*b[A]+(1-p)*N*N,b[A]>k[A]&&(k[A]=b[A]),f[A]-=S*_[A]/(Math.sqrt(k[A])/x+o)}}else for(let k=0;k<m;k++){let A=i!==0?d[k]+i*f[k]:d[k];_[k]=u*_[k]+(1-u)*A,b[k]=p*b[k]+(1-p)*A*A,f[k]-=S*_[k]/(Math.sqrt(b[k])/x+o)}c._impl.bumpVersion()}}}};var Jp=class extends Yr{static{l(this,"AdamW")}constructor(e,{lr:t=.001,betas:n=[.9,.999],eps:s=1e-8,weightDecay:o=.01,amsgrad:i=!1}={}){super(e,{lr:t,betas:[...n],eps:s,weightDecay:o,amsgrad:i})}step(){for(let e of this._paramGroups){let t=e.lr,n=e.betas,s=e.eps,o=e.weightDecay,i=e.amsgrad,[a,u]=n;for(let p of e.params){if(p.grad===null)continue;let c=p._impl.storage.data,h=p.grad._impl.storage.data,f=c.length,d=this._getState(p),m=c.constructor;d.step===void 0&&(d.step=0,d.expAvg=new m(f),d.expAvgSq=new m(f),i&&(d.maxExpAvgSq=new m(f))),d.step=d.step+1;let g=d.step,y=d.expAvg,_=d.expAvgSq,b=1-Math.pow(a,g),w=1-Math.pow(u,g),T=t/b,S=Math.sqrt(w);if(o!==0){let x=1-t*o;for(let k=0;k<f;k++)c[k]*=x}if(i){let x=d.maxExpAvgSq;for(let k=0;k<f;k++)y[k]=a*y[k]+(1-a)*h[k],_[k]=u*_[k]+(1-u)*h[k]*h[k],_[k]>x[k]&&(x[k]=_[k]),c[k]-=T*y[k]/(Math.sqrt(x[k])/S+s)}else for(let x=0;x<f;x++)y[x]=a*y[x]+(1-a)*h[x],_[x]=u*_[x]+(1-u)*h[x]*h[x],c[x]-=T*y[x]/(Math.sqrt(_[x])/S+s);p._impl.bumpVersion()}}}};var Na=class{static{l(this,"LRScheduler")}_optimizer;_baseLRs;_lastEpoch;_lastLR;constructor(e,t=-1){this._optimizer=e,this._baseLRs=e.paramGroups.map(n=>n.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 n=0;n<t.length;n++)t[n].lr=e[n]}},Zp=class extends Na{static{l(this,"StepLR")}_stepSize;_gamma;constructor(e,t,n=.1,s=-1){super(e,s),this._stepSize=t,this._gamma=n,this._init()}getLR(){let e=Math.pow(this._gamma,Math.floor(this._lastEpoch/this._stepSize));return this._baseLRs.map(t=>t*e)}},Qp=class extends Na{static{l(this,"CosineAnnealingLR")}_tMax;_etaMin;constructor(e,t,n=0,s=-1){super(e,s),this._tMax=t,this._etaMin=n,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)}},Xp=class{static{l(this,"ReduceLROnPlateau")}_optimizer;_mode;_factor;_patience;_threshold;_thresholdMode;_cooldown;_minLR;_eps;_best;_numBadEpochs;_cooldownCounter;constructor(e,{mode:t="min",factor:n=.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=n,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,n=Math.max(t*this._factor,this._minLR);t-n>this._eps&&(e.lr=n)}}};be();function WO(r){return(Array.isArray(r)?r:[...r]).filter(t=>t.grad!==null)}l(WO,"_gradParams");function ef(r,e,t=2){let n=WO(r);if(n.length===0)return 0;let s;if(t===2){let i=null;for(let a of n){let u=ht(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 n){let u=Qs(Pc(a.grad));i=i===null?u:ai(i,u)}s=Number(i.item())}else{let i=null;for(let a of n){let u=ht(Js(Pc(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 n)i.grad=ee(i.grad,o);return s}l(ef,"clipGradNorm_");function tf(r,e){for(let t of WO(r))t.grad=Vd(t.grad,-e,e)}l(tf,"clipGradValue_");es();Oe();l_();Ks();var Cs="f32",Mt=l(r=>new I([r],Cs),"VEC"),Dl=new I([],Cs);function us(r,e,t){return r.broadcast(e,[t],[]).getResult(0)}l(us,"bcast");var Pl=class extends Yr{static{l(this,"FusedOptimizer")}_target;_kernels;constructor(e,t,n=null){super(e,t),this._target=n||Us(),this._kernels=new Map}_kernel(e){let t=this._kernels.get(e);if(!t){let n=this._buildGraph(e);t=SM(n,this._target,{fusion:{enabled:!0}}),this._kernels.set(e,t)}return t}_buildGraph(e){throw new Error(`${this.constructor.name}._buildGraph() not implemented`)}},c_=class extends Pl{static{l(this,"FusedSGD")}constructor(e,{lr:t=.01,momentum:n=0,dampening:s=0,weightDecay:o=0,nesterov:i=!1}={},a=null){if(i&&(n===0||s!==0))throw new Error("Nesterov momentum requires momentum > 0 and dampening = 0");super(e,{lr:t,momentum:n,dampening:s,weightDecay:o,nesterov:i},a)}_buildGraph(e){let t=this._defaults.momentum,n=this._defaults.dampening,s=this._defaults.weightDecay,o=this._defaults.nesterov,i=t!==0,a=i?[Mt(e),Mt(e),Mt(e),Dl]:[Mt(e),Mt(e),Dl],u=i?[Mt(e),Mt(e)]:[Mt(e)];return hc("sgd_update",a,u,(c,h)=>{let f=h[0],d=h[1],m=i?h[3]:h[2],g=us(c,m,e),y=d;if(s!==0){let k=c.scalarConstant(s,Cs).getResult(0);y=c.add(d,c.mul(us(c,k,e),f).getResult(0)).getResult(0)}if(!i){let k=c.sub(f,c.mul(g,y).getResult(0)).getResult(0);c.returnOp([k]);return}let _=h[2],b=us(c,c.scalarConstant(t,Cs).getResult(0),e),w=us(c,c.scalarConstant(1-n,Cs).getResult(0),e),T=c.add(c.mul(b,_).getResult(0),c.mul(w,y).getResult(0)).getResult(0),S=T;o&&(S=c.add(y,c.mul(b,T).getResult(0)).getResult(0));let x=c.sub(f,c.mul(g,S).getResult(0)).getResult(0);c.returnOp([x,T])})}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 h=this._getState(i),f=h.momentumBuffer;f||(f=new a.constructor(p),h.momentumBuffer=f),f=f,c.run("sgd_update",a,u,f,o,a,f)}else c.run("sgd_update",a,u,o,a);i._impl.bumpVersion()}}}},p_=class extends Pl{static{l(this,"FusedAdam")}constructor(e,{lr:t=.001,betas:n=[.9,.999],eps:s=1e-8,weightDecay:o=0,amsgrad:i=!1}={},a=null){super(e,{lr:t,betas:[...n],eps:s,weightDecay:o,amsgrad:i},a)}_buildGraph(e){let t=this._defaults.betas,n=this._defaults.eps,s=this._defaults.weightDecay,o=this._defaults.amsgrad,[i,a]=t,u=o?[Mt(e),Mt(e),Mt(e),Mt(e),Mt(e),Dl,Dl]:[Mt(e),Mt(e),Mt(e),Mt(e),Dl,Dl],p=o?[Mt(e),Mt(e),Mt(e),Mt(e)]:[Mt(e),Mt(e),Mt(e)];return hc("adam_update",u,p,(c,h)=>{let f=h[0],d=h[1],m=h[2],g=h[3],y=o?h[4]:null,_=o?h[5]:h[4],b=o?h[6]:h[5],w=d;if(s!==0){let J=us(c,c.scalarConstant(s,Cs).getResult(0),e);w=c.add(d,c.mul(J,f).getResult(0)).getResult(0)}let T=us(c,c.scalarConstant(i,Cs).getResult(0),e),S=us(c,c.scalarConstant(a,Cs).getResult(0),e),x=us(c,c.scalarConstant(1-i,Cs).getResult(0),e),k=us(c,c.scalarConstant(1-a,Cs).getResult(0),e),A=us(c,c.scalarConstant(n,Cs).getResult(0),e),N=c.add(c.mul(T,m).getResult(0),c.mul(x,w).getResult(0)).getResult(0),v=c.mul(w,w).getResult(0),O=c.add(c.mul(S,g).getResult(0),c.mul(k,v).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),us(c,b,e)).getResult(0),A).getResult(0),z=c.mul(us(c,_,e),c.div(N,E).getResult(0)).getResult(0),D=c.sub(f,z).getResult(0);c.returnOp(o?[D,N,O,B]:[D,N,O])})}step(){for(let e of this._paramGroups){let t=e.lr,n=e.betas,s=e.amsgrad,[o,i]=n;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,h=this._getState(a);h.step===void 0&&(h.step=0,h.expAvg=new u.constructor(c),h.expAvgSq=new u.constructor(c),s&&(h.maxExpAvgSq=new u.constructor(c))),h.step=h.step+1;let f=h.step,d=1-Math.pow(o,f),m=1-Math.pow(i,f),g=new Float32Array([t/d]),y=new Float32Array([Math.sqrt(m)]),_=this._kernel(c),b=h.expAvg,w=h.expAvgSq;if(s){let T=h.maxExpAvgSq;_.run("adam_update",u,p,b,w,T,g,y,u,b,w,T)}else _.run("adam_update",u,p,b,w,g,y,u,b,w);a._impl.bumpVersion()}}}};var f_=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 n=0;n<t.length;n++)t[n]*=this._scale;return e._impl.bumpVersion&&e._impl.bumpVersion(),e}unscale_(e){if(!this.enabled)return!1;let t=1/this._scale,n=!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)||(n=!0),i[a]=u}}return this._unscaled.add(e),n&&(this._foundInf=!0),n}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)}};Sx();T_();Ks();var dI={};nt(dI,{Accuracy:()=>Uf,CSVLogger:()=>Vf,Callback:()=>$n,CallbackConnector:()=>Vl,ConfusionMatrix:()=>Yf,ConsoleLogger:()=>Ca,EarlyStopping:()=>Pf,EvaluationLoop:()=>Wl,F1Score:()=>Hf,FitLoop:()=>Kl,GradientAccumulationScheduler:()=>zf,HOOKS:()=>vx,LearningRateMonitor:()=>$f,LightningModule:()=>If,Logger:()=>$o,LoggerConnector:()=>Gl,MeanMetric:()=>Gf,Metric:()=>Yn,MetricAccumulator:()=>$l,MetricCollection:()=>qf,ModelCheckpoint:()=>Oi,Precision:()=>Kf,PredictionLoop:()=>Hl,ProgressCallback:()=>Ai,Recall:()=>Wf,SingleDeviceStrategy:()=>zl,Stage:()=>vi,SumMetric:()=>jf,Timer:()=>Ff,Trainer:()=>Df,TrainerState:()=>Fl,TrainingLoop:()=>ql,ValidationLoop:()=>Ul,applyCheckpoint:()=>Mx,deserializeCheckpoint:()=>N_,loadCheckpoint:()=>Rx,serializeCheckpoint:()=>Cf});var If=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:n=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:n!==null?n:u,onEpoch:s!==null?s:p,reduceFx:o,progBar:i})}logDict(e,t={}){for(let n of Object.keys(e))this.log(n,e[n],t)}manualBackward(e){this._trainer&&this._trainer.strategy?this._trainer.strategy.backward(e):e.backward()}};function JM(r){if(!r)throw new Error("configureOptimizers() returned null/undefined");if(Array.isArray(r)){let t=[],n=[];for(let s=0;s<r.length;s++){let o=r[s];c3(o)?(t.push(o.optimizer),n.push(YM(o.lrScheduler))):(t.push(o),n.push(null))}return{optimizers:t,schedulerConfigs:n}}let e=r;if(e.optimizer)return{optimizers:[e.optimizer],schedulerConfigs:[YM(e.lrScheduler)]};if(l3(r))return{optimizers:[r],schedulerConfigs:[null]};throw new Error("configureOptimizers() returned an unrecognized format")}l(JM,"parseOptimizersConfig");function YM(r){return r?"scheduler"in r&&r.scheduler?{scheduler:r.scheduler,interval:r.interval||"epoch",frequency:r.frequency||1,monitor:r.monitor||null}:ZM(r)?{scheduler:r,interval:"epoch",frequency:1,monitor:null}:null:null}l(YM,"normalizeSchedulerConfig");function ZM(r){return typeof r=="object"&&r!==null&&typeof r.step=="function"}l(ZM,"isSchedulerLike");function l3(r){return ZM(r)}l(l3,"isOptimizerLike");function c3(r){return typeof r=="object"&&r!==null&&"optimizer"in r}l(c3,"isOptimizerConfig");var vi=(o=>(o.IDLE="idle",o.TRAINING="training",o.VALIDATING="validating",o.TESTING="testing",o.PREDICTING="predicting",o))(vi||{}),p3={mean:l(r=>r.count===0?0:r.sum/r.count,"mean"),sum:l(r=>r.sum,"sum"),min:l(r=>r.min,"min"),max:l(r=>r.max,"max"),last:l(r=>r.last,"last")};function f3(){return{sum:0,count:0,min:1/0,max:-1/0,last:0}}l(f3,"createAccumulator");function h3(r,e){let t=typeof e=="number"?e:e.item();r.sum+=t,r.count+=1,t<r.min&&(r.min=t),t>r.max&&(r.max=t),r.last=t}l(h3,"updateAccumulator");var $l=class{static{l(this,"MetricAccumulator")}_accumulators;_reduceFns;constructor(){this._accumulators=new Map,this._reduceFns=new Map}update(e,t,n="mean"){this._accumulators.has(e)||(this._accumulators.set(e,f3()),this._reduceFns.set(e,n)),h3(this._accumulators.get(e),t)}compute(e){let t=this._accumulators.get(e);if(!t)return;let n=p3[this._reduceFns.get(e)];return n(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}},Fl=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 $l,this.epochMetrics=new $l}resetEpochMetrics(){this.epochMetrics.reset()}resetStepMetrics(){this.stepMetrics.reset()}},zl=class{static{l(this,"SingleDeviceStrategy")}device;constructor(){this.device=null}setup(e,t){this.device=t,(t.type==="gpu"||t.type==="webgpu")&&d3(e,"to")&&e.to(t)}toDevice(e){if(!this.device||this.device.type!=="gpu"&&this.device.type!=="webgpu")return e;if(m3(e))return e.to(this.device);if(Array.isArray(e)){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=this.toDevice(e[n]);return t}return e}backward(e){e.backward()}optimizerStep(e){e.step()}};function d3(r,e){return typeof r=="object"&&r!==null&&typeof r[e]=="function"}l(d3,"hasMethod");function m3(r){return typeof r=="object"&&r!==null&&"device"in r&&typeof r.to=="function"}l(m3,"hasDeviceTo");var vx=(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))(vx||{}),Vl=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 n=0;n<this._callbacks.length;n++){let s=this._callbacks[n][e];typeof s=="function"&&s.apply(this._callbacks[n],t)}}remove(e){let t=this._callbacks.indexOf(e);t!==-1&&this._callbacks.splice(t,1)}},Gl=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[n,s]of t){let{value:o,onStep:i,onEpoch:a,reduceFx:u,progBar:p}=s;if(a&&this._state.epochMetrics.update(n,o,u),i&&this._state.stepMetrics.update(n,o,u),p){this._state._progBarMetrics||(this._state._progBarMetrics=new Map);let c=typeof o=="number"?o:o.item();this._state._progBarMetrics.set(n,c)}}t.clear()}}flushStepMetrics(e){let t=this._state.stepMetrics.computeAll();if(Object.keys(t).length===0)return t;for(let n=0;n<this._loggers.length;n++)this._loggers[n].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 n=0;n<this._loggers.length;n++)this._loggers[n].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();Tm();sp();function Ni(r,e){return r==null?e:typeof r=="number"?r>0&&r<=1?Math.max(1,Math.round(r*e)):Math.min(r,e):e}l(Ni,"resolveLimit");async function jl(r){let{GradMode:e}=await Promise.resolve().then(()=>(sp(),_O)),t=e.isEnabled();e.setEnabled(!1);try{await r()}finally{e.setEnabled(t)}}l(jl,"noGradAsync");var ql=class{static{l(this,"TrainingLoop")}async run(e,t,n,s,o){let i=n.state,a=n.callbackConnector,u=n.loggerConnector,p=n.strategy,c=n.accumulateGradBatches,h=Ni(n.limitTrainBatches,t.length);i.numTrainingBatches=h,i.stage="training",e.train(),rl.setEnabled(!0),e.onTrainEpochStart(),a.dispatch("onTrainEpochStart",n,e);let f=0;for(let m of t){if(f>=h||i.shouldStop)break;if(i.maxSteps>0&&i.globalStep>=i.maxSteps){i.shouldStop=!0;break}let g=p.toDevice(m);a.dispatch("onTrainBatchStart",n,e,g,f);let y;e.automaticOptimization?y=await this._automaticStep(e,g,f,n,s,o,p,c,a):y=await Promise.resolve(e.trainingStep(g,f)),u.drain(e),i.globalStep%n.logEveryNSteps===0&&u.flushStepMetrics(i.globalStep),a.dispatch("onTrainBatchEnd",n,e,y,g,f),c0(),i.globalStep++,f++}c0();let d=u.flushEpochMetrics(i.globalStep);return this._stepEpochSchedulers(o,i.epoch),e.onTrainEpochEnd(),a.dispatch("onTrainEpochEnd",n,e),d}async _automaticStep(e,t,n,s,o,i,a,u,p){if(s.compile)return this._compiledStep(e,t,n,s,o,i,u);if(s.cudaGraph)return this._graphedStep(e,t,s,o,i,a);let c=await Promise.resolve(e.trainingStep(t,n)),h=c,f=c;if(oI(c)&&(h=c.loss,f=c),u>1&&(h=$t(h,u)),p.dispatch("onBeforeBackward",s,e,h),a.backward(h),p.dispatch("onAfterBackward",s,e),(n+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 f}async _compiledStep(e,t,n,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),Nx(c)&&(c=await c);else{let{compileWithBackward:_}=await Promise.resolve().then(()=>(T_(),KM)),{CPUTarget:b,CUDATarget:w,WebGPUTarget:T}=await Promise.resolve().then(()=>(Ks(),lv)),S=e._device&&e._device.type,x=S==="webgpu"?T():S==="gpu"?w():b();e.__compiledTrainStep=_({forward:p},u,{target:x,mode:s.compileMode});let k=e.log;e.log=()=>{};try{c=e.__compiledTrainStep(...u),Nx(c)&&(c=await c)}finally{e.log=k}}let h=e.__compiledTrainStep,f=h.capturedParams(),{ones:d}=await Promise.resolve().then(()=>(_t(),o1)),m=h.backward(d(c.shape));Nx(m)&&(m=await m);let g=m,y=g.length-f.length;for(let _=0;_<f.length;_++){let b=g[y+_];b&&(f[_].grad=b)}if((n+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,n,s,o){let i=await Promise.resolve(e.trainingStep(t,0)),a=i;if(oI(i)&&(a=i.loss),s.backward(a),o&&o.gradientClipVal){let{deviceClipGradNorm:u}=await Promise.resolve().then(()=>(Ad(),Nd));u([...e.parameters()],o.gradientClipVal)}for(let u=0;u<n.length;u++)s.optimizerStep(n[u]),n[u].zeroGrad();return a}async _graphedStep(e,t,n,s,o,i){let a=await Promise.resolve().then(()=>(XM(),QM)),u=await Promise.resolve().then(()=>(tI(),eI)),p=await Promise.resolve().then(()=>(sI(),rI)),c=e.__eagerGraphRunner;if(c||(c=e.__eagerGraphRunner={phase:"warmup",seen:0},u.setEagerDeferred(!0),EA(!0)),c.phase==="disabled")return this._eagerTrainStepCore(e,t,s,i,n);if(c.phase==="warmup"&&c.seen<n.cudaGraphWarmupSteps)return c.seen++,this._eagerTrainStepCore(e,t,s,i,n);let h=Ax(t);if(c.phase==="warmup"){c.inputs=h.map(y=>{let _=y._impl.storage.rawData;return u.deviceBufferForInput(_),u.pinResident(_),{dptr:u.deviceBufferDptr(_)}});let d=e.log,m;try{a.beginEagerCapture(),e.log=()=>{};try{m=await this._eagerTrainStepCore(e,t,s,i,n)}finally{e.log=d}c.captured=a.endEagerCapture()}catch(y){try{a.endEagerCapture()}catch{}return e.log=d,u.clearCapturePins(),c.phase="disabled",c.captureError=y&&typeof y=="object"&&"message"in y?y.message:y,this._eagerTrainStepCore(e,t,s,i,n)}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(n,g),this._stepStepSchedulers(o,n.state.globalStep),g}for(let d=0;d<c.inputs.length&&d<h.length;d++)c.inputs[d].dptr&&p.copyHostToDeviceAsync(c.inputs[d].dptr,h[d]._impl.storage.rawData);a.replay(c.exec),a.syncStream(),p.copyDeviceToHost(c.lossScratch,c.lossDptr);let f=c.lossScratch[0];return this._logGraphLoss(n,f),this._stepStepSchedulers(o,n.state.globalStep),f}_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 n=[...e.parameters()];t.gradientClipAlgorithm==="norm"?ef(n,t.gradientClipVal):tf(n,t.gradientClipVal)}_stepStepSchedulers(e,t){if(e)for(let n=0;n<e.length;n++){let s=e[n];!s||s.interval!=="step"||s.monitor||(t+1)%s.frequency===0&&s.scheduler.step()}}_stepEpochSchedulers(e,t){if(e)for(let n=0;n<e.length;n++){let s=e[n];!s||s.interval!=="epoch"||s.monitor||(t+1)%s.frequency===0&&s.scheduler.step()}}};function Ax(r,e=[]){if(r==null)return e;if(A3(r))return e.push(r),e;if(Array.isArray(r)){for(let t of r)Ax(t,e);return e}if(typeof r=="object"){let t=r;for(let n of Object.keys(t))Ax(t[n],e);return e}return e}l(Ax,"_flattenTensors");function Nx(r){return typeof r=="object"&&r!==null&&typeof r.then=="function"}l(Nx,"isThenable");function oI(r){return typeof r=="object"&&r!==null&&!("backward"in r)&&"loss"in r}l(oI,"isLossObject");function A3(r){return typeof r=="object"&&r!==null&&"shape"in r&&typeof r.contiguous=="function"}l(A3,"isFlattenTensor");var Ul=class{static{l(this,"ValidationLoop")}async run(e,t,n,s){let o=n.state,i=n.callbackConnector,a=n.loggerConnector,u=o.stage;o.stage="validating",e.eval(),e.onValidationEpochStart(),i.dispatch("onValidationStart",n,e),i.dispatch("onValidationEpochStart",n,e);let p=Ni(n.limitValBatches,t.length);o.numValBatches=p;let c=0;await jl(async()=>{for(let f of t){if(c>=p)break;i.dispatch("onValidationBatchStart",n,e,f,c);let d=await Promise.resolve(e.validationStep(f,c));a.drain(e),i.dispatch("onValidationBatchEnd",n,e,d,f,c),c++}});let h=a.flushEpochMetrics(o.globalStep);return this._stepPlateauSchedulers(s,h),e.onValidationEpochEnd(),i.dispatch("onValidationEpochEnd",n,e),i.dispatch("onValidationEnd",n,e),e.train(),o.stage=u,h}_stepPlateauSchedulers(e,t){if(e)for(let n=0;n<e.length;n++){let s=e[n];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 Kl=class{static{l(this,"FitLoop")}_trainingLoop;_validationLoop;constructor(){this._trainingLoop=new ql,this._validationLoop=new Ul}get trainingLoop(){return this._trainingLoop}get validationLoop(){return this._validationLoop}async run(e,t,n,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++)n&&this._shouldRunValidation(p,s)&&await this._validationLoop.run(e,n,s,i);u.dispatch("onTrainEnd",s,e)}_shouldRunValidation(e,t){let n=t.checkValEveryNEpoch;return(e+1)%n===0}};var Wl=class{static{l(this,"EvaluationLoop")}async run(e,t,n){let s=n.state,o=n.callbackConnector,i=n.loggerConnector,a=s.stage;s.stage="testing",e.eval(),e.onTestEpochStart(),o.dispatch("onTestStart",n,e),o.dispatch("onTestEpochStart",n,e);let u=Ni(n.limitTestBatches,t.length),p=0;await jl(async()=>{for(let h of t){if(p>=u)break;o.dispatch("onTestBatchStart",n,e,h,p);let f=await Promise.resolve(e.testStep(h,p));i.drain(e),o.dispatch("onTestBatchEnd",n,e,f,h,p),p++}});let c=i.flushEpochMetrics(s.globalStep);return e.onTestEpochEnd(),o.dispatch("onTestEpochEnd",n,e),o.dispatch("onTestEnd",n,e),e.train(),s.stage=a,c}};var Hl=class{static{l(this,"PredictionLoop")}async run(e,t,n){let s=n.state,o=n.callbackConnector,i=n.strategy,a=s.stage;s.stage="predicting",e.eval(),o.dispatch("onPredictStart",n,e);let u=[],p=Ni(n.limitTestBatches,t.length),c=0;return await jl(async()=>{for(let h of t){if(c>=p)break;let f=i.toDevice(h);o.dispatch("onPredictBatchStart",n,e,f,c);let d=await Promise.resolve(e.predictStep(f,c));await n._flushEagerInference(),u.push(d),o.dispatch("onPredictBatchEnd",n,e,d,f,c),c++}}),o.dispatch("onPredictEnd",n,e),e.train(),s.stage=a,u}};var $o=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 Ca=class extends $o{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 n=[`[step ${t}]`],s=Object.keys(e).sort();for(let o=0;o<s.length;o++){let i=e[s[o]];n.push(`${s[o]}: ${O3(i)}`)}console.log(n.join(" | "))}logHyperparams(e){let t=["[hyperparams]"],n=Object.keys(e).sort();for(let s=0;s<n.length;s++)t.push(`${n[s]}: ${e[n[s]]}`);console.log(t.join(" | "))}};function O3(r){return typeof r!="number"||Number.isInteger(r)?String(r):Math.abs(r)<.001&&r!==0?r.toExponential(3):r.toFixed(4)}l(O3,"formatNumber");var $n=class{static{l(this,"Callback")}setup(e,t,n){}teardown(e,t,n){}onFitStart(e,t){}onFitEnd(e,t){}onTrainStart(e,t){}onTrainEnd(e,t){}onTrainEpochStart(e,t){}onTrainEpochEnd(e,t){}onTrainBatchStart(e,t,n,s){}onTrainBatchEnd(e,t,n,s,o){}onValidationStart(e,t){}onValidationEnd(e,t){}onValidationEpochStart(e,t){}onValidationEpochEnd(e,t){}onValidationBatchStart(e,t,n,s){}onValidationBatchEnd(e,t,n,s,o){}onTestStart(e,t){}onTestEnd(e,t){}onTestBatchStart(e,t,n,s){}onTestBatchEnd(e,t,n,s,o){}onPredictStart(e,t){}onPredictEnd(e,t){}onPredictBatchStart(e,t,n,s){}onPredictBatchEnd(e,t,n,s,o){}onBeforeBackward(e,t,n){}onAfterBackward(e,t){}onBeforeOptimizerStep(e,t,n){}onBeforeZeroGrad(e,t,n){}onSaveCheckpoint(e,t,n){}onLoadCheckpoint(e,t,n){}};var B3=["\u258F","\u258E","\u258D","\u258C","\u258B","\u258A","\u2589"];function iI(r){globalThis.process.stdout.write(r)}l(iI,"stdoutWrite");var Ai=class extends $n{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 n=this._trainTotal(e);n&&this._render("Epoch",e.state.epoch+1,e.state.maxEpochs,0,n,e.state)}onTrainBatchEnd(e,t,n,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 n=this._trainTotal(e);n&&this._render("Epoch",e.state.epoch+1,e.state.maxEpochs,n,n,e.state)}onTrainEnd(e,t){this._active&&iI(`
281
+ `),this._active=!1,this._lastLen=0}onValidationEpochStart(e,t){this._valBatchCount=0,this._epochStartTime=Date.now()}onValidationBatchEnd(e,t,n,s,o){this._valBatchCount++;let i=this._valTotal(e);i&&this._render("Validation",null,null,this._valBatchCount,i,e.state)}onValidationEnd(e,t){let n=this._valTotal(e);n&&this._render("Validation",null,null,n,n,e.state)}_trainTotal(e){return e.state.numTrainingBatches??aI(e.limitTrainBatches)}_valTotal(e){return e.state.numValBatches??aI(e.limitValBatches)}_render(e,t,n,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}/${n}`:e,h=(Date.now()-this._epochStartTime)/1e3,f=h>0?s/h:0,d=f>0?(o-s)/f:0,m=`${uI(h)}<${uI(d)}, ${f.toFixed(2)}it/s`,g=this._formatProgBarMetrics(i),y=`${c}: ${u}%|${p}| ${s}/${o} [${m}${g}]`,_=Math.max(0,this._lastLen-y.length);iI("\r"+y+" ".repeat(_)),this._lastLen=y.length,this._active=!0}_bar(e){let t=this._barLength,n=e*t,s=Math.floor(n),o=Math.round((n-s)*8);if(o===8&&(s+=1,o=0),s>=t)return"\u2588".repeat(t);let i=o>0?B3[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 n=[];for(let[s,o]of t)n.push(`${s}=${R3(o)}`);return", "+n.join(", ")}};function aI(r){return r==null?null:typeof r=="number"&&r>1?r:null}l(aI,"resolveTotal");function uI(r){(!Number.isFinite(r)||r<0)&&(r=0);let e=Math.round(r),t=Math.floor(e/60),n=e%60;return`${String(t).padStart(2,"0")}:${String(n).padStart(2,"0")}`}l(uI,"fmtTime");function R3(r){return typeof r!="number"||Number.isInteger(r)?String(r):r.toFixed(4)}l(R3,"formatNum");function Fo(...r){return r.filter(e=>e!=null&&e!=="").join("/").replace(/\/+/g,"/")}l(Fo,"joinPath");jn();qr();ts();var cI="mlfw-ckpt-v1",Lf=8,M3={Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array};function I3(r){return ArrayBuffer.isView(r)&&!(r instanceof DataView)}l(I3,"isTypedArray");function Ox(r){return new Uint8Array(r.buffer,r.byteOffset,r.byteLength)}l(Ox,"byteView");function E3(r){return r.constructor.name}l(E3,"typedArrayName");function L3(r){return"$buf"in r&&typeof r.$buf=="number"}l(L3,"isBufferNode");function C3(r){return"$map"in r&&Array.isArray(r.$map)}l(C3,"isMapNode");function Cf(r){let e=[],t=k_(r,e),n=0;for(let u of e)u.meta.offset=n,u.meta.length=u.bytes.length,n+=u.bytes.length;let s={format:cI,tree:t,buffers:e.map(u=>u.meta)},o=new TextEncoder().encode(JSON.stringify(s)),i=new Uint8Array(Lf+o.length+n);new DataView(i.buffer).setBigUint64(0,BigInt(o.length),!0),i.set(o,Lf);let a=Lf+o.length;for(let u of e)i.set(u.bytes,a),a+=u.bytes.length;return i}l(Cf,"serializeCheckpoint");function N_(r){let e=r instanceof Uint8Array?r:new Uint8Array(r),t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=Number(t.getBigUint64(0,!0)),s=Lf+n,o=JSON.parse(new TextDecoder().decode(e.subarray(Lf,s)));if(o.format!==cI)throw new Error("mlfw: unrecognized checkpoint format: "+o.format);let i=o.buffers.map(a=>D3(a,e,s));return v_(o.tree,i)}l(N_,"deserializeCheckpoint");function k_(r,e){if(r instanceof We)return lI(e,{kind:"tensor",dtype:r.dtype,shape:r.shape},Ox(r._impl.storage.data));if(I3(r))return lI(e,{kind:"array",arrayType:E3(r)},Ox(r));if(r instanceof Map){let t=[];for(let[n,s]of r)t.push([n,k_(s,e)]);return{$map:t}}if(Array.isArray(r))return r.map(t=>k_(t,e));if(r&&typeof r=="object"){let t={},n=r;for(let s of Object.keys(n))t[s]=k_(n[s],e);return t}return r}l(k_,"encodeNode");function lI(r,e,t){let n=r.length;return r.push({meta:e,bytes:t}),{$buf:n}}l(lI,"pushBuffer");function D3(r,e,t){let n=t+r.offset,s=e.subarray(n,n+r.length),o=r.kind==="tensor"?Dt(r.dtype):M3[r.arrayType],i=r.kind==="tensor"?void 0:r.arrayType;if(!o)throw new Error("mlfw: unknown typed array in checkpoint: "+i);let a=new o(r.length/o.BYTES_PER_ELEMENT);return Ox(a).set(s),r.kind==="tensor"?ta(a,r.shape,r.dtype):a}l(D3,"decodeBuffer");function v_(r,e){if(r===null||typeof r!="object")return r;if(Array.isArray(r))return r.map(n=>v_(n,e));if(L3(r))return e[r.$buf];if(C3(r)){let n=new Map;for(let[s,o]of r.$map)n.set(s,v_(o,e));return n}let t={};for(let n of Object.keys(r))t[n]=v_(r[n],e);return t}l(v_,"decodeNode");var Bx=".ckpt",Oi=class extends $n{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:n=null,mode:s="min",saveTopK:o=1,saveLast:i=!0,everyNEpochs:a=1}={}){super(),this._dirpath=e,this._filename=t,this._monitor=n,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 n=e.state;if((n.epoch+1)%this._everyNEpochs!==0)return;this._ensureDir();let s=this._fillTemplate(n);if(this._saveLast){let c=Fo(this._dirpath,"last"+Bx);this._saveCheckpoint(t,e,c),this._lastModelPath=c}if(!this._monitor){let c=Fo(this._dirpath,s+Bx);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=n.epochMetrics.computeAll()[this._monitor];if(i===void 0)return;let a=Fo(this._dirpath,s+Bx),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,n=this._bestK.length;for(;t<n;){let s=t+n>>>1;this._compareFn(e,this._bestK[s].score)<0?n=s:t=s+1}return t}_updateBest(){this._bestK.length>0&&(this._bestModelPath=this._bestK[0].path)}_saveCheckpoint(e,t,n){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=n+".tmp";dn.writeBinary(i,Cf(s)),dn.rename(i,n)}_fillTemplate(e){return this._filename.replace("{epoch}",String(e.epoch)).replace("{step}",String(e.globalStep))}_ensureDir(){dn.exists(this._dirpath)||dn.mkdir(this._dirpath)}_tryDelete(e){try{dn.remove(e)}catch{}}};function Rx(r){return N_(dn.readBinary(r))}l(Rx,"loadCheckpoint");function Mx(r,e,t=[]){let n=r;if(n.modelState&&e.loadStateDict(n.modelState),n.optimizerStates){let s=Math.min(t.length,n.optimizerStates.length);for(let o=0;o<s;o++)t[o].loadStateDict(n.optimizerStates[o])}return r}l(Mx,"applyCheckpoint");ar();var Df=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:n="auto",precision:s="f32",callbacks:o=[],logger:i=!0,enableCheckpointing:a=!1,enableProgress:u=!0,gradientClipVal:p=null,gradientClipAlgorithm:c="norm",accumulateGradBatches:h=1,limitTrainBatches:f=null,limitValBatches:d=null,limitTestBatches:m=null,valCheckInterval:g=1,checkValEveryNEpoch:y=1,logEveryNSteps:_=50,deterministic:b=!1,fastDevRun:w=!1,defaultRootDir:T="./lightning_logs",compile:S=!1,compileMode:x="separate",cudaGraph:k=!1,cudaGraphWarmupSteps:A=3}={}){if(this._state=new Fl,this._state.maxEpochs=e,this._state.maxSteps=t,this._compile=S,this._compileMode=x,this._cudaGraph=k,this._cudaGraphWarmupSteps=A,this._accelerator=n,this._precision=s,this._gradientClipVal=p,this._gradientClipAlgorithm=c,this._accumulateGradBatches=h,this._limitTrainBatches=f,this._limitValBatches=d,this._limitTestBatches=m,this._valCheckInterval=g,this._checkValEveryNEpoch=y,this._logEveryNSteps=_,this._deterministic=b,this._defaultRootDir=T,w!==!1){let v=typeof w=="number"?w:1;this._limitTrainBatches=v,this._limitValBatches=v,this._limitTestBatches=v,this._state.maxEpochs=1}this._loggers=this._resolveLoggers(i),this._strategy=new zl,this._fitLoop=new Kl,this._evaluationLoop=new Wl,this._predictionLoop=new Hl;let N=[...o];u&&!N.some(v=>v instanceof Ai)&&N.push(new Ai),a&&!N.some(v=>v instanceof Oi)&&N.push(new Oi({dirpath:T})),this._callbackConnector=new Vl(N),this._loggerConnector=new Gl(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,n=null){this._model=e,e._trainer=this;let s=this._resolveDevice();this._guardEagerWebGPU(s,"fit",n!=null),this._guardCudaGraph(s,n!=null),e._device=s,await this._prepareDevice(s),this._strategy.setup(e,s);let{optimizers:o,schedulerConfigs:i}=JM(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,n,this,o,i),s===Od){let{teardownAfterFit:a}=await Promise.resolve().then(()=>(Ad(),Nd));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 n=this._resolveDevice();this._guardEagerWebGPU(n,"validate"),e._device=n,await this._prepareDevice(n),this._strategy.setup(e,n),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 n=this._resolveDevice();this._guardEagerWebGPU(n,"test"),e._device=n,await this._prepareDevice(n),this._strategy.setup(e,n),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 n=this._resolveDevice();return e._device=n,await this._prepareDevice(n),this._strategy.setup(e,n),await this._predictionLoop.run(e,t,this)}_resolveDevice(){return this._accelerator==="gpu"?Od:this._accelerator==="wasm"?sw:this._accelerator==="webgpu"?Ys:this._accelerator==="cpu"?ks:ks}_guardEagerWebGPU(e,t,n=!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(n)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(()=>(Kc(),Bw));await t()}else if(e.type==="webgpu"){let{preloadWebGPU:t}=await Promise.resolve().then(()=>(Kc(),Bw));this._webgpuMod=await t()}}async _flushEagerInference(){this._webgpuMod&&await this._webgpuMod.flushWebGPUEager()}_resolveLoggers(e){return e===!1||e===null?[]:e===!0?[new Ca]:Array.isArray(e)?e:[e]}_extractHyperparams(e,t){let n={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}_`:"";n[a+"optimizer"]=o.constructor.name,i.lr!==void 0&&(n[a+"lr"]=i.lr),i.weightDecay!==void 0&&(n[a+"weight_decay"]=i.weightDecay)}return n}};var Pf=class extends $n{static{l(this,"EarlyStopping")}_monitor;_patience;_mode;_minDelta;_checkOnTrainEpochEnd;_waitCount;_bestScore;_compareFn;constructor({monitor:e="val_loss",patience:t=3,mode:n="min",minDelta:s=0,checkOnTrainEpochEnd:o=!1}={}){super(),this._monitor=e,this._patience=t,this._mode=n,this._minDelta=s,this._checkOnTrainEpochEnd=o,this._waitCount=0,this._bestScore=null,this._compareFn=n==="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 n=e.state.epochMetrics.computeAll()[this._monitor];if(n!==void 0){if(this._bestScore===null||this._compareFn(n,this._bestScore)){this._bestScore=n,this._waitCount=0;return}this._waitCount++,this._waitCount>=this._patience&&(e.shouldStop=!0)}}reset(){this._waitCount=0,this._bestScore=null}};var $f=class extends $n{static{l(this,"LearningRateMonitor")}_logMomentum;_lrHistory;constructor({logMomentum:e=!1}={}){super(),this._logMomentum=e,this._lrHistory={}}get lrHistory(){return this._lrHistory}onTrainBatchStart(e,t,n,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 h=p.replace("lr","momentum");t.log(h,a[u].momentum,{onStep:!0,onEpoch:!1})}}}}};var Ff=class extends $n{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 zf=class extends $n{static{l(this,"GradientAccumulationScheduler")}_scheduling;_sortedEpochs;constructor({scheduling:e}){super(),this._scheduling=new Map;let t=Object.entries(e);for(let n=0;n<t.length;n++)this._scheduling.set(Number(t[n][0]),t[n][1]);this._sortedEpochs=[...this._scheduling.keys()].sort((n,s)=>n-s)}onTrainEpochStart(e,t){let n=e.state.epoch;this._scheduling.has(n)&&(e.accumulateGradBatches=this._scheduling.get(n))}getCurrentAccumulation(e){let t=1;for(let n=0;n<this._sortedEpochs.length&&this._sortedEpochs[n]<=e;n++)t=this._scheduling.get(this._sortedEpochs[n]);return t}};var Vf=class extends $o{static{l(this,"CSVLogger")}_saveDir;_flushInterval;_columns;_columnSet;_buffer;_filePath;_headerWritten;_version;constructor({saveDir:e="./lightning_logs",name:t="default",version:n=null,flushInterval:s=10}={}){super({name:t,version:n||0}),this._saveDir=e,this._flushInterval=s,this._columns=[],this._columnSet=new Set,this._buffer=[],this._filePath=null,this._headerWritten=!1,this._version=n}get logDir(){let e=this._version!==null?this._version:this._resolveVersion();return Fo(this._saveDir,this._name,`version_${e}`)}logMetrics(e,t){let n={step:t},s=Object.keys(e);for(let o=0;o<s.length;o++){let i=s[o];n[i]=e[i],this._columnSet.has(i)||(this._columnSet.add(i),this._columns.push(i),this._headerWritten=!1)}this._buffer.push(n),this._buffer.length>=this._flushInterval&&this._flush()}logHyperparams(e){this._ensureDir();let t=Fo(this.logDir,"hparams.json");dn.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(",");dn.writeFile(e,s+`
282
+ `),this._headerWritten=!0}let t=["step",...this._columns],n=[];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):"")}n.push(i.join(","))}dn.appendFile(e,n.join(`
283
+ `)+`
284
+ `),this._buffer.length=0}_getFilePath(){return this._filePath||(this._filePath=Fo(this.logDir,"metrics.csv")),this._filePath}_ensureDir(){let e=this.logDir;dn.exists(e)||dn.mkdir(e)}_resolveVersion(){if(this._version!==null)return this._version;let e=Fo(this._saveDir,this._name);if(!dn.exists(e))return this._version=0,0;let t=-1;try{let n=dn.readdir(e);for(let s=0;s<n.length;s++){let o=n[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 Yn=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 Gf=class extends Yn{static{l(this,"MeanMetric")}_sum;_count;constructor(){super(),this._sum=0,this._count=0}update(e,t=1){let n=typeof e=="number"?e:e.item();this._sum+=n*t,this._count+=t}compute(){return this._count===0?0:this._sum/this._count}reset(){super.reset(),this._sum=0,this._count=0}},jf=class extends Yn{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 qf=class{static{l(this,"MetricCollection")}_metrics;constructor(e={}){this._metrics=new Map;let t=Object.keys(e);for(let n=0;n<t.length;n++)this._metrics.set(t[n],e[t[n]])}add(e,t){return this._metrics.set(e,t),this}update(e,t){for(let[,n]of this._metrics)n.update(e,t)}compute(){let e={};for(let[t,n]of this._metrics)e[t]=n.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 Uf=class extends Yn{static{l(this,"Accuracy")}_task;_numClasses;_topK;_threshold;_correct;_total;constructor({task:e="multiclass",numClasses:t=null,topK:n=1,threshold:s=.5}={}){super(),this._task=e,this._numClasses=t,this._topK=n,this._threshold=s,this._correct=0,this._total=0}update(e,t){let n=e._impl.storage.data,s=t._impl.storage.data;this._task==="binary"?this._updateBinary(n,s):this._task==="multiclass"?this._updateMulticlass(e,t):this._task==="multilabel"&&this._updateMultilabel(n,s)}compute(){return this._total===0?0:this._correct/this._total}reset(){super.reset(),this._correct=0,this._total=0}_updateBinary(e,t){let n=t.length;for(let s=0;s<n;s++)(e[s]>=this._threshold?1:0)===t[s]&&this._correct++;this._total+=n}_updateMulticlass(e,t){let n=e.shape;if(n.length<2){let u=e._impl.storage.data,p=t._impl.storage.data,c=p.length;for(let h=0;h<c;h++)Math.round(u[h])===p[h]&&this._correct++;this._total+=c;return}let s=n[0],o=n[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 h=1;h<o;h++){let f=i[u*o+h];f>c&&(c=f,p=h)}p===a[u]&&this._correct++}else for(let u=0;u<s;u++){let p=P3(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 n=t.length;for(let s=0;s<n;s++)(e[s]>=this._threshold?1:0)===t[s]&&this._correct++;this._total+=n}};function P3(r,e,t,n){let s=[];for(let o=0;o<t;o++){let i=r[e+o];s.length<n?(s.push({val:i,idx:o}),s.length===n&&$3(s)):i>s[0].val&&(s[0]={val:i,idx:o},pI(s,0))}return s.map(o=>o.idx)}l(P3,"topKIndices");function $3(r){for(let e=(r.length>>>1)-1;e>=0;e--)pI(r,e)}l($3,"heapify");function pI(r,e){let t=r.length;for(;;){let n=e,s=2*e+1,o=2*e+2;if(s<t&&r[s].val<r[n].val&&(n=s),o<t&&r[o].val<r[n].val&&(n=o),n===e)break;let i=r[e];r[e]=r[n],r[n]=i,e=n}}l(pI,"siftDown");var Kf=class extends Yn{static{l(this,"Precision")}_task;_numClasses;_average;_tp;_fp;_support;constructor({task:e="multiclass",numClasses:t=2,average:n="macro"}={}){super(),this._task=e,this._numClasses=t,this._average=n,this._tp=new Int32Array(t),this._fp=new Int32Array(t),this._support=new Int32Array(t)}update(e,t){Ix(e,t,this._tp,this._fp,null,this._support,this._task,this._numClasses)}compute(){return z3(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)}},Wf=class extends Yn{static{l(this,"Recall")}_task;_numClasses;_average;_tp;_fn;_support;constructor({task:e="multiclass",numClasses:t=2,average:n="macro"}={}){super(),this._task=e,this._numClasses=t,this._average=n,this._tp=new Int32Array(t),this._fn=new Int32Array(t),this._support=new Int32Array(t)}update(e,t){Ix(e,t,this._tp,null,this._fn,this._support,this._task,this._numClasses)}compute(){return V3(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)}},Hf=class extends Yn{static{l(this,"F1Score")}_task;_numClasses;_average;_tp;_fp;_fn;_support;constructor({task:e="multiclass",numClasses:t=2,average:n="macro"}={}){super(),this._task=e,this._numClasses=t,this._average=n,this._tp=new Int32Array(t),this._fp=new Int32Array(t),this._fn=new Int32Array(t),this._support=new Int32Array(t)}update(e,t){Ix(e,t,this._tp,this._fp,this._fn,this._support,this._task,this._numClasses)}compute(){let e=fI(this._tp,this._fp,this._numClasses),t=hI(this._tp,this._fn,this._numClasses),n=new Float64Array(this._numClasses);for(let s=0;s<this._numClasses;s++){let o=e[s]+t[s];n[s]=o>0?2*e[s]*t[s]/o:0}return Ex(n,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 Ix(r,e,t,n,s,o,i,a){let u=r._impl.storage.data,p=e._impl.storage.data,c=p.length,h=r.shape.length>=2&&r.shape[1]>1;for(let f=0;f<c;f++){let d=p[f]|0,m;i==="binary"?m=u[f]>=.5?1:0:h?m=F3(u,f,a):m=u[f]|0,o[d]++,m===d?t&&t[d]++:(n&&n[m]++,s&&s[d]++)}}l(Ix,"updateConfusionCounters");function F3(r,e,t){let n=0,s=r[e*t];for(let o=1;o<t;o++){let i=r[e*t+o];i>s&&(s=i,n=o)}return n}l(F3,"argmaxRow");function fI(r,e,t){let n=new Float64Array(t);for(let s=0;s<t;s++){let o=r[s]+e[s];n[s]=o>0?r[s]/o:0}return n}l(fI,"computePrecisionPerClass");function hI(r,e,t){let n=new Float64Array(t);for(let s=0;s<t;s++){let o=r[s]+e[s];n[s]=o>0?r[s]/o:0}return n}l(hI,"computeRecallPerClass");function z3(r,e,t,n,s){let o=fI(r,e,n);return Ex(o,t,n,s)}l(z3,"computePrecision");function V3(r,e,t,n,s){let o=hI(r,e,n);return Ex(o,t,n,s)}l(V3,"computeRecall");function Ex(r,e,t,n){if(n==="none")return[...r];if(n==="micro"){let i=0,a=0;for(let u=0;u<t;u++)i+=r[u]*e[u],a+=e[u];return a>0?i/a:0}if(n==="weighted"){let i=0,a=0;for(let u=0;u<t;u++)a+=r[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+=r[i],s++);return s>0?o/s:0}l(Ex,"aggregate");var Yf=class extends Yn{static{l(this,"ConfusionMatrix")}_numClasses;_matrix;constructor({numClasses:e}){super(),this._numClasses=e,this._matrix=new Int32Array(e*e)}update(e,t){let n=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=G3(n,u,i):c=n[u]|0,this._matrix[p*i+c]++}}compute(){let e=this._numClasses,t=[];for(let n=0;n<e;n++){let s=new Array(e);for(let o=0;o<e;o++)s[o]=this._matrix[n*e+o];t.push(s)}return t}reset(){super.reset(),this._matrix.fill(0)}};function G3(r,e,t){let n=0,s=r[e*t];for(let o=1;o<t;o++){let i=r[e*t+o];i>s&&(s=i,n=o)}return n}l(G3,"argmaxRow");be();var mI={};nt(mI,{cholesky:()=>Dx,cov:()=>K3,det:()=>W3,eigh:()=>j3,inv:()=>q3,lstsq:()=>Zf,pinv:()=>U3,qr:()=>Cx,solve:()=>Jf,svd:()=>Lx});be();function Lx(r){let[e,t,n]=ot("svd",r);return{U:e,S:t,V:n}}l(Lx,"svd");function j3(r){let[e,t]=ot("eigh",r);return{values:e,vectors:t}}l(j3,"eigh");function Cx(r){let[e,t]=ot("qr",r);return{Q:e,R:t}}l(Cx,"qr");var Dx=l(r=>ot("cholesky",r),"cholesky"),q3=l(r=>ot("inv",r),"inv"),U3=l(r=>ot("pinv",r),"pinv"),K3=l(r=>ot("cov",r),"cov"),Jf=l((r,e)=>ot("solve",r,e),"solve"),Zf=l((r,e)=>ot("lstsq",r,e),"lstsq"),W3=l(r=>ot("det",r).item(),"det");var bI={};nt(bI,{DecisionTreeClassifier:()=>Qx,DecisionTreeRegressor:()=>Zx,ElasticNet:()=>A_,GaussianNB:()=>Jx,GradientBoostingClassifier:()=>nS,GradientBoostingRegressor:()=>tS,GridSearchCV:()=>sS,KFold:()=>I_,KMeans:()=>Wx,KNeighborsClassifier:()=>Hx,KNeighborsRegressor:()=>Yx,LabelEncoder:()=>$x,Lasso:()=>jx,LinearRegression:()=>Vx,LogisticRegression:()=>qx,MinMaxScaler:()=>zx,OneHotEncoder:()=>Fx,PCA:()=>Kx,RandomForestClassifier:()=>eS,RandomForestRegressor:()=>Xx,Ridge:()=>Gx,StandardScaler:()=>Px,TimeSeriesSplit:()=>rS,accuracy_score:()=>Ps,confusion_matrix:()=>J3,cross_val_score:()=>_I,mean_absolute_error:()=>Y3,mean_squared_error:()=>H3,r2_score:()=>Ds,train_test_split:()=>X3});var Px=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=tl(e);this.mean_=new Float64Array(t.cols),this.scale_=new Float64Array(t.cols);for(let n=0;n<t.cols;n++){let s=0;for(let a=0;a<t.rows;a++)s+=t.data[a*t.cols+n];s/=t.rows;let o=0;for(let a=0;a<t.rows;a++){let u=t.data[a*t.cols+n]-s;o+=u*u}o/=t.rows;let i=Math.sqrt(o);this.mean_[n]=this.withMean?s:0,this.scale_[n]=this.withStd&&i>0?i:1}return this._cols=t.cols,this}transform(e){let t=tl(e),n=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]-n[a])/s[a];return np(o,t.rows,t.cols,e.dtype)}fit_transform(e){return this.fit(e).transform(e)}inverse_transform(e){let t=tl(e),n=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]+n[a];return np(o,t.rows,t.cols,e.dtype)}},$x=class{static{l(this,"LabelEncoder")}classes_;_lookup;constructor(){this.classes_=null,this._lookup=null}fit(e){let t=zt(e),{classes:n}=rs(t.data,t.n);return this.classes_=n,this._lookup=new Map(n.map((s,o)=>[s,o])),this}transform(e){let t=zt(e),n=this._lookup,s=new Float64Array(t.n);for(let o=0;o<t.n;o++){let i=n.get(t.data[o]);if(i===void 0)throw new Error(`LabelEncoder: unseen label ${t.data[o]}`);s[o]=i}return B0(s,t.n,e.dtype)}fit_transform(e){let t=zt(e),{y:n,classes:s}=rs(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]=n[i];return B0(o,t.n,e.dtype)}inverse_transform(e){let t=zt(e),n=this.classes_;return Array.from({length:t.n},(s,o)=>n[Math.round(t.data[o])])}},Fx=class{static{l(this,"OneHotEncoder")}classes_;constructor(){this.classes_=null}fit(e){let t=zt(e);return this.classes_=rs(t.data,t.n).classes,this}transform(e){let t=zt(e),n=this.classes_,s=new Map(n.map((a,u)=>[a,u])),o=n.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 np(i,t.n,o,e.dtype)}fit_transform(e){return this.fit(e).transform(e)}},zx=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=tl(e);this.dataMin_=new Float64Array(t.cols),this.dataRange_=new Float64Array(t.cols);for(let n=0;n<t.cols;n++){let s=1/0,o=-1/0;for(let i=0;i<t.rows;i++){let a=t.data[i*t.cols+n];a<s&&(s=a),a>o&&(o=a)}this.dataMin_[n]=s,this.dataRange_[n]=o>s?o-s:1}return this}transform(e){let t=tl(e),[n,s]=this.featureRange,o=this.dataMin_,i=this.dataRange_,a=s-n,u=new Float64Array(t.rows*t.cols);for(let p=0;p<t.rows;p++)for(let c=0;c<t.cols;c++){let h=(t.data[p*t.cols+c]-o[c])/i[c];u[p*t.cols+c]=h*a+n}return np(u,t.rows,t.cols,e.dtype)}fit_transform(e){return this.fit(e).transform(e)}};function H3(r,e){let t=zt(r),n=zt(e),s=0;for(let o=0;o<t.n;o++){let i=t.data[o]-n.data[o];s+=i*i}return s/t.n}l(H3,"mean_squared_error");function Y3(r,e){let t=zt(r),n=zt(e),s=0;for(let o=0;o<t.n;o++)s+=Math.abs(t.data[o]-n.data[o]);return s/t.n}l(Y3,"mean_absolute_error");function Ds(r,e){let t=zt(r),n=zt(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]-n.data[a],p=t.data[a]-s;o+=u*u,i+=p*p}return i===0?0:1-o/i}l(Ds,"r2_score");function Ps(r,e){let t=zt(r),n=zt(e),s=0;for(let o=0;o<t.n;o++)t.data[o]===n.data[o]&&s++;return s/t.n}l(Ps,"accuracy_score");function J3(r,e){let t=zt(r),n=zt(e),s=0;for(let a=0;a<t.n;a++)s=Math.max(s,t.data[a],n.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]][n.data[a]]++;return i}l(J3,"confusion_matrix");be();_t();qr();function gI(r,e){return e?sa([r,Ht([r.shape[0],1],{device:r.device,dtype:r.dtype})],1):r}l(gI,"addBias");function Ux(r){return r.ndim===1?r.reshape([r.shape[0],1]):r}l(Ux,"asColumn");var Vx=class{static{l(this,"LinearRegression")}fitIntercept;weight_;constructor({fitIntercept:e=!0}={}){this.fitIntercept=e,this.weight_=null}fit(e,t){let n=gI(e,this.fitIntercept);return this.weight_=Zf(n,Ux(t)),this}predict(e){let t=gI(e,this.fitIntercept);return lt(t,this.weight_).reshape([e.shape[0]])}score(e,t){return Ds(t,this.predict(e))}},Gx=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 n=e.shape[1],s=Ux(t),o=e,i=s,a=null,u=null;this.fitIntercept&&(a=lr(e,[0],!0),u=lr(s,[0],!0),o=Pe(e,a),i=Pe(s,u));let p=o.transpose(0,1),c=ae(lt(p,o),ee(Rm(n,n,{device:e.device,dtype:e.dtype}),this.alpha));return this.coef_=Jf(c,lt(p,i)),this.intercept_=this.fitIntercept?Pe(u,lt(a,this.coef_)):$e([1,1],{device:e.device,dtype:e.dtype}),this}predict(e){return ae(lt(e,this.coef_),this.intercept_).reshape([e.shape[0]])}score(e,t){return Ds(t,this.predict(e))}},A_=class{static{l(this,"ElasticNet")}alpha;l1Ratio;fitIntercept;maxIter;tol;coef_;intercept_;constructor({alpha:e=1,l1Ratio:t=.5,fitIntercept:n=!0,maxIter:s=1e3,tol:o=1e-6}={}){this.alpha=e,this.l1Ratio=t,this.fitIntercept=n,this.maxIter=s,this.tol=o,this.coef_=null,this.intercept_=null}fit(e,t){let[n,s]=ot("elastic_net",e,Ux(t).reshape([e.shape[0]]),this.alpha,this.l1Ratio,this.maxIter,this.tol,this.fitIntercept);return this.coef_=n,this.intercept_=s,this}predict(e){let t=this.coef_.reshape([e.shape[1],1]);return ae(lt(e,t).reshape([e.shape[0]]),this.intercept_)}score(e,t){return Ds(t,this.predict(e))}},jx=class extends A_{static{l(this,"Lasso")}constructor({alpha:e=1,fitIntercept:t=!0,maxIter:n=1e3,tol:s=1e-6}={}){super({alpha:e,l1Ratio:1,fitIntercept:t,maxIter:n,tol:s})}},qx=class{static{l(this,"LogisticRegression")}C;lr;maxIter;W_;b_;classes_;constructor({C:e=1,lr:t=.5,maxIter:n=1e3}={}){this.C=e,this.lr=t,this.maxIter=n,this.W_=null,this.b_=null,this.classes_=null}fit(e,t){let n=e.shape[0],s=e.shape[1],o=zt(t),{y:i,classes:a}=rs(o.data,o.n);this.classes_=a;let u=a.length,p=new Float64Array(n*u);for(let y=0;y<n;y++)p[y*u+i[y]]=1;let c=Pt(p,{shape:[n,u],dtype:e.dtype,device:e.device}),h=$e([s,u],{device:e.device,dtype:e.dtype}),f=$e([1,u],{device:e.device,dtype:e.dtype}),d=1/this.C,m=e.transpose(0,1),g=this.lr/n;for(let y=0;y<this.maxIter;y++){let _=ae(lt(e,h),f),b=Pe(_,Qs(_,1,!0)),w=Kr(b),T=$t(w,ht(w,[1],!0)),S=Pe(T,c),x=ae(lt(m,S),ee(h,d)),k=ht(S,[0],!0);h=Pe(h,ee(x,g)),f=Pe(f,ee(k,g))}return this.W_=h,this.b_=f,this}decisionLogits(e){return ae(lt(e,this.W_),this.b_)}predict(e){let t=Ud(this.decisionLogits(e),1,!1).toArray(),n=new Float64Array(t.length),s=this.classes_;for(let o=0;o<t.length;o++)n[o]=s[t[o]];return Pt(n,{shape:[t.length],dtype:e.dtype,device:e.device})}score(e,t){return Ps(t,this.predict(e))}};be();var Kx=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],n=e.shape[1];this.mean_=lr(e,[0],!0);let s=Pe(e,this.mean_),{S:o,V:i}=Lx(s),a=i.shape[1],u=Math.min(this.nComponents??Math.min(t,n),a);this.components_=i.narrow(1,0,u),this._nc=u;let p=o.toArray(),c=t>1?t-1:1,h=0;for(let f=0;f<p.length;f++)h+=p[f]*p[f]/c;this.explainedVariance_=new Array(u),this.explainedVarianceRatio_=new Array(u);for(let f=0;f<u;f++){let d=p[f]*p[f]/c;this.explainedVariance_[f]=d,this.explainedVarianceRatio_[f]=h>0?d/h:0}return this}transform(e){return lt(Pe(e,this.mean_),this.components_)}fit_transform(e){return this.fit(e).transform(e)}inverse_transform(e){return ae(lt(e,this.components_.transpose(0,1)),this.mean_)}};be();var Wx=class{static{l(this,"KMeans")}nClusters;maxIter;nInit;randomState;clusterCenters_;labels_;inertia_;constructor({nClusters:e=8,maxIter:t=300,nInit:n=10,randomState:s=0}={}){this.nClusters=e,this.maxIter=t,this.nInit=n,this.randomState=s,this.clusterCenters_=null,this.labels_=null,this.inertia_=null}fit(e){let[t,n,s]=ot("kmeans",e,this.nClusters,this.maxIter,this.nInit,this.randomState);return this.clusterCenters_=t,this.labels_=n,this.inertia_=Number(s.item()),this}predict(e){return ot("kmeans_predict",e,this.clusterCenters_)}fit_predict(e){return this.fit(e),this.labels_}};be();var O_=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 ot("knn_predict",this._X,this._y,e,this.nNeighbors,this._classify)}},Hx=class extends O_{static{l(this,"KNeighborsClassifier")}constructor({nNeighbors:e=5}={}){super(e,!0)}score(e,t){return Ps(t,this.predict(e))}},Yx=class extends O_{static{l(this,"KNeighborsRegressor")}constructor({nNeighbors:e=5}={}){super(e,!1)}score(e,t){return Ds(t,this.predict(e))}};be();var Jx=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[n,s,o,i]=ot("gaussian_nb_fit",e,t);return this.means_=n,this.variances_=s,this.priors_=o,this.classes_=i,this}predict(e){return ot("gaussian_nb_predict",e,this.means_,this.variances_,this.priors_,this.classes_)}score(e,t){return Ps(t,this.predict(e))}};be();qr();var yI=1<<30;function M_(r,e,t,n,s){return ot("decision_tree_fit",r,e,t.maxDepth,t.minSamplesSplit,t.minSamplesLeaf,t.maxFeatures,n,s)}l(M_,"fitTree");function Yl(r,e){return ot("decision_tree_predict",r,e[0],e[1],e[2],e[3],e[4])}l(Yl,"treePredict");var B_=class{static{l(this,"BaseTree")}maxDepth;minSamplesSplit;minSamplesLeaf;maxFeatures;randomState;_classify;_nodes;constructor(e,t){this.maxDepth=e.maxDepth??yI,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=M_(e,t,this,this._classify,this.randomState),this}predict(e){return Yl(e,this._nodes)}},Zx=class extends B_{static{l(this,"DecisionTreeRegressor")}constructor(e={}){super(e,!1)}score(e,t){return Ds(t,this.predict(e))}},Qx=class extends B_{static{l(this,"DecisionTreeClassifier")}constructor(e={}){super(e,!0)}score(e,t){return Ps(t,this.predict(e))}};function Z3(r,e,t){return r>0?r:t?Math.max(1,Math.floor(Math.sqrt(e))):Math.max(1,Math.floor(e/3))}l(Z3,"defaultMaxFeatures");var R_=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??yI,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 n=e.shape[0],s=e.shape[1],o=cr(this.randomState),i=Z3(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(n);for(let f=0;f<n;f++)p[f]=Math.floor(o()*n);let c=ns(e,p),h=ns(t,p);this._trees.push(M_(c,h,a,this._classify,this.randomState+u+1))}return this}predict(e){let t=e.shape[0],n=this._trees.map(o=>Yl(e,o).toArray()),s=new Float64Array(t);if(this._classify)for(let o=0;o<t;o++){let i=new Map,a=n[0][o],u=0;for(let p=0;p<n.length;p++){let c=n[p][o],h=(i.get(c)||0)+1;i.set(c,h),h>u&&(u=h,a=c)}s[o]=a}else for(let o=0;o<t;o++){let i=0;for(let a=0;a<n.length;a++)i+=n[a][o];s[o]=i/n.length}return Pt(s,{shape:[t],dtype:e.dtype,device:e.device})}},Xx=class extends R_{static{l(this,"RandomForestRegressor")}constructor(e={}){super(e,!1)}score(e,t){return Ds(t,this.predict(e))}},eS=class extends R_{static{l(this,"RandomForestClassifier")}constructor(e={}){super(e,!0)}score(e,t){return Ps(t,this.predict(e))}},tS=class{static{l(this,"GradientBoostingRegressor")}nEstimators;learningRate;params;randomState;init_;_trees;constructor({nEstimators:e=100,learningRate:t=.1,maxDepth:n=3,minSamplesSplit:s=2,minSamplesLeaf:o=1,randomState:i=0}={}){this.nEstimators=e,this.learningRate=t,this.params={maxDepth:n,minSamplesSplit:s,minSamplesLeaf:o,maxFeatures:0},this.randomState=i,this.init_=0,this._trees=[]}fit(e,t){let n=zt(t),s=n.n,o=0;for(let a=0;a<s;a++)o+=n.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 f=0;f<s;f++)u[f]=n.data[f]-i[f];let p=Pt(u,{shape:[s],dtype:e.dtype,device:e.device}),c=M_(e,p,this.params,!1,this.randomState+a+1);this._trees.push(c);let h=Yl(e,c).toArray();for(let f=0;f<s;f++)i[f]+=this.learningRate*h[f]}return this}predict(e){let t=e.shape[0],n=new Float64Array(t).fill(this.init_);for(let s of this._trees){let o=Yl(e,s).toArray();for(let i=0;i<t;i++)n[i]+=this.learningRate*o[i]}return Pt(n,{shape:[t],dtype:e.dtype,device:e.device})}score(e,t){return Ds(t,this.predict(e))}},nS=class{static{l(this,"GradientBoostingClassifier")}nEstimators;learningRate;params;randomState;classes_;_stages;constructor({nEstimators:e=100,learningRate:t=.1,maxDepth:n=3,minSamplesSplit:s=2,minSamplesLeaf:o=1,randomState:i=0}={}){this.nEstimators=e,this.learningRate=t,this.params={maxDepth:n,minSamplesSplit:s,minSamplesLeaf:o,maxFeatures:0},this.randomState=i,this.classes_=null,this._stages=[]}fit(e,t){let n=zt(t),s=n.n,{y:o,classes:i}=rs(n.data,n.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=Q3(u,s,a),h=[];for(let f=0;f<a;f++){let d=new Float64Array(s);for(let _=0;_<s;_++)d[_]=(o[_]===f?1:0)-c[_*a+f];let m=Pt(d,{shape:[s],dtype:e.dtype,device:e.device}),g=M_(e,m,this.params,!1,this.randomState+p*a+f+1);h.push(g);let y=Yl(e,g).toArray();for(let _=0;_<s;_++)u[_*a+f]+=this.learningRate*y[_]}this._stages.push(h)}return this}predict(e){let t=e.shape[0],n=this.classes_,s=n.length,o=new Float64Array(t*s);for(let a of this._stages)for(let u=0;u<s;u++){let p=Yl(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]=n[u]}return Pt(i,{shape:[t],dtype:e.dtype,device:e.device})}score(e,t){return Ps(t,this.predict(e))}};function Q3(r,e,t){let n=new Float64Array(e*t);for(let s=0;s<e;s++){let o=-1/0;for(let a=0;a<t;a++)r[s*t+a]>o&&(o=r[s*t+a]);let i=0;for(let a=0;a<t;a++){let u=Math.exp(r[s*t+a]-o);n[s*t+a]=u,i+=u}for(let a=0;a<t;a++)n[s*t+a]/=i}return n}l(Q3,"softmaxRows");function X3(r,e,{testSize:t=.25,shuffle:n=!0,randomState:s=0}={}){let o=r.shape[0],i=n?S0(o,cr(s)):Array.from({length:o},(c,h)=>h),a=Math.max(1,Math.round(o*t)),u=i.slice(0,a),p=i.slice(a);return[ns(r,p),ns(r,u),ns(e,p),ns(e,u)]}l(X3,"train_test_split");var I_=class{static{l(this,"KFold")}nSplits;shuffle;randomState;constructor({nSplits:e=5,shuffle:t=!1,randomState:n=0}={}){this.nSplits=e,this.shuffle=t,this.randomState=n}split(e){let t=this.shuffle?S0(e,cr(this.randomState)):Array.from({length:e},(a,u)=>u),n=[],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));n.push({train:c,test:p}),i+=u}return n}},rS=class{static{l(this,"TimeSeriesSplit")}nSplits;constructor({nSplits:e=5}={}){this.nSplits=e}split(e){let t=Math.floor(e/(this.nSplits+1)),n=[];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);n.push({train:a,test:u})}return n}};function _I(r,e,t,{cv:n=5,scoring:s=null,shuffle:o=!0,randomState:i=0}={}){let a=new I_({nSplits:n,shuffle:o,randomState:i}).split(e.shape[0]),u=[];for(let{train:p,test:c}of a){let h=ns(e,p),f=ns(t,p),d=ns(e,c),m=ns(t,c),g=r().fit(h,f);u.push(s?s(m,g.predict(d)):g.score(d,m))}return u}l(_I,"cross_val_score");function eV(r){let e=Object.keys(r),t=[{}];for(let n of e){let s=[];for(let o of t)for(let i of r[n])s.push({...o,[n]:i});t=s}return t}l(eV,"cartesian");var sS=class{static{l(this,"GridSearchCV")}makeEstimator;paramGrid;cv;scoring;bestParams_;bestScore_;bestEstimator_;constructor(e,t,{cv:n=5,scoring:s=null}={}){this.makeEstimator=e,this.paramGrid=t,this.cv=n,this.scoring=s,this.bestParams_=null,this.bestScore_=-1/0,this.bestEstimator_=null}fit(e,t){for(let n of eV(this.paramGrid)){let s=_I(()=>this.makeEstimator(n),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_=n)}return this.bestEstimator_=this.makeEstimator(this.bestParams_).fit(e,t),this}predict(e){return this.bestEstimator_.predict(e)}};var AE={};nt(AE,{Generator:()=>q_,acf:()=>iE,andersonDarling:()=>rE,betaRegularized:()=>Xf,bisect:()=>wI,brentq:()=>E_,chi2:()=>Ql,chi2Gof:()=>JI,chi2Independence:()=>ZI,constrainedMinimize:()=>dS,convolve:()=>gE,correlate:()=>yE,cubicSpline:()=>UI,dagostinoK2:()=>nE,differentialEvolution:()=>cS,digammaScalar:()=>Ac,durbinWatson:()=>lE,erfScalar:()=>bu,erfcScalar:()=>wu,fft:()=>$a,fisherF:()=>II,gammaScalar:()=>Tu,ifft:()=>Xl,jarqueBera:()=>tE,ksTest1Samp:()=>XI,ksTest2Samp:()=>eE,lbfgs:()=>th,lbfgsB:()=>fS,levenbergMarquardt:()=>hS,lgammaScalar:()=>Tn,linearInterp:()=>qI,ljungBox:()=>uE,lowerGammaRegularized:()=>C_,mannWhitneyU:()=>sE,nelderMead:()=>uS,newton:()=>TI,normal:()=>MI,normalCdfScalar:()=>po,normalPdfScalar:()=>eh,normalPpfScalar:()=>Bi,pacf:()=>aE,periodogram:()=>cE,polyfit:()=>kE,polyroots:()=>NE,polyval:()=>vE,qr:()=>Cx,quadrature:()=>VI,rollingMax:()=>SE,rollingMean:()=>bE,rollingMin:()=>xE,rollingStd:()=>wE,rollingSum:()=>_E,simpson:()=>zI,studentT:()=>D_,tTest1Samp:()=>wS,tTestInd:()=>HI,tTestPaired:()=>YI,trapezoid:()=>FI});jn();Kn();function cs(r,e,t){return{root:r,iterations:e,converged:t}}l(cs,"result");function wI(r,e,t,n={}){let s=n.tol??1e-12,o=n.maxIter??100,i=r(e),a=r(t);if(i===0)return cs(e,0,!0);if(a===0)return cs(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 h=1;h<=o;h++){let f=.5*(u+p),d=r(f);if(d===0||.5*(p-u)<s)return cs(f,h,!0);c*d<0?p=f:(u=f,c=d)}return cs(.5*(u+p),o,!1)}l(wI,"bisect");function TI(r,e,t={}){let n=t.tol??1e-12,s=t.maxIter??100,o=t.step??1e-7,i=t.derivative??(u=>(r(u+o)-r(u-o))/(2*o)),a=e;for(let u=1;u<=s;u++){let p=r(a);if(Math.abs(p)<n)return cs(a,u,!0);let c=i(a);if(c===0)break;let h=a-p/c;if(Math.abs(h-a)<n)return cs(h,u,!0);a=h}return cs(a,s,!1)}l(TI,"newton");function E_(r,e,t,n={}){let s=n.tol??1e-12,o=n.maxIter??100,i=r(e),a=r(t);if(i===0)return cs(e,0,!0);if(a===0)return cs(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,h=!0;for(let f=1;f<=o;f++){let d;i!==p&&a!==p?d=e*a*p/((i-a)*(i-p))+t*i*p/((a-i)*(a-p))+u*i*a/((p-i)*(p-a)):d=t-a*(t-e)/(a-i);let m=.25*(3*e+t),g=!(d>Math.min(m,t)&&d<Math.max(m,t)),y=h&&Math.abs(d-t)>=.5*Math.abs(t-u),_=!h&&Math.abs(d-t)>=.5*Math.abs(u-c),b=h&&Math.abs(t-u)<s,w=!h&&Math.abs(u-c)<s;g||y||_||b||w?(d=.5*(e+t),h=!0):h=!1;let T=r(d);if(c=u,u=t,p=a,i*T<0?(t=d,a=T):(e=d,i=T),Math.abs(i)<Math.abs(a)&&([e,t]=[t,e],[i,a]=[a,i]),T===0)return cs(d,f,!0);if(Math.abs(a)<s||Math.abs(t-e)<s)return cs(t,f,!0)}return cs(t,o,!1)}l(E_,"brentq");Ji();var Jl=[-39.69683028665376,220.9460984245205,-275.9285104469687,138.357751867269,-30.66479806614716,2.506628277459239],Qf=[-54.47609879822406,161.5858368580409,-155.6989798598866,66.80131188771972,-13.28068155288572],Zl=[-.007784894002430293,-.3223964580411365,-2.400758277161838,-2.549732539343734,4.374664141464968,2.938163982698783],L_=[.007784695709041462,.3224671290700398,2.445134137142996,3.754408661907416],xI=.02425,oS=300,iS=3e-14,gr=1e-300,tV=2,nV=Math.SQRT2,rV=Math.sqrt(2*Math.PI),ige=1/Math.sqrt(Math.PI);function C_(r,e){if(e<=0)return 0;if(e<r+1){let i=1/r,a=i;for(let u=1;u<oS&&(i*=e/(r+u),a+=i,!(Math.abs(i)<Math.abs(a)*iS));u++);return a*Math.exp(-e+r*Math.log(e)-Tn(r))}let t=e+1-r,n=1/gr,s=1/t,o=s;for(let i=1;i<oS;i++){let a=-i*(i-r);t+=2,s=a*s+t,Math.abs(s)<gr&&(s=gr),n=t+a/n,Math.abs(n)<gr&&(n=gr),s=1/s;let u=s*n;if(o*=u,Math.abs(u-1)<iS)break}return 1-o*Math.exp(-e+r*Math.log(e)-Tn(r))}l(C_,"lowerGammaRegularized");function SI(r,e,t){let n=r+e,s=r+1,o=r-1,i=1,a=1-n*t/s;Math.abs(a)<gr&&(a=gr),a=1/a;let u=a;for(let p=1;p<oS;p++){let c=2*p,h=p*(e-p)*t/((o+c)*(r+c));a=1+h*a,Math.abs(a)<gr&&(a=gr),i=1+h/i,Math.abs(i)<gr&&(i=gr),a=1/a,u*=a*i,h=-(r+p)*(n+p)*t/((r+c)*(s+c)),a=1+h*a,Math.abs(a)<gr&&(a=gr),i=1+h/i,Math.abs(i)<gr&&(i=gr),a=1/a;let f=a*i;if(u*=f,Math.abs(f-1)<iS)break}return u}l(SI,"betacf");function Xf(r,e,t){if(t<=0)return 0;if(t>=1)return 1;let n=Math.exp(Tn(r+e)-Tn(r)-Tn(e)+r*Math.log(t)+e*Math.log(1-t));return t<(r+1)/(r+e+2)?n*SI(r,e,t)/r:1-n*SI(e,r,1-t)/e}l(Xf,"betaRegularized");function po(r){return .5*wu(-r/nV)}l(po,"normalCdfScalar");function eh(r){return Math.exp(-.5*r*r)/rV}l(eh,"normalPdfScalar");function kI(r){if(r<xI){let n=Math.sqrt(-2*Math.log(r));return(((((Zl[0]*n+Zl[1])*n+Zl[2])*n+Zl[3])*n+Zl[4])*n+Zl[5])/((((L_[0]*n+L_[1])*n+L_[2])*n+L_[3])*n+1)}if(r>1-xI)return-kI(1-r);let e=r-.5,t=e*e;return(((((Jl[0]*t+Jl[1])*t+Jl[2])*t+Jl[3])*t+Jl[4])*t+Jl[5])*e/(((((Qf[0]*t+Qf[1])*t+Qf[2])*t+Qf[3])*t+Qf[4])*t+1)}l(kI,"ppfSeed");function Bi(r,e={}){if(r<=0)return-1/0;if(r>=1)return 1/0;let t=e.refineSteps??tV,n=kI(r);for(let s=0;s<t;s++){let i=(po(n)-r)/eh(n);n-=i/(1+n*i/2)}return n}l(Bi,"normalPpfScalar");var sV=3,oV=2,iV=60,aV=1e-11;function ps(r,e){if(r instanceof We){let t=Je(r),n=new Float64Array(t.length);for(let s=0;s<t.length;s++)n[s]=e(Number(t[s]));return Q(n,r.shape,r.dtype,r.device)}return e(r)}l(ps,"mapElementwise");function aS(r,e,t,n,s={}){if(t<=0||t>=1)return t===0?s.lowerLimit??-1/0:t===1?1/0:NaN;let o=s.refineSteps??sV,i=s.tol??aV,a=n;for(let h=0;h<o;h++){let f=r(a)-t,d=e(a);if(!Number.isFinite(a)||d<=0)break;let m=a-f/d;if(a=s.lowerLimit!==void 0&&m<=s.lowerLimit?(a+s.lowerLimit)/2:m,Math.abs(f)<i)return a}if(Math.abs(r(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 h=0;h<iV;h++){if(s.lowerLimit===void 0&&(u=a-c),p=a+c,r(u)-t<0&&r(p)-t>0)return E_(f=>r(f)-t,u,p,{tol:i}).root;c*=oV}return a}l(aS,"invertCdf");var MI={cdf:l((r,e={})=>{let t=e.loc??0,n=e.scale??1;return ps(r,s=>po((s-t)/n))},"cdf"),pdf:l((r,e={})=>{let t=e.loc??0,n=e.scale??1;return ps(r,s=>eh((s-t)/n)/n)},"pdf"),ppf:l((r,e={})=>{let t=e.loc??0,n=e.scale??1;return ps(r,s=>t+n*Bi(s,e))},"ppf")};function vI(r,e){if(r===0)return .5;let t=Xf(e/2,.5,e/(e+r*r));return r>0?1-t/2:t/2}l(vI,"tCdfScalar");function NI(r,e){let t=Tn((e+1)/2)-Tn(e/2)-.5*Math.log(e*Math.PI);return Math.exp(t-(e+1)/2*Math.log(1+r*r/e))}l(NI,"tPdfScalar");var D_={cdf:l((r,e,t={})=>ps(r,n=>vI(n,e??t.df)),"cdf"),pdf:l((r,e,t={})=>ps(r,n=>NI(n,e??t.df)),"pdf"),ppf:l((r,e,t={})=>{let n=e??t.df;return ps(r,s=>aS(o=>vI(o,n),o=>NI(o,n),s,Bi(s),t))},"ppf")};function AI(r,e){return r<=0?0:C_(e/2,r/2)}l(AI,"chi2CdfScalar");function OI(r,e){if(r<=0)return 0;let t=e/2;return Math.exp((t-1)*Math.log(r)-r/2-t*Math.log(2)-Tn(t))}l(OI,"chi2PdfScalar");function uV(r,e){let t=Bi(r),n=2/(9*e),s=1-n+t*Math.sqrt(n);return Math.max(e*s*s*s,Number.EPSILON)}l(uV,"chi2Seed");var Ql={cdf:l((r,e,t={})=>ps(r,n=>AI(n,e??t.df)),"cdf"),pdf:l((r,e,t={})=>ps(r,n=>OI(n,e??t.df)),"pdf"),ppf:l((r,e,t={})=>{let n=e??t.df;return ps(r,s=>aS(o=>AI(o,n),o=>OI(o,n),s,uV(s,n),{...t,lowerLimit:0}))},"ppf")};function BI(r,e,t){return r<=0?0:Xf(e/2,t/2,e*r/(e*r+t))}l(BI,"fCdfScalar");function RI(r,e,t){if(r<=0)return 0;let n=Tn(e/2)+Tn(t/2)-Tn((e+t)/2);return Math.exp(e/2*Math.log(e/t)+(e/2-1)*Math.log(r)-(e+t)/2*Math.log(1+e/t*r)-n)}l(RI,"fPdfScalar");var II={cdf:l((r,e,t,n={})=>ps(r,s=>BI(s,e??n.d1,t??n.d2)),"cdf"),pdf:l((r,e,t,n={})=>ps(r,s=>RI(s,e??n.d1,t??n.d2)),"pdf"),ppf:l((r,e,t,n={})=>{let s=e??n.d1,o=t??n.d2;return ps(r,i=>aS(a=>BI(a,s,o),a=>RI(a,s,o),i,1,{...n,lowerLimit:0}))},"ppf")};function uS(r,e,t={}){let n=e.length,s=t.maxIter??200*n,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,h=t.sigma??.5,f=[e.slice()];for(let _=0;_<n;_++){let b=e.slice();b[_]=b[_]!==0?b[_]*(1+i):a,f.push(b)}let d=f.map(r),m=Array.from({length:n+1},(_,b)=>b),g=0;for(;g<s;g++){m.sort((k,A)=>d[k]-d[A]);let _=m.map(k=>f[k]),b=m.map(k=>d[k]);for(let k=0;k<=n;k++)f[k]=_[k],d[k]=b[k];if(Math.abs(d[n]-d[0])<o)break;let w=new Array(n).fill(0);for(let k=0;k<n;k++)for(let A=0;A<n;A++)w[A]+=f[k][A];for(let k=0;k<n;k++)w[k]/=n;let T=f[n],S=w.map((k,A)=>k+u*(k-T[A])),x=r(S);if(x<d[0]){let k=w.map((N,v)=>N+p*(S[v]-N)),A=r(k);A<x?(f[n]=k,d[n]=A):(f[n]=S,d[n]=x)}else if(x<d[n-1])f[n]=S,d[n]=x;else{let k=w.map((N,v)=>N+c*(T[v]-N)),A=r(k);if(A<d[n])f[n]=k,d[n]=A;else{let N=f[0];for(let v=1;v<=n;v++)f[v]=N.map((O,R)=>O+h*(f[v][R]-O)),d[v]=r(f[v])}}}let y=0;for(let _=1;_<=n;_++)d[_]<d[y]&&(y=_);return{point:f[y],value:d[y],iterations:g,converged:g<s}}l(uS,"nelderMead");function fs(r,e){let t=0;for(let n=0;n<r.length;n++)t+=r[n]*e[n];return t}l(fs,"dot");function Da(r){return Math.sqrt(fs(r,r))}l(Da,"norm");function P_(r,e){let t=new Array(r.length);for(let n=0;n<r.length;n++)t[n]=r[n]-e[n];return t}l(P_,"sub");function EI(r,e){let t=new Array(r.length);for(let n=0;n<r.length;n++)t[n]=r[n]*e;return t}l(EI,"scale");function LI(r){let e=0;for(let t=1;t<r.length;t++)r[t]<r[e]&&(e=t);return e}l(LI,"argmin");function CI(r,e,t){let n=e.length,s=new Array(n),o=e.slice();for(let i=0;i<n;i++){let a=o[i];o[i]=a+t;let u=r(o);o[i]=a-t;let p=r(o);o[i]=a,s[i]=(u-p)/(2*t)}return s}l(CI,"numericGradient");function Pa(r,e){return e?r.map((t,n)=>{let s=e[n];if(!s)return t;let[o,i]=s;return t<o?o:t>i?i:t}):r.slice()}l(Pa,"clampToBounds");var lV=10,cV=5,pV=.8,fV=.9,hV=1e3,dV=1e-10;function lS(r,e,t){let n;do n=el(r,e);while(t.includes(n));return n}l(lS,"pickDistinct");function cS(r,e,t={}){let n=e.length,s=cr(t.seed),o=t.populationSize??Math.max(cV,lV*n),i=t.mutation??pV,a=t.recombination??fV,u=t.maxIter??hV,p=t.tol??dV,c=[];for(let m=0;m<o;m++){let g=new Array(n);for(let y=0;y<n;y++){let[_,b]=e[y];g[y]=_+s()*(b-_)}c.push(g)}let h=c.map(r),f=LI(h),d=0;for(;d<u;d++){for(let y=0;y<o;y++){let _=lS(s,o,[y]),b=lS(s,o,[y,_]),w=lS(s,o,[y,_,b]),T=el(s,n),S=c[y].slice();for(let k=0;k<n;k++)if(s()<a||k===T){let[A,N]=e[k],v=c[_][k]+i*(c[b][k]-c[w][k]);v<A?v=A:v>N&&(v=N),S[k]=v}let x=r(S);x<h[y]&&(c[y]=S,h[y]=x,x<h[f]&&(f=y))}let m=h[0],g=h[0];for(let y=1;y<o;y++)h[y]<m&&(m=h[y]),h[y]>g&&(g=h[y]);if(g-m<p){d++;break}}return{point:c[f].slice(),value:h[f],iterations:d,converged:d<u}}l(cS,"differentialEvolution");var mV=10,gV=1e3,yV=1e-8,_V=1e-12,bV=1e-4,wV=.1,DI=.5,TV=40;function xV(r,e,t,n){let s=r.slice(),o=e.length,i=new Array(o);for(let a=o-1;a>=0;a--){i[a]=n[a]*fs(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=fs(a,u)/fs(u,u);for(let c=0;c<s.length;c++)s[c]*=p}for(let a=0;a<o;a++){let u=n[a]*fs(t[a],s);for(let p=0;p<s.length;p++)s[p]+=(i[a]-u)*e[a][p]}return s}l(xV,"twoLoop");function PI(r,e,t,n){let s=e.length,o=t.memory??mV,i=t.maxIter??gV,a=t.gtol??yV,u=t.ftol??_V,p=t.step??1e-7,c=t.gradient??(T=>CI(r,T,p)),h=l((T,S)=>n?T.map((x,k)=>{let A=n[k];if(!A)return x;let[N,v]=A;return S[k]<=N&&x>0||S[k]>=v&&x<0?0:x}):T,"projectGrad"),f=Pa(e,n),d=r(f),m=c(f),g=h(m,f),y=[],_=[],b=[],w=0;for(;w<i;w++){if(Da(g)<a)return{point:f,value:d,iterations:w,converged:!0};let T=EI(xV(g,y,_,b),-1);if(fs(T,g)>=0)for(let J=0;J<s;J++)T[J]=-g[J];let S=1,x=f,k=d,A=!1,N=!1,v=fs(g,T);for(let J=0;J<TV;J++){let j=f.map((G,K)=>G+S*T[K]),U=Pa(j,n),P=r(U);if(P<=d+bV*S*v){x=U,k=P,A=!0,N=n!=null&&U.some((G,K)=>G!==j[K]);break}let V=2*(P-d-v*S),he=V>0?-v*S*S/V:S*DI,le=wV*S,Ie=DI*S;S=he<le?le:he>Ie?Ie:he}if(!A)return{point:f,value:d,iterations:w,converged:!1};let O=c(x),R=h(O,x),B=P_(x,f),E=N?P_(R,g):P_(O,m),z=fs(B,E);z>Number.EPSILON*Da(B)*Da(E)&&(y.push(B),_.push(E),b.push(1/z),y.length>o&&(y.shift(),_.shift(),b.shift()));let D=Math.abs(k-d);if(f=x,d=k,m=O,g=R,D<u*(1+Math.abs(d))&&Da(B)<u*(1+Da(f)))return w++,{point:f,value:d,iterations:w,converged:!0}}return{point:f,value:d,iterations:w,converged:Da(g)<a}}l(PI,"minimize");function th(r,e,t={}){return PI(r,e,t,null)}l(th,"lbfgs");function fS(r,e,t,n={}){return PI(r,e,n,t)}l(fS,"lbfgsB");var SV=200,kV=1e-10,vV=.001,$I=10,NV=10,AV=1e12;function OV(r,e,t,n){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+n;let p=r(i);i[a]=u-n;let c=r(i);i[a]=u;for(let h=0;h<t;h++)o[h][a]=(p[h]-c[h])/(2*n)}return o}l(OV,"numericJacobian");function BV(r,e,t,n){let s=new Float64Array(n*n);for(let a=0;a<n;a++){for(let u=0;u<n;u++)s[a*n+u]=r[a][u];s[a*n+a]+=t*r[a][a]}let o=Float64Array.from(e),i=Jf(Q(s,[n,n],"f64"),Q(o,[n],"f64"));return Array.from(i.toArray())}l(BV,"solveNormal");function hS(r,e,t={}){let n=t.maxIter??SV,s=t.tol??kV,o=t.step??1e-7,i=t.jacobian??((m,g)=>OV(r,m,g,o)),a=e.slice(),u=r(a),p=u.length,c=a.length,h=fs(u,u),f=t.lambda??vV,d=0;for(;d<n;d++){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 T=0;for(let S=0;S<p;S++)T+=m[S][b]*m[S][w];g[b][w]=T,g[w][b]=T}let _=!1;for(;f<AV;){let b;try{b=BV(g,y,f,c)}catch{f*=$I;continue}let w=a.map((x,k)=>x-b[k]),T=r(w),S=fs(T,T);if(S<h){a=w,u=T;let x=h-S;if(h=S,f/=NV,_=!0,x<s*(1+h))return{point:a,value:h,iterations:d+1,converged:!0};break}f*=$I}if(!_)return{point:a,value:h,iterations:d,converged:h<s}}return{point:a,value:h,iterations:d,converged:!1}}l(hS,"levenbergMarquardt");var RV=10,MV=10,IV=20,EV=1e-8;function dS(r,e,t={}){let n=t.bounds??null,s=t.inequalities??[],o=t.equalities??[],i=t.outerIter??IV,a=t.ctol??EV,u=t.penaltyGrowth??MV,p=t.inner??th,c=t.innerOpts??{},h=t.penalty??RV,f=Pa(e,n),d=0,m=null;for(let g=0;g<i;g++){m=p(l(b=>{let w=Pa(b,n),T=r(w);for(let S of s){let x=S(w);x>0&&(T+=h*x*x)}for(let S of o){let x=S(w);T+=h*x*x}return T},"penalized"),f,c),f=Pa(m.point,n),d+=m.iterations;let _=0;for(let b of s)_=Math.max(_,b(f));for(let b of o)_=Math.max(_,Math.abs(b(f)));if(_<a)return{point:f,value:r(f),iterations:d,converged:!0};h*=u}return{point:f,value:r(f),iterations:d,converged:!1}}l(dS,"constrainedMinimize");function FI(r,e,t,n={}){let s=n.n??1024,o=(t-e)/s,i=.5*(r(e)+r(t));for(let a=1;a<s;a++)i+=r(e+a*o);return i*o}l(FI,"trapezoid");function zI(r,e,t,n={}){let s=n.n??1024;s%2===1&&(s+=1);let o=(t-e)/s,i=r(e)+r(t);for(let a=1;a<s;a++)i+=(a%2===0?2:4)*r(e+a*o);return i*o/3}l(zI,"simpson");function mS(r,e,t,n,s){return(s-n)/6*(r+4*t+e)}l(mS,"simpsonCell");function gS(r,e,t,n,s,o,i,a,u,p){let c=.5*(e+t),h=.5*(e+c),f=.5*(c+t),d=r(h),m=r(f),g=mS(n,o,d,e,c),y=mS(o,s,m,c,t),_=g+y-i;return u>=p||Math.abs(_)<=15*a?g+y+_/15:gS(r,e,c,n,o,d,g,.5*a,u+1,p)+gS(r,c,t,o,s,m,y,.5*a,u+1,p)}l(gS,"adaptiveStep");function VI(r,e,t,n={}){let s=n.tol??1e-10,o=n.maxDepth??50,i=.5*(e+t),a=r(e),u=r(t),p=r(i),c=mS(a,u,p,e,t);return gS(r,e,t,a,u,p,c,s,0,o)}l(VI,"quadrature");function jI(r,e){let t=1,n=r.length-1;for(;t<n;){let s=t+n>>1;r[s]<e?t=s+1:n=s}return t}l(jI,"upperInterval");function GI(r,e,t){let n=r.length;if(t<=r[0])return e[0];if(t>=r[n-1])return e[n-1];let s=jI(r,t),o=(t-r[s-1])/(r[s]-r[s-1]);return e[s-1]+o*(e[s]-e[s-1])}l(GI,"evalLinear");function qI(r,e,t){return typeof t!="number"?t.map(n=>GI(r,e,n)):GI(r,e,t)}l(qI,"linearInterp");function LV(r,e,t,n){let s=e.length,o=new Array(s),i=new Array(s);o[0]=t[0]/e[0],i[0]=n[0]/e[0];for(let u=1;u<s;u++){let p=e[u]-r[u]*o[u-1];o[u]=t[u]/p,i[u]=(n[u]-r[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(LV,"thomas");function UI(r,e){let t=r.length,n=new Array(t-1);for(let c=0;c<t-1;c++)n[c]=r[c+1]-r[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]=n[c-1],o[c]=2*(n[c-1]+n[c]),i[c]=n[c],a[c]=6*((e[c+1]-e[c])/n[c]-(e[c]-e[c-1])/n[c-1]);let u=LV(s,o,i,a),p=l(c=>{if(c<=r[0])return e[0];if(c>=r[t-1])return e[t-1];let h=jI(r,c),f=n[h-1],d=(r[h]-c)/f,m=(c-r[h-1])/f;return d*e[h-1]+m*e[h]+((d*d*d-d)*u[h-1]+(m*m*m-m)*u[h])*f*f/6},"evalOne");return{xs:r,ys:e,coefficients:u,evaluate:l((c=>typeof c!="number"?c.map(p):p(c)),"evaluate")}}l(UI,"cubicSpline");be();var $a=l(r=>ot("fft",r),"fft"),Xl=l(r=>ot("ifft",r),"ifft");jn();Kn();ar();function KI(r){let e=new Float64Array(r.length);for(let t=0;t<r.length;t++)e[t]=Number(r[t]);return e}l(KI,"float64From");function Ke(r){return r instanceof We?{data:KI(Je(r)),dtype:r.dtype,device:r.device}:typeof r=="number"?{data:Float64Array.of(r),dtype:"f64",device:ks}:{data:Float64Array.from(r),dtype:"f64",device:ks}}l(Ke,"hostVector");function $_(r){if(r instanceof We){if(r.ndim!==2)throw new Error(`numeric: expected a 2-D table, got ${r.ndim}-D`);let[s,o]=r.shape;return{data:KI(Je(r)),rows:s,cols:o,dtype:r.dtype,device:r.device}}let e=r.length,t=r[0].length,n=new Float64Array(e*t);for(let s=0;s<e;s++)for(let o=0;o<t;o++)n[s*t+o]=r[s][o];return{data:n,rows:e,cols:t,dtype:"f64",device:ks}}l($_,"hostGrid");function F_(r){let e=1;for(;e<r;)e<<=1;return e}l(F_,"nextPow2");var CV=100,DV=1e-16,PV=.12,$V=.11,WI=[.75,2.25],yS=[.2,.34,.6],FV=[-13.436,101.14,-223.73],zV=[-8.318,42.796,-59.938],VV=[.9177,-4.279,-1.38],GV=[1.2937,-5.709,.0186];function V_(r){let e=r.length,t=0;for(let o=0;o<e;o++)t+=r[o];let n=t/e,s=0;for(let o=0;o<e;o++){let i=r[o]-n;s+=i*i}return{n:e,mean:n,variance:s/(e-1)}}l(V_,"summary");function bS(r){let e=r.length,t=0;for(let a=0;a<e;a++)t+=r[a];let n=t/e,s=0,o=0,i=0;for(let a=0;a<e;a++){let u=r[a]-n,p=u*u;s+=p,o+=p*u,i+=p*p}return s/=e,o/=e,i/=e,{n:e,mean:n,m2:s,skew:o/Math.pow(s,1.5),kurt:i/(s*s)}}l(bS,"centralMoments");function Ri(r){return Math.min(Math.max(r,0),1)}l(Ri,"clampP");function _S(r,e){return Ri(2*(1-D_.cdf(Math.abs(r),e)))}l(_S,"tPvalue");function G_(r,e){return Ri(1-Ql.cdf(r,e))}l(G_,"chi2Pvalue");function wS(r,e={}){let{data:t}=Ke(r),n=e.popmean??0,{n:s,mean:o,variance:i}=V_(t),a=(o-n)/Math.sqrt(i/s),u=s-1;return{statistic:a,pvalue:_S(a,u),df:u}}l(wS,"tTest1Samp");function HI(r,e,t={}){let n=V_(Ke(r).data),s=V_(Ke(e).data);if(t.equalVar??!0){let p=n.n+s.n-2,c=((n.n-1)*n.variance+(s.n-1)*s.variance)/p,h=(n.mean-s.mean)/Math.sqrt(c*(1/n.n+1/s.n));return{statistic:h,pvalue:_S(h,p),df:p}}let o=n.variance/n.n,i=s.variance/s.n,a=(n.mean-s.mean)/Math.sqrt(o+i),u=(o+i)*(o+i)/(o*o/(n.n-1)+i*i/(s.n-1));return{statistic:a,pvalue:_S(a,u),df:u}}l(HI,"tTestInd");function YI(r,e,t={}){let n=Ke(r).data,s=Ke(e).data;if(n.length!==s.length)throw new Error(`tTestPaired: samples must have equal length, got ${n.length} and ${s.length}`);let o=new Float64Array(n.length);for(let i=0;i<n.length;i++)o[i]=n[i]-s[i];return wS(o,{popmean:t.popmean??0})}l(YI,"tTestPaired");function JI(r,e,t={}){let n=Ke(r).data,s=n.length,o;if(e==null){let u=0;for(let p=0;p<s;p++)u+=n[p];o=new Float64Array(s).fill(u/s)}else o=Ke(e).data;let i=0;for(let u=0;u<s;u++){let p=n[u]-o[u];i+=p*p/o[u]}let a=s-1-(t.ddof??0);return{statistic:i,pvalue:G_(i,a),df:a}}l(JI,"chi2Gof");function ZI(r){let{data:e,rows:t,cols:n}=$_(r),s=new Float64Array(t),o=new Float64Array(n),i=0;for(let p=0;p<t;p++)for(let c=0;c<n;c++){let h=e[p*n+c];s[p]+=h,o[c]+=h,i+=h}let a=0;for(let p=0;p<t;p++)for(let c=0;c<n;c++){let h=s[p]*o[c]/i,f=e[p*n+c]-h;a+=f*f/h}let u=(t-1)*(n-1);return{statistic:a,pvalue:G_(a,u),df:u}}l(ZI,"chi2Independence");function jV(r){if(r<=0)return 1;let e=0;for(let t=1;t<=CV;t++){let n=Math.exp(-2*t*t*r*r);if(e+=t%2===1?n:-n,n<DV)break}return Ri(2*e)}l(jV,"kolmogorovSf");function QI(r,e){let t=Math.sqrt(e);return jV(r*(t+PV+$V/t))}l(QI,"ksPvalue");function XI(r,e,t={}){let{data:n}=Ke(r),s=Float64Array.from(n).sort(),o=s.length,i=t.loc??0,a=t.scale??1,u=typeof e=="function"?e:c=>po((c-i)/a),p=0;for(let c=0;c<o;c++){let h=u(s[c]);p=Math.max(p,(c+1)/o-h,h-c/o)}return{statistic:p,pvalue:QI(p,o)}}l(XI,"ksTest1Samp");function eE(r,e){let t=Float64Array.from(Ke(r).data).sort(),n=Float64Array.from(Ke(e).data).sort(),s=t.length,o=n.length,i=0,a=0,u=0;for(;i<s&&a<o;){let c=Math.min(t[i],n[a]);for(;i<s&&t[i]<=c;)i++;for(;a<o&&n[a]<=c;)a++;u=Math.max(u,Math.abs(i/s-a/o))}let p=s*o/(s+o);return{statistic:u,pvalue:QI(u,p)}}l(eE,"ksTest2Samp");function tE(r){let{n:e,skew:t,kurt:n}=bS(Ke(r).data),s=e/6*(t*t+(n-3)*(n-3)/4);return{statistic:s,pvalue:G_(s,2),df:2}}l(tE,"jarqueBera");function qV(r){let{n:e,skew:t}=bS(r),n=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=n/a;return i*Math.log(u+Math.sqrt(u*u+1))}l(qV,"skewZ");function UV(r){let{n:e,kurt:t}=bS(r),n=3*(e-1)/(e+1),s=24*e*(e-2)*(e-3)/((e+1)*(e+1)*(e+3)*(e+5)),o=(t-n)/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(UV,"kurtosisZ");function nE(r){let{data:e}=Ke(r),t=qV(e),n=UV(e),s=t*t+n*n;return{statistic:s,pvalue:G_(s,2),df:2}}l(nE,"dagostinoK2");function z_(r,e){return r[0]+r[1]*e+r[2]*e*e}l(z_,"poly2");function KV(r){return r>=yS[2]?Ri(Math.exp(z_(GV,r))):r>yS[1]?Ri(Math.exp(z_(VV,r))):r>yS[0]?Ri(1-Math.exp(z_(zV,r))):Ri(1-Math.exp(z_(FV,r)))}l(KV,"adPvalue");function rE(r){let{data:e}=Ke(r),t=Float64Array.from(e).sort(),n=t.length,{mean:s,variance:o}=V_(t),i=Math.sqrt(o),a=0;for(let c=0;c<n;c++){let h=po((t[c]-s)/i),f=po(-(t[n-1-c]-s)/i);a+=(2*c+1)*(Math.log(h)+Math.log(f))}let u=-n-a/n,p=u*(1+WI[0]/n+WI[1]/(n*n));return{statistic:u,pvalue:KV(p)}}l(rE,"andersonDarling");function WV(r){let e=r.length,t=Array.from({length:e},(i,a)=>a);t.sort((i,a)=>r[i]-r[a]);let n=new Float64Array(e),s=0,o=0;for(;o<e;){let i=o;for(;i+1<e&&r[t[i+1]]===r[t[o]];)i++;let a=(o+i+2)/2;for(let p=o;p<=i;p++)n[t[p]]=a;let u=i-o+1;s+=u*u*u-u,o=i+1}return{ranks:n,tieSum:s}}l(WV,"rankData");function sE(r,e){let t=Ke(r).data,n=Ke(e).data,s=t.length,o=n.length,i=new Float64Array(s+o);i.set(t),i.set(n,s);let{ranks:a,tieSum:u}=WV(i),p=0;for(let y=0;y<s;y++)p+=a[y];let c=p-s*(s+1)/2,h=s+o,f=s*o/2,d=Math.sqrt(s*o/12*(h+1-u/(h*(h-1)))),m=c-f,g=(m-.5*Math.sign(m))/d;return{statistic:c,pvalue:Ri(2*(1-po(Math.abs(g))))}}l(sE,"mannWhitneyU");Kn();function zo(r,e){return Number(r[e])}l(zo,"f64");function oE(r){return Math.max(1,Math.min(r-1,Math.floor(10*Math.log10(r))))}l(oE,"defaultNlags");function HV(r,e,t){let n=r.length,s=0;for(let f=0;f<n;f++)s+=r[f];let o=s/n,i=F_(2*n),a=new Float64Array(i);for(let f=0;f<n;f++)a[f]=r[f]-o;let u=Je($a(Q(a,[i],"f64",e))),p=new Float64Array(2*i);for(let f=0;f<i;f++)p[2*f]=zo(u,2*f)*zo(u,2*f)+zo(u,2*f+1)*zo(u,2*f+1);let c=Je(Xl(Q(p,[i,2],"f64",e))),h=new Float64Array(t+1);for(let f=0;f<=t;f++)h[f]=zo(c,2*f)/n;return h}l(HV,"autocovariance");function TS(r,e,t){let n=HV(r,e,t),s=new Float64Array(t+1);for(let o=0;o<=t;o++)s[o]=n[o]/n[0];return s}l(TS,"normalizedAcf");function iE(r,e={}){let{data:t,device:n,dtype:s}=Ke(r),o=e.nlags??oE(t.length),i=TS(t,n,o);return Q(i,[o+1],s,n)}l(iE,"acf");function aE(r,e={}){let{data:t,device:n,dtype:s}=Ke(r),o=e.nlags??oE(t.length),i=TS(t,n,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 h=i[c],f=1;for(let g=1;g<c;g++)h-=u[g]*i[c-g],f-=u[g]*i[g];let d=h/f;p[c]=d;for(let g=1;g<c;g++)p[g]=u[g]-d*u[c-g];a[c]=d;let m=u;u=p,p=m}return Q(a,[o+1],s,n)}l(aE,"pacf");function uE(r,e={}){let{data:t,device:n}=Ke(r),s=t.length,o=e.lags??Math.min(10,s-1),i=TS(t,n,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-Ql.cdf(a,u),0),1),df:u}}l(uE,"ljungBox");function lE(r){let{data:e}=Ke(r),t=0,n=0;for(let s=0;s<e.length;s++)if(n+=e[s]*e[s],s>0){let o=e[s]-e[s-1];t+=o*o}return t/n}l(lE,"durbinWatson");function cE(r,e={}){let{data:t,device:n,dtype:s}=Ke(r),o=t.length,i=t;if(e.detrend!==!1){let c=0;for(let f=0;f<o;f++)c+=t[f];let h=c/o;i=new Float64Array(o);for(let f=0;f<o;f++)i[f]=t[f]-h}let a=Je($a(Q(i,[o],"f64",n))),u=Math.floor(o/2),p=new Float64Array(u+1);for(let c=0;c<=u;c++)p[c]=(zo(a,2*c)*zo(a,2*c)+zo(a,2*c+1)*zo(a,2*c+1))/o;return Q(p,[u+1],s,n)}l(cE,"periodogram");Kn();var YV=200,JV=1e-13,pE=.4,fE=.9;function nh(r,e){return Number(r[e])}l(nh,"f64");function ZV(r){let e=new Float64Array(r.length);for(let t=0;t<r.length;t++)e[t]=Number(r[t]);return e}l(ZV,"float64From");function hE(r,e,t){let n=new Float64Array(e);return n.set(r),Je($a(Q(n,[e],"f64",t)))}l(hE,"fftInterleaved");function QV(r,e,t){let n=r+e-1;if(t==="full")return{offset:0,length:n};let s=Math.max(r,e),o=Math.min(r,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(QV,"modeSlice");function mE(r,e,t,n,s){let o=r.length,i=e.length,a=F_(o+i-1),u=hE(r,a,n),p=hE(e,a,n),c=new Float64Array(2*a);for(let g=0;g<a;g++){let y=nh(u,2*g),_=nh(u,2*g+1),b=nh(p,2*g),w=nh(p,2*g+1);c[2*g]=y*b-_*w,c[2*g+1]=y*w+_*b}let h=Je(Xl(Q(c,[a,2],"f64",n))),{offset:f,length:d}=QV(o,i,t),m=new Float64Array(d);for(let g=0;g<d;g++)m[g]=nh(h,2*(f+g));return Q(m,[d],s,n)}l(mE,"fftConvolve");function gE(r,e,t={}){let n=Ke(r),s=Ke(e);return mE(n.data,s.data,t.mode??"full",n.device,n.dtype)}l(gE,"convolve");function yE(r,e,t={}){let n=Ke(r),s=Ke(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 mE(n.data,o,t.mode??"full",n.device,n.dtype)}l(yE,"correlate");function j_(r,e){let t=Math.floor(r);if(!(t>=1&&t<=e))throw new Error(`rolling: window must be in [1, ${e}], got ${r}`);return t}l(j_,"checkWindow");function _E(r,e){let{data:t,device:n,dtype:s}=Ke(r),o=t.length,i=j_(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,n)}l(_E,"rollingSum");function bE(r,e){let{data:t,device:n,dtype:s}=Ke(r),o=t.length,i=j_(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,n)}l(bE,"rollingMean");function wE(r,e,t={}){let{data:n,device:s,dtype:o}=Ke(r),i=n.length,a=j_(e,i),u=t.ddof??1,p=0;for(let m=0;m<i;m++)p+=n[m];let c=p/i,h=new Float64Array(i+1),f=new Float64Array(i+1);for(let m=0;m<i;m++){let g=n[m]-c;h[m+1]=h[m]+g,f[m+1]=f[m]+g*g}let d=new Float64Array(i-a+1);for(let m=0;m<d.length;m++){let g=h[m+a]-h[m],y=f[m+a]-f[m];d[m]=Math.sqrt(Math.max((y-g*g/a)/(a-u),0))}return Q(d,[d.length],o,s)}l(wE,"rollingStd");function TE(r,e,t){let{data:n,device:s,dtype:o}=Ke(r),i=n.length,a=j_(e,i),u=new Float64Array(i-a+1),p=new Int32Array(i),c=0,h=0;for(let f=0;f<i;f++){for(;h>c&&t(n[f],n[p[h-1]]);)h--;p[h++]=f,p[c]<=f-a&&c++,f>=a-1&&(u[f-a+1]=n[p[c]])}return Q(u,[u.length],o,s)}l(TE,"rollingExtreme");function xE(r,e){return TE(r,e,(t,n)=>t<=n)}l(xE,"rollingMin");function SE(r,e){return TE(r,e,(t,n)=>t>=n)}l(SE,"rollingMax");function kE(r,e,t){let n=Ke(r),s=Ke(e),o=n.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]*n.data[p]}let u=Zf(Q(a,[o,i],"f64",n.device),Q(s.data,[o,1],"f64",n.device));return Q(ZV(Je(u)),[i],"f64",n.device)}l(kE,"polyfit");function dE(r,e){let t=0;for(let n=0;n<r.length;n++)t=t*e+r[n];return t}l(dE,"horner");function vE(r,e){let t=Ke(r).data;if(typeof e=="number")return dE(t,e);let n=Ke(e),s=new Float64Array(n.data.length);for(let o=0;o<s.length;o++)s[o]=dE(t,n.data[o]);return Q(s,[s.length],n.dtype,n.device)}l(vE,"polyval");function NE(r){let{data:e,device:t}=Ke(r),n=0;for(;n<e.length-1&&e[n]===0;)n++;let s=e.length-1-n;if(s<1)return Q(new Float64Array(0),[0,2],"f64",t);let o=new Float64Array(s+1);for(let h=0;h<=s;h++)o[h]=e[n+h]/e[n];let i=new Float64Array(s),a=new Float64Array(s),u=1,p=0;for(let h=0;h<s;h++){let f=u*pE-p*fE;p=u*fE+p*pE,u=f,i[h]=u,a[h]=p}for(let h=0;h<YV;h++){let f=0;for(let d=0;d<s;d++){let m=1,g=0;for(let S=1;S<=s;S++){let x=m*i[d]-g*a[d]+o[S];g=m*a[d]+g*i[d],m=x}let y=1,_=0;for(let S=0;S<s;S++){if(S===d)continue;let x=i[d]-i[S],k=a[d]-a[S],A=y*x-_*k;_=y*k+_*x,y=A}let b=y*y+_*_,w=(m*y+g*_)/b,T=(g*y-m*_)/b;i[d]-=w,a[d]-=T,f=Math.max(f,Math.hypot(w,T))}if(f<JV)break}let c=new Float64Array(2*s);for(let h=0;h<s;h++)c[2*h]=i[h],c[2*h+1]=a[h];return Q(c,[s,2],"f64",t)}l(NE,"polyroots");be();var XV=.0331;function eG(r){let e=Array.isArray(r)?r:[r],t=1;for(let n of e)t*=n;return{shp:e,n:t}}l(eG,"normalizeShape");var q_=class{static{l(this,"Generator")}_next;constructor(e){this._next=cr(e)}_uniformPositive(){let e=this._next();for(;e<=0;)e=this._next();return e}_normalDraw(){return Bi(this._uniformPositive())}_gammaDraw(e){if(e<1)return this._gammaDraw(e+1)*Math.pow(this._uniformPositive(),1/e);let t=e-1/3,n=1/Math.sqrt(9*t);for(;;){let s,o;do s=this._normalDraw(),o=1+n*s;while(o<=0);o=o*o*o;let i=this._uniformPositive();if(i<1-XV*s*s*s*s||Math.log(i)<.5*s*s+t*(1-o+Math.log(o)))return t*o}}_fill(e,t,n){let{shp:s,n:o}=eG(e),i=new Float64Array(o);for(let a=0;a<o;a++)i[a]=n();return Q(i,s,t.dtype??"f64",t.device)}uniform(e,t={}){let n=t.low??0,s=t.high??1;return this._fill(e,t,()=>n+(s-n)*this._next())}normal(e,t={}){let n=t.loc??0,s=t.scale??1;return this._fill(e,t,()=>n+s*this._normalDraw())}standardT(e,t={}){let n=t.df;return this._fill(e,t,()=>{let s=this._normalDraw(),o=2*this._gammaDraw(n/2);return s/Math.sqrt(o/n)})}chi2(e,t={}){let n=t.df;return this._fill(e,t,()=>2*this._gammaDraw(n/2))}exponential(e,t={}){let n=t.scale??1;return this._fill(e,t,()=>-n*Math.log(this._uniformPositive()))}multivariateNormal(e,t,n=1,s={}){let o=Ke(e),i=$_(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=Dx(Q(i.data,[a,a],p,u)),h=this.normal([n,a],{device:u,dtype:p}),f=Q(o.data,[a],p,u);return ae(lt(h,c.transpose(0,1)),f)}};l1();O1();D1();K1();aO();mO();AO();OO(We);export{Uf as Accuracy,Yp as Adam,Jp as AdamW,Rp as AdaptiveAvgPool2d,Bp as AvgPool2d,Lp as BCELoss,ul as BatchNorm1d,Ap as BatchNorm2d,Sa as BatchSampler,Us as CPUTarget,ks as CPU_DEVICE,Vf as CSVLogger,ed as CUDATarget,$n as Callback,Yf as ConfusionMatrix,Ca as ConsoleLogger,yp as Conv1d,gp as Conv2d,Qp as CosineAnnealingLR,Ip as CrossEntropyLoss,Kp as DataLoader,wa as Dataset,pr as Dropout,kp as ELU,Pf as EarlyStopping,Cp as Embedding,PO as F,Hf as F1Score,Vp as Flatten,bp as GELU,Od as GPU_DEVICE,Pp as GRU,ll as GRUCell,rl as GradMode,zf as GradientAccumulationScheduler,Na as LRScheduler,$p as LSTM,cl as LSTMCell,Is as LayerNorm,Sp as LeakyReLU,$f as LearningRateMonitor,If as LightningModule,hn as Linear,Np as LogSoftmax,$o as Logger,Mp as MSELoss,Up as MapDataset,Op as MaxPool2d,Gf as MeanMetric,Yn as Metric,qf as MetricCollection,Oi as ModelCheckpoint,ge as Module,zp as ModuleDict,di as ModuleList,mi as MultiheadAttention,Ep as NLLLoss,Yr as Optimizer,bt as Parameter,jp as PositionalEncoding,Kf as Precision,Ai as ProgressCallback,xa as RandomSampler,_p as ReLU,Wf as Recall,Xp as ReduceLROnPlateau,Hp as SGD,gi as Sampler,Fp as Sequential,Ta as SequentialSampler,wp as SiLU,Tp as Sigmoid,vp as Softmax,Zp as StepLR,jf as SumMetric,ft as SymbolicTensor,xp as Tanh,We as Tensor,qp as TensorDataset,Ff as Timer,Wp as Tokenizer,ue as TraceLevel,Df as Trainer,Gp as Transformer,fl as TransformerDecoder,ba as TransformerDecoderLayer,pl as TransformerEncoder,_a as TransformerEncoderLayer,yi as Vocab,sw as WASM_DEVICE,Ys as WEBGPU_DEVICE,td as WasmTarget,nd as WebGPUTarget,Pc as abs,ae as add,Mx as applyCheckpoint,n1 as arange,Ud as argmax,kN as argmin,LN as argsort,ON as broadcast_in_dim,sa as cat,wN as ceil,BN as chunk,Vd as clamp,ef as clipGradNorm_,tf as clipGradValue_,AN as clone,u3 as compile,w_ as compileWithBackward,ci as contiguous,mN as cos,IN as cumsum,FO as data,ka as defaultCollate,Nt as dispatcher,$t as div,NN as dot,fn as empty,QP as emptyLike,yO as enableGrad,$c as eq,gN as erf,yN as erfc,Kr as exp,Lu as expand,Rm as eye,MN as flip,bN as floor,ZN as flushWebGPUEager,ta as fromBuffer,et as full,t$ as fullLike,Cd as gamma,jd as gather,zd as ge,Dd as gelu,ii as getDefaultDevice,Iu as gt,li as index_select,Sg as init,Fd as le,_N as lgamma,dI as lightning,mI as linalg,s1 as linspace,Rx as loadCheckpoint,Zs as log,Hd as log_softmax,xN as lt,lt as matmul,Qs as max,ai as maximum,lr as mean,dn as memfs,SN as min,Ld as minimum,bI as ml,ee as mul,Un as narrow,TN as ne,Ur as neg,$O as nn,gO as noGrad,AE as numeric,Gd as one_hot,Ht as ones,e$ as onesLike,Ft as ops,kM as optim,na as pad,Eu as permute,Js as pow,eA as preloadCudaRuntime,qc as preloadWebGPU,ZP as printModule,vN as prod,Bm as randn,n$ as randnLike,r1 as randperm,Mu as relu,Du as repeat,Cn as reshape,RN as roll,hN as rsqrt,Dc as scalar,Dp as scan,qd as scatter,ra as scatter_add,Ut as select,Cf as serializeCheckpoint,O2 as setDefaultDevice,ur as sigmoid,$d as sign,Pd as silu,dN as sin,Kd as slice,Ro as softmax,EN as sort,Bo as split,ui as sqrt,Cu as squeeze,Wr as stack,Pe as sub,ht as sum,Ns as tanh,Pt as tensor,Wd as tile,qO as tokenizer,CN as topk,HM as trace,gt as transpose,Sr as unsqueeze,qn as where,$e as zeros,XP as zerosLike};