@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.
- package/dist/index.browser.js +284 -0
- package/dist/index.node.js +428 -0
- package/package.json +9 -24
- package/dist/cli.js +0 -411
- package/dist/index.js +0 -207
- package/patches/terminal-kit+3.1.2.patch +0 -140
package/dist/index.js
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
var jl=Object.defineProperty;var QR=Object.getOwnPropertyDescriptor;var tO=Object.getOwnPropertyNames;var eO=Object.prototype.hasOwnProperty;var f=(n,t)=>jl(n,"name",{value:t,configurable:!0});var T=(n,t)=>()=>(n&&(t=n(n=0)),t);var Ft=(n,t)=>{for(var e in t)jl(n,e,{get:t[e],enumerable:!0})},sO=(n,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of tO(t))!eO.call(n,r)&&r!==e&&jl(n,r,{get:()=>t[r],enumerable:!(s=QR(t,r))||s.enumerable});return n};var nO=n=>sO(jl({},"__esModule",{value:!0}),n);var ew={};Ft(ew,{ArgKind:()=>ns,OperatorSchema:()=>Hl,SchemaArg:()=>Kl,parseSchema:()=>Mm});function Mm(n,t){let e=t||"mlc",s=n.indexOf("->"),r=s>=0?n.substring(0,s).trim():n.trim(),o=s>=0?n.substring(s+2).trim():"",i=r.indexOf("("),a=r.lastIndexOf(")"),c,l="",u=r.substring(0,i).trim(),h=u.indexOf(".");h>=0?(c=u.substring(0,h),l=u.substring(h+1)):c=u;let p=r.substring(i+1,a).trim(),d=p.length>0?oO(p):[],m=o.length>0?iO(o):[{kind:ns.TENSOR}];return new Hl(e,c,l,d,m)}function oO(n){return tw(n,",").map(e=>{let s=e.trim(),r=s.indexOf("="),o=s,i=null;r>=0&&(o=s.substring(0,r).trim(),i=s.substring(r+1).trim());let a=o.endsWith("!");a&&(o=o.substring(0,o.length-1).trim());let c=o.lastIndexOf(" "),l,u;c>=0?(l=o.substring(0,c).trim(),u=o.substring(c+1).trim()):(l=o,u="");let h=Im.get(l)||ns.SCALAR;return new Kl(u,h,i,a)})}function iO(n){let t=n.trim();if(t.startsWith("(")&&t.endsWith(")")){let s=t.substring(1,t.length-1);return tw(s,",").map(o=>{let i=o.trim();return{kind:Im.get(i)||ns.TENSOR}})}return[{kind:Im.get(t)||ns.TENSOR}]}function tw(n,t){let e=[],s=0,r=0;for(let o=0;o<n.length;o++){let i=n[o];i==="("||i==="["?s++:i===")"||i==="]"?s--:i===t&&s===0&&(e.push(n.substring(r,o)),r=o+1)}return e.push(n.substring(r)),e}var ns,rO,Kl,Hl,Im,Cm=T(()=>{ns=Object.freeze({TENSOR:"Tensor",SCALAR:"Scalar",INT:"int",FLOAT:"float",BOOL:"bool",INT_LIST:"int[]",TENSOR_LIST:"Tensor[]",DTYPE:"Dtype",DEVICE:"Device",STRING:"str",MEMORY_FORMAT:"MemoryFormat"}),rO=new Set([ns.TENSOR,ns.TENSOR_LIST]),Kl=class{static{f(this,"SchemaArg")}constructor(t,e,s,r){this.name=t,this.kind=e,this.defaultValue=s??null,this.isOut=r??!1}get isTensor(){return rO.has(this.kind)}},Hl=class{static{f(this,"OperatorSchema")}constructor(t,e,s,r,o){this.namespace=t,this.name=e,this.overload=s||"",this.args=Object.freeze(r),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 t=[];for(let e=0;e<this.args.length;e++)this.args[e].isTensor&&t.push(e);this._tensorArgIndices=Object.freeze(t)}return this._tensorArgIndices}get numTensorArgs(){return this.tensorArgIndices.length}},Im=new Map([["Tensor",ns.TENSOR],["Scalar",ns.SCALAR],["int",ns.INT],["float",ns.FLOAT],["bool",ns.BOOL],["int[]",ns.INT_LIST],["Tensor[]",ns.TENSOR_LIST],["Dtype",ns.DTYPE],["Device",ns.DEVICE],["str",ns.STRING],["MemoryFormat",ns.MEMORY_FORMAT]]);f(Mm,"parseSchema");f(oO,"_parseArgs");f(iO,"_parseReturns");f(tw,"_splitTopLevel")});function sw(n){return n instanceof Us?n:n&&typeof n=="object"&&n._impl?Us.tensor(n):typeof n=="number"?Number.isInteger(n)?Us.int(n):Us.float(n):typeof n=="boolean"?Us.bool(n):typeof n=="string"?Us.string(n):Array.isArray(n)?n.length>0&&n[0]&&n[0]._impl?Us.tensorList(n):Us.intList(n):(n==null,Us.none())}var rs,Us,Ls,ec=T(()=>{rs=Object.freeze({TENSOR:0,INT:1,FLOAT:2,BOOL:3,INT_LIST:4,TENSOR_LIST:5,STRING:6,NONE:7,DEVICE:8,DTYPE:9}),Us=class n{static{f(this,"IValue")}constructor(t,e){this.tag=t,this.value=e}static tensor(t){return new n(rs.TENSOR,t)}static int(t){return new n(rs.INT,t)}static float(t){return new n(rs.FLOAT,t)}static bool(t){return new n(rs.BOOL,t)}static intList(t){return new n(rs.INT_LIST,t)}static tensorList(t){return new n(rs.TENSOR_LIST,t)}static string(t){return new n(rs.STRING,t)}static none(){return new n(rs.NONE,null)}static device(t){return new n(rs.DEVICE,t)}static dtype(t){return new n(rs.DTYPE,t)}isTensor(){return this.tag===rs.TENSOR}isInt(){return this.tag===rs.INT}isFloat(){return this.tag===rs.FLOAT}isBool(){return this.tag===rs.BOOL}isIntList(){return this.tag===rs.INT_LIST}isTensorList(){return this.tag===rs.TENSOR_LIST}isString(){return this.tag===rs.STRING}isNone(){return this.tag===rs.NONE}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}},Ls=class n{static{f(this,"KernelFunction")}constructor(t,e){this._boxed=t||null,this._unboxed=e||null}static fromBoxed(t){return new n(t,null)}static fromUnboxed(t){return new n(null,t)}static fromBoth(t,e){return new n(t,e)}get isBoxed(){return this._boxed!==null}get isUnboxed(){return this._unboxed!==null}callUnboxed(t,...e){return this._unboxed?this._unboxed(t,...e):this._callBoxedAsUnboxed(t,e)}callBoxed(t,e){return this._boxed?this._boxed(t,e):this._callUnboxedAsBoxed(t,e)}_callBoxedAsUnboxed(t,e){let s=e.map(o=>sw(o)),r=this._boxed(t,s);return Array.isArray(r)?r.length===1?r[0].value:r.map(o=>o.value):r instanceof Us?r.value:r}_callUnboxedAsBoxed(t,e){let s=e.map(o=>o.value),r=this._unboxed(t,...s);return r==null?[]:[sw(r)]}};f(sw,"_toIValue")});function nw(n){dr=n}var dr,sc,Dm=T(()=>{Cm();ec();dr=null;f(nw,"_setDispatcher");sc=class{static{f(this,"Library")}constructor(t,e){this._namespace=t,this._kind=e,this._registrations=[]}def(t){let e=Mm(t,this._namespace);return dr&&dr.registerOp(e),this._registrations.push({type:"def",schema:e}),this}impl(t,e,s){let r=s instanceof Ls?s:Ls.fromUnboxed(s);return dr&&dr.registerKernel(`${this._namespace}::${t}`,e,r),this._registrations.push({type:"impl",name:t,key:e,kernelFn:r}),this}implBoxed(t,e,s){let r=Ls.fromBoxed(s);return dr&&dr.registerKernel(`${this._namespace}::${t}`,e,r),this._registrations.push({type:"impl",name:t,key:e,kernelFn:r}),this}fallback(t,e){let s=e instanceof Ls?e:Ls.fromBoxed(e);return dr&&dr.registerFallback(t,s),this._registrations.push({type:"fallback",key:t,kernelFn:s}),this}replay(t){for(let e of this._registrations)e.type==="def"?t.registerOp(e.schema):e.type==="impl"?t.registerKernel(`${this._namespace}::${e.name}`,e.key,e.kernelFn):e.type==="fallback"&&t.registerFallback(e.key,e.kernelFn)}}});function di(n){let t=aO[n];if(t===void 0)throw new Error(`No backend key for device: ${n}`);return t}function iw(n){return cO[n]??nt.AUTOGRAD}function rw(n){return n=n-(n>>1&1431655765),n=(n&858993459)+(n>>2&858993459),(n+(n>>4)&252645135)*16843009>>24}var nt,ow,aO,cO,Ws,nc,V2,rc,G2,js=T(()=>{nt=Object.freeze({CPU:0,GPU:1,WASM:2,META:3,LAZY:4,CUSTOM_0:5,CUSTOM_1:6,CUSTOM_2:7,CUSTOM_3:8,BATCHED:20,VMAP:24,FUNCTIONALIZE:28,AUTOCAST:32,AUTOGRAD:40,AUTOGRAD_CPU:41,AUTOGRAD_GPU:42,AUTOGRAD_WASM:43,TRACING:48,NUM_KEYS:49}),ow=new Array(nt.NUM_KEYS).fill(null);for(let[n,t]of Object.entries(nt))n!=="NUM_KEYS"&&t<nt.NUM_KEYS&&(ow[t]=n);aO=Object.freeze({cpu:nt.CPU,gpu:nt.GPU,wasm:nt.WASM,webgpu:nt.CUSTOM_0,meta:nt.META,lazy:nt.LAZY}),cO=Object.freeze({[nt.CPU]:nt.AUTOGRAD_CPU,[nt.GPU]:nt.AUTOGRAD_GPU,[nt.WASM]:nt.AUTOGRAD_WASM});f(di,"backendKeyForDevice");f(iw,"autogradKeyForBackend");Ws=class n{static{f(this,"DispatchKeySet")}constructor(t,e){this._lo=t|0,this._hi=e|0}static fromKey(t){return t<32?new n(1<<t,0):new n(0,1<<t-32)}static fromKeys(...t){let e=0,s=0;for(let r=0;r<t.length;r++){let o=t[r];o<32?e|=1<<o:s|=1<<o-32}return new n(e,s)}add(t){return t<32?new n(this._lo|1<<t,this._hi):new n(this._lo,this._hi|1<<t-32)}remove(t){return t<32?new n(this._lo&~(1<<t),this._hi):new n(this._lo,this._hi&~(1<<t-32))}has(t){return t<32?(this._lo&1<<t)!==0:(this._hi&1<<t-32)!==0}without(t){return this.remove(t)}union(t){return new n(this._lo|t._lo,this._hi|t._hi)}intersect(t){return new n(this._lo&t._lo,this._hi&t._hi)}subtract(t){return new n(this._lo&~t._lo,this._hi&~t._hi)}isEmpty(){return this._lo===0&&this._hi===0}equals(t){return this._lo===t._lo&&this._hi===t._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 rw(this._lo)+rw(this._hi)}*[Symbol.iterator](){let t=this._hi;for(;t!==0;){let s=31-Math.clz32(t);yield s+32,t&=~(1<<s)}let e=this._lo;for(;e!==0;){let s=31-Math.clz32(e);yield s,e&=~(1<<s)}}toString(){let t=[];for(let e of this)t.push(ow[e]||String(e));return`DispatchKeySet(${t.join(", ")})`}};f(rw,"_popcount32");nc=new Ws(0,0),V2=Ws.fromKeys(nt.CPU,nt.GPU,nt.WASM,nt.META,nt.LAZY,nt.CUSTOM_0,nt.CUSTOM_1,nt.CUSTOM_2,nt.CUSTOM_3),rc=Ws.fromKeys(nt.AUTOGRAD,nt.AUTOGRAD_CPU,nt.AUTOGRAD_GPU,nt.AUTOGRAD_WASM),G2=Ws.fromKeys(nt.BATCHED,nt.VMAP,nt.FUNCTIONALIZE,nt.AUTOCAST,nt.AUTOGRAD,nt.AUTOGRAD_CPU,nt.AUTOGRAD_GPU,nt.AUTOGRAD_WASM,nt.TRACING)});var Yl,aw=T(()=>{js();Yl=class{static{f(this,"OperatorEntry")}constructor(t){this._schema=t,this._kernels=new Array(nt.NUM_KEYS).fill(null),this._catchAll=null}get schema(){return this._schema}registerKernel(t,e){this._kernels[t]=e}removeKernel(t){this._kernels[t]=null}lookupKernel(t){return this._kernels[t]}hasKernel(t){return this._kernels[t]!==null}get catchAll(){return this._catchAll}setCatchAll(t){this._catchAll=t}bestKernel(t){for(let e of t){let s=this._kernels[e];if(s)return{key:e,kernel:s}}return this._catchAll?{key:-1,kernel:this._catchAll}:null}registeredKeys(){let t=[];for(let e=0;e<this._kernels.length;e++)this._kernels[e]&&t.push(e);return t}}});var Jl,cw=T(()=>{Jl=class{static{f(this,"OperatorHandle")}constructor(t,e){this._entry=t,this._schema=e}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(t){return this._entry.lookupKernel(t)}bestKernel(t){return this._entry.bestKernel(t)}}});var Zl,uw=T(()=>{js();Zl=class{static{f(this,"FallbackTable")}constructor(){this._kernels=new Array(nt.NUM_KEYS).fill(null)}register(t,e){this._kernels[t]=e}remove(t){this._kernels[t]=null}lookup(t){return this._kernels[t]}has(t){return this._kernels[t]!==null}registeredKeys(){let t=[];for(let e=0;e<this._kernels.length;e++)this._kernels[e]&&t.push(e);return t}}});function lw(n,t){go.push(null,n);let e;try{e=t()}catch(s){throw go.pop(),s}return e&&typeof e.then=="function"?e.then(s=>(go.pop(),s),s=>{throw go.pop(),s}):(go.pop(),e)}var Lm,go,Pm=T(()=>{js();Lm=class{static{f(this,"GuardStack")}constructor(){this._frames=[]}push(t,e){this._frames.push({exclude:t||nc,include:e||nc})}pop(){return this._frames.pop()}apply(t){let e=t;for(let s=this._frames.length-1;s>=0;s--){let r=this._frames[s];e=e.subtract(r.exclude),e=e.union(r.include)}return e}get depth(){return this._frames.length}clear(){this._frames.length=0}},go=new Lm;f(lw,"withIncludedKeys")});function $m(n,t){if(!t)return n;if(t.dispatchKeySet)return n.union(t.dispatchKeySet);if(Array.isArray(t))for(let e=0;e<t.length;e++)n=$m(n,t[e]);return n}function Fm(n,t){let e=nc;if(t){let s=t.tensorArgIndices;for(let r=0;r<s.length;r++){let o=s[r];o<n.length&&(e=$m(e,n[o]))}}else for(let s=0;s<n.length;s++)e=$m(e,n[s]);return e}var zm,Re,mi=T(()=>{js();aw();cw();uw();Pm();Dm();zm=class{static{f(this,"Dispatcher")}constructor(){this._entries=new Map,this._handles=new Map,this._fallbacks=new Zl}registerOp(t){let e=t.key();if(this._handles.has(e))return this._handles.get(e);let s=new Yl(t);this._entries.set(e,s);let r=new Jl(s,t);return this._handles.set(e,r),r}findOp(t){let e=t.includes("::")?t:`mlc::${t}`;return this._handles.get(e)||null}findOrRegisterOp(t){let e=this.findOp(t);if(!e){let{parseSchema:s}=(Cm(),nO(ew)),r=s(`${t}() -> Tensor`,"mlc");e=this.registerOp(r)}return e}registerKernel(t,e,s){let r=t.includes("::")?t:`mlc::${t}`,o=this._entries.get(r);if(!o)throw new Error(`Op '${r}' not registered`);o.registerKernel(e,s)}registerFallback(t,e){this._fallbacks.register(t,e)}dispatch(t,e,...s){let r=go.apply(e);return this._dispatchInternal(t,r,s)}redispatch(t,e,...s){return this._dispatchInternal(t,e,s)}_dispatchInternal(t,e,s){let r=e.highestPriority();if(r<0)throw new Error(`No dispatch key found for op '${t.name}'`);let o=t.lookupKernel(r);if(o||(o=this._fallbacks.lookup(r)),!o){let a=t.entry.catchAll;a&&(o=a)}if(!o)throw new Error(`No kernel registered for op '${t.name}' with dispatch key ${r}`);let i=e.without(r);return o.callUnboxed(i,...s)}callOp(t,...e){let s=this.findOp(t);if(!s)throw new Error(`Op '${t}' not found`);let r=Fm(e,s.schema);return this.dispatch(s,r,...e)}listOps(){return[...this._handles.keys()]}hasOp(t){let e=t.includes("::")?t:`mlc::${t}`;return this._handles.has(e)}get fallbacks(){return this._fallbacks}};f($m,"_unionArg");f(Fm,"computeKeySet");Re=new zm;nw(Re)});function mr(n){let t=fw[n];if(t===void 0)throw new Error(`Unknown dtype: ${n}`);return t}function Oe(n){return uO.has(n)}function Um(n){return lO.has(n)}function fO(n,t){return n===t?n:n===1?t:t===1?n:n===Z?t===Z?Z:t:t===Z?n:null}function qm(n,t){return n===t?!0:n&&typeof n.equals=="function"?n.equals(t):!1}function us(n){if(n instanceof E)return`tensor<${n.shape.map(e=>e===Z?"?":String(e)).join("x")}x${n.dtype}>`;if(n instanceof _o)return`tuple<${n.elements.map(us).join(", ")}>`;if(n instanceof Vm)return"token";if(n instanceof Gm){let t=n.inputs.map(us).join(", "),e=n.outputs.map(us).join(", ");return`(${t}) -> (${e})`}return"unknown"}var J,fw,uO,lO,Z,ye,E,_o,Vm,Gm,kt=T(()=>{J=Object.freeze({F16:"f16",BF16:"bf16",F32:"f32",F64:"f64",I8:"i8",I16:"i16",I32:"i32",I64:"i64",UI8:"ui8",BOOL:"bool",INDEX:"index"}),fw=Object.freeze({[J.F16]:2,[J.BF16]:2,[J.F32]:4,[J.F64]:8,[J.I8]:1,[J.I16]:2,[J.I32]:4,[J.I64]:8,[J.UI8]:1,[J.BOOL]:1,[J.INDEX]:4}),uO=new Set([J.F16,J.BF16,J.F32,J.F64]),lO=new Set([J.I8,J.I16,J.I32,J.I64,J.UI8]),Z=-1;f(mr,"scalarBytes");f(Oe,"isFloatType");f(Um,"isIntType");ye=class n{static{f(this,"Layout")}constructor(t){this.order=Object.freeze([...t]),this._hash=null}static rowMajor(t){let e=new Array(t);for(let s=0;s<t;s++)e[s]=s;return new n(e)}static columnMajor(t){let e=new Array(t);for(let s=0;s<t;s++)e[s]=t-1-s;return new n(e)}get rank(){return this.order.length}isIdentity(){for(let t=0;t<this.order.length;t++)if(this.order[t]!==t)return!1;return!0}inverse(){let t=new Array(this.order.length);for(let e=0;e<this.order.length;e++)t[this.order[e]]=e;return new n(t)}compose(t){if(this.order.length!==t.order.length)throw new Error("Cannot compose layouts of different ranks");let e=new Array(this.order.length);for(let s=0;s<this.order.length;s++)e[s]=this.order[t.order[s]];return new n(e)}computeStrides(t){let e=t.length,s=new Array(e),r=1;for(let o=e-1;o>=0;o--){let i=this.order[o];s[i]=r,t[i]===Z?r=Z:r!==Z&&(r*=t[i])}return s}equals(t){if(this===t)return!0;if(!(t instanceof n)||this.order.length!==t.order.length)return!1;for(let e=0;e<this.order.length;e++)if(this.order[e]!==t.order[e])return!1;return!0}hash(){if(this._hash!==null)return this._hash;let t=2166136261;for(let e=0;e<this.order.length;e++)t=(t^this.order[e])*16777619&2147483647;return this._hash=t,t}};f(fO,"broadcastDim");E=class n{static{f(this,"TensorType")}constructor(t,e,s=null){this.shape=Object.freeze([...t]),this.dtype=e,this.layout=s||ye.rowMajor(t.length),this._hash=null}get rank(){return this.shape.length}get isScalar(){return this.shape.length===0}get isFullyStatic(){return this.shape.every(t=>t>=0)}get hasDynamic(){return this.shape.some(t=>t===Z)}numel(){if(this.hasDynamic)return Z;let t=1;for(let e=0;e<this.shape.length;e++)t*=this.shape[e];return t}sizeInBytes(){let t=this.numel();return t===Z?Z:t*fw[this.dtype]}strides(){return this.layout.computeStrides(this.shape)}withShape(t){return new n(t,this.dtype,this.layout)}withDtype(t){return new n(this.shape,t,this.layout)}withLayout(t){return new n(this.shape,this.dtype,t)}equals(t){if(this===t)return!0;if(!(t instanceof n)||this.dtype!==t.dtype||this.shape.length!==t.shape.length)return!1;for(let e=0;e<this.shape.length;e++)if(this.shape[e]!==t.shape[e])return!1;return this.layout.equals(t.layout)}shapeEquals(t){if(!(t instanceof n)||this.dtype!==t.dtype||this.shape.length!==t.shape.length)return!1;for(let e=0;e<this.shape.length;e++)if(this.shape[e]!==t.shape[e])return!1;return!0}shapeCompatible(t){if(this.shape.length!==t.shape.length)return!1;for(let e=0;e<this.shape.length;e++){let s=this.shape[e],r=t.shape[e];if(!(s===Z||r===Z)&&s!==r)return!1}return!0}hash(){if(this._hash!==null)return this._hash;let t=2166136261;for(let e=0;e<this.shape.length;e++)t=(t^this.shape[e]&65535)*16777619&2147483647;return t=(t^this.dtype.charCodeAt(0))*16777619&2147483647,this._hash=t,t}static broadcastShape(...t){let e=0;for(let r=0;r<t.length;r++)t[r].length>e&&(e=t[r].length);let s=new Array(e);for(let r=0;r<e;r++){let o=1;for(let i=0;i<t.length;i++){let a=t[i],c=r-(e-a.length);if(!(c<0)&&(o=fO(o,a[c]),o===null))return null}s[r]=o}return s}static broadcastCompatible(...t){return n.broadcastShape(...t)!==null}},_o=class n{static{f(this,"TupleType")}constructor(t){this.elements=Object.freeze([...t])}equals(t){if(this===t)return!0;if(!(t instanceof n)||this.elements.length!==t.elements.length)return!1;for(let e=0;e<this.elements.length;e++)if(!qm(this.elements[e],t.elements[e]))return!1;return!0}},Vm=class n{static{f(this,"TokenType")}equals(t){return t instanceof n}},Gm=class n{static{f(this,"FunctionType")}constructor(t,e){this.inputs=Object.freeze([...t]),this.outputs=Object.freeze([...e])}equals(t){if(this===t)return!0;if(!(t instanceof n)||this.inputs.length!==t.inputs.length||this.outputs.length!==t.outputs.length)return!1;for(let e=0;e<this.inputs.length;e++)if(!qm(this.inputs[e],t.inputs[e]))return!1;for(let e=0;e<this.outputs.length;e++)if(!qm(this.outputs[e],t.outputs[e]))return!1;return!0}};f(qm,"typeEquals");f(us,"typeToString")});var pO,oc,yo,ic,Xl=T(()=>{pO=0,oc=class{static{f(this,"UseLink")}constructor(t,e){this.user=t,this.operandIndex=e,this.prev=null,this.next=null}},yo=class{static{f(this,"Value")}constructor(t,e=null,s=0){this.type=t,this.definingOp=e,this.resultIndex=s,this.id=pO++,this._useHead=null,this._useTail=null,this._useCount=0}get hasUses(){return this._useHead!==null}get useCount(){return this._useCount}addUse(t){t.prev=this._useTail,t.next=null,this._useTail?this._useTail.next=t:this._useHead=t,this._useTail=t,this._useCount++}removeUse(t){t.prev?t.prev.next=t.next:this._useHead=t.next,t.next?t.next.prev=t.prev:this._useTail=t.prev,t.prev=null,t.next=null,this._useCount--}*uses(){let t=this._useHead;for(;t;){let e=t.next;yield t,t=e}}getUsers(){let t=[],e=this._useHead;for(;e;)t.push(e.user),e=e.next;return t}replaceAllUsesWith(t){if(this===t)return;let e=this._useHead;for(;e;)e.user.operands[e.operandIndex]=t,e=e.next;this._useHead&&(t._useTail?(t._useTail.next=this._useHead,this._useHead.prev=t._useTail):t._useHead=this._useHead,t._useTail=this._useTail,t._useCount+=this._useCount),this._useHead=null,this._useTail=null,this._useCount=0}isBlockArgument(){return!1}},ic=class extends yo{static{f(this,"BlockArgument")}constructor(t,e,s){super(t,null,0),this.ownerBlock=e,this.argIndex=s}isBlockArgument(){return!0}}});var Ne,ee,Gr=T(()=>{Xl();Ne=class{static{f(this,"Block")}constructor(t=[]){this.parentRegion=null,this._head=null,this._tail=null,this._size=0,this.arguments=t.map((e,s)=>new ic(e,this,s))}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}getArgument(t){return this.arguments[t]}addArgument(t){let e=new ic(t,this,this.arguments.length);return this.arguments.push(e),e}removeArguments(t){let e=t instanceof Set?t:new Set(t);if(e.size===0)return this;let s=[];for(let r=0;r<this.arguments.length;r++)e.has(r)||s.push(this.arguments[r]);this.arguments=s;for(let r=0;r<s.length;r++)s[r].argIndex=r;return this}pushOp(t){t.parentBlock=this,t._prev=this._tail,t._next=null,this._tail?this._tail._next=t:this._head=t,this._tail=t,this._size++}insertBefore(t,e){t.parentBlock=this,t._prev=e._prev,t._next=e,e._prev?e._prev._next=t:this._head=t,e._prev=t,this._size++}insertAfter(t,e){t.parentBlock=this,t._prev=e,t._next=e._next,e._next?e._next._prev=t:this._tail=t,e._next=t,this._size++}removeOp(t){t.parentBlock===this&&(t._prev?t._prev._next=t._next:this._head=t._next,t._next?t._next._prev=t._prev:this._tail=t._prev,t._prev=null,t._next=null,t.parentBlock=null,this._size--)}*ops(){let t=this._head;for(;t;){let e=t._next;yield t,t=e}}*opsReverse(){let t=this._tail;for(;t;){let e=t._prev;yield t,t=e}}opsArray(){let t=[],e=this._head;for(;e;)t.push(e),e=e._next;return t}[Symbol.iterator](){return this.ops()}},ee=class{static{f(this,"Region")}constructor(t=[]){this.parentOp=null,this.blocks=[];for(let e=0;e<t.length;e++)this.addBlock(t[e])}get entryBlock(){return this.blocks[0]||null}get isEmpty(){return this.blocks.length===0}addBlock(t){return t.parentRegion=this,this.blocks.push(t),t}insertBlock(t,e){e.parentRegion=this,this.blocks.splice(t,0,e)}removeBlock(t){let e=this.blocks.indexOf(t);e!==-1&&(this.blocks.splice(e,1),t.parentRegion=null)}*[Symbol.iterator](){yield*this.blocks}}});var Me,U,H,Ql,we=T(()=>{Me=Object.freeze({NONE:0,READ:1,WRITE:2,ALLOCATE:4,CONTROL:8}),U=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"}),H=class{static{f(this,"OpDef")}constructor(t){this.name=t.name,this.numOperands=t.numOperands,this.numResults=t.numResults!==void 0?t.numResults:1,this.attrs=Object.freeze(t.attrs||[]),this.sideEffects=t.sideEffects||Me.NONE,this.traits=new Set(t.traits||[]),this.inferResultTypes=t.inferResultTypes||null,this.propagateSymbolicShapes=t.propagateSymbolicShapes||null,this.verify=t.verify||null,this.getMemoryEffects=t.getMemoryEffects||null,this.fold=t.fold||null,this.getCanonicalizationPatterns=t.getCanonicalizationPatterns||null,this.getFlops=t.getFlops||null,this.hasRegions=t.hasRegions||!1,this.numRegions=t.numRegions||0,this.regionSpecs=t.regions||null,this.genericAttrs=new Map(Object.entries(t.opAttrs||{}))}setAttr(t,e){return this.genericAttrs.set(t,e),this}getAttr(t){return this.genericAttrs.has(t)?this.genericAttrs.get(t):null}hasAttr(t){return this.genericAttrs.has(t)}hasTrait(t){return this.traits.has(t)}addTrait(t){return this.traits.add(t),this}get isCommutative(){return this.traits.has(U.COMMUTATIVE)}get isAssociative(){return this.traits.has(U.ASSOCIATIVE)}get isElementwise(){return this.traits.has(U.ELEMENTWISE)}get isTerminator(){return this.traits.has(U.TERMINATOR)}get isConstant(){return this.traits.has(U.CONSTANT)}get isReduction(){return this.traits.has(U.REDUCTION)}get isBroadcast(){return this.traits.has(U.BROADCAST)}get isInjective(){return this.traits.has(U.INJECTIVE)}get isOutEWiseFusable(){return this.traits.has(U.OUT_EWISE_FUSABLE)}get isOpaque(){return this.traits.has(U.OPAQUE)}get hasSideEffects(){return this.sideEffects!==Me.NONE}},Ql=class{static{f(this,"OpRegistry")}constructor(){this._defs=new Map}register(t){if(this._defs.has(t.name))throw new Error(`Op '${t.name}' already registered`);this._defs.set(t.name,t)}registerOpAttr(t,e,s){let r=this._defs.get(t);if(!r)throw new Error(`registerOpAttr: op '${t}' not registered`);return r.setAttr(e,s),r}registerTrait(t,e){let s=this._defs.get(t);if(!s)throw new Error(`registerTrait: op '${t}' not registered`);return s.addTrait(e),s}get(t){return this._defs.get(t)||null}has(t){return this._defs.has(t)}allOps(){return[...this._defs.values()]}names(){return[...this._defs.keys()]}}});var Q,tf,ef,Rt,sf,Ce=T(()=>{Q=Object.freeze({UNCHANGED:0,CHANGED:1,FAILED:2}),tf=class{static{f(this,"Pass")}constructor(t){this.name=t,this.preservedAnalyses=new Set,this.invalidatedAnalyses=new Set,this.requiredAnalyses=[],this.optLevel=0,this.trace=null}run(t,e){throw new Error("Not implemented")}},ef=class{static{f(this,"PassContext")}constructor({optLevel:t=1/0,disabledPasses:e=[],config:s={}}={}){this.optLevel=t,this.disabledPasses=e instanceof Set?e:new Set(e),this.config=s instanceof Map?s:new Map(Object.entries(s))}shouldRun(t){return!(this.disabledPasses.has(t.name)||(t.optLevel||0)>this.optLevel)}},Rt=class extends tf{static{f(this,"FunctionPass")}run(t,e){throw new Error("Not implemented")}},sf=class extends tf{static{f(this,"ModulePass")}run(t,e){throw new Error("Not implemented")}}});var ot,qe,hO,nf,Ue=T(()=>{Ce();ot=Object.freeze({SILENT:0,INFO:1,VERBOSE:2,DEBUG:3}),qe=class{static{f(this,"CompilationError")}constructor(t,e,s,r){this.phase=t,this.funcName=e,this.message=s,this.passName=r||null}toString(){let t="["+this.phase+"]";return this.funcName&&(t+=" "+this.funcName),this.passName&&(t+=" ("+this.passName+")"),t+=": "+this.message,t}},hO=f(()=>{},"NOOP"),nf=class{static{f(this,"TraceLog")}constructor(t={}){this.level=t.level??ot.SILENT,this.sink=typeof t.sink=="function"?t.sink:hO,this.irSnapshot={afterGraphPasses:!1,afterLowering:!1,afterScheduling:!1,...t.irSnapshot||{}},this._compileStart=0}emit(t){t.level>this.level||(t.timestamp=performance.now(),this.sink(t))}phaseStart(t){this.emit({type:"phase",action:"start",phase:t,level:ot.INFO})}phaseEnd(t,e){this.emit({type:"phase",action:"end",phase:t,durationMs:e,level:ot.INFO})}passRun(t,e,s,r,o){this.emit({type:"pass",passName:t,changed:e===Q.CHANGED,durationMs:s,opCountBefore:r,opCountAfter:o,level:ot.VERBOSE})}functionEvent(t,e,s){this.emit({type:"function",phase:t,funcName:e,...s,level:ot.INFO})}irDump(t,e){this.emit({type:"ir_snapshot",label:t,text:e,level:ot.DEBUG})}memoryStats(t,e){this.emit({type:"memory",funcName:t,...e,level:ot.VERBOSE})}autotuneStats(t,e){this.emit({type:"autotune",funcName:t,...e,level:ot.VERBOSE})}codegenStats(t,e){this.emit({type:"codegen",funcName:t,...e,level:ot.VERBOSE})}errorEvent(t,e,s,r){this.emit({type:"error",phase:t,funcName:e,message:s,passName:r||null,level:ot.INFO})}explain(t,e,s,r,o){this.emit({type:"explain",category:t,subject:e,decision:s,reason:r,...o||{},level:ot.DEBUG})}get explainsEnabled(){return this.level>=ot.DEBUG}shouldSnapshot(t){return this.level>=ot.DEBUG&&!!this.irSnapshot[t]}}});var Ht,gi,_i,ac=T(()=>{Ce();zn();Ue();Ht=class{static{f(this,"Pattern")}constructor(t,e=1){this.name=t,this.benefit=e,this.rootOpName=null}match(t){return!1}rewrite(t,e){return!1}},gi=class{static{f(this,"PatternSet")}constructor(){this.patterns=[],this._byOp=new Map,this._generic=[],this._sorted=!1}add(t){if(this.patterns.push(t),this._sorted=!1,t.rootOpName){let e=this._byOp.get(t.rootOpName);e||(e=[],this._byOp.set(t.rootOpName,e)),e.push(t)}else this._generic.push(t)}_ensureSorted(){if(this._sorted)return;let t=f((e,s)=>s.benefit-e.benefit,"cmp");for(let[,e]of this._byOp)e.sort(t);this._generic.sort(t),this._sorted=!0}get(){return[...this.patterns].sort((t,e)=>e.benefit-t.benefit)}getForOp(t){this._ensureSorted();let e=this._byOp.get(t);if(!e)return this._generic;if(this._generic.length===0)return e;let s=new Array(e.length+this._generic.length),r=0,o=0,i=0;for(;r<e.length&&o<this._generic.length;)e[r].benefit>=this._generic[o].benefit?s[i++]=e[r++]:s[i++]=this._generic[o++];for(;r<e.length;)s[i++]=e[r++];for(;o<this._generic.length;)s[i++]=this._generic[o++];return s}hasPatterns(){return this.patterns.length>0}},_i=class{static{f(this,"PatternApplicator")}constructor(t){this.patternSet=t}applyPatterns(t,e=10,s=null){let r=new We(t),o=0,i=[...t.opsRecursive()],a=0,c=new Set(i),l=f(d=>{!d||!d.parentBlock||c.has(d)||(c.add(d),i.push(d))},"enqueue"),u=Math.max(e,1)*Math.max(i.length,1)*4+1e3,h=0,p=!1;for(;a<i.length;){if(++h>u){p=!0;break}let d=i[a++];if(c.delete(d),!d.parentBlock)continue;let m=this.patternSet.getForOp(d.opName);for(let g of m){if(!g.match(d))continue;let _=d.parentBlock,y=d._prev,b=d._next,x=[];for(let w=0;w<d.numResults;w++)for(let A of d.getResult(w).getUsers())x.push(A);for(let w=0;w<d.numOperands;w++){let A=d.getOperand(w).definingOp;A&&x.push(A)}if(r.block=_,r.setInsertionPoint(d),!g.rewrite(d,r))continue;o++;for(let w of x)l(w);let S=y?y._next:_._head,v=_._size+2;for(;S&&S!==b&&v-- >0;)l(S),S=S._next;l(d);break}}return s&&(p&&s.level>=ot.INFO&&s.emit({type:"pass_detail",passName:"PatternApplicator",message:`pattern rewriting hit safety budget (${u}) without converging`,totalRewrites:o,level:ot.INFO}),s.level>=ot.DEBUG&&o>0&&s.emit({type:"pass_detail",passName:"PatternApplicator",totalRewrites:o,patternCount:this.patternSet.patterns.length,level:ot.DEBUG})),o>0?Q.CHANGED:Q.UNCHANGED}}});function bn(n){return(cc[n]||of).wasm}function af(n){return(cc[n]||of).load}function dw(n){return(cc[n]||of).store}function qr(n){return(cc[n]||of).bytes}function Wm(n){return hw[n]||null}function wn(n,t){let e=hw[n];return e&&e[t]||null}function Ur(n){return pw[n]||dO}function uc(n){return Ur(n).js}function an(n){return Ur(n).c}function jm(n){return Ur(n).cPtr}function Km(n){return Ur(n).suffix}function mO(n){return Ur(n).mathSuffix}function cf(n){return Ur(n).bytes}function ks(n){return Ur(n).isFloat}function Bs(n){return Ur(n).isInt}function uf(n,t){return n==="sum"||n==="mean"?0:n==="prod"?1:n==="max"?Bs(t)?(rf[t]||rf.i32)[0]:-1/0:n==="min"?Bs(t)?(rf[t]||rf.i32)[1]:1/0:0}function lf(n,t){let e=gO[n];return e?e+mO(t):n}function mw(n){return _O.has(n)}function ls(n){return(Hm[n]||gw).wgsl}function xn(n){return(Hm[n]||gw).bytes}function yw(n){return _w[n]||n}function bw(n){return n in _w}function ww(n,t,e){let s=yO[n];if(!s)return null;let r=s[t];return r&&r[e]||null}var pw,cc,of,hw,dO,rf,gO,_O,Hm,gw,_w,yO,As=T(()=>{pw={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}},cc={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}},of=cc.f32,hw={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"}};f(bn,"wasmType");f(af,"wasmLoad");f(dw,"wasmStore");f(qr,"wasmBytes");f(Wm,"wasmSimdEntry");f(wn,"wasmVecOp");dO=pw.f32;f(Ur,"dtypeInfo");f(uc,"jsTypedArray");f(an,"cType");f(jm,"cPtrType");f(Km,"cLiteralSuffix");f(mO,"cMathFuncSuffix");f(cf,"dtypeBytes");f(ks,"isDtypeFloat");f(Bs,"isDtypeInt");rf={i8:[-128,127],i16:[-32768,32767],i32:[-2147483648,2147483647],i64:[Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER],ui8:[0,255],index:[-2147483648,2147483647]};f(uf,"reduceInitValue");gO={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",erf:"erf",log2:"log2",log10:"log10",exp2:"exp2"};f(lf,"cMathFunc");_O=new Set(["exp","log","sqrt","tanh","abs","ceil","floor","sin","cos","max","min","pow","round","sign","log2","log10"]);f(mw,"isJSMathFunc");Hm={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}},gw=Hm.f32;f(ls,"wgslType");f(xn,"wgslBytes");_w={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"};f(yw,"wgslMathFunc");f(bw,"hasWgslMathFunc");yO={dot:{blas:{f32:"sgemm",f64:"dgemm"},cublas:{f32:"cublasSgemm",f64:"cublasDgemm",f16:"cublasHgemm"}},conv:{cudnn:{f32:"cudnnConvolutionForward",f16:"cudnnConvolutionForward"}},quantized_dot:{blas:{i8:"cblas_gemm_s8u8s32"},cublas:{i8:"cublasGemmEx"}},quantized_conv:{cudnn:{i8:"cudnnConvolutionForward"}}};f(ww,"libraryFunc")});function bi(n,t){let e={};return n.match(t,e)?e:null}var ff,Ym,Jm,yi,Sn,xw=T(()=>{ff=class{static{f(this,"DFPattern")}match(t,e){return!1}},Ym=class extends ff{static{f(this,"AnyPattern")}match(){return!0}},Jm=class extends ff{static{f(this,"OpPattern")}constructor(t,e){super(),this.name=t,this.operandPatterns=e}match(t,e){if(!t||t.opName!==this.name)return!1;if(this.operandPatterns.length===0)return!0;if(t.numOperands<this.operandPatterns.length)return!1;for(let s=0;s<this.operandPatterns.length;s++){let r=t.getOperand(s).definingOp;if(!this.operandPatterns[s].match(r,e))return!1}return!0}},yi=f(()=>new Ym,"wildcard"),Sn=f((n,...t)=>new Jm(n,t),"isOp");f(bi,"matchPattern")});function $n(n,t){return n&&n.opName==="constant"&&n.getAttr("value")===t}function Xm(n){return new Zm(n)}var bO,wO,xO,SO,vO,pf,wi,hf,df,Zm,xi,Si,vi,Bi,ki,Ai,Ri,mf,gf,_f,yf,bf,wf,xf,Sf,vf,Bf,Oi=T(()=>{ac();kt();As();xw();bO=Sn("transpose",Sn("transpose",yi())),wO=Sn("reshape",Sn("reshape",yi())),xO=Sn("neg",Sn("neg",yi())),SO=Sn("exp",Sn("log",yi())),vO=Sn("log",Sn("exp",yi()));f($n,"isConstantVal");pf=class extends Ht{static{f(this,"FoldTrivialReshape")}constructor(){super("fold_trivial_reshape",10),this.rootOpName="reshape"}match(t){let e=t.getOperand(0).type,s=t.getResult(0).type;return e instanceof E&&s instanceof E&&e.shapeEquals(s)}rewrite(t,e){return t.replaceAllResultsWith([t.getOperand(0)]),t.erase(),!0}},wi=class extends Ht{static{f(this,"ReshapeReshape")}constructor(){super("reshape_reshape",10),this.rootOpName="reshape"}match(t){return bi(wO,t)!==null}rewrite(t,e){let s=t.getOperand(0).definingOp.getOperand(0),r=t.getAttr("new_shape"),o=e.reshape(s,r);return t.replaceAllResultsWith([o.getResult(0)]),t.erase(),!0}},hf=class extends Ht{static{f(this,"FoldTrivialTranspose")}constructor(){super("fold_trivial_transpose",10),this.rootOpName="transpose"}match(t){let e=t.getAttr("permutation");if(!e)return!1;for(let s=0;s<e.length;s++)if(e[s]!==s)return!1;return!0}rewrite(t,e){return t.replaceAllResultsWith([t.getOperand(0)]),t.erase(),!0}},df=class extends Ht{static{f(this,"TransposeTranspose")}constructor(){super("transpose_transpose",10),this.rootOpName="transpose"}match(t){return bi(bO,t)!==null}rewrite(t,e){let s=t.getOperand(0).definingOp.getOperand(0),r=t.getOperand(0).definingOp.getAttr("permutation"),o=t.getAttr("permutation"),i=new Array(o.length);for(let c=0;c<o.length;c++)i[c]=r[o[c]];let a=e.transpose(s,i);return t.replaceAllResultsWith([a.getResult(0)]),t.erase(),!0}},Zm=class extends Ht{static{f(this,"CommutativeConstantRight")}constructor(t=null){super(`commutative_constant_right${t?"_"+t:""}`,5),this.rootOpName=t}match(t){if(t.numOperands!==2)return!1;let e=t.getOperand(0).definingOp,s=t.getOperand(1).definingOp;return e&&e.opName==="constant"&&!(s&&s.opName==="constant")}rewrite(t,e){let s=t.getOperand(0),r=t.getOperand(1);return t.replaceOperand(0,r),t.replaceOperand(1,s),!0}};f(Xm,"commutativeConstantRightFor");xi=class extends Ht{static{f(this,"AddZero")}constructor(){super("add_zero",5),this.rootOpName="add"}match(t){return $n(t.getOperand(1).definingOp,0)||$n(t.getOperand(0).definingOp,0)}rewrite(t,e){let s=$n(t.getOperand(1).definingOp,0)?t.getOperand(0):t.getOperand(1);return s.type.equals(t.getResult(0).type)?(t.replaceAllResultsWith([s]),t.erase(),!0):!1}},Si=class extends Ht{static{f(this,"SubZero")}constructor(){super("sub_zero",5),this.rootOpName="sub"}match(t){return $n(t.getOperand(1).definingOp,0)}rewrite(t,e){let s=t.getOperand(0);return s.type.equals(t.getResult(0).type)?(t.replaceAllResultsWith([s]),t.erase(),!0):!1}},vi=class extends Ht{static{f(this,"SubSelf")}constructor(t=!1){super("sub_self",5),this.rootOpName="sub",this.fastMath=t}match(t){return t.getOperand(0)!==t.getOperand(1)?!1:Bs(t.getResult(0).type.dtype)||this.fastMath}rewrite(t,e){let r=e.scalarConstant(0,t.getResult(0).type.dtype).getResult(0),o=t.getResult(0).type.shape;return o.length>0&&(r=e.broadcast(r,o,[]).getResult(0)),t.replaceAllResultsWith([r]),t.erase(),!0}},Bi=class extends Ht{static{f(this,"MulOne")}constructor(){super("mul_one",5),this.rootOpName="mul"}match(t){return $n(t.getOperand(1).definingOp,1)||$n(t.getOperand(0).definingOp,1)}rewrite(t,e){let s=$n(t.getOperand(1).definingOp,1)?t.getOperand(0):t.getOperand(1);return s.type.equals(t.getResult(0).type)?(t.replaceAllResultsWith([s]),t.erase(),!0):!1}},ki=class extends Ht{static{f(this,"MulZero")}constructor(t=!1){super("mul_zero",5),this.rootOpName="mul",this.fastMath=t}match(t){return!Bs(t.getResult(0).type.dtype)&&!this.fastMath?!1:$n(t.getOperand(1).definingOp,0)||$n(t.getOperand(0).definingOp,0)}rewrite(t,e){let r=e.scalarConstant(0,t.getResult(0).type.dtype).getResult(0),o=t.getResult(0).type.shape;return o.length>0&&(r=e.broadcast(r,o,[]).getResult(0)),t.replaceAllResultsWith([r]),t.erase(),!0}},Ai=class extends Ht{static{f(this,"DivOne")}constructor(){super("div_one",5),this.rootOpName="div"}match(t){return $n(t.getOperand(1).definingOp,1)}rewrite(t,e){let s=t.getOperand(0);return s.type.equals(t.getResult(0).type)?(t.replaceAllResultsWith([s]),t.erase(),!0):!1}},Ri=class extends Ht{static{f(this,"DoubleNeg")}constructor(){super("double_neg",5),this.rootOpName="neg"}match(t){return bi(xO,t)!==null}rewrite(t,e){let s=t.getOperand(0).definingOp.getOperand(0);return t.replaceAllResultsWith([s]),t.erase(),!0}},mf=class extends Ht{static{f(this,"ExpLog")}constructor(t=!1){super("exp_log",5),this.rootOpName="exp",this.fastMath=t}match(t){return this.fastMath&&bi(SO,t)!==null}rewrite(t,e){return t.replaceAllResultsWith([t.getOperand(0).definingOp.getOperand(0)]),t.erase(),!0}},gf=class extends Ht{static{f(this,"LogExp")}constructor(t=!1){super("log_exp",5),this.rootOpName="log",this.fastMath=t}match(t){return this.fastMath&&bi(vO,t)!==null}rewrite(t,e){return t.replaceAllResultsWith([t.getOperand(0).definingOp.getOperand(0)]),t.erase(),!0}},_f=class extends Ht{static{f(this,"DivSelf")}constructor(t=!1){super("div_self",5),this.rootOpName="div",this.fastMath=t}match(t){return this.fastMath?t.getOperand(0)===t.getOperand(1):!1}rewrite(t,e){let r=e.scalarConstant(1,t.getResult(0).type.dtype).getResult(0),o=t.getResult(0).type.shape;return o.length>0&&(r=e.broadcast(r,o,[]).getResult(0)),t.replaceAllResultsWith([r]),t.erase(),!0}},yf=class extends Ht{static{f(this,"MulNegNeg")}constructor(){super("mul_neg_neg",4),this.rootOpName="mul"}match(t){let e=t.getOperand(0).definingOp,s=t.getOperand(1).definingOp;return e&&e.opName==="neg"&&s&&s.opName==="neg"}rewrite(t,e){let s=t.getOperand(0).definingOp.getOperand(0),r=t.getOperand(1).definingOp.getOperand(0),o=e.mul(s,r);return t.replaceAllResultsWith([o.getResult(0)]),t.erase(),!0}},bf=class extends Ht{static{f(this,"AddNegToSub")}constructor(){super("add_neg_to_sub",4),this.rootOpName="add"}match(t){let e=t.getOperand(1).definingOp;return e&&e.opName==="neg"}rewrite(t,e){let s=t.getOperand(0),r=t.getOperand(1).definingOp.getOperand(0),o=e.sub(s,r);return t.replaceAllResultsWith([o.getResult(0)]),t.erase(),!0}},wf=class extends Ht{static{f(this,"SubNegToAdd")}constructor(){super("sub_neg_to_add",4),this.rootOpName="sub"}match(t){let e=t.getOperand(1).definingOp;return e&&e.opName==="neg"}rewrite(t,e){let s=t.getOperand(0),r=t.getOperand(1).definingOp.getOperand(0),o=e.add(s,r);return t.replaceAllResultsWith([o.getResult(0)]),t.erase(),!0}},xf=class extends Ht{static{f(this,"DoubleConvert")}constructor(){super("double_convert",6),this.rootOpName="convert"}match(t){let e=t.getOperand(0).definingOp;if(!e||e.opName!=="convert")return!1;let s=e.getOperand(0).type.dtype,r=t.getAttr("target_dtype");return s===r}rewrite(t,e){return t.replaceAllResultsWith([t.getOperand(0).definingOp.getOperand(0)]),t.erase(),!0}},Sf=class extends Ht{static{f(this,"LayoutTransformIdentity")}constructor(){super("layout_transform_identity",10),this.rootOpName="layout_transform"}match(t){let e=t.getAttr("src_layout"),s=t.getAttr("dst_layout");if(!e||!s||e.length!==s.length)return!1;for(let r=0;r<e.length;r++)if(e[r]!==s[r])return!1;return!0}rewrite(t,e){return t.replaceAllResultsWith([t.getOperand(0)]),t.erase(),!0}},vf=class extends Ht{static{f(this,"FoldTransposeIntoDot")}constructor(){super("fold_transpose_into_dot",10),this.rootOpName="dot"}match(t){for(let e=0;e<2;e++){let s=t.getOperand(e).definingOp;if(!s||s.opName!=="transpose")continue;let r=s.getAttr("permutation");if(!(!r||r.length!==2)&&!(r[0]!==1||r[1]!==0))return!0}return!1}rewrite(t,e){let s=[t.getOperand(0),t.getOperand(1)],r=[...t.getAttr("lhs_contracting")],o=[...t.getAttr("rhs_contracting")],i=[...t.getAttr("lhs_batch")||[]],a=[...t.getAttr("rhs_batch")||[]];for(let l=0;l<2;l++){let u=s[l].definingOp;if(!u||u.opName!=="transpose")continue;let h=u.getAttr("permutation");if(!h||h.length!==2||h[0]!==1||h[1]!==0)continue;s[l]=u.getOperand(0);let p=l===0?r:o,d=l===0?i:a;for(let m=0;m<p.length;m++)p[m]=p[m]===0?1:0;for(let m=0;m<d.length;m++)d[m]=d[m]===0?1:0}let c=e.dot(s[0],s[1],r,o);return i.length>0&&c.setAttr("lhs_batch",i),a.length>0&&c.setAttr("rhs_batch",a),t.replaceAllResultsWith([c.getResult(0)]),t.erase(),!0}},Bf=class extends Ht{static{f(this,"LayoutTransformCompose")}constructor(){super("layout_transform_compose",10),this.rootOpName="layout_transform"}match(t){let e=t.getOperand(0).definingOp;return e&&e.opName==="layout_transform"}rewrite(t,e){let s=t.getOperand(0).definingOp,r=s.getAttr("src_layout"),o=s.getAttr("dst_layout"),i=t.getAttr("dst_layout"),a=new Array(r.length);for(let u=0;u<i.length;u++)a[u]=r[o.indexOf(i[u])];let c=s.getOperand(0),l=e._inferAndBuild("layout_transform",[c],{src_layout:r,dst_layout:a});return t.replaceAllResultsWith([l.getResult(0)]),t.erase(),!0}}});function bo(n){if(n.length!==2)return null;let t=n[0],e=n[1];if(!(t instanceof E)||!(e instanceof E)||t.dtype!==e.dtype)return null;let s=E.broadcastShape(t.shape,e.shape);return s?[new E(s,t.dtype)]:null}function kf(n){if(n.length!==1)return null;let t=n[0];return t instanceof E?[new E(t.shape,t.dtype)]:null}function Sw(n){if(n.length!==2)return null;let t=n[0],e=n[1];if(!(t instanceof E)||!(e instanceof E))return null;let s=E.broadcastShape(t.shape,e.shape);return s?[new E(s,J.BOOL)]:null}function Qm(n){if(n.length!==1)return null;let t=n[0];return!(t instanceof E)||!Oe(t.dtype)?null:[new E(t.shape,t.dtype)]}function wo(n){let t=[];if(n.numOperands!==2)return t.push(`${n.opName} expects 2 operands, got ${n.numOperands}`),t;let e=n.getOperand(0).type,s=n.getOperand(1).type;return e instanceof E||t.push(`${n.opName} operand 0 is not tensor`),s instanceof E||t.push(`${n.opName} operand 1 is not tensor`),e instanceof E&&s instanceof E&&e.dtype!==s.dtype&&t.push(`${n.opName} dtype mismatch: ${e.dtype} vs ${s.dtype}`),t}function fc(n){let t=[];return n.numOperands!==1?(t.push(`${n.opName} expects 1 operand, got ${n.numOperands}`),t):(n.getOperand(0).type instanceof E||t.push(`${n.opName} operand is not tensor`),t)}function tg(n){let t=fc(n);if(t.length===0){let e=n.getOperand(0).type.dtype;Oe(e)||t.push(`${n.opName} requires float type, got ${e}`)}return t}var Ni,eg,Af=T(()=>{kt();we();f(bo,"inferBinaryElementwise");f(kf,"inferUnaryElementwise");f(Sw,"inferCompare");f(Qm,"inferUnaryFloat");f(wo,"verifyBinaryElementwise");f(fc,"verifyUnaryElementwise");f(tg,"verifyUnaryFloat");Ni=[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],eg=[...Ni,U.COMMUTATIVE,U.ASSOCIATIVE]});function Rf(n){return t=>{if(!(typeof t[0]!="number"||typeof t[1]!="number"))return n(t[0],t[1])}}function BO(n){return t=>typeof t[0]=="number"?n(t[0]):void 0}function vw(n){n.register(new H({name:"add",numOperands:2,numResults:1,traits:eg,inferResultTypes:bo,verify:wo,getCanonicalizationPatterns(){return[Xm("add"),new xi]},fold:Rf((t,e)=>t+e)})),n.register(new H({name:"mul",numOperands:2,numResults:1,traits:eg,inferResultTypes:bo,verify:wo,getCanonicalizationPatterns(){return[Xm("mul"),new Bi,new ki]},fold:Rf((t,e)=>t*e)})),n.register(new H({name:"sub",numOperands:2,numResults:1,traits:Ni,inferResultTypes:bo,verify:wo,getCanonicalizationPatterns(){return[new Si,new vi]},fold:Rf((t,e)=>t-e)})),n.register(new H({name:"div",numOperands:2,numResults:1,traits:Ni,inferResultTypes:bo,verify:wo,getCanonicalizationPatterns(){return[new Ai]},fold:Rf((t,e)=>t/e)}));for(let t of["rem","pow"])n.register(new H({name:t,numOperands:2,numResults:1,traits:Ni,inferResultTypes:bo,verify:wo}));n.register(new H({name:"neg",numOperands:1,numResults:1,traits:[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:kf,verify:fc,getCanonicalizationPatterns(){return[new Ri]},fold:BO(t=>-t)}));for(let t of["maximum","minimum"])n.register(new H({name:t,numOperands:2,numResults:1,traits:[...Ni,U.COMMUTATIVE],inferResultTypes:bo,verify:wo}))}var Bw=T(()=>{we();Oi();Af();f(Rf,"scalarBinaryFold");f(BO,"scalarUnaryFold");f(vw,"register")});function kw(n){for(let t of["abs","floor","ceil","round","sign","square","reciprocal"])n.register(new H({name:t,numOperands:1,numResults:1,traits:[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:kf,verify:fc}));n.register(new H({name:"exp",numOperands:1,numResults:1,traits:[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Qm,fold(t){return typeof t[0]=="number"?Math.exp(t[0]):void 0},verify:tg}));for(let t of["log","sqrt","rsqrt","tanh","sin","cos","erf","log2","log10","exp2"])n.register(new H({name:t,numOperands:1,numResults:1,traits:[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Qm,verify:tg}))}var Aw=T(()=>{we();Af();f(kw,"register")});function kO(n){if(n.length!==1)return null;let t=n[0];return!(t instanceof E)||t.dtype!==J.BOOL?null:[new E(t.shape,J.BOOL)]}function AO(n){if(n.length!==2)return null;let[t,e]=n;if(!(t instanceof E)||t.dtype!==J.BOOL||!(e instanceof E)||e.dtype!==J.BOOL)return null;let s=E.broadcastShape(t.shape,e.shape);return s?[new E(s,J.BOOL)]:null}function Rw(n){n.register(new H({name:"compare",numOperands:2,numResults:1,attrs:[{name:"direction",type:"string",required:!0}],traits:[U.ELEMENTWISE],inferResultTypes:Sw,verify(t){let e=[];if(t.numOperands!==2)return e.push("compare expects 2 operands"),e;if(!t.hasAttr("direction"))e.push("compare missing direction attr");else{let s=t.getAttr("direction");RO.has(s)||e.push(`compare invalid direction: ${s}`)}return e}})),n.register(new H({name:"select",numOperands:3,numResults:1,traits:[U.ELEMENTWISE],inferResultTypes(t){if(t.length!==3)return null;let[e,s,r]=t;if(!(e instanceof E)||e.dtype!==J.BOOL||!(s instanceof E)||!(r instanceof E)||s.dtype!==r.dtype)return null;let o=E.broadcastShape(e.shape,s.shape,r.shape);return o?[new E(o,s.dtype)]:null}})),n.register(new H({name:"where",numOperands:3,numResults:1,traits:[U.ELEMENTWISE],inferResultTypes(t){if(t.length!==3)return null;let[e,s,r]=t;if(!(e instanceof E)||!(s instanceof E)||!(r instanceof E)||s.dtype!==r.dtype)return null;let o=E.broadcastShape(e.shape,s.shape,r.shape);return o?[new E(o,s.dtype)]:null}})),n.register(new H({name:"logical_not",numOperands:1,numResults:1,traits:[U.ELEMENTWISE],inferResultTypes:kO}));for(let t of["logical_and","logical_or"])n.register(new H({name:t,numOperands:2,numResults:1,traits:[U.ELEMENTWISE],inferResultTypes:AO}));n.register(new H({name:"clamp",numOperands:3,numResults:1,traits:[U.ELEMENTWISE],inferResultTypes(t){if(t.length!==3)return null;let e=t[1];return e instanceof E?[new E(e.shape,e.dtype)]:null}}))}var RO,Ow=T(()=>{we();kt();Af();f(kO,"inferUnaryBool");f(AO,"inferBinaryBool");RO=new Set(["eq","ne","lt","le","gt","ge"]);f(Rw,"register")});function Nw(n){n.register(new H({name:"stop_gradient",numOperands:1,numResults:1,traits:[U.VIEW],inferResultTypes(t){return t.length!==1?null:[t[0]]},propagateSymbolicShapes(t,e){let s=e.get(t.getOperand(0));return s?[s]:null}})),n.register(new H({name:"reverse",numOperands:1,numResults:1,traits:[U.INJECTIVE],attrs:[{name:"dimensions",type:"array",required:!0}],inferResultTypes(t){return t.length!==1?null:[t[0]]},propagateSymbolicShapes(t,e){let s=e.get(t.getOperand(0));return s?[s]:null}})),n.register(new H({name:"broadcast_in_dim",numOperands:1,numResults:1,traits:[U.BROADCAST],attrs:[{name:"broadcast_dimensions",type:"array",required:!0},{name:"result_shape",type:"array",required:!0}],inferResultTypes(t,e){if(t.length!==1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("result_shape"):e.result_shape;return r?[new E(r,s.dtype)]:null},propagateSymbolicShapes(t,e){let s=e.get(t.getOperand(0));if(!s)return null;let r=t.getAttr("broadcast_dimensions"),o=t.getAttr("result_shape");if(!r||!o)return null;let i=o.map(a=>a===Z?null:a);for(let a=0;a<r.length;a++)(typeof s[a]!="number"||i[r[a]]===null)&&(i[r[a]]=s[a]);return[i]},fold(t){if(t.length===1&&typeof t[0]=="number")return t[0]},verify(t){let e=[];if(t.hasAttr("broadcast_dimensions")||e.push("broadcast_in_dim missing broadcast_dimensions"),t.hasAttr("result_shape")||e.push("broadcast_in_dim missing result_shape"),t.numOperands!==1&&e.push("broadcast_in_dim expects 1 operand"),e.length===0){let s=t.getAttr("broadcast_dimensions"),r=t.getAttr("result_shape"),o=t.getOperand(0).type;s.length!==o.rank&&e.push(`broadcast_dimensions length ${s.length} != input rank ${o.rank}`);for(let a=0;a<s.length;a++)s[a]<0||s[a]>=r.length?e.push(`broadcast_dimensions[${a}]=${s[a]} out of range for result rank ${r.length}`):o instanceof E&&o.shape[a]!==Z&&o.shape[a]!==1&&r[s[a]]!==Z&&o.shape[a]!==r[s[a]]&&e.push(`broadcast_in_dim: input dim ${a} size ${o.shape[a]} incompatible with result dim ${s[a]} size ${r[s[a]]}`);let i=new Set;for(let a of s)i.has(a)&&e.push(`broadcast_dimensions has duplicate: ${a}`),i.add(a)}return e}})),n.register(new H({name:"reshape",numOperands:1,numResults:1,traits:[U.VIEW],attrs:[{name:"new_shape",type:"array",required:!0}],inferResultTypes(t,e){if(t.length!==1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("new_shape"):e.new_shape;return r?[new E(r,s.dtype)]:null},verify(t){let e=[];if(t.hasAttr("new_shape")||e.push("reshape missing new_shape"),t.numOperands!==1&&e.push("reshape expects 1 operand"),e.length===0){let s=t.getOperand(0).type,r=t.getAttr("new_shape");if(s instanceof E&&s.isFullyStatic){let o=r.filter(i=>i===Z).length;if(o>1&&e.push("reshape can have at most one dynamic dimension"),o===0){let i=s.numel(),a=r.reduce((c,l)=>c*l,1);i!==a&&e.push(`reshape numel mismatch: input ${i} vs output ${a}`)}}}return e},propagateSymbolicShapes(t,e){let s=e.get(t.getOperand(0));if(!s)return null;let r=t.getAttr("new_shape"),o=[];for(let i of r)if(i===-1){let a=s.find(c=>typeof c!="number");o.push(a||-1)}else o.push(i);return[o]},getCanonicalizationPatterns(){return[new pf,new wi]},fold(t){return t[0]}})),n.register(new H({name:"transpose",numOperands:1,numResults:1,traits:[U.VIEW],attrs:[{name:"permutation",type:"array",required:!0}],inferResultTypes(t,e){if(t.length!==1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("permutation"):e.permutation;if(!r)return null;let o=r.map(i=>s.shape[i]);return[new E(o,s.dtype)]},propagateSymbolicShapes(t,e){let s=e.get(t.getOperand(0));if(!s)return null;let r=t.getAttr("permutation");return r?[r.map(o=>s[o])]:null},getCanonicalizationPatterns(){return[new hf]},fold(t,e,s){let r=t[0];if(r==null||typeof r=="number"||typeof r.length!="number")return;let o=e.get?e.get("permutation"):e.permutation;if(!o)return;let i=s&&s[0]?s[0].getResult(0).type:null,a=i instanceof E?i.shape:null;if(!a||a.length!==o.length)return;let c=new Array(a.length),l=1;for(let m=a.length-1;m>=0;m--)c[m]=l,l*=a[m];let u=o.map(m=>a[m]),h=r.length,p=new Array(h),d=new Array(o.length).fill(0);for(let m=0;m<h;m++){let g=0;for(let _=0;_<o.length;_++)g+=d[_]*c[o[_]];p[m]=r[g];for(let _=o.length-1;_>=0&&!(++d[_]<u[_]);_--)d[_]=0}return p},verify(t){let e=[];if(!t.hasAttr("permutation"))return e.push("transpose missing permutation"),e;if(t.numOperands!==1)return e.push("transpose expects 1 operand"),e;let s=t.getAttr("permutation"),r=t.getOperand(0).type;r instanceof E&&s.length!==r.rank&&e.push(`transpose permutation length ${s.length} != input rank ${r.rank}`);let o=new Set;for(let i of s)o.has(i)&&e.push(`transpose duplicate in permutation: ${i}`),o.add(i);return e}})),n.register(new H({name:"slice",numOperands:1,numResults:1,traits:[U.VIEW],attrs:[{name:"starts",type:"array",required:!0},{name:"limits",type:"array",required:!0},{name:"strides",type:"array",required:!1}],inferResultTypes(t,e){if(t.length!==1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("starts"):e.starts,o=e.get?e.get("limits"):e.limits,i=(e.get?e.get("strides"):e.strides)||r.map(()=>1),a=[];for(let c=0;c<r.length;c++)a.push(Math.ceil((o[c]-r[c])/i[c]));return[new E(a,s.dtype)]}})),n.register(new H({name:"concat",numOperands:-1,numResults:1,attrs:[{name:"dimension",type:"number",required:!0}],inferResultTypes(t,e){if(t.length<1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("dimension"):e.dimension;if(r===void 0)return null;let o=[...s.shape];for(let i=1;i<t.length;i++){let a=t[i];if(!(a instanceof E)||a.dtype!==s.dtype||a.rank!==s.rank)return null;o[r]===Z||a.shape[r]===Z?o[r]=Z:o[r]+=a.shape[r]}return[new E(o,s.dtype)]}})),n.register(new H({name:"pad",numOperands:2,numResults:1,traits:[U.INJECTIVE],attrs:[{name:"low",type:"array",required:!0},{name:"high",type:"array",required:!0},{name:"interior",type:"array",required:!1}],inferResultTypes(t,e){if(t.length<1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("low"):e.low,o=e.get?e.get("high"):e.high,i=(e.get?e.get("interior"):e.interior)||r.map(()=>0),a=[];for(let c=0;c<s.rank;c++)s.shape[c]===Z?a.push(Z):a.push(r[c]+s.shape[c]+(s.shape[c]-1)*i[c]+o[c]);return[new E(a,s.dtype)]}})),n.register(new H({name:"gather",numOperands:2,numResults:1,traits:[U.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(t,e){if(t.length!==2)return null;let s=t[0],r=t[1];if(!(s instanceof E)||!(r instanceof E))return null;let o=e.get?e.get("offset_dims"):e.offset_dims,i=new Set(e.get?e.get("collapsed_slice_dims"):e.collapsed_slice_dims),a=e.get?e.get("slice_sizes"):e.slice_sizes,c=e.get?e.get("index_vector_dim"):e.index_vector_dim,l=[];for(let _=0;_<r.rank;_++)_!==c&&l.push(r.shape[_]);let u=[];for(let _=0;_<a.length;_++)i.has(_)||u.push(a[_]);let h=new Set(o),p=[],d=0,m=0,g=l.length+u.length;for(let _=0;_<g;_++)h.has(_)?p.push(u[m++]):p.push(l[d++]);return[new E(p,s.dtype)]}})),n.register(new H({name:"scatter",numOperands:3,numResults:1,traits:[U.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(t){if(t.length<1)return null;let e=t[0];return e instanceof E?[new E(e.shape,e.dtype)]:null}})),n.register(new H({name:"split",numOperands:1,numResults:-1,attrs:[{name:"dimension",type:"number",required:!0},{name:"split_sizes",type:"array",required:!0}],inferResultTypes(t,e){if(t.length<1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("dimension"):e.dimension,o=e.get?e.get("split_sizes"):e.split_sizes;return r===void 0||!o?null:o.map(i=>{let a=[...s.shape];return a[r]=i,new E(a,s.dtype)})}}))}var Ew=T(()=>{we();kt();Oi();f(Nw,"register")});function Tw(n){n.register(new H({name:"reduce",numOperands:2,numResults:1,attrs:[{name:"dimensions",type:"array",required:!0},{name:"reduce_type",type:"string",required:!0}],traits:[U.REDUCTION],hasRegions:!0,numRegions:1,inferResultTypes(e,s){if(e.length<1)return null;let r=e[0];if(!(r instanceof E))return null;let o=s.get?s.get("dimensions"):s.dimensions;if(!o)return null;let i=new Set(o),a=[];for(let c=0;c<r.rank;c++)i.has(c)||a.push(r.shape[c]);return[new E(a,r.dtype)]},propagateSymbolicShapes(e,s){let r=s.get(e.getOperand(0));if(!r)return null;let o=e.getAttr("dimensions");if(!o)return null;let i=new Set(o),a=[];for(let c=0;c<r.length;c++)i.has(c)||a.push(r[c]);return[a]},verify(e){let s=[];if(e.hasAttr("dimensions")||s.push("reduce missing dimensions"),!e.hasAttr("reduce_type"))s.push("reduce missing reduce_type");else{let r=e.getAttr("reduce_type");OO.has(r)||s.push(`reduce invalid reduce_type: ${r}`)}return s}}));function t(e,s){if(e.length<1)return null;let r=e[0];if(!(r instanceof E))return null;let o=s.get?s.get("axis"):s.axis;if(o===void 0)return null;let i=(s.get?s.get("keep_dims"):s.keep_dims)||!1,a=[];for(let c=0;c<r.rank;c++)c===o?i&&a.push(1):a.push(r.shape[c]);return[new E(a,J.I32)]}f(t,"inferArgReduceTypes"),n.register(new H({name:"argmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"keep_dims",type:"boolean",required:!1}],traits:[U.REDUCTION],inferResultTypes:t})),n.register(new H({name:"argmin",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"keep_dims",type:"boolean",required:!1}],traits:[U.REDUCTION],inferResultTypes:t}))}var OO,Iw=T(()=>{we();kt();OO=new Set(["sum","max","min","prod","mean","and","or"]);f(Tw,"register")});function Mw(n){n.register(new H({name:"dot",numOperands:2,numResults:1,traits:[U.OPAQUE,U.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(t){let e=t.getOperand(0).type,s=t.getOperand(1).type;if(!(e instanceof E)||!(s instanceof E))return 0;let r=t.getAttr("lhs_contracting")||[],o=1;for(let c of r)e.shape[c]!==Z&&(o*=e.shape[c]);let i=t.getResult(0).type;if(!(i instanceof E))return 0;let a=i.numel();return a===Z?0:2*a*o},inferResultTypes:Of,getCanonicalizationPatterns(){return[new vf]},verify(t){let e=[];if(t.numOperands!==2)return e.push("dot expects 2 operands"),e;t.hasAttr("lhs_contracting")||e.push("dot missing lhs_contracting"),t.hasAttr("rhs_contracting")||e.push("dot missing rhs_contracting");let s=t.getOperand(0).type,r=t.getOperand(1).type;if(s instanceof E&&r instanceof E){s.dtype!==r.dtype&&e.push(`dot dtype mismatch: ${s.dtype} vs ${r.dtype}`);let o=t.getAttr("lhs_contracting")||[],i=t.getAttr("rhs_contracting")||[];if(o.length!==i.length)e.push(`dot contracting dimensions count mismatch: lhs ${o.length} vs rhs ${i.length}`);else for(let a=0;a<o.length;a++){let c=o[a],l=i[a];c>=s.rank&&e.push(`dot lhs_contracting[${a}]=${c} out of range (rank ${s.rank})`),l>=r.rank&&e.push(`dot rhs_contracting[${a}]=${l} out of range (rank ${r.rank})`),c<s.rank&&l<r.rank&&s.shape[c]!==Z&&r.shape[l]!==Z&&s.shape[c]!==r.shape[l]&&e.push(`dot contracting dim size mismatch at [${a}]: lhs dim ${c} size ${s.shape[c]} vs rhs dim ${l} size ${r.shape[l]}`)}}return e}})),n.register(new H({name:"cublas_gemm",numOperands:2,numResults:1,traits:[U.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:Of,getFlops(t){let e=t.getOperand(0).type,s=t.getResult(0).type;if(!(e instanceof E)||!(s instanceof E))return 0;let r=1;for(let i of t.getAttr("lhs_contracting")||[])e.shape[i]!==Z&&(r*=e.shape[i]);let o=s.numel();return o===Z?0:2*o*r},verify(t){let e=[];return t.numOperands!==2?(e.push("cublas_gemm expects 2 operands"),e):(t.hasAttr("lhs_contracting")||e.push("cublas_gemm missing lhs_contracting"),t.hasAttr("rhs_contracting")||e.push("cublas_gemm missing rhs_contracting"),e)}})),n.register(new H({name:"conv",numOperands:2,numResults:1,traits:[U.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(t){let e=t.getResult(0).type,s=t.getOperand(1).type;if(!(e instanceof E)||!(s instanceof E))return 0;let r=e.numel(),o=s.numel();return r===Z||o===Z?0:2*r*o/(s.shape[0]||1)},inferResultTypes:sg,verify(t){let e=[];return t.numOperands!==2?(e.push("conv expects 2 operands"),e):(t.hasAttr("strides")||e.push("conv missing strides"),t.hasAttr("padding")||e.push("conv missing padding"),t.hasAttr("input_layout")||e.push("conv missing input_layout"),t.hasAttr("kernel_layout")||e.push("conv missing kernel_layout"),e)}}))}function Of(n,t,e){if(n.length!==2)return null;let s=n[0],r=n[1];if(!(s instanceof E)||!(r instanceof E))return null;let o=e&&!Array.isArray(e)?e:{};if(!o.allowMixedDtype&&s.dtype!==r.dtype)return null;let i=new Set(t.get?t.get("lhs_contracting"):t.lhs_contracting),a=new Set(t.get?t.get("rhs_contracting"):t.rhs_contracting),c=new Set((t.get?t.get("lhs_batch"):t.lhs_batch)||[]),l=new Set((t.get?t.get("rhs_batch"):t.rhs_batch)||[]),u=[];for(let h=0;h<s.rank;h++)c.has(h)&&u.push(s.shape[h]);for(let h=0;h<s.rank;h++)!i.has(h)&&!c.has(h)&&u.push(s.shape[h]);for(let h=0;h<r.rank;h++)!a.has(h)&&!l.has(h)&&u.push(r.shape[h]);return[new E(u,o.outputDtype||s.dtype)]}function sg(n,t,e){if(n.length!==2)return null;let s=n[0],r=n[1];if(!(s instanceof E)||!(r instanceof E))return null;let o=e&&!Array.isArray(e)?e:{};if(!o.allowMixedDtype&&s.dtype!==r.dtype)return null;let i=t.get?t.get("strides"):t.strides,a=t.get?t.get("padding"):t.padding,c=(t.get?t.get("dilation"):t.dilation)||i.map(()=>1),l=i.length,u=s.shape[0],h=r.shape[0],p=[];for(let d=0;d<l;d++){let m=s.shape[d+2],g=r.shape[d+2],_=a[d][0]+a[d][1];if(m===Z||g===Z)p.push(Z);else{let y=(g-1)*c[d]+1;p.push(Math.floor((m+_-y)/i[d])+1)}}return[new E([u,h,...p],o.outputDtype||s.dtype)]}var ng=T(()=>{we();kt();Oi();f(Mw,"register");f(Of,"inferDotResultTypes");f(sg,"inferConvResultTypes")});function Cw(n){n.register(new H({name:"constant",numOperands:0,numResults:1,attrs:[{name:"value",type:"any",required:!0},{name:"tensor_type",type:"object",required:!0}],traits:[U.CONSTANT],inferResultTypes(t,e){let s=e.get?e.get("tensor_type"):e.tensor_type;return s?[s]:null}})),n.register(new H({name:"iota",numOperands:0,numResults:1,attrs:[{name:"iota_dimension",type:"number",required:!0},{name:"tensor_type",type:"object",required:!0}],inferResultTypes(t,e){let s=e.get?e.get("tensor_type"):e.tensor_type;return s?[s]:null}})),n.register(new H({name:"tuple",numOperands:-1,numResults:1,inferResultTypes(t){return[new _o(t)]}})),n.register(new H({name:"get_tuple_element",numOperands:1,numResults:1,attrs:[{name:"index",type:"number",required:!0}],inferResultTypes(t,e){if(t.length!==1)return null;let s=t[0];if(!s||!s.elements)return null;let r=e.get?e.get("index"):e.index;return r===void 0||r<0||r>=s.elements.length?null:[s.elements[r]]}})),n.register(new H({name:"convert",numOperands:1,numResults:1,attrs:[{name:"target_dtype",type:"string",required:!0}],traits:[U.ELEMENTWISE],inferResultTypes(t,e){if(t.length!==1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("target_dtype"):e.target_dtype;return[new E(s.shape,r)]}})),n.register(new H({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,e){if(t.length<1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("depth"):e.depth,o=(e.get?e.get("axis"):e.axis)??-1,i=(e.get?e.get("dtype"):e.dtype)||J.F32,a=[...s.shape],c=o<0?a.length+1+o:o;return a.splice(c,0,r),[new E(a,i)]}}))}var Dw=T(()=>{we();kt();f(Cw,"register")});function Lw(n){n.register(new H({name:"return",numOperands:-1,numResults:0,traits:[U.TERMINATOR]})),n.register(new H({name:"yield",numOperands:-1,numResults:0,traits:[U.TERMINATOR]})),n.register(new H({name:"if",numOperands:1,numResults:-1,hasRegions:!0,numRegions:2,sideEffects:Me.CONTROL,inferResultTypes(t,e,s){return s||null}})),n.register(new H({name:"while",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:2,sideEffects:Me.CONTROL,inferResultTypes(t){return[...t]}})),n.register(new H({name:"scan",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:1,sideEffects:Me.CONTROL,attrs:[{name:"num_carry",type:"number",required:!0},{name:"num_xs",type:"number",required:!0}],inferResultTypes(t,e,s){return s||null}})),n.register(new H({name:"custom_call",numOperands:-1,numResults:-1,attrs:[{name:"call_target_name",type:"string",required:!0},{name:"backend_config",type:"any",required:!1}],sideEffects:Me.WRITE,inferResultTypes(t,e,s){return s||null}})),n.register(new H({name:"fused_dot_epilogue",numOperands:-1,numResults:1,traits:[U.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(t,e,s){return s||null}})),n.register(new H({name:"fusion",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:1,attrs:[{name:"fusion_kind",type:"string",required:!1}],inferResultTypes(t,e,s){return s||null}}))}var Pw=T(()=>{we();f(Lw,"register")});function zw(n){n.register(new H({name:"layout_transform",numOperands:1,numResults:1,traits:[U.VIEW],attrs:[{name:"src_layout",type:"array",required:!0},{name:"dst_layout",type:"array",required:!0}],inferResultTypes(t,e){if(t.length!==1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("dst_layout"):e.dst_layout;return r?[new E(s.shape,s.dtype,new ye(r))]:null},verify(t){let e=[];if(t.numOperands!==1)return e.push("layout_transform expects 1 operand"),e;if(t.hasAttr("src_layout")||e.push("layout_transform missing src_layout"),t.hasAttr("dst_layout")||e.push("layout_transform missing dst_layout"),e.length===0){let s=t.getOperand(0).type,r=t.getAttr("src_layout"),o=t.getAttr("dst_layout");s instanceof E&&(r.length!==s.rank&&e.push(`src_layout length ${r.length} != input rank ${s.rank}`),o.length!==s.rank&&e.push(`dst_layout length ${o.length} != input rank ${s.rank}`))}return e},getCanonicalizationPatterns(){return[new Sf,new Bf]}}))}var $w=T(()=>{we();kt();Oi();f(zw,"register")});function NO(n,t){let e=n.getAttr("scale"),s=t.getAttr("scale"),r=n.getAttr("zero_point"),o=t.getAttr("zero_point"),i=n.getAttr("scheme"),a=t.getAttr("scheme");if(i!==a)return!1;if(typeof e=="number"&&typeof s=="number"){if(e!==s)return!1}else return!1;if(typeof r=="number"&&typeof o=="number"){if(r!==o)return!1}else return!1;return!0}var Nf,Fw=T(()=>{ac();kt();f(NO,"quantParamsMatch");Nf=class extends Ht{static{f(this,"QuantizeDequantizeIdentity")}constructor(){super("quantize_dequantize_identity",20),this.rootOpName="quantize"}match(t){let e=t.getOperand(0).definingOp;return!e||e.opName!=="dequantize"?!1:NO(t,e)}rewrite(t,e){let s=t.getOperand(0).definingOp.getOperand(0);if(!s.type||!t.getResult(0).type)return!1;let r=s.type,o=t.getResult(0).type;return r instanceof E&&o instanceof E&&r.dtype===o.dtype?(t.replaceAllResultsWith([s]),t.erase(),!0):!1}}});function Vw(n){n.register(new H({name:"quantize",numOperands:1,numResults:1,traits:[U.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(t,e){if(t.length!==1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("target_dtype"):e.target_dtype;return r?[new E(s.shape,r)]:null},verify(t){let e=[];if(t.numOperands!==1)return e.push("quantize expects 1 operand"),e;let s=t.getOperand(0).type;s instanceof E&&!Oe(s.dtype)&&e.push(`quantize input must be float, got ${s.dtype}`);let r=t.getAttr("target_dtype");r&&!rg.has(r)&&e.push(`quantize target_dtype must be i8 or ui8, got ${r}`);let o=t.getAttr("scale");return typeof o=="number"&&o<=0&&e.push("quantize scale must be positive"),e},fold(t,e){if(typeof t[0]!="number")return;let s=e.get?e.get("scale"):e.scale,r=e.get?e.get("zero_point"):e.zero_point;if(!(typeof s!="number"||typeof r!="number"))return Math.round(t[0]/s+r)},getCanonicalizationPatterns(){return[new Nf]}})),n.register(new H({name:"dequantize",numOperands:1,numResults:1,traits:[U.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(t,e){if(t.length!==1)return null;let s=t[0];if(!(s instanceof E))return null;let r=e.get?e.get("target_dtype"):e.target_dtype;return r?[new E(s.shape,r)]:null},verify(t){let e=[];if(t.numOperands!==1)return e.push("dequantize expects 1 operand"),e;let s=t.getOperand(0).type;s instanceof E&&!TO.has(s.dtype)&&e.push("dequantize input must be i8, ui8, or i32, got "+s.dtype);let r=t.getAttr("target_dtype");return r&&!Oe(r)&&e.push(`dequantize target_dtype must be float, got ${r}`),e},fold(t,e){if(typeof t[0]!="number")return;let s=e.get?e.get("scale"):e.scale,r=e.get?e.get("zero_point"):e.zero_point;if(!(typeof s!="number"||typeof r!="number"))return(t[0]-r)*s}})),n.register(new H({name:"quantized_dot",numOperands:2,numResults:1,traits:[U.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(t){let e=t.getOperand(0).type,s=t.getOperand(1).type;if(!(e instanceof E)||!(s instanceof E))return 0;let r=t.getAttr("lhs_contracting")||[],o=1;for(let c of r)e.shape[c]!==Z&&(o*=e.shape[c]);let i=t.getResult(0).type;if(!(i instanceof E))return 0;let a=i.numel();return a===Z?0:2*a*o},inferResultTypes(t,e){return Of(t,e,{outputDtype:J.I32,allowMixedDtype:!0})},verify(t){let e=[];if(t.numOperands!==2)return e.push("quantized_dot expects 2 operands"),e;t.hasAttr("lhs_contracting")||e.push("quantized_dot missing lhs_contracting"),t.hasAttr("rhs_contracting")||e.push("quantized_dot missing rhs_contracting"),t.hasAttr("lhs_scale")||e.push("quantized_dot missing lhs_scale"),t.hasAttr("rhs_scale")||e.push("quantized_dot missing rhs_scale");let s=t.getOperand(0).type,r=t.getOperand(1).type;return s instanceof E&&!rg.has(s.dtype)&&e.push(`quantized_dot lhs must be i8/ui8, got ${s.dtype}`),r instanceof E&&!rg.has(r.dtype)&&e.push(`quantized_dot rhs must be i8/ui8, got ${r.dtype}`),e}})),n.register(new H({name:"quantized_conv",numOperands:2,numResults:1,traits:[U.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(t,e){return sg(t,e,{outputDtype:J.I32,allowMixedDtype:!0})},verify(t){let e=[];return t.numOperands!==2?(e.push("quantized_conv expects 2 operands"),e):(t.hasAttr("strides")||e.push("quantized_conv missing strides"),t.hasAttr("padding")||e.push("quantized_conv missing padding"),t.hasAttr("input_layout")||e.push("quantized_conv missing input_layout"),t.hasAttr("kernel_layout")||e.push("quantized_conv missing kernel_layout"),t.hasAttr("input_scale")||e.push("quantized_conv missing input_scale"),t.hasAttr("kernel_scale")||e.push("quantized_conv missing kernel_scale"),e)}}))}var rg,TO,Gw=T(()=>{we();kt();Fw();ng();rg=new Set([J.I8,J.UI8]),TO=new Set([J.I8,J.UI8,J.I32]);f(Vw,"register")});function IO(n){if(n.length<1)return null;let t=n[0];return t instanceof E?[new E(t.shape,t.dtype)]:null}function vn(n){if(n.length<1)return null;let t=n[0];return!(t instanceof E)||!Oe(t.dtype)?null:[new E(t.shape,t.dtype)]}function gr(n){let t=[];if(n.numOperands<1)return t.push(`${n.opName} expects at least 1 operand`),t;let e=n.getOperand(0).type;return e instanceof E&&!Oe(e.dtype)&&t.push(`${n.opName} requires float input, got ${e.dtype}`),t}function qw(n){n.register(new H({name:"all_reduce",numOperands:1,numResults:1,traits:[U.OPAQUE],attrs:[{name:"reduce_op",type:"string",required:!1},{name:"mesh_axis",type:"number",required:!1}],inferResultTypes:IO})),n.register(new H({name:"all_gather",numOperands:1,numResults:1,traits:[U.OPAQUE],attrs:[{name:"mesh_axis",type:"number",required:!1},{name:"gather_dim",type:"number",required:!1}],inferResultTypes(t,e){let s=t[0];if(!(s instanceof E))return null;let r=e&&e.has("mesh_axis")?e.get("mesh_axis"):0,o=e&&e.has("gather_dim")?e.get("gather_dim"):1,i=[...s.shape];return i[o]=i[o]*i[r],[new E(i,s.dtype)]}})),n.register(new H({name:"scaled_dot_product_attention",numOperands:3,numResults:1,traits:[U.OPAQUE],attrs:[{name:"scale",type:"number",required:!0},{name:"causal",type:"boolean",required:!1}],inferResultTypes(t){let e=t[0],s=t[2];return!(e instanceof E)||!(s instanceof E)?null:[new E([...e.shape.slice(0,e.rank-1),s.shape[s.rank-1]],e.dtype)]}})),n.register(new H({name:"softmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0}],inferResultTypes:vn,verify:gr})),n.register(new H({name:"log_softmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0}],inferResultTypes:vn,verify:gr})),n.register(new H({name:"gelu",numOperands:1,numResults:1,traits:[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:vn,verify:gr})),n.register(new H({name:"sigmoid",numOperands:1,numResults:1,traits:[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:vn,verify:gr})),n.register(new H({name:"silu",numOperands:1,numResults:1,traits:[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:vn,verify:gr})),n.register(new H({name:"layer_norm",numOperands:3,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"epsilon",type:"number",required:!0}],inferResultTypes:vn,verify(t){let e=[];if(t.numOperands!==3)return e.push("layer_norm expects 3 operands (input, gamma, beta)"),e;let s=t.getOperand(0).type;return s instanceof E&&!Oe(s.dtype)&&e.push(`layer_norm requires float input, got ${s.dtype}`),e}})),n.register(new H({name:"batch_norm",numOperands:5,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"epsilon",type:"number",required:!0}],inferResultTypes:vn,verify(t){let e=[];if(t.numOperands!==5)return e.push("batch_norm expects 5 operands (input, gamma, beta, mean, var)"),e;let s=t.getOperand(0).type;return s instanceof E&&!Oe(s.dtype)&&e.push(`batch_norm requires float input, got ${s.dtype}`),e}}));for(let t of["selu","mish","hardswish","hardsigmoid"])n.register(new H({name:t,numOperands:1,numResults:1,traits:[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:vn,verify:gr}));n.register(new H({name:"elu",numOperands:1,numResults:1,attrs:[{name:"alpha",type:"number"}],traits:[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:vn,verify:gr})),n.register(new H({name:"leaky_relu",numOperands:1,numResults:1,attrs:[{name:"negative_slope",type:"number"}],traits:[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:vn,verify:gr})),n.register(new H({name:"celu",numOperands:1,numResults:1,attrs:[{name:"alpha",type:"number"}],traits:[U.ELEMENTWISE,U.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:vn,verify:gr})),n.register(new H({name:"embedding",numOperands:2,numResults:1,inferResultTypes(t){if(t.length<2)return null;let e=t[0],s=t[1];if(!(e instanceof E)||!(s instanceof E))return null;let r=[...s.shape,e.shape[e.rank-1]];return[new E(r,e.dtype)]}}))}var Uw=T(()=>{we();kt();f(IO,"inferSameAsInput");f(vn,"inferSameAsInputFloat");f(gr,"verifyUnaryFloat");f(qw,"register")});function Ww(n){n.register(new H({name:"copy_to_device",numOperands:1,numResults:1,attrs:[{name:"src_device",type:"string",required:!0},{name:"dst_device",type:"string",required:!0}],sideEffects:Me.READ|Me.WRITE,traits:[U.INJECTIVE],inferResultTypes(t){return t.length!==1?null:[t[0]]},getFlops(){return 0}}))}var jw=T(()=>{we();f(Ww,"register")});function Kw(n,t,e,s,r,o){let i=n+s+r-t;return(o?Math.ceil(i/e):Math.floor(i/e))+1}function Hw(n){n.register(new H({name:"pool2d",numOperands:1,numResults:1,traits:[U.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(t,e){if(t.length<1)return null;let s=t[0];if(!(s instanceof E)||s.rank!==4)return null;let r=e.get?e.get("kernel_size"):e.kernel_size,o=e.get?e.get("strides"):e.strides,i=e.get?e.get("padding"):e.padding,a=(e.get?e.get("ceil_mode"):e.ceil_mode)||!1,c=Kw(s.shape[2],r[0],o[0],i[0][0],i[0][1],a),l=Kw(s.shape[3],r[1],o[1],i[1][0],i[1][1],a);return[new E([s.shape[0],s.shape[1],c,l],s.dtype)]}}))}var Yw=T(()=>{we();kt();f(Kw,"computePoolOutputDim");f(Hw,"register")});function Jw(n){n.register(new H({name:"resize",numOperands:1,numResults:1,traits:[U.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(t,e){if(t.length<1)return null;let s=t[0];if(!(s instanceof E)||s.rank!==4)return null;let r=e.get?e.get("output_size"):e.output_size;return[new E([s.shape[0],s.shape[1],r[0],r[1]],s.dtype)]}}))}var Zw=T(()=>{we();kt();f(Jw,"register")});function CO(n=MO){let t=new Ql;for(let e of n)e(t);return t}var MO,st,Ee=T(()=>{we();Bw();Aw();Ow();Ew();Iw();ng();Dw();Pw();$w();Gw();Uw();jw();Yw();Zw();MO=[vw,kw,Rw,Nw,Tw,Mw,Cw,Lw,zw,Vw,qw,Ww,Hw,Jw];f(CO,"buildRegistry");st=CO()});function Xw(n){if(typeof n=="number")return n*2654435761&2147483647;if(typeof n=="string"){let t=0;for(let e=0;e<n.length;e++)t=(t<<5)-t+n.charCodeAt(e)&2147483647;return t}if(typeof n=="boolean")return n?1:0;if(Array.isArray(n)){let t=2654435769;for(let e=0;e<n.length;e++)t=(t^Xw(n[e]))*16777619&2147483647;return t}if(ArrayBuffer.isView(n)&&n.buffer instanceof ArrayBuffer){let t=new Uint8Array(n.buffer,n.byteOffset,n.byteLength),e=2654435769;e=(e^t.length)*16777619&2147483647;let s=t.length>256?Math.ceil(t.length/256):1;for(let r=0;r<t.length;r+=s)e=(e^t[r])*16777619&2147483647;return e}return typeof n=="object"&&n!==null&&typeof n.hash=="function"?n.hash():0}function Qw(n,t){if(n===t)return!0;if(typeof n!=typeof t)return!1;if(Array.isArray(n)){if(!Array.isArray(t)||n.length!==t.length)return!1;for(let e=0;e<n.length;e++)if(!Qw(n[e],t[e]))return!1;return!0}return typeof n=="object"&&n!==null&&typeof n.equals=="function"?n.equals(t):!1}function Fn(n,t=new Map){let e=new ee;for(let s of n.blocks){let r=s.arguments.map(i=>i.type),o=new s.constructor(r);for(let i=0;i<s.arguments.length;i++)t.set(s.arguments[i],o.arguments[i]);for(let i of s)o.pushOp(i.clone(t));e.addBlock(o)}return e}var DO,mt,Rs=T(()=>{Xl();Gr();Ee();DO=0,mt=class n{static{f(this,"Operation")}constructor(t,e,s,r=null,o=null){if(this.opName=t,this.id=DO++,this.parentBlock=null,this._prev=null,this._next=null,this.attributes=new Map,r)if(r instanceof Map)for(let[i,a]of r)this.attributes.set(i,a);else for(let i of Object.keys(r))this.attributes.set(i,r[i]);this.operands=new Array(e.length),this._operandLinks=new Array(e.length);for(let i=0;i<e.length;i++){this.operands[i]=e[i];let a=new oc(this,i);e[i].addUse(a),this._operandLinks[i]=a}this.results=new Array(s.length);for(let i=0;i<s.length;i++)this.results[i]=new yo(s[i],this,i);if(this.regions=[],o)for(let i=0;i<o.length;i++){let a=o[i]instanceof ee?o[i]:new ee;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(t){return this.operands[t]}getResult(t){return this.results[t]}getRegion(t){return this.regions[t]}getAttr(t){return this.attributes.get(t)}hasAttr(t){return this.attributes.has(t)}setAttr(t,e){this.attributes.set(t,e)}removeAttr(t){return this.attributes.delete(t)}replaceOperand(t,e){let s=this.operands[t];if(s===e)return;s.removeUse(this._operandLinks[t]),this.operands[t]=e;let r=new oc(this,t);e.addUse(r),this._operandLinks[t]=r}dropAllOperands(){for(let t=0;t<this.operands.length;t++)this.operands[t].removeUse(this._operandLinks[t]);this.operands=[],this._operandLinks=[]}erase(){for(let t=0;t<this.results.length;t++)if(this.results[t].hasUses)throw new Error(`Cannot erase ${this.opName}: result ${t} still has uses`);this.dropAllOperands(),this.parentBlock&&this.parentBlock.removeOp(this)}replaceAllResultsWith(t){for(let e=0;e<this.results.length;e++)this.results[e].replaceAllUsesWith(t[e])}isTerminator(){let t=st.get(this.opName);return!!(t&&t.isTerminator)}hasSideEffects(){let t=st.get(this.opName);return!!(t&&t.hasSideEffects)}getParentFunction(){let t=this.parentBlock;for(;t;){let e=t.parentOp;if(!e)return t._parentFunction||null;t=e.parentBlock}return null}clone(t=new Map){let e=this.operands.map(o=>t.get(o)||o),s=this.regions.map(o=>Fn(o,t)),r=new n(this.opName,e,this.results.map(o=>o.type),new Map(this.attributes),s);for(let o=0;o<this.results.length;o++)t.set(this.results[o],r.results[o]);return r}structuralHash(){let t=2166136261;for(let e=0;e<this.opName.length;e++)t=(t^this.opName.charCodeAt(e))*16777619&2147483647;for(let e=0;e<this.operands.length;e++)t=(t^this.operands[e].id)*16777619&2147483647;for(let[e,s]of this.attributes){for(let r=0;r<e.length;r++)t=(t^e.charCodeAt(r))*16777619&2147483647;t=(t^Xw(s))*16777619&2147483647}return t}structuralEquals(t){if(this.opName!==t.opName||this.operands.length!==t.operands.length||this.results.length!==t.results.length||this.attributes.size!==t.attributes.size)return!1;for(let e=0;e<this.operands.length;e++)if(this.operands[e]!==t.operands[e])return!1;for(let[e,s]of this.attributes)if(!t.attributes.has(e)||!Qw(s,t.attributes.get(e)))return!1;for(let e=0;e<this.results.length;e++)if(!this.results[e].type.equals(t.results[e].type))return!1;return!0}};f(Xw,"hashAttrValue");f(Qw,"attrValueEquals");f(Fn,"cloneRegion")});function*tx(n){if(!(!n.regions||n.regions.length===0))for(let t of n.regions)for(let e of t.blocks)for(let s of e.ops())yield s,yield*tx(s)}function*ex(n){if(!(!n.regions||n.regions.length===0))for(let t of n.regions)for(let e of t.blocks){yield e;for(let s of e.ops())yield*ex(s)}}var os,Wr=T(()=>{Gr();f(tx,"opsInRegions");f(ex,"blocksInRegions");os=class{static{f(this,"GraphFunction")}constructor(t,e,s){this.name=t,this.inputTypes=Object.freeze([...e]),this.outputTypes=Object.freeze([...s]),this.body=new ee;let r=new Ne(e);r._parentFunction=this,this.body.addBlock(r),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 t of this.body)yield*t}*opsRecursive(){for(let t of this.ops())yield t,yield*tx(t)}*blocksRecursive(){for(let t of this.body){yield t;for(let e of t)yield*ex(e)}}opsArray(){let t=[];for(let e of this.body)for(let s of e)t.push(s);return t}numOps(){let t=0;for(let e of this.body)t+=e.size;return t}findOp(t){for(let e of this.ops())if(t(e))return e;return null}findOps(t){let e=[];for(let s of this.ops())t(s)&&e.push(s);return e}getReturnOp(){let t=this.entryBlock.lastOp;return t&&t.opName==="return"?t:null}getReturnValues(){let t=this.getReturnOp();return t?[...t.operands]:[]}verify(){let t=[];if(!this.entryBlock)return t.push("Function has no entry block"),t;this.entryBlock.arguments.length!==this.inputTypes.length&&t.push(`Entry block has ${this.entryBlock.arguments.length} args but function expects ${this.inputTypes.length}`);let e=this.getReturnOp();return e?e.numOperands!==this.outputTypes.length&&t.push(`Return has ${e.numOperands} operands but function declares ${this.outputTypes.length} outputs`):t.push("Function body has no return op"),t}}});var _s,xo=T(()=>{_s=class{static{f(this,"GraphModule")}constructor(t="module"){this.name=t,this._functions=new Map,this._version=0}get version(){return this._version}addFunction(t){return this._functions.set(t.name,t),t._module=this,this._version++,t}getFunction(t){return this._functions.get(t)||null}hasFunction(t){return this._functions.has(t)}removeFunction(t){let e=this._functions.delete(t);return e&&this._version++,e}get functionCount(){return this._functions.size}functionNames(){return[...this._functions.keys()]}*functions(){yield*this._functions.values()}*[Symbol.iterator](){yield*this._functions.values()}verify(){let t=[];this._functions.size===0&&t.push("Module has no functions");for(let e of this._functions.values()){let s=e.verify();for(let r=0;r<s.length;r++)t.push(`${e.name}: ${s[r]}`)}return t}}});function sx(n){return n?n instanceof _o?`tuple(${n.types.map(sx).join(", ")})`:n.shape!==void 0&&n.dtype!==void 0?`[${n.shape.join(",")}]:${n.dtype}`:String(n):"?"}function Ef(n,t,e){let s=n.rank,r=t<0?s+t:t,o=n.shape.map((a,c)=>c===r?1:a),i=[];for(let a=0;a<s;a++)a!==r&&i.push(a<r?a:a-1+e);return{offsetDims:i,collapsedSliceDims:[r],startIndexMap:[r],indexVectorDim:e,sliceSizes:o}}function LO(n,t){let e=Math.max(n.length,t.length),s=new Array(e);for(let r=0;r<e;r++){let o=r<n.length?n[n.length-1-r]:1,i=r<t.length?t[t.length-1-r]:1;s[e-1-r]=o===1?i:o}return s}function Tf(n,t){let e=[];for(let s=0;s<n;s++)s!==t&&e.push(s);return e}function PO(n){if(!n)return new Map;if(n instanceof Map)return n;let t=new Map;for(let e of Object.keys(n))t.set(e,n[e]);return t}function pc(n,t,e,s){let r=new os(n,t,e),o=new We(r);return s(o,r.args),r}var We,zn=T(()=>{kt();Rs();Gr();Wr();xo();Ee();f(sx,"describeType");f(Ef,"indexSelectGatherOpts");f(LO,"bcastBatchDims");We=class n{static{f(this,"IRBuilder")}constructor(t){this.func=t,this.block=t.entryBlock,this._insertionPoint=null}setInsertionPoint(t){this._insertionPoint=t}setInsertionPointToEnd(){this._insertionPoint=null}_insert(t){return this._insertionPoint?this.block.insertBefore(t,this._insertionPoint):this.block.pushOp(t),t}_buildOp(t,e,s,r=null,o=null){let i=new mt(t,e,s,r,o);return this._insert(i)}_inferAndBuild(t,e,s=null,r=null,o=null){let i=st.get(t),a=o;if(!a&&i&&i.inferResultTypes){let c=e.map(u=>u.type),l=s instanceof Map?s:PO(s);a=i.inferResultTypes(c,l,o)}if(!a){let c=e.map(u=>sx(u.type)).join(", "),l=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 '${t}' (${l}); operands: [${c}]`)}return this._buildOp(t,e,a,s,r)}constant(t,e){return this._buildOp("constant",[],[e],{value:t,tensor_type:e})}scalarConstant(t,e=J.F32){let s=new E([],e);return this.constant(t,s)}tensorConstant(t,e,s=J.F32){let r=new E(e,s);return this.constant(t,r)}iota(t,e){return this._buildOp("iota",[],[e],{iota_dimension:t,tensor_type:e})}add(t,e){return this._inferAndBuild("add",[t,e])}sub(t,e){return this._inferAndBuild("sub",[t,e])}mul(t,e){return this._inferAndBuild("mul",[t,e])}div(t,e){return this._inferAndBuild("div",[t,e])}rem(t,e){return this._inferAndBuild("rem",[t,e])}pow(t,e){return this._inferAndBuild("pow",[t,e])}maximum(t,e){return this._inferAndBuild("maximum",[t,e])}minimum(t,e){return this._inferAndBuild("minimum",[t,e])}neg(t){return this._inferAndBuild("neg",[t])}stopGradient(t){return this._inferAndBuild("stop_gradient",[t])}reverse(t,e){return this._inferAndBuild("reverse",[t],{dimensions:e})}scaledDotProductAttention(t,e,s,r,o=!1){return this._inferAndBuild("scaled_dot_product_attention",[t,e,s],{scale:r,causal:o})}allReduce(t,e={}){return this._inferAndBuild("all_reduce",[t],{reduce_op:e.reduceOp||"sum",mesh_axis:e.meshAxis??0})}allGather(t,e={}){return this._inferAndBuild("all_gather",[t],{mesh_axis:e.meshAxis??0,gather_dim:e.gatherDim??1})}abs(t){return this._inferAndBuild("abs",[t])}exp(t){return this._inferAndBuild("exp",[t])}log(t){return this._inferAndBuild("log",[t])}sqrt(t){return this._inferAndBuild("sqrt",[t])}rsqrt(t){return this._inferAndBuild("rsqrt",[t])}tanh(t){return this._inferAndBuild("tanh",[t])}sin(t){return this._inferAndBuild("sin",[t])}cos(t){return this._inferAndBuild("cos",[t])}floor(t){return this._inferAndBuild("floor",[t])}ceil(t){return this._inferAndBuild("ceil",[t])}sign(t){return this._inferAndBuild("sign",[t])}erf(t){return this._inferAndBuild("erf",[t])}log2(t){return this._inferAndBuild("log2",[t])}log10(t){return this._inferAndBuild("log10",[t])}exp2(t){return this._inferAndBuild("exp2",[t])}square(t){return this._inferAndBuild("square",[t])}reciprocal(t){return this._inferAndBuild("reciprocal",[t])}logicalNot(t){return this._inferAndBuild("logical_not",[t])}logicalAnd(t,e){return this._inferAndBuild("logical_and",[t,e])}logicalOr(t,e){return this._inferAndBuild("logical_or",[t,e])}compare(t,e,s){return this._inferAndBuild("compare",[t,e],{direction:s})}select(t,e,s){return this._inferAndBuild("select",[t,e,s])}clamp(t,e,s){return this._inferAndBuild("clamp",[t,e,s])}broadcast(t,e,s){return this._inferAndBuild("broadcast_in_dim",[t],{result_shape:e,broadcast_dimensions:s})}reshape(t,e){return this._inferAndBuild("reshape",[t],{new_shape:e})}transpose(t,e){return this._inferAndBuild("transpose",[t],{permutation:e})}slice(t,e,s,r=null){let o={starts:e,limits:s};return r&&(o.strides=r),this._inferAndBuild("slice",[t],o)}concat(t,e){return this._inferAndBuild("concat",t,{dimension:e})}pad(t,e,s,r,o=null){let i={low:s,high:r};return o&&(i.interior=o),this._inferAndBuild("pad",[t,e],i)}reduce(t,e,s,r){let o=new E([],t.type.dtype),i=new ee,a=new Ne([o,o]);return i.addBlock(a),this._inferAndBuild("reduce",[t,e],{dimensions:s,reduce_type:r},[i])}dot(t,e,s,r,o=[],i=[]){return this._inferAndBuild("dot",[t,e],{lhs_contracting:s,rhs_contracting:r,lhs_batch:o,rhs_batch:i})}matmul(t,e){let s=t.type.rank,r=e.type.rank;if(s===1&&r===1)return this.dot(t,e,[0],[0]);let o=t,i=e,a=!1,c=!1;s===1&&(o=this.reshape(t,[1,t.type.shape[0]]).getResult(0),a=!0),r===1&&(i=this.reshape(e,[e.type.shape[0],1]).getResult(0),c=!0);let l=o.type.rank,u=i.type.rank,h=i.type.shape.slice(0,u-2),p;if(h.length===0)p=this.dot(o,i,[l-1],[0]);else{let g=o.type.shape.slice(0,l-2),_=LO(g,h),y=_.length,b=this._broadcastBatch(o,g,_),x=this._broadcastBatch(i,h,_),S=Array.from({length:y},(v,w)=>w);p=this.dot(b,x,[y+1],[y],S,S)}if(!a&&!c)return p;let d=p.getResult(0).type.shape,m=new Set;return c&&m.add(d.length-1),a&&m.add(d.length-2),this.reshape(p.getResult(0),d.filter((g,_)=>!m.has(_)))}_broadcastBatch(t,e,s){let r=t.type.shape.slice(t.type.rank-2),o=[...s,...r];if(e.length===s.length&&e.every((c,l)=>c===s[l]))return t;let i=s.length-e.length,a=[];for(let c=0;c<e.length;c++)a.push(i+c);return a.push(o.length-2,o.length-1),this.broadcast(t,o,a).getResult(0)}conv(t,e,s,r,o={}){return this._inferAndBuild("conv",[t,e],{strides:s,padding:r,dilation:o.dilation||s.map(()=>1),groups:o.groups||1,input_layout:o.inputLayout||"NCHW",kernel_layout:o.kernelLayout||"OIHW"})}convert(t,e){return this._inferAndBuild("convert",[t],{target_dtype:e})}customCall(t,e,s,r=null){let o={call_target_name:t};return r&&(o.backend_config=r),this._buildOp("custom_call",e,s,o)}fusion(t,e,s,r){let o=new ee,i=new Ne(t.map(c=>c.type));o.addBlock(i);let a=this._buildOp("fusion",t,e,{fusion_kind:s},[o]);if(r){let c=new n(Object.create(this.func,{}));c.block=i,r(c,i.arguments)}return a}ifOp(t,e,s,r){let o=new ee,i=new Ne([]);o.addBlock(i);let a=new ee,c=new Ne([]);a.addBlock(c);let l=this._buildOp("if",[t],e,null,[o,a]);if(s){let u=new n(Object.create(this.func,{}));u.block=i,s(u)}if(r){let u=new n(Object.create(this.func,{}));u.block=c,r(u)}return l}whileOp(t,e,s){let r=t.map(u=>u.type),o=new ee,i=new Ne(r);o.addBlock(i);let a=new ee,c=new Ne(r);a.addBlock(c);let l=this._buildOp("while",t,r,null,[o,a]);if(e){let u=new n(Object.create(this.func,{}));u.block=i,e(u,i.arguments)}if(s){let u=new n(Object.create(this.func,{}));u.block=c,s(u,c.arguments)}return l}scanOp(t,e,s){let r=t.map(g=>g.type.withShape(g.type.shape.slice(1))),o=e.map(g=>g.type),i=new ee,a=new Ne([...r,...o]);i.addBlock(a);let c=new n(Object.create(this.func,{}));c.block=a;let l=a.arguments.slice(0,r.length),u=a.arguments.slice(r.length),[h,p]=s(c,l,u);if(c.yieldOp([...h,...p]),t.length===0)throw new Error("scanOp requires at least one xs input");let d=t[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<t.length;g++)if(t[g].type.shape[0]!==d)throw new Error("scanOp requires all xs inputs to share the same leading length");let m=p.map(g=>g.type.withShape([d,...g.type.shape]));return this._buildOp("scan",[...t,...e],[...o,...m],{num_carry:e.length,num_xs:t.length},[i])}returnOp(t){return this._buildOp("return",t,[])}yieldOp(t){return this._buildOp("yield",t,[])}relu(t){let e=this.scalarConstant(0,t.type.dtype),s=this.broadcast(e.getResult(0),t.type.shape,[]);return this.maximum(t,s.getResult(0))}softmax(t,e=-1){let s=e<0?t.type.rank+e:e;return this._inferAndBuild("softmax",[t],{axis:s})}logSoftmax(t,e=-1){let s=e<0?t.type.rank+e:e;return this._inferAndBuild("log_softmax",[t],{axis:s})}sigmoid(t){return this._inferAndBuild("sigmoid",[t])}gelu(t){return this._inferAndBuild("gelu",[t])}silu(t){return this._inferAndBuild("silu",[t])}elu(t,e=1){return this._inferAndBuild("elu",[t],{alpha:e})}leakyRelu(t,e=.01){return this._inferAndBuild("leaky_relu",[t],{negative_slope:e})}celu(t,e=1){return this._inferAndBuild("celu",[t],{alpha:e})}selu(t){return this._inferAndBuild("selu",[t])}mish(t){return this._inferAndBuild("mish",[t])}hardswish(t){return this._inferAndBuild("hardswish",[t])}hardsigmoid(t){return this._inferAndBuild("hardsigmoid",[t])}layernorm(t,e,s,r=-1,o=1e-5){let i=r<0?t.type.rank+r:r;return this._inferAndBuild("layer_norm",[t,e,s],{axis:i,epsilon:o})}batchnorm(t,e,s,r,o,i=1,a=1e-5){return this._inferAndBuild("batch_norm",[t,e,s,r,o],{axis:i,epsilon:a})}where(t,e,s){return this._inferAndBuild("where",[t,e,s])}split(t,e,s){return this._inferAndBuild("split",[t],{dimension:e,split_sizes:s})}oneHot(t,e,s={}){return this._inferAndBuild("one_hot",[t],{depth:e,axis:s.axis??-1,on_value:s.onValue??1,off_value:s.offValue??0,dtype:s.dtype||t.type.dtype})}embedding(t,e){return this._inferAndBuild("embedding",[t,e])}gather(t,e,s){return this._inferAndBuild("gather",[t,e],{offset_dims:s.offsetDims,collapsed_slice_dims:s.collapsedSliceDims,start_index_map:s.startIndexMap,index_vector_dim:s.indexVectorDim,slice_sizes:s.sliceSizes})}scatter(t,e,s,r){let o=new E([],t.type.dtype),i=new ee;return i.addBlock(new Ne([o,o])),this._inferAndBuild("scatter",[t,e,s],{update_window_dims:r.updateWindowDims,inserted_window_dims:r.insertedWindowDims,scatter_dims_to_operand_dims:r.scatterDimsToOperandDims,index_vector_dim:r.indexVectorDim},[i])}scatterAdd(t,e,s,r){return this.scatter(t,e,s,r)}_dimCoordIndices(t,e,s){let r=s.length,o=t.type.dtype==="i32"?t:this.convert(t,"i32").getResult(0),i=[...s,1],a=[];for(let c=0;c<r;c++){let l=c===e?o:this.iota(c,new E(s,"i32")).getResult(0);a.push(this.reshape(l,i).getResult(0))}return this.concat(a,r).getResult(0)}gatherDim(t,e,s){let r=t.type.rank,o=s<0?r+s:s,i=[],a=[];for(let l=0;l<r;l++)i.push(l),a.push(1);let c=this._dimCoordIndices(e,o,e.type.shape);return this.gather(t,c,{offsetDims:[],collapsedSliceDims:i,startIndexMap:i,indexVectorDim:r,sliceSizes:a})}scatterAddDim(t,e,s,r){let o=t.type.rank,i=r<0?o+r:r,a=[];for(let l=0;l<o;l++)a.push(l);let c=this._dimCoordIndices(e,i,e.type.shape);return this.scatterAdd(t,c,s,{updateWindowDims:[],insertedWindowDims:a,scatterDimsToOperandDims:a,indexVectorDim:o})}argmax(t,e,s=!1){let r=e<0?t.type.rank+e:e;return this._inferAndBuild("argmax",[t],{axis:r,keep_dims:s})}argmin(t,e,s=!1){let r=e<0?t.type.rank+e:e;return this._inferAndBuild("argmin",[t],{axis:r,keep_dims:s})}pool2d(t,e,s,r,o,i={}){return this._inferAndBuild("pool2d",[t],{pool_type:e,kernel_size:s,strides:r,padding:o,ceil_mode:i.ceilMode||!1,count_include_pad:i.countIncludePad||!1,layout:i.layout||"NCHW"})}resize(t,e,s,r={}){return this._inferAndBuild("resize",[t],{output_size:e,method:s,coordinate_mode:r.coordinateMode||"asymmetric",layout:r.layout||"NCHW"})}};f(Tf,"broadcastDimsExcluding");f(PO,"toMap");f(pc,"buildFunction")});function De(n,t,e){if(e==null)return new D(n,t,e);let s=t instanceof C,r=e instanceof C;if(s&&r){let o=t.value,i=e.value;switch(n){case"+":return new C(o+i);case"-":return new C(o-i);case"*":return new C(o*i);case"//":if(i!==0)return new C(Math.trunc(o/i));break;case"%":if(i!==0)return new C((o%i+i)%i);break}}if(r){let o=e.value;if((n==="+"||n==="-")&&o===0||n==="*"&&o===1)return t;if(n==="*"&&o===0)return new C(0);if(n==="//"&&o===1)return t;if(n==="%"&&o===1)return new C(0)}if(s){let o=t.value;if(n==="+"&&o===0||n==="*"&&o===1)return e;if(n==="*"&&o===0)return new C(0)}return new D(n,t,e)}var V,xe,jr,ht,tt,og,hc,X,K,Mt,Os,fs,ft,Ei,If,So,_t,D,Yt,Ut,se,C,it,Ct=T(()=>{V=Object.freeze({SERIAL:"serial",PARALLEL:"parallel",VECTORIZED:"vectorized",UNROLLED:"unrolled",THREAD_BINDING:"thread_binding",RECURRENCE:"recurrence"}),xe=class n{static{f(this,"TensorNode")}constructor(){this.type=this.constructor.name.replace(/^_+/,""),this._parent=null,this._parentKey=null,this._parentIdx=-1}_setChild(t,e,s=-1){e instanceof n&&(e._parent=this,e._parentKey=t,e._parentIdx=s)}_setChildren(t,e){if(e)for(let s=0;s<e.length;s++)e[s]instanceof n&&(e[s]._parent=this,e[s]._parentKey=t,e[s]._parentIdx=s)}replaceWith(t){let e=this._parent;return e?(this._parentIdx>=0?e[this._parentKey][this._parentIdx]=t:e[this._parentKey]=t,t instanceof n&&(t._parent=e,t._parentKey=this._parentKey,t._parentIdx=this._parentIdx),this._parent=null,this._parentKey=null,this._parentIdx=-1,!0):!1}},jr=class extends xe{static{f(this,"PrimFunc")}constructor(t,e,s,r=new Map,o=[],i=null){super(),this.name=t,this.params=e,this.body=s,this.bufferMap=r,this.shapeParams=o,this.shapeParamMap=i||new Map,this._setChild("body",s)}},ht=class extends xe{static{f(this,"ForNode")}constructor(t,e,s,r,o,i=null){super(),this.loopVar=t,this.min=e,this.extent=s,this.kind=r,this.body=o,this.threadTag=i,this._setChild("body",o)}},tt=class extends xe{static{f(this,"BlockNode")}constructor(t,e,s,r,o,i=null){super(),this.name=t,this.iterVars=e,this.reads=s,this.writes=r,this.body=o,this.initBody=i,this._setChild("body",o),this._setChild("initBody",i)}},og=Object.freeze({DATA_PAR:"DataPar",COMM_REDUCE:"CommReduce"}),hc=class extends xe{static{f(this,"BlockRealizeNode")}constructor(t,e,s=og.DATA_PAR){super(),this.iterVar=t,this.binding=e,this.kind=s}},X=class extends xe{static{f(this,"BufferStoreNode")}constructor(t,e,s){super(),this.buffer=t,this.indices=e,this.value=s}},K=class extends xe{static{f(this,"BufferLoadNode")}constructor(t,e){super(),this.buffer=t,this.indices=e}},Mt=class extends xe{static{f(this,"IfThenElseNode")}constructor(t,e,s=null){super(),this.condition=t,this.thenBody=e,this.elseBody=s,this._setChild("thenBody",e),this._setChild("elseBody",s)}},Os=class extends xe{static{f(this,"LetStmtNode")}constructor(t,e,s){super(),this.variable=t,this.value=e,this.body=s,this._setChild("body",s)}},fs=class extends xe{static{f(this,"AllocateNode")}constructor(t,e,s){super(),this.buffer=t,this.scope=e,this.body=s,this._setChild("body",s)}},ft=class extends xe{static{f(this,"SeqNode")}constructor(t){super(),this.stmts=t,this._setChildren("stmts",t)}},Ei=class extends xe{static{f(this,"WhileNode")}constructor(t,e,s){super(),this.condVar=t,this.condBody=e,this.loopBody=s,this._setChild("condBody",e),this._setChild("loopBody",s)}},If=class extends xe{static{f(this,"EvaluateNode")}constructor(t){super(),this.value=t}},So=class extends xe{static{f(this,"SyncThreadsNode")}constructor(){super()}},_t=class extends xe{static{f(this,"CallExternNode")}constructor(t,e,s){super(),this.externName=t,this.args=e,this.dtype=s}},D=class extends xe{static{f(this,"MathOpNode")}constructor(t,e,s=null){super(),this.op=t,this.a=e,this.b=s}},Yt=class n extends xe{static{f(this,"CompareNode")}static JS_OPS={eq:"===",ne:"!==",lt:"<",le:"<=",gt:">",ge:">="};static C_OPS={eq:"==",ne:"!=",lt:"<",le:"<=",gt:">",ge:">="};constructor(t,e,s){super(),this.direction=t,this.a=e,this.b=s}toJS(){let t=n.JS_OPS[this.direction];if(!t)throw new Error(`CompareNode: unsupported direction '${this.direction}'`);return t}toC(){let t=n.C_OPS[this.direction];if(!t)throw new Error(`CompareNode: unsupported direction '${this.direction}'`);return t}},Ut=class extends xe{static{f(this,"CastNode")}constructor(t,e,s){super(),this.expr=t,this.fromDtype=e,this.toDtype=s}},se=class extends xe{static{f(this,"VariableNode")}constructor(t,e){super(),this.name=t,this.dtype=e}},C=class extends xe{static{f(this,"IntImmNode")}constructor(t){super(),this.value=t}},it=class extends xe{static{f(this,"FloatImmNode")}constructor(t){super(),this.value=t}};f(De,"mathOp")});var Ti,ig=T(()=>{Ti=Object.freeze({GLOBAL:"global",SHARED:"shared",LOCAL:"local",REGISTER:"register"})});var Vt,vo=T(()=>{As();Vt=class{static{f(this,"Buffer")}constructor(t,e,s,r,o=null,i=0,a=64){if(this.name=t,this.shape=e,this.dtype=s,this.scope=r,this.offset=i,this.alignment=a,this.broadcastDims=null,o)this.strides=o;else{this.strides=new Array(e.length);let c=1;for(let l=e.length-1;l>=0;l--)this.strides[l]=c,typeof e[l]=="number"&&(c*=e[l])}}get rank(){return this.shape.length}get isScalar(){return this.shape.length===0}numel(){let t=1;for(let e=0;e<this.shape.length;e++){if(typeof this.shape[e]!="number")return-1;t*=this.shape[e]}return t}sizeInBytes(){let t=this.numel();return t<0?-1:t*cf(this.dtype)}}});function cg(n,{name:t,compute:e,plevel:s=10,targetKind:r=null}){let o=ag.get(n);return o||(o=new dc(n),ag.set(n,o)),o.addImplementation(t,e,s,r),o}function Cf(n,t=null){let e=ag.get(n);if(!e)return null;let s=t?t.kind:null,r=new dc(n);for(let o of e.implementations)(o.targetKind===null||o.targetKind===s)&&r.implementations.push(o);return r.implementations.length>0?r:null}function ug(n,t=null){let e=Cf(n,t);return e?e.best():null}var Mf,dc,ag,lg=T(()=>{Mf=class{static{f(this,"OpImplementation")}constructor(t,e,s=10,r=null){this.name=t,this.compute=e,this.plevel=s,this.targetKind=r}},dc=class{static{f(this,"OpStrategy")}constructor(t){this.opName=t,this.implementations=[]}addImplementation(t,e,s=10,r=null){let o=new Mf(t,e,s,r),i=this.implementations.findIndex(a=>a.name===t);return i>=0?this.implementations[i]=o:this.implementations.push(o),this}get candidates(){return this.implementations}best(){let t=null;for(let e of this.implementations)(t===null||e.plevel>t.plevel)&&(t=e);return t}},ag=new Map;f(cg,"registerOpStrategy");f(Cf,"getOpStrategy");f(ug,"selectImplementation")});function pg(n,t){return Cf(n,t)?!0:Bn.has(n)}function vt(n,t,e=zO){cg(n,{name:`${n}.generic`,compute:t,plevel:e,targetKind:null})}function Kr(n,t){let e=ug(n,t);return e?e.compute:void 0}function ne(n,t,e){let s=t.length,r=n.allocVarArray("i",s),o=n.allocBindArray("v",r),i=new Array(s);for(let c=0;c<s;c++)i[c]=o[c].iterVar;let a=e?n.extentNodes(t,e):null;return{loopVars:r,loopBinds:o,indices:i,extentNodes:a}}function gc(n,t,e){let s=n;for(let r=t.length-1;r>=0;r--)s=new ht(t[r],new C(0),e[r],V.SERIAL,s);return s}function $O(n,t,e){let s=n;for(let r=t.length-1;r>=0;r--)s=new ht(t[r],new C(0),new C(e[r]),V.SERIAL,s);return s}function re(n,t,e,s){return s?gc(n,t,s):$O(n,t,e)}function je(n,t,e,s,r){let o=e.length,i=new Array(o),a=new Array(o),c=new Array(o),l=new Array(o);for(let u=0;u<o;u++)i[u]=n.allocVar(`${t}${e[u]}`),a[u]=new hc(n.allocVar(`${t}v${e[u]}`),i[u]),c[u]=a[u].iterVar,l[u]=n.extentNode(s[e[u]],r,e[u]);return{vars:i,ivs:a,indices:c,extentNodes:l,wrap(u){return gc(u,i,l)}}}function _r(n,t,e){let s=n.shape.length;if(n.broadcastDims){let a=n.broadcastDims,c=new Array(s);for(let l=0;l<s;l++)c[l]=n.shape[l]===1?new C(0):e[a[l]];return c}let o=t.shape.length-s,i=new Array(s);for(let a=0;a<s;a++)i[a]=n.shape[a]===1?new C(0):e[o+a];return i}function Ii(n){let t=new Array(n.length);for(let e=0;e<n.length;e++)t[e]={buffer:n[e]};return t}function mc(){let n=0;for(let s=0;s<arguments.length;s++)n+=arguments[s].length;let t=new Array(n),e=0;for(let s=0;s<arguments.length;s++){let r=arguments[s];for(let o=0;o<r.length;o++)t[e++]=r[o]}return t}function fg(n){let t=new Array(n.length);for(let e=0;e<n.length;e++)t[e]=n[e].iterVar;return t}function Vn(n,t,e,s,r){let o=s[0],{loopVars:i,loopBinds:a,indices:c,extentNodes:l}=ne(n,o.shape,o),u=new Array(e.length);for(let m=0;m<e.length;m++){let g=_r(e[m],o,c);u[m]=new K(e[m],g)}let h=r(t,u,o.dtype),p=new X(o,c,h),d=new tt(n.blockName(`${t.opName}_block`),a,Ii(e),[{buffer:o}],p);return re(d,i,o.shape,l)}function Mi(n,t){let e=t.getResult(0),s=t.getAttr("value"),r=e.type&&e.type.shape||[];if(typeof s=="number"&&r.length>0&&!n.bufferMap.has(e)){let g=e.type&&e.type.dtype||"f32",_=new Array(r.length).fill(1),y=new Vt(`buf_${n.varCounter++}`,_,g,Ti.GLOBAL);y.broadcastDims=Array.from({length:r.length},(x,S)=>S),n.bufferMap.set(e,y);let b=Bs(g)?new C(s):new it(s);return new X(y,_.map(()=>new C(0)),b)}let o=n.getOrAllocBuffer(e),i=Bs(o.dtype),a=f(g=>i?new C(g):new it(g),"imm");if(s&&typeof s!="number"&&typeof s.length=="number"){if(o.shape.length===0)return new X(o,[],a(s[0]));let g=new Array(o.shape.length),_=1;for(let b=o.shape.length-1;b>=0;b--)g[b]=_,_*=o.shape[b];let y=[];for(let b=0;b<s.length;b++){let x=new Array(o.shape.length);for(let S=0;S<o.shape.length;S++)x[S]=new C(Math.floor(b/g[S])%o.shape[S]);y.push(new X(o,x,a(s[b])))}return new ft(y)}let c=a(typeof s=="number"?s:0);if(o.shape.length===0)return new X(o,[],c);let{loopVars:l,loopBinds:u,indices:h,extentNodes:p}=ne(n,o.shape,o),d=new X(o,h,c),m=new tt(n.blockName(`${t.opName}_block`),u,[],[{buffer:o}],d);return re(m,l,o.shape,p)}function Hr(n){let t={};for(let e=0;e<n.length;e++)t[n[e]]=e;return t}function nx(n,t){if(!n.broadcastDims)return t;let e=n.broadcastDims,s=new Array(n.shape.length);for(let r=0;r<n.shape.length;r++)s[r]=n.shape[r]===1?new C(0):t[e[r]];return s}function _c(n,t,e,s){let r=t.getAttr("lhs_contracting")||[],o=t.getAttr("rhs_contracting")||[],i=t.getAttr("lhs_batch")||[],a=t.getAttr("rhs_batch")||[],c=t.getOperand(0).type.shape,l=t.getOperand(1).type.shape,u=new Set(r),h=new Set(i),p=new Set(o),d=new Set(a),m=[];for(let P=0;P<c.length;P++)!u.has(P)&&!h.has(P)&&m.push(P);let g=[];for(let P=0;P<l.length;P++)!p.has(P)&&!d.has(P)&&g.push(P);let _=n.allocVarArray("b",i.length),y=n.allocVarArray("ls",m.length),b=n.allocVarArray("rs",g.length),x=n.allocVarArray("c",r.length),S=n.allocBindArray("vb",_),v=n.allocBindArray("vls",y),w=n.allocBindArray("vrs",b),A=n.allocBindArray("vc",x),I=mc(fg(S),fg(v),fg(w)),B=new Array(c.length);for(let P=0;P<i.length;P++)B[i[P]]=S[P].iterVar;for(let P=0;P<m.length;P++)B[m[P]]=v[P].iterVar;for(let P=0;P<r.length;P++)B[r[P]]=A[P].iterVar;let k=new Array(l.length);for(let P=0;P<a.length;P++)k[a[P]]=S[P].iterVar;for(let P=0;P<g.length;P++)k[g[P]]=w[P].iterVar;for(let P=0;P<o.length;P++)k[o[P]]=A[P].iterVar;let N=nx(e,B),R=nx(s,k),O=mc(S,v,w,A),M=[{vars:_,dims:i,shape:c,buf:e},{vars:y,dims:m,shape:c,buf:e},{vars:b,dims:g,shape:l,buf:s},{vars:x,dims:r,shape:c,buf:e}];function F(P){let G=P;for(let W=M.length-1;W>=0;W--){let{vars:$,dims:L,shape:q,buf:yt}=M[W];for(let lt=$.length-1;lt>=0;lt--){let pt=yt.broadcastDims?new C(q[L[lt]]):n.extentNode(q[L[lt]],yt,L[lt]);G=new ht($[lt],new C(0),pt,V.SERIAL,G)}}return G}return f(F,"wrapAccBody"),{outIdx:I,lhsIdx:N,rhsIdx:R,allIvs:O,wrapAccBody:F}}var Bn,zO,Df,Ks=T(()=>{kt();ig();vo();As();Ct();lg();lg();Bn=new Set(["constant","scalar_constant"]),zO=10;f(pg,"hasLoweringRule");f(vt,"registerLoweringRule");f(Kr,"getLoweringRule");Df=class{static{f(this,"LoweringContext")}constructor(){this.bufferMap=new Map,this.varCounter=0,this.shapeParams=new Map,this.symbolToVar=new Map,this._blockCounter=0}blockName(t){return`${t}_${this._blockCounter++}`}allocVar(t,e="int32"){return new se(`${t}_${this.varCounter++}`,e)}getOrAllocBuffer(t){let e=this.bufferMap.get(t);if(e)return e;let s=t.type,r=s.shape||[],o=s.dtype||"f32",i=s.layout?s.layout.computeStrides(r):null;return e=new Vt(`buf_${this.varCounter++}`,r,o,Ti.GLOBAL,i),t.symbolicShape&&(e.symbolicShape=t.symbolicShape),this.bufferMap.set(t,e),this._registerDynamicDims(e),e}allocFreshBuffer(t){let e=t.type,s=e.shape||[],r=e.dtype||"f32",o=e.layout?e.layout.computeStrides(s):null,i=new Vt(`buf_${this.varCounter++}`,s,r,Ti.GLOBAL,o);return t.symbolicShape&&(i.symbolicShape=t.symbolicShape),this._registerDynamicDims(i),i}_registerDynamicDims(t){for(let e=0;e<t.shape.length;e++)t.shape[e]===Z&&this.extentNode(Z,t,e)}_shapeParamVar(t,e){let s=e>=0?`${t.name}:${e}`:`${t.name}:dyn`,r=this.shapeParams.get(s);if(r)return r;let o=t.symbolicShape&&e>=0&&typeof t.symbolicShape[e]!="number"?t.symbolicShape[e]:null;return o!==null&&this.symbolToVar.has(o)?r=this.symbolToVar.get(o):(r=this.allocVar("_ds"),o!==null&&this.symbolToVar.set(o,r)),this.shapeParams.set(s,r),r}extentNode(t,e,s=-1){return t!==Z?new C(t):this._shapeParamVar(e,s)}extentNodes(t,e){let s=new Array(t.length);for(let r=0;r<t.length;r++)s[r]=t[r]===Z?this._shapeParamVar(e,r):new C(t[r]);return s}allocVarArray(t,e){let s=new Array(e);for(let r=0;r<e;r++)s[r]=this.allocVar(`${t}${r}`);return s}allocBindArray(t,e){let s=new Array(e.length);for(let r=0;r<e.length;r++)s[r]=new hc(this.allocVar(`${t}${r}`),e[r]);return s}};f(ne,"makeLoopNest");f(gc,"wrapLoopsWithNodes");f($O,"wrapLoops");f(re,"wrapInLoops");f(je,"buildSpatialNest");f(_r,"computeBroadcastIndices");f(Ii,"bufRefs");f(mc,"concatIterVars");f(fg,"extractIterVars");f(Vn,"lowerPointwise");f(Mi,"lowerConstant");f(Hr,"parseLayout");f(nx,"physicalDotIndices");f(_c,"buildDotGeometry")});function hg(n,t,e){let s=Ci[n];return s?rx[n]?rx[n](t):t.length===2&&FO.has(s)?new D(s,t[0],t[1]):t.length===2&&VO.has(s)?new D(s,t[0],t[1]):t.length===1&&GO.has(s)?new D(s,t[0]):t.length===1&&s==="-"?new D("-",t[0]):new _t(s,t,e):null}function ox(){for(let n of Object.keys(Ci))vt(n,(t,e,s,r)=>Vn(t,e,s,r,(o,i,a)=>hg(o.opName,i,a)));vt("compare",(n,t,e,s)=>Vn(n,t,e,s,(r,o)=>new Yt(r.getAttr("direction")||"eq",o[0],o[1]))),vt("select",(n,t,e,s)=>Vn(n,t,e,s,(r,o)=>new Mt(o[0],o[1],o[2]))),vt("clamp",(n,t,e,s)=>Vn(n,t,e,s,(r,o,i)=>new _t("min",[new _t("max",[o[1],o[0]],i),o[2]],i))),vt("convert",(n,t,e,s)=>Vn(n,t,e,s,(r,o)=>new Ut(o[0],e[0].dtype,s[0].dtype))),vt("copy_to_device",(n,t,e,s)=>Vn(n,t,e,s,(r,o)=>o[0]))}var FO,VO,GO,rx,Ci,Lf=T(()=>{Ct();Ks();FO=new Set(["+","-","*","/"]),VO=new Set(["&&","||"]),GO=new Set(["!"]),rx={square:f(n=>new D("*",n[0],n[0]),"square"),reciprocal:f(n=>new D("/",new it(1),n[0]),"reciprocal")},Ci={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",log2:"log2",log10:"log10",exp2:"exp2",square:"square",reciprocal:"reciprocal",logical_not:"!",logical_and:"&&",logical_or:"||"};f(hg,"buildElementwiseExpr");f(ox,"register")});function ax(){vt("broadcast_in_dim",ix),vt("broadcast",ix),vt("transpose",(n,t,e,s)=>{let r=t.getAttr("permutation"),o=e[0],i=s[0],{loopVars:a,loopBinds:c,indices:l,extentNodes:u}=ne(n,i.shape,i),h=new Array(o.shape.length);for(let m=0;m<r.length;m++)h[r[m]]=l[m];let p=new X(i,l,new K(o,h)),d=new tt(n.blockName("transpose_block"),c,[{buffer:o}],[{buffer:i}],p);return re(d,a,i.shape,u)}),vt("reverse",(n,t,e,s)=>{let r=new Set(t.getAttr("dimensions")),o=e[0],i=s[0],{loopVars:a,loopBinds:c,indices:l,extentNodes:u}=ne(n,i.shape,i),h=new Array(o.shape.length);for(let m=0;m<o.shape.length;m++)h[m]=r.has(m)?new D("-",new D("-",u[m],new C(1)),l[m]):l[m];let p=new X(i,l,new K(o,h)),d=new tt(n.blockName("reverse_block"),c,[{buffer:o}],[{buffer:i}],p);return re(d,a,i.shape,u)}),vt("reshape",(n,t,e,s)=>{let r=e[0],o=s[0],{loopVars:i,loopBinds:a,indices:c,extentNodes:l}=ne(n,o.shape,o),u;if(r.shape.length===o.shape.length&&r.shape.every((m,g)=>m===o.shape[g]))u=c;else{for(let y=1;y<r.shape.length;y++)if(typeof r.shape[y]!="number"||r.shape[y]<0)throw new Error("reshape lowering requires static non-leading input dims");for(let y=1;y<o.shape.length;y++)if(typeof o.shape[y]!="number"||o.shape[y]<0)throw new Error("reshape lowering requires static non-leading output dims");let m=c[o.shape.length-1],g=1;for(let y=o.shape.length-2;y>=0;y--)g*=o.shape[y+1],m=De("+",m,De("*",c[y],new C(g)));u=new Array(r.shape.length);let _=m;for(let y=r.shape.length-1;y>=0;y--)y===0?u[y]=_:(u[y]=De("%",_,new C(r.shape[y])),_=De("//",_,new C(r.shape[y])))}let p=new X(o,c,new K(r,u)),d=new tt(n.blockName("reshape_block"),a,[{buffer:r}],[{buffer:o}],p);return re(d,i,o.shape,l)}),vt("slice",(n,t,e,s)=>{let r=e[0],o=s[0],i=t.getAttr("starts"),a=t.getAttr("strides")||i.map(()=>1),{loopVars:c,loopBinds:l,indices:u,extentNodes:h}=ne(n,o.shape,o),p=new Array(r.shape.length);for(let g=0;g<r.shape.length;g++){let _=new C(i[g]);a[g]===1?p[g]=new D("+",_,u[g]):p[g]=new D("+",_,new D("*",u[g],new C(a[g])))}let d=new X(o,u,new K(r,p)),m=new tt(n.blockName("slice_block"),l,[{buffer:r}],[{buffer:o}],d);return re(m,c,o.shape,h)}),vt("pad",(n,t,e,s)=>{let r=e[0],o=e[1],i=s[0],a=t.getAttr("low"),c=t.getAttr("interior")||a.map(()=>0),{loopVars:l,loopBinds:u,indices:h,extentNodes:p}=ne(n,i.shape,i),d=new Array(r.shape.length),m=new C(1);for(let S=0;S<r.shape.length;S++){let v=new D("+",h[S],new C(-a[S]));if(c[S]>0){let I=c[S]+1,B=new D("%",v,new C(I)),k=new Yt("eq",B,new C(0));m=new D("*",m,k),d[S]=new D("//",v,new C(I))}else d[S]=v;let w=new Yt("ge",d[S],new C(0)),A=new Yt("lt",d[S],new C(r.shape[S]));m=new D("*",m,new D("*",w,A))}let g=new K(r,d),_=new K(o,[]),y=new Mt(m,g,_),b=new X(i,h,y),x=new tt(n.blockName("pad_block"),u,[{buffer:r},{buffer:o}],[{buffer:i}],b);return re(x,l,i.shape,p)}),vt("concat",(n,t,e,s)=>{let r=s[0],o=t.getAttr("dimension"),i=[],a=0;for(let c=0;c<e.length;c++){let l=e[c],{loopVars:u,loopBinds:h,indices:p,extentNodes:d}=ne(n,l.shape,l),m=new Array(l.shape.length);for(let y=0;y<l.shape.length;y++)m[y]=y===o&&a>0?new D("+",p[y],new C(a)):p[y];let g=new X(r,m,new K(l,p)),_=new tt(n.blockName("concat"),h,[{buffer:l}],[{buffer:r}],g);i.push(re(_,u,l.shape,d)),a+=l.shape[o]}return i.length===1?i[0]:new ft(i)}),vt("iota",(n,t,e,s)=>{let r=s[0],o=t.getAttr("iota_dimension"),{loopVars:i,loopBinds:a,indices:c,extentNodes:l}=ne(n,r.shape,r),u=new Ut(c[o],"index",r.dtype),h=new X(r,c,u),p=new tt(n.blockName("iota_block"),a,[],[{buffer:r}],h);return re(p,i,r.shape,l)})}function ix(n,t,e,s){let r=e[0],o=s[0],i=t.getAttr("broadcast_dimensions")||[],{loopVars:a,loopBinds:c,indices:l,extentNodes:u}=ne(n,o.shape,o),h=new Array(r.shape.length);for(let m=0;m<r.shape.length;m++){let g=i.length>0?i[m]:m+(o.shape.length-r.shape.length);h[m]=r.shape[m]===1?new C(0):l[g]}let p=new X(o,l,new K(r,h)),d=new tt(n.blockName("broadcast_block"),c,[{buffer:r}],[{buffer:o}],p);return re(d,a,o.shape,u)}var cx=T(()=>{Ct();Ks();f(ax,"register");f(ix,"lowerBroadcast")});function WO(n,t){if(Bs(n)){let e=t?qO[n]??-2147483648:UO[n]??2147483647;return new C(e)}return new it(t?-1/0:1/0)}function lx(){vt("reduce",(t,e,s,r)=>{let o=s[0],i=s[1],a=r[0],c=e.getAttr("dimensions")||[],l=e.getAttr("reduce_type")||"sum",u=new Set(c),h=[],p=[];for(let O=0;O<o.shape.length;O++)(u.has(O)?p:h).push(O);let d=je(t,"si",h,o.shape,o),m=new X(a,d.indices,new K(i,[])),g=new tt(t.blockName("reduce_init"),d.ivs,[{buffer:i}],[{buffer:a}],m),_=h.length>0?d.wrap(g):g,y=je(t,"sa",h,o.shape,o),b=t.allocVarArray("r",p.length),x=t.allocBindArray("rv",b);for(let O of x)O.kind=og.COMM_REDUCE;let S=new Array(o.shape.length);for(let O=0;O<h.length;O++)S[h[O]]=y.ivs[O].iterVar;for(let O=0;O<p.length;O++)S[p[O]]=x[O].iterVar;let v=new K(a,y.indices),w=new K(o,S),A=ux[l]||ux.sum,I=new X(a,y.indices,A(v,w,a.dtype)),B=new Array(p.length);for(let O=0;O<p.length;O++)B[O]=t.extentNode(o.shape[p[O]],o,p[O]);let k=new tt(t.blockName("reduce_acc"),mc(y.ivs,x),[{buffer:o}],[{buffer:a}],I),N=gc(k,b,B);N=y.wrap(N);let R=[_,N];if(l==="mean"){let O=1,M=[];for(let L=0;L<p.length;L++){let q=o.shape[p[L]];q===Z?M.push(t.extentNode(Z,o,p[L])):O*=q}let F=je(t,"sm",h,o.shape,o),P=new K(a,F.indices),G;if(M.length===0)G=new D("*",P,new it(1/O));else{let L=new C(O);for(let q of M)L=new D("*",L,q);G=new D("/",P,L)}let W=new X(a,F.indices,G),$=new tt(t.blockName("mean_div"),F.ivs,[{buffer:a}],[{buffer:a}],W);R.push(h.length>0?F.wrap($):$)}return new ft(R)});function n(t,e){vt(t,(s,r,o,i)=>{let a=o[0],c=i[0],l=r.getAttr("axis"),u=r.getAttr("keep_dims")||!1,h=new Set([l]),p=[],d=l;for(let q=0;q<a.shape.length;q++)h.has(q)||p.push(q);let m=f(q=>{if(!u)return q.indices;let yt=new Array(a.shape.length);for(let lt=0;lt<p.length;lt++)yt[p[lt]]=q.indices[lt];return yt[d]=new C(0),yt},"outIndicesFor"),g=new Vt("_argval_"+s.varCounter,p.map(q=>a.shape[q]),a.dtype,"global");s.varCounter++;let _=je(s,"ai",p,a.shape,a),y=new X(g,_.indices,WO(a.dtype,e==="gt")),b=new X(c,m(_),new C(0)),x=new tt(s.blockName("arg_init"),_.ivs,[],[{buffer:g},{buffer:c}],new ft([y,b])),S=p.length>0?_.wrap(x):x,v=je(s,"as",p,a.shape,a),w=s.allocVar("ar"),A=s.allocBindArray("arv",[w]),I=new Array(a.shape.length);for(let q=0;q<p.length;q++)I[p[q]]=v.ivs[q].iterVar;I[d]=A[0].iterVar;let B=new K(a,I),k=new K(g,v.indices),N=new Yt(e,B,k),R=new Mt(N,B,k),O=m(v),M=new K(c,O),F=new Mt(N,A[0].iterVar,M),P=new X(c,O,F),G=new X(g,v.indices,R),W=new tt(s.blockName("arg_acc"),mc(v.ivs,A),[{buffer:a},{buffer:g}],[{buffer:g},{buffer:c}],new ft([P,G])),$=s.extentNode(a.shape[d],a,d),L=gc(W,[w],[$]);return L=v.wrap(L),new ft([S,L])})}f(n,"registerArgReduce"),n("argmax","gt"),n("argmin","lt")}var qO,UO,ux,fx=T(()=>{Ct();vo();kt();Ks();As();qO={i8:-128,i16:-32768,i32:-2147483648,i64:-2147483648,ui8:0,ui16:0,ui32:0,bool:0},UO={i8:127,i16:32767,i32:2147483647,i64:2147483647,ui8:255,ui16:65535,ui32:4294967295,bool:1};f(WO,"argReduceSentinel");ux={sum:f((n,t)=>new D("+",n,t),"sum"),mean:f((n,t)=>new D("+",n,t),"mean"),prod:f((n,t)=>new D("*",n,t),"prod"),max:f((n,t,e)=>new _t("max",[n,t],e),"max"),min:f((n,t,e)=>new _t("min",[n,t],e),"min")};f(lx,"register")});function px(n,t){return Oe(t)?new Ut(n,t,J.I32):n}function hx(){vt("dot",(n,t,e,s)=>{let r=e[0],o=e[1],i=s[0],a=_c(n,t,r,o),c=je(n,"di",Array.from({length:i.shape.length},(_,y)=>y),i.shape,i),l=new X(i,c.indices,new it(0)),u=new tt(n.blockName("matmul_init"),c.ivs,[],[{buffer:i}],l),h=c.wrap(u),p=new D("+",new K(i,a.outIdx),new D("*",new K(r,a.lhsIdx),new K(o,a.rhsIdx))),d=new X(i,a.outIdx,p),m=new tt(n.blockName("matmul"),a.allIvs,[{buffer:r},{buffer:o}],[{buffer:i}],d),g=a.wrapAccBody(m);return new ft([h,g])}),vt("conv",(n,t,e,s)=>{let r=e[0],o=e[1],i=s[0],a=t.getAttr("strides"),c=t.getAttr("padding"),l=t.getAttr("dilation")||a.map(()=>1),u=t.getAttr("groups")||1,h=Hr(t.getAttr("input_layout")),p=Hr(t.getAttr("kernel_layout")),d=a.length,m=r.shape[h.N],g=o.shape[p.O],_=o.shape[p.I],y=i.shape,b=je(n,"ci",Array.from({length:y.length},(xt,le)=>le),y,i),x=new X(i,b.indices,new it(0)),S=new tt(n.blockName("conv_init"),b.ivs,[],[{buffer:i}],x),v=b.wrap(S),w=n.allocVar("cn"),A=n.allocVar("coc"),I=n.allocVar("cic"),B=n.allocVarArray("co",d),k=n.allocVarArray("ck",d),N=[w,A,...B,I,...k],R=n.allocBindArray("cv",N),O=R[0].iterVar,M=R[1].iterVar,F=R.slice(2,2+d),P=R[2+d].iterVar,G=R.slice(3+d),W=new Array(y.length);W[h.N]=O,W[h.C]=M;let $=Object.keys(h).filter(xt=>xt!=="N"&&xt!=="C").sort();for(let xt=0;xt<d;xt++)W[h[$[xt]]]=F[xt].iterVar;let L=new Array(r.shape.length);L[h.N]=O;let q=Math.floor(g/u);u>1?L[h.C]=new D("+",new D("*",new D("//",M,new C(q)),new C(_)),P):L[h.C]=P;let yt=new Array(o.shape.length);yt[p.O]=M,yt[p.I]=P;let lt=null;for(let xt=0;xt<d;xt++){let le=$[xt],pr=le.toUpperCase(),Ae=De("+",De("*",F[xt].iterVar,new C(a[xt])),De("+",De("*",G[xt].iterVar,new C(l[xt])),new C(-c[xt][0])));if(L[h[le]]=Ae,yt[p[pr]]=G[xt].iterVar,c[xt][0]!==0||c[xt][1]!==0){let St=new Yt("ge",Ae,new C(0)),te=new Yt("lt",Ae,new C(r.shape[h[le]])),rn=new D("*",St,te);lt=lt?new D("*",lt,rn):rn}}let pt=new K(r,L),z=new K(o,yt),Y=new K(i,W),Bt=new D("*",pt,z),bt=lt?new Mt(lt,Bt,new it(0)):Bt,_e=new D("+",Y,bt),Ds=new X(i,W,_e),Ss=new tt(n.blockName("conv_acc"),R,[{buffer:r},{buffer:o}],[{buffer:i}],Ds),vs=new Array(d);for(let xt=0;xt<d;xt++){let le=$[xt].toUpperCase();vs[xt]=o.shape[p[le]]}let ke=Ss;for(let xt=d-1;xt>=0;xt--){let le=$[xt].toUpperCase();ke=new ht(k[xt],new C(0),n.extentNode(vs[xt],o,p[le]),V.SERIAL,ke)}ke=new ht(I,new C(0),n.extentNode(_,o,p.I),V.SERIAL,ke);for(let xt=d-1;xt>=0;xt--){let le=h[$[xt]];ke=new ht(B[xt],new C(0),n.extentNode(y[le],i,le),V.SERIAL,ke)}return ke=new ht(A,new C(0),n.extentNode(g,o,p.O),V.SERIAL,ke),ke=new ht(w,new C(0),n.extentNode(m,r,h.N),V.SERIAL,ke),new ft([v,ke])}),vt("gather",(n,t,e,s)=>{let r=e[0],o=e[1],i=s[0],a=new Set(t.getAttr("offset_dims")),c=new Set(t.getAttr("collapsed_slice_dims")),l=t.getAttr("start_index_map"),u=t.getAttr("index_vector_dim"),{loopVars:h,loopBinds:p,indices:d,extentNodes:m}=ne(n,i.shape,i),g=[],_=[];for(let v=0;v<i.shape.length;v++)a.has(v)?_.push(d[v]):g.push(d[v]);let y=new Array(r.shape.length),b=0;for(let v=0;v<r.shape.length;v++)c.has(v)?y[v]=new C(0):y[v]=_[b++];for(let v=0;v<l.length;v++){let w=new Array(o.shape.length),A=0;for(let k=0;k<o.shape.length;k++)k===u?w[k]=new C(v):w[k]=g[A++];let I=px(new K(o,w),o.dtype),B=l[v];y[B]=new D("+",y[B],I)}let x=new X(i,d,new K(r,y)),S=new tt(n.blockName("gather_block"),p,[{buffer:r},{buffer:o}],[{buffer:i}],x);return re(S,h,i.shape,m)}),vt("scatter",(n,t,e,s)=>{let r=e[0],o=e[1],i=e[2],a=s[0],c=new Set(t.getAttr("inserted_window_dims")),l=t.getAttr("scatter_dims_to_operand_dims"),u=t.getAttr("index_vector_dim"),h=new Set(t.getAttr("update_window_dims")),p=ne(n,r.shape,r),d=new X(a,p.indices,new K(r,p.indices)),m=new tt(n.blockName("scatter_copy"),p.loopBinds,[{buffer:r}],[{buffer:a}],d),g=re(m,p.loopVars,r.shape,p.extentNodes),{loopVars:_,loopBinds:y,indices:b,extentNodes:x}=ne(n,i.shape,i),S=[],v=[];for(let M=0;M<i.shape.length;M++)h.has(M)?v.push(b[M]):S.push(b[M]);let w=new Array(r.shape.length),A=0;for(let M=0;M<r.shape.length;M++)c.has(M)?w[M]=new C(0):w[M]=v[A++];for(let M=0;M<l.length;M++){let F=new Array(o.shape.length),P=0;for(let $=0;$<o.shape.length;$++)$===u?F[$]=new C(M):F[$]=S[P++];let G=px(new K(o,F),o.dtype),W=l[M];w[W]=new D("+",w[W],G)}let I=new K(i,b),B=new K(a,w),k=new D("+",B,I),N=new X(a,w,k),R=new tt(n.blockName("scatter_update"),y,[{buffer:i},{buffer:o}],[{buffer:a}],N),O=re(R,_,i.shape,x);return new ft([g,O])}),vt("fused_dot_epilogue",(n,t,e,s)=>{let r=t.getAttr("num_dot_operands")||2,o=e[0],i=e[1],a=e.slice(r),c=s[0],l=t.getAttr("epilogue_tags")||[],u=_c(n,t,o,i),h=je(n,"ei",Array.from({length:c.shape.length},(N,R)=>R),c.shape,c),p=new X(c,h.indices,new it(0)),d=new tt(n.blockName("matmul_init"),h.ivs,[],[{buffer:c}],p),m=h.wrap(d),g=new D("+",new K(c,u.outIdx),new D("*",new K(o,u.lhsIdx),new K(i,u.rhsIdx))),_=new X(c,u.outIdx,g),y=new tt(n.blockName("matmul_acc"),u.allIvs,[{buffer:o},{buffer:i}],[{buffer:c}],_),b=u.wrapAccBody(y);if(l.length===0)return new ft([m,b]);let x=je(n,"ep",Array.from({length:c.shape.length},(N,R)=>R),c.shape,c),S=x.indices,v=new K(c,S),w={v:0};for(let N of l){let R=Hs.get(N);R&&(v=R(v,a,w,c,S))}let A=Ii([c,...a]),I=new X(c,S,v),B=new tt(n.blockName("epilogue"),x.ivs,A,[{buffer:c}],I),k=x.wrap(B);return new ft([m,b,k])}),vt("cublas_gemm",Kr("dot"))}var Hs,dx=T(()=>{Ct();kt();Ks();f(px,"asIndexValue");Hs=new Map;Hs.set("bias",(n,t,e,s,r)=>{let o=t[e.v++];return new D("+",n,new K(o,_r(o,s,r)))});Hs.set("residual_add",(n,t,e,s,r)=>{let o=t[e.v++];return new D("+",n,new K(o,_r(o,s,r)))});Hs.set("scale",(n,t,e,s,r)=>{let o=t[e.v++];return new D("*",n,new K(o,_r(o,s,r)))});Hs.set("relu",(n,t,e,s)=>new _t("max",[n,new it(0)],s.dtype));Hs.set("clamp",(n,t,e,s,r)=>{let o=t[e.v++],i=t[e.v++];return new _t("min",[new _t("max",[n,new K(o,_r(o,s,r))],s.dtype),new K(i,_r(i,s,r))],s.dtype)});Hs.set("neg",n=>new D("-",n));Hs.set("exp",(n,t,e,s)=>new _t("exp",[n],s.dtype));Hs.set("tanh",(n,t,e,s)=>new _t("tanh",[n],s.dtype));Hs.set("sqrt",(n,t,e,s)=>new _t("sqrt",[n],s.dtype));Hs.set("abs",(n,t,e,s)=>new _t("abs",[n],s.dtype));Hs.set("log",(n,t,e,s)=>new _t("log",[n],s.dtype));Hs.set("activation",n=>n);f(hx,"register")});function Di(n,t,e){let{loopVars:s,loopBinds:r,indices:o,extentNodes:i}=ne(n,e.shape,e),a=new X(e,o,new K(t,o)),c=new tt(n.blockName("cf_copy"),r,[{buffer:t}],[{buffer:e}],a);return re(c,s,e.shape,i)}function jO(n,t,e,s){let{loopVars:r,loopBinds:o,indices:i,extentNodes:a}=ne(n,e.shape,e),c=new X(e,i,new K(t,[s,...i])),l=new tt(n.blockName("scan_in"),o,[{buffer:t}],[{buffer:e}],c);return re(l,r,e.shape,a)}function KO(n,t,e,s){let{loopVars:r,loopBinds:o,indices:i,extentNodes:a}=ne(n,t.shape,t),c=new X(e,[s,...i],new K(t,i)),l=new tt(n.blockName("scan_out"),o,[{buffer:t}],[{buffer:e}],c);return re(l,r,t.shape,a)}function yc(n,t,e){let s=t.entryBlock,r=new Map;for(let i=0;i<s.arguments.length;i++)r.set(s.arguments[i],e[i]);let o=[];for(let i of s.ops()){if(i.opName==="yield"){let p=new Array(i.numOperands);for(let d=0;d<i.numOperands;d++)p[d]=r.get(i.getOperand(d))||n.getOrAllocBuffer(i.getOperand(d));return{stmts:o,yieldBuffers:p}}if(Bn.has(i.opName)){o.push(Mi(n,i));continue}let a=new Array(i.numOperands);for(let p=0;p<i.numOperands;p++)a[p]=r.get(i.getOperand(p))||i.getOperand(p);let c=new Array(a.length);for(let p=0;p<a.length;p++)c[p]=a[p]instanceof Vt?a[p]:n.getOrAllocBuffer(a[p]);let l=new Array(i.numResults);for(let p=0;p<i.numResults;p++){let d={type:i.getResult(p).type};l[p]=n.getOrAllocBuffer(d),r.set(i.getResult(p),l[p])}let u=Kr(i.opName);if(!u)throw new Error(`No lowering rule for op '${i.opName}' inside region`);let h=u(n,i,c,l);h&&o.push(h)}return{stmts:o,yieldBuffers:[]}}function mx(){vt("if",(n,t,e,s)=>{let r=e[0],o=new K(r,[]),i=t.regions[0],a=t.regions[1],c=new Array(t.numResults);for(let d=0;d<t.numResults;d++)c[d]=s[d]||n.getOrAllocBuffer(t.getResult(d)),n.bufferMap.set(t.getResult(d),c[d]);let l=yc(n,i,[]),u=l.stmts.slice();for(let d=0;d<c.length&&d<l.yieldBuffers.length;d++){let m=l.yieldBuffers[d];m&&m!==c[d]&&u.push(Di(n,m,c[d]))}let h=u.length===1?u[0]:new ft(u),p=null;if(a&&a.entryBlock){let d=yc(n,a,[]),m=d.stmts.slice();for(let g=0;g<c.length&&g<d.yieldBuffers.length;g++){let _=d.yieldBuffers[g];_&&_!==c[g]&&m.push(Di(n,_,c[g]))}m.length>0&&(p=m.length===1?m[0]:new ft(m))}return new Mt(o,h,p)}),vt("while",(n,t,e,s)=>{let r=t.regions[0],o=t.regions[1],i=new Array(e.length),a=[];for(let _=0;_<e.length;_++){let y=s[_]||n.getOrAllocBuffer({type:t.getResult(_).type});i[_]=y,a.push(Di(n,e[_],y))}for(let _=0;_<t.numResults;_++)n.bufferMap.set(t.getResult(_),i[_]);let c=new Vt(`_wcond_${n.varCounter++}`,[],"bool",Ti.GLOBAL),l=yc(n,r,i),u=l.stmts.slice(),h=l.yieldBuffers[0];u.push(new X(c,[],new K(h,[])));let p=u.length===1?u[0]:new ft(u),d=yc(n,o,i),m=d.stmts.slice();for(let _=0;_<i.length&&_<d.yieldBuffers.length;_++){let y=d.yieldBuffers[_];y&&y!==i[_]&&m.push(Di(n,y,i[_]))}let g=m.length===1?m[0]:new ft(m);return new ft([...a,new Ei(c,p,g)])}),vt("scan",(n,t,e,s)=>{let r=t.getAttr("num_xs"),o=t.getAttr("num_carry"),i=e.slice(0,r),a=e.slice(r),c=new Array(o),l=[];for(let b=0;b<o;b++){let x=s[b]||n.getOrAllocBuffer({type:t.getResult(b).type});c[b]=x,l.push(Di(n,a[b],x)),n.bufferMap.set(t.getResult(b),x)}let u=s.length-o,h=new Array(u);for(let b=0;b<u;b++){let x=s[o+b]||n.getOrAllocBuffer({type:t.getResult(o+b).type});h[b]=x,n.bufferMap.set(t.getResult(o+b),x)}let p=n.allocVar("t"),d=n.extentNode(i[0].shape[0],i[0],0),m=[],g=new Array(r);for(let b=0;b<r;b++){let x=n.getOrAllocBuffer({type:{shape:i[b].shape.slice(1),dtype:i[b].dtype}});g[b]=x,m.push(jO(n,i[b],x,p))}let _=yc(n,t.regions[0],[...g,...c]);for(let b of _.stmts)m.push(b);for(let b=0;b<o;b++){let x=_.yieldBuffers[b];x&&x!==c[b]&&m.push(Di(n,x,c[b]))}for(let b=0;b<u;b++)m.push(KO(n,_.yieldBuffers[o+b],h[b],p));m.push(new So);let y=m.length===1?m[0]:new ft(m);return new ft([...l,new ht(p,new C(0),d,V.RECURRENCE,y)])})}var gx=T(()=>{Ct();vo();ig();Ks();f(Di,"copyBuffer");f(jO,"sliceCopyIn");f(KO,"sliceCopyOut");f(yc,"lowerRegionBody");f(mx,"register")});function _x(){vt("layout_transform",(n,t,e,s)=>{let r=e[0],o=s[0],i=t.getAttr("src_layout"),a=t.getAttr("dst_layout"),{loopVars:c,loopBinds:l,indices:u,extentNodes:h}=ne(n,o.shape,o),p=new X(o,u,new K(r,u)),d=new tt(n.blockName("layout_transform_block"),l,[{buffer:r}],[{buffer:o}],p);return re(d,c,o.shape,h)})}var yx=T(()=>{Ct();Ks();f(_x,"register")});function bx(){vt("quantize",(n,t,e,s)=>{let r=t.getAttr("scale"),o=t.getAttr("zero_point"),i=t.getAttr("target_dtype")||"i8",a=i==="ui8",c=8,l=a?0:-(1<<c-1),u=a?(1<<c)-1:(1<<c-1)-1;return Vn(n,t,e,s,(h,p)=>{let d=new D("/",p[0],new it(r)),m=o!==0?new D("+",d,new it(o)):d,g=new _t("round",[m],"f32"),_=new _t("min",[new _t("max",[g,new it(l)],"f32"),new it(u)],"f32");return new Ut(_,"f32",i)})}),vt("dequantize",(n,t,e,s)=>{let r=t.getAttr("scale"),o=t.getAttr("zero_point"),i=e[0].dtype||"i8",a=t.getAttr("target_dtype")||"f32";return Vn(n,t,e,s,(c,l)=>{let u=new Ut(l[0],i,a),h=o!==0?new D("-",u,new it(o)):u;return new D("*",h,new it(r))})}),vt("quantized_dot",(n,t,e,s)=>{let r=e[0],o=e[1],i=s[0],a=_c(n,t,r,o),c=je(n,"qdi",Array.from({length:i.shape.length},(A,I)=>I),i.shape,i),l=new X(i,c.indices,new C(0)),u=new tt(n.blockName("qmatmul_init"),c.ivs,[],[{buffer:i}],l),h=c.wrap(u),p=new Ut(new K(r,a.lhsIdx),r.dtype,"i32"),d=new Ut(new K(o,a.rhsIdx),o.dtype,"i32"),m=t.getAttr("lhs_zero_point")||0,g=t.getAttr("rhs_zero_point")||0,_=m!==0?new D("-",p,new C(m)):p,y=g!==0?new D("-",d,new C(g)):d,b=new D("*",_,y),x=new D("+",new K(i,a.outIdx),b),S=new X(i,a.outIdx,x),v=new tt(n.blockName("qmatmul_acc"),a.allIvs,[{buffer:r},{buffer:o}],[{buffer:i}],S),w=a.wrapAccBody(v);return new ft([h,w])}),vt("quantized_conv",(n,t,e,s)=>{let r=e[0],o=e[1],i=s[0],a=t.getAttr("strides"),c=t.getAttr("padding"),l=t.getAttr("dilation")||a.map(()=>1),u=t.getAttr("groups")||1,h=Hr(t.getAttr("input_layout")),p=Hr(t.getAttr("kernel_layout")),d=a.length,m=o.shape[p.O],g=o.shape[p.I],_=r.shape[h.N],y=i.shape,b=t.getAttr("input_zero_point")||0,x=t.getAttr("kernel_zero_point")||0,S=je(n,"qci",Array.from({length:y.length},(St,te)=>te),y,i),v=new X(i,S.indices,new C(0)),w=new tt(n.blockName("qconv_init"),S.ivs,[],[{buffer:i}],v),A=S.wrap(w),I=n.allocVar("qcn"),B=n.allocVar("qcoc"),k=n.allocVar("qcic"),N=n.allocVarArray("qco",d),R=n.allocVarArray("qck",d),O=[I,B,...N,k,...R],M=n.allocBindArray("qcv",O),F=M[0].iterVar,P=M[1].iterVar,G=M.slice(2,2+d),W=M[2+d].iterVar,$=M.slice(3+d),L=new Array(y.length);L[h.N]=F,L[h.C]=P;let q=Object.keys(h).filter(St=>St!=="N"&&St!=="C").sort();for(let St=0;St<d;St++)L[h[q[St]]]=G[St].iterVar;let yt=new Array(r.shape.length);yt[h.N]=F;let lt=Math.floor(m/u);u>1?yt[h.C]=new D("+",new D("*",new D("//",P,new C(lt)),new C(g)),W):yt[h.C]=W;let pt=new Array(o.shape.length);pt[p.O]=P,pt[p.I]=W;let z=null;for(let St=0;St<d;St++){let te=q[St],rn=te.toUpperCase(),Pn=De("+",De("*",G[St].iterVar,new C(a[St])),De("+",De("*",$[St].iterVar,new C(l[St])),new C(-c[St][0])));if(yt[h[te]]=Pn,pt[p[rn]]=$[St].iterVar,c[St][0]!==0||c[St][1]!==0){let Vr=new Yt("ge",Pn,new C(0)),hr=new Yt("lt",Pn,new C(r.shape[h[te]])),hi=new D("*",Vr,hr);z=z?new D("*",z,hi):hi}}let Y=new Ut(new K(r,yt),r.dtype,"i32"),Bt=new Ut(new K(o,pt),o.dtype,"i32"),bt=b!==0?new D("-",Y,new C(b)):Y,_e=x!==0?new D("-",Bt,new C(x)):Bt,Ds=new D("*",bt,_e),Ss=z?new Mt(z,Ds,new C(0)):Ds,vs=new K(i,L),ke=new D("+",vs,Ss),xt=new X(i,L,ke),le=new tt(n.blockName("qconv_acc"),M,[{buffer:r},{buffer:o}],[{buffer:i}],xt),pr=new Array(d);for(let St=0;St<d;St++){let te=q[St].toUpperCase();pr[St]=o.shape[p[te]]}let Ae=le;for(let St=d-1;St>=0;St--){let te=q[St].toUpperCase();Ae=new ht(R[St],new C(0),n.extentNode(pr[St],o,p[te]),V.SERIAL,Ae)}Ae=new ht(k,new C(0),n.extentNode(g,o,p.I),V.SERIAL,Ae);for(let St=d-1;St>=0;St--){let te=h[q[St]];Ae=new ht(N[St],new C(0),n.extentNode(y[te],i,te),V.SERIAL,Ae)}return Ae=new ht(B,new C(0),n.extentNode(m,o,p.O),V.SERIAL,Ae),Ae=new ht(I,new C(0),n.extentNode(_,r,h.N),V.SERIAL,Ae),new ft([A,Ae])})}var wx=T(()=>{Ct();Ks();f(bx,"register")});function Bo(n){return Ys.has(n)}function HO(){for(let n of Object.keys(Ci))Ys.set(n,(t,e,s)=>hg(t.opName,e,s));Ys.set("compare",(n,t)=>new Yt(n.getAttr("direction")||"eq",t[0],t[1])),Ys.set("select",(n,t)=>new Mt(t[0],t[1],t[2])),Ys.set("clamp",(n,t,e)=>new _t("min",[new _t("max",[t[1],t[0]],e),t[2]],e)),Ys.set("convert",(n,t)=>new Ut(t[0],n.getOperand(0).type.dtype,n.getAttr("target_dtype")||n.getResult(0).type.dtype)),Ys.set("broadcast_in_dim",(n,t)=>t[0]),Ys.set("broadcast",(n,t)=>t[0]),Ys.set("iota",()=>{throw new Error("iota fusion must be handled by the index-aware path in lowerFusion")}),Ys.set("quantize",(n,t)=>{let e=n.getAttr("scale"),s=n.getAttr("zero_point"),r=n.getAttr("target_dtype")||"i8",o=r==="ui8",i=o?0:-128,a=o?255:127,c=new D("/",t[0],new it(e)),l=new D("+",c,new it(s)),u=new _t("round",[l],"f32"),h=new _t("min",[new _t("max",[u,new it(i)],"f32"),new it(a)],"f32");return new Ut(h,"f32",r)}),Ys.set("dequantize",(n,t)=>{let e=n.getAttr("scale"),s=n.getAttr("zero_point"),r=n.getOperand(0).type?.dtype||"i8",o=n.getAttr("target_dtype")||"f32",i=new Ut(t[0],r,o),a=new D("-",i,new it(s));return new D("*",a,new it(e))})}function xx(n,t){let e=t.numOperands,s=t.numResults,r=new Array(e);for(let B=0;B<e;B++)r[B]=n.getOrAllocBuffer(t.getOperand(B));let o=new Array(s);for(let B=0;B<s;B++)o[B]=n.getOrAllocBuffer(t.getResult(B));let i=o[0],{loopVars:a,loopBinds:c,indices:l,extentNodes:u}=ne(n,i.shape,i),h=new Map,p=t.regions[0].entryBlock,d=new Map,m=[...p.ops()];for(let B=m.length-1;B>=0;B--){let k=m[B];if(k.opName==="broadcast_in_dim"||k.opName==="broadcast"){let N=k.getAttr("broadcast_dimensions");N&&N.length>0&&d.set(k.getOperand(0),N);continue}if(!(k.opName==="yield"||Bn.has(k.opName)))for(let N=0;N<k.numResults;N++){let R=d.get(k.getResult(N));if(R)for(let O=0;O<k.numOperands;O++)d.has(k.getOperand(O))||d.set(k.getOperand(O),R)}}let g=new Map,_=p.arguments;for(let B=0;B<_.length;B++){let k=d.get(_[B]);k&&g.set(B,k)}for(let B=0;B<p.arguments.length;B++){let k=g.get(B),N;if(k){let R=r[B];N=new Array(R.shape.length);for(let O=0;O<R.shape.length;O++)N[O]=R.shape[O]===1?new C(0):l[k[O]]}else N=_r(r[B],i,l);h.set(p.arguments[B],new K(r[B],N))}let y=new Map;for(let B of p.ops())for(let k=0;k<B.numOperands;k++){let N=B.getOperand(k);y.set(N,(y.get(N)||0)+1)}let b=new Map,x=0,S=[];function v(B){let k=h.get(B);if(k===void 0)throw new Error(`Fusion lowering: unmapped operand from '${B.definingOp?B.definingOp.opName:"unknown"}'`);if((y.get(B)||0)>1&&!YO.has(k.type)){if(!b.has(B)){let N=k.type==="CompareNode"?"i32":k.type==="CastNode"?k.toDtype:i.dtype,R=n.allocVar(`cse${x++}`,N);b.set(B,R),S.push({variable:R,value:k}),h.set(B,R)}return b.get(B)}return k}f(v,"getExpr");let w=[];for(let B of p.ops()){if(B.opName==="yield"){for(let R=0;R<B.numOperands;R++)w.push(new X(o[R],l,v(B.getOperand(R))));break}if(Bn.has(B.opName)){let R=B.getAttr("value");h.set(B.getResult(0),new it(typeof R=="number"?R:0));continue}if(B.opName==="iota"){let R=B.getAttr("iota_dimension")??B.getAttr("dimension")??0;h.set(B.getResult(0),l[R]);continue}let k=Ys.get(B.opName);if(!k)throw new Error(`Fusion lowering: unsupported op '${B.opName}' inside fusion body`);let N=new Array(B.numOperands);for(let R=0;R<B.numOperands;R++)N[R]=v(B.getOperand(R));h.set(B.getResult(0),k(B,N,i.dtype))}let A=w.length===1?w[0]:new ft(w);for(let B=S.length-1;B>=0;B--)A=new Os(S[B].variable,S[B].value,A);let I=new tt(n.blockName("fusion_block"),c,Ii(r),Ii(o),A);return re(I,a,i.shape,u)}function JO(n,t){if(n.length!==t.length)return!1;for(let e=0;e<n.length;e++)if(n[e]!==t[e])return!1;return!0}function Sx(n){let t=n.regions[0];if(!t)return!1;for(let e of t.entryBlock.ops())if(e.opName!=="yield"){if(Bn.has(e.opName)){if(typeof e.getAttr("value")=="number")continue;return!1}if(!Ys.has(e.opName))return!1}if(n.numResults>1){let e=n.getResult(0).type.shape;for(let s=1;s<n.numResults;s++)if(!JO(n.getResult(s).type.shape,e))return!1}return!0}function vx(n,t,e){let s=t.regions[0].entryBlock,r=new Map;for(let i=0;i<s.arguments.length;i++)r.set(s.arguments[i],t.getOperand(i));let o=new Map;for(let i of s.ops())if(i.opName==="yield"){for(let a=0;a<i.numOperands;a++)o.set(i.getOperand(a),t.getResult(a));break}for(let i of s.ops()){if(i.opName==="yield")continue;let a=new Array(i.numOperands);for(let p=0;p<i.numOperands;p++)a[p]=r.get(i.getOperand(p))||i.getOperand(p);let c=new Array(a.length);for(let p=0;p<a.length;p++)c[p]=n.getOrAllocBuffer(a[p]);let l=new Array(i.numResults);for(let p=0;p<i.numResults;p++){let d=i.getResult(p),m=o.get(d);if(m){let g=n.getOrAllocBuffer(m);l[p]=g,r.set(d,m)}else{let g={type:d.type};l[p]=n.getOrAllocBuffer(g),r.set(d,g)}}if(Bn.has(i.opName)){e.push(Mi(n,i));continue}let u=Kr(i.opName);if(!u)throw new Error(`Fusion lowering: no lowering rule for op '${i.opName}' inside fusion body`);let h=u(n,i,c,l);h&&e.push(h)}}function Bx(){HO()}var Ys,YO,kx=T(()=>{Ct();Ks();Lf();Ys=new Map;f(Bo,"canInlineFuse");f(HO,"initBuiltinFusionBuilders");YO=new Set(["BufferLoadNode","VariableNode","IntImmNode","FloatImmNode"]);f(xx,"lowerFusion");f(JO,"shapesEqual");f(Sx,"canLowerAsElementwiseFusion");f(vx,"lowerFusionAsIndividualOps");f(Bx,"register")});function Ax(n,t,e,s,r){let o=new D("-",new D("*",n,new C(t)),new C(s)),i=new D("+",o,new C(e)),a=new _t("max",[o,new C(0)],"index"),c=new _t("min",[i,new C(r)],"index"),l=new D("-",c,a);return new _t("max",[l,new C(0)],"index")}function ZO(n,t,e,s,r,o,i,a,c,l,u){let h=Ax(n,e,r,i,c),p=Ax(t,s,o,a,l),d=new D("*",h,p);return new Ut(d,"index",u)}function Rx(){vt("pool2d",(n,t,e,s)=>{let r=e[0],o=s[0],i=t.getAttr("pool_type"),a=t.getAttr("kernel_size"),c=t.getAttr("strides"),l=t.getAttr("padding"),u=t.getAttr("count_include_pad")||!1,h=Hr(t.getAttr("layout")||"NCHW"),p=h.N,d=h.C,m=h.H,g=h.W,_=r.shape[p],y=r.shape[d],b=r.shape[m],x=r.shape[g],S=o.shape[m],v=o.shape[g],w=a[0],A=a[1],I=c[0],B=c[1],k=l[0][0],N=l[1][0],R=i==="max",O=R?new it(-1/0):new it(0),M=je(n,"pi",[0,1,2,3],o.shape,o),F=new X(o,M.indices,O),P=new tt(n.blockName("pool_init"),M.ivs,[],[{buffer:o}],F),G=M.wrap(P),W=n.allocVar("pn"),$=n.allocVar("pc"),L=n.allocVar("poh"),q=n.allocVar("pow"),yt=n.allocVar("pkh"),lt=n.allocVar("pkw"),pt=[W,$,L,q,yt,lt],z=n.allocBindArray("pv",pt),Y=z[0].iterVar,Bt=z[1].iterVar,bt=z[2].iterVar,_e=z[3].iterVar,Ds=z[4].iterVar,Ss=z[5].iterVar,vs=new D("+",new D("*",bt,new C(I)),new D("-",Ds,new C(k))),ke=new D("+",new D("*",_e,new C(B)),new D("-",Ss,new C(N))),xt=new Yt("ge",vs,new C(0)),le=new Yt("lt",vs,new C(b)),pr=new Yt("ge",ke,new C(0)),Ae=new Yt("lt",ke,new C(x)),St=new D("*",new D("*",xt,le),new D("*",pr,Ae)),te=new Array(4);te[p]=Y,te[d]=Bt,te[m]=bt,te[g]=_e;let rn=new Array(4);rn[p]=Y,rn[d]=Bt,rn[m]=vs,rn[g]=ke;let Pn=new K(r,rn),Vr=new K(o,te),hr;if(R){let on=new Mt(St,Pn,new it(-1/0));hr=new _t("max",[Vr,on],o.dtype)}else{let on=new Mt(St,Pn,new it(0));hr=new D("+",Vr,on)}let hi=new X(o,te,hr),ss=new tt(n.blockName("pool_acc"),z,[{buffer:r}],[{buffer:o}],hi);ss=new ht(lt,new C(0),new C(A),V.SERIAL,ss),ss=new ht(yt,new C(0),new C(w),V.SERIAL,ss),ss=new ht(q,new C(0),new C(v),V.SERIAL,ss),ss=new ht(L,new C(0),new C(S),V.SERIAL,ss),ss=new ht($,new C(0),new C(y),V.SERIAL,ss),ss=new ht(W,new C(0),new C(_),V.SERIAL,ss);let mo=[G,ss];if(!R){let on=je(n,"pd",[0,1,2,3],o.shape,o),Om=on.indices[m],ql=on.indices[g],Nm=u?new it(w*A):ZO(Om,ql,I,B,w,A,k,N,b,x,o.dtype),Ul=new K(o,on.indices),Qa=u?new D("*",Ul,new it(1/(w*A))):new D("/",Ul,Nm),tc=new X(o,on.indices,Qa),Wl=new tt(n.blockName("pool_div"),on.ivs,[{buffer:o}],[{buffer:o}],tc);mo.push(on.wrap(Wl))}return new ft(mo)})}var Ox=T(()=>{Ct();Ks();f(Ax,"spatialCount");f(ZO,"avgPoolDivisorExpr");f(Rx,"register")});function Nx(){vt("resize",(n,t,e,s)=>{let r=e[0],o=s[0],i=t.getAttr("method"),a=r.shape[2],c=r.shape[3],l=o.shape[2],u=o.shape[3],h=je(n,"rz",[0,1,2,3],o.shape,o),[p,d,m,g]=h.indices,_=a/l,y=c/u,b;if(i==="nearest"){let v=new _t("floor",[new D("*",m,new it(_))],"f32"),w=new _t("floor",[new D("*",g,new it(y))],"f32"),A=new Ut(new _t("min",[new _t("max",[v,new it(0)],"f32"),new it(a-1)],"f32"),"f32","i32"),I=new Ut(new _t("min",[new _t("max",[w,new it(0)],"f32"),new it(c-1)],"f32"),"f32","i32");b=new K(r,[p,d,A,I])}else{let v=new D("*",m,new it(_)),w=new D("*",g,new it(y)),A=new _t("floor",[v],"f32"),I=new _t("floor",[w],"f32"),B=new _t("min",[new D("+",A,new it(1)),new it(a-1)],"f32"),k=new _t("min",[new D("+",I,new it(1)),new it(c-1)],"f32"),N=new D("-",v,A),R=new D("-",w,I),O=new _t("max",[A,new it(0)],"f32"),M=new _t("max",[I,new it(0)],"f32"),F=new Ut(O,"f32","i32"),P=new Ut(M,"f32","i32"),G=new Ut(B,"f32","i32"),W=new Ut(k,"f32","i32"),$=new K(r,[p,d,F,P]),L=new K(r,[p,d,F,W]),q=new K(r,[p,d,G,P]),yt=new K(r,[p,d,G,W]),lt=new D("-",new it(1),R),pt=new D("-",new it(1),N),z=new D("+",new D("*",$,lt),new D("*",L,R)),Y=new D("+",new D("*",q,lt),new D("*",yt,R));b=new D("+",new D("*",z,pt),new D("*",Y,N))}let x=new X(o,h.indices,b),S=new tt(n.blockName("resize_block"),h.ivs,[{buffer:r}],[{buffer:o}],x);return h.wrap(S)})}var Ex=T(()=>{Ct();Ks();f(Nx,"register")});function Tx(){vt("scaled_dot_product_attention",(n,t,e,s)=>{if(t.getAttr("causal"))throw new Error("causal flash attention lowering not yet supported");let[r,o,i]=e,a=s[0],c=t.getAttr("scale"),l=r.dtype,u=r.shape[0],h=r.shape[1],p=r.shape[2],d=r.shape[3],m=o.shape[2],g=i.shape[3],_=new Vt(n.blockName("fa_m"),[1],l,"local"),y=new Vt(n.blockName("fa_l"),[1],l,"local"),b=new Vt(n.blockName("fa_s"),[1],l,"local"),x=new Vt(n.blockName("fa_mn"),[1],l,"local"),S=new Vt(n.blockName("fa_p"),[1],l,"local"),v=new Vt(n.blockName("fa_cr"),[1],l,"local"),w=new Vt(n.blockName("fa_o"),[g],l,"local"),A=new se(n.blockName("fa_b"),"int32"),I=new se(n.blockName("fa_h"),"int32"),B=new se(n.blockName("fa_i"),"i32"),k=new se(n.blockName("fa_j"),"int32"),N=new se(n.blockName("fa_d"),"int32"),R=new se(n.blockName("fa_di"),"int32"),O=new se(n.blockName("fa_do"),"int32"),M=new se(n.blockName("fa_dw"),"int32"),F=new tt(n.blockName("fa_initm"),[],[],[{buffer:_}],Js(_,[Pt],new it(-1/0))),P=new tt(n.blockName("fa_initl"),[],[],[{buffer:y}],Js(y,[Pt],new it(0))),G=bc(R,g,new tt(n.blockName("fa_inito"),[],[],[{buffer:w}],Js(w,[R],new it(0)))),W=new tt(n.blockName("fa_sinit"),[],[],[{buffer:b}],Js(b,[Pt],new it(0))),$=bc(N,d,new tt(n.blockName("fa_sred"),[],[{buffer:r},{buffer:o},{buffer:b}],[{buffer:b}],Js(b,[Pt],Zs("+",Le(b,[Pt]),Zs("*",Le(r,[A,I,B,N]),Le(o,[A,I,k,N])))))),L=new ft([W,$]),q=new tt(n.blockName("fa_supd"),[],[{buffer:b},{buffer:_},{buffer:y}],[{buffer:b},{buffer:x},{buffer:S},{buffer:v},{buffer:y}],new ft([Js(b,[Pt],Zs("*",Le(b,[Pt]),new it(c))),Js(x,[Pt],dg("max",Le(_,[Pt]),Le(b,[Pt]))),Js(S,[Pt],dg("exp",Zs("-",Le(b,[Pt]),Le(x,[Pt])))),Js(v,[Pt],dg("exp",Zs("-",Le(_,[Pt]),Le(x,[Pt])))),Js(y,[Pt],Zs("+",Zs("*",Le(y,[Pt]),Le(v,[Pt])),Le(S,[Pt])))])),yt=bc(O,g,new tt(n.blockName("fa_oupd"),[],[{buffer:w},{buffer:v},{buffer:S},{buffer:i}],[{buffer:w}],Js(w,[O],Zs("+",Zs("*",Le(w,[O]),Le(v,[Pt])),Zs("*",Le(S,[Pt]),Le(i,[A,I,k,O])))))),lt=new tt(n.blockName("fa_mupd"),[],[{buffer:x}],[{buffer:_}],Js(_,[Pt],Le(x,[Pt]))),pt=bc(k,m,new ft([L,q,yt,lt])),z=bc(M,g,new tt(n.blockName("fa_finw"),[],[{buffer:w},{buffer:y}],[{buffer:a}],Js(a,[A,I,B,M],Zs("/",Le(w,[M]),Le(y,[Pt]))))),Y=new ft([F,P,G,pt,z]);for(let Ss of[w,v,S,x,b,y,_])Y=new fs(Ss,Ss.scope,Y);let Bt=Math.min(p,256),bt=Math.ceil(p/Bt),_e;if(bt===1)_e=new ht(B,Pt,new C(p),V.THREAD_BINDING,Y,"threadIdx.x");else{let Ss=new se(n.blockName("fa_ii"),"i32"),vs=new se(n.blockName("fa_io"),"i32"),ke=Zs("+",Zs("*",vs,new C(Bt)),Ss),xt=bt*Bt===p?Y:new Mt(new Yt("lt",B,new C(p)),Y),le=new ht(Ss,Pt,new C(Bt),V.THREAD_BINDING,new Os(B,ke,xt),"threadIdx.x");_e=new ht(vs,Pt,new C(bt),V.THREAD_BINDING,le,"blockIdx.z")}let Ds=new ht(I,Pt,new C(h),V.THREAD_BINDING,_e,"blockIdx.y");return new ht(A,Pt,new C(u),V.THREAD_BINDING,Ds,"blockIdx.x")})}var Pt,Le,Js,Zs,dg,bc,Ix=T(()=>{Ct();vo();Ks();Pt=new C(0),Le=f((n,t)=>new K(n,t),"ld"),Js=f((n,t,e)=>new X(n,t,e),"st"),Zs=f((n,t,e)=>new D(n,t,e),"mop"),dg=f((n,...t)=>new _t(n,t),"ext"),bc=f((n,t,e,s=V.SERIAL)=>new ht(n,Pt,new C(t),s,e),"forL");f(Tx,"register")});function Mx(n,t,e=new Set){if(!XO.has(t.opName))return!1;if(t.opName!=="fusion")return!0;let s=t.regions[0];if(!s)return!1;let r=s.entryBlock;for(let o=0;o<t.numOperands;o++){if(t.getOperand(o)!==n)continue;let i=r.arguments[o];for(let a of i.getUsers()){let c=`${i.id}:${a.id}`;if(!e.has(c)&&(e.add(c),!Mx(i,a,e)))return!1}}return!0}function QO(n){let t=[];for(let i of n.ops())t.push(i);let e=new Set(t),s=[],r=new Map,o=f(i=>{if(r.get(i)===2)return;r.set(i,1);let a=[{op:i,i:0}];for(;a.length>0;){let c=a[a.length-1],l=c.op;if(c.i<l.numOperands){let u=l.getOperand(c.i).definingOp;c.i++,u&&e.has(u)&&r.get(u)===void 0&&(r.set(u,1),a.push({op:u,i:0}));continue}r.set(l,2),s.push(l),a.pop()}},"visit");for(let i of t)o(i);return s}function wc(n,t=null){let e=new Df,s=[],r=new Map;for(let m of n.args){let g=e.allocVar("arg");s.push(g),r.set(g,e.getOrAllocBuffer(m))}let o=n.getReturnOp(),i=new Set;for(let m of n.args)i.add(e.getOrAllocBuffer(m));let a=[],c=new Set;for(let m=0;m<o.numOperands;m++){let g=e.allocVar("ret");s.push(g);let _=e.getOrAllocBuffer(o.getOperand(m));if(i.has(_)||c.has(_)){let y=e.allocFreshBuffer(o.getOperand(m));r.set(g,y),a.push({src:_,dst:y})}else r.set(g,_),c.add(_)}let l=new Set;for(let m=0;m<o.numOperands;m++)l.add(o.getOperand(m));let u=[];for(let m of n.ops())Bn.has(m.opName)&&u.push(Mi(e,m));for(let m of QO(n)){if(m.opName==="return"||m.opName==="yield"||Bn.has(m.opName))continue;if(m.opName==="fusion"){Sx(m)?u.push(xx(e,m)):vx(e,m,u);continue}if((m.opName==="broadcast_in_dim"||m.opName==="broadcast")&&!l.has(m.getResult(0))&&m.getOperand(0).getUsers().length===1&&m.getResult(0).getUsers().every(x=>Mx(m.getResult(0),x))){let x=e.getOrAllocBuffer(m.getOperand(0)),S=m.getResult(0).type.shape,v=m.getAttr("broadcast_dimensions"),w;if(v&&v.length>0)w=v;else{let A=S.length-x.shape.length;w=Array.from({length:x.shape.length},(I,B)=>B+A)}x.broadcastDims=w,e.bufferMap.set(m.getResult(0),x);continue}let g=Kr(m.opName,t);if(!g)throw new Error(`No lowering rule defined for op: ${m.opName}`);let _=new Array(m.numOperands);for(let x=0;x<m.numOperands;x++)_[x]=e.getOrAllocBuffer(m.getOperand(x));let y=new Array(m.numResults);for(let x=0;x<m.numResults;x++)y[x]=e.getOrAllocBuffer(m.getResult(x));let b=g(e,m,_,y);b&&u.push(b)}for(let{src:m,dst:g}of a){let{loopVars:_,loopBinds:y,indices:b,extentNodes:x}=ne(e,m.shape,m),S=new K(m,b),v=new X(g,b,S),w=new tt(e.blockName("copy_block"),y,[{buffer:m}],[{buffer:g}],v);u.push(re(w,_,m.shape,x))}let h=[],p=new Set;for(let m of e.shapeParams.values())p.has(m.name)||(p.add(m.name),h.push(m));for(let m of h)s.push(m);let d=new jr(n.name,s,u.length===1?u[0]:new ft(u),r,h,new Map(e.shapeParams));return n._partitionTarget&&(d._partitionTarget=n._partitionTarget),d}var XO,Li=T(()=>{Ct();Ks();Lf();cx();fx();dx();gx();yx();wx();kx();Ox();Ex();Ix();Lf();XO=new Set([...Object.keys(Ci),"compare","select","clamp","convert","copy_to_device","dot","fusion"]);f(Mx,"broadcastViewSafeForUser");ox();ax();lx();hx();mx();_x();bx();Bx();Rx();Nx();Tx();f(QO,"topologicalOps");f(wc,"lowerGraphToPrimFunc")});function Cx(n){let t=new Pf(n);return t.register("dot","blas",{dtypes:["f32","f64"],minElements:64}),t}function Dx(n){let t=new Pf(n);return t.register("dot","cublas",{dtypes:["f32","f64","f16"]}),t.register("conv","cudnn",{dtypes:["f32","f16"]}),t}var mg,Pf,Lx=T(()=>{As();mg=class{static{f(this,"LibraryCall")}constructor(t,e,s,r={}){this.opName=t,this.libraryName=e,this.functionName=s,this.constraints=r}},Pf=class{static{f(this,"LibrarySelector")}constructor(t){this.target=t,this._registry=new Map}register(t,e,s={}){let r=this._registry.get(t);r||(r=[],this._registry.set(t,r)),r.push({libraryName:e,constraints:s})}select(t,e,s){let r=this._registry.get(t);if(!r)return null;for(let o of r){if(!this._matchesConstraints(o.constraints,e,s))continue;let i=ww(t,o.libraryName,s);if(i)return new mg(t,o.libraryName,i,o.constraints)}return null}shouldUseLibrary(t,e,s){return this.select(t,e,s)!==null}_matchesConstraints(t,e,s){if(t.minElements){let r=1;for(let o=0;o<e.length;o++)r*=e[o];if(r<t.minElements)return!1}return!(t.dtypes&&t.dtypes.indexOf(s)<0||t.maxRank&&e.length>t.maxRank)}};f(Cx,"createCPULibrarySelector");f(Dx,"createGPULibrarySelector")});var Px={};Ft(Px,{CPUTarget:()=>Gn,CUDATarget:()=>zf,TargetFeatures:()=>ko,TargetKind:()=>fe,WasmTarget:()=>$f,WebGPUTarget:()=>Ff});var fe,ko,Gn,zf,$f,Ff,qn=T(()=>{fe=Object.freeze({CPU:"cpu",CUDA:"cuda",WEBGPU:"webgpu",WASM:"wasm",ACCELERATOR:"accelerator"}),ko=class{static{f(this,"TargetFeatures")}constructor(t){this.kind=t.kind,this.name=t.name,this.vectorWidth=t.vectorWidth||1,this.numCores=t.numCores||1,this.maxThreadsPerBlock=t.maxThreadsPerBlock||1,this.maxBlockDimX=t.maxBlockDimX||1,this.maxBlockDimY=t.maxBlockDimY||1,this.maxBlockDimZ=t.maxBlockDimZ||1,this.maxGridDimX=t.maxGridDimX||1,this.maxGridDimY=t.maxGridDimY||1,this.maxGridDimZ=t.maxGridDimZ||1,this.sharedMemoryBytes=t.sharedMemoryBytes||0,this.memoryBudgetBytes=t.memoryBudgetBytes||0,this.registersPerThread=t.registersPerThread||0,this.warpSize=t.warpSize||1,this.memoryBandwidthGBs=t.memoryBandwidthGBs||0,this.computeTFLOPs=t.computeTFLOPs||0,this.cacheLineSizeBytes=t.cacheLineSizeBytes||64,this.l1CacheBytes=t.l1CacheBytes||0,this.l2CacheBytes=t.l2CacheBytes||0,this.supportsFloat16=t.supportsFloat16??!1,this.supportsTensorCore=t.supportsTensorCore??!1,this.libraryOps=t.libraryOps||new Set,this.enableEpilogueFusion=t.enableEpilogueFusion??!1,this.preferredConvLayout=t.preferredConvLayout||null,this.preferredBlockFactor=t.preferredBlockFactor||0,this.supportsBlockedLayout=t.supportsBlockedLayout??!1,this.supportsInt8=t.supportsInt8??!1,this.simd=t.simd??!1,this.host=t.host||null,this.attrs=new Map(Object.entries(t.attrs||{}))}getAttr(t,e=null){return this.attrs.has(t)?this.attrs.get(t):e}hasAttr(t){return this.attrs.has(t)}withAttr(t,e){return this.attrs.set(t,e),this}isGPU(){return this.kind===fe.CUDA||this.kind===fe.WEBGPU}isWebGPU(){return this.kind===fe.WEBGPU}isCPU(){return this.kind===fe.CPU}isWasm(){return this.kind===fe.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(t){return this.libraryOps.has(t)}},Gn=f((n={})=>new ko({kind:fe.CPU,name:"cpu_generic",vectorWidth:8,numCores:8,cacheLineSizeBytes:64,l1CacheBytes:32*1024,l2CacheBytes:256*1024,memoryBandwidthGBs:50,computeTFLOPs:.5,supportsBlockedLayout:!0,preferredBlockFactor:8,supportsInt8:!0,...n}),"CPUTarget"),zf=f((n={})=>new ko({kind:fe.CUDA,name:"cuda_generic",vectorWidth:1,numCores:80,maxThreadsPerBlock:1024,maxBlockDimX:1024,maxBlockDimY:1024,maxBlockDimZ:64,maxGridDimX:2147483647,maxGridDimY:65535,maxGridDimZ:65535,sharedMemoryBytes:48*1024,registersPerThread:255,warpSize:32,memoryBandwidthGBs:900,computeTFLOPs:15,supportsFloat16:!0,supportsTensorCore:!1,libraryOps:new Set(["dot","conv"]),enableEpilogueFusion:!0,supportsInt8:!0,...n}),"CUDATarget"),$f=f((n={})=>new ko({kind:fe.WASM,name:"wasm_generic",vectorWidth:4,numCores:1,cacheLineSizeBytes:64,memoryBandwidthGBs:10,computeTFLOPs:.1,supportsInt8:!0,simd:!0,...n}),"WasmTarget"),Ff=f((n={})=>new ko({kind:fe.WEBGPU,name:"webgpu_generic",vectorWidth:1,numCores:32,maxThreadsPerBlock:256,maxBlockDimX:256,maxBlockDimY:256,maxBlockDimZ:64,maxGridDimX:65535,maxGridDimY:65535,maxGridDimZ:65535,sharedMemoryBytes:16384,warpSize:32,memoryBandwidthGBs:400,computeTFLOPs:8,supportsFloat16:!0,...n}),"WebGPUTarget")});function Fx(n){return yr[0]=(n&32767)<<13,Yr[0]*=$x,Yr[0]>=65536&&(yr[0]|=2139095040),yr[0]|=(n&32768)<<16,Yr[0]}function Vx(n){Yr[0]=n;let t=yr[0]&2147483648,e=(yr[0]^t)>>>0,s;if(e>=1199570944)s=e>2139095040?32256:31744;else if(e<947912704)yr[0]=e,Yr[0]+=.5,s=yr[0]-1056964608&65535;else{let r=e>>13&1;e=e+zx>>>0,e=e+r>>>0,s=e>>13&65535}return(s|t>>>16)&65535}function Vf(n){return yr[0]=n<<16>>>0,Yr[0]}function Gf(n){Yr[0]=n;let t=yr[0];if((t>>>23&255)===255)return t>>>16&65535;let e=32767+(t>>>16&1)>>>0;return t+e>>>0>>>16&65535}function qf(n,t){return n==="f16"?Vx(t):n==="bf16"?Gf(t):n==="i64"?typeof t=="bigint"?t:BigInt(Math.trunc(t)):t}function Uf(n,t){return n==="f16"?Fx(t):n==="bf16"?Vf(t):n==="i64"&&typeof t=="bigint"?Number(t):t}var Yr,yr,zx,$x,gg,Wf,Pi=T(()=>{Yr=new Float32Array(1),yr=new Uint32Array(Yr.buffer),zx=(-112<<23>>>0)+4095>>>0,$x=5192296858534828e18;f(Fx,"f16ToF32");f(Vx,"f32ToF16");f(Vf,"bf16ToF32");f(Gf,"f32ToBf16");f(qf,"coerceForStorage");f(Uf,"readFromStorage");gg={F16_ADD_BIAS:zx|0,F16_MAGIC_MUL:$x},Wf=globalThis;Wf.__mlfw_f16_to_f32=Fx;Wf.__mlfw_f32_to_f16=Vx;Wf.__mlfw_bf16_to_f32=Vf;Wf.__mlfw_f32_to_bf16=Gf});var _g,Gx,jf,qx=T(()=>{Ct();As();Pi();_g={f16:"__mlfw_f16_to_f32",bf16:"__mlfw_bf16_to_f32"},Gx={f16:"__mlfw_f32_to_f16",bf16:"__mlfw_f32_to_bf16"},jf=class{static{f(this,"CPUCodegen")}constructor(t){this.target=t,this._indent=0,this._lines=[],this._loopStack=[]}generate(t){this._indent=0,this._lines=[],this._aliases=new Map,this._accTarget=null,this._accVar=null,this._accCounter=0;let e=t.type==="LIRFunc",s=new Set,r=[];for(let[,l]of t.bufferMap)r.push(l.name),s.add(l.name);for(let l of t.shapeParams)r.push(l.name);this._paramBuffers=s,this._readBuffers=new Set;let o,i,a,c;e?(o=t.metadata.usedBuffers,i=t.metadata.allocatedBuffers,a=t.metadata.zeroBuffers,c=t.metadata.constantBuffers):(o=new Map,i=new Set,this._scanTree(t.body,o,i,this._readBuffers),a=this._findZeroOnlyBuffers(t.body,s),c=this._constantBuffers),this._zeroBuffers=a,this._constantBuffers=c,this._localBuffers=new Set,this._primFunc=t,this._emit(`function ${t.name}(${r.join(", ")}) {`),this._indent++;for(let[l,u]of o)if(!a.has(l)&&!c.has(l)&&!s.has(l)&&!i.has(l)){let h=u.numel();if(h>0)this._emit(`const ${l} = new ${uc(u.dtype)}(${h});`);else if(h<0){let p=[];for(let d=0;d<u.shape.length;d++)p.push(typeof u.shape[d]=="number"&&u.shape[d]>=0?String(u.shape[d]):this._resolveShapeParam(u,d));this._emit(`const ${l} = new ${uc(u.dtype)}(${p.join(" * ")});`)}}for(let[,l]of o)s.has(l.name)||this._localBuffers.add(l.name);return this._visitNode(t.body),this._indent--,this._emit("}"),this._cleanupSource(this._lines.join(`
|
|
2
|
-
`))}_emit(t){this._lines.push(" ".repeat(this._indent)+t)}_wrapLoad(t,e){let s=_g[t];return s?`${s}(${e})`:e}_wrapStoreVal(t,e){let s=Gx[t];return s?`${s}(${e})`:t==="i64"?`BigInt(${e})`:e}_zeroLit(t){return t==="i64"?"0n":"0"}_visitNode(t){let e=t;for(;e;)switch(e.type){case"SeqNode":for(let s=0;s<e.stmts.length-1;s++)this._visitNode(e.stmts[s]);e=e.stmts[e.stmts.length-1];continue;case"AllocateNode":{let s=e.buffer,r=s.numel();r>0?this._emit(`const ${s.name} = new ${uc(s.dtype)}(${r});`):r<0&&this._emit(`const ${s.name} = new ${uc(s.dtype)}(${this._dynamicNumel(s)});`),e=e.body;continue}case"LetStmtNode":this._emit(`const ${e.variable.name} = ${this._exprToJS(e.value)};`),e=e.body;continue;case"ForNode":this._visitForNode(e);return;case"BlockNode":this._visitBlockNode(e);return;case"IfThenElseNode":this._visitIfThenElseStmt(e);return;case"BufferStoreNode":this._visitBufferStoreNode(e);return;case"LIRFlatStoreNode":this._visitLIRFlatStore(e);return;case"LIRBindingsNode":this._visitLIRBindings(e);return;case"LIRAccumulatorNode":this._visitLIRAccumulator(e);return;case"WhileNode":this._visitWhileNode(e);return;case"EvaluateNode":return;default:return}}_visitForNode(t){if(this._isRedundantZeroFill(t))return;let e=t.loopVar.name;if(t.extent.type==="IntImmNode"&&t.extent.value===1){this._aliases.set(e,"0"),this._visitNode(t.body);return}let s=this._exprToJS(t.extent);if(t.kind===V.UNROLLED){let o=t.extent.type==="IntImmNode"?t.extent.value:null;if(o&&o<=32&&!this._isZeroFillBody(t.body)){for(let i=0;i<o;i++)this._emit("{ const "+e+" = "+i+";"),this._indent++,this._loopStack.push(e),this._visitNode(t.body),this._loopStack.pop(),this._indent--,this._emit("}");return}}let r=this._detectReductionAcc(t);if(r){let o="_acc_"+(this._accCounter=(this._accCounter||0)+1),i=this._accTarget,a=this._accVar;this._accTarget=r,this._accVar=o,this._emit("let "+o+" = "+this._wrapLoad(r.dtype,r.bufName+"["+r.idxExpr+"]")+";"),this._emit("for (let "+e+" = 0; "+e+" < "+s+"; "+e+"++) {"),this._indent++,this._loopStack.push(e),this._visitNode(t.body),this._loopStack.pop(),this._indent--,this._emit("}"),this._emit(r.bufName+"["+r.idxExpr+"] = "+this._wrapStoreVal(r.dtype,o)+";"),this._accTarget=i,this._accVar=a;return}this._emit("for (let "+e+" = 0; "+e+" < "+s+"; "+e+"++) {"),this._indent++,this._loopStack.push(e),this._visitNode(t.body),this._loopStack.pop(),this._indent--,this._emit("}")}_visitLIRFlatStore(t){if(!(this._zeroBuffers&&this._zeroBuffers.has(t.buffer.name))&&!(this._constantBuffers&&this._constantBuffers.has(t.buffer.name))){if(this._accTarget&&t.buffer.name===this._accTarget.bufName&&this._exprToJS(t.offsetExpr)===this._accTarget.idxExpr){this._emit(this._accVar+" = "+this._exprToJS(t.value)+";");return}this._emit(t.buffer.name+"["+this._exprToJS(t.offsetExpr)+"] = "+this._wrapStoreVal(t.dtype||t.buffer.dtype,this._exprToJS(t.value))+";")}}_visitLIRBindings(t){for(let e of t.bindings){let s=this._exprToJS(e.expr);this._aliases.set(e.name,s)}this._visitNode(t.body)}_visitLIRAccumulator(t){let e=t.localName,s=this._exprToJS(t.initLoad);this._emit("let "+e+" = "+s+";");let r=this._accTarget,o=this._accVar;this._accTarget={bufName:t.flushStore.buffer.name,idxExpr:this._exprToJS(t.flushStore.offsetExpr)},this._accVar=e;let i=t.loopVar.name,a=this._exprToJS(t.extent);this._emit("for (let "+i+" = 0; "+i+" < "+a+"; "+i+"++) {"),this._indent++,this._loopStack.push(i);let c=this._exprToJS(t.body),l=t.op||"+",u;l==="max"?u="Math.max("+e+", "+c+")":l==="min"?u="Math.min("+e+", "+c+")":u="("+e+" "+l+" "+c+")",this._emit(e+" = "+u+";"),this._loopStack.pop(),this._indent--,this._emit("}"),this._emit(t.flushStore.buffer.name+"["+this._accTarget.idxExpr+"] = "+this._wrapStoreVal(t.flushStore.dtype||t.flushStore.buffer.dtype,e)+";"),this._accTarget=r,this._accVar=o}_detectReductionAcc(t){let e=t.body;if(!e||e.type!=="BlockNode")return null;let s=e.body;if(!s||s.type!=="BufferStoreNode")return null;let r=s,o=r.value;if(!o||o.type!=="MathOpNode"||o.op!=="+")return null;let i=null;if(o.a&&o.a.type==="BufferLoadNode"&&o.a.buffer.name===r.buffer.name?i=o.a:o.b&&o.b.type==="BufferLoadNode"&&o.b.buffer.name===r.buffer.name&&(i=o.b),!i)return null;for(let u of e.iterVars)u.iterVar&&u.binding&&this._aliases.set(u.iterVar.name,this._exprToJS(u.binding));let a=this._flatIndex(r.buffer,r.indices),c=this._flatIndex(i.buffer,i.indices);if(a!==c)return null;let l=t.loopVar.name;return a.includes(l)?null:{bufName:r.buffer.name,idxExpr:a,dtype:r.buffer.dtype}}_visitBlockNode(t){for(let e of t.iterVars)if(e.iterVar&&e.binding){let s=this._exprToJS(e.binding);this._aliases.set(e.iterVar.name,s)}if(t.initBody){let e=this._loopStack.length>0?this._loopStack[this._loopStack.length-1]:null;e?(this._emit("if ("+e+" === 0) {"),this._indent++,this._visitNode(t.initBody),this._indent--,this._emit("}")):this._visitNode(t.initBody)}this._visitNode(t.body)}_visitIfThenElseStmt(t){this._emit(`if (${this._exprToJS(t.condition)}) {`),this._indent++,this._visitNode(t.thenBody),this._indent--,t.elseBody&&(this._emit("} else {"),this._indent++,this._visitNode(t.elseBody),this._indent--),this._emit("}")}_visitLetStmtNode(t){this._emit(`const ${t.variable.name} = ${this._exprToJS(t.value)};`),this._visitNode(t.body)}_visitWhileNode(t){this._visitNode(t.condBody),this._emit(`while (${t.condVar.name}[0]) {`),this._indent++,this._visitNode(t.loopBody),this._visitNode(t.condBody),this._indent--,this._emit("}")}_visitBufferStoreNode(t){if(!(this._zeroBuffers&&this._zeroBuffers.has(t.buffer.name))&&!(this._constantBuffers&&this._constantBuffers.has(t.buffer.name))){if(this._accTarget&&t.buffer.name===this._accTarget.bufName&&this._flatIndex(t.buffer,t.indices)===this._accTarget.idxExpr){this._emit(this._accVar+" = "+this._exprToJS(t.value)+";");return}this._emit(t.buffer.name+"["+this._flatIndex(t.buffer,t.indices)+"] = "+this._wrapStoreVal(t.buffer.dtype,this._exprToJS(t.value))+";")}}_exprToJS(t){if(!t)return"0";let e=[],s=[{node:t,phase:0}];for(;s.length>0;){let r=s[s.length-1],o=r.node;if(!o){s.pop(),e.push("0");continue}switch(o.type){case"IntImmNode":s.pop(),e.push(String(o.value));continue;case"FloatImmNode":s.pop(),e.push(String(o.value));continue;case"VariableNode":s.pop(),e.push(this._aliases.get(o.name)||o.name);continue;case"BufferLoadNode":{if(s.pop(),this._zeroBuffers&&this._zeroBuffers.has(o.buffer.name))e.push(this._zeroLit(o.buffer.dtype));else if(this._constantBuffers&&this._constantBuffers.has(o.buffer.name)){let i=this._constantBuffers.get(o.buffer.name);e.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?e.push(this._accVar):e.push(this._wrapLoad(o.buffer.dtype,o.buffer.name+"["+this._flatIndex(o.buffer,o.indices)+"]"));continue}case"LIRFlatLoadNode":{if(s.pop(),this._zeroBuffers&&this._zeroBuffers.has(o.buffer.name))e.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));e.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?e.push(this._accVar):e.push(this._wrapLoad(o.dtype||o.buffer.dtype,o.buffer.name+"["+this._exprToJS(o.offsetExpr)+"]"));continue}case"MathOpNode":if(r.phase===0)r.phase=1,s.push({node:o.a,phase:0});else if(r.phase===1&&o.b)r.phase=2,s.push({node:o.b,phase:0});else if(s.pop(),!o.b)e.push(`(${o.op}${e.pop()})`);else{let i=e.pop(),a=e.pop();(o.op==="+"||o.op==="-")&&i==="0"?e.push(a):o.op==="+"&&a==="0"?e.push(i):o.op==="*"&&(a==="0"||i==="0")?e.push("0"):o.op==="*"&&i==="1"?e.push(a):o.op==="*"&&a==="1"?e.push(i):o.op==="%"?e.push(`((${a} % ${i} + ${i}) % ${i})`):o.op==="//"?e.push(`((${a} / ${i}) | 0)`):e.push(`(${a} ${o.op} ${i})`)}continue;case"CompareNode":if(r.phase===0)r.phase=1,s.push({node:o.a,phase:0});else if(r.phase===1)r.phase=2,s.push({node:o.b,phase:0});else{s.pop();let i=e.pop(),a=e.pop();e.push(`(${a} ${o.toJS()} ${i})`)}continue;case"IfThenElseNode":if(r.phase===0)r.phase=1,s.push({node:o.condition,phase:0});else if(r.phase===1)r.phase=2,s.push({node:o.thenBody,phase:0});else if(r.phase===2)r.phase=3,s.push({node:o.elseBody,phase:0});else{s.pop();let i=e.pop(),a=e.pop(),c=e.pop();e.push(`(${c} ? ${a} : ${i})`)}continue;case"CastNode":if(r.phase===0)r.phase=1,s.push({node:o.expr,phase:0});else{s.pop();let i=e.pop();o.toDtype==="bool"?e.push(`(${i} ? 1 : 0)`):Bs(o.toDtype)?e.push(`(${i} | 0)`):_g[o.toDtype]?e.push(`${_g[o.toDtype]}(${Gx[o.toDtype]}(${i}))`):e.push(`(+${i})`)}continue;case"CallExternNode":if(r.phase<o.args.length){let i=r.phase;r.phase++,s.push({node:o.args[i],phase:0})}else{s.pop();let i=[];for(let c=0;c<o.args.length;c++)i.unshift(e.pop());let a=i.join(", ");if(mw(o.externName))e.push(`Math.${o.externName}(${a})`);else if(o.externName==="rsqrt")e.push(`(1.0 / Math.sqrt(${a}))`);else if(o.externName==="fmod")e.push(`((${i[0]} % ${i[1]} + ${i[1]}) % ${i[1]})`);else if(o.externName==="exp2")e.push(`Math.pow(2, ${a})`);else if(o.externName==="erf")e.push(`((x_erf => { const t = 1.0 / (1.0 + 0.3275911 * Math.abs(x_erf)); const p = t * (0.254829592 + t * (-0.284496736 + t * (1.421413741 + t * (-1.453152027 + t * 1.061405429)))); return (x_erf >= 0 ? 1 : -1) * (1.0 - p * Math.exp(-x_erf * x_erf)); })(${a}))`);else if(o.externName==="log10")e.push(`(Math.log(${a}) * ${1/Math.LN10})`);else throw new Error(`CPU codegen: unsupported extern function "${o.externName}"`)}continue;default:s.pop(),e.push("0");continue}}return e.length>0?e[0]:"0"}_dynamicNumel(t){let e=[];for(let s=0;s<t.shape.length;s++){let r=t.shape[s];typeof r=="number"&&r>=0?e.push(String(r)):e.push(this._resolveShapeParam(t,s))}return e.length===0?"1":e.join(" * ")}_flatIndex(t,e){if(e.length===0)return"0";if(e.length===1)return this._exprToJS(e[0]);let s=[];for(let r=0;r<e.length;r++){let o=this._exprToJS(e[r]);if(o==="0")continue;let i=t.strides[r];i===1?s.push(o):typeof i=="number"&&i>=0?s.push(`${o} * ${i}`):s.push(`${o} * ${this._computeDynamicStride(t,r)}`)}return s.length===0?"0":s.join(" + ")}_computeDynamicStride(t,e){let s=[];for(let r=e+1;r<t.shape.length;r++){let o=t.shape[r];typeof o=="number"&&o>=0?s.push(String(o)):s.push(this._resolveShapeParam(t,r))}return s.length===0?"1":s.join(" * ")}_resolveShapeParam(t,e){if(this._primFunc&&this._primFunc.shapeParamMap){let s=`${t.name}:${e}`,r=this._primFunc.shapeParamMap.get(s);if(r)return r.name}return"1"}_cleanupSource(t){let e=t.split(`
|
|
3
|
-
`),s=new Array(e.length).fill(null),r=new Map;for(let i=0;i<e.length;i++){let a=e[i].match(/^\s*const (\w+) = new \w+Array\(\d+\);\s*$/);a&&(s[i]=a[1]);let c=e[i].match(/[A-Za-z_]\w*/g);if(c)for(let l of c)r.set(l,(r.get(l)||0)+1)}let o=[];for(let i=0;i<e.length;i++){if(s[i]!==null&&r.get(s[i])===1)continue;let a=e[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(t){let e=t.body;for(;e;){if(e.type==="ForNode"){if(e.extent.type==="IntImmNode"&&e.extent.value===1){e=e.body;continue}return this._isRedundantZeroFill(e)}if(e.type==="BlockNode"){e=e.body;continue}if(e.type==="BufferStoreNode"||e.type==="LIRFlatStoreNode"){let s=e.value;if(!(s.type==="FloatImmNode"&&s.value===0||s.type==="IntImmNode"&&s.value===0)||!e.buffer)return!1;let o=e.buffer.name,i=this._zeroBuffers&&this._zeroBuffers.has(o),a=this._readBuffers&&this._readBuffers.has(o);return i||!a}return!1}return!1}_isZeroFillBody(t){let e=t;for(;e;){if(e.type==="ForNode"){e=e.body;continue}if(e.type==="BlockNode"){e=e.body;continue}if(e.type==="BufferStoreNode"||e.type==="LIRFlatStoreNode"){let s=e.value;return s.type==="FloatImmNode"&&s.value===0||s.type==="IntImmNode"&&s.value===0}return!1}return!1}_scanTree(t,e,s,r){let o=[t];for(;o.length>0;){let i=o.pop();if(!(!i||typeof i!="object")){switch(i.type){case"BufferStoreNode":case"BufferLoadNode":i.buffer&&e.set(i.buffer.name,i.buffer),i.type==="BufferLoadNode"&&i.buffer&&r&&r.add(i.buffer.name);break;case"AllocateNode":i.buffer&&s.add(i.buffer.name);break}if(i.body&&o.push(i.body),i.value&&typeof i.value=="object"&&i.value.type&&o.push(i.value),i.stmts)for(let a of i.stmts)o.push(a);if(i.thenBody&&o.push(i.thenBody),i.elseBody&&o.push(i.elseBody),i.initBody&&o.push(i.initBody),i.condition&&typeof i.condition=="object"&&i.condition.type&&o.push(i.condition),i.a&&typeof i.a=="object"&&i.a.type&&o.push(i.a),i.b&&typeof i.b=="object"&&i.b.type&&o.push(i.b),i.expr&&typeof i.expr=="object"&&i.expr.type&&o.push(i.expr),i.args)for(let a of i.args)typeof a=="object"&&a!==null&&a.type&&o.push(a);if(i.indices)for(let a of i.indices)typeof a=="object"&&a!==null&&a.type&&o.push(a);if(i.reads)for(let a of i.reads)a.buffer&&e.set(a.buffer.name,a.buffer);if(i.writes)for(let a of i.writes)a.buffer&&e.set(a.buffer.name,a.buffer);if(i.iterVars)for(let a of i.iterVars)a.binding&&typeof a.binding=="object"&&a.binding.type&&o.push(a.binding)}}}_findZeroOnlyBuffers(t,e){let s=new Map,r=[t];for(;r.length>0;){let i=r.pop();if(!(!i||typeof i!="object")){if(i.type==="BufferStoreNode"){let a=i.buffer.name;e.has(a)||(s.has(a)||s.set(a,[]),s.get(a).push(i.value))}if(i.body&&r.push(i.body),i.stmts)for(let a of i.stmts)r.push(a);i.thenBody&&r.push(i.thenBody),i.elseBody&&r.push(i.elseBody),i.initBody&&r.push(i.initBody),i.condBody&&r.push(i.condBody),i.loopBody&&r.push(i.loopBody)}}let o=new Set;this._constantBuffers=new Map;for(let[i,a]of s){if(a.length===0)continue;if(a.every(u=>u.type==="FloatImmNode"&&u.value===0||u.type==="IntImmNode"&&u.value===0)){o.add(i);continue}let c=a[0];(c.type==="FloatImmNode"||c.type==="IntImmNode")&&a.every(u=>u.type===c.type&&u.value===c.value)&&this._constantBuffers.set(i,String(c.value))}return o}}});var yg,Kf,Ux=T(()=>{Ct();As();yg=class{static{f(this,"CUDAKernel")}constructor(t,e,s,r,o,i,a,c){this.name=t,this.source=e,this.blockDim=s,this.gridDim=r,this.sharedMemBytes=o,this.params=i,this.outputIndices=a,this.scratch=c||[]}},Kf=class{static{f(this,"CUDACodegen")}constructor(t){this.target=t,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}generate(t){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=t,this._storeBuffers=new Set,this._promotedBuffers=new Set,this._promotedBufferDecls=[],this._declaredLocals=new Set,this._needsBarriers=!1,this._globalScratch=[],this._scratchNames=new Set,t.type==="LIRFunc"){for(let[d,m]of t.metadata.threadBindings){this._threadBindings.set(d,m);for(let g of m)g.isDynamic||this._applyBindingDim(d,g.extent)}this._sharedBuffers=t.metadata.sharedBuffers}else this._scanBindings(t.body);this._scanStoreTargets(t.body),this._analyzeSharing(t),this._collectGlobalScratch(t);let s=[],r=[],o=[],i=0;for(let[,d]of t.bufferMap)r.push(d.name),s.push(`${jm(d.dtype)} ${d.name}`),this._defaultDtype=d.dtype,this._storeBuffers.has(d.name)&&o.push(i),i++;for(let d of this._globalScratch)r.push(d.name),s.push(`${jm(d.dtype)} ${d.name}`);for(let d of t.shapeParams)r.push(d.name),s.push(`int ${d.name}`);this._emit(`__global__ void ${t.name}(${s.join(", ")}) {`),this._indent++;for(let d of this._sharedBuffers){let m=d.numel();this._emit(`__shared__ ${an(d.dtype)} ${d.name}[${m>0?m:1}];`)}for(let d of this._promotedBufferDecls)this._emit(`__shared__ ${an(d.dtype)} ${d.name}[${d.size}];`);let a=new Set;for(let[d,m]of this._threadBindings)for(let g of m)a.has(g.varName)||(this._emit(`const int ${g.varName} = ${d};`),a.add(g.varName));this._emitMissingLocalDecls(t),t.gpuWmma?this._emitWmmaBody(t.gpuWmma):t.gpuPipelined?this._emitPipelinedBody(t.gpuPipelined):this._visitNode(t.body),this._indent--,this._emit("}");let c=this.target,l=[Math.min(this._blockDim[0],c.maxBlockDimX),Math.min(this._blockDim[1],c.maxBlockDimY),Math.min(this._blockDim[2],c.maxBlockDimZ)],u=[Math.min(this._gridDim[0],c.maxGridDimX),Math.min(this._gridDim[1],c.maxGridDimY),Math.min(this._gridDim[2],c.maxGridDimZ)],h=l[0]*l[1]*l[2];if(h>c.maxThreadsPerBlock)throw new Error(`[codegen] kernel '${t.name}' block ${l.join("x")} = ${h} threads exceeds maxThreadsPerBlock ${c.maxThreadsPerBlock}`);let p=this._sharedBuffers.reduce((d,m)=>d+Math.max(m.sizeInBytes(),0),0)+this._promotedBufferDecls.reduce((d,m)=>d+Math.max(m.size,0)*cf(m.dtype),0);if(p>c.sharedMemoryBytes)throw new Error(`[codegen] kernel '${t.name}' shared memory ${p} bytes exceeds device limit ${c.sharedMemoryBytes}`);return new yg(t.name,this._lines.join(`
|
|
5
|
-
`),l,u,p,r,o,this._globalScratch)}_scanBindings(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="ForNode"&&s.kind===V.THREAD_BINDING&&s.threadTag){let r=s.extent.type==="IntImmNode"?s.extent.value:0,o=s.extent.type!=="IntImmNode",i={varName:s.loopVar.name,extent:r,isDynamic:o,extentNode:s.extent};this._threadBindings.has(s.threadTag)?this._threadBindings.get(s.threadTag).push(i):this._threadBindings.set(s.threadTag,[i]),o||this._applyBindingDim(s.threadTag,r)}if(s.type==="AllocateNode"&&s.scope==="shared"&&this._sharedBuffers.push(s.buffer),s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody)}}}_applyBindingDim(t,e){let s=t.indexOf(".");if(s<0)return;let r=t.substring(0,s),o=t.charCodeAt(s+1)-120;o<0||o>2||(r==="threadIdx"?this._blockDim[o]=Math.max(this._blockDim[o],e):r==="blockIdx"&&(this._gridDim[o]=Math.max(this._gridDim[o],e)))}_emit(t){this._lines.push(" ".repeat(this._indent)+t)}_visitNode(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s)switch(s.type){case"SeqNode":for(let r=s.stmts.length-1;r>=0;r--)e.push(s.stmts[r]);continue;case"AllocateNode":this._visitAllocateNode(s),e.push(s.body);continue;case"ForNode":this._visitForNode(s);continue;case"BlockNode":this._visitBlockNode(s);continue;case"IfThenElseNode":this._visitIfStmt(s);continue;case"LetStmtNode":this._visitLetStmtNode(s);continue;case"BufferStoreNode":this._visitBufferStoreNode(s);continue;case"SyncThreadsNode":this._emit("__syncthreads();");continue;case"LIRFlatStoreNode":this._visitLIRFlatStore(s);continue;case"LIRBindingsNode":this._visitLIRBindings(s);continue;case"LIRAccumulatorNode":this._visitLIRAccumulator(s);continue;case"WhileNode":this._visitWhileNode(s);continue;case"EvaluateNode":continue;default:continue}}}_matchFullReduction(t){let e=[],s=t;for(;s&&s.type==="ForNode";){if(s.kind!==V.SERIAL)return null;let p=s.extent.type==="IntImmNode"?s.extent.value:0;if(p<=0)return null;e.push({extC:this._exprToC(s.extent),extVal:p,varName:s.loopVar.name}),s=s.body}if(!s||s.type!=="BlockNode")return null;let r=s,o=r.body;if(!o||o.type!=="BufferStoreNode"||r.iterVars&&r.iterVars.length!==e.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(p=>this._exprToC(p)).join(","),c=f(p=>p&&p.type==="BufferLoadNode"&&p.buffer===o.buffer&&p.indices.map(d=>this._exprToC(d)).join(",")===a,"isOutLoad"),l=null;if(c(i.a))l=i.b;else if(c(i.b))l=i.a;else return null;let u=new Set(e.map(p=>p.varName));if(a.split(/[^A-Za-z0-9_]/).some(p=>u.has(p)))return null;let h=e.reduce((p,d)=>p*d.extVal,1);return h<2048?null:{loops:e,block:r,store:o,valExpr:l,total:h}}_emitParallelReduction(t,e){this._blockDim=[256,1,1],this._didParallelReduce=!0;let r=an(e.store.buffer.dtype),o=this._flatIndex(e.store.buffer,e.store.indices);this._emit(`__shared__ ${r} _redsh[256];`),this._emit(`${r} _racc = 0;`),this._emit(`for (int _rf = threadIdx.x; _rf < ${e.total}; _rf += 256) {`),this._indent++,this._emit("int _rem = _rf;");for(let i=e.loops.length-1;i>=0;i--)this._emit(`const int ${e.loops[i].varName} = _rem % ${e.loops[i].extC}; _rem /= ${e.loops[i].extC};`);for(let i of e.block.iterVars)i.iterVar&&i.binding&&this._emit(`const int ${i.iterVar.name} = ${this._exprToC(i.binding)};`);this._emit(`_racc = _racc + ${this._exprToC(e.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) ${e.store.buffer.name}[${o}] = _redsh[0];`)}_visitForNode(t){if(t.kind===V.THREAD_BINDING){let r=t.extent.type==="IntImmNode"?t.extent.value:0,o=t.threadTag,i=this._getMaxBindingExtent(o);r>0&&i>0&&r<i?(this._emit(`if (${o} < ${r}) {`),this._indent++,this._visitNode(t.body),this._indent--,this._emit("}")):this._visitNode(t.body),this._needsBarriers&&this._emit("__syncthreads();");return}if(this._threadBindings.size===0&&!this._didParallelReduce){let r=this._matchFullReduction(t);if(r){this._emitParallelReduction(t,r);return}}let e=t.loopVar.name,s=this._exprToC(t.extent);(t.kind===V.UNROLLED||t.kind===V.VECTORIZED)&&this._emit("#pragma unroll"),this._emit(`for (int ${e} = 0; ${e} < ${s}; ${e}++) {`),this._indent++,this._visitNode(t.body),this._indent--,this._emit("}")}_visitBlockNode(t){for(let e of t.iterVars)e.iterVar&&e.binding&&this._emit(`const int ${e.iterVar.name} = ${this._exprToC(e.binding)};`);t.initBody&&this._visitNode(t.initBody),this._visitNode(t.body)}_visitAllocateNode(t){if(t.scope!=="shared"){if(this._promotedBuffers.has(t.buffer.name)||this._scratchNames.has(t.buffer.name)||this._declaredLocals.has(t.buffer.name))return;this._declaredLocals.add(t.buffer.name);let e=t.buffer.numel();e>0?this._emit(`${an(t.buffer.dtype)} ${t.buffer.name}[${e}];`):this._emit(`${an(t.buffer.dtype)}* ${t.buffer.name} = (${an(t.buffer.dtype)}*)alloca(${this._dynamicNumel(t.buffer)} * sizeof(${an(t.buffer.dtype)}));`)}}_visitIfStmt(t){this._emit(`if (${this._exprToC(t.condition)}) {`),this._indent++,this._visitNode(t.thenBody),this._indent--,t.elseBody&&(this._emit("} else {"),this._indent++,this._visitNode(t.elseBody),this._indent--),this._emit("}")}_visitLetStmtNode(t){let e=t.variable.dtype||this._defaultDtype;this._emit(`${an(e)} ${t.variable.name} = ${this._exprToC(t.value)};`),this._visitNode(t.body)}_visitWhileNode(t){this._visitNode(t.condBody);let e=Array.isArray(t.condVar.shape)?`${t.condVar.name}[0]`:t.condVar.name;this._emit(`while (${e}) {`),this._indent++,this._visitNode(t.loopBody),this._visitNode(t.condBody),this._indent--,this._emit("}")}_visitBufferStoreNode(t){this._emit(`${t.buffer.name}[${this._flatIndex(t.buffer,t.indices)}] = ${this._exprToC(t.value)};`)}_visitLIRFlatStore(t){this._emit(`${t.buffer.name}[${this._exprToC(t.offsetExpr)}] = ${this._exprToC(t.value)};`)}_visitLIRBindings(t){for(let e of t.bindings)this._emit(`const int ${e.name} = ${this._exprToC(e.expr)};`);this._visitNode(t.body)}_visitLIRAccumulator(t){let e=t.localName,s=t.dtype||this._defaultDtype;this._emit(`${an(s)} ${e} = ${this._exprToC(t.initLoad)};`);let r=t.loopVar.name,o=this._exprToC(t.extent);this._emit(`for (int ${r} = 0; ${r} < ${o}; ${r}++) {`),this._indent++;let i=this._exprToC(t.body),a=t.op||"+",c;a==="max"||a==="min"?Bs(t.dtype)?c=`((${e}) ${a==="max"?">":"<"} (${i}) ? (${e}) : (${i}))`:c=`${a==="max"?"fmaxf":"fminf"}(${e}, ${i})`:c=`(${e} ${a} ${i})`,this._emit(`${e} = ${c};`),this._indent--,this._emit("}"),this._emit(`${t.flushStore.buffer.name}[${this._exprToC(t.flushStore.offsetExpr)}] = ${e};`)}_exprToC(t){if(!t)return"0";switch(t.type){case"IntImmNode":return String(t.value);case"FloatImmNode":return this._emitFloatLiteral(t.value);case"VariableNode":return t.name;case"BufferLoadNode":return`${t.buffer.name}[${this._flatIndex(t.buffer,t.indices)}]`;case"LIRFlatLoadNode":return`${t.buffer.name}[${this._exprToC(t.offsetExpr)}]`;case"MathOpNode":{let e=this._exprToC(t.a);if(!t.b)return`(${t.op}${e})`;let s=this._exprToC(t.b);return t.op==="//"?`(${e} / ${s})`:`(${e} ${t.op} ${s})`}case"CompareNode":return`(${this._exprToC(t.a)} ${t.toC()} ${this._exprToC(t.b)})`;case"IfThenElseNode":return`(${this._exprToC(t.condition)} ? ${this._exprToC(t.thenBody)} : ${this._exprToC(t.elseBody)})`;case"CastNode":return`((${an(t.toDtype)})(${this._exprToC(t.expr)}))`;case"CallExternNode":return this._emitExternCall(t);default:return"0"}}_emitFloatLiteral(t){if(t===1/0)return"INFINITY";if(t===-1/0)return"(-INFINITY)";let e=Km(this._defaultDtype),s=String(t);return`${/[.eEnN]/.test(s)?s:s+".0"}${e}`}_emitExternCall(t){let e=t.args.length,s=new Array(e);for(let a=0;a<e;a++)s[a]=this._exprToC(t.args[a]);let r=s.join(", "),o=t.dtype||this._defaultDtype;if(t.externName==="rsqrt")return`${lf("rsqrt",o)||"rsqrtf"}(${r})`;if(t.externName==="sign"){let a=s[0],c=`0.0${Km(o)}`;return`((${a} > ${c}) - (${a} < ${c}))`}if(t.externName==="min"||t.externName==="max"){if(Bs(o)){let a=t.externName==="min"?"<":">";return`((${s[0]}) ${a} (${s[1]}) ? (${s[0]}) : (${s[1]}))`}return`${lf(t.externName,o)}(${r})`}return`${lf(t.externName,o)}(${r})`}_flatIndex(t,e){if(e.length===0)return"0";if(e.length===1)return this._exprToC(e[0]);let s=new Array(e.length);for(let r=0;r<e.length;r++){let o=this._exprToC(e[r]),i=t.strides[r];i===1?s[r]=o:typeof i=="number"&&i>=0?s[r]=`${o} * ${i}`:s[r]=`${o} * ${this._computeDynamicStride(t,r)}`}return s.join(" + ")}_computeDynamicStride(t,e){let s=[];for(let r=e+1;r<t.shape.length;r++){let o=t.shape[r];typeof o=="number"&&o>=0?s.push(String(o)):s.push(this._resolveShapeParam(t,r))}return s.length===0?"1":s.join(" * ")}_dynamicNumel(t){let e=[];for(let s=0;s<t.shape.length;s++){let r=t.shape[s];typeof r=="number"&&r>=0?e.push(String(r)):e.push(this._resolveShapeParam(t,s))}return e.length===0?"1":e.join(" * ")}_getMaxBindingExtent(t){let e=this._threadBindings.get(t);if(!e)return 0;let s=0;for(let r of e)r.extent>s&&(s=r.extent);return s}_scanStoreTargets(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s){if((s.type==="BufferStoreNode"||s.type==="LIRFlatStoreNode")&&this._storeBuffers.add(s.buffer.name),s.type==="LIRAccumulatorNode"&&s.flushStore&&this._storeBuffers.add(s.flushStore.buffer.name),s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),s.loopBody&&e.push(s.loopBody),s.condBody&&e.push(s.condBody),s.initBody&&e.push(s.initBody)}}}_emitWmmaBody(t){let{M:e,N:s,K:r,a:o,b:i,c:a}=t;this._blockDim=[32,1,1],this._gridDim=[Math.ceil(e/16),Math.ceil(s/16),1],this._emit("const int warpM = blockIdx.x;"),this._emit("const int warpN = blockIdx.y;"),this._emit("fragment<accumulator, 16, 16, 16, float> cf;"),this._emit("fill_fragment(cf, 0.0f);"),this._emit(`for (int kk = 0; kk < ${r}; kk += 16) {`),this._indent++,this._emit("fragment<matrix_a, 16, 16, 16, half, row_major> af;"),this._emit("fragment<matrix_b, 16, 16, 16, half, row_major> bf;"),this._emit(`load_matrix_sync(af, ${o} + warpM * 16 * ${r} + kk, ${r});`),this._emit(`load_matrix_sync(bf, ${i} + kk * ${s} + warpN * 16, ${s});`),this._emit("mma_sync(cf, af, bf, cf);"),this._indent--,this._emit("}"),this._emit(`store_matrix_sync(${a} + warpM * 16 * ${s} + warpN * 16, cf, ${s}, mem_row_major);`)}_emitPipelinedBody(t){let{M:e,N:s,K:r,a:o,b:i,c:a,tile:c=16}=t;this._blockDim=[c,c,1],this._gridDim=[Math.ceil(s/c),Math.ceil(e/c),1];let l=c;this._emit(`__shared__ float As[2][${l}][${l}];`),this._emit(`__shared__ float Bs[2][${l}][${l}];`),this._emit(`const int row = blockIdx.y * ${l} + threadIdx.y;`),this._emit(`const int col = blockIdx.x * ${l} + threadIdx.x;`),this._emit("float acc = 0.0f;"),this._emit(`const int nTiles = ${r} / ${l};`),this._emit(`__pipeline_memcpy_async(&As[0][threadIdx.y][threadIdx.x], &${o}[row * ${r} + threadIdx.x], sizeof(float));`),this._emit(`__pipeline_memcpy_async(&Bs[0][threadIdx.y][threadIdx.x], &${i}[threadIdx.y * ${s} + col], sizeof(float));`),this._emit("__pipeline_commit();"),this._emit("for (int t = 0; t < nTiles; t++) {"),this._indent++,this._emit("const int cur = t & 1, nxt = (t + 1) & 1;"),this._emit("if (t + 1 < nTiles) {"),this._indent++,this._emit(`__pipeline_memcpy_async(&As[nxt][threadIdx.y][threadIdx.x], &${o}[row * ${r} + (t + 1) * ${l} + threadIdx.x], sizeof(float));`),this._emit(`__pipeline_memcpy_async(&Bs[nxt][threadIdx.y][threadIdx.x], &${i}[((t + 1) * ${l} + threadIdx.y) * ${s} + col], sizeof(float));`),this._emit("__pipeline_commit();"),this._indent--,this._emit("}"),this._emit("__pipeline_wait_prior(t + 1 < nTiles ? 1 : 0);"),this._emit("__syncthreads();"),this._emit(`for (int kk = 0; kk < ${l}; kk++) acc += As[cur][threadIdx.y][kk] * Bs[cur][kk][threadIdx.x];`),this._emit("__syncthreads();"),this._indent--,this._emit("}"),this._emit(`${a}[row * ${s} + col] = acc;`)}_analyzeSharing(t){if(t.gpuWmma||t.gpuPipelined){this._needsBarriers=!1;return}if(t.gpuRegisterBlocked){this._needsBarriers=!1;return}let e=!1;for(let[,a]of this._threadBindings){let c=new Set;for(let l of a)l.extent>0&&c.add(l.extent);if(c.size>1){e=!0;break}}if(!e)return;this._needsBarriers=!0;let s=new Set;for(let[,a]of t.bufferMap)s.add(a.name);let r=[t.body];for(;r.length>0;){let a=r.pop();if(a){if(a.type==="AllocateNode"&&a.scope!=="shared"){if(!s.has(a.buffer.name)){let c=a.buffer.numel(),l=c>0?c:this._estimateBufferSize(a.buffer);l>0&&(this._promotedBuffers.add(a.buffer.name),this._promotedBufferDecls.push({name:a.buffer.name,dtype:a.buffer.dtype,size:l}))}r.push(a.body);continue}if(a.body&&r.push(a.body),a.stmts)for(let c of a.stmts)r.push(c);a.thenBody&&r.push(a.thenBody),a.elseBody&&r.push(a.elseBody)}}let o=new Map;this._scanBufferRefs(t.body,o);let i=new Set;this._scanAllocateNodes(t.body,i);for(let[a,c]of o){if(s.has(a)||i.has(a)||this._promotedBuffers.has(a))continue;let l=c.numel(),u=l>0?l:this._estimateBufferSize(c);u>0&&(this._promotedBuffers.add(a),this._promotedBufferDecls.push({name:a,dtype:c.dtype,size:u}))}}_emitMissingLocalDecls(t){let e=new Set;for(let[,o]of t.bufferMap)e.add(o.name);let s=new Set;this._scanAllocateNodes(t.body,s);let r=new Map;this._scanBufferRefs(t.body,r);for(let[o,i]of r){if(e.has(o)||s.has(o)||this._promotedBuffers.has(o)||this._scratchNames.has(o)||this._declaredLocals.has(o))continue;let a=i.numel(),c=a>0?a:this._estimateBufferSize(i);c>0&&(this._declaredLocals.add(o),this._emit(`${an(i.dtype)} ${o}[${c}];`))}}_collectGlobalScratch(t){if(this._threadBindings.size>0)return;let e=32768,s=new Set;for(let[,a]of t.bufferMap)s.add(a.name);let r=f((a,c)=>{if(!c||s.has(a)||this._promotedBuffers.has(a)||this._scratchNames.has(a))return;let l=typeof c.numel=="function"?c.numel():0,u=l>0?l:this._estimateBufferSize(c);u>e&&(this._scratchNames.add(a),this._globalScratch.push({name:a,dtype:c.dtype,size:u}))},"consider"),o=[t.body];for(;o.length>0;){let a=o.pop();if(a){if(a.type==="AllocateNode"&&a.scope!=="shared"&&r(a.buffer.name,a.buffer),a.body&&o.push(a.body),a.stmts)for(let c of a.stmts)o.push(c);a.thenBody&&o.push(a.thenBody),a.elseBody&&o.push(a.elseBody),a.loopBody&&o.push(a.loopBody),a.condBody&&o.push(a.condBody),a.initBody&&o.push(a.initBody)}}let i=new Map;this._scanBufferRefs(t.body,i);for(let[a,c]of i)r(a,c)}_scanAllocateNodes(t,e){let s=[t];for(;s.length>0;){let r=s.pop();if(r){if(r.type==="AllocateNode"){e.add(r.buffer.name),s.push(r.body);continue}if(r.body&&s.push(r.body),r.stmts)for(let o of r.stmts)s.push(o);r.thenBody&&s.push(r.thenBody),r.elseBody&&s.push(r.elseBody),r.loopBody&&s.push(r.loopBody),r.condBody&&s.push(r.condBody),r.initBody&&s.push(r.initBody)}}}_scanBufferRefs(t,e){let s=[t];for(;s.length>0;){let r=s.pop();if(r){if((r.type==="BufferLoadNode"||r.type==="BufferStoreNode"||r.type==="LIRFlatLoadNode"||r.type==="LIRFlatStoreNode")&&r.buffer&&e.set(r.buffer.name,r.buffer),r.type==="LIRAccumulatorNode"&&(r.flushStore&&r.flushStore.buffer&&e.set(r.flushStore.buffer.name,r.flushStore.buffer),r.initLoad&&r.initLoad.buffer&&e.set(r.initLoad.buffer.name,r.initLoad.buffer)),r.value&&s.push(r.value),r.body&&s.push(r.body),r.stmts)for(let o of r.stmts)s.push(o);if(r.thenBody&&s.push(r.thenBody),r.elseBody&&s.push(r.elseBody),r.loopBody&&s.push(r.loopBody),r.condBody&&s.push(r.condBody),r.initBody&&s.push(r.initBody),r.indices)for(let o of r.indices)s.push(o);if(r.a&&s.push(r.a),r.b&&s.push(r.b),r.condition&&s.push(r.condition),r.expr&&s.push(r.expr),r.args)for(let o of r.args)s.push(o);r.offsetExpr&&s.push(r.offsetExpr),r.extent&&s.push(r.extent)}}}_estimateBufferSize(t){let e=1;for(let s of t.shape)typeof s=="number"&&s>0?e*=s:e*=1;return e}_resolveShapeParam(t,e){if(this._primFunc&&this._primFunc.shapeParamMap){let s=`${t.name}:${e}`,r=this._primFunc.shapeParamMap.get(s);if(r)return r.name}return"1"}}});function Jr(n){return eN[n]||"f32"}function Ns(n){if(!n)return"f32";if(n._dtype)return n._dtype;switch(n.type){case"IntImmNode":return"i32";case"FloatImmNode":return"f32";case"LIRFlatLoadNode":return Jr(n.dtype);case"BufferLoadNode":return Jr(n.buffer.dtype);case"CastNode":return Jr(n.toDtype);case"CallExternNode":return Jr(n.dtype||"f32");case"CompareNode":return"i32";case"VariableNode":return Jr(n.dtype||"i32");case"MathOpNode":{let t=Ns(n.a);if(ks(t))return t;if(n.b){let e=Ns(n.b);if(ks(e))return e}return t}case"IfThenElseNode":return Ns(n.thenBody);default:return"f32"}}function br(n){!n||typeof n!="object"||!n.type||(n._dtype=Ns(n))}function Wx(n){return tN.has(n)}var Ao,Hf,xc,Sc,Yf,zi,Jf,tN,eN,Zf=T(()=>{As();Ao=class n{static{f(this,"LIRNode")}constructor(){this.type=this.constructor.name.replace(/^_+/,""),this._parent=null,this._parentKey=null,this._parentIdx=-1}_setChild(t,e,s=-1){(e instanceof n||e&&e._parent!==void 0)&&(e._parent=this,e._parentKey=t,e._parentIdx=s)}_setChildren(t,e){if(e)for(let s=0;s<e.length;s++){let r=e[s];(r instanceof n||r&&r._parent!==void 0)&&(r._parent=this,r._parentKey=t,r._parentIdx=s)}}replaceWith(t){let e=this._parent;return e?(this._parentIdx>=0?e[this._parentKey][this._parentIdx]=t:e[this._parentKey]=t,t&&(t instanceof n||t._parent!==void 0)&&(t._parent=e,t._parentKey=this._parentKey,t._parentIdx=this._parentIdx),this._parent=null,this._parentKey=null,this._parentIdx=-1,!0):!1}},Hf=class extends Ao{static{f(this,"LIRFunc")}constructor(t,e,s,r,o,i,a){super(),this.name=t,this.params=e,this.body=s,this.bufferMap=r,this.shapeParams=o,this.shapeParamMap=i,this.metadata=a,this._setChild("body",s)}},xc=class extends Ao{static{f(this,"LIRFlatLoadNode")}constructor(t,e,s){super(),this.buffer=t,this.offsetExpr=e,this.dtype=s,this._setChild("offsetExpr",e)}},Sc=class extends Ao{static{f(this,"LIRFlatStoreNode")}constructor(t,e,s,r){super(),this.buffer=t,this.offsetExpr=e,this.value=s,this.dtype=r,this._setChild("offsetExpr",e),this._setChild("value",s)}},Yf=class extends Ao{static{f(this,"LIRAccumulatorNode")}constructor(t){super(),this.localName=t.localName,this.dtype=t.dtype,this.op=t.op||"+",this.initLoad=t.initLoad,this.loopVar=t.loopVar,this.extent=t.extent,this.loopKind=t.loopKind,this.body=t.body,this.flushStore=t.flushStore,this.initBody=t.initBody||null,this._setChild("initLoad",t.initLoad),this._setChild("body",t.body),this._setChild("flushStore",t.flushStore),this._setChild("initBody",t.initBody||null)}},zi=class extends Ao{static{f(this,"LIRBindingsNode")}constructor(t,e){super(),this.bindings=t,this.body=e,this._setChild("body",e)}},Jf=class{static{f(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}},tN=new Set(["sqrt","abs","ceil","floor","min","max"]),eN={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"};f(Jr,"normalizeDtype");f(Ns,"inferDtype");f(br,"annotateDtype");f(Wx,"isWasmNativeOp")});var $i,Xf,jx=T(()=>{Ct();As();Zf();Pi();$i=new Set(["f16","bf16"]),Xf=class{static{f(this,"WasmCodegen")}constructor(t){this.target=t,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(t){this._lines=[],this._indent=0,this._locals.clear(),this._localCounter=0,this._imports.clear(),this._bufferOffsets.clear(),this._totalMemBytes=0,this._primFunc=t,this._wasmAcc=null,this._waccCounter=0,this._hasParallel=!1,this._parallelExtent=0,this._vectorMode=null,this._vecTmpCounter=0;let e=t.type==="LIRFunc";if(e){for(let[u,h]of t.metadata.memoryLayout.bufferOffsets)this._bufferOffsets.set(u,h);this._totalMemBytes=t.metadata.memoryLayout.totalBytes;for(let[u,h]of t.metadata.externCalls){let p=this._mathImportSig(u,h.argCount);this._imports.set(u,p)}for(let[u,h]of t.metadata.locals)this._ensureLocal(u,bn(h))}else this._layoutBuffers(t),this._scanMathImports(t.body);this._scanParallel(t.body);let s=[];for(let[,u]of t.bufferMap)s.push(u.name);for(let u of t.shapeParams)s.push(u.name);this._emit("(module"),this._indent++;let r=Math.max(1,Math.ceil(this._totalMemBytes/65536)),o=Math.max(256,r);this._emit(`(memory (export "memory") ${r} ${o})`);for(let[u,h]of this._imports)this._emit(`(import "math" "${u}" (func $math_${u} ${h}))`);let i=[];for(let[,u]of t.bufferMap)i.push("(param i32)");let a=[];for(let u of t.shapeParams)i.push("(param i32)"),this._ensureLocal(u.name,"i32"),a.push(u.name);this._hasParallel&&(i.push("(param i32)"),i.push("(param i32)"),this._ensureLocal("_par_start","i32"),this._ensureLocal("_par_end","i32"),s.push("_par_start"),s.push("_par_end")),this._emit('(func (export "'+t.name+'") '+i.join(" ")),this._indent++,e||(this._prescanLocals(t.body),this._vecTmpCounter=0),this.target.supportsSimd()&&(this._prescanVecLocalsAll(t.body),this._vecTmpCounter=0),this._fixLetStmtLocals(t.body),this._intMinMaxDepth=0,this._prescanIntMinMax(t.body),this._ensureHalfScratch(t);let c=[];for(let[u,h]of this._locals)c.push("(local $"+u+" "+h+")");c.length>0&&this._emit(c.join(" "));let l=t.bufferMap.size;for(let u=0;u<a.length;u++)this._emit("(local.get "+(l+u)+")"),this._emit("local.set $"+a[u]);if(this._hasParallel){let u=l+a.length;this._emit("(local.get "+u+")"),this._emit("local.set $_par_start"),this._emit("(local.get "+(u+1)+")"),this._emit("local.set $_par_end")}return this._visitNode(t.body),this._indent--,this._emit(")"),this._indent--,this._emit(")"),{name:t.name,wat:this._lines.join(`
|
|
6
|
-
`),memoryPages:r,bufferOffsets:new Map(this._bufferOffsets),imports:this._imports,params:s,parallel:this._hasParallel?{extent:this._parallelExtent,outputIndices:this._findOutputIndices(t),poolSafe:this._isParallelSafe(t)}:null}}_isParallelSafe(t){let e=[],s=[t.body];for(;s.length>0;){let l=s.pop();if(l){if(l.type==="ForNode"&&l.kind===V.PARALLEL&&e.push(l),l.body&&s.push(l.body),l.stmts)for(let u of l.stmts)s.push(u);l.thenBody&&s.push(l.thenBody),l.elseBody&&s.push(l.elseBody),l.loopBody&&s.push(l.loopBody)}}if(e.length!==1)return!1;let r=e[0];if(!(t.body&&t.body.stmts?t.body.stmts:[t.body]).includes(r))return!1;let i=f((l,u)=>{let h=[l];for(;h.length>0;){let p=h.pop();if(p){if((p.type==="BufferStoreNode"||p.type==="LIRFlatStoreNode")&&u.add(p),p.type==="LIRAccumulatorNode"&&p.flushStore&&u.add(p.flushStore),p.body&&h.push(p.body),p.stmts)for(let d of p.stmts)h.push(d);p.thenBody&&h.push(p.thenBody),p.elseBody&&h.push(p.elseBody),p.loopBody&&h.push(p.loopBody)}}},"collectStores"),a=new Set,c=new Set;i(t.body,a),i(r.body,c);for(let l of a)if(!c.has(l))return!1;return!0}_ensureLocal(t,e){this._locals.has(t)||this._locals.set(t,e)}_emit(t){this._lines.push(" ".repeat(this._indent)+t)}_emitLoadOp(t){if($i.has(t)){this._emitHalfDecode(t);return}this._emit(af(t))}_emitStoreOp(t){if($i.has(t)){this._emitHalfEncode(t);return}this._emit(dw(t))}_emitHalfDecode(t){if(this._emit("i32.load16_u"),this._emit("local.set $_half_i"),t==="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 "+gg.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(t){if(this._emit("local.set $_half_f"),this._emit("(local.get $_half_f)"),this._emit("i32.reinterpret_f32"),this._emit("local.set $_half_i"),t==="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 "+gg.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(t){let e=!1;for(let[,s]of t.bufferMap)if($i.has(s.dtype)){e=!0;break}if(!e&&t.metadata&&t.metadata.locals){for(let[,s]of t.metadata.locals)if($i.has(s)){e=!0;break}}e||(e=this._treeHasHalf(t.body)),e&&(this._ensureLocal("_half_f","f32"),this._ensureLocal("_half_i","i32"),this._ensureLocal("_half_i2","i32"))}_treeHasHalf(t){let e=[t];for(;e.length>0;){let s=e.pop();if(!(!s||typeof s!="object")){if((s.type==="BufferLoadNode"||s.type==="BufferStoreNode")&&s.buffer&&$i.has(s.buffer.dtype)||(s.type==="LIRFlatLoadNode"||s.type==="LIRFlatStoreNode")&&$i.has(s.dtype))return!0;if(s.body&&e.push(s.body),s.value&&typeof s.value=="object"&&e.push(s.value),s.stmts)for(let r of s.stmts)e.push(r);if(s.a&&e.push(s.a),s.b&&e.push(s.b),s.expr&&e.push(s.expr),s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),s.initBody&&e.push(s.initBody),s.condition&&e.push(s.condition),s.offsetExpr&&e.push(s.offsetExpr),s.args)for(let r of s.args)e.push(r);if(s.indices)for(let r of s.indices)e.push(r)}}return!1}_visitNode(t){let e=t;for(;e;)switch(e.type){case"SeqNode":for(let s=0;s<e.stmts.length-1;s++)this._visitNode(e.stmts[s]);e=e.stmts[e.stmts.length-1];continue;case"AllocateNode":e=e.body;continue;case"LetStmtNode":{let s=this._vectorMode;if(s&&s.simd&&this._dependsOnVecVar(e.value)){let o=e.variable.name+"_vlet";this._ensureLocal(o,"v128"),s.vecLets||(s.vecLets=new Set),s.vecLets.add(e.variable.name),this._emitVecExpr(e.value),this._emit("local.set $"+o),e=e.body;continue}let r=Ns(e.value)||e.variable.dtype||this._defaultDtype;this._locals.set(e.variable.name,bn(r)),this._emitCoercedTo(e.value,this._numPrefix(r)),this._emit(`local.set $${e.variable.name}`),e=e.body;continue}case"ForNode":this._visitFor(e);return;case"BlockNode":this._visitBlock(e);return;case"IfThenElseNode":this._visitIf(e);return;case"BufferStoreNode":if(this._vectorMode){this._emitVecStore(e);return}this._visitStore(e);return;case"LIRFlatStoreNode":if(this._vectorMode){this._emitVecFlatStore(e);return}this._visitLIRFlatStore(e);return;case"LIRBindingsNode":this._visitLIRBindings(e);return;case"LIRAccumulatorNode":this._visitLIRAccumulator(e);return;case"WhileNode":this._visitWhile(e);return;case"EvaluateNode":return;default:return}}_findOutputIndices(t){let e=new Set,s=[t.body];for(;s.length>0;){let i=s.pop();if(i){if((i.type==="BufferStoreNode"||i.type==="LIRFlatStoreNode")&&i.buffer&&e.add(i.buffer.name),i.type==="LIRAccumulatorNode"&&i.flushStore&&i.flushStore.buffer&&e.add(i.flushStore.buffer.name),i.body&&s.push(i.body),i.stmts)for(let a of i.stmts)s.push(a);i.thenBody&&s.push(i.thenBody),i.elseBody&&s.push(i.elseBody),i.loopBody&&s.push(i.loopBody)}}let r=[],o=0;for(let[,i]of t.bufferMap)e.has(i.name)&&r.push(o),o++;return r}_scanParallel(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="ForNode"&&s.kind===V.PARALLEL){this._hasParallel=!0,this._parallelExtent=s.extent&&s.extent.type==="IntImmNode"?s.extent.value:0;return}if(s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody)}}}_visitFor(t){let e=t.loopVar.name,s=this._constExtent(t.extent);if(t.kind===V.PARALLEL){if(s!==null&&this._parallelExtent&&s!==this._parallelExtent){this._emitForLoop(e,t.extent,t.body);return}this._emit("(local.get $_par_start)"),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._emit("(local.get $_par_end)"),this._emit("i32.ge_s"),this._emit("br_if $break_"+e),this._visitNode(t.body),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(")");return}if(t.kind===V.VECTORIZED&&this.target.supportsSimd()&&s!==null){this._visitVectorizedFor(t);return}if((t.kind===V.UNROLLED||t.kind===V.VECTORIZED)&&s!==null&&s<=32&&!this._isZeroFillBody(t.body)){for(let o=0;o<s;o++)this._emit("(i32.const "+o+")"),this._emit("local.set $"+e),this._visitNode(t.body);return}let r=this._detectWasmAcc(t);if(r){let o="_wacc_"+(this._waccCounter=(this._waccCounter||0)+1);this._ensureLocal(o,bn(r.buf.dtype)),this._emitAddr(r.buf,r.outerIndices),this._emitLoadOp(r.buf.dtype),this._emit("local.set $"+o),this._wasmAcc={local:o,bufName:r.buf.name,indices:r.indices},this._emitForLoop(e,t.extent,t.body),this._emitAddr(r.buf,r.outerIndices),this._emit("(local.get $"+o+")"),this._emitStoreOp(r.buf.dtype),this._wasmAcc=null;return}this._emitForLoop(e,t.extent,t.body)}_visitLIRFlatStore(t){if(this._wasmAcc&&t.buffer.name===this._wasmAcc.bufName){this._emitCoercedTo(t.value,this._numPrefix(t.dtype)),this._emit("local.set $"+this._wasmAcc.local);return}this._emitFlatAddr(t.buffer,t.offsetExpr),this._emitCoercedTo(t.value,this._numPrefix(t.dtype)),this._emitStoreOp(t.dtype)}_visitLIRBindings(t){for(let e of t.bindings)this._emitExpr(e.expr),this._emit(`local.set $${e.name}`);this._visitNode(t.body)}_vecAccumOperandsUnitStride(t){let e=t.loopVar&&t.loopVar.name;if(!e)return!1;let s=f(i=>{let a=[i];for(;a.length>0;){let c=a.pop();if(!(!c||typeof c!="object")){if(c.type==="VariableNode"&&c.name===e)return!0;if(c.a&&a.push(c.a),c.b&&a.push(c.b),c.expr&&a.push(c.expr),c.args)for(let l of c.args)a.push(l);if(c.indices)for(let l of c.indices)a.push(l);c.offsetExpr&&a.push(c.offsetExpr)}}return!1},"usesVar"),r=f(i=>{let a=[i];for(;a.length>0;){let c=a.pop();if(!(!c||typeof c!="object")){if(c.type==="MathOpNode"&&c.op==="*"&&(s(c.a)||s(c.b)))return!0;if(c.a&&a.push(c.a),c.b&&a.push(c.b),c.expr&&a.push(c.expr),c.args)for(let l of c.args)a.push(l)}}return!1},"stridedMul"),o=[t.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(s(i.indices[a]))return!1}if(i.type==="LIRFlatLoadNode"&&i.offsetExpr&&r(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(t,e){if(t==="*")return"mul";if(t==="max"||t==="min"){if(ks(e))return t;throw new Error(`wasm accumulator: integer ${t} reduction not supported (dtype ${e})`)}return"add"}_visitLIRAccumulator(t){let e=t.localName,s=t.dtype;this._ensureLocal(e,bn(s));let r=this._constExtent(t.extent),o=t.op||"+",i=o==="+"&&r!==null&&t.loopKind===V.VECTORIZED&&this.target.supportsSimd()?Wm(s):null,a=i?this.target.vectorWidth:0;if(i&&r>=a&&this._vecAccumOperandsUnitStride(t)){this._visitVecAccumulator(t,i,a,r);return}this._emitExpr(t.initLoad),this._emit("local.set $"+e);let c=this._wasmAcc;this._wasmAcc={local:e,bufName:t.flushStore.buffer.name};let l=t.loopVar.name;this._emit("(i32.const 0)"),this._emit("local.set $"+l),this._emit("(block $break_"+l),this._indent++,this._emit("(loop $loop_"+l),this._indent++,this._emit("(local.get $"+l+")"),this._emitExpr(t.extent),this._emit("i32.ge_s"),this._emit("br_if $break_"+l),this._emit("(local.get $"+e+")"),this._emitCoercedTo(t.body,this._numPrefix(s)),this._emit(this._numPrefix(s)+"."+this._accumInstr(o,s)),this._emit("local.set $"+e),this._emit("(local.get $"+l+")"),this._emit("(i32.const 1)"),this._emit("i32.add"),this._emit("local.set $"+l),this._emit("br $loop_"+l),this._indent--,this._emit(")"),this._indent--,this._emit(")"),this._emitFlatAddr(t.flushStore.buffer,t.flushStore.offsetExpr),this._emit("(local.get $"+e+")"),this._emitStoreOp(t.flushStore.dtype),this._wasmAcc=c}_visitVecAccumulator(t,e,s,r){let o=t.localName,i=t.dtype,a=t.loopVar.name,c=ks(i),l=c?"f32.add":"i32.add",u=wn(i,"add"),h=Math.floor(r/s)*s,p=h,d=o+"_vec";this._ensureLocal(d,"v128"),this._emitExpr(t.initLoad),this._emit("local.set $"+o),this._emit(c?"(f32.const 0)":"(i32.const 0)"),this._emit(e.splat),this._emit("local.set $"+d);let m=this._wasmAcc;this._wasmAcc={local:o,bufName:t.flushStore.buffer.name},this._vectorMode={dtype:i,lanes:s,loopVar:a,simd:e,laneVars:this._computeLaneVars(t)},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(t.body),this._emit(u),this._emit("local.set $"+d),this._emit("(local.get $"+a+")"),this._emit("(i32.const "+s+")"),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<s;g++)this._emit("(local.get $"+d+")"),this._emit(e.extractLane+" "+g),this._emit(l);this._emit("local.set $"+o),p<r&&(this._emit("(i32.const "+p+")"),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 "+r+")"),this._emit("i32.ge_s"),this._emit("br_if $tbreak_"+a),this._emit("(local.get $"+o+")"),this._emitCoerced(t.body,c),this._emit(l),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(t.flushStore.buffer,t.flushStore.offsetExpr),this._emit("(local.get $"+o+")"),this._emitStoreOp(t.flushStore.dtype),this._wasmAcc=m}_emitFlatAddr(t,e){let s=this._bufferOffsets.get(t.name)||0,r=qr(t.dtype||"f32");if(!e||e.type==="IntImmNode"&&e.value===0){this._emit(`(i32.const ${s})`);return}let o=this._vectorMode,i=r+"#"+this._exprKey(e);if(o&&o.addrLocal&&this._exprKey(e)!=="?"){(!o._addrEmitted||o._addrKey!==i)&&(this._emitExpr(e),this._emit(`(i32.const ${r})`),this._emit("i32.mul"),this._emit("local.set $"+o.addrLocal),o._addrEmitted=!0,o._addrKey=i),this._emit("(local.get $"+o.addrLocal+")"),s>0&&(this._emit(`(i32.const ${s})`),this._emit("i32.add"));return}this._emitExpr(e),this._emit(`(i32.const ${r})`),this._emit("i32.mul"),s>0&&(this._emit(`(i32.const ${s})`),this._emit("i32.add"))}_emitForLoop(t,e,s){this._emit("(i32.const 0)"),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._emitExpr(e),this._emit("i32.ge_s"),this._emit("br_if $break_"+t),this._loopVarStack.push(t),this._visitNode(s),this._loopVarStack.pop(),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(")")}_visitBlock(t){for(let e of t.iterVars)if(e.iterVar&&e.binding){this._emitExpr(e.binding),this._emit(`local.set $${e.iterVar.name}`);let s=this._vectorMode;s&&s.laneVars&&this._dependsOnVecVar(e.binding)&&s.laneVars.add(e.iterVar.name)}if(t.initBody){let e=this._loopVarStack.length>0?this._loopVarStack[this._loopVarStack.length-1]:null;e?(this._emit("(local.get $"+e+")"),this._emit("i32.eqz"),this._emit("(if"),this._indent++,this._emit("(then"),this._indent++,this._visitNode(t.initBody),this._indent--,this._emit(")"),this._indent--,this._emit(")")):this._visitNode(t.initBody)}this._visitNode(t.body)}_visitStore(t){if(this._wasmAcc&&this._isAccTarget(t.buffer,t.indices)){this._emitCoercedTo(t.value,this._numPrefix(t.buffer.dtype)),this._emit("local.set $"+this._wasmAcc.local);return}this._emitAddr(t.buffer,t.indices),this._emitCoercedTo(t.value,this._numPrefix(t.buffer.dtype)),this._emitStoreOp(t.buffer.dtype)}_visitIf(t){this._emitExpr(t.condition),this._emit("(if"),this._indent++,this._emit("(then"),this._indent++,this._visitNode(t.thenBody),this._indent--,this._emit(")"),t.elseBody&&(this._emit("(else"),this._indent++,this._visitNode(t.elseBody),this._indent--,this._emit(")")),this._indent--,this._emit(")")}_visitWhile(t){this._visitNode(t.condBody),this._emit("(block $wbreak"),this._indent++,this._emit("(loop $wloop"),this._indent++,this._emitAddr(t.condVar,[]),this._emitLoadOp(t.condVar.dtype),this._emit("i32.eqz"),this._emit("br_if $wbreak"),this._visitNode(t.loopBody),this._visitNode(t.condBody),this._emit("br $wloop"),this._indent--,this._emit(")"),this._indent--,this._emit(")")}_emitAddr(t,e){let s=this._bufferOffsets.get(t.name)||0,r=qr(t.dtype);if(e.length===0){this._emit(`(i32.const ${s})`);return}let o=this._vectorMode;if(o&&o.addrLocal&&e.length>0){let i=r+"#"+e.map(a=>this._exprKey(a)).join(",");(!o._addrEmitted||o._addrKey!==i)&&(this._emitFlatIndex(t,e),this._emit(`(i32.const ${r})`),this._emit("i32.mul"),this._emit("local.set $"+o.addrLocal),o._addrEmitted=!0,o._addrKey=i),this._emit("(local.get $"+o.addrLocal+")"),s>0&&(this._emit(`(i32.const ${s})`),this._emit("i32.add"));return}this._emitFlatIndex(t,e),this._emit(`(i32.const ${r})`),this._emit("i32.mul"),s>0&&(this._emit(`(i32.const ${s})`),this._emit("i32.add"))}_emitFlatIndex(t,e){if(e.length===1){this._emitExpr(e[0]);return}let s=!0;for(let r=0;r<e.length;r++){this._emitExpr(e[r]);let o=t.strides[r];typeof o=="number"&&o>=0?o!==1&&(this._emit(`(i32.const ${o})`),this._emit("i32.mul")):(this._emitDynamicStride(t,r),this._emit("i32.mul")),s||this._emit("i32.add"),s=!1}}_emitDynamicStride(t,e){let s=0;for(let r=e+1;r<t.shape.length;r++){let o=t.shape[r];if(typeof o=="number"&&o>=0)this._emit(`(i32.const ${o})`);else{let i=this._resolveShapeParam(t,r);this._emit(`(local.get $${i})`)}s>0&&this._emit("i32.mul"),s++}s===0&&this._emit("(i32.const 1)")}_resolveShapeParam(t,e){if(this._primFunc&&this._primFunc.shapeParamMap){let s=`${t.name}:${e}`,r=this._primFunc.shapeParamMap.get(s);if(r)return r.name}return"_ds_0"}_emitExpr(t){if(!t){this._emit("(i32.const 0)");return}switch(t.type){case"IntImmNode":this._emit(`(i32.const ${t.value})`);break;case"FloatImmNode":this._emit(`(f32.const ${t.value})`);break;case"VariableNode":this._emit(`(local.get $${t.name})`);break;case"BufferLoadNode":this._wasmAcc&&this._isAccTarget(t.buffer,t.indices)?this._emit("(local.get $"+this._wasmAcc.local+")"):(this._emitAddr(t.buffer,t.indices),this._emitLoadOp(t.buffer.dtype));break;case"LIRFlatLoadNode":this._wasmAcc&&t.buffer.name===this._wasmAcc.bufName?this._emit("(local.get $"+this._wasmAcc.local+")"):(this._emitFlatAddr(t.buffer,t.offsetExpr),this._emitLoadOp(t.dtype));break;case"MathOpNode":this._emitMathOp(t);break;case"CompareNode":this._emitCompare(t);break;case"CastNode":this._emitCast(t);break;case"CallExternNode":this._emitCallExtern(t);break;case"IfThenElseNode":{let e=t._dtype||Ns(t.thenBody),s=this._numPrefix(e);this._emitExpr(t.condition),ks(this._wasmExprDtype(t.condition))&&(this._emit("(f32.const 0)"),this._emit("f32.ne")),this._emit("(if (result "+s+")"),this._indent++,this._emit("(then"),this._indent++,this._emitCoercedTo(t.thenBody,s),this._indent--,this._emit(")"),this._emit("(else"),this._indent++,this._emitCoercedTo(t.elseBody,s),this._indent--,this._emit(")"),this._indent--,this._emit(")");break}default:this._emit("(i32.const 0)");break}}_numPrefix(t){return t==="f64"?"f64":t==="i64"?"i64":ks(t)?"f32":"i32"}_exprPrefix(t){return this._numPrefix(t&&t._dtype||Ns(t))}_convertTo(t,e){t!==e&&(e==="f64"?t==="f32"?this._emit("f64.promote_f32"):t==="i32"?this._emit("f64.convert_i32_s"):t==="i64"&&this._emit("f64.convert_i64_s"):e==="f32"?t==="f64"?this._emit("f32.demote_f64"):t==="i32"?this._emit("f32.convert_i32_s"):t==="i64"&&this._emit("f32.convert_i64_s"):e==="i32"?t==="f64"?this._emit("i32.trunc_f64_s"):t==="f32"?this._emit("i32.trunc_f32_s"):t==="i64"&&this._emit("i32.wrap_i64"):e==="i64"&&(t==="i32"?this._emit("i64.extend_i32_s"):t==="f32"?this._emit("i64.trunc_f32_s"):t==="f64"&&this._emit("i64.trunc_f64_s")))}_emitCoercedTo(t,e){this._emitExpr(t),this._convertTo(this._exprPrefix(t),e)}_emitCoerced(t,e){this._emitCoercedTo(t,e?"f32":"i32")}_emitMathOp(t){let e=this._exprPrefix(t.a),s=t.b?this._exprPrefix(t.b):e,r=this._joinPrefix(e,s);if(!t.b){t.op==="-"?r==="f32"||r==="f64"?(this._emitCoercedTo(t.a,r),this._emit(r+".neg")):(this._emit("("+r+".const 0)"),this._emitCoercedTo(t.a,r),this._emit(r+".sub")):t.op==="!"&&(this._emitExpr(t.a),this._emit("i32.eqz"));return}if(t.op==="&&"){this._emitExpr(t.a),this._emitExpr(t.b),this._emit("i32.and");return}if(t.op==="||"){this._emitExpr(t.a),this._emitExpr(t.b),this._emit("i32.or");return}let o=r==="f32"||r==="f64";if(!o){if(t.op==="/"||t.op==="//"){this._emitIntDiv(t,r);return}if(t.op==="%"){this._emitIntRem(t,r);return}}switch(this._emitCoercedTo(t.a,r),this._emitCoercedTo(t.b,r),t.op){case"+":this._emit(`${r}.add`);break;case"-":this._emit(`${r}.sub`);break;case"*":this._emit(`${r}.mul`);break;case"/":this._emit(o?`${r}.div`:`${r}.div_s`);break;case"%":this._emit(`${r}.rem_s`);break;case"//":this._emit(`${r}.div_s`);break;case"<":this._emit(o?`${r}.lt`:`${r}.lt_s`);break;case">":this._emit(o?`${r}.gt`:`${r}.gt_s`);break;case"<=":this._emit(o?`${r}.le`:`${r}.le_s`);break;case">=":this._emit(o?`${r}.ge`:`${r}.ge_s`);break;default:this._emit(`${r}.add`);break}}_joinPrefix(t,e){return t==="f64"||e==="f64"?"f64":t==="f32"||e==="f32"?"f32":t==="i64"||e==="i64"?"i64":"i32"}_emitCompare(t){let e=this._exprPrefix(t.a),s=this._exprPrefix(t.b),r=this._joinPrefix(e,s),o=r==="f32"||r==="f64";this._emitCoercedTo(t.a,r),this._emitCoercedTo(t.b,r);let i={eq:"eq",ne:"ne",lt:o?"lt":"lt_s",le:o?"le":"le_s",gt:o?"gt":"gt_s",ge:o?"ge":"ge_s"};this._emit(r+"."+(i[t.direction]||"eq"))}_emitCast(t){this._emitExpr(t.expr),this._convertTo(this._numPrefix(t.fromDtype),this._numPrefix(t.toDtype))}_emitCallExtern(t){if((t.externName==="min"||t.externName==="max")&&!ks(t.dtype)){this._emitIntMinMax(t);return}if(t.externName==="abs"&&!ks(t.dtype)){this._emitIntAbs(t);return}let e=new Set(["sqrt","abs","ceil","floor","min","max","rsqrt"]),s=t.externName==="abs"||e.has(t.externName)?this._numPrefix(t.dtype)==="i32"?"f32":this._numPrefix(t.dtype):"f32";if(t.externName==="rsqrt"){this._emit("("+s+".const 1)");for(let r of t.args)this._emitCoercedTo(r,s);this._emit(s+".sqrt"),this._emit(s+".div");return}for(let r of t.args)this._emitCoercedTo(r,s);switch(t.externName){case"sqrt":this._emit(s+".sqrt");break;case"abs":this._emit(s+".abs");break;case"ceil":this._emit(s+".ceil");break;case"floor":this._emit(s+".floor");break;case"min":this._emit(s+".min");break;case"max":this._emit(s+".max");break;default:this._imports.has(t.externName)&&(this._emit(`call $math_${t.externName}`),this._numPrefix(t.dtype)==="f64"&&this._emit("f64.promote_f32"));break}}_emitIntAbs(t){let e=this._intMinMaxEmitDepth||0,s="_iabs"+e;this._intMinMaxEmitDepth=e+1,this._emitExpr(t.args[0]),this._emit("local.set $"+s),this._intMinMaxEmitDepth=e,this._emit("(i32.const 0)"),this._emit("(local.get $"+s+")"),this._emit("i32.sub"),this._emit("(local.get $"+s+")"),this._emit("(local.get $"+s+")"),this._emit("(i32.const 0)"),this._emit("i32.lt_s"),this._emit("select")}_emitIntMinMax(t){let e=this._intMinMaxEmitDepth||0,s="_immm_a"+e,r="_immm_b"+e;this._intMinMaxEmitDepth=e+1,this._emitExpr(t.args[0]),this._emit("local.set $"+s),this._emitExpr(t.args[1]),this._emit("local.set $"+r),this._intMinMaxEmitDepth=e,this._emit("(local.get $"+s+")"),this._emit("(local.get $"+r+")"),this._emit("(local.get $"+s+")"),this._emit("(local.get $"+r+")"),this._emit(t.externName==="min"?"i32.lt_s":"i32.gt_s"),this._emit("select")}_isIntDivNode(t){if(t.type!=="MathOpNode"||!t.b||t.op!=="/"&&t.op!=="//"&&t.op!=="%")return!1;let e=this._joinPrefix(this._exprPrefix(t.a),this._exprPrefix(t.b));return e==="i32"||e==="i64"}_emitIntDiv(t,e){let s=this._intDivEmitDepth||0,r="_idiv_a"+s,o="_idiv_b"+s;this._intDivEmitDepth=s+1,this._emitCoercedTo(t.a,e),this._emit("local.set $"+r),this._emitCoercedTo(t.b,e),this._emit("local.set $"+o),this._intDivEmitDepth=s;let i=e==="i64"?"-9223372036854775808":"-2147483648";this._emit("(local.get $"+r+")"),this._emit("("+e+".const 1)"),this._emit("(local.get $"+o+")"),this._emit("(local.get $"+o+")"),this._emit(e+".eqz"),this._emit("(local.get $"+r+")"),this._emit("("+e+".const "+i+")"),this._emit(e+".eq"),this._emit("(local.get $"+o+")"),this._emit("("+e+".const -1)"),this._emit(e+".eq"),this._emit("i32.and"),this._emit("i32.or"),this._emit("select"),this._emit(e+".div_s"),this._emit("local.set $"+r),this._emit("("+e+".const 0)"),this._emit("(local.get $"+r+")"),this._emit("(local.get $"+o+")"),this._emit(e+".eqz"),this._emit("select")}_emitIntRem(t,e){let s=this._intDivEmitDepth||0,r="_idiv_a"+s,o="_idiv_b"+s;this._intDivEmitDepth=s+1,this._emitCoercedTo(t.a,e),this._emit("local.set $"+r),this._emitCoercedTo(t.b,e),this._emit("local.set $"+o),this._intDivEmitDepth=s,this._emit("(local.get $"+r+")"),this._emit("("+e+".const 1)"),this._emit("(local.get $"+o+")"),this._emit("(local.get $"+o+")"),this._emit(e+".eqz"),this._emit("select"),this._emit(e+".rem_s"),this._emit("local.set $"+r),this._emit("("+e+".const 0)"),this._emit("(local.get $"+r+")"),this._emit("(local.get $"+o+")"),this._emit(e+".eqz"),this._emit("select")}_prescanIntMinMax(t){let e=f((s,r,o)=>{if(!s||typeof s!="object")return;let i=r,a=o;if(s.type==="CallExternNode"&&(s.externName==="min"||s.externName==="max")&&!ks(s.dtype))this._ensureLocal("_immm_a"+r,"i32"),this._ensureLocal("_immm_b"+r,"i32"),r+1>this._intMinMaxDepth&&(this._intMinMaxDepth=r+1),i=r+1;else if(s.type==="CallExternNode"&&s.externName==="abs"&&!ks(s.dtype))this._ensureLocal("_iabs"+r,"i32"),r+1>this._intMinMaxDepth&&(this._intMinMaxDepth=r+1),i=r+1;else if(this._isIntDivNode(s)){let c=bn(this._joinPrefix(this._exprPrefix(s.a),this._exprPrefix(s.b)));this._ensureLocal("_idiv_a"+o,c),this._ensureLocal("_idiv_b"+o,c),a=o+1}if(s.body&&e(s.body,i,a),s.value&&typeof s.value=="object"&&e(s.value,i,a),s.a&&e(s.a,i,a),s.b&&e(s.b,i,a),s.expr&&e(s.expr,i,a),s.condition&&e(s.condition,i,a),s.offsetExpr&&e(s.offsetExpr,i,a),s.thenBody&&e(s.thenBody,i,a),s.elseBody&&e(s.elseBody,i,a),s.initBody&&e(s.initBody,i,a),s.stmts)for(let c of s.stmts)e(c,i,a);if(s.args)for(let c of s.args)e(c,i,a);if(s.indices)for(let c of s.indices)e(c,i,a);if(s.bindings)for(let c of s.bindings)e(c.expr,i,a);if(s.iterVars)for(let c of s.iterVars)c.binding&&e(c.binding,i,a)},"visit");e(t,0,0)}_mathImportSig(t,e){return`${Array(e).fill("(param f32)").join(" ")} (result f32)`}_constExtent(t){return t.type==="IntImmNode"?t.value:null}_isZeroFillBody(t){let e=t;for(;e;){if(e.type==="ForNode"){e=e.body;continue}if(e.type==="BlockNode"){e=e.body;continue}if(e.type==="BufferStoreNode"||e.type==="LIRFlatStoreNode"){let s=e.value;return s.type==="FloatImmNode"&&s.value===0||s.type==="IntImmNode"&&s.value===0}return!1}return!1}_collectBindings(t,e){let s=[t];for(;s.length>0;){let r=s.pop();if(!(!r||typeof r!="object")){if(r.type==="BlockNode"&&r.iterVars)for(let o of r.iterVars)o.iterVar&&o.binding&&e.push({name:o.iterVar.name,expr:o.binding});if(r.type==="LIRBindingsNode"&&r.bindings)for(let o of r.bindings)e.push({name:o.name,expr:o.expr});if(r.body&&s.push(r.body),r.stmts)for(let o of r.stmts)s.push(o);r.thenBody&&s.push(r.thenBody),r.elseBody&&s.push(r.elseBody),r.initBody&&s.push(r.initBody),r.loopBody&&s.push(r.loopBody)}}}_computeLaneVars(t){let e=new Set([t.loopVar.name]),s=[];this._collectBindings(t.body,s);let r=f(i=>{let a=[],c=[i];for(;c.length>0;){let l=c.pop();if(!(!l||typeof l!="object")&&(l.type==="VariableNode"&&a.push(l.name),l.a&&c.push(l.a),l.b&&c.push(l.b),l.expr&&c.push(l.expr),l.args))for(let u of l.args)c.push(u)}return a},"varsIn"),o=!0;for(;o;){o=!1;for(let i of s)e.has(i.name)||r(i.expr).some(a=>e.has(a))&&(e.add(i.name),o=!0)}return e}_vecLoadsContiguous(t,e){let s=f(i=>{let a=[i];for(;a.length>0;){let c=a.pop();if(!(!c||typeof c!="object")){if(c.type==="VariableNode"&&e.has(c.name))return!0;if(c.a&&a.push(c.a),c.b&&a.push(c.b),c.expr&&a.push(c.expr),c.args)for(let l of c.args)a.push(l);if(c.indices)for(let l of c.indices)a.push(l);c.offsetExpr&&a.push(c.offsetExpr)}}return!1},"usesLane"),r=f(i=>{let a=[i];for(;a.length>0;){let c=a.pop();if(!(!c||typeof c!="object")){if(c.type==="MathOpNode"&&c.op==="*"&&(s(c.a)||s(c.b)))return!0;if(c.a&&a.push(c.a),c.b&&a.push(c.b),c.expr&&a.push(c.expr),c.args)for(let l of c.args)a.push(l)}}return!1},"stridedMul"),o=[t];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(s(i.indices[a]))return!1}if(i.type==="LIRFlatLoadNode"&&i.offsetExpr&&r(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(t,e){let s=[t];for(;s.length>0;){let r=s.pop();if(!r)continue;let o=null;if(r.type==="BufferStoreNode"?o=r.indices:r.type==="LIRFlatStoreNode"&&(o=r.offsetExpr),o!=null){let i=[],a=Array.isArray(o)?[...o]:[o];for(;a.length>0;){let c=a.pop();if(!(!c||typeof c!="object")){if(c.type==="VariableNode"&&i.push(c.name),c.a&&a.push(c.a),c.b&&a.push(c.b),c.expr&&a.push(c.expr),c.args)for(let l of c.args)a.push(l);if(c.indices)for(let l of c.indices)a.push(l);c.offsetExpr&&a.push(c.offsetExpr)}}if(!i.some(c=>e.has(c)))return!1}if(r.body&&s.push(r.body),r.stmts)for(let i of r.stmts)s.push(i);r.thenBody&&s.push(r.thenBody),r.elseBody&&s.push(r.elseBody),r.loopBody&&s.push(r.loopBody)}return!0}_visitVectorizedFor(t){let e=t.loopVar.name,s=this._constExtent(t.extent),r=this._inferBodyDtype(t.body)||this._defaultDtype,o=this.target.vectorWidth,i=Wm(r);if(!i||s<o||this._treeHasHalf(t.body)){this._emitForLoop(e,t.extent,t.body);return}let a=this._computeLaneVars(t);if(!this._vecStoresLaneIndexed(t.body,a)||!this._vecLoadsContiguous(t.body,a)){this._emitForLoop(e,t.extent,t.body);return}let c=Math.floor(s/o)*o,l=s-c,u=qr(r),d=this._countBufAccesses(t.body)>=2?"_vaddr_"+e:null;if(c>0&&(this._vectorMode={dtype:r,lanes:o,loopVar:e,simd:i,addrLocal:d,laneVars:a},c===o?(this._emit("(i32.const 0)"),this._emit("local.set $"+e),this._emitVecAddrReset(),this._visitNode(t.body)):(this._emit("(i32.const 0)"),this._emit("local.set $"+e),this._emit("(block $vbreak_"+e),this._indent++,this._emit("(loop $vloop_"+e),this._indent++,this._emit("(local.get $"+e+")"),this._emit("(i32.const "+c+")"),this._emit("i32.ge_s"),this._emit("br_if $vbreak_"+e),this._emitVecAddrReset(),this._visitNode(t.body),this._emit("(local.get $"+e+")"),this._emit("(i32.const "+o+")"),this._emit("i32.add"),this._emit("local.set $"+e),this._emit("br $vloop_"+e),this._indent--,this._emit(")"),this._indent--,this._emit(")")),this._vectorMode=null),l>0)for(let m=c;m<s;m++)this._emit("(i32.const "+m+")"),this._emit("local.set $"+e),this._visitNode(t.body)}_emitVecAddrReset(){this._vectorMode&&this._vectorMode.addrLocal&&(this._vectorMode._addrEmitted=!1,this._vectorMode._addrKey=null)}_countBufAccesses(t){let e=0,s=[t];for(;s.length>0;){let r=s.pop();if(!(!r||typeof r!="object")){if((r.type==="BufferLoadNode"||r.type==="BufferStoreNode"||r.type==="LIRFlatLoadNode"||r.type==="LIRFlatStoreNode")&&e++,r.body&&s.push(r.body),r.value&&typeof r.value=="object"&&s.push(r.value),r.a&&s.push(r.a),r.b&&s.push(r.b),r.stmts)for(let o of r.stmts)s.push(o);if(r.args)for(let o of r.args)s.push(o);if(r.indices)for(let o of r.indices)s.push(o);r.thenBody&&s.push(r.thenBody),r.elseBody&&s.push(r.elseBody),r.expr&&s.push(r.expr),r.condition&&s.push(r.condition),r.offsetExpr&&s.push(r.offsetExpr)}}return e}_inferBodyDtype(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="BufferStoreNode"&&s.buffer)return s.buffer.dtype;if(s.type==="LIRFlatStoreNode")return s.dtype||this._defaultDtype;if(s.type==="BufferLoadNode"&&s.buffer)return s.buffer.dtype;if(s.type==="LIRFlatLoadNode")return s.dtype||this._defaultDtype;if(s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),s.value&&typeof s.value=="object"&&e.push(s.value)}}return null}_emitVecStore(t){let e=this._vectorMode;this._emitAddr(t.buffer,t.indices),this._emitVecExpr(t.value),this._emit(e.simd.vecStore)}_emitVecFlatStore(t){let e=this._vectorMode;this._emitFlatAddr(t.buffer,t.offsetExpr),this._emitVecExpr(t.value),this._emit(e.simd.vecStore)}_dependsOnVecVar(t){let e=this._vectorMode;if(!e)return!0;let s=e.laneVars,r=Array.isArray(t)?[...t]:[t];for(;r.length>0;){let o=r.pop();if(!(!o||typeof o!="object")){if(o.type==="VariableNode"&&(o.name===e.loopVar||s&&s.has(o.name)))return!0;if(o.a&&r.push(o.a),o.b&&r.push(o.b),o.expr&&r.push(o.expr),o.args)for(let i of o.args)r.push(i);if(o.indices)for(let i of o.indices)r.push(i);o.offsetExpr&&r.push(o.offsetExpr)}}return!1}_emitVecExpr(t){if(!t){this._emit("(i32.const 0)");return}let e=this._vectorMode,s=e.dtype;switch(t.type){case"BufferLoadNode":this._dependsOnVecVar(t.indices)?(this._emitAddr(t.buffer,t.indices),this._emit(e.simd.vecLoad)):(this._emitAddr(t.buffer,t.indices),this._emit(af(t.buffer.dtype)),this._emit(e.simd.splat));break;case"LIRFlatLoadNode":this._dependsOnVecVar(t.offsetExpr)?(this._emitFlatAddr(t.buffer,t.offsetExpr),this._emit(e.simd.vecLoad)):(this._emitFlatAddr(t.buffer,t.offsetExpr),this._emit(af(t.dtype)),this._emit(e.simd.splat));break;case"FloatImmNode":this._emit("(f32.const "+t.value+")"),this._emit(e.simd.splat);break;case"IntImmNode":ks(s)?(this._emit("(f32.const "+t.value+")"),this._emit(e.simd.splat)):(this._emit("(i32.const "+t.value+")"),this._emit(e.simd.splat));break;case"VariableNode":e.vecLets&&e.vecLets.has(t.name)?this._emit("(local.get $"+t.name+"_vlet)"):t.name===e.loopVar?this._emit("(local.get $"+t.name+")"):this._locals.get(t.name)==="f32"?(this._emit("(local.get $"+t.name+")"),this._emit(e.simd.splat)):(this._emit("(local.get $"+t.name+")"),ks(s)&&this._emit("f32.convert_i32_s"),this._emit(e.simd.splat));break;case"MathOpNode":this._emitVecMathOp(t);break;case"CompareNode":this._emitVecCompare(t);break;case"CallExternNode":this._emitVecCallExtern(t);break;case"CastNode":this._emitVecExpr(t.expr),ks(t.toDtype)&&this._isVecMaskExpr(t.expr)&&(this._emit("(f32.const 1)"),this._emit("f32x4.splat"),this._emit("v128.and"));break;case"IfThenElseNode":this._emitVecSelect(t);break;default:this._emitExpr(t);break}}_isVecMaskExpr(t){return t?t.type==="CompareNode"||t.type==="MathOpNode"&&(t.op==="&&"||t.op==="||"||t.op==="!"):!1}_emitVecMathOp(t){let e=this._vectorMode,s=e.dtype;if(!t.b){if(t.op==="-"){let c=wn(s,"neg");c?(this._emitVecExpr(t.a),this._emit(c)):(this._emit("(i32.const 0)"),this._emit(e.simd.splat),this._emitVecExpr(t.a),this._emit(wn(s,"sub")))}else t.op==="!"&&(this._emitVecExpr(t.a),this._emit("v128.not"));return}if(t.op==="&&"){this._emitVecExpr(t.a),this._emitVecExpr(t.b),this._emit("v128.and");return}if(t.op==="||"){this._emitVecExpr(t.a),this._emitVecExpr(t.b),this._emit("v128.or");return}let o={"+":"add","-":"sub","*":"mul","/":"div"}[t.op];if(o){let c=wn(s,o);if(c){this._emitVecExpr(t.a),this._emitVecExpr(t.b),this._emit(c);return}}let a={"<":"lt",">":"gt","<=":"le",">=":"ge"}[t.op];if(a){let c=wn(s,a);if(c){this._emitVecExpr(t.a),this._emitVecExpr(t.b),this._emit(c);return}}this._emitExpr(t),this._emit(e.simd.splat)}_emitVecCompare(t){let e=this._vectorMode,s=e.dtype,r=wn(s,t.direction);r?(this._emitVecExpr(t.a),this._emitVecExpr(t.b),this._emit(r)):(this._emitExpr(t),this._emit(e.simd.splat))}_emitVecCallExtern(t){let e=this._vectorMode,s=e.dtype,r=wn(s,t.externName);if(r){t.externName==="min"||t.externName==="max"?(this._emitVecExpr(t.args[0]),this._emitVecExpr(t.args[1])):this._emitVecExpr(t.args[0]),this._emit(r);return}if(t.externName==="rsqrt"){let o=wn(s,"sqrt");if(o){this._emit("(f32.const 1)"),this._emit(e.simd.splat),this._emitVecExpr(t.args[0]),this._emit(o),this._emit(wn(s,"div"));return}}this._emitScalarizeFallback(t)}_emitVecSelect(t){let e=this._vectorMode;this._emitVecExpr(t.thenBody),this._emitVecExpr(t.elseBody),this._emitVecExpr(t.condition),this._emit(e.simd.bitselect)}_emitScalarizeFallback(t){let e=this._vectorMode,s=e.lanes,r=e.simd.extractLane,o=e.simd.replaceLane,i=e.simd.splat,a="_vtmp_"+this._vecTmpCounter++;this._ensureLocal(a,"v128");let c=[];for(let l=0;l<s;l++){let u="_vl_"+a+"_"+l;this._ensureLocal(u,bn(e.dtype)),c.push(u)}this._emitVecExpr(t.args[0]),this._emit("local.set $"+a);for(let l=0;l<s;l++){if(this._emit("(local.get $"+a+")"),this._emit(r+" "+l),t.args.length>1){let u="_vtmp2_"+a;if(l===0){this._ensureLocal(u,"v128");let h=this._vectorMode;this._vectorMode=e,this._emitVecExpr(t.args[1]),this._vectorMode=h,this._emit("local.set $"+u)}this._emit("(local.get $"+u+")"),this._emit(r+" "+l)}this._imports.has(t.externName)&&this._emit("call $math_"+t.externName),this._emit("local.set $"+c[l])}this._emit("(local.get $"+c[s-1]+")"),this._emit(i);for(let l=s-2;l>=0;l--)this._emit("(local.get $"+c[l]+")"),this._emit(o+" "+l)}_prescanVecLocalsAll(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="ForNode"&&s.kind===V.VECTORIZED&&(this._prescanVecLocals(s.body),this._countBufAccesses(s.body)>=2&&this._ensureLocal("_vaddr_"+s.loopVar.name,"i32"),this._prescanVecLets(s)),s.type==="LIRAccumulatorNode"&&s.loopKind===V.VECTORIZED&&(this._ensureLocal(s.localName+"_vec","v128"),this._prescanVecLocals(s.body)),s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),s.initBody&&e.push(s.initBody),s.loopBody&&e.push(s.loopBody)}}}_prescanVecLets(t){let e=this._computeLaneVars(t),s=f(o=>{let i=[o];for(;i.length>0;){let a=i.pop();if(!(!a||typeof a!="object")){if(a.type==="VariableNode"&&e.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 c of a.args)i.push(c);if(a.indices)for(let c of a.indices)i.push(c);a.offsetExpr&&i.push(a.offsetExpr)}}return!1},"dependsOn"),r=[t.body];for(;r.length>0;){let o=r.pop();if(!(!o||typeof o!="object")){if(o.type==="LetStmtNode"&&o.variable&&s(o.value)&&this._ensureLocal(o.variable.name+"_vlet","v128"),o.body&&r.push(o.body),o.stmts)for(let i of o.stmts)r.push(i);o.thenBody&&r.push(o.thenBody),o.elseBody&&r.push(o.elseBody),o.initBody&&r.push(o.initBody)}}}_prescanVecLocals(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="CallExternNode"&&s.externName&&!wn(this._defaultDtype,s.externName)&&s.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,bn(this._defaultDtype));s.args.length>1&&this._ensureLocal("_vtmp2_"+o,"v128"),this._vecTmpCounter++}if(s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);if(s.value&&typeof s.value=="object"&&s.value.type&&e.push(s.value),s.a&&typeof s.a=="object"&&e.push(s.a),s.b&&typeof s.b=="object"&&e.push(s.b),s.args)for(let r of s.args)typeof r=="object"&&e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody)}}}_layoutBuffers(t){let e=0,s=16;this._dynamicBuffers=new Set;let r=f(i=>{e=Math.ceil(e/s)*s,this._bufferOffsets.set(i.name,e);let a=i.numel();!i.shape.some(l=>typeof l!="number"||l<0)&&a>0?e+=a*qr(i.dtype):(this._dynamicBuffers.add(i.name),e+=65536)},"place");for(let[,i]of t.bufferMap)r(i);let o=new Map;this._collectBuffers(t.body,o);for(let[i,a]of o)this._bufferOffsets.has(i)||r(a);this._totalMemBytes=e}_collectBuffers(t,e){let s=[t];for(;s.length>0;){let r=s.pop();if(r){if((r.type==="BufferStoreNode"||r.type==="BufferLoadNode")&&r.buffer&&e.set(r.buffer.name,r.buffer),r.body&&s.push(r.body),r.stmts)for(let o of r.stmts)s.push(o);if(r.thenBody&&s.push(r.thenBody),r.elseBody&&s.push(r.elseBody),r.initBody&&s.push(r.initBody),r.value&&typeof r.value=="object"&&r.value.type&&s.push(r.value),r.reads)for(let o of r.reads)o.buffer&&e.set(o.buffer.name,o.buffer);if(r.writes)for(let o of r.writes)o.buffer&&e.set(o.buffer.name,o.buffer)}}}_scanMathImports(t){let e=[t];for(;e.length>0;){let s=e.pop();if(!(!s||typeof s!="object")){if(s.type==="CallExternNode"&&s.externName){let r=s.externName;if(r!=="sqrt"&&r!=="min"&&r!=="max"){let o=this._mathImportSig(r,s.args.length);this._imports.set(r,o)}}if(s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);if(s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),s.initBody&&e.push(s.initBody),s.value&&typeof s.value=="object"&&s.value.type&&e.push(s.value),s.a&&typeof s.a=="object"&&e.push(s.a),s.b&&typeof s.b=="object"&&e.push(s.b),s.expr&&typeof s.expr=="object"&&e.push(s.expr),s.args)for(let r of s.args)typeof r=="object"&&e.push(r);if(s.indices)for(let r of s.indices)typeof r=="object"&&e.push(r);s.condition&&typeof s.condition=="object"&&e.push(s.condition)}}}_prescanLocals(t){this._waccCounter=0;let e=[t];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="ForNode"){this._ensureLocal(s.loopVar.name,"i32");let r=this._accPatternDtype(s);r&&this._ensureLocal("_wacc_"+ ++this._waccCounter,bn(r)),s.kind===V.VECTORIZED&&this.target.supportsSimd()&&this._prescanVecLocals(s.body)}if(s.type==="BlockNode")for(let r of s.iterVars)r.iterVar&&this._ensureLocal(r.iterVar.name,"i32");if(s.type==="LetStmtNode"&&s.variable){let r=Ns(s.value)||s.variable.dtype||this._defaultDtype;this._ensureLocal(s.variable.name,bn(r))}if(s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),s.initBody&&e.push(s.initBody)}}this._waccCounter=0}_wasmExprDtype(t){if(!t||t.type==="CompareNode"||t.type==="MathOpNode"&&(t.op==="!"||t.op==="&&"||t.op==="||"||t.op==="<"||t.op===">"||t.op==="<="||t.op===">="))return"i32";if(t.type==="VariableNode"){let e=this._locals.get(t.name);if(e)return e==="f32"?"f32":"i32"}return Ns(t)}_fixLetStmtLocals(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="LetStmtNode"&&s.variable&&s.value){let r=Ns(s.value);r&&this._locals.set(s.variable.name,bn(r))}if(s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),s.initBody&&e.push(s.initBody)}}}_accPatternDtype(t){let e=t.body;if(!e||e.type!=="BlockNode")return null;let s=e.body;if(!s||s.type!=="BufferStoreNode")return null;let r=s.value;return!r||r.type!=="MathOpNode"||r.op!=="+"?null:r.a&&r.a.type==="BufferLoadNode"&&r.a.buffer.name===s.buffer.name||r.b&&r.b.type==="BufferLoadNode"&&r.b.buffer.name===s.buffer.name?s.buffer.dtype:null}_inferDtype(t){return t&&t._dtype||Ns(t)}_detectWasmAcc(t){let e=t.body;if(!e||e.type!=="BlockNode")return null;let s=e.body;if(!s||s.type!=="BufferStoreNode")return null;let r=s,o=r.value;if(!o||o.type!=="MathOpNode"||o.op!=="+")return null;let i=null;if(o.a&&o.a.type==="BufferLoadNode"&&o.a.buffer.name===r.buffer.name?i=o.a:o.b&&o.b.type==="BufferLoadNode"&&o.b.buffer.name===r.buffer.name&&(i=o.b),!i)return null;let a=this._indicesKey(r.buffer,r.indices),c=this._indicesKey(i.buffer,i.indices);if(a!==c)return null;let l=r.indices.map(u=>{if(u.type!=="VariableNode")return u;for(let h of e.iterVars)if(h.iterVar&&h.iterVar.name===u.name&&h.binding)return h.binding;return u});return{buf:r.buffer,indices:r.indices,outerIndices:l}}_isAccTarget(t,e){return!this._wasmAcc||t.name!==this._wasmAcc.bufName?!1:this._indicesKey(t,e)===this._indicesKey({name:this._wasmAcc.bufName,shape:t.shape,strides:t.strides},this._wasmAcc.indices)}_indicesKey(t,e){let s=[];for(let r=0;r<e.length;r++)s.push(this._exprKey(e[r]));return t.name+":"+s.join(",")}_exprKey(t){return t?t.type==="VariableNode"?"$"+t.name:t.type==="IntImmNode"?String(t.value):t.type==="MathOpNode"?"("+this._exprKey(t.a)+t.op+(t.b?this._exprKey(t.b):"")+")":"?":"?"}}});var sN,nN,rN,bg,Qf,wg,Kx=T(()=>{Ct();As();sN=new Set(["!","&&","||"]),nN=new Set(["<",">","<=",">=","==","!="]),rN={"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"},bg=class{static{f(this,"WebGPUKernel")}constructor(t,e,s,r,o,i,a){this.name=t,this.source=e,this.workgroupSize=s,this.dispatchSize=r,this.sharedMemBytes=o,this.params=i,this.bindings=a}},Qf=class{static{f(this,"WebGPUCodegen")}constructor(t){this.target=t,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(t){if(this._indent=0,this._lines=[],this._threadBindings.clear(),this._sharedBuffers=[],this._workgroupSize=[1,1,1],this._dispatchSize=[1,1,1],this._primFunc=t,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,t.type==="LIRFunc"){for(let[w,A]of t.metadata.threadBindings){this._threadBindings.set(w,A);for(let I of A)I.isDynamic||this._applyBindingDim(w,I.extent)}this._sharedBuffers=t.metadata.sharedBuffers}else this._scanBindings(t.body);this._scanStoreTargets(t.body),this._analyzeSharing(t);let s=[],r=[],o=0,i=this._checkF16Usage(t);i&&this._emit("enable f16;"),i&&this._emit("");let a=t.bufferMap.size+(t.shapeParams.length>0?1:0),c=[...t.bufferMap.values()],l=a>6&&c.every(w=>w.numel()>0);this._packedMode=l,this._packedOffsets=null;let u=new Map;{let w=0;for(let[,A]of t.bufferMap)u.set(A.name,w++)}{let w=null;for(let[,A]of t.bufferMap)if(this._storeBuffers.has(A.name)){w=A.dtype;break}this._defaultDtype=w||(c.length?c[0].dtype:"f32")}let h={f32:"f32",i32:"i32",u32:"u32",f16:"f16"};if(l){this._packedOffsets=new Map;let w=f(B=>Math.ceil(B/4)*4,"align4"),A=new Map;for(let[,B]of t.bufferMap){s.push(B.name);let k=this._storeBuffers.has(B.name),N=ls(B.dtype),R=(k?"w:":"r:")+N,O=A.get(R);O||(O={isWrite:k,wt:N,dtype:h[N]||B.dtype,bufs:[],off:0},A.set(R,O));let M=B.numel(),F=(k?"_pw_":"_pr_")+N;this._packedOffsets.set(B.name,{storage:F,offset:O.off}),O.bufs.push({name:B.name,offset:O.off,size:M,dtype:B.dtype,argIndex:u.get(B.name)}),O.off+=w(M)}let I=[...A.values()].sort((B,k)=>B.isWrite===k.isWrite?B.wt.localeCompare(k.wt):B.isWrite?1:-1);for(let B of I){let k=(B.isWrite?"_pw_":"_pr_")+B.wt,N=B.isWrite?"storage, read_write":"storage, read";r.push({index:o,name:k,mode:B.isWrite?"read_write":"read",packed:B.bufs,packedSize:B.off,dtype:B.dtype}),this._emit(`@group(0) @binding(${o}) var<${N}> ${k}: array<${B.wt}>;`),o++}}else for(let[,w]of t.bufferMap){s.push(w.name);let A=this._storeBuffers.has(w.name)?"read_write":"read",I=A==="read_write"?"storage, read_write":"storage, read";r.push({index:o,name:w.name,mode:A,dtype:w.dtype,argIndex:u.get(w.name)}),this._emit(`@group(0) @binding(${o}) var<${I}> ${w.name}: array<${ls(w.dtype)}>;`),o++}if(t.shapeParams.length>0){this._emit(""),this._emit("struct ShapeParams {"),this._indent++;for(let w of t.shapeParams)s.push(w.name),this._emit(`${w.name}: u32,`);this._indent--,this._emit("}"),r.push({index:o,name:"_shapes",mode:"read"}),this._emit(`@group(0) @binding(${o}) var<uniform> _shapes: ShapeParams;`),o++}this._emit("");for(let w of this._sharedBuffers){let A=w.numel();this._emit(`var<workgroup> ${w.name}: array<${ls(w.dtype)}, ${A>0?A:1}>;`)}for(let w of this._promotedBufferDecls)this._emit(`var<workgroup> ${w.name}: array<${ls(w.dtype)}, ${w.size}>;`);for(let w of this._wgPoolDecls)this._emit(`var<workgroup> ${w.pool}: array<${ls(w.dtype)}, ${w.size>0?w.size:1}>;`);(this._sharedBuffers.length>0||this._promotedBufferDecls.length>0||this._wgPoolDecls.length>0)&&this._emit("");let p=[],d=!this._serializeThreads&&this._hasBindingPrefix("threadIdx"),m=!this._serializeThreads&&this._hasBindingPrefix("blockIdx");d&&p.push("@builtin(local_invocation_id) _lid: vec3u"),m&&p.push("@builtin(workgroup_id) _wid: vec3u"),!d&&!m&&p.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 w=0;this._workgroupSize[1]>this._workgroupSize[w]&&(w=1),this._workgroupSize[2]>this._workgroupSize[w]&&(w=2),this._workgroupSize[w]=Math.max(1,this._workgroupSize[w]>>1)}let _=this._workgroupSize[0],y=this._workgroupSize[1],b=this._workgroupSize[2];if(this._emit(`@compute @workgroup_size(${_}, ${y}, ${b})`),this._emit(`fn ${t.name}(${p.join(", ")}) {`),this._indent++,!this._serializeThreads){let w=new Set;for(let[A,I]of this._threadBindings)if(rN[A]){for(let k of I)if(!w.has(k.varName)){let N=this._wgslBuiltinAccess(A);this._emit(`let ${k.varName}: i32 = i32(${N});`),w.add(k.varName)}}}this._assignLocalSlots(t),this._emitMissingLocalDecls(t),this._visitNode(t.body),this._indent--,this._emit("}");let x=this.target,S=[Math.min(this._workgroupSize[0],x.maxBlockDimX),Math.min(this._workgroupSize[1],x.maxBlockDimY),Math.min(this._workgroupSize[2],x.maxBlockDimZ)],v=[Math.min(this._dispatchSize[0],x.maxGridDimX),Math.min(this._dispatchSize[1],x.maxGridDimY),Math.min(this._dispatchSize[2],x.maxGridDimZ)];return new bg(t.name,this._lines.join(`
|
|
7
|
-
`),S,v,this._sharedBuffers.reduce((w,A)=>w+Math.max(A.sizeInBytes(),0),0),s,r)}_checkF16Usage(t){for(let[,e]of t.bufferMap)if(e.dtype==="f16")return!0;return!1}_hasBindingPrefix(t){for(let e of this._threadBindings.keys())if(e.startsWith(t))return!0;return!1}_wgslBuiltinAccess(t){let e=t.indexOf(".");if(e<0)return"_gid.x";let s=t.substring(0,e),r=t.substring(e+1);return s==="threadIdx"?`_lid.${r}`:s==="blockIdx"?`_wid.${r}`:`_gid.${r}`}_scanStoreTargets(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s){if((s.type==="BufferStoreNode"||s.type==="LIRFlatStoreNode")&&this._storeBuffers.add(s.buffer.name),s.type==="LIRAccumulatorNode"&&s.flushStore&&this._storeBuffers.add(s.flushStore.buffer.name),s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),s.loopBody&&e.push(s.loopBody),s.condBody&&e.push(s.condBody),s.initBody&&e.push(s.initBody)}}}_scanBindings(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="ForNode"&&s.kind===V.THREAD_BINDING&&s.threadTag){let r=s.extent.type==="IntImmNode"?s.extent.value:0,o=s.extent.type!=="IntImmNode",i={varName:s.loopVar.name,extent:r,isDynamic:o,extentNode:s.extent};this._threadBindings.has(s.threadTag)?this._threadBindings.get(s.threadTag).push(i):this._threadBindings.set(s.threadTag,[i]),o||this._applyBindingDim(s.threadTag,r)}if(s.type==="AllocateNode"&&s.scope==="shared"&&this._sharedBuffers.push(s.buffer),s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody)}}}_applyBindingDim(t,e){let s=t.indexOf(".");if(s<0)return;let r=t.substring(0,s),o=t.charCodeAt(s+1)-120;o<0||o>2||(r==="threadIdx"?this._workgroupSize[o]=Math.max(this._workgroupSize[o],e):r==="blockIdx"&&(this._dispatchSize[o]=Math.max(this._dispatchSize[o],e)))}_getMaxBindingExtent(t){let e=this._threadBindings.get(t);if(!e)return 0;let s=0;for(let r of e)r.extent>s&&(s=r.extent);return s}_hasRecurrence(t){let e=[t.body];for(;e.length>0;){let s=e.pop();if(!(!s||typeof s!="object")){if(s.type==="SyncThreadsNode")return!0;for(let r of["body","loopBody","condBody","initBody","thenBody","elseBody"])s[r]&&e.push(s[r]);if(s.stmts)for(let r of s.stmts)e.push(r)}}return!1}_analyzeSharing(t){let e=new Set;for(let[,h]of this._threadBindings)for(let p of h)p.extent>0&&e.add(p.extent);let s=this._threadBindings.size>0?this._findCrossThreadBuffers(t):new Set,r=this._threadBindings.size>0?this._findCrossExtentBuffers(t):new Set;this._crossThread=s,this._crossExtent=r;let o=this._hasRecurrence(t);if(!o&&e.size<=1&&s.size===0&&r.size===0)return;let i=this.target.sharedMemoryBytes||16384,a=this._sharedBuffers.reduce((h,p)=>h+Math.max(p.sizeInBytes(),0),0),c=new Set;for(let[,h]of t.bufferMap)c.add(h.name);let l=this._collectPromotionCandidates(t,c);l.sort((h,p)=>(s.has(p.name)?1:0)-(s.has(h.name)?1:0));let u=this._dispatchSize[0]*this._dispatchSize[1]*this._dispatchSize[2];if(o){let h=this.target.maxThreadsPerBlock||256,p=this._workgroupSize[0]*this._workgroupSize[1]*this._workgroupSize[2],d=this._packWorkgroupPool(t,l),m=a+d.bytes<=i;if(u===1&&p<=h&&m){this._needsBarriers=!0,this._wgPoolOffsets=d.offsets,this._wgPoolDecls=d.decls;for(let g of l)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=(s.size>0||r.size>0)&&u>1,this._serializeThreads&&(this._workgroupSize=[1,1,1],this._dispatchSize=[1,1,1]),this._needsBarriers=!this._serializeThreads;for(let h of l){let p=h.size*(xn(h.dtype)||4);a+p<=i&&(this._promotedBuffers.add(h.name),this._promotedBufferDecls.push(h),a+=p)}}_collectPromotionCandidates(t,e){let s=[],r=[t.body];for(;r.length>0;){let c=r.pop();if(c){if(c.type==="AllocateNode"&&c.scope!=="shared"){if(!e.has(c.buffer.name)){let l=c.buffer.numel(),u=l>0?l:this._estimateBufferSize(c.buffer);u>0&&s.push({name:c.buffer.name,dtype:c.buffer.dtype,size:u})}r.push(c.body);continue}if(c.body&&r.push(c.body),c.stmts)for(let l of c.stmts)r.push(l);c.thenBody&&r.push(c.thenBody),c.elseBody&&r.push(c.elseBody)}}let o=new Map;this._scanBufferRefs(t.body,o);let i=new Set;this._scanAllocateNodes(t.body,i);let a=new Set(s.map(c=>c.name));for(let[c,l]of o){if(e.has(c)||i.has(c)||a.has(c))continue;let u=l.numel(),h=u>0?u:this._estimateBufferSize(l);h>0&&(s.push({name:c,dtype:l.dtype,size:h}),a.add(c))}return s}_findRecurrenceBody(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="ForNode"&&s.kind===V.RECURRENCE)return s.body;if(s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody)}}return null}_namesTouchedOutside(t,e,s){let r=new Set,o=f(i=>{if(!(!i||i===e)){(i.type==="BufferLoadNode"||i.type==="BufferStoreNode"||i.type==="LIRFlatLoadNode"||i.type==="LIRFlatStoreNode")&&i.buffer&&s.has(i.buffer.name)&&r.add(i.buffer.name),i.type==="LIRAccumulatorNode"&&(i.flushStore&&i.flushStore.buffer&&s.has(i.flushStore.buffer.name)&&r.add(i.flushStore.buffer.name),i.initLoad&&i.initLoad.buffer&&s.has(i.initLoad.buffer.name)&&r.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 c=i[a];if(c)if(Array.isArray(c))for(let l of c)o(l);else typeof c=="object"&&o(c)}}},"walk");return o(t),r}_packWorkgroupPool(t,e){let s=new Set(e.map(p=>p.name)),r=this._findRecurrenceBody(t.body),{minPos:o,maxPos:i}=r?this._livenessWalk(r,s):{minPos:new Map,maxPos:new Map},a=r?this._namesTouchedOutside(t.body,r,s):s,c=new Map;for(let p of e){c.has(p.dtype)||c.set(p.dtype,[]);let d=a.has(p.name)||!o.has(p.name),m=d?0:o.get(p.name),g=d?Number.MAX_SAFE_INTEGER:i.get(p.name);c.get(p.dtype).push({name:p.name,size:p.size,first:m,last:g})}let l=new Map,u=[],h=0;for(let[p,d]of c){d.sort((y,b)=>y.first-b.first||b.size-y.size);let m=[],g=`_wg_${ls(p)}`,_=0;for(let y of d){let b=m.filter(S=>S.first<=y.last&&y.first<=S.last).map(S=>[S.offset,S.offset+S.size]).sort((S,v)=>S[0]-v[0]),x=0;for(let[S,v]of b){if(x+y.size<=S)break;x<v&&(x=v)}m.push({offset:x,size:y.size,first:y.first,last:y.last}),l.set(y.name,{pool:g,offset:x}),x+y.size>_&&(_=x+y.size)}u.push({pool:g,dtype:p,size:_}),h+=_*(xn(p)||4)}return{offsets:l,decls:u,bytes:h}}_findCrossThreadBuffers(t){let e=new Set;for(let[,a]of t.bufferMap)e.add(a.name);let s=new Set,r=f((a,c)=>{if(c.length===0)return!1;let l=new Set;for(let u of a)this._collectVarNames(u,l);return c.some(u=>l.has(u))},"indexUsesLoopVar"),o=f((a,c)=>{let l=new Set;this._collectVarNames(a,l);for(let u of l)if(c.includes(u))return!0;return!1},"aliasOf"),i=f((a,c)=>{if(!a)return;let l=c;if((a.type==="ForNode"&&a.kind!==V.THREAD_BINDING||a.type==="WhileNode"||a.type==="LIRAccumulatorNode")&&a.loopVar&&(l=[...c,a.loopVar.name]),a.type==="LIRBindingsNode"&&a.bindings)for(let h of a.bindings)!l.includes(h.name)&&o(h.expr,l)&&(l=[...l,h.name]);if(a.type==="BlockNode"&&a.iterVars)for(let h of a.iterVars)h.iterVar&&h.binding&&!l.includes(h.iterVar.name)&&o(h.binding,l)&&(l=[...l,h.iterVar.name]);a.type==="LetStmtNode"&&a.variable&&!l.includes(a.variable.name)&&o(a.value,l)&&(l=[...l,a.variable.name]),a.type==="BufferLoadNode"&&a.buffer&&!e.has(a.buffer.name)&&r(a.indices||[],l)&&s.add(a.buffer.name),a.type==="LIRFlatLoadNode"&&a.buffer&&!e.has(a.buffer.name)&&a.offsetExpr&&r([a.offsetExpr],l)&&s.add(a.buffer.name);for(let h of["body","loopBody","condBody","initBody","thenBody","elseBody","value","a","b","condition","expr","offsetExpr"])a[h]&&i(a[h],l);if(a.stmts)for(let h of a.stmts)i(h,l);if(a.indices)for(let h of a.indices)i(h,l);if(a.args)for(let h of a.args)i(h,l);a.initLoad&&i(a.initLoad,l),a.flushStore&&i(a.flushStore,l)},"walk");return i(t.body,[]),s}_findCrossExtentBuffers(t){let e=new Set;for(let[,u]of t.bufferMap)e.add(u.name);let s=new Map,r=new Map,o=new Set,i=f(u=>u&&(u.type==="FloatImmNode"||u.type==="IntImmNode"),"isConst"),a=f((u,h,p)=>{if(e.has(h))return;let d=u.get(h);d||(d=new Set,u.set(h,d)),d.add(p)},"record"),c=f((u,h)=>{if(!u)return;let p=h;if(u.type==="ForNode"&&u.kind===V.THREAD_BINDING){let d=u.extent&&u.extent.type==="IntImmNode"?u.extent.value:0;d>0&&(p=h*d)}u.type==="BufferStoreNode"&&u.buffer&&(a(s,u.buffer.name,p),i(u.value)||o.add(u.buffer.name)),u.type==="LIRFlatStoreNode"&&u.buffer&&(a(s,u.buffer.name,p),i(u.value)||o.add(u.buffer.name)),u.type==="LIRAccumulatorNode"&&u.flushStore&&u.flushStore.buffer&&(a(s,u.flushStore.buffer.name,p),o.add(u.flushStore.buffer.name)),u.type==="BufferLoadNode"&&u.buffer&&a(r,u.buffer.name,p),u.type==="LIRFlatLoadNode"&&u.buffer&&a(r,u.buffer.name,p);for(let d of["body","loopBody","condBody","initBody","thenBody","elseBody","value","a","b","condition","expr","offsetExpr"])u[d]&&c(u[d],p);if(u.stmts)for(let d of u.stmts)c(d,p);if(u.indices)for(let d of u.indices)c(d,p);if(u.args)for(let d of u.args)c(d,p);u.initLoad&&c(u.initLoad,p),u.flushStore&&c(u.flushStore,p)},"walk");c(t.body,1);let l=new Set;for(let[u,h]of r){if(!o.has(u))continue;let p=s.get(u);if(p){for(let d of h)if(!p.has(d)){l.add(u);break}}}return l}_collectVarNames(t,e){let s=[t];for(;s.length>0;){let r=s.pop();if(!(!r||typeof r!="object")){if(r.type==="VariableNode"){e.add(r.name);continue}for(let o of["a","b","condition","thenBody","elseBody","expr","offsetExpr"])r[o]&&s.push(r[o]);if(r.indices)for(let o of r.indices)s.push(o);if(r.args)for(let o of r.args)s.push(o)}}}_emit(t){this._lines.push(" ".repeat(this._indent)+t)}_visitNode(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s)switch(s.type){case"SeqNode":for(let r=s.stmts.length-1;r>=0;r--)e.push(s.stmts[r]);continue;case"AllocateNode":this._visitAllocateNode(s),e.push(s.body);continue;case"ForNode":this._visitForNode(s);continue;case"BlockNode":this._visitBlockNode(s);continue;case"IfThenElseNode":this._visitIfStmt(s);continue;case"LetStmtNode":this._visitLetStmtNode(s);continue;case"BufferStoreNode":this._visitBufferStoreNode(s);continue;case"LIRFlatStoreNode":this._visitLIRFlatStore(s);continue;case"LIRBindingsNode":this._visitLIRBindings(s);continue;case"LIRAccumulatorNode":this._visitLIRAccumulator(s);continue;case"WhileNode":this._visitWhileNode(s);continue;case"SyncThreadsNode":this._needsBarriers&&(this._emit("storageBarrier();"),this._emit("workgroupBarrier();"));continue;case"EvaluateNode":continue;default:continue}}}_visitForNode(t){if(t.kind===V.THREAD_BINDING){if(this._serializeThreads){let a=t.loopVar.name,c=this._exprToWGSL(t.extent);this._emit(`for (var ${a}: i32 = 0; ${a} < ${c}; ${a} = ${a} + 1) {`),this._indent++,this._visitNode(t.body),this._indent--,this._emit("}");return}let r=t.extent.type==="IntImmNode"?t.extent.value:0,o=t.threadTag,i=this._getMaxBindingExtent(o);if(r>0&&i>0&&r<i){let a=this._wgslBuiltinAccess(o);this._emit(`if (i32(${a}) < ${r}) {`),this._indent++,this._visitNode(t.body),this._indent--,this._emit("}")}else this._visitNode(t.body);this._needsBarriers&&this._emit("workgroupBarrier();");return}let e=t.loopVar.name,s=this._exprToWGSL(t.extent);this._emit(`for (var ${e}: i32 = 0; ${e} < ${s}; ${e} = ${e} + 1) {`),this._indent++,this._visitNode(t.body),this._indent--,this._emit("}")}_visitBlockNode(t){for(let e of t.iterVars)e.iterVar&&e.binding&&this._emit(`let ${e.iterVar.name}: i32 = ${this._exprToWGSL(e.binding)};`);t.initBody&&this._visitNode(t.initBody),this._visitNode(t.body)}_emitMissingLocalDecls(){for(let t of this._slotDecls)t.scalar?this._emit(`var ${t.name}: ${ls(t.dtype)};`):this._emit(`var ${t.name}: array<${ls(t.dtype)}, ${t.size}>;`)}_collectLocalBuffers(t){let e=new Set;for(let[,c]of t.bufferMap)e.add(c.name);let s=new Set;for(let c of this._sharedBuffers)s.add(c.name);let r=new Map,o=f(c=>!e.has(c)&&!s.has(c)&&!this._promotedBuffers.has(c),"isLocal"),i=new Map;this._scanBufferRefs(t.body,i);for(let[c,l]of i)o(c)&&r.set(c,l);let a=[t.body];for(;a.length>0;){let c=a.pop();if(c){if(c.type==="AllocateNode"&&c.scope!=="shared"&&c.buffer&&o(c.buffer.name)&&r.set(c.buffer.name,c.buffer),c.body&&a.push(c.body),c.stmts)for(let l of c.stmts)a.push(l);c.thenBody&&a.push(c.thenBody),c.elseBody&&a.push(c.elseBody),c.loopBody&&a.push(c.loopBody),c.condBody&&a.push(c.condBody),c.initBody&&a.push(c.initBody)}}return r}_assignLocalSlots(t){let e=this._collectLocalBuffers(t);if(e.size===0)return;let{minPos:s,maxPos:r}=this._computeBufferLiveness(t,e),o=[...e.keys()].sort((d,m)=>s.get(d)-s.get(m)||r.get(d)-r.get(m));this._localSlots=new Map;let i=new Map,a=0,c=0,l=this._workgroupSize[0]*this._workgroupSize[1]*this._workgroupSize[2]*(this._dispatchSize[0]*this._dispatchSize[1]*this._dispatchSize[2]),u=this._crossThread||new Set,h=this._crossExtent||new Set,p=f((d,m)=>!this._serializeThreads&&this._threadBindings.size>0&&!u.has(d)&&!h.has(d)&&m.numel()>1&&m.numel()<=l,"scalarEligible");for(let d of o){let m=e.get(d),g=m.numel()>0?m.numel():this._estimateBufferSize(m),_=Math.max(g,1),y=s.get(d),b=r.get(d);if(p(d,m)){let w=`_s${c++}`;this._slotDecls.push({name:w,dtype:m.dtype,size:1,scalar:!0}),this._localSlots.set(d,w),this._scalarSlotNames.add(w);continue}let x=i.get(m.dtype);x||(x=new wg((w,A)=>w.freeAt-A.freeAt),i.set(m.dtype,x));let S=null,v=x.peek();if(v&&v.freeAt<y&&(S=x.pop()),S)_>S.decl.size&&(S.decl.size=_);else{let w={name:`_lt${a++}`,dtype:m.dtype,size:_};this._slotDecls.push(w),S={decl:w}}S.freeAt=b,this._localSlots.set(d,S.decl.name),x.push(S)}}_computeBufferLiveness(t,e){return this._livenessWalk(t.body,e)}_livenessWalk(t,e){let s=new Map,r=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,c=[],l=null,u=0,h=f(d=>{e.has(d)&&(s.has(d)||s.set(d,a),r.set(d,a),c.length>0&&l.add(d))},"touch"),p=f(d=>{if(!d)return;a++;let m=o.has(d.type),g=!1;m&&(c.length===0&&(l=new Set,u=a,g=!0),c.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 _ of i){let y=d[_];if(y)if(Array.isArray(y))for(let b of y)p(b);else typeof y=="object"&&p(y)}if(m&&(c.pop(),g)){let _=a;for(let y of l)s.set(y,Math.min(s.get(y),u)),r.set(y,Math.max(r.get(y),_));l=null}},"walk");return p(t),{minPos:s,maxPos:r}}_scanAllocateNodes(t,e){let s=[t];for(;s.length>0;){let r=s.pop();if(r){if(r.type==="AllocateNode"){e.add(r.buffer.name),s.push(r.body);continue}if(r.body&&s.push(r.body),r.stmts)for(let o of r.stmts)s.push(o);r.thenBody&&s.push(r.thenBody),r.elseBody&&s.push(r.elseBody),r.loopBody&&s.push(r.loopBody),r.condBody&&s.push(r.condBody),r.initBody&&s.push(r.initBody)}}}_scanBufferRefs(t,e){let s=[t];for(;s.length>0;){let r=s.pop();if(r){if((r.type==="BufferLoadNode"||r.type==="BufferStoreNode"||r.type==="LIRFlatLoadNode"||r.type==="LIRFlatStoreNode")&&r.buffer&&e.set(r.buffer.name,r.buffer),r.type==="LIRAccumulatorNode"&&(r.flushStore&&r.flushStore.buffer&&e.set(r.flushStore.buffer.name,r.flushStore.buffer),r.initLoad&&r.initLoad.buffer&&e.set(r.initLoad.buffer.name,r.initLoad.buffer)),r.value&&s.push(r.value),r.body&&s.push(r.body),r.stmts)for(let o of r.stmts)s.push(o);if(r.thenBody&&s.push(r.thenBody),r.elseBody&&s.push(r.elseBody),r.loopBody&&s.push(r.loopBody),r.condBody&&s.push(r.condBody),r.initBody&&s.push(r.initBody),r.indices)for(let o of r.indices)s.push(o);if(r.a&&s.push(r.a),r.b&&s.push(r.b),r.condition&&s.push(r.condition),r.expr&&s.push(r.expr),r.args)for(let o of r.args)s.push(o);r.offsetExpr&&s.push(r.offsetExpr),r.extent&&s.push(r.extent)}}}_estimateBufferSize(t){let e=1;for(let s of t.shape)typeof s=="number"&&s>0?e*=s:e*=1;return e}_visitAllocateNode(t){if(t.scope!=="shared"){if(this._promotedBuffers.has(t.buffer.name)||this._localSlots&&this._localSlots.has(t.buffer.name))return;let e=t.buffer.numel(),s=e>0?e:this._estimateBufferSize(t.buffer);s>0&&this._emit(`var ${t.buffer.name}: array<${ls(t.buffer.dtype)}, ${s}>;`)}}_visitIfStmt(t){this._emit(`if (${this._boolExpr(t.condition)}) {`),this._indent++,this._visitNode(t.thenBody),this._indent--,t.elseBody&&(this._emit("} else {"),this._indent++,this._visitNode(t.elseBody),this._indent--),this._emit("}")}_visitLetStmtNode(t){let e=t.variable.dtype||this._defaultDtype,s=ls(e),r=this._numericExpr(t.value,e);this._emit(`var ${t.variable.name}: ${s} = ${r};`),this._visitNode(t.body)}_visitWhileNode(t){this._visitNode(t.condBody);let e=Array.isArray(t.condVar.shape)?`${this._packedBufAccess(t.condVar.name,"0")} != 0`:t.condVar.name;this._emit(`while (${e}) {`),this._indent++,this._visitNode(t.loopBody),this._visitNode(t.condBody),this._indent--,this._emit("}")}_visitBufferStoreNode(t){let e=this._flatIndex(t.buffer,t.indices),s=this._packedBufAccess(t.buffer.name,e),r=this._numericExpr(t.value,t.buffer.dtype);this._emit(`${s} = ${r};`)}_visitLIRFlatStore(t){let e=this._exprToWGSL(t.offsetExpr),s=this._packedBufAccess(t.buffer.name,e),r=this._numericExpr(t.value,t.buffer.dtype);this._emit(`${s} = ${r};`)}_visitLIRBindings(t){for(let e of t.bindings)this._emit(`let ${e.name}: i32 = ${this._numericExpr(e.expr,"i32")};`);this._visitNode(t.body)}_visitLIRAccumulator(t){let e=t.localName,s=ls(t.dtype||this._defaultDtype);this._emit(`var ${e}: ${s} = ${this._exprToWGSL(t.initLoad)};`);let r=t.loopVar.name,o=this._exprToWGSL(t.extent);this._emit(`for (var ${r}: i32 = 0; ${r} < ${o}; ${r} = ${r} + 1) {`),this._indent++,this._emit(`${e} = (${e} + ${this._exprToWGSL(t.body)});`),this._indent--,this._emit("}");let i=this._exprToWGSL(t.flushStore.offsetExpr),a=this._packedBufAccess(t.flushStore.buffer.name,i);this._emit(`${a} = ${e};`)}_isBoolExpr(t){return t?!!(t.type==="CompareNode"||t.type==="MathOpNode"&&(sN.has(t.op)||nN.has(t.op))):!1}_numericExpr(t,e){if(this._isBoolExpr(t)){let s=e?ls(e):"i32";return s==="f32"?`select(0.0, 1.0, ${this._exprToWGSL(t)})`:`select(${s}(0), ${s}(1), ${this._exprToWGSL(t)})`}return this._exprToWGSL(t)}_boolExpr(t){return this._isBoolExpr(t)?this._exprToWGSL(t):`(${this._exprToWGSL(t)} != 0)`}_exprToWGSL(t){if(!t)return"0";switch(t.type){case"IntImmNode":return String(t.value);case"FloatImmNode":return this._emitFloatLiteral(t.value);case"VariableNode":return this._resolveVariable(t.name);case"BufferLoadNode":return this._packedBufAccess(t.buffer.name,this._flatIndex(t.buffer,t.indices));case"LIRFlatLoadNode":return this._packedBufAccess(t.buffer.name,this._exprToWGSL(t.offsetExpr));case"MathOpNode":{if(!t.b)return t.op==="!"?`(!${this._boolExpr(t.a)})`:`(${t.op}${this._numericExpr(t.a)})`;if(t.op==="&&")return`(${this._boolExpr(t.a)} && ${this._boolExpr(t.b)})`;if(t.op==="||")return`(${this._boolExpr(t.a)} || ${this._boolExpr(t.b)})`;let e=this._numericExpr(t.a),s=this._numericExpr(t.b);return t.op==="//"?`(${e} / ${s})`:t.op==="%"?`(${e} % ${s})`:`(${e} ${t.op} ${s})`}case"CompareNode":return`(${this._numericExpr(t.a)} ${t.toC()} ${this._numericExpr(t.b)})`;case"IfThenElseNode":return`select(${this._exprToWGSL(t.elseBody)}, ${this._exprToWGSL(t.thenBody)}, ${this._boolExpr(t.condition)})`;case"CastNode":return`${ls(t.toDtype)}(${this._exprToWGSL(t.expr)})`;case"CallExternNode":return this._emitExternCall(t);default:return"0"}}_resolveVariable(t){if(this._primFunc&&this._primFunc.shapeParams){for(let e of this._primFunc.shapeParams)if(e.name===t)return`i32(_shapes.${t})`}return t}_emitFloatLiteral(t){return t===1/0?"f32(0x1.fffffep+127)":t===-1/0?"f32(-0x1.fffffep+127)":Number.isInteger(t)?`${t}.0`:String(t)}_emitExternCall(t){let e=t.args.length,s=new Array(e);for(let i=0;i<e;i++)s[i]=this._exprToWGSL(t.args[i]);let r=s.join(", ");if(t.externName==="fmod")return`(${s[0]} % ${s[1]})`;if(t.externName==="erf")return`((select(-1.0, 1.0, ${s[0]} >= 0.0)) * (1.0 - (1.0 / (1.0 + 0.3275911 * abs(${s[0]}))) * (0.254829592 + (1.0 / (1.0 + 0.3275911 * abs(${s[0]}))) * (-0.284496736 + (1.0 / (1.0 + 0.3275911 * abs(${s[0]}))) * (1.421413741 + (1.0 / (1.0 + 0.3275911 * abs(${s[0]}))) * (-1.453152027 + (1.0 / (1.0 + 0.3275911 * abs(${s[0]}))) * 1.061405429)))) * exp(-${s[0]} * ${s[0]})))`;if(t.externName==="log10")return`(log(${s[0]}) * ${1/Math.LN10})`;let o=yw(t.externName);if(o===t.externName&&!bw(t.externName))throw new Error(`WebGPU codegen: unsupported extern function "${t.externName}"`);return`${o}(${r})`}_packedBufAccess(t,e){if(this._wgPoolOffsets&&this._wgPoolOffsets.has(t)){let s=this._wgPoolOffsets.get(t);return s.offset===0?`${s.pool}[${e}]`:`${s.pool}[${s.offset}u + u32(${e})]`}if(this._packedMode&&this._packedOffsets&&this._packedOffsets.has(t)){let s=this._packedOffsets.get(t);return s.offset===0?`${s.storage}[${e}]`:`${s.storage}[${s.offset}u + u32(${e})]`}if(this._localSlots&&this._localSlots.has(t)){let s=this._localSlots.get(t);return this._scalarSlotNames.has(s)?s:`${s}[${e}]`}return`${t}[${e}]`}_flatIndex(t,e){if(e.length===0)return"0";if(e.length===1)return this._exprToWGSL(e[0]);let s=new Array(e.length);for(let r=0;r<e.length;r++){let o=this._exprToWGSL(e[r]),i=t.strides[r];i===1?s[r]=o:typeof i=="number"&&i>=0?s[r]=`${o} * ${i}`:s[r]=`${o} * ${this._computeDynamicStride(t,r)}`}return s.join(" + ")}_computeDynamicStride(t,e){let s=[];for(let r=e+1;r<t.shape.length;r++){let o=t.shape[r];typeof o=="number"&&o>=0?s.push(String(o)):s.push(this._resolveShapeParam(t,r))}return s.length===0?"1":s.join(" * ")}_resolveShapeParam(t,e){if(this._primFunc&&this._primFunc.shapeParamMap){let s=`${t.name}:${e}`,r=this._primFunc.shapeParamMap.get(s);if(r)return`i32(_shapes.${r.name})`}return"1"}},wg=class{static{f(this,"MinHeap")}constructor(t){this._items=[],this._compare=t}get size(){return this._items.length}peek(){return this._items.length>0?this._items[0]:null}push(t){let e=this._items;e.push(t);let s=e.length-1;for(;s>0;){let r=s-1>>1;if(this._compare(e[s],e[r])>=0)break;[e[s],e[r]]=[e[r],e[s]],s=r}}pop(){let t=this._items,e=t[0],s=t.pop();if(t.length>0){t[0]=s;let r=0,o=t.length;for(;;){let i=2*r+1,a=2*r+2,c=r;if(i<o&&this._compare(t[i],t[c])<0&&(c=i),a<o&&this._compare(t[a],t[c])<0&&(c=a),c===r)break;[t[r],t[c]]=[t[c],t[r]],r=c}}return e}}});function Hx(n){let{source:t,name:e,metadata:s}=n,r=[];for(let i=0;i<s.paramCount;i++)r.push(`buf_${i}`);let o=[];return o.push(t),o.push(""),o.push(`// ${e}(${r.map(i=>`/* ${i}: Float32Array */`).join(", ")});`),o.join(`
|
|
8
|
-
`)}var Yx=T(()=>{f(Hx,"buildSnippet")});function at(n){let t=[];do{let e=n&127;n>>>=7,n&&(e|=128),t.push(e)}while(n);return t}function dN(n){let t=[],e=!0;for(;e;){let s=n&127;n>>=7,n===0&&!(s&64)||n===-1&&s&64?e=!1:s|=128,t.push(s)}return t}function mN(n){n=BigInt(n);let t=[],e=!0;for(;e;){let s=Number(n&0x7fn);n>>=7n,n===0n&&!(s&64)||n===-1n&&s&64?e=!1:s|=128,t.push(s)}return t}function tp(n){let t=new TextEncoder().encode(n);return[...at(t.length),...t]}function gN(n){let t=new ArrayBuffer(4);return new Float32Array(t)[0]=n,[...new Uint8Array(t)]}function _N(n){let t=new ArrayBuffer(8);return new Float64Array(t)[0]=n,[...new Uint8Array(t)]}function Un(n,t){for(let e=0;e<t.length;e++)n.push(t[e]);return n}function Fi(n,t){let e=[n];return Un(e,at(t.length)),Un(e,t),e}function Ro(n){let t=[];Un(t,at(n.length));for(let e of n)Un(t,e);return t}function yN(n){let t=[],e=0;for(;e<n.length;){let s=n[e];if(s<=" "){e++;continue}if(s===";"&&n[e+1]===";"){e=n.indexOf(`
|
|
9
|
-
`,e),e<0&&(e=n.length);continue}if(s==="("&&n[e+1]===";"){e=n.indexOf(";)",e+2),e=e<0?n.length:e+2;continue}if(s==="("||s===")"){t.push(s),e++;continue}if(s==='"'){let o=e+1;for(;o<n.length&&n[o]!=='"';)o++;t.push(n.substring(e,o+1)),e=o+1;continue}let r=e;for(;r<n.length&&n[r]>" "&&n[r]!=="("&&n[r]!==")";)r++;t.push(n.substring(e,r)),e=r}return t}function bN(n){let t=0,e=f(()=>n[t++],"eat"),s=f(()=>n[t],"peek"),r=f(g=>{if(e()!==g)throw new Error("expected "+g+" at "+(t-1))},"expect");r("("),r("module");let o=[],i=1,a=256,c="",l=[],u=[],h=[],p=-1,d=-1;for(;s()!==")";){if(s()!=="("){t++;continue}t++;let g=e();if(g==="memory"){for(;s()==="(";)m();s()!==")"&&(i=parseInt(e())||1),s()!==")"&&(a=parseInt(e())||256),r(")")}else if(g==="import"){let _=e().replace(/"/g,""),y=e().replace(/"/g,"");r("("),r("func"),s().startsWith("$")&&t++;let b=[],x=[];for(;s()==="(";){t++;let S=e();if(S==="param")for(;s()!==")";){let v=e();v==="f32"?b.push(kc):v==="f64"?b.push(Bc):v==="i32"?b.push(vc):v==="v128"&&b.push(Ac)}else if(S==="result")for(;s()!==")";){let v=e();v==="f32"?x.push(kc):v==="f64"?x.push(Bc):v==="i32"?x.push(vc):v==="v128"&&x.push(Ac)}r(")")}r(")"),r(")"),o.push({module:_,name:y,params:b,results:x})}else if(g==="func"){for(;s()==="(";){let y=t;t++;let b=e();if(b==="export")c=e().replace(/"/g,""),r(")");else if(b==="param"){for(;s()!==")";){let x=e();x==="i32"?l.push(vc):x==="i64"?l.push(vg):x==="f32"?l.push(kc):x==="f64"?l.push(Bc):x==="v128"&&l.push(Ac)}r(")")}else if(b==="result"){for(;s()!==")";)e();r(")")}else if(b==="local"){for(;s()!==")";){let x=e();x.startsWith("$")?h.push(x.replace("$","")):x==="i32"?u.push(vc):x==="i64"?u.push(vg):x==="f32"?u.push(kc):x==="f64"?u.push(Bc):x==="v128"&&u.push(Ac)}r(")")}else{t=y;break}}p=t;let _=1;for(;_>0;){let y=e();y==="("?_++:y===")"&&_--}d=t-1}else m()}function m(){if(s()!=="("){t++;return}let g=0;do{let _=e();_==="("?g++:_===")"&&g--}while(g>0)}return f(m,"skipSExpr"),{imports:o,memMin:i,memMax:a,funcExportName:c,funcParams:l,funcLocals:u,funcLocalNames:h,bodyTokens:n.slice(p,d)}}function wN(n,t,e){let s=[],r=0,o=f(()=>n[r],"peek"),i=f(()=>n[r++],"eat"),a=[],c=-1;for(let _ of t.values())_>c&&(c=_);function l(_){let y=_.replace("$","");if(t.has(y))return t.get(y);let b=parseInt(y,10);if(!isNaN(b))return b>c&&(c=b),b;let x=c+1;return c=x,t.set(y,x),x}f(l,"localIdx");function u(){return o()&&o().startsWith("$")?i().replace("$",""):""}f(u,"eatLabel");function h(_,y){let b=_.indexOf(y);return b>=0?_.substring(b+y.length):""}f(h,"extractName");function p(){for(;r<n.length&&o()!==")";){let _=o();if(_==="("){r++;let y=i();if(y==="i32.const")s.push(65),s.push(...dN(parseInt(i()))),m(")");else if(y==="i64.const")s.push(66),s.push(...mN(i())),m(")");else if(y==="f32.const")s.push(67),s.push(...gN(parseFloat(i()))),m(")");else if(y==="f64.const")s.push(68),s.push(..._N(parseFloat(i()))),m(")");else if(y==="local.get")s.push(32),s.push(...at(l(i()))),m(")");else if(y==="local.set")s.push(33),s.push(...at(l(i()))),m(")");else if(y==="block"){let b=i().replace("$","");a.push(b),s.push(2,xg),p(),s.push(11),a.pop(),m(")")}else if(y==="loop"){let b=i().replace("$","");a.push(b),s.push(3,xg),p(),s.push(11),a.pop(),m(")")}else if(y==="if"){let b=xg;if(o()==="("){let x=r;if(r++,o()==="result"){i();let S=i();b=S==="f32"?kc:S==="f64"?Bc:S==="v128"?Ac:S==="i64"?vg:vc,m(")")}else r=x}for(s.push(4,b),a.push("_if");r<n.length&&o()!==")"&&o()==="(";){let x=r;r++;let S=i();if(S==="then")p(),m(")");else if(S==="else")s.push(5),p(),m(")");else{r=x;break}}s.push(11),a.pop(),m(")")}else r-=2,g();continue}if(r++,_==="br_if")s.push(13),s.push(...at(d(u())));else if(_==="br")s.push(12),s.push(...at(d(u())));else if(_.startsWith("call")){s.push(16);let y=h(_,"math_")||(o()&&o().startsWith("$")?h(i(),"math_"):""),b=e.get(y);if(b===void 0)throw new Error(`WAT encoder: call to unresolved import '${y}'`);s.push(...at(b))}else _==="local.get"?(s.push(32),s.push(...at(l(u())))):_==="local.set"?(s.push(33),s.push(...at(l(u())))):_.endsWith(".extract_lane")||_.endsWith(".replace_lane")?(s.push(...Sg.get(_)),s.push(parseInt(i(),10))):Sg.has(_)&&s.push(...Sg.get(_))}}f(p,"emitBlock");function d(_){for(let y=a.length-1;y>=0;y--)if(a[y]===_)return a.length-1-y;return 0}f(d,"resolveBr");function m(_){if(i()!==_)throw new Error("expect "+_)}f(m,"expect");function g(){if(o()!=="("){r++;return}let _=0;do{let y=i();y==="("?_++:y===")"&&_--}while(_>0)}return f(g,"skipSExpr"),p(),s}function ep(n){let t=yN(n),e=bN(t),s=new Map,r=e.funcParams.length;for(let w=0;w<e.funcLocalNames.length;w++)s.set(e.funcLocalNames[w],r+w);let o=new Map;for(let w=0;w<e.imports.length;w++)o.set(e.imports[w].name,w);let i=[],a=new Map;function c(w,A){let I=w.join(",")+">"+A.join(",");if(a.has(I))return a.get(I);let B=i.length;return i.push({params:w,results:A}),a.set(I,B),B}f(c,"getType");for(let w of e.imports)w.typeIdx=c(w.params,w.results);let l=c(e.funcParams,[]),u=Fi(aN,Ro(i.map(w=>[hN,...at(w.params.length),...w.params,...at(w.results.length),...w.results]))),h=[];e.imports.length>0&&(h=Fi(cN,Ro(e.imports.map(w=>[...tp(w.module),...tp(w.name),0,...at(w.typeIdx)]))));let p=Fi(uN,Ro([[...at(l)]])),d=Fi(lN,Ro([[1,...at(e.memMin),...at(e.memMax)]])),m=e.imports.length,g=Fi(fN,Ro([[...tp("memory"),2,...at(0)],[...tp(e.funcExportName),0,...at(m)]])),_=[];if(e.funcLocals.length>0){let w=[],A=e.funcLocals[0],I=1;for(let B=1;B<e.funcLocals.length;B++)e.funcLocals[B]===A?I++:(w.push([...at(I),A]),A=e.funcLocals[B],I=1);w.push([...at(I),A]),Un(_,Ro(w))}else _.push(0);let y=wN(e.bodyTokens,s,o),b=[];Un(b,_),Un(b,y),b.push(11);let x=[];Un(x,at(b.length)),Un(x,b);let S=Fi(pN,Ro([x])),v=[];for(let w of[oN,iN,u,h,p,d,g,S])Un(v,w);return new Uint8Array(v)}var oN,iN,aN,cN,uN,lN,fN,pN,vc,vg,Bc,kc,Ac,hN,xg,Sg,Bg=T(()=>{oN=[0,97,115,109],iN=[1,0,0,0],aN=1,cN=2,uN=3,lN=5,fN=7,pN=10,vc=127,vg=126,Bc=124,kc=125,Ac=123,hN=96,xg=64;f(at,"uleb");f(dN,"sleb");f(mN,"slebBig");f(tp,"encStr");f(gN,"encF32");f(_N,"encF64");f(Un,"pushAll");f(Fi,"section");f(Ro,"vec");Sg=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,...at(0),4,0]],["v128.store",[253,...at(11),4,0]],["v128.bitselect",[253,...at(82)]],["v128.and",[253,...at(78)]],["v128.or",[253,...at(80)]],["v128.not",[253,...at(77)]],["f32x4.splat",[253,...at(19)]],["i32x4.splat",[253,...at(17)]],["f32x4.add",[253,...at(228)]],["f32x4.sub",[253,...at(229)]],["f32x4.mul",[253,...at(230)]],["f32x4.div",[253,...at(231)]],["f32x4.neg",[253,...at(225)]],["f32x4.abs",[253,...at(224)]],["f32x4.sqrt",[253,...at(227)]],["f32x4.ceil",[253,...at(103)]],["f32x4.floor",[253,...at(104)]],["f32x4.min",[253,...at(232)]],["f32x4.max",[253,...at(233)]],["f32x4.eq",[253,...at(65)]],["f32x4.ne",[253,...at(66)]],["f32x4.lt",[253,...at(67)]],["f32x4.gt",[253,...at(68)]],["f32x4.le",[253,...at(69)]],["f32x4.ge",[253,...at(70)]],["f32x4.extract_lane",[253,...at(31)]],["f32x4.replace_lane",[253,...at(32)]],["i32x4.add",[253,...at(174)]],["i32x4.sub",[253,...at(177)]],["i32x4.mul",[253,...at(181)]],["i32x4.abs",[253,...at(160)]],["i32x4.min_s",[253,...at(182)]],["i32x4.max_s",[253,...at(184)]],["i32x4.eq",[253,...at(55)]],["i32x4.ne",[253,...at(56)]],["i32x4.lt_s",[253,...at(57)]],["i32x4.gt_s",[253,...at(58)]],["i32x4.le_s",[253,...at(59)]],["i32x4.ge_s",[253,...at(60)]],["i32x4.extract_lane",[253,...at(27)]],["i32x4.replace_lane",[253,...at(28)]]]);f(yN,"tokenize");f(bN,"parseModule");f(wN,"encodeBody");f(ep,"encodeWat")});function Jx(n){let{source:t,name:e,metadata:s}=n,{bufferOffsets:r,imports:o}=s,i=[...r.entries()],a=ep(t),c=[];if(c.push("(async () => {"),c.push(""),c.push("const binary = new Uint8Array(["+a.join(",")+"]);"),c.push(""),o&&o.size>0){c.push("const mathImports = {");for(let[u]of o)u==="fmod"?c.push(" fmod: (a, b) => a % b,"):u==="rsqrt"?c.push(" rsqrt: x => 1 / Math.sqrt(x),"):c.push(` ${u}: Math.${u},`);c.push("};"),c.push("const { instance } = await WebAssembly.instantiate(binary, { math: mathImports });")}else c.push("const { instance } = await WebAssembly.instantiate(binary);");c.push("const memory = instance.exports.memory;"),c.push("");for(let[u,h]of i)c.push(`// ${u}: new Float32Array(memory.buffer, ${h}, N).set(data);`);c.push(""),c.push(`instance.exports.${e}(${i.map(([,u])=>u).join(", ")});`),c.push("");let l=i[i.length-1];return l&&(c.push(`const result = new Float32Array(memory.buffer, ${l[1]}, N);`),c.push("console.log(result);")),c.push("})();"),c.join(`
|
|
10
|
-
`)}var Zx=T(()=>{Bg();f(Jx,"buildSnippet")});function Xx(n){let{source:t,name:e,metadata:s}=n,{bindings:r,dispatchSize:o}=s,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(t)+";"),i.push(""),i.push("const pipeline = device.createComputePipeline({"),i.push(' layout: "auto",'),i.push(" compute: { module: device.createShaderModule({ code: wgsl }), entryPoint: "+JSON.stringify(e)+" }"),i.push("});"),i.push(""),i.push("const entries = [];"),i.push("const gpuBuffers = [];"),i.push("");for(let a of r)a.name==="_shapes"?xN(i,a):a.packed?SN(i,a):a.mode==="read_write"?vN(i,a):BN(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(""),kN(i,r),i.push(""),i.push("gpuBuffers.forEach(b => b.destroy());"),i.push("device.destroy();"),i.push("})();"),i.join(`
|
|
11
|
-
`)}function xN(n,t){n.push(`// binding ${t.index}: uniform (_shapes)`),n.push("{"),n.push(" const shapeData = new Uint32Array([/* shape values */]);"),n.push(" const size = Math.max(Math.ceil(shapeData.byteLength / 16) * 16, 16);"),n.push(" const buf = device.createBuffer({ size, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });"),n.push(" device.queue.writeBuffer(buf, 0, shapeData);"),n.push(" entries.push({ binding: "+t.index+", resource: { buffer: buf } });"),n.push(" gpuBuffers.push(buf);"),n.push("}")}function SN(n,t){let e=t.mode==="read_write",s=e?"GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST":"GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST";if(n.push(`// binding ${t.index}: packed ${t.name} (${t.packed.length} buffers, ${t.packedSize} elems)`),n.push("{"),n.push(` const buf = device.createBuffer({ size: ${t.packedSize*4}, usage: ${s}, mappedAtCreation: true });`),n.push(" const mapped = new Float32Array(buf.getMappedRange());"),e)n.push(" mapped.fill(0);");else for(let r of t.packed)n.push(` // mapped.set(${r.name}_data, ${r.offset}); // size=${r.size}`);n.push(" buf.unmap();"),n.push(" entries.push({ binding: "+t.index+", resource: { buffer: buf } });"),n.push(" gpuBuffers.push(buf);"),n.push("}")}function vN(n,t){n.push(`// binding ${t.index}: output ${t.name}`),n.push("{"),n.push(" const size = N * 4; // N = output element count"),n.push(" const buf = device.createBuffer({ size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, mappedAtCreation: true });"),n.push(" new Float32Array(buf.getMappedRange()).fill(0);"),n.push(" buf.unmap();"),n.push(" entries.push({ binding: "+t.index+", resource: { buffer: buf } });"),n.push(" gpuBuffers.push(buf);"),n.push("}")}function BN(n,t){n.push(`// binding ${t.index}: input ${t.name}`),n.push("{"),n.push(` const data = ${t.name}_data; // Float32Array`),n.push(" const buf = device.createBuffer({ size: Math.max(data.byteLength, 4), usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST, mappedAtCreation: true });"),n.push(" new Float32Array(buf.getMappedRange()).set(data);"),n.push(" buf.unmap();"),n.push(" entries.push({ binding: "+t.index+", resource: { buffer: buf } });"),n.push(" gpuBuffers.push(buf);"),n.push("}")}function kN(n,t){if(t.filter(s=>s.mode==="read_write").length===0){n.push("device.queue.submit([encoder.finish()]);");return}for(let s=0;s<t.length;s++){let r=t[s];if(r.mode==="read_write")if(r.packed)for(let o of r.packed)n.push("{"),n.push(` const rb = device.createBuffer({ size: ${o.size*4}, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST });`),n.push(` encoder.copyBufferToBuffer(gpuBuffers[${s}], ${o.offset*4}, rb, 0, ${o.size*4});`),n.push(" device.queue.submit([encoder.finish()]);"),n.push(" await rb.mapAsync(GPUMapMode.READ);"),n.push(` console.log("${o.name}:", new Float32Array(rb.getMappedRange()));`),n.push(" rb.unmap(); rb.destroy();"),n.push("}");else n.push("{"),n.push(` const src = gpuBuffers[${s}];`),n.push(" const rb = device.createBuffer({ size: src.size, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST });"),n.push(" encoder.copyBufferToBuffer(src, 0, rb, 0, src.size);"),n.push(" device.queue.submit([encoder.finish()]);"),n.push(" await rb.mapAsync(GPUMapMode.READ);"),n.push(` console.log("${r.name}:", new Float32Array(rb.getMappedRange()));`),n.push(" rb.unmap(); rb.destroy();"),n.push("}")}}var Qx=T(()=>{f(Xx,"buildSnippet");f(xN,"emitShapeBinding");f(SN,"emitPackedBinding");f(vN,"emitOutputBinding");f(BN,"emitInputBinding");f(kN,"emitReadback")});function tS(n){let{source:t,name:e,metadata:s}=n,{blockDim:r,gridDim:o,sharedMemBytes:i,params:a}=s,c=[];return c.push(t),c.push(""),c.push("// Launch:"),c.push(`// dim3 block(${r[0]}, ${r[1]}, ${r[2]});`),c.push(`// dim3 grid(${o[0]}, ${o[1]}, ${o[2]});`),i>0?c.push(`// ${e}<<<grid, block, ${i}>>>(${a.join(", ")});`):c.push(`// ${e}<<<grid, block>>>(${a.join(", ")});`),c.join(`
|
|
12
|
-
`)}var eS=T(()=>{f(tS,"buildSnippet")});function sp(n,t){sS.set(n,t),t.runtimeKind&&t.snippet&&nS.set(t.runtimeKind,t.snippet)}function rS(n){return sS.get(n)||null}function oS(n){return nS.get(n)||null}var sS,nS,iS=T(()=>{qn();qx();Ux();jx();Kx();Yx();Zx();Qx();eS();sS=new Map,nS=new Map;f(sp,"registerCodegen");f(rS,"getCodegenEntry");f(oS,"getSnippetBuilder");sp(fe.CPU,{runtimeKind:"js",snippet:Hx,compile(n,t){return{source:new jf(t).generate(n),metadata:{kind:"js",paramCount:n.params.length}}}});sp(fe.WASM,{runtimeKind:"wasm",snippet:Jx,compile(n,t){let e=new Xf(t).generate(n),s={kind:"wasm",memoryPages:e.memoryPages,bufferOffsets:e.bufferOffsets,imports:e.imports,params:e.params,bufferMap:n.bufferMap};return e.parallel&&(s.parallel=e.parallel),{source:e.wat,metadata:s}}});sp(fe.WEBGPU,{runtimeKind:"webgpu",snippet:Xx,compile(n,t){let e=new Qf(t).generate(n);return{source:e.source,metadata:{kind:"webgpu",workgroupSize:e.workgroupSize,dispatchSize:e.dispatchSize,sharedMemBytes:e.sharedMemBytes,params:e.params,bindings:e.bindings}}}});sp(fe.CUDA,{runtimeKind:"cuda",snippet:tS,compile(n,t,e){if(e&&e.matmulBackend==="cublas"&&n.cublasInfo)return{source:"",metadata:{kind:"cuda",cublas:n.cublasInfo,outputIndices:[n.cublasInfo.cIdx]}};let s=new Kf(t).generate(n);return{source:s.source,metadata:{kind:"cuda",blockDim:s.blockDim,gridDim:s.gridDim,sharedMemBytes:s.sharedMemBytes,params:s.params,outputIndices:s.outputIndices,scratch:s.scratch}}}})});function aS(n){let t=[],e=[n.body];for(;e.length>0;){let m=e.pop();if(m){if(m.type==="BlockNode"){t.push(m),e.push(m.body);continue}if(m.body&&e.push(m.body),m.stmts)for(let g of m.stmts)e.push(g);m.thenBody&&e.push(m.thenBody),m.elseBody&&e.push(m.elseBody)}}let s=null;for(let m of t)if(m.name.includes("matmul"))m.reads.length>=2&&m.writes.length>=1&&(s=m);else return null;if(!s)return null;let r=s.reads[0].buffer,o=s.reads[1].buffer,i=s.writes[0].buffer;if(r.dtype!=="f32"||o.dtype!=="f32"||i.dtype!=="f32"||r.shape.length!==2||o.shape.length!==2||i.shape.length!==2)return null;let a=i.shape[0],c=i.shape[1],l=r.shape[1];if(![a,c,l].every(m=>typeof m=="number"))return null;let u=[];for(let[,m]of n.bufferMap)u.push(m.name);let h=u.indexOf(r.name),p=u.indexOf(o.name),d=u.indexOf(i.name);return h<0||p<0||d<0?null:{M:a,N:c,K:l,aIdx:h,bIdx:p,cIdx:d}}var Rc,Wn,Oc=T(()=>{Lx();iS();f(aS,"detectPureMatmul");Rc=class{static{f(this,"CompiledKernel")}constructor(t,e,s,r={}){this.name=t,this.source=e,this.target=s,this.metadata=r}snippet(){let t=oS(this.metadata.kind);if(!t)throw new Error(`No snippet for kind: ${this.metadata.kind}`);return t(this)}},Wn=class{static{f(this,"BackendPipeline")}constructor(t,e={}){this.target=t,this.matmulBackend=e.matmulBackend||"native",this.librarySelector=t.isCPU()?Cx(t):t.isGPU()?Dx(t):null}compile(t){let e=rS(this.target.kind);if(!e)throw new Error(`Unsupported target kind: ${this.target.kind}`);let{source:s,metadata:r}=e.compile(t,this.target,this);return new Rc(t.name,s,this.target,r)}compileAll(t){return t.map(e=>this.compile(e))}}});function np(n,t){cS.set(n,t)}function uS(n){return cS.get(n)||null}var cS,rp=T(()=>{cS=new Map;f(np,"registerMeasurer");f(uS,"getMeasurer")});var lS={};Ft(lS,{runWasmParallel:()=>EN});import{Worker as AN}from"node:worker_threads";import RN from"node:os";function NN(){if(!kg){let n=Math.max(1,Math.min(RN.cpus().length-1,16));kg=new Ag(n)}return kg}async function EN(n,t,e,s,r,o){let i=NN();i._ensureWorkers();let{binary:a,bufferOffsets:c}=n,l=[...c.values()],u=Math.min(l.length,e.length),{extent:h,outputIndices:p}=r,d=new Set(p||[]),m=Math.min(i.numWorkers,Math.max(1,h)),g=Math.ceil(h/m),_=[];for(let w=0;w<u;w++){let A=e[w],B=(A instanceof Float32Array?A.length:0)/h;_.push(B>=1&&Number.isInteger(B)?B:0)}let y=l.slice(0,u);if(s)for(let w of s)y.push(w);let b=new SharedArrayBuffer(4*m),x=a.buffer.slice(a.byteOffset,a.byteOffset+a.byteLength),S=[];for(let w=0;w<m;w++){let A=w*g,I=Math.min(A+g,h);if(A>=h)break;let B=[];for(let N=0;N<u;N++){let R=e[N];R instanceof Float32Array&&B.push({offset:l[N],length:R.length,data:R.buffer.slice(0),fullLength:R.length,elemStart:0})}let k=[...y,A,I];S.push(i.dispatch(w,{wasmBinary:x,mathImportNames:o,bufferEntries:B,callArgs:k,controlBuffer:b,workerIdx:w,outputIndices:p||[]}))}let v=await Promise.all(S);for(let w=0;w<u;w++){if(!d.has(w)&&d.size>0)continue;let A=e[w];if(!(A instanceof Float32Array))continue;let I=_[w];if(I>0)for(let B of v){let k=B.outputs[w];if(!k)continue;let R=B.workerIdx*g,O=Math.min(R+g,h),M=new Float32Array(k),F=R*I,P=O*I;A.set(M.subarray(F,P),F)}else{let B=v.find(k=>k.workerIdx===0);B&&B.outputs[w]&&A.set(new Float32Array(B.outputs[w]))}}}var ON,kg,Ag,fS=T(()=>{ON=`
|
|
13
|
-
const { parentPort } = require('node:worker_threads');
|
|
14
|
-
|
|
15
|
-
const MATH = {
|
|
16
|
-
exp: Math.exp, log: Math.log, sin: Math.sin, cos: Math.cos,
|
|
17
|
-
tan: Math.tan, tanh: Math.tanh, pow: Math.pow,
|
|
18
|
-
fmod: (a, b) => a % b,
|
|
19
|
-
rsqrt: x => 1 / Math.sqrt(x),
|
|
20
|
-
sign: Math.sign, round: Math.round,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
parentPort.on('message', (msg) => {
|
|
24
|
-
const { wasmBinary, mathImportNames, bufferEntries,
|
|
25
|
-
callArgs, controlBuffer, workerIdx, outputIndices } = msg;
|
|
26
|
-
|
|
27
|
-
const compiled = new WebAssembly.Module(new Uint8Array(wasmBinary));
|
|
28
|
-
|
|
29
|
-
const mathImports = {};
|
|
30
|
-
for (const name of mathImportNames) {
|
|
31
|
-
mathImports[name] = MATH[name] || Math[name] || (x => x);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const instance = new WebAssembly.Instance(compiled, { math: mathImports });
|
|
35
|
-
const wasmMem = instance.exports.memory;
|
|
36
|
-
const fn = Object.values(instance.exports).find(v => typeof v === 'function');
|
|
37
|
-
|
|
38
|
-
for (const entry of bufferEntries) {
|
|
39
|
-
new Float32Array(wasmMem.buffer, entry.offset, entry.length).set(new Float32Array(entry.data));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
fn(...callArgs);
|
|
43
|
-
|
|
44
|
-
const outSet = new Set(outputIndices);
|
|
45
|
-
const outputs = [];
|
|
46
|
-
const transferable = [];
|
|
47
|
-
for (let i = 0; i < bufferEntries.length; i++) {
|
|
48
|
-
if (outSet.has(i)) {
|
|
49
|
-
const entry = bufferEntries[i];
|
|
50
|
-
const out = new Float32Array(entry.length);
|
|
51
|
-
out.set(new Float32Array(wasmMem.buffer, entry.offset, entry.length));
|
|
52
|
-
outputs.push(out.buffer);
|
|
53
|
-
transferable.push(out.buffer);
|
|
54
|
-
} else {
|
|
55
|
-
outputs.push(null);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
const ctrl = new Int32Array(controlBuffer);
|
|
59
|
-
parentPort.postMessage({ workerIdx, outputs }, transferable);
|
|
60
|
-
Atomics.store(ctrl, workerIdx, 1);
|
|
61
|
-
Atomics.notify(ctrl, workerIdx);
|
|
62
|
-
});
|
|
63
|
-
`,kg=null;f(NN,"getPool");Ag=class{static{f(this,"WasmThreadPool")}constructor(t){this.numWorkers=t,this._workers=[],this._ready=[]}_ensureWorkers(){if(!(this._workers.length>0))for(let t=0;t<this.numWorkers;t++){let e=new AN(ON,{eval:!0});e.unref(),this._workers.push(e)}}dispatch(t,e){return this._ensureWorkers(),new Promise(s=>{let r=f(o=>{o.workerIdx===t&&(this._workers[t].removeListener("message",r),s(o))},"handler");this._workers[t].on("message",r),this._workers[t].postMessage(e)})}terminate(){for(let t of this._workers)t.terminate();this._workers=[]}};f(EN,"runWasmParallel")});import{readdirSync as Nc,existsSync as Oo}from"fs";import{join as wr,delimiter as pS}from"path";function mS(){let n=[];return process.env.CUDA_PATH&&n.push(process.env.CUDA_PATH),!No&&process.env.CUDA_HOME&&n.push(process.env.CUDA_HOME),n}function gS(){let n=[];if(No){let t="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA";if(Oo(t))for(let e of Nc(t))n.push(wr(t,e))}else{let t="/usr/local";if(Oo(t))for(let e of Nc(t))(e==="cuda"||e.startsWith("cuda-"))&&n.push(wr(t,e))}return n}function TN(n){return No?[wr(n,"bin")]:[wr(n,"lib64"),wr(n,"targets/x86_64-linux/lib")]}function IN(){return No?[]:["/usr/lib/x86_64-linux-gnu","/lib/x86_64-linux-gnu"]}function dS(n){let t=n.match(/\d+/g);return t?t.map(Number):[]}function MN(n,t){let e=dS(n),s=dS(t),r=Math.max(e.length,s.length);for(let o=0;o<r;o++){let i=(e[o]||0)-(s[o]||0);if(i!==0)return i}return 0}function CN(n){let t=process.env[hS]||"";t.split(pS).includes(n)||(process.env[hS]=t?n+pS+t:n)}function jn(n){let{pattern:t,fallback:e,extraDirs:s}=No?n.win:n.linux,r=[];for(let o of[...mS(),...gS()])for(let i of TN(o))r.push(i);for(let o of IN())r.push(o);if(s)for(let o of s())r.push(o);for(let o of r){if(!Oo(o))continue;let i=Nc(o).filter(a=>t.test(a)&&!a.includes(".alt"));if(i.length>0)return CN(o),i.sort(MN),wr(o,i.pop())}return e}function DN(){for(let n of[...mS(),...gS()]){let t=wr(n,"include");if(Oo(t))return t}return No?null:Oo("/usr/local/cuda/include")?"/usr/local/cuda/include":null}function LN(){let n=[],t="C:/Program Files/NVIDIA/CUDNN";if(Oo(t))for(let e of Nc(t).sort().reverse()){let s=wr(t,e,"bin");if(Oo(s))for(let r of Nc(s).filter(o=>o.startsWith("12.")).sort().reverse())n.push(wr(s,r,"x64"))}return n}var No,hS,op,_S,yS,ip,bS,wS,Ec=T(()=>{No=process.platform==="win32",hS=No?"PATH":"LD_LIBRARY_PATH";f(mS,"envRoots");f(gS,"toolkitRoots");f(TN,"libDirs");f(IN,"systemDirs");f(dS,"versionKey");f(MN,"compareVersion");f(CN,"prependSearchPath");f(jn,"loadCudaLib");f(DN,"resolveIncludeDir");op=DN(),_S={win:{pattern:/^nvcuda\.dll$/,fallback:"nvcuda.dll"},linux:{pattern:/^libcuda\.so(\.\d+)*$/,fallback:"libcuda.so.1"}},yS={win:{pattern:/^nvrtc64_\d+_\d+\.dll$/,fallback:"nvrtc64_120_0.dll"},linux:{pattern:/^libnvrtc\.so(\.\d+)*$/,fallback:"libnvrtc.so.12"}},ip={win:{pattern:/^cudart64_\d+\.dll$/,fallback:"cudart64_12.dll"},linux:{pattern:/^libcudart\.so(\.\d+)*$/,fallback:"libcudart.so.12"}},bS={win:{pattern:/^cublas64_\d+\.dll$/,fallback:"cublas64_12.dll"},linux:{pattern:/^libcublas\.so(\.\d+)*$/,fallback:"libcublas.so.12"}};f(LN,"winCudnnDirs");wS={win:{pattern:/^cudnn64_9\.dll$/,fallback:"cudnn64_9.dll",extraDirs:LN},linux:{pattern:/^libcudnn\.so(\.\d+)*$/,fallback:"libcudnn.so.9"}}});import xS from"koffi";function Ot(n,t){if(t!==0)throw new Error("CUDA driver error "+t+" in "+n)}function BS(n){let t=[0n];xr.getProgramLogSize(n,t);let e=Number(t[0]);if(e<=1)return"";let s=new Uint8Array(e);xr.getProgramLog(n,s);let r="";for(let o of s){if(o===0)break;r+=String.fromCharCode(o)}return r}var Gt,Eo,gt,xr,SS,vS,Kn=T(()=>{Ec();Gt=xS.load(jn(_S)),Eo=xS.load(jn(yS)),gt={init:Gt.func("int cuInit(uint)"),deviceGet:Gt.func("int cuDeviceGet(_Out_ int *dev, int ordinal)"),deviceGetAttribute:Gt.func("int cuDeviceGetAttribute(_Out_ int *pi, int attrib, int dev)"),primaryCtxRetain:Gt.func("int cuDevicePrimaryCtxRetain(_Out_ void **pctx, int dev)"),primaryCtxRelease:Gt.func("int cuDevicePrimaryCtxRelease_v2(int dev)"),ctxSynchronize:Gt.func("int cuCtxSynchronize()"),moduleLoadData:Gt.func("int cuModuleLoadData(_Out_ void **mod, void *image)"),moduleGetFunction:Gt.func("int cuModuleGetFunction(_Out_ void **func, void *mod, str name)"),memGetInfo:Gt.func("int cuMemGetInfo_v2(_Out_ uint64 *free, _Out_ uint64 *total)"),memAlloc:Gt.func("int cuMemAlloc_v2(_Out_ uint64 *dptr, size_t bytes)"),memFree:Gt.func("int cuMemFree_v2(uint64 dptr)"),memcpyHtoD:Gt.func("int cuMemcpyHtoD_v2(uint64 dst, void *src, size_t n)"),memcpyDtoH:Gt.func("int cuMemcpyDtoH_v2(void *dst, uint64 src, size_t n)"),memcpyDtoD:Gt.func("int cuMemcpyDtoD_v2(uint64 dst, uint64 src, size_t n)"),memcpyHtoDAsync:Gt.func("int cuMemcpyHtoDAsync_v2(uint64 dst, void *src, size_t n, void *stream)"),memcpyDtoHAsync:Gt.func("int cuMemcpyDtoHAsync_v2(void *dst, uint64 src, size_t n, void *stream)"),memcpyDtoDAsync:Gt.func("int cuMemcpyDtoDAsync_v2(uint64 dst, uint64 src, size_t n, void *stream)"),memsetD8:Gt.func("int cuMemsetD8_v2(uint64 dst, uint8 uc, size_t n)"),memsetD8Async:Gt.func("int cuMemsetD8Async(uint64 dst, uint8 uc, size_t n, void *stream)"),launchKernel:Gt.func("int cuLaunchKernel(void *f, uint gx, uint gy, uint gz, uint bx, uint by, uint bz, uint shmem, void *stream, void **params, void **extra)"),streamCreate:Gt.func("int cuStreamCreate(_Out_ void **stream, uint flags)"),streamSynchronize:Gt.func("int cuStreamSynchronize(void *stream)"),ctxSetCurrent:Gt.func("int cuCtxSetCurrent(void *ctx)"),streamBeginCapture:Gt.func("int cuStreamBeginCapture_v2(void *stream, int mode)"),streamEndCapture:Gt.func("int cuStreamEndCapture(void *stream, _Out_ void **graph)"),graphInstantiate:Gt.func("int cuGraphInstantiateWithFlags(_Out_ void **exec, void *graph, uint64 flags)"),graphLaunch:Gt.func("int cuGraphLaunch(void *exec, void *stream)"),graphExecDestroy:Gt.func("int cuGraphExecDestroy(void *exec)"),graphDestroy:Gt.func("int cuGraphDestroy(void *graph)")},xr={createProgram:Eo.func("int nvrtcCreateProgram(_Out_ void **prog, str src, str name, int n, void *h, void *inc)"),compileProgram:Eo.func("int nvrtcCompileProgram(void *prog, int n, str *opts)"),destroyProgram:Eo.func("int nvrtcDestroyProgram(void **prog)"),getPTXSize:Eo.func("int nvrtcGetPTXSize(void *prog, _Out_ size_t *sz)"),getPTX:Eo.func("int nvrtcGetPTX(void *prog, _Out_ uint8_t *ptx)"),getProgramLogSize:Eo.func("int nvrtcGetProgramLogSize(void *prog, _Out_ size_t *sz)"),getProgramLog:Eo.func("int nvrtcGetProgramLog(void *prog, _Out_ uint8_t *log)")},SS=75,vS=76;f(Ot,"checkCU");f(BS,"readProgramLog")});function Dt(){if(Vi)return Ot("cuCtxSetCurrent",gt.ctxSetCurrent(Vi.ctx)),Vi;Ot("cuInit",gt.init(0));let n=[0];Ot("cuDeviceGet",gt.deviceGet(n,0));let t=[0],e=[0];Ot("cuDeviceGetAttribute",gt.deviceGetAttribute(t,SS,n[0])),Ot("cuDeviceGetAttribute",gt.deviceGetAttribute(e,vS,n[0]));let s=[null];Ot("cuDevicePrimaryCtxRetain",gt.primaryCtxRetain(s,n[0])),Ot("cuCtxSetCurrent",gt.ctxSetCurrent(s[0]));let r=[null];Ot("cuStreamCreate",gt.streamCreate(r,0));let o=[0n],i=[0n];return Ot("cuMemGetInfo",gt.memGetInfo(o,i)),Vi={dev:n[0],ctx:s[0],stream:r[0],arch:"sm_"+t[0]+e[0],totalMem:Number(i[0])},process.on("exit",()=>{try{gt.primaryCtxRelease(Vi.dev)}catch{}}),Vi}var Vi,Hn=T(()=>{Kn();Vi=null;f(Dt,"getDevice")});function ap(n){let t=2166136261;for(let e=0;e<n.length;e++)t^=n.charCodeAt(e),t=Math.imul(t,16777619);return(t>>>0).toString(16)}function cp(n,t){let e=ap(n),s=AS.get(e);if(s)return s;let{arch:r}=Dt(),o="";n.includes("__half")&&(o+=`#include <cuda_fp16.h>
|
|
64
|
-
`),n.includes("__nv_bfloat16")&&(o+=`#include <cuda_bf16.h>
|
|
65
|
-
`),/u?int(8|16|64)_t/.test(n)&&(o+=zN),/mma_sync|wmma::|fragment</.test(n)&&(o+=`#include <mma.h>
|
|
66
|
-
using namespace nvcuda::wmma;
|
|
67
|
-
`),n.includes("__pipeline_memcpy_async")&&(o+=`#include <cuda_pipeline.h>
|
|
68
|
-
`);let i=o+PN+`extern "C" {
|
|
69
|
-
`+n+`
|
|
70
|
-
}
|
|
71
|
-
`,a=[null];Ot("nvrtcCreateProgram",xr.createProgram(a,i,t+".cu",0,null,null));let c=["--gpu-architecture="+r];if(op&&c.push("--include-path="+op),xr.compileProgram(a[0],c.length,c)!==0){let p=BS(a[0]);throw xr.destroyProgram(a),new Error("NVRTC compile failed for kernel "+t+`:
|
|
72
|
-
`+p+`
|
|
73
|
-
--- source ---
|
|
74
|
-
`+n)}let u=[0n];Ot("nvrtcGetPTXSize",xr.getPTXSize(a[0],u));let h=new Uint8Array(Number(u[0]));return Ot("nvrtcGetPTX",xr.getPTX(a[0],h)),xr.destroyProgram(a),AS.set(e,h),h}function Rg(n){let t=RS.get(n);return t||(t=Yn(n.source,n.name),RS.set(n,t),t)}function Yn(n,t){let e=ap(n)+":"+t,s=kS.get(e);if(s)return s;let r=cp(n,t),o=[null];Ot("cuModuleLoadData",gt.moduleLoadData(o,r));let i=[null];Ot("cuModuleGetFunction",gt.moduleGetFunction(i,o[0],t));let a={func:i[0],module:o[0]};return kS.set(e,a),a}var kS,PN,zN,AS,RS,Og=T(()=>{Kn();Hn();kS=new Map,PN=`#ifndef INFINITY
|
|
75
|
-
#define INFINITY __int_as_float(0x7f800000)
|
|
76
|
-
#endif
|
|
77
|
-
#ifndef NAN
|
|
78
|
-
#define NAN __int_as_float(0x7fffffff)
|
|
79
|
-
#endif
|
|
80
|
-
`,zN=`typedef signed char int8_t;
|
|
81
|
-
typedef short int16_t;
|
|
82
|
-
typedef int int32_t;
|
|
83
|
-
typedef long long int64_t;
|
|
84
|
-
typedef unsigned char uint8_t;
|
|
85
|
-
typedef unsigned short uint16_t;
|
|
86
|
-
typedef unsigned int uint32_t;
|
|
87
|
-
typedef unsigned long long uint64_t;
|
|
88
|
-
`;f(ap,"hashSource");AS=new Map;f(cp,"compileToPTX");RS=new WeakMap;f(Rg,"getProgramFor");f(Yn,"getProgram")});var NS={};Ft(NS,{acquire:()=>oe,alloc:()=>OS,copyDeviceToHost:()=>Jn,copyDeviceToHostAsync:()=>Ng,copyHostToDevice:()=>Zr,copyHostToDeviceAsync:()=>up,drainPool:()=>lp,free:()=>Sr,release:()=>be});function OS(n){let t=[0n];return Ot("cuMemAlloc",gt.memAlloc(t,n)),t[0]}function Zr(n,t){Ot("cuMemcpyHtoD",gt.memcpyHtoD(n,t,t.byteLength))}function Jn(n,t){Ot("cuMemcpyDtoH",gt.memcpyDtoH(n,t,n.byteLength))}function up(n,t){Ot("cuMemcpyHtoDAsync",gt.memcpyHtoDAsync(n,t,t.byteLength,Dt().stream))}function Ng(n,t){Ot("cuMemcpyDtoHAsync",gt.memcpyDtoHAsync(n,t,n.byteLength,Dt().stream))}function Sr(n){gt.memFree(n)}function oe(n){let t=Tc.get(n);return t&&t.length>0?t.pop():OS(n)}function be(n,t){let e=Tc.get(t);e||(e=[],Tc.set(t,e)),e.push(n)}function lp(){let n=0;for(let t of Tc.values())for(let e of t)Sr(e),n++;return Tc.clear(),n}var Tc,To=T(()=>{Kn();Hn();f(OS,"alloc");f(Zr,"copyHostToDevice");f(Jn,"copyDeviceToHost");f(up,"copyHostToDeviceAsync");f(Ng,"copyDeviceToHostAsync");f(Sr,"free");Tc=new Map;f(oe,"acquire");f(be,"release");f(lp,"drainPool")});function Tg(n){ES=n}function Ke(){return ES}function fp(n){TS=n}function is(){return TS}function Gi(n){IS=n}function MS(){return IS}function CS(n){Eg=n}function Ig(){Eg&&Eg()}var ES,Eg,TS,IS,vr=T(()=>{ES=!1,Eg=null,TS=!1,IS=!1;f(Tg,"setEagerDeferred");f(Ke,"isEagerDeferred");f(fp,"setEagerCapturing");f(is,"isEagerCapturing");f(Gi,"setCudaGraphArmed");f(MS,"isCudaGraphArmed");f(CS,"setEagerFlushHook");f(Ig,"eagerFlush")});function $N(n){let t=Buffer.alloc(8);return t.writeBigUInt64LE(BigInt(n)),t}function ys(n){return new pp("f32",n)}function Mg(n){let t=Buffer.alloc(4);return n instanceof pp?(n.kind==="f32"?t.writeFloatLE(n.value):t.writeInt32LE(n.value|0),t):(Number.isInteger(n)?t.writeInt32LE(n|0):t.writeFloatLE(n),t)}function Xs(n,t,e,s,r,o,i=!0){let{stream:a}=Dt(),c=[];for(let l of r)c.push($N(l));for(let l of o)c.push(Mg(l));Ot("cuLaunchKernel",gt.launchKernel(n,t[0],t[1],t[2],e[0],e[1],e[2],s,a,c,null)),i&&!is()&&Ot("cuStreamSynchronize",gt.streamSynchronize(a))}var pp,Cg=T(()=>{Kn();Hn();vr();f($N,"devicePtrParam");pp=class{static{f(this,"TypedScalar")}constructor(t,e){this.kind=t,this.value=e}};f(ys,"f32");f(Mg,"scalarParam");f(Xs,"launch")});import DS from"koffi";function qi(){FN(0)}function PS(){let n=GN();if(n!==0)throw new Error("cudaDeviceSynchronize failed: "+n)}function WN(n){let t=[null],e=VN(t,n);if(e!==0)throw new Error("cudaMalloc failed: "+e);return t[0]}function hp(n,t){let e=LS(n,t,t.byteLength,qN);if(e!==0)throw new Error("cudaMemcpy H2D failed: "+e)}function Lg(n,t){let e=LS(n,t,n.byteLength,UN);if(e!==0)throw new Error("cudaMemcpy D2H failed: "+e)}function Pg(n){return typeof n=="bigint"?n:typeof n=="number"?BigInt(n):DS.address(n)}function zg(n){let t=Dg.get(n);return t&&t.length>0?t.pop():WN(n)}function zS(n,t){let e=Dg.get(t);e||(e=[],Dg.set(t,e)),e.push(n)}var Ic,FN,VN,LS,zF,GN,qN,UN,Dg,$g=T(()=>{Ec();Ic=DS.load(jn(ip)),FN=Ic.func("int cudaSetDevice(int d)"),VN=Ic.func("int cudaMalloc(_Out_ void **p, size_t s)"),LS=Ic.func("int cudaMemcpy(void *dst, void *src, size_t n, int kind)"),zF=Ic.func("int cudaFree(void *p)"),GN=Ic.func("int cudaDeviceSynchronize()"),qN=1,UN=2;f(qi,"setDevice");f(PS,"devSync");f(WN,"devAlloc");f(hp,"devH2D");f(Lg,"devD2H");f(Pg,"devAddr");Dg=new Map;f(zg,"acquireDevice");f(zS,"releaseDevice")});var FS={};Ft(FS,{CU_STREAM_CAPTURE_MODE_GLOBAL:()=>jN,CU_STREAM_CAPTURE_MODE_RELAXED:()=>KN,CU_STREAM_CAPTURE_MODE_THREAD_LOCAL:()=>$S,beginEagerCapture:()=>Fg,destroyEagerGraph:()=>qg,endEagerCapture:()=>dp,isGraphCapturing:()=>HN,replay:()=>Vg,syncStream:()=>Gg});function HN(){return is()}function Fg(n=$S){let{stream:t}=Dt();fp(!0);try{Ot("cuStreamBeginCapture",gt.streamBeginCapture(t,n))}catch(e){throw fp(!1),e}}function dp(){let{stream:n}=Dt(),t=[null],e=gt.streamEndCapture(n,t);fp(!1),Ot("cuStreamEndCapture",e);let s=[null];return Ot("cuGraphInstantiateWithFlags",gt.graphInstantiate(s,t[0],0n)),{graph:t[0],exec:s[0]}}function Vg(n){let{stream:t}=Dt();Ot("cuGraphLaunch",gt.graphLaunch(n,t))}function Gg(){let{stream:n}=Dt();Ot("cuStreamSynchronize",gt.streamSynchronize(n))}function qg(n){n&&(n.exec&>.graphExecDestroy(n.exec),n.graph&>.graphDestroy(n.graph))}var jN,$S,KN,mp=T(()=>{Kn();Hn();vr();jN=0,$S=1,KN=2;f(HN,"isGraphCapturing");f(Fg,"beginEagerCapture");f(dp,"endEagerCapture");f(Vg,"replay");f(Gg,"syncStream");f(qg,"destroyEagerGraph")});var US={};Ft(US,{cublasGemmBatchedDevice:()=>Yg,cublasGemmDevice:()=>Hg,cublasMatmul:()=>nE,cublasMatmulDevice:()=>Sp});import GS from"koffi";function xp(){if(!Mc){let n=[null],t=ZN(n);if(t!==0)throw new Error("cublasCreate failed: "+t);Mc=n[0],XN(Mc,Dt().stream),QN(Mc,3)}return Mc}function Wg(n){let t=Kg.get(n);if(t&&t.length>0)return t.pop();let e=[null];if(YN(e,n))throw new Error("cudaMalloc failed");return e[0]}function jg(n,t){let e=Kg.get(t);e||(e=[],Kg.set(t,e)),e.push(n)}function Sp(n,t,e,s,r,o,i=!1){yp(0);let a=xp(),u=qS(a,i?1:0,0,t,n,e,bp,r,i?e:t,s,e,wp,o,t);if(u!==0)throw new Error("cublasSgemm failed: "+u)}function Hg(n,t,e,s,r,o,i,a){yp(0);let c=xp(),d=tE(c,i?_p:gp,r?_p:gp,t,n,e,bp,o,i?e:t,s,r?n:e,wp,a,t);if(d!==0)throw new Error("cublasSgemm failed: "+d)}function Yg(n,t,e,s,r,o,i,a,c,l,u,h){yp(0);let p=xp(),d=i?_p:gp,m=l?_p:gp,g=i?t:s,y=eE(p,m,d,e,t,s,bp,a,l?s:e,BigInt(c),r,g,BigInt(o),wp,u,e,BigInt(h),n);if(y!==0)throw new Error("cublasSgemmStridedBatched failed: "+y)}function nE(n,t,e,s,r,o,i=!1){yp(0);let a=xp(),c=Wg(s.byteLength),l=Wg(r.byteLength),u=Wg(o.byteLength);try{Ug(c,s,s.byteLength,VS),Ug(l,r,r.byteLength,VS);let d=qS(a,i?1:0,0,t,n,e,bp,l,i?e:t,c,e,wp,u,t);if(d!==0)throw new Error("cublasSgemm failed: "+d);JN(),Ug(o,u,o.byteLength,sE)}finally{jg(c,s.byteLength),jg(l,r.byteLength),jg(u,o.byteLength)}}var Cc,Ui,yp,YN,Ug,HF,JN,ZN,XN,QN,qS,tE,eE,VS,sE,gp,_p,bp,wp,Mc,Kg,vp=T(()=>{Hn();Ec();Cc=GS.load(jn(ip)),Ui=GS.load(jn(bS)),yp=Cc.func("int cudaSetDevice(int d)"),YN=Cc.func("int cudaMalloc(_Out_ void **p, size_t s)"),Ug=Cc.func("int cudaMemcpy(void *dst, void *src, size_t n, int kind)"),HF=Cc.func("int cudaFree(void *p)"),JN=Cc.func("int cudaDeviceSynchronize()"),ZN=Ui.func("int cublasCreate_v2(_Out_ void **h)"),XN=Ui.func("int cublasSetStream_v2(void *h, void *streamId)"),QN=Ui.func("int cublasSetMathMode(void *h, int mode)"),qS=Ui.func("int cublasSgemm_v2(void *h, int ta, int tb, int m, int n, int k, float *alpha, void *A, int lda, void *B, int ldb, float *beta, void *C, int ldc)"),tE=Ui.func("int cublasSgemm_v2(void *h, int ta, int tb, int m, int n, int k, float *alpha, uint64 A, int lda, uint64 B, int ldb, float *beta, uint64 C, int ldc)"),eE=Ui.func("int cublasSgemmStridedBatched(void *h, int ta, int tb, int m, int n, int k, float *alpha, uint64 A, int lda, int64 sa, uint64 B, int ldb, int64 sb, float *beta, uint64 C, int ldc, int64 sc, int batch)"),VS=1,sE=2,gp=0,_p=1,bp=new Float32Array([1]),wp=new Float32Array([0]),Mc=null;f(xp,"handle");Kg=new Map;f(Wg,"acquire");f(jg,"release");f(Sp,"cublasMatmulDevice");f(Hg,"cublasGemmDevice");f(Yg,"cublasGemmBatchedDevice");f(nE,"cublasMatmul")});function KS(n){Jg=n}function HS(){return Jg}function YS(n,t){let e=ap(n)+":"+t,s=WS.get(e);if(s)return s;let r=cp(n,t);qi();let o=[null];Ot("cuModuleLoadData",gt.moduleLoadData(o,r));let i=[null];return Ot("cuModuleGetFunction",gt.moduleGetFunction(i,o[0],t)),WS.set(e,i[0]),i[0]}function rE(n){let t=Buffer.alloc(8);return t.writeBigUInt64LE(n),t}function JS(n,t,e,s,r,o,i=null){let a=[];for(let c of r)a.push(rE(c));for(let c of o)a.push(Mg(c));Ot("cuLaunchKernel",gt.launchKernel(n,t[0],t[1],t[2],e[0],e[1],e[2],s,i,a,null))}function oE(n,t,e){let s=n.inputSlots.concat(n.outputSlots),r=n.kernel.metadata;if(r.cublas){let{M:o,N:i,K:a,aIdx:c,bIdx:l,cIdx:u,transB:h}=r.cublas;Sp(o,i,a,t[s[c]],t[s[l]],t[s[u]],h)}else{let o=s.map(i=>Pg(t[i]));JS(iE(n),r.gridDim,r.blockDim,0,o,n.shapeValues||[],e)}}function iE(n){return ZS.get(n.name)}function aE(n,t,e){qi();let s=Dt().stream,r=new Map;for(let c of e)c.kernel.metadata.cublas||r.set(c.name,YS(c.kernel.source,c.kernel.name));ZS=r;let o=new Set;for(let c of e)for(let l of c.outputSlots)o.add(l);let i=new Set(n.argSlots),a=jS.get(n);if(a)for(let c=0;c<n.numSlots;c++)t[c]&&!o.has(c)&&hp(a.dptr[c],t[c].data);else{let c=new Array(n.numSlots).fill(null);for(let l=0;l<n.numSlots;l++){let u=t[l];u&&(c[l]=zg(Math.max(u.data.byteLength,1)),o.has(l)||hp(c[l],u.data))}Fg();try{for(let u of e)oE(u,c,s);let l=dp();a={dptr:c,graph:l.graph,exec:l.exec}}catch(l){try{dp()}catch{}throw l}jS.set(n,a)}Vg(a.exec),Gg();for(let c=0;c<n.numSlots;c++)a.dptr[c]!==null&&o.has(c)&&i.has(c)&&Lg(t[c].data,a.dptr[c])}async function XS(n,t,e){if(Jg)return aE(n,t,e);for(let c of e)c.kernel.metadata.cublas||cp(c.kernel.source,c.kernel.name);qi();let s=new Map;for(let c of e)c.kernel.metadata.cublas||s.set(c.name,YS(c.kernel.source,c.kernel.name));let r=new Set;for(let c of e)for(let l of c.outputSlots)r.add(l);let o=new Array(n.numSlots).fill(null),i=new Array(n.numSlots).fill(0);for(let c=0;c<n.numSlots;c++){let l=t[c];if(!l)continue;let u=Math.max(l.data.byteLength,1);i[c]=u,o[c]=zg(u),r.has(c)||hp(o[c],l.data)}for(let c of e){let l=c.inputSlots.concat(c.outputSlots),u=c.kernel.metadata;if(u.cublas){let{M:h,N:p,K:d,aIdx:m,bIdx:g,cIdx:_,transB:y}=u.cublas;Sp(h,p,d,o[l[m]],o[l[g]],o[l[_]],y)}else{let h=l.map(p=>Pg(o[p]));JS(s.get(c.name),u.gridDim,u.blockDim,0,h,c.shapeValues||[])}}PS();let a=new Set(n.argSlots);for(let c=0;c<n.numSlots;c++)o[c]!==null&&r.has(c)&&a.has(c)&&Lg(t[c].data,o[c]);for(let c=0;c<n.numSlots;c++)o[c]!==null&&zS(o[c],i[c])}var WS,jS,Jg,ZS,QS=T(()=>{Kn();Og();$g();Hn();mp();vp();Cg();WS=new Map,jS=new WeakMap,Jg=!1;f(KS,"setCudaGraphEnabled");f(HS,"isCudaGraphEnabled");f(YS,"loadFunctionOnPrimary");f(rE,"bufferParam");f(JS,"launchOnPrimary");f(oE,"_runStepGraphed");ZS=null;f(iE,"funcsFor");f(aE,"runCudaPlanGraphed");f(XS,"runCudaPlan")});var ev={};Ft(ev,{clearCapturePins:()=>pE,deviceBufferDptr:()=>hE,deviceBufferForInplace:()=>Bp,deviceBufferForInput:()=>Zn,deviceBufferForOutput:()=>Xr,downloadAndValidate:()=>Xg,downloadStaticAsync:()=>mE,flushDeferred:()=>kp,hostReadHook:()=>n_,isEagerDeferred:()=>Ke,pinResident:()=>Qg,releaseAllResident:()=>s_,setCudaGraphArmed:()=>Gi,setEagerDeferred:()=>Tg,unpinResident:()=>fE,uploadIfStale:()=>Io,uploadStaticAsync:()=>dE});function uE(n){let t=cn.get(n);if(t)return cn.delete(n),cn.set(n,t),t;if(cn.size>=cE){let e=cn.keys().next().value,s=cn.get(e);cn.delete(e),be(s.dptr,s.bytes)}return t={dptr:oe(n.byteLength),bytes:n.byteLength,valid:!1},cn.set(n,t),t}function Io(n){let t=uE(n);return t.valid||(Zr(t.dptr,n),t.valid=!0),t.dptr}function Xg(n,t){Jn(n,t);let e=cn.get(n);e&&(e.valid=!0)}function Qg(n){Lc.add(n)}function fE(n){Lc.delete(n)}function pE(){Pc.clear()}function hE(n){let t=un.get(n);return t?t.dptr:null}function dE(n,t){up(n,t)}function mE(n,t){Ng(n,t)}function gE(){return Zg||(Zg=Math.floor(Dt().totalMem*lE)),Zg}function t_(n,t){if(is())throw new Error("illegal device sync during CUDA graph capture");Ot("cuStreamSynchronize",gt.streamSynchronize(Dt().stream)),Jn(n,t)}function _E(n){let t=gE();for(let[e,s]of un){if(Dc+n<=t)break;Lc.has(e)||Pc.has(e)||(s.hostStale&&t_(e,s.dptr),be(s.dptr,s.bytes),Dc-=s.bytes,un.delete(e))}}function e_(n){let t=un.get(n);return t?(un.delete(n),un.set(n,t)):(is()||_E(n.byteLength),t={dptr:oe(n.byteLength),bytes:n.byteLength,deviceFresh:!1,hostStale:!1},un.set(n,t),Dc+=t.bytes),is()&&Pc.add(n),t}function tv(n,t){is()?up(n,t):Zr(n,t)}function Zn(n){let t=e_(n);return t.deviceFresh||(tv(t.dptr,n),t.deviceFresh=!0),t.dptr}function Xr(n){let t=e_(n);return t.deviceFresh=!0,t.hostStale=!0,t.dptr}function Bp(n){let t=e_(n);return t.deviceFresh||(tv(t.dptr,n),t.deviceFresh=!0),t.hostStale=!0,t.dptr}function kp(){for(let[n,t]of un)Lc.has(n)||Pc.has(n)||(be(t.dptr,t.bytes),Dc-=t.bytes,un.delete(n))}function s_(){for(let[n,t]of un)t.hostStale&&t_(n,t.dptr),be(t.dptr,t.bytes);un.clear(),Dc=0,Lc.clear(),Pc.clear();for(let n of cn.values())be(n.dptr,n.bytes);cn.clear()}function n_(n){let t=un.get(n);if(is()){if(t&&t.hostStale)throw new Error("illegal host read of device-resident tensor during CUDA graph capture");return}let e=cn.get(n);e&&(e.valid=!1),t&&(t.hostStale&&(t_(n,t.dptr),t.hostStale=!1),t.deviceFresh=!1)}var cE,cn,lE,un,Lc,Pc,Dc,Zg,zc=T(()=>{To();Hn();Kn();vr();cE=1024,cn=new Map;f(uE,"_safeEntry");f(Io,"uploadIfStale");f(Xg,"downloadAndValidate");lE=.5,un=new Map,Lc=new Set,Pc=new Set,Dc=0,Zg=0;f(Qg,"pinResident");f(fE,"unpinResident");f(pE,"clearCapturePins");f(hE,"deviceBufferDptr");f(dE,"uploadStaticAsync");f(mE,"downloadStaticAsync");f(gE,"_cap");f(t_,"_syncDownload");f(_E,"_evict");f(e_,"_defEntry");f(tv,"_upload");f(Zn,"deviceBufferForInput");f(Xr,"deviceBufferForOutput");f(Bp,"deviceBufferForInplace");f(kp,"flushDeferred");f(s_,"releaseAllResident");f(n_,"hostReadHook");CS(kp);Tg(!0)});function Jt(n){return bE[n]||Float32Array}function Ap(n,t){if(n===t)return n;let e=Wi.get(n)??0,s=Wi.get(t)??0;return e>=s?n:t}function It(n,t){let e=rv.get(n);e||(e=new Set,rv.set(n,e)),e.add(t)}function Mo(n){return yE(n)}var j,yE,bE,sv,nv,Wi,rv,Qs=T(()=>{kt();j=J,yE=mr,bE=Object.freeze({[j.F16]:Uint16Array,[j.BF16]:Uint16Array,[j.F32]:Float32Array,[j.F64]:Float64Array,[j.I8]:Int8Array,[j.I16]:Int16Array,[j.I32]:Int32Array,[j.I64]:BigInt64Array,[j.UI8]:Uint8Array,[j.BOOL]:Uint8Array,[j.INDEX]:Int32Array});f(Jt,"typedArrayCtor");sv=[j.F16,j.BF16,j.F32,j.F64],nv=[j.UI8,j.I8,j.I16,j.I32,j.I64],Wi=new Map;for(let n=0;n<sv.length;n++)Wi.set(sv[n],100+n);for(let n=0;n<nv.length;n++)Wi.set(nv[n],n);Wi.set(j.BOOL,-1);Wi.set(j.INDEX,50);f(Ap,"resultDtype");rv=new Map;f(It,"_addCastPair");It(j.BOOL,j.I8);It(j.BOOL,j.I16);It(j.BOOL,j.I32);It(j.BOOL,j.I64);It(j.BOOL,j.F16);It(j.BOOL,j.F32);It(j.BOOL,j.F64);It(j.UI8,j.I16);It(j.UI8,j.I32);It(j.UI8,j.I64);It(j.UI8,j.F16);It(j.UI8,j.F32);It(j.UI8,j.F64);It(j.I8,j.I16);It(j.I8,j.I32);It(j.I8,j.I64);It(j.I8,j.F16);It(j.I8,j.F32);It(j.I8,j.F64);It(j.I16,j.I32);It(j.I16,j.I64);It(j.I16,j.F32);It(j.I16,j.F64);It(j.I32,j.I64);It(j.I32,j.F64);It(j.F16,j.F32);It(j.F16,j.F64);It(j.BF16,j.F32);It(j.BF16,j.F64);It(j.BOOL,j.BF16);It(j.UI8,j.BF16);It(j.I8,j.BF16);It(j.F32,j.F64);f(Mo,"dtypeSize")});function Do(){return wE}var pe,Co,Rp,r_,ov,ji,Ki,wE,tn=T(()=>{js();pe=Object.freeze({CPU:"cpu",GPU:"gpu",WASM:"wasm",WEBGPU:"webgpu",META:"meta",LAZY:"lazy"}),Co=class{static{f(this,"Device")}constructor(t,e=0){this.type=t,this.index=e}dispatchKey(){return di(this.type)}equals(t){return this.type===t.type&&this.index===t.index}hash(){let t=2166136261;for(let e=0;e<this.type.length;e++)t=(t^this.type.charCodeAt(e))*16777619&2147483647;return t=(t^this.index)*16777619&2147483647,t}toString(){return this.index===0?this.type:`${this.type}:${this.index}`}},Rp=new Co(pe.CPU),r_=new Co(pe.GPU),ov=new Co(pe.WASM),ji=new Co(pe.WEBGPU),Ki=new Co(pe.META),wE=Rp;f(Do,"getDefaultDevice")});function o_(n){return iv.get(n)||iv.get(pe.CPU)}var Op,Np,iv,Lo,i_=T(()=>{Qs();tn();Op=class{static{f(this,"CPUAllocator")}allocate(t,e){let s=Jt(e);return new s(t)}free(){}},Np=class{static{f(this,"MetaAllocator")}allocate(){return null}free(){}},iv=new Map([[pe.CPU,new Op],[pe.WASM,new Op],[pe.META,new Np],[pe.LAZY,new Np]]);f(o_,"getAllocator");Lo=class n{static{f(this,"StorageImpl")}static#t=null;static setHostReadHook(t){n.#t=t}constructor(t,e,s,r){this._data=t,this._nbytes=e,this._device=s,this._allocator=r,this._refCount=1}static allocate(t,e,s){let r=o_(s.type),o=Mo(e),i=Math.max(Math.ceil(t/o),1),a=r.allocate(i,e);return new n(a,t,s,r)}static fromData(t,e){let s=t?t.byteLength:0,r=o_(e.type);return new n(t,s,e,r)}retain(){return this._refCount++,this}release(){this._refCount--,this._refCount===0&&(this._allocator&&this._data&&this._allocator.free(this._data),this._data=null)}get refCount(){return this._refCount}get data(){return n.#t&&this._data&&n.#t(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(t,e){if(t<=this._nbytes&&this._data)return;let s=Mo(e),r=Math.max(Math.ceil(t/s),1),o=this._allocator.allocate(r,e);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=t}clone(){let t=o_(this._device.type),e=null;if(this._data){n.#t&&n.#t(this._data);let s=this._data.constructor;e=new s(this._data.length),e.set(this._data)}return new n(e,this._nbytes,this._device,t)}}});function He(n){let t=n.length,e=new Array(t),s=1;for(let r=t-1;r>=0;r--)e[r]=s,s*=n[r];return e}function Pe(n){let t=1;for(let e=0;e<n.length;e++)t*=n[e];return t}function a_(n,t){let e=n.length;if(e===0)return!0;let s=1;for(let r=e-1;r>=0;r--){if(n[r]===0)return!0;if(n[r]!==1&&t[r]!==s)return!1;s*=n[r]}return!0}function Hi(n,t){let e=n.length,s=t.length,r=Math.max(e,s),o=new Array(r);for(let i=0;i<r;i++){let a=i<e?n[e-1-i]:1,c=i<s?t[s-1-i]:1;if(a===c)o[r-1-i]=a;else if(a===1)o[r-1-i]=c;else if(c===1)o[r-1-i]=a;else return null}return o}function Ep(n,t){let e=n.length,s=t.length;if(e===1&&s===1)return[];let r=e===1?[1,n[0]]:n,o=s===1?[t[0],1]:t,i=r.length,a=o.length,c=Hi(r.slice(0,i-2),o.slice(0,a-2));if(c===null)return null;let l=[...c,r[i-2],o[a-1]],u=new Set;return s===1&&u.add(l.length-1),e===1&&u.add(l.length-2),l.filter((h,p)=>!u.has(p))}function av(n,t,e){let s=Pe(n),r=-1,o=1,i=new Array(e.length);for(let a=0;a<e.length;a++)if(e[a]===-1){if(r!==-1)return null;r=a,i[a]=-1}else i[a]=e[a],o*=e[a];if(r!==-1){if(o===0||(i[r]=s/o|0,i[r]*o!==s))return null}else if(Pe(i)!==s)return null;return a_(n,t)?{sizes:i,strides:He(i),needsCopy:!1}:{sizes:i,strides:He(i),needsCopy:!0}}var kn=T(()=>{f(He,"computeStrides");f(Pe,"computeNumel");f(a_,"isContiguous");f(Hi,"broadcastShapes");f(Ep,"matmulOutputShape");f(av,"inferReshape")});function cv(n,t){if(n.length<4)return!1;let e=n.length,s=1,r=e===4?xE:SE;if(!r)return!1;for(let o=e-1;o>=0;o--){let i=r[o];if(n[i]===0||t[i]!==s)return!1;s*=n[i]}return!0}var c_,xE,SE,u_=T(()=>{c_=Object.freeze({CONTIGUOUS:"contiguous",CHANNELS_LAST:"channels_last",PRESERVE:"preserve"}),xE=Object.freeze([0,2,3,1]),SE=Object.freeze([0,2,3,4,1]);f(cv,"isChannelsLast")});var he,Br=T(()=>{js();kn();u_();u_();he=class{static{f(this,"TensorImpl")}constructor(t,e,s,r,o,i){this._storage=t,this._storageOffset=e,this._sizes=Object.freeze([...s]),this._strides=Object.freeze(r?[...r]:He(s)),this._dtype=o,this._device=i,this._numel=Pe(s),this._keySet=Ws.fromKey(di(i.type)),this._autogradMeta=null,this._version=0,this._contiguousCache=null}get storage(){return this._storage}get storageOffset(){return this._storageOffset}size(t){let e=t<0?this._sizes.length+t:t;return this._sizes[e]}stride(t){let e=t<0?this._strides.length+t:t;return this._strides[e]}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(t){return!t||t===c_.CONTIGUOUS?(this._contiguousCache===null&&(this._contiguousCache=a_(this._sizes,this._strides)),this._contiguousCache):t===c_.CHANNELS_LAST?cv(this._sizes,this._strides):!1}setSizesAndStrides(t,e){this._sizes=Object.freeze([...t]),this._strides=Object.freeze(e?[...e]:He(t)),this._numel=Pe(t),this._contiguousCache=null}setStorageOffset(t){this._storageOffset=t}bumpVersion(){this._version++}get version(){return this._version}get autogradMeta(){return this._autogradMeta}setAutogradMeta(t){this._autogradMeta=t,this._updateKeySet()}keySet(){return this._keySet}addKeyToSet(t){this._keySet=this._keySet.add(t)}removeKeyFromSet(t){this._keySet=this._keySet.remove(t)}_updateKeySet(){let t=Ws.fromKey(di(this._device.type));if(this._autogradMeta&&this._autogradMeta.requiresGrad){let e=di(this._device.type);t=t.add(iw(e))}this._keySet=t}get isMeta(){return this._storage&&this._storage.isMeta}shallowCopyFrom(t){this._storage=t._storage,this._storageOffset=t._storageOffset,this._sizes=t._sizes,this._strides=t._strides,this._dtype=t._dtype,this._device=t._device,this._numel=t._numel,this._keySet=t._keySet,this._contiguousCache=null}}});var ln,Yi=T(()=>{ln=class{static{f(this,"AutogradMeta")}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(t){this._grad=t}get gradFn(){return this._gradFn}setGradFn(t,e){this._gradFn=t,this._outputNr=e??0}get outputNr(){return this._outputNr}get requiresGrad(){return this._requiresGrad}set requiresGrad(t){this._requiresGrad=t}get retainGrad(){return this._retainGrad}set retainGrad(t){this._retainGrad=t}get isLeaf(){return this._gradFn===null}get versionAtCreation(){return this._versionAtCreation}set versionAtCreation(t){this._versionAtCreation=t}getGradAccumulator(){if(this._gradAccumulator){let t=this._gradAccumulator.deref?this._gradAccumulator.deref():this._gradAccumulator;if(t)return t}return null}setGradAccumulator(t){this._gradAccumulator=typeof WeakRef<"u"?new WeakRef(t):t}clearGrad(){this._grad=null}clearGradFn(){this._gradFn=null,this._outputNr=0}}});function lv(n,t,e,s,r,o){let i=t[r];if(r===t.length-1){let c=new Array(i);if(o)for(let l=0;l<i;l++)c[l]=Uf(o,n[s+l*e[r]]);else for(let l=0;l<i;l++)c[l]=n[s+l*e[r]];return c}let a=new Array(i);for(let c=0;c<i;c++)a[c]=lv(n,t,e,s+c*e[r],r+1,o);return a}function vE(){if(!l_)throw new Error("Autograd engine not initialized. Import autograd/engine.js first.");return l_}function fv(n){l_=n}var uv,zt,l_,Ps=T(()=>{Br();Yi();Pi();uv=new Set(["f16","bf16","i64"]),zt=class n{static{f(this,"Tensor")}constructor(t){this._impl=t}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 t=this._impl.storage;if(!t||t.isMeta)return null;let e=t.data;return this._impl.storageOffset===0&&this._impl.isContiguous()&&e.length===this.numel,e}get requiresGrad(){let t=this._impl.autogradMeta;return t?t.requiresGrad:!1}get gradFn(){let t=this._impl.autogradMeta;return t?t.gradFn:null}get grad(){let t=this._impl.autogradMeta;return t?t.grad:null}set grad(t){this._ensureAutogradMeta().grad=t}get isLeaf(){let t=this._impl.autogradMeta;return t?t.isLeaf:!0}get version(){return this._impl.version}requiresGrad_(t=!0){let e=this._ensureAutogradMeta();return e.requiresGrad=t,this._impl._updateKeySet(),this}retainGrad(){return this._ensureAutogradMeta().retainGrad=!0,this}detach(){let t=new he(this._impl.storage,this._impl.storageOffset,this._impl.sizes(),this._impl.strides(),this._impl.dtype,this._impl.device);return new n(t)}item(){if(this.numel!==1)throw new Error(`item() requires tensor with exactly 1 element, got ${this.numel}`);let e=this._impl.storage.data[this._impl.storageOffset];return uv.has(this._impl.dtype)?Uf(this._impl.dtype,e):e}toArray(){let t=this.shape,e=this.strides,s=this._impl.storage.data,r=this._impl.storageOffset,o=uv.has(this._impl.dtype)?this._impl.dtype:null;return t.length===0?o?Uf(o,s[r]):s[r]:lv(s,t,e,r,0,o)}toString(){return`Tensor(shape=[${this.shape.join(", ")}], dtype=${this.dtype}, device=${this.device})`}*[Symbol.iterator](){let t=this.shape[0];if(t===void 0)throw new Error("Cannot iterate over a 0-d tensor");for(let e=0;e<t;e++)yield this._select(0,e)}_select(t,e){let s=this._impl.sizes(),r=this._impl.strides(),o=[],i=[];for(let l=0;l<s.length;l++)l!==t&&(o.push(s[l]),i.push(r[l]));let a=this._impl.storageOffset+e*r[t],c=new he(this._impl.storage,a,o,i,this._impl.dtype,this._impl.device);return new n(c)}_ensureAutogradMeta(){if(!this._impl.autogradMeta){let t=new ln;t.versionAtCreation=this._impl.version,this._impl.setAutogradMeta(t)}return this._impl.autogradMeta}backward(t){let{backward:e}=vE();e(this,t)}};f(lv,"_toNestedArray");l_=null;f(vE,"require_autograd_engine");f(fv,"setAutogradEngine")});var ps,Po=T(()=>{i_();ps=class n{static{f(this,"Storage")}constructor(t){this._impl=t}static allocate(t,e,s){return new n(Lo.allocate(t,e,s))}static fromData(t,e){return new n(Lo.fromData(t,e))}get impl(){return this._impl}get data(){return this._impl.data}get rawData(){return this._impl.rawData}get nbytes(){return this._impl.nbytes}get device(){return this._impl.device}get isValid(){return this._impl.isValid}get isMeta(){return this._impl.isMeta}retain(){return this._impl.retain(),this}release(){this._impl.release()}clone(){return new n(this._impl.clone())}resize(t,e){this._impl.resize(t,e)}}});function pv(n,t,e,s){let r=n.length,o=e<0?r+e:e,i=s<0?r+s:s,a=[...n],c=[...t];return a[o]=n[i],a[i]=n[o],c[o]=t[i],c[i]=t[o],{sizes:a,strides:c}}function hv(n,t,e){let s=n.length,r=new Array(s),o=new Array(s);for(let i=0;i<s;i++){let a=e[i]<0?s+e[i]:e[i];r[i]=n[a],o[i]=t[a]}return{sizes:r,strides:o}}function dv(n,t,e){let s=n.length,r=e.length,o=r-s,i=new Array(r),a=new Array(r);for(let c=0;c<r;c++){let l=c-o;if(l<0)a[c]=e[c],i[c]=0;else if(n[l]===1&&e[c]!==1)a[c]=e[c],i[c]=0;else if(n[l]===e[c]||e[c]===-1)a[c]=n[l],i[c]=t[l];else throw new Error(`Cannot expand size ${n[l]} to ${e[c]} at dim ${c}`)}return{sizes:a,strides:i}}function mv(n,t,e,s,r,o){let i=n.length,a=e<0?i+e:e,c=n[a],l=s??0,u=r??c,h=o??1;l<0&&(l+=c),u<0&&(u+=c),l=Math.max(0,Math.min(l,c)),u=Math.max(0,Math.min(u,c));let p=Math.max(0,Math.ceil((u-l)/h)),d=l*t[a],m=[...n],g=[...t];return m[a]=p,g[a]=t[a]*h,{sizes:m,strides:g,offsetDelta:d}}function gv(n,t,e){let s=n.length,r=e<0?s+1+e:e,o=[...n],i=[...t],a=r<s?n[r]*t[r]:1;return o.splice(r,0,1),i.splice(r,0,a),{sizes:o,strides:i}}function f_(n,t,e){if(e!=null){let o=n.length,i=e<0?o+e:e;if(n[i]!==1)return{sizes:[...n],strides:[...t]};let a=[...n],c=[...t];return a.splice(i,1),c.splice(i,1),{sizes:a,strides:c}}let s=[],r=[];for(let o=0;o<n.length;o++)n[o]!==1&&(s.push(n[o]),r.push(t[o]));return{sizes:s,strides:r}}function _v(n,t,e,s,r){let o=n.length,i=e<0?o+e:e,a=[...n];a[i]=r;let c=s*t[i];return{sizes:a,strides:[...t],offsetDelta:c}}function yv(n,t,e,s){let r=n.length,o=e<0?r+e:e,i=s<0?n[o]+s:s,a=[],c=[];for(let u=0;u<r;u++)u!==o&&(a.push(n[u]),c.push(t[u]));let l=i*t[o];return{sizes:a,strides:c,offsetDelta:l}}var bv=T(()=>{f(pv,"computeTranspose");f(hv,"computePermute");f(dv,"computeExpand");f(mv,"computeSlice");f(gv,"computeUnsqueeze");f(f_,"computeSqueeze");f(_v,"computeNarrow");f(yv,"computeSelect")});function wv(n){p_=n.GradMode,zo=n.ReshapeBackward,h_=n.TransposeBackward,d_=n.PermuteBackward,$c=n.SliceBackward,m_=n.ExpandBackward,g_=n.SelectBackward,__=n.GradAccumulator}function kr(n,t,e,...s){if(!p_||!p_.isEnabled())return;let r=n._impl.autogradMeta;if(!r||!r.requiresGrad)return;let o=new e(...s);o.saveInputMetadata(0,[...n.shape],n.dtype);let i=r.gradFn;if(i)o.setNextEdge(0,i,r.outputNr||0);else{let c=r.getGradAccumulator();!c&&__&&(c=new __(n),r.setGradAccumulator(c)),c&&o.setNextEdge(0,c,0)}let a=new ln;a.setGradFn(o,0),a.requiresGrad=!0,t._impl.setAutogradMeta(a),t._impl._updateKeySet()}function Ar(n,t,e){return n.tracer.recordOp(t,[n],e)}function BE(n,t){let e=t.reduce((i,a)=>i*a,1),s=1,r=-1,o=n.slice();for(let i=0;i<o.length;i++)o[i]===-1?r=i:s*=o[i];return r>=0&&(o[r]=s===0?0:e/s),o}function Xn(n,t,e,s){let r=new he(n._impl.storage,n._impl.storageOffset+(s||0),t,e,n._impl.dtype,n._impl.device),o=n._impl.autogradMeta;return o&&r.setAutogradMeta(o),new zt(r)}function An(n,t){if(n.isSymbolic&&n.tracer)return Ar(n,"reshape",{new_shape:BE(t,n.shape)});let e=av(n.shape,n.strides,t);if(!e)throw new Error(`Cannot reshape tensor of shape [${n.shape}] to [${t}]`);let s;if(!e.needsCopy)s=Xn(n,e.sizes,e.strides,0);else{let r=n.isContiguous?n:vv(n);s=Xn(r,e.sizes,e.strides,0)}return zo&&kr(n,s,zo),s}function en(n,t,e){if(n.isSymbolic&&n.tracer){let i=n.shape.length,a=t<0?i+t:t,c=e<0?i+e:e;return Ar(n,"transpose",{dim0:a,dim1:c})}let{sizes:s,strides:r}=pv(n.shape,n.strides,t,e),o=Xn(n,s,r,0);return h_&&kr(n,o,h_,t,e),o}function b_(n,t){if(n.isSymbolic&&n.tracer){let o=n.shape.length,i=t.map(a=>a<0?o+a:a);return Ar(n,"permute",{dims:i})}let{sizes:e,strides:s}=hv(n.shape,n.strides,t),r=Xn(n,e,s,0);return d_&&kr(n,r,d_,t),r}function xv(n,t){if(n.isSymbolic&&n.tracer){let o=n.shape.length,i=t.length-o,a=t.map((l,u)=>l===-1?n.shape[u-i]:l),c=Array.from({length:o},(l,u)=>u+i);return Ar(n,"broadcast_in_dim",{result_shape:a,broadcast_dimensions:c})}let{sizes:e,strides:s}=dv(n.shape,n.strides,t),r=Xn(n,e,s,0);return m_&&kr(n,r,m_),r}function Fc(n,t,e,s,r){let o=n.length,i=t<0?o+t:t,a=n[i],c=e??0,l=s??a,u=r??1;c<0&&(c+=a),l<0&&(l+=a),c=Math.max(0,Math.min(c,a)),l=Math.max(0,Math.min(l,a));let h=new Array(o).fill(0),p=n.slice(),d=new Array(o).fill(1);return h[i]=c,p[i]=l,d[i]=u,{starts:h,limits:p,strides:d,d:i}}function kE(n,t,e,s,r){if(n.isSymbolic&&n.tracer){let{starts:l,limits:u,strides:h}=Fc(n.shape,t,e,s,r);return Ar(n,"slice",{starts:l,limits:u,strides:h})}let{sizes:o,strides:i,offsetDelta:a}=mv(n.shape,n.strides,t,e,s,r),c=Xn(n,o,i,a);if($c){let l=Fc(n.shape,t,e,s,r);kr(n,c,$c,l.d,l.starts[l.d],l.limits[l.d],l.strides[l.d])}return c}function sn(n,t){if(n.isSymbolic&&n.tracer){let o=n.shape.length,i=t<0?o+1+t:t,a=n.shape.slice();return a.splice(i,0,1),Ar(n,"reshape",{new_shape:a})}let{sizes:e,strides:s}=gv(n.shape,n.strides,t),r=Xn(n,e,s,0);return zo&&kr(n,r,zo),r}function Vc(n,t){if(n.isSymbolic&&n.tracer){let{sizes:o}=f_(n.shape,n.strides,t);return Ar(n,"reshape",{new_shape:o})}let{sizes:e,strides:s}=f_(n.shape,n.strides,t),r=Xn(n,e,s,0);return zo&&kr(n,r,zo),r}function Gc(n,t,e,s){if(n.isSymbolic&&n.tracer){let{starts:c,limits:l,strides:u}=Fc(n.shape,t,e,e+s,1);return Ar(n,"slice",{starts:c,limits:l,strides:u})}let{sizes:r,strides:o,offsetDelta:i}=_v(n.shape,n.strides,t,e,s),a=Xn(n,r,o,i);if($c){let c=Fc(n.shape,t,e,e+s,1);kr(n,a,$c,c.d,c.starts[c.d],c.limits[c.d],c.strides[c.d])}return a}function as(n,t,e){if(n.isSymbolic&&n.tracer){let a=n.shape.length,c=t<0?a+t:t,l=e<0?n.shape[c]+e:e,{starts:u,limits:h,strides:p}=Fc(n.shape,c,l,l+1,1),d=Ar(n,"slice",{starts:u,limits:h,strides:p}),m=n.shape.filter((g,_)=>_!==c);return d.tracer.recordOp("reshape",[d],{new_shape:m})}let{sizes:s,strides:r,offsetDelta:o}=yv(n.shape,n.strides,t,e),i=Xn(n,s,r,o);if(g_){let a=n.shape.length,c=t<0?a+t:t,l=e<0?n.shape[c]+e:e;kr(n,i,g_,c,l)}return i}function Sv(n){y_=n}function fn(n){let t=n._impl;return n.isContiguous&&t.storageOffset===0&&t.storage.rawData.length===n.numel?n:vv(n)}function vv(n){if(y_){let g=y_(n);if(g)return g}let t=n.shape,e=n.strides,s=n._impl.storage.data,r=n._impl.storageOffset,o=n.numel,i=He(t),a=n._impl.dtype,c=ps.allocate(o*Mo(a),a,n._impl.device),l=c.data,u=t.length,h=new Int32Array(u),p=r;for(let g=0;g<o;g++){l[g]=s[p];for(let _=u-1;_>=0&&(h[_]++,p+=e[_],!(h[_]<t[_]));_--)p-=h[_]*e[_],h[_]=0}let d=new he(c,0,t,i,n._impl.dtype,n._impl.device),m=n._impl.autogradMeta;return m&&d.setAutogradMeta(m),new zt(d)}function Bv(n,t){let e=n.shape,s=e.length;if(t.length<s)throw new Error("repeat: reps length must be >= tensor rank");let r=t.length-s,o=r>0?[...Array(r).fill(1),...e]:e.slice(),i=[],a=[],c=[];for(let l=0;l<o.length;l++)i.push(1,o[l]),a.push(t[l],o[l]),c.push(t[l]*o[l]);return An(xv(An(n,i),a),c)}function AE(n,t){let e=n.shape.length,s=t.length<e?[...Array(e-t.length).fill(1),...t]:t;return Bv(n,s)}function $o(n,t,e=0){let s=n.shape.length,r=e<0?s+e:e,o=n.shape[r],i;if(Array.isArray(t))i=t;else{i=[];for(let l=0;l<o;l+=t)i.push(Math.min(t,o-l))}let a=[],c=0;for(let l of i)a.push(Gc(n,r,c,l)),c+=l;return a}function RE(n,t,e=0){let s=n.shape.length,r=e<0?s+e:e,o=n.shape[r],i=Math.ceil(o/t);return $o(n,i,r)}function kv(n){let t=n.prototype;t.reshape=function(...e){let s=e.length===1&&Array.isArray(e[0])?e[0]:e;return An(this,s)},t.transpose=function(e,s){return en(this,e,s)},t.permute=function(...e){let s=e.length===1&&Array.isArray(e[0])?e[0]:e;return b_(this,s)},t.expand=function(...e){let s=e.length===1&&Array.isArray(e[0])?e[0]:e;return xv(this,s)},t.slice=function(e,s,r,o){return kE(this,e,s,r,o)},t.unsqueeze=function(e){return sn(this,e)},t.squeeze=function(e){return Vc(this,e)},t.narrow=function(e,s,r){return Gc(this,e,s,r)},t.select=function(e,s){return as(this,e,s)},t.repeat=function(...e){let s=e.length===1&&Array.isArray(e[0])?e[0]:e;return Bv(this,s)},t.tile=function(...e){let s=e.length===1&&Array.isArray(e[0])?e[0]:e;return AE(this,s)},t.split=function(e,s=0){return $o(this,e,s)},t.chunk=function(e,s=0){return RE(this,e,s)},t.contiguous=function(){return fn(this)},t.t=function(){if(this.ndim!==2)throw new Error("t() expects a 2D tensor");return en(this,0,1)}}var p_,zo,h_,d_,$c,m_,g_,__,y_,Ye=T(()=>{Ps();Br();Po();Qs();kn();bv();Yi();p_=null,zo=null,h_=null,d_=null,$c=null,m_=null,g_=null,__=null;f(wv,"_initViewAutograd");f(kr,"_wrapWithAutograd");f(Ar,"_traceView");f(BE,"_resolveShape");f(Xn,"_makeView");f(An,"reshape");f(en,"transpose");f(b_,"permute");f(xv,"expand");f(Fc,"_sliceAttrs");f(kE,"slice");f(sn,"unsqueeze");f(Vc,"squeeze");f(Gc,"narrow");f(as,"select");y_=null;f(Sv,"setGpuContiguousHook");f(fn,"contiguous");f(vv,"_copyContiguous");f(Bv,"repeat");f(AE,"tile");f($o,"split");f(RE,"chunk");f(kv,"installViewOps")});var OE,Tp,wt,Qn=T(()=>{OE=0,Tp=class{static{f(this,"Edge")}constructor(t,e){this.node=t,this.inputNr=e}},wt=class{static{f(this,"AutogradNode")}constructor(t){this._id=OE++,this._numInputs=t||0,this._nextEdges=[],this._savedTensors=[],this._inputMetadata=[],this._opArgs=null}setOpArgs(t){this._opArgs=t}opArgs(){return this._opArgs}get id(){return this._id}get numInputs(){return this._numInputs}get nextEdges(){return this._nextEdges}addNextEdge(t,e){this._nextEdges.push(new Tp(t,e))}setNextEdge(t,e,s){for(;this._nextEdges.length<=t;)this._nextEdges.push(null);this._nextEdges[t]=new Tp(e,s)}saveTensor(t){this._savedTensors.push(t)}savedTensors(){return this._savedTensors}saveInputMetadata(t,e,s){this._inputMetadata[t]={shape:e,dtype:s}}inputMetadata(t){return this._inputMetadata[t]||null}apply(t){throw new Error(`${this.name()}.apply() not implemented`)}name(){return this.constructor.name}releaseVariables(){this._savedTensors=[],this._inputMetadata=[]}}});function Rv(n){let t=Av.get(n);return t||(t=Re.findOp(n),t&&Av.set(n,t)),t}var Av,Ov=T(()=>{mi();Av=new Map;f(Rv,"getHandle")});function tr(n,t){let e=t?.dtype??j.F32,s=t?.device??Do(),r=t?.requiresGrad??!1;if(ArrayBuffer.isView(n))return NE(n,t?.shape,e,s,r);if(Array.isArray(n)){let{flat:o,shape:i}=TE(n),a=t?.shape??i;return EE(o,a,e,s,r)}if(typeof n=="number")return Nv(n,e,s,r);throw new Error("Unsupported data type for tensor()")}function Fo(n,t,e,s){let r=s?.device??Do(),o=s?.requiresGrad??!1,i=He(t),a=ps.fromData(n,r),c=s?.offset??0,l=new he(a,c,t,i,e,r),u=new zt(l);return o&&u.requiresGrad_(!0),u}function w_(n,t){let e=t?.dtype??j.F32,s=t?.device??Do();return Nv(n,e,s,t?.requiresGrad??!1)}function Nv(n,t,e,s){let r=Jt(t),o=new r(1);o[0]=x_.has(t)?qf(t,n):n;let i=ps.fromData(o,e),a=new he(i,0,[],[],t,e),c=new zt(a);return s&&c.requiresGrad_(!0),c}function NE(n,t,e,s,r){let o=Jt(e),i=new o(n.length);if(x_.has(e))for(let p=0;p<n.length;p++)i[p]=qf(e,n[p]);else for(let p=0;p<n.length;p++)i[p]=n[p];let a=t??[n.length],c=He(a),l=ps.fromData(i,s),u=new he(l,0,a,c,e,s),h=new zt(u);return r&&h.requiresGrad_(!0),h}function EE(n,t,e,s,r){let o=Jt(e),i=new o(n.length);if(x_.has(e))for(let h=0;h<n.length;h++)i[h]=qf(e,n[h]);else for(let h=0;h<n.length;h++)i[h]=n[h];let a=He(t),c=ps.fromData(i,s),l=new he(c,0,t,a,e,s),u=new zt(l);return r&&u.requiresGrad_(!0),u}function TE(n){let t=[],e=n;for(;Array.isArray(e);)t.push(e.length),e=e[0];let s=[];return Ev(n,s,t,0),{flat:s,shape:t}}function Ev(n,t,e,s){if(s===e.length-1){for(let r=0;r<n.length;r++)t.push(n[r]);return}for(let r=0;r<n.length;r++)Ev(n[r],t,e,s+1)}var x_,Qr=T(()=>{Ps();Br();Po();Qs();tn();kn();Pi();f(tr,"tensor");f(Fo,"fromBuffer");f(w_,"scalar");x_=new Set(["f16","bf16","i64"]);f(Nv,"_fromScalar");f(NE,"_fromTypedArray");f(EE,"_fromFlatArray");f(TE,"_flattenNested");f(Ev,"_recurFlatten")});function hs(n,t){return n&&n._impl?n:w_(n,{dtype:t.dtype,device:t.device})}function dt(n,...t){let e=Rv(n);if(!e)return Re.callOp(n,...t);let s=Fm(t,e.schema);return Re.dispatch(e,s,...t)}function ut(n,t){return dt("add",n,hs(t,n))}function Wt(n,t){return dt("sub",n,hs(t,n))}function et(n,t){return dt("mul",n,hs(t,n))}function Se(n,t){return dt("div",n,hs(t,n))}function Rn(n){return dt("neg",n)}function to(n,t){return dt("pow",n,hs(t,n))}function Tv(n,t){return dt("rem",n,hs(t,n))}function Rr(n,t){return dt("maximum",n,hs(t,n))}function qc(n,t){return dt("minimum",n,hs(t,n))}function On(n){return dt("exp",n)}function Or(n){return dt("log",n)}function Nr(n){return dt("sqrt",n)}function S_(n){return dt("rsqrt",n)}function Ji(n){return dt("abs",n)}function v_(n){return dt("sin",n)}function B_(n){return dt("cos",n)}function pn(n){return dt("tanh",n)}function bs(n){return dt("sigmoid",n)}function Vo(n){return dt("relu",n)}function Uc(n){return dt("gelu",n)}function Wc(n){return dt("silu",n)}function jc(n){return dt("sign",n)}function k_(n){return dt("floor",n)}function A_(n){return dt("ceil",n)}function Zi(n,t){return dt("eq",n,hs(t,n))}function R_(n,t){return dt("ne",n,hs(t,n))}function O_(n,t){return dt("lt",n,hs(t,n))}function Kc(n,t){return dt("le",n,hs(t,n))}function Go(n,t){return dt("gt",n,hs(t,n))}function Hc(n,t){return dt("ge",n,hs(t,n))}function Es(n,t,e){return dt("where",n,t,e)}function N_(n,t,e){return dt("clamp",n,hs(t,n),hs(e,n))}function Xi(n,t,e,s=0){return dt("pad",n,hs(s,n),t,e)}function E_(n,t){return dt("one_hot",n,t)}function qo(n,t,e){return dt("index_select",n,e,t)}function T_(n,t,e){return dt("gather",n,e,t)}function Qi(n,t,e,s){return dt("scatter_add",n,e,s,t)}function ie(n,t,e){return dt("sum",n,t,e)}function Nn(n,t,e){return dt("mean",n,t,e)}function Er(n,t,e){return dt("max",n,t,e)}function I_(n,t,e){return dt("min",n,t,e)}function M_(n,t,e){return dt("argmax",n,t,e)}function C_(n,t,e){return dt("argmin",n,t,e)}function D_(n,t,e){return dt("prod",n,t,e)}function Je(n,t){let e=$_();if(e){let s=e(n,t);if(s!==null)return s}return dt("matmul",n,t)}function L_(n,t){return dt("dot",n,t)}function Ip(n,t){return dt("cat",n,t)}function En(n,t){return dt("stack",n,t)}function P_(n){return dt("clone",n)}function Mp(n,t,e){return dt("transpose",n,t,e)}function er(n,t){return dt("softmax",n,t)}function Yc(n,t){return dt("log_softmax",n,t)}function Iv(n,t,e,s,r){return dt("layer_norm",n,t,e,s,r)}function Mv(n,t,e,s,r,o,i){return dt("batch_norm",n,t,e,s,r,o,i)}function Cv(n,t,e,s,r,o){return dt("conv2d",n,t,e,s,r,o)}function z_(n,t,e,s,r){return dt("pool2d",n,t,e,s,r)}var $t=T(()=>{mi();Ov();Qr();hn();f(hs,"_asTensor");f(dt,"_dispatch");f(ut,"add");f(Wt,"sub");f(et,"mul");f(Se,"div");f(Rn,"neg");f(to,"pow");f(Tv,"remainder");f(Rr,"maximum");f(qc,"minimum");f(On,"exp");f(Or,"log");f(Nr,"sqrt");f(S_,"rsqrt");f(Ji,"abs");f(v_,"sin");f(B_,"cos");f(pn,"tanh");f(bs,"sigmoid");f(Vo,"relu");f(Uc,"gelu");f(Wc,"silu");f(jc,"sign");f(k_,"floor");f(A_,"ceil");f(Zi,"eq");f(R_,"ne");f(O_,"lt");f(Kc,"le");f(Go,"gt");f(Hc,"ge");f(Es,"where");f(N_,"clamp");f(Xi,"pad");f(E_,"one_hot");f(qo,"index_select");f(T_,"gather");f(Qi,"scatter_add");f(ie,"sum");f(Nn,"mean");f(Er,"max");f(I_,"min");f(M_,"argmax");f(C_,"argmin");f(D_,"prod");f(Je,"matmul");f(L_,"dot");f(Ip,"cat");f(En,"stack");f(P_,"clone");f(Mp,"transpose_op");f(er,"softmax");f(Yc,"log_softmax");f(Iv,"layer_norm");f(Mv,"batch_norm");f(Cv,"conv2d");f(z_,"pool2d")});var $v={};Ft($v,{arange:()=>Dv,empty:()=>ze,eye:()=>Lv,full:()=>Lt,linspace:()=>zv,ones:()=>$e,randn:()=>Dp,randperm:()=>Pv,zeros:()=>Nt});function Cp(n){return{dtype:n?.dtype??j.F32,device:n?.device??Do(),requiresGrad:n?.requiresGrad??!1}}function Jc(n,t,e,s){let r=He(n),i=Pe(n)*Mo(t),a=ps.allocate(i,t,e),c=new he(a,0,n,r,t,e),l=new zt(c);return s&&l.requiresGrad_(!0),l}function ze(n,t){let{dtype:e,device:s,requiresGrad:r}=Cp(t);return Jc(n,e,s,r)}function F_(n,t,e){let s=ze(n,t),r=s.data;if(r)return r.fill(e),s;if(s.device&&s.device.type==="meta"){let o=ze(n,{dtype:t?.dtype});return o.data.fill(e),o}return s}function Nt(n,t){return F_(n,t,0)}function $e(n,t){return F_(n,t,1)}function Lt(n,t,e){return F_(n,e,t)}function Dp(n,t){let e=ze(n,t),s=e.data;if(s){let r=s.length;for(let o=0;o<r;o+=2){let i=Math.random()||1e-10,a=Math.random(),c=Math.sqrt(-2*Math.log(i)),l=6.283185307179586*a;s[o]=c*Math.cos(l),o+1<r&&(s[o+1]=c*Math.sin(l))}}return e}function Dv(n,t,e,s){let r,o,i;t===void 0&&e===void 0?(r=0,o=n,i=1):e===void 0?(r=n,o=t,i=1):(r=n,o=t,i=e);let a=Math.max(0,Math.ceil((o-r)/i)),{dtype:c,device:l,requiresGrad:u}=Cp(s),h=Jc([a],c,l,u),p=h.data;if(p)for(let d=0;d<a;d++)p[d]=r+d*i;return h}function Lv(n,t,e){let s=t??n,{dtype:r,device:o,requiresGrad:i}=Cp(e),a=Jc([n,s],r,o,i),c=a.data;if(c){c.fill(0);let l=Math.min(n,s);for(let u=0;u<l;u++)c[u*s+u]=1}return a}function Pv(n,t){let e=t?.dtype??j.I32,s=t?.device??Do(),r=Jc([n],e,s,t?.requiresGrad??!1),o=r.data;for(let i=0;i<n;i++)o[i]=i;for(let i=n-1;i>0;i--){let a=Math.floor(Math.random()*(i+1)),c=o[i];o[i]=o[a],o[a]=c}return r}function zv(n,t,e,s){let{dtype:r,device:o,requiresGrad:i}=Cp(s),a=Jc([e],r,o,i),c=a.data;if(c&&e>0)if(e===1)c[0]=n;else{let l=(t-n)/(e-1);for(let u=0;u<e;u++)c[u]=n+u*l}return a}var ae=T(()=>{Ps();Br();Po();Qs();tn();kn();f(Cp,"_defaultOpts");f(Jc,"_makeTensor");f(ze,"empty");f(F_,"_filled");f(Nt,"zeros");f($e,"ones");f(Lt,"full");f(Dp,"randn");f(Dv,"arange");f(Lv,"eye");f(Pv,"randperm");f(zv,"linspace")});function V_(n,t){return n<0?t+n:n}var Lp,Pp,zp,$p,ta,Fp,G_=T(()=>{Qn();$t();ae();Ye();f(V_,"_normDim");Lp=class extends wt{static{f(this,"CatBackward")}constructor(){super(0)}apply(t){let e=t[0],s=this.opArgs(),r=e.shape.length,o=V_(s&&s.length>1?s[1]??0:0,r),i=[],a=0,c=0;for(;this.inputMetadata(c);){let u=this.inputMetadata(c).shape[o];i.push(fn(Gc(e,o,a,u))),a+=u,c++}return i}},Pp=class extends wt{static{f(this,"StackBackward")}constructor(){super(0)}apply(t){let e=t[0],s=this.opArgs(),r=e.shape.length,o=V_(s&&s.length>1?s[1]??0:0,r),i=[],a=0;for(;this.inputMetadata(a);)i.push(fn(as(e,o,a))),a++;return i}},zp=class extends wt{static{f(this,"ClampBackward")}constructor(){super(3)}apply(t){let e=t[0],[s,r,o]=this.savedTensors(),i=Nt(e.shape,{dtype:e.dtype,device:e.device}),a=Hc(s.detach(),r.detach()),c=Es(a,e,i),l=Kc(s.detach(),o.detach());return[Es(l,c,i),null,null]}},$p=class extends wt{static{f(this,"PadBackward")}constructor(){super(2)}apply(t){let e=t[0],r=this.opArgs()[2],i=this.inputMetadata(0).shape,a=e;for(let c=0;c<i.length;c++){let l=r[c]||0;a=Gc(a,c,l,i[c])}return[fn(a),null]}},ta=class n extends wt{static{f(this,"IndexSelectBackward")}static#t=null;static setGpuBackward(t){n.#t=t}constructor(){super(2)}apply(t){let e=t[0],[,s]=this.savedTensors(),o=this.inputMetadata(0).shape,i=o.length,a=this.opArgs(),c=V_(a&&a.length>2?a[2]??0:0,i);if(n.#t){let A=n.#t(e,s,o,c);if(A)return[A]}let l=Nt(o,{dtype:e.dtype,device:e.device}),u=l._impl.storage.data,h=l.strides,p=fn(e),d=p._impl.storage.data,m=p._impl.storageOffset,g=p.shape,_=p.strides,y=fn(s),b=y._impl.storage.data,x=y._impl.storageOffset,S=g.length,v=new Int32Array(S),w=m;for(let A=0;A<p.numel;A++){let I=0;for(let B=0;B<S;B++){let k=B===c?b[x+v[B]]:v[B];I+=k*h[B]}u[I]+=d[w];for(let B=S-1;B>=0;B--){if(v[B]++,v[B]<g[B]){w+=_[B];break}w-=(g[B]-1)*_[B],v[B]=0}}return[l,null]}},Fp=class extends wt{static{f(this,"WhereBackward")}constructor(){super(3)}apply(t){let e=t[0],[s]=this.savedTensors(),r=Nt(e.shape,{dtype:e.dtype,device:e.device}),o=Es(s.detach(),e,r),i=Es(s.detach(),r,e);return[null,o,i]}}});import IE from"koffi";function Vp(n,t,e){is()?gt.memcpyDtoDAsync(n,t,e,Dt().stream):gt.memcpyDtoD(n,t,e)}function ME(n,t){is()?gt.memsetD8Async(n,0,t,Dt().stream):gt.memsetD8(n,0,t)}function Gv(){if(Zc!==null)return Zc;try{let n=IE.load(jn(wS));Te={create:n.func("int cudnnCreate(_Out_ void **h)"),setStream:n.func("int cudnnSetStream(void *h, void *stream)"),getErrorString:n.func("str cudnnGetErrorString(int status)"),createRNNDesc:n.func("int cudnnCreateRNNDescriptor(_Out_ void **d)"),setRNNDesc:n.func("int cudnnSetRNNDescriptor_v8(void *d, int algo, int cellMode, int biasMode, int dirMode, int inputMode, int dataType, int mathPrec, int mathType, int inputSize, int hiddenSize, int projSize, int numLayers, void *dropoutDesc, uint auxFlags)"),createRNNDataDesc:n.func("int cudnnCreateRNNDataDescriptor(_Out_ void **d)"),setRNNDataDesc:n.func("int cudnnSetRNNDataDescriptor(void *d, int dataType, int layout, int maxSeqLength, int batchSize, int vectorSize, void *seqLengthArray, void *paddingFill)"),createTensorDesc:n.func("int cudnnCreateTensorDescriptor(_Out_ void **d)"),setTensorNdDesc:n.func("int cudnnSetTensorNdDescriptor(void *d, int dataType, int nbDims, void *dimA, void *strideA)"),createDropoutDesc:n.func("int cudnnCreateDropoutDescriptor(_Out_ void **d)"),setDropoutDesc:n.func("int cudnnSetDropoutDescriptor(void *d, void *h, float dropout, uint64 states, size_t stateSize, uint64 seed)"),dropoutGetStatesSize:n.func("int cudnnDropoutGetStatesSize(void *h, _Out_ size_t *sz)"),getRNNWeightSpaceSize:n.func("int cudnnGetRNNWeightSpaceSize(void *h, void *d, _Out_ size_t *sz)"),getRNNTempSpaceSizes:n.func("int cudnnGetRNNTempSpaceSizes(void *h, void *d, int fwdMode, void *xDesc, _Out_ size_t *work, _Out_ size_t *reserve)"),getRNNWeightParams:n.func("int cudnnGetRNNWeightParams(void *h, void *d, int pseudoLayer, size_t wss, uint64 weightSpace, int linLayerID, void *mDesc, _Out_ uint64 *mAddr, void *bDesc, _Out_ uint64 *bAddr)"),rnnForward:n.func("int cudnnRNNForward(void *h, void *d, int fwdMode, uint64 devSeqLengths, void *xDesc, uint64 x, void *yDesc, uint64 y, void *hDesc, uint64 hx, uint64 hy, void *cDesc, uint64 cx, uint64 cy, size_t wss, uint64 weightSpace, size_t workSize, uint64 workSpace, size_t reserveSize, uint64 reserveSpace)"),rnnBackwardData:n.func("int cudnnRNNBackwardData_v8(void *h, void *d, uint64 devSeqLengths, void *yDesc, uint64 y, uint64 dy, void *xDesc, uint64 dx, void *hDesc, uint64 hx, uint64 dhy, uint64 dhx, void *cDesc, uint64 cx, uint64 dcy, uint64 dcx, size_t wss, uint64 weightSpace, size_t workSize, uint64 workSpace, size_t reserveSize, uint64 reserveSpace)"),rnnBackwardWeights:n.func("int cudnnRNNBackwardWeights_v8(void *h, void *d, int addGrad, uint64 devSeqLengths, void *xDesc, uint64 x, void *hDesc, uint64 hx, void *yDesc, uint64 y, size_t wss, uint64 dweightSpace, size_t workSize, uint64 workSpace, size_t reserveSize, uint64 reserveSpace)")},Zc=!0}catch{Zc=!1}return Zc}function qv(){return Gv()}function Xe(n,t){if(t!==0)throw new Error("cuDNN "+n+" failed: "+t+" ("+Te.getErrorString(t)+")")}function Xc(){if(!Gv())throw new Error("cuDNN not available");let n=Dt();if(qi(),!Gp){let t=Uo();Xe("create",Te.create(t)),Gp=t[0],Xe("setStream",Te.setStream(Gp,n.stream))}return Gp}function qE(){if(!q_){let n=Xc(),t=Uo();Xe("createDropout",Te.createDropoutDesc(t));let e=[0n];Xe("dropStatesSize",Te.dropoutGetStatesSize(n,e));let s=oe(Math.max(Number(e[0]),1));Xe("setDropout",Te.setDropoutDesc(t[0],n,0,s,e[0],0n)),q_=t[0]}return q_}function UE(n,t){let e=Uo();return Xe("createTensor",Te.createTensorDesc(e)),Xe("setTensorNd",Te.setTensorNdDesc(e[0],qp,n.length,new Int32Array(n),new Int32Array(t))),e[0]}function Fv(n,t,e,s){let r=Uo();return Xe("createRNNData",Te.createRNNDataDesc(r)),Xe("setRNNData",Te.setRNNDataDesc(r[0],qp,FE,n,t,e,s,null)),r[0]}function WE({inputSize:n,hiddenSize:t,seqLen:e,batch:s,numLayers:r}){let o=`${n}_${t}_${e}_${s}_${r}`,i=Vv.get(o);if(i)return i;let a=Xc(),c=Uo();Xe("createRNN",Te.createRNNDesc(c)),Xe("setRNN",Te.setRNNDesc(c[0],CE,DE,LE,PE,zE,qp,qp,$E,n,t,t,r,qE(),0));let l=c[0],u=new Int32Array(s).fill(e),h=Fv(e,s,n,u),p=Fv(e,s,t,u),d=UE([r,s,t],[s*t,t,1]),m=[0n];Xe("wss",Te.getRNNWeightSpaceSize(a,l,m));let g=[0n],_=[0n];Xe("temp",Te.getRNNTempSpaceSizes(a,l,Uv,h,g,_));let y=oe(s*4);return gt.memcpyHtoD(y,u,s*4),i={rd:l,xDesc:h,yDesc:p,hDesc:d,devSeq:y,wss:m[0],wssN:Number(m[0]),workSize:g[0],workN:Math.max(Number(g[0]),1),reserveSize:_[0],reserveN:Math.max(Number(_[0]),1),inputSize:n,hiddenSize:t,seqLen:e,batch:s,numLayers:r},Vv.set(o,i),i}function Wv(n,t,e,s){let r=Xc(),o=Uo(),i=Uo();Xe("cwm",Te.createTensorDesc(o)),Xe("cwb",Te.createTensorDesc(i));let{numLayers:a,hiddenSize:c,inputSize:l,wss:u}=n;for(let h=0;h<a;h++){let p=h===0?l:c,d=e[h];for(let m=0;m<8;m++){let g=m<4,_=m%4,y=[0n],b=[0n];Xe("wparam",Te.getRNNWeightParams(r,n.rd,h,u,t,m,o[0],y,i[0],b));let S=(g?c*p:c*c)*4,v=c*4,w=BigInt(g?d.x2hW:d.h2hW)+BigInt(_*S),A=BigInt(g?d.x2hB:d.h2hB)+BigInt(_*v);s?(Vp(w,y[0],S),Vp(A,b[0],v)):(Vp(y[0],w,S),Vp(b[0],A,v))}}}function jv(n,t,e,s,r,o,i,a,c=!0){let l=Xc(),u=WE({...e,numLayers:t.length}),h=oe(u.wssN);Wv(u,h,t,!1);let p=oe(u.workN),d=c?oe(u.reserveN):0n;return Xe("forward",Te.rnnForward(l,u.rd,c?Uv:VE,u.devSeq,u.xDesc,n,u.yDesc,o,u.hDesc,s||0n,i,u.hDesc,r||0n,a,u.wss,h,u.workSize,p,c?u.reserveSize:0n,d)),{p:u,weightSpace:h,workSpace:p,reserveSpace:d,training:c}}function Kv(n){n&&(be(n.weightSpace,n.p.wssN),be(n.workSpace,n.p.workN),n.reserveSpace&&n.reserveSpace!==0n&&be(n.reserveSpace,n.p.reserveN))}function Hv(n,t,e,s,r,o,i,a,c,l,u,h){let{p,weightSpace:d,workSpace:m,reserveSpace:g}=n,_=Xc(),y=oe(p.wssN);ME(y,p.wssN),Xe("backwardData",Te.rnnBackwardData(_,p.rd,p.devSeq,p.yDesc,e,o,p.xDesc,c,p.hDesc,s||0n,i||0n,l||0n,p.hDesc,r||0n,a||0n,u||0n,p.wss,d,p.workSize,m,p.reserveSize,g)),Xe("backwardWeights",Te.rnnBackwardWeights(_,p.rd,GE,p.devSeq,p.xDesc,t,p.hDesc,s||0n,p.yDesc,e,p.wss,y,p.workSize,m,p.reserveSize,g)),Wv(p,y,h,!0),be(d,p.wssN),be(m,p.workN),be(g,p.reserveN),be(y,p.wssN)}var Te,Zc,qp,CE,DE,LE,PE,zE,$E,FE,Uv,VE,GE,Uo,Gp,q_,Vv,U_=T(()=>{Kn();Hn();$g();To();Ec();vr();f(Vp,"d2d");f(ME,"memZero");Zc=null;f(Gv,"ensure");f(qv,"cudnnAvailable");qp=0,CE=0,DE=2,LE=2,PE=0,zE=0,$E=2,FE=0,Uv=1,VE=0,GE=0;f(Xe,"ck");Uo=f(()=>[null],"o2"),Gp=null,q_=null;f(Xc,"handle");f(qE,"dropoutDesc");f(UE,"tensorDesc");f(Fv,"dataDesc");Vv=new Map;f(WE,"plan");f(Wv,"packWeights");f(jv,"cudnnLSTMForward");f(Kv,"releaseLSTMForward");f(Hv,"cudnnLSTMBackward")});var Up={};Ft(Up,{GradMode:()=>sr,enableGrad:()=>Jv,noGrad:()=>Yv});function Yv(n){let t=eo;eo=!1;try{return n()}finally{eo=t}}function Jv(n){let t=eo;eo=!0;try{return n()}finally{eo=t}}var eo,sr,Tr=T(()=>{eo=!0,sr={isEnabled(){return eo},setEnabled(n){eo=n}};f(Yv,"noGrad");f(Jv,"enableGrad")});var Ir,Wp=T(()=>{Qn();$t();Ir=class extends wt{static{f(this,"GradAccumulator")}constructor(t){super(0),this._variable=typeof WeakRef<"u"?new WeakRef(t):{deref:f(()=>t,"deref")}}apply(t){let e=t[0],s=this._variable.deref();if(!s)return[];let r=s._impl.autogradMeta;return r?(r.grad===null?r.grad=e:r.grad=ut(r.grad,e),[]):[]}name(){return"GradAccumulator"}}});function zs(n){return Ke()?Zn(n):Io(n)}function rr(n,t){if(Ke())return Xr(n);let e=oe(n.byteLength);return t.push([n,e]),e}function Zv(n){for(let[t,e]of n)Jn(t,e),be(e,t.byteLength)}function W_(n,t,e){let s=new ln;s.setGradFn(n,e),s.requiresGrad=!0,t._impl.setAutogradMeta(s),t._impl._updateKeySet()}function Xv(n,t,e,s=null,r=null){let{inputSize:o,hiddenSize:i,seqLen:a,batch:c}=e,l=t.length,u=[],h=nr(n),p=zs(h),d=t.map(G=>({x2hW:zs(nr(G.x2h.weight)),x2hB:zs(nr(G.x2h.bias)),h2hW:zs(nr(G.h2h.weight)),h2hB:zs(nr(G.h2h.bias))})),m=s?nr(s):null,g=r?nr(r):null,_=s?zs(m):0n,y=r?zs(g):0n,b=l*c*i,x=[l,c,i],S=new Float32Array(a*c*i),v=new Float32Array(b),w=new Float32Array(b),A=rr(S,u),I=rr(v,u),B=rr(w,u),k=[n];for(let G of t)k.push(G.x2h.weight,G.x2h.bias,G.h2h.weight,G.h2h.bias);s&&k.push(s,r);let N=!1;for(let G of k)if(G._impl.autogradMeta&&G.requiresGrad){N=!0;break}let R=sr.isEnabled()&&N,O=jv(p,d,e,_,y,A,I,B,R);Zv(u);let M=jt(S,[a,c,i],n.dtype,n.device),F=jt(v,x,n.dtype,n.device),P=jt(w,x,n.dtype,n.device);if(R){let G={xArr:h,yArr:S,hxArr:m,cxArr:g,opts:e,numLayers:l,hasInit:!!s,dtype:n.dtype,device:n.device,inputShape:[...n.shape],stateShape:x,weightShapes:t.map($=>({x2hW:[...$.x2h.weight.shape],x2hB:[...$.x2h.bias.shape],h2hW:[...$.h2h.weight.shape],h2hB:[...$.h2h.bias.shape]}))},W=new j_(O,G,k.length);for(let $=0;$<k.length;$++){W.saveInputMetadata($,[...k[$].shape],k[$].dtype);let L=k[$]._impl.autogradMeta;if(L&&L.requiresGrad)if(L.gradFn)W.setNextEdge($,L.gradFn,L.outputNr||0);else{let q=L.getGradAccumulator();q||(q=new Ir(k[$]),L.setGradAccumulator(q)),W.setNextEdge($,q,0)}else W.setNextEdge($,null,0)}W_(W,M,0),W_(W,F,1),W_(W,P,2)}else Kv(O);return[M,F,P]}var nr,jp,j_,Qv=T(()=>{Qn();Yi();Tr();Wp();hn();zc();To();U_();nr=f(n=>K_(n),"carr"),jp=f(n=>n.reduce((t,e)=>t*e,1),"prod");f(zs,"devIn");f(rr,"devOut");f(Zv,"flushOut");j_=class extends wt{static{f(this,"CudnnLSTMBackward")}constructor(t,e,s){super(s),this.fwd=t,this.info=e}apply(t){let[e,s,r]=t,o=this.info,{seqLen:i,batch:a,inputSize:c,hiddenSize:l}=o.opts,u=o.numLayers,h=u*a*l,p=[],d=zs(o.xArr),m=zs(o.yArr),g=o.hasInit?zs(o.hxArr):0n,_=o.hasInit?zs(o.cxArr):0n,y=zs(nr(e)),b=s?zs(nr(s)):0n,x=r?zs(nr(r)):0n,S=new Float32Array(i*a*c),v=rr(S,p),w=null,A=null,I=0n,B=0n;o.hasInit&&(w=new Float32Array(h),A=new Float32Array(h),I=rr(w,p),B=rr(A,p));let k=o.weightShapes.map(M=>({x2hW:new Float32Array(jp(M.x2hW)),x2hB:new Float32Array(jp(M.x2hB)),h2hW:new Float32Array(jp(M.h2hW)),h2hB:new Float32Array(jp(M.h2hB))})),N=k.map(M=>({x2hW:rr(M.x2hW,p),x2hB:rr(M.x2hB,p),h2hW:rr(M.h2hW,p),h2hB:rr(M.h2hB,p)}));Hv(this.fwd,d,m,g,_,y,b,x,v,I,B,N),Zv(p);let R=f((M,F)=>jt(M,F,o.dtype,o.device),"w"),O=[R(S,o.inputShape)];for(let M=0;M<k.length;M++){let F=k[M],P=o.weightShapes[M];O.push(R(F.x2hW,P.x2hW),R(F.x2hB,P.x2hB),R(F.h2hW,P.h2hW),R(F.h2hB,P.h2hB))}return o.hasInit&&O.push(R(w,o.stateShape),R(A,o.stateShape)),O}};f(W_,"attach");f(Xv,"cudnnLSTMOp")});function tB(n){return Ke()?Zn(n):Io(n)}function jE(n,t){if(Ke())return Xr(n);let e=oe(n.byteLength);return t.push([n,e]),e}function KE(n){for(let[t,e]of n)Jn(t,e),be(e,t.byteLength)}function eB(n){let t=sB(n);if(t)return{data:n._impl.storage.rawData,lay:t};let e=Y_();if(Ke()&&e){let r=e(n._impl.storage.rawData,n.shape,n.strides,n._impl.storageOffset,n.dtype),o=n.shape,i=o.length,a=o[i-2],c=o[i-1],l=1;for(let u=0;u<i-2;u++)l*=o[u];return{data:r,lay:{trans:!1,batchStride:a*c,batch:l,rows:a,cols:c}}}let s=fn(n);return{data:s._impl.storage.rawData,lay:sB(s)}}function sB(n){if(n.storageOffset!==0)return null;let t=n.shape,e=n.strides,s=t.length;if(s<2)return null;let r=t[s-2],o=t[s-1],i=e[s-2],a=e[s-1],c;if(i===o&&a===1)c=!1;else if(i===1&&a===r)c=!0;else return null;let l=r*o;for(let h=s-3;h>=0;h--){if(e[h]!==l)return null;l*=t[h]}let u=1;for(let h=0;h<s-2;h++)u*=t[h];return{trans:c,batchStride:r*o,batch:u,rows:r,cols:o}}function HE(n,t){let e=new ln;e.setGradFn(n,0),e.requiresGrad=!0,t._impl.setAutogradMeta(e),t._impl._updateKeySet()}function nB(n,t){if(!n.device||n.device.type!==pe.GPU||t.device.type!==pe.GPU||n.dtype!=="f32"||t.dtype!=="f32")return null;let e=n.shape.length,s=t.shape.length;if(e<2||s<2||e!==s)return null;let r=n.shape[e-2],o=n.shape[e-1],i=t.shape[s-1];if(t.shape[s-2]!==o)return null;let a=eB(n),c=eB(t),l=a.lay,u=c.lay;if(!l||!u||l.batch!==u.batch)return null;let h=l.batch,p=[],d=tB(a.data),m=tB(c.data),g=new Float32Array(h*r*i),_=jE(g,p);h>1?Yg(h,r,i,o,d,l.batchStride,l.trans,m,u.batchStride,u.trans,_,r*i):Hg(r,i,o,d,l.trans,m,u.trans,_),KE(p);let y=[...n.shape.slice(0,e-2),r,i],b=jt(g,y,n.dtype,n.device);if(sr.isEnabled()&&(n._impl.autogradMeta&&n.requiresGrad||t._impl.autogradMeta&&t.requiresGrad)){let x=new H_(n,t),S=[n,t];for(let v=0;v<2;v++){x.saveInputMetadata(v,[...S[v].shape],S[v].dtype);let w=S[v]._impl.autogradMeta;if(w&&w.requiresGrad)if(w.gradFn)x.setNextEdge(v,w.gradFn,w.outputNr||0);else{let A=w.getGradAccumulator();A||(A=new Ir(S[v]),w.setGradAccumulator(A)),x.setNextEdge(v,A,0)}else x.setNextEdge(v,null,0)}HE(x,b)}return b}var H_,rB=T(()=>{Qn();Yi();Tr();Wp();hn();Ye();tn();zc();To();vp();$t();f(tB,"devIn");f(jE,"devOut");f(KE,"flushOut");f(eB,"operandData");f(sB,"operandLayout");H_=class extends wt{static{f(this,"MatmulBackward")}constructor(t,e){super(2),this.A=t,this.B=e}apply(t){let e=t[0],s=this.A,r=this.B,o=s.shape.length,i=r.shape.length,a=Je(e,r.transpose(i-2,i-1)),c=Je(s.transpose(o-2,o-1),e);return[a,c]}};f(HE,"attach");f(nB,"gpuMatmul")});var Hp={};Ft(Hp,{adamGraphKernelSource:()=>dB,deviceAdam:()=>mB,deviceClipGradNorm:()=>iT,deviceConcat:()=>lB,deviceContiguous:()=>Z_,freeOptimizerDeviceState:()=>fB,isCudaGraphEnabled:()=>HS,measureCudaKernel:()=>iB,preloadCublas:()=>aB,releaseCudaMemory:()=>aT,runCudaKernel:()=>JE,runCudaKernelResident:()=>J_,runCudaKernelSync:()=>cB,runCudaPlan:()=>XS,setCudaGraphEnabled:()=>KS,stepIncKernelSource:()=>hB,teardownAfterFit:()=>cT});function iB(n,t,e=[],s={}){Dt();let{func:r}=Yn(n.source,n.name),o=n.metadata,i=s.warmup??5,a=s.repeat??30,c=s.minWarmupMs??25,l=s.maxWarmup??1e5,u=o.gridDim,h=o.blockDim,p=o.sharedMemBytes||0,d=t.map(g=>Math.max(g,1)),m=d.map(g=>oe(g));try{let g=performance.now(),_=0;for(;(_<i||performance.now()-g<c)&&(Xs(r,u,h,p,m,e),!(++_>=l)););let y=[];for(let b=0;b<a;b++){let x=performance.now();Xs(r,u,h,p,m,e),y.push(performance.now()-x)}return y}finally{for(let g=0;g<m.length;g++)be(m[g],d[g])}}async function aB(){return Qc||(Qc=await Promise.resolve().then(()=>(vp(),US))),Qc}function cB(n,t,e){let s=n.metadata;if(s.cublas){if(!Qc)throw new Error("cuBLAS module not preloaded; call preloadCublas() before sync execution");let{M:u,N:h,K:p,aIdx:d,bIdx:m,cIdx:g,transB:_}=s.cublas;Qc.cublasMatmul(u,h,p,t[d],t[m],t[g],_);return}Dt();let{func:r}=Rg(n),o=[],i=[];for(let u of t)ArrayBuffer.isView(u)?o.push(u):i.push(u);if(e)for(let u of e)i.push(u);let a=s.outputIndices||o.map((u,h)=>h),c=[];for(let u of o){let h=oe(u.byteLength);Zr(h,u),c.push(h)}Xs(r,s.gridDim,s.blockDim,s.sharedMemBytes||0,c,i);let l=new Set(a);for(let u=0;u<o.length;u++)l.has(u)&&Jn(o[u],c[u]);for(let u=0;u<c.length;u++)be(c[u],o[u].byteLength)}function J_(n,t,e){let s=n.metadata;if(s.cublas)throw new Error("cuBLAS kernels are not supported on the eager device-resident path");Dt();let{func:r}=Rg(n),o=[],i=[];for(let u of t)ArrayBuffer.isView(u)?o.push(u):i.push(u);if(e)for(let u of e)i.push(u);let a=s._outputSet||(s._outputSet=new Set(s.outputIndices||o.map((u,h)=>h))),c=new Array(o.length),l=YE(s.scratch);if(Ke()){for(let u=0;u<o.length;u++)c[u]=a.has(u)?Xr(o[u]):Zn(o[u]);Xs(r,s.gridDim,s.blockDim,s.sharedMemBytes||0,[...c,...l.ptrs],i,!1),oB(l);return}for(let u=0;u<o.length;u++)c[u]=Io(o[u]);Xs(r,s.gridDim,s.blockDim,s.sharedMemBytes||0,[...c,...l.ptrs],i,!1);for(let u=0;u<o.length;u++)a.has(u)&&Xg(o[u],c[u]);oB(l)}function YE(n){if(!n||n.length===0)return{ptrs:[],bufs:[]};let t=[],e=[];for(let s of n){let r=s.size*Jt(s.dtype).BYTES_PER_ELEMENT,o=oe(r);t.push(o),e.push([o,r])}return{ptrs:t,bufs:e}}function oB(n){for(let[t,e]of n.bufs)be(t,e)}async function JE(n,t,e){n.metadata.cublas&&await aB(),cB(n,t,e)}function Kp(n){return ZE[n]||"float"}function uB(n,t){return{gridDim:[Math.max(Math.ceil(n/256),1),1,1],blockDim:[256,1,1],sharedMemBytes:0,outputIndices:t}}function XE(n,t){return`extern "C" __global__ void ${t}(const ${n}* in, ${n}* out, int n, int rank,
|
|
89
|
-
int s0,int s1,int s2,int s3,int s4,int s5,int s6,int s7,
|
|
90
|
-
int t0,int t1,int t2,int t3,int t4,int t5,int t6,int t7, int off) {
|
|
91
|
-
int i = blockIdx.x*blockDim.x + threadIdx.x; if (i >= n) return;
|
|
92
|
-
int shp[8] = {s0,s1,s2,s3,s4,s5,s6,s7};
|
|
93
|
-
int strd[8] = {t0,t1,t2,t3,t4,t5,t6,t7};
|
|
94
|
-
long long src = off; int rem = i;
|
|
95
|
-
for (int d = rank-1; d >= 0; d--) { int x = rem % shp[d]; rem /= shp[d]; src += (long long)x*strd[d]; }
|
|
96
|
-
out[i] = in[src];
|
|
97
|
-
}`}function Z_(n,t,e,s,r){let o=Kp(r),i=`gather_${r}`,a=or.get(i);a||(a=XE(o,i),or.set(i,a));let c=t.length,l=1;for(let d=0;d<c;d++)l*=t[d];let u=new(Jt(r))(Math.max(l,1)),h=new Array(8).fill(1),p=new Array(8).fill(0);for(let d=0;d<c;d++)h[d]=t[d],p[d]=e[d];return J_({source:a,name:i,metadata:uB(l,[1])},[n,u,l,c,...h,...p,s|0],null),u}function QE(n,t){return`extern "C" __global__ void ${t}(const ${n}* in, ${n}* out, int pre, int dk, int tail, int total, int offset) {
|
|
98
|
-
int i = blockIdx.x*blockDim.x + threadIdx.x; int n = pre*dk*tail; if (i >= n) return;
|
|
99
|
-
int t = i % tail; int r = i / tail; int j = r % dk; int p = r / dk;
|
|
100
|
-
out[(long long)p*total*tail + (long long)(offset+j)*tail + t] = in[i];
|
|
101
|
-
}`}function lB(n,t,e,s,r,o,i){let a=Kp(i),c=`catcopy_${i}`,l=or.get(c);l||(l=QE(a,c),or.set(c,l));let u=n==="stack",h=r.length,p=s<0?h+s:s,d=1;for(let y=0;y<p;y++)d*=r[y];let m=1;for(let y=p+1;y<h;y++)m*=r[y];let g=r[p],_=0;for(let y=0;y<t.length;y++){let b=u?1:e[y][p],x=d*b*m;J_({source:l,name:c,metadata:uB(x,[1])},[t[y],o,d,b,m,g,_],null),_+=b}}function tT(n,t){return`extern "C" __global__ void ${t}(${n}* w, const ${n}* g, ${n}* m, ${n}* v, int n, float b1, float b2, float ob1, float ob2, float eps, float ss, float bc2s, float wd) {
|
|
102
|
-
int i = blockIdx.x*blockDim.x + threadIdx.x; if (i >= n) return;
|
|
103
|
-
float gi = (float)g[i] + wd * (float)w[i];
|
|
104
|
-
float mi = b1*(float)m[i] + ob1*gi;
|
|
105
|
-
float vi = b2*(float)v[i] + ob2*gi*gi;
|
|
106
|
-
m[i] = (${n})mi; v[i] = (${n})vi;
|
|
107
|
-
w[i] = (${n})((float)w[i] - ss * mi / (sqrtf(vi)/bc2s + eps));
|
|
108
|
-
}`}function eT(n){return`extern "C" __global__ void ${n}(int* t) {
|
|
109
|
-
if (threadIdx.x == 0 && blockIdx.x == 0) t[0] = t[0] + 1;
|
|
110
|
-
}`}function sT(n,t){return`extern "C" __global__ void ${t}(${n}* w, const ${n}* g, ${n}* m, ${n}* v, const int* t, int n, float b1, float b2, float ob1, float ob2, float eps, float lr, float wd) {
|
|
111
|
-
int i = blockIdx.x*blockDim.x + threadIdx.x; if (i >= n) return;
|
|
112
|
-
int step = t[0];
|
|
113
|
-
double bc1 = 1.0 - pow((double)b1, (double)step);
|
|
114
|
-
double bc2 = 1.0 - pow((double)b2, (double)step);
|
|
115
|
-
float ss = (float)((double)lr / bc1);
|
|
116
|
-
float bc2s = (float)sqrt(bc2);
|
|
117
|
-
float gi = (float)g[i] + wd * (float)w[i];
|
|
118
|
-
float mi = b1*(float)m[i] + ob1*gi;
|
|
119
|
-
float vi = b2*(float)v[i] + ob2*gi*gi;
|
|
120
|
-
m[i] = (${n})mi; v[i] = (${n})vi;
|
|
121
|
-
w[i] = (${n})((float)w[i] - ss * mi / (sqrtf(vi)/bc2s + eps));
|
|
122
|
-
}`}function nT(n){return`extern "C" __global__ void ${n}(const float* g, double* acc, int n) {
|
|
123
|
-
extern __shared__ double sdata[];
|
|
124
|
-
int tid = threadIdx.x;
|
|
125
|
-
int i = blockIdx.x*blockDim.x + threadIdx.x;
|
|
126
|
-
double v = (i < n) ? (double)g[i]*(double)g[i] : 0.0;
|
|
127
|
-
sdata[tid] = v;
|
|
128
|
-
__syncthreads();
|
|
129
|
-
for (int s = blockDim.x/2; s > 0; s >>= 1) { if (tid < s) sdata[tid] += sdata[tid+s]; __syncthreads(); }
|
|
130
|
-
if (tid == 0) atomicAdd(acc, sdata[0]);
|
|
131
|
-
}`}function rT(n){return`extern "C" __global__ void ${n}(const double* acc, const float* mp, float* coef) {
|
|
132
|
-
if (threadIdx.x == 0 && blockIdx.x == 0) {
|
|
133
|
-
double norm = sqrt(acc[0]);
|
|
134
|
-
double c = (double)mp[0] / (norm + (double)mp[1]);
|
|
135
|
-
coef[0] = c < 1.0 ? (float)c : 1.0f;
|
|
136
|
-
}
|
|
137
|
-
}`}function oT(n){return`extern "C" __global__ void ${n}(float* g, const float* coef, int n) {
|
|
138
|
-
int i = blockIdx.x*blockDim.x + threadIdx.x; if (i < n) g[i] = g[i] * coef[0];
|
|
139
|
-
}`}function iT(n,t,e=1e-6){Dt(),Mr===null&&(Mr=oe(8),tu=oe(4),eu=oe(8));let s=Dt().stream;is()||Zr(eu,new Float32Array([t,e])),is()?gt.memsetD8Async(Mr,0,8,s):gt.memsetD8(Mr,0,8);let r=Yn(nT("clip_accum"),"clip_accum").func,o=Yn(rT("clip_coef"),"clip_coef").func,i=Yn(oT("clip_scale"),"clip_scale").func,a=[];for(let c of n){if(!c.grad)continue;let l=c.grad._impl.storage.rawData;if(!l)continue;let u=l.length,h=Bp(l);a.push([h,u]),Xs(r,[Math.ceil(u/256),1,1],[256,1,1],256*8,[h,Mr],[u],!1)}if(a.length!==0){Xs(o,[1,1,1],[1,1,1],0,[Mr,eu,tu],[],!1);for(let[c,l]of a)Xs(i,[Math.ceil(l/256),1,1],[256,1,1],0,[c,tu],[l],!1)}}function fB(n){let t=n&&n._state;if(!(!t||typeof t.values!="function"))for(let e of t.values())e&&(e._mDev!==void 0&&(Sr(e._mDev),delete e._mDev),e._vDev!==void 0&&(Sr(e._vDev),delete e._vDev),e._tDev!==void 0&&(Sr(e._tDev),delete e._tDev))}function pB(){Mr!==null&&(Sr(Mr),Sr(tu),Sr(eu),Mr=null,tu=null,eu=null)}function aT(){return s_(),pB(),lp()}function cT(n,t){let e=n&&n.__eagerGraphRunner;if(e){try{qg(e.captured)}catch{}n._cudaGraphPhase=e.phase,delete n.__eagerGraphRunner}if(t)for(let s of t)fB(s);return Gi(!1),kp(),pB(),lp()}function hB(){let n="step_inc",t=or.get(n);return t||(t=eT(n),or.set(n,t)),{source:t,name:n}}function dB(n="f32"){let t=`adam_graph_${n}`,e=or.get(t);return e||(e=sT(Kp(n),t),or.set(t,e)),{source:e,name:t}}function uT(n,t){Zr(n,new Int32Array([t|0]))}function mB(n,t,e){if(!Ke())return!1;let s=n.dtype||"f32";if(s!=="f32")return!1;let r=n._impl.storage.rawData,o=n.grad._impl.storage.rawData;if(!r||!o)return!1;let i=r.length;Dt();let a=MS();t._mDev||(t._mDev=oe(i*4),gt.memsetD8(t._mDev,0,i*4),t._vDev=oe(i*4),gt.memsetD8(t._vDev,0,i*4),a&&(t._tDev=oe(4),gt.memsetD8(t._tDev,0,4)),Qg(r));let c=Bp(r),l=Zn(o);if(is()){if(t._tDev===void 0)throw new Error("CUDA graph Adam requires a device step counter; warmup must run before capture");let d=hB(),m=dB(s),g=Yn(d.source,d.name).func,_=Yn(m.source,m.name).func;return Xs(g,[1,1,1],[1,1,1],0,[t._tDev],[],!1),Xs(_,[Math.ceil(i/256),1,1],[256,1,1],0,[c,l,t._mDev,t._vDev,t._tDev],[i,ys(e.beta1),ys(e.beta2),ys(e.omb1),ys(e.omb2),ys(e.eps),ys(e.lr),ys(e.wd)],!1),!0}let u=`adam_${s}`,h=or.get(u);h||(h=tT(Kp(s),u),or.set(u,h));let{func:p}=Yn(h,u);return Xs(p,[Math.ceil(i/256),1,1],[256,1,1],0,[c,l,t._mDev,t._vDev],[i,ys(e.beta1),ys(e.beta2),ys(e.omb1),ys(e.omb2),ys(e.eps),ys(e.stepSize),ys(e.bc2sqrt),ys(e.wd)],!1),a&&t._tDev!==void 0&&uT(t._tDev,t.step),!0}function lT(n){if(!Ke()||!n.device||n.device.type!==pe.GPU)return null;let t=Z_(n._impl.storage.rawData,n.shape,n.strides,n._impl.storageOffset,n.dtype);return jt(t,[...n.shape],n.dtype,n.device)}function pT(n,t,e,s){if(!Ke()||s!==0||e.length!==2||!n.device||n.device.type!==pe.GPU||t.dtype!=="i32")return null;let r=e[0],o=e[1],i=fn(n),a=fn(t),c=a.numel;if(i.numel!==c*o)return null;Dt();let l=new Float32Array(r*o),{func:u}=Yn(fT,"index_add0"),h=Xr(l),p=Dt().stream;is()?gt.memsetD8Async(h,0,r*o*4,p):gt.memsetD8(h,0,r*o*4);let d=Zn(i._impl.storage.rawData),m=Zn(a._impl.storage.rawData);return Xs(u,[Math.ceil(c*o/256),1,1],[256,1,1],0,[d,m,h],[c,o],!1),jt(l,[r,o],n.dtype,n.device)}var Qc,ZE,or,Mr,tu,eu,fT,Yp=T(()=>{Hn();Og();To();Cg();QS();zc();vr();mp();Kn();i_();rp();hn();Ye();G_();tn();Qs();U_();Qv();rB();Lo.setHostReadHook(n_);f(iB,"measureCudaKernel");Qc=null;f(aB,"preloadCublas");f(cB,"runCudaKernelSync");f(J_,"runCudaKernelResident");f(YE,"_acquireScratch");f(oB,"_releaseScratch");f(JE,"runCudaKernel");ZE={f32:"float",f64:"double",i64:"long long",i32:"int",i16:"short",i8:"signed char",u8:"unsigned char",bool:"unsigned char"};f(Kp,"_ctype");or=new Map;f(uB,"_meta");f(XE,"_gatherKernel");f(Z_,"deviceContiguous");f(QE,"_catKernel");f(lB,"deviceConcat");f(tT,"_adamKernel");f(eT,"_stepIncKernel");f(sT,"_adamGraphKernel");f(nT,"_clipAccumKernel");f(rT,"_clipCoefKernel");f(oT,"_clipScaleKernel");Mr=null,tu=null,eu=null;f(iT,"deviceClipGradNorm");f(fB,"freeOptimizerDeviceState");f(pB,"freeClipScratch");f(aT,"releaseCudaMemory");f(cT,"teardownAfterFit");f(hB,"stepIncKernelSource");f(dB,"adamGraphKernelSource");f(uT,"_setDeviceInt");f(mB,"deviceAdam");f(lT,"gpuContiguousTensor");Sv(lT);fT=`extern "C" __global__ void index_add0(const float* g, const int* idx, float* out, int K, int E) {
|
|
140
|
-
int t = blockIdx.x*blockDim.x + threadIdx.x; if (t >= K*E) return;
|
|
141
|
-
int i = t / E, j = t % E;
|
|
142
|
-
atomicAdd(&out[idx[i]*E + j], g[i*E + j]);
|
|
143
|
-
}`;f(pT,"gpuIndexSelectBackward");ta.setGpuBackward(pT);X_(Z_);Q_(lB);ey(mB);sy(nB);qv()&&ty(Xv);np("cuda",iB)});var OB={};Ft(OB,{ensureWebGPUEager:()=>bT,flushWebGPUEager:()=>xT,instantiateWebGPU:()=>mT,prewarmPipelines:()=>AB,recordWebGPUEager:()=>eh,resetDevice:()=>hT,runWebGPUKernel:()=>gT,runWebGPUPlan:()=>_T,webgpuEagerOp:()=>vT,webgpuEagerReady:()=>wT,webgpuRNN:()=>AT});function ea(n){switch(ls(n)){case"i32":return Int32Array;case"u32":return Uint32Array;case"f16":return Uint16Array;default:return Float32Array}}function Qp(n,t,e,s){if(e==="bf16")for(let r=0;r<t.length;r++)n[s+r]=Vf(t[r]);else if(e==="i64")for(let r=0;r<t.length;r++)n[s+r]=Number(BigInt.asIntN(32,t[r]));else n.set(t,s)}function ay(n,t,e,s,r){if(e==="bf16")for(let o=0;o<r;o++)n[o]=Gf(t[s+o]);else if(e==="i64")for(let o=0;o<r;o++)n[o]=BigInt(t[s+o]);else n.set(t.subarray(s,s+r))}function ir(n){return Math.ceil(n/4)*4}async function cy(){return ce||nu||(nu=(async()=>{let n=typeof navigator<"u"&&navigator.gpu;if(!n)try{let i=await import("webgpu");xB=i,n=i.create([]),i.globals&&(ru=i.globals.GPUBufferUsage,Zp=i.globals.GPUMapMode,Xp=i.globals.GPUShaderStage)}catch{throw new Error('WebGPU not available: install the "webgpu" npm package or run in a browser with WebGPU support')}!ru&&typeof GPUBufferUsage<"u"&&(ru=GPUBufferUsage,Zp=GPUMapMode,Xp=GPUShaderStage);let t=await n.requestAdapter();if(!t)throw new Error("WebGPU: no adapter found");let e=t.limits||{},s={},r=["maxStorageBuffersPerShaderStage","maxStorageBufferBindingSize","maxBufferSize","maxBindingsPerBindGroup"];for(let i of r)e[i]!==void 0&&(s[i]=e[i]);let o=[];return t.features&&t.features.has("shader-f16")&&o.push("shader-f16"),ce=await t.requestDevice({requiredLimits:s,requiredFeatures:o}),!gB&&typeof process<"u"&&process.on&&(gB=!0,process.on("exit",()=>{ce&&(ce.destroy(),ce=null)})),ce})(),nu)}function sa(){return ru}function uy(){return Zp.READ}function hT(){ce&&(ce.destroy(),ce=null),nu=null,ru=null,Zp=null,Xp=null,xB=null}function dT(n){return n.name==="_shapes"?"uniform":n.mode==="read_write"?"storage":"read-only-storage"}function SB(n,t){let e=n.createShaderModule({code:t.source}),s=[];for(let i of t.metadata.bindings)s.push({binding:i.index,visibility:Xp.COMPUTE,buffer:{type:dT(i)}});let r=n.createBindGroupLayout({entries:s}),o=n.createPipelineLayout({bindGroupLayouts:[r]});return{shaderModule:e,bindGroupLayout:r,pipelineLayout:o}}function vB(n,t){let{shaderModule:e,bindGroupLayout:s,pipelineLayout:r}=SB(n,t);return{pipeline:n.createComputePipeline({layout:r,compute:{module:e,entryPoint:t.name}}),bindGroupLayout:s}}function BB(n,t){let e=th.get(t);return e||(e=vB(n,t),th.set(t,e)),e}async function mT(n){let t=await cy(),{pipeline:e,bindGroupLayout:s}=vB(t,n);return{device:t,pipeline:e,bindGroupLayout:s,kernel:n,workgroupSize:n.metadata.workgroupSize,dispatchSize:n.metadata.dispatchSize,bindings:n.metadata.bindings}}function kB(n){let t=new Map,e=0;for(let s of n)if(s.name!=="_shapes")if(s.packed)for(let r of s.packed)t.set(r.name,r.argIndex!==void 0?r.argIndex:e++);else t.set(s.name,s.argIndex!==void 0?s.argIndex:e++);return t}async function gT(n,t,e){let{device:s,pipeline:r,bindGroupLayout:o,bindings:i,dispatchSize:a}=n,c=sa(),l=[],u=[],h=kB(i);for(let _=0;_<i.length;_++){let y=i[_];if(y.name==="_shapes"){let k=new Uint32Array(e||[]),N=Math.max(Math.ceil(k.byteLength/16)*16,16),R=s.createBuffer({size:N,usage:c.UNIFORM|c.COPY_DST});s.queue.writeBuffer(R,0,k),l.push(R),u.push({binding:y.index,resource:{buffer:R}});continue}let b=xn(y.dtype),x=ea(y.dtype);if(y.packed){let k=ir(y.packedSize*b),R=y.mode==="read_write"?c.STORAGE|c.COPY_SRC|c.COPY_DST:c.STORAGE|c.COPY_DST,O=s.createBuffer({size:Math.max(k,4),usage:R,mappedAtCreation:!0}),M=new x(O.getMappedRange());for(let F of y.packed){let P=h.get(F.name),G=t[P];G&&Qp(M,G,F.dtype,F.offset)}O.unmap(),l.push(O),u.push({binding:y.index,resource:{buffer:O}});continue}let S=h.get(y.name),v=t[S],w=ir(v.length*b),I=y.mode==="read_write"?c.STORAGE|c.COPY_SRC|c.COPY_DST:c.STORAGE|c.COPY_DST,B=s.createBuffer({size:Math.max(w,4),usage:I,mappedAtCreation:!0});Qp(new x(B.getMappedRange()),v,y.dtype,0),B.unmap(),l.push(B),u.push({binding:y.index,resource:{buffer:B}})}let p=s.createBindGroup({layout:o,entries:u}),d=s.createCommandEncoder(),m=d.beginComputePass();m.setPipeline(r),m.setBindGroup(0,p),m.dispatchWorkgroups(a[0],a[1],a[2]),m.end();let g=[];for(let _=0;_<i.length;_++){let y=i[_];if(y.name==="_shapes"||y.mode!=="read_write")continue;let b=l[_],x=xn(y.dtype),S=ea(y.dtype);if(y.packed)for(let v of y.packed){let w=v.offset*x,A=ir(v.size*x),I=s.createBuffer({size:A,usage:c.MAP_READ|c.COPY_DST});d.copyBufferToBuffer(b,w,I,0,A),g.push({readBuf:I,tensorIdx:h.get(v.name),size:v.size,dtype:v.dtype,ViewCtor:S})}else{let v=b.size,w=s.createBuffer({size:v,usage:c.MAP_READ|c.COPY_DST});d.copyBufferToBuffer(b,0,w,0,v),g.push({readBuf:w,tensorIdx:h.get(y.name),size:t[h.get(y.name)].length,dtype:y.dtype,ViewCtor:S})}}s.queue.submit([d.finish()]);for(let _ of g){await _.readBuf.mapAsync(uy());let y=new _.ViewCtor(_.readBuf.getMappedRange());ay(t[_.tensorIdx],y,_.dtype,0,_.size),_.readBuf.unmap(),_.readBuf.destroy()}for(let _ of l)_.destroy()}async function AB(n,t){if(typeof n.createComputePipelineAsync!="function")return;let e=[],s=new Set;for(let r of t){if(!r||s.has(r)||th.has(r))continue;s.add(r);let{shaderModule:o,bindGroupLayout:i,pipelineLayout:a}=SB(n,r);e.push(n.createComputePipelineAsync({layout:a,compute:{module:o,entryPoint:r.name}}).then(c=>th.set(r,{pipeline:c,bindGroupLayout:i}),()=>{}))}e.length>0&&await Promise.all(e)}async function _T(n,t,e){let s=await cy(),r=sa();await AB(s,e.map(b=>b.kernel));let o=new Set;for(let b of e)for(let x of b.outputSlots)o.add(x);let i=new Array(n.numSlots).fill(null),a=new Array(n.numSlots).fill("f32");for(let b=0;b<n.numSlots;b++){let x=t[b];if(!x)continue;let S=x.dtype||"f32";a[b]=S;let v=Math.max(ir(x.data.length*xn(S)),4),w=!o.has(b),A=s.createBuffer({size:v,usage:r.STORAGE|r.COPY_DST|r.COPY_SRC,mappedAtCreation:w});w&&(Qp(new(ea(S))(A.getMappedRange()),x.data,S,0),A.unmap()),i[b]=A}let c=32,l=[],u=[],h={encoder:s.createCommandEncoder(),pending:0},p=f(()=>{++h.pending>=c&&(s.queue.submit([h.encoder.finish()]),h.encoder=s.createCommandEncoder(),h.pending=0)},"maybeFlush"),d=f(b=>{let x=h.encoder,{pipeline:S,bindGroupLayout:v}=BB(s,b.kernel),w=b.inputSlots.concat(b.outputSlots),A=[],I=[],B=0;for(let O of b.kernel.metadata.bindings)if(O.name==="_shapes"){let M=new Uint32Array(b.shapeValues||[]),F=Math.max(Math.ceil(M.byteLength/16)*16,16),P=s.createBuffer({size:F,usage:r.UNIFORM|r.COPY_DST});s.queue.writeBuffer(P,0,M),l.push(P),A.push({binding:O.index,resource:{buffer:P}})}else if(O.packed){let M=xn(O.dtype),F=s.createBuffer({size:Math.max(ir(O.packedSize*M),4),usage:r.STORAGE|r.COPY_DST|r.COPY_SRC});u.push(F);let P=O.mode==="read_write";for(let G of O.packed){let W=w[G.argIndex],$=ir(G.size*M);P?I.push({slot:W,src:F,srcOff:G.offset*M,bytes:$}):x.copyBufferToBuffer(i[W],0,F,G.offset*M,$)}A.push({binding:O.index,resource:{buffer:F}})}else A.push({binding:O.index,resource:{buffer:i[w[B++]]}});let k=s.createBindGroup({layout:v,entries:A}),N=x.beginComputePass();N.setPipeline(S),N.setBindGroup(0,k);let R=b.kernel.metadata.dispatchSize;N.dispatchWorkgroups(R[0],R[1],R[2]),N.end();for(let O of I)x.copyBufferToBuffer(O.src,O.srcOff,i[O.slot],0,O.bytes);p()},"encodeStep"),m=n.scanLoops?[...n.scanLoops].sort((b,x)=>b.loopStart-x.loopStart):n.scanLoop?[n.scanLoop]:null;if(m&&m.length){let b=0;for(let x of m){for(;b<x.loopStart;b++)d(e[b]);for(let S of x.carry)h.encoder.copyBufferToBuffer(i[S.initSlot],0,i[S.a],0,S.bytes);for(let S=0;S<x.T;S++){for(let v of x.xs)h.encoder.copyBufferToBuffer(i[v.xsSlot],S*v.stepBytes,i[v.xtSlot],0,v.stepBytes);for(let v=x.loopStart;v<x.loopEnd;v++)d(e[v]);for(let v of x.ys)h.encoder.copyBufferToBuffer(i[v.ytSlot],0,i[v.ysSlot],S*v.stepBytes,v.stepBytes);for(let v of x.carry){let w=i[v.a];i[v.a]=i[v.b],i[v.b]=w}}for(let S of x.carry)h.encoder.copyBufferToBuffer(i[S.a],0,i[S.finalSlot],0,S.bytes);b=x.loopEnd}for(;b<e.length;b++)d(e[b])}else for(let b of e)d(b);let g=h.encoder,_=new Set(n.argSlots),y=[];for(let b=0;b<n.numSlots;b++){if(!i[b]||!o.has(b)||!_.has(b))continue;let x=t[b],S=Math.max(ir(x.data.length*xn(a[b])),4),v=s.createBuffer({size:S,usage:r.MAP_READ|r.COPY_DST});g.copyBufferToBuffer(i[b],0,v,0,S),y.push({rb:v,dtype:a[b],size:x.data.length,dst:x.data})}s.queue.submit([g.finish()]);for(let b of y)await b.rb.mapAsync(uy()),ay(b.dst,new(ea(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 l)b.destroy();for(let b of u)b.destroy()}function RB(n){let t=iy.get(n);if(t&&t.length)return t.pop();let e=sa();return ce.createBuffer({size:n,usage:e.STORAGE|e.COPY_SRC|e.COPY_DST})}function _B(n,t){let e=iy.get(t);e||(e=[],iy.set(t,e)),e.push(n)}async function bT(){return await cy(),Tn||(Tn=ce.createCommandEncoder()),ce}function wT(){return!!ce&&!!Tn}function yB(n,t,e){let s=ou.get(n);if(s)return s.buf;let r=sa(),o=Math.max(ir(n.length*xn(t)),4);if(e){let a=ce.createBuffer({size:o,usage:r.STORAGE|r.COPY_SRC|r.COPY_DST,mappedAtCreation:!0});return Qp(new(ea(t))(a.getMappedRange()),n,t,0),a.unmap(),ou.set(n,{buf:a,bytes:o,dtype:t,persistent:e}),a}let i=RB(o);return ou.set(n,{buf:i,bytes:o,dtype:t,persistent:!1}),i}function eh(n,t,e){let{pipeline:s,bindGroupLayout:r}=BB(ce,n),{bindings:o,dispatchSize:i}=n.metadata,a=kB(o),c=sa(),l=[],u=[];for(let d of o){if(d.name==="_shapes"){let _=new Uint32Array(e||[]),y=Math.max(Math.ceil(_.byteLength/16)*16,16),b=ce.createBuffer({size:y,usage:c.UNIFORM|c.COPY_DST});ce.queue.writeBuffer(b,0,_),ry.push(b),l.push({binding:d.index,resource:{buffer:b}});continue}if(d.packed){let _=xn(d.dtype),y=Math.max(ir(d.packedSize*_),4),b=RB(y);oy.push({buf:b,bytes:y});let x=d.mode==="read_write";for(let S of d.packed){let v=yB(t[a.get(S.name)],S.dtype,!x),w=ir(S.size*_);x?u.push({pbuf:b,off:S.offset*_,sub:v,bytes:w}):Tn.copyBufferToBuffer(v,0,b,S.offset*_,w)}l.push({binding:d.index,resource:{buffer:b}});continue}let m=t[a.get(d.name)],g=yB(m,d.dtype,d.mode!=="read_write");l.push({binding:d.index,resource:{buffer:g}})}let h=ce.createBindGroup({layout:r,entries:l}),p=Tn.beginComputePass();p.setPipeline(s),p.setBindGroup(0,h),p.dispatchWorkgroups(i[0],i[1],i[2]),p.end();for(let d of u)Tn.copyBufferToBuffer(d.pbuf,d.off,d.sub,0,d.bytes);++ny>=yT&&(ce.queue.submit([Tn.finish()]),Tn=ce.createCommandEncoder(),ny=0)}async function xT(){if(!ce)return;let n=sa(),t=[];for(let[e,s]of ou){if(s.persistent)continue;let r=ce.createBuffer({size:s.bytes,usage:n.MAP_READ|n.COPY_DST});Tn.copyBufferToBuffer(s.buf,0,r,0,s.bytes),t.push({rb:r,ha:e,buf:s.buf,bytes:s.bytes,dtype:s.dtype})}ce.queue.submit([Tn.finish()]),Tn=ce.createCommandEncoder(),ny=0;for(let e of ry)e.destroy();ry.length=0,await Promise.all(t.map(e=>e.rb.mapAsync(uy())));for(let e of t)ay(e.ha,new(ea(e.dtype))(e.rb.getMappedRange()),e.dtype,0,e.ha.length),e.rb.unmap(),e.rb.destroy();for(let e of oy)_B(e.buf,e.bytes);oy.length=0;for(let e of t)_B(e.buf,e.bytes),ou.delete(e.ha)}function ST(n,t,e,s,r){let o=`${n.join(",")}|${t.join(",")}|${e}|${s}`,i=bB.get(o);if(i)return i;let a=ls(s),c="";for(let u=n.length-1;u>=0;u--)c+=` let idx${u} = rem % ${n[u]}u; rem = rem / ${n[u]}u; src = src + idx${u} * ${t[u]}u;
|
|
144
|
-
`;return i={name:"contig",source:`@group(0) @binding(0) var<storage, read> inp : array<${a}>;
|
|
145
|
-
@group(0) @binding(1) var<storage, read_write> outp : array<${a}>;
|
|
146
|
-
@compute @workgroup_size(64)
|
|
147
|
-
fn contig(@builtin(global_invocation_id) gid : vec3<u32>) {
|
|
148
|
-
let i = gid.x;
|
|
149
|
-
if (i >= ${r}u) { return; }
|
|
150
|
-
var rem = i;
|
|
151
|
-
var src = ${e}u;
|
|
152
|
-
`+c+` outp[i] = inp[src];
|
|
153
|
-
}`,metadata:{bindings:[{index:0,name:"inp",dtype:s,mode:"read"},{index:1,name:"outp",dtype:s,mode:"read_write"}],dispatchSize:[Math.ceil(r/64),1,1]}},bB.set(o,i),i}function Jp(n){let t=n._impl.storage.rawData;if(n.isContiguous&&n._impl.storageOffset===0&&t.length===n.numel)return t;let e=new t.constructor(n.numel);return eh(ST(n.shape,n.strides,n._impl.storageOffset,n.dtype,n.numel),[t,e],void 0),e}function vT(n,t,e){let s=t.map(Jp);s.push(e),eh(n,s,void 0)}function BT(n,t,e){let s=`lstm|${n}|${t}|${e}`,r=wB.get(s);if(r)return r;let o=4*t,i=o*n,a=o*n+o*t,c=a+o;return r={name:"rnn",source:`fn sig(x: f32) -> f32 { return 1.0 / (1.0 + exp(-x)); }
|
|
154
|
-
@group(0) @binding(0) var<storage, read> xin : array<f32>;
|
|
155
|
-
@group(0) @binding(1) var<storage, read> w : array<f32>;
|
|
156
|
-
@group(0) @binding(2) var<storage, read> h0 : array<f32>;
|
|
157
|
-
@group(0) @binding(3) var<storage, read> c0 : array<f32>;
|
|
158
|
-
@group(0) @binding(4) var<storage, read_write> ys : array<f32>;
|
|
159
|
-
@group(0) @binding(5) var<storage, read_write> hn : array<f32>;
|
|
160
|
-
@group(0) @binding(6) var<storage, read_write> cn : array<f32>;
|
|
161
|
-
var<workgroup> wh : array<f32, ${t}>;
|
|
162
|
-
var<workgroup> wc : array<f32, ${t}>;
|
|
163
|
-
var<workgroup> wg : array<f32, ${o}>;
|
|
164
|
-
@compute @workgroup_size(${su})
|
|
165
|
-
fn rnn(@builtin(local_invocation_id) lid : vec3<u32>) {
|
|
166
|
-
let tid = lid.x;
|
|
167
|
-
for (var p = tid; p < ${t}u; p = p + ${su}u) { wh[p] = h0[p]; wc[p] = c0[p]; }
|
|
168
|
-
workgroupBarrier();
|
|
169
|
-
for (var t = 0u; t < ${e}u; t = t + 1u) {
|
|
170
|
-
for (var j = tid; j < ${o}u; j = j + ${su}u) {
|
|
171
|
-
var acc = w[${a}u + j] + w[${c}u + j];
|
|
172
|
-
let xb = t * ${n}u;
|
|
173
|
-
let wxb = j * ${n}u;
|
|
174
|
-
for (var e = 0u; e < ${n}u; e = e + 1u) { acc = acc + xin[xb + e] * w[wxb + e]; }
|
|
175
|
-
let whb = ${i}u + j * ${t}u;
|
|
176
|
-
for (var q = 0u; q < ${t}u; q = q + 1u) { acc = acc + wh[q] * w[whb + q]; }
|
|
177
|
-
wg[j] = acc;
|
|
178
|
-
}
|
|
179
|
-
workgroupBarrier();
|
|
180
|
-
for (var j = tid; j < ${t}u; j = j + ${su}u) {
|
|
181
|
-
let ii = sig(wg[j]);
|
|
182
|
-
let ff = sig(wg[${t}u + j]);
|
|
183
|
-
let gg = tanh(wg[${2*t}u + j]);
|
|
184
|
-
let oo = sig(wg[${3*t}u + j]);
|
|
185
|
-
let cv = ff * wc[j] + ii * gg;
|
|
186
|
-
wc[j] = cv;
|
|
187
|
-
let nh = oo * tanh(cv);
|
|
188
|
-
wh[j] = nh;
|
|
189
|
-
ys[t * ${t}u + j] = nh;
|
|
190
|
-
}
|
|
191
|
-
workgroupBarrier();
|
|
192
|
-
}
|
|
193
|
-
for (var p = tid; p < ${t}u; p = p + ${su}u) { hn[p] = wh[p]; cn[p] = wc[p]; }
|
|
194
|
-
}`,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]}},wB.set(s,r),r}function kT(n,t,e){let s=`${t}|${e}`;if(n._webgpuPacked&&n._webgpuPackedTag===s)return n._webgpuPacked;let r=4*e,o=f(h=>h.contiguous()._impl.storage.rawData,"raw"),i=o(n.x2h.weight),a=o(n.h2h.weight),c=n.x2h.bias?o(n.x2h.bias):new Float32Array(r),l=n.h2h.bias?o(n.h2h.bias):new Float32Array(r),u=new Float32Array(r*t+r*e+r+r);return u.set(i,0),u.set(a,r*t),u.set(c,r*t+r*e),u.set(l,r*t+r*e+r),n._webgpuPacked=u,n._webgpuPackedTag=s,u}function AT(n,t,e,s,r){if(e.batch!==1)return null;let o=e.hiddenSize,i=e.seqLen,a=t.length,c=Jp(n),l=[],u=[];for(let d=0;d<a;d++){let m=d===0?e.inputSize:o,g=kT(t[d],m,o),_,y;s!=null&&r!=null?(_=Jp(as(s,0,d)),y=Jp(as(r,0,d))):(_=new Float32Array(o),y=new Float32Array(o));let b=new Float32Array(i*o),x=new Float32Array(o),S=new Float32Array(o);eh(BT(m,o,i),[c,g,_,y,b,x,S],void 0),c=b,l.push(x),u.push(S)}let h=jt(c,[i,1,o],"f32",ji),p=f(d=>a===1?jt(d[0],[1,1,o],"f32",ji):En(d.map(m=>jt(m,[1,o],"f32",ji)),0),"wrapState");return[h,p(l),p(u)]}var ce,nu,ru,Zp,Xp,xB,gB,th,Tn,ny,yT,ou,ry,oy,iy,bB,su,wB,NB=T(()=>{As();Pi();hn();tn();$t();Ye();ce=null,nu=null,ru=null,Zp=null,Xp=null,xB=null,gB=!1;f(ea,"wgslViewCtor");f(Qp,"packTensorInto");f(ay,"unpackTensorFrom");f(ir,"align4");f(cy,"ensureDevice");f(sa,"bufUsage");f(uy,"mapModeRead");f(hT,"resetDevice");f(dT,"bindingBufferType");f(SB,"pipelineParts");f(vB,"createPipeline");th=new WeakMap;f(BB,"pipelineFor");f(mT,"instantiateWebGPU");f(kB,"buildParamIndex");f(gT,"runWebGPUKernel");f(AB,"prewarmPipelines");f(_T,"runWebGPUPlan");Tn=null,ny=0,yT=64,ou=new Map,ry=[],oy=[],iy=new Map;f(RB,"acquireStorage");f(_B,"releaseStorage");f(bT,"ensureWebGPUEager");f(wT,"webgpuEagerReady");f(yB,"eagerBufferFor");f(eh,"recordWebGPUEager");f(xT,"flushWebGPUEager");bB=new Map;f(ST,"contigKernel");f(Jp,"webgpuEagerInput");f(vT,"webgpuEagerOp");su=256,wB=new Map;f(BT,"lstmKernelWGSL");f(kT,"lstmPackedWeights");f(AT,"webgpuRNN")});var gy={};Ft(gy,{getBackend:()=>my,hasBackend:()=>RT,preloadCudaRuntime:()=>NT,preloadWebGPU:()=>sh,registerBackend:()=>au});function au(n,t){dy.set(n,t)}function my(n){return dy.get(n)||null}function RT(n){return dy.has(n)}function OT(){return ly||(ly=Promise.resolve().then(()=>(fS(),lS))),ly}function hy(){return fy||(fy=Promise.resolve().then(()=>(Yp(),Hp))),fy}async function NT(){if(iu)return iu;let n=await hy();return n.preloadCublas&&await n.preloadCublas(),iu=n,n}async function sh(){if(py)return py;let n=await Promise.resolve().then(()=>(NB(),OB)),{setWebGPUEagerFn:t,setWebgpuRNN:e}=await Promise.resolve().then(()=>(hn(),TB));return await n.ensureWebGPUEager(),t(n.webgpuEagerOp),e(n.webgpuRNN),py=n,n}function EB(n){let t;try{t=ep(n.source)}catch(o){throw new Error("encodeWat: "+o.message+`
|
|
195
|
-
`+n.source)}let e;try{e=new WebAssembly.Module(t)}catch(o){throw new Error("WASM: "+o.message+`
|
|
196
|
-
`+n.source)}let s={};if(n.metadata.imports)for(let[o]of n.metadata.imports)s[o]=ET[o]||Math[o]||(i=>i);let r=new WebAssembly.Instance(e,{math:s});return{exports:r.exports,memory:r.exports.memory,bufferOffsets:n.metadata.bufferOffsets,funcName:n.name,binary:t,parallel:n.metadata.parallel||null,mathNames:n.metadata.imports?[...n.metadata.imports.keys()]:[]}}function nh(n,t,e,s,r){let{exports:o,memory:i,bufferOffsets:a,funcName:c}=n,l=o[c],u=[...a.values()],h=Math.min(u.length,t.length);for(let d=0;d<h;d++){let m=t[d];ArrayBuffer.isView(m)&&new m.constructor(i.buffer,u[d],m.length).set(m)}let p=u.slice(0,h);if(e)for(let d of e)p.push(d);s!==void 0&&r!==void 0&&p.push(s,r),l(...p);for(let d=0;d<h;d++){let m=t[d];ArrayBuffer.isView(m)&&m.set(new m.constructor(i.buffer,u[d],m.length))}}function TT(n,t,e=[],s={}){let r=s.warmup??5,o=s.repeat??30,i=EB(n),a=t.map(h=>new Float32Array(Math.max(1,Math.ceil(h/4)))),c=e||[],l=f(()=>{i.parallel?nh(i,a,c,0,i.parallel.extent):nh(i,a,c)},"once");for(let h=0;h<r;h++)l();let u=[];for(let h=0;h<o;h++){let p=performance.now();l(),u.push(performance.now()-p)}return u}var dy,ly,fy,iu,py,ET,rh=T(()=>{Bg();rp();dy=new Map;f(au,"registerBackend");f(my,"getBackend");f(RT,"hasBackend");ly=null;f(OT,"getWasmPool");fy=null;f(hy,"getCudaRuntime");iu=null;f(NT,"preloadCudaRuntime");py=null;f(sh,"preloadWebGPU");ET={exp:Math.exp,log:Math.log,sin:Math.sin,cos:Math.cos,tan:Math.tan,tanh:Math.tanh,pow:Math.pow,fmod:f((n,t)=>n%t,"fmod"),rsqrt:f(n=>1/Math.sqrt(n),"rsqrt"),sign:Math.sign,round:Math.round};f(EB,"instantiateWasm");f(nh,"runWasmKernel");au("js",{instantiate(n){return new Function("return "+n.source)()},runSync(n,t,e){let s=e?[...t,...e]:t;return n(...s)},runAsync(n,t,e){let s=e?[...t,...e]:t;return n(...s)},isAsync(){return!1}});au("wasm",{instantiate(n){return EB(n)},runSync(n,t,e){n.parallel?nh(n,t,e,0,n.parallel.extent):nh(n,t,e)},async runAsync(n,t,e){if(n.parallel&&n.parallel.poolSafe){let{runWasmParallel:s}=await OT();await s(n,n.funcName,t,e,n.parallel,n.mathNames);return}this.runSync(n,t,e)},isAsync(n){return!!(n&&n.parallel&&n.parallel.poolSafe)}});f(TT,"measureWasm");np("wasm",TT);au("webgpu",{instantiate(n){return sh().then(t=>t.instantiateWebGPU(n))},runSync(){throw new Error("WebGPU kernel requires async execution \u2014 use runAsync()")},async runAsync(n,t,e){let{runWebGPUKernel:s}=await sh();await s(n,t,e)},async runPlan(n,t,e){let{runWebGPUPlan:s}=await sh();await s(n,t,e)},isAsync(){return!0}});au("cuda",{instantiate(n){return{kernel:n}},runSync(n,t,e){if(!iu)throw new Error("CUDA sync runtime not preloaded \u2014 call preloadCudaRuntime() before synchronous execution");iu.runCudaKernelResident(n.kernel,t,e)},async runAsync(n,t,e){let{runCudaKernel:s}=await hy();await s(n.kernel,t,e)},async runPlan(n,t,e){let{runCudaPlan:s}=await hy();await s(n,t,e)},isAsync(){return!0}})});function _y(n){return IT[n]||Float32Array}function MT(n){return n instanceof Float64Array?"f64":n instanceof Int32Array?"i32":n instanceof Int16Array?"i16":n instanceof Int8Array?"i8":n instanceof Uint16Array?"ui16":n instanceof Uint8Array?"ui8":n instanceof BigInt64Array?"i64":"f32"}function IB(n,t){if(!(!n.returnFixups||n.returnFixups.length===0))for(let e of n.returnFixups){let s=t[n.argSlots[e.pos]];if(!(!s||!s.data))if(e.kind==="copy"){let r=t[e.srcSlot];r&&r.data&&s.data.set(r.data.subarray(0,s.data.length))}else e.kind==="const"&&s.data.fill(e.value)}}var IT,ar,yy,na,oh=T(()=>{rh();Oc();IT={f16:Uint16Array,bf16:Uint16Array,f32:Float32Array,f64:Float64Array,i8:Int8Array,i16:Int16Array,i32:Int32Array,i64:BigInt64Array,ui8:Uint8Array,bool:Uint8Array,index:Int32Array};f(_y,"typedArrayCtor");f(MT,"dtypeOfTypedArray");f(IB,"_applyReturnFixups");ar=class n{static{f(this,"RuntimeTensor")}constructor(t,e,s,r=null){this.data=t,this.shape=e,this.dtype=s,this.strides=r||n.defaultStrides(e)}static defaultStrides(t){let e=new Array(t.length),s=1;for(let r=t.length-1;r>=0;r--)e[r]=s,s*=t[r];return e}get numel(){let t=1;for(let e=0;e<this.shape.length;e++)t*=this.shape[e];return t}get rank(){return this.shape.length}static zeros(t,e="f32"){let s=1;for(let r=0;r<t.length;r++)s*=t[r];return s=Math.max(s,1),new n(new(_y(e))(s),t,e)}static fromArray(t,e,s="f32"){let r=_y(s);return new n(new r(t),e,s)}get(t){let e=0;for(let s=0;s<t.length;s++)e+=t[s]*this.strides[s];return this.data[e]}set(t,e){let s=0;for(let r=0;r<t.length;r++)s+=t[r]*this.strides[r];this.data[s]=e}},yy=class{static{f(this,"KernelRegistry")}constructor(){this._kernels=new Map}register(t,e){this._kernels.set(t,e)}get(t){return this._kernels.get(t)||null}has(t){return this._kernels.has(t)}names(){return[...this._kernels.keys()]}},na=class n{static{f(this,"RuntimeModule")}constructor(t){this.name=t,this.kernels=new yy,this._instances=new Map}addCompiledKernel(t){this.kernels.register(t.name,t);let e=my(t.metadata.kind);if(!e)throw new Error("No runtime backend registered for kind: "+t.metadata.kind);this._instances.set(t.name,{backend:e,instance:e.instantiate(t)})}setShapeParamMap(t,e,s){this._shapeParamMaps||(this._shapeParamMaps=new Map),this._shapeParamMaps.set(t,e),s&&(this._bufferMaps||(this._bufferMaps=new Map),this._bufferMaps.set(t,s))}_prepareArgs(t,e){let s=[],r=new Map;for(let a=0;a<e.length;a++)e[a]instanceof ar?(s.push(e[a].data),r.set(a,e[a].shape)):s.push(e[a]);let o=this._shapeParamMaps&&this._shapeParamMaps.get(t),i=null;if(o&&o.size>0){let a=this._bufferMaps&&this._bufferMaps.get(t);i=n._extractShapeParams(o,r,e,a)}return{tensorArgs:s,shapeValues:i}}run(t,...e){let s=this._instances.get(t);if(!s)throw new Error("Kernel '"+t+"' not found or not executable");if(s.instance instanceof Promise)throw new Error("Kernel '"+t+"' requires async execution \u2014 use runAsync()");let{tensorArgs:r,shapeValues:o}=this._prepareArgs(t,e);return s.backend.runSync(s.instance,r,o)}async runAsync(t,...e){let s=this._instances.get(t);if(!s)throw new Error("Kernel '"+t+"' not found or not executable");let{tensorArgs:r,shapeValues:o}=this._prepareArgs(t,e),i=await s.instance;return s.backend.runAsync(i,r,o)}isAsync(t){let e=this._instances.get(t);if(!e)return!1;let s=e.instance instanceof Promise?null:e.instance;return e.backend.isAsync(s)}async runPlanAsync(t,e){let s=new Array(t.numSlots).fill(null);for(let o=0;o<e.length;o++){let i=e[o];s[t.argSlots[o]]=i instanceof ar?i:new ar(i,[i.length],MT(i))}for(let o of t.intermediates){let i=1;for(let a of o.shape)i*=a;s[o.slot]=new ar(new(_y(o.dtype))(Math.max(i,1)),o.shape,o.dtype)}for(let o of t.steps){let i=this._instances.get(o.name);i&&i.instance instanceof Promise&&(i.instance=await i.instance)}let r=this._uniformPlanBackend(t);if(r&&r.runPlan){let o=t.steps.map(i=>{let a=[];for(let l of i.inputSlots)a.push(s[l]);for(let l of i.outputSlots)a.push(s[l]);let{shapeValues:c}=this._prepareArgs(i.name,a);return{name:i.name,inputSlots:i.inputSlots,outputSlots:i.outputSlots,kernel:this.kernels.get(i.name),shapeValues:c}});await r.runPlan(t,s,o),IB(t,s);return}for(let o of t.steps){let i=[];for(let a of o.inputSlots)i.push(s[a]);for(let a of o.outputSlots)i.push(s[a]);await this.runAsync(o.name,...i)}IB(t,s)}_uniformPlanBackend(t){let e=null;for(let s of t.steps){let r=this._instances.get(s.name);if(!r||r.instance instanceof Promise)return null;if(e===null)e=r.backend;else if(r.backend!==e)return null}return e}static _extractShapeParams(t,e,s,r){let o=new Map;if(r){let c=0;for(let[l,u]of r){let h=typeof l=="string"?l:u&&u.name;h!==void 0&&o.set(h,c),c++}}let i=new Map,a=[];for(let[c,l]of t){if(i.has(l.name))continue;i.set(l.name,!0);let u=c.lastIndexOf(":"),h=c.substring(0,u),p=parseInt(c.substring(u+1),10),d=null;if(o.has(h)){let m=e.get(o.get(h));m&&p<m.length&&m[p]>0&&(d=m[p])}if(d===null){for(let[,m]of e)if(p<m.length&&m[p]>0){d=m[p];break}}a.push(d!==null?d:1)}return a}getKernelSource(t){let e=this.kernels.get(t);return e?e.source:null}getKernelSnippet(t){let e=this.kernels.get(t);return e?e.snippet():null}listKernels(){return this.kernels.names()}serialize(){let t=[];for(let e of this.kernels.names()){let s=this.kernels.get(e);t.push({name:s.name,source:s.source,target:s.target.name,metadata:s.metadata})}return{name:this.name,kernels:t}}static deserialize(t){let e=new n(t.name);for(let s of t.kernels)e.addCompiledKernel(new Rc(s.name,s.source,{name:s.target},s.metadata));return e}}});var ih,MB=T(()=>{ih=class{static{f(this,"AnalysisManager")}constructor(){this._cache=new WeakMap}getAnalysis(t,e){let s=this._cache.get(e);s||(s=new Map,this._cache.set(e,s));let r=s.get(t);if(!r||r.version!==e.version){let o=this._resolveDeps(t,e,s);r={data:t.compute(e,o),version:e.version},s.set(t,r)}return r.data}_resolveDeps(t,e,s){let r=t.dependencies;if(!r||r.length===0)return{};let o={};for(let i of r){let a=i.depKey||i.name;o[a]=this.getAnalysis(i,e)}return o}invalidate(t,e=null){let s=this._cache.get(t);if(!s)return;if(!e){this._cache.delete(t);return}let r=[];for(let o of s.keys())if(!e.has(o)&&!e.has(o.name))r.push(o);else{let i=s.get(o);i&&(i.version=t.version)}for(let o of r)s.delete(o),this._cascadeInvalidate(s,o)}_cascadeInvalidate(t,e){for(let[s]of t){let r=s.dependencies;r&&r.includes(e)&&(t.delete(s),this._cascadeInvalidate(t,s))}}invalidateFunctions(t,e=null){for(let s of t)this.invalidate(s,e)}invalidateAll(){this._cache=new WeakMap}}});function cu(n){if(typeof n.numOps=="function")return n.numOps();if(typeof n[Symbol.iterator]=="function"){let t=0;for(let e of n)typeof e.numOps=="function"&&(t+=e.numOps());return t}return-1}var ra,Wo,by=T(()=>{MB();Ce();Ue();Ue();f(cu,"countOps");ra=class{static{f(this,"FixedPointGroup")}constructor(t,e,s=8){this.name=t,this.passes=e,this.maxIterations=s}},Wo=class{static{f(this,"PassManager")}constructor(){this.passes=[],this.analysisManager=new ih,this.trace=null,this.verifyHook=null,this.instruments=[]}addPass(t){this.passes.push(t)}addInstrument(t){this.instruments.push(t)}_notify(t,e,s,r){for(let o of this.instruments)typeof o[t]=="function"&&o[t](e,s,r)}setTrace(t){this.trace=t}setVerifyHook(t){this.verifyHook=t}_verifyAfter(t,e,s){if(!this.verifyHook)return null;let r=this.verifyHook(e,s);if(!r||r.length===0)return null;let o=s?e.name||"<module>":e.name;return new qe("verification",o,`pass '${t.name}' produced invalid IR: ${r.join("; ")}`,t.name)}_applyPass(t,e,s,r){this.trace&&(t.trace=this.trace),this.instruments.length&&this._notify("runBeforePass",t,e,null);let o=s.verbose,i=s.resilient,a=!1,c=!1;if(t instanceof sf){let l=o?cu(e):-1,u=o?performance.now():0,h;try{h=t.run(e,this.analysisManager)}catch(p){if(!i)throw p;return this.analysisManager.invalidateAll(),r.push(Q.FAILED),s.errors.push(new qe("graphPasses",e.name||"<module>",p.message,t.name)),{changed:a,fatal:!1}}if(r.push(h),o&&this.trace.passRun(t.name,h,performance.now()-u,l,cu(e)),h===Q.CHANGED){a=!0,s.anyChanged=!0,this.analysisManager.invalidateFunctions(e,t.preservedAnalyses);let p=this._verifyAfter(t,e,!0);p&&(s.errors.push(p),i||(c=!0))}else h===Q.FAILED&&(this.analysisManager.invalidateAll(),s.errors.push(new qe("graphPasses",e.name||"<module>",`pass '${t.name}' failed`,t.name)),i||(c=!0))}else if(t instanceof Rt){let l=!1;for(let u of e){if(s.failedFunctions.has(u.name))continue;let h=o?cu(u):-1,p=o?performance.now():0;if(i)try{for(let m of t.requiredAnalyses)this.analysisManager.getAnalysis(m,u);let d=t.run(u,this.analysisManager);if(o&&this.trace.passRun(t.name,d,performance.now()-p,h,cu(u)),d===Q.CHANGED){l=!0,s.anyChanged=!0,u.bumpVersion(),this.analysisManager.invalidate(u,t.preservedAnalyses);let m=this._verifyAfter(t,u,!1);m&&(s.errors.push(m),s.failedFunctions.add(u.name))}else d===Q.FAILED&&(this.analysisManager.invalidate(u),s.errors.push(new qe("graphPasses",u.name,`pass '${t.name}' failed`,t.name)),s.failedFunctions.add(u.name))}catch(d){s.errors.push(new qe("graphPasses",u.name,d.message,t.name)),s.failedFunctions.add(u.name)}else{for(let m of t.requiredAnalyses)this.analysisManager.getAnalysis(m,u);let d=t.run(u,this.analysisManager);if(o&&this.trace.passRun(t.name,d,performance.now()-p,h,cu(u)),d===Q.CHANGED){l=!0,s.anyChanged=!0,u.bumpVersion(),this.analysisManager.invalidate(u,t.preservedAnalyses);let m=this._verifyAfter(t,u,!1);if(m){s.errors.push(m),s.failedFunctions.add(u.name),c=!0;break}}else if(d===Q.FAILED){this.analysisManager.invalidate(u),s.errors.push(new qe("graphPasses",u.name,`pass '${t.name}' failed`,t.name)),s.failedFunctions.add(u.name),c=!0;break}}}r.push(l?Q.CHANGED:Q.UNCHANGED),a=l}return this.instruments.length&&this._notify("runAfterPass",t,e,a?Q.CHANGED:Q.UNCHANGED),t.trace=null,{changed:a,fatal:c}}_runGroup(t,e,s,r){let o=t.maxIterations>0?t.maxIterations:1;for(let i=0;i<o;i++){let a=!1;for(let c of t.passes){if(!s.passContext.shouldRun(c))continue;let{changed:l,fatal:u}=this._applyPass(c,e,s,r);if(u)return!0;l&&(a=!0)}if(!a)return!1}return this.trace&&this.trace.passRun(`${t.name}:max-iter`,Q.UNCHANGED,0,-1,-1),!1}run(t,e={}){let s={verbose:this.trace&&this.trace.level>=ot.VERBOSE,resilient:e.errorMode==="resilient",errors:[],failedFunctions:new Set,anyChanged:!1,passContext:e.passContext||new ef},r=[];for(let o of this.passes){if(!(o instanceof ra)&&!s.passContext.shouldRun(o))continue;if(o instanceof ra?this._runGroup(o,t,s,r):this._applyPass(o,t,s,r).fatal)return{changed:s.anyChanged,results:r,errors:s.errors,failedFunctions:s.failedFunctions.size>0?s.failedFunctions:null}}return{changed:s.anyChanged,results:r,errors:s.errors.length>0?s.errors:null,failedFunctions:s.failedFunctions.size>0?s.failedFunctions:null}}}});function Ie(n,t){wy.set(n,t)}function Fe(n,t,e,s){return n.broadcast(n.scalarConstant(t,e).getResult(0),s,[]).getResult(0)}var wy,oa,xy=T(()=>{Ce();zn();kt();Ue();wy=new Map;f(Ie,"registerDecomposition");oa=class extends Rt{static{f(this,"DecompositionPass")}constructor(t=null){super("DecompositionPass"),this.target=t}_shouldDecompose(t){if(!this.target)return!0;let e=this.target.getAttr?this.target.getAttr("nativeOps"):null;return!(e&&e.has(t.opName))}run(t){let e=[];for(let o of t.opsRecursive())wy.has(o.opName)&&this._shouldDecompose(o)&&e.push(o);if(e.length===0)return Q.UNCHANGED;let s=new We(t),r=[];for(let o of e){if(!o.parentBlock)continue;let i=wy.get(o.opName);s.block=o.parentBlock,s.setInsertionPoint(o),r.push(o.opName),i(o,s)}if(this.trace&&this.trace.level>=ot.DEBUG){let o={};for(let i of r)o[i]=(o[i]||0)+1;this.trace.emit({type:"pass_detail",passName:this.name,decomposed:o,totalDecomposed:r.length,level:ot.DEBUG})}return Q.CHANGED}};Ie("stop_gradient",n=>{n.replaceAllResultsWith([n.getOperand(0)]),n.erase()});Ie("all_reduce",(n,t)=>{let e=n.getOperand(0),s=n.getAttr("mesh_axis")??0,r=n.getAttr("reduce_op")||"sum",o=e.type.shape,i=e.type.dtype,a=r==="max"?-1/0:r==="min"?1/0:r==="prod"||r==="and"?1:0,c=t.reduce(e,t.scalarConstant(a,i).getResult(0),[s],r).getResult(0),l=[];for(let h=0;h<o.length;h++)h!==s&&l.push(h);let u=t.broadcast(c,o,l).getResult(0);n.replaceAllResultsWith([u]),n.erase()});Ie("all_gather",(n,t)=>{let e=n.getOperand(0),s=e.type.shape,r=n.getAttr("mesh_axis")??0,o=n.getAttr("gather_dim")??1,i=s[r],a=s.filter((m,g)=>g!==r),c=o<r?o:o-1,l=[];for(let m=0;m<i;m++){let g=s.map((b,x)=>x===r?m:0),_=s.map((b,x)=>x===r?m+1:b),y=t.slice(e,g,_).getResult(0);l.push(t.reshape(y,a).getResult(0))}let u=l.length===1?l[0]:t.concat(l,c).getResult(0),h=[...s];h[o]=i*s[o];let p=[];for(let m=0;m<h.length;m++)m!==r&&p.push(m);let d=t.broadcast(u,h,p).getResult(0);n.replaceAllResultsWith([d]),n.erase()});Ie("softmax",(n,t)=>{let e=n.getOperand(0),s=n.getAttr("axis"),r=e.type.rank,o=e.type.dtype,i=e.type.shape,a=Tf(r,s),c=t.reduce(e,t.scalarConstant(-1/0,o).getResult(0),[s],"max"),l=t.broadcast(c.getResult(0),i,a),u=t.sub(e,l.getResult(0)),h=t.exp(u.getResult(0)),p=t.reduce(h.getResult(0),t.scalarConstant(0,o).getResult(0),[s],"sum"),d=t.broadcast(p.getResult(0),i,a),m=t.div(h.getResult(0),d.getResult(0));n.replaceAllResultsWith([m.getResult(0)]),n.erase()});Ie("log_softmax",(n,t)=>{let e=n.getOperand(0),s=n.getAttr("axis"),r=e.type.rank,o=e.type.dtype,i=e.type.shape,a=Tf(r,s),c=t.reduce(e,t.scalarConstant(-1/0,o).getResult(0),[s],"max"),l=t.broadcast(c.getResult(0),i,a),u=t.sub(e,l.getResult(0)),h=t.exp(u.getResult(0)),p=t.reduce(h.getResult(0),t.scalarConstant(0,o).getResult(0),[s],"sum"),d=t.log(p.getResult(0)),m=t.broadcast(d.getResult(0),i,a),g=t.sub(u.getResult(0),m.getResult(0));n.replaceAllResultsWith([g.getResult(0)]),n.erase()});Ie("sigmoid",(n,t)=>{let e=n.getOperand(0),s=e.type.dtype,r=e.type.shape,o=t.neg(e),i=t.exp(o.getResult(0)),a=t.broadcast(t.scalarConstant(1,s).getResult(0),r,[]),c=t.add(a.getResult(0),i.getResult(0)),l=t.div(a.getResult(0),c.getResult(0));n.replaceAllResultsWith([l.getResult(0)]),n.erase()});Ie("gelu",(n,t)=>{let e=n.getOperand(0),s=e.type.dtype,r=e.type.shape,o=t.broadcast(t.scalarConstant(1.702,s).getResult(0),r,[]),i=t.mul(o.getResult(0),e),a=t.neg(i.getResult(0)),c=t.exp(a.getResult(0)),l=t.broadcast(t.scalarConstant(1,s).getResult(0),r,[]),u=t.add(l.getResult(0),c.getResult(0)),h=t.div(l.getResult(0),u.getResult(0)),p=t.mul(e,h.getResult(0));n.replaceAllResultsWith([p.getResult(0)]),n.erase()});Ie("silu",(n,t)=>{let e=n.getOperand(0),s=e.type.dtype,r=e.type.shape,o=t.neg(e),i=t.exp(o.getResult(0)),a=t.broadcast(t.scalarConstant(1,s).getResult(0),r,[]),c=t.add(a.getResult(0),i.getResult(0)),l=t.div(a.getResult(0),c.getResult(0)),u=t.mul(e,l.getResult(0));n.replaceAllResultsWith([u.getResult(0)]),n.erase()});Ie("layer_norm",(n,t)=>{let e=n.getOperand(0),s=n.getOperand(1),r=n.getOperand(2),o=n.getAttr("axis"),i=n.getAttr("epsilon"),a=e.type.rank,c=e.type.dtype,l=e.type.shape,u=Tf(a,o),h=t.reduce(e,t.scalarConstant(0,c).getResult(0),[o],"mean"),p=t.broadcast(h.getResult(0),l,u),d=t.sub(e,p.getResult(0)),m=t.mul(d.getResult(0),d.getResult(0)),g=t.reduce(m.getResult(0),t.scalarConstant(0,c).getResult(0),[o],"mean"),_=t.broadcast(t.scalarConstant(i,c).getResult(0),g.getResult(0).type.shape,[]),y=t.add(g.getResult(0),_.getResult(0)),b=t.rsqrt(y.getResult(0)),x=t.broadcast(b.getResult(0),l,u),S=t.mul(d.getResult(0),x.getResult(0)),v=t.broadcast(s,l,[o]),w=t.mul(S.getResult(0),v.getResult(0)),A=t.broadcast(r,l,[o]),I=t.add(w.getResult(0),A.getResult(0));n.replaceAllResultsWith([I.getResult(0)]),n.erase()});Ie("batch_norm",(n,t)=>{let e=n.getOperand(0),s=n.getOperand(1),r=n.getOperand(2),o=n.getOperand(3),i=n.getOperand(4),a=n.getAttr("axis"),c=n.getAttr("epsilon"),l=e.type.rank,u=e.type.dtype,h=e.type.shape,p=t.broadcast(t.scalarConstant(c,u).getResult(0),i.type.shape,[]),d=t.add(i,p.getResult(0)),m=t.rsqrt(d.getResult(0)),g=t.broadcast(o,h,[a]),_=t.sub(e,g.getResult(0)),y=t.broadcast(m.getResult(0),h,[a]),b=t.mul(_.getResult(0),y.getResult(0)),x=t.broadcast(s,h,[a]),S=t.mul(b.getResult(0),x.getResult(0)),v=t.broadcast(r,h,[a]),w=t.add(S.getResult(0),v.getResult(0));n.replaceAllResultsWith([w.getResult(0)]),n.erase()});Ie("where",(n,t)=>{let e=n.getOperand(0);if(e.type.dtype!==J.BOOL){let r=t.broadcast(t.scalarConstant(0,e.type.dtype).getResult(0),e.type.shape,[]);e=t.compare(e,r.getResult(0),"ne").getResult(0)}let s=t.select(e,n.getOperand(1),n.getOperand(2));n.replaceAllResultsWith([s.getResult(0)]),n.erase()});Ie("split",(n,t)=>{let e=n.getOperand(0),s=n.getAttr("dimension"),r=n.getAttr("split_sizes"),o=e.type.shape,i=[],a=0;for(let c of r){let l=o.map((h,p)=>p===s?a:0),u=o.map((h,p)=>p===s?a+c:h);i.push(t.slice(e,l,u).getResult(0)),a+=c}n.replaceAllResultsWith(i),n.erase()});Ie("one_hot",(n,t)=>{let e=n.getOperand(0),s=n.getAttr("depth"),r=n.getAttr("axis")??-1,o=n.getAttr("on_value")??1,i=n.getAttr("off_value")??0,a=n.getResult(0).type,c=a.dtype,l=a.shape,u=r<0?e.type.rank+1+r:r,h=new E(l,J.I32),p=t._inferAndBuild("iota",[],{iota_dimension:u,tensor_type:h}),d=e.type.rank,m=[];for(let v=0;v<d;v++)m.push(v<u?v:v+1);let g=t.broadcast(e,l,m),_=t.convert(g.getResult(0),J.I32),y=t.compare(_.getResult(0),p.getResult(0),"eq"),b=t.broadcast(t.scalarConstant(o,c).getResult(0),l,[]),x=t.broadcast(t.scalarConstant(i,c).getResult(0),l,[]),S=t.select(y.getResult(0),b.getResult(0),x.getResult(0));n.replaceAllResultsWith([S.getResult(0)]),n.erase()});f(Fe,"bcast");Ie("elu",(n,t)=>{let e=n.getOperand(0),s=n.getAttr("alpha")??1,r=e.type.dtype,o=e.type.shape,i=Fe(t,0,r,o),a=t.compare(e,i,"gt").getResult(0),c=Fe(t,1,r,o),l=t.exp(e).getResult(0),u=t.sub(l,c).getResult(0),h=Fe(t,s,r,o),p=t.mul(h,u).getResult(0),d=t.select(a,e,p);n.replaceAllResultsWith([d.getResult(0)]),n.erase()});Ie("leaky_relu",(n,t)=>{let e=n.getOperand(0),s=n.getAttr("negative_slope")??.01,r=e.type.dtype,o=e.type.shape,i=Fe(t,0,r,o),a=t.compare(e,i,"gt").getResult(0),c=Fe(t,s,r,o),l=t.mul(c,e).getResult(0),u=t.select(a,e,l);n.replaceAllResultsWith([u.getResult(0)]),n.erase()});Ie("celu",(n,t)=>{let e=n.getOperand(0),s=n.getAttr("alpha")??1,r=e.type.dtype,o=e.type.shape,i=Fe(t,0,r,o),a=t.maximum(e,i).getResult(0),c=Fe(t,s,r,o),l=t.div(e,c).getResult(0),u=t.exp(l).getResult(0),h=Fe(t,1,r,o),p=t.sub(u,h).getResult(0),d=t.mul(c,p).getResult(0),m=t.minimum(i,d).getResult(0),g=t.add(a,m);n.replaceAllResultsWith([g.getResult(0)]),n.erase()});Ie("selu",(n,t)=>{let e=n.getOperand(0),s=e.type.dtype,r=e.type.shape,o=1.0507009873554805,i=1.6732632423543772,a=Fe(t,0,s,r),c=t.compare(e,a,"gt").getResult(0),l=Fe(t,1,s,r),u=t.exp(e).getResult(0),h=t.sub(u,l).getResult(0),p=Fe(t,i,s,r),d=t.mul(p,h).getResult(0),m=t.select(c,e,d).getResult(0),g=Fe(t,o,s,r),_=t.mul(g,m);n.replaceAllResultsWith([_.getResult(0)]),n.erase()});Ie("mish",(n,t)=>{let e=n.getOperand(0),s=e.type.dtype,r=e.type.shape,o=Fe(t,1,s,r),i=t.exp(e).getResult(0),a=t.add(o,i).getResult(0),c=t.log(a).getResult(0),l=t.tanh(c).getResult(0),u=t.mul(e,l);n.replaceAllResultsWith([u.getResult(0)]),n.erase()});Ie("hardswish",(n,t)=>{let e=n.getOperand(0),s=e.type.dtype,r=e.type.shape,o=Fe(t,3,s,r),i=Fe(t,0,s,r),a=Fe(t,6,s,r),c=t.add(e,o).getResult(0),l=t.minimum(t.maximum(c,i).getResult(0),a).getResult(0),u=t.div(l,a).getResult(0),h=t.mul(e,u);n.replaceAllResultsWith([h.getResult(0)]),n.erase()});Ie("hardsigmoid",(n,t)=>{let e=n.getOperand(0),s=e.type.dtype,r=e.type.shape,o=Fe(t,6,s,r),i=Fe(t,.5,s,r),a=Fe(t,0,s,r),c=Fe(t,1,s,r),l=t.div(e,o).getResult(0),u=t.add(l,i).getResult(0),h=t.minimum(t.maximum(u,a).getResult(0),c);n.replaceAllResultsWith([h.getResult(0)]),n.erase()});Ie("embedding",(n,t)=>{let e=n.getOperand(0),s=n.getOperand(1),r=e.type.shape[e.type.rank-1],o=s.type.rank,i=t._inferAndBuild("gather",[e,s],{offset_dims:Array.from({length:1},(a,c)=>o+c),collapsed_slice_dims:[0],start_index_map:[0],slice_sizes:[1,r],index_vector_dim:o});n.replaceAllResultsWith([i.getResult(0)]),n.erase()})});function CT(){if(uu)return uu;uu=new gi;for(let n of st.allOps())if(n.getCanonicalizationPatterns){let t=n.getCanonicalizationPatterns();if(t)for(let e of t)uu.add(e)}return uu}var uu,jo,Sy=T(()=>{Ce();ac();Ee();uu=null;f(CT,"getCanonicalizationPatterns");jo=class extends Rt{static{f(this,"CanonicalizePass")}constructor(){super("canonicalize")}run(t,e){let s=CT();return new _i(s).applyPatterns(t,10,this.trace)}}});var ah,vy,ia,CB=T(()=>{Ee();we();ah=class{static{f(this,"MemoryEffect")}constructor(t,e){this.kind=t,this.value=e}},vy=class{static{f(this,"MemoryEffectResult")}constructor(t,e){this.opEffects=t,this._valueEffects=e}hasSideEffect(t){let e=this.opEffects.get(t);return e&&e.length>0}getEffects(t){return this.opEffects.get(t)||[]}getEffectsOn(t){return this._valueEffects.get(t)||[]}getReadersOf(t){let e=this._valueEffects.get(t);if(!e)return[];let s=[];for(let r=0;r<e.length;r++)e[r].effect.kind===Me.READ&&s.push(e[r].op);return s}getWritersOf(t){let e=this._valueEffects.get(t);if(!e)return[];let s=[];for(let r=0;r<e.length;r++)e[r].effect.kind===Me.WRITE&&s.push(e[r].op);return s}},ia=class{static get name(){return"memory_effect"}static get depKey(){return"memoryEffect"}static get dependencies(){return[]}static compute(t){let e=new Map,s=new Map,r=f((i,a,c)=>{let l=s.get(i);l||(l=[],s.set(i,l)),l.push({op:a,effect:c})},"addValueEffect"),o=typeof t.opsRecursive=="function"?t.opsRecursive():t.ops();for(let i of o){let a=st.get(i.opName),c=[],l=Me.NONE;if(a&&a.sideEffects?l=a.sideEffects:i.hasSideEffects&&i.hasSideEffects()&&(l=Me.WRITE),l!==Me.NONE)if(a&&a.getMemoryEffects){let u=a.getMemoryEffects(i);for(let h of u)c.push(h),r(h.value,i,h)}else{if(l&Me.READ)for(let u=0;u<i.numOperands;u++){let h=new ah(Me.READ,i.getOperand(u));c.push(h),r(i.getOperand(u),i,h)}if(l&Me.WRITE)for(let u=0;u<i.numResults;u++){let h=new ah(Me.WRITE,i.getResult(u));c.push(h),r(i.getResult(u),i,h)}}e.set(i,c)}return new vy(e,s)}}});var DT,so,By=T(()=>{DT={ceildiv:"ceilDiv"},so=class n{static{f(this,"SymInt")}constructor(t,e=null,s=[]){this.type=t,this.name=e,this.args=s}static var(t){return new n("var",t)}static const(t){return t}static add(t,e){return typeof t=="number"&&typeof e=="number"?t+e:t===0?e:e===0?t:n.equals(t,e)?n.mul(2,t):new n("add",null,[t,e])}static sub(t,e){return typeof t=="number"&&typeof e=="number"?t-e:e===0?t:n.equals(t,e)?0:new n("sub",null,[t,e])}static neg(t){return typeof t=="number"?-t:t instanceof n&&t.type==="neg"?t.args[0]:new n("neg",null,[t])}static mul(t,e){return typeof t=="number"&&typeof e=="number"?t*e:t===0||e===0?0:t===1?e:e===1?t:new n("mul",null,[t,e])}static div(t,e){if(e===0)throw new Error("SymInt.div: division by zero");return typeof t=="number"&&typeof e=="number"?Math.floor(t/e):t===0?0:e===1?t:n.equals(t,e)?1:new n("div",null,[t,e])}static mod(t,e){if(e===0)throw new Error("SymInt.mod: modulo by zero");return typeof t=="number"&&typeof e=="number"?(t%e+e)%e:t===0||e===1||n.equals(t,e)?0:new n("mod",null,[t,e])}static max(t,e){return typeof t=="number"&&typeof e=="number"?Math.max(t,e):t===e||n.equals(t,e)?t:new n("max",null,[t,e])}static min(t,e){return typeof t=="number"&&typeof e=="number"?Math.min(t,e):t===e||n.equals(t,e)?t:new n("min",null,[t,e])}static ceilDiv(t,e){return typeof t=="number"&&typeof e=="number"?Math.ceil(t/e):new n("ceildiv",null,[t,e])}static equals(t,e){if(t===e)return!0;if(typeof t=="number"||typeof e=="number"||!(t instanceof n)||!(e instanceof n)||t.type!==e.type)return!1;if(t.type==="var")return t.name===e.name;if(t.args.length!==e.args.length)return!1;if(t.type==="add"||t.type==="mul"||t.type==="max"||t.type==="min"){let s=n.equals(t.args[0],e.args[0])&&n.equals(t.args[1],e.args[1]),r=n.equals(t.args[0],e.args[1])&&n.equals(t.args[1],e.args[0]);return s||r}for(let s=0;s<t.args.length;s++)if(!n.equals(t.args[s],e.args[s]))return!1;return!0}static substitute(t,e,s){if(typeof t=="number"||!(t instanceof n))return t;if(t.type==="var")return t.name===e?s:t;let r=t.args.map(i=>n.substitute(i,e,s)),o=n[DT[t.type]||t.type];return o&&r.length===2?o(r[0],r[1]):o&&r.length===1?o(r[0]):new n(t.type,t.name,r)}static evaluate(t,e){if(typeof t=="number"||!(t instanceof n))return t;if(t.type==="var"){if(e.has(t.name))return e.get(t.name);throw new Error(`Unbound symbolic variable: ${t.name}`)}let s=t.args.map(r=>n.evaluate(r,e));switch(t.type){case"add":return s[0]+s[1];case"sub":return s[0]-s[1];case"mul":return s[0]*s[1];case"div":return Math.floor(s[0]/s[1]);case"mod":return(s[0]%s[1]+s[1])%s[1];case"max":return Math.max(s[0],s[1]);case"min":return Math.min(s[0],s[1]);case"neg":return-s[0];case"ceildiv":return Math.ceil(s[0]/s[1]);default:throw new Error(`Unknown SymInt op: ${t.type}`)}}static freeVars(t,e=new Set){if(typeof t=="number"||!(t instanceof n))return e;if(t.type==="var")return e.add(t.name),e;for(let s of t.args)n.freeVars(s,e);return e}static isConst(t){return typeof t=="number"}static toConst(t){return typeof t=="number"?t: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"}}});var ky,no,ch=T(()=>{Ee();kt();By();ky=class{static{f(this,"ShapeResult")}constructor(t){this.shapes=t}shapeOf(t){return this.shapes.get(t)||null}},no=class{static get name(){return"shape"}static get depKey(){return"shape"}static get dependencies(){return[]}static compute(t,e){let s=new Map,r=0,o=new Map;if(e&&e.symbols.size>0)for(let[c,l]of e.symbols){let u=`${l.inputIdx}:${l.dimIdx}`;o.set(u,c)}let i=f((c,l)=>c?c.map((u,h)=>{if(u!==Z)return u;if(l!==void 0){let p=o.get(`${l}:${h}`);if(p)return so.var(p)}return so.var(`d${r++}`)}):null,"resolveShape"),a=t.args;for(let c=0;c<a.length;c++)s.set(a[c],i(a[c].type.shape,c));for(let c of t.ops()){let l=st.get(c.opName);if(l&&l.propagateSymbolicShapes){let u=l.propagateSymbolicShapes(c,s);if(u){for(let h=0;h<Math.min(u.length,c.numResults);h++)u[h]&&s.set(c.getResult(h),u[h]);continue}}if(l&&l.inferResultTypes){let u=c.operands.map(p=>p.type),h=l.inferResultTypes(u,c.attributes,c.results.map(p=>p.type));if(h){for(let p=0;p<Math.min(h.length,c.numResults);p++)s.set(c.getResult(p),i(h[p].shape));continue}}for(let u=0;u<c.numResults;u++)s.set(c.getResult(u),i(c.getResult(u).type.shape))}return new ky(s)}}});var cr,Ay=T(()=>{Ce();CB();ch();Ue();cr=class extends Rt{static{f(this,"DCEPass")}constructor(){super("dce"),this.preservedAnalyses=new Set([no]),this.requiredAnalyses=[ia]}run(t,e){let s=!1,r=e?e.getAnalysis(ia,t):ia.compute(t),o=[];for(let a of t.opsRecursive())this._isDead(a,r)&&o.push(a);let i=0;for(;o.length>0;){let a=o.pop();if(!a.parentBlock||!this._isDead(a,r))continue;let c=[];for(let l=0;l<a.numOperands;l++){let u=a.getOperand(l).definingOp;u&&u.parentBlock&&c.push(u)}a.erase(),s=!0,i++;for(let l of c)l.parentBlock&&this._isDead(l,r)&&o.push(l)}return this.trace&&this.trace.level>=ot.DEBUG&&i>0&&this.trace.emit({type:"pass_detail",passName:this.name,erasedCount:i,level:ot.DEBUG}),s?Q.CHANGED:Q.UNCHANGED}_isDead(t,e){if(t.opName==="return"||t.opName==="yield"||t.regions&&t.regions.length>0)return!1;for(let s=0;s<t.numResults;s++)if(t.getResult(s).hasUses)return!1;return!e.hasSideEffect(t)}}});var uh,lh,DB=T(()=>{uh=class n{static{f(this,"ScheduleStep")}constructor(t,e){this.primitive=t,this.args=e}serialize(){return{primitive:this.primitive,args:this.args}}static deserialize(t){return new n(t.primitive,t.args)}},lh=class n{static{f(this,"ScheduleTrace")}constructor(){this.steps=[]}record(t,e){let s=new uh(t,e);return this.steps.push(s),s}serialize(){return this.steps.map(t=>t.serialize())}static deserialize(t){let e=new n;for(let s of t)e.steps.push(uh.deserialize(s));return e}replay(t){for(let e of this.steps){let s=t[e.primitive];if(typeof s!="function")throw new Error(`Unknown schedule primitive: ${e.primitive}`);t._replaying=!0;try{s.call(t,...e.args)}finally{t._replaying=!1}}}get length(){return this.steps.length}clear(){this.steps.length=0}}});function Zt(n,t){if(!(!n||typeof n!="object"))switch(n.type){case"VariableNode":n.name&&t.add(n.name);return;case"BufferLoadNode":if(n.indices)for(let e of n.indices)Zt(e,t);return;case"BufferStoreNode":if(n.indices)for(let e of n.indices)Zt(e,t);Zt(n.value,t);return;case"MathOpNode":Zt(n.a,t),n.b&&Zt(n.b,t);return;case"CompareNode":Zt(n.a,t),Zt(n.b,t);return;case"CastNode":Zt(n.expr,t);return;case"CallExternNode":for(let e of n.args)Zt(e,t);return;case"IfThenElseNode":Zt(n.condition,t),Zt(n.thenBody,t),n.elseBody&&Zt(n.elseBody,t);return;case"SeqNode":for(let e of n.stmts)Zt(e,t);return;case"LetStmtNode":Zt(n.value,t),Zt(n.body,t);return;default:return}}function fh(n,t){let e=new Set((n.writes||[]).map(r=>r.buffer&&r.buffer.name)),s=[n.body,n.initBody];for(;s.length>0;){let r=s.pop();if(!(!r||typeof r!="object")){if(r.type==="BufferStoreNode"&&r.buffer&&e.has(r.buffer.name)&&r.indices)for(let o of r.indices)Zt(o,t);if(r.type==="BufferStoreNode"){r.value&&s.push(r.value);continue}if(r.type==="SeqNode"){for(let o of r.stmts)s.push(o);continue}if(r.type==="IfThenElseNode"){s.push(r.thenBody),r.elseBody&&s.push(r.elseBody);continue}if(r.type==="LetStmtNode"){s.push(r.body);continue}}}}function LT(n,t){let e=new Set;if(Zt(t.body,e),t.initBody&&Zt(t.initBody,e),!e.has(n))return!1;let s=new Set;return fh(t,s),!s.has(n)}function Ko(n,t){if(!(!n||typeof n!="object"))switch(n.type){case"BlockNode":t.push(n),Ko(n.body,t),n.initBody&&Ko(n.initBody,t);return;case"ForNode":case"AllocateNode":case"LetStmtNode":Ko(n.body,t);return;case"SeqNode":for(let e of n.stmts)Ko(e,t);return;case"IfThenElseNode":Ko(n.thenBody,t),n.elseBody&&Ko(n.elseBody,t);return;default:return}}function Ry(n){let t=n.loopVar.name,e=[];Ko(n.body,e);for(let s of e)if(LT(t,s))return s.name;return null}var Oy=T(()=>{f(Zt,"collectVarsUsed");f(fh,"collectWriteIndexVars");f(LT,"loopVarIsReductionOf");f(Ko,"collectBlocksUnder");f(Ry,"loopCarriesReduction")});var aa,Ny=T(()=>{Ct();Oy();aa=class n{static{f(this,"ScheduleValidator")}static validate(t){let e=[],s={boundVars:new Set,threadBindings:new Map,parallelExtents:new Map,parLoops:[],innermostLoopVar:null,loopStack:[],errors:e};for(let r of t.params)s.boundVars.add(r.name);return n._visitNode(t.body,s),n._checkPartitionConsistency(s),e}static _checkPartitionConsistency(t){if(t.parallelExtents.size<=1)return;let e=[...t.parallelExtents.entries()].map(([s,r])=>`'${r}'(extent ${s})`);t.errors.push(`Ambiguous parallel partition: ${t.parallelExtents.size} distinct parallel extents [${e.join(", ")}] \u2014 runtime partitions a single axis, mismatched-extent parallel loops corrupt buffers`)}static _visitNode(t,e){if(t)switch(t.type){case"ForNode":n._visitFor(t,e);break;case"BlockNode":n._visitBlock(t,e);break;case"SeqNode":for(let s of t.stmts)n._visitNode(s,e);break;case"IfThenElseNode":n._visitExpr(t.condition,e),n._visitNode(t.thenBody,e),t.elseBody&&n._visitNode(t.elseBody,e);break;case"AllocateNode":n._visitNode(t.body,e);break;case"LetStmtNode":e.boundVars.add(t.variable.name),n._visitNode(t.body,e),e.boundVars.delete(t.variable.name);break;case"BufferStoreNode":n._validateBufferAccess(t,e);break;case"EvaluateNode":break;default:break}}static _visitFor(t,e){let s=t.loopVar.name;if(e.boundVars.has(s)&&e.errors.push(`Duplicate loop variable: ${s}`),t.kind===V.THREAD_BINDING&&(t.threadTag?(e.threadBindings.has(t.threadTag)&&e.errors.push(`Duplicate thread binding '${t.threadTag}': already bound to '${e.threadBindings.get(t.threadTag)}'`),e.threadBindings.set(t.threadTag,s)):e.errors.push(`Thread-bound loop '${s}' missing threadTag`)),t.kind===V.VECTORIZED){let i=t.extent;i.type==="IntImmNode"&&i.value<=0&&e.errors.push(`Vectorized loop '${s}' has non-positive extent ${i.value}`)}if(t.kind===V.PARALLEL){let i=t.extent;i&&i.type==="IntImmNode"&&!e.parallelExtents.has(i.value)&&e.parallelExtents.set(i.value,s)}(t.kind===V.PARALLEL||t.kind===V.THREAD_BINDING)&&n._checkNoNestedParallel(t.body,s,e);let r=t.kind===V.PARALLEL||t.kind===V.VECTORIZED;r&&e.parLoops.push({varName:s,kind:t.kind}),e.boundVars.add(s);let o=e.innermostLoopVar;e.innermostLoopVar=s,e.loopStack.push(s),n._visitNode(t.body,e),e.loopStack.pop(),e.innermostLoopVar=o,e.boundVars.delete(s),r&&e.parLoops.pop(),t.kind===V.THREAD_BINDING&&t.threadTag&&e.threadBindings.delete(t.threadTag)}static _checkNoNestedParallel(t,e,s){t&&t.type==="ForNode"&&t.kind===V.PARALLEL&&s.errors.push(`Parallel loop '${t.loopVar.name}' nested inside parallel/thread-bound loop '${e}'`)}static _visitBlock(t,e){for(let s of t.iterVars)s.iterVar&&e.boundVars.add(s.iterVar.name);if(t.initBody&&e.innermostLoopVar){let s=new Set;fh(t,s);let r=new Set;Zt(t.body,r),Zt(t.initBody,r);let o=0;for(let i of e.loopStack)r.has(i)&&!s.has(i)&&o++;(s.has(e.innermostLoopVar)||o>1)&&e.errors.push(`Reduction block '${t.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 '${e.innermostLoopVar}', ${o} enclosing reduction loops)`)}if(e.parLoops.length>0){let s=new Set;Zt(t.body,s),t.initBody&&Zt(t.initBody,s);let r=new Set;fh(t,r);for(let o of e.parLoops)if(s.has(o.varName)&&!r.has(o.varName)){let i=o.kind===V.VECTORIZED?"Vectorized":"Parallel";e.errors.push(`${i} loop '${o.varName}' carries a reduction in block '${t.name}': the loop variable is read but never written, so parallel iterations race on the accumulator`)}}n._visitNode(t.body,e),t.initBody&&n._visitNode(t.initBody,e);for(let s of t.iterVars)s.iterVar&&e.boundVars.delete(s.iterVar.name)}static _validateBufferAccess(t,e){if(t.buffer?n._checkRank(t,e):e.errors.push("BufferStore with null buffer"),t.indices)for(let s of t.indices)n._visitExpr(s,e);t.value&&n._visitExpr(t.value,e)}static _checkRank(t,e){t.indices&&t.buffer.shape&&t.indices.length!==t.buffer.shape.length&&e.errors.push(`Buffer '${t.buffer.name}' rank mismatch: ${t.indices.length} indices for rank-${t.buffer.shape.length} buffer`)}static _visitExpr(t,e){if(!(!t||typeof t!="object"))switch(t.type){case"BufferLoadNode":if(t.buffer?n._checkRank(t,e):e.errors.push("BufferLoad with null buffer"),t.indices)for(let s of t.indices)n._visitExpr(s,e);break;case"BufferStoreNode":n._validateBufferAccess(t,e);break;case"MathOpNode":case"CompareNode":n._visitExpr(t.a,e),n._visitExpr(t.b,e);break;case"CastNode":n._visitExpr(t.expr,e);break;case"CallExternNode":for(let s of t.args)n._visitExpr(s,e);break;case"IfThenElseNode":n._visitExpr(t.condition,e),n._visitNode(t.thenBody,e),t.elseBody&&n._visitNode(t.elseBody,e);break}}}});var Ho,ca,Ey=T(()=>{Ct();Ho=class{static{f(this,"SRef")}constructor(t,e=null){this.node=t,this.parent=e,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 t=[],e=this.parent;for(;e;)t.push(e),e=e.parent;return t}loopAncestors(){let t=[],e=this.parent;for(;e;)e.isLoop&&t.push(e),e=e.parent;return t}childBlocks(){let t=[],e=[...this.children];for(;e.length>0;){let s=e.pop();if(s.isBlock)t.push(s);else for(let r of s.children)e.push(r)}return t}childLoops(){return this.children.filter(t=>t.isLoop)}},ca=class{static{f(this,"SRefTree")}constructor(t){this._nodeToSRef=new Map,this._blockNameToSRef=new Map,this._loopSRefs=[],this._blockSRefs=[],this.root=this._build(t.body,null)}_build(t,e){let s=[{node:t,parentSRef:e}];for(;s.length>0;){let{node:r,parentSRef:o}=s.pop();if(r)switch(r.type){case"ForNode":{let i=new Ho(r,o);this._nodeToSRef.set(r,i),this._loopSRefs.push(i),o&&o.children.push(i),s.push({node:r.body,parentSRef:i});break}case"BlockNode":{let i=new Ho(r,o);this._nodeToSRef.set(r,i),this._blockNameToSRef.set(r.name,i),this._blockSRefs.push(i),o&&o.children.push(i),s.push({node:r.body,parentSRef:i}),r.initBody&&s.push({node:r.initBody,parentSRef:i});break}case"SeqNode":for(let i=r.stmts.length-1;i>=0;i--)s.push({node:r.stmts[i],parentSRef:o});break;case"IfThenElseNode":r.elseBody&&s.push({node:r.elseBody,parentSRef:o}),s.push({node:r.thenBody,parentSRef:o});break;case"AllocateNode":case"LetStmtNode":s.push({node:r.body,parentSRef:o});break}}return this._nodeToSRef.get(t)||null}getSRef(t){return this._nodeToSRef.get(t)||null}getBlockSRef(t){return this._blockNameToSRef.get(t)||null}allBlocks(){return this._blockSRefs}allLoops(){return this._loopSRefs}loopsOf(t){let e=this._blockNameToSRef.get(t);return e?e.loopAncestors().filter(s=>s.node.kind!==V.RECURRENCE).reverse():[]}replaceLoop(t,e,s){let r=this._nodeToSRef.get(t);if(!r)return;let o=r.parent,i=new Ho(e,o),a=new Ho(s,i);i.children.push(a);for(let c of r.children)c.parent=a,a.children.push(c);if(o){let c=o.children.indexOf(r);c>=0&&(o.children[c]=i)}else this.root=i;this._nodeToSRef.delete(t),this._nodeToSRef.set(e,i),this._nodeToSRef.set(s,a),this._loopSRefs.push(i,a)}reorderLoops(t,e,s){let r=t.map(l=>this._nodeToSRef.get(l)).filter(Boolean);if(r.length<2)return;let o=this._nodeToSRef.get(e),i=o?o.parent:null;if(i){let l=i.children.indexOf(o);l>=0&&(i.children[l]=r[0])}else this.root=r[0];for(let l=0;l<r.length;l++)r[l].parent=l===0?i:r[l-1],r[l].children=l<r.length-1?[r[l+1]]:[];let a=r[r.length-1],c=o?this._collectDescendantsNotIn(o,new Set(r)):[];for(let l of c)l.parent=a,a.children.push(l)}_collectDescendantsNotIn(t,e){let s=[],r=[...t.children];for(;r.length>0;){let o=r.pop();if(e.has(o))for(let i of o.children)r.push(i);else s.push(o)}return s}rebuildFrom(t){this._nodeToSRef.clear(),this._blockNameToSRef.clear(),this._loopSRefs.length=0,this._blockSRefs.length=0,this.root=this._build(t,null)}replaceNode(t,e){let s=this._nodeToSRef.get(t);if(!s)return;let r=new Ho(e,s.parent);r.children=s.children;for(let o of r.children)o.parent=r;if(s.parent){let o=s.parent.children.indexOf(s);o>=0&&(s.parent.children[o]=r)}else this.root=r;this._nodeToSRef.delete(t),this._nodeToSRef.set(e,r),e.type==="BlockNode"&&(this._blockNameToSRef.delete(t.name),this._blockNameToSRef.set(e.name,r))}}});function PT(n){let t=new Set,e=new Set;return Ve(n.body,t,e),n.initBody&&Ve(n.initBody,t,e),{reads:t,writes:e,accesses:zT(n)}}function LB(n,t){return n?n.map(e=>{if(e&&e.type==="VariableNode"&&t.has(e.name)){let s=t.get(e.name);return s.ext===null?null:[s.min,s.ext]}return e&&e.type==="IntImmNode"?[e.value,1]:null}):null}function zT(n){let t=[],e=new Map,s=f(r=>{if(!(!r||typeof r!="object"))switch(r.type){case"ForNode":{let o=r.extent,i=o&&o.type==="IntImmNode"?o.value:typeof o=="number"?o:null,a=r.min,c=a&&a.type==="IntImmNode"?a.value:typeof a=="number"?a:0;r.loopVar&&e.set(r.loopVar.name,{min:c,ext:i}),s(r.body);return}case"BufferLoadNode":if(r.buffer&&t.push({name:r.buffer.name,kind:"read",ranges:LB(r.indices,e)}),r.indices)for(let o of r.indices)s(o);return;case"BufferStoreNode":if(r.buffer&&t.push({name:r.buffer.name,kind:"write",ranges:LB(r.indices,e)}),r.indices)for(let o of r.indices)s(o);s(r.value);return;case"MathOpNode":s(r.a),r.b&&s(r.b);return;case"CompareNode":s(r.a),s(r.b);return;case"CallExternNode":for(let o of r.args)s(o);return;case"IfThenElseNode":s(r.condition),s(r.thenBody),r.elseBody&&s(r.elseBody);return;case"CastNode":s(r.expr);return;case"LetStmtNode":s(r.value),s(r.body);return;case"SeqNode":for(let o of r.stmts)s(o);return;case"BlockNode":r.initBody&&s(r.initBody),s(r.body);return;default:return}},"walk");return s(n.body),n.initBody&&s(n.initBody),t}function PB(n,t){if(!n||!t||n.length!==t.length)return!0;for(let e=0;e<n.length;e++){let s=n[e],r=t[e];if(!(s===null||r===null)&&(s[0]+s[1]<=r[0]||r[0]+r[1]<=s[0]))return!1}return!0}function $T(n,t,e){for(let s of n)for(let r of t)if(s.name===r.name&&!(s.kind==="read"&&r.kind==="read")&&!(e&&!e(s,r))&&PB(s.ranges,r.ranges))return!0;return!1}function Ve(n,t,e){if(!(!n||typeof n!="object"))switch(n.type){case"BufferLoadNode":if(n.buffer&&t.add(n.buffer.name),n.indices)for(let s of n.indices)Ve(s,t,e);return;case"BufferStoreNode":if(n.buffer&&e.add(n.buffer.name),n.indices)for(let s of n.indices)Ve(s,t,e);Ve(n.value,t,e);return;case"MathOpNode":Ve(n.a,t,e),n.b&&Ve(n.b,t,e);return;case"CompareNode":Ve(n.a,t,e),Ve(n.b,t,e);return;case"CallExternNode":for(let s of n.args)Ve(s,t,e);return;case"IfThenElseNode":Ve(n.condition,t,e),Ve(n.thenBody,t,e),n.elseBody&&Ve(n.elseBody,t,e);return;case"CastNode":Ve(n.expr,t,e);return;case"LetStmtNode":Ve(n.value,t,e),Ve(n.body,t,e);return;case"SeqNode":for(let s of n.stmts)Ve(s,t,e);return;case"ForNode":Ve(n.body,t,e);return;case"BlockNode":n.initBody&&Ve(n.initBody,t,e),Ve(n.body,t,e);return;default:return}}var Ty,Iy,ph,zB=T(()=>{Ty=Object.freeze({RAW:"read-after-write",WAR:"write-after-read",WAW:"write-after-write"}),Iy=class{static{f(this,"Dependency")}constructor(t,e,s,r){this.src=t,this.dst=e,this.kind=s,this.bufferName=r}};f(PT,"collectBufferAccesses");f(LB,"indexRanges");f(zT,"collectRegions");f(PB,"rangesOverlap");f($T,"accessesConflict");f(Ve,"visitExprTree");ph=class{static{f(this,"DependencyAnalysis")}constructor(t){this._tree=t,this._blockAccesses=new Map;for(let e of t.allBlocks())this._blockAccesses.set(e.node.name,PT(e.node))}getReads(t){let e=this._blockAccesses.get(t);return e?[...e.reads]:[]}getWrites(t){let e=this._blockAccesses.get(t);return e?[...e.writes]:[]}computeDeps(t,e){let s=this._blockAccesses.get(t),r=this._blockAccesses.get(e);if(!s||!r)return[];let o=[],i=new Set;for(let a of s.accesses)for(let c of r.accesses){if(a.name!==c.name||!PB(a.ranges,c.ranges))continue;let l=null;if(a.kind==="write"&&c.kind==="read"?l=Ty.RAW:a.kind==="write"&&c.kind==="write"?l=Ty.WAW:a.kind==="read"&&c.kind==="write"&&(l=Ty.WAR),!l)continue;let u=`${l}:${a.name}`;i.has(u)||(i.add(u),o.push(new Iy(t,e,l,a.name)))}return o}canReorder(t,e){let s=this._blockAccesses.get(t),r=this._blockAccesses.get(e);return!s||!r?!0:!$T(s.accesses,r.accesses)}}});var My,Cy,hh,$B=T(()=>{Ey();zB();Ct();My=class{static{f(this,"LoopBinding")}constructor(t,e,s,r,o){this.loopSRef=t,this.iterVar=e,this.extent=s,this.kind=r,this.threadTag=o}},Cy=class{static{f(this,"BlockBinding")}constructor(t,e,s,r){this.blockSRef=t,this.iterVars=e,this.readBuffers=s,this.writeBuffers=r}},hh=class{static{f(this,"ScheduleState")}constructor(t){this.primFunc=t,this._dirty=!0,this._tree=null,this._depAnalysis=null,this._loopBindings=null,this._blockBindings=null}_ensureBuilt(){if(this._dirty){this._tree=new ca(this.primFunc),this._depAnalysis=new ph(this._tree),this._loopBindings=new Map,this._blockBindings=new Map;for(let t of this._tree.allLoops()){let e=t.node,s=e.extent&&e.extent.type==="IntImmNode"?e.extent.value:null;this._loopBindings.set(e.loopVar.name,new My(t,e.loopVar.name,s,e.kind,e.threadTag))}for(let t of this._tree.allBlocks()){let e=t.node,s=[];for(let i of e.iterVars)i.iterVar&&s.push(i.iterVar.name);let r=[];for(let i of e.reads)r.push(i.buffer.name);let o=[];for(let i of e.writes)o.push(i.buffer.name);this._blockBindings.set(e.name,new Cy(t,s,r,o))}this._dirty=!1}}get tree(){return this._ensureBuilt(),this._tree}get depAnalysis(){return this._ensureBuilt(),this._depAnalysis}invalidate(){this._dirty=!0,this._tree=null,this._depAnalysis=null,this._loopBindings=null,this._blockBindings=null}getLoopBinding(t){return this._ensureBuilt(),this._loopBindings.get(t)||null}getBlockBinding(t){return this._ensureBuilt(),this._blockBindings.get(t)||null}getBlock(t){let e=this.tree.getBlockSRef(t);if(!e)throw new Error(`Block '${t}' not found in schedule state`);return e}getLoopsOf(t){return this.tree.loopsOf(t)}getReads(t){return this.depAnalysis.getReads(t)}getWrites(t){return this.depAnalysis.getWrites(t)}getDeps(t,e){return this.depAnalysis.computeDeps(t,e)}canReorder(t,e){return this.depAnalysis.canReorder(t,e)}threadBindingSummary(){this._ensureBuilt();let t={};for(let[,e]of this._loopBindings)e.kind===V.THREAD_BINDING&&e.threadTag&&(t[e.threadTag]={varName:e.iterVar,extent:e.extent});return t}blockDim(){let t=this.threadBindingSummary();return[t["threadIdx.x"]?.extent||1,t["threadIdx.y"]?.extent||1,t["threadIdx.z"]?.extent||1]}gridDim(){let t=this.threadBindingSummary();return[t["blockIdx.x"]?.extent||1,t["blockIdx.y"]?.extent||1,t["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()}}}});function Kt(n,t,e){if(!n||typeof n!="object")return n;if(n.type==="VariableNode"&&n.name===t)return e();switch(n.type){case"MathOpNode":n.a=Kt(n.a,t,e),n.b&&(n.b=Kt(n.b,t,e));break;case"CompareNode":n.a=Kt(n.a,t,e),n.b=Kt(n.b,t,e);break;case"BufferLoadNode":for(let s=0;s<n.indices.length;s++)n.indices[s]=Kt(n.indices[s],t,e);break;case"BufferStoreNode":for(let s=0;s<n.indices.length;s++)n.indices[s]=Kt(n.indices[s],t,e);n.value=Kt(n.value,t,e);break;case"CallExternNode":for(let s=0;s<n.args.length;s++)n.args[s]=Kt(n.args[s],t,e);break;case"CastNode":n.expr=Kt(n.expr,t,e);break;case"IfThenElseNode":n.condition=Kt(n.condition,t,e),n.thenBody=Kt(n.thenBody,t,e),n.elseBody&&(n.elseBody=Kt(n.elseBody,t,e));break;case"ForNode":n.body=Kt(n.body,t,e);break;case"BlockNode":n.body=Kt(n.body,t,e),n.initBody&&(n.initBody=Kt(n.initBody,t,e));for(let s=0;s<n.iterVars.length;s++)n.iterVars[s].binding&&(n.iterVars[s].binding=Kt(n.iterVars[s].binding,t,e));break;case"SeqNode":for(let s=0;s<n.stmts.length;s++)n.stmts[s]=Kt(n.stmts[s],t,e);break;case"LetStmtNode":n.value=Kt(n.value,t,e),n.body=Kt(n.body,t,e);break;case"BlockRealizeNode":n.binding&&(n.binding=Kt(n.binding,t,e));break}return n}function Qe(n,t,e,s){if(!n||typeof n!="object")return n;if(n.type==="BufferLoadNode"&&n.buffer&&n.buffer.name===t)return s.n++,e(n);switch(n.type){case"MathOpNode":n.a=Qe(n.a,t,e,s),n.b&&(n.b=Qe(n.b,t,e,s));break;case"CompareNode":n.a=Qe(n.a,t,e,s),n.b=Qe(n.b,t,e,s);break;case"BufferLoadNode":for(let r=0;r<n.indices.length;r++)n.indices[r]=Qe(n.indices[r],t,e,s);break;case"BufferStoreNode":for(let r=0;r<n.indices.length;r++)n.indices[r]=Qe(n.indices[r],t,e,s);n.value=Qe(n.value,t,e,s);break;case"CallExternNode":for(let r=0;r<n.args.length;r++)n.args[r]=Qe(n.args[r],t,e,s);break;case"CastNode":n.expr=Qe(n.expr,t,e,s);break;case"IfThenElseNode":n.condition=Qe(n.condition,t,e,s),n.thenBody=Qe(n.thenBody,t,e,s),n.elseBody&&(n.elseBody=Qe(n.elseBody,t,e,s));break;case"ForNode":n.body=Qe(n.body,t,e,s);break;case"BlockNode":n.body=Qe(n.body,t,e,s),n.initBody&&(n.initBody=Qe(n.initBody,t,e,s));break;case"SeqNode":for(let r=0;r<n.stmts.length;r++)n.stmts[r]=Qe(n.stmts[r],t,e,s);break;case"LetStmtNode":n.value=Qe(n.value,t,e,s),n.body=Qe(n.body,t,e,s);break}return n}function la(n,t){if(!n||typeof n!="object")return!1;if(n.type==="BufferLoadNode"&&n.buffer&&n.buffer.name===t)return!0;for(let e of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])if(n[e]&&la(n[e],t))return!0;if(n.args){for(let e of n.args)if(la(e,t))return!0}if(n.indices){for(let e of n.indices)if(la(e,t))return!0}if(n.stmts){for(let e of n.stmts)if(la(e,t))return!0}return!1}function At(n){if(!n||typeof n!="object")return n;if(Array.isArray(n))return n.map(At);let t=Object.create(Object.getPrototypeOf(n));switch(t.type=n.type,t._parent=null,t._parentKey=null,t._parentIdx=-1,n.type){case"ForNode":t.loopVar=n.loopVar,t.min=At(n.min),t.extent=At(n.extent),t.kind=n.kind,t.body=At(n.body),t.threadTag=n.threadTag,t._setChild("body",t.body);break;case"BlockNode":t.name=n.name,t.iterVars=n.iterVars.map(At),t.reads=n.reads,t.writes=n.writes,t.body=At(n.body),t.initBody=n.initBody?At(n.initBody):null,t._setChild("body",t.body),t._setChild("initBody",t.initBody);break;case"SeqNode":t.stmts=n.stmts.map(At),t._setChildren("stmts",t.stmts);break;case"IfThenElseNode":t.condition=At(n.condition),t.thenBody=At(n.thenBody),t.elseBody=n.elseBody?At(n.elseBody):null,t._setChild("thenBody",t.thenBody),t._setChild("elseBody",t.elseBody);break;case"BufferStoreNode":t.buffer=n.buffer,t.indices=n.indices.map(At),t.value=At(n.value);break;case"BufferLoadNode":t.buffer=n.buffer,t.indices=n.indices.map(At);break;case"BlockRealizeNode":t.iterVar=n.iterVar,t.binding=At(n.binding);break;case"MathOpNode":t.op=n.op,t.a=At(n.a),t.b=At(n.b);break;case"CompareNode":t.direction=n.direction,t.a=At(n.a),t.b=At(n.b);break;case"CastNode":t.expr=At(n.expr),t.fromDtype=n.fromDtype,t.toDtype=n.toDtype;break;case"CallExternNode":t.externName=n.externName,t.args=n.args.map(At),t.dtype=n.dtype;break;default:for(let e of Object.keys(n))e==="_parent"||e==="_parentKey"||e==="_parentIdx"||(t[e]=n[e]);break}return t}function Yo(n){return n.type==="IntImmNode"?n.value:null}function ua(n,t="int32"){return new se(`${n}_${VT++}`,t)}var FT,VT,Ts,lu=T(()=>{Ct();vo();DB();Ny();$B();Ey();Oy();FT=new Set(["+","*","min","max"]);f(Kt,"substituteVar");f(Qe,"replaceBufferLoads");f(la,"loadsBuffer");f(At,"cloneExprTree");f(Yo,"getConstExtent");VT=0;f(ua,"freshVar");Ts=class{static{f(this,"Schedule")}constructor(t){this.func=t,this.trace=new lh,this.state=new hh(t),this._replaying=!1,this._srefTree=new ca(t)}_rebuildSRefTree(){this._srefTree.rebuildFrom(this.func.body),this.state.invalidate()}getBlock(t){let e=this._srefTree.getBlockSRef(t);if(!e)throw new Error(`Block '${t}' not found`);return e.node}getLoops(t){return this._srefTree.loopsOf(t).map(e=>e.node)}_resolveLoop(t){if(typeof t!="string")return t;let e=null,s=f(r=>{if(!(!r||typeof r!="object"||e)){if(r.type==="ForNode"&&r.loopVar&&r.loopVar.name===t){e=r;return}if(r.body&&s(r.body),r.initBody&&s(r.initBody),r.stmts)for(let o of r.stmts)s(o);r.thenBody&&s(r.thenBody),r.elseBody&&s(r.elseBody)}},"walk");return s(this.func.body),e||t}split(t,e){t=this._resolveLoop(t);let s=Yo(t.extent);if(s===null)throw new Error(`Cannot split loop '${t.loopVar.name}' with non-constant extent`);if(e<=0||!Number.isInteger(e))throw new Error(`Split factor must be a positive integer, got ${e}`);let r=Math.ceil(s/e),o=ua(`${t.loopVar.name}_o`),i=ua(`${t.loopVar.name}_i`),a=t.loopVar.name,c=At(t.body),l=new ht(i,new C(0),new C(e),t.kind,c,t.threadTag);if(s%e!==0){let p=new D("+",new D("*",o,new C(e)),i),d=new D("<",p,new C(s)),m=new Mt(d,l.body);l.body=m,l._setChild("body",m)}let h=new ht(o,new C(0),new C(r),t.kind,l,t.threadTag);return Kt(l.body,a,()=>new D("+",new D("*",o,new C(e)),i)),this._replaceNode(t,h),this._rebuildSRefTree(),this._replaying||this.trace.record("split",[t.loopVar.name,e]),[h,l]}reorder(...t){if(t.length===1&&Array.isArray(t[0])&&(t=t[0]),t=t.map(p=>this._resolveLoop(p)),t.length<2)return;for(let p of t)if(p.type!=="ForNode")throw new Error("reorder expects ForNode arguments");let e=new Set(t),s=null,r=1/0,o=new Map,i=f((p,d)=>{if(p){if(p.type==="ForNode"){e.has(p)&&(o.set(p,d),d<r&&(r=d,s=p)),i(p.body,d+1);return}if(p.type==="SeqNode")for(let m of p.stmts)i(m,d);p.type==="BlockNode"&&i(p.body,d)}},"findDepths");i(this.func.body,0);let a=[...t].sort((p,d)=>o.get(p)-o.get(d)),l=a[a.length-1].body,u=this._srefTree.getSRef(s),h=u?u.parent:null;this._replaceNode(s,t[0]);for(let p of t)p!==t[0]&&(p._parent=null,p._parentKey=null,p._parentIdx=-1);for(let p=0;p<t.length;p++){let d=p<t.length-1?t[p+1]:l;t[p].body=d,t[p]._setChild("body",d)}this._rebuildSRefTree(),this._replaying||this.trace.record("reorder",[t.map(p=>p.loopVar.name)])}fuseLoops(t,e){if(t=this._resolveLoop(t),e=this._resolveLoop(e),t.type!=="ForNode"||e.type!=="ForNode")throw new Error("fuseLoops expects two ForNode arguments");if(t.body!==e)throw new Error("fuseLoops requires inner loop to be direct child of outer loop");let s=Yo(t.extent),r=Yo(e.extent);if(s===null||r===null)throw new Error("Cannot fuse loops with non-constant extents");let o=s*r,i=ua(`${t.loopVar.name}_${e.loopVar.name}_fused`),a=t.loopVar.name,c=e.loopVar.name,l=new ht(i,new C(0),new C(o),t.kind,e.body);return Kt(l.body,a,()=>new D("/",i,new C(r))),Kt(l.body,c,()=>new D("%",i,new C(r))),this._replaceNode(t,l),this._srefTree.replaceNode(t,l),this.state.invalidate(),this._replaying||this.trace.record("fuseLoops",[a,c]),l}tile(t,e,s){if(e.length!==s.length)throw new Error("tile: loopIndices and tileSizes must have same length");let r=this.getLoops(t),o=e.map(h=>{if(h>=r.length)throw new Error(`tile: loop index ${h} out of range`);return r[h]}),i=[],a=[];for(let h=0;h<o.length;h++){let d=this.getLoops(t).find(_=>_.loopVar.name===o[h].loopVar.name||_===o[h]);if(!d)throw new Error(`tile: lost track of loop at index ${h}`);let[m,g]=this.split(d,s[h]);i.push(m),a.push(g)}let c=this.getLoops(t),l=[],u=[];for(let h of c)i.some(p=>p.loopVar.name===h.loopVar.name)?l.push(h):a.some(p=>p.loopVar.name===h.loopVar.name)&&u.push(h);return l.length>0&&u.length>0&&this.reorder(...l,...u),{outerLoops:l,innerLoops:u}}vectorize(t){if(t=this._resolveLoop(t),t.type!=="ForNode")throw new Error("vectorize expects ForNode");if(Yo(t.extent)===null)throw new Error("Cannot vectorize loop with non-constant extent");let s=Ry(t);if(s!==null)throw new Error(`Cannot vectorize reduction loop '${t.loopVar.name}' (loop-carried dependency in block '${s}')`);t.kind=V.VECTORIZED,this.state.invalidate(),this._replaying||this.trace.record("vectorize",[t.loopVar.name])}unroll(t){if(t=this._resolveLoop(t),t.type!=="ForNode")throw new Error("unroll expects ForNode");t.kind=V.UNROLLED,this.state.invalidate(),this._replaying||this.trace.record("unroll",[t.loopVar.name])}parallelize(t){if(t=this._resolveLoop(t),t.type!=="ForNode")throw new Error("parallelize expects ForNode");let e=Ry(t);if(e!==null)throw new Error(`Cannot parallelize reduction loop '${t.loopVar.name}' (loop-carried dependency in block '${e}')`);t.kind=V.PARALLEL,this.state.invalidate(),this._replaying||this.trace.record("parallelize",[t.loopVar.name])}bindThread(t,e){if(t=this._resolveLoop(t),t.type!=="ForNode")throw new Error("bindThread expects ForNode");let s=["blockIdx.x","blockIdx.y","blockIdx.z","threadIdx.x","threadIdx.y","threadIdx.z"];if(!s.includes(e))throw new Error(`Invalid thread tag: ${e}. Must be one of: ${s.join(", ")}`);t.kind=V.THREAD_BINDING,t.threadTag=e,this.state.invalidate(),this._replaying||this.trace.record("bindThread",[t.loopVar.name,e])}rfactor(t,e,s){let r=this.getBlock(t),o=this.getLoops(t),i=o.find(F=>F.loopVar.name===e);if(!i)throw new Error(`rfactor: reduction loop '${e}' not found for block '${t}'`);let a=Yo(i.extent);if(a===null)throw new Error(`rfactor: reduction loop '${e}' has non-constant extent`);if(!Number.isInteger(s)||s<=1||s>=a||a%s!==0)throw new Error(`rfactor: factor ${s} must divide reduction extent ${a} with 1 < factor < ${a}`);let c=r.body;if(!c||c.type!=="BufferStoreNode"||!c.value||c.value.type!=="MathOpNode")throw new Error(`rfactor: block '${t}' body is not a single accumulating store`);let l=c.buffer,u=c.indices,h=c.value.op,p=f(F=>F&&F.type==="BufferLoadNode"&&F.buffer===l,"isAccLoad"),d;if(p(c.value.a))d=c.value.b;else if(p(c.value.b))d=c.value.a;else throw new Error(`rfactor: accumulator load not found in block '${t}' body`);if(!FT.has(h))throw new Error(`rfactor: op '${h}' is not associative+commutative; cannot factor reduction`);let m=r.initBody&&r.initBody.type==="BufferStoreNode"&&r.initBody.value?r.initBody.value:new C(0),g=o.filter(F=>F.loopVar.name!==e),_=a/s,y=new Vt(`${l.name}_rf`,[s,...l.shape],l.dtype,l.scope),b=ua(`${e}_rfi`),x=ua(`${e}_rfo`),S=ua(`${e}_rfp`),v=f(F=>[F,...u.map(At)],"cfIdx"),w=Kt(At(d),e,()=>new D("+",new D("*",x,new C(s)),b)),A=new X(y,v(b),new D(h,new K(y,v(b)),w)),I=new X(y,v(b),At(m)),B=new tt(`${t}_rf_p`,[],r.reads.map(F=>({buffer:F.buffer})),[{buffer:y}],A,I),k=new ht(x,new C(0),new C(_),V.SERIAL,B);k=new ht(b,new C(0),new C(s),V.SERIAL,k);for(let F=g.length-1;F>=0;F--)k=new ht(g[F].loopVar,new C(0),At(g[F].extent),V.SERIAL,k);let N=new X(l,u.map(At),new D(h,new K(l,u.map(At)),new K(y,v(S)))),R=new X(l,u.map(At),At(m)),O=new tt(`${t}_rf_c`,[],[{buffer:y}],[{buffer:l}],N,R),M=new ht(S,new C(0),new C(s),V.SERIAL,O);for(let F=g.length-1;F>=0;F--)M=new ht(g[F].loopVar,new C(0),At(g[F].extent),V.SERIAL,M);return this._replaceNode(o[0],new ft([k,M])),this._rebuildSRefTree(),this._replaying||this.trace.record("rfactor",[t,e,s]),y}decomposeReduction(t){let e=this.getBlock(t);if(!e.initBody)throw new Error(`decomposeReduction: block '${t}' has no initBody`);let s=this.getLoops(t),r=e.body;if(!r||r.type!=="BufferStoreNode")throw new Error(`decomposeReduction: block '${t}' body is not a store`);let o=r.buffer,i=new Set;for(let m of r.indices)Zt(m,i);let a=s.filter(m=>i.has(m.loopVar.name));if(s.filter(m=>!i.has(m.loopVar.name)).length===0)throw new Error(`decomposeReduction: block '${t}' has no reduction loop`);let l=new X(o,r.indices.map(At),At(e.initBody.value)),h=new tt(`${t}_init`,[],[],[{buffer:o}],l);for(let m=a.length-1;m>=0;m--)h=new ht(a[m].loopVar,new C(0),At(a[m].extent),V.SERIAL,h);let d=new tt(`${t}_upd`,[],e.reads.map(m=>({buffer:m.buffer})),[{buffer:o}],At(r));for(let m=s.length-1;m>=0;m--)d=new ht(s[m].loopVar,new C(0),At(s[m].extent),V.SERIAL,d);this._replaceNode(s[0],new ft([h,d])),this._rebuildSRefTree(),this._replaying||this.trace.record("decomposeReduction",[t])}_redirectReads(t,e,s){if(!(!t||typeof t!="object")){t.type==="BufferLoadNode"&&t.buffer===e&&(t.buffer=s);for(let r of["a","b","expr","value","condition","thenBody","elseBody","body","initBody","min","extent"])t[r]&&this._redirectReads(t[r],e,s);if(Array.isArray(t.stmts))for(let r of t.stmts)this._redirectReads(r,e,s);if(Array.isArray(t.indices))for(let r of t.indices)this._redirectReads(r,e,s);if(Array.isArray(t.args))for(let r of t.args)this._redirectReads(r,e,s)}}_redirectBuffer(t,e,s){if(!(!t||typeof t!="object")){(t.type==="BufferLoadNode"||t.type==="BufferStoreNode")&&t.buffer===e&&(t.buffer=s);for(let r of["a","b","expr","value","condition","thenBody","elseBody","body","initBody","min","extent"])t[r]&&this._redirectBuffer(t[r],e,s);if(Array.isArray(t.stmts))for(let r of t.stmts)this._redirectBuffer(r,e,s);if(Array.isArray(t.indices))for(let r of t.indices)this._redirectBuffer(r,e,s);if(Array.isArray(t.args))for(let r of t.args)this._redirectBuffer(r,e,s)}}cacheWrite(t,e,s="local"){let r=this.getBlock(t),o=this.getLoops(t);if(o.length===0)throw new Error("cacheWrite: block has no enclosing loops");let i=(r.writes||[]).find(_=>_.buffer&&_.buffer.name===e);if(!i)throw new Error(`cacheWrite: block '${t}' does not write '${e}'`);let a=i.buffer,c=new Vt(`${e}_${t}_cachew`,[...a.shape],a.dtype,s);this._redirectBuffer(r.body,a,c),r.initBody&&this._redirectBuffer(r.initBody,a,c),i.buffer=c;let l=a.shape.map((_,y)=>new se(`${c.name}_o${y}`,"int32")),u=new X(a,l,new K(c,l)),p=new tt(`${c.name}_flush`,l.map(_=>({iterVar:_,binding:_})),[{buffer:c}],[{buffer:a}],u);for(let _=a.shape.length-1;_>=0;_--)p=new ht(l[_],new C(0),new C(a.shape[_]),V.SERIAL,p);let d=o[0],m=new ft([]),g=new fs(c,s,m);this._replaceNode(d,g),m.stmts.push(d,p),this._rebuildSRefTree(),this._replaying||this.trace.record("cacheWrite",[t,e,s])}setScope(t,e,s){let o=(this.getBlock(t).writes||[]).find(i=>i.buffer&&i.buffer.name===e);if(!o)throw new Error(`setScope: block '${t}' does not write '${e}'`);o.buffer.scope=s,this.state.invalidate(),this._replaying||this.trace.record("setScope",[t,e,s])}storageAlign(t,e,s,r,o){let i=this.getBlock(t),a=[...i.writes||[],...i.reads||[]].find(c=>c.buffer&&c.buffer.name===e);if(!a)throw new Error(`storageAlign: block '${t}' does not access '${e}'`);if(!Number.isInteger(r)||r<=0)throw new Error("storageAlign: factor must be a positive integer");a.buffer.storageAlign={axis:s,factor:r,offset:o||0},this.state.invalidate(),this._replaying||this.trace.record("storageAlign",[t,e,s,r,o||0])}computeInline(t){let e=this.getBlock(t);if(!e)throw new Error(`computeInline: block '${t}' not found`);if(e.initBody)throw new Error("computeInline: cannot inline a reduction block (has init)");let s=null,r=f(m=>{if(!(!m||s)){if(m.type==="BufferStoreNode"){s=m;return}if(m.type==="SeqNode"){for(let g of m.stmts)r(g);return}(m.type==="BlockNode"||m.type==="ForNode")&&r(m.body)}},"findStore");if(r(e.body),!s)throw new Error("computeInline: producer has no single store to inline");let o=s.buffer,i=s.indices.map(m=>m&&m.type==="VariableNode"?m.name:null);if(i.some(m=>m===null))throw new Error("computeInline: producer indices must be simple loop variables");if(la(s.value,o.name))throw new Error("computeInline: producer is self-referential (recurrence), cannot inline");let a=f(m=>{if(!m||typeof m!="object")return!1;if((m.type==="BufferLoadNode"||m.type==="BufferStoreNode")&&m.indices){for(let g of m.indices)if(la(g,o.name))return!0}for(let g of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])if(m[g]&&a(m[g]))return!0;if(m.args){for(let g of m.args)if(a(g))return!0}if(m.indices){for(let g of m.indices)if(a(g))return!0}if(m.stmts){for(let g of m.stmts)if(a(g))return!0}return!1},"usesBInIndex");if(a(this.func.body))throw new Error(`computeInline: buffer '${o.name}' is used inside an index expression (indirect), cannot safely inline`);let c=s.value,l={n:0},u=i.map((m,g)=>`__inl_${t}_${g}`),h=f(m=>{let g=At(c);for(let _=0;_<i.length;_++){let y=u[_];g=Kt(g,i[_],()=>new se(y,"int32"))}for(let _=0;_<i.length;_++){let y=m.indices[_];g=Kt(g,u[_],()=>At(y))}return g},"makeRepl");if(Qe(this.func.body,o.name,h,l),l.n===0)throw new Error(`computeInline: buffer '${o.name}' has no consumers to inline into`);let p=this.getLoops(t),d=p.length>0?p[0]:e;this._replaceNode(d,new ft([])),this._rebuildSRefTree(),this._replaying||this.trace.record("computeInline",[t])}_relocateBlockToLoop(t,e,s){if(!this.getBlock(t))throw new Error(`computeAt: block '${t}' not found`);let o=this._resolveLoop(e);if(!o||o.type!=="ForNode")throw new Error("computeAt: target must be a loop");let i=this.getLoops(t);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 c=Yo(a.extent),l=Yo(o.extent);if(c===null||l===null||c!==l)throw new Error("computeAt: aligned case requires equal static extent on the block and target loops");let u=At(a.body);Kt(u,a.loopVar.name,()=>o.loopVar),this._replaceNode(a,new ft([]));let h=o.body;if(h&&h.type==="SeqNode")s?h.stmts.unshift(u):h.stmts.push(u);else{let p=s?new ft([u,h]):new ft([h,u]);o.body=p,o._setChild("body",p)}return this._rebuildSRefTree(),o.loopVar.name}computeAt(t,e){let s=this._relocateBlockToLoop(t,e,!0);this._replaying||this.trace.record("computeAt",[t,s])}reverseComputeAt(t,e){let s=this._relocateBlockToLoop(t,e,!1);this._replaying||this.trace.record("reverseComputeAt",[t,s])}cacheRead(t,e,s="local"){let r=this.getBlock(t),o=this.getLoops(t);if(o.length===0)throw new Error("cacheRead: block has no enclosing loops");let i=(r.reads||[]).find(_=>_.buffer&&_.buffer.name===e);if(!i)throw new Error(`cacheRead: block '${t}' does not read '${e}'`);let a=i.buffer,c=new Vt(`${e}_${t}_cache`,[...a.shape],a.dtype,s),l=a.shape.map((_,y)=>new se(`${c.name}_i${y}`,"int32")),u=new X(c,l,new K(a,l)),p=new tt(`${c.name}_fill`,l.map(_=>({iterVar:_,binding:_})),[{buffer:a}],[{buffer:c}],u);for(let _=a.shape.length-1;_>=0;_--)p=new ht(l[_],new C(0),new C(a.shape[_]),V.SERIAL,p);this._redirectReads(r.body,a,c),r.initBody&&this._redirectReads(r.initBody,a,c),i.buffer=c;let d=o[0],m=new ft([p]),g=new fs(c,s,m);this._replaceNode(d,g),m.stmts.push(d),this._rebuildSRefTree(),this._replaying||this.trace.record("cacheRead",[t,e,s])}fuseConsumer(t,e){let s=this.getBlock(t),r=this.getBlock(e),o=this.getLoops(t),i=this.getLoops(e);if(!s.body||s.body.type!=="BufferStoreNode")throw new Error(`fuseConsumer: producer '${t}' body is not a store`);let a=new Set;for(let m of s.body.indices)Zt(m,a);let c=o.filter(m=>a.has(m.loopVar.name));if(c.length===0||i.length!==c.length)throw new Error(`fuseConsumer: producer/consumer spatial rank mismatch (${c.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 l=c[c.length-1],u=At(l.body),h=At(r.body);for(let m=0;m<i.length;m++){let g=c[m].loopVar.name;h=Kt(h,i[m].loopVar.name,()=>new se(g,"int32"))}let p=new tt(`${e}_fused`,[],r.reads.map(m=>({buffer:m.buffer})),r.writes.map(m=>({buffer:m.buffer})),h),d=new ft([u,p]);for(let m=c.length-1;m>=0;m--){let g=c[m];d=new ht(g.loopVar,new C(0),At(g.extent),g.kind,d,g.threadTag)}this._replaceNode(o[0],d),this._removeNode(i[0]),this._rebuildSRefTree(),this._replaying||this.trace.record("fuseConsumer",[t,e])}annotate(t,e,s){if(t=this._resolveLoop(t),t.type!=="ForNode")throw new Error("annotate expects ForNode");t.annotations||(t.annotations={}),t.annotations[e]=s,this.state.invalidate(),this._replaying||this.trace.record("annotate",[t.loopVar.name,e,s])}getTrace(){return this.trace}verify(){return aa.validate(this.func)}_replaceNode(t,e){if(t._parent){t.replaceWith(e);return}(this.func.body===t||this.func.body===void 0)&&(this.func.body=e,this.func._setChild&&this.func._setChild("body",e))}_removeNode(t){let e=t._parent;if(e&&e.type==="SeqNode"&&Array.isArray(e.stmts)){let s=e.stmts.indexOf(t);if(s>=0){e.stmts.splice(s,1),e._setChildren&&e._setChildren("stmts",e.stmts);return}}throw new Error("_removeNode: node parent is not a SeqNode; cannot remove without duplicating it")}}});function de(n,t){let e=Dy.get(n);return e||(e=new Map,GT(n.body,e,[]),Dy.set(n,e)),e.get(t)||null}function FB(n){n&&Dy.delete(n)}function VB(n,t){let e=[n];for(;e.length>0;){let s=e.pop();if(!(!s||typeof s!="object")){if(s.type==="VariableNode"){t.add(s.name);continue}for(let r of["a","b","condition","thenBody","elseBody","expr","value","offsetExpr","extent"])s[r]&&e.push(s[r]);if(s.indices)for(let r of s.indices)e.push(r);if(s.args)for(let r of s.args)e.push(r)}}}function GB(n){let t=new Set((n.writes||[]).map(o=>o.buffer&&o.buffer.name)),e=new Set,s=[n.body,n.initBody];for(;s.length>0;){let o=s.pop();if(o){if(o.type==="BufferStoreNode"&&o.buffer&&t.has(o.buffer.name))for(let i of o.indices)VB(i,e);if(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.value&&s.push(o.value)}}let r=new Set;for(let o of n.iterVars||[]){let i=new Set;if(VB(o.binding,i),!(o.iterVar&&e.has(o.iterVar.name)||[...i].some(c=>e.has(c))))for(let c of i)r.add(c)}return r}function GT(n,t,e){let s=[{node:n,loops:[...e]}];for(;s.length>0;){let{node:r,loops:o}=s.pop();if(r)if(r.type==="ForNode")s.push({node:r.body,loops:[...o,r]});else if(r.type==="BlockNode"){let i=GB(r);t.set(r.name,{loopCount:o.length,hasReduction:r.initBody!==null||i.size>0,reductionLoopVars:i,readBuffers:r.reads.map(a=>a.buffer.name),writeBuffers:r.writes.map(a=>a.buffer.name),loops:[...o]}),s.push({node:r.body,loops:o})}else if(r.type==="SeqNode")for(let i=r.stmts.length-1;i>=0;i--)s.push({node:r.stmts[i],loops:o});else r.type==="IfThenElseNode"?(r.elseBody&&s.push({node:r.elseBody,loops:o}),s.push({node:r.thenBody,loops:o})):r.type==="AllocateNode"?s.push({node:r.body,loops:o}):r.type==="LetStmtNode"&&s.push({node:r.body,loops:o})}}function qB(n){let t=0,e=[n.body];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="BlockNode"&&(t++,t>1))return!0;if(s.type==="ForNode"&&e.push(s.body),s.type==="SeqNode")for(let r of s.stmts)e.push(r)}}return!1}function Wy(n){let t=[n.body];for(;t.length>0;){let e=t.pop();if(e){if(e.type==="BlockNode"&&(e.initBody!==null||GB(e).size>0))return!0;if(e.body&&t.push(e.body),e.stmts)for(let s of e.stmts)t.push(s);e.thenBody&&t.push(e.thenBody),e.elseBody&&t.push(e.elseBody),e.initBody&&t.push(e.initBody)}}return!1}function qT(n){let t=[n.body];for(;t.length>0;){let e=t.pop();if(e){if(e.type==="ForNode"&&e.kind===V.RECURRENCE)return!0;if(e.body&&t.push(e.body),e.stmts)for(let s of e.stmts)t.push(s);e.thenBody&&t.push(e.thenBody),e.elseBody&&t.push(e.elseBody),e.initBody&&t.push(e.initBody)}}return!1}function jy(n,t,e){let s=Math.min(e.maxThreadsPerBlock,256),r=t.extent;if(r.type==="IntImmNode"&&r.value>s){let[o,i]=n.split(t,s);n.bindThread(i,"threadIdx.x"),qT(n.func)||n.bindThread(o,"blockIdx.x")}else n.bindThread(t,"threadIdx.x")}function fa(n,t){return t.reductionLoopVars?t.reductionLoopVars.has(n.loopVar.name):!1}function Ky(n,t){return n.body===t}function UT(n){let t=[],e=[n];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="BlockNode"&&t.push(s.name),s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),s.initBody&&e.push(s.initBody)}}return t}var In,Dy,Ly,Py,zy,$y,Fy,Vy,Gy,qy,Uy,Cr,ro=T(()=>{qn();Ct();In=class{static{f(this,"ScheduleRule")}constructor(t){this.name=t}matches(t,e,s){throw new Error("ScheduleRule.matches must be implemented")}apply(t,e,s){throw new Error("ScheduleRule.apply must be implemented")}},Dy=new WeakMap;f(de,"classifyBlock");f(FB,"invalidateClassifyCache");f(VB,"collectVarNames");f(GB,"computeReductionLoopVars");f(GT,"collectBlockInfo");f(qB,"hasMultipleBlocks");Ly=class extends In{static{f(this,"ElementwiseCPURule")}constructor(){super("elementwise_cpu")}matches(t,e,s){if(s.kind!==fe.CPU)return!1;let r=de(t,e);if(!r||r.hasReduction||r.loopCount<1||r.loops.length>0&&qB(r.loops[0]))return!1;let o=1;for(let i of r.loops){let a=i.extent&&i.extent.type==="IntImmNode"?i.extent.value:1;o*=a}return o>=s.numCores*s.vectorWidth}apply(t,e,s){let r=t.getLoops(e);if(r.length===0)return;if(r.length===1){let a=r[0].extent;if(a.type==="IntImmNode"&&a.value>=s.vectorWidth*2){let[c,l]=t.split(r[0],s.vectorWidth);t.parallelize(c),t.vectorize(l);return}t.parallelize(r[0]);return}t.parallelize(r[0]);let o=r[r.length-1],i=o.extent;if(i.type==="IntImmNode"&&i.value>=s.vectorWidth&&i.value%s.vectorWidth===0){let[a,c]=t.split(o,s.vectorWidth);t.vectorize(c)}}};f(Wy,"primFuncHasReduction");f(qT,"primFuncHasRecurrence");f(jy,"bindFusedSpatialGPU");Py=class extends In{static{f(this,"ElementwiseGPURule")}constructor(){super("elementwise_gpu")}matches(t,e,s){if(!s.isGPU())return!1;let r=de(t,e);return r?!r.hasReduction&&r.loopCount>=1:!1}apply(t,e,s){let r=t.getLoops(e);if(r.length===0)return;let o=r[0];for(let l=1;l<r.length;l++){let h=t.getLoops(e).find(p=>p.loopVar.name===r[l].loopVar.name);h&&Ky(o,h)&&(o=t.fuseLoops(o,h))}let i=o.extent;if(i.type!=="IntImmNode"){t.bindThread(o,"threadIdx.x");return}let a=i.value,c=Math.min(s.maxThreadsPerBlock,1024);if(Wy(t.func)&&a<=c){t.bindThread(o,"threadIdx.x");return}jy(t,o,s)}},zy=class extends In{static{f(this,"ReductionCPURule")}constructor(){super("reduction_cpu")}matches(t,e,s){if(s.kind!==fe.CPU)return!1;let r=de(t,e);return r?r.hasReduction:!1}apply(t,e,s){let r=t.getLoops(e);if(r.length===0)return;let o=[],i=[],a=de(t.func,e);for(let c of r)a&&fa(c,a)?i.push(c):o.push(c);o.length>0&&t.parallelize(o[0])}},$y=class extends In{static{f(this,"ReductionGPURule")}constructor(){super("reduction_gpu")}matches(t,e,s){if(!s.isGPU())return!1;let r=de(t,e);return r?r.hasReduction:!1}apply(t,e,s){let r=t.getLoops(e);if(r.length===0)return;let o=[],i=de(t.func,e);for(let c of r)(!i||!fa(c,i))&&o.push(c);if(o.length===0)return;let a=o[0];for(let c=1;c<o.length;c++){let u=t.getLoops(e).find(h=>h.loopVar.name===o[c].loopVar.name);u&&Ky(a,u)&&(a=t.fuseLoops(a,u))}jy(t,a,s)}},Fy=class extends In{static{f(this,"MatmulTiledCPURule")}constructor(){super("matmul_tiled_cpu")}matches(t,e,s){if(s.kind!==fe.CPU)return!1;let r=de(t,e);if(!r||!r.hasReduction||!e.includes("matmul")||r.loopCount<3)return!1;let o=s.l1CacheBytes||32768,i=Math.max(8,Math.min(64,Math.floor(Math.sqrt(o/4))));return r.loops.reduce((c,l)=>{let u=l.extent&&l.extent.type==="IntImmNode"?l.extent.value:0;return u>c?u:c},0)>=i}apply(t,e,s){let r=t.getLoops(e);if(r.length<3)return;let o=s.l1CacheBytes||32768,i=Math.max(8,Math.min(64,Math.floor(Math.sqrt(o/4)))),a=[],c=[];for(let u=0;u<Math.min(2,r.length);u++){let h=r[u].extent.type==="IntImmNode"?r[u].extent.value:null;h&&h>=i&&(a.push(u),c.push(i))}if(a.length===0)return;let{outerLoops:l}=t.tile(e,a,c);l.length>0&&t.parallelize(l[0])}},Vy=class extends In{static{f(this,"MatmulTiledGPURule")}constructor(){super("matmul_tiled_gpu")}matches(t,e,s){if(!s.isGPU())return!1;let r=de(t,e);if(!r||!r.hasReduction||!e.includes("matmul")||r.loopCount<3)return!1;let o=s.sharedMemoryBytes||49152,a=Math.max(16,Math.min(128,Math.floor(Math.sqrt(o/8))));return r.loops.reduce((l,u)=>{let h=u.extent&&u.extent.type==="IntImmNode"?u.extent.value:0;return h>l?h:l},0)>=a}apply(t,e,s){let r=t.getLoops(e);if(r.length<3)return;let o=de(t.func,e),i=r.filter(c=>!o||!fa(c,o));if(i.length===0)return;let a=i[0];for(let c=1;c<i.length;c++){let u=t.getLoops(e).find(h=>h.loopVar.name===i[c].loopVar.name);u&&Ky(a,u)&&(a=t.fuseLoops(a,u))}jy(t,a,s)}},Gy=class extends In{static{f(this,"ElementwiseWasmRule")}constructor(){super("elementwise_wasm")}matches(t,e,s){if(s.kind!==fe.WASM)return!1;let r=de(t,e);return!(!r||r.hasReduction||r.loopCount<1||r.loops.length>0&&qB(r.loops[0]))}apply(t,e,s){let r=t.getLoops(e);if(r.length===0)return;let o=s.numCores||1,i=s.vectorWidth||4;if(o>1&&r.length>=1){let l=r[0].extent,u=l&&l.type==="IntImmNode"?l.value:0;if(u>=o*4){if(r.length===1&&s.supportsSimd&&s.supportsSimd()&&u>=i*2&&u%i===0){let[h,p]=t.split(r[0],i);t.parallelize(h),t.vectorize(p);return}if(t.parallelize(r[0]),r.length>1){let h=r[r.length-1],p=h.extent;if(p.type==="IntImmNode"&&p.value>=i&&p.value%i===0){let[,d]=t.split(h,i);t.vectorize(d)}}return}}let a=r[r.length-1],c=a.extent;if(c.type==="IntImmNode"&&c.value>=i*2){let[l,u]=t.split(a,i);t.vectorize(u);return}c.type==="IntImmNode"&&c.value>=i&&t.vectorize(a)}},qy=class extends In{static{f(this,"ReductionWasmRule")}constructor(){super("reduction_wasm")}matches(t,e,s){if(s.kind!==fe.WASM)return!1;let r=s.numCores>1,o=s.supportsSimd&&s.supportsSimd();if(!r&&!o)return!1;let i=de(t,e);return i?i.hasReduction&&i.loopCount>=2:!1}apply(t,e,s){let r=t.getLoops(e);if(r.length<2)return;let o=de(t.func,e),i=[],a=[];for(let c of r)!o||!fa(c,o)?i.push(c):a.push(c);if(i.length>0){let c=i[0].extent;(c&&c.type==="IntImmNode"?c.value:0)>=(s.numCores||1)*4&&t.parallelize(i[0])}if(s.supportsSimd&&s.supportsSimd()&&a.length>0){let c=a[a.length-1];(c.extent&&c.extent.type==="IntImmNode"?c.extent.value:0)>=s.vectorWidth*2&&t.vectorize(c)}}},Uy=class extends In{static{f(this,"FallbackRule")}constructor(){super("fallback")}matches(){return!0}apply(t,e,s){let r=t.getLoops(e);r.length!==0&&s.isCPU()&&r.length>=1&&t.parallelize(r[0])}};f(fa,"isReductionLoop");f(Ky,"findDirectChild");Cr=class n{static{f(this,"SchedulePolicy")}constructor(t,e=null,s=null){this.target=t,this.rules=e||n.defaultRules(),this.trace=s}static defaultRules(){return[new Fy,new Vy,new zy,new $y,new qy,new Ly,new Py,new Gy,new Uy]}selectRule(t,e){for(let s of this.rules)if(s.matches(t,e,this.target))return s;return null}applyToBlock(t,e){let s=this.selectRule(t.func,e);return s?(s.apply(t,e,this.target),FB(t.func),this._explain(e,s.name,`matched rule '${s.name}' for ${this.target.name}`),s.name):(this._explain(e,"none","no schedule rule matched; runs sequentially"),null)}_explain(t,e,s){this.trace&&this.trace.explainsEnabled&&this.trace.explain("schedule",t,e,s,{target:this.target.name})}applyToAllBlocks(t){FB(t.func);let e=UT(t.func.body),s=new Set,r=new Map;for(let o of e){if(s.has(o))continue;s.add(o);let i=this.applyToBlock(t,o);i&&r.set(o,i)}return r}};f(UT,"collectAllBlockNames")});function WT(n,t,e,s){let r=n;for(let o=0;o<t.length;o++)r+="|"+t[o].shape.join(",")+":"+t[o].dtype;if(e)for(let[o,i]of Object.entries(e))r+="|"+o+"="+JSON.stringify(i);return r+="|"+s.name,r}function jT(n){let t=WB.get(n);return t||(t=new na("jit_"+n),WB.set(n,t)),t}function HT(n){let t=1;for(let e of n.shape)t*=typeof e=="number"&&e>0?e:1;return Math.max(t,1)}function YT(n,t,e){let s=[];for(let[,r]of e.bufferMap)s.push(new(Jt(r.dtype))(HT(r)));n.run(t.name,...s)}function JT(n,t,e,s){try{let r=wc(n,t);if(r.shapeParams&&r.shapeParams.length>0)return null;new Cr(t).applyToAllBlocks(new Ts(r));let o=e.compile(r);return s.addCompiledKernel(o),t.isWebGPU()||YT(s,o,r),o}catch{return null}}function XT(n,t,e){let s=t.map(a=>new E(a.shape,a.dtype)),r=n+"_jit_"+ZT++,o=pc(r,s,[],(a,c)=>{let l;if(KT[n]){let u=e?.dim,h=c[0].type.rank,p=u!=null?(Array.isArray(u)?u:[u]).map(g=>g<0?h+g:g):Array.from({length:h},(g,_)=>_),d=tI(n,c[0].type.dtype),m=a.scalarConstant(d,c[0].type.dtype);l=a.reduce(c[0],m.getResult(0),p,n)}else typeof jB[n]=="function"?l=jB[n](a,c,e):typeof a[n]=="function"?l=QT(a,n,c,e):l=a._inferAndBuild(n,c,e);a.returnOp([l.getResult(0)])}),i=o.getReturnOp();return i&&i.operands.length>0&&(o.outputTypes=Object.freeze([i.operands[0].type])),o}function QT(n,t,e,s){return e.length===1?n[t](e[0]):e.length===2?n[t](e[0],e[1]):e.length===3?n[t](e[0],e[1],e[2]):n._inferAndBuild(t,e,s)}function tI(n,t){return uf(n,t)}function KB(n,t,e,s){let r=WT(n,t,e,s),o=UB.get(r);if(o)return o;let i=XT(n,t,e),a=new _s(n+"_jit_mod");a.addFunction(i);let c=new Wo;c.addPass(new oa),c.addPass(new jo),c.addPass(new cr),c.run(a);let l=jT(s.name),u=new Wn(s),p=typeof s.isGPU=="function"&&s.isGPU()?JT(i,s,u,l):null;p||(p=u.compile(wc(i,s)),l.addCompiledKernel(p));let d=i.getReturnOp(),m=d&&d.operands.length>0?d.operands[0].type.dtype:null;return o={funcName:p.name,runtime:l,numInputs:t.length,outDtype:m,compiled:p},UB.set(r,o),o}var UB,WB,KT,jB,ZT,HB=T(()=>{zn();xo();kt();Li();Oc();oh();by();xy();Sy();Ay();As();lu();ro();Qs();UB=new Map,WB=new Map;f(WT,"_cacheKey");f(jT,"_getRuntime");KT={sum:"sum",mean:"mean",max:"max",min:"min",prod:"prod"},jB={matmul:"matmul",dot:f((n,t)=>n.dot(t[0],t[1],[t[0].type.rank-1],[0]),"dot"),clone:f((n,t)=>n._inferAndBuild("add",[t[0],n.scalarConstant(0,t[0].type.dtype).getResult(0)]),"clone"),transpose:f((n,t,e)=>{let s=t[0].type.rank,r=e?.dim0??0,o=e?.dim1??1,i=Array.from({length:s},(a,c)=>c);return i[r]=o,i[o]=r,n.transpose(t[0],i)},"transpose"),softmax:f((n,t,e)=>n.softmax(t[0],e?.dim??-1),"softmax"),log_softmax:f((n,t,e)=>n.logSoftmax(t[0],e?.dim??-1),"log_softmax"),layer_norm:f((n,t,e)=>n.layernorm(t[0],t[1],t[2],e?.axis??-1,e?.eps??1e-5),"layer_norm"),batch_norm:f((n,t,e)=>n.batchnorm(t[0],t[1],t[2],t[3],t[4],e?.axis??1,e?.eps??1e-5),"batch_norm"),conv2d:f((n,t,e)=>n.conv(t[0],t[1],e?.strides??[1,1],e?.padding??[[0,0],[0,0]],{dilation:e?.dilation??[1,1],groups:e?.groups??1}),"conv2d"),pool2d:f((n,t,e)=>n.pool2d(t[0],e?.pool_type??"max",e?.kernel_size??[2,2],e?.strides??[2,2],e?.padding??[[0,0],[0,0]]),"pool2d"),embedding:f((n,t)=>n.embedding(t[0],t[1]),"embedding"),argmax:f((n,t,e)=>n.argmax(t[0],e?.dim??0,e?.keepdim??!1),"argmax"),argmin:f((n,t,e)=>n.argmin(t[0],e?.dim??0,e?.keepdim??!1),"argmin"),eq:f((n,t)=>n.compare(t[0],t[1],"eq"),"eq"),ne:f((n,t)=>n.compare(t[0],t[1],"ne"),"ne"),lt:f((n,t)=>n.compare(t[0],t[1],"lt"),"lt"),le:f((n,t)=>n.compare(t[0],t[1],"le"),"le"),gt:f((n,t)=>n.compare(t[0],t[1],"gt"),"gt"),ge:f((n,t)=>n.compare(t[0],t[1],"ge"),"ge"),clamp:f((n,t)=>n.clamp(t[1],t[0],t[2]),"clamp"),pad:f((n,t,e)=>n.pad(t[0],t[1],e.low,e.high),"pad"),one_hot:f((n,t,e)=>n.oneHot(t[0],e.depth,{dtype:J.F32}),"one_hot"),index_select:f((n,t,e)=>n.gather(t[0],t[1],Ef(t[0].type,e.dim??0,t[1].type.rank)),"index_select"),gather:f((n,t,e)=>n.gatherDim(t[0],t[1],e.dim??0),"gather"),scatter_add:f((n,t,e)=>n.scatterAddDim(t[0],t[1],t[2],e.dim??0),"scatter_add"),cat:f((n,t,e)=>{let s=t[0].type.rank,r=(e?.dim??0)<0?s+(e?.dim??0):e?.dim??0;return n.concat(t,r)},"cat"),stack:f((n,t,e)=>{let s=t[0].type.rank,r=(e?.dim??0)<0?s+1+(e?.dim??0):e?.dim??0,o=t.map(i=>{let a=[...i.type.shape];return a.splice(r,0,1),n.reshape(i,a).getResult(0)});return n.concat(o,r)},"stack")};f(HT,"_bufferNumel");f(YT,"_trialLaunch");f(JT,"_compileScheduledGPU");ZT=0;f(XT,"_buildGraphFunc");f(QT,"_callBuilder");f(tI,"_reductionInit");f(KB,"jitCompile")});var TB={};Ft(TB,{getCudnnLSTM:()=>Yy,getGpuAdamFn:()=>Zy,getGpuContiguousFn:()=>Y_,getGpuMatmul:()=>$_,getWebgpuRNN:()=>Jy,gpuContiguousArray:()=>K_,registerJITKernels:()=>Qy,setCudnnLSTM:()=>ty,setGpuAdamFn:()=>ey,setGpuConcatFn:()=>Q_,setGpuContiguousFn:()=>X_,setGpuMatmul:()=>sy,setWebGPUEagerFn:()=>iI,setWebgpuRNN:()=>pI,tensorToContiguous:()=>Is,wrapResult:()=>jt});function iI(n){YB=n}function cI(n,t){let e=[],s={},r=aI[n],o=0;for(let i=0;i<t.length;i++){let a=t[i];if(a&&a._impl)e.push(a);else if(Array.isArray(a)&&a.length>0&&a[0]&&a[0]._impl)for(let c of a)c&&c._impl&&e.push(c);else a!=null&&(r&&o<r.length&&(s[r[o]]=a),o++)}return{tensors:e,scalars:s}}function JB(n,t,e){if(t.length===0)return[];if(new Set(["sum","mean","max","min","prod","argmax","argmin"]).has(n)){let o=t[0].shape,i=e.dim,a=e.keepdim;if(i==null)return a?o.map(()=>1):[];let c=new Set((Array.isArray(i)?i:[i]).map(u=>u<0?o.length+u:u)),l=[];for(let u=0;u<o.length;u++)c.has(u)?a&&l.push(1):l.push(o[u]);return l}if(n==="matmul")return Ep(t[0].shape,t[1].shape);if(n==="dot")return[];if(n==="transpose"){let o=[...t[0].shape],i=e.dim0??0,a=e.dim1??1,c=o[i];return o[i]=o[a],o[a]=c,o}if(n==="conv2d"){let o=t[0].shape,i=t[1].shape,a=e.strides||[1,1],c=e.padding||[[0,0],[0,0]],l=e.dilation||[1,1],u=[];for(let h=0;h<2;h++){let p=c[h][0]+c[h][1],d=(i[h+2]-1)*l[h]+1;u.push(Math.floor((o[h+2]+p-d)/a[h])+1)}return[o[0],i[0],...u]}if(n==="pool2d"){let o=t[0].shape,i=e.kernel_size||[2,2],a=e.strides||i,c=e.padding||[[0,0],[0,0]],l=[];for(let u=0;u<2;u++){let h=c[u][0]+c[u][1];l.push(Math.floor((o[u+2]+h-i[u])/a[u])+1)}return[o[0],o[1],...l]}if(n==="clamp"){let o=t[0].shape;for(let i=1;i<t.length;i++)o=Hi(o,t[i].shape)||o;return o}if(n==="pad"){let o=t[0].shape,i=e.low||[],a=e.high||[];return o.map((c,l)=>c+(i[l]||0)+(a[l]||0))}if(n==="one_hot")return[...t[0].shape,e.depth];if(n==="cat"){let o=t[0].shape.length,i=(e.dim??0)<0?o+(e.dim??0):e.dim??0,a=[...t[0].shape];return a[i]=t.reduce((c,l)=>c+l.shape[i],0),a}if(n==="stack"){let o=t[0].shape.length,i=(e.dim??0)<0?o+1+(e.dim??0):e.dim??0,a=[...t[0].shape];return a.splice(i,0,t.length),a}if(n==="index_select"){let o=[...t[0].shape],i=o.length,a=(e.dim??0)<0?i+(e.dim??0):e.dim??0;return o[a]=t[1].shape.reduce((c,l)=>c*l,1),o}if(n==="gather")return[...t[1].shape];if(n==="scatter_add")return[...t[0].shape];if(n==="softmax"||n==="log_softmax")return[...t[0].shape];if(n==="layer_norm"||n==="batch_norm")return[...t[0].shape];if(n==="embedding"){let o=t[0].shape;return[...t[1].shape,o[1]]}return t.length===1?[...t[0].shape]:Hi(t[0].shape,t[1].shape)||[...t[0].shape]}function Is(n){let t=n._impl.storage.data,e=n._impl.storageOffset,s=n.numel;if(n.isContiguous&&e===0&&t.length===s)return n.data;let r=n.shape,o=n.strides,i=t.constructor,a=new i(s),c=r.length,l=new Int32Array(c),u=e;for(let h=0;h<s;h++){a[h]=t[u];for(let p=c-1;p>=0;p--){if(l[p]++,l[p]<r[p]){u+=o[p];break}u-=(r[p]-1)*o[p],l[p]=0}}return a}function jt(n,t,e,s){let r=He(t),o=ps.fromData(n,s),i=new he(o,0,t,r,e,s);return new zt(i)}function dh(n,t,e){let s=1;for(let r=t;r<e;r++)s*=n[r];return s}function uI(n,t,e,s){let r=t.length,o=e<0?r+1+e:e,i=dh(t,0,o),a=dh(t,o,r),c=n.length;for(let l=0;l<i;l++)for(let u=0;u<c;u++)s.set(n[u].subarray(l*a,(l+1)*a),(l*c+u)*a)}function lI(n,t,e,s){let r=t[0].length,o=e<0?r+e:e,i=dh(t[0],0,o),a=dh(t[0],o+1,r),c=0;for(let l of t)c+=l[o];for(let l=0;l<i;l++){let u=l*c*a;for(let h=0;h<n.length;h++){let p=t[h][o]*a;s.set(n[h].subarray(l*p,(l+1)*p),u),u+=p}}}function fI(n,t,e){let s=JB(n,t,e),r=t[0].dtype,o=Jt(r),i=new o(Math.max(Pe(s),1)),a=e.dim??0;if(Ke()&&Hy){let l=t.map(u=>Xy(u));return Hy(n,l,t.map(u=>u.shape),a,s,i,r),jt(i,s,r,t[0].device)}let c=t.map(l=>Is(l));return n==="stack"?uI(c,t[0].shape,a,i):lI(c,t.map(l=>l.shape),a,i),jt(i,s,r,t[0].device)}function X_(n){mh=n}function Y_(){return mh}function Q_(n){Hy=n}function ty(n){ZB=n}function Yy(){return ZB}function pI(n){XB=n}function Jy(){return XB}function ey(n){QB=n}function Zy(){return QB}function sy(n){tk=n}function $_(){return tk}function Xy(n){if(n.isContiguous&&n._impl.storageOffset===0){let t=n._impl.storage.rawData;if(t&&t.length===n.numel)return t}return Ke()&&mh?mh(n._impl.storage.rawData,n.shape,n.strides,n._impl.storageOffset,n.dtype):Is(n)}function K_(n){return Xy(n)}function hI(n,t){let e=oI[t];if(!e)return null;let s=t===nt.GPU,r=t===nt.CUSTOM_0,o=s&&(n==="stack"||n==="cat");return(i,...a)=>{let{tensors:c,scalars:l}=cI(n,a);if(c.length===0)throw new Error(`JIT dispatch: no tensor args for op '${n}'`);if(o)return fI(n,c,l);let u=e(),h=KB(n,c,l,u),p=JB(n,c,l),d=h.outDtype||Ap(c[0].dtype,c.length>1?c[1].dtype:c[0].dtype),m=Pe(p),g=Jt(d),_=new g(Math.max(m,1));if(r)return YB(h.compiled,c,_),jt(_,p,d,c[0].device);let y=c.map(b=>s?Xy(b):Is(b));return y.push(_),h.runtime.run(h.funcName,...y),jt(_,p,d,c[0].device)}}function Qy(){let n=Re.listOps(),t=[nt.CPU,nt.GPU,nt.WASM,nt.CUSTOM_0];for(let e of n){let s=Re.findOp(e);if(!s)continue;let r=s.name;for(let o of t){if(s.entry.hasKernel(o))continue;let i=hI(r,o);i&&s.entry.registerKernel(o,Ls.fromUnboxed(i))}}}var eI,sI,nI,rI,oI,YB,aI,mh,Hy,ZB,XB,QB,tk,hn=T(()=>{js();ec();mi();HB();vr();qn();Ps();Br();Po();kn();Qs();oI={[nt.CPU]:()=>eI??=Gn(),[nt.GPU]:()=>sI??=zf(),[nt.WASM]:()=>nI??=$f(),[nt.CUSTOM_0]:()=>rI??=Ff()},YB=null;f(iI,"setWebGPUEagerFn");aI={sum:["dim","keepdim"],mean:["dim","keepdim"],max:["dim","keepdim"],min:["dim","keepdim"],prod:["dim","keepdim"],argmax:["dim","keepdim"],argmin:["dim","keepdim"],transpose:["dim0","dim1"],softmax:["dim"],log_softmax:["dim"],layer_norm:["axis","eps"],batch_norm:["axis","eps"],conv2d:["strides","padding","dilation","groups"],pool2d:["pool_type","kernel_size","strides","padding"],pad:["low","high"],one_hot:["depth"],index_select:["dim"],gather:["dim"],scatter_add:["dim"],cat:["dim"],stack:["dim"]};f(cI,"_extractTensorsAndScalars");f(JB,"_inferOutputShape");f(Is,"tensorToContiguous");f(jt,"wrapResult");f(dh,"_prod");f(uI,"_hostStack");f(lI,"_hostCat");f(fI,"_runHostConcatLike");mh=null;f(X_,"setGpuContiguousFn");f(Y_,"getGpuContiguousFn");Hy=null;f(Q_,"setGpuConcatFn");ZB=null;f(ty,"setCudnnLSTM");f(Yy,"getCudnnLSTM");XB=null;f(pI,"setWebgpuRNN");f(Jy,"getWebgpuRNN");QB=null;f(ey,"setGpuAdamFn");f(Zy,"getGpuAdamFn");tk=null;f(sy,"setGpuMatmul");f($_,"getGpuMatmul");f(Xy,"_gpuInputArray");f(K_,"gpuContiguousArray");f(hI,"_wrapOpForJIT");f(Qy,"registerJITKernels")});var zI,ue,ga=T(()=>{Ps();Br();Po();js();tn();kn();zI=Ws.fromKey(nt.TRACING),ue=class extends zt{static{f(this,"SymbolicTensor")}constructor(t,e,s,r,o){let i=He(e),a=ps.allocate(0,s,Ki),c=new he(a,0,e,i,s,Ki);super(c),this._irValue=t,this._tracer=r,this._symbolicShape=o}get irValue(){return this._irValue}get tracer(){return this._tracer}get symbolicShape(){return this._symbolicShape}get dispatchKeySet(){return super.dispatchKeySet.union(zI)}get isSymbolic(){return!0}}});var YI,Qh,Hk=T(()=>{kt();By();YI={eq:f((n,t)=>n===t,"eq"),ne:f((n,t)=>n!==t,"ne"),gt:f((n,t)=>n>t,"gt"),ge:f((n,t)=>n>=t,"ge"),lt:f((n,t)=>n<t,"lt"),le:f((n,t)=>n<=t,"le")},Qh=class{static{f(this,"ShapeEnv")}constructor(){this._symbols=new Map,this._guards=[],this._bindings=new Map,this._nextId=0}allocate(t,e,s){let r=`s${this._nextId++}`;return this._symbols.set(r,{hint:s,inputIdx:t,dimIdx:e}),r}produceShapeSpec(t,e,s){let r=new Array(e.length),o=new Array(e.length);for(let i=0;i<e.length;i++)if(s&&s.has(i)){let a=this.allocate(t,i,e[i]);r[i]=Z,o[i]=a}else{let a=this.allocate(t,i,e[i]);this.guardRelation(a,"eq",e[i]),r[i]=e[i],o[i]=e[i]}return{irShape:r,symShape:o}}guardRelation(t,e,s){this._guards.push({lhs:t,op:e,rhs:s})}guardDivisible(t,e){this._guards.push({type:"divisible",sym:t,divisor:e})}bindInputShapes(t){this._bindings.clear();for(let[e,s]of this._symbols)this._bindings.set(e,t[s.inputIdx].shape[s.dimIdx])}evaluateGuards(){for(let t=0;t<this._guards.length;t++){let e=this._guards[t];if(e.type==="divisible"){if(this._resolve(e.sym)%e.divisor!==0)return{passed:!1,failedGuard:e};continue}let s=this._resolve(e.lhs),r=this._resolve(e.rhs);if(!YI[e.op](s,r))return{passed:!1,failedGuard:e}}return{passed:!0,failedGuard:null}}resolveSymbolicShape(t){let e=new Array(t.length);for(let s=0;s<t.length;s++)e[s]=this._resolve(t[s]);return e}_resolve(t){return typeof t=="number"?t:typeof t=="string"?this._bindings.get(t):t instanceof so?so.evaluate(t,this._bindings):t}get symbols(){return this._symbols}get guards(){return this._guards}get bindings(){return this._bindings}}});function Pu(n,t,e,s){let r=t[0].type.rank,o=e?.dim,i=o!=null?(Array.isArray(o)?o:[o]).map(h=>h<0?r+h:h):Array.from({length:r},(h,p)=>p),a=n.scalarConstant(uf(s,t[0].type.dtype),t[0].type.dtype),c=n.reduce(t[0],a.getResult(0),i,s);if(!e?.keepdim)return c;let l=new Set(i),u=t[0].type.shape.map((h,p)=>l.has(p)?1:h);return n.reshape(c.getResult(0),u)}function Zo(){return td}var JI,td,ed,zu=T(()=>{zn();xo();Wr();kt();Ee();ga();Hk();As();f(Pu,"_traceReduce");JI={matmul:f((n,t)=>n.matmul(t[0],t[1]),"matmul"),softmax:f((n,t,e)=>n.softmax(t[0],e?.dim??-1),"softmax"),log_softmax:f((n,t,e)=>n.logSoftmax(t[0],e?.dim??-1),"log_softmax"),layer_norm:f((n,t,e)=>n.layernorm(t[0],t[1],t[2],e?.axis??-1,e?.eps??1e-5),"layer_norm"),batch_norm:f((n,t,e)=>n.batchnorm(t[0],t[1],t[2],t[3],t[4],e?.axis??1,e?.eps??1e-5),"batch_norm"),embedding:f((n,t)=>n.embedding(t[0],t[1]),"embedding"),relu:f((n,t)=>n.relu(t[0]),"relu"),sigmoid:f((n,t)=>n.sigmoid(t[0]),"sigmoid"),gelu:f((n,t)=>n.gelu(t[0]),"gelu"),silu:f((n,t)=>n.silu(t[0]),"silu"),conv2d:f((n,t,e)=>n.conv(t[0],t[1],e?.strides??[1,1],e?.padding??[[0,0],[0,0]],{dilation:e?.dilation,groups:e?.groups}),"conv2d"),pool2d:f((n,t,e)=>n.pool2d(t[0],e?.pool_type??"max",e?.kernel_size??[2,2],e?.strides??[2,2],e?.padding??[[0,0],[0,0]]),"pool2d"),maximum:f((n,t)=>n.maximum(t[0],t[1]),"maximum"),minimum:f((n,t)=>n.minimum(t[0],t[1]),"minimum"),sum:f((n,t,e)=>Pu(n,t,e,"sum"),"sum"),mean:f((n,t,e)=>Pu(n,t,e,"mean"),"mean"),max:f((n,t,e)=>Pu(n,t,e,"max"),"max"),min:f((n,t,e)=>Pu(n,t,e,"min"),"min"),prod:f((n,t,e)=>Pu(n,t,e,"prod"),"prod"),argmax:f((n,t,e)=>n.argmax(t[0],e?.dim??0,e?.keepdim??!1),"argmax"),argmin:f((n,t,e)=>n.argmin(t[0],e?.dim??0,e?.keepdim??!1),"argmin"),eq:f((n,t)=>n.compare(t[0],t[1],"eq"),"eq"),ne:f((n,t)=>n.compare(t[0],t[1],"ne"),"ne"),lt:f((n,t)=>n.compare(t[0],t[1],"lt"),"lt"),le:f((n,t)=>n.compare(t[0],t[1],"le"),"le"),gt:f((n,t)=>n.compare(t[0],t[1],"gt"),"gt"),ge:f((n,t)=>n.compare(t[0],t[1],"ge"),"ge"),transpose:f((n,t,e)=>{let s=t[0].type.rank,r=e?.dim0??0,o=e?.dim1??1,i=Array.from({length:s},(a,c)=>c);return i[r]=o,i[o]=r,n.transpose(t[0],i)},"transpose"),clamp:f((n,t)=>n.clamp(t[1],t[0],t[2]),"clamp"),pad:f((n,t,e)=>n.pad(t[0],t[1],e.low,e.high),"pad"),one_hot:f((n,t,e)=>n.oneHot(t[0],e.depth,{dtype:J.F32}),"one_hot"),index_select:f((n,t,e)=>n.gather(t[0],t[1],Ef(t[0].type,e?.dim??0,t[1].type.rank)),"index_select"),gather:f((n,t,e)=>n.gatherDim(t[0],t[1],e?.dim??0),"gather"),scatter_add:f((n,t,e)=>n.scatterAddDim(t[0],t[1],t[2],e?.dim??0),"scatter_add"),cat:f((n,t,e)=>{let s=t[0].type.rank,r=(e?.dim??0)<0?s+(e?.dim??0):e?.dim??0;return n.concat(t,r)},"cat"),stack:f((n,t,e)=>{let s=t[0].type.rank,r=(e?.dim??0)<0?s+1+(e?.dim??0):e?.dim??0,o=t.map(i=>{let a=[...i.type.shape];return a.splice(r,0,1),n.reshape(i,a).getResult(0)});return n.concat(o,r)},"stack"),permute:f((n,t,e)=>n.transpose(t[0],e.dims),"permute"),reshape:f((n,t,e)=>n.reshape(t[0],e.new_shape),"reshape"),slice:f((n,t,e)=>n.slice(t[0],e.starts,e.limits,e.strides),"slice")},td=null;f(Zo,"getActiveTracer");ed=class{static{f(this,"Tracer")}constructor(t){this._name=t||"traced",this._shapeEnv=new Qh,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(t,e,s){let r=this._inputTypes.length,{irShape:o,symShape:i}=this._shapeEnv.produceShapeSpec(r,t,s);if(s)for(let c=0;c<i.length;c++)typeof i[c]=="string"&&this._shapeEnv.guardRelation(i[c],"gt",0);let a=new E(o,e);return this._inputTypes.push(a),this._inputSymShapes.push(i),{shape:o,dtype:e,tensorType:a}}_initGraph(){this._func=new os(this._name,this._inputTypes,[]),this._func.inputTypes=[...this._func.inputTypes],this._builder=new We(this._func),this._module=new _s(this._name);let t=[],e=this._func.args;for(let s=0;s<e.length;s++){let r=e[s],o=this._inputTypes[s];r.symbolicShape=this._inputSymShapes[s];let i=new ue(r,o.shape,o.dtype,this,this._inputSymShapes[s]);t.push(i)}return this._inputs=t,t}recordOp(t,e,s){let r=[];for(let u of e)u instanceof ue&&r.push(u.irValue);let o,i=JI[t];i?o=i(this._builder,r,s):typeof this._builder[t]=="function"?o=this._builder[t](...r):o=this._builder._inferAndBuild(t,r,s||null);let a=o.getResult(0),c=a.type,l=this._propagateSymbolicShape(t,o,e,c);return a.symbolicShape=l,new ue(a,c.shape,c.dtype,this,l)}_propagateSymbolicShape(t,e,s,r){let o=s.filter(l=>l instanceof ue),i=st.get(e.opName||t);if(i&&i.propagateSymbolicShapes){let l=new Map;for(let h of o)l.set(h.irValue,h.symbolicShape);let u=i.propagateSymbolicShapes(e,l);if(u&&u[0])return u[0]}let a=r.shape,c=new Array(a.length);for(let l=0;l<a.length;l++){if(a[l]!==Z){c[l]=a[l];continue}let u=null;for(let h of o){let p=h.symbolicShape;if(!p)continue;let d=a.length-p.length,m=l-d;if(m>=0&&m<p.length&&typeof p[m]=="string"){u=p[m];break}}c[l]=u!==null?u:Z}return c}scan(t,e,s){let r=f(h=>h instanceof ue?h.irValue:this.captureConstant(h).irValue,"toIr"),o=t.map(r),i=e.map(r),a=this._builder.scanOp(o,i,(h,p,d)=>{let m=this._builder;this._builder=h;try{let g=f(b=>new ue(b,b.type.shape,b.type.dtype,this,[...b.type.shape]),"wrap"),[_,y]=s(d.map(g),p.map(g));return[_.map(b=>b.irValue),y.map(b=>b.irValue)]}finally{this._builder=m}}),c=e.length,l=[],u=[];for(let h=0;h<a.numResults;h++){let p=a.getResult(h),d=new ue(p,p.type.shape,p.type.dtype,this,[...p.type.shape]);h<c?l.push(d):u.push(d)}return[l,u]}captureConstant(t){let e=this._capturedParams.get(t);if(e)return e;if(t.shape.length===0&&t.data){let a=t.data[0],l=this._builder.scalarConstant(a,t.dtype).getResult(0),u=new ue(l,[],t.dtype,this,[]);return this._capturedParams.set(t,u),u}let s=new E(t.shape,t.dtype);this._func.inputTypes.push(s);let o=this._func.entryBlock.addArgument(s),i=new ue(o,t.shape,t.dtype,this,[...t.shape]);return this._capturedParams.set(t,i),this._capturedParamOrder.push(t),i}get capturedParams(){return this._capturedParamOrder}markOutput(t){t instanceof ue&&(this._builder.returnOp([t.irValue]),this._outputSymShapes=[t.symbolicShape]),this._outputTypes=[new E(t.shape,t.dtype)]}markOutputs(t){let e=t.map(s=>s.irValue);this._builder.returnOp(e),this._outputTypes=t.map(s=>new E(s.shape,s.dtype)),this._outputSymShapes=t.map(s=>s instanceof ue?s.symbolicShape:[...s.shape])}get outputSymShapes(){return this._outputSymShapes}getGraphModule(){return this._func.outputTypes=Object.freeze(this._outputTypes),Object.isFrozen(this._func.inputTypes)||(this._func.inputTypes=Object.freeze(this._func.inputTypes)),this._module.addFunction(this._func),this._module}activate(){td=this}deactivate(){td===this&&(td=null)}}});function aA(n){let t=new gi;return t.add(new xi),t.add(new Si),t.add(new vi(n)),t.add(new Bi),t.add(new ki(n)),t.add(new Ai),t.add(new Ri),t.add(new df),t.add(new wi),t.add(new yf),t.add(new bf),t.add(new wf),t.add(new xf),n&&(t.add(new _f(n)),t.add(new mf(n)),t.add(new gf(n))),t}var dM,mM,cd,cA=T(()=>{Ce();ac();ch();Oi();f(aA,"buildAlgebraicPatterns");dM=aA(!1),mM=aA(!0),cd=class extends Rt{static{f(this,"AlgebraicSimplificationPass")}constructor(t={}){super("algebraic_simplify"),this.preservedAnalyses=new Set([no]),this.patterns=t.fastMath?mM:dM}run(t,e){return new _i(this.patterns).applyPatterns(t,10,this.trace)}}});function uA(n){if(gM.has(n))return!0;let t=st.get(n);return!!(t&&t.isConstant)}function _M(n,t){return!Um(t)||typeof n!="number"?!0:Number.isInteger(n)&&Number.isSafeInteger(n)}function lA(n,t,e){if(e.has(n))return e.get(n);let s=yM(n,t,e);return e.set(n,s),s}function yM(n,t,e){let s=n.definingOp;if(!s)return;if(uA(s.opName))return s.getAttr("value");if(t.has(s))return;t.add(s);let r=st.get(s.opName);if(!r||!r.fold||r.sideEffects||r.hasTrait&&r.hasTrait(U.SIDE_EFFECT)||s.regions.length>0)return;let o=new Array(s.numOperands),i=new Array(s.numOperands);for(let a=0;a<s.numOperands;a++){let c=lA(s.getOperand(a),t,e);if(c===void 0)return;o[a]=c,i[a]=s.getOperand(a).definingOp}try{return r.fold(o,s.attributes,i)}catch{return}}var gM,ud,fA=T(()=>{Ce();zn();Ee();we();Ue();kt();gM=new Set(["constant","scalar_constant"]);f(uA,"isConstantProducer");f(_M,"isFoldResultRepresentable");f(lA,"resolveConstantValue");f(yM,"computeConstantValue");ud=class extends Rt{static{f(this,"ConstantFoldPass")}constructor(){super("constant_fold")}run(t,e){let s=!1,r=0,o=new We(t),i=new Map;for(let a of[...t.opsRecursive()]){if(!a.parentBlock||uA(a.opName))continue;let c=st.get(a.opName);if(!c||a.regions.length>0||c.sideEffects||c.hasTrait&&c.hasTrait(U.SIDE_EFFECT)||c.getMemoryEffects&&c.getMemoryEffects(a).length>0||!c.fold||a.numOperands===0)continue;let l=new Array(a.numOperands),u=new Array(a.numOperands),h=!0;for(let p=0;p<a.numOperands;p++){let d=lA(a.getOperand(p),new Set,i);if(d===void 0){h=!1;break}l[p]=d,u[p]=a.getOperand(p).definingOp}if(h)try{let p=c.fold(l,a.attributes,u);if(p===void 0||!_M(p,a.getResult(0).type.dtype))continue;o.block=a.parentBlock,o.setInsertionPoint(a);let d=o.constant(p,a.getResult(0).type);a.replaceAllResultsWith([d.getResult(0)]),a.erase(),s=!0,r++}catch{}}return this.trace&&this.trace.level>=ot.DEBUG&&r>0&&this.trace.emit({type:"pass_detail",passName:this.name,foldedCount:r,level:ot.DEBUG}),s?Q.CHANGED:Q.UNCHANGED}}});var ld,pA=T(()=>{Ce();Ee();we();ch();Ue();ld=class extends Rt{static{f(this,"CSEPass")}constructor(){super("cse"),this.preservedAnalyses=new Set([no])}run(t,e){let s=!1,r=0,o=typeof t.blocksRecursive=="function"?[...t.blocksRecursive()]:[...t.body];for(let i of o){let a=new Map;for(let c of[...i.ops()]){if(!c.parentBlock||c.regions&&c.regions.length>0)continue;let l=st.get(c.opName);if(l&&(l.sideEffects||l.hasTrait&&l.hasTrait(U.SIDE_EFFECT))||l&&l.getMemoryEffects&&l.getMemoryEffects(c).length>0)continue;let u=c.structuralHash();if(!a.has(u)){a.set(u,[c]);continue}let h=a.get(u),p=!1;for(let d of h)if(d.parentBlock&&d.structuralEquals(c)){let m=[];for(let g=0;g<d.numResults;g++)m.push(d.getResult(g));c.replaceAllResultsWith(m),c.erase(),s=!0,r++,p=!0;break}p||h.push(c)}}return this.trace&&this.trace.level>=ot.DEBUG&&r>0&&this.trace.emit({type:"pass_detail",passName:this.name,eliminated:r,level:ot.DEBUG}),s?Q.CHANGED:Q.UNCHANGED}}});function M0(n,t){let e=I0[n],s=I0[t];return e===void 0||s===void 0?!1:n===Et.REDUCTION?t===Et.ELEMENTWISE:t===Et.REDUCTION?e<=I0[Et.INJECTIVE]:!0}function Aa(n){let t=!1,e=!1,s=!1,r=!1;for(let o of n){let i=st.get(o.opName);if(!i||i.isOpaque){s=!0;continue}i.isReduction?t=!0:i.isInjective?e=!0:i.isBroadcast?r=!0:i.isElementwise||(s=!0)}return s?Et.OPAQUE:t?Et.REDUCTION:e?Et.INJECTIVE:r?Et.BROADCAST:Et.ELEMENTWISE}function ai(n){let t=st.get(n.opName);return!t||t.isOpaque?Et.OPAQUE:t.isReduction?Et.REDUCTION:t.isInjective?Et.INJECTIVE:t.isBroadcast?Et.BROADCAST:t.isElementwise?Et.ELEMENTWISE:Et.OPAQUE}function hA(n,t){if(n.length!==t.length)return!1;for(let e=0;e<n.length;e++)if(!(n[e]===Z||t[e]===Z)&&n[e]!==t[e])return!1;return!0}var Et,I0,ka,fd,Ra=T(()=>{kt();Ee();Li();Et=Object.freeze({ELEMENTWISE:"kElementwise",BROADCAST:"kBroadcast",REDUCTION:"kReduction",INJECTIVE:"kInjective",OPAQUE:"kOpaque",HORIZONTAL:"kHorizontal"}),I0={[Et.ELEMENTWISE]:0,[Et.BROADCAST]:1,[Et.INJECTIVE]:2,[Et.REDUCTION]:3};f(M0,"canFusePatterns");f(Aa,"classifyFusionKind");f(ai,"classifyOpPattern");ka=Object.freeze({legal:!0}),fd=class{static{f(this,"FusionLegality")}constructor(t={}){this.maxFusionSize=t.maxFusionSize||512,this.maxSharedMemory=t.maxSharedMemory||49152,this.libraryOps=t.libraryOps||new Set,this.allowReductionFusion=t.allowReductionFusion!==!1,this._lowerableCache=new Map}isOpLowerable(t){let e=this._lowerableCache.get(t);return e===void 0&&(e=t==="return"||t==="yield"||Bo(t)||pg(t),this._lowerableCache.set(t,e)),e}canFuse(t,e){if(!t||!e)return{legal:!1,reason:"null op"};if(t===e)return{legal:!1,reason:"same op"};if(t.regions.length>0&&t.opName!=="fusion"&&t.opName!=="reduce")return{legal:!1,reason:"producer has control flow regions"};if(e.regions.length>0&&e.opName!=="fusion"&&e.opName!=="reduce")return{legal:!1,reason:"consumer has control flow regions"};let s=st.get(t.opName),r=st.get(e.opName);if(!s)return{legal:!1,reason:"unknown producer op"};if(!r)return{legal:!1,reason:"unknown consumer op"};if(!this.isOpLowerable(t.opName))return{legal:!1,reason:`producer op '${t.opName}' has no lowering rule`};if(!this.isOpLowerable(e.opName))return{legal:!1,reason:`consumer op '${e.opName}' has no lowering rule`};if(s.isOpaque)return{legal:!1,reason:"producer is opaque (use EpilogueFusionPass for dot/conv epilogues)"};if(r.isOpaque)return{legal:!1,reason:"consumer is opaque"};let o=ai(t),i=ai(e);return M0(o,i)?(o===Et.REDUCTION||i===Et.REDUCTION)&&!this.allowReductionFusion?{legal:!1,reason:"reduction fusion disabled by target"}:o===Et.ELEMENTWISE&&i===Et.ELEMENTWISE?this._checkElementwisePair(t,e):(o===Et.BROADCAST||o===Et.REDUCTION)&&i===Et.ELEMENTWISE?ka:this._checkProducerConsumerShapes(t,e):{legal:!1,reason:`cannot fuse pattern ${o} -> ${i}`}}canMergeGroups(t,e){if(t.size+e.size>this.maxFusionSize)return{legal:!1,reason:"merged group exceeds max fusion size"};let s=0;for(let r of t.ops){let o=st.get(r.opName);if(o&&o.isReduction&&s++,o&&o.isOpaque)return{legal:!1,reason:"opaque op in merge (use EpilogueFusionPass)"}}for(let r of e.ops){let o=st.get(r.opName);if(o&&o.isReduction&&s++,o&&o.isOpaque)return{legal:!1,reason:"opaque op in merge (use EpilogueFusionPass)"}}return s>1?{legal:!1,reason:"merged group would contain multiple reductions"}:ka}_checkElementwisePair(t,e){let s=this._getOutputShape(t),r=this._getOutputShape(e);return s&&r&&!hA(s,r)?{legal:!1,reason:`elementwise shape mismatch: [${s}] vs [${r}]`}:ka}_checkProducerConsumerShapes(t,e){let s=this._getOutputShape(t);if(!s)return ka;for(let r=0;r<e.numOperands;r++)if(e.getOperand(r).definingOp===t){let o=e.getOperand(r).type;if(o instanceof E)return hA(s,o.shape)?ka:{legal:!1,reason:`shape mismatch on data edge: [${s}] vs [${o.shape}]`}}return ka}_getOutputShape(t){for(let e=0;e<t.numResults;e++){let s=t.getResult(e).type;if(s instanceof E)return s.shape}return null}};f(hA,"shapesCompatible")});function bM(n){for(let t=0;t<n.numResults;t++){let e=n.getResult(t).type;if(e instanceof E)return e.shape.join(",")}return null}var Oa,pd,C0=T(()=>{kt();Ee();Ra();Li();Oa=class{static{f(this,"FusionGroup")}constructor(t){this.id=t,this.ops=[],this.opSet=new Set,this.kind=null,this._inputValues=null,this._outputValues=null,this.minTopoPos=1/0,this.maxTopoPos=-1/0}addOp(t,e){this.opSet.has(t)||(this.ops.push(t),this.opSet.add(t),this._inputValues=null,this._outputValues=null,e!==void 0&&(e<this.minTopoPos&&(this.minTopoPos=e),e>this.maxTopoPos&&(this.maxTopoPos=e)))}hasOp(t){return this.opSet.has(t)}merge(t){for(let e of t.ops)this.addOp(e);t.minTopoPos<this.minTopoPos&&(this.minTopoPos=t.minTopoPos),t.maxTopoPos>this.maxTopoPos&&(this.maxTopoPos=t.maxTopoPos)}computeIO(){if(this._inputValues&&this._outputValues)return;this._inputValues=[],this._outputValues=[];let t=new Set,e=new Set;for(let s of this.ops){for(let r=0;r<s.numOperands;r++){let o=s.getOperand(r);o.definingOp&&this.opSet.has(o.definingOp)||t.has(o)||(t.add(o),this._inputValues.push(o))}for(let r=0;r<s.numResults;r++){let o=s.getResult(r);if(!e.has(o)){for(let i of o.uses())if(!this.opSet.has(i.user)){e.add(o),this._outputValues.push(o);break}}}}}getInputValues(){return this.computeIO(),this._inputValues}getOutputValues(){return this.computeIO(),this._outputValues}get size(){return this.ops.length}classifyKind(){return this.kind=Aa(this.ops),this.kind}allOpsInlineFusable(){for(let t of this.ops){let e=st.get(t.opName);if(!e)return!1;if(!(e.isReduction||e.isConstant)&&!Bo(t.opName))return!1}return!0}};f(bM,"outputShapeKey");pd=class{static{f(this,"FusionGroupBuilder")}constructor(t){this.legality=t,this._nextId=0,this._topoIndex=null}buildProducerConsumerGroups(t){this._topoIndex=new Map;let e=0;for(let i of t.ops())this._topoIndex.set(i,e++);let s=[],r=new Map;for(let i of t.ops()){let a=st.get(i.opName);if(!a||a.isConstant||a.isTerminator||a.isReduction)continue;let c=this._topoIndex.get(i);for(let l=0;l<i.numOperands;l++){let u=i.getOperand(l).definingOp;if(!u)continue;let h=st.get(u.opName);if(!h||h.isConstant||h.isReduction)continue;let p=r.get(i),d=r.get(u);if(p&&p===d||!this.legality.canFuse(u,i).legal)continue;let g=this._topoIndex.get(u);if(!p&&!d){let _=new Oa(this._nextId++);_.addOp(u,g),_.addOp(i,c),r.set(u,_),r.set(i,_)}else if(p&&!d)p.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(p,u)&&(p.addOp(u,g),r.set(u,p));else if(!p&&d)d.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(d,i)&&(d.addOp(i,c),r.set(i,d));else if(this.legality.canMergeGroups(p,d).legal&&!this._mergeWouldCreateCycle(p,d)){p.merge(d);for(let y of d.ops)r.set(y,p)}}}let o=new Set;for(let i of r.values())o.has(i)||i.size<2||(o.add(i),i.classifyKind(),s.push(i));return s}_wouldCreateCycle(t,e){for(let s=0;s<e.numOperands;s++){let r=e.getOperand(s).definingOp;if(!(!r||t.hasOp(r)||this._topoIndex.get(r)<t.minTopoPos)&&this._dependsOnGroup(r,t))return!0}return!1}_dependsOnOps(t,e,s){let r=new Set,o=[t];for(r.add(t);o.length>0;){let i=o.pop();for(let a=0;a<i.numOperands;a++){let c=i.getOperand(a).definingOp;if(!c||r.has(c))continue;if(e.has(c))return!0;this._topoIndex.get(c)<s||(r.add(c),o.push(c))}}return!1}_dependsOnGroup(t,e){return this._dependsOnOps(t,e.opSet,e.minTopoPos)}_mergeWouldCreateCycle(t,e){let s=Math.min(t.minTopoPos,e.minTopoPos),r=Math.max(t.maxTopoPos,e.maxTopoPos),o=new Set([...t.opSet,...e.opSet]);for(let i of o)for(let a=0;a<i.numOperands;a++){let c=i.getOperand(a).definingOp;if(!c||o.has(c))continue;let l=this._topoIndex.get(c);if(!(l<s||l>r)&&this._dependsOnOps(c,o,s))return!0}return!1}_bucketable(t,e){return e&&!e.isConstant&&!e.isTerminator&&!e.isOpaque&&t.numResults>0&&bM(t)!==null}buildHorizontalGroups(t){let e=[...t.ops()];this._topoIndex=new Map;for(let c=0;c<e.length;c++)this._topoIndex.set(e[c],c);let s=[],r=new Map,o=this.legality.maxFusionSize||e.length,i=new Map,a=0;for(let c=0;c<e.length;c++){let l=e[c];if(r.has(l))continue;let u=st.get(l.opName);if(!this._bucketable(l,u))continue;let h=l.getResult(0).type;a++,i.set(l,a);let p=new Oa(this._nextId++);p.addOp(l,c),r.set(l,p);let d=Math.min(e.length,c+1+o);for(let m=c+1;m<d;m++){let g=e[m],_=!1;for(let b=0;b<g.numOperands;b++){let x=g.getOperand(b).definingOp;if(x&&i.get(x)===a){_=!0;break}}if(_){i.set(g,a);continue}if(p.size>=o||r.has(g))continue;let y=st.get(g.opName);this._bucketable(g,y)&&h.equals(g.getResult(0).type)&&(this._sharesInput(l,g)||u.isElementwise&&y.isElementwise)&&(p.addOp(g,m),r.set(g,p),i.set(g,a))}p.size>=2&&(p.kind=Et.HORIZONTAL,s.push(p))}return s}buildAllGroups(t){let e=this.buildProducerConsumerGroups(t),s=this.buildHorizontalGroups(t),r=new Map;for(let c of e)for(let l of c.ops)r.set(l,c);let o=new Set(r.keys()),i=[];for(let c of s){let l=!1;for(let u of c.ops)if(o.has(u)){l=!0;break}l||i.push(c)}for(let c of i)for(let l of c.ops)r.set(l,c);if(!this._condensedHasCycle(t,r))return[...e,...i];for(let c of i)for(let l of c.ops)r.delete(l);let a=[...e];for(let c of i){for(let l of c.ops)r.set(l,c);if(this._condensedHasCycle(t,r)){for(let l of c.ops)r.delete(l);continue}a.push(c)}return a}_condensedHasCycle(t,e){let s=f(u=>e.get(u)||u,"repOf"),r=new Map,o=new Set;for(let u of t.ops()){let h=s(u);o.add(h);for(let p=0;p<u.numOperands;p++){let d=u.getOperand(p).definingOp;if(!d)continue;let m=s(d);if(m===h)continue;o.add(m);let g=r.get(m);g||(g=new Set,r.set(m,g)),g.add(h)}}let i=0,a=1,c=2,l=new Map;for(let u of o)l.set(u,i);for(let u of o){if(l.get(u)!==i)continue;let h=[u];for(;h.length>0;){let p=h[h.length-1],d=l.get(p);if(d===i){l.set(p,a);let m=r.get(p);if(m)for(let g of m){let _=l.get(g);if(_===a)return!0;_===i&&h.push(g)}}else d===a&&l.set(p,c),h.pop()}}return!1}_sharesInput(t,e){if(t.numOperands===0||e.numOperands===0)return!1;let s=new Set;for(let r=0;r<t.numOperands;r++)s.add(t.getOperand(r));for(let r=0;r<e.numOperands;r++)if(s.has(e.getOperand(r)))return!0;return!1}_hasDependency(t,e){for(let s=0;s<t.numResults;s++){let r=t.getResult(s);for(let o=0;o<e.numOperands;o++)if(e.getOperand(o)===r)return!0}return!1}}});var Na,D0=T(()=>{kt();Ee();Na=class{static{f(this,"FusionCostModel")}constructor(t={}){this.memoryBandwidthGBs=t.memoryBandwidthGBs||900,this.computeTFLOPs=t.computeTFLOPs||15,this.launchOverheadUs=t.launchOverheadUs||5,this.minBenefitRatio=t.minBenefitRatio||1.05,this.maxRegistersPerThread=t.maxRegistersPerThread||255,this.maxSharedMemory=t.maxSharedMemory||49152,this.maxCodeSizeOps=t.maxCodeSizeOps||256,this.libraryOps=t.libraryOps||new Set,this.registerBytesPerOp=t.registerBytesPerOp||8,this.policy=t.policy||null}estimateOpCost(t){let e=this.estimateFLOPs(t),s=this.estimateBytes(t);return{flops:e,bytes:s,arithmeticIntensity:s>0?e/s:0}}estimateFLOPs(t){let e=st.get(t.opName);if(e&&e.getFlops)return e.getFlops(t);let s=1;for(let r=0;r<t.numResults;r++){let o=t.getResult(r).type;if(o instanceof E){let i=o.numel();i!==Z&&(s=i);break}}if(e&&e.isReduction&&t.numOperands>0){let r=t.getOperand(0).type;if(r instanceof E){let o=r.numel();if(o!==Z)return o}}return s}estimateBytes(t){let e=0;for(let s=0;s<t.numOperands;s++){let r=t.getOperand(s).type;if(r instanceof E){let o=r.sizeInBytes();o!==Z&&(e+=o)}}for(let s=0;s<t.numResults;s++){let r=t.getResult(s).type;if(r instanceof E){let o=r.sizeInBytes();o!==Z&&(e+=o)}}return e}estimateGroupCost(t){let e=t.opSet,s=new Map,r=0,o=0,i=0,a=0,c=!1,l=!1,u=0,h=0,p=0,d=0,m=new Set;for(let S of t.ops){let v=this.estimateFLOPs(S);s.set(S,v),r+=v,o+=this.estimateBytes(S),this.libraryOps.has(S.opName)&&a++;let w=st.get(S.opName);if(w){let A=this._outputElements(S);w.isReduction&&(c=!0,u+=A),w.isElementwise&&(l=!0,h+=A)}for(let A=0;A<S.numResults;A++){let I=S.getResult(A);m.add(I);let B=0,k=!1;for(let N of I.uses())e.has(N.user)?B++:k=!0;B>1&&(i+=(B-1)*v),(B>0||k)&&p++}for(let A=0;A<S.numOperands;A++){let I=S.getOperand(A);if(!m.has(I))continue;let B=!1;for(let k of I.uses())if(e.has(k.user)&&k.user!==S){B=!0;break}B||p--}p>d&&(d=p)}let g=t.getInputValues(),_=t.getOutputValues(),y=0;for(let S of g)if(S.type instanceof E){let v=S.type.sizeInBytes();v!==Z&&(y+=v)}for(let S of _)if(S.type instanceof E){let v=S.type.sizeInBytes();v!==Z&&(y+=v)}let b=0;for(let S of t.ops)for(let v=0;v<S.numResults;v++){let w=S.getResult(v),A=0;for(let I of w.uses())if(e.has(I.user)&&(A++,A>1))break;if(A>1){let I=w.type;if(I instanceof E){let B=I.sizeInBytes();B!==Z&&(b+=B)}}}let x=0;return c&&l&&h>0&&u>0&&(x=Math.abs(h-u)),{unfusedFLOPs:r,unfusedBytes:o,fusedFLOPs:r+i,fusedBytes:y,recomputeCost:i,memorySaved:o-y,launchSaved:(t.size-1)*this.launchOverheadUs,registerPressure:d*this.registerBytesPerOp,sharedMemoryUsage:b,parallelismLoss:x,libraryCallLoss:a}}shouldFuse(t){if(t.size<2)return{fuse:!1,reason:"group too small"};if(this.policy&&typeof this.policy.shouldFuse=="function"){let s=this.policy.shouldFuse(t,this);if(s)return s}let e=this.estimateGroupCost(t);if(e.libraryCallLoss>0)return{fuse:!1,reason:"fusion would lose library call opportunity",cost:e};if(e.registerPressure>this.maxRegistersPerThread)return{fuse:!1,reason:`register pressure ${e.registerPressure} exceeds limit ${this.maxRegistersPerThread}`,cost:e};if(e.sharedMemoryUsage>this.maxSharedMemory)return{fuse:!1,reason:`shared memory ${e.sharedMemoryUsage} exceeds limit ${this.maxSharedMemory}`,cost:e};if(t.size>this.maxCodeSizeOps)return{fuse:!1,reason:`code size ${t.size} exceeds limit ${this.maxCodeSizeOps}`,cost:e};if(e.memorySaved<=0&&e.launchSaved<=0)return{fuse:!1,reason:"no memory or launch benefit",cost:e};if(e.parallelismLoss>0){let s=e.memorySaved+e.launchSaved*1e3;if(e.parallelismLoss>s*this.minBenefitRatio)return{fuse:!1,reason:"parallelism loss outweighs fusion benefit",cost:e}}return{fuse:!0,reason:`saves ${e.memorySaved} bytes, ${e.launchSaved}us launch`,cost:e}}_outputElements(t){for(let e=0;e<t.numResults;e++){let s=t.getResult(e).type;if(s instanceof E){let r=s.numel();if(r!==Z)return r}}return 0}}});function Ea(n){let t=null;for(let e of n.ops())t=e;return t&&t.opName==="yield"?t:null}function Ta(n){let t=0,e=n.regions[0]?.entryBlock;if(!e)return 0;for(let s of e.ops())s.opName!=="yield"&&t++;return t}function Ia(n){let t=0,e=n.regions[0]?.entryBlock;if(!e)return 0;for(let s of e.ops()){let r=st.get(s.opName);r&&r.isReduction&&t++}return t}function Ma(n){let t=n.regions[0]?.entryBlock;if(!t)return!1;for(let e of t.ops())if(e.opName!=="yield"&&!Bo(e.opName))return!1;return!0}function wM(n){let t=new Map,e=0;for(let s=n.firstOp;s;s=s._next)t.set(s,e++);return t}function Ca(n){let t=wM(n);return(e,s)=>{let r=t.get(e),o=t.get(s);return r!==void 0&&o!==void 0&&r<o}}function Da(n,t){let e=new Array(n.numOperands);for(let s=0;s<n.numOperands;s++){let r=n.getOperand(s);e[s]=t.get(r)||r}return e}var al=T(()=>{Ee();Li();f(Ea,"getYieldOp");f(Ta,"countInnerOps");f(Ia,"countReductions");f(Ma,"allInnerOpsFusable");f(wM,"blockPositionIndex");f(Ca,"makeComesBefore");f(Da,"remapOperands")});var hd,dA=T(()=>{Ce();Rs();Gr();Ra();C0();D0();al();Ue();hd=class extends Rt{static{f(this,"FusionPass")}constructor(t={}){super("FusionPass");let e=t.target||{};this.legality=new fd({maxFusionSize:e.maxFusionSize||t.maxFusionSize,maxSharedMemory:e.sharedMemoryBytes||t.maxSharedMemory,libraryOps:e.libraryOps||t.libraryOps,allowReductionFusion:t.allowReductionFusion}),this.costModel=new Na({memoryBandwidthGBs:e.memoryBandwidthGBs,computeTFLOPs:e.computeTFLOPs,maxRegistersPerThread:e.registersPerThread,maxSharedMemory:e.sharedMemoryBytes,libraryOps:e.libraryOps,policy:e.getAttr?e.getAttr("fusionPolicy"):null,...t.cost}),this.groupBuilder=new pd(this.legality)}run(t,e){let s=this.groupBuilder.buildAllGroups(t),r=[];for(let i of s)this._createsCycle(t,i)?this._traceDecision(i,!1,"fusing would create a dependency cycle"):r.push(i);let o=[];for(let i of r){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 Q.UNCHANGED;for(let i of o)this._materializeFusion(t,i);return Q.CHANGED}_traceDecision(t,e,s){if(!this.trace||this.trace.level<ot.DEBUG)return;let r=t.ops.map(o=>o.opName);this.trace.emit({type:"fusion_decision",passName:this.name,groupSize:t.ops.length,ops:r,anchor:r[r.length-1]||null,fuse:e,reason:s||null,level:ot.DEBUG}),this.trace.explain("fusion",r.join("+"),e?"fused":"not-fused",s||null,{groupSize:r.length})}_createsCycle(t,e){let s=e.opSet,r=e.getOutputValues(),o=new Set,i=[];for(let c of r)for(let l of c.uses())s.has(l.user)||(i.push(l.user),o.add(l.user));let a=0;for(;a<i.length;){let c=i[a++];if(s.has(c))return!0;for(let l=0;l<c.numResults;l++)for(let u of c.getResult(l).uses())o.has(u.user)||(o.add(u.user),i.push(u.user))}return!1}_materializeFusion(t,e){let s=this._topologicalSort(e);if(s===null||s.length===0)return;e._inputValues=null,e._outputValues=null;let r=e.getInputValues(),o=e.getOutputValues(),i=r.map(y=>y.type),a=o.map(y=>y.type),c=new ee,l=new Ne(i);c.addBlock(l);let u=new Map;for(let y=0;y<r.length;y++)u.set(r[y],l.arguments[y]);for(let y of s){let b=[];for(let w=0;w<y.numOperands;w++){let A=y.getOperand(w),I=u.get(A);b.push(I!==void 0?I:A)}let x=y.results.map(w=>w.type),S=y.regions.length>0?y.regions.map(w=>Fn(w)):null,v=new mt(y.opName,b,x,new Map(y.attributes),S);l.pushOp(v);for(let w=0;w<y.numResults;w++)u.set(y.getResult(w),v.getResult(w))}let h=o.map(y=>{let b=u.get(y);if(b===void 0)throw new Error("Fusion materialization: output value not found in valueMap");return b}),p=new mt("yield",h,[]);l.pushOp(p);let d=Ca(s[0].parentBlock),m=null;for(let y of r){let b=y.definingOp;!b||e.hasOp(b)||(!m||!d(b,m))&&(m=b)}let g=new mt("fusion",r,a,{fusion_kind:e.kind||Et.ELEMENTWISE},[c]),_=s[0].parentBlock;if(_){m?_.insertAfter(g,m):_.insertBefore(g,s[0]);for(let y=0;y<o.length;y++)o[y].replaceAllUsesWith(g.getResult(y));for(let y of s)y.dropAllOperands(),y.parentBlock&&y.parentBlock.removeOp(y)}}_topologicalSort(t){let e=[],s=new Set,r=new Set,o=f(i=>{if(s.has(i))return!0;if(r.has(i))return!1;r.add(i);for(let a=0;a<i.numOperands;a++){let c=i.getOperand(a).definingOp;if(c&&t.hasOp(c)&&!o(c))return!1}return r.delete(i),s.add(i),e.push(i),!0},"visit");for(let i of t.ops)if(!o(i))return null;return e}}});function mA(n){if(L0.has(n.opName))return!0;let t=st.get(n.opName);return t!==null&&t.isElementwise}function SM(n,t){let e=n.getOperand(0).definingOp;return e&&t.has(e)&&!L0.has(e.opName)?n.getOperand(1).definingOp:e}function vM(n,t){if(!n.parentBlock||n.parentBlock!==t.parentBlock)return!1;let e=n.parentBlock.firstOp;for(;e;){if(e===n)return!0;if(e===t)return!1;e=e._next}return!1}function BM(n,t){for(let e of n)if(e!==t){for(let s=0;s<e.numResults;s++)for(let r of e.getResult(s).uses())if(!n.has(r.user))return!0}return!1}function kM(n,t){let e=st.get(n.opName),s=e&&e.getAttr("epilogueTag");return s?s(n,t):"activation"}function AM(n){let t=[],e=new Set,s=new Set;function r(p){if(!(s.has(p)||p===n)){s.add(p);for(let d=0;d<p.numOperands;d++){let m=p.getOperand(d).definingOp;m&&m!==n&&!s.has(m)&&L0.has(m.opName)&&r(m)}t.push(p),e.add(p)}}f(r,"absorb");let o=[],i=n.getResult(0);for(let p of i.uses())mA(p.user)&&o.push(p.user);for(;o.length>0;){let p=o.pop();if(!s.has(p)){r(p);for(let d=0;d<p.numResults;d++)for(let m of p.getResult(d).uses())!s.has(m.user)&&mA(m.user)&&o.push(m.user)}}let a=[];for(let p of t)_A.has(p.opName)||gA.has(p.opName)||a.push(kM(p,e));let c=t[t.length-1];for(let p=t.length-1;p>=0;p--){let d=t[p];for(let m=0;m<d.numResults;m++){for(let g of d.getResult(m).uses())if(!e.has(g.user)){c=d,p=-1;break}if(p<0)break}}let l=new Set;for(let p=0;p<n.numOperands;p++)l.add(n.getOperand(p));let u=[],h=new Set;for(let p of t)for(let d=0;d<p.numOperands;d++){let m=p.getOperand(d);if(h.has(m))continue;h.add(m);let g=m.definingOp;g!==n&&(g&&e.has(g)||l.has(m)||u.push(m))}return{chain:t,chainSet:e,tags:a,lastOp:c,extras:u}}var gA,_A,L0,xM,dd,yA=T(()=>{Ce();Rs();Ee();Ue();gA=new Set(["constant","scalar_constant"]),_A=new Set(["broadcast_in_dim","broadcast"]),L0=new Set([...gA,..._A]);f(mA,"isEpilogueCandidate");xM=new Map([["add",(n,t)=>t.has(SM(n,t))?"residual_add":"bias"],["sub",()=>"bias"],["mul",()=>"scale"],["maximum",()=>"relu"],["clamp",()=>"clamp"],["neg",()=>"neg"],["exp",()=>"exp"],["tanh",()=>"tanh"],["sqrt",()=>"sqrt"],["abs",()=>"abs"],["log",()=>"log"]]);f(SM,"resolveOtherOperand");for(let[n,t]of xM)st.has(n)&&st.registerOpAttr(n,"epilogueTag",t);f(vM,"comesBefore");f(BM,"hasEscapingUse");f(kM,"classifyTag");f(AM,"collectChainAndAnalyze");dd=class extends Rt{static{f(this,"EpilogueFusionPass")}constructor(t={}){super("EpilogueFusionPass"),this.maxEpilogueOps=t.maxEpilogueOps||16,this.target=t.target||null}run(t){if(this.target&&!this.target.enableEpilogueFusion)return Q.UNCHANGED;let e=!1,s=[];for(let r of t.ops()){let o=st.get(r.opName);o&&o.isOutEWiseFusable&&s.push(r)}for(let r of s){let o=AM(r);if(o.chain.length===0||o.chain.length>this.maxEpilogueOps)continue;let{chain:i,tags:a,lastOp:c,extras:l}=o,u=0;for(let b of a)b==="bias"||b==="residual_add"||b==="scale"?u++:b==="clamp"&&(u+=2);if(u!==l.length)continue;let h=[r.getOperand(0),r.getOperand(1),...l],p=c.getResult(0).type,d=new Map(r.attributes);d.set("epilogue_ops",i.map(b=>b.opName)),d.set("epilogue_tags",a),d.set("num_dot_operands",2),d.set("num_extra_inputs",l.length);let m=new mt("fused_dot_epilogue",h,[p],d),g=r.parentBlock;if(!g)continue;let _=new Set(i);if(_.add(r),BM(_,c))continue;let y=null;for(let b of h){let x=b.definingOp;!x||_.has(x)||(!y||!vM(x,y))&&(y=x)}y&&y.parentBlock===g?g.insertAfter(m,y):g.insertBefore(m,r),c.getResult(0).replaceAllUsesWith(m.getResult(0));for(let b=i.length-1;b>=0;b--)i[b].dropAllOperands(),i[b].parentBlock&&i[b].parentBlock.removeOp(i[b]);r.dropAllOperands(),r.parentBlock&&r.parentBlock.removeOp(r),e=!0}return this.trace&&this.trace.level>=ot.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,dotsFound:s.length,changed:e,level:ot.DEBUG}),e?Q.CHANGED:Q.UNCHANGED}}});var md,bA=T(()=>{Ce();Rs();Gr();kt();Ee();Ue();Ra();al();md=class extends Rt{static{f(this,"FusionMergerPass")}constructor(t={}){super("FusionMergerPass"),this.maxFusionSize=t.maxFusionSize||512,this.maxReductions=t.maxReductions||1,this.launchOverheadUs=t.launchOverheadUs||5,this.minMemorySavings=t.minMemorySavings||0}run(t){let e=[];for(let c of t.ops())c.opName==="fusion"&&e.push(c);if(e.length<2)return Q.UNCHANGED;let s=new Set(e),r=this._buildProducerConsumerEdges(e,s);if(r.length===0)return Q.UNCHANGED;let o=!1,i=0,a=new Set;for(let{producer:c,consumer:l,sharedResults:u}of r)a.has(c)||a.has(l)||this._canMerge(c,l)&&this._shouldMerge(c,l,u)&&(this._mergeCreatesCycle(c,l,s)||(this._merge(c,l,u),a.add(c),a.add(l),o=!0,i++));return this.trace&&this.trace.level>=ot.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:e.length,edges:r.length,mergeCount:i,level:ot.DEBUG}),o?Q.CHANGED:Q.UNCHANGED}_buildProducerConsumerEdges(t,e){let s=[],r=new Map;for(let o of t)for(let i=0;i<o.numResults;i++){let a=o.getResult(i);for(let c of a.uses()){if(!e.has(c.user))continue;let l=c.user;if(l===o)continue;let u=`${o.id}|${l.id}`,h=r.get(u);h||(h={producer:o,consumer:l,sharedResults:new Map},r.set(u,h),s.push(h)),h.sharedResults.set(i,c.operandIndex)}}return s}_canMerge(t,e){let s=Ta(t),r=Ta(e);if(s+r>this.maxFusionSize)return!1;let o=0;return o+=Ia(t),o+=Ia(e),!(o>this.maxReductions||!Ma(t)||!Ma(e))}_shouldMerge(t,e,s){let r=0;for(let[c]of s){let l=t.getResult(c);if(l.type instanceof E){let u=l.type.sizeInBytes();u!==Z&&(r+=u)}}let o=!0;for(let c=0;c<t.numResults;c++){let l=t.getResult(c);for(let u of l.uses())if(u.user!==e){o=!1;break}if(!o)break}if(o)return!0;let i=this._estimateRecomputeCost(t);return r+this.launchOverheadUs*1e3>i}_mergeCreatesCycle(t,e,s){let r=new Set,o=[];for(let a=0;a<e.numOperands;a++){let l=e.getOperand(a).definingOp;l&&l!==t&&l.opName!=="constant"&&(r.has(l)||(r.add(l),o.push(l)))}let i=0;for(;i<o.length;){let a=o[i++];if(a===t)return!0;for(let c=0;c<a.numOperands;c++){let l=a.getOperand(c).definingOp;l&&!r.has(l)&&(r.add(l),o.push(l))}}return!1}_merge(t,e,s){let r=t.regions[0].entryBlock,o=e.regions[0].entryBlock,i=Ea(r),a=Ea(o);if(!i||!a)return;let c=new Map;for(let[B,k]of s)c.set(k,B);let l=[],u=new Map;for(let B=0;B<t.numOperands;B++)u.set(B,l.length),l.push(t.getOperand(B));let h=new Map;for(let B=0;B<e.numOperands;B++){if(c.has(B))continue;let k=e.getOperand(B),N=-1;for(let R=0;R<l.length;R++)if(l[R]===k){N=R;break}N>=0?h.set(B,N):(h.set(B,l.length),l.push(k))}let p=l.map(B=>B.type),d=new ee,m=new Ne(p);d.addBlock(m);let g=new Map;for(let B=0;B<r.arguments.length;B++)g.set(r.arguments[B],m.arguments[u.get(B)]);for(let B of r.ops()){if(B.opName==="yield")continue;let k=Da(B,g),N=B.results.map(M=>M.type),R=B.regions.length>0?B.regions.map(M=>Fn(M)):null,O=new mt(B.opName,k,N,new Map(B.attributes),R);m.pushOp(O);for(let M=0;M<B.numResults;M++)g.set(B.getResult(M),O.getResult(M))}for(let B=0;B<i.numOperands;B++){let k=i.getOperand(B),N=g.get(k)||k;for(let[R,O]of c)O===B&&g.set(o.arguments[R],N)}for(let B=0;B<o.arguments.length;B++){if(c.has(B))continue;let k=h.get(B);k!==void 0&&g.set(o.arguments[B],m.arguments[k])}for(let B of o.ops()){if(B.opName==="yield")continue;let k=Da(B,g),N=B.results.map(M=>M.type),R=B.regions.length>0?B.regions.map(M=>Fn(M)):null,O=new mt(B.opName,k,N,new Map(B.attributes),R);m.pushOp(O);for(let M=0;M<B.numResults;M++)g.set(B.getResult(M),O.getResult(M))}let _=[],y=[],b=[];for(let B=0;B<t.numResults;B++){let k=!1;for(let N of t.getResult(B).uses())if(N.user!==e){k=!0;break}if(k){let N=i.getOperand(B),R=g.get(N)||N;b.push({resultIdx:B,value:R}),_.push(R),y.push(t.getResult(B).type)}}for(let B=0;B<e.numResults;B++){let k=a.getOperand(B),N=g.get(k)||k;_.push(N),y.push(e.getResult(B).type)}let x=new mt("yield",_,[]);m.pushOp(x);let S=[];for(let B of m.ops())B.opName!=="yield"&&S.push(B);let v=Aa(S),w=new mt("fusion",l,y,{fusion_kind:v},[d]),A=e;if(!A.parentBlock)return;A.parentBlock.insertBefore(w,A);let I=0;for(let{resultIdx:B}of b)t.getResult(B).replaceAllUsesWith(w.getResult(I)),I++;for(let B=0;B<e.numResults;B++)e.getResult(B).replaceAllUsesWith(w.getResult(I)),I++;t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t),e.dropAllOperands(),e.parentBlock&&e.parentBlock.removeOp(e)}_estimateRecomputeCost(t){let e=0,s=t.regions[0]?.entryBlock;if(!s)return 0;for(let r of s.ops()){if(r.opName==="yield")continue;let o=st.get(r.opName);if(o&&o.getFlops)e+=o.getFlops(r);else for(let i=0;i<r.numResults;i++){let a=r.getResult(i).type;if(a instanceof E){let c=a.numel();c!==Z&&(e+=c);break}}}return e}}});function RM(n,t){let e=Math.min(n.id,t.id),s=Math.max(n.id,t.id);return`${e}|${s}`}var gd,wA=T(()=>{Ce();Rs();Gr();kt();Ra();Ue();al();gd=class extends Rt{static{f(this,"MultiOutputFusionPass")}constructor(t={}){super("MultiOutputFusionPass"),this.maxFusionSize=t.maxFusionSize||512,this.maxReductions=t.maxReductions||1,this.minSharedInputBytes=t.minSharedInputBytes||0,this.maxOutputs=t.maxOutputs||8}run(t){let e=[];for(let a of t.ops())a.opName==="fusion"&&e.push(a);if(e.length<2)return Q.UNCHANGED;let s=this._findCandidates(e);if(s.length===0)return Q.UNCHANGED;let r=!1,o=0,i=new Set;for(let{left:a,right:c,sharedInputs:l,sharedBytes:u}of s)i.has(a)||i.has(c)||this._canMerge(a,c)&&(this._mergeCreatesCycle(a,c)||(this._mergeMultiOutput(a,c,l),i.add(a),i.add(c),r=!0,o++));return this.trace&&this.trace.level>=ot.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:e.length,candidates:s.length,mergeCount:o,level:ot.DEBUG}),r?Q.CHANGED:Q.UNCHANGED}_findCandidates(t){let e=new Map;for(let o of t)for(let i=0;i<o.numOperands;i++){let c=o.getOperand(i).id,l=e.get(c);l||(l=[],e.set(c,l)),l.push(o)}let s=new Map;for(let[o,i]of e)if(!(i.length<2))for(let a=0;a<i.length;a++)for(let c=a+1;c<i.length;c++){let l=i[a],u=i[c];if(l===u)continue;let h=RM(l,u),p=s.get(h);p||(p={left:l,right:u,sharedInputs:new Set,sharedBytes:0},s.set(h,p)),p.sharedInputs.add(o)}let r=[];for(let o of s.values()){let i=0;for(let a of o.sharedInputs){let c=e.get(a);if(!c||c.length===0)continue;let l=c[0];for(let u=0;u<l.numOperands;u++){let h=l.getOperand(u);if(h.id===a&&h.type instanceof E){let p=h.type.sizeInBytes();p!==Z&&(i+=p);break}}}o.sharedBytes=i,i>=this.minSharedInputBytes&&r.push(o)}return r.sort((o,i)=>i.sharedBytes-o.sharedBytes),r}_canMerge(t,e){if(this._hasProducerConsumerEdge(t,e)||this._hasProducerConsumerEdge(e,t))return!1;let s=Ta(t),r=Ta(e);if(s+r>this.maxFusionSize||t.numResults+e.numResults>this.maxOutputs)return!1;let i=0;return i+=Ia(t),i+=Ia(e),!(i>this.maxReductions||!Ma(t)||!Ma(e))}_mergeCreatesCycle(t,e){let s=new Set,r=[];for(let i=0;i<t.numResults;i++){let a=t.getResult(i);for(let c of a.uses())c.user!==e&&!s.has(c.user)&&(s.add(c.user),r.push(c.user))}let o=0;for(;o<r.length;){let i=r[o++];if(i===e)return!0;for(let a=0;a<i.numResults;a++)for(let c of i.getResult(a).uses())s.has(c.user)||(s.add(c.user),r.push(c.user))}s.clear(),r.length=0;for(let i=0;i<e.numResults;i++){let a=e.getResult(i);for(let c of a.uses())c.user!==t&&!s.has(c.user)&&(s.add(c.user),r.push(c.user))}for(o=0;o<r.length;){let i=r[o++];if(i===t)return!0;for(let a=0;a<i.numResults;a++)for(let c of i.getResult(a).uses())s.has(c.user)||(s.add(c.user),r.push(c.user))}return!1}_mergeMultiOutput(t,e,s){let r=t.regions[0].entryBlock,o=e.regions[0].entryBlock,i=Ea(r),a=Ea(o);if(!i||!a)return;let c=[],l=new Map,u=new Map;for(let k=0;k<t.numOperands;k++){let N=t.getOperand(k),R=N.id,O=l.get(R);O===void 0&&(O=c.length,l.set(R,O),c.push(N)),u.set(k,O)}let h=new Map;for(let k=0;k<e.numOperands;k++){let N=e.getOperand(k),R=N.id,O=l.get(R);O===void 0&&(O=c.length,l.set(R,O),c.push(N)),h.set(k,O)}let p=c.map(k=>k.type),d=new ee,m=new Ne(p);d.addBlock(m);let g=new Map;for(let k=0;k<r.arguments.length;k++)g.set(r.arguments[k],m.arguments[u.get(k)]);for(let k of r.ops()){if(k.opName==="yield")continue;let N=Da(k,g),R=k.results.map(M=>M.type),O=new mt(k.opName,N,R,new Map(k.attributes));m.pushOp(O);for(let M=0;M<k.numResults;M++)g.set(k.getResult(M),O.getResult(M))}for(let k=0;k<o.arguments.length;k++)g.set(o.arguments[k],m.arguments[h.get(k)]);for(let k of o.ops()){if(k.opName==="yield")continue;let N=Da(k,g),R=k.results.map(M=>M.type),O=new mt(k.opName,N,R,new Map(k.attributes));m.pushOp(O);for(let M=0;M<k.numResults;M++)g.set(k.getResult(M),O.getResult(M))}let _=[],y=[];for(let k=0;k<i.numOperands;k++){let N=i.getOperand(k);_.push(g.get(N)||N),y.push(t.getResult(k).type)}for(let k=0;k<a.numOperands;k++){let N=a.getOperand(k);_.push(g.get(N)||N),y.push(e.getResult(k).type)}let b=new mt("yield",_,[]);m.pushOp(b);let x=[];for(let k of m.ops())k.opName!=="yield"&&x.push(k);let S=Aa(x),v=new mt("fusion",c,y,{fusion_kind:S},[d]),w=t.parentBlock;if(!w)return;let A=Ca(w),I=null;for(let k of c){let N=k.definingOp;!N||N===t||N===e||(!I||!A(N,I))&&(I=N)}I&&I.parentBlock===w?w.insertAfter(v,I):w.insertBefore(v,t);for(let k=0;k<t.numResults;k++)t.getResult(k).replaceAllUsesWith(v.getResult(k));let B=t.numResults;for(let k=0;k<e.numResults;k++)e.getResult(k).replaceAllUsesWith(v.getResult(B+k));t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t),e.dropAllOperands(),e.parentBlock&&e.parentBlock.removeOp(e)}_hasProducerConsumerEdge(t,e){for(let s=0;s<t.numResults;s++)for(let r of t.getResult(s).uses())if(r.user===e)return!0;return!1}};f(RM,"pairKey")});var P0,qt,ur=T(()=>{P0=class{static{f(this,"UseDefResult")}constructor(t,e,s,r,o){this.topologicalOrder=t,this.valueToOp=e,this.opUsers=s,this.depth=r,this.height=o}},qt=class{static get name(){return"use_def"}static get depKey(){return"useDef"}static get dependencies(){return[]}static compute(t){let e=[],s=new Map,r=new Map,o=new Set,i=new Set,a=f(u=>{if(o.has(u))return;i.add(u);let h=[{op:u,i:0}];for(;h.length>0;){let p=h[h.length-1],d=p.op;if(p.i<d.numOperands){let m=d.getOperand(p.i).definingOp;if(p.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),e.push(d),h.pop()}},"visit");for(let u of t.ops()){for(let h=0;h<u.numResults;h++)s.set(u.getResult(h),u);r.set(u,new Set)}let c=new Map,l=new Map;for(let u of t.ops())a(u);for(let u of e){let h=0;for(let p=0;p<u.numOperands;p++){let d=u.getOperand(p).definingOp;if(d){r.get(d).add(u);let m=c.get(d)||0;m+1>h&&(h=m+1)}}c.set(u,h)}for(let u=e.length-1;u>=0;u--){let h=e[u],p=0,d=r.get(h)||new Set;for(let m of d){let g=l.get(m)||0;g+1>p&&(p=g+1)}l.set(h,p)}return new P0(e,s,r,c,l)}}});var z0,_d,xA=T(()=>{ur();z0=class{static{f(this,"DominanceResult")}constructor(t){this.idom=t}postDominates(t,e){let s=e;for(;s;){if(s===t)return!0;s=this.idom.get(s)}return!1}immediatePDom(t){return this.idom.get(t)||null}pathToPDom(t){let e=[],s=this.idom.get(t);for(;s&&(e.push(s),s!==this.idom.get(s));)s=this.idom.get(s);return e}},_d=class{static get name(){return"post_dominance"}static get depKey(){return"postDominance"}static get dependencies(){return[qt]}static compute(t,e={}){let r=(e.useDef||qt.compute(t)).topologicalOrder,o=new Map;for(let m of r)o.set(m,[]);for(let m of r)for(let g=0;g<m.numResults;g++){let _=m.getResult(g);for(let y of _.uses()){let b=y.user;o.has(b)&&o.get(m).push(b)}}let i=new Set;for(let m of r)(m.opName==="return"||m.opName==="yield")&&i.add(m);let a={opName:"__pdom_root__"},c=Math.max(1,Math.ceil(Math.log2(r.length+2))+1),l=new Map,u=new Map,h=new Map;u.set(a,0),h.set(a,new Array(c).fill(a));let p=f((m,g)=>{l.set(m,g),u.set(m,u.get(g)+1);let _=new Array(c);_[0]=g;for(let y=1;y<c;y++)_[y]=h.get(_[y-1])[y-1];h.set(m,_)},"link"),d=f((m,g)=>{if(u.get(m)<u.get(g)){let y=m;m=g,g=y}let _=u.get(m)-u.get(g);for(let y=0;y<c;y++)_>>y&1&&(m=h.get(m)[y]);if(m===g)return m;for(let y=c-1;y>=0;y--)h.get(m)[y]!==h.get(g)[y]&&(m=h.get(m)[y],g=h.get(g)[y]);return h.get(m)[0]},"lca");for(let m=r.length-1;m>=0;m--){let g=r[m];if(i.has(g)){p(g,a);continue}let _=null;for(let y of o.get(g))l.has(y)&&(_=_===null?y:d(_,y));_!==null&&p(g,_)}for(let[m,g]of l)g===a&&l.delete(m);return new z0(l,r)}}});var SA,yd,vA=T(()=>{Ce();Rs();Gr();Ee();Ra();C0();D0();xA();Ue();ur();al();SA=new Set(["return","yield","constant","scalar_constant"]),yd=class extends Rt{static{f(this,"DominatorFusionPass")}constructor(t={}){super("DominatorFusionPass"),this.requiredAnalyses=[qt];let e=t.target||{};this.maxFusionSize=e.maxFusionSize||t.maxFusionSize||512,this.maxReductions=t.maxReductions||1,this.libraryOps=e.libraryOps||t.libraryOps||new Set,this.costModel=new Na({memoryBandwidthGBs:e.memoryBandwidthGBs,computeTFLOPs:e.computeTFLOPs,maxRegistersPerThread:e.registersPerThread,maxSharedMemory:e.sharedMemoryBytes,libraryOps:e.libraryOps,...t.cost})}run(t,e){let s=e?e.getAnalysis(qt,t):qt.compute(t),r=_d.compute(t,{useDef:s}),o=s.topologicalOrder,i=this._buildGroups(o,r);if(i.length===0)return Q.UNCHANGED;let a=[];for(let c of i){if(!c.allOpsInlineFusable()){this._explain(c,!1,"group contains ops without inline fusion support");continue}let l=this.costModel.shouldFuse(c);this._explain(c,l.fuse,l.reason),l.fuse&&a.push(c)}if(a.length===0)return Q.UNCHANGED;for(let c of a)this._materialize(t,c);return this.trace&&this.trace.level>=ot.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,groupsBuilt:i.length,groupsFused:a.length,level:ot.DEBUG}),Q.CHANGED}_explain(t,e,s){if(!this.trace||!this.trace.explainsEnabled)return;let r=t.ops.map(o=>o.opName);this.trace.explain("fusion",r.join("+"),e?"fused":"not-fused",s||null,{groupSize:r.length,strategy:"dominator"})}_buildGroups(t,e){let s=new Map,r=[],o=0;for(let l=t.length-1;l>=0;l--){let u=t[l];if(SA.has(u.opName))continue;let h=ai(u);if(h===Et.OPAQUE||this.libraryOps.has(u.opName))continue;let p=e.immediatePDom(u);if(!p||SA.has(p.opName))continue;let d=ai(p);if(d===Et.OPAQUE||this.libraryOps.has(p.opName)||!this._canFusePatterns(h,d)||!this._pathAllFusable(u,p,t,e))continue;let m=s.get(p);if(m){if(m.size+1>this.maxFusionSize||!this._checkReductionLimit(m,u))continue;m.addOp(u),s.set(u,m),this._absorbIntermediates(u,p,m,s,t,e)}else{let g=s.get(u);if(g){if(g.size+1>this.maxFusionSize||!this._checkReductionLimit(g,p))continue;g.addOp(p),s.set(p,g),this._absorbIntermediates(u,p,g,s,t,e)}else{let _=new Oa(o++);if(_.addOp(u),_.addOp(p),!this._checkGroupReductions(_))continue;s.set(u,_),s.set(p,_),r.push(_),this._absorbIntermediates(u,p,_,s,t,e)}}}let i=new Set,a=[];for(let l of r)i.has(l)||l.size<2||(i.add(l),l.classifyKind(),a.push(l));for(let l of s.values())i.has(l)||l.size<2||(i.add(l),l.classifyKind(),a.push(l));let c=[];for(let l of a){if(!this._checkGroupReductions(l)){this._explain(l,!1,`group exceeds the ${this.maxReductions}-reduction limit`);continue}if(this._createsCycle(l)){this._explain(l,!1,"fusing would create a dependency cycle");continue}c.push(l)}return c}_createsCycle(t){let e=new Set;for(let o of t.getInputValues())o.definingOp&&!t.hasOp(o.definingOp)&&e.add(o.definingOp);if(e.size===0)return!1;let s=new Set,r=[];for(let o of t.getOutputValues())for(let i of o.uses())t.hasOp(i.user)||r.push(i.user);for(;r.length>0;){let o=r.pop();if(!s.has(o)){if(s.add(o),e.has(o))return!0;for(let i=0;i<o.numResults;i++)for(let a of o.getResult(i).uses())t.hasOp(a.user)||r.push(a.user)}}return!1}_canFusePatterns(t,e){return M0(t,e)}_pathAllFusable(t,e,s,r){let o=new Set,i=[t];for(o.add(t),o.add(e);i.length>0;){let a=i.pop();for(let c=0;c<a.numResults;c++)for(let l of a.getResult(c).uses()){let u=l.user;if(u===e||o.has(u)||!r.postDominates(e,u))continue;if(o.add(u),ai(u)===Et.OPAQUE||this.libraryOps.has(u.opName))return!1;i.push(u)}}return!0}_absorbIntermediates(t,e,s,r,o,i){let a=new Set,c=[t];for(a.add(t),a.add(e);c.length>0;){let l=c.pop();for(let u=0;u<l.numResults;u++)for(let h of l.getResult(u).uses()){let p=h.user;p===e||a.has(p)||!i.postDominates(e,p)||(a.add(p),ai(p)===Et.OPAQUE||this.libraryOps.has(p.opName))||(s.size<this.maxFusionSize&&this._checkReductionLimit(s,p)&&(s.addOp(p),r.set(p,s)),c.push(p))}}}_checkGroupReductions(t){let e=0;for(let s of t.ops){let r=st.get(s.opName);r&&r.isReduction&&e++}return e<=this.maxReductions}_checkReductionLimit(t,e){let s=st.get(e.opName);if(!s||!s.isReduction)return!0;let r=0;for(let o of t.ops){let i=st.get(o.opName);i&&i.isReduction&&r++}return r<this.maxReductions}_materialize(t,e){let s=this._topoSort(e);if(!s||s.length===0)return;let r=e.getInputValues(),o=e.getOutputValues(),i=r.map(_=>_.type),a=o.map(_=>_.type),c=new ee,l=new Ne(i);c.addBlock(l);let u=new Map;for(let _=0;_<r.length;_++)u.set(r[_],l.arguments[_]);for(let _ of s){let y=[];for(let v=0;v<_.numOperands;v++){let w=_.getOperand(v),A=u.get(w);y.push(A!==void 0?A:w)}let b=_.results.map(v=>v.type),x=_.regions.length>0?_.regions.map(v=>Fn(v)):null,S=new mt(_.opName,y,b,new Map(_.attributes),x);l.pushOp(S);for(let v=0;v<_.numResults;v++)u.set(_.getResult(v),S.getResult(v))}let h=o.map(_=>{let y=u.get(_);if(y===void 0)throw new Error("DominatorFusion: output not found in valueMap");return y});l.pushOp(new mt("yield",h,[]));let p=new mt("fusion",r,a,{fusion_kind:e.kind||Et.ELEMENTWISE},[c]),d=s[0].parentBlock;if(!d)return;let m=Ca(d),g=null;for(let _ of r){let y=_.definingOp;!y||e.hasOp(y)||(!g||!m(y,g))&&(g=y)}g&&g.parentBlock===d?d.insertAfter(p,g):d.insertBefore(p,s[0]);for(let _=0;_<o.length;_++)o[_].replaceAllUsesWith(p.getResult(_));for(let _ of s)_.dropAllOperands(),_.parentBlock&&_.parentBlock.removeOp(_)}_topoSort(t){let e=[],s=new Set,r=new Set,o=f(i=>{if(s.has(i))return!0;if(r.has(i))return!1;r.add(i);for(let a=0;a<i.numOperands;a++){let c=i.getOperand(a).definingOp;if(c&&t.hasOp(c)&&!o(c))return!1}return r.delete(i),s.add(i),e.push(i),!0},"visit");for(let i of t.ops)if(!o(i))return null;return e}}});function OM(n,t){return n===t?!0:!n||!t?!1:typeof n.equals=="function"?n.equals(t):!1}var lo,bd,BA=T(()=>{kt();lo=class{static{f(this,"LayoutPreference")}constructor(t,e,s=0){this.inputs=t,this.outputs=e,this.cost=s}},bd=class{static{f(this,"LayoutPolicy")}constructor(t){this.target=t,this._rules=new Map,this._initDefaultRules()}registerRule(t,e){this._rules.set(t,e)}getPreference(t){let e=this._rules.get(t.opName);return e?e(t,this.target):null}estimateConversionCost(t,e,s){if(!(s instanceof E)||OM(t,e))return 0;let r=s.numel();return r<0?1024:r*2}estimateBenefit(t,e,s){if(!(e instanceof E))return 0;let r=e.numel();if(r<0)return 0;let o=t.opName;if(o==="dot"||o==="conv"||o==="matmul")return r*4*s;if(o==="reduce")return r*2*s;let i=this.target.cacheLineBytes||64;return r*4<=i*4?0:Math.floor(r*.5)}_initDefaultRules(){this._rules.set("conv",(t,e)=>{let r=t.getOperand(0).type?.rank||4;if(e.preferredConvLayout)return new lo([e.preferredConvLayout,null],[e.preferredConvLayout]);if(e.isGPU()&&r===4){let o=new ye([0,2,3,1]);return new lo([o,null],[o])}if(e.isCPU()&&r===4){let o=new ye([0,2,3,1]);return new lo([o,null],[o])}return null}),this._rules.set("dot",(t,e)=>{let s=t.getOperand(0).type,r=t.getOperand(1).type;if(!s||!r)return null;let o=ye.rowMajor(s.rank);if(e.isCPU()&&r.rank===2){let a=ye.columnMajor(r.rank);return new lo([o,a],[o])}let i=ye.rowMajor(r.rank);return new lo([o,i],[o])}),this._rules.set("reduce",(t,e)=>{let s=t.getResult(0).type;return s?new lo([null],[ye.rowMajor(s.rank)]):null})}};f(OM,"layoutEquals")});function kA(n,t){let e=new Map,s=null,r=0;for(let i=0;i<n.numOperands;i++){let a=n.getOperand(i),c=t.get(a);if(!c)continue;let l=c.hash?c.hash():0,u=(e.get(l)||0)+1;e.set(l,u),u>r&&(r=u,s=c)}if(s)return s;let o=n.getResult(0);return o&&o.type instanceof E?ye.rowMajor(o.type.rank):ye.rowMajor(1)}function NM(n,t){return n===t?!0:!n||!t?!1:typeof n.equals=="function"?n.equals(t):!1}function AA(n){if(n instanceof ye)return n;if(n&&typeof n.toLayout=="function")try{return n.toLayout()}catch{}return n&&n.baseOrder&&!n.isBlocked?.()?new ye(n.baseOrder):n&&n.order?new ye(n.order):null}var $0,wd,RA=T(()=>{ur();kt();Ee();we();$0=class{static{f(this,"LayoutAnalysisResult")}constructor(t,e,s){this.assignments=t,this.conversions=e,this.totalCost=s}},wd=class{static get name(){return"layout"}static get depKey(){return"layout"}static get dependencies(){return[qt]}static compute(t,e,s){let r=e.useDef,o=new Map,i=r.topologicalOrder;for(let l of t.args)l.type instanceof E&&o.set(l,l.type.layout||ye.rowMajor(l.type.rank));for(let l=0;l<i.length;l++){let u=i[l];if(u.opName==="return"||u.opName==="yield")continue;let h=s?s.getPreference(u):null;if(h&&h.outputs.length>0){for(let m=0;m<u.numResults;m++){let g=u.getResult(m);if(!(g.type instanceof E))continue;let _=h.outputs[m]||null;_?o.set(g,_):o.set(g,kA(u,o))}continue}let p=st.get(u.opName),d=p&&p.hasTrait(U.ELEMENTWISE);for(let m=0;m<u.numResults;m++){let g=u.getResult(m);g.type instanceof E&&(d?o.set(g,kA(u,o)):o.set(g,ye.rowMajor(g.type.rank)))}}let a=[],c=0;for(let l=0;l<i.length;l++){let u=i[l],h=s?s.getPreference(u):null;if(h)for(let p=0;p<u.numOperands;p++){let d=u.getOperand(p);if(!(d.type instanceof E))continue;let m=o.get(d);if(!m)continue;let g=h.inputs[p];if(!g||NM(m,g))continue;let _=AA(m),y=AA(g);!_||!y||_.equals(y)||(a.push({value:d,consumer:u,operandIdx:p,from:_,to:y}),s&&(c+=s.estimateConversionCost(_,y,d.type)))}}return new $0(o,a,c)}};f(kA,"resolveFromInputs");f(NM,"layoutEquals");f(AA,"toPlainLayout")});function EM(n,t,e){let s=n.id,r=t.hash?t.hash():0,o=e.hash?e.hash():0;return`${s}:${r}:${o}`}var xd,OA=T(()=>{Ce();Rs();kt();BA();RA();ur();Ue();xd=class extends Rt{static{f(this,"LayoutTransformPass")}constructor(t={}){super("LayoutTransformPass"),this.target=t.target||null,this._policy=null}run(t,e){if(!this.target)return Q.UNCHANGED;this._policy||(this._policy=new bd(this.target));let s=e?e.getAnalysis(qt,t):qt.compute(t),r=wd.compute(t,{useDef:s},this._policy);if(r.conversions.length===0)return Q.UNCHANGED;let o=new Map;for(let i=0;i<r.conversions.length;i++){let a=r.conversions[i],{value:c,consumer:l,operandIdx:u,from:h,to:p}=a,d=EM(c,h,p),m=o.get(d);if(!m){let g=this._policy.estimateConversionCost(h,p,c.type),_=c.uses?[...c.uses()].length:1,y=this._policy.estimateBenefit(l,c.type,_);if(g>y)continue;let b=h instanceof ye?h.order:Array.from({length:c.type.rank},(A,I)=>I),x=p instanceof ye?p.order:Array.from({length:c.type.rank},(A,I)=>I),S=new E(c.type.shape,c.type.dtype,p),v=new mt("layout_transform",[c],[S],{src_layout:[...b],dst_layout:[...x]}),w=c.definingOp;w&&w.parentBlock?w.parentBlock.insertAfter(v,w):l.parentBlock&&l.parentBlock.insertBefore(v,l),m=v.getResult(0),o.set(d,m)}l.replaceOperand(u,m)}return this.trace&&this.trace.level>=ot.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,conversions:r.conversions.length,uniqueTransforms:o.size,level:ot.DEBUG}),Q.CHANGED}};f(EM,"valueLayoutKey")});function NA(n,t){if(n===t)return!0;if(typeof n=="number"&&typeof t=="number")return n===t;if(typeof n!=typeof t||n.length!==t.length)return!1;for(let e=0;e<n.length;e++)if(n[e]!==t[e])return!1;return!0}function EA(n){let t=0;for(let e=0;e<n.length;e++)t=(t<<5)-t+n.charCodeAt(e)&2147483647;return t}function MM(n){if(typeof n=="number"){let e=new Float64Array([n]),s=new Uint32Array(e.buffer);return(s[0]^s[1])&2147483647}let t=n.length;for(let e=0;e<Math.min(n.length,8);e++){let s=new Float64Array([n[e]]),r=new Uint32Array(s.buffer);t=(t^(r[0]^r[1]))*16777619&2147483647}return t}var $s,TM,IM,Fs,F0=T(()=>{kt();$s=Object.freeze({PER_TENSOR_SYMMETRIC:"per_tensor_symmetric",PER_TENSOR_ASYMMETRIC:"per_tensor_asymmetric",PER_CHANNEL:"per_channel",PER_GROUP:"per_group"}),TM=new Set(Object.values($s)),IM=new Set([J.I8,J.UI8]),Fs=class n{static{f(this,"QuantizationParams")}constructor(t){this.scheme=t.scheme,this.scale=t.scale,this.zeroPoint=t.zeroPoint,this.axis=t.axis??null,this.groupSize=t.groupSize??null,this.dtype=t.dtype||J.I8,this.numBits=t.numBits||mr(this.dtype)*8,this._hash=null}clampRange(){if(this.isSymmetric()){let s=2**(this.numBits-1)-1;return[-s,s]}if(this.dtype===J.UI8)return[0,2**this.numBits-1];let t=-(2**(this.numBits-1)),e=2**(this.numBits-1)-1;return[t,e]}quantize(t){let[e,s]=this.clampRange(),r=Math.round(t/this.getScalarScale()+this.getScalarZeroPoint());return Math.max(e,Math.min(s,r))}dequantize(t){return(t-this.getScalarZeroPoint())*this.getScalarScale()}quantizeArray(t){let[e,s]=this.clampRange(),r=new Array(t.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<t.length;a++)r[a]=Math.max(e,Math.min(s,Math.round(t[a]/o+i)));return r}dequantizeArray(t){let e=new Array(t.length);if(this.isPerChannel())throw new Error("Use dequantizeArrayPerChannel(intArr, shape) for per-channel dequantization");let s=this.getScalarScale(),r=this.getScalarZeroPoint();for(let o=0;o<t.length;o++)e[o]=(t[o]-r)*s;return e}_channelStride(t){let e=1;for(let s=this.axis+1;s<t.length;s++)e*=t[s];return e}quantizeArrayPerChannel(t,e){let[s,r]=this.clampRange(),o=this._channelStride(e),i=e[this.axis],a=new Array(t.length);for(let c=0;c<t.length;c++){let l=Math.floor(c/o)%i,u=this.getScaleForChannel(l),h=this.getZeroPointForChannel(l);a[c]=Math.max(s,Math.min(r,Math.round(t[c]/u+h)))}return a}dequantizeArrayPerChannel(t,e){let s=this._channelStride(e),r=e[this.axis],o=new Array(t.length);for(let i=0;i<t.length;i++){let a=Math.floor(i/s)%r;o[i]=(t[i]-this.getZeroPointForChannel(a))*this.getScaleForChannel(a)}return o}quantizeArrayPerGroup(t){let[e,s]=this.clampRange(),r=new Array(t.length);for(let o=0;o<t.length;o++){let i=Math.floor(o/this.groupSize),a=this.scale[i],c=this.zeroPoint[i];r[o]=Math.max(e,Math.min(s,Math.round(t[o]/a+c)))}return r}dequantizeArrayPerGroup(t){let e=new Array(t.length);for(let s=0;s<t.length;s++){let r=Math.floor(s/this.groupSize);e[s]=(t[s]-this.zeroPoint[r])*this.scale[r]}return e}getScaleForGroup(t){return this.scale[t]}getZeroPointForGroup(t){return this.zeroPoint[t]}getScaleForChannel(t){return this.isPerChannel()?this.scale[t]:this.getScalarScale()}getZeroPointForChannel(t){return this.isPerChannel()?this.zeroPoint[t]: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===$s.PER_CHANNEL}isPerGroup(){return this.scheme===$s.PER_GROUP}isSymmetric(){return this.scheme===$s.PER_TENSOR_SYMMETRIC}equals(t){return this===t?!0:!(!(t instanceof n)||this.scheme!==t.scheme||this.dtype!==t.dtype||this.numBits!==t.numBits||this.axis!==t.axis||this.groupSize!==t.groupSize||!NA(this.scale,t.scale)||!NA(this.zeroPoint,t.zeroPoint))}hash(){if(this._hash!==null)return this._hash;let t=2166136261;return t=(t^EA(this.scheme))*16777619&2147483647,t=(t^EA(this.dtype))*16777619&2147483647,t=(t^this.numBits)*16777619&2147483647,t=(t^MM(this.scale))*16777619&2147483647,this._hash=t,t}serialize(){let t=this.isPerChannel()||this.isPerGroup();return{scheme:this.scheme,scale:t?[...this.scale]:this.scale,zeroPoint:t?[...this.zeroPoint]:this.zeroPoint,axis:this.axis,groupSize:this.groupSize,dtype:this.dtype,numBits:this.numBits}}static deserialize(t){return new n(t)}static fromRange(t,e,s,r=J.I8,o=8){if(s===$s.PER_TENSOR_SYMMETRIC){let p=Math.max(Math.abs(t),Math.abs(e)),d=2**(o-1)-1,m=p/d||1e-10;return new n({scheme:s,scale:m,zeroPoint:0,dtype:r,numBits:o})}let[i,a]=r===J.UI8?[0,2**o-1]:[-(2**(o-1)),2**(o-1)-1],l=(e-t||1e-10)/(a-i),u=Math.round(i-t/l),h=Math.max(i,Math.min(a,u));return new n({scheme:s,scale:l,zeroPoint:h,dtype:r,numBits:o})}static fromRangePerChannel(t,e,s,r=J.I8,o=8){let i=t.length,a=new Float64Array(i),c=new Int32Array(i),l=2**(o-1)-1;for(let u=0;u<i;u++){let h=Math.max(Math.abs(t[u]),Math.abs(e[u]));a[u]=h/l||1e-10,c[u]=0}return new n({scheme:$s.PER_CHANNEL,scale:a,zeroPoint:c,axis:s,dtype:r,numBits:o})}static defaultForActivation(t,e=J.I8,s=8){return n.fromRange(-6,6,t,e,s)}static fromConstantArray(t,e,s=J.I8,r=8){let o=1/0,i=-1/0;for(let a=0;a<t.length;a++)t[a]<o&&(o=t[a]),t[a]>i&&(i=t[a]);return isFinite(o)||(o=-1),isFinite(i)||(i=1),o===i&&(o-=.5,i+=.5),n.fromRange(o,i,e,s,r)}static fromConstantArrayPerChannel(t,e,s,r=J.I8,o=8){let i=e[s],a=1;for(let u=s+1;u<e.length;u++)a*=e[u];let c=new Array(i).fill(1/0),l=new Array(i).fill(-1/0);for(let u=0;u<t.length;u++){let h=Math.floor(u/a)%i,p=t[u];p<c[h]&&(c[h]=p),p>l[h]&&(l[h]=p)}for(let u=0;u<i;u++)isFinite(c[u])||(c[u]=-1),isFinite(l[u])||(l[u]=1),c[u]===l[u]&&(c[u]-=.5,l[u]+=.5);return n.fromRangePerChannel(c,l,s,r,o)}static fromConstantArrayPerGroup(t,e,s=J.I8,r=4){let o=Math.ceil(t.length/e),i=new Float64Array(o),a=new Int32Array(o),c=2**(r-1)-1;for(let l=0;l<o;l++){let u=l*e,h=Math.min(u+e,t.length),p=0;for(let d=u;d<h;d++){let m=Math.abs(t[d]);m>p&&(p=m)}i[l]=p/c||1e-10,a[l]=0}return new n({scheme:$s.PER_GROUP,scale:i,zeroPoint:a,groupSize:e,dtype:s,numBits:r})}static isQuantizableDtype(t){return IM.has(t)}static isValidScheme(t){return TM.has(t)}};f(NA,"scaleEquals");f(EA,"hashStr");f(MM,"hashScaleValue")});function PM(n){for(let t=0;t<n.numOperands;t++){let e=n.getOperand(t).definingOp;if(e&&e.opName==="constant")return!0}return!1}function zM(n,t){for(let e=0;e<n.numOperands;e++)if(!t.has(n.getOperand(e)))return!1;return n.numOperands>0}function $M(n,t,e){for(let s=0;s<n.numOperands;s++){let r=n.getOperand(s);if(!t.has(r)&&(!(r.type instanceof E)||!Oe(r.type.dtype)))return!1}return n.numOperands>0}var CM,DM,LM,Sd,vd,TA=T(()=>{Ce();Rs();kt();Ee();we();ur();F0();Ue();CM=new Set(["softmax","sqrt","div","rsqrt","log","exp","tanh"]),DM=new Set(["dot","conv","add","mul","sub"]),LM=new Map([["dot","quantized_dot"],["conv","quantized_conv"]]);for(let[n,t]of LM)st.has(n)&&st.registerOpAttr(n,"quantizedVariant",t);Sd=class{static{f(this,"QuantizationConfig")}constructor(t={}){this.scheme=t.scheme||$s.PER_TENSOR_SYMMETRIC,this.calibration=t.calibration||null,this.targetDtype=t.targetDtype||J.I8,this.excludeOps=t.excludeOps||CM,this.quantizableOps=t.quantizableOps||DM,this.sensitivityThreshold=t.sensitivityThreshold||0,this.sensitivityResult=t.sensitivityResult||null,this.weightOnly=t.weightOnly||!1,this.target=t.target||null}},vd=class extends Rt{static{f(this,"QuantizationPass")}constructor(t={}){super("QuantizationPass"),this.requiredAnalyses=[qt],this.config=t instanceof Sd?t:new Sd(t)}run(t,e){let r=(e?e.getAnalysis(qt,t):qt.compute(t)).topologicalOrder,o=new Set,i=this.config,a=!1;if(i.target&&!i.target.supportsInt8)return Q.UNCHANGED;for(let l=0;l<r.length;l++){let u=r[l];if(u.opName==="return"||u.opName==="yield")continue;if(i.excludeOps.has(u.opName)||!i.quantizableOps.has(u.opName)){for(let g=0;g<u.numOperands;g++)o.has(u.getOperand(g))&&(a=this._insertDequantBefore(u,g,u.getOperand(g),i)||a);continue}if(i.sensitivityResult&&i.sensitivityThreshold>0&&i.sensitivityResult.isSensitive(u,i.sensitivityThreshold)||i.weightOnly&&!PM(u))continue;let h=st.get(u.opName),p=h?h.getAttr("quantizedVariant"):null;if(p&&$M(u,o,i)){i.scheme===$s.PER_CHANNEL&&this._canPerChannelDot(u,o)?a=this._replacePerChannelDot(u,i)||a:a=this._replaceWithNativeQuantized(u,p,o,i)||a;continue}let d=st.get(u.opName);if(d&&d.hasTrait(U.ELEMENTWISE)&&zM(u,o)){for(let g=0;g<u.numResults;g++)o.add(u.getResult(g));continue}a=this._insertDequantQuantBoundary(u,o,i)||a}let c=t.getReturnOp();if(c)for(let l=0;l<c.numOperands;l++){let u=c.getOperand(l);o.has(u)&&(a=this._insertDequantBefore(c,l,u,i)||a)}return this.trace&&this.trace.level>=ot.DEBUG&&a&&this.trace.emit({type:"pass_detail",passName:this.name,opsProcessed:r.length,changed:a,level:ot.DEBUG}),a?Q.CHANGED:Q.UNCHANGED}_getQuantParams(t,e){let s=mr(e.targetDtype)*8;if(e.calibration&&e.calibration.hasData(t))return e.calibration.getQuantParams(t,e.scheme,e.targetDtype);let r=t.definingOp;if(r&&r.opName==="constant"){let o=r.getAttr("value");if(typeof o=="number")return Fs.fromRange(-Math.abs(o)||-1,Math.abs(o)||1,e.scheme,e.targetDtype,s);if(o&&typeof o.length=="number")return Fs.fromConstantArray(o,e.scheme,e.targetDtype,s)}return t.type instanceof E&&Oe(t.type.dtype)?Fs.defaultForActivation(e.scheme,e.targetDtype,s):null}_insertQuantizeAfter(t,e,s){let r=t.getResult(e),o=this._getQuantParams(r,s);if(!o)return null;let i=new E(r.type.shape,s.targetDtype),a=new mt("quantize",[r],[i],{scale:o.getScalarScale(),zero_point:o.getScalarZeroPoint(),scheme:s.scheme,target_dtype:s.targetDtype,...o.axis!==null?{axis:o.axis}:{}});return t.parentBlock&&t.parentBlock.insertAfter(a,t),a.getResult(0)}_insertDequantBefore(t,e,s,r){let o=this._getQuantParams(s,r);if(!o)return!1;let i=J.F32,a=new E(s.type.shape,i),c=new mt("dequantize",[s],[a],{scale:o.getScalarScale(),zero_point:o.getScalarZeroPoint(),scheme:r.scheme,target_dtype:i});return t.parentBlock&&(t.parentBlock.insertBefore(c,t),t.replaceOperand(e,c.getResult(0))),!0}_insertDequantQuantBoundary(t,e,s){let r=!1;for(let o=0;o<t.numOperands;o++){let i=t.getOperand(o);e.has(i)&&(r=this._insertDequantBefore(t,o,i,s)||r)}for(let o=0;o<t.numResults;o++){let i=t.getResult(o);if(!(i.type instanceof E)||!Oe(i.type.dtype))continue;let a=this._insertQuantizeAfter(t,o,s);if(a){let c=[...i.uses()];for(let l of c)l.user!==a.definingOp&&(l.user.replaceOperand(l.operandIndex,a),e.add(a));r=!0}}return r}_replaceWithNativeQuantized(t,e,s,r){let o=[],i={};for(let[h,p]of t.attributes||[])i[h]=p;for(let h=0;h<t.numOperands;h++){let p=t.getOperand(h);if(s.has(p)){o.push(p);let y=this._getQuantParams(p,r);if(y){let b=h===0?"lhs":"rhs";if(e==="quantized_dot")i[`${b}_scale`]=y.getScalarScale(),i[`${b}_zero_point`]=y.getScalarZeroPoint();else{let x=h===0?"input":"kernel";i[`${x}_scale`]=y.getScalarScale(),i[`${x}_zero_point`]=y.getScalarZeroPoint()}}continue}let d=this._getQuantParams(p,r);if(!d)return!1;let m=new E(p.type.shape,r.targetDtype),g=new mt("quantize",[p],[m],{scale:d.getScalarScale(),zero_point:d.getScalarZeroPoint(),scheme:r.scheme,target_dtype:r.targetDtype});t.parentBlock&&t.parentBlock.insertBefore(g,t),o.push(g.getResult(0));let _=h===0?e==="quantized_dot"?"lhs":"input":e==="quantized_dot"?"rhs":"kernel";i[`${_}_scale`]=d.getScalarScale(),i[`${_}_zero_point`]=d.getScalarZeroPoint()}let a=i.lhs_scale||i.input_scale||1,c=i.rhs_scale||i.kernel_scale||1;i.output_scale=a*c,i.output_zero_point=0;let l=new E(t.getResult(0).type.shape,J.I32),u=new mt(e,o,[l],i);if(t.parentBlock){t.parentBlock.insertBefore(u,t);let h=new E(t.getResult(0).type.shape,t.getResult(0).type.dtype),p=i.output_scale||1,d=new mt("dequantize",[u.getResult(0)],[h],{scale:p,zero_point:i.output_zero_point||0,scheme:r.scheme,target_dtype:t.getResult(0).type.dtype});t.parentBlock.insertBefore(d,t),t.replaceAllResultsWith([d.getResult(0)]),t.erase()}return!0}_canPerChannelDot(t,e){if(t.opName!=="dot")return!1;let s=t.getOperand(0),r=t.getOperand(1);if(e.has(s)||e.has(r)||!(s.type instanceof E)||s.type.shape.length!==2||!(r.type instanceof E)||r.type.shape.length!==2)return!1;let o=r.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=t.getAttr("rhs_contracting")||[],c=t.getAttr("lhs_contracting")||[];return!(a.length!==1||c.length!==1||(t.getAttr("rhs_batch")||[]).length!==0||(t.getAttr("lhs_batch")||[]).length!==0)}_activationParams(t,e){let s=mr(e.targetDtype)*8,r=$s.PER_TENSOR_SYMMETRIC;return e.calibration&&e.calibration.hasData(t)?e.calibration.getQuantParams(t,r,e.targetDtype):Fs.defaultForActivation(r,e.targetDtype,s)}_replacePerChannelDot(t,e){let s=t.getOperand(0),r=t.getOperand(1),o=r.type.shape,i=r.definingOp.getAttr("value"),a=mr(e.targetDtype)*8,l=t.getAttr("rhs_contracting")[0]===0?1:0,u=Fs.fromConstantArrayPerChannel([...i],o,l,e.targetDtype,a),h=u.quantizeArrayPerChannel([...i],o),p=this._activationParams(s,e),d=p.getScalarScale(),m=p.getScalarZeroPoint(),g=t.parentBlock;if(!g)return!1;let _=new E(o,e.targetDtype),y=new mt("constant",[],[_],{value:h,tensor_type:_});g.insertBefore(y,t);let b=new E(s.type.shape,e.targetDtype),x=new mt("quantize",[s],[b],{scale:d,zero_point:m,scheme:$s.PER_TENSOR_SYMMETRIC,target_dtype:e.targetDtype});g.insertBefore(x,t);let S=t.getResult(0).type.shape,v={};for(let[$,L]of t.attributes||[])v[$]=L;v.lhs_scale=d,v.lhs_zero_point=m,v.rhs_scale=1,v.rhs_zero_point=0,v.output_scale=1,v.output_zero_point=0;let w=new E(S,J.I32),A=new mt("quantized_dot",[x.getResult(0),y.getResult(0)],[w],v);g.insertBefore(A,t);let I=new E(S,J.F32),B=new mt("convert",[A.getResult(0)],[I],{target_dtype:J.F32});g.insertBefore(B,t);let k=o[l],N=new Array(k);for(let $=0;$<k;$++)N[$]=d*u.getScaleForChannel($);let R=new Set(t.getAttr("lhs_contracting")||[]),O=0;for(let $=0;$<s.type.shape.length;$++)R.has($)||O++;let M=O,F=new E([k],J.F32),P=new mt("constant",[],[F],{value:N,tensor_type:F});g.insertBefore(P,t);let G=new mt("broadcast_in_dim",[P.getResult(0)],[I],{broadcast_dimensions:[M],result_shape:S});g.insertBefore(G,t);let W=new mt("mul",[B.getResult(0),G.getResult(0)],[I],{});return g.insertBefore(W,t),t.replaceAllResultsWith([W.getResult(0)]),t.erase(),!0}};f(PM,"hasConstantOperand");f(zM,"allOperandsQuantized");f($M,"allOperandsCanQuantize")});var V0,G0,Bd,IA=T(()=>{V0=class{static{f(this,"BufferInterval")}constructor(t,e,s,r){this.buffer=t,this.firstUse=e,this.lastUse=s,this.scope=r}get size(){return this.buffer.sizeInBytes()}overlaps(t){return this.firstUse<=t.lastUse&&t.firstUse<=this.lastUse}},G0=class{static{f(this,"BufferLivenessResult")}constructor(t,e,s){this.intervals=t,this.stmtOrder=e,this.paramBuffers=s}isParam(t){return this.paramBuffers.has(t)}getTemporaries(){let t=[];for(let[e,s]of this.intervals)this.paramBuffers.has(e)||t.push(s);return t}interfere(t,e){let s=this.intervals.get(t),r=this.intervals.get(e);return!s||!r?!1:s.overlaps(r)}},Bd=class{static{f(this,"BufferLiveness")}static analyze(t){let e=new Map,s=[],r=0,o=new Set;for(let[,p]of t.bufferMap)o.add(p);let i=[],a=f(p=>{if(!p)return;let d=e.get(p);d?(r<d.firstUse&&(d.firstUse=r),r>d.lastUse&&(d.lastUse=r)):(d=new V0(p,r,r,p.scope),e.set(p,d)),i.push(p)},"touch"),c=f(p=>{if(!(!p||typeof p!="object")){if(p.type==="BufferLoadNode"){a(p.buffer);return}if(p.type==="CallExternNode"){for(let d of p.args)c(d);return}p.a&&c(p.a),p.b&&c(p.b),p.expr&&c(p.expr),p.cond&&c(p.cond),p.condition&&c(p.condition),p.thenBody&&c(p.thenBody),p.elseBody&&c(p.elseBody)}},"touchExpr"),l=f(p=>{if(p){if(p.type==="BufferStoreNode"){a(p.buffer),c(p.value);for(let d of p.indices)c(d)}else if(p.type==="BufferLoadNode"){a(p.buffer);for(let d of p.indices)c(d)}else if(p.type==="SeqNode")for(let d of p.stmts)l(d)}},"touchBody"),u=f((p,d)=>{for(let m=p;m<i.length;m++){let g=e.get(i[m]);g&&d>g.lastUse&&(g.lastUse=d)}},"extendRegion"),h=f(p=>{if(p)switch(p.type){case"SeqNode":for(let d of p.stmts)h(d);break;case"ForNode":{let d=r,m=i.length;h(p.body);let g=r>d?r-1:d;u(m,g);break}case"BlockNode":s.push({idx:r,node:p});for(let d of p.reads)a(d.buffer);for(let d of p.writes)a(d.buffer);l(p.body),p.initBody&&l(p.initBody),r++;break;case"AllocateNode":a(p.buffer),h(p.body);break;case"IfThenElseNode":{let d=r,m=i.length;h(p.thenBody),p.elseBody&&h(p.elseBody);let g=r>d?r-1:d;u(m,g);break}case"LetStmtNode":h(p.body);break;default:l(p);break}},"walk");return h(t.body),new G0(e,s,o)}}});function FM(n,t){if(n.shape.length!==t.shape.length)return!1;for(let e=0;e<n.shape.length;e++)if(n.shape[e]!==t.shape[e])return!1;return!0}function VM(n,t){let e=[n];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="BlockNode"&&t.push(s),s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),s.initBody&&e.push(s.initBody)}}}var q0,kd,MA=T(()=>{q0=class{static{f(this,"InplaceCandidate")}constructor(t,e,s){this.srcBuffer=t,this.dstBuffer=e,this.reason=s}},kd=class{static{f(this,"InplaceAnalysis")}static analyze(t,e,s=new Set){let r=[],o=[];VM(t.body,o);let i=new Map;for(let l of e.stmtOrder)i.set(l.node,l.idx);let a=new Map;for(let l of e.stmtOrder)for(let u of l.node.reads){let h=a.get(u.buffer);(h===void 0||l.idx>h)&&a.set(u.buffer,l.idx)}let c=new Set;for(let l of o){let u=i.get(l);if(u!==void 0)for(let h of l.writes){let p=h.buffer;if(!(e.isParam(p)&&!s.has(p)))for(let d of l.reads){let m=d.buffer;if(m===p||c.has(m)||e.isParam(m)&&!s.has(m)||!FM(m,p)||m.dtype!==p.dtype||m.scope!==p.scope)continue;let g=e.intervals.get(m),_=e.intervals.get(p);if(!(!g||!_)&&g.lastUse<=_.firstUse){let y=a.get(m);if(y===void 0||y<=u){r.push(new q0(m,p,`${m.name} last used at ${g.lastUse}, ${p.name} first used at ${_.firstUse}`)),c.add(m);break}}}}}return r}};f(FM,"shapesMatch");f(VM,"collectBlocks")});function CA(n){return{best:null,consider(t,e,s){return e<s?null:n==="best-fit"?((this.best===null||e<this.best.gap)&&(this.best={offset:t,gap:e}),null):t},result(t){return this.best!==null?this.best.offset:t}}}var U0,W0,Ad,DA=T(()=>{f(CA,"gapSelector");U0=class{static{f(this,"MemoryBlock")}constructor(t,e,s){this.offset=t,this.size=e,this.buffer=s}get end(){return this.offset+this.size}overlaps(t){return this.offset<t.end&&t.offset<this.end}},W0=class{static{f(this,"MemoryPool")}constructor(t,e=64,s="best-fit"){this.scope=t,this.alignment=e,this.strategy=s,this.blocks=[],this.peakUsage=0}allocate(t,e){let s=this._align(t),r=this._findFreeOffset(s);return this.placeAt(r,s,e)}placeAt(t,e,s){let r=this._align(e),o=new U0(t,r,s);this.blocks.push(o);let i=t+r;return i>this.peakUsage&&(this.peakUsage=i),o}_align(t){return Math.ceil(t/this.alignment)*this.alignment}_findFreeOffset(t){let e=this.blocks.slice().sort((o,i)=>o.offset-i.offset),s=0,r=CA(this.strategy);for(let o of e){let i=this._align(s),a=o.offset-i,c=r.consider(i,a,t);if(c!==null)return c;o.end>s&&(s=o.end)}return r.result(this._align(s))}fragmentation(){if(this.peakUsage===0)return 0;let t=this.blocks.reduce((e,s)=>e+s.size,0);return Math.max(0,1-t/this.peakUsage)}release(t){let e=this.blocks.indexOf(t);e>=0&&this.blocks.splice(e,1)}},Ad=class{static{f(this,"BufferAssignment")}constructor(){this.assignments=new Map,this.pools=new Map,this.inplaceMap=new Map}assign(t,e=[],s=64,r="best-fit"){for(let u of e)this.inplaceMap.set(u.dstBuffer,u.srcBuffer);let o=new Map;for(let u of t)o.set(u.buffer,u);let i=new Map;for(let u of t)i.set(u.buffer,u.lastUse);let a=new Map;for(let[u,h]of this.inplaceMap)!o.has(u)||!o.has(h)||(a.has(h)||a.set(h,[]),a.get(h).push(u));if(a.size>0){let u=new Map;for(let h of t){let p=h.buffer;if(u.get(p)===1)continue;let d=[p];for(;d.length;){let m=d[d.length-1],g=a.get(m);if(u.get(m)===void 0){if(u.set(m,0),g)for(let _ of g)u.get(_)===void 0&&d.push(_);continue}if(d.pop(),u.get(m)!==1){if(g){let _=i.get(m);for(let y of g){let b=i.get(y);b>_&&(_=b)}i.set(m,_)}u.set(m,1)}}}}let c=[...t].sort((u,h)=>{let p=u.size,d=h.size,m=p>0,g=d>0;if(m&&g){let _=d-p;if(_!==0)return _}else if(m!==g)return m?-1:1;return u.firstUse-h.firstUse}),l=new Map;for(let u of c){let h=u.buffer;if(this.inplaceMap.has(h)){let x=this.inplaceMap.get(h),S=this.assignments.get(x);if(S){this.assignments.set(h,{offset:S.offset,size:u.size,scope:u.scope,pool:S.pool,inplaceOf:x});continue}}let p=u.size;if(p===0)continue;if(p<0){this.assignments.set(h,{offset:0,size:0,scope:u.scope,pool:u.scope,inplaceOf:null,isDynamic:!0});continue}let d=u.scope,m=this.pools.get(d);m||(m=new W0(d,s),this.pools.set(d,m)),l.has(d)||l.set(d,[]);let g=l.get(d),_=i.get(h)??u.lastUse,y=this._interferenceOffset(g,u.firstUse,_,m._align(p),s,r),b=m.placeAt(y,p,h);g.push({firstUse:u.firstUse,lastUseEff:_,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(t,e,s,r,o,i){let a=[];for(let u of t)u.firstUse<=s&&e<=u.lastUseEff&&a.push([u.offset,u.offset+u.size]);a.sort((u,h)=>u[0]-h[0]);let c=0,l=CA(i);for(let[u,h]of a){let p=Math.ceil(c/o)*o,d=u-p,m=l.consider(p,d,r);if(m!==null)return m;h>c&&(c=h)}return l.result(Math.ceil(c/o)*o)}getOffset(t){let e=this.assignments.get(t);return e?e.offset:-1}getAssignment(t){return this.assignments.get(t)||null}peakMemory(t=null){if(t){let s=this.pools.get(t);return s?s.peakUsage:0}let e=0;for(let[,s]of this.pools)e+=s.peakUsage;return e}}});var j0,Rd,LA=T(()=>{IA();MA();DA();Ct();j0=class{static{f(this,"MemoryPlan")}constructor(t,e,s){this.assignment=t,this.liveness=e,this.inplaceCandidates=s}peakMemory(t=null){return this.assignment.peakMemory(t)}getReport(){let t=new Map;for(let[r,o]of this.assignment.pools)t.set(r,{peakUsage:o.peakUsage,numBuffers:0,numReused:0});for(let[r,o]of this.assignment.assignments){let i=t.get(o.scope);i&&(i.numBuffers++,o.inplaceOf&&i.numReused++)}let e=this.liveness.getTemporaries().length,s=this.inplaceCandidates.length;return{peakMemory:this.assignment.peakMemory(),scopeBreakdown:t,totalTemporaries:e,totalInplace:s,assignments:this.assignment.assignments}}},Rd=class{static{f(this,"MemoryPlanner")}constructor(t={}){this.alignment=t.alignment||64,this.enableInplace=t.enableInplace!==!1,this.allocStrategy=t.allocStrategy||"best-fit"}plan(t){let e=Bd.analyze(t),s=e.getTemporaries(),r=[];this.enableInplace&&(r=kd.analyze(t,e));let o=new Ad;return o.assign(s,r,this.alignment,this.allocStrategy),new j0(o,e,r)}planAndRewrite(t){let e=this.plan(t);return{func:this._insertAllocations(t,e),plan:e}}_insertAllocations(t,e){let s=e.liveness.getTemporaries();if(s.length===0)return t;let r=[...s].sort((i,a)=>a.firstUse-i.firstUse),o=t.body;for(let i of r){let a=i.buffer,c=e.assignment.getAssignment(a);c&&(c.inplaceOf||(o=new fs(a,c.isDynamic?"dynamic":c.scope,o)))}return t.body=o,t._setChild("body",o),t}}});var Od,K0,ci,H0=T(()=>{Ct();Od=["iterCount","depth","parallelLoops","vectorizedLoops","unrolledLoops","threadBoundLoops","serialLoops","threadBlockSize","gridSize","underReduction","numMathOps","numExternCalls","numReads","numWrites","stride1Accesses","stridedAccesses","reuseCount","touchedBytes","arithmeticIntensity","vectorized","parallelized","innermostExtent"],K0=class{static{f(this,"ScheduleFeatures")}constructor(t){this.numLoops=t.numLoops||0,this.numBlocks=t.numBlocks||0,this.totalIterations=t.totalIterations||0,this.maxLoopDepth=t.maxLoopDepth||0,this.numParallelLoops=t.numParallelLoops||0,this.numVectorizedLoops=t.numVectorizedLoops||0,this.numUnrolledLoops=t.numUnrolledLoops||0,this.numThreadBound=t.numThreadBound||0,this.numSerialLoops=t.numSerialLoops||0,this.totalBufferBytes=t.totalBufferBytes||0,this.numBufferReads=t.numBufferReads||0,this.numBufferWrites=t.numBufferWrites||0,this.numMathOps=t.numMathOps||0,this.numExternCalls=t.numExternCalls||0,this.arithmeticIntensity=t.arithmeticIntensity||0,this.innermostExtent=t.innermostExtent||0,this.outermostExtent=t.outermostExtent||0,this.hasReduction=t.hasReduction||!1,this.reductionDepth=t.reductionDepth||0,this.threadBlockSize=t.threadBlockSize||0,this.gridSize=t.gridSize||0,this.strideOneAccesses=t.strideOneAccesses||0,this.nonStrideOneAccesses=t.nonStrideOneAccesses||0}},ci=class n{static{f(this,"FeatureExtractor")}static extract(t){let e={numLoops:0,numBlocks:0,totalIterations:1,maxLoopDepth:0,currentDepth:0,numParallelLoops:0,numVectorizedLoops:0,numUnrolledLoops:0,numThreadBound:0,numSerialLoops:0,totalBufferBytes:0,buffersSeen:new Set,numBufferReads:0,numBufferWrites:0,numMathOps:0,numExternCalls:0,hasReduction:!1,reductionDepth:0,threadBlockSize:1,gridSize:1,innermostExtent:0,outermostExtent:0,loopExtents:[],strideOneAccesses:0,nonStrideOneAccesses:0};n._visitIterative(t.body,e);let s=e.totalBufferBytes,r=e.numMathOps+e.numExternCalls;return e.arithmeticIntensity=s>0?r/s:0,new K0(e)}static extractStatements(t){let e=[],s=[],r=0,o=[{node:t.body,action:"enter"}];for(;o.length>0;){let{node:i,action:a}=o.pop();if(i){if(a==="leaveFor"){s.pop();continue}if(a==="leaveBlock"){r--;continue}switch(i.type){case"ForNode":s.push(i),o.push({node:null,action:"leaveFor"}),o.push({node:i.body,action:"enter"});break;case"BlockNode":i.initBody&&(r++,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 c=i.stmts.length-1;c>=0;c--)o.push({node:i.stmts[c],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":e.push(n._statementVector(i,s,r));break}}}return e}static _statementVector(t,e,s){let r=1,o=0,i=0,a=0,c=0,l=0,u=1,h=1;for(let w of e){let A=w.extent&&w.extent.type==="IntImmNode"?w.extent.value:1;switch(r*=A,w.kind){case V.PARALLEL:o++;break;case V.VECTORIZED:i++;break;case V.UNROLLED:a++;break;case V.THREAD_BINDING:c++,w.threadTag&&w.threadTag.startsWith("threadIdx")?u*=A:w.threadTag&&w.threadTag.startsWith("blockIdx")&&(h*=A);break;default:l++;break}}let p={math:0,extern:0};n._countExpr(t.value,p);let d=e.map(w=>w.loopVar.name),m=[{buffer:t.buffer,indices:t.indices}];n._collectLoads(t.value,m);let g=0,_=0,y=0,b=0;for(let w of m){let A=w.buffer&&w.buffer.sizeInBytes?w.buffer.sizeInBytes():0;A>0&&(b+=A);let I=w.indices&&w.indices.length>0?w.indices[w.indices.length-1]:null;I&&I.type==="VariableNode"?g++:_++;let B=new Set;if(w.indices)for(let k of w.indices)n._collectVars(k,B);for(let k of d)B.has(k)||y++}let x=e.length>0?e[e.length-1]:null,S=x&&x.extent&&x.extent.type==="IntImmNode"?x.extent.value:0,v={iterCount:r,depth:e.length,parallelLoops:o,vectorizedLoops:i,unrolledLoops:a,threadBoundLoops:c,serialLoops:l,threadBlockSize:u,gridSize:h,underReduction:s>0?1:0,numMathOps:p.math,numExternCalls:p.extern,numReads:m.length-1,numWrites:1,stride1Accesses:g,stridedAccesses:_,reuseCount:y,touchedBytes:b,arithmeticIntensity:b>0?(p.math+p.extern)/b:0,vectorized:i>0?1:0,parallelized:o+c>0?1:0,innermostExtent:S};return Od.map(w=>v[w]||0)}static _countExpr(t,e){if(!(!t||typeof t!="object"))switch(t.type){case"MathOpNode":e.math++,n._countExpr(t.a,e),t.b&&n._countExpr(t.b,e);break;case"CompareNode":e.math++,n._countExpr(t.a,e),n._countExpr(t.b,e);break;case"CallExternNode":e.extern++;for(let s of t.args)n._countExpr(s,e);break;default:break}}static _collectLoads(t,e){if(!(!t||typeof t!="object")){if(t.type==="BufferLoadNode"){e.push({buffer:t.buffer,indices:t.indices});return}if(t.a&&n._collectLoads(t.a,e),t.b&&n._collectLoads(t.b,e),t.args)for(let s of t.args)n._collectLoads(s,e)}}static _collectVars(t,e){if(!(!t||typeof t!="object")){if(t.type==="VariableNode"){e.add(t.name);return}if(t.a&&n._collectVars(t.a,e),t.b&&n._collectVars(t.b,e),t.args)for(let s of t.args)n._collectVars(s,e);if(t.indices)for(let s of t.indices)n._collectVars(s,e)}}static _visitIterative(t,e){let s=[{node:t,action:"enter"}];for(;s.length>0;){let{node:r,action:o}=s.pop();if(r){if(o==="leave_for"){e.currentDepth--;continue}switch(r.type){case"ForNode":{e.numLoops++,e.currentDepth++,e.currentDepth>e.maxLoopDepth&&(e.maxLoopDepth=e.currentDepth);let i=r.extent.type==="IntImmNode"?r.extent.value:1;switch(e.loopExtents.push(i),e.numLoops===1&&(e.outermostExtent=i),e.innermostExtent=i,r.kind){case V.PARALLEL:e.numParallelLoops++;break;case V.VECTORIZED:e.numVectorizedLoops++;break;case V.UNROLLED:e.numUnrolledLoops++;break;case V.THREAD_BINDING:e.numThreadBound++,r.threadTag&&r.threadTag.startsWith("threadIdx")?e.threadBlockSize*=i:r.threadTag&&r.threadTag.startsWith("blockIdx")&&(e.gridSize*=i);break;default:e.numSerialLoops++;break}e.totalIterations*=i,s.push({node:null,action:"leave_for"}),s.push({node:r.body,action:"enter"});break}case"BlockNode":e.numBlocks++,r.initBody&&(e.hasReduction=!0,e.reductionDepth=e.currentDepth);for(let i of r.reads)n._visitBuffer(i.buffer,e);for(let i of r.writes)n._visitBuffer(i.buffer,e);s.push({node:r.body,action:"enter"}),r.initBody&&s.push({node:r.initBody,action:"enter"});break;case"SeqNode":for(let i=r.stmts.length-1;i>=0;i--)s.push({node:r.stmts[i],action:"enter"});break;case"AllocateNode":n._visitBuffer(r.buffer,e),s.push({node:r.body,action:"enter"});break;case"IfThenElseNode":r.elseBody&&s.push({node:r.elseBody,action:"enter"}),s.push({node:r.thenBody,action:"enter"});break;case"LetStmtNode":s.push({node:r.body,action:"enter"});break;case"BufferStoreNode":e.numBufferWrites++,n._visitBuffer(r.buffer,e),n._checkStride(r.buffer,r.indices,e),n._visitExpr(r.value,e);break}}}}static _visitBuffer(t,e){if(!t||e.buffersSeen.has(t))return;e.buffersSeen.add(t);let s=t.sizeInBytes();s>0&&(e.totalBufferBytes+=s)}static _visitExpr(t,e){if(t)switch(t.type){case"MathOpNode":e.numMathOps++,n._visitExpr(t.a,e),t.b&&n._visitExpr(t.b,e);break;case"CallExternNode":e.numExternCalls++;for(let s of t.args)n._visitExpr(s,e);break;case"BufferLoadNode":e.numBufferReads++,n._checkStride(t.buffer,t.indices,e);break;case"CompareNode":e.numMathOps++,n._visitExpr(t.a,e),n._visitExpr(t.b,e);break;default:break}}static _checkStride(t,e,s){if(!t||!e||e.length===0)return;let r=e[e.length-1];for(;r&&r.type==="MathOpNode"&&r.b&&r.b.type==="IntImmNode";)if(r.op==="+"&&r.b.value===0)r=r.a;else if(r.op==="*"&&r.b.value===1)r=r.a;else break;r&&r.type==="VariableNode"?s.strideOneAccesses++:s.nonStrideOneAccesses++}}});function PA(n,t){if(n.length===0)return 0;let e=0;for(let s of n)e+=t[s];return e/n.length}function Y0(n,t,e,s,r,o){let i=e.length;if(s>=r||i<o*2)return{leaf:PA(e,t)};let a=n[0].length,c=0,l=0;for(let d of e){let m=t[d];c+=m,l+=m*m}let u=null,h=null;for(let d=0;d<a;d++){let m=e.slice().sort((y,b)=>n[y][d]-n[b][d]),g=0,_=0;for(let y=0;y<m.length-1;y++){let b=t[m[y]];g+=b,_+=b*b;let x=n[m[y]][d],S=n[m[y+1]][d];if(x===S)continue;let v=y+1,w=i-v;if(v<o||w<o)continue;let A=c-g,I=l-_,B=_-g*g/v+(I-A*A/w);(!u||B<u.cost)&&(u={f:d,thr:(x+S)/2,sIdx:y,cost:B},h=m)}}if(!u)return{leaf:PA(e,t)};let p=h;return{f:u.f,thr:u.thr,left:Y0(n,t,p.slice(0,u.sIdx+1),s+1,r,o),right:Y0(n,t,p.slice(u.sIdx+1),s+1,r,o)}}function zA(n,t){for(;n&&n.leaf===void 0;)n=t[n.f]<=n.thr?n.left:n.right;return n?n.leaf:0}var cl,$A=T(()=>{f(PA,"leafValue");f(Y0,"buildTree");f(zA,"predictTree");cl=class n{static{f(this,"GradientBoostedTrees")}constructor(t={}){this.numTrees=t.numTrees??60,this.maxDepth=t.maxDepth??3,this.lr=t.lr??.1,this.minSamples=t.minSamples??1,this.trees=null,this.base=0}fit(t,e){let s=t.length;if(s===0)return;let r=0;for(let c of e)r+=c;r/=s;let o=new Array(s).fill(r),i=[];for(let c=0;c<s;c++)i.push(c);let a=[];for(let c=0;c<this.numTrees;c++){let l=new Array(s);for(let h=0;h<s;h++)l[h]=e[h]-o[h];let u=Y0(t,l,i,0,this.maxDepth,this.minSamples);for(let h=0;h<s;h++)o[h]+=this.lr*zA(u,t[h]);a.push(u)}this.trees=a,this.base=r}predict(t){if(!this.trees)return 0;let e=this.base;for(let s of this.trees)e+=this.lr*zA(s,t);return e}serialize(){return{trees:this.trees,base:this.base,lr:this.lr}}static deserialize(t){let e=new n({lr:t.lr});return e.trees=t.trees,e.base=t.base,e}}});function FA(n){let t=n[0].length,e=new Array(t+1).fill(0);for(let s of n)for(let r=0;r<t;r++){let o=s[r]||0;UM.has(r)?o>e[r]&&(e[r]=o):e[r]+=o}for(let s of WM)s<t&&(e[s]/=n.length);return e[t]=n.length,e}var GM,qM,UM,WM,J0,jM,Nd,La,Pa,Z0=T(()=>{H0();$A();GM=new Set(["depth","threadBlockSize","gridSize","underReduction","vectorized","parallelized","innermostExtent"]),qM=new Set(["arithmeticIntensity"]),UM=new Set(Od.map((n,t)=>GM.has(n)?t:-1).filter(n=>n>=0)),WM=new Set(Od.map((n,t)=>qM.has(n)?t:-1).filter(n=>n>=0));f(FA,"aggregateStatements");J0=class{static{f(this,"CostEstimate")}constructor(t,e){this.score=t,this.breakdown=e}},jM={parallelism:2,vectorization:1.5,memoryCoalescing:2,occupancy:1,arithmeticIntensity:1,loopOverhead:-.5,codeSize:-.3},Nd=class{static{f(this,"AnalyticalCostModel")}constructor(t,e={}){this.target=t,this._weights={...jM,...t&&t.costModelWeights?t.costModelWeights:{},...e.weights||{}}}estimate(t){let e=ci.extract(t);return this.estimateFromFeatures(e)}score(t){return this.estimate(t).score}estimateFromFeatures(t){let e=this._scoreParallelism(t),s=this._scoreVectorization(t),r=this._scoreMemoryAccess(t),o=this._scoreOccupancy(t),i=this._scoreIntensity(t),a=this._scoreOverhead(t),c=this._scoreCodeSize(t),l=this._weights,u=l.parallelism*e+l.vectorization*s+l.memoryCoalescing*r+l.occupancy*o+l.arithmeticIntensity*i+l.loopOverhead*a+l.codeSize*c;return new J0(u,{parallelism:e,vectorization:s,memoryCoalescing:r,occupancy:o,arithmeticIntensity:i,loopOverhead:a,codeSize:c})}_scoreParallelism(t){if(this.target.isGPU()){let s=t.threadBlockSize*t.gridSize,r=this.target.maxParallelism();return Math.min(1,s/Math.max(r*.1,1))}return t.numParallelLoops/Math.max(t.numLoops,1)}_scoreVectorization(t){return t.numLoops===0?0:this.target.isGPU()?t.strideOneAccesses/Math.max(t.strideOneAccesses+t.nonStrideOneAccesses,1):t.numVectorizedLoops>0?Math.min(1,t.innermostExtent/this.target.vectorWidth):0}_scoreMemoryAccess(t){let e=t.strideOneAccesses+t.nonStrideOneAccesses;return e===0?1:t.strideOneAccesses/e}_scoreOccupancy(t){if(!this.target.isGPU())return 1;if(t.threadBlockSize===0)return 0;let e=this.target.warpSize,s=Math.ceil(t.threadBlockSize/e),r=Math.floor(this.target.maxThreadsPerBlock/e);return Math.min(1,s/r)}_scoreIntensity(t){let e=this.target.supportsFloat16?1.5:1;return Math.min(1,t.arithmeticIntensity*10*e)}_scoreOverhead(t){return t.numSerialLoops/Math.max(t.numLoops,1)}_scoreCodeSize(t){return Math.min(1,(t.numMathOps+t.numExternCalls)/256)}compare(t,e){return this.estimate(t).score-this.estimate(e).score}},La=class n{static{f(this,"LearnedCostModel")}constructor(t=null,e={}){this.opts={numTrees:e.numTrees??60,maxDepth:e.maxDepth??3,lr:e.lr??.1,minSamples:e.minSamples??1},this._gbt=t?cl.deserialize(t):null,this._X=[],this._Y=[]}addSample(t,e){!t||t.length===0||Number.isFinite(e)&&(this._X.push(FA(t)),this._Y.push(e))}train(){if(this._X.length===0)return;let t=new cl(this.opts);t.fit(this._X,this._Y),this._gbt=t}predict(t){return!this._gbt||!t||t.length===0?0:this._gbt.predict(FA(t))}get trained(){return this._gbt!==null}serialize(){return{gbt:this._gbt?this._gbt.serialize():null,numSamples:this._X.length}}static deserialize(t){return new n(t&&t.gbt?t.gbt:null)}},Pa=class{static{f(this,"GuidedCostModel")}constructor(t,e){this.analytical=t,this.learned=e}score(t){return this.learned&&this.learned.trained?this.learned.predict(ci.extractStatements(t)):this.analytical.score(t)}}});function*VA(n){let t=[n];for(;t.length>0;){let e=t.pop();if(e){if(yield e,e.body&&t.push(e.body),e.stmts)for(let s of e.stmts)t.push(s);e.thenBody&&t.push(e.thenBody),e.elseBody&&t.push(e.elseBody),e.initBody&&t.push(e.initBody)}}}function GA(n,t){for(let e of VA(n))if(e.type==="BlockNode"&&e.name===t)return e;return null}function ul(n){let t=[];for(let e of VA(n))e.type==="BlockNode"&&t.push(e.name);return t}function Ed(n,t){let e=de(n,t);if(!e)return{spatial:0,reduction:0,reads:0,hasReduction:!1};let s=0,r=0;for(let o of e.loops)e.reductionLoopVars.has(o.loopVar.name)?r++:s++;return{spatial:s,reduction:r,reads:e.readBuffers.length,hasReduction:e.hasReduction}}var Td=T(()=>{ro();f(VA,"walkStmts");f(GA,"findBlock");f(ul,"collectAllBlockNames");f(Ed,"analyzeBlockStructure")});function UA(n){return n.isGPU()?HM:KM}function X0(n){let t=0,e=0;for(let[s,r]of n.order)s==="S"?t=Math.max(t,r+1):e=Math.max(e,r+1);return{spatialLevels:t,reductionLevels:e}}var KM,HM,qA,Q0=T(()=>{KM={name:"mlt_cpu",order:[["S",0],["S",1],["S",2],["S",3],["R",0]],roles:{S0:"parallelize",S3:"vectorize"}},HM={name:"mlt_gpu",order:[["S",0],["S",1],["S",2],["R",0]],roles:{S0:"blockIdx",S1:"threadIdx",S2:"unroll"}},qA={name:"ssrsrs_cpu",order:[["S",0],["S",1],["R",0],["S",2],["R",1],["S",3]],roles:{S0:"parallelize",S3:"vectorize",R1:"unroll"}};f(UA,"getTileStructure");f(X0,"levelCounts")});var dn,Vs,Id=T(()=>{dn=class{static{f(this,"SearchVariable")}constructor(t,e){this.name=t,this.candidates=e}sample(t){return this.candidates[t(this.candidates.length)]}},Vs=class{static{f(this,"ScheduleSketch")}constructor(t,e,s){this.name=t,this.variables=e,this._apply=s}instantiate(t){return(e,s,r)=>{this._apply(e,s,r,t)}}sampleParams(t){let e={};for(let s of this.variables)e[s.name]=s.sample(t);return e}}});function YM(n){let t=[];for(let e=1;e*e<=n;e++){if(n%e!==0)continue;t.push(e);let s=n/e;s!==e&&t.push(s)}return t.sort((e,s)=>e-s),t}function JM(n,t){let e=Math.min(n.length,t.length);for(let s=0;s<e;s++)if(n[s]!==t[s])return n[s]-t[s];return n.length-t.length}function ZM(n,t){let e=new Set,s=[];n.sort(JM);for(let i of n){let a=i.join(",");e.has(a)||(e.add(a),s.push(i))}if(s.length<=t)return s;let r=new Set,o=[];for(let i=0;i<t;i++){let a=Math.floor(i*(s.length-1)/(t-1));r.has(a)||(r.add(a),o.push(s[a]))}return o}function tb(n,t,e=48){if(!Number.isFinite(n)||n<1||t<=1)return[[n]];let s=[],r=e*8,o=f((i,a,c)=>{if(!(s.length>=r)){if(a===t-1){s.push([...c,i]);return}for(let l of YM(i))if(c.push(l),o(i/l,a+1,c),c.pop(),s.length>=r)return}},"rec");return o(n,0,[]),ZM(s,e)}var WA=T(()=>{f(YM,"divisorsOf");f(JM,"cmpTuple");f(ZM,"selectDiverse");f(tb,"enumerateFactorizations")});function Md(n){return n.extent&&n.extent.type==="IntImmNode"?n.extent.value:null}function KA(n,t,e,s){let r=[],o=e;for(let i=0;i<s.length-1;i++){let a=n.getLoops(t).find(h=>h.loopVar.name===o);if(!a)return null;let c=1;for(let h=i+1;h<s.length;h++)c*=s[h];let[l,u]=n.split(a,c);r.push(l.loopVar.name),o=u.loopVar.name}return r.push(o),r}function XM(n,t,e,s,r){let o=f(i=>n.getLoops(t).find(a=>a.loopVar.name===i),"find");for(let[i,a]of e.order){let c=e.roles[i+a];if(!c)continue;let l=i==="S"?s:r;if(c==="parallelize"){let u=l[0]&&a<l[0].length?o(l[0][a]):null;u&&n.parallelize(u)}else if(c==="vectorize"){let u=l[l.length-1],h=u&&a<u.length?o(u[a]):null;h&&n.vectorize(h)}else if(c==="unroll")for(let u of l){let h=a<u.length?o(u[a]):null;h&&n.unroll(h)}else(c==="blockIdx"||c==="threadIdx")&&l.forEach((u,h)=>{if(h>=jA.length)return;let p=a<u.length?o(u[a]):null;p&&n.bindThread(p,`${c}.${jA[h]}`)})}}function HA(n,t,e,s,r,o){let i=[];for(let h=0;h<s.length;h++){let p=KA(n,t,s[h],o[`s${h}`]);if(!p)return;i.push(p)}let a=[];for(let h=0;h<r.length;h++){let p=KA(n,t,r[h],o[`r${h}`]);if(!p)return;a.push(p)}let c=[];for(let[h,p]of e.order){let d=h==="S"?i:a;for(let m of d)p<m.length&&c.push(m[p])}let l=new Map(n.getLoops(t).map(h=>[h.loopVar.name,h])),u=c.map(h=>l.get(h)).filter(Boolean);u.length>=2&&n.reorder(...u),XM(n,t,e,i,a)}function YA(n){let t=n.loops.filter(s=>!n.reductionLoopVars.has(s.loopVar.name)),e=n.loops.filter(s=>n.reductionLoopVars.has(s.loopVar.name));return{spatialLoops:t,reductionLoops:e}}function JA(n,t,e,s){let r=[];return n.forEach((o,i)=>r.push(new dn(`s${i}`,tb(Md(o),e)))),t.forEach((o,i)=>r.push(new dn(`r${i}`,tb(Md(o),s)))),r}function ZA(n,t){let{spatialLevels:e,reductionLevels:s}=X0(t),{spatialLoops:r,reductionLoops:o}=YA(n);if(r.length===0)return null;for(let l of[...r,...o])if(Md(l)===null)return null;let i=JA(r,o,e,s),a=r.map(l=>l.loopVar.name),c=o.map(l=>l.loopVar.name);return new Vs(t.name,i,(l,u,h,p)=>{HA(l,u,t,a,c,p)})}function XA(n,t){let{spatialLevels:e,reductionLevels:s}=X0(t),{spatialLoops:r,reductionLoops:o}=YA(n);if(r.length===0||o.length===0)return null;for(let l of[...r,...o])if(Md(l)===null)return null;let i=JA(r,o,e,s),a=r.map(l=>l.loopVar.name),c=o.map(l=>l.loopVar.name);return new Vs(t.name,i,(l,u,h,p)=>{l.decomposeReduction(u),HA(l,`${u}_upd`,t,a,c,p)})}var jA,QA=T(()=>{Id();WA();Q0();jA=["x","y","z"];f(Md,"staticExtent");f(KA,"multiLevelSplit");f(XM,"applyRoles");f(HA,"tileBlock");f(YA,"splitLoops");f(JA,"tilingVariables");f(ZA,"createMultiLevelTilingSketch");f(XA,"createSSRSRSTilingSketch")});function e1(n){return Math.min(n&&n.maxThreadsPerBlock||256,256)}function tC(n){let t=new Set;for(let e=2;e*e<=n;e++){if(n%e!==0)continue;t.add(e);let s=n/e;s>1&&s<n&&t.add(s)}return[...t].sort((e,s)=>e-s)}function s1(n){return new Vs("fused",[],(t,e,s,r)=>{t.fuseConsumer(e,n);let o=t.getLoops(e);o.length>0&&t.parallelize(o[0])})}function n1(n){let t=n.loops.filter(i=>n.reductionLoopVars.has(i.loopVar.name));if(t.length!==1)return null;let e=t[0],s=e.extent&&e.extent.type==="IntImmNode"?e.extent.value:null;if(s===null)return null;let r=tC(s);if(r.length===0)return null;let o=e.loopVar.name;return new Vs("rfactor",[new dn("rf_factor",r)],(i,a,c,l)=>{i.rfactor(a,o,l.rf_factor);let u=i.getLoops(`${a}_rf_p`);u.length>0&&i.parallelize(u[0])})}function r1(){return new Vs("elementwise_cpu",[new dn("vector_width",QM)],(n,t,e,s)=>{let r=n.getLoops(t);if(r.length===0)return;if(r.length===1){let a=r[0].extent;if(a.type==="IntImmNode"&&a.value>=s.vector_width*2){let[c,l]=n.split(r[0],s.vector_width);n.parallelize(c),n.vectorize(l)}else n.parallelize(r[0]);return}n.parallelize(r[0]);let o=r[r.length-1],i=o.extent;if(i.type==="IntImmNode"&&i.value>=s.vector_width){let[,a]=n.split(o,s.vector_width);n.vectorize(a)}})}function o1(){return new Vs("elementwise_gpu",[new dn("block_size",t1)],(n,t,e,s)=>{let r=n.getLoops(t);if(r.length===0)return;let o=r[0];for(let u=1;u<r.length;u++){let p=n.getLoops(t).find(d=>d.loopVar.name===r[u].loopVar.name);p&&o.body===p&&(o=n.fuseLoops(o,p))}let i=o.extent;if(i.type!=="IntImmNode"){n.bindThread(o,"threadIdx.x");return}let a=i.value,c=Math.min(e.maxThreadsPerBlock,1024);if(Wy(n.func)&&a<=c){n.bindThread(o,"threadIdx.x");return}let l=Math.min(s.block_size,e1(e));if(a>l){let[u,h]=n.split(o,l);n.bindThread(u,"blockIdx.x"),n.bindThread(h,"threadIdx.x")}else n.bindThread(o,"threadIdx.x")})}function i1(){return new Vs("reduction_cpu",[],(n,t,e,s)=>{let r=n.getLoops(t);r.length>0&&n.parallelize(r[0])})}function a1(){return new Vs("reduction_gpu",[new dn("block_size",t1)],(n,t,e,s)=>{let r=n.getLoops(t);if(r.length===0)return;let o=de(n.func,t),i=r.filter(u=>!o||!fa(u,o));if(i.length===0)return;let a=i[0];for(let u=1;u<i.length;u++){let p=n.getLoops(t).find(d=>d.loopVar.name===i[u].loopVar.name);p&&a.body===p&&(a=n.fuseLoops(a,p))}let c=a.extent;if(c.type!=="IntImmNode"){n.bindThread(a,"threadIdx.x");return}let l=Math.min(s.block_size,e1(e));if(c.value>l){let[u,h]=n.split(a,l);n.bindThread(u,"blockIdx.x"),n.bindThread(h,"threadIdx.x")}else n.bindThread(a,"threadIdx.x")})}var t1,QM,c1=T(()=>{ro();Id();t1=[32,64,128,256,512,1024],QM=[1,2,4,8,16];f(e1,"gpuThreadCap");f(tC,"reductionDivisors");f(s1,"createFusedTilingSketch");f(n1,"createRfactorSketch");f(r1,"createElementwiseCPUSketch");f(o1,"createElementwiseGPUSketch");f(i1,"createReductionCPUSketch");f(a1,"createReductionGPUSketch")});function rb(n,t){let e=[];for(let s=1;s<=t;s*=2)s>=n&&e.push(s);return e}function h1(n,t){if(!de(n,t))return null;let s=GA(n.body,t);if(!s||s.reads.length<2||s.writes.length<1)return null;let r=s.reads[0].buffer,o=s.reads[1].buffer,i=s.writes[0].buffer;if(!r||!o||!i||r.shape.length!==2||o.shape.length!==2||i.shape.length!==2)return null;let a=i.shape[0],c=i.shape[1],l=r.shape[1];return![a,c,l].every(u=>typeof u=="number"&&u>0)||o.shape[0]!==l||o.shape[1]!==c||r.dtype!=="f32"||o.dtype!=="f32"||i.dtype!=="f32"?null:{A:r,B:o,C:i,M:a,N:c,K:l}}function eC(n,t,e=32){let s=n.maxThreadsPerBlock||1024,r=n.warpSize||32,o=n.sharedMemoryBytes||49152,i=n.registersPerThread||255,a=4,{M:c,N:l,K:u}=t,h=rb(2,8),p=rb(2,r/2),d=rb(r/4,r/2),m=2*r,g=Math.min(s,8*r),_=4*r,y=new Set,b=[];for(let x of h)for(let S of h)if(!(x*S>_)&&!(x*S+x+S+r>i))for(let v of p)for(let w of p){let A=w*v;if(A<m||A>g||A%r!==0)continue;let I=x*v,B=S*w;if(!(I>c||B>l||I>_||B>_))for(let k of d){if(k>u||(I*k+k*B)*a>o)continue;let R=`${I}_${B}_${k}_${x}_${S}`;y.has(R)||(y.add(R),b.push({BM:I,BN:B,BK:k,TM:x,TN:S,threads:A}))}}return b.sort((x,S)=>f1(S,r)-f1(x,r)),b.slice(0,e)}function f1(n,t){let e=n.TM*n.TN,s=-Math.abs(n.TM-n.TN),r=-Math.abs(Math.log2(n.BM)-Math.log2(n.BN)),o=n.threads>=4*t&&n.threads<=8*t?1:0,i=n.BK===t/4?1:0;return o*100+e*4+s*6+r*4+i}function sC(n,t){let{A:e,B:s,C:r,M:o,N:i,K:a}=n,{BM:c,BN:l,BK:u,TM:h,TN:p}=t,d=l/p,m=c/h,g=d*m,_=Math.ceil(a/u),y=c*u,b=u*l,x=Math.ceil(y/g),S=Math.ceil(b/g),v=o%c!==0,w=i%l!==0,A=a%u!==0,I=y%g!==0,B=b%g!==0,k=new Vt("rb_As",[y],"f32","shared"),N=new Vt("rb_Bs",[b],"f32","shared"),R=new Vt("rb_acc",[h*p],"f32","local"),O=new Vt("rb_af",[h],"f32","local"),M=new Vt("rb_bf",[p],"f32","local"),F=ve("rb_bx"),P=ve("rb_by"),G=ve("rb_tx"),W=ve("rb_ty"),$=ve("rb_tid"),L=ve("rb_brow"),q=ve("rb_bcol"),yt=ve("rb_k0"),lt=f((gs,XR)=>ds(Gs(gs,Tt(p)),XR),"accIdx"),pt=ve("rb_im"),z=ve("rb_in"),Y=Lr(pt,h,Lr(z,p,new X(R,[lt(pt,z)],eb()))),Bt=ve("rb_la"),bt=ve("rb_aidx"),_e=ds(L,u1(bt,Tt(u))),Ds=ds(yt,l1(bt,Tt(u))),Ss=new K(e,[_e,Ds]);if(v||A){let gs=v?Cn(_e,Tt(o)):null;A&&(gs=gs?sb(gs,Cn(Ds,Tt(a))):Cn(Ds,Tt(a))),Ss=new Mt(gs,Ss,eb())}let vs=new X(k,[bt],Ss);I&&(vs=new Mt(Cn(bt,Tt(y)),vs));let ke=nb(Bt,x,new Os(bt,ds($,Gs(Bt,Tt(g))),vs)),xt=ve("rb_lb"),le=ve("rb_bidx"),pr=ds(yt,u1(le,Tt(l))),Ae=ds(q,l1(le,Tt(l))),St=new K(s,[pr,Ae]);if(A||w){let gs=A?Cn(pr,Tt(a)):null;w&&(gs=gs?sb(gs,Cn(Ae,Tt(i))):Cn(Ae,Tt(i))),St=new Mt(gs,St,eb())}let te=new X(N,[le],St);B&&(te=new Mt(Cn(le,Tt(b)),te));let rn=nb(xt,S,new Os(le,ds($,Gs(xt,Tt(g))),te)),Pn=ve("rb_kk"),Vr=ve("rb_fi"),hr=ve("rb_fj"),hi=Lr(Vr,h,new X(O,[Vr],new K(k,[ds(Gs(ds(Gs(W,Tt(h)),Vr),Tt(u)),Pn)]))),Qb=Lr(hr,p,new X(M,[hr],new K(N,[ds(Gs(Pn,Tt(l)),ds(Gs(G,Tt(p)),hr))]))),ss=ve("rb_mi"),mo=ve("rb_ni"),on=Lr(ss,h,Lr(mo,p,new X(R,[lt(ss,mo)],ds(new K(R,[lt(ss,mo)]),Gs(new K(O,[ss]),new K(M,[mo])))))),Om=Lr(Pn,u,new ft([hi,Qb,on])),ql=ve("rb_kt"),Nm=new Os(yt,Gs(ql,Tt(u)),new ft([ke,rn,new So,Om,new So])),Ul=nb(ql,_,Nm),Qa=ve("rb_wm"),tc=ve("rb_wn"),Wl=ds(ds(L,Gs(W,Tt(h))),Qa),Em=ds(ds(q,Gs(G,Tt(p))),tc),Tm=new X(r,[Wl,Em],new K(R,[lt(Qa,tc)]));if(v||w){let gs=v?Cn(Wl,Tt(o)):null;w&&(gs=gs?sb(gs,Cn(Em,Tt(i))):Cn(Em,Tt(i))),Tm=new Mt(gs,Tm)}let WR=Lr(Qa,h,Lr(tc,p,Tm)),jR=new ft([Y,Ul,WR]),KR=new fs(R,"local",new fs(O,"local",new fs(M,"local",jR))),HR=new Os($,ds(Gs(W,Tt(d)),G),new Os(L,Gs(P,Tt(c)),new Os(q,Gs(F,Tt(l)),KR))),YR=Math.ceil(i/l),JR=Math.ceil(o/c),ZR=Cd(P,"blockIdx.y",JR,Cd(F,"blockIdx.x",YR,Cd(W,"threadIdx.y",m,Cd(G,"threadIdx.x",d,HR))));return new fs(k,"shared",new fs(N,"shared",ZR))}function nC(n){let t=new dn("config_index",n.map((s,r)=>r)),e=new Vs("matmul_register_block_gpu",[t],(s,r,o,i)=>{let a=h1(s.func,r);if(!a)return;let c=n[i.config_index];if(!c)return;let l=sC(a,c);s.func.body=l,s.func._setChild&&s.func._setChild("body",l),s.func.gpuRegisterBlocked=!0});return e.configs=n,e.enumerate=()=>n.map((s,r)=>({config_index:r})),e}function rC(n){let t=ul(n.body),e=null;for(let r of t){let o=Ed(n,r);if(o.hasReduction&&o.spatial===2&&o.reads>=2){if(e)return null;e=r}}if(!e)return null;let s=h1(n,e);if(!s)return null;for(let r of t){if(r===e)continue;let o=de(n,r);if(!o||o.hasReduction||o.readBuffers.length>0)return null;for(let i of o.writeBuffers)if(i!==s.C.name)return null}return{reductionBlock:e,dims:s}}function d1(n,t,e){let s=rC(n);if(!s)return null;let r=p1.get(n);if(r===void 0){let o=eC(e,s.dims);r=o.length>0?nC(o):null,p1.set(n,r)}return r?t===s.reductionBlock?[r]:[]:null}var Tt,eb,ve,ds,Gs,u1,l1,Cn,sb,nb,Lr,Cd,p1,m1=T(()=>{Ct();vo();ro();Id();Td();Tt=f(n=>new C(n),"I"),eb=f(()=>new it(0),"FZERO"),ve=f(n=>new se(n,"i32"),"IV"),ds=f((n,t)=>De("+",n,t),"ADD"),Gs=f((n,t)=>De("*",n,t),"MUL"),u1=f((n,t)=>De("//",n,t),"DIV"),l1=f((n,t)=>De("%",n,t),"MOD"),Cn=f((n,t)=>new Yt("lt",n,t),"LT"),sb=f((n,t)=>new D("&&",n,t),"AND"),nb=f((n,t,e)=>new ht(n,Tt(0),Tt(t),V.SERIAL,e),"forS"),Lr=f((n,t,e)=>new ht(n,Tt(0),Tt(t),V.UNROLLED,e),"forU"),Cd=f((n,t,e,s)=>new ht(n,Tt(0),Tt(e),V.THREAD_BINDING,s,t),"forT");f(rb,"pow2Range");f(h1,"matmulTileDims");f(eC,"enumerateRegisterBlockConfigs");f(f1,"goodness");f(sC,"buildRegisterBlockedMatmul");f(nC,"createMatmulRegisterBlockGPUSketch");f(rC,"analyzePureMatmul");p1=new WeakMap;f(d1,"richMatmulSketches")});function oC(n,t){let e=[n];for(;e.length;){let s=e.pop();if(!(!s||typeof s!="object")&&(s.type==="BlockNode"&&t(s),s.body&&e.push(s.body),s.initBody&&e.push(s.initBody),s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),Array.isArray(s.stmts)))for(let r of s.stmts)e.push(r)}}function Dd(n){let t=[];oC(n.body,s=>{t.push({name:s.name,node:s,reads:(s.reads||[]).map(r=>r.buffer&&r.buffer.name).filter(Boolean),writes:(s.writes||[]).map(r=>r.buffer&&r.buffer.name).filter(Boolean)})});let e=new Map;for(let s of t)for(let r of s.reads)e.has(r)||e.set(r,[]),e.get(r).push(s.name);return{blocks:t,consumersByBuf:e,byName:new Map(t.map(s=>[s.name,s]))}}function ob(n){let t=[];for(let e of n){if(!e||e.type!=="VariableNode")return null;t.push(e.name)}return t}function ll(n,t,e){if(!n||typeof n!="object")return e;n.type==="BufferLoadNode"&&n.buffer&&n.buffer.name===t&&e.push(n);for(let s of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])n[s]&&ll(n[s],t,e);if(Array.isArray(n.args))for(let s of n.args)ll(s,t,e);if(Array.isArray(n.indices))for(let s of n.indices)ll(s,t,e);if(Array.isArray(n.stmts))for(let s of n.stmts)ll(s,t,e);return e}function za(n,t){if(!n||typeof n!="object")return t;n.type==="BufferLoadNode"&&n.buffer&&n.buffer.name&&t.add(n.buffer.name);for(let e of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])n[e]&&za(n[e],t);if(Array.isArray(n.args))for(let e of n.args)za(e,t);if(Array.isArray(n.indices))for(let e of n.indices)za(e,t);if(Array.isArray(n.stmts))for(let e of n.stmts)za(e,t);return t}function Ld(n,t,e,s){let r=t.byName.get(e);if(!r||r.writes.length!==1)return null;let o=r.writes[0],i=[...new Set((t.consumersByBuf.get(o)||[]).filter(x=>x!==e))];if(i.length!==1)return null;let a=i[0],c=s(n,e),l=s(n,a);if(!c||!l||l.hasReduction)return null;let u=t.byName.get(a);if(!u||u.writes.length!==1||u.writes[0]===o)return null;let h=new Set(r.reads);if(za(r.node.body,h),za(r.node.initBody,h),h.has(u.writes[0]))return null;let p=r.node.body&&r.node.body.type==="BufferStoreNode"?r.node.body:null,d=u.node.body&&u.node.body.type==="BufferStoreNode"?u.node.body:null;if(!p||!d)return null;let m=c.loops.filter(x=>!c.reductionLoopVars.has(x.loopVar.name)).map(x=>x.loopVar.name),g=ob(p.indices);if(!g||g.join(",")!==m.join(","))return null;let _=l.loops.map(x=>x.loopVar.name);if(_.length!==m.length)return null;let y=ob(d.indices);if(!y||y.join(",")!==_.join(","))return null;let b=ll(d.value,o,[]);if(b.length===0)return null;for(let x of b){let S=ob(x.indices);if(!S||S.join(",")!==y.join(","))return null}return a}var ib=T(()=>{f(oC,"walkBlocks");f(Dd,"buildBlockDAG");f(ob,"indexVarNames");f(ll,"collectLoads");f(za,"collectLoadedBuffers");f(Ld,"findFusibleConsumer")});function ab(n){return n.isGPU()?a1():i1()}function iC(n){return n.isGPU()?o1():r1()}function aC(n,t,e,s){let r=de(n,t);if(!r)return[ab(e)];let o=[],i=ZA(r,UA(e));if(i&&o.push(i),e.kind===fe.CPU){let a=XA(r,qA);a&&o.push(a);let c=n1(r);c&&o.push(c);let l=s?Ld(n,s,t,de):null;l&&o.push(s1(l))}return o.push(ab(e)),o}function ub(n,{priority:t=100}={}){cb.push({matches:n.matches,derive:n.derive,priority:t}),cb.sort((e,s)=>e.priority-s.priority)}function g1(n,t,e,s={}){if(s.richGpu&&e.isGPU()){let i=d1(n,t,e);if(i!==null)return i}if(e.kind!==fe.CPU&&!e.isGPU())return[];let r=Ed(n,t),o=Dd(n);for(let i of cb)if(i.matches(r,e))return i.derive(n,t,e,o);return[]}var cb,_1=T(()=>{qn();ro();Td();Q0();QA();c1();m1();ib();f(ab,"reductionSketch");f(iC,"elementwiseSketch");f(aC,"deriveMultiLevel");cb=[];f(ub,"registerSketchRule");ub({matches:f(n=>n.hasReduction&&n.spatial>=1&&n.reads>=2,"matches"),derive:aC},{priority:10});ub({matches:f(n=>n.hasReduction,"matches"),derive:f((n,t,e)=>[ab(e)],"derive")},{priority:20});ub({matches:f(()=>!0,"matches"),derive:f((n,t,e)=>[iC(e)],"derive")},{priority:30});f(g1,"deriveSketches")});function lb(n,t,e,s,r={}){return g1(n,t,e,r)}var y1=T(()=>{_1();f(lb,"getSketchesForBlock")});function Pd(n){let t=new Map,e=[n];for(;e.length>0;){let s=e.pop();if(s){if(s.type==="BlockNode"&&t.set(s.name,s),s.body&&e.push(s.body),s.stmts)for(let r of s.stmts)e.push(r);s.thenBody&&e.push(s.thenBody),s.elseBody&&e.push(s.elseBody),s.initBody&&e.push(s.initBody)}}return t}function zd(n,t,e,s=null){let r=s||Pd(n.body),o=r.get(t)||null,i=[t];if(o){let a=[];for(let l of o.reads)a.push(`${l.buffer.shape.join("x")}:${l.buffer.dtype}`);for(let l of o.writes)a.push(`${l.buffer.shape.join("x")}:${l.buffer.dtype}`);i.push(a.join(","));let c=[];if(Be(o.body,c),o.initBody&&Be(o.initBody,c),i.push(c.join(";")),o.writes.length===1){let l=o.writes[0].buffer.name,u=[];for(let h of cC(r).get(l)||[]){if(h===o)continue;let p=[];Be(h.body,p),h.initBody&&Be(h.initBody,p),u.push(`${h.name}#${p.join(";")}`)}u.length>0&&(u.sort(),i.push(`consumers:${u.join("|")}`))}}return i.push(e.name),i.push(e.kind),uC(i.join("|"))}function cC(n){if(n.__readersByBuffer)return n.__readersByBuffer;let t=new Map;for(let e of n.values()){if(!e.reads)continue;let s=new Set;for(let r of e.reads)r.buffer&&s.add(r.buffer.name);for(let r of s){let o=t.get(r);o||(o=[],t.set(r,o)),o.push(e)}}return n.__readersByBuffer=t,t}function Be(n,t){if(!(!n||typeof n!="object"))switch(n.type){case"BufferStoreNode":if(t.push("store"),n.indices)for(let e of n.indices)Be(e,t);Be(n.value,t);return;case"BufferLoadNode":if(t.push(`load:${n.buffer.name}`),n.indices)for(let e of n.indices)Be(e,t);return;case"MathOpNode":t.push(`math:${n.op}`),Be(n.a,t),n.b&&Be(n.b,t);return;case"CallExternNode":t.push(`call:${n.externName}`);for(let e of n.args)Be(e,t);return;case"CompareNode":t.push(`cmp:${n.direction}`),Be(n.a,t),Be(n.b,t);return;case"CastNode":t.push(`cast:${n.fromDtype}->${n.toDtype}`),Be(n.expr,t);return;case"IfThenElseNode":Be(n.condition,t),Be(n.thenBody,t),n.elseBody&&Be(n.elseBody,t);return;case"SeqNode":for(let e of n.stmts)Be(e,t);return;case"ForNode":Be(n.body,t);return;case"BlockNode":n.initBody&&Be(n.initBody,t),Be(n.body,t);return;case"LetStmtNode":Be(n.value,t),Be(n.body,t);return;default:return}}function uC(n){let t=2166136261;for(let e=0;e<n.length;e++)t^=n.charCodeAt(e),t=t*16777619>>>0;return t.toString(16).padStart(8,"0")}var fb=T(()=>{f(Pd,"buildBlockMap");f(zd,"computeWorkloadKey");f(cC,"readersByBuffer");f(Be,"collectBlockOps");f(uC,"fnv1a")});var b1,fl,pl,w1=T(()=>{fb();b1="mlfw-codegen-1",fl=class{static{f(this,"TuningRecord")}constructor(t,e,s,r,o,i){this.workloadKey=t,this.sketchName=e,this.params=s,this.score=r,this.traceData=o,this.version=i,this.timestamp=Date.now()}},pl=class n{static{f(this,"TuningDatabase")}constructor(t=1){this.version=t,this._records=new Map}computeWorkloadKey(t,e,s,r=null){return zd(t,e,s,r)}store(t,e){let s=this._records.get(t);s||(s=[],this._records.set(t,s)),s.push(e),s.sort((r,o)=>o.score-r.score),s.length>10&&(s.length=10)}lookup(t){let e=this._records.get(t);return!e||e.length===0?null:e[0]}lookupTopK(t,e=5){let s=this._records.get(t);return s?s.slice(0,e):[]}has(t){return this._records.has(t)&&this._records.get(t).length>0}get size(){let t=0;for(let[,e]of this._records)t+=e.length;return t}serialize(){let t=[];for(let[e,s]of this._records)for(let r of s)t.push({workloadKey:r.workloadKey,sketchName:r.sketchName,params:r.params,score:r.score,traceData:r.traceData,version:r.version,timestamp:r.timestamp});return{version:this.version,codegenVersion:b1,entries:t}}static deserialize(t){let e=new n(t.version);if(t.codegenVersion!==void 0&&t.codegenVersion!==b1)return e;for(let s of t.entries){let r=new fl(s.workloadKey,s.sketchName,s.params,s.score,s.traceData,s.version);r.timestamp=s.timestamp,e.store(s.workloadKey,r)}return e}saveToFile(t,e){return e.writeFile(t,JSON.stringify(this.serialize())),t}static loadFromFile(t,e){return e.exists(t)?n.deserialize(JSON.parse(e.readFile(t))):new n}clear(){this._records.clear()}}});function fo(n,t){if(!n)return 0;if(n.type==="ForNode"){let e=n.extent&&n.extent.type==="IntImmNode"?n.extent.value:1,s=n.threadTag?t:t*e;return fo(n.body,s)}if(n.type==="BlockNode")return Math.max(t,fo(n.body,t),n.initBody?fo(n.initBody,t):0);if(n.type==="SeqNode"){let e=t;for(let s of n.stmts)e=Math.max(e,fo(s,t));return e}return n.type==="IfThenElseNode"?Math.max(fo(n.thenBody,t),n.elseBody?fo(n.elseBody,t):t):n.type==="AllocateNode"||n.type==="LetStmtNode"?fo(n.body,t):t}function x1(n,t=.1){let e=n.slice().sort((g,_)=>g-_),s=e.length;if(s===0)return{median:0,min:0,trimmedMean:0,cv:0};let r=e[s>>1],o=e[0],i=Math.floor(s*t),a=i,c=s-i>a?s-i:s,l=0,u=0;for(let g=a;g<c;g++)l+=e[g],u++;let h=u>0?l/u:r,p=0;for(let g=a;g<c;g++){let _=e[g]-h;p+=_*_}let d=u>1?Math.sqrt(p/(u-1)):0,m=h>0?d/h:0;return{median:r,min:o,trimmedMean:h,cv:m}}var lC,$d,Fd,S1=T(()=>{Oc();lC=1e6;f(fo,"maxSerialTripCount");$d=class{static{f(this,"BenchmarkResult")}constructor(t,e,s,r,o=null,i=0){this.medianMs=t,this.minMs=e,this.trimmedMeanMs=o??t,this.cv=i,this.samples=s,this._totalBytes=r}get throughputGBs(){return this.minMs<=0||!this._totalBytes?0:this._totalBytes/(this.minMs*1e6)}};f(x1,"robustStats");Fd=class{static{f(this,"BenchmarkRunner")}constructor(t,e={}){this.target=t,this.warmup=e.warmup??3,this.repeat=e.repeat??10,this.minRepeatMs=e.minRepeatMs??0,this.maxCv=e.maxCv??0,this.maxReMeasures=e.maxReMeasures??1,this.measurer=e.measurer||null,this._bufferCache=new Map}_getOrAllocBuffers(t){let e=0,s=[];for(let[,i]of t.bufferMap){let a=Math.max(i.numel(),1);s.push(a);let c=i.sizeInBytes();c>0&&(e+=c)}let r=s.join(","),o=this._bufferCache.get(r);o||(o=s.map(i=>new Float32Array(i)),this._bufferCache.set(r,o));for(let i of o)for(let a=0;a<i.length;a++)i[a]=Math.random()*2-1;return{buffers:o,totalBytes:e}}run(t){if(!t||!t.body)return null;if(!this.target.isCPU())return this.measurer?this._runMeasured(t):null;let e=new Wn(this.target),s;try{s=e.compile(t)}catch{return null}if(s.metadata.kind!=="js")return null;let r;try{r=new Function("return "+s.source)()}catch{return null}let{buffers:o,totalBytes:i}=this._getOrAllocBuffers(t);for(let l=0;l<this.warmup;l++)try{r(...o)}catch{return null}let a=[],c=null;for(let l=0;l<=this.maxReMeasures&&(this._collect(r,o,a),c=x1(a),!(this.maxCv<=0||c.cv<=this.maxCv));l++);return new $d(c.median,c.min,a,i,c.trimmedMean,c.cv)}_runMeasured(t){if(t.shapeParams&&t.shapeParams.length>0||fo(t.body,1)>lC)return null;let e;try{e=new Wn(this.target).compile(t)}catch{return null}let s=[],r=0;for(let[,a]of t.bufferMap){let c=Math.max(a.sizeInBytes(),1);s.push(c),r+=c}let o;try{o=this.measurer(e,s,[],{warmup:this.warmup,repeat:this.repeat})}catch{return null}if(!o||o.length===0)return null;let i=x1(o);return new $d(i.median,i.min,o,r,i.trimmedMean,i.cv)}_collect(t,e,s){let r=0,o=this.repeat*3;for(let i=0;i<o&&(i<this.repeat||r<this.minRepeatMs);i++){let a=performance.now();t(...e);let c=performance.now()-a;s.push(c),r+=c}}}});var Vd,v1=T(()=>{Vd=class{static{f(this,"Deadline")}constructor(t=1/0,e=null){this._clock=e||(()=>performance.now()),this._endAt=t===1/0||t==null?1/0:this._clock()+t}get expired(){return this._clock()>=this._endAt}remainingMs(){return this._endAt===1/0?1/0:Math.max(0,this._endAt-this._clock())}}});function Pr(n){let t=f(e=>{if(!e||typeof e!="object")return e;if(Array.isArray(e))return e.map(t);let s=Object.create(Object.getPrototypeOf(e));switch(s.type=e.type,s._parent=null,s._parentKey=null,s._parentIdx=-1,e.type){case"PrimFunc":s.name=e.name,s.params=e.params,s.body=t(e.body),s.bufferMap=new Map(e.bufferMap),s.shapeParams=e.shapeParams,s.shapeParamMap=e.shapeParamMap instanceof Map?new Map(e.shapeParamMap):e.shapeParamMap,s._setChild("body",s.body);break;case"ForNode":s.loopVar=e.loopVar,s.min=t(e.min),s.extent=t(e.extent),s.kind=e.kind,s.body=t(e.body),s.threadTag=e.threadTag,s._setChild("body",s.body);break;case"BlockNode":s.name=e.name,s.iterVars=e.iterVars.map(t),s.reads=e.reads,s.writes=e.writes,s.body=t(e.body),s.initBody=t(e.initBody),s._setChild("body",s.body),s._setChild("initBody",s.initBody);break;case"SeqNode":s.stmts=e.stmts.map(t),s._setChildren("stmts",s.stmts);break;case"AllocateNode":s.buffer=e.buffer,s.scope=e.scope,s.body=t(e.body),s._setChild("body",s.body);break;case"LetStmtNode":s.variable=e.variable,s.value=t(e.value),s.body=t(e.body),s._setChild("body",s.body);break;case"IfThenElseNode":s.condition=t(e.condition),s.thenBody=t(e.thenBody),s.elseBody=t(e.elseBody),s._setChild("thenBody",s.thenBody),s._setChild("elseBody",s.elseBody);break;case"WhileNode":s.condVar=e.condVar,s.condBody=t(e.condBody),s.loopBody=t(e.loopBody),s._setChild("condBody",s.condBody),s._setChild("loopBody",s.loopBody);break;default:for(let r of Object.keys(e)){if(r==="_parent"||r==="_parentKey"||r==="_parentIdx")continue;let o=e[r];o instanceof Map?s[r]=new Map(o):Array.isArray(o)?s[r]=o.map(t):typeof o=="object"&&o!==null&&o.type?s[r]=t(o):s[r]=o}break}return s},"cloneNode");return t(n)}function pb(n){let t=f(e=>{if(!e||typeof e!="object")return e;if(Array.isArray(e))return e.map(t);let s=Object.create(Object.getPrototypeOf(e));s.type=e.type,s._parent=null,s._parentKey=null,s._parentIdx=-1;for(let r of Object.keys(e)){if(r==="_parent"||r==="_parentKey"||r==="_parentIdx")continue;let o=e[r];o instanceof Map?s[r]=new Map(o):Array.isArray(o)?s[r]=o.map(t):typeof o=="object"&&o!==null&&o.type?s[r]=t(o):s[r]=o}return s._setChild&&(s.body&&s._setChild("body",s.body),s.initBody&&s._setChild("initBody",s.initBody),s.thenBody&&s._setChild("thenBody",s.thenBody),s.elseBody&&s._setChild("elseBody",s.elseBody)),s._setChildren&&s.stmts&&s._setChildren("stmts",s.stmts),s},"cloneNode");return t(n)}function B1(n,t,e){let s=e.get(t);if(!s)return null;let r=[],o=s._parent;for(;o&&o!==n;)o.type==="ForNode"&&r.push(o),o=o._parent;r.reverse();let i=pb(s);for(let l=r.length-1;l>=0;l--){let u=r[l],h=new ht(u.loopVar,pb(u.min),pb(u.extent),u.kind,i,u.threadTag);h._setChild("body",i),i=h}let a=new Map;for(let l of s.reads)a.set(l.buffer.name,l.buffer);for(let l of s.writes)a.set(l.buffer.name,l.buffer);let c=[];for(let l of n.params)a.has(l.name)&&c.push(l);return new jr("__tune_"+t,c,i,a,[])}var hb=T(()=>{Ct();f(Pr,"clonePrimFunc");f(pb,"cloneBlockSubtree");f(B1,"extractBlockMini")});function pC(n,t,e){let s={...n};for(let r of t.variables)e.rngFloat()<e.mutationRate&&(s[r.name]=r.sample(o=>e.rng(o)));return s}function k1(n={}){return n.strategy==="random"?new db({numTrials:n.numTrials,seed:n.seed,deadline:n.deadline}):new mb({populationSize:n.populationSize,numGenerations:n.numGenerations,mutationRate:n.mutationRate,eliteRatio:n.eliteRatio,seed:n.seed,deadline:n.deadline})}var Gd,db,mb,fC,A1=T(()=>{Gd=class{static{f(this,"SearchCandidate")}constructor(t,e,s){this.sketchName=t,this.params=e,this.score=s}},db=class{static{f(this,"RandomSearch")}constructor(t={}){this.numTrials=t.numTrials||64,this.seed=t.seed||42,this.deadline=t.deadline||null,this._rngState=this.seed}_rng(t){return this._rngState=this._rngState*1664525+1013904223&2147483647,this._rngState%t}_expired(){return this.deadline?this.deadline.expired:!1}search(t,e){let s=[];for(let r of t){if(this._expired())break;for(let o=0;o<this.numTrials&&!this._expired();o++){let i=r.sampleParams(c=>this._rng(c)),a=e(r,i);a&&s.push(new Gd(r.name,i,a.score))}}return s.sort((r,o)=>o.score-r.score),{candidates:s,population:null}}},mb=class{static{f(this,"EvolutionarySearch")}constructor(t={}){this.populationSize=t.populationSize||32,this.numGenerations=t.numGenerations||10,this.mutationRate=t.mutationRate||.3,this.eliteRatio=t.eliteRatio||.2,this.seed=t.seed||42,this.deadline=t.deadline||null,this._rngState=this.seed}_rng(t){return this._rngState=this._rngState*1664525+1013904223&2147483647,this._rngState%t}_rngFloat(){return this._rngState=this._rngState*1664525+1013904223&2147483647,this._rngState/2147483647}_expired(){return this.deadline?this.deadline.expired:!1}search(t,e,s=null){let r=s&&s.length?s:this._initPopulation(t),o=new Map,i=f((c,l)=>{let u=c.name+"|"+JSON.stringify(l);if(o.has(u))return o.get(u);let h=e(c,l);return o.set(u,h),h},"evalMemo");for(let c=0;c<this.numGenerations&&!this._expired();c++){let l=[];for(let d of r){let m=i(d.sketch,d.params);m&&l.push({...d,score:m.score})}if(l.sort((d,m)=>m.score-d.score),l.length===0)break;let u=Math.max(1,Math.floor(l.length*this.eliteRatio)),h=l.slice(0,u),p=h.map(d=>({sketch:d.sketch,params:{...d.params}}));for(;p.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;p.push(this._mutate(g.sketch,{...g.params}))}else p.push(this._mutate(d.sketch,this._crossover(d,m)))}r=p}let a=[];for(let c of r){let l=i(c.sketch,c.params);l&&a.push(new Gd(c.sketch.name,c.params,l.score))}return a.sort((c,l)=>l.score-c.score),{candidates:a,population:r}}_initPopulation(t){let e=[];for(let s=0;s<this.populationSize;s++){let r=t[this._rng(t.length)],o=r.sampleParams(i=>this._rng(i));e.push({sketch:r,params:o})}return e}_crossover(t,e){if(t.sketch!==e.sketch)return{...t.params};let s={};for(let r of t.sketch.variables)s[r.name]=this._rngFloat()<.5?t.params[r.name]:e.params[r.name];return s}_mutate(t,e){let s={rngFloat:f(()=>this._rngFloat(),"rngFloat"),rng:f(i=>this._rng(i),"rng"),mutationRate:this.mutationRate},r=[pC,...fC],o=e;for(let i of r)o=i(o,t,s);return{sketch:t,params:o}}},fC=[];f(pC,"defaultResampleMutator");f(k1,"createSearchStrategy")});function Ud(n){let t={"threadIdx.x":1,"threadIdx.y":1,"threadIdx.z":1},e=new Set,s=f(r=>{if(!(!r||typeof r!="object"||e.has(r))){if(e.add(r),r.type==="ForNode"&&t[r.threadTag]!==void 0){let o=r.extent&&r.extent.type==="IntImmNode"?r.extent.value:1;o>t[r.threadTag]&&(t[r.threadTag]=o)}for(let o in r){let i=r[o];Array.isArray(i)?i.forEach(s):i&&typeof i=="object"&&s(i)}}},"visit");return s(n.body),hC.reduce((r,o)=>r*t[o],1)}var hC,qd,R1=T(()=>{lu();Ny();H0();hb();A1();Z0();hC=["threadIdx.x","threadIdx.y","threadIdx.z"];f(Ud,"gpuThreadBlockSize");qd=class{static{f(this,"BlockTuningSession")}constructor(t){this.target=t.target,this.primFunc=t.primFunc,this.blockName=t.blockName,this.sketches=t.sketches,this.benchmarkRunner=t.benchmarkRunner||null,this.config=t.config,this.deadline=t.deadline||null;let e=this.sketches.some(o=>o.name==="fused");e?(this.learnedModel=new La,this.costModel=new Pa(t.costModel.analytical,this.learnedModel)):(this.costModel=t.costModel,this.learnedModel=t.learnedModel);let s=e?null:B1(t.primFunc,t.blockName,t.blockMap);this.evalFunc=s||t.primFunc,this.evalBlockName=t.blockName,this.sketchByName=new Map;for(let o of this.sketches)this.sketchByName.set(o.name,o);let r=this.sketches.length===1&&typeof this.sketches[0].enumerate=="function";this.enumSketch=r?this.sketches[0]:null,this.enumParams=r?this.sketches[0].enumerate():null,this.strategy=k1({...this.config,deadline:this.deadline}),this.population=null,this._best=null,this.plateaued=!1}runRound(){let t=this._best?this._best.measuredScore:-1/0,e=this._produceCandidates();if(e.length===0)return this.plateaued=!0,0;if(this.benchmarkRunner)this._measureAndLearn(e);else{let r=e[0];this._consider({sketchName:r.sketchName,params:r.params,score:r.score,measuredScore:r.score}),this.plateaued=!0}let s=this._best?this._best.measuredScore:-1/0;return Math.max(0,s-t)}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 t=this.sketchByName.get(this._best.sketchName);if(!t)return null;try{let e=new Ts(Pr(this.primFunc));return t.instantiate(this._best.params)(e,this.blockName,this.target),e.trace.serialize()}catch{return null}}_produceCandidates(){if(this.enumSketch){let r=[];for(let o of this.enumParams){let i=this._evaluate(this.enumSketch,o);i&&r.push({sketchName:this.enumSketch.name,params:o,score:i.score})}return r.sort((o,i)=>i.score-o.score),r}let t=f((r,o)=>this._evaluate(r,o),"evaluator"),{candidates:e,population:s}=this.strategy.search(this.sketches,t,this.population);return this.population=s,e}_evaluate(t,e){try{let s=Pr(this.evalFunc),r=new Ts(s);if(t.instantiate(e)(r,this.evalBlockName,this.target),aa.validate(s).length>0)return null;let i=this.target.maxThreadsPerBlock;return this.target.isGPU&&this.target.isGPU()&&i&&Ud(s)>i?null:{score:this.costModel.score(s)}}catch{return null}}_measureAndLearn(t){let e=t.slice(0,this.config.topKForBenchmark);for(let s of e){if(this.deadline&&this.deadline.expired)break;let r=this._measure(s);if(!r)continue;let o=-r.result.medianMs;this.learnedModel.addSample(r.features,o),this._consider({sketchName:s.sketchName,params:s.params,score:o,measuredScore:o,medianMs:r.result.medianMs,minMs:r.result.minMs})}this.learnedModel.train()}_measure(t){let e=this.sketchByName.get(t.sketchName);if(!e)return null;let s,r;try{s=Pr(this.primFunc),e.instantiate(t.params)(new Ts(s),this.blockName,this.target),r=Pr(this.evalFunc),e.instantiate(t.params)(new Ts(r),this.evalBlockName,this.target)}catch{return null}let o=this.benchmarkRunner.run(s);return o?{result:o,features:ci.extractStatements(r)}:null}_consider(t){(!this._best||t.measuredScore>this._best.measuredScore)&&(this._best=t)}}});var gb,Wd,O1=T(()=>{gb=class{static{f(this,"GradientSchedulerPolicy")}pick(t){let e=t.filter(i=>!i.plateaued);if(e.length===0)return null;let s=e.filter(i=>i.rounds===0);if(s.length>0)return s[0];let r=null,o=-1/0;for(let i of e){let a=i.gainEwma!==void 0?i.gainEwma:i.lastGain,c=i.weight*a;c>o&&(o=c,r=i)}return r}},Wd=class{static{f(this,"TaskScheduler")}constructor(t=null){this.policy=t||new gb}run(t,e,s={}){let r=s.maxRoundsPerTask??8,o=s.plateauPatience??2,i=s.gainEwmaAlpha??.5;for(let a of t)a.rounds=0,a.lastGain=0,a.gainEwma=0,a.stale=0,a.plateaued=!1;for(;!(e&&e.expired);){let a=this.policy.pick(t);if(!a)break;let c=a.session.runRound();a.rounds++,a.lastGain=c,a.gainEwma=i*c+(1-i)*a.gainEwma,c<=0?a.stale++:a.stale=0,(a.session.plateaued||a.stale>=o||a.rounds>=r)&&(a.plateaued=!0)}}}});function dC(n){if(n.isCPU())return null;let t=uS(n.kind);if(!t)throw new Error("hardwareMeasure requested for target '"+n.kind+"' but no measurer is registered for it; the corresponding runtime must be loaded (Node: import '#io/cuda_runtime') before compiling");return t}var jd,Kd,N1=T(()=>{lu();Z0();y1();w1();S1();v1();fb();Td();ib();ro();R1();hb();Ct();O1();rp();f(dC,"resolveMeasurer");jd=class{static{f(this,"AutotuneConfig")}constructor(t={}){this.strategy=t.strategy||"evolutionary",this.numTrials=t.numTrials||64,this.populationSize=t.populationSize||32,this.numGenerations=t.numGenerations||10,this.mutationRate=t.mutationRate,this.eliteRatio=t.eliteRatio,this.seed=t.seed||42,this.timeBudgetMs=t.timeBudgetMs||3e4,this.clock=t.clock||null,this.tuningDB=t.tuningDB||null,this.useTuningDB=t.useTuningDB!==!1,this.measurer=t.measurer||null,this.hardwareMeasure=t.hardwareMeasure??!1,this.enableBenchmark=t.enableBenchmark??(this.hardwareMeasure||!!t.measurer),this.benchmarkWarmup=t.benchmarkWarmup??3,this.benchmarkRepeat=t.benchmarkRepeat??10,this.benchmarkMaxCv=t.benchmarkMaxCv??0,this.topKForBenchmark=t.topKForBenchmark??5,this.maxRoundsPerTask=t.maxRoundsPerTask??8,this.plateauPatience=t.plateauPatience??2,this.schedulerPolicy=t.schedulerPolicy||null}},Kd=class{static{f(this,"Autotuner")}constructor(t,e={}){this.target=t,this.config=e instanceof jd?e:new jd(e),this.config.hardwareMeasure&&(this.config.measurer=dC(t)),this.analyticalModel=new Nd(t),this.learnedModel=new La,this.costModel=new Pa(this.analyticalModel,this.learnedModel),this.db=this.config.tuningDB instanceof pl?this.config.tuningDB:new pl,this.benchmarkRunner=this.config.enableBenchmark?new Fd(t,{warmup:this.config.benchmarkWarmup,repeat:this.config.benchmarkRepeat,maxCv:this.config.benchmarkMaxCv,measurer:this.config.measurer}):null,this.scheduler=new Wd(this.config.schedulerPolicy)}tune(t,e=null){let s=e?[e]:ul(t.body),r=Pd(t.body),o=new Vd(this.config.timeBudgetMs,this.config.clock),i=new Map,a=new Map;for(let u of s){let h=zd(t,u,this.target,r);a.set(u,h);let p=i.get(h);if(p){p.weight++;continue}if(this.config.useTuningDB&&this.db.has(h)){i.set(h,{key:h,kind:"cache",cached:this.db.lookup(h),weight:1});continue}let d=lb(t,u,this.target,r,{richGpu:this.config.richGpu??!!this.config.measurer});if(d.length===0){i.set(h,{key:h,kind:"empty",weight:1});continue}let m=new qd({target:this.target,primFunc:t,blockName:u,blockMap:r,sketches:d,costModel:this.costModel,learnedModel:this.learnedModel,benchmarkRunner:this.benchmarkRunner,config:this.config,deadline:o});i.set(h,{key:h,kind:"session",session:m,weight:1})}let c=[...i.values()].filter(u=>u.kind==="session");c.length>0&&this.scheduler.run(c,o,this.config);let l=new Map;for(let u of s){let h=i.get(a.get(u));if(h.kind==="cache"){l.set(u,{sketchName:h.cached.sketchName,params:h.cached.params,score:h.cached.score,fromCache:!0});continue}if(h.kind==="empty")continue;let p=h.session.best();if(p){if(this.config.useTuningDB&&!h.stored){let d=new fl(h.key,p.sketchName,p.params,p.score,h.session.bestTrace(),this.db.version);d.medianMs=p.medianMs||null,d.minMs=p.minMs||null,this.db.store(h.key,d),h.stored=!0}l.set(u,{sketchName:p.sketchName,params:p.params,score:p.score,fromCache:!1,medianMs:p.medianMs,minMs:p.minMs})}}return l}tuneAndApply(t,e=null){let s=this.tune(t,e);if(s.size>0){let r=this._applyBestSchedule(t,s);if(r)return{func:r.func,results:s,applied:!0}}return{func:t,results:s,applied:!1}}_applyBestSchedule(t,e){let s=this._buildTunedSchedule(t,e);if(s&&this._scheduleIsValid(s))return this._adoptSchedule(t,s),{func:t};let r=this._buildDefaultSchedule(t);return r?(this._adoptSchedule(t,r),{func:t}):s?(this._adoptSchedule(t,s),{func:t}):null}_buildTunedSchedule(t,e){try{let s=Pr(t),r=new Ts(s),o=Pd(s.body),i=Dd(s),a=new Set,c=[];for(let u of e)if(u[1].sketchName==="fused"){let h=Ld(s,i,u[0],de);h&&a.add(h),c.unshift(u)}else c.push(u);let l=new Set;for(let[u,h]of c)if(!(a.has(u)||l.has(h))&&(l.add(h),!(!h.sketchName||!h.params)))try{let d=lb(s,u,this.target,o,{richGpu:this.config.richGpu??!!this.config.measurer}).find(m=>m.name===h.sketchName);d&&this._fitsThreadBlock(s,u,d,h.params)&&d.instantiate(h.params)(r,u,this.target)}catch{continue}return this._scheduleResidualBlocks(r,a),s}catch{return null}}_scheduleResidualBlocks(t,e){let s=null;for(let r of ul(t.func.body))if(!(e.has(r)||this._blockIsParallelized(t,r))){s||(s=new Cr(this.target));try{s.applyToBlock(t,r)}catch{}}}_blockIsParallelized(t,e){let s;try{s=t.getLoops(e)}catch{return!0}for(let r of s)if(r.kind===V.THREAD_BINDING||r.kind===V.PARALLEL||r.kind===V.VECTORIZED)return!0;return!1}_scheduleIsValid(t){return!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock?!0:Ud(t)<=this.target.maxThreadsPerBlock}_buildDefaultSchedule(t){try{let e=Pr(t);return new Cr(this.target).applyToAllBlocks(new Ts(e)),e}catch{return null}}_adoptSchedule(t,e){Object.assign(t,e),t._setChild("body",t.body)}_fitsThreadBlock(t,e,s,r){if(!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock)return!0;try{let o=Pr(t);return s.instantiate(r)(new Ts(o),e,this.target),Ud(o)<=this.target.maxThreadsPerBlock}catch{return!1}}}});var Hd,E1=T(()=>{Ct();Hd=class{static{f(this,"TensorVerifier")}constructor(){this.errors=[],this.boundVars=new Set}verify(t){if(this.errors=[],this.boundVars.clear(),!(t instanceof jr))return this.errors.push("Expected PrimFunc at root"),this.errors;for(let e of t.params)this.boundVars.add(e.name);if(t.shapeParams)for(let e of t.shapeParams)this.boundVars.add(e.name);return this.visit(t.body),this.errors}visit(t){let e=[t];for(;e.length>0;){let s=e.pop();if(s)switch(s.type){case"SeqNode":for(let r=s.stmts.length-1;r>=0;r--)e.push(s.stmts[r]);break;case"ForNode":this.boundVars.has(s.loopVar.name)&&this.errors.push(`Loop variable ${s.loopVar.name} already bound`),this.boundVars.add(s.loopVar.name),e.push({type:"_unbind",name:s.loopVar.name}),e.push(s.body);break;case"BlockNode":for(let r of s.iterVars)r.iterVar&&(this.boundVars.has(r.iterVar.name)&&this.errors.push(`Block variable ${r.iterVar.name} already bound`),this.boundVars.add(r.iterVar.name));for(let r=s.iterVars.length-1;r>=0;r--)s.iterVars[r].iterVar&&e.push({type:"_unbind",name:s.iterVars[r].iterVar.name});e.push(s.body),s.initBody&&e.push(s.initBody);break;case"AllocateNode":s.buffer||this.errors.push("Allocate missing buffer"),e.push(s.body);break;case"LetStmtNode":this._visitExpr(s.value),this.boundVars.add(s.variable.name),e.push({type:"_unbind",name:s.variable.name}),e.push(s.body);break;case"IfThenElseNode":this._visitExpr(s.condition),s.elseBody&&e.push(s.elseBody),e.push(s.thenBody);break;case"WhileNode":s.condVar||this.errors.push("WhileNode missing condition variable"),e.push(s.loopBody),e.push(s.condBody);break;case"BufferStoreNode":if(s.buffer||this.errors.push("BufferStore missing buffer"),(!s.indices||s.indices.length!==s.buffer.shape.length)&&this.errors.push(`BufferStore rank mismatch for ${s.buffer?s.buffer.name:"unknown"}`),s.indices)for(let r of s.indices)this._visitExpr(r);this._visitExpr(s.value);break;case"EvaluateNode":this._visitExpr(s.value);break;case"_unbind":this.boundVars.delete(s.name);break;default:this._visitExpr(s);break}}}_visitExpr(t){if(t)switch(t.type){case"BufferLoadNode":if(t.buffer||this.errors.push("BufferLoad missing buffer"),(!t.indices||t.indices.length!==t.buffer.shape.length)&&this.errors.push(`BufferLoad rank mismatch for ${t.buffer?t.buffer.name:"unknown"}`),t.indices)for(let e of t.indices)this._visitExpr(e);break;case"MathOpNode":this._visitExpr(t.a),t.b&&this._visitExpr(t.b);break;case"CompareNode":this._visitExpr(t.a),this._visitExpr(t.b);break;case"CastNode":this._visitExpr(t.expr);break;case"CallExternNode":t.externName||this.errors.push("CallExtern missing function name");for(let e of t.args)this._visitExpr(e);break;case"IfThenElseNode":this._visitExpr(t.condition),this._visitExpr(t.thenBody),t.elseBody&&this._visitExpr(t.elseBody);break;case"VariableNode":this.boundVars.has(t.name)||this.errors.push(`Unbound variable used: ${t.name}`);break;case"BlockRealizeNode":t.binding&&this._visitExpr(t.binding);break;case"IntImmNode":case"FloatImmNode":break}}}});function _b(n){let t=[];n.functionCount===0&&t.push(new me("Module has no functions"));for(let e of n)hl(e,t);return t}function hl(n,t=[]){if(!n.entryBlock)return t.push(new me("Function has no entry block",null,n)),t;n.entryBlock.arguments.length!==n.inputTypes.length&&t.push(new me(`Entry block has ${n.entryBlock.arguments.length} args, expected ${n.inputTypes.length}`,null,n));for(let r=0;r<n.entryBlock.arguments.length;r++){let o=n.entryBlock.arguments[r],i=n.inputTypes[r];i&&!o.type.equals(i)&&t.push(new me(`Block arg ${r} type ${us(o.type)} != expected ${us(i)}`,null,n))}let e=new Set;for(let r of n.entryBlock.arguments)e.add(r);T1(n.body,e);for(let r of n.body)I1(r,n,e,t);let s=n.getReturnOp();if(!s)t.push(new me("Missing return op",null,n));else{s.numOperands!==n.outputTypes.length&&t.push(new me(`Return has ${s.numOperands} operands, function declares ${n.outputTypes.length} outputs`,s,n));for(let r=0;r<Math.min(s.numOperands,n.outputTypes.length);r++){let o=s.getOperand(r).type,i=n.outputTypes[r];i instanceof E&&o instanceof E&&(o.dtype!==i.dtype&&t.push(new me(`Return operand ${r} dtype ${o.dtype} != expected ${i.dtype}`,s,n)),o.shapeCompatible(i)||t.push(new me(`Return operand ${r} shape incompatible: ${us(o)} vs ${us(i)}`,s,n)))}}return t}function T1(n,t){for(let e of n){for(let s of e.arguments)t.add(s);for(let s of e){for(let r=0;r<s.numResults;r++)t.add(s.getResult(r));for(let r of s.regions)T1(r,t)}}}function I1(n,t,e,s){for(let r of n)mC(r,t,e,s);if(n.size>0){let r=n.lastOp;if(n.parentRegion&&n.parentRegion.parentOp){let o=n.parentRegion.parentOp.opName,i=st.get(o);i&&i.hasRegions&&(r.isTerminator()||s.push(new me(`Block in ${o} region must end with terminator, got '${r.opName}'`,r,t)))}}}function mC(n,t,e,s){for(let o=0;o<n.numOperands;o++){let i=n.getOperand(o);if(!i){s.push(new me(`Operand ${o} is null`,n,t));continue}if(!(i instanceof yo)){s.push(new me(`Operand ${o} is not a Value`,n,t));continue}e.has(i)||s.push(new me(`Operand ${o} used before definition`,n,t))}for(let o=0;o<n.numResults;o++){let i=n.getResult(o);i||s.push(new me(`Result ${o} is null`,n,t)),i&&i.definingOp!==n&&s.push(new me(`Result ${o} definingOp mismatch`,n,t))}let r=st.get(n.opName);if(!r){!["return","yield"].includes(n.opName)&&!st.has(n.opName)&&s.push(new me(`Unknown op '${n.opName}'`,n,t));return}r.numOperands>=0&&n.numOperands!==r.numOperands&&s.push(new me(`'${n.opName}' expects ${r.numOperands} operands, got ${n.numOperands}`,n,t)),r.numResults>=0&&n.numResults!==r.numResults&&s.push(new me(`'${n.opName}' expects ${r.numResults} results, got ${n.numResults}`,n,t));for(let o of r.attrs)o.required&&!n.hasAttr(o.name)&&s.push(new me(`'${n.opName}' missing required attribute '${o.name}'`,n,t));if(r.hasRegions&&r.numRegions>0&&n.numRegions!==r.numRegions&&s.push(new me(`'${n.opName}' expects ${r.numRegions} regions, got ${n.numRegions}`,n,t)),r.verify){let o=r.verify(n);if(o)for(let i of o)s.push(new me(i,n,t))}if(r.inferResultTypes&&n.numResults>0){let o=[];for(let a=0;a<n.numOperands;a++)o.push(n.getOperand(a).type);let i=r.inferResultTypes(o,n.attributes,n.results.map(a=>a.type));if(i)for(let a=0;a<Math.min(i.length,n.numResults);a++){let c=n.getResult(a).type,l=i[a];l instanceof E&&c instanceof E&&(c.dtype!==l.dtype&&s.push(new me(`Result ${a} dtype ${c.dtype} != inferred ${l.dtype}`,n,t)),c.shapeCompatible(l)||s.push(new me(`Result ${a} shape ${us(c)} incompatible with inferred ${us(l)}`,n,t)))}}for(let o of n.regions){let i=new Set(e);for(let a of o){for(let c of a.arguments)i.add(c);I1(a,t,i,s)}}}var me,M1=T(()=>{kt();Xl();Ee();me=class{static{f(this,"VerificationError")}constructor(t,e=null,s=null){this.message=t,this.op=e,this.func=s}toString(){let t="";return this.func&&(t+=`[${this.func.name}] `),this.op&&(t+=`op '${this.op.opName}' (id=${this.op.id}): `),t+this.message}};f(_b,"verifyModule");f(hl,"verifyFunction");f(T1,"collectDefinedValues");f(I1,"verifyBlock");f(mC,"verifyOperation")});var Yd,yb,Jd,bb,C1=T(()=>{F0();kt();Yd=class{static{f(this,"ValueObserver")}constructor(){this.min=1/0,this.max=-1/0,this.count=0,this.histogram=null}update(t){for(let e=0;e<t.length;e++){let s=t[e];s<this.min&&(this.min=s),s>this.max&&(this.max=s)}this.count+=t.length,this.histogram&&this.histogram.update(t)}enableHistogram(t=2048){this.histogram=new yb(t)}},yb=class{static{f(this,"HistogramCollector")}constructor(t=2048){this.numBins=t,this.bins=new Float64Array(t),this.rangeMin=0,this.rangeMax=0,this.initialized=!1,this.totalCount=0}update(t){this.initialized||this._initRange(t);let e=this.rangeMax-this.rangeMin;if(e<=0)return;for(let r=0;r<t.length;r++){let o=t[r];if(o<this.rangeMin||o>this.rangeMax){this._expandAndRebucket(t);return}}let s=this.numBins/e;for(let r=0;r<t.length;r++){let o=Math.min(this.numBins-1,Math.floor((t[r]-this.rangeMin)*s));this.bins[o]++}this.totalCount+=t.length}_initRange(t){let e=1/0,s=-1/0;for(let r=0;r<t.length;r++)t[r]<e&&(e=t[r]),t[r]>s&&(s=t[r]);e===s&&(s=e+1),this.rangeMin=e,this.rangeMax=s,this.initialized=!0}_expandAndRebucket(t){let e=this.rangeMin,s=this.rangeMax;for(let u=0;u<t.length;u++)t[u]<e&&(e=t[u]),t[u]>s&&(s=t[u]);let r=new Float64Array(this.bins),o=this.rangeMin,a=this.rangeMax-o;this.rangeMin=e,this.rangeMax=s;let c=s-e,l=this.numBins/c;if(this.bins.fill(0),a>0){let u=a/this.numBins;for(let h=0;h<this.numBins;h++){if(r[h]===0)continue;let p=o+(h+.5)*u,d=Math.min(this.numBins-1,Math.floor((p-e)*l));this.bins[d]+=r[h]}}for(let u=0;u<t.length;u++){let h=Math.min(this.numBins-1,Math.floor((t[u]-e)*l));this.bins[h]++}this.totalCount+=t.length}computePercentileThreshold(t){let e=this.totalCount*t,s=0,r=(this.rangeMax-this.rangeMin)/this.numBins;for(let o=0;o<this.numBins;o++)if(s+=this.bins[o],s>=e)return this.rangeMin+(o+1)*r;return this.rangeMax}computeEntropyThreshold(t){if(this.totalCount===0)return this.rangeMax;let e=new Float64Array(this.numBins);for(let i=0;i<this.numBins;i++)e[i]=this.bins[i]/this.totalCount;let s=this.numBins,r=1/0;for(let i=t;i<=this.numBins;i++){let a=i/t,c=new Float64Array(i);for(let u=0;u<t;u++){let h=Math.floor(u*a),p=Math.min(i,Math.floor((u+1)*a)),d=0;for(let g=h;g<p;g++)d+=e[g];let m=p-h;if(m>0){let g=d/m;for(let _=h;_<p;_++)c[_]=g}}let l=0;for(let u=0;u<i;u++)e[u]>0&&c[u]>0&&(l+=e[u]*Math.log(e[u]/c[u]));l<r&&(r=l,s=i)}let o=(this.rangeMax-this.rangeMin)/this.numBins;return this.rangeMin+s*o}},Jd=class{static{f(this,"CalibrationCollector")}constructor(t="minmax"){this.observers=new Map,this.mode=t}attach(t){for(let e of t.ops())for(let s=0;s<e.numResults;s++){let r=e.getResult(s);if(r.type instanceof E&&Oe(r.type.dtype)){let o=new Yd;(this.mode==="entropy"||this.mode==="percentile")&&o.enableHistogram(),this.observers.set(r,o)}}for(let e of t.args)if(e.type instanceof E&&Oe(e.type.dtype)){let s=new Yd;(this.mode==="entropy"||this.mode==="percentile")&&s.enableHistogram(),this.observers.set(e,s)}}observe(t,e){let s=this.observers.get(t);s&&s.update(e)}getResult(){return new bb(this.observers,this.mode)}},bb=class{static{f(this,"CalibrationResult")}constructor(t,e){this._observers=t,this._mode=e}getRange(t){let e=this._observers.get(t);return!e||e.count===0?null:{min:e.min,max:e.max}}getQuantParams(t,e,s){let r=this.getRange(t);if(!r)return null;let o=e===$s.PER_TENSOR_ASYMMETRIC;if(this._mode==="percentile"&&this._observers.get(t).histogram){if(o)return Fs.fromRange(r.min,r.max,e,s);let a=this._observers.get(t).histogram.computePercentileThreshold(.999);return Fs.fromRange(-a,a,e,s)}if(this._mode==="entropy"&&this._observers.get(t).histogram){if(o)return Fs.fromRange(r.min,r.max,e,s);let i=this._observers.get(t).histogram,a=s==="ui8"?256:255,c=i.computeEntropyThreshold(a);return Fs.fromRange(-c,c,e,s)}return Fs.fromRange(r.min,r.max,e,s)}hasData(t){let e=this._observers.get(t);return e&&e.count>0}values(){return this._observers.keys()}}});var wb,Zd,D1=T(()=>{ur();kt();wb=class{static{f(this,"LivenessResult")}constructor(t,e,s,r,o,i,a){this.liveIn=t,this.liveOut=e,this.intervals=s,this.opIndex=r,this.peakPressure=o,this.peakOp=i,this.pressureAtOp=a}interfere(t,e){if(t===e)return!0;let s=this.intervals.get(t),r=this.intervals.get(e);return s&&r?s.start<=r.end&&r.start<=s.end:!1}liveAtOp(t){return this.liveIn.get(t)||new Set}intervalOf(t){return this.intervals.get(t)||null}},Zd=class n{static get name(){return"liveness"}static get depKey(){return"liveness"}static get dependencies(){return[qt]}static buildIntervals(t,e){let s=new Map;for(let o=0;o<e.length;o++)s.set(e[o],o);let r=new Map;for(let o of t.args)r.set(o,{start:-1,end:-1});for(let o=0;o<e.length;o++){let i=e[o];for(let a=0;a<i.numResults;a++)r.set(i.getResult(a),{start:o,end:o})}for(let o=0;o<e.length;o++){let i=e[o];for(let a=0;a<i.numOperands;a++){let c=i.getOperand(a),l=r.get(c);l&&l.end<o&&(l.end=o)}}return{intervals:r,opIndex:s}}static compute(t,e={}){let s=e.useDef||qt.compute(t),r=s.topologicalOrder,{intervals:o,opIndex:i}=n.buildIntervals(t,r),a=new Map,c=new Map;for(let d of r)a.set(d,new Set),c.set(d,new Set);let l=!0;for(;l;){l=!1;for(let d=r.length-1;d>=0;d--){let m=r[d],g=c.get(m),_=g.size,y=s.opUsers.get(m);if(y)for(let S of y){let v=a.get(S);if(v)for(let w of v)g.add(w)}let b=a.get(m),x=b.size;for(let S of g)b.add(S);for(let S=0;S<m.numResults;S++)b.delete(m.getResult(S));for(let S=0;S<m.numOperands;S++)b.add(m.getOperand(S));(g.size!==_||b.size!==x)&&(l=!0)}}for(let[d,m]of i){let g=a.get(d);for(let _ of g){let y=o.get(_);y&&y.end<m&&(y.end=m)}}let u=0,h=null,p=new Map;for(let d=0;d<r.length;d++){let m=r[d],g=a.get(m),_=0;for(let y of g)if(y.type instanceof E){let b=y.type.sizeInBytes();b!==Z&&(_+=b)}p.set(m,_),_>u&&(u=_,h=m)}return new wb(a,c,o,i,u,h,p)}}});var gC,Xd,Qd,L1=T(()=>{Ce();Rs();kt();Ee();Ue();ur();D1();gC=new Set(["return","yield","constant","scalar_constant"]),Xd=class{static{f(this,"RematerializationConfig")}constructor(t={}){this.memoryBudget=t.memoryBudget||1/0,this.maxIterations=t.maxIterations||100,this.maxRecomputeCost=t.maxRecomputeCost||1/0,this.excludeOps=t.excludeOps||new Set}},Qd=class extends Rt{static{f(this,"RematerializationPass")}constructor(t={}){super("RematerializationPass"),this.config=t instanceof Xd?t:new Xd(t)}run(t,e){if(this.config.memoryBudget===1/0)return Q.UNCHANGED;let s=!1,r=0,o=0;for(;r<this.config.maxIterations;){let i=qt.compute(t),{peakPressure:a,candidates:c}=this._analyzeIntervalPressure(t,i);if(o=a,a<=this.config.memoryBudget||c.length===0)break;c.sort((l,u)=>u.score-l.score),this._rematerialize(t,c[0]),s=!0,r++}return this.trace&&this.trace.level>=ot.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,iterations:r,peakPressure:o,budget:this.config.memoryBudget,changed:s,level:ot.DEBUG}),s?Q.CHANGED:Q.UNCHANGED}_analyzeIntervalPressure(t,e){let s=e.topologicalOrder,{intervals:r}=Zd.buildIntervals(t,s),o=[];for(let[d,m]of r){if(!(d.type instanceof E))continue;let g=d.type.sizeInBytes();g===Z||g<=0||(o.push({idx:m.start,delta:g,value:d}),o.push({idx:m.end+1,delta:-g,value:null}))}o.sort((d,m)=>d.idx-m.idx||d.delta-m.delta);let i=0,a=0,c=0,l=new Set,u=new Set,h=0;for(let d=-1;d<=s.length;d++){for(;h<o.length&&o[h].idx<=d;)i+=o[h].delta,o[h].delta>0&&o[h].value&&u.add(o[h].value),o[h].delta<0&&o[h].value&&u.delete(o[h].value),h++;if(i>a){a=i,c=d,l.clear();for(let m of u)l.add(m)}}let p=[];for(let d of l){if(!this._canRematerialize(d))continue;let m=d.definingOp,g=this._computeMemorySaved(d);if(g<=0)continue;let _=this._estimateRecomputeCost(m);_>=this.config.maxRecomputeCost||_!==0&&p.push({value:d,definingOp:m,memorySaved:g,recomputeCost:_,score:g/_})}return{peakPressure:a,peakIdx:c,candidates:p}}_canRematerialize(t){if(t.isBlockArgument())return!1;let e=t.definingOp;if(!e||gC.has(e.opName)||this.config.excludeOps.has(e.opName)||e.regions.length>0||e.hasSideEffects()||t.useCount<=1)return!1;for(let s=0;s<e.numOperands;s++){let r=e.getOperand(s);if(r.definingOp&&r.definingOp.hasSideEffects())return!1}return!0}_computeMemorySaved(t){if(!(t.type instanceof E))return 0;let e=t.type.sizeInBytes();return e===Z?0:e}_operandExtensionCost(t,e,s){let r=0;for(let o=0;o<t.numOperands;o++){let i=t.getOperand(o),a=0;for(let c of i.uses()){let l=s.get(c.user)||0;l>a&&(a=l)}if(e>a&&i.type instanceof E){let c=i.type.sizeInBytes();c!==Z&&(r+=c)}}return r}_estimateRecomputeCost(t){let e=st.get(t.opName);if(!e||e.isOpaque)return 1/0;if(e.getFlops){let s=e.getFlops(t);if(s>0)return s}if(e.isElementwise||e.isBroadcast||e.isView){let s=0;for(let r=0;r<t.numResults;r++){let o=t.getResult(r).type;if(o instanceof E){let i=o.numel();if(i!==Z){s=i;break}}}return s||1}if(e.isReduction)for(let s=0;s<t.numOperands;s++){let r=t.getOperand(s).type;if(r instanceof E){let o=r.numel();if(o!==Z)return o}}return 1/0}_rematerialize(t,e){let{value:s,definingOp:r}=e,o=[];for(let h of s.uses())o.push({user:h.user,operandIndex:h.operandIndex});if(o.length<=1)return;let i=new Map,a=0;for(let h of t.ops())i.set(h,a++);o.sort((h,p)=>(i.get(h.user)||0)-(i.get(p.user)||0));let c=i.get(o[o.length-1].user)||0,l=this._operandExtensionCost(r,c,i),u=this._computeMemorySaved(s);if(!(l>=u))for(let h=1;h<o.length;h++){let p=o[h],d=p.user;if(!d.parentBlock)continue;let m=new mt(r.opName,[...r.operands],r.results.map(g=>g.type),new Map(r.attributes));d.parentBlock.insertBefore(m,d),d.replaceOperand(p.operandIndex,m.getResult(s.resultIndex))}}}});function ui(n){let t=new Set(n),e=[],s=new Map,r=f(o=>{let i=s.get(o);if(i!==2){if(i===1)throw new Error("partition topo sort: cycle detected");s.set(o,1);for(let a=0;a<o.numOperands;a++){let c=o.getOperand(a).definingOp;c&&t.has(c)&&r(c)}s.set(o,2),e.push(o)}},"visit");for(let o of n)r(o);return e}function dl(n,{labelOf:t,sameLabel:e=f((i,a)=>i===a,"sameLabel"),canMerge:s=f(()=>!0,"canMerge"),onAttach:r=f(()=>{},"onAttach"),sort:o=ui}){let i=o(n),a=new Map,c=new Map,l=[],u=0,h=f((m,g)=>{if(m===g)return!0;let _=[g],y=new Set;for(;_.length>0;){let b=_.pop();if(b===m)return!0;if(y.has(b))continue;y.add(b);let x=c.get(b);if(x)for(let S of x)_.push(S)}return!1},"isUpstreamOf"),p=f(m=>{let g=new Set;for(let _=0;_<m.numOperands;_++){let y=m.getOperand(_).definingOp;if(!y)continue;let b=a.get(y);b&&g.add(b)}return g},"operandParts"),d=f((m,g)=>{for(let _ of p(m)){if(_===g)continue;let y=c.get(g);y||(y=new Set,c.set(g,y)),y.add(_)}},"recordEdges");for(let m of i){let g=t(m);if(g==null)continue;let _=!1;for(let y=0;y<m.numOperands;y++){let b=m.getOperand(y).definingOp;if(!b)continue;let x=a.get(b);if(!x||!e(x.label,g)||!s(x,m,g))continue;let S=!1;for(let v of p(m))if(v!==x&&h(x,v)){S=!0;break}if(!S){x.ops.push(m),x.opSet.add(m),r(x,m),a.set(m,x),d(m,x),_=!0;break}}if(!_){let y={id:u++,label:g,ops:[m],opSet:new Set([m])};r(y,m),l.push(y),a.set(m,y),d(m,y)}}return{partitions:l,opToPart:a,preds:c}}function tm(n,t,{pullConstants:e=!1,isConstant:s=f(()=>!1,"isConstant")}={}){let r=[],o=new Set,i=[],a=new Set,c=[],l=new Set;for(let u of t){for(let h=0;h<u.numOperands;h++){let p=u.getOperand(h),d=p.definingOp;if(!(d&&n.has(d))){if(e&&d&&s(d)){l.has(d)||(l.add(d),c.push(d));continue}o.has(p)||(o.add(p),r.push(p))}}for(let h=0;h<u.numResults;h++){let p=u.getResult(h);if(a.has(p))continue;let d=!1;for(let m of p.uses())if(!n.has(m.user)){d=!0;break}d&&(a.add(p),i.push(p))}}return{inputs:r,outputs:i,constDefs:c}}function xb(n,t){let e=new Map,s=new Map;for(let i of n)e.set(i,0),s.set(i,[]);for(let i of n){let a=t.get(i);if(a)for(let c of a)s.has(c)&&(s.get(c).push(i),e.set(i,e.get(i)+1))}let r=[];for(let i of n)e.get(i)===0&&r.push(i);let o=[];for(;r.length>0;){let i=r.shift();o.push(i);for(let a of s.get(i)){let c=e.get(a)-1;e.set(a,c),c===0&&r.push(a)}}return o.length===n.length?o:null}var em=T(()=>{f(ui,"topoSortOps");f(dl,"buildPartitions");f(tm,"computePartitionIO");f(xb,"topoSortPartitions")});function vb(n){let t=0;for(let e=0;e<n.numResults;e++){let s=n.getResult(e).type;s instanceof E&&s.isFullyStatic&&(t+=s.sizeInBytes())}return t}var Sb,$a,Bb,sm,P1=T(()=>{Ee();kt();em();Sb=class{static{f(this,"Partition")}constructor(t,e){this.id=t,this.target=e,this.ops=[],this.opSet=new Set,this._inputValues=null,this._outputValues=null,this._memoryBytes=0}addOp(t){this.opSet.has(t)||(this.ops.push(t),this.opSet.add(t),this._inputValues=null,this._outputValues=null,this._memoryBytes+=vb(t))}hasOp(t){return this.opSet.has(t)}merge(t){for(let e of t.ops)this.addOp(e)}computeIO(){if(this._inputValues&&this._outputValues)return;this._inputValues=[],this._outputValues=[];let t=new Set,e=new Set;for(let s of this.ops){for(let r=0;r<s.numOperands;r++){let o=s.getOperand(r);o.definingOp&&this.opSet.has(o.definingOp)||t.has(o)||(t.add(o),this._inputValues.push(o))}for(let r=0;r<s.numResults;r++){let o=s.getResult(r);if(!e.has(o)){for(let i of o.uses())if(!this.opSet.has(i.user)){e.add(o),this._outputValues.push(o);break}}}}}getInputValues(){return this.computeIO(),this._inputValues}getOutputValues(){return this.computeIO(),this._outputValues}get size(){return this.ops.length}get memoryBytes(){return this._memoryBytes}};f(vb,"estimateOpMemory");$a=class{static{f(this,"PartitionerConfig")}constructor(t={}){this.targets=t.targets||[],this.defaultTarget=t.defaultTarget||null,this.opTargetOverrides=t.opTargetOverrides||new Map,this.memoryLimits=t.memoryLimits||new Map,this.minPartitionSize=t.minPartitionSize||1,this.costWeights={transferCost:1,loadBalance:.5,...t.costWeights||{}}}},Bb=class{static{f(this,"PartitionResult")}constructor(t,e,s){this.partitions=t,this.opToPartition=e,this.transferEdges=s}getPartition(t){return this.opToPartition.get(t)||null}getPartitionsForTarget(t){return this.partitions.filter(e=>e.target===t||e.target.name===t.name)}get numPartitions(){return this.partitions.length}},sm=class{static{f(this,"GraphPartitioner")}constructor(t){this.config=t instanceof $a?t:new $a(t),this._supportCache=new Map,this._buildSupportMap()}partition(t){let e=this._collectPartitionableOps(t),s=this._assignTargets(e),r=this._buildPartitions(e,s),o=this._mergeSmallPartitions(r,s),i=this._computeTransferEdges(o),a=new Map;for(let c of o)for(let l of c.ops)a.set(l,c);return new Bb(o,a,i)}_buildSupportMap(){for(let t of this.config.targets){let e=new Set;for(let s of st.names())this._targetSupportsOp(t,s)&&e.add(s);this._supportCache.set(t,e)}}_targetSupportsOp(t,e){if(t.hasLibraryOp(e))return!0;let s=st.get(e);return s?s.isConstant||s.isTerminator?!0:t.isGPU()?s.isElementwise||s.isReduction||s.isBroadcast||s.isInjective||e==="dot"||e==="conv"||e==="batch_dot"||e==="softmax"||e==="layer_norm"||e==="batch_norm":t.isCPU()?!0:t.isWasm()?!s.isOpaque&&e!=="custom_call":!1:!1}_collectPartitionableOps(t){let e=[];for(let s of t.ops()){let r=st.get(s.opName);!r||r.isTerminator||e.push(s)}return e}_assignTargets(t){let e=new Map;for(let s of t){let r=s.getAttr("device");if(r){let a=this._resolveDeviceAttr(r);if(a){e.set(s,a);continue}}let o=this.config.opTargetOverrides.get(s.opName);if(o){e.set(s,o);continue}let i=this._selectBestTarget(s);e.set(s,i)}return e}_resolveDeviceAttr(t){return typeof t=="string"?this.config.targets.find(e=>e.name===t||e.kind===t)||null:t}_selectBestTarget(t){let e=this.config.defaultTarget||this.config.targets[0],s=-1/0;for(let r of this.config.targets){let o=this._supportCache.get(r);if(!o||!o.has(t.opName))continue;let i=this._scoreTargetForOp(r,t);i>s&&(s=i,e=r)}return e}_scoreTargetForOp(t,e){let s=st.get(e.opName);if(!s)return 0;let r=0;if(t.hasLibraryOp(e.opName)&&(r+=100),s.isReduction||s.isElementwise){let o=0;for(let i=0;i<e.numOperands;i++){let a=e.getOperand(i).type;a instanceof E&&a.isFullyStatic&&(o+=a.numel())}t.isGPU()&&o>1024?r+=50:t.isCPU()&&o<=1024&&(r+=30)}return r+=t.computeTFLOPs*10,r}_buildPartitions(t,e){let{partitions:s}=dl(t,{sort:f(i=>this._topologicalSort(i),"sort"),labelOf:f(i=>e.get(i),"labelOf"),sameLabel:f((i,a)=>i===a||i.name===a.name,"sameLabel"),canMerge:f((i,a,c)=>this._fitsMemoryLimit(i,a,c),"canMerge"),onAttach:f((i,a)=>{i.memoryBytes=(i.memoryBytes||0)+vb(a)},"onAttach")}),r=new Map;for(let i of s){let a=new Sb(i.id,i.label);for(let c of i.ops)a.addOp(c);r.has(i.label.name)||r.set(i.label.name,[]),r.get(i.label.name).push(a)}let o=[];for(let i of r.values())for(let a of i)o.push(a);return o}_fitsMemoryLimit(t,e,s){let r=this.config.memoryLimits.get(s.name);if(!r)return!0;let o=vb(e);return t.memoryBytes+o<=r}_mergeSmallPartitions(t,e){if(t.length<=1)return t;let s=new Map;for(let u of t)for(let h of u.ops)s.set(h,u);let r=f(u=>{let h=new Set;for(let p of u.ops)for(let d=0;d<p.numResults;d++)for(let m of p.getResult(d).uses()){let g=s.get(m.user);g&&g!==u&&h.add(g)}return h},"directSuccs"),o=f((u,h)=>{let p=new Set([u]),d=[...r(u)].filter(m=>m!==h);for(;d.length>0;){let m=d.pop();if(m===h)return!0;if(!p.has(m)){p.add(m);for(let g of r(m))d.push(g)}}return!1},"reachesThroughIntermediate"),i=f((u,h)=>o(u,h)||o(h,u),"mergeCreatesCycle"),a=[],c=new Set;for(let u=0;u<t.length;u++){if(c.has(u))continue;let h=t[u];if(h.size>=this.config.minPartitionSize){a.push(h);continue}let p=-1,d=-1/0;for(let m=0;m<t.length;m++){if(u===m||c.has(m))continue;let g=t[m];if(g.target.name!==h.target.name||i(h,g))continue;let _=this._mergeScore(h,g);_>d&&(d=_,p=m)}if(p>=0){t[p].merge(h);for(let m of h.ops)s.set(m,t[p]);c.add(u)}else a.push(h)}let l=new Set;for(let u of a)for(let h of u.ops)l.add(h);for(let u of t){let h=!1;for(let p of u.ops)if(!l.has(p)){h=!0;break}if(h){a.push(u);for(let p of u.ops)l.add(p)}}return a}_mergeScore(t,e){let s=0,r=new Set;for(let o of t.ops)for(let i=0;i<o.numResults;i++)r.add(o.getResult(i));for(let o of e.ops)for(let i=0;i<o.numOperands;i++)r.has(o.getOperand(i))&&s++;return s}_computeTransferEdges(t){let e=[],s=new Set,r=new Map;for(let o of t)for(let i of o.ops)r.set(i,o);for(let o of t)for(let i of o.ops)for(let a=0;a<i.numOperands;a++){let c=i.getOperand(a).definingOp;if(!c)continue;let l=r.get(c);if(!l||l===o)continue;let u=i.getOperand(a),h=`${l.id}|${o.id}|${u.id}`;s.has(h)||(s.add(h),e.push({src:l,dst:o,value:u,sizeBytes:u.type instanceof E&&u.type.isFullyStatic?u.type.sizeInBytes():0}))}return e}_topologicalSort(t){let e=new Set(t),s=new Map,r=new Map;for(let a of t)s.set(a,0),r.set(a,[]);for(let a of t)for(let c=0;c<a.numOperands;c++){let l=a.getOperand(c).definingOp;l&&e.has(l)&&(r.get(l).push(a),s.set(a,s.get(a)+1))}let o=[];for(let a of t)s.get(a)===0&&o.push(a);let i=[];for(;o.length>0;){let a=o.shift();i.push(a);for(let c of r.get(a)){let l=s.get(c)-1;s.set(c,l),l===0&&o.push(c)}}return i}}});var nm,rm,z1=T(()=>{Ce();Rs();Wr();P1();em();Ue();nm=class extends Rt{static{f(this,"GraphPartitionPass")}constructor(t={}){super("GraphPartitionPass"),this.partitionerConfig=new $a(t),this.partitionResult=null}run(t,e){if(this.partitionerConfig.targets.length<2)return Q.UNCHANGED;let s=new sm(this.partitionerConfig);return this.partitionResult=s.partition(t),this.partitionResult.numPartitions<=1?Q.UNCHANGED:(this._annotateOps(),this._insertTransferOps(t),this.trace&&this.trace.level>=ot.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,numPartitions:this.partitionResult.numPartitions,transferEdges:this.partitionResult.transferEdges.length,level:ot.DEBUG}),t.bumpVersion(),Q.CHANGED)}_annotateOps(){for(let t of this.partitionResult.partitions)for(let e of t.ops)e.setAttr("partition_id",t.id),e.setAttr("partition_target",t.target.name)}_insertTransferOps(t){let e=t.entryBlock,s=this._buildOrderIndex(e),{useMap:r,firstInPart:o}=this._buildInsertionIndex(e);for(let i of this.partitionResult.transferEdges){let a=i.value,c=i.src.target.name,l=i.dst.target.name;if(c===l)continue;let u=new mt("copy_to_device",[a],[a.type],{src_device:c,dst_device:l});u.setAttr("partition_id",i.dst.id),u.setAttr("partition_target",l);let h=r.get(i.dst),p=h&&h.get(a)||o.get(i.dst)||null,d=a.definingOp;if(p&&d&&s.has(d)&&s.has(p)&&s.get(d)>=s.get(p))e.insertAfter(u,d);else if(p)e.insertBefore(u,p);else if(d&&s.has(d))e.insertAfter(u,d);else{let g=t.getReturnOp();g?e.insertBefore(u,g):e.pushOp(u)}let m=u.getResult(0);for(let g of i.dst.ops)for(let _=0;_<g.numOperands;_++)g.getOperand(_)===a&&g.replaceOperand(_,m)}}_buildInsertionIndex(t){let e=[...new Set(this.partitionResult.transferEdges.map(o=>o.dst))],s=new Map,r=new Map;for(let o of e)s.set(o,new Map);for(let o of t.ops())for(let i of e){if(!i.hasOp(o))continue;r.has(i)||r.set(i,o);let a=s.get(i);for(let c=0;c<o.numOperands;c++){let l=o.getOperand(c);a.has(l)||a.set(l,o)}}return{useMap:s,firstInPart:r}}_buildOrderIndex(t){let e=new Map,s=0;for(let r of t.ops())e.set(r,s++);return e}},rm=class extends Rt{static{f(this,"PartitionMaterializationPass")}constructor(t={}){super("PartitionMaterializationPass"),this.targets=t.targets||[]}run(t,e){let s=this._collectPartitions(t);if(s.size<=1)return Q.UNCHANGED;let r=this._getModule(t);if(!r)return Q.UNCHANGED;let o=this._materializePartitions(t,s);for(let i of o)r.addFunction(i);return this._rewriteOriginalFunction(t,o,s),this.trace&&this.trace.level>=ot.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,partitions:s.size,subFunctions:o.length,level:ot.DEBUG}),t.bumpVersion(),Q.CHANGED}_collectPartitions(t){let e=new Map;for(let s of t.ops()){let r=s.getAttr("partition_id");r!==void 0&&(e.has(r)||e.set(r,{id:r,target:s.getAttr("partition_target"),ops:[]}),e.get(r).ops.push(s))}return e}_getModule(t){return t._module||null}_materializePartitions(t,e){let s=[];for(let[r,o]of e){let i=new Set(o.ops),{inputs:a,outputs:c}=tm(i,o.ops),l=a.map(y=>y.type),u=c.map(y=>y.type),h=`${t.name}_partition_${r}`,p=new os(h,l,u);p._partitionTarget=o.target;let d=new Map;for(let y=0;y<a.length;y++)d.set(a[y],p.args[y]);let m=ui(o.ops);for(let y of m){let b=[];for(let w=0;w<y.numOperands;w++){let A=d.get(y.getOperand(w));A?b.push(A):b.push(y.getOperand(w))}let x=[];for(let w=0;w<y.numResults;w++)x.push(y.getResult(w).type);let S=y.regions.length>0?y.regions.map(w=>Fn(w)):null,v=new mt(y.opName,b,x,new Map(y.attributes),S);p.entryBlock.pushOp(v);for(let w=0;w<y.numResults;w++)d.set(y.getResult(w),v.getResult(w))}let g=c.map(y=>d.get(y)||y),_=new mt("return",g,[]);p.entryBlock.pushOp(_),s.push(p)}return s}_rewriteOriginalFunction(t,e,s){for(let r of e)t.setAttr?.(`sub_${r.name}`,r._partitionTarget)}}});function lr(n){return _C.has(n.opName)}function $1(n){let t=0;for(let e=0;e<n.numResults;e++){let s=n.getResult(e).type;if(!s||!s.isFullyStatic)continue;let r=s.sizeInBytes();r>t&&(t=r)}return t}function bC(n){let t=n.definingOp;if(t&&t.opName==="broadcast"){let e=t.getOperand(0);t=e&&e.definingOp}if(t&&lr(t)){let e=t.getAttr("value");if(typeof e=="number")return e}return 0}function kb(n){if(n.opName!=="dot"&&n.opName!=="cublas_gemm")return null;let t=n.getOperand(0).type,e=n.getOperand(1).type,s=n.getResult(0).type;if(!t||!e||!s||t.dtype!=="f32"||e.dtype!=="f32"||s.dtype!=="f32")return null;let r=n.getAttr("lhs_batch")||[],o=n.getAttr("rhs_batch")||[];if(r.length>0||o.length>0)return null;let i=n.getAttr("lhs_contracting")||[],a=n.getAttr("rhs_contracting")||[];if(i.length!==1||a.length!==1||e.rank!==2||i[0]!==t.rank-1||a[0]!==0&&a[0]!==1||!t.isFullyStatic||!e.isFullyStatic||!s.isFullyStatic)return null;let c=n.getOperand(0).definingOp,l=n.getOperand(1).definingOp;if(c&&lr(c)||l&&lr(l))return null;let u=a[0]===1,h=t.shape[t.rank-1];if((u?e.shape[1]:e.shape[0])!==h)return null;let d=1;for(let g=0;g<t.rank-1;g++)d*=t.shape[g];let m=u?e.shape[0]:e.shape[1];return d<=0||m<=0||h<=0?null:{M:d,N:m,K:h,transB:u}}function F1(n){return{labelOf:f(t=>n.get(t),"labelOf"),canMerge:f((t,e)=>Math.max(t.maxBuf||0,$1(e))<=yC,"canMerge"),onAttach:f((t,e)=>{t.maxBuf=Math.max(t.maxBuf||0,$1(e))},"onAttach")}}function im(n,t,e){let s=n.opSet,r=ui(n.ops),{inputs:o,outputs:i,constDefs:a}=tm(s,r,{pullConstants:!0,isConstant:lr});for(let p of o)if(!p.type||!p.type.isFullyStatic)return null;for(let p of i)if(!p.type||!p.type.isFullyStatic)return null;let c=new os(t,o.map(p=>p.type),i.map(p=>p.type)),l=new Map;for(let p=0;p<o.length;p++)l.set(o[p],c.args[p]);for(let p of a)c.entryBlock.pushOp(p.clone(l));for(let p of r)c.entryBlock.pushOp(p.clone(l));let u=i.map(p=>l.get(p));if(u.some(p=>p===void 0))return null;c.entryBlock.pushOp(new mt("return",u,[]));let h=n.ops.length===1&&e.has(n.ops[0])?n.ops[0]:null;return{part:n,subFunc:c,inputs:o,outputs:i,dotOp:h}}function om(n){if(wC.has(n.opName))return!0;if(n.regions)for(let t of n.regions){let e=t.entryBlock;if(e){for(let s of e.ops())if(om(s))return!0}}return!1}function xC(n){let t=0;for(let e=0;e<n.numResults;e++){let s=n.getResult(e).type;if(!s||!s.isFullyStatic)continue;let r=1;for(let o of s.shape)r*=o;r>t&&(t=r)}return t}function V1(n,t=256){if(n.functionCount!==1)return!1;let e=n.functions().next().value,s=new Map,r=f(o=>{if(!o)return 0;let i=s.get(o);if(i!==void 0)return i;s.set(o,0);let a=om(o)?xC(o):0;for(let c=0;c<o.numOperands;c++){let l=r(o.getOperand(c).definingOp);l>a&&(a=l)}return s.set(o,a),a},"maxBoundaryInSubtree");for(let o of e.ops())if(!(ml.has(o.opName)||!om(o))){for(let i=0;i<o.numOperands;i++)if(r(o.getOperand(i).definingOp)>t)return!0}return!1}function G1(n,t,e){let s=new Map,r=0,o=f(d=>{let m=s.get(d);return m===void 0&&(m=r++,s.set(d,m)),m},"getSlot");for(let d of n.args)o(d);for(let d of e)for(let m of d.outputs)o(m);let i=[];for(let d of n.args)i.push(o(d));let a=[],c=new Set;for(let d=0;d<t.numOperands;d++){let m=t.getOperand(d),g=i.length,_=m.isBlockArgument&&m.isBlockArgument(),y=m.definingOp&&lr(m.definingOp);if(!_&&!y&&s.has(m)){let b=s.get(m);if(!c.has(b)){c.add(b),i.push(b);continue}i.push(r++),a.push({pos:g,kind:"copy",srcSlot:b});continue}if(_){i.push(r++),a.push({pos:g,kind:"copy",srcSlot:o(m)});continue}if(y){i.push(r++),a.push({pos:g,kind:"const",value:bC(m)});continue}return null}let l=[];for(let d of e){let m=[];for(let _ of d.inputs){let y=s.get(_);if(y===void 0)return null;m.push(y)}let g=d.outputs.map(_=>s.get(_));l.push({name:d.subFunc.name,inputSlots:m,outputSlots:g})}let u=new Set(i),h=[],p=new Set;for(let[d,m]of s)if(!(u.has(m)||p.has(m))){if(p.add(m),!d.type||!d.type.isFullyStatic)return null;h.push({slot:m,shape:[...d.type.shape],dtype:d.type.dtype})}return{plan:{numSlots:r,argSlots:i,intermediates:h,steps:l,returnFixups:a}}}function Fa(n,t=2){if(n.functionCount!==1)return null;let e=n.functions().next().value,s=e.getReturnOp();if(!s)return null;let r=[],o=new Map,i=0;for(let g of e.ops())ml.has(g.opName)||lr(g)||(om(g)?o.set(g,"boundary#"+i++):o.set(g,"native"),r.push(g));if(i<t||r.length===0)return null;let{partitions:a,preds:c}=dl(r,F1(o));if(a.length<2)return null;let l=xb(a,c);if(!l)return null;let u=e.name,h=[],p=new Map,d=0;for(let g of l){let _=im(g,u+"_p"+d++,p);if(!_)return null;h.push(_)}let m=G1(e,s,h);if(!m)return null;n.removeFunction(e.name);for(let g of h)n.addFunction(g.subFunc);return{plan:m.plan}}function q1(n){if(n.functionCount!==1)return null;let t=n.functions().next().value,e=t.getReturnOp();if(!e)return null;let s=[],r=new Map,o=new Map,i=0;for(let g of t.ops()){if(ml.has(g.opName)||lr(g))continue;let _=kb(g);_?(r.set(g,"cublas#"+i),o.set(g,_),i++):r.set(g,"native"),s.push(g)}if(i===0||s.length===0)return null;let{partitions:a,preds:c}=dl(s,F1(r));if(a.length<2)return null;let l=xb(a,c);if(!l)return null;let u=t.name,h=[],p=0;for(let g of l){let _=im(g,u+"_p"+p++,o);if(!_)return null;h.push(_)}let d=G1(t,e,h);if(!d)return null;let m=new Map;for(let g of h){if(!g.dotOp)continue;let _=o.get(g.dotOp),y=g.inputs.indexOf(g.dotOp.getOperand(0)),b=g.inputs.indexOf(g.dotOp.getOperand(1)),x=g.outputs.indexOf(g.dotOp.getResult(0));y<0||b<0||x<0||m.set(g.subFunc.name,{M:_.M,N:_.N,K:_.K,transB:_.transB,aIdx:y,bIdx:b,cIdx:g.inputs.length+x})}if(m.size===0)return null;n.removeFunction(t.name);for(let g of h)n.addFunction(g.subFunc);return{plan:d.plan,cublasInfos:m}}var _C,ml,yC,wC,am=T(()=>{Wr();Rs();em();_C=new Set(["constant","scalar_constant"]),ml=new Set(["return","yield"]);f(lr,"isConstantOp");yC=32*1024;f($1,"maxResultBytes");f(bC,"constScalarOf");f(kb,"cublasDotInfo");f(F1,"bufferLimitedConfig");f(im,"materializePartition");wC=new Set(["dot","cublas_gemm","reduce"]);f(om,"containsBoundaryOp");f(xC,"maxBoundaryResultBytes");f(V1,"hasDependentBoundaries");f(G1,"buildExecutionPlan");f(Fa,"splitGraphForNative");f(q1,"splitGraphForCublas")});function W1(n){return U1.push(n),n}var U1,cm,j1=T(()=>{Ce();U1=[];f(W1,"registerBackendOpRewrite");cm=class extends Rt{static{f(this,"BackendOpRewritePass")}constructor(t={}){super(t.name||"BackendOpRewritePass"),this.config=t,this.rewrites=t.rewrites||U1}run(t){let e=!1;for(let s of[...t.ops()]){let r=s.parentBlock;if(r){for(let o of this.rewrites)if(o.match(s,this.config)){o.build(s,r,this.config),e=!0;break}}}return e?Q.CHANGED:Q.UNCHANGED}}});var SC,um,K1=T(()=>{Rs();am();j1();SC=W1({name:"dot->cublas_gemm",match:f(n=>n.opName==="dot"&&n.numOperands===2&&kb(n)!==null,"match"),build:f((n,t)=>{let e=n.results.map(r=>r.type),s=new mt("cublas_gemm",[n.getOperand(0),n.getOperand(1)],e,new Map(n.attributes),null);t.insertBefore(s,n),n.replaceAllResultsWith(s.results),n.erase()},"build")}),um=class extends cm{static{f(this,"CublasRewritePass")}constructor(t={}){super({...t,name:"CublasRewritePass",rewrites:[SC]})}}});function gl(n){let t=1;for(let e of n){if(typeof e!="number"||e<0)return-1;t*=e}return t}function _l(n){return n==="f16"||n==="i16"?2:n==="i8"||n==="u8"?1:4}function vC(n,t,e){let s=e.sharedMemoryBytes||16384,r=0;for(let c of t.entryBlock.ops())if(c.opName!=="yield")for(let l=0;l<c.numResults;l++){let u=c.getResult(l).type;if(!u||!u.shape)continue;let h=gl(u.shape);if(h<0)return!0;let p=h*_l(u.dtype);p>r&&(r=p)}let o=n.getAttr("num_carry"),i=n.getAttr("num_xs"),a=0;for(let c=0;c<o;c++){let l=n.getOperand(i+c).type,u=l&&l.shape?gl(l.shape):-1;u>0&&(a+=u*_l(l.dtype))}return 3*r+2*a>s}function BC(n,t){let s=n.regions[0].entryBlock,r=n.getAttr("num_carry"),o=[],i=null;for(let b of s.ops()){if(b.opName==="yield"){i=b;continue}o.push(b)}if(!i)return null;let a=new Set(o),c=new Set(s.arguments),l=[],u=new Set,h=[],p=new Set;for(let b of o)for(let x=0;x<b.numOperands;x++){let S=b.getOperand(x),v=S.definingOp;if(!c.has(S)&&!(v&&a.has(v))){if(v&&lr(v)){p.has(v)||(p.add(v),h.push(v));continue}u.has(S)||(u.add(S),l.push(S))}}let d=[...s.arguments,...l];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 os(t,d.map(b=>b.type),m.map(b=>b.type)),_=new Map;for(let b=0;b<d.length;b++)_.set(d[b],g.args[b]);for(let b of h)g.entryBlock.pushOp(b.clone(_));for(let b of o)g.entryBlock.pushOp(b.clone(_));let y=m.map(b=>_.get(b));return y.some(b=>b===void 0)?null:(g.entryBlock.pushOp(new mt("return",y,[])),{bodyFunc:g,captured:l,numCarry:r,numYs:m.length-r})}function kC(n,t,e,s,r){let{getSlot:o,newSlot:i,steps:a,addedFuncs:c}=s,l=new _s(n.name+"_mod");l.addFunction(n);let u=Fa(l,r);if(u){let h=u.plan,p=t.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=f(S=>{let v=p+S;return d.has(v)?d.get(v):h.argSlots[v]},"retSlot"),_=new Map;for(let S=0;S<p;S++)_.set(h.argSlots[S],o(t[S]));for(let S of h.intermediates)_.set(S.slot,i(S.shape,S.dtype));for(let S=0;S<e.length;S++){let v=g(S);_.has(v)||_.set(v,o(e[S]))}let y=f(S=>{let v=_.get(S);return v===void 0?null:v},"mapSlot"),b=[],x=!0;for(let S of h.steps){let v=S.inputSlots.map(y),w=S.outputSlots.map(y);if(v.includes(null)||w.includes(null)){x=!1;break}b.push({name:S.name,inputSlots:v,outputSlots:w})}if(x){for(let S of b)a.push(S);for(let S of l.functions())c.push(S);return!0}}}return a.push({name:n.name,inputSlots:t.map(o),outputSlots:e.map(o)}),c.push(n),!0}function H1(n,t,e){let s=im({ops:n,opSet:new Set(n)},t,new Map);return s?kC(s.subFunc,s.inputs,s.outputs,e,1):!1}function AC(n,t,e){let{getSlot:s,newSlot:r,steps:o,scanLoops:i,addedFuncs:a}=e,c=n.regions[0];if(!c||!c.entryBlock)return!1;let l=BC(n,t);if(!l)return!1;let{bodyFunc:u,captured:h,numCarry:p,numYs:d}=l,m=n.getAttr("num_xs"),g=new _s(t+"_mod");g.addFunction(u);let _=Fa(g,2),y=_?_.plan:null,b=[],x=[];for(let L=0;L<p;L++){let q=n.getOperand(m+L).type;b.push(q.shape),x.push(q.dtype)}let S=[],v=[];for(let L=0;L<m;L++){let q=n.getOperand(L).type;S.push(q.shape.slice(1)),v.push(q.dtype)}let w=[],A=[];for(let L=0;L<d;L++){let q=n.getResult(p+L).type;w.push(q.shape.slice(1)),A.push(q.dtype)}let I=b.map((L,q)=>r(L,x[q])),B=b.map((L,q)=>r(L,x[q])),k=S.map((L,q)=>r(L,v[q])),N=[],R=[],O=[],M=[];for(let L=0;L<p;L++)R.push(s(n.getOperand(m+L)));for(let L=0;L<p;L++)O.push(s(n.getResult(L)));for(let L of h)M.push(s(L));let F=[],P=[];for(let L=0;L<m;L++)F.push(s(n.getOperand(L)));for(let L=0;L<d;L++)P.push(s(n.getResult(p+L)));let G=o.length;if(!y)N=w.map((L,q)=>r(L,A[q])),o.push({name:u.name,inputSlots:[...k,...I,...M],outputSlots:[...B,...N]}),a.push(u);else{let L=m+p+h.length,q=new Map;for(let z of y.returnFixups||[]){if(z.kind!=="copy")return!1;q.set(z.pos,z.srcSlot)}let yt=f(z=>{let Y=L+z;return q.has(Y)?q.get(Y):y.argSlots[Y]},"bodyReturnSlot"),lt=new Map;for(let z=0;z<m;z++)lt.set(y.argSlots[z],k[z]);for(let z=0;z<p;z++)lt.set(y.argSlots[m+z],I[z]);for(let z=0;z<h.length;z++)lt.set(y.argSlots[m+p+z],M[z]);for(let z of y.intermediates)lt.set(z.slot,r(z.shape,z.dtype));for(let z=0;z<p;z++){let Y=yt(z);lt.has(Y)||lt.set(Y,B[z])}for(let z=0;z<d;z++){let Y=yt(p+z),Bt=lt.get(Y);Bt===void 0&&(Bt=r(w[z],A[z]),lt.set(Y,Bt)),N.push(Bt)}let pt=f(z=>{let Y=lt.get(z);return Y===void 0?null:Y},"mapSlot");for(let z of y.steps){let Y=z.inputSlots.map(pt),Bt=z.outputSlots.map(pt);if(Y.includes(null)||Bt.includes(null))return!1;o.push({name:z.name,inputSlots:Y,outputSlots:Bt})}for(let z of g.functions())a.push(z)}let W=o.length,$=n.getOperand(0).type.shape[0];return typeof $!="number"||$<0?!1:(i.push({T:$,loopStart:G,loopEnd:W,carry:b.map((L,q)=>({a:I[q],b:B[q],initSlot:R[q],finalSlot:O[q],bytes:gl(L)*_l(x[q])})),xs:k.map((L,q)=>({xtSlot:L,xsSlot:F[q],stepBytes:gl(S[q])*_l(v[q])})),ys:N.map((L,q)=>({ytSlot:L,ysSlot:P[q],stepBytes:gl(w[q])*_l(A[q])}))}),!0)}function Ab(n,t,e=!1){if(!t||typeof t.isWebGPU!="function"||!t.isWebGPU()||n.functionCount!==1)return null;let s=n.functions().next().value,r=s.getReturnOp();if(!r)return null;let o=[];for(let R of s.ops())R.opName==="scan"&&o.push(R);if(o.length===0)return null;let i=new Set,a=f(R=>{if(!(!R||i.has(R))){i.add(R);for(let O=0;O<R.numOperands;O++)a(R.getOperand(O).definingOp)}},"visit");for(let R=0;R<r.numOperands;R++)a(r.getOperand(R).definingOp);for(let R of o)if(!i.has(R))return null;if(!e){let R=!1;for(let O of o){let M=O.regions[0];if(M&&M.entryBlock&&vC(O,M,t)){R=!0;break}}if(!R)return null}let c=[];for(let R of i)lr(R)||ml.has(R.opName)||c.push(R);let l=ui(c),u=new Map,h=0,p=f(R=>{let O=u.get(R);return O===void 0&&(O=h++,u.set(R,O)),O},"getSlot");for(let R of s.args)p(R);let d=[],m=f((R,O)=>{let M=h++;return d.push({slot:M,shape:[...R],dtype:O}),M},"newSlot"),g=[],_=[],y=[],b={getSlot:p,newSlot:m,steps:g,scanLoops:_,addedFuncs:y},x=new Set(o),S=[],v=0,w=0;for(let R of l)if(x.has(R)){if(S.length&&!H1(S,s.name+"_seg"+v++,b)||(S=[],!AC(R,s.name+"_scan"+w++,b)))return null}else S.push(R);if(S.length&&!H1(S,s.name+"_seg"+v++,b))return null;let A=s.args.map(p),I=[],B=new Set;for(let R=0;R<r.numOperands;R++){let O=r.getOperand(R),M=A.length;if(u.has(O)&&!(O.isBlockArgument&&O.isBlockArgument())){let F=p(O);B.has(F)?(A.push(h++),I.push({pos:M,kind:"copy",srcSlot:F})):(B.add(F),A.push(F))}else if(O.isBlockArgument&&O.isBlockArgument())A.push(h++),I.push({pos:M,kind:"copy",srcSlot:p(O)});else return null}let k=new Set(A),N=new Set;for(let[R,O]of u)if(!(k.has(O)||N.has(O))){if(N.add(O),!R.type||!R.type.isFullyStatic)return null;d.some(M=>M.slot===O)||d.push({slot:O,shape:[...R.type.shape],dtype:R.type.dtype})}n.removeFunction(s.name);for(let R of y)n.addFunction(R);return{plan:{numSlots:h,argSlots:A,intermediates:d,steps:g,returnFixups:I,scanLoops:_}}}var Y1=T(()=>{Rs();Wr();xo();am();f(gl,"numel");f(_l,"dtypeBytes");f(vC,"isScanOversized");f(BC,"buildScanBodyFunction");f(kC,"inlineNativeSplit");f(H1,"emitSegment");f(AC,"emitScanLoop");f(Ab,"splitGraphForScan")});function Ob(n){Rb.push(n),Rb.sort((t,e)=>(t.priority??100)-(e.priority??100))}function RC(n){for(let t of Rb)if(t.applies(n))return t;return null}function J1(n,t){let e=RC(t);return e?e.run(n,t):null}var Rb,Z1=T(()=>{am();Y1();Rb=[];f(Ob,"registerGraphSplitStrategy");f(RC,"selectGraphSplitStrategy");f(J1,"splitGraph");Ob({name:"cublas",priority:10,applies:f(n=>n.config.matmulBackend==="cublas","applies"),run:f(n=>q1(n),"run")});Ob({name:"cuda-native-matmul-chain",priority:20,applies:f(n=>n.cudaMatmulChain,"applies"),run:f(n=>Fa(n),"run")});Ob({name:"webgpu",priority:30,applies:f(n=>n.isWebGPU,"applies"),run:f((n,t)=>{let e=Ab(n,t.target);return!e&&V1(n,t.target.maxThreadsPerBlock||256)&&(e=Ab(n,t.target,!0),e||(e=Fa(n,2))),e},"run")})});function Nb(n,t,e){return OC.filter(s=>s.phase===n).sort((s,r)=>s.priority-r.priority).map(s=>s.factory(t,e)).filter(Boolean)}var OC,X1=T(()=>{OC=[];f(Nb,"graphPassesForPhase")});function NC(n){let t=[...n.entries()];return t.sort((e,s)=>e[0]<s[0]?-1:e[0]>s[0]?1:0),t}function lm(n){return n==null?"null":typeof n=="number"?Number.isFinite(n)?String(n):n===1/0?"inf":n===-1/0?"-inf":"nan":typeof n=="boolean"?String(n):typeof n=="string"?`"${n}"`:n instanceof E?us(n):Array.isArray(n)?n.length===0?"[]":Array.isArray(n[0])?"["+n.map(t=>lm(t)).join(", ")+"]":"["+n.map(t=>lm(t)).join(", ")+"]":typeof n=="object"&&n.constructor===Object?"{"+Object.entries(n).sort((e,s)=>e[0].localeCompare(s[0])).map(([e,s])=>`${e}: ${lm(s)}`).join(", ")+"}":String(n)}var fm,Q1=T(()=>{kt();fm=class{static{f(this,"IRPrinter")}constructor(t={}){this.indent=0,this.indentStr=t.indentStr||" ",this.valueNames=new Map,this._nextValueId=0}printModule(t){let e=[];e.push(`module @${t.name} {`),this.indent++;let s=!0;for(let r of t)s||e.push(""),s=!1,this.printFunction(r,e);return this.indent--,e.push("}"),e.join(`
|
|
197
|
-
`)}printFunction(t,e=null){let s=e===null;s&&(e=[]),this.valueNames.clear(),this._nextValueId=0;let r=t.args.map((i,a)=>`${this._nameValue(i)}: ${us(i.type)}`),o=t.outputTypes.map(i=>us(i)).join(", ");e.push(`${this._indentPrefix()}func @${t.name}(${r.join(", ")}) -> (${o}) {`),this.indent++;for(let i of t.body)this.printBlock(i,e,i===t.entryBlock);return this.indent--,e.push(`${this._indentPrefix()}}`),s?e.join(`
|
|
198
|
-
`):void 0}printBlock(t,e,s=!1){if(!s&&t.arguments.length>0){let r=t.arguments.map(o=>`${this._nameValue(o)}: ${us(o.type)}`);e.push(`${this._indentPrefix()}^bb(${r.join(", ")}):`)}for(let r of t)this.printOperation(r,e)}printOperation(t,e=null){let s=e===null;s&&(e=[]);let r=this._indentPrefix(),o=r;if(t.numResults>0){let i=[];for(let a=0;a<t.numResults;a++)i.push(this._nameValue(t.getResult(a)));o+=i.join(", ")+" = "}if(o+=t.opName,t.numOperands>0){let i=[];for(let a=0;a<t.numOperands;a++)i.push(this._valueName(t.getOperand(a)));o+="("+i.join(", ")+")"}else o+="()";if(t.attributes.size>0){let i=[];for(let[a,c]of NC(t.attributes))i.push(`${a} = ${lm(c)}`);o+=" {"+i.join(", ")+"}"}if(t.numResults>0){let i=[];for(let a=0;a<t.numResults;a++)i.push(us(t.getResult(a).type));o+=" : "+i.join(", ")}e.push(o);for(let i=0;i<t.regions.length;i++){let a=t.regions[i];e.push(`${r}{`),this.indent++;for(let c of a){if(c.arguments.length>0){let l=c.arguments.map(u=>`${this._nameValue(u)}: ${us(u.type)}`);e.push(`${this._indentPrefix()}^bb(${l.join(", ")}):`)}for(let l of c)this.printOperation(l,e)}this.indent--,e.push(`${r}}`)}return s?e.join(`
|
|
199
|
-
`):void 0}_nameValue(t){if(this.valueNames.has(t))return this.valueNames.get(t);let e=`%${this._nextValueId++}`;return this.valueNames.set(t,e),e}_valueName(t){return this.valueNames.has(t)?this.valueNames.get(t):"%?"}_indentPrefix(){let t="";for(let e=0;e<this.indent;e++)t+=this.indentStr;return t}};f(NC,"sortedEntries");f(lm,"formatAttrValue")});function Tb(n){return new Eb().print(n)}var Eb,tR=T(()=>{Eb=class{static{f(this,"TensorIRPrinter")}constructor(){this.indent=0,this.out=[]}print(t){return this.out=[],this.visit(t),this.out.join("")}push(t){this.out.push(t)}newline(){this.out.push(`
|
|
200
|
-
`+" ".repeat(this.indent))}visit(t){if(!t)return;let e="visit"+t.type;this[e]?this[e](t):this.push(`[UnknownNode: ${t.type}]`)}visitPrimFunc(t){this.push(`prim_func ${t.name}(${t.params.map(e=>e.name).join(", ")}) {`),this.indent++,this.newline();for(let[e,s]of t.bufferMap)this.push(`${s.name} = buffer_map(${e.name}, shape=[${s.shape.join(",")}], dtype=${s.dtype})`),this.newline();this.visit(t.body),this.indent--,this.newline(),this.push("}")}visitSeqNode(t){for(let e=0;e<t.stmts.length;e++)this.visit(t.stmts[e]),e<t.stmts.length-1&&this.newline()}visitForNode(t){let e=t.kind==="serial"?"":`@${t.kind} `,s=t.threadTag?`[${t.threadTag}] `:"";this.push(`for ${t.loopVar.name} in 0..`),this.visit(t.extent),this.push(` ${e}${s}{`),this.indent++,this.newline(),this.visit(t.body),this.indent--,this.newline(),this.push("}")}visitBlockNode(t){this.push(`block ${t.name} {`),this.indent++,this.newline();for(let e of t.iterVars)this.push(`bind ${e.iterVar.name} = `),this.visit(e.binding),this.newline();t.reads.length>0&&(this.push("reads(["),this.push(t.reads.map(e=>`${e.buffer.name}[...]`).join(", ")),this.push("])"),this.newline()),t.writes.length>0&&(this.push("writes(["),this.push(t.writes.map(e=>`${e.buffer.name}[...]`).join(", ")),this.push("])"),this.newline()),t.initBody&&(this.push("init {"),this.indent++,this.newline(),this.visit(t.initBody),this.indent--,this.newline(),this.push("}"),this.newline()),this.visit(t.body),this.indent--,this.newline(),this.push("}")}visitBufferStoreNode(t){this.push(`${t.buffer.name}[`);for(let e=0;e<t.indices.length;e++)this.visit(t.indices[e]),e<t.indices.length-1&&this.push(", ");this.push("] = "),this.visit(t.value)}visitBufferLoadNode(t){this.push(`${t.buffer.name}[`);for(let e=0;e<t.indices.length;e++)this.visit(t.indices[e]),e<t.indices.length-1&&this.push(", ");this.push("]")}visitIfThenElseNode(t){this.push("if ("),this.visit(t.condition),this.push(") {"),this.indent++,this.newline(),this.visit(t.thenBody),this.indent--,this.newline(),this.push("}"),t.elseBody&&(this.push(" else {"),this.indent++,this.newline(),this.visit(t.elseBody),this.indent--,this.newline(),this.push("}"))}visitLetStmtNode(t){this.push(`let ${t.variable.name} = `),this.visit(t.value),this.newline(),this.visit(t.body)}visitAllocateNode(t){this.push(`allocate ${t.buffer.name}[${t.buffer.shape.join(", ")}] (${t.scope}) {`),this.indent++,this.newline(),this.visit(t.body),this.indent--,this.newline(),this.push("}")}visitEvaluateNode(t){this.push("evaluate "),this.visit(t.value)}visitMathOpNode(t){this.push("("),this.visit(t.a),t.b&&(this.push(` ${t.op} `),this.visit(t.b)),this.push(")")}visitCompareNode(t){this.push("("),this.visit(t.a),this.push(` ${t.op} `),this.visit(t.b),this.push(")")}visitCallExternNode(t){this.push(`${t.externName}(`);for(let e=0;e<t.args.length;e++)this.visit(t.args[e]),e<t.args.length-1&&this.push(", ");this.push(")")}visitVariableNode(t){this.push(t.name)}visitIntImmNode(t){this.push(t.value.toString())}visitFloatImmNode(t){this.push(t.value.toString())}visitCastNode(t){this.push(`cast<${t.toDtype}>(`),this.visit(t.expr),this.push(")")}};f(Tb,"printTensorIR")});function eR(n,t){let e=new Jf;for(let[,s]of n.bufferMap)e.paramBuffers.add(s.name);return EC(n.body,e,t),IC(n,e,t),MC(n.body,e),e}function EC(n,t,e){let s=[n];for(;s.length>0;){let r=s.pop();if(!(!r||typeof r!="object")){switch(r.type){case"ForNode":if(r.loopVar&&t.locals.set(r.loopVar.name,"i32"),r.kind===V.THREAD_BINDING&&r.threadTag){let o=r.extent&&r.extent.type==="IntImmNode"?r.extent.value:0,i=!r.extent||r.extent.type!=="IntImmNode",a={varName:r.loopVar.name,extent:o,isDynamic:i,extentNode:r.extent};t.threadBindings.has(r.threadTag)?t.threadBindings.get(r.threadTag).push(a):t.threadBindings.set(r.threadTag,[a])}break;case"LetStmtNode":r.variable&&t.locals.set(r.variable.name,Jr(r.variable.dtype));break;case"BufferStoreNode":case"BufferLoadNode":r.buffer&&t.usedBuffers.set(r.buffer.name,r.buffer);break;case"AllocateNode":r.buffer&&t.allocatedBuffers.add(r.buffer.name),r.scope==="shared"&&r.buffer&&t.sharedBuffers.push(r.buffer);break;case"CallExternNode":r.externName&&!Wx(r.externName)&&t.externCalls.set(r.externName,{argCount:r.args?r.args.length:0,dtype:r.dtype||"f32"});break;case"BlockNode":if(r.reads)for(let o of r.reads)o.buffer&&t.usedBuffers.set(o.buffer.name,o.buffer);if(r.writes)for(let o of r.writes)o.buffer&&t.usedBuffers.set(o.buffer.name,o.buffer);if(r.iterVars)for(let o of r.iterVars)o.iterVar&&t.locals.set(o.iterVar.name,Jr(o.iterVar.dtype)),o.binding&&typeof o.binding=="object"&&o.binding.type&&s.push(o.binding);break}if(r.body&&s.push(r.body),r.value&&typeof r.value=="object"&&r.value.type&&s.push(r.value),r.stmts)for(let o of r.stmts)s.push(o);if(r.thenBody&&s.push(r.thenBody),r.elseBody&&s.push(r.elseBody),r.initBody&&s.push(r.initBody),r.condBody&&s.push(r.condBody),r.loopBody&&s.push(r.loopBody),r.condition&&typeof r.condition=="object"&&r.condition.type&&s.push(r.condition),r.a&&typeof r.a=="object"&&r.a.type&&s.push(r.a),r.b&&typeof r.b=="object"&&r.b.type&&s.push(r.b),r.expr&&typeof r.expr=="object"&&r.expr.type&&s.push(r.expr),r.args)for(let o of r.args)typeof o=="object"&&o!==null&&o.type&&s.push(o);if(r.indices)for(let o of r.indices)typeof o=="object"&&o!==null&&o.type&&s.push(o)}}}function IC(n,t,e){let s=t.memoryLayout.alignment,r=0,o=f(i=>{let a=i.shape.some(u=>typeof u!="number"||u<0),c=i.numel();if(!a&&c>=0)return c*qr(i.dtype);let l=1;for(let u of i.shape)l*=typeof u=="number"&&u>0?u:1;return Math.max(TC,l*qr(i.dtype))},"bufBytes");for(let[,i]of n.bufferMap)r=Math.ceil(r/s)*s,t.memoryLayout.bufferOffsets.set(i.name,r),r+=o(i);for(let[i,a]of t.usedBuffers)t.memoryLayout.bufferOffsets.has(i)||(r=Math.ceil(r/s)*s,t.memoryLayout.bufferOffsets.set(i,r),r+=o(a));t.memoryLayout.totalBytes=r}function MC(n,t){let e=new Map,s=[n];for(;s.length>0;){let r=s.pop();if(!(!r||typeof r!="object")){if(r.type==="BufferStoreNode"&&r.buffer){let o=r.buffer.name;t.paramBuffers.has(o)||(e.has(o)||e.set(o,[]),e.get(o).push(r.value))}if(r.body&&s.push(r.body),r.value&&typeof r.value=="object"&&r.value.type&&s.push(r.value),r.stmts)for(let o of r.stmts)s.push(o);r.thenBody&&s.push(r.thenBody),r.elseBody&&s.push(r.elseBody),r.initBody&&s.push(r.initBody),r.condBody&&s.push(r.condBody),r.loopBody&&s.push(r.loopBody)}}for(let[r,o]of e)if(o.every(a=>a&&a.type==="IntImmNode"&&a.value===0||a&&a.type==="FloatImmNode"&&a.value===0)&&t.zeroBuffers.add(r),o.length===1&&o[0]){let a=o[0];(a.type==="IntImmNode"||a.type==="FloatImmNode")&&t.constantBuffers.set(r,a.value)}}var TC,sR=T(()=>{Zf();As();Ct();f(eR,"scanMetadata");f(EC,"walkTree");TC=65536;f(IC,"computeMemoryLayout");f(MC,"detectZeroBuffers")});function yl(n,t,e){let s=typeof n.offset=="number"?n.offset:0;if(t.length===0)return new C(s);if(t.length===1)return s===0?t[0]:new D("+",t[0],new C(s));let r=[];s!==0&&r.push(new C(s));for(let o=0;o<t.length;o++){let i=t[o];if(i.type==="IntImmNode"&&i.value===0)continue;let a=n.strides[o];if(a===1)r.push(i);else if(typeof a=="number"&&a>=0)r.push(new D("*",i,new C(a)));else{let c=CC(n,o,e);r.push(new D("*",i,c))}}return r.length===0?new C(0):r.reduce((o,i)=>new D("+",o,i))}function CC(n,t,e){let s=[];for(let r=t+1;r<n.shape.length;r++){let o=n.shape[r];typeof o=="number"&&o>=0?s.push(new C(o)):s.push(DC(n,r,e))}return s.length===0?new C(1):s.reduce((r,o)=>new D("*",r,o))}function DC(n,t,e){if(e){let s=`${n.name}:${t}`,r=e.get(s);if(r)return new se(r.name,"index")}return new C(1)}var nR=T(()=>{Ct();f(yl,"flattenIndex");f(CC,"computeDynamicStride");f(DC,"resolveShapeParam")});function rR(n,t){let e=eR(n,t),s={shapeParamMap:n.shapeParamMap,accCounter:0,metadata:e},r=mn(n.body,s);return new Hf(n.name,n.params,r,n.bufferMap,n.shapeParams,n.shapeParamMap,e)}function mn(n,t){if(!n||typeof n!="object")return n;switch(n.type){case"ForNode":return LC(n,t);case"BlockNode":return PC(n,t);case"SeqNode":return zC(n,t);case"BufferStoreNode":return $C(n,t);case"LetStmtNode":return FC(n,t);case"AllocateNode":return VC(n,t);case"IfThenElseNode":return GC(n,t);case"WhileNode":return qC(n,t);case"EvaluateNode":return new If(Cs(n.value,t));case"SyncThreadsNode":return n;default:return n}}function LC(n,t){let e=WC(n);if(e)return jC(n,e,t);let s=mn(n.body,t);return new ht(n.loopVar,n.min,n.extent,n.kind,s,n.threadTag)}function PC(n,t){let e=[];for(let o of n.iterVars)o.iterVar&&o.binding&&e.push({name:o.iterVar.name,dtype:o.iterVar.dtype,expr:Cs(o.binding,t)});let s=mn(n.body,t),r=n.initBody?mn(n.initBody,t):null;return e.length>0?r?(s=new ft([new zi(e,r),new zi(e,s)]),r=null):s=new zi(e,s):r&&(s=new ft([r,s])),s}function zC(n,t){let e=[];for(let s of n.stmts)e.push(mn(s,t));return new ft(e)}function $C(n,t){let e=yl(n.buffer,n.indices,t.shapeParamMap),s=Cs(n.value,t),r=n.buffer.dtype||Ns(n.value);return new Sc(n.buffer,e,s,r)}function FC(n,t){return new Os(n.variable,Cs(n.value,t),mn(n.body,t))}function VC(n,t){return new fs(n.buffer,n.scope,mn(n.body,t))}function GC(n,t){return new Mt(Cs(n.condition,t),mn(n.thenBody,t),n.elseBody?mn(n.elseBody,t):null)}function qC(n,t){return new Ei(n.condVar,mn(n.condBody,t),mn(n.loopBody,t))}function Cs(n,t){if(!n||typeof n!="object"||!n.type)return n;switch(n.type){case"BufferLoadNode":{let e=yl(n.buffer,n.indices,t.shapeParamMap),s=new xc(n.buffer,e,n.buffer.dtype);return br(s),s}case"MathOpNode":{let e=Cs(n.a,t),s=n.b?Cs(n.b,t):null,r={...n,a:e,b:s};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),br(r),r}case"CompareNode":{let e=Cs(n.a,t),s=Cs(n.b,t),r={...n,a:e,b:s};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),br(r),r}case"CastNode":{let e=Cs(n.expr,t),s={...n,expr:e};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),br(s),s}case"CallExternNode":{let e=n.args.map(r=>Cs(r,t)),s={...n,args:e};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),br(s),s}case"IfThenElseNode":{let e=Cs(n.condition,t),s=Cs(n.thenBody,t),r=n.elseBody?Cs(n.elseBody,t):null,o={...n,condition:e,thenBody:s,elseBody:r};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),br(o),o}default:return br(n),n}}function WC(n){let t=n.body;if(!t||t.type!=="BlockNode")return null;let e=t.body;if(!e||e.type!=="BufferStoreNode")return null;let s=e,r=s.value;if(!r||r.type!=="MathOpNode"||!UC.has(r.op))return null;let o=null,i=null;if(r.a&&r.a.type==="BufferLoadNode"&&r.a.buffer.name===s.buffer.name?(o=r.a,i=r.b):r.b&&r.b.type==="BufferLoadNode"&&r.b.buffer.name===s.buffer.name&&(o=r.b,i=r.a),!o)return null;let a=Ib(s.indices),c=Ib(o.indices);if(a!==c||a.includes("?"))return null;let l=s.indices.map(p=>{if(p.type!=="VariableNode")return p;for(let d of t.iterVars)if(d.iterVar&&d.iterVar.name===p.name&&d.binding)return d.binding;return p}),u=n.loopVar.name,h=Ib(l);return h.includes("?")||h.includes("$"+u)?null:{store:s,loadSide:o,valueSide:i,outerIndices:l,block:t,op:s.value.op}}function jC(n,t,e){let s=`_acc_${e.accCounter++}`,r=Ns(t.loadSide);e.metadata.locals.set(s,r);let o=new Map;for(let d of t.block.iterVars)d.iterVar&&d.binding&&o.set(d.iterVar.name,d.binding);let i=o.size>0?qs(t.valueSide,o):t.valueSide,a=yl(t.store.buffer,t.outerIndices,e.shapeParamMap),c=new xc(t.store.buffer,a,t.store.buffer.dtype);br(c);let l=Cs(i,e),u=yl(t.store.buffer,t.outerIndices,e.shapeParamMap),h=new Sc(t.store.buffer,u,null,t.store.buffer.dtype),p=t.block.initBody?o.size>0?bl(t.block.initBody,o):t.block.initBody:null;return new Yf({localName:s,dtype:r,op:t.op,initLoad:c,loopVar:n.loopVar,extent:n.extent,loopKind:n.kind,body:l,flushStore:h,initBody:p?mn(p,e):null})}function qs(n,t){if(!n||typeof n!="object"||!n.type)return n;if(n.type==="VariableNode"&&t.has(n.name))return t.get(n.name);if(n.type==="BufferLoadNode"){let e=n.indices.map(o=>qs(o,t));if(!e.some((o,i)=>o!==n.indices[i]))return n;let r={...n,indices:e};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}if(n.type==="MathOpNode"){let e=qs(n.a,t),s=n.b?qs(n.b,t):null;if(e===n.a&&s===n.b)return n;let r={...n,a:e,b:s};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}if(n.type==="CompareNode"){let e=qs(n.a,t),s=qs(n.b,t);if(e===n.a&&s===n.b)return n;let r={...n,a:e,b:s};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}if(n.type==="CastNode"){let e=qs(n.expr,t);if(e===n.expr)return n;let s={...n,expr:e};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="CallExternNode"){let e=n.args.map(o=>qs(o,t));if(!e.some((o,i)=>o!==n.args[i]))return n;let r={...n,args:e};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}if(n.type==="IfThenElseNode"){let e=qs(n.condition,t),s=qs(n.thenBody,t),r=n.elseBody?qs(n.elseBody,t):null;if(e===n.condition&&s===n.thenBody&&r===n.elseBody)return n;let o={...n,condition:e,thenBody:s,elseBody:r};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),o}return n}function bl(n,t){if(!n||typeof n!="object"||!n.type)return n;switch(n.type){case"BufferStoreNode":{let e=n.indices.map(o=>qs(o,t)),s=qs(n.value,t);if(e.every((o,i)=>o===n.indices[i])&&s===n.value)return n;let r={...n,indices:e,value:s};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}case"SeqNode":{let e=n.stmts.map(r=>bl(r,t)),s={...n,stmts:e};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}case"ForNode":{let e=bl(n.body,t);if(e===n.body)return n;let s={...n,body:e};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}case"IfThenElseNode":{let e=qs(n.condition,t),s=bl(n.thenBody,t),r=n.elseBody?bl(n.elseBody,t):null,o={...n,condition:e,thenBody:s,elseBody:r};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),o}default:return n}}function Ib(n){return n.map(Mb).join(",")}function Mb(n){return n?n.type==="VariableNode"?"$"+n.name:n.type==="IntImmNode"?String(n.value):n.type==="MathOpNode"?"("+Mb(n.a)+n.op+(n.b?Mb(n.b):"")+")":"?":"?"}var UC,oR=T(()=>{Zf();sR();nR();Ct();f(rR,"lowerToLIR");f(mn,"lowerStmt");f(LC,"lowerForNode");f(PC,"lowerBlockNode");f(zC,"lowerSeqNode");f($C,"lowerBufferStore");f(FC,"lowerLetStmt");f(VC,"lowerAllocate");f(GC,"lowerIfThenElse");f(qC,"lowerWhile");f(Cs,"lowerExpr");UC=new Set(["+","*","max","min"]);f(WC,"detectAccumulator");f(jC,"lowerAccumulator");f(qs,"substituteVars");f(bl,"substituteVarsStmt");f(Ib,"indicesKey");f(Mb,"exprKey")});function iR(n){let t=[],e={errors:t,boundVars:new Set,path:[],bufferNames:new Set};if(!n||n.type!=="LIRFunc")return t.push(new fr("root must be LIRFunc")),t;for(let[,s]of n.bufferMap)e.bufferNames.add(s.name);for(let s of n.shapeParams)e.boundVars.add(s.name);if(n.metadata)for(let[s]of n.metadata.locals)e.boundVars.add(s);return gn(n.body,e),t}function gn(n,t){if(!(!n||typeof n!="object")){switch(t.path.push(n.type),n.type){case"ForNode":KC(n,t);break;case"SeqNode":for(let e of n.stmts)gn(e,t);break;case"LIRFlatStoreNode":HC(n,t);break;case"LIRAccumulatorNode":YC(n,t);break;case"LIRBindingsNode":JC(n,t);break;case"LetStmtNode":{es(n.value,t);let e=t.boundVars.has(n.variable.name);t.boundVars.add(n.variable.name),gn(n.body,t),e||t.boundVars.delete(n.variable.name);break}case"AllocateNode":{let e=n.buffer?t.bufferNames.has(n.buffer.name):!0;n.buffer&&t.bufferNames.add(n.buffer.name),gn(n.body,t),n.buffer&&!e&&t.bufferNames.delete(n.buffer.name);break}case"IfThenElseNode":es(n.condition,t),gn(n.thenBody,t),n.elseBody&&gn(n.elseBody,t);break;case"WhileNode":gn(n.condBody,t),gn(n.loopBody,t);break;case"EvaluateNode":es(n.value,t);break;default:break}t.path.pop()}}function KC(n,t){n.extent?es(n.extent,t):t.errors.push(new fr("ForNode missing extent",[...t.path]));let e=!0;n.loopVar?(e=t.boundVars.has(n.loopVar.name),t.boundVars.add(n.loopVar.name)):t.errors.push(new fr("ForNode missing loopVar",[...t.path])),gn(n.body,t),n.loopVar&&!e&&t.boundVars.delete(n.loopVar.name)}function HC(n,t){n.buffer||t.errors.push(new fr("LIRFlatStoreNode missing buffer",[...t.path])),es(n.offsetExpr,t),es(n.value,t)}function YC(n,t){n.localName||t.errors.push(new fr("LIRAccumulatorNode missing localName",[...t.path])),n.dtype||t.errors.push(new fr("LIRAccumulatorNode missing dtype",[...t.path]));let e=n.localName?t.boundVars.has(n.localName):!0;n.localName&&t.boundVars.add(n.localName),es(n.initLoad,t),es(n.body,t),gn(n.flushStore,t),n.initBody&&gn(n.initBody,t),n.localName&&!e&&t.boundVars.delete(n.localName)}function JC(n,t){let e=[];for(let s of n.bindings)es(s.expr,t),t.boundVars.has(s.name)||e.push(s.name),t.boundVars.add(s.name);gn(n.body,t);for(let s of e)t.boundVars.delete(s)}function es(n,t){if(!(!n||typeof n!="object"||!n.type))switch(n.type){case"LIRFlatLoadNode":n.buffer||t.errors.push(new fr("LIRFlatLoadNode missing buffer",[...t.path])),es(n.offsetExpr,t);break;case"MathOpNode":es(n.a,t),n.b&&es(n.b,t);break;case"CompareNode":es(n.a,t),es(n.b,t);break;case"CastNode":es(n.expr,t);break;case"CallExternNode":if(n.args)for(let e of n.args)es(e,t);break;case"IfThenElseNode":es(n.condition,t),es(n.thenBody,t),n.elseBody&&es(n.elseBody,t);break;case"VariableNode":n.name!==void 0&&!t.boundVars.has(n.name)&&t.errors.push(new fr(`unbound variable '${n.name}'`,[...t.path]));break;case"IntImmNode":case"FloatImmNode":break;default:break}}var fr,aR=T(()=>{fr=class{static{f(this,"LIRVerificationError")}constructor(t,e){this.message=t,this.nodePath=e||[]}toString(){let t=this.nodePath.length>0?` at ${this.nodePath.join(" > ")}`:"";return`LIR verification: ${this.message}${t}`}};f(iR,"verifyLIR");f(gn,"verifyStmt");f(KC,"verifyForNode");f(HC,"verifyFlatStore");f(YC,"verifyAccumulator");f(JC,"verifyBindings");f(es,"verifyExpr")});function pm(n){return n&&typeof n=="object"?n:{}}function Cb(n,...t){let e=new Set(t),s={};for(let r of Object.keys(n))e.has(r)||(s[r]=n[r]);return s}function cR(n,t,e={}){return new zr({target:t,...e}).compileFunction(n)}var hm,Db,zr,dm=T(()=>{xo();by();Sy();cA();fA();pA();Ay();dA();yA();bA();wA();vA();OA();TA();Li();lu();ro();LA();Oc();oh();N1();E1();M1();C1();xy();L1();z1();K1();Z1();X1();Ue();Q1();tR();oR();aR();Ue();f(pm,"spread");f(Cb,"omit");hm=class{static{f(this,"CompilerConfig")}constructor(t={}){this.target=t.target,this.verify=t.verify!==!1,this.verifyMode=t.verify==="full"?"full":"normal",this.errorMode=t.errorMode||"strict";let e=t.fusion||{};this.fusion={enabled:e.enabled??t.enableFusion??!0,strategy:e.strategy??t.fusionStrategy??"xla",epilogue:e.epilogue??t.enableEpilogueFusion,...pm(t.fusionConfig),...Cb(e,"enabled","strategy","epilogue")};let s=t.scheduling||{},r=this.target&&typeof this.target.isWebGPU=="function"&&this.target.isWebGPU();this.scheduling={enabled:s.enabled??t.enableSchedule??r,autotune:s.autotune??t.enableAutotune??!1,...pm(t.autotuneConfig),...Cb(s,"enabled","autotune")},this.matmulBackend=t.matmulBackend||"native";let o=t.quantization||{};this.quantization={enabled:o.enabled??t.enableQuantization??!1,...pm(t.quantizationConfig),...Cb(o,"enabled")};let i=t.optimization||{};this.optimization={layout:i.layout??t.enableLayoutOptimization??!1,rematerialization:i.rematerialization??t.enableRematerialization??!1,rematConfig:i.rematConfig??t.rematerializationConfig??{},fastMath:i.fastMath??t.fastMath??!1,maxSimplifyIterations:i.maxSimplifyIterations??t.maxSimplifyIterations??8};let a=t.memory||{};this.memory={alignment:a.alignment??t.memoryAlignment??64,inplaceReuse:a.inplaceReuse??t.enableInplaceReuse??!0,allocStrategy:a.allocStrategy??t.allocStrategy??"best-fit"};let c=t.partition||{};this.partition={enabled:c.enabled??!1,targets:c.targets||[],defaultTarget:c.defaultTarget||null,opTargetOverrides:c.opTargetOverrides||new Map,memoryLimits:c.memoryLimits||new Map,minPartitionSize:c.minPartitionSize||1,costWeights:c.costWeights||{}},this.passContext=t.passContext||null;let l=t.trace||{};this.trace={level:l.level??ot.SILENT,sink:l.sink??null,irSnapshot:{afterGraphPasses:!1,afterLowering:!1,afterScheduling:!1,...pm(l.irSnapshot)}}}get usePartition(){return this.partition.enabled&&this.partition.targets.length>=2}},Db=class{static{f(this,"CompilationResult")}constructor(t,e,s){this.module=t,this.trace=e,this.errors=s||[]}get succeeded(){return this.errors.length===0}get failedFunctions(){let t=new Set;for(let e of this.errors)e.funcName&&t.add(e.funcName);return t}run(t,...e){return this.module.run(t,...e)}async runAsync(t,...e){return this.module.runAsync(t,...e)}isAsync(t){return this.module.isAsync(t)}getSource(t){return this.module.getKernelSource(t)}getSnippet(t){return this.module.getKernelSnippet(t)}listKernels(){return this.module.listKernels()}},zr=class{static{f(this,"Compiler")}constructor(t){if(this.config=t instanceof hm?t:new hm(t),!this.config.target)throw new Error("Compiler requires a target")}compile(t){let e=new nf(this.config.trace),s=this.config.errorMode==="resilient",r=[],o=new Set,i=performance.now();e.phaseStart("compile"),this.config.verify&&this._verifyGraph(t,"before graph passes",e,r,o,s);let a=this._runGraphPasses(t,e,r,o,s);this.config.usePartition&&this._runPartitioning(t,e);let c=typeof this.config.target.isWebGPU=="function"&&this.config.target.isWebGPU(),l=J1(t,{config:this.config,target:this.config.target,cudaMatmulChain:a,isWebGPU:c});this.config.verify&&this._verifyGraph(t,"after graph passes",e,r,o,s);let u=this._lowerAll(t,e,r,o,s);if(this.config.matmulBackend==="cublas")for(let d of u)d.cublasInfo=l&&l.cublasInfos?l.cublasInfos.get(d.name)||null:aS(d);this._scheduleAll(u,e,r,o,s),this.config.verifyMode==="full"&&this._verifyAll(u,r,o,s),this._planMemory(u,e,r,o,s),this.config.verify&&this._verifyAll(u,r,o,s);let h=this._lowerToLIR(u,e,r,o,s),p=this._codegen(h,e,r,o,s);if(l&&(p.executionPlan=l.plan),e.phaseEnd("compile",performance.now()-i),!s&&r.length>0)throw new Error(r[0].toString());return new Db(p,e,r)}compileFunction(t){let e=new _s("single");return e.addFunction(t),this.compile(e)}calibrate(t,e="minmax"){let s=new Jd(e);for(let r of t)s.attach(r);return s}_runGraphPasses(t,e,s,r,o){let i=new Wo;for(let m of Nb("pre",this.config,this.config.target))i.addPass(m);i.addPass(new oa(this.config.target)),i.addPass(new ra("canonicalize",[new jo,new cd({fastMath:this.config.optimization.fastMath}),new ud,new ld,new cr],this.config.optimization.maxSimplifyIterations)),this.config.optimization.layout&&this.config.target&&(i.addPass(new xd({target:this.config.target})),i.addPass(new cr)),this.config.quantization.enabled&&(i.addPass(new vd({...this.config.quantization,target:this.config.target})),i.addPass(new jo),i.addPass(new cr));let a=0;for(let m of t)for(let g of m.ops())g.opName==="dot"&&a++;let c=this.config.target,l=(c.getAttr&&c.getAttr("matmulChainThreshold"))??(c.kind==="cuda"?2:1/0),u=a>=l;if(this.config.matmulBackend!=="cublas"&&!u&&(this.config.fusion.epilogue!==void 0?this.config.fusion.epilogue:this.config.target&&this.config.target.enableEpilogueFusion)&&(i.addPass(new dd({target:this.config.target})),i.addPass(new cr)),this.config.fusion.enabled){let m=this.config.fusion;m.strategy==="dominator"?i.addPass(new yd({target:this.config.target,...m})):(i.addPass(new hd({target:this.config.target,cost:{launchOverheadUs:5},...m})),i.addPass(new md({maxFusionSize:this.config.target?.maxFusionSize,...m})),i.addPass(new gd({maxFusionSize:this.config.target?.maxFusionSize,...m}))),i.addPass(new cr)}if(this.config.matmulBackend==="cublas"&&i.addPass(new um),this.config.optimization.rematerialization){let m={...this.config.optimization.rematConfig};m.memoryBudget===void 0&&this.config.target&&this.config.target.memoryBudgetBytes>0&&(m.memoryBudget=this.config.target.memoryBudgetBytes),i.addPass(new Qd(m))}for(let m of Nb("post",this.config,this.config.target))i.addPass(m);i.setTrace(e),this.config.verifyMode==="full"&&i.setVerifyHook((m,g)=>(g?_b(m):hl(m)).map(y=>y.toString())),e.phaseStart("graphPasses");let p=performance.now(),d=i.run(t,{errorMode:o?"resilient":"strict",passContext:this.config.passContext});if(d.errors){for(let m of d.errors)s.push(m),e.errorEvent(m.phase,m.funcName,m.message,m.passName);if(d.failedFunctions)for(let m of d.failedFunctions)r.add(m)}if(e.phaseEnd("graphPasses",performance.now()-p),e.shouldSnapshot("afterGraphPasses")){let m=new fm;e.irDump("afterGraphPasses",m.printModule(t))}return u}_runPartitioning(t,e){let s=new Wo;s.addPass(new nm(this.config.partition)),s.addPass(new rm({targets:this.config.partition.targets})),s.setTrace(e),e.phaseStart("partition");let r=performance.now();s.run(t),e.phaseEnd("partition",performance.now()-r)}_lowerAll(t,e,s,r,o){e.phaseStart("lowering");let i=performance.now(),a=[];for(let c of t)if(!r.has(c.name))try{let l=performance.now(),u=wc(c,this.config.target);e.functionEvent("lowering",c.name,{durationMs:performance.now()-l}),a.push(u),e.shouldSnapshot("afterLowering")&&e.irDump("afterLowering:"+c.name,Tb(u))}catch(l){let u=new qe("lowering",c.name,l.message);if(s.push(u),r.add(c.name),e.errorEvent("lowering",c.name,l.message),!o)break}return e.phaseEnd("lowering",performance.now()-i),a}_scheduleAll(t,e,s,r,o){e.phaseStart("scheduling");let i=performance.now(),a=this.config.scheduling;if(a.autotune){let c=new Kd(this.config.target,a);for(let l of t)if(!r.has(l.name)&&!l.cublasInfo)try{let u=performance.now(),h=c.tuneAndApply(l),p=performance.now()-u,d=0,m=0;if(h&&h.results){m=h.results.size;for(let[g,_]of h.results)_.fromCache&&d++,e.explainsEnabled&&e.explain("schedule",g,_.sketchName,`autotuned: best of search${_.fromCache?" (cached)":""}, score ${_.score!=null?_.score.toFixed(3):"n/a"}`,{target:this.config.target.name,params:_.params})}e.autotuneStats(l.name,{durationMs:p,blockCount:m,applied:!!(h&&h.applied),cacheHits:d})}catch(u){let h=new qe("scheduling",l.name,u.message);if(s.push(h),r.add(l.name),e.errorEvent("scheduling",l.name,u.message),!o)break}}else if(a.enabled){let c=new Cr(this.config.target,null,e);for(let l of t)if(!r.has(l.name)&&!l.cublasInfo)try{let u=performance.now(),h=new Ts(l);c.applyToAllBlocks(h),e.functionEvent("scheduling",l.name,{durationMs:performance.now()-u})}catch(u){let h=new qe("scheduling",l.name,u.message);if(s.push(h),r.add(l.name),e.errorEvent("scheduling",l.name,u.message),!o)break}}if(e.phaseEnd("scheduling",performance.now()-i),e.shouldSnapshot("afterScheduling"))for(let c of t)r.has(c.name)||e.irDump("afterScheduling:"+c.name,Tb(c))}_planMemory(t,e,s,r,o){e.phaseStart("memoryPlanning");let i=performance.now(),a=this.config.memory.alignment||this.config.target?.cacheLineSizeBytes||64,c=new Rd({alignment:a,enableInplace:this.config.memory.inplaceReuse,allocStrategy:this.config.memory.allocStrategy});for(let l of t)if(!r.has(l.name)&&!l.gpuRegisterBlocked)try{let u=performance.now(),{plan:h}=c.planAndRewrite(l),p=h.getReport();e.memoryStats(l.name,{durationMs:performance.now()-u,peakMemory:p.peakMemory,totalTemporaries:p.totalTemporaries,totalInplace:p.totalInplace})}catch(u){let h=new qe("memoryPlanning",l.name,u.message);if(s.push(h),r.add(l.name),e.errorEvent("memoryPlanning",l.name,u.message),!o)break}e.phaseEnd("memoryPlanning",performance.now()-i)}_verifyGraph(t,e,s,r,o,i){if(i){for(let c of t){if(o.has(c.name))continue;let l=hl?hl(c):[];if(l.length>0){let u=l.map(h=>h.toString()).join("; ");r.push(new qe("verification",c.name,u)),o.add(c.name),s.errorEvent("verification",c.name,u)}}return}let a=_b(t);if(a.length>0)throw new Error("Graph verification failed ("+e+"): "+a.map(c=>c.toString()).join("; "))}_verifyAll(t,e,s,r){let o=new Hd;for(let i of t){if(s.has(i.name))continue;let a=o.verify(i);if(a.length>0){let c=a.join("; ");if(r)e.push(new qe("verification",i.name,c)),s.add(i.name);else throw new Error("TensorIR verification failed for "+i.name+": "+c)}}}_lowerToLIR(t,e,s,r,o){e.phaseStart("lirLowering");let i=performance.now(),a=[];for(let c of t)if(!r.has(c.name))try{let l=performance.now(),u=rR(c,this.config.target);if(c.cublasInfo&&(u.cublasInfo=c.cublasInfo),c.gpuRegisterBlocked&&(u.gpuRegisterBlocked=!0),this.config.verifyMode==="full"){let h=iR(u);if(h.length>0)throw new Error("LIR verification failed: "+h.map(p=>p.toString()).join("; "))}e.functionEvent("lirLowering",c.name,{durationMs:performance.now()-l}),a.push(u)}catch(l){let u=new qe("lirLowering",c.name,l.message);if(s.push(u),r.add(c.name),e.errorEvent("lirLowering",c.name,l.message),!o)break}return e.phaseEnd("lirLowering",performance.now()-i),a}_codegen(t,e,s,r,o){e.phaseStart("codegen");let i=performance.now(),a=new na("compiled"),c=this.config.usePartition,l={matmulBackend:this.config.matmulBackend},u=new Map,h=f(d=>(u.has(d.name)||u.set(d.name,new Wn(d,l)),u.get(d.name)),"getBackend"),p=c?null:new Wn(this.config.target,l);for(let d of t)if(!r.has(d.name))try{let m=performance.now(),g;if(c){let y=d._partitionTarget,b=y?this.config.partition.targets.find(x=>x.name===y):this.config.target;g=h(b||this.config.target)}else g=p;let _=g.compile(d);a.addCompiledKernel(_),d.shapeParamMap&&d.shapeParamMap.size>0&&a.setShapeParamMap(d.name,d.shapeParamMap,d.bufferMap),e.codegenStats(d.name,{durationMs:performance.now()-m,sourceSize:_.source.length,targetName:_.target.name})}catch(m){let g=new qe("codegen",d.name,m.message);if(s.push(g),r.add(d.name),e.errorEvent("codegen",d.name,m.message),!o)break}return e.phaseEnd("codegen",performance.now()-i),a}};f(cR,"compileGraph")});function XC(n){return(t,...e)=>{let s=Zo();if(!s)throw new Error(`TRACING dispatch key active but no tracer is set for op '${n}'`);let r=[],o={},i=ZC[n],a=0,c=f(u=>u instanceof ue||u&&u._impl,"isTensor"),l=f(u=>{u instanceof ue||u&&u._impl&&u.isSymbolic?r.push(u):u&&u._impl&&r.push(s.captureConstant(u))},"pushTensor");for(let u of e)if(Array.isArray(u)&&u.length>0&&c(u[0]))for(let h of u)l(h);else c(u)?l(u):i&&(u!=null&&a<i.length&&(o[i[a]]=u),a++);return s.recordOp(n,r,o)}}function lR(){let n=Re.listOps();for(let t of n){let e=Re.findOp(t);if(!e)continue;let s=e.name,r=Ls.fromUnboxed(XC(s));e.entry.registerKernel(nt.TRACING,r)}}var ZC,fR=T(()=>{js();ec();mi();zu();ga();ZC={sum:["dim","keepdim"],mean:["dim","keepdim"],max:["dim","keepdim"],min:["dim","keepdim"],prod:["dim","keepdim"],argmax:["dim","keepdim"],argmin:["dim","keepdim"],transpose:["dim0","dim1"],softmax:["dim"],log_softmax:["dim"],layer_norm:["axis","eps"],batch_norm:["axis","eps"],conv2d:["strides","padding","dilation","groups"],pool2d:["pool_type","kernel_size","strides","padding"],pad:["low","high"],one_hot:["depth"],index_select:["dim"],gather:["dim"],scatter_add:["dim"],cat:["dim"],stack:["dim"]};f(XC,"_tracingKernel");f(lR,"registerTracingDispatch")});function t2(n){return n&&n.shape&&n.shape.length>=2&&QC.has(n.dtype)}function pR(n,t,e=t2){let s=n.graph.functions().next().value;if(!s)return n;let r=s.entryBlock,o=n.numUserInputs,i=n.capturedParams,a=[];for(let h=0;h<i.length;h++){let p=o+h,d=r.getArgument(p);d&&e(i[h],h,d)&&a.push({j:h,argIndex:p,arg:d,param:i[h]})}if(a.length===0)return n;for(let h of a){let p=t(h.param),d=h.arg.type,m=new mt("constant",[],[d],{value:p,tensor_type:d}),g=r.firstOp;g?r.insertBefore(m,g):r.pushOp(m),h.arg.replaceAllUsesWith(m.getResult(0))}let c=new Set(a.map(h=>h.argIndex));r.removeArguments(c),s.inputTypes=Object.freeze(s.inputTypes.filter((h,p)=>!c.has(p)));let l=new Set(a.map(h=>h.j)),u=i.filter((h,p)=>!l.has(p));return{...n,capturedParams:u}}var QC,hR=T(()=>{Rs();QC=new Set(["f16","f32","f64"]);f(t2,"defaultWeightPredicate");f(pR,"foldWeightParams")});function e2(){dR||(lR(),dR=!0)}function s2(n,t){if(!n)return new Array(t.length).fill(null);let e=new Array(t.length);for(let s=0;s<t.length;s++){let r=n[s];if(r===!0){let o=new Set;for(let i=0;i<t[s].shape.length;i++)o.add(i);e[s]=o}else r instanceof Set?e[s]=r:e[s]=null}return e}function ym(n,t,e){e2();let s=e?.name||n.name||"traced",r=new ed(s),o=s2(e?.dynamicShapes,t);for(let h=0;h<t.length;h++)r.createInput(t[h].shape,t[h].dtype,o[h]);let i=t.length,a=r._initGraph();function c(h){Array.isArray(h)?r.markOutputs(h):r.markOutput(h),r.deactivate();let p=r.getGraphModule(),d=p.functions().next().value;return{graph:p,capturedParams:[...r.capturedParams],numUserInputs:i,outputTypes:d.outputTypes,shapeEnv:r.shapeEnv,outputSymShapes:r.outputSymShapes}}f(c,"_finalize"),r.activate();let l=Ws.fromKey(nt.TRACING),u=lw(l,()=>n(...a));if(u&&typeof u.then=="function")return u.then(h=>c(h),h=>{throw r.deactivate(),h});try{return c(u)}catch(h){throw r.deactivate(),h}}function mR(n,t,e){let s=ym(n,t,e);return s&&typeof s.then=="function"?s.then(r=>r.graph):s.graph}function n2(n,t,e){let s=n.result.listKernels();if(s.length===0)throw new Error("No kernels compiled");let r=s[0],o=t.length>0?t[0].device:"cpu",i=new Array(t.length);for(let g=0;g<t.length;g++)i[g]=Is(t[g]);let a=n.capturedParams,c=new Array(a.length);for(let g=0;g<a.length;g++)c[g]=Is(a[g]);let l=n.outputTypes,u=n.outputSymShapes,h=new Array(l.length),p=new Array(l.length);for(let g=0;g<l.length;g++){let _=u&&e?e.resolveSymbolicShape(u[g]):l[g].shape,y=l[g].dtype,b=Pe(_),x=Jt(y);h[g]=new x(Math.max(b,1)),p[g]=_}let d=new Array(i.length+c.length+h.length),m=0;for(let g=0;g<i.length;g++)d[m++]=new ar(i[g],t[g].shape,t[g].dtype);for(let g=0;g<c.length;g++)d[m++]=new ar(c[g],a[g].shape,a[g].dtype);for(let g=0;g<h.length;g++)d[m++]=new ar(h[g],p[g],l[g].dtype);return{funcName:r,device:o,outputTypes:l,outputArrays:h,outputShapes:p,allArgs:d}}function Lb(n,t,e,s){if(t.length===1)return jt(e[0],s[0],t[0].dtype,n);let r=new Array(t.length);for(let o=0;o<t.length;o++)r[o]=jt(e[o],s[o],t[o].dtype,n);return r}function r2(n,t,e){let{funcName:s,device:r,outputTypes:o,outputArrays:i,outputShapes:a,allArgs:c}=n2(n,t,e),l=n.result.module.executionPlan;return l?n.result.module.runPlanAsync(l,c).then(()=>Lb(r,o,i,a)):n.result.isAsync(s)?n.result.runAsync(s,...c).then(()=>Lb(r,o,i,a)):(n.result.run(s,...c),Lb(r,o,i,a))}function o2(n,t,e){if(e?.backward)return _compileWithBackward(n,t,e);let s=e?.target??Gn(),r={target:s,verify:!1,...e},o=e?.dynamic_shapes||null,i=e?.shapeBuckets||null,a=e?.foldWeights??e?.quantization?.foldWeights??!1,c=[];function l(b,x,S){if(!b||typeof b!="object"||b.repro)return b;try{b.repro={name:n.constructor?.name||"compiled",phase:S,target:s?.name,inputs:(x||[]).map(v=>({shape:v.shape,dtype:v.dtype})),config:{fusion:r.fusion,scheduling:r.scheduling,optimization:r.optimization,quantization:r.quantization,dynamicShapes:!!o}}}catch{return b}return b}f(l,"_attachRepro");function u(b){let x=a?pR(b,Is):b;return{result:new zr(r).compile(x.graph),graph:x.graph,capturedParams:x.capturedParams,numUserInputs:x.numUserInputs,outputTypes:x.outputTypes,shapeEnv:x.shapeEnv,outputSymShapes:x.outputSymShapes}}f(u,"_finalize");function h(b,x){try{let S=ym((...v)=>n.forward(...v),b,{name:n.constructor.name||"compiled",dynamicShapes:x});return S&&typeof S.then=="function"?S.then(u,v=>{throw l(v,b,"compile")}):u(S)}catch(S){throw l(S,b,"compile")}}f(h,"_compileWith");function p(b){return h(b,o)}f(p,"_compile");function d(b){return b.map((x,S)=>({shape:x,dtype:t[S].dtype}))}f(d,"_bucketInputs");function m(b){for(let x=0;x<c.length;x++){let S=c[x];S.shapeEnv.bindInputShapes(b);let{passed:v}=S.shapeEnv.evaluateGuards();if(v)return S}return null}f(m,"_findCachedEntry");function g(b,x){try{let S=r2(b,x,b.shapeEnv);return S&&typeof S.then=="function"?S.then(void 0,v=>{throw l(v,x,"run")}):S}catch(S){throw l(S,x,"run")}}f(g,"_execute");function _(...b){let x=m(b);if(!x){let S=p(b);if(S&&typeof S.then=="function")return S.then(v=>(c.push(v),v.shapeEnv.bindInputShapes(b),g(v,b)));x=S,c.push(x),x.shapeEnv.bindInputShapes(b)}return g(x,b)}f(_,"compiledForward");let y=null;if(t){let b=[];if(i)for(let x of i)b.push(h(d(x),null));if(b.push(p(t)),b.some(x=>x&&typeof x.then=="function"))y=Promise.all(b).then(x=>{for(let S of x)c.push(S)});else for(let x of b)c.push(x)}return _.original=n,_.graph=b=>mR((...S)=>n.forward(...S),b||t,{name:n.constructor.name||"compiled",dynamicShapes:o}),_.source=()=>{if(c.length===0)return null;let b=c[0],x=b.result.listKernels();return x.length>0?b.result.getSource(x[0]):null},_.kernels=()=>c.length===0?[]:c[0].result.listKernels(),_.snippet=()=>{if(c.length===0)return null;let b=c[0],x=b.result.listKernels();return x.length>0?b.result.getSnippet(x[0]):null},_.result=()=>c.length>0?c[0].result:null,_._ready=y,_}var dR,Pb=T(()=>{zu();fR();js();Pm();dm();qn();hn();oh();Qs();kn();hR();dR=!1;f(e2,"_ensureTracing");f(s2,"_normalizeDynamicShapes");f(ym,"_traceCore");f(mR,"trace");f(n2,"_prepareExecution");f(Lb,"_wrapOutputs");f(r2,"executeCompiled");f(o2,"compile")});var Ln,wl=T(()=>{Ln=class{static{f(this,"GradAccumulator")}constructor(t){this._builder=t,this._grads=new Map}accumulate(t,e){if(!e)return;let s=this._grads.get(t);if(!s)this._grads.set(t,e);else{let r=this._builder.add(s,e).getResult(0);this._grads.set(t,r)}}get(t){return this._grads.get(t)||null}has(t){return this._grads.has(t)}}});function rt(n,t){zb.set(n,t)}function li(n){return zb.get(n)||null}function bm(n){let t=zb.get(n);if(t)return t;if($b.has(n))return null;throw new Error(`autodiff: op '${n}' is on the gradient path but has no VJP rule and is not a registered gradient barrier. Register one with registerVJPRule('${n}', ...) or registerGradientBarrier('${n}').`)}function Fb(n,t){gR.set(n,t)}function _R(n){return gR.get(n)||null}function Vb(n){$b.add(n)}function wm(n){return $b.has(n)}var zb,$b,gR,yn=T(()=>{zb=new Map,$b=new Set;f(rt,"registerVJPRule");f(li,"getVJPRule");f(bm,"requireVJPRuleOrBarrier");gR=new Map;f(Fb,"registerRegionVJP");f(_R,"getRegionVJP");f(Vb,"registerGradientBarrier");f(wm,"isGradientBarrier")});function xl(n){let t=new Set(n.arguments.map(r=>r.id));for(let r of n.ops())for(let o of r.results)t.add(o.id);let e=new Set,s=[];for(let r of n.ops())for(let o of r.operands)t.has(o.id)||e.has(o.id)||o.definingOp&&o.definingOp.opName==="constant"||(e.add(o.id),s.push(o));return s}function xm(n,t){let e=n.scalarConstant(0,t.type.dtype).getResult(0);return n.broadcast(e,t.type.shape,[]).getResult(0)}function yR(n,t,e){let s=t.type.shape,r=s.map((a,c)=>c===0?e:0),o=s.map((a,c)=>c===0?e+1:a),i=n.slice(t,r,o).getResult(0);return n.reshape(i,s.slice(1)).getResult(0)}function i2(n,t,e){let s=e.slice(1),r=t.map(o=>n.reshape(o,[1,...s]).getResult(0));return r.length===1?n.reshape(r[0],e).getResult(0):n.concat(r,0).getResult(0)}function Sm(n,t,e,s,r,o,i=new Map){let a=new Map;for(let g=0;g<t.arguments.length;g++)a.set(t.arguments[g].id,e[g]);for(let[g,_]of s)a.set(g,_);let c=[],l=null;for(let g of t.ops())g.opName==="yield"?l=g:c.push(g);let u=f(g=>{if(a.has(g.id))return a.get(g.id);let _=g.definingOp;if(_&&_.opName==="constant"){let y=i.get(g.id);return y===void 0&&(y=n._buildOp("constant",[],[g.type],new Map(_.attributes),null).getResult(0),i.set(g.id,y)),a.set(g.id,y),y}return g},"matOperand");for(let g of c){let _=g.operands.map(u),y=n._buildOp(g.opName,_,g.results.map(b=>b.type),new Map(g.attributes),null);for(let b=0;b<g.numResults;b++)a.set(g.getResult(b).id,y.getResult(b))}let h=l.operands.map(g=>a.get(g.id));if(o)return{forwardYields:h};let p=new Ln(n);for(let g=0;g<l.operands.length;g++)r[g]&&p.accumulate(l.operands[g].id,r[g]);for(let g=c.length-1;g>=0;g--){let _=c[g];if(_.opName==="constant")continue;let y=_.results.map(v=>p.get(v.id));if(y.every(v=>v===null))continue;let b=bm(_.opName);if(!b)continue;let x={builder:n,op:_,operands:_.operands.map(u),results:_.results.map(v=>a.get(v.id)),gradOutputs:y,attrs:_.attributes},S=b(x);if(S)for(let v=0;v<_.numOperands;v++)S[v]&&p.accumulate(_.getOperand(v).id,qb(n,S[v],_.getOperand(v).type.shape))}let d=t.arguments.map(g=>p.get(g.id)),m=new Map;for(let g of s.keys())m.set(g,p.get(g));return{forwardYields:h,gradArgs:d,gradFree:m}}function Gb(n,t){for(let e of n.ops())if(a2.has(e.opName))throw new Error(`${t} VJP does not support nested control flow ('${e.opName}') in the body`)}function c2(n,t,e,s,r){let o=n.regions[0].blocks[0],i=n.regions[1].blocks[0];Gb(o,"if"),Gb(i,"if");let a=s(n.getOperand(0)),c=[];for(let _=0;_<n.numResults;_++)c.push(t.get(n.getResult(_).id));let l=xl(o),u=xl(i),h=new Map(l.map(_=>[_.id,s(_)])),p=new Map(u.map(_=>[_.id,s(_)])),{gradFree:d}=Sm(e,o,[],h,c,!1),{gradFree:m}=Sm(e,i,[],p,c,!1),g=new Map;for(let _ of l)g.set(_.id,_);for(let _ of u)g.set(_.id,_);for(let[_,y]of g){if(!r.has(_))continue;let b=d.get(_),x=m.get(_);if(!b&&!x)continue;let S=xm(e,y),v=e.broadcast(a,y.type.shape,[]).getResult(0);t.accumulate(_,e.select(v,b??S,x??S).getResult(0))}}function u2(n,t){if(!n||t<=1)return null;if(n==="sqrt"||n===!0)return Math.max(1,Math.ceil(Math.sqrt(t)));if(typeof n=="number"&&n>=1){let e=Math.floor(n);return e>=t?null:e}return null}function l2(n,t,e,s,r,o=null){let i=n.regions[0].blocks[0];Gb(i,"scan");let a=n.getAttr("num_carry"),c=n.getAttr("num_xs"),l=n.numResults-a,u=[];for(let k=0;k<c;k++)u.push(n.getOperand(k));let h=[];for(let k=0;k<a;k++)h.push(n.getOperand(c+k));let p=u[0].type.shape[0],d=xl(i),m=u.map(s),g=h.map(s),_=new Map(d.map(k=>[k.id,s(k)])),y=new Map,b=f(k=>m.map(N=>yR(e,N,k)),"sliceX"),x=f((k,N)=>Sm(e,i,[...k,...N],_,null,!0,y).forwardYields.slice(0,a),"stepForward"),S=[];for(let k=0;k<l;k++)S.push(t.get(n.getResult(a+k).id));let v=[];for(let k=0;k<a;k++){let N=t.get(n.getResult(k).id);v.push(N??xm(e,g[k]))}let w=new Map,A=m.map(()=>new Array(p)),I=f((k,N,R)=>{let O=[...N,...R],M=S.map(W=>W===null?null:yR(e,W,k)),F=[...v,...M],{gradArgs:P,gradFree:G}=Sm(e,i,O,_,F,!1,y);for(let W=0;W<c;W++)A[W][k]=P[W]??xm(e,N[W]);v=[];for(let W=0;W<a;W++)v.push(P[c+W]??xm(e,R[W]));for(let[W,$]of G){if(!$)continue;let L=w.get(W);w.set(W,L?e.add(L,$).getResult(0):$)}},"backwardStep"),B=u2(o,p);if(B){let k=Math.ceil(p/B),N=new Array(k),R=g;N[0]=R;for(let O=0;O<p;O++){R=x(b(O),R);let M=(O+1)/B;Number.isInteger(M)&&M<k&&(N[M]=R)}for(let O=k-1;O>=0;O--){let M=O*B,F=Math.min(M+B,p),P=[],G=[N[O]],W=N[O];for(let $=M;$<F;$++){let L=b($);P.push(L),W=x(L,W),G.push(W)}for(let $=F-1;$>=M;$--)I($,P[$-M],G[$-M])}}else{let k=g,N=[k],R=[];for(let O=0;O<p;O++){let M=b(O);R.push(M),k=x(M,k),N.push(k)}for(let O=p-1;O>=0;O--)I(O,R[O],N[O])}for(let k=0;k<c;k++)r.has(u[k].id)&&t.accumulate(u[k].id,i2(e,A[k],m[k].type.shape));for(let k=0;k<a;k++)r.has(h[k].id)&&t.accumulate(h[k].id,v[k]);for(let[k,N]of w)N&&r.has(k)&&t.accumulate(k,N)}var a2,bR=T(()=>{wl();yn();Sl();Fb("scan",(n,t)=>l2(n,t.accumulator,t.builder,t.materialize,t.needsGrad,t.scanCheckpoint));Fb("if",(n,t)=>c2(n,t.accumulator,t.builder,t.materialize,t.needsGrad));f(xl,"regionFreeVars");f(xm,"zeroLike");f(yR,"sliceStep");f(i2,"stackSteps");f(Sm,"diffBodyStep");a2=new Set(["scan","while","if"]);f(Gb,"assertNoNestedControlFlow");f(c2,"buildCondBackward");f(u2,"resolveSegmentLength");f(l2,"buildScanBackward")});function Bl(n,{accumulator:t,builder:e,needsGrad:s,resolveValue:r,handleRegionOp:o=null}){for(let i=n.length-1;i>=0;i--){let a=n[i];if(a.opName==="return"||a.opName==="constant"||!a.results.some(m=>s.has(m.id)))continue;let l=[];for(let m=0;m<a.numResults;m++)l.push(t.get(a.getResult(m).id));if(l.every(m=>m===null)||o&&o(a))continue;let u=bm(a.opName);if(!u)continue;let h=new Array(a.numOperands);for(let m=0;m<a.numOperands;m++)h[m]=r(a.getOperand(m));let p=new Array(a.numResults);for(let m=0;m<a.numResults;m++)p[m]=r(a.getResult(m));let d=u({builder:e,op:a,operands:h,results:p,gradOutputs:l,attrs:a.attributes});if(d)for(let m=0;m<a.numOperands;m++){if(m>=d.length||!d[m])continue;let g=a.getOperand(m);s.has(g.id)&&t.accumulate(g.id,qb(e,d[m],g.type.shape))}}}function wR(n){let t=[];for(let e of n.regions)e.blocks[0]&&t.push(...xl(e.blocks[0]));return t}function qb(n,t,e){let s=t.type.shape;if(s.length===e.length&&s.every((c,l)=>c===e[l]))return t;let r=s.length-e.length,o=[];for(let c=0;c<r;c++)o.push(c);for(let c=0;c<e.length;c++)e[c]===1&&s[r+c]!==1&&o.push(r+c);let i=t;if(o.length>0){let c=n.scalarConstant(0,t.type.dtype).getResult(0);i=n.reduce(t,c,o,"sum").getResult(0)}let a=i.type.shape;return a.length===e.length&&a.every((c,l)=>c===e[l])||(i=n.reshape(i,e).getResult(0)),i}var vl,kl,Sl=T(()=>{Wr();zn();ur();wl();yn();bR();vl=new Set(["scan","if"]);f(Bl,"backpropOps");f(wR,"regionControlFlowFreeVars");f(qb,"reduceGradToOperandShape");kl=class{static{f(this,"BackwardGraphBuilder")}constructor(t={}){this._rematPolicy=t.rematPolicy||null,this._checkpointPolicy=t.checkpointPolicy||null,this._scanCheckpoint=t.scanCheckpoint||null}build(t){if(this._checkpointPolicy)return this._buildCheckpointed(t);let s=qt.compute(t).topologicalOrder,r=t.getReturnOp();if(!r)throw new Error("Forward function has no return op");let o=r.operands,i=t.args,a=this._computeGradReachability(t,s),{savedValues:c,savedValueIndices:l}=this._identifySavedValues(s,a,i),u=o.map(w=>w.type),h=c.map(w=>w.type),p=[...u,...h],d=[];for(let w=0;w<i.length;w++)a.has(i[w].id)&&d.push(i[w].type);let m=new os(`backward_${t.name}`,p,d),g=new We(m),_=m.args,y=_.slice(0,u.length),b=_.slice(u.length),x=new Map;for(let w=0;w<c.length;w++)x.set(c[w].id,b[w]);for(let w=0;w<i.length;w++)l.has(i[w].id)&&x.set(i[w].id,b[l.get(i[w].id)]);let S=new Ln(g);for(let w=0;w<o.length;w++){let A=o[w];S.accumulate(A.id,y[w])}Bl(s,{accumulator:S,builder:g,needsGrad:a,resolveValue:f(w=>this._materialize(w,x,g),"resolveValue"),handleRegionOp:f(w=>{let A=_R(w.opName);return A?(A(w,{accumulator:S,builder:g,materialize:f(I=>this._materialize(I,x,g),"materialize"),needsGrad:a,scanCheckpoint:this._scanCheckpoint}),!0):!1},"handleRegionOp")});let v=[];for(let w=0;w<i.length;w++)if(a.has(i[w].id)){let A=S.get(i[w].id);if(A)v.push(A);else{let I=g.scalarConstant(0,i[w].type.dtype).getResult(0),B=g.broadcast(I,i[w].type.shape,[]).getResult(0);v.push(B)}}return g.returnOp(v),{backwardFunc:m,savedValues:c,gradInputIndices:this._getGradInputIndices(i,a)}}_materialize(t,e,s){if(e.has(t.id))return e.get(t.id);if(!t.definingOp)return t;let r=new Set([t.id]),o=[{val:t,i:0}];for(;o.length>0;){let i=o[o.length-1],a=i.val,c=a.definingOp;if(e.has(a.id)||!c){r.delete(a.id),o.pop();continue}if(i.i<c.numOperands){let p=c.getOperand(i.i);i.i++,p.definingOp&&!e.has(p.id)&&!r.has(p.id)&&(r.add(p.id),o.push({val:p,i:0}));continue}let l=new Array(c.numOperands);for(let p=0;p<c.numOperands;p++){let d=c.getOperand(p);l[p]=e.has(d.id)?e.get(d.id):d}let u=c.results.map(p=>p.type),h=s._buildOp(c.opName,l,u,new Map(c.attributes),null);for(let p=0;p<c.numResults;p++)e.set(c.getResult(p).id,h.getResult(p));r.delete(a.id),o.pop()}return e.has(t.id)?e.get(t.id):t}_computeGradReachability(t,e){let s=new Set,r=t.getReturnOp();for(let o of r.operands)s.add(o.id);for(let o=e.length-1;o>=0;o--){let i=e[o];if(!(i.opName==="return"||!i.results.some(c=>s.has(c.id)))){if(vl.has(i.opName)){for(let c=0;c<i.numOperands;c++)s.add(i.getOperand(c).id);for(let c of wR(i))s.add(c.id);continue}if(li(i.opName)&&!wm(i.opName))for(let c=0;c<i.numOperands;c++)s.add(i.getOperand(c).id)}}return s}_identifySavedValues(t,e,s){let r=[],o=new Map,i=new Set(s.map(h=>h.id));for(let h of t)if(!(h.opName==="return"||h.opName==="constant"||!li(h.opName)||!h.results.some(m=>e.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,r.length),r.push(g))}for(let m=0;m<h.numResults;m++){let g=h.getResult(m);e.has(g.id)&&!o.has(g.id)&&this._shouldSaveResult(h)&&(o.set(g.id,r.length),r.push(g))}}let a=new Set(o.keys()),c=new Map(s.map(h=>[h.id,h])),l=new Set,u=f(h=>{if(a.has(h.id)||l.has(h.id))return;l.add(h.id);let p=h.definingOp;if(!p){i.has(h.id)&&!o.has(h.id)&&(o.set(h.id,r.length),r.push(c.get(h.id)));return}for(let d=0;d<p.numOperands;d++)u(p.getOperand(d))},"collect");for(let h of t)if(!(h.opName==="return"||h.opName==="constant")&&h.results.some(p=>e.has(p.id))){if(vl.has(h.opName)){for(let p of h.operands)u(p);for(let p of wR(h))u(p);continue}if(li(h.opName)){for(let p=0;p<h.numOperands;p++)u(h.getOperand(p));for(let p=0;p<h.numResults;p++)u(h.getResult(p))}}return{savedValues:r,savedValueIndices:o}}_shouldSaveResult(t){if(this._rematPolicy)return!this._rematPolicy.shouldRematerialize(t);let e=t.opName;return!new Set(["neg","abs","sign","floor","ceil"]).has(e)}_getGradInputIndices(t,e){let s=[];for(let r=0;r<t.length;r++)e.has(t[r].id)&&s.push(r);return s}_buildCheckpointed(t){let s=qt.compute(t).topologicalOrder,r=t.getReturnOp();if(!r)throw new Error("Forward function has no return op");let o=r.operands,i=t.args,a=this._computeGradReachability(t,s),c=this._checkpointPolicy.segment(s,t);for(let k of c)for(let N of k.ops)if(vl.has(N.opName))throw new Error(`Checkpointed backward does not support region control-flow op '${N.opName}'; build the backward without a checkpointPolicy, which differentiates scan/if via buildScanBackward/buildCondBackward.`);let l=new Set,u=[],h=new Map,p=new Set(i.map(k=>k.id));for(let k of i)a.has(k.id)&&!l.has(k.id)&&(l.add(k.id),h.set(k.id,u.length),u.push(k));for(let k of c){for(let N of k.boundaryInputs)if(!p.has(N)&&!l.has(N)){l.add(N);let R=this._findValue(s,N);R&&(h.set(N,u.length),u.push(R))}for(let N of k.boundaryOutputs)if(!l.has(N)){l.add(N);let R=this._findValue(s,N);R&&(h.set(N,u.length),u.push(R))}}for(let k of o)l.has(k.id)||(l.add(k.id),h.set(k.id,u.length),u.push(k));let d=o.map(k=>k.type),m=u.map(k=>k.type),g=[...d,...m],_=[];for(let k=0;k<i.length;k++)a.has(i[k].id)&&_.push(i[k].type);let y=new os(`backward_${t.name}`,g,_),b=new We(y),x=y.args,S=x.slice(0,d.length),v=x.slice(d.length),w=new Map;for(let k=0;k<u.length;k++)w.set(u[k].id,v[k]);let A=new Ln(b);for(let k=0;k<o.length;k++)A.accumulate(o[k].id,S[k]);let I=new Map;for(let k of s)if(k.opName==="constant"){let N=k.getResult(0).type,R=b._buildOp("constant",[],[N],new Map(k.attributes),null);I.set(k.getResult(0).id,R.getResult(0))}for(let k=c.length-1;k>=0;k--){let N=c[k],R=new Map;for(let O of N.ops){let M=new Array(O.numOperands);for(let G=0;G<O.numOperands;G++){let W=O.getOperand(G),$=R.get(W.id)||w.get(W.id)||I.get(W.id);M[G]=$||W}let F=O.results.map(G=>G.type),P=b._buildOp(O.opName,M,F,new Map(O.attributes),null);for(let G=0;G<O.numResults;G++)R.set(O.getResult(G).id,P.getResult(G))}Bl(N.ops,{accumulator:A,builder:b,needsGrad:a,resolveValue:f(O=>R.get(O.id)||w.get(O.id)||I.get(O.id)||O,"resolveValue")})}let B=[];for(let k=0;k<i.length;k++)if(a.has(i[k].id)){let N=A.get(i[k].id);if(N)B.push(N);else{let R=b.scalarConstant(0,i[k].type.dtype).getResult(0),O=b.broadcast(R,i[k].type.shape,[]).getResult(0);B.push(O)}}return b.returnOp(B),{backwardFunc:y,savedValues:u,gradInputIndices:this._getGradInputIndices(i,a)}}_findValue(t,e){for(let s of t)for(let r=0;r<s.numResults;r++)if(s.getResult(r).id===e)return s.getResult(r);return null}}});var fi,vm=T(()=>{fi=class{static{f(this,"RematPolicy")}constructor(t={}){this._maxRematDepth=t.maxRematDepth||1,this._sizeThreshold=t.sizeThreshold||1024*1024,this._alwaysRemat=new Set(t.alwaysRemat||["neg","abs","sign","floor","ceil","exp","log","sqrt","rsqrt","sin","cos","tanh"]),this._neverRemat=new Set(t.neverRemat||["matmul","dot","conv","reduce","custom_call","pool2d"])}shouldRematerialize(t){if(this._alwaysRemat.has(t.opName))return!0;if(this._neverRemat.has(t.opName))return!1;let e=t.numResults>0?t.getResult(0).type:null;return!e||!e.shape||e.numel()>this._sizeThreshold?!1:this._isElementwise(t)}_isElementwise(t){if(t.numOperands===0||t.numResults===0)return!1;let e=t.getResult(0).type.shape;for(let s=0;s<t.numOperands;s++){let r=t.getOperand(s).type;if(!r||!r.shape||r.shape.length!==e.length)return!1;for(let o=0;o<e.length;o++)if(r.shape[o]!==e[o])return!1}return!0}}});var Al,Ub=T(()=>{Wr();zn();ur();wl();yn();vm();Sl();Al=class{static{f(this,"JointGraphBuilder")}constructor(t={}){this._rematPolicy=t.rematPolicy||new fi(t.remat||{}),this._checkpointPolicy=t.checkpointPolicy||null}build(t){if(this._checkpointPolicy)return this._buildCheckpointed(t);let s=qt.compute(t).topologicalOrder;this._assertNoRegionControlFlow(s);let r=t.getReturnOp();if(!r)throw new Error("Forward function has no return op");let o=r.operands,i=t.args,a=o.map(S=>S.type),c=[...t.inputTypes,...a],l=[...t.outputTypes,...t.inputTypes],u=new os(`joint_${t.name}`,c,l),h=new We(u),p=u.args,d=p.slice(0,t.inputTypes.length),m=p.slice(t.inputTypes.length),g=new Map;for(let S=0;S<i.length;S++)g.set(i[S].id,d[S]);for(let S of s){if(S.opName==="return")continue;let v=new Array(S.numOperands);for(let I=0;I<S.numOperands;I++){let B=S.getOperand(I);v[I]=g.get(B.id)||B}let w=S.results.map(I=>I.type),A=h._buildOp(S.opName,v,w,new Map(S.attributes),null);for(let I=0;I<S.numResults;I++)g.set(S.getResult(I).id,A.getResult(I))}let _=o.map(S=>g.get(S.id)),y=this._computeGradReachability(t,s),b=new Ln(h);for(let S=0;S<o.length;S++)b.accumulate(o[S].id,m[S]);Bl(s,{accumulator:b,builder:h,needsGrad:y,resolveValue:f(S=>g.get(S.id)||S,"resolveValue")});let x=[];for(let S=0;S<i.length;S++){let v=b.get(i[S].id);if(v)x.push(v);else{let w=h.scalarConstant(0,i[S].type.dtype).getResult(0),A=h.broadcast(w,i[S].type.shape,[]).getResult(0);x.push(A)}}return h.returnOp([..._,...x]),{jointFunc:u,numForwardOutputs:o.length,numGradInputs:x.length}}_computeGradReachability(t,e){let s=new Set,r=t.getReturnOp();for(let o of r.operands)s.add(o.id);for(let o=e.length-1;o>=0;o--){let i=e[o];if(!(i.opName==="return"||!i.results.some(c=>s.has(c.id)))&&li(i.opName)&&!wm(i.opName))for(let c=0;c<i.numOperands;c++)s.add(i.getOperand(c).id)}return s}_assertNoRegionControlFlow(t){for(let e of t)if(vl.has(e.opName))throw new Error(`JointGraphBuilder does not support region control-flow op '${e.opName}'; use BackwardGraphBuilder (separate mode) without a checkpointPolicy, which differentiates scan/if.`)}_buildCheckpointed(t){let s=qt.compute(t).topologicalOrder;this._assertNoRegionControlFlow(s);let r=t.getReturnOp();if(!r)throw new Error("Forward function has no return op");let o=r.operands,i=t.args,a=o.map(v=>v.type),c=[...t.inputTypes,...a],l=[...t.outputTypes,...t.inputTypes],u=new os(`joint_${t.name}`,c,l),h=new We(u),p=u.args,d=p.slice(0,t.inputTypes.length),m=p.slice(t.inputTypes.length),g=new Map;for(let v=0;v<i.length;v++)g.set(i[v].id,d[v]);for(let v of s){if(v.opName==="return")continue;let w=new Array(v.numOperands);for(let B=0;B<v.numOperands;B++){let k=v.getOperand(B);w[B]=g.get(k.id)||k}let A=v.results.map(B=>B.type),I=h._buildOp(v.opName,w,A,new Map(v.attributes),null);for(let B=0;B<v.numResults;B++)g.set(v.getResult(B).id,I.getResult(B))}let _=o.map(v=>g.get(v.id)),y=this._computeGradReachability(t,s),b=this._checkpointPolicy.segment(s,t),x=new Ln(h);for(let v=0;v<o.length;v++)x.accumulate(o[v].id,m[v]);for(let v=b.length-1;v>=0;v--){let w=b[v],A=new Map;for(let I of w.ops){let B=new Array(I.numOperands);for(let R=0;R<I.numOperands;R++){let O=I.getOperand(R);B[R]=A.get(O.id)||g.get(O.id)||O}let k=I.results.map(R=>R.type),N=h._buildOp(I.opName,B,k,new Map(I.attributes),null);for(let R=0;R<I.numResults;R++)A.set(I.getResult(R).id,N.getResult(R))}Bl(w.ops,{accumulator:x,builder:h,needsGrad:y,resolveValue:f(I=>A.get(I.id)||g.get(I.id)||I,"resolveValue")})}let S=[];for(let v=0;v<i.length;v++){let w=x.get(i[v].id);if(w)S.push(w);else{let A=h.scalarConstant(0,i[v].type.dtype).getResult(0),I=h.broadcast(A,i[v].type.shape,[]).getResult(0);S.push(I)}}return h.returnOp([..._,...S]),{jointFunc:u,numForwardOutputs:o.length,numGradInputs:S.length}}}});var xR=T(()=>{});function SR(n,t){let e=n.gradOutputs[0],[s,r]=n.operands,o=n.builder.scalarConstant(0,s.type.dtype).getResult(0),i=n.builder.broadcast(o,s.type.shape,[]).getResult(0),a=n.builder.compare(s,r,t).getResult(0),c=n.builder.select(a,e,i).getResult(0),l=n.builder.select(a,i,e).getResult(0);return[c,l]}function vR(n){let t=n.gradOutputs[0],[e,s]=n.operands,r=n.builder.scalarConstant(0,s.type.dtype).getResult(0),o=n.builder.broadcast(r,t.type.shape,[]).getResult(0),i=n.builder.where(e,t,o).getResult(0),a=n.builder.where(e,o,t).getResult(0);return[null,i,a]}var BR=T(()=>{yn();rt("add",n=>{let t=n.gradOutputs[0];return[t,t]});rt("sub",n=>{let t=n.gradOutputs[0],e=n.builder.neg(t).getResult(0);return[t,e]});f(SR,"_minMaxVJP");rt("maximum",n=>SR(n,"ge"));rt("minimum",n=>SR(n,"le"));rt("clamp",n=>{let t=n.gradOutputs[0],[e,s,r]=n.operands,o=n.builder.scalarConstant(0,s.type.dtype).getResult(0),i=n.builder.broadcast(o,s.type.shape,[]).getResult(0),a=n.builder.compare(s,e,"ge").getResult(0),c=n.builder.where(a,t,i).getResult(0),l=n.builder.compare(s,r,"le").getResult(0);return[null,n.builder.where(l,c,i).getResult(0),null]});f(vR,"_whereVJP");rt("where",vR);rt("select",vR);rt("mul",n=>{let t=n.gradOutputs[0],[e,s]=n.operands,r=n.builder.mul(t,s).getResult(0),o=n.builder.mul(t,e).getResult(0);return[r,o]});rt("div",n=>{let t=n.gradOutputs[0],[e,s]=n.operands,r=n.builder.div(t,s).getResult(0),o=n.builder.mul(s,s).getResult(0),i=n.builder.neg(e).getResult(0),a=n.builder.mul(t,i).getResult(0),c=n.builder.div(a,o).getResult(0);return[r,c]});rt("neg",n=>{let t=n.gradOutputs[0];return[n.builder.neg(t).getResult(0)]});rt("pow",n=>{let t=n.gradOutputs[0],[e,s]=n.operands,r=n.builder.scalarConstant(1,e.type.dtype).getResult(0),o=n.builder.broadcast(r,e.type.shape,[]).getResult(0),i=n.builder.sub(s,o).getResult(0),a=n.builder.pow(e,i).getResult(0),c=n.builder.mul(s,a).getResult(0),l=n.builder.mul(t,c).getResult(0),u=n.builder.pow(e,s).getResult(0),h=n.builder.log(e).getResult(0),p=n.builder.mul(u,h).getResult(0),d=n.builder.mul(t,p).getResult(0);return[l,d]})});var kR=T(()=>{yn();rt("exp",n=>{let t=n.gradOutputs[0],e=n.results[0];return[n.builder.mul(t,e).getResult(0)]});rt("log",n=>{let t=n.gradOutputs[0],[e]=n.operands;return[n.builder.div(t,e).getResult(0)]});rt("sqrt",n=>{let t=n.gradOutputs[0],e=n.results[0],s=n.builder.scalarConstant(2,e.type.dtype).getResult(0),r=n.builder.broadcast(s,e.type.shape,[]).getResult(0),o=n.builder.mul(r,e).getResult(0);return[n.builder.div(t,o).getResult(0)]});rt("tanh",n=>{let t=n.gradOutputs[0],e=n.results[0],s=n.builder.mul(e,e).getResult(0),r=n.builder.scalarConstant(1,e.type.dtype).getResult(0),o=n.builder.broadcast(r,e.type.shape,[]).getResult(0),i=n.builder.sub(o,s).getResult(0);return[n.builder.mul(t,i).getResult(0)]});rt("sigmoid",n=>{let t=n.gradOutputs[0],e=n.results[0],s=n.builder.scalarConstant(1,e.type.dtype).getResult(0),r=n.builder.broadcast(s,e.type.shape,[]).getResult(0),o=n.builder.sub(r,e).getResult(0),i=n.builder.mul(e,o).getResult(0);return[n.builder.mul(t,i).getResult(0)]});rt("relu",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=n.builder.scalarConstant(0,e.type.dtype).getResult(0),r=n.builder.broadcast(s,e.type.shape,[]).getResult(0),o=n.builder.compare(e,r,"gt").getResult(0);return[n.builder.select(o,t,r).getResult(0)]});rt("gelu",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.dtype,r=e.type.shape,o=n.builder.broadcast(n.builder.scalarConstant(1.702,s).getResult(0),r,[]).getResult(0),i=n.builder.broadcast(n.builder.scalarConstant(1,s).getResult(0),r,[]).getResult(0),a=n.builder.mul(o,e).getResult(0),c=n.builder.sigmoid(a).getResult(0),l=n.builder.sub(i,c).getResult(0),u=n.builder.mul(a,l).getResult(0),h=n.builder.add(i,u).getResult(0),p=n.builder.mul(c,h).getResult(0);return[n.builder.mul(t,p).getResult(0)]});rt("silu",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.dtype,r=e.type.shape,o=n.builder.sigmoid(e).getResult(0),i=n.builder.scalarConstant(1,s).getResult(0),a=n.builder.broadcast(i,r,[]).getResult(0),c=n.builder.sub(a,o).getResult(0),l=n.builder.mul(e,c).getResult(0),u=n.builder.add(o,n.builder.mul(o,l).getResult(0)).getResult(0);return[n.builder.mul(t,u).getResult(0)]});rt("sin",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=n.builder.cos(e).getResult(0);return[n.builder.mul(t,s).getResult(0)]});rt("cos",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=n.builder.sin(e).getResult(0),r=n.builder.neg(s).getResult(0);return[n.builder.mul(t,r).getResult(0)]});rt("abs",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=n.builder.sign(e).getResult(0);return[n.builder.mul(t,s).getResult(0)]});rt("erf",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.dtype,r=e.type.shape,o=n.builder.broadcast(n.builder.scalarConstant(2/Math.sqrt(Math.PI),s).getResult(0),r,[]).getResult(0),i=n.builder.mul(e,e).getResult(0),a=n.builder.neg(i).getResult(0),c=n.builder.exp(a).getResult(0),l=n.builder.mul(o,c).getResult(0);return[n.builder.mul(t,l).getResult(0)]});rt("log2",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.dtype,r=e.type.shape,o=n.builder.broadcast(n.builder.scalarConstant(Math.LN2,s).getResult(0),r,[]).getResult(0),i=n.builder.mul(e,o).getResult(0);return[n.builder.div(t,i).getResult(0)]});rt("log10",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.dtype,r=e.type.shape,o=n.builder.broadcast(n.builder.scalarConstant(Math.LN10,s).getResult(0),r,[]).getResult(0),i=n.builder.mul(e,o).getResult(0);return[n.builder.div(t,i).getResult(0)]});rt("exp2",n=>{let t=n.gradOutputs[0],e=n.results[0],s=e.type.dtype,r=e.type.shape,o=n.builder.broadcast(n.builder.scalarConstant(Math.LN2,s).getResult(0),r,[]).getResult(0),i=n.builder.mul(e,o).getResult(0);return[n.builder.mul(t,i).getResult(0)]});rt("square",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.dtype,r=e.type.shape,o=n.builder.broadcast(n.builder.scalarConstant(2,s).getResult(0),r,[]).getResult(0),i=n.builder.mul(o,e).getResult(0);return[n.builder.mul(t,i).getResult(0)]});rt("reciprocal",n=>{let t=n.gradOutputs[0],e=n.results[0],s=n.builder.mul(e,e).getResult(0),r=n.builder.neg(s).getResult(0);return[n.builder.mul(t,r).getResult(0)]});rt("rsqrt",n=>{let t=n.gradOutputs[0],e=n.results[0],s=n.builder.mul(n.builder.mul(e,e).getResult(0),e).getResult(0),r=n.builder.broadcast(n.builder.scalarConstant(-.5,e.type.dtype).getResult(0),e.type.shape,[]).getResult(0),o=n.builder.mul(r,s).getResult(0);return[n.builder.mul(t,o).getResult(0)]})});var AR=T(()=>{yn();rt("dot",n=>{let t=n.gradOutputs[0],[e,s]=n.operands,r=n.op.getAttr("lhs_contracting"),o=n.op.getAttr("rhs_contracting"),i=n.op.getAttr("lhs_batch")||[],a=n.op.getAttr("rhs_batch")||[],c=e.type.rank,l=s.type.rank,u=t.type.rank,h=[];for(let y=0;y<c;y++)!r.includes(y)&&!i.includes(y)&&h.push(y);let p=[];for(let y=0;y<l;y++)!o.includes(y)&&!a.includes(y)&&p.push(y);let d=[];for(let y=i.length;y<i.length+p.length;y++)d.push(y+h.length);let m=[];for(let y=i.length;y<i.length+h.length;y++)m.push(y);let g=n.builder.dot(t,s,d,p,Array.from({length:i.length},(y,b)=>b),a).getResult(0),_=n.builder.dot(e,t,h,m,i,Array.from({length:i.length},(y,b)=>b)).getResult(0);return[g,_]});rt("matmul",n=>{let t=n.gradOutputs[0],[e,s]=n.operands,r=f(l=>{let u=Array.from({length:l},(h,p)=>p);return u[l-2]=l-1,u[l-1]=l-2,u},"swapLastTwo"),o=n.builder.transpose(s,r(s.type.rank)).getResult(0),i=n.builder.matmul(t,o).getResult(0),a=n.builder.transpose(e,r(e.type.rank)).getResult(0),c=n.builder.matmul(a,t).getResult(0);return[i,c]});rt("conv",n=>{let t=n.gradOutputs[0],[e,s]=n.operands,r=n.builder,o=n.op.getAttr("strides"),i=n.op.getAttr("padding"),a=n.op.getAttr("dilation")||o.map(()=>1),c=n.op.getAttr("groups")||1,l=n.op.getAttr("input_layout"),u=n.op.getAttr("kernel_layout");if(!(o.length===2&&o.every(I=>I===1)&&a.every(I=>I===1)&&c===1&&l==="NCHW"&&u==="OIHW"))throw new Error("conv VJP supports only 2D stride-1 dilation-1 groups-1 NCHW/OIHW conv");let p=s.type.shape,d=p[2],m=p[3],[g,_]=i,y=r.reverse(r.transpose(s,[1,0,2,3]).getResult(0),[2,3]).getResult(0),b=[[d-1-g[0],d-1-g[1]],[m-1-_[0],m-1-_[1]]],x=r.conv(t,y,[1,1],b).getResult(0),S=r.transpose(e,[1,0,2,3]).getResult(0),v=r.transpose(t,[1,0,2,3]).getResult(0),w=r.conv(S,v,[1,1],[g,_]).getResult(0),A=r.transpose(w,[1,0,2,3]).getResult(0);return[x,A]})});var RR=T(()=>{yn();rt("reduce",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=n.op.getAttr("reduce_type"),r=n.op.getAttr("dimensions"),o=e.type.shape,i=e.type.dtype,a=new Set(r),c=o.map((h,p)=>a.has(p)?1:h),l=o.map((h,p)=>p),u=n.builder.reshape(t,c).getResult(0);if(s==="sum")return[n.builder.broadcast(u,o,l).getResult(0),null];if(s==="mean"){let h=1;for(let _=0;_<o.length;_++)a.has(_)&&(h*=o[_]);let p=n.builder.broadcast(u,o,l).getResult(0),d=n.builder.scalarConstant(h,i).getResult(0),m=n.builder.broadcast(d,o,[]).getResult(0);return[n.builder.div(p,m).getResult(0),null]}if(s==="max"||s==="min"){let h=n.builder.reshape(n.results[0],c).getResult(0),p=n.builder.broadcast(h,o,l).getResult(0),d=n.builder.compare(e,p,"eq").getResult(0),m=n.builder.convert(d,i).getResult(0),g=n.builder.broadcast(u,o,l).getResult(0);return[n.builder.mul(g,m).getResult(0),null]}throw s==="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 '${s}' on the gradient path (would silently drop the gradient)`)})});var OR=T(()=>{yn();rt("reshape",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.shape;return[n.builder.reshape(t,s).getResult(0)]});rt("reverse",n=>[n.builder.reverse(n.gradOutputs[0],n.op.getAttr("dimensions")).getResult(0)]);rt("transpose",n=>{let t=n.gradOutputs[0],e=n.op.getAttr("permutation"),s=new Array(e.length);for(let r=0;r<e.length;r++)s[e[r]]=r;return[n.builder.transpose(t,s).getResult(0)]});rt("broadcast_in_dim",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=n.op.getAttr("broadcast_dimensions"),r=e.type.shape,o=n.results[0].type.shape,i=e.type.dtype,a=[];for(let h=0;h<o.length;h++)if(!s.includes(h))a.push(h);else{let p=s.indexOf(h);r[p]===1&&o[h]!==1&&a.push(h)}if(a.length===0)return[t];let c=n.builder.scalarConstant(0,i).getResult(0),l=n.builder.reduce(t,c,a,"sum").getResult(0),u=l.type.shape;return u.length!==r.length||!u.every((h,p)=>h===r[p])?[n.builder.reshape(l,r).getResult(0)]:[l]});rt("slice",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=n.op.getAttr("starts"),r=e.type.shape,o=t.type.shape,i=n.op.getAttr("strides")||r.map(()=>1),a=e.type.dtype,c=[...s],l=new Array(r.length),u=new Array(r.length);for(let p=0;p<r.length;p++)u[p]=i[p]-1,l[p]=r[p]-s[p]-(o[p]-1)*i[p]-1;let h=n.builder.scalarConstant(0,a).getResult(0);return[n.builder.pad(t,h,c,l,u).getResult(0)]});rt("concat",n=>{let t=n.gradOutputs[0],e=n.op.getAttr("dimension"),s=n.operands,r=[],o=0;for(let i=0;i<s.length;i++){let a=s[i].type.shape,c=new Array(a.length).fill(0),l=[...t.type.shape];c[e]=o,l[e]=o+a[e],r.push(n.builder.slice(t,c,l).getResult(0)),o+=a[e]}return r});rt("gather",n=>{let t=n.gradOutputs[0],[e,s]=n.operands,r=n.builder.scalarConstant(0,e.type.dtype).getResult(0),o=n.builder.broadcast(r,e.type.shape,[]).getResult(0);return[n.builder.scatterAdd(o,s,t,{updateWindowDims:n.op.getAttr("offset_dims"),insertedWindowDims:n.op.getAttr("collapsed_slice_dims"),scatterDimsToOperandDims:n.op.getAttr("start_index_map"),indexVectorDim:n.op.getAttr("index_vector_dim")}).getResult(0),null]});rt("scatter",n=>{let t=n.gradOutputs[0],[e,s]=n.operands,r=n.op.getAttr("inserted_window_dims"),o=e.type.shape.map((a,c)=>r.includes(c)?1:a),i=n.builder.gather(t,s,{offsetDims:n.op.getAttr("update_window_dims"),collapsedSliceDims:r,startIndexMap:n.op.getAttr("scatter_dims_to_operand_dims"),indexVectorDim:n.op.getAttr("index_vector_dim"),sliceSizes:o}).getResult(0);return[t,null,i]});rt("pad",n=>{let t=n.gradOutputs[0],e=n.op.getAttr("low"),s=n.op.getAttr("high"),[r]=n.operands,o=r.type.shape,i=[...e],a=new Array(o.length);for(let c=0;c<o.length;c++)a[c]=e[c]+o[c];return[n.builder.slice(t,i,a).getResult(0),null]})});var NR=T(()=>{yn();rt("softmax",n=>{let t=n.gradOutputs[0],e=n.results[0],s=n.op.getAttr("axis"),r=e.type.dtype,o=e.type.shape,i=n.builder.mul(t,e).getResult(0),a=n.builder.scalarConstant(0,r).getResult(0),c=n.builder.reduce(i,a,[s],"sum").getResult(0),l=[];for(let p=0;p<o.length;p++)p!==s&&l.push(p);let u=n.builder.broadcast(c,o,l).getResult(0),h=n.builder.sub(t,u).getResult(0);return[n.builder.mul(e,h).getResult(0)]});rt("log_softmax",n=>{let t=n.gradOutputs[0],e=n.results[0],[s]=n.operands,r=n.op.getAttr("axis"),o=e.type.dtype,i=e.type.shape,a=n.builder.exp(e).getResult(0),c=n.builder.scalarConstant(0,o).getResult(0),l=n.builder.reduce(t,c,[r],"sum").getResult(0),u=[];for(let d=0;d<i.length;d++)d!==r&&u.push(d);let h=n.builder.broadcast(l,i,u).getResult(0),p=n.builder.mul(a,h).getResult(0);return[n.builder.sub(t,p).getResult(0)]});rt("layer_norm",n=>{let t=n.gradOutputs[0],[e,s]=n.operands,r=n.op.getAttr("axis"),o=n.op.getAttr("epsilon"),i=e.type.dtype,a=e.type.shape,c=[r<0?a.length+r:r],l=a[c[0]],u=[];for(let pt=0;pt<a.length;pt++)c.includes(pt)||u.push(pt);let h=n.builder.scalarConstant(0,i).getResult(0),p=n.builder.reduce(e,h,c,"mean").getResult(0),d=n.builder.broadcast(p,a,u).getResult(0),m=n.builder.sub(e,d).getResult(0),g=n.builder.mul(m,m).getResult(0),_=n.builder.reduce(g,h,c,"mean").getResult(0),y=n.builder.scalarConstant(o,i).getResult(0),b=n.builder.broadcast(y,_.type.shape,[]).getResult(0),x=n.builder.add(_,b).getResult(0),S=n.builder.rsqrt(x).getResult(0),v=n.builder.broadcast(S,a,u).getResult(0),w=[];for(let pt=0;pt<a.length;pt++)c.includes(pt)&&w.push(pt);let A=n.builder.broadcast(s,a,w).getResult(0),I=n.builder.mul(t,A).getResult(0),B=n.builder.scalarConstant(l,i).getResult(0),k=n.builder.broadcast(B,a,[]).getResult(0),N=n.builder.mul(k,I).getResult(0),R=n.builder.reduce(I,h,c,"sum").getResult(0),O=n.builder.broadcast(R,a,u).getResult(0),M=n.builder.mul(m,v).getResult(0),F=n.builder.mul(I,M).getResult(0),P=n.builder.reduce(F,h,c,"sum").getResult(0),G=n.builder.broadcast(P,a,u).getResult(0),W=n.builder.mul(M,G).getResult(0),$=n.builder.sub(N,n.builder.add(O,W).getResult(0)).getResult(0),L=n.builder.div(v,k).getResult(0),q=n.builder.mul(L,$).getResult(0),yt=n.builder.reduce(n.builder.mul(t,M).getResult(0),h,u,"sum").getResult(0),lt=n.builder.reduce(t,h,u,"sum").getResult(0);return[q,yt,lt]});rt("scaled_dot_product_attention",n=>{let t=n.gradOutputs[0],[e,s,r]=n.operands,o=n.op.getAttr("scale");if(n.op.getAttr("causal"))throw new Error("causal scaled_dot_product_attention VJP not supported");let i=n.builder,a=e.type.dtype,c=e.type.rank,l=[];for(let k=0;k<c;k++)l.push(k);l[c-2]=c-1,l[c-1]=c-2;let u=f(k=>i.transpose(k,l).getResult(0),"lastT"),h=f(k=>i.broadcast(i.scalarConstant(o,a).getResult(0),k,[]).getResult(0),"scaleBrTo"),p=i.matmul(e,u(s)).getResult(0),d=i.mul(p,h(p.type.shape)).getResult(0),m=i.softmax(d,c-1).getResult(0),g=i.matmul(u(m),t).getResult(0),_=i.matmul(t,u(r)).getResult(0),y=i.scalarConstant(0,a).getResult(0),b=i.mul(_,m).getResult(0),x=i.reduce(b,y,[c-1],"sum").getResult(0),S=[];for(let k=0;k<c-1;k++)S.push(k);let v=i.broadcast(x,m.type.shape,S).getResult(0),w=i.mul(m,i.sub(_,v).getResult(0)).getResult(0),A=i.mul(w,h(w.type.shape)).getResult(0),I=i.matmul(A,s).getResult(0),B=i.matmul(u(A),e).getResult(0);return[I,B,g]});rt("pool2d",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=n.builder,r=n.op.getAttr("pool_type"),o=n.op.getAttr("kernel_size"),i=n.op.getAttr("strides"),a=n.op.getAttr("padding"),c=n.op.getAttr("layout")||"NCHW",l=a.every(A=>A[0]===0&&A[1]===0),u=i[0]===o[0]&&i[1]===o[1];if(c!=="NCHW"||!l||!u||r!=="avg"&&r!=="max")throw new Error("pool2d VJP supports only non-overlapping (stride=kernel) avg/max pooling without padding, NCHW");let[h,p,d,m]=t.type.shape,[g,_]=o,y=e.type.shape,b=f(A=>s.reshape(s.broadcast(A,[h,p,d,g,m,_],[0,1,2,4]).getResult(0),y).getResult(0),"upsample"),x=b(t);if(r==="avg"){let A=s.broadcast(s.scalarConstant(g*_,e.type.dtype).getResult(0),y,[]).getResult(0);return[s.div(x,A).getResult(0)]}let S=b(n.results[0]),v=s.compare(e,S,"eq").getResult(0),w=s.broadcast(s.scalarConstant(0,e.type.dtype).getResult(0),y,[]).getResult(0);return[s.select(v,x,w).getResult(0)]});rt("batch_norm",n=>{let t=n.gradOutputs[0],[e,s,,r,o]=n.operands,i=n.op.getAttr("axis"),a=n.op.getAttr("epsilon"),c=e.type.dtype,l=e.type.shape,u=n.builder,h=[];for(let O=0;O<l.length;O++)O!==i&&h.push(O);let p=u.scalarConstant(0,c).getResult(0),d=u.broadcast(u.scalarConstant(a,c).getResult(0),o.type.shape,[]).getResult(0),m=u.rsqrt(u.add(o,d).getResult(0)).getResult(0),g=u.broadcast(m,l,[i]).getResult(0),_=u.broadcast(r,l,[i]).getResult(0),y=u.broadcast(s,l,[i]).getResult(0),b=u.sub(e,_).getResult(0),x=u.mul(b,g).getResult(0),S=u.mul(t,y).getResult(0),v=u.mul(S,g).getResult(0),w=u.reduce(u.mul(t,x).getResult(0),p,h,"sum").getResult(0),A=u.reduce(t,p,h,"sum").getResult(0),I=u.neg(u.reduce(v,p,h,"sum").getResult(0)).getResult(0),B=u.mul(u.mul(g,g).getResult(0),g).getResult(0),k=u.broadcast(u.scalarConstant(-.5,c).getResult(0),l,[]).getResult(0),N=u.mul(u.mul(u.mul(S,b).getResult(0),B).getResult(0),k).getResult(0),R=u.reduce(N,p,h,"sum").getResult(0);return[v,w,A,I,R]});rt("elu",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=n.results[0],r=e.type.dtype,o=e.type.shape,i=n.op.getAttr("alpha")??1,a=n.builder.broadcast(n.builder.scalarConstant(0,r).getResult(0),o,[]).getResult(0),c=n.builder.broadcast(n.builder.scalarConstant(1,r).getResult(0),o,[]).getResult(0),l=n.builder.broadcast(n.builder.scalarConstant(i,r).getResult(0),o,[]).getResult(0),u=n.builder.compare(e,a,"gt").getResult(0),h=n.builder.add(s,l).getResult(0),p=n.builder.select(u,c,h).getResult(0);return[n.builder.mul(t,p).getResult(0)]});rt("leaky_relu",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.dtype,r=e.type.shape,o=n.op.getAttr("negative_slope")??.01,i=n.builder.broadcast(n.builder.scalarConstant(0,s).getResult(0),r,[]).getResult(0),a=n.builder.broadcast(n.builder.scalarConstant(1,s).getResult(0),r,[]).getResult(0),c=n.builder.broadcast(n.builder.scalarConstant(o,s).getResult(0),r,[]).getResult(0),l=n.builder.compare(e,i,"gt").getResult(0),u=n.builder.select(l,a,c).getResult(0);return[n.builder.mul(t,u).getResult(0)]});rt("celu",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.dtype,r=e.type.shape,o=n.op.getAttr("alpha")??1,i=n.builder.broadcast(n.builder.scalarConstant(0,s).getResult(0),r,[]).getResult(0),a=n.builder.broadcast(n.builder.scalarConstant(1,s).getResult(0),r,[]).getResult(0),c=n.builder.compare(e,i,"gt").getResult(0),l=n.builder.broadcast(n.builder.scalarConstant(o,s).getResult(0),r,[]).getResult(0),u=n.builder.div(e,l).getResult(0),h=n.builder.exp(u).getResult(0),p=n.builder.select(c,a,h).getResult(0);return[n.builder.mul(t,p).getResult(0)]});rt("selu",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.dtype,r=e.type.shape,o=1.0507009873554805,i=1.6732632423543772,a=n.builder.broadcast(n.builder.scalarConstant(0,s).getResult(0),r,[]).getResult(0),c=n.builder.broadcast(n.builder.scalarConstant(o,s).getResult(0),r,[]).getResult(0),l=n.builder.compare(e,a,"gt").getResult(0),u=n.builder.broadcast(n.builder.scalarConstant(i,s).getResult(0),r,[]).getResult(0),h=n.builder.exp(e).getResult(0),p=n.builder.mul(u,h).getResult(0),d=n.builder.select(l,c,n.builder.mul(c,p).getResult(0)).getResult(0);return[n.builder.mul(t,d).getResult(0)]});rt("hardswish",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.dtype,r=e.type.shape,o=n.builder.broadcast(n.builder.scalarConstant(-3,s).getResult(0),r,[]).getResult(0),i=n.builder.broadcast(n.builder.scalarConstant(3,s).getResult(0),r,[]).getResult(0),a=n.builder.broadcast(n.builder.scalarConstant(0,s).getResult(0),r,[]).getResult(0),c=n.builder.broadcast(n.builder.scalarConstant(1,s).getResult(0),r,[]).getResult(0),l=n.builder.broadcast(n.builder.scalarConstant(2,s).getResult(0),r,[]).getResult(0),u=n.builder.broadcast(n.builder.scalarConstant(6,s).getResult(0),r,[]).getResult(0),h=n.builder.compare(e,o,"le").getResult(0),p=n.builder.compare(e,i,"ge").getResult(0),d=n.builder.add(n.builder.mul(l,e).getResult(0),i).getResult(0),m=n.builder.div(d,u).getResult(0),g=n.builder.select(h,a,n.builder.select(p,c,m).getResult(0)).getResult(0);return[n.builder.mul(t,g).getResult(0)]});rt("hardsigmoid",n=>{let t=n.gradOutputs[0],[e]=n.operands,s=e.type.dtype,r=e.type.shape,o=n.builder.broadcast(n.builder.scalarConstant(-3,s).getResult(0),r,[]).getResult(0),i=n.builder.broadcast(n.builder.scalarConstant(3,s).getResult(0),r,[]).getResult(0),a=n.builder.broadcast(n.builder.scalarConstant(0,s).getResult(0),r,[]).getResult(0),c=n.builder.broadcast(n.builder.scalarConstant(1/6,s).getResult(0),r,[]).getResult(0),l=n.builder.compare(e,o,"le").getResult(0),u=n.builder.compare(e,i,"ge").getResult(0),h=n.builder.select(l,a,n.builder.select(u,a,c).getResult(0)).getResult(0);return[n.builder.mul(t,h).getResult(0)]});rt("embedding",n=>{let t=n.gradOutputs[0],[e,s]=n.operands,r=e.type.shape,o=e.type.dtype,i=s.type.rank,a=s.type.shape,c=n.builder.scalarConstant(0,o).getResult(0),l=n.builder.broadcast(c,r,[]).getResult(0);return[n.builder.scatter(l,s,t,{updateWindowDims:[i],insertedWindowDims:[0],scatterDimsToOperandDims:[0],indexVectorDim:i}).getResult(0),null]})});var ER=T(()=>{yn();Vb("stop_gradient");rt("stop_gradient",()=>[null]);for(let n of["compare","logical_not","argmax","argmin","iota"])Vb(n)});var TR=T(()=>{yn();wl();Sl();Ub();vm();xR();BR();kR();AR();RR();OR();NR();ER()});var MR={};Ft(MR,{compileWithBackward:()=>IR});function IR(n,t,e={}){let s=e.target??Gn(),r=e.mode||"separate",o=e.rematPolicy||new fi(e.remat||{}),i={target:s,verify:!1,...e,backward:void 0,mode:void 0,rematPolicy:void 0,remat:void 0},a=e.dynamic_shapes||null,c=[],l=null,u=null,h=null;function p(B){let k=ym((...R)=>n.forward(...R),B,{name:n.constructor.name||"compiled",dynamicShapes:a}),N=f(R=>{let O=R.graph.functions().next().value,M=r==="joint"?m(O,R,o):d(O,R,o);return M.shapeEnv=R.shapeEnv,M.outputSymShapes=R.outputSymShapes,M},"finish");return k&&typeof k.then=="function"?k.then(N):N(k)}f(p,"_compile");function d(B,k,N){let R=new kl({rematPolicy:N}),{backwardFunc:O,savedValues:M,gradInputIndices:F}=R.build(B),P=B.getReturnOp(),G=[...P.operands],W=G.length,$=new Map(B.args.map((bt,_e)=>[bt.id,_e])),L=new Map(G.map((bt,_e)=>[bt.id,_e])),q=[],yt=new Map;for(let bt of M)$.has(bt.id)||L.has(bt.id)||yt.has(bt.id)||(yt.set(bt.id,W+q.length),q.push(bt));q.length>0&&(P.erase(),new We(B).returnOp([...G,...q]),B.outputTypes=Object.freeze([...G.map(bt=>bt.type),...q.map(bt=>bt.type)]));let lt=M.map(bt=>$.has(bt.id)?{kind:"arg",index:$.get(bt.id)}:L.has(bt.id)?{kind:"output",index:L.get(bt.id)}:{kind:"output",index:yt.get(bt.id)}),pt=new _s("forward");pt.addFunction(B);let z=new zr(i).compile(pt),Y=new _s("backward");Y.addFunction(O);let Bt=new zr(i).compile(Y);return{mode:"separate",fwdResult:z,bwdResult:Bt,forwardFunc:B,backwardFunc:O,savedValues:M,savedSources:lt,numRealOutputs:W,gradInputIndices:F,capturedParams:k.capturedParams,numUserInputs:k.numUserInputs,outputTypes:k.outputTypes}}f(d,"_compileSeparate");function m(B,k,N){let R=new Al({rematPolicy:N}),{jointFunc:O,numForwardOutputs:M,numGradInputs:F}=R.build(B),P=new _s("joint");return P.addFunction(O),{mode:"joint",result:new zr(i).compile(P),jointFunc:O,numForwardOutputs:M,numGradInputs:F,capturedParams:k.capturedParams,numUserInputs:k.numUserInputs,outputTypes:k.outputTypes,inputTypes:B.inputTypes}}f(m,"_compileJoint");function g(B,k){return B.outputSymShapes&&B.shapeEnv?B.shapeEnv.resolveSymbolicShape(B.outputSymShapes[k]):B.outputTypes[k].shape}f(g,"_resolveOutputShape");function _(B,k,N){let R=B.module||B;return R.executionPlan?R.runPlanAsync(R.executionPlan,N):B.isAsync(k)?B.runAsync(k,...N):(B.run(k,...N),null)}f(_,"_runK");function y(B,k){let R=B.fwdResult.listKernels()[0],O=k.length>0?k[0].device:"cpu",M=k.map(pt=>Is(pt)),P=B.capturedParams.map(pt=>Is(pt)),G=B.forwardFunc.outputTypes,W=B.numRealOutputs,$=new Array(G.length),L=new Array(G.length);for(let pt=0;pt<G.length;pt++){let z=pt<W?g(B,pt):G[pt].shape,Y=G[pt].dtype,Bt=Pe(z),bt=Jt(Y);$[pt]=new bt(Math.max(Bt,1)),L[pt]=z}let q=[...M,...P,...$],yt=f(()=>({results:W===1?jt($[0],L[0],G[0].dtype,O):Array.from({length:W},(z,Y)=>jt($[Y],L[Y],G[Y].dtype,O)),inputArrays:M,paramArrays:P,outputArrays:$,device:O}),"build"),lt=_(B.fwdResult,R,q);return lt?lt.then(yt):yt()}f(y,"_executeSeparateForward");function b(B,k,N){let O=B.bwdResult.listKernels()[0],M=k.map(Y=>Is(Y)),F=B.savedValues,P=B.savedSources,G=[...N.inputArrays,...N.paramArrays],W=new Array(F.length);for(let Y=0;Y<F.length;Y++){let Bt=P[Y];W[Y]=Bt.kind==="arg"?G[Bt.index]:N.outputArrays[Bt.index]}let $=B.backwardFunc,L=$.outputTypes.length,q=new Array(L),yt=new Array(L);for(let Y=0;Y<L;Y++){let Bt=$.outputTypes[Y].shape,bt=$.outputTypes[Y].dtype,_e=Pe(Bt),Ds=Jt(bt);q[Y]=new Ds(Math.max(_e,1)),yt[Y]=Bt}let lt=[...M,...W,...q],pt=f(()=>q.map((Y,Bt)=>jt(Y,yt[Bt],$.outputTypes[Bt].dtype,N.device)),"build"),z=_(B.bwdResult,O,lt);return z?z.then(pt):pt()}f(b,"_executeSeparateBackward");function x(B){for(let k=0;k<c.length;k++){let N=c[k];N.shapeEnv.bindInputShapes(B);let{passed:R}=N.shapeEnv.evaluateGuards();if(R)return N}return null}f(x,"_findCachedEntry");function S(B,k){if(u=B,B.mode==="joint")return A(B,k);let N=y(B,k);return N&&N.then?N.then(R=>(l=R,R.results)):(l=N,N.results)}f(S,"_forwardWith");function v(B){let k=x(B);if(k)return S(k,B);let N=p(B);return N&&typeof N.then=="function"?N.then(R=>(c.push(R),R.shapeEnv.bindInputShapes(B),S(R,B))):(c.push(N),N.shapeEnv.bindInputShapes(B),S(N,B))}f(v,"_runForward");function w(...B){return h?h.then(()=>v(B)):v(B)}f(w,"compiledForward");function A(B,k){let R=B.result.listKernels()[0],O=k.length>0?k[0].device:"cpu",M=k.map(z=>Is(z)),P=B.capturedParams.map(z=>Is(z)),G=B.jointFunc,W=G.outputTypes.length,$=new Array(W),L=new Array(W);for(let z=0;z<W;z++){let Y=G.outputTypes[z].shape,Bt=G.outputTypes[z].dtype,bt=Pe(Y),_e=Jt(Bt);$[z]=new _e(Math.max(bt,1)),L[z]=Y}let q=new Array(B.numForwardOutputs);for(let z=0;z<B.numForwardOutputs;z++){let Y=B.outputTypes[z],Bt=Pe(Y.shape),bt=Jt(Y.dtype);q[z]=new bt(Math.max(Bt,1))}l={inputArrays:M,paramArrays:P,gradOutputArrays:q,outputArrays:$,outputShapes:L,device:O,compiled:B};let yt=[...M,...P,...q,...$],lt=f(()=>{let z=[];for(let Y=0;Y<B.numForwardOutputs;Y++)z.push(jt($[Y],L[Y],G.outputTypes[Y].dtype,O));return z.length===1?z[0]:z},"build"),pt=_(B.result,R,yt);return pt?pt.then(lt):lt()}f(A,"_executeJointForward"),w.backward=function(...B){if(!u||!l)throw new Error("Must run forward before backward");return u.mode==="joint"?I(u,B,l):b(u,B,l)};function I(B,k,N){let{inputArrays:R,paramArrays:O,outputArrays:M,outputShapes:F,device:P}=N,G=k.map(z=>Is(z));for(let z=0;z<G.length;z++)N.gradOutputArrays[z].set(G[z]);let W=B.jointFunc,$=W.outputTypes.length,L=new Array($),q=new Array($);for(let z=0;z<$;z++){let Y=W.outputTypes[z].shape,Bt=W.outputTypes[z].dtype,bt=Pe(Y),_e=Jt(Bt);L[z]=new _e(Math.max(bt,1)),q[z]=Y}let yt=[...R,...O,...G,...L],lt=f(()=>{let z=[];for(let Y=B.numForwardOutputs;Y<$;Y++)z.push(jt(L[Y],q[Y],W.outputTypes[Y].dtype,P));return z},"build"),pt=_(B.result,B.result.listKernels()[0],yt);return pt?pt.then(lt):lt()}if(f(I,"_executeJointBackward"),w.original=n,w.backwardGraph=()=>{if(c.length===0)return null;let B=c[0];return B.mode==="joint"?B.jointFunc:B.backwardFunc},w.forwardGraph=()=>{if(c.length===0)return null;let B=c[0];return B.mode==="joint"?B.jointFunc:B.forwardFunc},w.capturedParams=()=>c.length?c[0].capturedParams:[],t){let B=p(t);B&&typeof B.then=="function"?h=B.then(k=>(c.push(k),h=null,k)):c.push(B)}return w}var Wb=T(()=>{Pb();dm();qn();xo();Sl();zn();Ub();vm();TR();hn();Qs();kn();f(IR,"compileWithBackward")});Dm();js();hn();Ps();Br();Po();tn();kn();Qs();function pa(n,t){let e=He(n),s=ps.allocate(0,t,Ki),r=new he(s,0,n,e,t,Ki);return new zt(r)}f(pa,"_metaTensor");function oo(n,t,e){let s=Hi(t.shape,e.shape);if(!s)throw new Error(`Incompatible shapes: [${t.shape}] vs [${e.shape}]`);let r=Ap(t.dtype,e.dtype);return pa(s,r)}f(oo,"_metaBinary");function ws(n,t){return pa([...t.shape],t.dtype)}f(ws,"_metaUnary");function fu(n,t,e,s){let r=[...t.shape],o=e!=null?Array.isArray(e)?e:[e]:Array.from({length:r.length},(c,l)=>l);if(o.length===r.length||e===void 0)return pa(s?r.map(()=>1):[],t.dtype);let i=[],a=new Set(o.map(c=>c<0?r.length+c:c));for(let c=0;c<r.length;c++)a.has(c)?s&&i.push(1):i.push(r[c]);return pa(i,t.dtype)}f(fu,"_metaReduction");var ek=oo,sk=oo,nk=oo,rk=oo,ok=oo,ik=oo,ak=oo,ck=oo,uk=ws,lk=ws,fk=ws,pk=ws,hk=ws,dk=ws,mk=ws,gk=ws,_k=ws,yk=ws,bk=ws,wk=ws,xk=ws,Sk=ws,vk=ws,Bk=ws,kk=fu,Ak=fu,Rk=fu,Ok=fu,Nk=fu;function Ek(n,t,e){let s=Ep(t.shape,e.shape);if(s===null)throw new Error("metaMatmul: unsupported shapes");return pa(s,t.dtype)}f(Ek,"metaMatmul");function Tk(n,t){return pa([...t.shape],t.dtype)}f(Tk,"metaClone");var dI=["add(Tensor self, Tensor other) -> Tensor","sub(Tensor self, Tensor other) -> Tensor","mul(Tensor self, Tensor other) -> Tensor","div(Tensor self, Tensor other) -> Tensor","neg(Tensor self) -> Tensor","pow(Tensor self, Tensor exponent) -> Tensor","rem(Tensor self, Tensor other) -> Tensor","maximum(Tensor self, Tensor other) -> Tensor","minimum(Tensor self, Tensor other) -> Tensor","exp(Tensor self) -> Tensor","log(Tensor self) -> Tensor","sqrt(Tensor self) -> Tensor","rsqrt(Tensor self) -> Tensor","abs(Tensor self) -> Tensor","sin(Tensor self) -> Tensor","cos(Tensor self) -> Tensor","tanh(Tensor self) -> Tensor","sigmoid(Tensor self) -> Tensor","relu(Tensor self) -> Tensor","gelu(Tensor self) -> Tensor","silu(Tensor self) -> Tensor","sign(Tensor self) -> Tensor","floor(Tensor self) -> Tensor","ceil(Tensor self) -> Tensor","eq(Tensor self, Tensor other) -> Tensor","ne(Tensor self, Tensor other) -> Tensor","lt(Tensor self, Tensor other) -> Tensor","le(Tensor self, Tensor other) -> Tensor","gt(Tensor self, Tensor other) -> Tensor","ge(Tensor self, Tensor other) -> Tensor","where(Tensor condition, Tensor self, Tensor other) -> Tensor","clamp(Tensor self, Tensor min, Tensor max) -> Tensor","pad(Tensor self, Tensor value, int[] low, int[] high) -> Tensor","one_hot(Tensor indices, int depth) -> Tensor","index_select(Tensor self, Tensor index, int dim) -> Tensor","gather(Tensor self, Tensor index, int dim) -> Tensor","scatter_add(Tensor self, Tensor index, Tensor src, int dim) -> Tensor","sum(Tensor self, int[] dim, bool keepdim) -> Tensor","mean(Tensor self, int[] dim, bool keepdim) -> Tensor","max(Tensor self, int[] dim, bool keepdim) -> Tensor","min(Tensor self, int[] dim, bool keepdim) -> Tensor","prod(Tensor self, int[] dim, bool keepdim) -> Tensor","argmax(Tensor self, int dim, bool keepdim) -> Tensor","argmin(Tensor self, int dim, bool keepdim) -> Tensor","matmul(Tensor self, Tensor other) -> Tensor","dot(Tensor self, Tensor other) -> Tensor","cat(Tensor[] tensors, int dim) -> Tensor","stack(Tensor[] tensors, int dim) -> Tensor","clone(Tensor self) -> Tensor","fill(Tensor self, Scalar value) -> Tensor","transpose(Tensor self, int dim0, int dim1) -> Tensor","softmax(Tensor self, int dim) -> Tensor","log_softmax(Tensor self, int dim) -> Tensor","layer_norm(Tensor input, Tensor weight, Tensor bias, int axis, float eps) -> Tensor","batch_norm(Tensor input, Tensor weight, Tensor bias, Tensor mean, Tensor var, int axis, float eps) -> Tensor","conv2d(Tensor input, Tensor weight, int[] strides, int[] padding, int[] dilation, int groups) -> Tensor","pool2d(Tensor input, str pool_type, int[] kernel_size, int[] strides, int[] padding) -> Tensor","embedding(Tensor weight, Tensor indices) -> Tensor"],mI=nt.META,gI={add:ek,sub:sk,mul:nk,div:rk,pow:ok,rem:ik,maximum:ak,minimum:ck,neg:uk,exp:lk,log:fk,sqrt:pk,rsqrt:hk,abs:dk,sin:mk,cos:gk,tanh:_k,sigmoid:yk,relu:bk,gelu:wk,silu:xk,sign:Sk,floor:vk,ceil:Bk,sum:kk,mean:Ak,max:Rk,min:Ok,prod:Nk,matmul:Ek,clone:Tk},Ik=!1;function Mk(){if(Ik)return;Ik=!0;let n=new sc("mlc","DEF");for(let e of dI)n.def(e);let t=new sc("mlc","IMPL");for(let[e,s]of Object.entries(gI))t.impl(e,mI,s);Qy()}f(Mk,"registerNativeOps");js();ec();mi();Tr();Qn();$t();ae();var gh=class extends wt{static{f(this,"AddBackward")}constructor(){super(2)}apply(t){let e=t[0];return[e,e]}},_h=class extends wt{static{f(this,"SubBackward")}constructor(){super(2)}apply(t){let e=t[0];return[e,Rn(e)]}},yh=class extends wt{static{f(this,"MulBackward")}constructor(){super(2)}apply(t){let e=t[0],[s,r]=this.savedTensors();return[et(e,r.detach()),et(e,s.detach())]}},bh=class extends wt{static{f(this,"DivBackward")}constructor(){super(2)}apply(t){let e=t[0],[s,r]=this.savedTensors(),o=Se(e,r.detach()),i=Rn(Se(et(e,s.detach()),et(r.detach(),r.detach())));return[o,i]}},wh=class extends wt{static{f(this,"NegBackward")}constructor(){super(1)}apply(t){let e=t[0];return[Rn(e)]}},xh=class extends wt{static{f(this,"PowBackward")}constructor(){super(2)}apply(t){let e=t[0],[s,r]=this.savedTensors(),o=$e(r.shape,{dtype:r.dtype,device:r.device}),i=et(e,et(r.detach(),to(s.detach(),Wt(r.detach(),o)))),a=et(e,et(to(s.detach(),r.detach()),Or(s.detach())));return[i,a]}};Qn();$t();ae();var Sh=class extends wt{static{f(this,"ExpBackward")}constructor(){super(1)}apply(t){let[e]=this.savedTensors();return[et(t[0],On(e.detach()))]}},vh=class extends wt{static{f(this,"LogBackward")}constructor(){super(1)}apply(t){let[e]=this.savedTensors();return[Se(t[0],e.detach())]}},Bh=class extends wt{static{f(this,"SqrtBackward")}constructor(){super(1)}apply(t){let[e]=this.savedTensors(),s=e.detach(),r=Nr(s),o=Lt(s.shape,2,{dtype:s.dtype,device:s.device});return[Se(t[0],et(o,r))]}},kh=class extends wt{static{f(this,"TanhBackward")}constructor(){super(1)}apply(t){let[e]=this.savedTensors(),s=e.detach(),r=pn(s),o=$e(s.shape,{dtype:s.dtype,device:s.device});return[et(t[0],Wt(o,et(r,r)))]}},Ah=class extends wt{static{f(this,"SigmoidBackward")}constructor(){super(1)}apply(t){let[e]=this.savedTensors(),s=e.detach(),r=bs(s),o=$e(s.shape,{dtype:s.dtype,device:s.device});return[et(t[0],et(r,Wt(o,r)))]}},Rh=class extends wt{static{f(this,"SoftmaxBackward")}constructor(){super(1)}apply(t){let[e]=this.savedTensors(),s=this.opArgs(),r=s&&s.length>1&&s[1]!=null?s[1]:-1,o=t[0],i=er(e.detach(),r),a=ie(et(o,i),r,!0);return[et(i,Wt(o,a))]}},Oh=class extends wt{static{f(this,"LogSoftmaxBackward")}constructor(){super(1)}apply(t){let[e]=this.savedTensors(),s=this.opArgs(),r=s&&s.length>1&&s[1]!=null?s[1]:-1,o=t[0],i=er(e.detach(),r),a=ie(o,r,!0);return[Wt(o,et(i,a))]}},Nh=class extends wt{static{f(this,"ReluBackward")}constructor(){super(1)}apply(t){let[e]=this.savedTensors(),s=e.detach();return[et(t[0],jc(Vo(s)))]}},Eh=class extends wt{static{f(this,"GeluBackward")}constructor(){super(1)}apply(t){let e=t[0],[s]=this.savedTensors(),r=s.detach(),o=r.shape,i=r.dtype,a=r.device,c=Lt(o,1.702,{dtype:i,device:a}),l=$e(o,{dtype:i,device:a}),u=et(c,r),h=bs(u),p=et(h,ut(l,et(u,Wt(l,h))));return[et(e,p)]}},Th=class extends wt{static{f(this,"SiluBackward")}constructor(){super(1)}apply(t){let e=t[0],[s]=this.savedTensors(),r=s.detach(),o=bs(r),i=$e(r.shape,{dtype:r.dtype,device:r.device}),a=et(o,ut(i,et(r,Wt(i,o))));return[et(e,a)]}};Qn();$t();ae();Ye();function Ck(n,t){if(n==null){let s=[];for(let r=0;r<t;r++)s.push(r);return s}return(Array.isArray(n)?n:[n]).map(s=>s<0?s+t:s).sort((s,r)=>s-r)}f(Ck,"_normalizeDims");function Dk(n,t,e,s){let r=n;if(!s)for(let i of e)r=sn(r,i);let o=Nt(t,{dtype:r.dtype,device:r.device});return ut(o,r)}f(Dk,"_unreduce");var Ih=class extends wt{static{f(this,"SumBackward")}constructor(){super(1)}apply(t){let e=t[0],r=this.inputMetadata(0).shape,o=this.opArgs(),i=o?o[1]:void 0,a=o?o[2]:!1,c=Ck(i,r.length);return[Dk(e,r,c,a)]}},Mh=class extends wt{static{f(this,"MeanBackward")}constructor(){super(1)}apply(t){let e=t[0],r=this.inputMetadata(0).shape,o=this.opArgs(),i=o?o[1]:void 0,a=o?o[2]:!1,c=Ck(i,r.length),l=1;for(let p of c)l*=r[p];let u=Dk(e,r,c,a),h=Lt(r,l,{dtype:e.dtype,device:e.device});return[Se(u,h)]}};Qn();$t();Ye();function Lk(n,t){let e=n;for(;e.ndim>t.length;)e=ie(e,0,!1);for(let s=0;s<t.length;s++)t[s]===1&&e.shape[s]!==1&&(e=ie(e,s,!0));return e}f(Lk,"_sumToShape");var Ch=class extends wt{static{f(this,"MatmulBackward")}constructor(){super(2)}apply(t){let e=t[0],[s,r]=this.savedTensors(),o=s.detach(),i=r.detach(),a=o.ndim,c=i.ndim;if(a===1&&c===1)return[et(e,i),et(e,o)];if(a===2&&c===1){let h=sn(e,1),p=sn(i,0);return[Je(h,p),Vc(Je(en(o,0,1),h),1)]}if(a===1&&c===2){let h=Je(e,en(i,0,1)),p=Je(sn(o,1),sn(e,0));return[h,p]}let l=Je(e,en(i,c-2,c-1)),u=Je(en(o,a-2,a-1),e);return l=Lk(l,o.shape),u=Lk(u,i.shape),[l,u]}},Dh=class extends wt{static{f(this,"DotBackward")}constructor(){super(2)}apply(t){let e=t[0],[s,r]=this.savedTensors();return[et(e,r.detach()),et(e,s.detach())]}};G_();var t0=new Map;function Xt(n,t){t0.set(n,t)}f(Xt,"_register");Xt("add",()=>new gh);Xt("sub",()=>new _h);Xt("mul",()=>new yh);Xt("div",()=>new bh);Xt("neg",()=>new wh);Xt("pow",()=>new xh);Xt("exp",()=>new Sh);Xt("log",()=>new vh);Xt("sqrt",()=>new Bh);Xt("tanh",()=>new kh);Xt("sigmoid",()=>new Ah);Xt("relu",()=>new Nh);Xt("softmax",()=>new Rh);Xt("log_softmax",()=>new Oh);Xt("gelu",()=>new Eh);Xt("silu",()=>new Th);Xt("sum",()=>new Ih);Xt("mean",()=>new Mh);Xt("matmul",()=>new Ch);Xt("dot",()=>new Dh);Xt("cat",()=>new Lp);Xt("stack",()=>new Pp);Xt("clamp",()=>new zp);Xt("pad",()=>new $p);Xt("index_select",()=>new ta);Xt("where",()=>new Fp);function Pk(n){let t=t0.get(n);return t?t():null}f(Pk,"getGradFn");function zk(n){return t0.has(n)}f(zk,"hasGradFn");Wp();Yi();Br();Ps();tn();vr();Ps();ae();$t();function $k(n,t){let e=n.gradFn;if(!e)throw new Error("Cannot call backward on a tensor that does not require grad");let s=t;if(!s){if(n.numel!==1)throw new Error("grad must be specified for non-scalar tensors");s=$e(n.shape,{dtype:n.dtype,device:n.device})}let r=new Map;yI(e,r,new Set);let i=new Map;i.set(e.id,[s]);let a=[];for(let[l,u]of r)u===0&&a.push(l);let c=[];for(;a.length>0;){let l=a.pop();c.push(l);for(let u of l.nextEdges){if(!u||!u.node)continue;let h=u.node,p=r.get(h)-1;r.set(h,p),p===0&&a.push(h)}}for(let l of c){let u=i.get(l.id);if(!u)continue;let h=l.apply(u),p=h;if(h&&(p=h.map((m,g)=>{if(!m)return m;let _=l.inputMetadata(g);return _?_I(m,_.shape):m})),l.releaseVariables(),!p)continue;let d=l.nextEdges;for(let m=0;m<d.length;m++){let g=d[m];if(!g||!g.node||m>=p.length||!p[m])continue;let _=g.node.id,y=i.get(_);if(y)y[g.inputNr]=y[g.inputNr]?ut(y[g.inputNr],p[m]):p[m];else{let b=[];b[g.inputNr]=p[m],i.set(_,b)}}}}f($k,"backward");function _I(n,t){let e=n.shape;if(e.length===t.length&&e.every((o,i)=>o===t[i]))return n;let s=n,r=e.length-t.length;for(let o=0;o<r;o++)s=ie(s,0,!1);for(let o=t.length-1;o>=0;o--)t[o]===1&&s.shape[o]!==1&&(s=ie(s,o,!0));return s}f(_I,"_reduceBroadcastGrad");function yI(n,t,e){let s=[n];for(t.set(n,0);s.length>0;){let r=s.shift();if(!e.has(r.id)){e.add(r.id);for(let o of r.nextEdges){if(!o||!o.node)continue;let i=o.node;t.has(i)||t.set(i,0),t.set(i,t.get(i)+1),e.has(i.id)||s.push(i)}}}}f(yI,"_countDeps");Ye();Qn();$t();ae();Ye();var Lh=class extends wt{static{f(this,"ReshapeBackward")}constructor(){super(1)}apply(t){let e=this.inputMetadata(0);return[An(t[0],e.shape)]}},Ph=class extends wt{static{f(this,"TransposeBackward")}constructor(t,e){super(1),this._dim0=t,this._dim1=e}apply(t){return[en(t[0],this._dim0,this._dim1)]}},zh=class extends wt{static{f(this,"SliceBackward")}constructor(t,e,s,r){super(1),this._dim=t,this._start=e,this._end=s,this._step=r}apply(t){let e=t[0],s=this.inputMetadata(0),r=this._dim,o=s.shape[r],i=this._step||1;if(i===1){let y=s.shape.map(()=>0),b=s.shape.map(()=>0);return y[r]=this._start,b[r]=o-this._end,[Xi(e,y,b,0)]}let a=Nt(s.shape,{dtype:e.dtype,device:e.device}),c=a._impl.storage.data,l=e._impl.storage.data,u=e._impl.storageOffset,h=e.shape,p=e.strides,d=a.strides,m=h.length,g=new Int32Array(m),_=u;for(let y=0;y<e.numel;y++){let b=0;for(let x=0;x<m;x++){let S=x===r?this._start+g[x]*i:g[x];b+=S*d[x]}c[b]+=l[_];for(let x=m-1;x>=0;x--){if(g[x]++,g[x]<h[x]){_+=p[x];break}_-=(h[x]-1)*p[x],g[x]=0}}return[a]}},$h=class extends wt{static{f(this,"SelectBackward")}constructor(t,e){super(1),this._dim=t,this._index=e}apply(t){let e=t[0],s=this.inputMetadata(0),r=this._dim,o=s.shape[r],i=sn(e,r),a=s.shape.map(()=>0),c=s.shape.map(()=>0);return a[r]=this._index,c[r]=o-1-this._index,[Xi(i,a,c,0)]}},Fh=class extends wt{static{f(this,"ExpandBackward")}constructor(){super(1)}apply(t){let e=t[0],r=this.inputMetadata(0).shape,o=e.shape,i=[],a=o.length-r.length;for(let l=0;l<a;l++)i.push(l);for(let l=0;l<r.length;l++)r[l]===1&&o[l+a]!==1&&i.push(l+a);let c=e;return i.length>0&&(c=ie(e,i,!0)),[An(c,r)]}},Vh=class extends wt{static{f(this,"PermuteBackward")}constructor(t){super(1),this._dims=t}apply(t){let e=new Array(this._dims.length);for(let s=0;s<this._dims.length;s++)e[this._dims[s]]=s;return[b_(t[0],e)]}};fv({backward:$k});wv({GradMode:sr,ReshapeBackward:Lh,TransposeBackward:Ph,PermuteBackward:Vh,SliceBackward:zh,ExpandBackward:Fh,SelectBackward:$h,GradAccumulator:Ir});function bI(n){let t=n._impl;if(Ke()&&t.device&&t.device.type===pe.GPU){t.storage.retain();let r=new he(t.storage,t.storageOffset,t.sizes(),t.strides(),t.dtype,t.device);return new zt(r)}let e=t.storage.clone(),s=new he(e,t.storageOffset,t.sizes(),t.strides(),t.dtype,t.device);return new zt(s)}f(bI,"_snapshotTensor");function wI(n){for(let t=0;t<n.length;t++){let e=n[t];if(e&&e._impl&&e.requiresGrad)return!0;if(Array.isArray(e)){for(let s=0;s<e.length;s++)if(e[s]&&e[s]._impl&&e[s].requiresGrad)return!0}}return!1}f(wI,"_anyRequiresGrad");function xI(n){let t=[];for(let e=0;e<n.length;e++){let s=n[e];if(s&&s._impl)t.push(s);else if(Array.isArray(s))for(let r=0;r<s.length;r++)s[r]&&s[r]._impl&&t.push(s[r])}return t}f(xI,"_extractTensors");function SI(n){let t=n._impl.autogradMeta;if(!t)return null;let e=t.getGradAccumulator();return e||(e=new Ir(n),t.setGradAccumulator(e)),e}f(SI,"_getOrCreateAccumulator");function vI(n,t){let e=Re.dispatch.bind(Re);return(s,...r)=>{if(!sr.isEnabled()||!wI(r)){let l=s.subtract(rc);return Re.redispatch(t,l,...r)}let o=Pk(n);if(!o){let l=s.subtract(rc);return Re.redispatch(t,l,...r)}let i=xI(r);o.setOpArgs(r);for(let l=0;l<i.length;l++)o.saveTensor(bI(i[l])),o.saveInputMetadata(l,[...i[l].shape],i[l].dtype);for(let l=0;l<i.length;l++){let u=i[l];if(u.requiresGrad){let h=u.gradFn;if(h){let p=u._impl.autogradMeta?u._impl.autogradMeta.outputNr:0;o.setNextEdge(l,h,p)}else{let p=SI(u);p&&o.setNextEdge(l,p,0)}}}let a=s.subtract(rc),c=Re.redispatch(t,a,...r);if(c&&c._impl){c._impl.autogradMeta||c._impl.setAutogradMeta(new ln);let l=c._impl.autogradMeta;l.setGradFn(o,0),l.requiresGrad=!0,c._impl._updateKeySet()}return c}}f(vI,"wrapWithAutograd");function BI(n){return(t,...e)=>{let s=t.subtract(rc);return Re.redispatch(n,s,...e)}}f(BI,"_makePassthrough");function Fk(){let n=Re.listOps(),t=[nt.AUTOGRAD,nt.AUTOGRAD_CPU,nt.AUTOGRAD_GPU,nt.AUTOGRAD_WASM];for(let e of n){let s=Re.findOp(e);if(!s)continue;let r=s.name,o=zk(r)?vI(r,s):BI(s),i=Ls.fromUnboxed(o);for(let a of t)s.entry.registerKernel(a,i)}}f(Fk,"registerAutogradKernels");$t();$t();ae();Qr();function e0(n,t,e,s){let r=Nt(n.shape,{dtype:n.dtype}),o=Qi(r,t,e,s),i=Qi(r,t,e,$e(s.shape,{dtype:n.dtype}));return Es(Go(i,Nt(i.shape,{dtype:i.dtype})),o,n)}f(e0,"scatter");function ha(n,t){return n<0?t+n:n}f(ha,"_normDim");function s0(n,t,e=0){let s=n.shape.length,r=ha(e,s),o=n.shape[r],i=(t%o+o)%o;return i===0?n:Ip([n.narrow(r,o-i,i),n.narrow(r,0,o-i)],r)}f(s0,"roll");function n0(n,t){let e=Array.isArray(t)?t:[t],s=n.shape.length,r=n;for(let o of e){let i=ha(o,s),a=r.shape[i],c=new Array(a);for(let l=0;l<a;l++)c[l]=a-1-l;r=qo(r,i,tr(c,{dtype:"i32"}))}return r}f(n0,"flip");function r0(n,t=0){let e=n.shape.length,s=ha(t,e),r=n.shape[s],o=n;for(let i=1;i<r;i*=2){let a=[...o.shape];a[s]=i;let c=Ip([Nt(a,{dtype:o.dtype}),o.narrow(s,0,r-i)],s);o=ut(o,c)}return o}f(r0,"cumsum");function kI(n){let t=1;for(;t<n;)t<<=1;return t}f(kI,"_nextPow2");function Gh(n,t,e){let s=n.shape.length,r=s-1,o=n.shape[r],i=kI(o),a=t?-1/0:1/0,c=n;if(i>o){let h=new Array(s).fill(0),p=new Array(s).fill(0);p[r]=i-o,c=Xi(n,h,p,a)}let l=new Array(s).fill(1);l[r]=i;let u=null;if(e){let h=new Array(i);for(let p=0;p<i;p++)h[p]=p;u=tr(h,{dtype:"i32"}).reshape(l)}for(let h=2;h<=i;h<<=1)for(let p=h>>1;p>=1;p>>=1){let d=new Array(i),m=new Array(i);for(let S=0;S<i;S++){let v=S^p;d[S]=v;let w=(S&h)===0,A=t?!w:w;v>S?m[S]=A?1:0:m[S]=A?0:1}let g=tr(d,{dtype:"i32"}),_=qo(c,r,g),y=qc(c,_),b=Rr(c,_),x=tr(m,{dtype:"f32"}).reshape(l);if(e){let S=qo(u,r,g),v=Es(x,Zi(y,c),Zi(b,c));u=Es(v,u,S)}c=Es(x,y,b)}return i>o&&(c=c.narrow(r,0,o),e&&(u=u.narrow(r,0,o))),e?{values:c,indices:u}:c}f(Gh,"_bitonicLastDim");function o0(n,t=-1,e=!1){let s=n.shape.length,r=ha(t,s);if(r===s-1)return Gh(n,e,!1);let o=n.transpose(r,s-1);return Gh(o,e,!1).transpose(r,s-1)}f(o0,"sort");function Vk(n,t,e){let s=n.shape.length,r=ha(t,s);if(r===s-1)return Gh(n,e,!0);let o=n.transpose(r,s-1),i=Gh(o,e,!0);return{values:i.values.transpose(r,s-1),indices:i.indices.transpose(r,s-1)}}f(Vk,"_sortWithIndices");function i0(n,t=-1,e=!1){return Vk(n,t,e).indices}f(i0,"argsort");function a0(n,t,e=-1,s=!0){let r=n.shape.length,o=ha(e,r),{values:i,indices:a}=Vk(n,o,s);return[i.narrow(o,0,t),a.narrow(o,0,t)]}f(a0,"topk");Ye();Qr();function Gk(n){let t=n.prototype;t.to=function(e){if(this.device.equals(e))return this;let r=this.contiguous().data.slice(0,this.numel);return Fo(r,this.shape,this.dtype,{device:e})},t.add=function(e){return ut(this,e)},t.sub=function(e){return Wt(this,e)},t.mul=function(e){return et(this,e)},t.div=function(e){return Se(this,e)},t.neg=function(){return Rn(this)},t.pow=function(e){return to(this,e)},t.remainder=function(e){return Tv(this,e)},t.maximum=function(e){return Rr(this,e)},t.minimum=function(e){return qc(this,e)},t.exp=function(){return On(this)},t.log=function(){return Or(this)},t.sqrt=function(){return Nr(this)},t.rsqrt=function(){return S_(this)},t.abs=function(){return Ji(this)},t.sin=function(){return v_(this)},t.cos=function(){return B_(this)},t.tanh=function(){return pn(this)},t.sigmoid=function(){return bs(this)},t.relu=function(){return Vo(this)},t.gelu=function(){return Uc(this)},t.silu=function(){return Wc(this)},t.sign=function(){return jc(this)},t.floor=function(){return k_(this)},t.ceil=function(){return A_(this)},t.eq=function(e){return Zi(this,e)},t.ne=function(e){return R_(this,e)},t.lt=function(e){return O_(this,e)},t.le=function(e){return Kc(this,e)},t.gt=function(e){return Go(this,e)},t.ge=function(e){return Hc(this,e)},t.sum=function(e,s){return ie(this,e,s)},t.mean=function(e,s){return Nn(this,e,s)},t.max=function(e,s){return Er(this,e,s)},t.min=function(e,s){return I_(this,e,s)},t.argmax=function(e,s){return M_(this,e,s)},t.argmin=function(e,s){return C_(this,e,s)},t.prod=function(e,s){return D_(this,e,s)},t.matmul=function(e){return Je(this,e)},t.dot=function(e){return L_(this,e)},t.mm=function(e){return Je(this,e)},t.softmax=function(e=-1){return er(this,e)},t.log_softmax=function(e=-1){return Yc(this,e)},t.requires_grad=function(e=!0){return this.requiresGrad_(e)},t.clone=function(){return P_(this)},t.roll=function(e,s=0){return s0(this,e,s)},t.flip=function(e){return n0(this,e)},t.cumsum=function(e=0){return r0(this,e)},t.sort=function(e=-1,s=!1){return o0(this,e,s)},t.topk=function(e,s=-1,r=!0){return a0(this,e,s,r)},t.argsort=function(e=-1,s=!1){return i0(this,e,s)},t.gather=function(e,s){return T_(this,e,s)},t.scatter_add=function(e,s,r){return Qi(this,e,s,r)},t.scatter=function(e,s,r){return e0(this,e,s,r)},kv(n)}f(Gk,"installOps");Ps();Ps();ae();ae();function pu(n,t){return{dtype:t?.dtype??n.dtype,device:t?.device??n.device,requiresGrad:t?.requiresGrad??!1}}f(pu,"_likeOpts");function RI(n,t){return ze(n.shape,pu(n,t))}f(RI,"emptyLike");function OI(n,t){return Nt(n.shape,pu(n,t))}f(OI,"zerosLike");function NI(n,t){return $e(n.shape,pu(n,t))}f(NI,"onesLike");function EI(n,t,e){return Lt(n.shape,t,pu(n,e))}f(EI,"fullLike");function TI(n,t){return Dp(n.shape,pu(n,t))}f(TI,"randnLike");Qr();$t();Tr();var Zk={};Ft(Zk,{AdaptiveAvgPool2d:()=>Tu,AvgPool2d:()=>Eu,BCELoss:()=>Du,BatchNorm1d:()=>_a,BatchNorm2d:()=>Ou,Conv1d:()=>yu,Conv2d:()=>_u,CrossEntropyLoss:()=>Mu,Dropout:()=>Ms,ELU:()=>ku,Embedding:()=>Lu,F:()=>Jk,Flatten:()=>Uu,GELU:()=>wu,GRU:()=>Fu,GRUCell:()=>ya,GroupNorm:()=>Xh,LSTM:()=>Vu,LSTMCell:()=>ba,LayerNorm:()=>Mn,LeakyReLU:()=>Bu,Linear:()=>Ge,LogSoftmax:()=>Ru,MSELoss:()=>Iu,MaxPool2d:()=>Nu,Module:()=>ct,ModuleDict:()=>qu,ModuleList:()=>io,MultiheadAttention:()=>ao,NLLLoss:()=>Cu,Parameter:()=>Qt,PositionalEncoding:()=>ju,ReLU:()=>bu,Sequential:()=>Gu,SiLU:()=>xu,Sigmoid:()=>Su,Softmax:()=>Au,Tanh:()=>vu,Transformer:()=>Wu,TransformerDecoder:()=>xa,TransformerDecoderLayer:()=>Qo,TransformerEncoder:()=>wa,TransformerEncoderLayer:()=>Xo,functional:()=>ma,init:()=>qh});Ps();var Qt=class extends zt{static{f(this,"Parameter")}constructor(t,e=!0){t instanceof zt?super(t._impl):super(t),e&&this.requiresGrad_(!0)}get isParameter(){return!0}};var ct=class n{static{f(this,"Module")}constructor(){this._parameters=new Map,this._buffers=new Map,this._modules=new Map,this._training=!0}forward(){throw new Error(`${this.constructor.name}.forward() not implemented`)}call(...t){return this.forward(...t)}registerParameter(t,e){if(e!==null&&!(e instanceof Qt))throw new Error("Expected Parameter instance");this._parameters.set(t,e)}registerBuffer(t,e){this._buffers.set(t,e)}registerModule(t,e){if(e!==null&&!(e instanceof n))throw new Error("Expected Module instance");this._modules.set(t,e)}*parameters(t=!0){this._autoDetect();for(let[,e]of this._parameters)e!==null&&(yield e);if(t)for(let[,e]of this._modules)e!==null&&(yield*e.parameters(!0))}*namedParameters(t="",e=!0){this._autoDetect();let s=t?t+".":"";for(let[r,o]of this._parameters)o!==null&&(yield[s+r,o]);if(e)for(let[r,o]of this._modules)o!==null&&(yield*o.namedParameters(s+r,!0))}*buffers(t=!0){for(let[,e]of this._buffers)e!==null&&(yield e);if(t)for(let[,e]of this._modules)e!==null&&(yield*e.buffers(!0))}*children(){this._autoDetect();for(let[,t]of this._modules)t!==null&&(yield t)}*namedChildren(){this._autoDetect();for(let[t,e]of this._modules)e!==null&&(yield[t,e])}*modules(){yield this,this._autoDetect();for(let[,t]of this._modules)t!==null&&(yield*t.modules())}*namedModules(t=""){yield[t,this],this._autoDetect();let e=t?t+".":"";for(let[s,r]of this._modules)r!==null&&(yield*r.namedModules(e+s))}stateDict(t=""){this._autoDetect();let e=new Map,s=t?t+".":"";for(let[r,o]of this._parameters)o!==null&&e.set(s+r,o);for(let[r,o]of this._buffers)o!==null&&e.set(s+r,o);for(let[r,o]of this._modules)if(o!==null)for(let[i,a]of o.stateDict(s+r))e.set(i,a);return e}loadStateDict(t){this._autoDetect();for(let[e,s]of this._parameters){let r=e;if(t.has(r)&&s!==null){let o=t.get(r),i=s._impl.storage.data,a=o._impl?o._impl.storage.data:o.data;for(let c=0;c<i.length;c++)i[c]=a[c]}}for(let[e,s]of this._modules)if(s!==null){let r=new Map,o=e+".";for(let[i,a]of t)i.startsWith(o)&&r.set(i.substring(o.length),a);r.size>0&&s.loadStateDict(r)}}train(t=!0){this._training=t;for(let[,e]of this._modules)e!==null&&e.train(t);return this}eval(){return this.train(!1)}get training(){return this._training}to(t){this._autoDetect();for(let[e,s]of this._parameters)s!==null&&typeof s.to=="function"&&(this._parameters.set(e,new Qt(s.to(t),s.requiresGrad)),this[e]=this._parameters.get(e));for(let[e,s]of this._buffers)s!==null&&typeof s.to=="function"&&(this._buffers.set(e,s.to(t)),this[e]=this._buffers.get(e));for(let[,e]of this._modules)e!==null&&e.to(t);return this}apply(t){for(let[,e]of this._modules)e!==null&&e.apply(t);return t(this),this}zeroGrad(){for(let t of this.parameters())if(t.grad){let e=t.grad._impl.storage.data;e&&e.fill(0)}return this}_autoDetect(){if(this._detected)return;this._detected=!0;let t=Object.keys(this);for(let e of t){if(e.startsWith("_"))continue;let s=this[e];s instanceof Qt&&!this._parameters.has(e)?this._parameters.set(e,s):s instanceof n&&!this._modules.has(e)&&this._modules.set(e,s)}}toString(){return this._buildRepr("")}_buildRepr(t){this._autoDetect();let e=this.constructor.name;if(this._modules.size===0)return e+"()";let s=[e+"("];for(let[r,o]of this._modules)o!==null&&s.push(t+" ("+r+"): "+o._buildRepr(t+" "));return s.push(t+")"),s.join(`
|
|
201
|
-
`)}};var qh={};Ft(qh,{_calculateFanInFanOut:()=>Dr,constant_:()=>CI,kaiming_normal_:()=>PI,kaiming_uniform_:()=>da,normal_:()=>hu,ones_:()=>MI,uniform_:()=>Jo,xavier_normal_:()=>LI,xavier_uniform_:()=>DI,zeros_:()=>II});function Dr(n){let t=n.shape,e=t.length;if(e<2)throw new Error("Fan in/out requires at least 2D tensor");let s=t[1],r=t[0],o=1;for(let i=2;i<e;i++)o*=t[i];return{fanIn:s*o,fanOut:r*o}}f(Dr,"_calculateFanInFanOut");function Jo(n,t=0,e=1){let s=n._impl.storage.data;if(!s)return n;let r=e-t;for(let o=0;o<s.length;o++)s[o]=t+Math.random()*r;return n._impl.bumpVersion(),n}f(Jo,"uniform_");function hu(n,t=0,e=1){let s=n._impl.storage.data;if(!s)return n;for(let r=0;r<s.length;r+=2){let o=Math.random()||1e-10,i=Math.random(),a=Math.sqrt(-2*Math.log(o)),c=6.283185307179586*i;s[r]=t+e*a*Math.cos(c),r+1<s.length&&(s[r+1]=t+e*a*Math.sin(c))}return n._impl.bumpVersion(),n}f(hu,"normal_");function II(n){let t=n._impl.storage.data;return t&&t.fill(0),n._impl.bumpVersion(),n}f(II,"zeros_");function MI(n){let t=n._impl.storage.data;return t&&t.fill(1),n._impl.bumpVersion(),n}f(MI,"ones_");function CI(n,t){let e=n._impl.storage.data;return e&&e.fill(t),n._impl.bumpVersion(),n}f(CI,"constant_");function DI(n,t=1){let{fanIn:e,fanOut:s}=Dr(n),r=t*Math.sqrt(2/(e+s)),o=Math.sqrt(3)*r;return Jo(n,-o,o)}f(DI,"xavier_uniform_");function LI(n,t=1){let{fanIn:e,fanOut:s}=Dr(n),r=t*Math.sqrt(2/(e+s));return hu(n,0,r)}f(LI,"xavier_normal_");function da(n,t=0,e="fan_in",s="leaky_relu"){let{fanIn:r,fanOut:o}=Dr(n),i=e==="fan_in"?r:o,c=qk(s,t)/Math.sqrt(i),l=Math.sqrt(3)*c;return Jo(n,-l,l)}f(da,"kaiming_uniform_");function PI(n,t=0,e="fan_in",s="leaky_relu"){let{fanIn:r,fanOut:o}=Dr(n),i=e==="fan_in"?r:o,c=qk(s,t)/Math.sqrt(i);return hu(n,0,c)}f(PI,"kaiming_normal_");function qk(n,t=.01){switch(n){case"linear":case"sigmoid":return 1;case"tanh":return 5/3;case"relu":return Math.sqrt(2);case"leaky_relu":return Math.sqrt(2/(1+t*t));default:return 1}}f(qk,"_calculateGain");var ma={};Ft(ma,{elu:()=>d0,gelu:()=>mu,leaky_relu:()=>h0,log_softmax:()=>p0,relu:()=>du,sigmoid:()=>u0,silu:()=>c0,softmax:()=>f0,tanh:()=>l0});$t();ae();function du(n){return Vo(n)}f(du,"relu");function mu(n){return Uc(n)}f(mu,"gelu");function c0(n){return Wc(n)}f(c0,"silu");function u0(n){return bs(n)}f(u0,"sigmoid");function l0(n){return pn(n)}f(l0,"tanh");function f0(n,t=-1){return er(n,t)}f(f0,"softmax");function p0(n,t=-1){return Yc(n,t)}f(p0,"log_softmax");function h0(n,t=.01){let e=et(n,Lt(n.shape,t,{dtype:n.dtype,device:n.device}));return Rr(n,e)}f(h0,"leaky_relu");function d0(n,t=1){let e=Lt(n.shape,0,{dtype:n.dtype,device:n.device}),s=Lt(n.shape,1,{dtype:n.dtype,device:n.device}),r=Lt(n.shape,t,{dtype:n.dtype,device:n.device}),o=Go(n,e),i=et(r,Wt(On(n),s));return Es(o,n,i)}f(d0,"elu");var Wh={};Ft(Wh,{batch_norm:()=>_0,group_norm:()=>g0,layer_norm:()=>m0});$t();ae();ga();function m0(n,t,e,s,r=1e-5){if((n instanceof ue||n.isSymbolic)&&e&&s){let m=n.ndim-t.length;return Iv(n,e,s,m,r)}let o=n.ndim-t.length,i=[];for(let m=o;m<n.ndim;m++)i.push(m);let a=n;for(let m=i.length-1;m>=0;m--)a=Nn(a,i[m],!0);let c=Wt(n,a),u=et(c,c);for(let m=i.length-1;m>=0;m--)u=Nn(u,i[m],!0);let h=Lt([],r),p=Se(Lt([],1),Nr(ut(u,h))),d=et(c,p);return e&&(d=et(d,e)),s&&(d=ut(d,s)),d}f(m0,"layer_norm");function g0(n,t,e,s,r=1e-5){let o=n.shape,i=o[0],a=o[1],c=o.slice(2),l=n.reshape([i,t,a/t,...c]),u=[];for(let x=2;x<l.ndim;x++)u.push(x);let h=l;for(let x=u.length-1;x>=0;x--)h=Nn(h,u[x],!0);let p=Wt(l,h),m=et(p,p);for(let x=u.length-1;x>=0;x--)m=Nn(m,u[x],!0);let g=Lt([],r),_=Se(Lt([],1),Nr(ut(m,g))),y=et(p,_).reshape(o),b=[1,a,...c.map(()=>1)];return e&&(y=et(y,e.reshape(b))),s&&(y=ut(y,s.reshape(b))),y}f(g0,"group_norm");var Uh=1;function $I(n,t){let e=new Array(n).fill(1);return e[Uh]=t,e}f($I,"channelShape");function Uk(n,t){let e=n;for(let s=t.length-1;s>=0;s--)e=Nn(e,t[s],!0);return e}f(Uk,"reduceMeanOver");function Wk(n,t,e){let s=n.data;if(!s)return;let r=t.reshape([n.shape[0]]).toArray();for(let o=0;o<s.length;o++)s[o]=s[o]*(1-e)+r[o]*e}f(Wk,"blendRunning");function _0(n,t,e,s,r,o=!0,i=1e-5,a=.1){let c=n instanceof ue||n.isSymbolic;if(!o)return Mv(n,s,r,t,e,Uh,i);let l=[];for(let _=0;_<n.ndim;_++)_!==Uh&&l.push(_);let u=Uk(n,l),h=Wt(n,u),p=Uk(et(h,h),l),d=Se(Lt([],1),Nr(ut(p,Lt([],i)))),m=et(h,d),g=$I(n.ndim,n.shape[Uh]);return s&&(m=et(m,s.reshape(g))),r&&(m=ut(m,r.reshape(g))),c||(t&&Wk(t,u,a),e&&Wk(e,p,a)),m}f(_0,"batch_norm");var b0={};Ft(b0,{linear:()=>y0});$t();Ye();ga();function y0(n,t,e){let s;n instanceof ue||n.isSymbolic?s=Mp(t,0,1):s=en(t,0,1);let r=Je(n,s);return e?ut(r,e):r}f(y0,"linear");var Kh={};Ft(Kh,{conv1d:()=>w0,conv2d:()=>jh});$t();Ye();function jh(n,t,e,s=[1,1],r=[[0,0],[0,0]],o=[1,1],i=1){let a=Array.isArray(s)?s:[s,s],c=FI(r),l=Array.isArray(o)?o:[o,o],u=Cv(n,t,a,c,l,i);if(e){let h=An(e,[1,e.shape[0],1,1]);return ut(u,h)}return u}f(jh,"conv2d");function w0(n,t,e,s=1,r=0,o=1,i=1){let a=sn(n,2),c=sn(t,2),l=[1,Array.isArray(s)?s[0]:s],u=[[0,0],VI(r)],h=[1,Array.isArray(o)?o[0]:o],p=jh(a,c,null,l,u,h,i),d=Vc(p,2);if(e){let m=An(e,[1,e.shape[0],1]);return ut(d,m)}return d}f(w0,"conv1d");function FI(n){return typeof n=="number"?[[n,n],[n,n]]:Array.isArray(n)&&typeof n[0]=="number"?n.map(t=>[t,t]):n}f(FI,"_normalizePadding");function VI(n){return typeof n=="number"?[n,n]:n}f(VI,"_normalizePadding1d");var Yh={};Ft(Yh,{adaptive_avg_pool2d:()=>S0,avg_pool2d:()=>Hh,max_pool2d:()=>x0});$t();function x0(n,t,e,s=[[0,0],[0,0]]){let r=Array.isArray(t)?t:[t,t],o=e?Array.isArray(e)?e:[e,e]:r,i=jk(s);return z_(n,"max",r,o,i)}f(x0,"max_pool2d");function Hh(n,t,e,s=[[0,0],[0,0]]){let r=Array.isArray(t)?t:[t,t],o=e?Array.isArray(e)?e:[e,e]:r,i=jk(s);return z_(n,"avg",r,o,i)}f(Hh,"avg_pool2d");function S0(n,t){let[e,s]=Array.isArray(t)?t:[t,t],r=n.shape[2],o=n.shape[3],i=Math.floor(r/e),a=Math.floor(o/s);return Hh(n,[i,a],[i,a],[[0,0],[0,0]])}f(S0,"adaptive_avg_pool2d");function jk(n){if(typeof n=="number")return[[n,n],[n,n]];if(Array.isArray(n)&&typeof n[0]=="number"){let t=n.map(e=>[e,e]);return t.length===1?[t[0],t[0]]:t}return n}f(jk,"_normalizePadding");var v0={};Ft(v0,{dropout:()=>gu});$t();ae();function gu(n,t=.5,e=!0){if(!e||t===0)return n;if(t===1)return Lt(n.shape,0,{dtype:n.dtype,device:n.device});let s=GI(n.shape,1-t,n.dtype,n.device),r=Lt(n.shape,1/(1-t),{dtype:n.dtype,device:n.device});return et(et(n,s),r)}f(gu,"dropout");function GI(n,t,e,s){let r=ze(n,{dtype:e,device:s}),o=r._impl.storage.data;if(o)for(let i=0;i<o.length;i++)o[i]=Math.random()<t?1:0;return r}f(GI,"_bernoulliMask");var Zh={};Ft(Zh,{binary_cross_entropy:()=>A0,cross_entropy:()=>k0,mse_loss:()=>B0,nll_loss:()=>Jh});$t();ae();Ye();kn();function B0(n,t,e="mean"){let s=Wt(n,t),r=et(s,s);return Kk(r,e)}f(B0,"mse_loss");function qI(n,t){let e=UI(n,t),s=Wt(n,e),r=On(s),o=WI(r,t),i=Or(o);return Wt(s,i)}f(qI,"_logSoftmaxAutograd");function UI(n,t){let e=t<0?n.ndim+t:t;return Er(n,e,!0)}f(UI,"_dimMax");function WI(n,t){let e=t<0?n.ndim+t:t;return ie(n,e,!0)}f(WI,"_dimSum");function Jh(n,t,e="mean",s=null){let r=n.ndim-1,o=n.shape[r],i=E_(t,o),a=ie(et(n,i),r),c=a,l=null;if(s!==null){let h=as(i,r,s),p=ut(Rn(h),1);c=et(a,p),l=ie(p)}let u=Rn(ie(c));if(e==="sum")return u;if(e==="mean")return l!==null?Se(u,l):Se(u,Pe(t.shape));throw new Error(`nll_loss: unknown reduction '${e}'`)}f(Jh,"nll_loss");function k0(n,t,e="mean",s=null){let r=qI(n,-1);return Jh(r,t,e,s)}f(k0,"cross_entropy");function A0(n,t,e="mean"){let s=Lt(n.shape,1e-7,{dtype:n.dtype,device:n.device}),r=Lt(n.shape,1,{dtype:n.dtype,device:n.device}),o=Or(ut(n,s)),i=Or(ut(Wt(r,n),s)),a=Rn(ut(et(t,o),et(Wt(r,t),i)));return Kk(a,e)}f(A0,"binary_cross_entropy");function Kk(n,t){if(t==="mean")return Nn(n);if(t==="sum")return ie(n);if(t==="none")return n;throw new Error(`Unknown reduction: ${t}`)}f(Kk,"_reduce");var O0={};Ft(O0,{embedding:()=>R0});$t();function R0(n,t){let e=n.shape[n.shape.length-1],s=t.shape.reduce((o,i)=>o*i,1);return qo(n,0,t.reshape([s])).reshape([...t.shape,e])}f(R0,"embedding");var E0={};Ft(E0,{scaled_dot_product_attention:()=>N0});$t();Ye();ga();ae();function jI(n,t){if(n instanceof ue||n.isSymbolic)return er(n,t);let e=t<0?n.ndim+t:t,s=Er(n,e,!0).detach(),r=Wt(n,s),o=On(r),i=ie(o,e,!0);return Se(o,i)}f(jI,"_softmax");function KI(n){let t=n.ndim;return n instanceof ue||n.isSymbolic?Mp(n,t-2,t-1):en(n,t-2,t-1)}f(KI,"_transposeLastTwo");function HI(n,t){let e=ze([n,t]),s=e._impl.storage.data,r=t-n;for(let o=0;o<n;o++)for(let i=0;i<t;i++)s[o*t+i]=i<=o+r?0:-1/0;return e}f(HI,"_generateCausalMask");function N0(n,t,e,s=null,r=0,o=!1,i=!1){let a=n.shape[n.ndim-1],c=n.shape[n.ndim-2],l=t.shape[t.ndim-2],u=Lt([],1/Math.sqrt(a)),h=KI(t),p=Je(n,h);p=et(p,u),o&&(p=ut(p,HI(c,l))),s&&(p=ut(p,s));let d=jI(p,-1);return r>0&&i&&(d=gu(d,r,!0)),Je(d,e)}f(N0,"scaled_dot_product_attention");ae();var Ge=class extends ct{static{f(this,"Linear")}constructor(t,e,s=!0){super(),this.inFeatures=t,this.outFeatures=e,this.weight=new Qt(ze([e,t])),this.bias=s?new Qt(Nt([e])):null,this._resetParameters()}_resetParameters(){if(da(this.weight,Math.sqrt(5)),this.bias){let{fanIn:t}=Dr(this.weight),e=1/Math.sqrt(t);Jo(this.bias,-e,e)}}forward(t){return y0(t,this.weight,this.bias)}};ae();var _u=class extends ct{static{f(this,"Conv2d")}constructor(t,e,s,r={}){super(),this.inChannels=t,this.outChannels=e,this.kernelSize=Array.isArray(s)?s:[s,s],this.stride=r.stride?Array.isArray(r.stride)?r.stride:[r.stride,r.stride]:[1,1],this.padding=r.padding??0,this.dilation=r.dilation?Array.isArray(r.dilation)?r.dilation:[r.dilation,r.dilation]:[1,1],this.groups=r.groups??1;let o=this.kernelSize[0],i=this.kernelSize[1];this.weight=new Qt(ze([e,t/this.groups,o,i])),this.bias=r.bias!==!1?new Qt(Nt([e])):null,this._resetParameters()}_resetParameters(){if(da(this.weight,Math.sqrt(5)),this.bias){let{fanIn:t}=Dr(this.weight),e=1/Math.sqrt(t);Jo(this.bias,-e,e)}}forward(t){return jh(t,this.weight,this.bias,this.stride,this.padding,this.dilation,this.groups)}},yu=class extends ct{static{f(this,"Conv1d")}constructor(t,e,s,r={}){super(),this.inChannels=t,this.outChannels=e,this.kernelSize=Array.isArray(s)?s[0]:s,this.stride=r.stride??1,this.padding=r.padding??0,this.dilation=r.dilation??1,this.groups=r.groups??1,this.weight=new Qt(ze([e,t/this.groups,this.kernelSize])),this.bias=r.bias!==!1?new Qt(Nt([e])):null,da(this.weight,Math.sqrt(5))}forward(t){return w0(t,this.weight,this.bias,this.stride,this.padding,this.dilation,this.groups)}};var bu=class extends ct{static{f(this,"ReLU")}forward(t){return du(t)}},wu=class extends ct{static{f(this,"GELU")}forward(t){return mu(t)}},xu=class extends ct{static{f(this,"SiLU")}forward(t){return c0(t)}},Su=class extends ct{static{f(this,"Sigmoid")}forward(t){return u0(t)}},vu=class extends ct{static{f(this,"Tanh")}forward(t){return l0(t)}},Bu=class extends ct{static{f(this,"LeakyReLU")}constructor(t=.01){super(),this.negativeSlope=t}forward(t){return h0(t,this.negativeSlope)}},ku=class extends ct{static{f(this,"ELU")}constructor(t=1){super(),this.alpha=t}forward(t){return d0(t,this.alpha)}},Au=class extends ct{static{f(this,"Softmax")}constructor(t=-1){super(),this.dim=t}forward(t){return f0(t,this.dim)}},Ru=class extends ct{static{f(this,"LogSoftmax")}constructor(t=-1){super(),this.dim=t}forward(t){return p0(t,this.dim)}};ae();var Mn=class extends ct{static{f(this,"LayerNorm")}constructor(t,e=1e-5,s=!0){super(),this.normalizedShape=Array.isArray(t)?t:[t],this.eps=e,this.weight=s?new Qt($e(this.normalizedShape)):null,this.bias=s?new Qt(Nt(this.normalizedShape)):null}forward(t){return m0(t,this.normalizedShape,this.weight,this.bias,this.eps)}},Xh=class extends ct{static{f(this,"GroupNorm")}constructor(t,e,s=1e-5,r=!0){super(),this.numGroups=t,this.numChannels=e,this.eps=s,this.weight=r?new Qt($e([e])):null,this.bias=r?new Qt(Nt([e])):null}forward(t){return g0(t,this.numGroups,this.weight,this.bias,this.eps)}},_a=class extends ct{static{f(this,"BatchNorm1d")}constructor(t,e=1e-5,s=!0){super(),this.numFeatures=t,this.eps=e,this.weight=s?new Qt($e([t])):null,this.bias=s?new Qt(Nt([t])):null,this.runningMean=Nt([t]),this.runningVar=$e([t]),this.registerBuffer("runningMean",this.runningMean),this.registerBuffer("runningVar",this.runningVar)}forward(t){return _0(t,this.runningMean,this.runningVar,this.weight,this.bias,this.training,this.eps)}},Ou=class extends _a{static{f(this,"BatchNorm2d")}constructor(t,e=1e-5,s=!0){super(t,e,s)}};var Nu=class extends ct{static{f(this,"MaxPool2d")}constructor(t,e,s=0){super(),this.kernelSize=Array.isArray(t)?t:[t,t],this.stride=e?Array.isArray(e)?e:[e,e]:this.kernelSize,this.padding=s}forward(t){return x0(t,this.kernelSize,this.stride,this.padding)}},Eu=class extends ct{static{f(this,"AvgPool2d")}constructor(t,e,s=0){super(),this.kernelSize=Array.isArray(t)?t:[t,t],this.stride=e?Array.isArray(e)?e:[e,e]:this.kernelSize,this.padding=s}forward(t){return Hh(t,this.kernelSize,this.stride,this.padding)}},Tu=class extends ct{static{f(this,"AdaptiveAvgPool2d")}constructor(t){super(),this.outputSize=Array.isArray(t)?t:[t,t]}forward(t){return S0(t,this.outputSize)}};var Ms=class extends ct{static{f(this,"Dropout")}constructor(t=.5){super(),this.p=t}forward(t){return gu(t,this.p,this.training)}};var Iu=class extends ct{static{f(this,"MSELoss")}constructor(t="mean"){super(),this.reduction=t}forward(t,e){return B0(t,e,this.reduction)}},Mu=class extends ct{static{f(this,"CrossEntropyLoss")}constructor(t="mean",e=-100){super(),this.reduction=t,this.ignoreIndex=e}forward(t,e){return k0(t,e,this.reduction,this.ignoreIndex===-100?null:this.ignoreIndex)}},Cu=class extends ct{static{f(this,"NLLLoss")}constructor(t="mean",e=-100){super(),this.reduction=t,this.ignoreIndex=e}forward(t,e){return Jh(t,e,this.reduction,this.ignoreIndex===-100?null:this.ignoreIndex)}},Du=class extends ct{static{f(this,"BCELoss")}constructor(t="mean"){super(),this.reduction=t}forward(t,e){return A0(t,e,this.reduction)}};ae();var Lu=class extends ct{static{f(this,"Embedding")}constructor(t,e){super(),this.numEmbeddings=t,this.embeddingDim=e,this.weight=new Qt(ze([t,e])),hu(this.weight)}forward(t){return R0(this.weight,t)}};ae();$t();Ye();zu();Ye();$t();function ZI(n){if(Array.isArray(n[0])){let t=new Array(n[0].length);for(let e=0;e<t.length;e++)t[e]=En(n.map(s=>s[e]),0);return t}return En(n,0)}f(ZI,"stackSteps");function $u(n,t,e){let s=Array.isArray(t),r=Array.isArray(e),o=r?e:[e],i=s?t:[t],a=Zo();if(!a){let p=t,d=[],m=o[0].shape[0];for(let g=0;g<m;g++){let _=r?o.map(x=>as(x,0,g)):as(o[0],0,g),[y,b]=n(p,_);p=y,d.push(b)}return[p,ZI(d)]}let c=!1,l=f((p,d)=>{let[m,g]=n(s?p:p[0],r?d:d[0]);return c=Array.isArray(g),[s?m:[m],c?g:[g]]},"step"),[u,h]=a.scan(o,i,l);return[s?u:u[0],c?h:h[0]]}f($u,"scan");var ya=class extends ct{static{f(this,"GRUCell")}constructor(t,e,s=!0){super(),this.inputSize=t,this.hiddenSize=e,this.x2h=new Ge(t,3*e,s),this.h2h=new Ge(e,3*e,s)}forward(t,e=null){let s=e!==null?e:Nt([t.shape[0],this.hiddenSize],{device:t.device}),r=this.x2h.forward(t),o=this.h2h.forward(s),[i,a,c]=$o(r,this.hiddenSize,-1),[l,u,h]=$o(o,this.hiddenSize,-1),p=bs(ut(i,l)),d=bs(ut(a,u)),m=pn(ut(c,et(p,h)));return ut(m,et(d,Wt(s,m)))}},Fu=class extends ct{static{f(this,"GRU")}constructor(t,e,s=1,r=!1,o=!0){super(),this.inputSize=t,this.hiddenSize=e,this.numLayers=s,this.batchFirst=r,this.cells=[];for(let i=0;i<s;i++){let a=new ya(i===0?t:e,e,o);this.cells.push(a),this.registerModule("cell_"+i,a)}}forward(t,e=null){let s=this.batchFirst?t.transpose(0,1):t,r=s.shape[1],o=s,i=[];for(let c=0;c<this.numLayers;c++){let l=e!==null?as(e,0,c):Nt([r,this.hiddenSize],{device:s.device}),u=this.cells[c],[h,p]=$u((d,m)=>{let g=u.forward(m,d);return[g,g]},l,o);i.push(h),o=p}let a=o;return this.batchFirst&&(a=a.transpose(0,1)),[a,En(i,0)]}};ae();$t();Ye();zu();hn();tn();var ba=class extends ct{static{f(this,"LSTMCell")}constructor(t,e,s=!0){super(),this.inputSize=t,this.hiddenSize=e,this.x2h=new Ge(t,4*e,s),this.h2h=new Ge(e,4*e,s)}forward(t,e=null){let s=e!==null?e[0]:Nt([t.shape[0],this.hiddenSize],{device:t.device}),r=e!==null?e[1]:Nt([t.shape[0],this.hiddenSize],{device:t.device}),o=ut(this.x2h.forward(t),this.h2h.forward(s)),[i,a,c,l]=$o(o,this.hiddenSize,-1),u=bs(i),h=bs(a),p=pn(c),d=bs(l),m=ut(et(h,r),et(u,p));return[et(d,pn(m)),m]}},Vu=class extends ct{static{f(this,"LSTM")}constructor(t,e,s=1,r=!1,o=!0){super(),this.inputSize=t,this.hiddenSize=e,this.numLayers=s,this.batchFirst=r,this.cells=[];for(let i=0;i<s;i++){let a=new ba(i===0?t:e,e,o);this.cells.push(a),this.registerModule("cell_"+i,a)}}forward(t,e=null){let s=Yy();if(s&&t.device.type===pe.GPU&&!Zo()){let h=this.batchFirst?t.transpose(0,1):t,p={inputSize:this.inputSize,hiddenSize:this.hiddenSize,seqLen:h.shape[0],batch:h.shape[1]},[d,m,g]=s(h,this.cells,p,e?e[0]:null,e?e[1]:null);return[this.batchFirst?d.transpose(0,1):d,[m,g]]}let r=Jy();if(r&&t.device.type===pe.WEBGPU&&!Zo()){let h=this.batchFirst?t.transpose(0,1):t,p={inputSize:this.inputSize,hiddenSize:this.hiddenSize,seqLen:h.shape[0],batch:h.shape[1]},d=r(h,this.cells,p,e?e[0]:null,e?e[1]:null);if(d){let[m,g,_]=d;return[this.batchFirst?m.transpose(0,1):m,[g,_]]}}let o=this.batchFirst?t.transpose(0,1):t,i=o.shape[1],a=o,c=[],l=[];for(let h=0;h<this.numLayers;h++){let p=e!==null?as(e[0],0,h):Nt([i,this.hiddenSize],{device:o.device}),d=e!==null?as(e[1],0,h):Nt([i,this.hiddenSize],{device:o.device}),m=this.cells[h],[[g,_],y]=$u((b,x)=>{let[S,v]=m.forward(x,b);return[[S,v],S]},[p,d],a);c.push(g),l.push(_),a=y}let u=a;return this.batchFirst&&(u=u.transpose(0,1)),[u,[En(c,0),En(l,0)]]}};var Gu=class extends ct{static{f(this,"Sequential")}constructor(...t){super();for(let e=0;e<t.length;e++)this[String(e)]=t[e],this.registerModule(String(e),t[e]);this._length=t.length}forward(t){let e=t;for(let s=0;s<this._length;s++)e=this[String(s)].forward(e);return e}get length(){return this._length}*[Symbol.iterator](){for(let t=0;t<this._length;t++)yield this[String(t)]}push(t){let e=this._length;return this[String(e)]=t,this.registerModule(String(e),t),this._length++,this}},io=class extends ct{static{f(this,"ModuleList")}constructor(t){if(super(),this._list=[],t)for(let e=0;e<t.length;e++)this._list.push(t[e]),this.registerModule(String(e),t[e])}get length(){return this._list.length}get(t){return this._list[t]}push(t){let e=this._list.length;return this._list.push(t),this.registerModule(String(e),t),this}*[Symbol.iterator](){for(let t of this._list)yield t}forward(){throw new Error("ModuleList does not implement forward()")}},qu=class extends ct{static{f(this,"ModuleDict")}constructor(t){if(super(),this._dict=new Map,t)for(let[e,s]of Object.entries(t))this._dict.set(e,s),this.registerModule(e,s)}get(t){return this._dict.get(t)}set(t,e){return this._dict.set(t,e),this.registerModule(t,e),this}has(t){return this._dict.has(t)}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()")}};Ye();var Uu=class extends ct{static{f(this,"Flatten")}constructor(t=1,e=-1){super(),this.startDim=t,this.endDim=e}forward(t){let e=t.shape,s=e.length,r=this.startDim<0?s+this.startDim:this.startDim,o=this.endDim<0?s+this.endDim:this.endDim,i=1;for(let c=r;c<=o;c++)i*=e[c];let a=[];for(let c=0;c<r;c++)a.push(e[c]);a.push(i);for(let c=o+1;c<s;c++)a.push(e[c]);return An(t,a)}};$t();ae();ae();function Yk(n){return n==="gelu"?mu:du}f(Yk,"_getActivation");var ao=class extends ct{static{f(this,"MultiheadAttention")}constructor(t,e,s=0,r=!0,o=null,i=null,a=!0){super(),this.embedDim=t,this.numHeads=e,this.headDim=Math.floor(t/e),this.batchFirst=a,this.dropout=s,this.qProj=new Ge(t,t,r),this.kProj=new Ge(o??t,t,r),this.vProj=new Ge(i??t,t,r),this.outProj=new Ge(t,t,r)}forward(t,e,s,r=null,o=null,i=!1){this.batchFirst||(t=t.transpose(0,1),e=e.transpose(0,1),s=s.transpose(0,1));let a=t.shape[0],c=t.shape[1],l=e.shape[1],u=this.qProj.forward(t),h=this.kProj.forward(e),p=this.vProj.forward(s);if(u=u.reshape([a,c,this.numHeads,this.headDim]).permute([0,2,1,3]),h=h.reshape([a,l,this.numHeads,this.headDim]).permute([0,2,1,3]),p=p.reshape([a,l,this.numHeads,this.headDim]).permute([0,2,1,3]),o){let g=Lt(o.shape,-1/0),_=Lt(o.shape,0),y=Es(o,g,_);y=y.unsqueeze(1).unsqueeze(2),r=r?ut(r,y):y}let d=N0(u,h,p,r,this.dropout,i,this.training);d=d.permute([0,2,1,3]).reshape([a,c,this.embedDim]);let m=this.outProj.forward(d);return this.batchFirst||(m=m.transpose(0,1)),m}},Xo=class extends ct{static{f(this,"TransformerEncoderLayer")}constructor(t,e,s=2048,r=.1,o="relu",i=1e-5,a=!0,c=!1){super(),this.selfAttn=new ao(t,e,r,!0,null,null,a),this.linear1=new Ge(t,s),this.linear2=new Ge(s,t),this.norm1=new Mn(t,i),this.norm2=new Mn(t,i),this.dropout1=new Ms(r),this.dropout2=new Ms(r),this.dropoutFFN=new Ms(r),this._activation=Yk(o),this._activationName=o,this.normFirst=c,this._dModel=t,this._nhead=e,this._dimFeedforward=s,this._dropout=r,this._layerNormEps=i,this._batchFirst=a}forward(t,e=null,s=null,r=!1){return this.normFirst?this._forwardPreNorm(t,e,s,r):this._forwardPostNorm(t,e,s,r)}_forwardPostNorm(t,e,s,r){let o=this.selfAttn.forward(t,t,t,e,s,r);o=this.norm1.forward(ut(t,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(ut(o,this.dropout2.forward(i))),o}_forwardPreNorm(t,e,s,r){let o=this.norm1.forward(t),i=this.selfAttn.forward(o,o,o,e,s,r);i=ut(t,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=ut(i,this.dropout2.forward(a)),i}},Qo=class extends ct{static{f(this,"TransformerDecoderLayer")}constructor(t,e,s=2048,r=.1,o="relu",i=1e-5,a=!0,c=!1){super(),this.selfAttn=new ao(t,e,r,!0,null,null,a),this.crossAttn=new ao(t,e,r,!0,null,null,a),this.linear1=new Ge(t,s),this.linear2=new Ge(s,t),this.norm1=new Mn(t,i),this.norm2=new Mn(t,i),this.norm3=new Mn(t,i),this.dropout1=new Ms(r),this.dropout2=new Ms(r),this.dropout3=new Ms(r),this.dropoutFFN=new Ms(r),this._activation=Yk(o),this._activationName=o,this.normFirst=c,this._dModel=t,this._nhead=e,this._dimFeedforward=s,this._dropout=r,this._layerNormEps=i,this._batchFirst=a}forward(t,e,s=null,r=null,o=null,i=null,a=!1){return this.normFirst?this._forwardPreNorm(t,e,s,r,o,i,a):this._forwardPostNorm(t,e,s,r,o,i,a)}_forwardPostNorm(t,e,s,r,o,i,a){let c=this.selfAttn.forward(t,t,t,s,o,a);c=this.norm1.forward(ut(t,this.dropout1.forward(c)));let l=this.crossAttn.forward(c,e,e,r,i);c=this.norm2.forward(ut(c,this.dropout2.forward(l)));let u=this._activation(this.linear1.forward(c));return u=this.linear2.forward(this.dropoutFFN.forward(u)),c=this.norm3.forward(ut(c,this.dropout3.forward(u))),c}_forwardPreNorm(t,e,s,r,o,i,a){let c=this.norm1.forward(t),l=this.selfAttn.forward(c,c,c,s,o,a);l=ut(t,this.dropout1.forward(l));let u=this.norm2.forward(l),h=this.crossAttn.forward(u,e,e,r,i);l=ut(l,this.dropout2.forward(h));let p=this._activation(this.linear1.forward(this.norm3.forward(l)));return p=this.linear2.forward(this.dropoutFFN.forward(p)),l=ut(l,this.dropout3.forward(p)),l}};function XI(n){return new Xo(n._dModel,n._nhead,n._dimFeedforward,n._dropout,n._activationName,n._layerNormEps,n._batchFirst,n.normFirst)}f(XI,"_cloneEncoderLayer");function QI(n){return new Qo(n._dModel,n._nhead,n._dimFeedforward,n._dropout,n._activationName,n._layerNormEps,n._batchFirst,n.normFirst)}f(QI,"_cloneDecoderLayer");var wa=class extends ct{static{f(this,"TransformerEncoder")}constructor(t,e,s=null){super(),this.layers=new io(Array.from({length:e},()=>XI(t))),this.norm=s}forward(t,e=null,s=null,r=!1){let o=t;for(let i of this.layers)o=i.forward(o,e,s,r);return this.norm&&(o=this.norm.forward(o)),o}},xa=class extends ct{static{f(this,"TransformerDecoder")}constructor(t,e,s=null){super(),this.layers=new io(Array.from({length:e},()=>QI(t))),this.norm=s}forward(t,e,s=null,r=null,o=null,i=null,a=!1){let c=t;for(let l of this.layers)c=l.forward(c,e,s,r,o,i,a);return this.norm&&(c=this.norm.forward(c)),c}},Wu=class extends ct{static{f(this,"Transformer")}constructor({dModel:t=512,nhead:e=8,numEncoderLayers:s=6,numDecoderLayers:r=6,dimFeedforward:o=2048,dropout:i=.1,activation:a="relu",batchFirst:c=!0,normFirst:l=!1,layerNormEps:u=1e-5}={}){super();let h=new Xo(t,e,o,i,a,u,c,l),p=new Qo(t,e,o,i,a,u,c,l);this.encoder=new wa(h,s),this.decoder=new xa(p,r),this.dModel=t}forward(t,e,s=null,r=null,o=null,i=null,a=null,c=null){let l=this.encoder.forward(t,s,i);return this.decoder.forward(e,l,r,o,a,c)}static generateSquareSubsequentMask(t){let e=ze([t,t]),s=e._impl.storage.data;for(let r=0;r<t;r++)for(let o=0;o<t;o++)s[r*t+o]=o<=r?0:-1/0;return e}};$t();ae();var ju=class extends ct{static{f(this,"PositionalEncoding")}constructor(t,e=5e3,s=.1){super(),this.dropoutLayer=new Ms(s);let r=Nt([1,e,t]),o=r._impl.storage.data;for(let i=0;i<e;i++)for(let a=0;a<t;a+=2){let c=i*Math.exp(-(a*Math.log(1e4))/t);o[i*t+a]=Math.sin(c),a+1<t&&(o[i*t+a+1]=Math.cos(c))}this.pe=r,this.registerBuffer("pe",r)}forward(t){let e=t.shape[1],s=this.pe.narrow(1,0,e);return this.dropoutLayer.forward(ut(t,s))}};var Jk={...ma,...Wh,...b0,...Kh,...Yh,...v0,...Zh,...O0,...E0};var Xk={};Ft(Xk,{BatchSampler:()=>ni,DataLoader:()=>Yu,Dataset:()=>ti,MapDataset:()=>Hu,RandomSampler:()=>si,Sampler:()=>co,SequentialSampler:()=>ei,TensorDataset:()=>Ku,defaultCollate:()=>ri});Ye();var ti=class{static{f(this,"Dataset")}get length(){throw new Error("Subclass must implement get length()")}get(t){throw new Error("Subclass must implement get(index)")}*[Symbol.iterator](){let t=this.length;for(let e=0;e<t;e++)yield this.get(e)}},Ku=class extends ti{static{f(this,"TensorDataset")}constructor(...t){if(super(),t.length===0)throw new Error("TensorDataset requires at least one tensor");let e=t[0].shape[0];for(let s=1;s<t.length;s++)if(t[s].shape[0]!==e)throw new Error(`Size mismatch at dim 0: tensor 0 has ${e}, tensor ${s} has ${t[s].shape[0]}`);this._tensors=t,this._length=e}get length(){return this._length}get(t){let e=new Array(this._tensors.length);for(let s=0;s<this._tensors.length;s++)e[s]=as(this._tensors[s],0,t);return e}},Hu=class extends ti{static{f(this,"MapDataset")}constructor(t,e){super(),this._dataset=t,this._transform=e}get length(){return this._dataset.length}get(t){return this._transform(this._dataset.get(t))}};var co=class{static{f(this,"Sampler")}*[Symbol.iterator](){throw new Error("Subclass must implement [Symbol.iterator]()")}},ei=class extends co{static{f(this,"SequentialSampler")}constructor(t){super(),this._dataSource=t}*[Symbol.iterator](){let t=this._dataSource.length;for(let e=0;e<t;e++)yield e}},si=class extends co{static{f(this,"RandomSampler")}constructor(t){super(),this._dataSource=t}*[Symbol.iterator](){let t=this._dataSource.length,e=new Int32Array(t);for(let s=0;s<t;s++)e[s]=s;for(let s=t-1;s>0;s--){let r=Math.floor(Math.random()*(s+1)),o=e[s];e[s]=e[r],e[r]=o}for(let s=0;s<t;s++)yield e[s]}},ni=class extends co{static{f(this,"BatchSampler")}constructor(t,e,s=!1){super(),this._sampler=t,this._batchSize=e,this._dropLast=s}*[Symbol.iterator](){let t=[];for(let e of this._sampler)t.push(e),t.length===this._batchSize&&(yield t,t=[]);t.length>0&&!this._dropLast&&(yield t)}};Ps();Qr();Qs();function tM(n,t=0){let e=n.length,s=n[0].shape,r=n[0].dtype,o=[...s];o.splice(t,0,e);let i=s.reduce((l,u)=>l*u,1),a=Jt(r),c=new a(e*i);for(let l=0;l<e;l++){let u=n[l],h=u.data,p=u._impl?u._impl.storageOffset:0;if(u.isContiguous)c.set(h.subarray(p,p+i),l*i);else{let d=u.shape,m=u.strides,g=u._impl.storage.data,_=d.length,y=new Int32Array(_),b=p;for(let x=0;x<i;x++){c[l*i+x]=g[b];for(let S=_-1;S>=0;S--){if(y[S]++,y[S]<d[S]){b+=m[S];break}b-=(d[S]-1)*m[S],y[S]=0}}}}return Fo(c,o,r)}f(tM,"stackTensors");function ri(n){let t=n[0];if(t instanceof zt)return tM(n,0);if(typeof t=="number")return tr(n);if(Array.isArray(t)){let e=new Array(t.length);for(let s=0;s<t.length;s++){let r=new Array(n.length);for(let o=0;o<n.length;o++)r[o]=n[o][s];e[s]=ri(r)}return e}if(t!==null&&typeof t=="object"&&t.constructor===Object){let e=Object.keys(t),s={};for(let r=0;r<e.length;r++){let o=e[r],i=new Array(n.length);for(let a=0;a<n.length;a++)i[a]=n[a][o];s[o]=ri(i)}return s}throw new Error(`defaultCollate: unsupported element type ${typeof t}`)}f(ri,"defaultCollate");var Yu=class{static{f(this,"DataLoader")}constructor(t,e={}){if(this._dataset=t,this._collate=e.collate??ri,e.batchSampler!=null){if(e.batchSize!=null||e.shuffle||e.sampler!=null||e.dropLast)throw new Error("batchSampler is mutually exclusive with batchSize, shuffle, sampler, and dropLast");this._batchSampler=e.batchSampler,this._batchSize=null}else{let s=e.batchSize??1,r=e.dropLast??!1;this._batchSize=s;let o;if(e.sampler!=null){if(e.shuffle)throw new Error("sampler and shuffle are mutually exclusive");o=e.sampler}else o=e.shuffle?new si(t):new ei(t);this._batchSampler=new ni(o,s,r)}}get dataset(){return this._dataset}get length(){let t=this._dataset.length;if(this._batchSize==null){let e=0;for(let s of this._batchSampler)e++;return e}return this._batchSampler._dropLast?Math.floor(t/this._batchSize):Math.ceil(t/this._batchSize)}*[Symbol.iterator](){for(let t of this._batchSampler){let e=new Array(t.length);for(let s=0;s<t.length;s++)e[s]=this._dataset.get(t[s]);yield this._collate(e)}}};var rA={};Ft(rA,{DEFAULT_SPECIALS:()=>ad,TOKENIZER_FORMAT:()=>va,TOKENIZER_VERSION:()=>Ba,Tokenizer:()=>Xu,Vocab:()=>uo});var uo=class n{static{f(this,"Vocab")}constructor(t=[]){this._tokenToId=new Map,this._idToToken=[];for(let e of t)this.add(e)}add(t){let e=this._tokenToId.get(t);return e===void 0&&(e=this._idToToken.length,this._tokenToId.set(t,e),this._idToToken.push(t)),e}getId(t,e=-1){let s=this._tokenToId.get(t);return s===void 0?e:s}getToken(t){return t>=0&&t<this._idToToken.length?this._idToToken[t]:void 0}has(t){return this._tokenToId.has(t)}get size(){return this._idToToken.length}tokens(){return this._idToToken.slice()}static fromTokens(t){if(!Array.isArray(t))throw new Error("mlfw tokenizer: vocab must be an array");let e=new n,s=new Set;for(let r of t){if(typeof r!="string")throw new Error("mlfw tokenizer: vocab entries must be strings");if(s.has(r))throw new Error(`mlfw tokenizer: duplicate vocab token '${r}'`);s.add(r),e.add(r)}return e}};var sd=class n{static{f(this,"WordStrategy")}constructor({lowercase:t=!1}={}){this._lowercase=t}fit(){}segment(t){return(this._lowercase?String(t).toLowerCase():String(t)).split(/\s+/).filter(Boolean)}detokenize(t){return t.join(" ")}toJSON(){return{lowercase:this._lowercase}}static fromJSON(t={}){return new n({lowercase:t.lowercase??!1})}};var nd=class n{static{f(this,"CharStrategy")}constructor({lowercase:t=!1}={}){this._lowercase=t}fit(){}segment(t){let e=this._lowercase?String(t).toLowerCase():String(t);return Array.from(e)}detokenize(t){return t.join("")}toJSON(){return{lowercase:this._lowercase}}static fromJSON(t={}){return new n({lowercase:t.lowercase??!1})}};function rd(n,t){return n+""+t}f(rd,"pairKey");var od=class n{static{f(this,"BpeStrategy")}constructor({numMerges:t=1e3,lowercase:e=!1,endOfWord:s="</w>"}={}){this._numMerges=t,this._lowercase=e,this._eow=s,this._ranks=new Map,this._encodeCache=new Map}_pretokenize(t){return(this._lowercase?String(t).toLowerCase():String(t)).split(/\s+/).filter(Boolean)}_baseSymbols(t){let e=Array.from(t);return e.push(this._eow),e}fit(t){this._encodeCache=new Map;let e=new Map;for(let p of t)for(let d of this._pretokenize(p))e.set(d,(e.get(d)||0)+1);let s=new Map;for(let p of e.keys())s.set(p,this._baseSymbols(p));let r=new Map,o=new Map,i=[],a=f((p,d)=>p[0]>d[0]||p[0]===d[0]&&p[1]<d[1],"higher"),c=f((p,d)=>{i.push([p,d]);let m=i.length-1;for(;m>0;){let g=m-1>>1;if(a(i[m],i[g])){let _=i[g];i[g]=i[m],i[m]=_,m=g}else break}},"pushHeap"),l=f(()=>{let p=i[0],d=i.pop();if(i.length>0){i[0]=d;let m=0,g=i.length;for(;;){let _=m,y=2*m+1,b=2*m+2;if(y<g&&a(i[y],i[_])&&(_=y),b<g&&a(i[b],i[_])&&(_=b),_===m)break;let x=i[_];i[_]=i[m],i[m]=x,m=_}}return p},"popHeap"),u=f(p=>{let d=s.get(p),m=e.get(p);for(let g=0;g+1<d.length;g++){let _=rd(d[g],d[g+1]),y=(r.get(_)||0)+m;r.set(_,y),c(y,_);let b=o.get(_);b||(b=new Set,o.set(_,b)),b.add(p)}},"addWordPairs"),h=f(p=>{let d=s.get(p),m=e.get(p);for(let g=0;g+1<d.length;g++){let _=rd(d[g],d[g+1]),y=(r.get(_)||0)-m;y<=0?r.delete(_):(r.set(_,y),c(y,_));let b=o.get(_);b&&b.delete(p)}},"removeWordPairs");for(let p of e.keys())u(p);this._ranks=new Map;for(let p=0;p<this._numMerges;p++){let d=null,m=0;for(;i.length>0;){let[S,v]=l(),w=r.get(v);if(w===S&&w>0){d=v,m=w;break}}if(d===null||m<=0)break;let g=d.indexOf(""),_=d.slice(0,g),y=d.slice(g+1),b=_+y;this._ranks.set(d,p);let x=o.get(d);if(!x||x.size===0)break;for(let S of[...x]){h(S);let v=s.get(S),w=[];for(let A=0;A<v.length;A++)A+1<v.length&&v[A]===_&&v[A+1]===y?(w.push(b),A++):w.push(v[A]);s.set(S,w),u(S)}}}_encodeWord(t){let e=this._encodeCache.get(t);if(e!==void 0)return e;let s=this._baseSymbols(t);for(;s.length>1;){let r=1/0,o=-1;for(let i=0;i+1<s.length;i++){let a=this._ranks.get(rd(s[i],s[i+1]));a!==void 0&&a<r&&(r=a,o=i)}if(o<0)break;s=s.slice(0,o).concat(s[o]+s[o+1],s.slice(o+2))}return this._encodeCache.set(t,s),s}segment(t){let e=[];for(let s of this._pretokenize(t))for(let r of this._encodeWord(s))e.push(r);return e}detokenize(t){let e="";for(let s of t)e+=s;return e.split(this._eow).join(" ").trim()}toJSON(){let t=[...this._ranks.entries()].map(([e])=>{let s=e.indexOf("");return[e.slice(0,s),e.slice(s+1)]});return{numMerges:this._numMerges,lowercase:this._lowercase,endOfWord:this._eow,merges:t}}static fromJSON(t={}){if(!Array.isArray(t.merges))throw new Error("mlfw tokenizer: bpe strategy merges must be an array");let e=new n({numMerges:t.numMerges??t.merges.length,lowercase:t.lowercase??!1,endOfWord:t.endOfWord??"</w>"});e._ranks=new Map;for(let s=0;s<t.merges.length;s++){let r=t.merges[s];if(!Array.isArray(r)||r.length!==2||r.some(o=>typeof o!="string"))throw new Error("mlfw tokenizer: bpe merges must be string pairs");e._ranks.set(rd(r[0],r[1]),s)}return e}};Qr();import{writeFileSync as Qk,appendFileSync as eM,readFileSync as tA,existsSync as sM,mkdirSync as nM,readdirSync as rM,unlinkSync as oM,renameSync as iM}from"node:fs";import{resolve as Ju}from"node:path";var ts={readFile(n){return tA(Ju(n),"utf8")},readBinary(n){return tA(Ju(n))},writeFile(n,t){Qk(n,t)},writeBinary(n,t){Qk(Ju(n),t)},appendFile(n,t){eM(n,t)},exists(n){return sM(n)},mkdir(n){nM(n,{recursive:!0})},readdir(n){return rM(n)},remove(n){oM(n)},rename(n,t){iM(Ju(n),Ju(t))}};var va="mlfw-tokenizer",Ba=1,ad=Object.freeze({pad:"<pad>",unk:"<unk>",bos:"<bos>",eos:"<eos>"}),eA=`${va}-v${Ba}`,id={word:sd,char:nd,bpe:od},Zu=Object.freeze(["pad","unk","bos","eos"]),Xu=class n{static{f(this,"Tokenizer")}constructor(t={}){let e=t.mode??"word",s=id[e];if(!s)throw new Error(`Unknown tokenizer mode '${e}'. Available: ${Object.keys(id).join(", ")}`);this._mode=e,this._specials=T0(t.specialTokens),sA(this._specials),this._maxVocab=t.vocabSize??null,nA(this._maxVocab,this._specials),this._strategy=new s(t),this._vocab=null}get mode(){return this._mode}get vocabSize(){return this._ensureFit(),this._vocab.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")}_specialId(t){return this._ensureFit(),this._vocab.getId(this._specials[t])}fit(t){let e=Array.isArray(t)?t:[t];this._strategy.fit(e,{vocabSize:this._maxVocab});let s=new uo(Object.values(this._specials));if(this._maxVocab==null)for(let r of e)for(let o of this._strategy.segment(r))s.add(o);else{let r=new Map;for(let a of e)for(let c of this._strategy.segment(a))r.set(c,(r.get(c)||0)+1);let o=[...r.entries()].sort((a,c)=>c[1]-a[1]),i=this._maxVocab-s.size;for(let a=0;a<o.length&&a<i;a++)s.add(o[a][0])}return this._vocab=s,this}toJSON(){this._ensureFit();let t=this._strategy.toJSON();return{format:va,version:Ba,mode:this._mode,config:{vocabSize:this._maxVocab,...t},specialTokens:{...this._specials},vocab:this._vocab.tokens(),strategy:t}}save(t){if(typeof t!="string")throw new Error("mlfw tokenizer: save(path) requires a file path string");let e=t+".tmp";ts.writeFile(e,cM(this.toJSON())),ts.rename(e,t)}static load(t){if(typeof t!="string")throw new Error("mlfw tokenizer: load(path) requires a file path string");let e=ts.readFile(t),s=typeof e=="string"?e:new TextDecoder().decode(e);return n.fromJSON(uM(s))}static fromJSON(t){aM(t);let e=id[t.mode],s=new n({mode:t.mode,vocabSize:t.config.vocabSize,specialTokens:t.specialTokens});s._strategy=e.fromJSON(t.strategy),s._vocab=uo.fromTokens(t.vocab);for(let r of Object.values(s._specials))if(!s._vocab.has(r))throw new Error(`mlfw tokenizer: special token '${r}' is missing from vocab`);return s}encode(t,e={}){this._ensureFit();let s=[];e.addBos&&s.push(this.bosId);let r=this.unkId;for(let o of this._strategy.segment(t))s.push(this._vocab.getId(o,r));return e.addEos&&s.push(this.eosId),s}decode(t,e={}){this._ensureFit();let s=e.skipSpecial??!0,r=new Set(Object.values(this._specials).map(i=>this._vocab.getId(i))),o=[];for(let i of t){if(s&&r.has(i))continue;let a=this._vocab.getToken(i);a!==void 0&&o.push(a)}return this._strategy.detokenize(o)}encodeBatch(t,e={}){this._ensureFit();let r=(Array.isArray(t)?t:[t]).map(l=>this.encode(l,e)),o=e.maxLen??r.reduce((l,u)=>Math.max(l,u.length),0),i=e.padId??this.padId,a=r.length,c=new Int32Array(a*o).fill(i);for(let l=0;l<a;l++){let u=r[l],h=Math.min(u.length,o);for(let p=0;p<h;p++)c[l*o+p]=u[p]}return tr(c,{shape:[a,o],dtype:"i32"})}};function T0(n){if(Array.isArray(n)){let t={...ad};for(let e=0;e<n.length&&e<Zu.length;e++)t[Zu[e]]=n[e];return t}return{...ad,...n??{}}}f(T0,"normalizeSpecials");function sA(n){let t=new Set;for(let e of Zu){let s=n[e];if(typeof s!="string"||s.length===0)throw new Error(`mlfw tokenizer: special token '${e}' must be a non-empty string`);if(t.has(s))throw new Error(`mlfw tokenizer: duplicate special token '${s}'`);t.add(s)}}f(sA,"validateSpecials");function nA(n,t){if(n!=null&&(!Number.isInteger(n)||n<Object.keys(t).length))throw new Error(`mlfw tokenizer: vocabSize must be an integer >= ${Object.keys(t).length}`)}f(nA,"validateVocabSize");function aM(n){if(!n||typeof n!="object")throw new Error("mlfw tokenizer: artifact must be an object");if(n.format!==va)throw new Error("mlfw tokenizer: unrecognized tokenizer format");if(n.version!==Ba)throw new Error(`mlfw tokenizer: unsupported tokenizer version ${n.version}`);if(!id[n.mode])throw new Error(`mlfw tokenizer: unknown tokenizer mode '${n.mode}'`);if(!n.config||typeof n.config!="object")throw new Error("mlfw tokenizer: config must be an object");if(sA(T0(n.specialTokens)),nA(n.config.vocabSize,T0(n.specialTokens)),!Array.isArray(n.vocab))throw new Error("mlfw tokenizer: vocab must be an array");if(!n.strategy||typeof n.strategy!="object")throw new Error("mlfw tokenizer: strategy must be an object")}f(aM,"validateTokenizerData");function cM(n){let t=[eA,oi("m",[n.mode]),oi("z",[n.config.vocabSize==null?"":String(n.config.vocabSize)]),oi("s",Zu.map(e=>n.specialTokens[e]))];if(n.mode==="bpe"){let e=[];for(let s of n.strategy.merges)e.push(s[0],s[1]);t.push(oi("g",[String(n.strategy.lowercase?1:0),String(n.strategy.numMerges),n.strategy.endOfWord])),t.push(oi("r",e))}else t.push(oi("g",[String(n.strategy.lowercase?1:0)]));return t.push(oi("v",n.vocab)),t.join(`
|
|
202
|
-
`)}f(cM,"serializeTokenizer");function uM(n){if(!n.startsWith(eA))throw new Error("mlfw tokenizer: unrecognized tokenizer format");let t=new Map;for(let l of n.split(/\r?\n/).slice(1)){if(!l)continue;let u=l.indexOf(" ");if(u<0)throw new Error("mlfw tokenizer: malformed tokenizer artifact");t.set(l.slice(0,u),lM(l.slice(u+1)))}let e=Sa(t,"m")[0],s=Sa(t,"z")[0],r=Sa(t,"s"),o=Sa(t,"g"),i=Sa(t,"v"),a=Object.fromEntries(Zu.map((l,u)=>[l,r[u]])),c={lowercase:o[0]==="1"};if(e==="bpe"){let l=Sa(t,"r");if(l.length%2!==0)throw new Error("mlfw tokenizer: bpe merges must be string pairs");c.numMerges=Number(o[1]),c.endOfWord=o[2],c.merges=[];for(let u=0;u<l.length;u+=2)c.merges.push([l[u],l[u+1]])}return{format:va,version:Ba,mode:e,config:{vocabSize:s===""?null:Number(s),...c},specialTokens:a,vocab:i,strategy:c}}f(uM,"parseTokenizer");function oi(n,t){return`${n} ${t.map(fM).join(" ")}`}f(oi,"record");function lM(n){return n.split(" ").map(pM)}f(lM,"parseRecord");function fM(n){return String(n).replace(/\\/g,"\\\\").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r")}f(fM,"escapeField");function pM(n){let t="";for(let e=0;e<n.length;e++){let s=n[e];if(s!=="\\"){t+=s;continue}let r=n[++e];r==="t"?t+=" ":r==="n"?t+=`
|
|
203
|
-
`:r==="r"?t+="\r":r==="\\"?t+="\\":t+=r??""}return t}f(pM,"unescapeField");function Sa(n,t){let e=n.get(t);if(!e)throw new Error(`mlfw tokenizer: missing '${t}' record`);return e}f(Sa,"requiredField");var uR={};Ft(uR,{Adam:()=>tl,AdamW:()=>el,CosineAnnealingLR:()=>nl,FusedAdam:()=>gm,FusedOptimizer:()=>Ga,FusedSGD:()=>mm,GradScaler:()=>_m,LRScheduler:()=>ii,Optimizer:()=>nn,ReduceLROnPlateau:()=>rl,SGD:()=>Qu,StepLR:()=>sl,clipGradNorm_:()=>ol,clipGradValue_:()=>il});var nn=class{static{f(this,"Optimizer")}constructor(t,e){this._defaults={...e},this._paramGroups=[],this._state=new Map,this._paramIndex=new WeakMap,this._nextId=0;let s=hM(t);for(let r of s)this._addParamGroup(r)}get paramGroups(){return this._paramGroups}get defaults(){return this._defaults}step(){throw new Error(`${this.constructor.name}.step() not implemented`)}zeroGrad(t=!0){for(let e of this._paramGroups)for(let s of e.params)s.grad!==null&&(t?s.grad=null:s.grad._impl.storage.data.fill(0))}stateDict(){let t=new Map;for(let[e,s]of this._state){let r={};for(let o of Object.keys(s)){let i=s[o];r[o]=oA(i)?new i.constructor(i):i}t.set(e,r)}return{state:t,paramGroups:this._paramGroups.map(e=>{let s={};for(let r of Object.keys(e)){if(r==="params")continue;let o=e[r];s[r]=Array.isArray(o)?[...o]:o}return s})}}loadStateDict(t){for(let e=0;e<this._paramGroups.length;e++){let s=t.paramGroups[e];for(let r of Object.keys(s))this._paramGroups[e][r]=Array.isArray(s[r])?[...s[r]]:s[r]}this._state=new Map;for(let[e,s]of t.state){let r={};for(let o of Object.keys(s)){let i=s[o];r[o]=oA(i)?new i.constructor(i):i}this._state.set(e,r)}}_addParamGroup(t){let e={...this._defaults};for(let r of Object.keys(t))r!=="params"&&(e[r]=t[r]);let s=Array.isArray(t.params)?t.params:[...t.params];for(let r of s){if(this._paramIndex.has(r))throw new Error("Parameter appears in more than one parameter group");this._paramIndex.set(r,this._nextId++)}e.params=s,this._paramGroups.push(e)}_getParamId(t){return this._paramIndex.get(t)}_getState(t){let e=this._paramIndex.get(t),s=this._state.get(e);return s||(s={},this._state.set(e,s)),s}};function hM(n){if(n==null)throw new Error("Optimizer requires at least one parameter");let t=Array.isArray(n)?n:[...n];if(t.length===0)throw new Error("Optimizer got an empty parameter list");return t[0]&&typeof t[0]=="object"&&"params"in t[0]?t:[{params:t}]}f(hM,"normalizeParamGroups");function oA(n){return n instanceof Float32Array||n instanceof Float64Array||n instanceof Int32Array||n instanceof Int16Array||n instanceof Int8Array||n instanceof Uint8Array||n instanceof Uint16Array||n instanceof Uint32Array}f(oA,"isTypedArray");var Qu=class extends nn{static{f(this,"SGD")}constructor(t,{lr:e=.01,momentum:s=0,dampening:r=0,weightDecay:o=0,nesterov:i=!1}={}){if(i&&(s===0||r!==0))throw new Error("Nesterov momentum requires momentum > 0 and dampening = 0");super(t,{lr:e,momentum:s,dampening:r,weightDecay:o,nesterov:i})}step(){for(let t of this._paramGroups){let{lr:e,momentum:s,dampening:r,weightDecay:o,nesterov:i}=t;for(let a of t.params){if(a.grad===null)continue;let c=a._impl.storage.data,l=a.grad._impl.storage.data,u=c.length,h=this._getState(a);if(s===0)if(o===0)for(let p=0;p<u;p++)c[p]-=e*l[p];else for(let p=0;p<u;p++)c[p]-=e*(l[p]+o*c[p]);else{let p=h.momentumBuffer;if(p)for(let d=0;d<u;d++){let m=o!==0?l[d]+o*c[d]:l[d];p[d]=s*p[d]+(1-r)*m}else{p=new c.constructor(u);for(let d=0;d<u;d++)p[d]=o!==0?l[d]+o*c[d]:l[d];h.momentumBuffer=p}if(i)for(let d=0;d<u;d++){let m=o!==0?l[d]+o*c[d]:l[d];c[d]-=e*(m+s*p[d])}else for(let d=0;d<u;d++)c[d]-=e*p[d]}a._impl.bumpVersion()}}}};hn();tn();var tl=class extends nn{static{f(this,"Adam")}constructor(t,{lr:e=.001,betas:s=[.9,.999],eps:r=1e-8,weightDecay:o=0,amsgrad:i=!1}={}){super(t,{lr:e,betas:[...s],eps:r,weightDecay:o,amsgrad:i})}step(){let t=Zy();for(let e of this._paramGroups){let{lr:s,betas:r,eps:o,weightDecay:i,amsgrad:a}=e,[c,l]=r;for(let u of e.params){if(u.grad===null)continue;let h=this._getState(u);if(t&&!a&&u.device&&u.device.type===pe.GPU){h.step=(h.step||0)+1;let w=h.step,A=1-Math.pow(c,w),I=1-Math.pow(l,w);if(t(u,h,{beta1:c,beta2:l,omb1:1-c,omb2:1-l,eps:o,stepSize:s/A,bc2sqrt:Math.sqrt(I),wd:i,lr:s})){u._impl.bumpVersion();continue}}let p=u._impl.storage.data,d=u.grad._impl.storage.data,m=p.length;h.expAvg===void 0&&(h.step===void 0&&(h.step=0),h.expAvg=new p.constructor(m),h.expAvgSq=new p.constructor(m),a&&(h.maxExpAvgSq=new p.constructor(m))),h.step++;let g=h.step,_=h.expAvg,y=h.expAvgSq,b=1-Math.pow(c,g),x=1-Math.pow(l,g),S=s/b,v=Math.sqrt(x);if(a){let w=h.maxExpAvgSq;for(let A=0;A<m;A++){let I=i!==0?d[A]+i*p[A]:d[A];_[A]=c*_[A]+(1-c)*I,y[A]=l*y[A]+(1-l)*I*I,y[A]>w[A]&&(w[A]=y[A]),p[A]-=S*_[A]/(Math.sqrt(w[A])/v+o)}}else for(let w=0;w<m;w++){let A=i!==0?d[w]+i*p[w]:d[w];_[w]=c*_[w]+(1-c)*A,y[w]=l*y[w]+(1-l)*A*A,p[w]-=S*_[w]/(Math.sqrt(y[w])/v+o)}u._impl.bumpVersion()}}}};var el=class extends nn{static{f(this,"AdamW")}constructor(t,{lr:e=.001,betas:s=[.9,.999],eps:r=1e-8,weightDecay:o=.01,amsgrad:i=!1}={}){super(t,{lr:e,betas:[...s],eps:r,weightDecay:o,amsgrad:i})}step(){for(let t of this._paramGroups){let{lr:e,betas:s,eps:r,weightDecay:o,amsgrad:i}=t,[a,c]=s;for(let l of t.params){if(l.grad===null)continue;let u=l._impl.storage.data,h=l.grad._impl.storage.data,p=u.length,d=this._getState(l);d.step===void 0&&(d.step=0,d.expAvg=new u.constructor(p),d.expAvgSq=new u.constructor(p),i&&(d.maxExpAvgSq=new u.constructor(p))),d.step++;let m=d.step,g=d.expAvg,_=d.expAvgSq,y=1-Math.pow(a,m),b=1-Math.pow(c,m),x=e/y,S=Math.sqrt(b);if(o!==0){let v=1-e*o;for(let w=0;w<p;w++)u[w]*=v}if(i){let v=d.maxExpAvgSq;for(let w=0;w<p;w++)g[w]=a*g[w]+(1-a)*h[w],_[w]=c*_[w]+(1-c)*h[w]*h[w],_[w]>v[w]&&(v[w]=_[w]),u[w]-=x*g[w]/(Math.sqrt(v[w])/S+r)}else for(let v=0;v<p;v++)g[v]=a*g[v]+(1-a)*h[v],_[v]=c*_[v]+(1-c)*h[v]*h[v],u[v]-=x*g[v]/(Math.sqrt(_[v])/S+r);l._impl.bumpVersion()}}}};var ii=class{static{f(this,"LRScheduler")}constructor(t,e=-1){this._optimizer=t,this._baseLRs=t.paramGroups.map(s=>s.lr),this._lastEpoch=e,this._lastLR=null}_init(){this.step()}getLR(){throw new Error(`${this.constructor.name}.getLR() not implemented`)}getLastLR(){return this._lastLR}step(){this._lastEpoch++;let t=this.getLR();this._lastLR=t;let e=this._optimizer.paramGroups;for(let s=0;s<e.length;s++)e[s].lr=t[s]}},sl=class extends ii{static{f(this,"StepLR")}constructor(t,e,s=.1,r=-1){super(t,r),this._stepSize=e,this._gamma=s,this._init()}getLR(){let t=Math.pow(this._gamma,Math.floor(this._lastEpoch/this._stepSize));return this._baseLRs.map(e=>e*t)}},nl=class extends ii{static{f(this,"CosineAnnealingLR")}constructor(t,e,s=0,r=-1){super(t,r),this._tMax=e,this._etaMin=s,this._init()}getLR(){let t=(1+Math.cos(Math.PI*this._lastEpoch/this._tMax))/2;return this._baseLRs.map(e=>this._etaMin+(e-this._etaMin)*t)}},rl=class{static{f(this,"ReduceLROnPlateau")}constructor(t,{mode:e="min",factor:s=.1,patience:r=10,threshold:o=1e-4,thresholdMode:i="rel",cooldown:a=0,minLR:c=0,eps:l=1e-8}={}){this._optimizer=t,this._mode=e,this._factor=s,this._patience=r,this._threshold=o,this._thresholdMode=i,this._cooldown=a,this._minLR=c,this._eps=l,this._best=e==="min"?1/0:-1/0,this._numBadEpochs=0,this._cooldownCounter=0}step(t){if(t===void 0)throw new Error("ReduceLROnPlateau.step() requires a metric value");this._cooldownCounter>0&&(this._cooldownCounter--,this._numBadEpochs=0),this._isBetter(t)?(this._best=t,this._numBadEpochs=0):this._numBadEpochs++,this._numBadEpochs>this._patience&&(this._reduceAllLRs(),this._cooldownCounter=this._cooldown,this._numBadEpochs=0)}_isBetter(t){return this._mode==="min"?this._thresholdMode==="rel"?t<this._best*(1-this._threshold):t<this._best-this._threshold:this._thresholdMode==="rel"?t>this._best*(1+this._threshold):t>this._best+this._threshold}_reduceAllLRs(){for(let t of this._optimizer.paramGroups){let e=Math.max(t.lr*this._factor,this._minLR);t.lr-e>this._eps&&(t.lr=e)}}};$t();function iA(n){return(Array.isArray(n)?n:[...n]).filter(e=>e.grad!==null)}f(iA,"_gradParams");function ol(n,t,e=2){let s=iA(n);if(s.length===0)return 0;let r;if(e===2){let i=null;for(let a of s){let c=ie(et(a.grad,a.grad));i=i===null?c:ut(i,c)}r=Math.sqrt(i.item())}else if(e===1/0){let i=null;for(let a of s){let c=Er(Ji(a.grad));i=i===null?c:Rr(i,c)}r=i.item()}else{let i=null;for(let a of s){let c=ie(to(Ji(a.grad),e));i=i===null?c:ut(i,c)}r=Math.pow(i.item(),1/e)}let o=t/(r+1e-6);if(o<1)for(let i of s)i.grad=et(i.grad,o);return r}f(ol,"clipGradNorm_");function il(n,t){for(let e of iA(n))e.grad=N_(e.grad,-t,t)}f(il,"clipGradValue_");zn();kt();dm();qn();var Dn=J.F32,ge=f(n=>new E([n],Dn),"VEC"),Va=new E([],Dn);function _n(n,t,e){return n.broadcast(t,[e],[]).getResult(0)}f(_n,"bcast");var Ga=class extends nn{static{f(this,"FusedOptimizer")}constructor(t,e,s=null){super(t,e),this._target=s||Gn(),this._kernels=new Map}_kernel(t){let e=this._kernels.get(t);if(!e){let s=this._buildGraph(t);e=cR(s,this._target,{fusion:{enabled:!0}}),this._kernels.set(t,e)}return e}_buildGraph(t){throw new Error(`${this.constructor.name}._buildGraph() not implemented`)}},mm=class extends Ga{static{f(this,"FusedSGD")}constructor(t,{lr:e=.01,momentum:s=0,dampening:r=0,weightDecay:o=0,nesterov:i=!1}={},a=null){if(i&&(s===0||r!==0))throw new Error("Nesterov momentum requires momentum > 0 and dampening = 0");super(t,{lr:e,momentum:s,dampening:r,weightDecay:o,nesterov:i},a)}_buildGraph(t){let{momentum:e,dampening:s,weightDecay:r,nesterov:o}=this._defaults,i=e!==0,a=i?[ge(t),ge(t),ge(t),Va]:[ge(t),ge(t),Va],c=i?[ge(t),ge(t)]:[ge(t)];return pc("sgd_update",a,c,(u,h)=>{let p=h[0],d=h[1],m=i?h[3]:h[2],g=_n(u,m,t),_=d;if(r!==0){let A=u.scalarConstant(r,Dn).getResult(0);_=u.add(d,u.mul(_n(u,A,t),p).getResult(0)).getResult(0)}if(!i){let A=u.sub(p,u.mul(g,_).getResult(0)).getResult(0);u.returnOp([A]);return}let y=h[2],b=_n(u,u.scalarConstant(e,Dn).getResult(0),t),x=_n(u,u.scalarConstant(1-s,Dn).getResult(0),t),S=u.add(u.mul(b,y).getResult(0),u.mul(x,_).getResult(0)).getResult(0),v=S;o&&(v=u.add(_,u.mul(b,S).getResult(0)).getResult(0));let w=u.sub(p,u.mul(g,v).getResult(0)).getResult(0);u.returnOp([w,S])})}step(){for(let t of this._paramGroups){let{lr:e,momentum:s}=t,r=s!==0,o=new Float32Array([e]);for(let i of t.params){if(i.grad===null)continue;let a=i._impl.storage.data,c=i.grad._impl.storage.data,l=a.length,u=this._kernel(l);if(r){let h=this._getState(i),p=h.momentumBuffer;p||(p=new a.constructor(l),h.momentumBuffer=p),u.run("sgd_update",a,c,p,o,a,p)}else u.run("sgd_update",a,c,o,a);i._impl.bumpVersion()}}}},gm=class extends Ga{static{f(this,"FusedAdam")}constructor(t,{lr:e=.001,betas:s=[.9,.999],eps:r=1e-8,weightDecay:o=0,amsgrad:i=!1}={},a=null){super(t,{lr:e,betas:[...s],eps:r,weightDecay:o,amsgrad:i},a)}_buildGraph(t){let{betas:e,eps:s,weightDecay:r,amsgrad:o}=this._defaults,[i,a]=e,c=o?[ge(t),ge(t),ge(t),ge(t),ge(t),Va,Va]:[ge(t),ge(t),ge(t),ge(t),Va,Va],l=o?[ge(t),ge(t),ge(t),ge(t)]:[ge(t),ge(t),ge(t)];return pc("adam_update",c,l,(u,h)=>{let p=h[0],d=h[1],m=h[2],g=h[3],_=o?h[4]:null,y=o?h[5]:h[4],b=o?h[6]:h[5],x=d;if(r!==0){let G=_n(u,u.scalarConstant(r,Dn).getResult(0),t);x=u.add(d,u.mul(G,p).getResult(0)).getResult(0)}let S=_n(u,u.scalarConstant(i,Dn).getResult(0),t),v=_n(u,u.scalarConstant(a,Dn).getResult(0),t),w=_n(u,u.scalarConstant(1-i,Dn).getResult(0),t),A=_n(u,u.scalarConstant(1-a,Dn).getResult(0),t),I=_n(u,u.scalarConstant(s,Dn).getResult(0),t),B=u.add(u.mul(S,m).getResult(0),u.mul(w,x).getResult(0)).getResult(0),k=u.mul(x,x).getResult(0),N=u.add(u.mul(v,g).getResult(0),u.mul(A,k).getResult(0)).getResult(0),R=N,O=null;o&&(O=u.maximum(_,N).getResult(0),R=O);let M=u.add(u.div(u.sqrt(R).getResult(0),_n(u,b,t)).getResult(0),I).getResult(0),F=u.mul(_n(u,y,t),u.div(B,M).getResult(0)).getResult(0),P=u.sub(p,F).getResult(0);u.returnOp(o?[P,B,N,O]:[P,B,N])})}step(){for(let t of this._paramGroups){let{lr:e,betas:s,eps:r,weightDecay:o,amsgrad:i}=t,[a,c]=s;for(let l of t.params){if(l.grad===null)continue;let u=l._impl.storage.data,h=l.grad._impl.storage.data,p=u.length,d=this._getState(l);d.step===void 0&&(d.step=0,d.expAvg=new u.constructor(p),d.expAvgSq=new u.constructor(p),i&&(d.maxExpAvgSq=new u.constructor(p))),d.step++;let m=d.step,g=1-Math.pow(a,m),_=1-Math.pow(c,m),y=new Float32Array([e/g]),b=new Float32Array([Math.sqrt(_)]),x=this._kernel(p),S=d.expAvg,v=d.expAvgSq;if(i){let w=d.maxExpAvgSq;x.run("adam_update",u,h,S,v,w,y,b,u,S,v,w)}else x.run("adam_update",u,h,S,v,y,b,u,S,v);l._impl.bumpVersion()}}}};var _m=class{static{f(this,"GradScaler")}constructor(t={}){this.enabled=t.enabled!==!1,this._scale=t.initScale??65536,this._growthFactor=t.growthFactor??2,this._backoffFactor=t.backoffFactor??.5,this._growthInterval=t.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(t){if(!this.enabled)return t;let e=t._impl.storage.data;for(let s=0;s<e.length;s++)e[s]*=this._scale;return t._impl.bumpVersion&&t._impl.bumpVersion(),t}unscale_(t){if(!this.enabled)return!1;let e=1/this._scale,s=!1;for(let r of t.paramGroups)for(let o of r.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 c=i[a]*e;Number.isFinite(c)||(s=!0),i[a]=c}}return this._unscaled.add(t),s&&(this._foundInf=!0),s}step(t){return this.enabled?(this._unscaled.has(t)||this.unscale_(t),this._foundInf?!1:(t.step(),!0)):(t.step(),!0)}update(t){this.enabled&&(t!==void 0?(this._scale=t,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)}};Pb();Wb();qn();var UR={};Ft(UR,{Accuracy:()=>zl,CSVLogger:()=>Cl,Callback:()=>ms,CallbackConnector:()=>ja,ConfusionMatrix:()=>Gl,ConsoleLogger:()=>pi,EarlyStopping:()=>El,EvaluationLoop:()=>Za,F1Score:()=>Vl,FitLoop:()=>Ja,GradientAccumulationScheduler:()=>Ml,HOOKS:()=>LR,LearningRateMonitor:()=>Tl,LightningModule:()=>Rl,Logger:()=>$r,LoggerConnector:()=>Ka,MeanMetric:()=>Dl,Metric:()=>xs,MetricAccumulator:()=>qa,MetricCollection:()=>Pl,ModelCheckpoint:()=>ho,Precision:()=>$l,PredictionLoop:()=>Xa,ProgressCallback:()=>po,Recall:()=>Fl,SingleDeviceStrategy:()=>Wa,Stage:()=>cs,SumMetric:()=>Ll,Timer:()=>Il,Trainer:()=>Nl,TrainerState:()=>Ua,TrainingLoop:()=>Ha,ValidationLoop:()=>Ya,applyCheckpoint:()=>Jb,deserializeCheckpoint:()=>Rm,loadCheckpoint:()=>Yb,serializeCheckpoint:()=>Am});var Rl=class extends ct{static{f(this,"LightningModule")}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(t){this._automaticOptimization=t}get optimizers(){return this._currentOptimizers}trainingStep(t,e){throw new Error(`${this.constructor.name}.trainingStep() not implemented`)}validationStep(t,e){}testStep(t,e){}predictStep(t,e){return this.forward(Array.isArray(t)&&t.length===1?t[0]:t)}configureOptimizers(){throw new Error(`${this.constructor.name}.configureOptimizers() not implemented`)}onTrainEpochStart(){}onTrainEpochEnd(){}onValidationEpochStart(){}onValidationEpochEnd(){}onTestEpochStart(){}onTestEpochEnd(){}log(t,e,{onStep:s=null,onEpoch:r=null,reduceFx:o="mean",progBar:i=!1}={}){let a=this._trainer?this._trainer.state.stage:"training",c=a==="training",l=a!=="training";this._logBuffer.set(t,{value:e,onStep:s!==null?s:c,onEpoch:r!==null?r:l,reduceFx:o,progBar:i})}logDict(t,e={}){for(let s of Object.keys(t))this.log(s,t[s],e)}manualBackward(t){this._trainer&&this._trainer.strategy?this._trainer.strategy.backward(t):t.backward()}};function DR(n){if(!n)throw new Error("configureOptimizers() returned null/undefined");if(Array.isArray(n)){let t=[],e=[];for(let s=0;s<n.length;s++){let r=n[s];r.optimizer?(t.push(r.optimizer),e.push(CR(r.lrScheduler))):(t.push(r),e.push(null))}return{optimizers:t,schedulerConfigs:e}}if(n.optimizer)return{optimizers:[n.optimizer],schedulerConfigs:[CR(n.lrScheduler)]};if(typeof n.step=="function")return{optimizers:[n],schedulerConfigs:[null]};throw new Error("configureOptimizers() returned an unrecognized format")}f(DR,"parseOptimizersConfig");function CR(n){return n?n.scheduler?{scheduler:n.scheduler,interval:n.interval||"epoch",frequency:n.frequency||1,monitor:n.monitor||null}:typeof n.step=="function"?{scheduler:n,interval:"epoch",frequency:1,monitor:null}:null:null}f(CR,"normalizeSchedulerConfig");var cs=Object.freeze({IDLE:"idle",TRAINING:"training",VALIDATING:"validating",TESTING:"testing",PREDICTING:"predicting"}),f2=Object.freeze({mean:f(n=>n.count===0?0:n.sum/n.count,"mean"),sum:f(n=>n.sum,"sum"),min:f(n=>n.min,"min"),max:f(n=>n.max,"max"),last:f(n=>n.last,"last")});function p2(){return{sum:0,count:0,min:1/0,max:-1/0,last:0}}f(p2,"createAccumulator");function h2(n,t){let e=typeof t=="number"?t:t.item();n.sum+=e,n.count+=1,e<n.min&&(n.min=e),e>n.max&&(n.max=e),n.last=e}f(h2,"updateAccumulator");var qa=class{static{f(this,"MetricAccumulator")}constructor(){this._accumulators=new Map,this._reduceFns=new Map}update(t,e,s="mean"){this._accumulators.has(t)||(this._accumulators.set(t,p2()),this._reduceFns.set(t,s)),h2(this._accumulators.get(t),e)}compute(t){let e=this._accumulators.get(t);if(!e)return;let s=f2[this._reduceFns.get(t)];return s(e)}computeAll(){let t={};for(let[e]of this._accumulators)t[e]=this.compute(e);return t}reset(){this._accumulators.clear(),this._reduceFns.clear()}has(t){return this._accumulators.has(t)}get size(){return this._accumulators.size}},Ua=class{static{f(this,"TrainerState")}constructor(){this.stage=cs.IDLE,this.epoch=0,this.globalStep=0,this.maxEpochs=0,this.maxSteps=-1,this.shouldStop=!1,this.stepMetrics=new qa,this.epochMetrics=new qa}resetEpochMetrics(){this.epochMetrics.reset()}resetStepMetrics(){this.stepMetrics.reset()}},Wa=class{static{f(this,"SingleDeviceStrategy")}constructor(){this.device=null}setup(t,e){this.device=e,(e.type==="gpu"||e.type==="webgpu")&&typeof t.to=="function"&&t.to(e)}toDevice(t){if(!this.device||this.device.type!=="gpu"&&this.device.type!=="webgpu")return t;if(t&&t.device&&typeof t.to=="function")return t.to(this.device);if(Array.isArray(t)){let e=new Array(t.length);for(let s=0;s<t.length;s++)e[s]=this.toDevice(t[s]);return e}return t}backward(t){t.backward()}optimizerStep(t){t.step()}};var LR=Object.freeze({ON_FIT_START:"onFitStart",ON_FIT_END:"onFitEnd",ON_TRAIN_START:"onTrainStart",ON_TRAIN_END:"onTrainEnd",ON_TRAIN_EPOCH_START:"onTrainEpochStart",ON_TRAIN_EPOCH_END:"onTrainEpochEnd",ON_TRAIN_BATCH_START:"onTrainBatchStart",ON_TRAIN_BATCH_END:"onTrainBatchEnd",ON_VALIDATION_START:"onValidationStart",ON_VALIDATION_END:"onValidationEnd",ON_VALIDATION_EPOCH_START:"onValidationEpochStart",ON_VALIDATION_EPOCH_END:"onValidationEpochEnd",ON_VALIDATION_BATCH_START:"onValidationBatchStart",ON_VALIDATION_BATCH_END:"onValidationBatchEnd",ON_TEST_START:"onTestStart",ON_TEST_END:"onTestEnd",ON_TEST_BATCH_START:"onTestBatchStart",ON_TEST_BATCH_END:"onTestBatchEnd",ON_PREDICT_START:"onPredictStart",ON_PREDICT_END:"onPredictEnd",ON_PREDICT_BATCH_START:"onPredictBatchStart",ON_PREDICT_BATCH_END:"onPredictBatchEnd",SETUP:"setup",TEARDOWN:"teardown",ON_BEFORE_BACKWARD:"onBeforeBackward",ON_AFTER_BACKWARD:"onAfterBackward",ON_BEFORE_OPTIMIZER_STEP:"onBeforeOptimizerStep",ON_BEFORE_ZERO_GRAD:"onBeforeZeroGrad",ON_SAVE_CHECKPOINT:"onSaveCheckpoint",ON_LOAD_CHECKPOINT:"onLoadCheckpoint"}),ja=class{static{f(this,"CallbackConnector")}constructor(t=[]){this._callbacks=t}get callbacks(){return this._callbacks}add(t){this._callbacks.push(t)}dispatch(t,...e){for(let s=0;s<this._callbacks.length;s++){let r=this._callbacks[s][t];r&&r.call(this._callbacks[s],...e)}}remove(t){let e=this._callbacks.indexOf(t);e!==-1&&this._callbacks.splice(e,1)}},Ka=class{static{f(this,"LoggerConnector")}constructor(t=[],e=null){this._loggers=Array.isArray(t)?t:[t],this._state=e}drain(t){let e=t._logBuffer;if(e.size!==0){for(let[s,r]of e){let{value:o,onStep:i,onEpoch:a,reduceFx:c,progBar:l}=r;if(a&&this._state.epochMetrics.update(s,o,c),i&&this._state.stepMetrics.update(s,o,c),l){this._state._progBarMetrics||(this._state._progBarMetrics=new Map);let u=typeof o=="number"?o:o.item();this._state._progBarMetrics.set(s,u)}}e.clear()}}flushStepMetrics(t){let e=this._state.stepMetrics.computeAll();if(Object.keys(e).length===0)return e;for(let s=0;s<this._loggers.length;s++)this._loggers[s].logMetrics(e,t);return this._state.stepMetrics.reset(),e}flushEpochMetrics(t){let e=this._state.epochMetrics.computeAll();if(Object.keys(e).length===0)return e;for(let s=0;s<this._loggers.length;s++)this._loggers[s].logMetrics(e,t);return this._state.epochMetrics.reset(),e}logHyperparams(t){for(let e=0;e<this._loggers.length;e++)this._loggers[e].logHyperparams(t)}};$t();vr();Tr();var Ha=class{static{f(this,"TrainingLoop")}async run(t,e,s,r,o){let i=s.state,a=s.callbackConnector,c=s.loggerConnector,l=s.strategy,u=s.accumulateGradBatches,h=d2(s.limitTrainBatches,e.length);i.numTrainingBatches=h,i.stage=cs.TRAINING,t.train(),sr.setEnabled(!0),t.onTrainEpochStart(),a.dispatch("onTrainEpochStart",s,t);let p=0;for(let m of e){if(p>=h||i.shouldStop)break;if(i.maxSteps>0&&i.globalStep>=i.maxSteps){i.shouldStop=!0;break}let g=l.toDevice(m);a.dispatch("onTrainBatchStart",s,t,g,p);let _;t.automaticOptimization?_=await this._automaticStep(t,g,p,s,r,o,l,u,a):_=await Promise.resolve(t.trainingStep(g,p)),c.drain(t),i.globalStep%s.logEveryNSteps===0&&c.flushStepMetrics(i.globalStep),a.dispatch("onTrainBatchEnd",s,t,_,g,p),Ig(),i.globalStep++,p++}Ig();let d=c.flushEpochMetrics(i.globalStep);return this._stepEpochSchedulers(o,i.epoch),t.onTrainEpochEnd(),a.dispatch("onTrainEpochEnd",s,t),d}async _automaticStep(t,e,s,r,o,i,a,c,l){if(r.compile)return this._compiledStep(t,e,s,r,o,i,c);if(r.cudaGraph)return this._graphedStep(t,e,r,o,i,a);let u=await Promise.resolve(t.trainingStep(e,s)),h=u,p=u;if(u&&typeof u=="object"&&!u.backward&&(h=u.loss,p=u),c>1&&(h=Se(h,c)),l.dispatch("onBeforeBackward",r,t,h),a.backward(h),l.dispatch("onAfterBackward",r,t),(s+1)%c===0){for(let m=0;m<o.length;m++)this._clipGradients(t,r),l.dispatch("onBeforeOptimizerStep",r,t,o[m]),a.optimizerStep(o[m]),l.dispatch("onBeforeZeroGrad",r,t,o[m]),o[m].zeroGrad();this._stepStepSchedulers(i,r.state.globalStep)}return p}async _compiledStep(t,e,s,r,o,i,a){let c=Array.isArray(e)?e:[e],l=f((..._)=>t.trainingStep(Array.isArray(e)?_:_[0],0),"callForward"),u;if(t.__compiledTrainStep)u=t.__compiledTrainStep(...c),u&&u.then&&(u=await u);else{let{compileWithBackward:_}=await Promise.resolve().then(()=>(Wb(),MR)),{CPUTarget:y,CUDATarget:b,WebGPUTarget:x}=await Promise.resolve().then(()=>(qn(),Px)),S=t._device&&t._device.type,v=S==="webgpu"?x():S==="gpu"?b():y();t.__compiledTrainStep=_({forward:l},c,{target:v,mode:r.compileMode});let w=t.log;t.log=()=>{};try{u=t.__compiledTrainStep(...c),u&&u.then&&(u=await u)}finally{t.log=w}}let h=t.__compiledTrainStep,p=h.capturedParams(),{ones:d}=await Promise.resolve().then(()=>(ae(),$v)),m=h.backward(d(u.shape));m&&m.then&&(m=await m);let g=m.length-p.length;for(let _=0;_<p.length;_++){let y=m[g+_];y&&(p[_].grad=y)}if((s+1)%a===0){for(let _=0;_<o.length;_++)this._clipGradients(t,r),o[_].step(),o[_].zeroGrad();this._stepStepSchedulers(i,r.state.globalStep)}return t.log&&t.log("train_loss",u),u}async _eagerTrainStepCore(t,e,s,r,o){let i=await Promise.resolve(t.trainingStep(e,0)),a=i;if(i&&typeof i=="object"&&!i.backward&&(a=i.loss),r.backward(a),o&&o.gradientClipVal){let{deviceClipGradNorm:c}=await Promise.resolve().then(()=>(Yp(),Hp));c([...t.parameters()],o.gradientClipVal)}for(let c=0;c<s.length;c++)r.optimizerStep(s[c]),s[c].zeroGrad();return a}async _graphedStep(t,e,s,r,o,i){let a=await Promise.resolve().then(()=>(mp(),FS)),c=await Promise.resolve().then(()=>(zc(),ev)),l=await Promise.resolve().then(()=>(To(),NS)),u=t.__eagerGraphRunner;if(u||(u=t.__eagerGraphRunner={phase:"warmup",seen:0},c.setEagerDeferred(!0),Gi(!0)),u.phase==="disabled")return this._eagerTrainStepCore(t,e,r,i,s);if(u.phase==="warmup"&&u.seen<s.cudaGraphWarmupSteps)return u.seen++,this._eagerTrainStepCore(t,e,r,i,s);let h=jb(e);if(u.phase==="warmup"){u.inputs=h.map(_=>{let y=_._impl.storage.rawData;return c.deviceBufferForInput(y),c.pinResident(y),{dptr:c.deviceBufferDptr(y)}});let d=t.log,m;try{a.beginEagerCapture(),t.log=()=>{};try{m=await this._eagerTrainStepCore(t,e,r,i,s)}finally{t.log=d}u.captured=a.endEagerCapture()}catch(_){try{a.endEagerCapture()}catch{}if(t.log=d,c.clearCapturePins(),u.phase="disabled",u.captureError=_&&_.message,process.env.MLFW_DEBUG_CUDAGRAPH)throw _;return this._eagerTrainStepCore(t,e,r,i,s)}u.exec=u.captured.exec,u.lossDptr=c.deviceBufferDptr(m._impl.storage.rawData),u.lossScratch=new Float32Array(1),u.phase="replay",a.replay(u.exec),a.syncStream(),l.copyDeviceToHost(u.lossScratch,u.lossDptr);let g=u.lossScratch[0];return this._logGraphLoss(s,g),this._stepStepSchedulers(o,s.state.globalStep),g}for(let d=0;d<u.inputs.length&&d<h.length;d++)u.inputs[d].dptr&&l.copyHostToDeviceAsync(u.inputs[d].dptr,h[d]._impl.storage.rawData);a.replay(u.exec),a.syncStream(),l.copyDeviceToHost(u.lossScratch,u.lossDptr);let p=u.lossScratch[0];return this._logGraphLoss(s,p),this._stepStepSchedulers(o,s.state.globalStep),p}_logGraphLoss(t,e){t.state.stepMetrics.update("train_loss",e),t.state.epochMetrics.update("train_loss",e),t.state._progBarMetrics||(t.state._progBarMetrics=new Map),t.state._progBarMetrics.set("train_loss",e)}_clipGradients(t,e){if(!e.gradientClipVal)return;let s=[...t.parameters()];e.gradientClipAlgorithm==="norm"?ol(s,e.gradientClipVal):il(s,e.gradientClipVal)}_stepStepSchedulers(t,e){if(t)for(let s=0;s<t.length;s++){let r=t[s];!r||r.interval!=="step"||r.monitor||(e+1)%r.frequency===0&&r.scheduler.step()}}_stepEpochSchedulers(t,e){if(t)for(let s=0;s<t.length;s++){let r=t[s];!r||r.interval!=="epoch"||r.monitor||(e+1)%r.frequency===0&&r.scheduler.step()}}};function jb(n,t=[]){if(n==null)return t;if(n.shape!==void 0&&typeof n.contiguous=="function")return t.push(n),t;if(Array.isArray(n)){for(let e of n)jb(e,t);return t}if(typeof n=="object"){for(let e of Object.keys(n))jb(n[e],t);return t}return t}f(jb,"_flattenTensors");function d2(n,t){return n==null?t:typeof n=="number"?n>0&&n<=1?Math.max(1,Math.round(n*t)):Math.min(n,t):t}f(d2,"resolveLimit");var Ya=class{static{f(this,"ValidationLoop")}async run(t,e,s,r){let o=s.state,i=s.callbackConnector,a=s.loggerConnector,c=o.stage;o.stage=cs.VALIDATING,t.eval(),t.onValidationEpochStart(),i.dispatch("onValidationStart",s,t),i.dispatch("onValidationEpochStart",s,t);let l=g2(s.limitValBatches,e.length);o.numValBatches=l;let u=0;await m2(async()=>{for(let p of e){if(u>=l)break;i.dispatch("onValidationBatchStart",s,t,p,u);let d=await Promise.resolve(t.validationStep(p,u));a.drain(t),i.dispatch("onValidationBatchEnd",s,t,d,p,u),u++}});let h=a.flushEpochMetrics(o.globalStep);return this._stepPlateauSchedulers(r,h),t.onValidationEpochEnd(),i.dispatch("onValidationEpochEnd",s,t),i.dispatch("onValidationEnd",s,t),t.train(),o.stage=c,h}_stepPlateauSchedulers(t,e){if(t)for(let s=0;s<t.length;s++){let r=t[s];if(!r||!r.monitor)continue;let o=r.scheduler;if(typeof o.step=="function"&&o.step.length>0){let i=e[r.monitor];i!==void 0&&o.step(i)}}}};async function m2(n){let{GradMode:t}=await Promise.resolve().then(()=>(Tr(),Up)),e=t.isEnabled();t.setEnabled(!1);try{await n()}finally{t.setEnabled(e)}}f(m2,"noGradAsync");function g2(n,t){return n==null?t:typeof n=="number"?n>0&&n<=1?Math.max(1,Math.round(n*t)):Math.min(n,t):t}f(g2,"resolveLimit");var Ja=class{static{f(this,"FitLoop")}constructor(){this._trainingLoop=new Ha,this._validationLoop=new Ya}get trainingLoop(){return this._trainingLoop}get validationLoop(){return this._validationLoop}async run(t,e,s,r,o,i){let a=r.state,c=r.callbackConnector;c.dispatch("onTrainStart",r,t);for(let l=0;l<a.maxEpochs&&!(a.shouldStop||a.maxSteps>0&&a.globalStep>=a.maxSteps||(a.epoch=l,a.resetEpochMetrics(),await this._trainingLoop.run(t,e,r,o,i),a.shouldStop));l++)s&&this._shouldRunValidation(l,r)&&await this._validationLoop.run(t,s,r,i);c.dispatch("onTrainEnd",r,t)}_shouldRunValidation(t,e){let s=e.checkValEveryNEpoch;return(t+1)%s===0}};var Za=class{static{f(this,"EvaluationLoop")}async run(t,e,s){let r=s.state,o=s.callbackConnector,i=s.loggerConnector,a=r.stage;r.stage=cs.TESTING,t.eval(),t.onTestEpochStart(),o.dispatch("onTestStart",s,t),o.dispatch("onTestEpochStart",s,t);let c=y2(s.limitTestBatches,e.length),l=0;await _2(async()=>{for(let h of e){if(l>=c)break;o.dispatch("onTestBatchStart",s,t,h,l);let p=await Promise.resolve(t.testStep(h,l));i.drain(t),o.dispatch("onTestBatchEnd",s,t,p,h,l),l++}});let u=i.flushEpochMetrics(r.globalStep);return t.onTestEpochEnd(),o.dispatch("onTestEpochEnd",s,t),o.dispatch("onTestEnd",s,t),t.train(),r.stage=a,u}};async function _2(n){let{GradMode:t}=await Promise.resolve().then(()=>(Tr(),Up)),e=t.isEnabled();t.setEnabled(!1);try{await n()}finally{t.setEnabled(e)}}f(_2,"noGradAsync");function y2(n,t){return n==null?t:typeof n=="number"?n>0&&n<=1?Math.max(1,Math.round(n*t)):Math.min(n,t):t}f(y2,"resolveLimit");var Xa=class{static{f(this,"PredictionLoop")}async run(t,e,s){let r=s.state,o=s.callbackConnector,i=s.strategy,a=r.stage;r.stage=cs.PREDICTING,t.eval(),o.dispatch("onPredictStart",s,t);let c=[],l=w2(s.limitTestBatches,e.length),u=0;return await b2(async()=>{for(let h of e){if(u>=l)break;let p=i.toDevice(h);o.dispatch("onPredictBatchStart",s,t,p,u);let d=await Promise.resolve(t.predictStep(p,u));await s._flushEagerInference(),c.push(d),o.dispatch("onPredictBatchEnd",s,t,d,p,u),u++}}),o.dispatch("onPredictEnd",s,t),t.train(),r.stage=a,c}};async function b2(n){let{GradMode:t}=await Promise.resolve().then(()=>(Tr(),Up)),e=t.isEnabled();t.setEnabled(!1);try{await n()}finally{t.setEnabled(e)}}f(b2,"noGradAsync");function w2(n,t){return n==null?t:typeof n=="number"?n>0&&n<=1?Math.max(1,Math.round(n*t)):Math.min(n,t):t}f(w2,"resolveLimit");var $r=class{static{f(this,"Logger")}constructor({name:t="default",version:e=0}={}){this._name=t,this._version=e}get name(){return this._name}get version(){return this._version}logMetrics(t,e){throw new Error(`${this.constructor.name}.logMetrics() not implemented`)}logHyperparams(t){throw new Error(`${this.constructor.name}.logHyperparams() not implemented`)}finalize(){}};var pi=class extends $r{static{f(this,"ConsoleLogger")}constructor(t={}){super(t),this._logFrequency=t.logFrequency||1,this._callCount=0}logMetrics(t,e){if(this._callCount++,this._callCount%this._logFrequency!==0)return;let s=[`[step ${e}]`],r=Object.keys(t).sort();for(let o=0;o<r.length;o++){let i=t[r[o]];s.push(`${r[o]}: ${x2(i)}`)}console.log(s.join(" | "))}logHyperparams(t){let e=["[hyperparams]"],s=Object.keys(t).sort();for(let r=0;r<s.length;r++)e.push(`${s[r]}: ${t[s[r]]}`);console.log(e.join(" | "))}};function x2(n){return typeof n!="number"||Number.isInteger(n)?String(n):Math.abs(n)<.001&&n!==0?n.toExponential(3):n.toFixed(4)}f(x2,"formatNumber");var ms=class{static{f(this,"Callback")}setup(t,e,s){}teardown(t,e,s){}onFitStart(t,e){}onFitEnd(t,e){}onTrainStart(t,e){}onTrainEnd(t,e){}onTrainEpochStart(t,e){}onTrainEpochEnd(t,e){}onTrainBatchStart(t,e,s,r){}onTrainBatchEnd(t,e,s,r,o){}onValidationStart(t,e){}onValidationEnd(t,e){}onValidationEpochStart(t,e){}onValidationEpochEnd(t,e){}onValidationBatchStart(t,e,s,r){}onValidationBatchEnd(t,e,s,r,o){}onTestStart(t,e){}onTestEnd(t,e){}onTestBatchStart(t,e,s,r){}onTestBatchEnd(t,e,s,r,o){}onPredictStart(t,e){}onPredictEnd(t,e){}onPredictBatchStart(t,e,s,r){}onPredictBatchEnd(t,e,s,r,o){}onBeforeBackward(t,e,s){}onAfterBackward(t,e){}onBeforeOptimizerStep(t,e,s){}onBeforeZeroGrad(t,e,s){}onSaveCheckpoint(t,e,s){}onLoadCheckpoint(t,e,s){}};var S2=["\u258F","\u258E","\u258D","\u258C","\u258B","\u258A","\u2589"],po=class extends ms{static{f(this,"ProgressCallback")}constructor({barLength:t=24}={}){super(),this._barLength=t,this._trainBatchCount=0,this._valBatchCount=0,this._epochStartTime=0,this._lastLen=0,this._active=!1}onTrainEpochStart(t,e){this._trainBatchCount=0,this._epochStartTime=Date.now();let s=this._trainTotal(t);s&&this._render("Epoch",t.state.epoch+1,t.state.maxEpochs,0,s,t.state)}onTrainBatchEnd(t,e,s,r,o){this._trainBatchCount++;let i=this._trainTotal(t);i&&this._render("Epoch",t.state.epoch+1,t.state.maxEpochs,this._trainBatchCount,i,t.state)}onTrainEpochEnd(t,e){let s=this._trainTotal(t);s&&this._render("Epoch",t.state.epoch+1,t.state.maxEpochs,s,s,t.state)}onTrainEnd(t,e){this._active&&process.stdout.write(`
|
|
204
|
-
`),this._active=!1,this._lastLen=0}onValidationEpochStart(t,e){this._valBatchCount=0,this._epochStartTime=Date.now()}onValidationBatchEnd(t,e,s,r,o){this._valBatchCount++;let i=this._valTotal(t);i&&this._render("Validation",null,null,this._valBatchCount,i,t.state)}onValidationEnd(t,e){let s=this._valTotal(t);s&&this._render("Validation",null,null,s,s,t.state)}_trainTotal(t){return t.state.numTrainingBatches??PR(t.limitTrainBatches)}_valTotal(t){return t.state.numValBatches??PR(t.limitValBatches)}_render(t,e,s,r,o,i){let a=o>0?Math.min(r/o,1):0,c=String(Math.round(a*100)).padStart(3," "),l=this._bar(a),u=e!==null?`${t} ${e}/${s}`:t,h=(Date.now()-this._epochStartTime)/1e3,p=h>0?r/h:0,d=p>0?(o-r)/p:0,m=`${zR(h)}<${zR(d)}, ${p.toFixed(2)}it/s`,g=this._formatProgBarMetrics(i),_=`${u}: ${c}%|${l}| ${r}/${o} [${m}${g}]`,y=Math.max(0,this._lastLen-_.length);process.stdout.write("\r"+_+" ".repeat(y)),this._lastLen=_.length,this._active=!0}_bar(t){let e=this._barLength,s=t*e,r=Math.floor(s),o=Math.round((s-r)*8);if(o===8&&(r+=1,o=0),r>=e)return"\u2588".repeat(e);let i=o>0?S2[o-1]:"",a=e-r-(i?1:0);return"\u2588".repeat(r)+i+" ".repeat(a)}_formatProgBarMetrics(t){let e=t._progBarMetrics;if(!e||e.size===0)return"";let s=[];for(let[r,o]of e)s.push(`${r}=${v2(o)}`);return", "+s.join(", ")}};function PR(n){return n==null?null:typeof n=="number"&&n>1?n:null}f(PR,"resolveTotal");function zR(n){(!Number.isFinite(n)||n<0)&&(n=0);let t=Math.round(n),e=Math.floor(t/60),s=t%60;return`${String(e).padStart(2,"0")}:${String(s).padStart(2,"0")}`}f(zR,"fmtTime");function v2(n){return typeof n!="number"||Number.isInteger(n)?String(n):n.toFixed(4)}f(v2,"formatNum");function Fr(...n){return n.filter(t=>t!=null&&t!=="").join("/").replace(/\/+/g,"/")}f(Fr,"joinPath");Ps();Qr();Qs();var FR="mlfw-ckpt-v1",Ol=8,B2={Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array};function k2(n){return ArrayBuffer.isView(n)&&!(n instanceof DataView)}f(k2,"isTypedArray");function Kb(n){return new Uint8Array(n.buffer,n.byteOffset,n.byteLength)}f(Kb,"byteView");function Am(n){let t=[],e=Bm(n,t),s=0;for(let c of t)c.meta.offset=s,c.meta.length=c.bytes.length,s+=c.bytes.length;let r={format:FR,tree:e,buffers:t.map(c=>c.meta)},o=new TextEncoder().encode(JSON.stringify(r)),i=new Uint8Array(Ol+o.length+s);new DataView(i.buffer).setBigUint64(0,BigInt(o.length),!0),i.set(o,Ol);let a=Ol+o.length;for(let c of t)i.set(c.bytes,a),a+=c.bytes.length;return i}f(Am,"serializeCheckpoint");function Rm(n){let t=n instanceof Uint8Array?n:new Uint8Array(n),e=new DataView(t.buffer,t.byteOffset,t.byteLength),s=Number(e.getBigUint64(0,!0)),r=Ol+s,o=JSON.parse(new TextDecoder().decode(t.subarray(Ol,r)));if(o.format!==FR)throw new Error("mlfw: unrecognized checkpoint format: "+o.format);let i=o.buffers.map(a=>A2(a,t,r));return km(o.tree,i)}f(Rm,"deserializeCheckpoint");function Bm(n,t){if(n instanceof zt)return $R(t,{kind:"tensor",dtype:n.dtype,shape:n.shape},Kb(n._impl.storage.data));if(k2(n))return $R(t,{kind:"array",arrayType:n.constructor.name},Kb(n));if(n instanceof Map){let e=[];for(let[s,r]of n)e.push([s,Bm(r,t)]);return{$map:e}}if(Array.isArray(n))return n.map(e=>Bm(e,t));if(n&&typeof n=="object"){let e={};for(let s of Object.keys(n))e[s]=Bm(n[s],t);return e}return n}f(Bm,"encodeNode");function $R(n,t,e){let s=n.length;return n.push({meta:t,bytes:e}),{$buf:s}}f($R,"pushBuffer");function A2(n,t,e){let s=e+n.offset,r=t.subarray(s,s+n.length),o=n.kind==="tensor"?Jt(n.dtype):B2[n.arrayType];if(!o)throw new Error("mlfw: unknown typed array in checkpoint: "+n.arrayType);let i=new o(n.length/o.BYTES_PER_ELEMENT);return Kb(i).set(r),n.kind==="tensor"?Fo(i,n.shape,n.dtype):i}f(A2,"decodeBuffer");function km(n,t){if(n===null||typeof n!="object")return n;if(Array.isArray(n))return n.map(s=>km(s,t));if("$buf"in n)return t[n.$buf];if("$map"in n){let s=new Map;for(let[r,o]of n.$map)s.set(r,km(o,t));return s}let e={};for(let s of Object.keys(n))e[s]=km(n[s],t);return e}f(km,"decodeNode");var Hb=".ckpt",ho=class extends ms{static{f(this,"ModelCheckpoint")}constructor({dirpath:t="./lightning_logs/checkpoints",filename:e="epoch={epoch}-step={step}",monitor:s=null,mode:r="min",saveTopK:o=1,saveLast:i=!0,everyNEpochs:a=1}={}){super(),this._dirpath=t,this._filename=e,this._monitor=s,this._mode=r,this._saveTopK=o,this._saveLast=i,this._everyNEpochs=a,this._bestK=[],this._recent=[],this._compareFn=r==="min"?(c,l)=>c-l:(c,l)=>l-c,this._bestModelPath=null,this._lastModelPath=null}get bestModelPath(){return this._bestModelPath}get lastModelPath(){return this._lastModelPath}get bestKModels(){return this._bestK}onTrainEpochEnd(t,e){let s=t.state;if((s.epoch+1)%this._everyNEpochs!==0)return;this._ensureDir();let r=this._fillTemplate(s);if(this._saveLast){let u=Fr(this._dirpath,"last"+Hb);this._saveCheckpoint(e,t,u),this._lastModelPath=u}if(!this._monitor){let u=Fr(this._dirpath,r+Hb);if(this._saveCheckpoint(e,t,u),this._saveTopK>=0)for(this._recent.push(u);this._recent.length>this._saveTopK;)this._tryDelete(this._recent.shift());return}let i=s.epochMetrics.computeAll()[this._monitor];if(i===void 0)return;let a=Fr(this._dirpath,r+Hb),c={score:i,path:a};if(this._saveTopK<0){this._saveCheckpoint(e,t,a),this._bestK.push(c),this._updateBest();return}let l=this._findInsertIndex(i);if(l<this._saveTopK){if(this._saveCheckpoint(e,t,a),this._bestK.splice(l,0,c),this._bestK.length>this._saveTopK){let u=this._bestK.pop();this._tryDelete(u.path)}this._updateBest()}}_findInsertIndex(t){let e=0,s=this._bestK.length;for(;e<s;){let r=e+s>>>1;this._compareFn(t,this._bestK[r].score)<0?s=r:e=r+1}return e}_updateBest(){this._bestK.length>0&&(this._bestModelPath=this._bestK[0].path)}_saveCheckpoint(t,e,s){let r={epoch:e.state.epoch,globalStep:e.state.globalStep,modelState:t.stateDict()},o=t._currentOptimizers;o&&o.length>0&&(r.optimizerStates=o.map(a=>a.stateDict())),e.callbackConnector.dispatch("onSaveCheckpoint",e,t,r);let i=s+".tmp";ts.writeBinary(i,Am(r)),ts.rename(i,s)}_fillTemplate(t){return this._filename.replace("{epoch}",t.epoch).replace("{step}",t.globalStep)}_ensureDir(){ts.exists(this._dirpath)||ts.mkdir(this._dirpath)}_tryDelete(t){try{ts.remove(t)}catch{}}};function Yb(n){return Rm(ts.readBinary(n))}f(Yb,"loadCheckpoint");function Jb(n,t,e=[]){if(n.modelState&&t.loadStateDict(n.modelState),n.optimizerStates){let s=Math.min(e.length,n.optimizerStates.length);for(let r=0;r<s;r++)e[r].loadStateDict(n.optimizerStates[r])}return n}f(Jb,"applyCheckpoint");tn();var Nl=class{static{f(this,"Trainer")}constructor({maxEpochs:t=10,maxSteps:e=-1,accelerator:s="auto",precision:r="f32",callbacks:o=[],logger:i=!0,enableCheckpointing:a=!1,enableProgress:c=!0,gradientClipVal:l=null,gradientClipAlgorithm:u="norm",accumulateGradBatches:h=1,limitTrainBatches:p=null,limitValBatches:d=null,limitTestBatches:m=null,valCheckInterval:g=1,checkValEveryNEpoch:_=1,logEveryNSteps:y=50,deterministic:b=!1,fastDevRun:x=!1,defaultRootDir:S="./lightning_logs",compile:v=!1,compileMode:w="separate",cudaGraph:A=!1,cudaGraphWarmupSteps:I=3}={}){if(this._state=new Ua,this._state.maxEpochs=t,this._state.maxSteps=e,this._compile=v,this._compileMode=w,this._cudaGraph=A,this._cudaGraphWarmupSteps=I,this._accelerator=s,this._precision=r,this._gradientClipVal=l,this._gradientClipAlgorithm=u,this._accumulateGradBatches=h,this._limitTrainBatches=p,this._limitValBatches=d,this._limitTestBatches=m,this._valCheckInterval=g,this._checkValEveryNEpoch=_,this._logEveryNSteps=y,this._deterministic=b,this._defaultRootDir=S,x!==!1){let k=typeof x=="number"?x:1;this._limitTrainBatches=k,this._limitValBatches=k,this._limitTestBatches=k,this._state.maxEpochs=1}this._loggers=this._resolveLoggers(i),this._strategy=new Wa,this._fitLoop=new Ja,this._evaluationLoop=new Za,this._predictionLoop=new Xa;let B=[...o];c&&!B.some(k=>k instanceof po)&&B.push(new po),a&&!B.some(k=>k instanceof ho)&&B.push(new ho({dirpath:S})),this._callbackConnector=new ja(B),this._loggerConnector=new Ka(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(t){this._accumulateGradBatches=t}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(t){this._state.shouldStop=t}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(t,e,s=null){this._model=t,t._trainer=this;let r=this._resolveDevice();this._guardEagerWebGPU(r,"fit",s!=null),this._guardCudaGraph(r,s!=null),t._device=r,await this._prepareDevice(r),this._strategy.setup(t,r);let{optimizers:o,schedulerConfigs:i}=DR(await Promise.resolve(t.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(t._currentOptimizers=o,this._loggerConnector.logHyperparams(this._extractHyperparams(t,o)),this._callbackConnector.dispatch("setup",this,t,cs.TRAINING),this._callbackConnector.dispatch("onFitStart",this,t),this._state.shouldStop=!1,await this._fitLoop.run(t,e,s,this,o,i),r===r_){let{teardownAfterFit:a}=await Promise.resolve().then(()=>(Yp(),Hp));a(t,o)}this._callbackConnector.dispatch("onFitEnd",this,t),this._callbackConnector.dispatch("teardown",this,t,cs.TRAINING);for(let a=0;a<this._loggers.length;a++)this._loggers[a].finalize()}async validate(t,e){t._trainer=this,this._model=t;let s=this._resolveDevice();this._guardEagerWebGPU(s,"validate"),t._device=s,await this._prepareDevice(s),this._strategy.setup(t,s),this._callbackConnector.dispatch("setup",this,t,cs.VALIDATING);let r=await this._fitLoop.validationLoop.run(t,e,this,null);this._callbackConnector.dispatch("teardown",this,t,cs.VALIDATING);for(let o=0;o<this._loggers.length;o++)this._loggers[o].finalize();return r}async test(t,e){t._trainer=this,this._model=t;let s=this._resolveDevice();this._guardEagerWebGPU(s,"test"),t._device=s,await this._prepareDevice(s),this._strategy.setup(t,s),this._callbackConnector.dispatch("setup",this,t,cs.TESTING);let r=await this._evaluationLoop.run(t,e,this);this._callbackConnector.dispatch("teardown",this,t,cs.TESTING);for(let o=0;o<this._loggers.length;o++)this._loggers[o].finalize();return r}async predict(t,e){t._trainer=this,this._model=t;let s=this._resolveDevice();return t._device=s,await this._prepareDevice(s),this._strategy.setup(t,s),await this._predictionLoop.run(t,e,this)}_resolveDevice(){return this._accelerator==="gpu"?r_:this._accelerator==="wasm"?ov:this._accelerator==="webgpu"?ji:this._accelerator==="cpu"?Rp:Rp}_guardEagerWebGPU(t,e,s=!1){if(t.type==="webgpu"){if(e==="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(s)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"): ${e}() 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(t,e=!1){if(this._cudaGraph){if(t.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(e)throw new Error("Trainer(cudaGraph=true) v1 does not support in-fit validation. Call fit() without a valLoader.")}}async _prepareDevice(t){if(t.type==="gpu"){let{preloadCudaRuntime:e}=await Promise.resolve().then(()=>(rh(),gy));await e()}else if(t.type==="webgpu"){let{preloadWebGPU:e}=await Promise.resolve().then(()=>(rh(),gy));this._webgpuMod=await e()}}async _flushEagerInference(){this._webgpuMod&&await this._webgpuMod.flushWebGPUEager()}_resolveLoggers(t){return t===!1||t===null?[]:t===!0?[new pi]:Array.isArray(t)?t:[t]}_extractHyperparams(t,e){let s={maxEpochs:this._state.maxEpochs,maxSteps:this._state.maxSteps,accelerator:this._accelerator,precision:this._precision,accumulateGradBatches:this._accumulateGradBatches};for(let r=0;r<e.length;r++){let o=e[r],i=o.defaults,a=e.length>1?`optimizer_${r}_`:"";s[a+"optimizer"]=o.constructor.name,i.lr!==void 0&&(s[a+"lr"]=i.lr),i.weightDecay!==void 0&&(s[a+"weight_decay"]=i.weightDecay)}return s}};var El=class extends ms{static{f(this,"EarlyStopping")}constructor({monitor:t="val_loss",patience:e=3,mode:s="min",minDelta:r=0,checkOnTrainEpochEnd:o=!1}={}){super(),this._monitor=t,this._patience=e,this._mode=s,this._minDelta=r,this._checkOnTrainEpochEnd=o,this._waitCount=0,this._bestScore=null,this._compareFn=s==="min"?(i,a)=>i<a-r:(i,a)=>i>a+r}get monitor(){return this._monitor}get patience(){return this._patience}get bestScore(){return this._bestScore}get waitCount(){return this._waitCount}onValidationEnd(t,e){this._checkOnTrainEpochEnd||this._check(t)}onTrainEpochEnd(t,e){this._checkOnTrainEpochEnd&&this._check(t)}_check(t){let s=t.state.epochMetrics.computeAll()[this._monitor];if(s!==void 0){if(this._bestScore===null||this._compareFn(s,this._bestScore)){this._bestScore=s,this._waitCount=0;return}this._waitCount++,this._waitCount>=this._patience&&(t.shouldStop=!0)}}reset(){this._waitCount=0,this._bestScore=null}};var Tl=class extends ms{static{f(this,"LearningRateMonitor")}constructor({logMomentum:t=!1}={}){super(),this._logMomentum=t,this._lrHistory={}}get lrHistory(){return this._lrHistory}onTrainBatchStart(t,e,s,r){let o=e._currentOptimizers;if(o)for(let i=0;i<o.length;i++){let a=o[i].paramGroups;for(let c=0;c<a.length;c++){let l=o.length>1||a.length>1?`lr_opt${i}_group${c}`:"lr",u=a[c].lr;if(e.log(l,u,{onStep:!0,onEpoch:!1,progBar:!1}),this._lrHistory[l]||(this._lrHistory[l]=[]),this._lrHistory[l].push({step:t.state.globalStep,lr:u}),this._logMomentum&&a[c].momentum!==void 0){let h=l.replace("lr","momentum");e.log(h,a[c].momentum,{onStep:!0,onEpoch:!1})}}}}};var Il=class extends ms{static{f(this,"Timer")}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(t,e){this._fitStartTime=performance.now()}onFitEnd(t,e){this._totalTrainingTime=(performance.now()-this._fitStartTime)/1e3}onTrainEpochStart(t,e){this._epochStartTime=performance.now()}onTrainEpochEnd(t,e){this._epochDurations.push((performance.now()-this._epochStartTime)/1e3)}onValidationStart(t,e){this._valStartTime=performance.now()}onValidationEnd(t,e){this._validationDurations.push((performance.now()-this._valStartTime)/1e3)}};var Ml=class extends ms{static{f(this,"GradientAccumulationScheduler")}constructor({scheduling:t}){super(),this._scheduling=new Map;let e=Object.entries(t);for(let s=0;s<e.length;s++)this._scheduling.set(Number(e[s][0]),e[s][1]);this._sortedEpochs=[...this._scheduling.keys()].sort((s,r)=>s-r)}onTrainEpochStart(t,e){let s=t.state.epoch;this._scheduling.has(s)&&(t.accumulateGradBatches=this._scheduling.get(s))}getCurrentAccumulation(t){let e=1;for(let s=0;s<this._sortedEpochs.length&&this._sortedEpochs[s]<=t;s++)e=this._scheduling.get(this._sortedEpochs[s]);return e}};var Cl=class extends $r{static{f(this,"CSVLogger")}constructor({saveDir:t="./lightning_logs",name:e="default",version:s=null,flushInterval:r=10}={}){super({name:e,version:s||0}),this._saveDir=t,this._flushInterval=r,this._columns=[],this._columnSet=new Set,this._buffer=[],this._filePath=null,this._headerWritten=!1,this._version=s}get logDir(){let t=this._version!==null?this._version:this._resolveVersion();return Fr(this._saveDir,this._name,`version_${t}`)}logMetrics(t,e){let s={step:e},r=Object.keys(t);for(let o=0;o<r.length;o++){let i=r[o];s[i]=t[i],this._columnSet.has(i)||(this._columnSet.add(i),this._columns.push(i),this._headerWritten=!1)}this._buffer.push(s),this._buffer.length>=this._flushInterval&&this._flush()}logHyperparams(t){this._ensureDir();let e=Fr(this.logDir,"hparams.json");ts.writeFile(e,JSON.stringify(t,null,2))}finalize(){this._buffer.length>0&&this._flush()}_flush(){this._ensureDir();let t=this._getFilePath();if(!this._headerWritten){let r=["step",...this._columns].join(",");ts.writeFile(t,r+`
|
|
205
|
-
`),this._headerWritten=!0}let e=["step",...this._columns],s=[];for(let r=0;r<this._buffer.length;r++){let o=this._buffer[r],i=[];for(let a=0;a<e.length;a++){let c=o[e[a]];i.push(c!==void 0?String(c):"")}s.push(i.join(","))}ts.appendFile(t,s.join(`
|
|
206
|
-
`)+`
|
|
207
|
-
`),this._buffer.length=0}_getFilePath(){return this._filePath||(this._filePath=Fr(this.logDir,"metrics.csv")),this._filePath}_ensureDir(){let t=this.logDir;ts.exists(t)||ts.mkdir(t)}_resolveVersion(){if(this._version!==null)return this._version;let t=Fr(this._saveDir,this._name);if(!ts.exists(t))return this._version=0,0;let e=-1;try{let s=ts.readdir(t);for(let r=0;r<s.length;r++){let o=s[r].match(/^version_(\d+)$/);if(o){let i=parseInt(o[1],10);i>e&&(e=i)}}}catch{}return this._version=e+1,this._version}};var xs=class{static{f(this,"Metric")}constructor(){this._computed=null}update(t,e){throw new Error(`${this.constructor.name}.update() not implemented`)}compute(){throw new Error(`${this.constructor.name}.compute() not implemented`)}reset(){this._computed=null}forward(t,e){return this.update(t,e),this._computed=this.compute(),this._computed}get value(){return this._computed}};var Dl=class extends xs{static{f(this,"MeanMetric")}constructor(){super(),this._sum=0,this._count=0}update(t,e=1){let s=typeof t=="number"?t:t.item();this._sum+=s*e,this._count+=e}compute(){return this._count===0?0:this._sum/this._count}reset(){super.reset(),this._sum=0,this._count=0}},Ll=class extends xs{static{f(this,"SumMetric")}constructor(){super(),this._sum=0}update(t){this._sum+=typeof t=="number"?t:t.item()}compute(){return this._sum}reset(){super.reset(),this._sum=0}};var Pl=class{static{f(this,"MetricCollection")}constructor(t={}){this._metrics=new Map;let e=Object.keys(t);for(let s=0;s<e.length;s++)this._metrics.set(e[s],t[e[s]])}add(t,e){return this._metrics.set(t,e),this}update(t,e){for(let[,s]of this._metrics)s.update(t,e)}compute(){let t={};for(let[e,s]of this._metrics)t[e]=s.compute();return t}reset(){for(let[,t]of this._metrics)t.reset()}forward(t,e){return this.update(t,e),this.compute()}get(t){return this._metrics.get(t)}has(t){return this._metrics.has(t)}get size(){return this._metrics.size}[Symbol.iterator](){return this._metrics.entries()}};var zl=class extends xs{static{f(this,"Accuracy")}constructor({task:t="multiclass",numClasses:e=null,topK:s=1,threshold:r=.5}={}){super(),this._task=t,this._numClasses=e,this._topK=s,this._threshold=r,this._correct=0,this._total=0}update(t,e){let s=t._impl.storage.data,r=e._impl.storage.data;this._task==="binary"?this._updateBinary(s,r):this._task==="multiclass"?this._updateMulticlass(t,e):this._task==="multilabel"&&this._updateMultilabel(s,r)}compute(){return this._total===0?0:this._correct/this._total}reset(){super.reset(),this._correct=0,this._total=0}_updateBinary(t,e){let s=e.length;for(let r=0;r<s;r++)(t[r]>=this._threshold?1:0)===e[r]&&this._correct++;this._total+=s}_updateMulticlass(t,e){let s=t.shape;if(s.length<2){let c=t._impl.storage.data,l=e._impl.storage.data,u=l.length;for(let h=0;h<u;h++)Math.round(c[h])===l[h]&&this._correct++;this._total+=u;return}let r=s[0],o=s[1],i=t._impl.storage.data,a=e._impl.storage.data;if(this._topK===1)for(let c=0;c<r;c++){let l=0,u=i[c*o];for(let h=1;h<o;h++){let p=i[c*o+h];p>u&&(u=p,l=h)}l===a[c]&&this._correct++}else for(let c=0;c<r;c++){let l=R2(i,c*o,o,this._topK);for(let u=0;u<l.length;u++)if(l[u]===a[c]){this._correct++;break}}this._total+=r}_updateMultilabel(t,e){let s=e.length;for(let r=0;r<s;r++)(t[r]>=this._threshold?1:0)===e[r]&&this._correct++;this._total+=s}};function R2(n,t,e,s){let r=[];for(let o=0;o<e;o++){let i=n[t+o];r.length<s?(r.push({val:i,idx:o}),r.length===s&&O2(r)):i>r[0].val&&(r[0]={val:i,idx:o},VR(r,0))}return r.map(o=>o.idx)}f(R2,"topKIndices");function O2(n){for(let t=(n.length>>>1)-1;t>=0;t--)VR(n,t)}f(O2,"heapify");function VR(n,t){let e=n.length;for(;;){let s=t,r=2*t+1,o=2*t+2;if(r<e&&n[r].val<n[s].val&&(s=r),o<e&&n[o].val<n[s].val&&(s=o),s===t)break;let i=n[t];n[t]=n[s],n[s]=i,t=s}}f(VR,"siftDown");var $l=class extends xs{static{f(this,"Precision")}constructor({task:t="multiclass",numClasses:e=2,average:s="macro"}={}){super(),this._task=t,this._numClasses=e,this._average=s,this._tp=new Int32Array(e),this._fp=new Int32Array(e),this._support=new Int32Array(e)}update(t,e){Zb(t,e,this._tp,this._fp,null,this._support,this._task,this._numClasses)}compute(){return E2(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)}},Fl=class extends xs{static{f(this,"Recall")}constructor({task:t="multiclass",numClasses:e=2,average:s="macro"}={}){super(),this._task=t,this._numClasses=e,this._average=s,this._tp=new Int32Array(e),this._fn=new Int32Array(e),this._support=new Int32Array(e)}update(t,e){Zb(t,e,this._tp,null,this._fn,this._support,this._task,this._numClasses)}compute(){return T2(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)}},Vl=class extends xs{static{f(this,"F1Score")}constructor({task:t="multiclass",numClasses:e=2,average:s="macro"}={}){super(),this._task=t,this._numClasses=e,this._average=s,this._tp=new Int32Array(e),this._fp=new Int32Array(e),this._fn=new Int32Array(e),this._support=new Int32Array(e)}update(t,e){Zb(t,e,this._tp,this._fp,this._fn,this._support,this._task,this._numClasses)}compute(){let t=GR(this._tp,this._fp,this._numClasses),e=qR(this._tp,this._fn,this._numClasses),s=new Float64Array(this._numClasses);for(let r=0;r<this._numClasses;r++){let o=t[r]+e[r];s[r]=o>0?2*t[r]*e[r]/o:0}return Xb(s,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 Zb(n,t,e,s,r,o,i,a){let c=n._impl.storage.data,l=t._impl.storage.data,u=l.length,h=n.shape.length>=2&&n.shape[1]>1;for(let p=0;p<u;p++){let d=l[p]|0,m;i==="binary"?m=c[p]>=.5?1:0:h?m=N2(c,p,a):m=c[p]|0,o[d]++,m===d?e&&e[d]++:(s&&s[m]++,r&&r[d]++)}}f(Zb,"updateConfusionCounters");function N2(n,t,e){let s=0,r=n[t*e];for(let o=1;o<e;o++){let i=n[t*e+o];i>r&&(r=i,s=o)}return s}f(N2,"argmaxRow");function GR(n,t,e){let s=new Float64Array(e);for(let r=0;r<e;r++){let o=n[r]+t[r];s[r]=o>0?n[r]/o:0}return s}f(GR,"computePrecisionPerClass");function qR(n,t,e){let s=new Float64Array(e);for(let r=0;r<e;r++){let o=n[r]+t[r];s[r]=o>0?n[r]/o:0}return s}f(qR,"computeRecallPerClass");function E2(n,t,e,s,r){let o=GR(n,t,s);return Xb(o,e,s,r)}f(E2,"computePrecision");function T2(n,t,e,s,r){let o=qR(n,t,s);return Xb(o,e,s,r)}f(T2,"computeRecall");function Xb(n,t,e,s){if(s==="none")return[...n];if(s==="micro"){let i=0,a=0;for(let c=0;c<e;c++)i+=n[c]*t[c],a+=t[c];return a>0?i/a:0}if(s==="weighted"){let i=0,a=0;for(let c=0;c<e;c++)a+=n[c]*t[c],i+=t[c];return i>0?a/i:0}let r=0,o=0;for(let i=0;i<e;i++)t[i]>0&&(o+=n[i],r++);return r>0?o/r:0}f(Xb,"aggregate");var Gl=class extends xs{static{f(this,"ConfusionMatrix")}constructor({numClasses:t}){super(),this._numClasses=t,this._matrix=new Int32Array(t*t)}update(t,e){let s=t._impl.storage.data,r=e._impl.storage.data,o=r.length,i=this._numClasses,a=t.shape.length>=2&&t.shape[1]>1;for(let c=0;c<o;c++){let l=r[c]|0,u;a?u=I2(s,c,i):u=s[c]|0,this._matrix[l*i+u]++}}compute(){let t=this._numClasses,e=[];for(let s=0;s<t;s++){let r=new Array(t);for(let o=0;o<t;o++)r[o]=this._matrix[s*t+o];e.push(r)}return e}reset(){super.reset(),this._matrix.fill(0)}};function I2(n,t,e){let s=0,r=n[t*e];for(let o=1;o<e;o++){let i=n[t*e+o];i>r&&(r=i,s=o)}return s}f(I2,"argmaxRow");Mk();Fk();Gk(zt);export{zl as Accuracy,tl as Adam,el as AdamW,Tu as AdaptiveAvgPool2d,Eu as AvgPool2d,Du as BCELoss,_a as BatchNorm1d,Ou as BatchNorm2d,ni as BatchSampler,Gn as CPUTarget,Cl as CSVLogger,zf as CUDATarget,ms as Callback,Gl as ConfusionMatrix,pi as ConsoleLogger,yu as Conv1d,_u as Conv2d,nl as CosineAnnealingLR,Mu as CrossEntropyLoss,Yu as DataLoader,ti as Dataset,Ms as Dropout,ku as ELU,El as EarlyStopping,Lu as Embedding,Jk as F,Vl as F1Score,Uu as Flatten,wu as GELU,Fu as GRU,ya as GRUCell,Ml as GradientAccumulationScheduler,ii as LRScheduler,Vu as LSTM,ba as LSTMCell,Mn as LayerNorm,Bu as LeakyReLU,Tl as LearningRateMonitor,Rl as LightningModule,Ge as Linear,Ru as LogSoftmax,$r as Logger,Iu as MSELoss,Hu as MapDataset,Nu as MaxPool2d,Dl as MeanMetric,xs as Metric,Pl as MetricCollection,ho as ModelCheckpoint,ct as Module,qu as ModuleDict,io as ModuleList,ao as MultiheadAttention,Cu as NLLLoss,nn as Optimizer,Qt as Parameter,ju as PositionalEncoding,$l as Precision,po as ProgressCallback,si as RandomSampler,bu as ReLU,Fl as Recall,rl as ReduceLROnPlateau,Qu as SGD,co as Sampler,Gu as Sequential,ei as SequentialSampler,xu as SiLU,Su as Sigmoid,Au as Softmax,sl as StepLR,Ll as SumMetric,vu as Tanh,zt as Tensor,Ku as TensorDataset,Il as Timer,Xu as Tokenizer,Nl as Trainer,Wu as Transformer,xa as TransformerDecoder,Qo as TransformerDecoderLayer,wa as TransformerEncoder,Xo as TransformerEncoderLayer,uo as Vocab,$f as WasmTarget,Ff as WebGPUTarget,Ji as abs,ut as add,Jb as applyCheckpoint,Dv as arange,M_ as argmax,C_ as argmin,i0 as argsort,Ip as cat,A_ as ceil,N_ as clamp,ol as clipGradNorm_,il as clipGradValue_,P_ as clone,o2 as compile,IR as compileWithBackward,B_ as cos,r0 as cumsum,Xk as data,ri as defaultCollate,Se as div,L_ as dot,ze as empty,RI as emptyLike,Jv as enableGrad,Zi as eq,On as exp,Lv as eye,n0 as flip,k_ as floor,Fo as fromBuffer,Lt as full,EI as fullLike,T_ as gather,Hc as ge,Uc as gelu,Go as gt,qo as index_select,qh as init,Kc as le,UR as lightning,zv as linspace,Yb as loadCheckpoint,Or as log,Yc as log_softmax,O_ as lt,Je as matmul,Er as max,Rr as maximum,Nn as mean,I_ as min,qc as minimum,et as mul,R_ as ne,Rn as neg,Zk as nn,Yv as noGrad,E_ as one_hot,$e as ones,NI as onesLike,uR as optim,Xi as pad,to as pow,D_ as prod,Dp as randn,TI as randnLike,Pv as randperm,Vo as relu,s0 as roll,S_ as rsqrt,w_ as scalar,$u as scan,e0 as scatter,Qi as scatter_add,bs as sigmoid,jc as sign,Wc as silu,v_ as sin,er as softmax,o0 as sort,Nr as sqrt,En as stack,Wt as sub,ie as sum,pn as tanh,tr as tensor,rA as tokenizer,a0 as topk,mR as trace,Es as where,Nt as zeros,OI as zerosLike};
|