@slexisvn/mlfw 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js DELETED
@@ -1,411 +0,0 @@
1
- #!/usr/bin/env node
2
- var _d=Object.defineProperty;var WF=Object.getOwnPropertyDescriptor;var KF=Object.getOwnPropertyNames;var HF=Object.prototype.hasOwnProperty;var c=(e,t)=>_d(e,"name",{value:t,configurable:!0});var I=(e,t)=>()=>(e&&(t=e(e=0)),t);var Xt=(e,t)=>{for(var s in t)_d(e,s,{get:t[s],enumerable:!0})},JF=(e,t,s,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of KF(t))!HF.call(e,r)&&r!==s&&_d(e,r,{get:()=>t[r],enumerable:!(n=WF(t,r))||n.enumerable});return e};var YF=e=>JF(_d({},"__esModule",{value:!0}),e);var EE={};Xt(EE,{ArgKind:()=>Ps,OperatorSchema:()=>bd,SchemaArg:()=>wd,parseSchema:()=>ab});function ab(e,t){let s=t||"mlc",n=e.indexOf("->"),r=n>=0?e.substring(0,n).trim():e.trim(),i=n>=0?e.substring(n+2).trim():"",o=r.indexOf("("),a=r.lastIndexOf(")"),l,u="",f=r.substring(0,o).trim(),h=f.indexOf(".");h>=0?(l=f.substring(0,h),u=f.substring(h+1)):l=f;let p=r.substring(o+1,a).trim(),d=p.length>0?XF(p):[],m=i.length>0?ZF(i):[{kind:Ps.TENSOR}];return new bd(s,l,u,d,m)}function XF(e){return kE(e,",").map(s=>{let n=s.trim(),r=n.indexOf("="),i=n,o=null;r>=0&&(i=n.substring(0,r).trim(),o=n.substring(r+1).trim());let a=i.endsWith("!");a&&(i=i.substring(0,i.length-1).trim());let l=i.lastIndexOf(" "),u,f;l>=0?(u=i.substring(0,l).trim(),f=i.substring(l+1).trim()):(u=i,f="");let h=ob.get(u)||Ps.SCALAR;return new wd(f,h,o,a)})}function ZF(e){let t=e.trim();if(t.startsWith("(")&&t.endsWith(")")){let n=t.substring(1,t.length-1);return kE(n,",").map(i=>{let o=i.trim();return{kind:ob.get(o)||Ps.TENSOR}})}return[{kind:ob.get(t)||Ps.TENSOR}]}function kE(e,t){let s=[],n=0,r=0;for(let i=0;i<e.length;i++){let o=e[i];o==="("||o==="["?n++:o===")"||o==="]"?n--:o===t&&n===0&&(s.push(e.substring(r,i)),r=i+1)}return s.push(e.substring(r)),s}var Ps,QF,wd,bd,ob,lb=I(()=>{Ps=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"}),QF=new Set([Ps.TENSOR,Ps.TENSOR_LIST]),wd=class{static{c(this,"SchemaArg")}constructor(t,s,n,r){this.name=t,this.kind=s,this.defaultValue=n??null,this.isOut=r??!1}get isTensor(){return QF.has(this.kind)}},bd=class{static{c(this,"OperatorSchema")}constructor(t,s,n,r,i){this.namespace=t,this.name=s,this.overload=n||"",this.args=Object.freeze(r),this.returns=Object.freeze(i),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 s=0;s<this.args.length;s++)this.args[s].isTensor&&t.push(s);this._tensorArgIndices=Object.freeze(t)}return this._tensorArgIndices}get numTensorArgs(){return this.tensorArgIndices.length}},ob=new Map([["Tensor",Ps.TENSOR],["Scalar",Ps.SCALAR],["int",Ps.INT],["float",Ps.FLOAT],["bool",Ps.BOOL],["int[]",Ps.INT_LIST],["Tensor[]",Ps.TENSOR_LIST],["Dtype",Ps.DTYPE],["Device",Ps.DEVICE],["str",Ps.STRING],["MemoryFormat",Ps.MEMORY_FORMAT]]);c(ab,"parseSchema");c(XF,"_parseArgs");c(ZF,"_parseReturns");c(kE,"_splitTopLevel")});function NE(e){return e instanceof Jn?e:e&&typeof e=="object"&&e._impl?Jn.tensor(e):typeof e=="number"?Number.isInteger(e)?Jn.int(e):Jn.float(e):typeof e=="boolean"?Jn.bool(e):typeof e=="string"?Jn.string(e):Array.isArray(e)?e.length>0&&e[0]&&e[0]._impl?Jn.tensorList(e):Jn.intList(e):(e==null,Jn.none())}var Ds,Jn,Dn,$f=I(()=>{Ds=Object.freeze({TENSOR:0,INT:1,FLOAT:2,BOOL:3,INT_LIST:4,TENSOR_LIST:5,STRING:6,NONE:7,DEVICE:8,DTYPE:9}),Jn=class e{static{c(this,"IValue")}constructor(t,s){this.tag=t,this.value=s}static tensor(t){return new e(Ds.TENSOR,t)}static int(t){return new e(Ds.INT,t)}static float(t){return new e(Ds.FLOAT,t)}static bool(t){return new e(Ds.BOOL,t)}static intList(t){return new e(Ds.INT_LIST,t)}static tensorList(t){return new e(Ds.TENSOR_LIST,t)}static string(t){return new e(Ds.STRING,t)}static none(){return new e(Ds.NONE,null)}static device(t){return new e(Ds.DEVICE,t)}static dtype(t){return new e(Ds.DTYPE,t)}isTensor(){return this.tag===Ds.TENSOR}isInt(){return this.tag===Ds.INT}isFloat(){return this.tag===Ds.FLOAT}isBool(){return this.tag===Ds.BOOL}isIntList(){return this.tag===Ds.INT_LIST}isTensorList(){return this.tag===Ds.TENSOR_LIST}isString(){return this.tag===Ds.STRING}isNone(){return this.tag===Ds.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}},Dn=class e{static{c(this,"KernelFunction")}constructor(t,s){this._boxed=t||null,this._unboxed=s||null}static fromBoxed(t){return new e(t,null)}static fromUnboxed(t){return new e(null,t)}static fromBoth(t,s){return new e(t,s)}get isBoxed(){return this._boxed!==null}get isUnboxed(){return this._unboxed!==null}callUnboxed(t,...s){return this._unboxed?this._unboxed(t,...s):this._callBoxedAsUnboxed(t,s)}callBoxed(t,s){return this._boxed?this._boxed(t,s):this._callUnboxedAsBoxed(t,s)}_callBoxedAsUnboxed(t,s){let n=s.map(i=>NE(i)),r=this._boxed(t,n);return Array.isArray(r)?r.length===1?r[0].value:r.map(i=>i.value):r instanceof Jn?r.value:r}_callUnboxedAsBoxed(t,s){let n=s.map(i=>i.value),r=this._unboxed(t,...n);return r==null?[]:[NE(r)]}};c(NE,"_toIValue")});function RE(e){qi=e}var qi,zf,cb=I(()=>{lb();$f();qi=null;c(RE,"_setDispatcher");zf=class{static{c(this,"Library")}constructor(t,s){this._namespace=t,this._kind=s,this._registrations=[]}def(t){let s=ab(t,this._namespace);return qi&&qi.registerOp(s),this._registrations.push({type:"def",schema:s}),this}impl(t,s,n){let r=n instanceof Dn?n:Dn.fromUnboxed(n);return qi&&qi.registerKernel(`${this._namespace}::${t}`,s,r),this._registrations.push({type:"impl",name:t,key:s,kernelFn:r}),this}implBoxed(t,s,n){let r=Dn.fromBoxed(n);return qi&&qi.registerKernel(`${this._namespace}::${t}`,s,r),this._registrations.push({type:"impl",name:t,key:s,kernelFn:r}),this}fallback(t,s){let n=s instanceof Dn?s:Dn.fromBoxed(s);return qi&&qi.registerFallback(t,n),this._registrations.push({type:"fallback",key:t,kernelFn:n}),this}replay(t){for(let s of this._registrations)s.type==="def"?t.registerOp(s.schema):s.type==="impl"?t.registerKernel(`${this._namespace}::${s.name}`,s.key,s.kernelFn):s.type==="fallback"&&t.registerFallback(s.key,s.kernelFn)}}});function oc(e){let t=t$[e];if(t===void 0)throw new Error(`No backend key for device: ${e}`);return t}function TE(e){return e$[e]??it.AUTOGRAD}function BE(e){return e=e-(e>>1&1431655765),e=(e&858993459)+(e>>2&858993459),(e+(e>>4)&252645135)*16843009>>24}var it,CE,t$,e$,Yn,Uf,X8,Vf,Z8,Qn=I(()=>{it=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}),CE=new Array(it.NUM_KEYS).fill(null);for(let[e,t]of Object.entries(it))e!=="NUM_KEYS"&&t<it.NUM_KEYS&&(CE[t]=e);t$=Object.freeze({cpu:it.CPU,gpu:it.GPU,wasm:it.WASM,webgpu:it.CUSTOM_0,meta:it.META,lazy:it.LAZY}),e$=Object.freeze({[it.CPU]:it.AUTOGRAD_CPU,[it.GPU]:it.AUTOGRAD_GPU,[it.WASM]:it.AUTOGRAD_WASM});c(oc,"backendKeyForDevice");c(TE,"autogradKeyForBackend");Yn=class e{static{c(this,"DispatchKeySet")}constructor(t,s){this._lo=t|0,this._hi=s|0}static fromKey(t){return t<32?new e(1<<t,0):new e(0,1<<t-32)}static fromKeys(...t){let s=0,n=0;for(let r=0;r<t.length;r++){let i=t[r];i<32?s|=1<<i:n|=1<<i-32}return new e(s,n)}add(t){return t<32?new e(this._lo|1<<t,this._hi):new e(this._lo,this._hi|1<<t-32)}remove(t){return t<32?new e(this._lo&~(1<<t),this._hi):new e(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 e(this._lo|t._lo,this._hi|t._hi)}intersect(t){return new e(this._lo&t._lo,this._hi&t._hi)}subtract(t){return new e(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 BE(this._lo)+BE(this._hi)}*[Symbol.iterator](){let t=this._hi;for(;t!==0;){let n=31-Math.clz32(t);yield n+32,t&=~(1<<n)}let s=this._lo;for(;s!==0;){let n=31-Math.clz32(s);yield n,s&=~(1<<n)}}toString(){let t=[];for(let s of this)t.push(CE[s]||String(s));return`DispatchKeySet(${t.join(", ")})`}};c(BE,"_popcount32");Uf=new Yn(0,0),X8=Yn.fromKeys(it.CPU,it.GPU,it.WASM,it.META,it.LAZY,it.CUSTOM_0,it.CUSTOM_1,it.CUSTOM_2,it.CUSTOM_3),Vf=Yn.fromKeys(it.AUTOGRAD,it.AUTOGRAD_CPU,it.AUTOGRAD_GPU,it.AUTOGRAD_WASM),Z8=Yn.fromKeys(it.BATCHED,it.VMAP,it.FUNCTIONALIZE,it.AUTOCAST,it.AUTOGRAD,it.AUTOGRAD_CPU,it.AUTOGRAD_GPU,it.AUTOGRAD_WASM,it.TRACING)});var xd,IE=I(()=>{Qn();xd=class{static{c(this,"OperatorEntry")}constructor(t){this._schema=t,this._kernels=new Array(it.NUM_KEYS).fill(null),this._catchAll=null}get schema(){return this._schema}registerKernel(t,s){this._kernels[t]=s}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 s of t){let n=this._kernels[s];if(n)return{key:s,kernel:n}}return this._catchAll?{key:-1,kernel:this._catchAll}:null}registeredKeys(){let t=[];for(let s=0;s<this._kernels.length;s++)this._kernels[s]&&t.push(s);return t}}});var Sd,OE=I(()=>{Sd=class{static{c(this,"OperatorHandle")}constructor(t,s){this._entry=t,this._schema=s}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 Ad,ME=I(()=>{Qn();Ad=class{static{c(this,"FallbackTable")}constructor(){this._kernels=new Array(it.NUM_KEYS).fill(null)}register(t,s){this._kernels[t]=s}remove(t){this._kernels[t]=null}lookup(t){return this._kernels[t]}has(t){return this._kernels[t]!==null}registeredKeys(){let t=[];for(let s=0;s<this._kernels.length;s++)this._kernels[s]&&t.push(s);return t}}});function LE(e,t){qa.push(null,e);let s;try{s=t()}catch(n){throw qa.pop(),n}return s&&typeof s.then=="function"?s.then(n=>(qa.pop(),n),n=>{throw qa.pop(),n}):(qa.pop(),s)}var ub,qa,fb=I(()=>{Qn();ub=class{static{c(this,"GuardStack")}constructor(){this._frames=[]}push(t,s){this._frames.push({exclude:t||Uf,include:s||Uf})}pop(){return this._frames.pop()}apply(t){let s=t;for(let n=this._frames.length-1;n>=0;n--){let r=this._frames[n];s=s.subtract(r.exclude),s=s.union(r.include)}return s}get depth(){return this._frames.length}clear(){this._frames.length=0}},qa=new ub;c(LE,"withIncludedKeys")});function pb(e,t){if(!t)return e;if(t.dispatchKeySet)return e.union(t.dispatchKeySet);if(Array.isArray(t))for(let s=0;s<t.length;s++)e=pb(e,t[s]);return e}function db(e,t){let s=Uf;if(t){let n=t.tensorArgIndices;for(let r=0;r<n.length;r++){let i=n[r];i<e.length&&(s=pb(s,e[i]))}}else for(let n=0;n<e.length;n++)s=pb(s,e[n]);return s}var hb,Ue,ja=I(()=>{Qn();IE();OE();ME();fb();cb();hb=class{static{c(this,"Dispatcher")}constructor(){this._entries=new Map,this._handles=new Map,this._fallbacks=new Ad}registerOp(t){let s=t.key();if(this._handles.has(s))return this._handles.get(s);let n=new xd(t);this._entries.set(s,n);let r=new Sd(n,t);return this._handles.set(s,r),r}findOp(t){let s=t.includes("::")?t:`mlc::${t}`;return this._handles.get(s)||null}findOrRegisterOp(t){let s=this.findOp(t);if(!s){let{parseSchema:n}=(lb(),YF(EE)),r=n(`${t}() -> Tensor`,"mlc");s=this.registerOp(r)}return s}registerKernel(t,s,n){let r=t.includes("::")?t:`mlc::${t}`,i=this._entries.get(r);if(!i)throw new Error(`Op '${r}' not registered`);i.registerKernel(s,n)}registerFallback(t,s){this._fallbacks.register(t,s)}dispatch(t,s,...n){let r=qa.apply(s);return this._dispatchInternal(t,r,n)}redispatch(t,s,...n){return this._dispatchInternal(t,s,n)}_dispatchInternal(t,s,n){let r=s.highestPriority();if(r<0)throw new Error(`No dispatch key found for op '${t.name}'`);let i=t.lookupKernel(r);if(i||(i=this._fallbacks.lookup(r)),!i){let a=t.entry.catchAll;a&&(i=a)}if(!i)throw new Error(`No kernel registered for op '${t.name}' with dispatch key ${r}`);let o=s.without(r);return i.callUnboxed(o,...n)}callOp(t,...s){let n=this.findOp(t);if(!n)throw new Error(`Op '${t}' not found`);let r=db(s,n.schema);return this.dispatch(n,r,...s)}listOps(){return[...this._handles.keys()]}hasOp(t){let s=t.includes("::")?t:`mlc::${t}`;return this._handles.has(s)}get fallbacks(){return this._fallbacks}};c(pb,"_unionArg");c(db,"computeKeySet");Ue=new hb;RE(Ue)});function ji(e){let t=PE[e];if(t===void 0)throw new Error(`Unknown dtype: ${e}`);return t}function ss(e){return s$.has(e)}function _b(e){return n$.has(e)}function r$(e,t){return e===t?e:e===1?t:t===1?e:e===X?t===X?X:t:t===X?e:null}function yb(e,t){return e===t?!0:e&&typeof e.equals=="function"?e.equals(t):!1}function Ws(e){if(e instanceof T)return`tensor<${e.shape.map(s=>s===X?"?":String(s)).join("x")}x${e.dtype}>`;if(e instanceof Wa)return`tuple<${e.elements.map(Ws).join(", ")}>`;if(e instanceof mb)return"token";if(e instanceof gb){let t=e.inputs.map(Ws).join(", "),s=e.outputs.map(Ws).join(", ");return`(${t}) -> (${s})`}return"unknown"}var Q,PE,s$,n$,X,Ve,T,Wa,mb,gb,It=I(()=>{Q=Object.freeze({F16:"f16",BF16:"bf16",F32:"f32",F64:"f64",I8:"i8",I16:"i16",I32:"i32",I64:"i64",UI8:"ui8",BOOL:"bool",INDEX:"index"}),PE=Object.freeze({[Q.F16]:2,[Q.BF16]:2,[Q.F32]:4,[Q.F64]:8,[Q.I8]:1,[Q.I16]:2,[Q.I32]:4,[Q.I64]:8,[Q.UI8]:1,[Q.BOOL]:1,[Q.INDEX]:4}),s$=new Set([Q.F16,Q.BF16,Q.F32,Q.F64]),n$=new Set([Q.I8,Q.I16,Q.I32,Q.I64,Q.UI8]),X=-1;c(ji,"scalarBytes");c(ss,"isFloatType");c(_b,"isIntType");Ve=class e{static{c(this,"Layout")}constructor(t){this.order=Object.freeze([...t]),this._hash=null}static rowMajor(t){let s=new Array(t);for(let n=0;n<t;n++)s[n]=n;return new e(s)}static columnMajor(t){let s=new Array(t);for(let n=0;n<t;n++)s[n]=t-1-n;return new e(s)}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 s=0;s<this.order.length;s++)t[this.order[s]]=s;return new e(t)}compose(t){if(this.order.length!==t.order.length)throw new Error("Cannot compose layouts of different ranks");let s=new Array(this.order.length);for(let n=0;n<this.order.length;n++)s[n]=this.order[t.order[n]];return new e(s)}computeStrides(t){let s=t.length,n=new Array(s),r=1;for(let i=s-1;i>=0;i--){let o=this.order[i];n[o]=r,t[o]===X?r=X:r!==X&&(r*=t[o])}return n}equals(t){if(this===t)return!0;if(!(t instanceof e)||this.order.length!==t.order.length)return!1;for(let s=0;s<this.order.length;s++)if(this.order[s]!==t.order[s])return!1;return!0}hash(){if(this._hash!==null)return this._hash;let t=2166136261;for(let s=0;s<this.order.length;s++)t=(t^this.order[s])*16777619&2147483647;return this._hash=t,t}};c(r$,"broadcastDim");T=class e{static{c(this,"TensorType")}constructor(t,s,n=null){this.shape=Object.freeze([...t]),this.dtype=s,this.layout=n||Ve.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===X)}numel(){if(this.hasDynamic)return X;let t=1;for(let s=0;s<this.shape.length;s++)t*=this.shape[s];return t}sizeInBytes(){let t=this.numel();return t===X?X:t*PE[this.dtype]}strides(){return this.layout.computeStrides(this.shape)}withShape(t){return new e(t,this.dtype,this.layout)}withDtype(t){return new e(this.shape,t,this.layout)}withLayout(t){return new e(this.shape,this.dtype,t)}equals(t){if(this===t)return!0;if(!(t instanceof e)||this.dtype!==t.dtype||this.shape.length!==t.shape.length)return!1;for(let s=0;s<this.shape.length;s++)if(this.shape[s]!==t.shape[s])return!1;return this.layout.equals(t.layout)}shapeEquals(t){if(!(t instanceof e)||this.dtype!==t.dtype||this.shape.length!==t.shape.length)return!1;for(let s=0;s<this.shape.length;s++)if(this.shape[s]!==t.shape[s])return!1;return!0}shapeCompatible(t){if(this.shape.length!==t.shape.length)return!1;for(let s=0;s<this.shape.length;s++){let n=this.shape[s],r=t.shape[s];if(!(n===X||r===X)&&n!==r)return!1}return!0}hash(){if(this._hash!==null)return this._hash;let t=2166136261;for(let s=0;s<this.shape.length;s++)t=(t^this.shape[s]&65535)*16777619&2147483647;return t=(t^this.dtype.charCodeAt(0))*16777619&2147483647,this._hash=t,t}static broadcastShape(...t){let s=0;for(let r=0;r<t.length;r++)t[r].length>s&&(s=t[r].length);let n=new Array(s);for(let r=0;r<s;r++){let i=1;for(let o=0;o<t.length;o++){let a=t[o],l=r-(s-a.length);if(!(l<0)&&(i=r$(i,a[l]),i===null))return null}n[r]=i}return n}static broadcastCompatible(...t){return e.broadcastShape(...t)!==null}},Wa=class e{static{c(this,"TupleType")}constructor(t){this.elements=Object.freeze([...t])}equals(t){if(this===t)return!0;if(!(t instanceof e)||this.elements.length!==t.elements.length)return!1;for(let s=0;s<this.elements.length;s++)if(!yb(this.elements[s],t.elements[s]))return!1;return!0}},mb=class e{static{c(this,"TokenType")}equals(t){return t instanceof e}},gb=class e{static{c(this,"FunctionType")}constructor(t,s){this.inputs=Object.freeze([...t]),this.outputs=Object.freeze([...s])}equals(t){if(this===t)return!0;if(!(t instanceof e)||this.inputs.length!==t.inputs.length||this.outputs.length!==t.outputs.length)return!1;for(let s=0;s<this.inputs.length;s++)if(!yb(this.inputs[s],t.inputs[s]))return!1;for(let s=0;s<this.outputs.length;s++)if(!yb(this.outputs[s],t.outputs[s]))return!1;return!0}};c(yb,"typeEquals");c(Ws,"typeToString")});var i$,Gf,Ka,qf,vd=I(()=>{i$=0,Gf=class{static{c(this,"UseLink")}constructor(t,s){this.user=t,this.operandIndex=s,this.prev=null,this.next=null}},Ka=class{static{c(this,"Value")}constructor(t,s=null,n=0){this.type=t,this.definingOp=s,this.resultIndex=n,this.id=i$++,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 s=t.next;yield t,t=s}}getUsers(){let t=[],s=this._useHead;for(;s;)t.push(s.user),s=s.next;return t}replaceAllUsesWith(t){if(this===t)return;let s=this._useHead;for(;s;)s.user.operands[s.operandIndex]=t,s=s.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}},qf=class extends Ka{static{c(this,"BlockArgument")}constructor(t,s,n){super(t,null,0),this.ownerBlock=s,this.argIndex=n}isBlockArgument(){return!0}}});var ns,Se,No=I(()=>{vd();ns=class{static{c(this,"Block")}constructor(t=[]){this.parentRegion=null,this._head=null,this._tail=null,this._size=0,this.arguments=t.map((s,n)=>new qf(s,this,n))}get size(){return this._size}get isEmpty(){return this._head===null}get firstOp(){return this._head}get lastOp(){return this._tail}get parentOp(){return this.parentRegion?this.parentRegion.parentOp:null}getArgument(t){return this.arguments[t]}addArgument(t){let s=new qf(t,this,this.arguments.length);return this.arguments.push(s),s}removeArguments(t){let s=t instanceof Set?t:new Set(t);if(s.size===0)return this;let n=[];for(let r=0;r<this.arguments.length;r++)s.has(r)||n.push(this.arguments[r]);this.arguments=n;for(let r=0;r<n.length;r++)n[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,s){t.parentBlock=this,t._prev=s._prev,t._next=s,s._prev?s._prev._next=t:this._head=t,s._prev=t,this._size++}insertAfter(t,s){t.parentBlock=this,t._prev=s,t._next=s._next,s._next?s._next._prev=t:this._tail=t,s._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 s=t._next;yield t,t=s}}*opsReverse(){let t=this._tail;for(;t;){let s=t._prev;yield t,t=s}}opsArray(){let t=[],s=this._head;for(;s;)t.push(s),s=s._next;return t}[Symbol.iterator](){return this.ops()}},Se=class{static{c(this,"Region")}constructor(t=[]){this.parentOp=null,this.blocks=[];for(let s=0;s<t.length;s++)this.addBlock(t[s])}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,s){s.parentRegion=this,this.blocks.splice(t,0,s)}removeBlock(t){let s=this.blocks.indexOf(t);s!==-1&&(this.blocks.splice(s,1),t.parentRegion=null)}*[Symbol.iterator](){yield*this.blocks}}});var fs,j,Y,kd,He=I(()=>{fs=Object.freeze({NONE:0,READ:1,WRITE:2,ALLOCATE:4,CONTROL:8}),j=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"}),Y=class{static{c(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||fs.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,s){return this.genericAttrs.set(t,s),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(j.COMMUTATIVE)}get isAssociative(){return this.traits.has(j.ASSOCIATIVE)}get isElementwise(){return this.traits.has(j.ELEMENTWISE)}get isTerminator(){return this.traits.has(j.TERMINATOR)}get isConstant(){return this.traits.has(j.CONSTANT)}get isReduction(){return this.traits.has(j.REDUCTION)}get isBroadcast(){return this.traits.has(j.BROADCAST)}get isInjective(){return this.traits.has(j.INJECTIVE)}get isOutEWiseFusable(){return this.traits.has(j.OUT_EWISE_FUSABLE)}get isOpaque(){return this.traits.has(j.OPAQUE)}get hasSideEffects(){return this.sideEffects!==fs.NONE}},kd=class{static{c(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,s,n){let r=this._defs.get(t);if(!r)throw new Error(`registerOpAttr: op '${t}' not registered`);return r.setAttr(s,n),r}registerTrait(t,s){let n=this._defs.get(t);if(!n)throw new Error(`registerTrait: op '${t}' not registered`);return n.addTrait(s),n}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 et,Ed,Nd,Mt,Rd,hs=I(()=>{et=Object.freeze({UNCHANGED:0,CHANGED:1,FAILED:2}),Ed=class{static{c(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,s){throw new Error("Not implemented")}},Nd=class{static{c(this,"PassContext")}constructor({optLevel:t=1/0,disabledPasses:s=[],config:n={}}={}){this.optLevel=t,this.disabledPasses=s instanceof Set?s:new Set(s),this.config=n instanceof Map?n:new Map(Object.entries(n))}shouldRun(t){return!(this.disabledPasses.has(t.name)||(t.optLevel||0)>this.optLevel)}},Mt=class extends Ed{static{c(this,"FunctionPass")}run(t,s){throw new Error("Not implemented")}},Rd=class extends Ed{static{c(this,"ModulePass")}run(t,s){throw new Error("Not implemented")}}});var at,ws,o$,Bd,ps=I(()=>{hs();at=Object.freeze({SILENT:0,INFO:1,VERBOSE:2,DEBUG:3}),ws=class{static{c(this,"CompilationError")}constructor(t,s,n,r){this.phase=t,this.funcName=s,this.message=n,this.passName=r||null}toString(){let t="["+this.phase+"]";return this.funcName&&(t+=" "+this.funcName),this.passName&&(t+=" ("+this.passName+")"),t+=": "+this.message,t}},o$=c(()=>{},"NOOP"),Bd=class{static{c(this,"TraceLog")}constructor(t={}){this.level=t.level??at.SILENT,this.sink=typeof t.sink=="function"?t.sink:o$,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:at.INFO})}phaseEnd(t,s){this.emit({type:"phase",action:"end",phase:t,durationMs:s,level:at.INFO})}passRun(t,s,n,r,i){this.emit({type:"pass",passName:t,changed:s===et.CHANGED,durationMs:n,opCountBefore:r,opCountAfter:i,level:at.VERBOSE})}functionEvent(t,s,n){this.emit({type:"function",phase:t,funcName:s,...n,level:at.INFO})}irDump(t,s){this.emit({type:"ir_snapshot",label:t,text:s,level:at.DEBUG})}memoryStats(t,s){this.emit({type:"memory",funcName:t,...s,level:at.VERBOSE})}autotuneStats(t,s){this.emit({type:"autotune",funcName:t,...s,level:at.VERBOSE})}codegenStats(t,s){this.emit({type:"codegen",funcName:t,...s,level:at.VERBOSE})}errorEvent(t,s,n,r){this.emit({type:"error",phase:t,funcName:s,message:n,passName:r||null,level:at.INFO})}explain(t,s,n,r,i){this.emit({type:"explain",category:t,subject:s,decision:n,reason:r,...i||{},level:at.DEBUG})}get explainsEnabled(){return this.level>=at.DEBUG}shouldSnapshot(t){return this.level>=at.DEBUG&&!!this.irSnapshot[t]}}});var ue,ac,lc,jf=I(()=>{hs();li();ps();ue=class{static{c(this,"Pattern")}constructor(t,s=1){this.name=t,this.benefit=s,this.rootOpName=null}match(t){return!1}rewrite(t,s){return!1}},ac=class{static{c(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 s=this._byOp.get(t.rootOpName);s||(s=[],this._byOp.set(t.rootOpName,s)),s.push(t)}else this._generic.push(t)}_ensureSorted(){if(this._sorted)return;let t=c((s,n)=>n.benefit-s.benefit,"cmp");for(let[,s]of this._byOp)s.sort(t);this._generic.sort(t),this._sorted=!0}get(){return[...this.patterns].sort((t,s)=>s.benefit-t.benefit)}getForOp(t){this._ensureSorted();let s=this._byOp.get(t);if(!s)return this._generic;if(this._generic.length===0)return s;let n=new Array(s.length+this._generic.length),r=0,i=0,o=0;for(;r<s.length&&i<this._generic.length;)s[r].benefit>=this._generic[i].benefit?n[o++]=s[r++]:n[o++]=this._generic[i++];for(;r<s.length;)n[o++]=s[r++];for(;i<this._generic.length;)n[o++]=this._generic[i++];return n}hasPatterns(){return this.patterns.length>0}},lc=class{static{c(this,"PatternApplicator")}constructor(t){this.patternSet=t}applyPatterns(t,s=10,n=null){let r=new bs(t),i=0,o=[...t.opsRecursive()],a=0,l=new Set(o),u=c(d=>{!d||!d.parentBlock||l.has(d)||(l.add(d),o.push(d))},"enqueue"),f=Math.max(s,1)*Math.max(o.length,1)*4+1e3,h=0,p=!1;for(;a<o.length;){if(++h>f){p=!0;break}let d=o[a++];if(l.delete(d),!d.parentBlock)continue;let m=this.patternSet.getForOp(d.opName);for(let g of m){if(!g.match(d))continue;let y=d.parentBlock,_=d._prev,w=d._next,b=[];for(let S=0;S<d.numResults;S++)for(let E of d.getResult(S).getUsers())b.push(E);for(let S=0;S<d.numOperands;S++){let E=d.getOperand(S).definingOp;E&&b.push(E)}if(r.block=y,r.setInsertionPoint(d),!g.rewrite(d,r))continue;i++;for(let S of b)u(S);let x=_?_._next:y._head,A=y._size+2;for(;x&&x!==w&&A-- >0;)u(x),x=x._next;u(d);break}}return n&&(p&&n.level>=at.INFO&&n.emit({type:"pass_detail",passName:"PatternApplicator",message:`pattern rewriting hit safety budget (${f}) without converging`,totalRewrites:i,level:at.INFO}),n.level>=at.DEBUG&&i>0&&n.emit({type:"pass_detail",passName:"PatternApplicator",totalRewrites:i,patternCount:this.patternSet.patterns.length,level:at.DEBUG})),i>0?et.CHANGED:et.UNCHANGED}}});function Pr(e){return(Wf[e]||Td).wasm}function Id(e){return(Wf[e]||Td).load}function $E(e){return(Wf[e]||Td).store}function Ro(e){return(Wf[e]||Td).bytes}function wb(e){return FE[e]||null}function Dr(e,t){let s=FE[e];return s&&s[t]||null}function Bo(e){return DE[e]||a$}function Kf(e){return Bo(e).js}function wr(e){return Bo(e).c}function bb(e){return Bo(e).cPtr}function xb(e){return Bo(e).suffix}function l$(e){return Bo(e).mathSuffix}function Od(e){return Bo(e).bytes}function wn(e){return Bo(e).isFloat}function _n(e){return Bo(e).isInt}function Md(e,t){return e==="sum"||e==="mean"?0:e==="prod"?1:e==="max"?_n(t)?(Cd[t]||Cd.i32)[0]:-1/0:e==="min"?_n(t)?(Cd[t]||Cd.i32)[1]:1/0:0}function Ld(e,t){let s=c$[e];return s?s+l$(t):e}function zE(e){return u$.has(e)}function Ks(e){return(Sb[e]||UE).wgsl}function Fr(e){return(Sb[e]||UE).bytes}function GE(e){return VE[e]||e}function qE(e){return e in VE}function jE(e,t,s){let n=f$[e];if(!n)return null;let r=n[t];return r&&r[s]||null}var DE,Wf,Td,FE,a$,Cd,c$,u$,Sb,UE,VE,f$,bn=I(()=>{DE={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}},Wf={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}},Td=Wf.f32,FE={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"}};c(Pr,"wasmType");c(Id,"wasmLoad");c($E,"wasmStore");c(Ro,"wasmBytes");c(wb,"wasmSimdEntry");c(Dr,"wasmVecOp");a$=DE.f32;c(Bo,"dtypeInfo");c(Kf,"jsTypedArray");c(wr,"cType");c(bb,"cPtrType");c(xb,"cLiteralSuffix");c(l$,"cMathFuncSuffix");c(Od,"dtypeBytes");c(wn,"isDtypeFloat");c(_n,"isDtypeInt");Cd={i8:[-128,127],i16:[-32768,32767],i32:[-2147483648,2147483647],i64:[Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER],ui8:[0,255],index:[-2147483648,2147483647]};c(Md,"reduceInitValue");c$={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"};c(Ld,"cMathFunc");u$=new Set(["exp","log","sqrt","tanh","abs","ceil","floor","sin","cos","max","min","pow","round","sign","log2","log10"]);c(zE,"isJSMathFunc");Sb={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}},UE=Sb.f32;c(Ks,"wgslType");c(Fr,"wgslBytes");VE={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"};c(GE,"wgslMathFunc");c(qE,"hasWgslMathFunc");f$={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"}}};c(jE,"libraryFunc")});function uc(e,t){let s={};return e.match(t,s)?s:null}var Pd,Ab,vb,cc,$r,WE=I(()=>{Pd=class{static{c(this,"DFPattern")}match(t,s){return!1}},Ab=class extends Pd{static{c(this,"AnyPattern")}match(){return!0}},vb=class extends Pd{static{c(this,"OpPattern")}constructor(t,s){super(),this.name=t,this.operandPatterns=s}match(t,s){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 n=0;n<this.operandPatterns.length;n++){let r=t.getOperand(n).definingOp;if(!this.operandPatterns[n].match(r,s))return!1}return!0}},cc=c(()=>new Ab,"wildcard"),$r=c((e,...t)=>new vb(e,t),"isOp");c(uc,"matchPattern")});function ci(e,t){return e&&e.opName==="constant"&&e.getAttr("value")===t}function Eb(e){return new kb(e)}var h$,p$,d$,m$,g$,Dd,fc,Fd,$d,kb,hc,pc,dc,mc,gc,yc,_c,zd,Ud,Vd,Gd,qd,jd,Wd,Kd,Hd,Jd,wc=I(()=>{jf();It();bn();WE();h$=$r("transpose",$r("transpose",cc())),p$=$r("reshape",$r("reshape",cc())),d$=$r("neg",$r("neg",cc())),m$=$r("exp",$r("log",cc())),g$=$r("log",$r("exp",cc()));c(ci,"isConstantVal");Dd=class extends ue{static{c(this,"FoldTrivialReshape")}constructor(){super("fold_trivial_reshape",10),this.rootOpName="reshape"}match(t){let s=t.getOperand(0).type,n=t.getResult(0).type;return s instanceof T&&n instanceof T&&s.shapeEquals(n)}rewrite(t,s){return t.replaceAllResultsWith([t.getOperand(0)]),t.erase(),!0}},fc=class extends ue{static{c(this,"ReshapeReshape")}constructor(){super("reshape_reshape",10),this.rootOpName="reshape"}match(t){return uc(p$,t)!==null}rewrite(t,s){let n=t.getOperand(0).definingOp.getOperand(0),r=t.getAttr("new_shape"),i=s.reshape(n,r);return t.replaceAllResultsWith([i.getResult(0)]),t.erase(),!0}},Fd=class extends ue{static{c(this,"FoldTrivialTranspose")}constructor(){super("fold_trivial_transpose",10),this.rootOpName="transpose"}match(t){let s=t.getAttr("permutation");if(!s)return!1;for(let n=0;n<s.length;n++)if(s[n]!==n)return!1;return!0}rewrite(t,s){return t.replaceAllResultsWith([t.getOperand(0)]),t.erase(),!0}},$d=class extends ue{static{c(this,"TransposeTranspose")}constructor(){super("transpose_transpose",10),this.rootOpName="transpose"}match(t){return uc(h$,t)!==null}rewrite(t,s){let n=t.getOperand(0).definingOp.getOperand(0),r=t.getOperand(0).definingOp.getAttr("permutation"),i=t.getAttr("permutation"),o=new Array(i.length);for(let l=0;l<i.length;l++)o[l]=r[i[l]];let a=s.transpose(n,o);return t.replaceAllResultsWith([a.getResult(0)]),t.erase(),!0}},kb=class extends ue{static{c(this,"CommutativeConstantRight")}constructor(t=null){super(`commutative_constant_right${t?"_"+t:""}`,5),this.rootOpName=t}match(t){if(t.numOperands!==2)return!1;let s=t.getOperand(0).definingOp,n=t.getOperand(1).definingOp;return s&&s.opName==="constant"&&!(n&&n.opName==="constant")}rewrite(t,s){let n=t.getOperand(0),r=t.getOperand(1);return t.replaceOperand(0,r),t.replaceOperand(1,n),!0}};c(Eb,"commutativeConstantRightFor");hc=class extends ue{static{c(this,"AddZero")}constructor(){super("add_zero",5),this.rootOpName="add"}match(t){return ci(t.getOperand(1).definingOp,0)||ci(t.getOperand(0).definingOp,0)}rewrite(t,s){let n=ci(t.getOperand(1).definingOp,0)?t.getOperand(0):t.getOperand(1);return n.type.equals(t.getResult(0).type)?(t.replaceAllResultsWith([n]),t.erase(),!0):!1}},pc=class extends ue{static{c(this,"SubZero")}constructor(){super("sub_zero",5),this.rootOpName="sub"}match(t){return ci(t.getOperand(1).definingOp,0)}rewrite(t,s){let n=t.getOperand(0);return n.type.equals(t.getResult(0).type)?(t.replaceAllResultsWith([n]),t.erase(),!0):!1}},dc=class extends ue{static{c(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:_n(t.getResult(0).type.dtype)||this.fastMath}rewrite(t,s){let r=s.scalarConstant(0,t.getResult(0).type.dtype).getResult(0),i=t.getResult(0).type.shape;return i.length>0&&(r=s.broadcast(r,i,[]).getResult(0)),t.replaceAllResultsWith([r]),t.erase(),!0}},mc=class extends ue{static{c(this,"MulOne")}constructor(){super("mul_one",5),this.rootOpName="mul"}match(t){return ci(t.getOperand(1).definingOp,1)||ci(t.getOperand(0).definingOp,1)}rewrite(t,s){let n=ci(t.getOperand(1).definingOp,1)?t.getOperand(0):t.getOperand(1);return n.type.equals(t.getResult(0).type)?(t.replaceAllResultsWith([n]),t.erase(),!0):!1}},gc=class extends ue{static{c(this,"MulZero")}constructor(t=!1){super("mul_zero",5),this.rootOpName="mul",this.fastMath=t}match(t){return!_n(t.getResult(0).type.dtype)&&!this.fastMath?!1:ci(t.getOperand(1).definingOp,0)||ci(t.getOperand(0).definingOp,0)}rewrite(t,s){let r=s.scalarConstant(0,t.getResult(0).type.dtype).getResult(0),i=t.getResult(0).type.shape;return i.length>0&&(r=s.broadcast(r,i,[]).getResult(0)),t.replaceAllResultsWith([r]),t.erase(),!0}},yc=class extends ue{static{c(this,"DivOne")}constructor(){super("div_one",5),this.rootOpName="div"}match(t){return ci(t.getOperand(1).definingOp,1)}rewrite(t,s){let n=t.getOperand(0);return n.type.equals(t.getResult(0).type)?(t.replaceAllResultsWith([n]),t.erase(),!0):!1}},_c=class extends ue{static{c(this,"DoubleNeg")}constructor(){super("double_neg",5),this.rootOpName="neg"}match(t){return uc(d$,t)!==null}rewrite(t,s){let n=t.getOperand(0).definingOp.getOperand(0);return t.replaceAllResultsWith([n]),t.erase(),!0}},zd=class extends ue{static{c(this,"ExpLog")}constructor(t=!1){super("exp_log",5),this.rootOpName="exp",this.fastMath=t}match(t){return this.fastMath&&uc(m$,t)!==null}rewrite(t,s){return t.replaceAllResultsWith([t.getOperand(0).definingOp.getOperand(0)]),t.erase(),!0}},Ud=class extends ue{static{c(this,"LogExp")}constructor(t=!1){super("log_exp",5),this.rootOpName="log",this.fastMath=t}match(t){return this.fastMath&&uc(g$,t)!==null}rewrite(t,s){return t.replaceAllResultsWith([t.getOperand(0).definingOp.getOperand(0)]),t.erase(),!0}},Vd=class extends ue{static{c(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,s){let r=s.scalarConstant(1,t.getResult(0).type.dtype).getResult(0),i=t.getResult(0).type.shape;return i.length>0&&(r=s.broadcast(r,i,[]).getResult(0)),t.replaceAllResultsWith([r]),t.erase(),!0}},Gd=class extends ue{static{c(this,"MulNegNeg")}constructor(){super("mul_neg_neg",4),this.rootOpName="mul"}match(t){let s=t.getOperand(0).definingOp,n=t.getOperand(1).definingOp;return s&&s.opName==="neg"&&n&&n.opName==="neg"}rewrite(t,s){let n=t.getOperand(0).definingOp.getOperand(0),r=t.getOperand(1).definingOp.getOperand(0),i=s.mul(n,r);return t.replaceAllResultsWith([i.getResult(0)]),t.erase(),!0}},qd=class extends ue{static{c(this,"AddNegToSub")}constructor(){super("add_neg_to_sub",4),this.rootOpName="add"}match(t){let s=t.getOperand(1).definingOp;return s&&s.opName==="neg"}rewrite(t,s){let n=t.getOperand(0),r=t.getOperand(1).definingOp.getOperand(0),i=s.sub(n,r);return t.replaceAllResultsWith([i.getResult(0)]),t.erase(),!0}},jd=class extends ue{static{c(this,"SubNegToAdd")}constructor(){super("sub_neg_to_add",4),this.rootOpName="sub"}match(t){let s=t.getOperand(1).definingOp;return s&&s.opName==="neg"}rewrite(t,s){let n=t.getOperand(0),r=t.getOperand(1).definingOp.getOperand(0),i=s.add(n,r);return t.replaceAllResultsWith([i.getResult(0)]),t.erase(),!0}},Wd=class extends ue{static{c(this,"DoubleConvert")}constructor(){super("double_convert",6),this.rootOpName="convert"}match(t){let s=t.getOperand(0).definingOp;if(!s||s.opName!=="convert")return!1;let n=s.getOperand(0).type.dtype,r=t.getAttr("target_dtype");return n===r}rewrite(t,s){return t.replaceAllResultsWith([t.getOperand(0).definingOp.getOperand(0)]),t.erase(),!0}},Kd=class extends ue{static{c(this,"LayoutTransformIdentity")}constructor(){super("layout_transform_identity",10),this.rootOpName="layout_transform"}match(t){let s=t.getAttr("src_layout"),n=t.getAttr("dst_layout");if(!s||!n||s.length!==n.length)return!1;for(let r=0;r<s.length;r++)if(s[r]!==n[r])return!1;return!0}rewrite(t,s){return t.replaceAllResultsWith([t.getOperand(0)]),t.erase(),!0}},Hd=class extends ue{static{c(this,"FoldTransposeIntoDot")}constructor(){super("fold_transpose_into_dot",10),this.rootOpName="dot"}match(t){for(let s=0;s<2;s++){let n=t.getOperand(s).definingOp;if(!n||n.opName!=="transpose")continue;let r=n.getAttr("permutation");if(!(!r||r.length!==2)&&!(r[0]!==1||r[1]!==0))return!0}return!1}rewrite(t,s){let n=[t.getOperand(0),t.getOperand(1)],r=[...t.getAttr("lhs_contracting")],i=[...t.getAttr("rhs_contracting")],o=[...t.getAttr("lhs_batch")||[]],a=[...t.getAttr("rhs_batch")||[]];for(let u=0;u<2;u++){let f=n[u].definingOp;if(!f||f.opName!=="transpose")continue;let h=f.getAttr("permutation");if(!h||h.length!==2||h[0]!==1||h[1]!==0)continue;n[u]=f.getOperand(0);let p=u===0?r:i,d=u===0?o: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 l=s.dot(n[0],n[1],r,i);return o.length>0&&l.setAttr("lhs_batch",o),a.length>0&&l.setAttr("rhs_batch",a),t.replaceAllResultsWith([l.getResult(0)]),t.erase(),!0}},Jd=class extends ue{static{c(this,"LayoutTransformCompose")}constructor(){super("layout_transform_compose",10),this.rootOpName="layout_transform"}match(t){let s=t.getOperand(0).definingOp;return s&&s.opName==="layout_transform"}rewrite(t,s){let n=t.getOperand(0).definingOp,r=n.getAttr("src_layout"),i=n.getAttr("dst_layout"),o=t.getAttr("dst_layout"),a=new Array(r.length);for(let f=0;f<o.length;f++)a[f]=r[i.indexOf(o[f])];let l=n.getOperand(0),u=s._inferAndBuild("layout_transform",[l],{src_layout:r,dst_layout:a});return t.replaceAllResultsWith([u.getResult(0)]),t.erase(),!0}}});function Ha(e){if(e.length!==2)return null;let t=e[0],s=e[1];if(!(t instanceof T)||!(s instanceof T)||t.dtype!==s.dtype)return null;let n=T.broadcastShape(t.shape,s.shape);return n?[new T(n,t.dtype)]:null}function Yd(e){if(e.length!==1)return null;let t=e[0];return t instanceof T?[new T(t.shape,t.dtype)]:null}function KE(e){if(e.length!==2)return null;let t=e[0],s=e[1];if(!(t instanceof T)||!(s instanceof T))return null;let n=T.broadcastShape(t.shape,s.shape);return n?[new T(n,Q.BOOL)]:null}function Nb(e){if(e.length!==1)return null;let t=e[0];return!(t instanceof T)||!ss(t.dtype)?null:[new T(t.shape,t.dtype)]}function Ja(e){let t=[];if(e.numOperands!==2)return t.push(`${e.opName} expects 2 operands, got ${e.numOperands}`),t;let s=e.getOperand(0).type,n=e.getOperand(1).type;return s instanceof T||t.push(`${e.opName} operand 0 is not tensor`),n instanceof T||t.push(`${e.opName} operand 1 is not tensor`),s instanceof T&&n instanceof T&&s.dtype!==n.dtype&&t.push(`${e.opName} dtype mismatch: ${s.dtype} vs ${n.dtype}`),t}function Jf(e){let t=[];return e.numOperands!==1?(t.push(`${e.opName} expects 1 operand, got ${e.numOperands}`),t):(e.getOperand(0).type instanceof T||t.push(`${e.opName} operand is not tensor`),t)}function Rb(e){let t=Jf(e);if(t.length===0){let s=e.getOperand(0).type.dtype;ss(s)||t.push(`${e.opName} requires float type, got ${s}`)}return t}var bc,Bb,Qd=I(()=>{It();He();c(Ha,"inferBinaryElementwise");c(Yd,"inferUnaryElementwise");c(KE,"inferCompare");c(Nb,"inferUnaryFloat");c(Ja,"verifyBinaryElementwise");c(Jf,"verifyUnaryElementwise");c(Rb,"verifyUnaryFloat");bc=[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],Bb=[...bc,j.COMMUTATIVE,j.ASSOCIATIVE]});function Xd(e){return t=>{if(!(typeof t[0]!="number"||typeof t[1]!="number"))return e(t[0],t[1])}}function y$(e){return t=>typeof t[0]=="number"?e(t[0]):void 0}function HE(e){e.register(new Y({name:"add",numOperands:2,numResults:1,traits:Bb,inferResultTypes:Ha,verify:Ja,getCanonicalizationPatterns(){return[Eb("add"),new hc]},fold:Xd((t,s)=>t+s)})),e.register(new Y({name:"mul",numOperands:2,numResults:1,traits:Bb,inferResultTypes:Ha,verify:Ja,getCanonicalizationPatterns(){return[Eb("mul"),new mc,new gc]},fold:Xd((t,s)=>t*s)})),e.register(new Y({name:"sub",numOperands:2,numResults:1,traits:bc,inferResultTypes:Ha,verify:Ja,getCanonicalizationPatterns(){return[new pc,new dc]},fold:Xd((t,s)=>t-s)})),e.register(new Y({name:"div",numOperands:2,numResults:1,traits:bc,inferResultTypes:Ha,verify:Ja,getCanonicalizationPatterns(){return[new yc]},fold:Xd((t,s)=>t/s)}));for(let t of["rem","pow"])e.register(new Y({name:t,numOperands:2,numResults:1,traits:bc,inferResultTypes:Ha,verify:Ja}));e.register(new Y({name:"neg",numOperands:1,numResults:1,traits:[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Yd,verify:Jf,getCanonicalizationPatterns(){return[new _c]},fold:y$(t=>-t)}));for(let t of["maximum","minimum"])e.register(new Y({name:t,numOperands:2,numResults:1,traits:[...bc,j.COMMUTATIVE],inferResultTypes:Ha,verify:Ja}))}var JE=I(()=>{He();wc();Qd();c(Xd,"scalarBinaryFold");c(y$,"scalarUnaryFold");c(HE,"register")});function YE(e){for(let t of["abs","floor","ceil","round","sign","square","reciprocal"])e.register(new Y({name:t,numOperands:1,numResults:1,traits:[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Yd,verify:Jf}));e.register(new Y({name:"exp",numOperands:1,numResults:1,traits:[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Nb,fold(t){return typeof t[0]=="number"?Math.exp(t[0]):void 0},verify:Rb}));for(let t of["log","sqrt","rsqrt","tanh","sin","cos","erf","log2","log10","exp2"])e.register(new Y({name:t,numOperands:1,numResults:1,traits:[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Nb,verify:Rb}))}var QE=I(()=>{He();Qd();c(YE,"register")});function _$(e){if(e.length!==1)return null;let t=e[0];return!(t instanceof T)||t.dtype!==Q.BOOL?null:[new T(t.shape,Q.BOOL)]}function w$(e){if(e.length!==2)return null;let[t,s]=e;if(!(t instanceof T)||t.dtype!==Q.BOOL||!(s instanceof T)||s.dtype!==Q.BOOL)return null;let n=T.broadcastShape(t.shape,s.shape);return n?[new T(n,Q.BOOL)]:null}function XE(e){e.register(new Y({name:"compare",numOperands:2,numResults:1,attrs:[{name:"direction",type:"string",required:!0}],traits:[j.ELEMENTWISE],inferResultTypes:KE,verify(t){let s=[];if(t.numOperands!==2)return s.push("compare expects 2 operands"),s;if(!t.hasAttr("direction"))s.push("compare missing direction attr");else{let n=t.getAttr("direction");b$.has(n)||s.push(`compare invalid direction: ${n}`)}return s}})),e.register(new Y({name:"select",numOperands:3,numResults:1,traits:[j.ELEMENTWISE],inferResultTypes(t){if(t.length!==3)return null;let[s,n,r]=t;if(!(s instanceof T)||s.dtype!==Q.BOOL||!(n instanceof T)||!(r instanceof T)||n.dtype!==r.dtype)return null;let i=T.broadcastShape(s.shape,n.shape,r.shape);return i?[new T(i,n.dtype)]:null}})),e.register(new Y({name:"where",numOperands:3,numResults:1,traits:[j.ELEMENTWISE],inferResultTypes(t){if(t.length!==3)return null;let[s,n,r]=t;if(!(s instanceof T)||!(n instanceof T)||!(r instanceof T)||n.dtype!==r.dtype)return null;let i=T.broadcastShape(s.shape,n.shape,r.shape);return i?[new T(i,n.dtype)]:null}})),e.register(new Y({name:"logical_not",numOperands:1,numResults:1,traits:[j.ELEMENTWISE],inferResultTypes:_$}));for(let t of["logical_and","logical_or"])e.register(new Y({name:t,numOperands:2,numResults:1,traits:[j.ELEMENTWISE],inferResultTypes:w$}));e.register(new Y({name:"clamp",numOperands:3,numResults:1,traits:[j.ELEMENTWISE],inferResultTypes(t){if(t.length!==3)return null;let s=t[1];return s instanceof T?[new T(s.shape,s.dtype)]:null}}))}var b$,ZE=I(()=>{He();It();Qd();c(_$,"inferUnaryBool");c(w$,"inferBinaryBool");b$=new Set(["eq","ne","lt","le","gt","ge"]);c(XE,"register")});function tN(e){e.register(new Y({name:"stop_gradient",numOperands:1,numResults:1,traits:[j.VIEW],inferResultTypes(t){return t.length!==1?null:[t[0]]},propagateSymbolicShapes(t,s){let n=s.get(t.getOperand(0));return n?[n]:null}})),e.register(new Y({name:"reverse",numOperands:1,numResults:1,traits:[j.INJECTIVE],attrs:[{name:"dimensions",type:"array",required:!0}],inferResultTypes(t){return t.length!==1?null:[t[0]]},propagateSymbolicShapes(t,s){let n=s.get(t.getOperand(0));return n?[n]:null}})),e.register(new Y({name:"broadcast_in_dim",numOperands:1,numResults:1,traits:[j.BROADCAST],attrs:[{name:"broadcast_dimensions",type:"array",required:!0},{name:"result_shape",type:"array",required:!0}],inferResultTypes(t,s){if(t.length!==1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("result_shape"):s.result_shape;return r?[new T(r,n.dtype)]:null},propagateSymbolicShapes(t,s){let n=s.get(t.getOperand(0));if(!n)return null;let r=t.getAttr("broadcast_dimensions"),i=t.getAttr("result_shape");if(!r||!i)return null;let o=i.map(a=>a===X?null:a);for(let a=0;a<r.length;a++)(typeof n[a]!="number"||o[r[a]]===null)&&(o[r[a]]=n[a]);return[o]},fold(t){if(t.length===1&&typeof t[0]=="number")return t[0]},verify(t){let s=[];if(t.hasAttr("broadcast_dimensions")||s.push("broadcast_in_dim missing broadcast_dimensions"),t.hasAttr("result_shape")||s.push("broadcast_in_dim missing result_shape"),t.numOperands!==1&&s.push("broadcast_in_dim expects 1 operand"),s.length===0){let n=t.getAttr("broadcast_dimensions"),r=t.getAttr("result_shape"),i=t.getOperand(0).type;n.length!==i.rank&&s.push(`broadcast_dimensions length ${n.length} != input rank ${i.rank}`);for(let a=0;a<n.length;a++)n[a]<0||n[a]>=r.length?s.push(`broadcast_dimensions[${a}]=${n[a]} out of range for result rank ${r.length}`):i instanceof T&&i.shape[a]!==X&&i.shape[a]!==1&&r[n[a]]!==X&&i.shape[a]!==r[n[a]]&&s.push(`broadcast_in_dim: input dim ${a} size ${i.shape[a]} incompatible with result dim ${n[a]} size ${r[n[a]]}`);let o=new Set;for(let a of n)o.has(a)&&s.push(`broadcast_dimensions has duplicate: ${a}`),o.add(a)}return s}})),e.register(new Y({name:"reshape",numOperands:1,numResults:1,traits:[j.VIEW],attrs:[{name:"new_shape",type:"array",required:!0}],inferResultTypes(t,s){if(t.length!==1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("new_shape"):s.new_shape;return r?[new T(r,n.dtype)]:null},verify(t){let s=[];if(t.hasAttr("new_shape")||s.push("reshape missing new_shape"),t.numOperands!==1&&s.push("reshape expects 1 operand"),s.length===0){let n=t.getOperand(0).type,r=t.getAttr("new_shape");if(n instanceof T&&n.isFullyStatic){let i=r.filter(o=>o===X).length;if(i>1&&s.push("reshape can have at most one dynamic dimension"),i===0){let o=n.numel(),a=r.reduce((l,u)=>l*u,1);o!==a&&s.push(`reshape numel mismatch: input ${o} vs output ${a}`)}}}return s},propagateSymbolicShapes(t,s){let n=s.get(t.getOperand(0));if(!n)return null;let r=t.getAttr("new_shape"),i=[];for(let o of r)if(o===-1){let a=n.find(l=>typeof l!="number");i.push(a||-1)}else i.push(o);return[i]},getCanonicalizationPatterns(){return[new Dd,new fc]},fold(t){return t[0]}})),e.register(new Y({name:"transpose",numOperands:1,numResults:1,traits:[j.VIEW],attrs:[{name:"permutation",type:"array",required:!0}],inferResultTypes(t,s){if(t.length!==1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("permutation"):s.permutation;if(!r)return null;let i=r.map(o=>n.shape[o]);return[new T(i,n.dtype)]},propagateSymbolicShapes(t,s){let n=s.get(t.getOperand(0));if(!n)return null;let r=t.getAttr("permutation");return r?[r.map(i=>n[i])]:null},getCanonicalizationPatterns(){return[new Fd]},fold(t,s,n){let r=t[0];if(r==null||typeof r=="number"||typeof r.length!="number")return;let i=s.get?s.get("permutation"):s.permutation;if(!i)return;let o=n&&n[0]?n[0].getResult(0).type:null,a=o instanceof T?o.shape:null;if(!a||a.length!==i.length)return;let l=new Array(a.length),u=1;for(let m=a.length-1;m>=0;m--)l[m]=u,u*=a[m];let f=i.map(m=>a[m]),h=r.length,p=new Array(h),d=new Array(i.length).fill(0);for(let m=0;m<h;m++){let g=0;for(let y=0;y<i.length;y++)g+=d[y]*l[i[y]];p[m]=r[g];for(let y=i.length-1;y>=0&&!(++d[y]<f[y]);y--)d[y]=0}return p},verify(t){let s=[];if(!t.hasAttr("permutation"))return s.push("transpose missing permutation"),s;if(t.numOperands!==1)return s.push("transpose expects 1 operand"),s;let n=t.getAttr("permutation"),r=t.getOperand(0).type;r instanceof T&&n.length!==r.rank&&s.push(`transpose permutation length ${n.length} != input rank ${r.rank}`);let i=new Set;for(let o of n)i.has(o)&&s.push(`transpose duplicate in permutation: ${o}`),i.add(o);return s}})),e.register(new Y({name:"slice",numOperands:1,numResults:1,traits:[j.VIEW],attrs:[{name:"starts",type:"array",required:!0},{name:"limits",type:"array",required:!0},{name:"strides",type:"array",required:!1}],inferResultTypes(t,s){if(t.length!==1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("starts"):s.starts,i=s.get?s.get("limits"):s.limits,o=(s.get?s.get("strides"):s.strides)||r.map(()=>1),a=[];for(let l=0;l<r.length;l++)a.push(Math.ceil((i[l]-r[l])/o[l]));return[new T(a,n.dtype)]}})),e.register(new Y({name:"concat",numOperands:-1,numResults:1,attrs:[{name:"dimension",type:"number",required:!0}],inferResultTypes(t,s){if(t.length<1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("dimension"):s.dimension;if(r===void 0)return null;let i=[...n.shape];for(let o=1;o<t.length;o++){let a=t[o];if(!(a instanceof T)||a.dtype!==n.dtype||a.rank!==n.rank)return null;i[r]===X||a.shape[r]===X?i[r]=X:i[r]+=a.shape[r]}return[new T(i,n.dtype)]}})),e.register(new Y({name:"pad",numOperands:2,numResults:1,traits:[j.INJECTIVE],attrs:[{name:"low",type:"array",required:!0},{name:"high",type:"array",required:!0},{name:"interior",type:"array",required:!1}],inferResultTypes(t,s){if(t.length<1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("low"):s.low,i=s.get?s.get("high"):s.high,o=(s.get?s.get("interior"):s.interior)||r.map(()=>0),a=[];for(let l=0;l<n.rank;l++)n.shape[l]===X?a.push(X):a.push(r[l]+n.shape[l]+(n.shape[l]-1)*o[l]+i[l]);return[new T(a,n.dtype)]}})),e.register(new Y({name:"gather",numOperands:2,numResults:1,traits:[j.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,s){if(t.length!==2)return null;let n=t[0],r=t[1];if(!(n instanceof T)||!(r instanceof T))return null;let i=s.get?s.get("offset_dims"):s.offset_dims,o=new Set(s.get?s.get("collapsed_slice_dims"):s.collapsed_slice_dims),a=s.get?s.get("slice_sizes"):s.slice_sizes,l=s.get?s.get("index_vector_dim"):s.index_vector_dim,u=[];for(let y=0;y<r.rank;y++)y!==l&&u.push(r.shape[y]);let f=[];for(let y=0;y<a.length;y++)o.has(y)||f.push(a[y]);let h=new Set(i),p=[],d=0,m=0,g=u.length+f.length;for(let y=0;y<g;y++)h.has(y)?p.push(f[m++]):p.push(u[d++]);return[new T(p,n.dtype)]}})),e.register(new Y({name:"scatter",numOperands:3,numResults:1,traits:[j.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 s=t[0];return s instanceof T?[new T(s.shape,s.dtype)]:null}})),e.register(new Y({name:"split",numOperands:1,numResults:-1,attrs:[{name:"dimension",type:"number",required:!0},{name:"split_sizes",type:"array",required:!0}],inferResultTypes(t,s){if(t.length<1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("dimension"):s.dimension,i=s.get?s.get("split_sizes"):s.split_sizes;return r===void 0||!i?null:i.map(o=>{let a=[...n.shape];return a[r]=o,new T(a,n.dtype)})}}))}var eN=I(()=>{He();It();wc();c(tN,"register")});function sN(e){e.register(new Y({name:"reduce",numOperands:2,numResults:1,attrs:[{name:"dimensions",type:"array",required:!0},{name:"reduce_type",type:"string",required:!0}],traits:[j.REDUCTION],hasRegions:!0,numRegions:1,inferResultTypes(s,n){if(s.length<1)return null;let r=s[0];if(!(r instanceof T))return null;let i=n.get?n.get("dimensions"):n.dimensions;if(!i)return null;let o=new Set(i),a=[];for(let l=0;l<r.rank;l++)o.has(l)||a.push(r.shape[l]);return[new T(a,r.dtype)]},propagateSymbolicShapes(s,n){let r=n.get(s.getOperand(0));if(!r)return null;let i=s.getAttr("dimensions");if(!i)return null;let o=new Set(i),a=[];for(let l=0;l<r.length;l++)o.has(l)||a.push(r[l]);return[a]},verify(s){let n=[];if(s.hasAttr("dimensions")||n.push("reduce missing dimensions"),!s.hasAttr("reduce_type"))n.push("reduce missing reduce_type");else{let r=s.getAttr("reduce_type");x$.has(r)||n.push(`reduce invalid reduce_type: ${r}`)}return n}}));function t(s,n){if(s.length<1)return null;let r=s[0];if(!(r instanceof T))return null;let i=n.get?n.get("axis"):n.axis;if(i===void 0)return null;let o=(n.get?n.get("keep_dims"):n.keep_dims)||!1,a=[];for(let l=0;l<r.rank;l++)l===i?o&&a.push(1):a.push(r.shape[l]);return[new T(a,Q.I32)]}c(t,"inferArgReduceTypes"),e.register(new Y({name:"argmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"keep_dims",type:"boolean",required:!1}],traits:[j.REDUCTION],inferResultTypes:t})),e.register(new Y({name:"argmin",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"keep_dims",type:"boolean",required:!1}],traits:[j.REDUCTION],inferResultTypes:t}))}var x$,nN=I(()=>{He();It();x$=new Set(["sum","max","min","prod","mean","and","or"]);c(sN,"register")});function rN(e){e.register(new Y({name:"dot",numOperands:2,numResults:1,traits:[j.OPAQUE,j.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 s=t.getOperand(0).type,n=t.getOperand(1).type;if(!(s instanceof T)||!(n instanceof T))return 0;let r=t.getAttr("lhs_contracting")||[],i=1;for(let l of r)s.shape[l]!==X&&(i*=s.shape[l]);let o=t.getResult(0).type;if(!(o instanceof T))return 0;let a=o.numel();return a===X?0:2*a*i},inferResultTypes:Zd,getCanonicalizationPatterns(){return[new Hd]},verify(t){let s=[];if(t.numOperands!==2)return s.push("dot expects 2 operands"),s;t.hasAttr("lhs_contracting")||s.push("dot missing lhs_contracting"),t.hasAttr("rhs_contracting")||s.push("dot missing rhs_contracting");let n=t.getOperand(0).type,r=t.getOperand(1).type;if(n instanceof T&&r instanceof T){n.dtype!==r.dtype&&s.push(`dot dtype mismatch: ${n.dtype} vs ${r.dtype}`);let i=t.getAttr("lhs_contracting")||[],o=t.getAttr("rhs_contracting")||[];if(i.length!==o.length)s.push(`dot contracting dimensions count mismatch: lhs ${i.length} vs rhs ${o.length}`);else for(let a=0;a<i.length;a++){let l=i[a],u=o[a];l>=n.rank&&s.push(`dot lhs_contracting[${a}]=${l} out of range (rank ${n.rank})`),u>=r.rank&&s.push(`dot rhs_contracting[${a}]=${u} out of range (rank ${r.rank})`),l<n.rank&&u<r.rank&&n.shape[l]!==X&&r.shape[u]!==X&&n.shape[l]!==r.shape[u]&&s.push(`dot contracting dim size mismatch at [${a}]: lhs dim ${l} size ${n.shape[l]} vs rhs dim ${u} size ${r.shape[u]}`)}}return s}})),e.register(new Y({name:"cublas_gemm",numOperands:2,numResults:1,traits:[j.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:Zd,getFlops(t){let s=t.getOperand(0).type,n=t.getResult(0).type;if(!(s instanceof T)||!(n instanceof T))return 0;let r=1;for(let o of t.getAttr("lhs_contracting")||[])s.shape[o]!==X&&(r*=s.shape[o]);let i=n.numel();return i===X?0:2*i*r},verify(t){let s=[];return t.numOperands!==2?(s.push("cublas_gemm expects 2 operands"),s):(t.hasAttr("lhs_contracting")||s.push("cublas_gemm missing lhs_contracting"),t.hasAttr("rhs_contracting")||s.push("cublas_gemm missing rhs_contracting"),s)}})),e.register(new Y({name:"conv",numOperands:2,numResults:1,traits:[j.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 s=t.getResult(0).type,n=t.getOperand(1).type;if(!(s instanceof T)||!(n instanceof T))return 0;let r=s.numel(),i=n.numel();return r===X||i===X?0:2*r*i/(n.shape[0]||1)},inferResultTypes:Cb,verify(t){let s=[];return t.numOperands!==2?(s.push("conv expects 2 operands"),s):(t.hasAttr("strides")||s.push("conv missing strides"),t.hasAttr("padding")||s.push("conv missing padding"),t.hasAttr("input_layout")||s.push("conv missing input_layout"),t.hasAttr("kernel_layout")||s.push("conv missing kernel_layout"),s)}}))}function Zd(e,t,s){if(e.length!==2)return null;let n=e[0],r=e[1];if(!(n instanceof T)||!(r instanceof T))return null;let i=s&&!Array.isArray(s)?s:{};if(!i.allowMixedDtype&&n.dtype!==r.dtype)return null;let o=new Set(t.get?t.get("lhs_contracting"):t.lhs_contracting),a=new Set(t.get?t.get("rhs_contracting"):t.rhs_contracting),l=new Set((t.get?t.get("lhs_batch"):t.lhs_batch)||[]),u=new Set((t.get?t.get("rhs_batch"):t.rhs_batch)||[]),f=[];for(let h=0;h<n.rank;h++)l.has(h)&&f.push(n.shape[h]);for(let h=0;h<n.rank;h++)!o.has(h)&&!l.has(h)&&f.push(n.shape[h]);for(let h=0;h<r.rank;h++)!a.has(h)&&!u.has(h)&&f.push(r.shape[h]);return[new T(f,i.outputDtype||n.dtype)]}function Cb(e,t,s){if(e.length!==2)return null;let n=e[0],r=e[1];if(!(n instanceof T)||!(r instanceof T))return null;let i=s&&!Array.isArray(s)?s:{};if(!i.allowMixedDtype&&n.dtype!==r.dtype)return null;let o=t.get?t.get("strides"):t.strides,a=t.get?t.get("padding"):t.padding,l=(t.get?t.get("dilation"):t.dilation)||o.map(()=>1),u=o.length,f=n.shape[0],h=r.shape[0],p=[];for(let d=0;d<u;d++){let m=n.shape[d+2],g=r.shape[d+2],y=a[d][0]+a[d][1];if(m===X||g===X)p.push(X);else{let _=(g-1)*l[d]+1;p.push(Math.floor((m+y-_)/o[d])+1)}}return[new T([f,h,...p],i.outputDtype||n.dtype)]}var Tb=I(()=>{He();It();wc();c(rN,"register");c(Zd,"inferDotResultTypes");c(Cb,"inferConvResultTypes")});function iN(e){e.register(new Y({name:"constant",numOperands:0,numResults:1,attrs:[{name:"value",type:"any",required:!0},{name:"tensor_type",type:"object",required:!0}],traits:[j.CONSTANT],inferResultTypes(t,s){let n=s.get?s.get("tensor_type"):s.tensor_type;return n?[n]:null}})),e.register(new Y({name:"iota",numOperands:0,numResults:1,attrs:[{name:"iota_dimension",type:"number",required:!0},{name:"tensor_type",type:"object",required:!0}],inferResultTypes(t,s){let n=s.get?s.get("tensor_type"):s.tensor_type;return n?[n]:null}})),e.register(new Y({name:"tuple",numOperands:-1,numResults:1,inferResultTypes(t){return[new Wa(t)]}})),e.register(new Y({name:"get_tuple_element",numOperands:1,numResults:1,attrs:[{name:"index",type:"number",required:!0}],inferResultTypes(t,s){if(t.length!==1)return null;let n=t[0];if(!n||!n.elements)return null;let r=s.get?s.get("index"):s.index;return r===void 0||r<0||r>=n.elements.length?null:[n.elements[r]]}})),e.register(new Y({name:"convert",numOperands:1,numResults:1,attrs:[{name:"target_dtype",type:"string",required:!0}],traits:[j.ELEMENTWISE],inferResultTypes(t,s){if(t.length!==1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("target_dtype"):s.target_dtype;return[new T(n.shape,r)]}})),e.register(new Y({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,s){if(t.length<1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("depth"):s.depth,i=(s.get?s.get("axis"):s.axis)??-1,o=(s.get?s.get("dtype"):s.dtype)||Q.F32,a=[...n.shape],l=i<0?a.length+1+i:i;return a.splice(l,0,r),[new T(a,o)]}}))}var oN=I(()=>{He();It();c(iN,"register")});function aN(e){e.register(new Y({name:"return",numOperands:-1,numResults:0,traits:[j.TERMINATOR]})),e.register(new Y({name:"yield",numOperands:-1,numResults:0,traits:[j.TERMINATOR]})),e.register(new Y({name:"if",numOperands:1,numResults:-1,hasRegions:!0,numRegions:2,sideEffects:fs.CONTROL,inferResultTypes(t,s,n){return n||null}})),e.register(new Y({name:"while",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:2,sideEffects:fs.CONTROL,inferResultTypes(t){return[...t]}})),e.register(new Y({name:"scan",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:1,sideEffects:fs.CONTROL,attrs:[{name:"num_carry",type:"number",required:!0},{name:"num_xs",type:"number",required:!0}],inferResultTypes(t,s,n){return n||null}})),e.register(new Y({name:"custom_call",numOperands:-1,numResults:-1,attrs:[{name:"call_target_name",type:"string",required:!0},{name:"backend_config",type:"any",required:!1}],sideEffects:fs.WRITE,inferResultTypes(t,s,n){return n||null}})),e.register(new Y({name:"fused_dot_epilogue",numOperands:-1,numResults:1,traits:[j.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,s,n){return n||null}})),e.register(new Y({name:"fusion",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:1,attrs:[{name:"fusion_kind",type:"string",required:!1}],inferResultTypes(t,s,n){return n||null}}))}var lN=I(()=>{He();c(aN,"register")});function cN(e){e.register(new Y({name:"layout_transform",numOperands:1,numResults:1,traits:[j.VIEW],attrs:[{name:"src_layout",type:"array",required:!0},{name:"dst_layout",type:"array",required:!0}],inferResultTypes(t,s){if(t.length!==1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("dst_layout"):s.dst_layout;return r?[new T(n.shape,n.dtype,new Ve(r))]:null},verify(t){let s=[];if(t.numOperands!==1)return s.push("layout_transform expects 1 operand"),s;if(t.hasAttr("src_layout")||s.push("layout_transform missing src_layout"),t.hasAttr("dst_layout")||s.push("layout_transform missing dst_layout"),s.length===0){let n=t.getOperand(0).type,r=t.getAttr("src_layout"),i=t.getAttr("dst_layout");n instanceof T&&(r.length!==n.rank&&s.push(`src_layout length ${r.length} != input rank ${n.rank}`),i.length!==n.rank&&s.push(`dst_layout length ${i.length} != input rank ${n.rank}`))}return s},getCanonicalizationPatterns(){return[new Kd,new Jd]}}))}var uN=I(()=>{He();It();wc();c(cN,"register")});function S$(e,t){let s=e.getAttr("scale"),n=t.getAttr("scale"),r=e.getAttr("zero_point"),i=t.getAttr("zero_point"),o=e.getAttr("scheme"),a=t.getAttr("scheme");if(o!==a)return!1;if(typeof s=="number"&&typeof n=="number"){if(s!==n)return!1}else return!1;if(typeof r=="number"&&typeof i=="number"){if(r!==i)return!1}else return!1;return!0}var tm,fN=I(()=>{jf();It();c(S$,"quantParamsMatch");tm=class extends ue{static{c(this,"QuantizeDequantizeIdentity")}constructor(){super("quantize_dequantize_identity",20),this.rootOpName="quantize"}match(t){let s=t.getOperand(0).definingOp;return!s||s.opName!=="dequantize"?!1:S$(t,s)}rewrite(t,s){let n=t.getOperand(0).definingOp.getOperand(0);if(!n.type||!t.getResult(0).type)return!1;let r=n.type,i=t.getResult(0).type;return r instanceof T&&i instanceof T&&r.dtype===i.dtype?(t.replaceAllResultsWith([n]),t.erase(),!0):!1}}});function hN(e){e.register(new Y({name:"quantize",numOperands:1,numResults:1,traits:[j.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,s){if(t.length!==1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("target_dtype"):s.target_dtype;return r?[new T(n.shape,r)]:null},verify(t){let s=[];if(t.numOperands!==1)return s.push("quantize expects 1 operand"),s;let n=t.getOperand(0).type;n instanceof T&&!ss(n.dtype)&&s.push(`quantize input must be float, got ${n.dtype}`);let r=t.getAttr("target_dtype");r&&!Ib.has(r)&&s.push(`quantize target_dtype must be i8 or ui8, got ${r}`);let i=t.getAttr("scale");return typeof i=="number"&&i<=0&&s.push("quantize scale must be positive"),s},fold(t,s){if(typeof t[0]!="number")return;let n=s.get?s.get("scale"):s.scale,r=s.get?s.get("zero_point"):s.zero_point;if(!(typeof n!="number"||typeof r!="number"))return Math.round(t[0]/n+r)},getCanonicalizationPatterns(){return[new tm]}})),e.register(new Y({name:"dequantize",numOperands:1,numResults:1,traits:[j.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,s){if(t.length!==1)return null;let n=t[0];if(!(n instanceof T))return null;let r=s.get?s.get("target_dtype"):s.target_dtype;return r?[new T(n.shape,r)]:null},verify(t){let s=[];if(t.numOperands!==1)return s.push("dequantize expects 1 operand"),s;let n=t.getOperand(0).type;n instanceof T&&!v$.has(n.dtype)&&s.push("dequantize input must be i8, ui8, or i32, got "+n.dtype);let r=t.getAttr("target_dtype");return r&&!ss(r)&&s.push(`dequantize target_dtype must be float, got ${r}`),s},fold(t,s){if(typeof t[0]!="number")return;let n=s.get?s.get("scale"):s.scale,r=s.get?s.get("zero_point"):s.zero_point;if(!(typeof n!="number"||typeof r!="number"))return(t[0]-r)*n}})),e.register(new Y({name:"quantized_dot",numOperands:2,numResults:1,traits:[j.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 s=t.getOperand(0).type,n=t.getOperand(1).type;if(!(s instanceof T)||!(n instanceof T))return 0;let r=t.getAttr("lhs_contracting")||[],i=1;for(let l of r)s.shape[l]!==X&&(i*=s.shape[l]);let o=t.getResult(0).type;if(!(o instanceof T))return 0;let a=o.numel();return a===X?0:2*a*i},inferResultTypes(t,s){return Zd(t,s,{outputDtype:Q.I32,allowMixedDtype:!0})},verify(t){let s=[];if(t.numOperands!==2)return s.push("quantized_dot expects 2 operands"),s;t.hasAttr("lhs_contracting")||s.push("quantized_dot missing lhs_contracting"),t.hasAttr("rhs_contracting")||s.push("quantized_dot missing rhs_contracting"),t.hasAttr("lhs_scale")||s.push("quantized_dot missing lhs_scale"),t.hasAttr("rhs_scale")||s.push("quantized_dot missing rhs_scale");let n=t.getOperand(0).type,r=t.getOperand(1).type;return n instanceof T&&!Ib.has(n.dtype)&&s.push(`quantized_dot lhs must be i8/ui8, got ${n.dtype}`),r instanceof T&&!Ib.has(r.dtype)&&s.push(`quantized_dot rhs must be i8/ui8, got ${r.dtype}`),s}})),e.register(new Y({name:"quantized_conv",numOperands:2,numResults:1,traits:[j.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,s){return Cb(t,s,{outputDtype:Q.I32,allowMixedDtype:!0})},verify(t){let s=[];return t.numOperands!==2?(s.push("quantized_conv expects 2 operands"),s):(t.hasAttr("strides")||s.push("quantized_conv missing strides"),t.hasAttr("padding")||s.push("quantized_conv missing padding"),t.hasAttr("input_layout")||s.push("quantized_conv missing input_layout"),t.hasAttr("kernel_layout")||s.push("quantized_conv missing kernel_layout"),t.hasAttr("input_scale")||s.push("quantized_conv missing input_scale"),t.hasAttr("kernel_scale")||s.push("quantized_conv missing kernel_scale"),s)}}))}var Ib,v$,pN=I(()=>{He();It();fN();Tb();Ib=new Set([Q.I8,Q.UI8]),v$=new Set([Q.I8,Q.UI8,Q.I32]);c(hN,"register")});function k$(e){if(e.length<1)return null;let t=e[0];return t instanceof T?[new T(t.shape,t.dtype)]:null}function zr(e){if(e.length<1)return null;let t=e[0];return!(t instanceof T)||!ss(t.dtype)?null:[new T(t.shape,t.dtype)]}function Wi(e){let t=[];if(e.numOperands<1)return t.push(`${e.opName} expects at least 1 operand`),t;let s=e.getOperand(0).type;return s instanceof T&&!ss(s.dtype)&&t.push(`${e.opName} requires float input, got ${s.dtype}`),t}function dN(e){e.register(new Y({name:"all_reduce",numOperands:1,numResults:1,traits:[j.OPAQUE],attrs:[{name:"reduce_op",type:"string",required:!1},{name:"mesh_axis",type:"number",required:!1}],inferResultTypes:k$})),e.register(new Y({name:"all_gather",numOperands:1,numResults:1,traits:[j.OPAQUE],attrs:[{name:"mesh_axis",type:"number",required:!1},{name:"gather_dim",type:"number",required:!1}],inferResultTypes(t,s){let n=t[0];if(!(n instanceof T))return null;let r=s&&s.has("mesh_axis")?s.get("mesh_axis"):0,i=s&&s.has("gather_dim")?s.get("gather_dim"):1,o=[...n.shape];return o[i]=o[i]*o[r],[new T(o,n.dtype)]}})),e.register(new Y({name:"scaled_dot_product_attention",numOperands:3,numResults:1,traits:[j.OPAQUE],attrs:[{name:"scale",type:"number",required:!0},{name:"causal",type:"boolean",required:!1}],inferResultTypes(t){let s=t[0],n=t[2];return!(s instanceof T)||!(n instanceof T)?null:[new T([...s.shape.slice(0,s.rank-1),n.shape[n.rank-1]],s.dtype)]}})),e.register(new Y({name:"softmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0}],inferResultTypes:zr,verify:Wi})),e.register(new Y({name:"log_softmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0}],inferResultTypes:zr,verify:Wi})),e.register(new Y({name:"gelu",numOperands:1,numResults:1,traits:[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:zr,verify:Wi})),e.register(new Y({name:"sigmoid",numOperands:1,numResults:1,traits:[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:zr,verify:Wi})),e.register(new Y({name:"silu",numOperands:1,numResults:1,traits:[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:zr,verify:Wi})),e.register(new Y({name:"layer_norm",numOperands:3,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"epsilon",type:"number",required:!0}],inferResultTypes:zr,verify(t){let s=[];if(t.numOperands!==3)return s.push("layer_norm expects 3 operands (input, gamma, beta)"),s;let n=t.getOperand(0).type;return n instanceof T&&!ss(n.dtype)&&s.push(`layer_norm requires float input, got ${n.dtype}`),s}})),e.register(new Y({name:"batch_norm",numOperands:5,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"epsilon",type:"number",required:!0}],inferResultTypes:zr,verify(t){let s=[];if(t.numOperands!==5)return s.push("batch_norm expects 5 operands (input, gamma, beta, mean, var)"),s;let n=t.getOperand(0).type;return n instanceof T&&!ss(n.dtype)&&s.push(`batch_norm requires float input, got ${n.dtype}`),s}}));for(let t of["selu","mish","hardswish","hardsigmoid"])e.register(new Y({name:t,numOperands:1,numResults:1,traits:[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:zr,verify:Wi}));e.register(new Y({name:"elu",numOperands:1,numResults:1,attrs:[{name:"alpha",type:"number"}],traits:[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:zr,verify:Wi})),e.register(new Y({name:"leaky_relu",numOperands:1,numResults:1,attrs:[{name:"negative_slope",type:"number"}],traits:[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:zr,verify:Wi})),e.register(new Y({name:"celu",numOperands:1,numResults:1,attrs:[{name:"alpha",type:"number"}],traits:[j.ELEMENTWISE,j.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:zr,verify:Wi})),e.register(new Y({name:"embedding",numOperands:2,numResults:1,inferResultTypes(t){if(t.length<2)return null;let s=t[0],n=t[1];if(!(s instanceof T)||!(n instanceof T))return null;let r=[...n.shape,s.shape[s.rank-1]];return[new T(r,s.dtype)]}}))}var mN=I(()=>{He();It();c(k$,"inferSameAsInput");c(zr,"inferSameAsInputFloat");c(Wi,"verifyUnaryFloat");c(dN,"register")});function gN(e){e.register(new Y({name:"copy_to_device",numOperands:1,numResults:1,attrs:[{name:"src_device",type:"string",required:!0},{name:"dst_device",type:"string",required:!0}],sideEffects:fs.READ|fs.WRITE,traits:[j.INJECTIVE],inferResultTypes(t){return t.length!==1?null:[t[0]]},getFlops(){return 0}}))}var yN=I(()=>{He();c(gN,"register")});function _N(e,t,s,n,r,i){let o=e+n+r-t;return(i?Math.ceil(o/s):Math.floor(o/s))+1}function wN(e){e.register(new Y({name:"pool2d",numOperands:1,numResults:1,traits:[j.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,s){if(t.length<1)return null;let n=t[0];if(!(n instanceof T)||n.rank!==4)return null;let r=s.get?s.get("kernel_size"):s.kernel_size,i=s.get?s.get("strides"):s.strides,o=s.get?s.get("padding"):s.padding,a=(s.get?s.get("ceil_mode"):s.ceil_mode)||!1,l=_N(n.shape[2],r[0],i[0],o[0][0],o[0][1],a),u=_N(n.shape[3],r[1],i[1],o[1][0],o[1][1],a);return[new T([n.shape[0],n.shape[1],l,u],n.dtype)]}}))}var bN=I(()=>{He();It();c(_N,"computePoolOutputDim");c(wN,"register")});function xN(e){e.register(new Y({name:"resize",numOperands:1,numResults:1,traits:[j.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,s){if(t.length<1)return null;let n=t[0];if(!(n instanceof T)||n.rank!==4)return null;let r=s.get?s.get("output_size"):s.output_size;return[new T([n.shape[0],n.shape[1],r[0],r[1]],n.dtype)]}}))}var SN=I(()=>{He();It();c(xN,"register")});function N$(e=E$){let t=new kd;for(let s of e)s(t);return t}var E$,rt,rs=I(()=>{He();JE();QE();ZE();eN();nN();Tb();oN();lN();uN();pN();mN();yN();bN();SN();E$=[HE,YE,XE,tN,sN,rN,iN,aN,cN,hN,dN,gN,wN,xN];c(N$,"buildRegistry");rt=N$()});function AN(e){if(typeof e=="number")return e*2654435761&2147483647;if(typeof e=="string"){let t=0;for(let s=0;s<e.length;s++)t=(t<<5)-t+e.charCodeAt(s)&2147483647;return t}if(typeof e=="boolean")return e?1:0;if(Array.isArray(e)){let t=2654435769;for(let s=0;s<e.length;s++)t=(t^AN(e[s]))*16777619&2147483647;return t}if(ArrayBuffer.isView(e)&&e.buffer instanceof ArrayBuffer){let t=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),s=2654435769;s=(s^t.length)*16777619&2147483647;let n=t.length>256?Math.ceil(t.length/256):1;for(let r=0;r<t.length;r+=n)s=(s^t[r])*16777619&2147483647;return s}return typeof e=="object"&&e!==null&&typeof e.hash=="function"?e.hash():0}function vN(e,t){if(e===t)return!0;if(typeof e!=typeof t)return!1;if(Array.isArray(e)){if(!Array.isArray(t)||e.length!==t.length)return!1;for(let s=0;s<e.length;s++)if(!vN(e[s],t[s]))return!1;return!0}return typeof e=="object"&&e!==null&&typeof e.equals=="function"?e.equals(t):!1}function ui(e,t=new Map){let s=new Se;for(let n of e.blocks){let r=n.arguments.map(o=>o.type),i=new n.constructor(r);for(let o=0;o<n.arguments.length;o++)t.set(n.arguments[o],i.arguments[o]);for(let o of n)i.pushOp(o.clone(t));s.addBlock(i)}return s}var R$,bt,xn=I(()=>{vd();No();rs();R$=0,bt=class e{static{c(this,"Operation")}constructor(t,s,n,r=null,i=null){if(this.opName=t,this.id=R$++,this.parentBlock=null,this._prev=null,this._next=null,this.attributes=new Map,r)if(r instanceof Map)for(let[o,a]of r)this.attributes.set(o,a);else for(let o of Object.keys(r))this.attributes.set(o,r[o]);this.operands=new Array(s.length),this._operandLinks=new Array(s.length);for(let o=0;o<s.length;o++){this.operands[o]=s[o];let a=new Gf(this,o);s[o].addUse(a),this._operandLinks[o]=a}this.results=new Array(n.length);for(let o=0;o<n.length;o++)this.results[o]=new Ka(n[o],this,o);if(this.regions=[],i)for(let o=0;o<i.length;o++){let a=i[o]instanceof Se?i[o]:new Se;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,s){this.attributes.set(t,s)}removeAttr(t){return this.attributes.delete(t)}replaceOperand(t,s){let n=this.operands[t];if(n===s)return;n.removeUse(this._operandLinks[t]),this.operands[t]=s;let r=new Gf(this,t);s.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 s=0;s<this.results.length;s++)this.results[s].replaceAllUsesWith(t[s])}isTerminator(){let t=rt.get(this.opName);return!!(t&&t.isTerminator)}hasSideEffects(){let t=rt.get(this.opName);return!!(t&&t.hasSideEffects)}getParentFunction(){let t=this.parentBlock;for(;t;){let s=t.parentOp;if(!s)return t._parentFunction||null;t=s.parentBlock}return null}clone(t=new Map){let s=this.operands.map(i=>t.get(i)||i),n=this.regions.map(i=>ui(i,t)),r=new e(this.opName,s,this.results.map(i=>i.type),new Map(this.attributes),n);for(let i=0;i<this.results.length;i++)t.set(this.results[i],r.results[i]);return r}structuralHash(){let t=2166136261;for(let s=0;s<this.opName.length;s++)t=(t^this.opName.charCodeAt(s))*16777619&2147483647;for(let s=0;s<this.operands.length;s++)t=(t^this.operands[s].id)*16777619&2147483647;for(let[s,n]of this.attributes){for(let r=0;r<s.length;r++)t=(t^s.charCodeAt(r))*16777619&2147483647;t=(t^AN(n))*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 s=0;s<this.operands.length;s++)if(this.operands[s]!==t.operands[s])return!1;for(let[s,n]of this.attributes)if(!t.attributes.has(s)||!vN(n,t.attributes.get(s)))return!1;for(let s=0;s<this.results.length;s++)if(!this.results[s].type.equals(t.results[s].type))return!1;return!0}};c(AN,"hashAttrValue");c(vN,"attrValueEquals");c(ui,"cloneRegion")});function*kN(e){if(!(!e.regions||e.regions.length===0))for(let t of e.regions)for(let s of t.blocks)for(let n of s.ops())yield n,yield*kN(n)}function*EN(e){if(!(!e.regions||e.regions.length===0))for(let t of e.regions)for(let s of t.blocks){yield s;for(let n of s.ops())yield*EN(n)}}var Fs,Co=I(()=>{No();c(kN,"opsInRegions");c(EN,"blocksInRegions");Fs=class{static{c(this,"GraphFunction")}constructor(t,s,n){this.name=t,this.inputTypes=Object.freeze([...s]),this.outputTypes=Object.freeze([...n]),this.body=new Se;let r=new ns(s);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*kN(t)}*blocksRecursive(){for(let t of this.body){yield t;for(let s of t)yield*EN(s)}}opsArray(){let t=[];for(let s of this.body)for(let n of s)t.push(n);return t}numOps(){let t=0;for(let s of this.body)t+=s.size;return t}findOp(t){for(let s of this.ops())if(t(s))return s;return null}findOps(t){let s=[];for(let n of this.ops())t(n)&&s.push(n);return s}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 s=this.getReturnOp();return s?s.numOperands!==this.outputTypes.length&&t.push(`Return has ${s.numOperands} operands but function declares ${this.outputTypes.length} outputs`):t.push("Function body has no return op"),t}}});var cn,Ya=I(()=>{cn=class{static{c(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 s=this._functions.delete(t);return s&&this._version++,s}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 s of this._functions.values()){let n=s.verify();for(let r=0;r<n.length;r++)t.push(`${s.name}: ${n[r]}`)}return t}}});function NN(e){return e?e instanceof Wa?`tuple(${e.types.map(NN).join(", ")})`:e.shape!==void 0&&e.dtype!==void 0?`[${e.shape.join(",")}]:${e.dtype}`:String(e):"?"}function em(e,t,s){let n=e.rank,r=t<0?n+t:t,i=e.shape.map((a,l)=>l===r?1:a),o=[];for(let a=0;a<n;a++)a!==r&&o.push(a<r?a:a-1+s);return{offsetDims:o,collapsedSliceDims:[r],startIndexMap:[r],indexVectorDim:s,sliceSizes:i}}function B$(e,t){let s=Math.max(e.length,t.length),n=new Array(s);for(let r=0;r<s;r++){let i=r<e.length?e[e.length-1-r]:1,o=r<t.length?t[t.length-1-r]:1;n[s-1-r]=i===1?o:i}return n}function sm(e,t){let s=[];for(let n=0;n<e;n++)n!==t&&s.push(n);return s}function C$(e){if(!e)return new Map;if(e instanceof Map)return e;let t=new Map;for(let s of Object.keys(e))t.set(s,e[s]);return t}function Yf(e,t,s,n){let r=new Fs(e,t,s),i=new bs(r);return n(i,r.args),r}var bs,li=I(()=>{It();xn();No();Co();Ya();rs();c(NN,"describeType");c(em,"indexSelectGatherOpts");c(B$,"bcastBatchDims");bs=class e{static{c(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,s,n,r=null,i=null){let o=new bt(t,s,n,r,i);return this._insert(o)}_inferAndBuild(t,s,n=null,r=null,i=null){let o=rt.get(t),a=i;if(!a&&o&&o.inferResultTypes){let l=s.map(f=>f.type),u=n instanceof Map?n:C$(n);a=o.inferResultTypes(l,u,i)}if(!a){let l=s.map(f=>NN(f.type)).join(", "),u=o?o.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}' (${u}); operands: [${l}]`)}return this._buildOp(t,s,a,n,r)}constant(t,s){return this._buildOp("constant",[],[s],{value:t,tensor_type:s})}scalarConstant(t,s=Q.F32){let n=new T([],s);return this.constant(t,n)}tensorConstant(t,s,n=Q.F32){let r=new T(s,n);return this.constant(t,r)}iota(t,s){return this._buildOp("iota",[],[s],{iota_dimension:t,tensor_type:s})}add(t,s){return this._inferAndBuild("add",[t,s])}sub(t,s){return this._inferAndBuild("sub",[t,s])}mul(t,s){return this._inferAndBuild("mul",[t,s])}div(t,s){return this._inferAndBuild("div",[t,s])}rem(t,s){return this._inferAndBuild("rem",[t,s])}pow(t,s){return this._inferAndBuild("pow",[t,s])}maximum(t,s){return this._inferAndBuild("maximum",[t,s])}minimum(t,s){return this._inferAndBuild("minimum",[t,s])}neg(t){return this._inferAndBuild("neg",[t])}stopGradient(t){return this._inferAndBuild("stop_gradient",[t])}reverse(t,s){return this._inferAndBuild("reverse",[t],{dimensions:s})}scaledDotProductAttention(t,s,n,r,i=!1){return this._inferAndBuild("scaled_dot_product_attention",[t,s,n],{scale:r,causal:i})}allReduce(t,s={}){return this._inferAndBuild("all_reduce",[t],{reduce_op:s.reduceOp||"sum",mesh_axis:s.meshAxis??0})}allGather(t,s={}){return this._inferAndBuild("all_gather",[t],{mesh_axis:s.meshAxis??0,gather_dim:s.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,s){return this._inferAndBuild("logical_and",[t,s])}logicalOr(t,s){return this._inferAndBuild("logical_or",[t,s])}compare(t,s,n){return this._inferAndBuild("compare",[t,s],{direction:n})}select(t,s,n){return this._inferAndBuild("select",[t,s,n])}clamp(t,s,n){return this._inferAndBuild("clamp",[t,s,n])}broadcast(t,s,n){return this._inferAndBuild("broadcast_in_dim",[t],{result_shape:s,broadcast_dimensions:n})}reshape(t,s){return this._inferAndBuild("reshape",[t],{new_shape:s})}transpose(t,s){return this._inferAndBuild("transpose",[t],{permutation:s})}slice(t,s,n,r=null){let i={starts:s,limits:n};return r&&(i.strides=r),this._inferAndBuild("slice",[t],i)}concat(t,s){return this._inferAndBuild("concat",t,{dimension:s})}pad(t,s,n,r,i=null){let o={low:n,high:r};return i&&(o.interior=i),this._inferAndBuild("pad",[t,s],o)}reduce(t,s,n,r){let i=new T([],t.type.dtype),o=new Se,a=new ns([i,i]);return o.addBlock(a),this._inferAndBuild("reduce",[t,s],{dimensions:n,reduce_type:r},[o])}dot(t,s,n,r,i=[],o=[]){return this._inferAndBuild("dot",[t,s],{lhs_contracting:n,rhs_contracting:r,lhs_batch:i,rhs_batch:o})}matmul(t,s){let n=t.type.rank,r=s.type.rank;if(n===1&&r===1)return this.dot(t,s,[0],[0]);let i=t,o=s,a=!1,l=!1;n===1&&(i=this.reshape(t,[1,t.type.shape[0]]).getResult(0),a=!0),r===1&&(o=this.reshape(s,[s.type.shape[0],1]).getResult(0),l=!0);let u=i.type.rank,f=o.type.rank,h=o.type.shape.slice(0,f-2),p;if(h.length===0)p=this.dot(i,o,[u-1],[0]);else{let g=i.type.shape.slice(0,u-2),y=B$(g,h),_=y.length,w=this._broadcastBatch(i,g,y),b=this._broadcastBatch(o,h,y),x=Array.from({length:_},(A,S)=>S);p=this.dot(w,b,[_+1],[_],x,x)}if(!a&&!l)return p;let d=p.getResult(0).type.shape,m=new Set;return l&&m.add(d.length-1),a&&m.add(d.length-2),this.reshape(p.getResult(0),d.filter((g,y)=>!m.has(y)))}_broadcastBatch(t,s,n){let r=t.type.shape.slice(t.type.rank-2),i=[...n,...r];if(s.length===n.length&&s.every((l,u)=>l===n[u]))return t;let o=n.length-s.length,a=[];for(let l=0;l<s.length;l++)a.push(o+l);return a.push(i.length-2,i.length-1),this.broadcast(t,i,a).getResult(0)}conv(t,s,n,r,i={}){return this._inferAndBuild("conv",[t,s],{strides:n,padding:r,dilation:i.dilation||n.map(()=>1),groups:i.groups||1,input_layout:i.inputLayout||"NCHW",kernel_layout:i.kernelLayout||"OIHW"})}convert(t,s){return this._inferAndBuild("convert",[t],{target_dtype:s})}customCall(t,s,n,r=null){let i={call_target_name:t};return r&&(i.backend_config=r),this._buildOp("custom_call",s,n,i)}fusion(t,s,n,r){let i=new Se,o=new ns(t.map(l=>l.type));i.addBlock(o);let a=this._buildOp("fusion",t,s,{fusion_kind:n},[i]);if(r){let l=new e(Object.create(this.func,{}));l.block=o,r(l,o.arguments)}return a}ifOp(t,s,n,r){let i=new Se,o=new ns([]);i.addBlock(o);let a=new Se,l=new ns([]);a.addBlock(l);let u=this._buildOp("if",[t],s,null,[i,a]);if(n){let f=new e(Object.create(this.func,{}));f.block=o,n(f)}if(r){let f=new e(Object.create(this.func,{}));f.block=l,r(f)}return u}whileOp(t,s,n){let r=t.map(f=>f.type),i=new Se,o=new ns(r);i.addBlock(o);let a=new Se,l=new ns(r);a.addBlock(l);let u=this._buildOp("while",t,r,null,[i,a]);if(s){let f=new e(Object.create(this.func,{}));f.block=o,s(f,o.arguments)}if(n){let f=new e(Object.create(this.func,{}));f.block=l,n(f,l.arguments)}return u}scanOp(t,s,n){let r=t.map(g=>g.type.withShape(g.type.shape.slice(1))),i=s.map(g=>g.type),o=new Se,a=new ns([...r,...i]);o.addBlock(a);let l=new e(Object.create(this.func,{}));l.block=a;let u=a.arguments.slice(0,r.length),f=a.arguments.slice(r.length),[h,p]=n(l,u,f);if(l.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,...s],[...i,...m],{num_carry:s.length,num_xs:t.length},[o])}returnOp(t){return this._buildOp("return",t,[])}yieldOp(t){return this._buildOp("yield",t,[])}relu(t){let s=this.scalarConstant(0,t.type.dtype),n=this.broadcast(s.getResult(0),t.type.shape,[]);return this.maximum(t,n.getResult(0))}softmax(t,s=-1){let n=s<0?t.type.rank+s:s;return this._inferAndBuild("softmax",[t],{axis:n})}logSoftmax(t,s=-1){let n=s<0?t.type.rank+s:s;return this._inferAndBuild("log_softmax",[t],{axis:n})}sigmoid(t){return this._inferAndBuild("sigmoid",[t])}gelu(t){return this._inferAndBuild("gelu",[t])}silu(t){return this._inferAndBuild("silu",[t])}elu(t,s=1){return this._inferAndBuild("elu",[t],{alpha:s})}leakyRelu(t,s=.01){return this._inferAndBuild("leaky_relu",[t],{negative_slope:s})}celu(t,s=1){return this._inferAndBuild("celu",[t],{alpha:s})}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,s,n,r=-1,i=1e-5){let o=r<0?t.type.rank+r:r;return this._inferAndBuild("layer_norm",[t,s,n],{axis:o,epsilon:i})}batchnorm(t,s,n,r,i,o=1,a=1e-5){return this._inferAndBuild("batch_norm",[t,s,n,r,i],{axis:o,epsilon:a})}where(t,s,n){return this._inferAndBuild("where",[t,s,n])}split(t,s,n){return this._inferAndBuild("split",[t],{dimension:s,split_sizes:n})}oneHot(t,s,n={}){return this._inferAndBuild("one_hot",[t],{depth:s,axis:n.axis??-1,on_value:n.onValue??1,off_value:n.offValue??0,dtype:n.dtype||t.type.dtype})}embedding(t,s){return this._inferAndBuild("embedding",[t,s])}gather(t,s,n){return this._inferAndBuild("gather",[t,s],{offset_dims:n.offsetDims,collapsed_slice_dims:n.collapsedSliceDims,start_index_map:n.startIndexMap,index_vector_dim:n.indexVectorDim,slice_sizes:n.sliceSizes})}scatter(t,s,n,r){let i=new T([],t.type.dtype),o=new Se;return o.addBlock(new ns([i,i])),this._inferAndBuild("scatter",[t,s,n],{update_window_dims:r.updateWindowDims,inserted_window_dims:r.insertedWindowDims,scatter_dims_to_operand_dims:r.scatterDimsToOperandDims,index_vector_dim:r.indexVectorDim},[o])}scatterAdd(t,s,n,r){return this.scatter(t,s,n,r)}_dimCoordIndices(t,s,n){let r=n.length,i=t.type.dtype==="i32"?t:this.convert(t,"i32").getResult(0),o=[...n,1],a=[];for(let l=0;l<r;l++){let u=l===s?i:this.iota(l,new T(n,"i32")).getResult(0);a.push(this.reshape(u,o).getResult(0))}return this.concat(a,r).getResult(0)}gatherDim(t,s,n){let r=t.type.rank,i=n<0?r+n:n,o=[],a=[];for(let u=0;u<r;u++)o.push(u),a.push(1);let l=this._dimCoordIndices(s,i,s.type.shape);return this.gather(t,l,{offsetDims:[],collapsedSliceDims:o,startIndexMap:o,indexVectorDim:r,sliceSizes:a})}scatterAddDim(t,s,n,r){let i=t.type.rank,o=r<0?i+r:r,a=[];for(let u=0;u<i;u++)a.push(u);let l=this._dimCoordIndices(s,o,s.type.shape);return this.scatterAdd(t,l,n,{updateWindowDims:[],insertedWindowDims:a,scatterDimsToOperandDims:a,indexVectorDim:i})}argmax(t,s,n=!1){let r=s<0?t.type.rank+s:s;return this._inferAndBuild("argmax",[t],{axis:r,keep_dims:n})}argmin(t,s,n=!1){let r=s<0?t.type.rank+s:s;return this._inferAndBuild("argmin",[t],{axis:r,keep_dims:n})}pool2d(t,s,n,r,i,o={}){return this._inferAndBuild("pool2d",[t],{pool_type:s,kernel_size:n,strides:r,padding:i,ceil_mode:o.ceilMode||!1,count_include_pad:o.countIncludePad||!1,layout:o.layout||"NCHW"})}resize(t,s,n,r={}){return this._inferAndBuild("resize",[t],{output_size:s,method:n,coordinate_mode:r.coordinateMode||"asymmetric",layout:r.layout||"NCHW"})}};c(sm,"broadcastDimsExcluding");c(C$,"toMap");c(Yf,"buildFunction")});function ds(e,t,s){if(s==null)return new L(e,t,s);let n=t instanceof M,r=s instanceof M;if(n&&r){let i=t.value,o=s.value;switch(e){case"+":return new M(i+o);case"-":return new M(i-o);case"*":return new M(i*o);case"//":if(o!==0)return new M(Math.trunc(i/o));break;case"%":if(o!==0)return new M((i%o+o)%o);break}}if(r){let i=s.value;if((e==="+"||e==="-")&&i===0||e==="*"&&i===1)return t;if(e==="*"&&i===0)return new M(0);if(e==="//"&&i===1)return t;if(e==="%"&&i===1)return new M(0)}if(n){let i=t.value;if(e==="+"&&i===0||e==="*"&&i===1)return s;if(e==="*"&&i===0)return new M(0)}return new L(e,t,s)}var V,Je,To,wt,st,Ob,Qf,Z,H,Ht,Sn,Hs,yt,xc,nm,Qa,vt,L,fe,ie,Ae,M,ut,Jt=I(()=>{V=Object.freeze({SERIAL:"serial",PARALLEL:"parallel",VECTORIZED:"vectorized",UNROLLED:"unrolled",THREAD_BINDING:"thread_binding",RECURRENCE:"recurrence"}),Je=class e{static{c(this,"TensorNode")}constructor(){this.type=this.constructor.name.replace(/^_+/,""),this._parent=null,this._parentKey=null,this._parentIdx=-1}_setChild(t,s,n=-1){s instanceof e&&(s._parent=this,s._parentKey=t,s._parentIdx=n)}_setChildren(t,s){if(s)for(let n=0;n<s.length;n++)s[n]instanceof e&&(s[n]._parent=this,s[n]._parentKey=t,s[n]._parentIdx=n)}replaceWith(t){let s=this._parent;return s?(this._parentIdx>=0?s[this._parentKey][this._parentIdx]=t:s[this._parentKey]=t,t instanceof e&&(t._parent=s,t._parentKey=this._parentKey,t._parentIdx=this._parentIdx),this._parent=null,this._parentKey=null,this._parentIdx=-1,!0):!1}},To=class extends Je{static{c(this,"PrimFunc")}constructor(t,s,n,r=new Map,i=[],o=null){super(),this.name=t,this.params=s,this.body=n,this.bufferMap=r,this.shapeParams=i,this.shapeParamMap=o||new Map,this._setChild("body",n)}},wt=class extends Je{static{c(this,"ForNode")}constructor(t,s,n,r,i,o=null){super(),this.loopVar=t,this.min=s,this.extent=n,this.kind=r,this.body=i,this.threadTag=o,this._setChild("body",i)}},st=class extends Je{static{c(this,"BlockNode")}constructor(t,s,n,r,i,o=null){super(),this.name=t,this.iterVars=s,this.reads=n,this.writes=r,this.body=i,this.initBody=o,this._setChild("body",i),this._setChild("initBody",o)}},Ob=Object.freeze({DATA_PAR:"DataPar",COMM_REDUCE:"CommReduce"}),Qf=class extends Je{static{c(this,"BlockRealizeNode")}constructor(t,s,n=Ob.DATA_PAR){super(),this.iterVar=t,this.binding=s,this.kind=n}},Z=class extends Je{static{c(this,"BufferStoreNode")}constructor(t,s,n){super(),this.buffer=t,this.indices=s,this.value=n}},H=class extends Je{static{c(this,"BufferLoadNode")}constructor(t,s){super(),this.buffer=t,this.indices=s}},Ht=class extends Je{static{c(this,"IfThenElseNode")}constructor(t,s,n=null){super(),this.condition=t,this.thenBody=s,this.elseBody=n,this._setChild("thenBody",s),this._setChild("elseBody",n)}},Sn=class extends Je{static{c(this,"LetStmtNode")}constructor(t,s,n){super(),this.variable=t,this.value=s,this.body=n,this._setChild("body",n)}},Hs=class extends Je{static{c(this,"AllocateNode")}constructor(t,s,n){super(),this.buffer=t,this.scope=s,this.body=n,this._setChild("body",n)}},yt=class extends Je{static{c(this,"SeqNode")}constructor(t){super(),this.stmts=t,this._setChildren("stmts",t)}},xc=class extends Je{static{c(this,"WhileNode")}constructor(t,s,n){super(),this.condVar=t,this.condBody=s,this.loopBody=n,this._setChild("condBody",s),this._setChild("loopBody",n)}},nm=class extends Je{static{c(this,"EvaluateNode")}constructor(t){super(),this.value=t}},Qa=class extends Je{static{c(this,"SyncThreadsNode")}constructor(){super()}},vt=class extends Je{static{c(this,"CallExternNode")}constructor(t,s,n){super(),this.externName=t,this.args=s,this.dtype=n}},L=class extends Je{static{c(this,"MathOpNode")}constructor(t,s,n=null){super(),this.op=t,this.a=s,this.b=n}},fe=class e extends Je{static{c(this,"CompareNode")}static JS_OPS={eq:"===",ne:"!==",lt:"<",le:"<=",gt:">",ge:">="};static C_OPS={eq:"==",ne:"!=",lt:"<",le:"<=",gt:">",ge:">="};constructor(t,s,n){super(),this.direction=t,this.a=s,this.b=n}toJS(){let t=e.JS_OPS[this.direction];if(!t)throw new Error(`CompareNode: unsupported direction '${this.direction}'`);return t}toC(){let t=e.C_OPS[this.direction];if(!t)throw new Error(`CompareNode: unsupported direction '${this.direction}'`);return t}},ie=class extends Je{static{c(this,"CastNode")}constructor(t,s,n){super(),this.expr=t,this.fromDtype=s,this.toDtype=n}},Ae=class extends Je{static{c(this,"VariableNode")}constructor(t,s){super(),this.name=t,this.dtype=s}},M=class extends Je{static{c(this,"IntImmNode")}constructor(t){super(),this.value=t}},ut=class extends Je{static{c(this,"FloatImmNode")}constructor(t){super(),this.value=t}};c(ds,"mathOp")});var Sc,Mb=I(()=>{Sc=Object.freeze({GLOBAL:"global",SHARED:"shared",LOCAL:"local",REGISTER:"register"})});var ee,Xa=I(()=>{bn();ee=class{static{c(this,"Buffer")}constructor(t,s,n,r,i=null,o=0,a=64){if(this.name=t,this.shape=s,this.dtype=n,this.scope=r,this.offset=o,this.alignment=a,this.broadcastDims=null,i)this.strides=i;else{this.strides=new Array(s.length);let l=1;for(let u=s.length-1;u>=0;u--)this.strides[u]=l,typeof s[u]=="number"&&(l*=s[u])}}get rank(){return this.shape.length}get isScalar(){return this.shape.length===0}numel(){let t=1;for(let s=0;s<this.shape.length;s++){if(typeof this.shape[s]!="number")return-1;t*=this.shape[s]}return t}sizeInBytes(){let t=this.numel();return t<0?-1:t*Od(this.dtype)}}});function Pb(e,{name:t,compute:s,plevel:n=10,targetKind:r=null}){let i=Lb.get(e);return i||(i=new Xf(e),Lb.set(e,i)),i.addImplementation(t,s,n,r),i}function im(e,t=null){let s=Lb.get(e);if(!s)return null;let n=t?t.kind:null,r=new Xf(e);for(let i of s.implementations)(i.targetKind===null||i.targetKind===n)&&r.implementations.push(i);return r.implementations.length>0?r:null}function Db(e,t=null){let s=im(e,t);return s?s.best():null}var rm,Xf,Lb,Fb=I(()=>{rm=class{static{c(this,"OpImplementation")}constructor(t,s,n=10,r=null){this.name=t,this.compute=s,this.plevel=n,this.targetKind=r}},Xf=class{static{c(this,"OpStrategy")}constructor(t){this.opName=t,this.implementations=[]}addImplementation(t,s,n=10,r=null){let i=new rm(t,s,n,r),o=this.implementations.findIndex(a=>a.name===t);return o>=0?this.implementations[o]=i:this.implementations.push(i),this}get candidates(){return this.implementations}best(){let t=null;for(let s of this.implementations)(t===null||s.plevel>t.plevel)&&(t=s);return t}},Lb=new Map;c(Pb,"registerOpStrategy");c(im,"getOpStrategy");c(Db,"selectImplementation")});function zb(e,t){return im(e,t)?!0:Ur.has(e)}function Ct(e,t,s=T$){Pb(e,{name:`${e}.generic`,compute:t,plevel:s,targetKind:null})}function Io(e,t){let s=Db(e,t);return s?s.compute:void 0}function ve(e,t,s){let n=t.length,r=e.allocVarArray("i",n),i=e.allocBindArray("v",r),o=new Array(n);for(let l=0;l<n;l++)o[l]=i[l].iterVar;let a=s?e.extentNodes(t,s):null;return{loopVars:r,loopBinds:i,indices:o,extentNodes:a}}function th(e,t,s){let n=e;for(let r=t.length-1;r>=0;r--)n=new wt(t[r],new M(0),s[r],V.SERIAL,n);return n}function I$(e,t,s){let n=e;for(let r=t.length-1;r>=0;r--)n=new wt(t[r],new M(0),new M(s[r]),V.SERIAL,n);return n}function ke(e,t,s,n){return n?th(e,t,n):I$(e,t,s)}function xs(e,t,s,n,r){let i=s.length,o=new Array(i),a=new Array(i),l=new Array(i),u=new Array(i);for(let f=0;f<i;f++)o[f]=e.allocVar(`${t}${s[f]}`),a[f]=new Qf(e.allocVar(`${t}v${s[f]}`),o[f]),l[f]=a[f].iterVar,u[f]=e.extentNode(n[s[f]],r,s[f]);return{vars:o,ivs:a,indices:l,extentNodes:u,wrap(f){return th(f,o,u)}}}function Ki(e,t,s){let n=e.shape.length;if(e.broadcastDims){let a=e.broadcastDims,l=new Array(n);for(let u=0;u<n;u++)l[u]=e.shape[u]===1?new M(0):s[a[u]];return l}let i=t.shape.length-n,o=new Array(n);for(let a=0;a<n;a++)o[a]=e.shape[a]===1?new M(0):s[i+a];return o}function Ac(e){let t=new Array(e.length);for(let s=0;s<e.length;s++)t[s]={buffer:e[s]};return t}function Zf(){let e=0;for(let n=0;n<arguments.length;n++)e+=arguments[n].length;let t=new Array(e),s=0;for(let n=0;n<arguments.length;n++){let r=arguments[n];for(let i=0;i<r.length;i++)t[s++]=r[i]}return t}function $b(e){let t=new Array(e.length);for(let s=0;s<e.length;s++)t[s]=e[s].iterVar;return t}function fi(e,t,s,n,r){let i=n[0],{loopVars:o,loopBinds:a,indices:l,extentNodes:u}=ve(e,i.shape,i),f=new Array(s.length);for(let m=0;m<s.length;m++){let g=Ki(s[m],i,l);f[m]=new H(s[m],g)}let h=r(t,f,i.dtype),p=new Z(i,l,h),d=new st(e.blockName(`${t.opName}_block`),a,Ac(s),[{buffer:i}],p);return ke(d,o,i.shape,u)}function vc(e,t){let s=t.getResult(0),n=t.getAttr("value"),r=s.type&&s.type.shape||[];if(typeof n=="number"&&r.length>0&&!e.bufferMap.has(s)){let g=s.type&&s.type.dtype||"f32",y=new Array(r.length).fill(1),_=new ee(`buf_${e.varCounter++}`,y,g,Sc.GLOBAL);_.broadcastDims=Array.from({length:r.length},(b,x)=>x),e.bufferMap.set(s,_);let w=_n(g)?new M(n):new ut(n);return new Z(_,y.map(()=>new M(0)),w)}let i=e.getOrAllocBuffer(s),o=_n(i.dtype),a=c(g=>o?new M(g):new ut(g),"imm");if(n&&typeof n!="number"&&typeof n.length=="number"){if(i.shape.length===0)return new Z(i,[],a(n[0]));let g=new Array(i.shape.length),y=1;for(let w=i.shape.length-1;w>=0;w--)g[w]=y,y*=i.shape[w];let _=[];for(let w=0;w<n.length;w++){let b=new Array(i.shape.length);for(let x=0;x<i.shape.length;x++)b[x]=new M(Math.floor(w/g[x])%i.shape[x]);_.push(new Z(i,b,a(n[w])))}return new yt(_)}let l=a(typeof n=="number"?n:0);if(i.shape.length===0)return new Z(i,[],l);let{loopVars:u,loopBinds:f,indices:h,extentNodes:p}=ve(e,i.shape,i),d=new Z(i,h,l),m=new st(e.blockName(`${t.opName}_block`),f,[],[{buffer:i}],d);return ke(m,u,i.shape,p)}function Oo(e){let t={};for(let s=0;s<e.length;s++)t[e[s]]=s;return t}function RN(e,t){if(!e.broadcastDims)return t;let s=e.broadcastDims,n=new Array(e.shape.length);for(let r=0;r<e.shape.length;r++)n[r]=e.shape[r]===1?new M(0):t[s[r]];return n}function eh(e,t,s,n){let r=t.getAttr("lhs_contracting")||[],i=t.getAttr("rhs_contracting")||[],o=t.getAttr("lhs_batch")||[],a=t.getAttr("rhs_batch")||[],l=t.getOperand(0).type.shape,u=t.getOperand(1).type.shape,f=new Set(r),h=new Set(o),p=new Set(i),d=new Set(a),m=[];for(let P=0;P<l.length;P++)!f.has(P)&&!h.has(P)&&m.push(P);let g=[];for(let P=0;P<u.length;P++)!p.has(P)&&!d.has(P)&&g.push(P);let y=e.allocVarArray("b",o.length),_=e.allocVarArray("ls",m.length),w=e.allocVarArray("rs",g.length),b=e.allocVarArray("c",r.length),x=e.allocBindArray("vb",y),A=e.allocBindArray("vls",_),S=e.allocBindArray("vrs",w),E=e.allocBindArray("vc",b),R=Zf($b(x),$b(A),$b(S)),v=new Array(l.length);for(let P=0;P<o.length;P++)v[o[P]]=x[P].iterVar;for(let P=0;P<m.length;P++)v[m[P]]=A[P].iterVar;for(let P=0;P<r.length;P++)v[r[P]]=E[P].iterVar;let k=new Array(u.length);for(let P=0;P<a.length;P++)k[a[P]]=x[P].iterVar;for(let P=0;P<g.length;P++)k[g[P]]=S[P].iterVar;for(let P=0;P<i.length;P++)k[i[P]]=E[P].iterVar;let B=RN(s,v),N=RN(n,k),C=Zf(x,A,S,E),O=[{vars:y,dims:o,shape:l,buf:s},{vars:_,dims:m,shape:l,buf:s},{vars:w,dims:g,shape:u,buf:n},{vars:b,dims:r,shape:l,buf:s}];function F(P){let z=P;for(let q=O.length-1;q>=0;q--){let{vars:U,dims:D,shape:G,buf:At}=O[q];for(let pt=U.length-1;pt>=0;pt--){let mt=At.broadcastDims?new M(G[D[pt]]):e.extentNode(G[D[pt]],At,D[pt]);z=new wt(U[pt],new M(0),mt,V.SERIAL,z)}}return z}return c(F,"wrapAccBody"),{outIdx:R,lhsIdx:B,rhsIdx:N,allIvs:C,wrapAccBody:F}}var Ur,T$,om,Xn=I(()=>{It();Mb();Xa();bn();Jt();Fb();Fb();Ur=new Set(["constant","scalar_constant"]),T$=10;c(zb,"hasLoweringRule");c(Ct,"registerLoweringRule");c(Io,"getLoweringRule");om=class{static{c(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,s="int32"){return new Ae(`${t}_${this.varCounter++}`,s)}getOrAllocBuffer(t){let s=this.bufferMap.get(t);if(s)return s;let n=t.type,r=n.shape||[],i=n.dtype||"f32",o=n.layout?n.layout.computeStrides(r):null;return s=new ee(`buf_${this.varCounter++}`,r,i,Sc.GLOBAL,o),t.symbolicShape&&(s.symbolicShape=t.symbolicShape),this.bufferMap.set(t,s),this._registerDynamicDims(s),s}allocFreshBuffer(t){let s=t.type,n=s.shape||[],r=s.dtype||"f32",i=s.layout?s.layout.computeStrides(n):null,o=new ee(`buf_${this.varCounter++}`,n,r,Sc.GLOBAL,i);return t.symbolicShape&&(o.symbolicShape=t.symbolicShape),this._registerDynamicDims(o),o}_registerDynamicDims(t){for(let s=0;s<t.shape.length;s++)t.shape[s]===X&&this.extentNode(X,t,s)}_shapeParamVar(t,s){let n=s>=0?`${t.name}:${s}`:`${t.name}:dyn`,r=this.shapeParams.get(n);if(r)return r;let i=t.symbolicShape&&s>=0&&typeof t.symbolicShape[s]!="number"?t.symbolicShape[s]:null;return i!==null&&this.symbolToVar.has(i)?r=this.symbolToVar.get(i):(r=this.allocVar("_ds"),i!==null&&this.symbolToVar.set(i,r)),this.shapeParams.set(n,r),r}extentNode(t,s,n=-1){return t!==X?new M(t):this._shapeParamVar(s,n)}extentNodes(t,s){let n=new Array(t.length);for(let r=0;r<t.length;r++)n[r]=t[r]===X?this._shapeParamVar(s,r):new M(t[r]);return n}allocVarArray(t,s){let n=new Array(s);for(let r=0;r<s;r++)n[r]=this.allocVar(`${t}${r}`);return n}allocBindArray(t,s){let n=new Array(s.length);for(let r=0;r<s.length;r++)n[r]=new Qf(this.allocVar(`${t}${r}`),s[r]);return n}};c(ve,"makeLoopNest");c(th,"wrapLoopsWithNodes");c(I$,"wrapLoops");c(ke,"wrapInLoops");c(xs,"buildSpatialNest");c(Ki,"computeBroadcastIndices");c(Ac,"bufRefs");c(Zf,"concatIterVars");c($b,"extractIterVars");c(fi,"lowerPointwise");c(vc,"lowerConstant");c(Oo,"parseLayout");c(RN,"physicalDotIndices");c(eh,"buildDotGeometry")});function Ub(e,t,s){let n=kc[e];return n?BN[e]?BN[e](t):t.length===2&&O$.has(n)?new L(n,t[0],t[1]):t.length===2&&M$.has(n)?new L(n,t[0],t[1]):t.length===1&&L$.has(n)?new L(n,t[0]):t.length===1&&n==="-"?new L("-",t[0]):new vt(n,t,s):null}function CN(){for(let e of Object.keys(kc))Ct(e,(t,s,n,r)=>fi(t,s,n,r,(i,o,a)=>Ub(i.opName,o,a)));Ct("compare",(e,t,s,n)=>fi(e,t,s,n,(r,i)=>new fe(r.getAttr("direction")||"eq",i[0],i[1]))),Ct("select",(e,t,s,n)=>fi(e,t,s,n,(r,i)=>new Ht(i[0],i[1],i[2]))),Ct("clamp",(e,t,s,n)=>fi(e,t,s,n,(r,i,o)=>new vt("min",[new vt("max",[i[1],i[0]],o),i[2]],o))),Ct("convert",(e,t,s,n)=>fi(e,t,s,n,(r,i)=>new ie(i[0],s[0].dtype,n[0].dtype))),Ct("copy_to_device",(e,t,s,n)=>fi(e,t,s,n,(r,i)=>i[0]))}var O$,M$,L$,BN,kc,am=I(()=>{Jt();Xn();O$=new Set(["+","-","*","/"]),M$=new Set(["&&","||"]),L$=new Set(["!"]),BN={square:c(e=>new L("*",e[0],e[0]),"square"),reciprocal:c(e=>new L("/",new ut(1),e[0]),"reciprocal")},kc={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:"||"};c(Ub,"buildElementwiseExpr");c(CN,"register")});function IN(){Ct("broadcast_in_dim",TN),Ct("broadcast",TN),Ct("transpose",(e,t,s,n)=>{let r=t.getAttr("permutation"),i=s[0],o=n[0],{loopVars:a,loopBinds:l,indices:u,extentNodes:f}=ve(e,o.shape,o),h=new Array(i.shape.length);for(let m=0;m<r.length;m++)h[r[m]]=u[m];let p=new Z(o,u,new H(i,h)),d=new st(e.blockName("transpose_block"),l,[{buffer:i}],[{buffer:o}],p);return ke(d,a,o.shape,f)}),Ct("reverse",(e,t,s,n)=>{let r=new Set(t.getAttr("dimensions")),i=s[0],o=n[0],{loopVars:a,loopBinds:l,indices:u,extentNodes:f}=ve(e,o.shape,o),h=new Array(i.shape.length);for(let m=0;m<i.shape.length;m++)h[m]=r.has(m)?new L("-",new L("-",f[m],new M(1)),u[m]):u[m];let p=new Z(o,u,new H(i,h)),d=new st(e.blockName("reverse_block"),l,[{buffer:i}],[{buffer:o}],p);return ke(d,a,o.shape,f)}),Ct("reshape",(e,t,s,n)=>{let r=s[0],i=n[0],{loopVars:o,loopBinds:a,indices:l,extentNodes:u}=ve(e,i.shape,i),f;if(r.shape.length===i.shape.length&&r.shape.every((m,g)=>m===i.shape[g]))f=l;else{for(let _=1;_<r.shape.length;_++)if(typeof r.shape[_]!="number"||r.shape[_]<0)throw new Error("reshape lowering requires static non-leading input dims");for(let _=1;_<i.shape.length;_++)if(typeof i.shape[_]!="number"||i.shape[_]<0)throw new Error("reshape lowering requires static non-leading output dims");let m=l[i.shape.length-1],g=1;for(let _=i.shape.length-2;_>=0;_--)g*=i.shape[_+1],m=ds("+",m,ds("*",l[_],new M(g)));f=new Array(r.shape.length);let y=m;for(let _=r.shape.length-1;_>=0;_--)_===0?f[_]=y:(f[_]=ds("%",y,new M(r.shape[_])),y=ds("//",y,new M(r.shape[_])))}let p=new Z(i,l,new H(r,f)),d=new st(e.blockName("reshape_block"),a,[{buffer:r}],[{buffer:i}],p);return ke(d,o,i.shape,u)}),Ct("slice",(e,t,s,n)=>{let r=s[0],i=n[0],o=t.getAttr("starts"),a=t.getAttr("strides")||o.map(()=>1),{loopVars:l,loopBinds:u,indices:f,extentNodes:h}=ve(e,i.shape,i),p=new Array(r.shape.length);for(let g=0;g<r.shape.length;g++){let y=new M(o[g]);a[g]===1?p[g]=new L("+",y,f[g]):p[g]=new L("+",y,new L("*",f[g],new M(a[g])))}let d=new Z(i,f,new H(r,p)),m=new st(e.blockName("slice_block"),u,[{buffer:r}],[{buffer:i}],d);return ke(m,l,i.shape,h)}),Ct("pad",(e,t,s,n)=>{let r=s[0],i=s[1],o=n[0],a=t.getAttr("low"),l=t.getAttr("interior")||a.map(()=>0),{loopVars:u,loopBinds:f,indices:h,extentNodes:p}=ve(e,o.shape,o),d=new Array(r.shape.length),m=new M(1);for(let x=0;x<r.shape.length;x++){let A=new L("+",h[x],new M(-a[x]));if(l[x]>0){let R=l[x]+1,v=new L("%",A,new M(R)),k=new fe("eq",v,new M(0));m=new L("*",m,k),d[x]=new L("//",A,new M(R))}else d[x]=A;let S=new fe("ge",d[x],new M(0)),E=new fe("lt",d[x],new M(r.shape[x]));m=new L("*",m,new L("*",S,E))}let g=new H(r,d),y=new H(i,[]),_=new Ht(m,g,y),w=new Z(o,h,_),b=new st(e.blockName("pad_block"),f,[{buffer:r},{buffer:i}],[{buffer:o}],w);return ke(b,u,o.shape,p)}),Ct("concat",(e,t,s,n)=>{let r=n[0],i=t.getAttr("dimension"),o=[],a=0;for(let l=0;l<s.length;l++){let u=s[l],{loopVars:f,loopBinds:h,indices:p,extentNodes:d}=ve(e,u.shape,u),m=new Array(u.shape.length);for(let _=0;_<u.shape.length;_++)m[_]=_===i&&a>0?new L("+",p[_],new M(a)):p[_];let g=new Z(r,m,new H(u,p)),y=new st(e.blockName("concat"),h,[{buffer:u}],[{buffer:r}],g);o.push(ke(y,f,u.shape,d)),a+=u.shape[i]}return o.length===1?o[0]:new yt(o)}),Ct("iota",(e,t,s,n)=>{let r=n[0],i=t.getAttr("iota_dimension"),{loopVars:o,loopBinds:a,indices:l,extentNodes:u}=ve(e,r.shape,r),f=new ie(l[i],"index",r.dtype),h=new Z(r,l,f),p=new st(e.blockName("iota_block"),a,[],[{buffer:r}],h);return ke(p,o,r.shape,u)})}function TN(e,t,s,n){let r=s[0],i=n[0],o=t.getAttr("broadcast_dimensions")||[],{loopVars:a,loopBinds:l,indices:u,extentNodes:f}=ve(e,i.shape,i),h=new Array(r.shape.length);for(let m=0;m<r.shape.length;m++){let g=o.length>0?o[m]:m+(i.shape.length-r.shape.length);h[m]=r.shape[m]===1?new M(0):u[g]}let p=new Z(i,u,new H(r,h)),d=new st(e.blockName("broadcast_block"),l,[{buffer:r}],[{buffer:i}],p);return ke(d,a,i.shape,f)}var ON=I(()=>{Jt();Xn();c(IN,"register");c(TN,"lowerBroadcast")});function F$(e,t){if(_n(e)){let s=t?P$[e]??-2147483648:D$[e]??2147483647;return new M(s)}return new ut(t?-1/0:1/0)}function LN(){Ct("reduce",(t,s,n,r)=>{let i=n[0],o=n[1],a=r[0],l=s.getAttr("dimensions")||[],u=s.getAttr("reduce_type")||"sum",f=new Set(l),h=[],p=[];for(let C=0;C<i.shape.length;C++)(f.has(C)?p:h).push(C);let d=xs(t,"si",h,i.shape,i),m=new Z(a,d.indices,new H(o,[])),g=new st(t.blockName("reduce_init"),d.ivs,[{buffer:o}],[{buffer:a}],m),y=h.length>0?d.wrap(g):g,_=xs(t,"sa",h,i.shape,i),w=t.allocVarArray("r",p.length),b=t.allocBindArray("rv",w);for(let C of b)C.kind=Ob.COMM_REDUCE;let x=new Array(i.shape.length);for(let C=0;C<h.length;C++)x[h[C]]=_.ivs[C].iterVar;for(let C=0;C<p.length;C++)x[p[C]]=b[C].iterVar;let A=new H(a,_.indices),S=new H(i,x),E=MN[u]||MN.sum,R=new Z(a,_.indices,E(A,S,a.dtype)),v=new Array(p.length);for(let C=0;C<p.length;C++)v[C]=t.extentNode(i.shape[p[C]],i,p[C]);let k=new st(t.blockName("reduce_acc"),Zf(_.ivs,b),[{buffer:i}],[{buffer:a}],R),B=th(k,w,v);B=_.wrap(B);let N=[y,B];if(u==="mean"){let C=1,O=[];for(let D=0;D<p.length;D++){let G=i.shape[p[D]];G===X?O.push(t.extentNode(X,i,p[D])):C*=G}let F=xs(t,"sm",h,i.shape,i),P=new H(a,F.indices),z;if(O.length===0)z=new L("*",P,new ut(1/C));else{let D=new M(C);for(let G of O)D=new L("*",D,G);z=new L("/",P,D)}let q=new Z(a,F.indices,z),U=new st(t.blockName("mean_div"),F.ivs,[{buffer:a}],[{buffer:a}],q);N.push(h.length>0?F.wrap(U):U)}return new yt(N)});function e(t,s){Ct(t,(n,r,i,o)=>{let a=i[0],l=o[0],u=r.getAttr("axis"),f=r.getAttr("keep_dims")||!1,h=new Set([u]),p=[],d=u;for(let G=0;G<a.shape.length;G++)h.has(G)||p.push(G);let m=c(G=>{if(!f)return G.indices;let At=new Array(a.shape.length);for(let pt=0;pt<p.length;pt++)At[p[pt]]=G.indices[pt];return At[d]=new M(0),At},"outIndicesFor"),g=new ee("_argval_"+n.varCounter,p.map(G=>a.shape[G]),a.dtype,"global");n.varCounter++;let y=xs(n,"ai",p,a.shape,a),_=new Z(g,y.indices,F$(a.dtype,s==="gt")),w=new Z(l,m(y),new M(0)),b=new st(n.blockName("arg_init"),y.ivs,[],[{buffer:g},{buffer:l}],new yt([_,w])),x=p.length>0?y.wrap(b):b,A=xs(n,"as",p,a.shape,a),S=n.allocVar("ar"),E=n.allocBindArray("arv",[S]),R=new Array(a.shape.length);for(let G=0;G<p.length;G++)R[p[G]]=A.ivs[G].iterVar;R[d]=E[0].iterVar;let v=new H(a,R),k=new H(g,A.indices),B=new fe(s,v,k),N=new Ht(B,v,k),C=m(A),O=new H(l,C),F=new Ht(B,E[0].iterVar,O),P=new Z(l,C,F),z=new Z(g,A.indices,N),q=new st(n.blockName("arg_acc"),Zf(A.ivs,E),[{buffer:a},{buffer:g}],[{buffer:g},{buffer:l}],new yt([P,z])),U=n.extentNode(a.shape[d],a,d),D=th(q,[S],[U]);return D=A.wrap(D),new yt([x,D])})}c(e,"registerArgReduce"),e("argmax","gt"),e("argmin","lt")}var P$,D$,MN,PN=I(()=>{Jt();Xa();It();Xn();bn();P$={i8:-128,i16:-32768,i32:-2147483648,i64:-2147483648,ui8:0,ui16:0,ui32:0,bool:0},D$={i8:127,i16:32767,i32:2147483647,i64:2147483647,ui8:255,ui16:65535,ui32:4294967295,bool:1};c(F$,"argReduceSentinel");MN={sum:c((e,t)=>new L("+",e,t),"sum"),mean:c((e,t)=>new L("+",e,t),"mean"),prod:c((e,t)=>new L("*",e,t),"prod"),max:c((e,t,s)=>new vt("max",[e,t],s),"max"),min:c((e,t,s)=>new vt("min",[e,t],s),"min")};c(LN,"register")});function DN(e,t){return ss(t)?new ie(e,t,Q.I32):e}function FN(){Ct("dot",(e,t,s,n)=>{let r=s[0],i=s[1],o=n[0],a=eh(e,t,r,i),l=xs(e,"di",Array.from({length:o.shape.length},(y,_)=>_),o.shape,o),u=new Z(o,l.indices,new ut(0)),f=new st(e.blockName("matmul_init"),l.ivs,[],[{buffer:o}],u),h=l.wrap(f),p=new L("+",new H(o,a.outIdx),new L("*",new H(r,a.lhsIdx),new H(i,a.rhsIdx))),d=new Z(o,a.outIdx,p),m=new st(e.blockName("matmul"),a.allIvs,[{buffer:r},{buffer:i}],[{buffer:o}],d),g=a.wrapAccBody(m);return new yt([h,g])}),Ct("conv",(e,t,s,n)=>{let r=s[0],i=s[1],o=n[0],a=t.getAttr("strides"),l=t.getAttr("padding"),u=t.getAttr("dilation")||a.map(()=>1),f=t.getAttr("groups")||1,h=Oo(t.getAttr("input_layout")),p=Oo(t.getAttr("kernel_layout")),d=a.length,m=r.shape[h.N],g=i.shape[p.O],y=i.shape[p.I],_=o.shape,w=xs(e,"ci",Array.from({length:_.length},(Rt,Ce)=>Ce),_,o),b=new Z(o,w.indices,new ut(0)),x=new st(e.blockName("conv_init"),w.ivs,[],[{buffer:o}],b),A=w.wrap(x),S=e.allocVar("cn"),E=e.allocVar("coc"),R=e.allocVar("cic"),v=e.allocVarArray("co",d),k=e.allocVarArray("ck",d),B=[S,E,...v,R,...k],N=e.allocBindArray("cv",B),C=N[0].iterVar,O=N[1].iterVar,F=N.slice(2,2+d),P=N[2+d].iterVar,z=N.slice(3+d),q=new Array(_.length);q[h.N]=C,q[h.C]=O;let U=Object.keys(h).filter(Rt=>Rt!=="N"&&Rt!=="C").sort();for(let Rt=0;Rt<d;Rt++)q[h[U[Rt]]]=F[Rt].iterVar;let D=new Array(r.shape.length);D[h.N]=C;let G=Math.floor(g/f);f>1?D[h.C]=new L("+",new L("*",new L("//",O,new M(G)),new M(y)),P):D[h.C]=P;let At=new Array(i.shape.length);At[p.O]=O,At[p.I]=P;let pt=null;for(let Rt=0;Rt<d;Rt++){let Ce=U[Rt],Vi=Ce.toUpperCase(),es=ds("+",ds("*",F[Rt].iterVar,new M(a[Rt])),ds("+",ds("*",z[Rt].iterVar,new M(u[Rt])),new M(-l[Rt][0])));if(D[h[Ce]]=es,At[p[Vi]]=z[Rt].iterVar,l[Rt][0]!==0||l[Rt][1]!==0){let Bt=new fe("ge",es,new M(0)),xe=new fe("lt",es,new M(r.shape[h[Ce]])),yr=new L("*",Bt,xe);pt=pt?new L("*",pt,yr):yr}}let mt=new H(r,D),$=new H(i,At),K=new H(o,q),_t=new L("*",mt,$),dt=pt?new Ht(pt,_t,new ut(0)):_t,jt=new L("+",K,dt),Pn=new Z(o,q,jt),gn=new st(e.blockName("conv_acc"),N,[{buffer:r},{buffer:i}],[{buffer:o}],Pn),yn=new Array(d);for(let Rt=0;Rt<d;Rt++){let Ce=U[Rt].toUpperCase();yn[Rt]=i.shape[p[Ce]]}let ts=gn;for(let Rt=d-1;Rt>=0;Rt--){let Ce=U[Rt].toUpperCase();ts=new wt(k[Rt],new M(0),e.extentNode(yn[Rt],i,p[Ce]),V.SERIAL,ts)}ts=new wt(R,new M(0),e.extentNode(y,i,p.I),V.SERIAL,ts);for(let Rt=d-1;Rt>=0;Rt--){let Ce=h[U[Rt]];ts=new wt(v[Rt],new M(0),e.extentNode(_[Ce],o,Ce),V.SERIAL,ts)}return ts=new wt(E,new M(0),e.extentNode(g,i,p.O),V.SERIAL,ts),ts=new wt(S,new M(0),e.extentNode(m,r,h.N),V.SERIAL,ts),new yt([A,ts])}),Ct("gather",(e,t,s,n)=>{let r=s[0],i=s[1],o=n[0],a=new Set(t.getAttr("offset_dims")),l=new Set(t.getAttr("collapsed_slice_dims")),u=t.getAttr("start_index_map"),f=t.getAttr("index_vector_dim"),{loopVars:h,loopBinds:p,indices:d,extentNodes:m}=ve(e,o.shape,o),g=[],y=[];for(let A=0;A<o.shape.length;A++)a.has(A)?y.push(d[A]):g.push(d[A]);let _=new Array(r.shape.length),w=0;for(let A=0;A<r.shape.length;A++)l.has(A)?_[A]=new M(0):_[A]=y[w++];for(let A=0;A<u.length;A++){let S=new Array(i.shape.length),E=0;for(let k=0;k<i.shape.length;k++)k===f?S[k]=new M(A):S[k]=g[E++];let R=DN(new H(i,S),i.dtype),v=u[A];_[v]=new L("+",_[v],R)}let b=new Z(o,d,new H(r,_)),x=new st(e.blockName("gather_block"),p,[{buffer:r},{buffer:i}],[{buffer:o}],b);return ke(x,h,o.shape,m)}),Ct("scatter",(e,t,s,n)=>{let r=s[0],i=s[1],o=s[2],a=n[0],l=new Set(t.getAttr("inserted_window_dims")),u=t.getAttr("scatter_dims_to_operand_dims"),f=t.getAttr("index_vector_dim"),h=new Set(t.getAttr("update_window_dims")),p=ve(e,r.shape,r),d=new Z(a,p.indices,new H(r,p.indices)),m=new st(e.blockName("scatter_copy"),p.loopBinds,[{buffer:r}],[{buffer:a}],d),g=ke(m,p.loopVars,r.shape,p.extentNodes),{loopVars:y,loopBinds:_,indices:w,extentNodes:b}=ve(e,o.shape,o),x=[],A=[];for(let O=0;O<o.shape.length;O++)h.has(O)?A.push(w[O]):x.push(w[O]);let S=new Array(r.shape.length),E=0;for(let O=0;O<r.shape.length;O++)l.has(O)?S[O]=new M(0):S[O]=A[E++];for(let O=0;O<u.length;O++){let F=new Array(i.shape.length),P=0;for(let U=0;U<i.shape.length;U++)U===f?F[U]=new M(O):F[U]=x[P++];let z=DN(new H(i,F),i.dtype),q=u[O];S[q]=new L("+",S[q],z)}let R=new H(o,w),v=new H(a,S),k=new L("+",v,R),B=new Z(a,S,k),N=new st(e.blockName("scatter_update"),_,[{buffer:o},{buffer:i}],[{buffer:a}],B),C=ke(N,y,o.shape,b);return new yt([g,C])}),Ct("fused_dot_epilogue",(e,t,s,n)=>{let r=t.getAttr("num_dot_operands")||2,i=s[0],o=s[1],a=s.slice(r),l=n[0],u=t.getAttr("epilogue_tags")||[],f=eh(e,t,i,o),h=xs(e,"ei",Array.from({length:l.shape.length},(B,N)=>N),l.shape,l),p=new Z(l,h.indices,new ut(0)),d=new st(e.blockName("matmul_init"),h.ivs,[],[{buffer:l}],p),m=h.wrap(d),g=new L("+",new H(l,f.outIdx),new L("*",new H(i,f.lhsIdx),new H(o,f.rhsIdx))),y=new Z(l,f.outIdx,g),_=new st(e.blockName("matmul_acc"),f.allIvs,[{buffer:i},{buffer:o}],[{buffer:l}],y),w=f.wrapAccBody(_);if(u.length===0)return new yt([m,w]);let b=xs(e,"ep",Array.from({length:l.shape.length},(B,N)=>N),l.shape,l),x=b.indices,A=new H(l,x),S={v:0};for(let B of u){let N=Zn.get(B);N&&(A=N(A,a,S,l,x))}let E=Ac([l,...a]),R=new Z(l,x,A),v=new st(e.blockName("epilogue"),b.ivs,E,[{buffer:l}],R),k=b.wrap(v);return new yt([m,w,k])}),Ct("cublas_gemm",Io("dot"))}var Zn,$N=I(()=>{Jt();It();Xn();c(DN,"asIndexValue");Zn=new Map;Zn.set("bias",(e,t,s,n,r)=>{let i=t[s.v++];return new L("+",e,new H(i,Ki(i,n,r)))});Zn.set("residual_add",(e,t,s,n,r)=>{let i=t[s.v++];return new L("+",e,new H(i,Ki(i,n,r)))});Zn.set("scale",(e,t,s,n,r)=>{let i=t[s.v++];return new L("*",e,new H(i,Ki(i,n,r)))});Zn.set("relu",(e,t,s,n)=>new vt("max",[e,new ut(0)],n.dtype));Zn.set("clamp",(e,t,s,n,r)=>{let i=t[s.v++],o=t[s.v++];return new vt("min",[new vt("max",[e,new H(i,Ki(i,n,r))],n.dtype),new H(o,Ki(o,n,r))],n.dtype)});Zn.set("neg",e=>new L("-",e));Zn.set("exp",(e,t,s,n)=>new vt("exp",[e],n.dtype));Zn.set("tanh",(e,t,s,n)=>new vt("tanh",[e],n.dtype));Zn.set("sqrt",(e,t,s,n)=>new vt("sqrt",[e],n.dtype));Zn.set("abs",(e,t,s,n)=>new vt("abs",[e],n.dtype));Zn.set("log",(e,t,s,n)=>new vt("log",[e],n.dtype));Zn.set("activation",e=>e);c(FN,"register")});function Ec(e,t,s){let{loopVars:n,loopBinds:r,indices:i,extentNodes:o}=ve(e,s.shape,s),a=new Z(s,i,new H(t,i)),l=new st(e.blockName("cf_copy"),r,[{buffer:t}],[{buffer:s}],a);return ke(l,n,s.shape,o)}function $$(e,t,s,n){let{loopVars:r,loopBinds:i,indices:o,extentNodes:a}=ve(e,s.shape,s),l=new Z(s,o,new H(t,[n,...o])),u=new st(e.blockName("scan_in"),i,[{buffer:t}],[{buffer:s}],l);return ke(u,r,s.shape,a)}function z$(e,t,s,n){let{loopVars:r,loopBinds:i,indices:o,extentNodes:a}=ve(e,t.shape,t),l=new Z(s,[n,...o],new H(t,o)),u=new st(e.blockName("scan_out"),i,[{buffer:t}],[{buffer:s}],l);return ke(u,r,t.shape,a)}function sh(e,t,s){let n=t.entryBlock,r=new Map;for(let o=0;o<n.arguments.length;o++)r.set(n.arguments[o],s[o]);let i=[];for(let o of n.ops()){if(o.opName==="yield"){let p=new Array(o.numOperands);for(let d=0;d<o.numOperands;d++)p[d]=r.get(o.getOperand(d))||e.getOrAllocBuffer(o.getOperand(d));return{stmts:i,yieldBuffers:p}}if(Ur.has(o.opName)){i.push(vc(e,o));continue}let a=new Array(o.numOperands);for(let p=0;p<o.numOperands;p++)a[p]=r.get(o.getOperand(p))||o.getOperand(p);let l=new Array(a.length);for(let p=0;p<a.length;p++)l[p]=a[p]instanceof ee?a[p]:e.getOrAllocBuffer(a[p]);let u=new Array(o.numResults);for(let p=0;p<o.numResults;p++){let d={type:o.getResult(p).type};u[p]=e.getOrAllocBuffer(d),r.set(o.getResult(p),u[p])}let f=Io(o.opName);if(!f)throw new Error(`No lowering rule for op '${o.opName}' inside region`);let h=f(e,o,l,u);h&&i.push(h)}return{stmts:i,yieldBuffers:[]}}function zN(){Ct("if",(e,t,s,n)=>{let r=s[0],i=new H(r,[]),o=t.regions[0],a=t.regions[1],l=new Array(t.numResults);for(let d=0;d<t.numResults;d++)l[d]=n[d]||e.getOrAllocBuffer(t.getResult(d)),e.bufferMap.set(t.getResult(d),l[d]);let u=sh(e,o,[]),f=u.stmts.slice();for(let d=0;d<l.length&&d<u.yieldBuffers.length;d++){let m=u.yieldBuffers[d];m&&m!==l[d]&&f.push(Ec(e,m,l[d]))}let h=f.length===1?f[0]:new yt(f),p=null;if(a&&a.entryBlock){let d=sh(e,a,[]),m=d.stmts.slice();for(let g=0;g<l.length&&g<d.yieldBuffers.length;g++){let y=d.yieldBuffers[g];y&&y!==l[g]&&m.push(Ec(e,y,l[g]))}m.length>0&&(p=m.length===1?m[0]:new yt(m))}return new Ht(i,h,p)}),Ct("while",(e,t,s,n)=>{let r=t.regions[0],i=t.regions[1],o=new Array(s.length),a=[];for(let y=0;y<s.length;y++){let _=n[y]||e.getOrAllocBuffer({type:t.getResult(y).type});o[y]=_,a.push(Ec(e,s[y],_))}for(let y=0;y<t.numResults;y++)e.bufferMap.set(t.getResult(y),o[y]);let l=new ee(`_wcond_${e.varCounter++}`,[],"bool",Sc.GLOBAL),u=sh(e,r,o),f=u.stmts.slice(),h=u.yieldBuffers[0];f.push(new Z(l,[],new H(h,[])));let p=f.length===1?f[0]:new yt(f),d=sh(e,i,o),m=d.stmts.slice();for(let y=0;y<o.length&&y<d.yieldBuffers.length;y++){let _=d.yieldBuffers[y];_&&_!==o[y]&&m.push(Ec(e,_,o[y]))}let g=m.length===1?m[0]:new yt(m);return new yt([...a,new xc(l,p,g)])}),Ct("scan",(e,t,s,n)=>{let r=t.getAttr("num_xs"),i=t.getAttr("num_carry"),o=s.slice(0,r),a=s.slice(r),l=new Array(i),u=[];for(let w=0;w<i;w++){let b=n[w]||e.getOrAllocBuffer({type:t.getResult(w).type});l[w]=b,u.push(Ec(e,a[w],b)),e.bufferMap.set(t.getResult(w),b)}let f=n.length-i,h=new Array(f);for(let w=0;w<f;w++){let b=n[i+w]||e.getOrAllocBuffer({type:t.getResult(i+w).type});h[w]=b,e.bufferMap.set(t.getResult(i+w),b)}let p=e.allocVar("t"),d=e.extentNode(o[0].shape[0],o[0],0),m=[],g=new Array(r);for(let w=0;w<r;w++){let b=e.getOrAllocBuffer({type:{shape:o[w].shape.slice(1),dtype:o[w].dtype}});g[w]=b,m.push($$(e,o[w],b,p))}let y=sh(e,t.regions[0],[...g,...l]);for(let w of y.stmts)m.push(w);for(let w=0;w<i;w++){let b=y.yieldBuffers[w];b&&b!==l[w]&&m.push(Ec(e,b,l[w]))}for(let w=0;w<f;w++)m.push(z$(e,y.yieldBuffers[i+w],h[w],p));m.push(new Qa);let _=m.length===1?m[0]:new yt(m);return new yt([...u,new wt(p,new M(0),d,V.RECURRENCE,_)])})}var UN=I(()=>{Jt();Xa();Mb();Xn();c(Ec,"copyBuffer");c($$,"sliceCopyIn");c(z$,"sliceCopyOut");c(sh,"lowerRegionBody");c(zN,"register")});function VN(){Ct("layout_transform",(e,t,s,n)=>{let r=s[0],i=n[0],o=t.getAttr("src_layout"),a=t.getAttr("dst_layout"),{loopVars:l,loopBinds:u,indices:f,extentNodes:h}=ve(e,i.shape,i),p=new Z(i,f,new H(r,f)),d=new st(e.blockName("layout_transform_block"),u,[{buffer:r}],[{buffer:i}],p);return ke(d,l,i.shape,h)})}var GN=I(()=>{Jt();Xn();c(VN,"register")});function qN(){Ct("quantize",(e,t,s,n)=>{let r=t.getAttr("scale"),i=t.getAttr("zero_point"),o=t.getAttr("target_dtype")||"i8",a=o==="ui8",l=8,u=a?0:-(1<<l-1),f=a?(1<<l)-1:(1<<l-1)-1;return fi(e,t,s,n,(h,p)=>{let d=new L("/",p[0],new ut(r)),m=i!==0?new L("+",d,new ut(i)):d,g=new vt("round",[m],"f32"),y=new vt("min",[new vt("max",[g,new ut(u)],"f32"),new ut(f)],"f32");return new ie(y,"f32",o)})}),Ct("dequantize",(e,t,s,n)=>{let r=t.getAttr("scale"),i=t.getAttr("zero_point"),o=s[0].dtype||"i8",a=t.getAttr("target_dtype")||"f32";return fi(e,t,s,n,(l,u)=>{let f=new ie(u[0],o,a),h=i!==0?new L("-",f,new ut(i)):f;return new L("*",h,new ut(r))})}),Ct("quantized_dot",(e,t,s,n)=>{let r=s[0],i=s[1],o=n[0],a=eh(e,t,r,i),l=xs(e,"qdi",Array.from({length:o.shape.length},(E,R)=>R),o.shape,o),u=new Z(o,l.indices,new M(0)),f=new st(e.blockName("qmatmul_init"),l.ivs,[],[{buffer:o}],u),h=l.wrap(f),p=new ie(new H(r,a.lhsIdx),r.dtype,"i32"),d=new ie(new H(i,a.rhsIdx),i.dtype,"i32"),m=t.getAttr("lhs_zero_point")||0,g=t.getAttr("rhs_zero_point")||0,y=m!==0?new L("-",p,new M(m)):p,_=g!==0?new L("-",d,new M(g)):d,w=new L("*",y,_),b=new L("+",new H(o,a.outIdx),w),x=new Z(o,a.outIdx,b),A=new st(e.blockName("qmatmul_acc"),a.allIvs,[{buffer:r},{buffer:i}],[{buffer:o}],x),S=a.wrapAccBody(A);return new yt([h,S])}),Ct("quantized_conv",(e,t,s,n)=>{let r=s[0],i=s[1],o=n[0],a=t.getAttr("strides"),l=t.getAttr("padding"),u=t.getAttr("dilation")||a.map(()=>1),f=t.getAttr("groups")||1,h=Oo(t.getAttr("input_layout")),p=Oo(t.getAttr("kernel_layout")),d=a.length,m=i.shape[p.O],g=i.shape[p.I],y=r.shape[h.N],_=o.shape,w=t.getAttr("input_zero_point")||0,b=t.getAttr("kernel_zero_point")||0,x=xs(e,"qci",Array.from({length:_.length},(Bt,xe)=>xe),_,o),A=new Z(o,x.indices,new M(0)),S=new st(e.blockName("qconv_init"),x.ivs,[],[{buffer:o}],A),E=x.wrap(S),R=e.allocVar("qcn"),v=e.allocVar("qcoc"),k=e.allocVar("qcic"),B=e.allocVarArray("qco",d),N=e.allocVarArray("qck",d),C=[R,v,...B,k,...N],O=e.allocBindArray("qcv",C),F=O[0].iterVar,P=O[1].iterVar,z=O.slice(2,2+d),q=O[2+d].iterVar,U=O.slice(3+d),D=new Array(_.length);D[h.N]=F,D[h.C]=P;let G=Object.keys(h).filter(Bt=>Bt!=="N"&&Bt!=="C").sort();for(let Bt=0;Bt<d;Bt++)D[h[G[Bt]]]=z[Bt].iterVar;let At=new Array(r.shape.length);At[h.N]=F;let pt=Math.floor(m/f);f>1?At[h.C]=new L("+",new L("*",new L("//",P,new M(pt)),new M(g)),q):At[h.C]=q;let mt=new Array(i.shape.length);mt[p.O]=P,mt[p.I]=q;let $=null;for(let Bt=0;Bt<d;Bt++){let xe=G[Bt],yr=xe.toUpperCase(),ai=ds("+",ds("*",z[Bt].iterVar,new M(a[Bt])),ds("+",ds("*",U[Bt].iterVar,new M(u[Bt])),new M(-l[Bt][0])));if(At[h[xe]]=ai,mt[p[yr]]=U[Bt].iterVar,l[Bt][0]!==0||l[Bt][1]!==0){let Eo=new fe("ge",ai,new M(0)),Gi=new fe("lt",ai,new M(r.shape[h[xe]])),ic=new L("*",Eo,Gi);$=$?new L("*",$,ic):ic}}let K=new ie(new H(r,At),r.dtype,"i32"),_t=new ie(new H(i,mt),i.dtype,"i32"),dt=w!==0?new L("-",K,new M(w)):K,jt=b!==0?new L("-",_t,new M(b)):_t,Pn=new L("*",dt,jt),gn=$?new Ht($,Pn,new M(0)):Pn,yn=new H(o,D),ts=new L("+",yn,gn),Rt=new Z(o,D,ts),Ce=new st(e.blockName("qconv_acc"),O,[{buffer:r},{buffer:i}],[{buffer:o}],Rt),Vi=new Array(d);for(let Bt=0;Bt<d;Bt++){let xe=G[Bt].toUpperCase();Vi[Bt]=i.shape[p[xe]]}let es=Ce;for(let Bt=d-1;Bt>=0;Bt--){let xe=G[Bt].toUpperCase();es=new wt(N[Bt],new M(0),e.extentNode(Vi[Bt],i,p[xe]),V.SERIAL,es)}es=new wt(k,new M(0),e.extentNode(g,i,p.I),V.SERIAL,es);for(let Bt=d-1;Bt>=0;Bt--){let xe=h[G[Bt]];es=new wt(B[Bt],new M(0),e.extentNode(_[xe],o,xe),V.SERIAL,es)}return es=new wt(v,new M(0),e.extentNode(m,i,p.O),V.SERIAL,es),es=new wt(R,new M(0),e.extentNode(y,r,h.N),V.SERIAL,es),new yt([E,es])})}var jN=I(()=>{Jt();Xn();c(qN,"register")});function Za(e){return tr.has(e)}function U$(){for(let e of Object.keys(kc))tr.set(e,(t,s,n)=>Ub(t.opName,s,n));tr.set("compare",(e,t)=>new fe(e.getAttr("direction")||"eq",t[0],t[1])),tr.set("select",(e,t)=>new Ht(t[0],t[1],t[2])),tr.set("clamp",(e,t,s)=>new vt("min",[new vt("max",[t[1],t[0]],s),t[2]],s)),tr.set("convert",(e,t)=>new ie(t[0],e.getOperand(0).type.dtype,e.getAttr("target_dtype")||e.getResult(0).type.dtype)),tr.set("broadcast_in_dim",(e,t)=>t[0]),tr.set("broadcast",(e,t)=>t[0]),tr.set("iota",()=>{throw new Error("iota fusion must be handled by the index-aware path in lowerFusion")}),tr.set("quantize",(e,t)=>{let s=e.getAttr("scale"),n=e.getAttr("zero_point"),r=e.getAttr("target_dtype")||"i8",i=r==="ui8",o=i?0:-128,a=i?255:127,l=new L("/",t[0],new ut(s)),u=new L("+",l,new ut(n)),f=new vt("round",[u],"f32"),h=new vt("min",[new vt("max",[f,new ut(o)],"f32"),new ut(a)],"f32");return new ie(h,"f32",r)}),tr.set("dequantize",(e,t)=>{let s=e.getAttr("scale"),n=e.getAttr("zero_point"),r=e.getOperand(0).type?.dtype||"i8",i=e.getAttr("target_dtype")||"f32",o=new ie(t[0],r,i),a=new L("-",o,new ut(n));return new L("*",a,new ut(s))})}function WN(e,t){let s=t.numOperands,n=t.numResults,r=new Array(s);for(let v=0;v<s;v++)r[v]=e.getOrAllocBuffer(t.getOperand(v));let i=new Array(n);for(let v=0;v<n;v++)i[v]=e.getOrAllocBuffer(t.getResult(v));let o=i[0],{loopVars:a,loopBinds:l,indices:u,extentNodes:f}=ve(e,o.shape,o),h=new Map,p=t.regions[0].entryBlock,d=new Map,m=[...p.ops()];for(let v=m.length-1;v>=0;v--){let k=m[v];if(k.opName==="broadcast_in_dim"||k.opName==="broadcast"){let B=k.getAttr("broadcast_dimensions");B&&B.length>0&&d.set(k.getOperand(0),B);continue}if(!(k.opName==="yield"||Ur.has(k.opName)))for(let B=0;B<k.numResults;B++){let N=d.get(k.getResult(B));if(N)for(let C=0;C<k.numOperands;C++)d.has(k.getOperand(C))||d.set(k.getOperand(C),N)}}let g=new Map,y=p.arguments;for(let v=0;v<y.length;v++){let k=d.get(y[v]);k&&g.set(v,k)}for(let v=0;v<p.arguments.length;v++){let k=g.get(v),B;if(k){let N=r[v];B=new Array(N.shape.length);for(let C=0;C<N.shape.length;C++)B[C]=N.shape[C]===1?new M(0):u[k[C]]}else B=Ki(r[v],o,u);h.set(p.arguments[v],new H(r[v],B))}let _=new Map;for(let v of p.ops())for(let k=0;k<v.numOperands;k++){let B=v.getOperand(k);_.set(B,(_.get(B)||0)+1)}let w=new Map,b=0,x=[];function A(v){let k=h.get(v);if(k===void 0)throw new Error(`Fusion lowering: unmapped operand from '${v.definingOp?v.definingOp.opName:"unknown"}'`);if((_.get(v)||0)>1&&!V$.has(k.type)){if(!w.has(v)){let B=k.type==="CompareNode"?"i32":k.type==="CastNode"?k.toDtype:o.dtype,N=e.allocVar(`cse${b++}`,B);w.set(v,N),x.push({variable:N,value:k}),h.set(v,N)}return w.get(v)}return k}c(A,"getExpr");let S=[];for(let v of p.ops()){if(v.opName==="yield"){for(let N=0;N<v.numOperands;N++)S.push(new Z(i[N],u,A(v.getOperand(N))));break}if(Ur.has(v.opName)){let N=v.getAttr("value");h.set(v.getResult(0),new ut(typeof N=="number"?N:0));continue}if(v.opName==="iota"){let N=v.getAttr("iota_dimension")??v.getAttr("dimension")??0;h.set(v.getResult(0),u[N]);continue}let k=tr.get(v.opName);if(!k)throw new Error(`Fusion lowering: unsupported op '${v.opName}' inside fusion body`);let B=new Array(v.numOperands);for(let N=0;N<v.numOperands;N++)B[N]=A(v.getOperand(N));h.set(v.getResult(0),k(v,B,o.dtype))}let E=S.length===1?S[0]:new yt(S);for(let v=x.length-1;v>=0;v--)E=new Sn(x[v].variable,x[v].value,E);let R=new st(e.blockName("fusion_block"),l,Ac(r),Ac(i),E);return ke(R,a,o.shape,f)}function G$(e,t){if(e.length!==t.length)return!1;for(let s=0;s<e.length;s++)if(e[s]!==t[s])return!1;return!0}function KN(e){let t=e.regions[0];if(!t)return!1;for(let s of t.entryBlock.ops())if(s.opName!=="yield"){if(Ur.has(s.opName)){if(typeof s.getAttr("value")=="number")continue;return!1}if(!tr.has(s.opName))return!1}if(e.numResults>1){let s=e.getResult(0).type.shape;for(let n=1;n<e.numResults;n++)if(!G$(e.getResult(n).type.shape,s))return!1}return!0}function HN(e,t,s){let n=t.regions[0].entryBlock,r=new Map;for(let o=0;o<n.arguments.length;o++)r.set(n.arguments[o],t.getOperand(o));let i=new Map;for(let o of n.ops())if(o.opName==="yield"){for(let a=0;a<o.numOperands;a++)i.set(o.getOperand(a),t.getResult(a));break}for(let o of n.ops()){if(o.opName==="yield")continue;let a=new Array(o.numOperands);for(let p=0;p<o.numOperands;p++)a[p]=r.get(o.getOperand(p))||o.getOperand(p);let l=new Array(a.length);for(let p=0;p<a.length;p++)l[p]=e.getOrAllocBuffer(a[p]);let u=new Array(o.numResults);for(let p=0;p<o.numResults;p++){let d=o.getResult(p),m=i.get(d);if(m){let g=e.getOrAllocBuffer(m);u[p]=g,r.set(d,m)}else{let g={type:d.type};u[p]=e.getOrAllocBuffer(g),r.set(d,g)}}if(Ur.has(o.opName)){s.push(vc(e,o));continue}let f=Io(o.opName);if(!f)throw new Error(`Fusion lowering: no lowering rule for op '${o.opName}' inside fusion body`);let h=f(e,o,l,u);h&&s.push(h)}}function JN(){U$()}var tr,V$,YN=I(()=>{Jt();Xn();am();tr=new Map;c(Za,"canInlineFuse");c(U$,"initBuiltinFusionBuilders");V$=new Set(["BufferLoadNode","VariableNode","IntImmNode","FloatImmNode"]);c(WN,"lowerFusion");c(G$,"shapesEqual");c(KN,"canLowerAsElementwiseFusion");c(HN,"lowerFusionAsIndividualOps");c(JN,"register")});function QN(e,t,s,n,r){let i=new L("-",new L("*",e,new M(t)),new M(n)),o=new L("+",i,new M(s)),a=new vt("max",[i,new M(0)],"index"),l=new vt("min",[o,new M(r)],"index"),u=new L("-",l,a);return new vt("max",[u,new M(0)],"index")}function q$(e,t,s,n,r,i,o,a,l,u,f){let h=QN(e,s,r,o,l),p=QN(t,n,i,a,u),d=new L("*",h,p);return new ie(d,"index",f)}function XN(){Ct("pool2d",(e,t,s,n)=>{let r=s[0],i=n[0],o=t.getAttr("pool_type"),a=t.getAttr("kernel_size"),l=t.getAttr("strides"),u=t.getAttr("padding"),f=t.getAttr("count_include_pad")||!1,h=Oo(t.getAttr("layout")||"NCHW"),p=h.N,d=h.C,m=h.H,g=h.W,y=r.shape[p],_=r.shape[d],w=r.shape[m],b=r.shape[g],x=i.shape[m],A=i.shape[g],S=a[0],E=a[1],R=l[0],v=l[1],k=u[0][0],B=u[1][0],N=o==="max",C=N?new ut(-1/0):new ut(0),O=xs(e,"pi",[0,1,2,3],i.shape,i),F=new Z(i,O.indices,C),P=new st(e.blockName("pool_init"),O.ivs,[],[{buffer:i}],F),z=O.wrap(P),q=e.allocVar("pn"),U=e.allocVar("pc"),D=e.allocVar("poh"),G=e.allocVar("pow"),At=e.allocVar("pkh"),pt=e.allocVar("pkw"),mt=[q,U,D,G,At,pt],$=e.allocBindArray("pv",mt),K=$[0].iterVar,_t=$[1].iterVar,dt=$[2].iterVar,jt=$[3].iterVar,Pn=$[4].iterVar,gn=$[5].iterVar,yn=new L("+",new L("*",dt,new M(R)),new L("-",Pn,new M(k))),ts=new L("+",new L("*",jt,new M(v)),new L("-",gn,new M(B))),Rt=new fe("ge",yn,new M(0)),Ce=new fe("lt",yn,new M(w)),Vi=new fe("ge",ts,new M(0)),es=new fe("lt",ts,new M(b)),Bt=new L("*",new L("*",Rt,Ce),new L("*",Vi,es)),xe=new Array(4);xe[p]=K,xe[d]=_t,xe[m]=dt,xe[g]=jt;let yr=new Array(4);yr[p]=K,yr[d]=_t,yr[m]=yn,yr[g]=ts;let ai=new H(r,yr),Eo=new H(i,xe),Gi;if(N){let _r=new Ht(Bt,ai,new ut(-1/0));Gi=new vt("max",[Eo,_r],i.dtype)}else{let _r=new Ht(Bt,ai,new ut(0));Gi=new L("+",Eo,_r)}let ic=new Z(i,xe,Gi),Ls=new st(e.blockName("pool_acc"),$,[{buffer:r}],[{buffer:i}],ic);Ls=new wt(pt,new M(0),new M(E),V.SERIAL,Ls),Ls=new wt(At,new M(0),new M(S),V.SERIAL,Ls),Ls=new wt(G,new M(0),new M(A),V.SERIAL,Ls),Ls=new wt(D,new M(0),new M(x),V.SERIAL,Ls),Ls=new wt(U,new M(0),new M(_),V.SERIAL,Ls),Ls=new wt(q,new M(0),new M(y),V.SERIAL,Ls);let Ga=[z,Ls];if(!N){let _r=xs(e,"pd",[0,1,2,3],i.shape,i),sb=_r.indices[m],md=_r.indices[g],nb=f?new ut(S*E):q$(sb,md,R,v,S,E,k,B,w,b,i.dtype),gd=new H(i,_r.indices),Df=f?new L("*",gd,new ut(1/(S*E))):new L("/",gd,nb),Ff=new Z(i,_r.indices,Df),yd=new st(e.blockName("pool_div"),_r.ivs,[{buffer:i}],[{buffer:i}],Ff);Ga.push(_r.wrap(yd))}return new yt(Ga)})}var ZN=I(()=>{Jt();Xn();c(QN,"spatialCount");c(q$,"avgPoolDivisorExpr");c(XN,"register")});function tR(){Ct("resize",(e,t,s,n)=>{let r=s[0],i=n[0],o=t.getAttr("method"),a=r.shape[2],l=r.shape[3],u=i.shape[2],f=i.shape[3],h=xs(e,"rz",[0,1,2,3],i.shape,i),[p,d,m,g]=h.indices,y=a/u,_=l/f,w;if(o==="nearest"){let A=new vt("floor",[new L("*",m,new ut(y))],"f32"),S=new vt("floor",[new L("*",g,new ut(_))],"f32"),E=new ie(new vt("min",[new vt("max",[A,new ut(0)],"f32"),new ut(a-1)],"f32"),"f32","i32"),R=new ie(new vt("min",[new vt("max",[S,new ut(0)],"f32"),new ut(l-1)],"f32"),"f32","i32");w=new H(r,[p,d,E,R])}else{let A=new L("*",m,new ut(y)),S=new L("*",g,new ut(_)),E=new vt("floor",[A],"f32"),R=new vt("floor",[S],"f32"),v=new vt("min",[new L("+",E,new ut(1)),new ut(a-1)],"f32"),k=new vt("min",[new L("+",R,new ut(1)),new ut(l-1)],"f32"),B=new L("-",A,E),N=new L("-",S,R),C=new vt("max",[E,new ut(0)],"f32"),O=new vt("max",[R,new ut(0)],"f32"),F=new ie(C,"f32","i32"),P=new ie(O,"f32","i32"),z=new ie(v,"f32","i32"),q=new ie(k,"f32","i32"),U=new H(r,[p,d,F,P]),D=new H(r,[p,d,F,q]),G=new H(r,[p,d,z,P]),At=new H(r,[p,d,z,q]),pt=new L("-",new ut(1),N),mt=new L("-",new ut(1),B),$=new L("+",new L("*",U,pt),new L("*",D,N)),K=new L("+",new L("*",G,pt),new L("*",At,N));w=new L("+",new L("*",$,mt),new L("*",K,B))}let b=new Z(i,h.indices,w),x=new st(e.blockName("resize_block"),h.ivs,[{buffer:r}],[{buffer:i}],b);return h.wrap(x)})}var eR=I(()=>{Jt();Xn();c(tR,"register")});function sR(){Ct("scaled_dot_product_attention",(e,t,s,n)=>{if(t.getAttr("causal"))throw new Error("causal flash attention lowering not yet supported");let[r,i,o]=s,a=n[0],l=t.getAttr("scale"),u=r.dtype,f=r.shape[0],h=r.shape[1],p=r.shape[2],d=r.shape[3],m=i.shape[2],g=o.shape[3],y=new ee(e.blockName("fa_m"),[1],u,"local"),_=new ee(e.blockName("fa_l"),[1],u,"local"),w=new ee(e.blockName("fa_s"),[1],u,"local"),b=new ee(e.blockName("fa_mn"),[1],u,"local"),x=new ee(e.blockName("fa_p"),[1],u,"local"),A=new ee(e.blockName("fa_cr"),[1],u,"local"),S=new ee(e.blockName("fa_o"),[g],u,"local"),E=new Ae(e.blockName("fa_b"),"int32"),R=new Ae(e.blockName("fa_h"),"int32"),v=new Ae(e.blockName("fa_i"),"i32"),k=new Ae(e.blockName("fa_j"),"int32"),B=new Ae(e.blockName("fa_d"),"int32"),N=new Ae(e.blockName("fa_di"),"int32"),C=new Ae(e.blockName("fa_do"),"int32"),O=new Ae(e.blockName("fa_dw"),"int32"),F=new st(e.blockName("fa_initm"),[],[],[{buffer:y}],er(y,[Zt],new ut(-1/0))),P=new st(e.blockName("fa_initl"),[],[],[{buffer:_}],er(_,[Zt],new ut(0))),z=nh(N,g,new st(e.blockName("fa_inito"),[],[],[{buffer:S}],er(S,[N],new ut(0)))),q=new st(e.blockName("fa_sinit"),[],[],[{buffer:w}],er(w,[Zt],new ut(0))),U=nh(B,d,new st(e.blockName("fa_sred"),[],[{buffer:r},{buffer:i},{buffer:w}],[{buffer:w}],er(w,[Zt],sr("+",ms(w,[Zt]),sr("*",ms(r,[E,R,v,B]),ms(i,[E,R,k,B])))))),D=new yt([q,U]),G=new st(e.blockName("fa_supd"),[],[{buffer:w},{buffer:y},{buffer:_}],[{buffer:w},{buffer:b},{buffer:x},{buffer:A},{buffer:_}],new yt([er(w,[Zt],sr("*",ms(w,[Zt]),new ut(l))),er(b,[Zt],Vb("max",ms(y,[Zt]),ms(w,[Zt]))),er(x,[Zt],Vb("exp",sr("-",ms(w,[Zt]),ms(b,[Zt])))),er(A,[Zt],Vb("exp",sr("-",ms(y,[Zt]),ms(b,[Zt])))),er(_,[Zt],sr("+",sr("*",ms(_,[Zt]),ms(A,[Zt])),ms(x,[Zt])))])),At=nh(C,g,new st(e.blockName("fa_oupd"),[],[{buffer:S},{buffer:A},{buffer:x},{buffer:o}],[{buffer:S}],er(S,[C],sr("+",sr("*",ms(S,[C]),ms(A,[Zt])),sr("*",ms(x,[Zt]),ms(o,[E,R,k,C])))))),pt=new st(e.blockName("fa_mupd"),[],[{buffer:b}],[{buffer:y}],er(y,[Zt],ms(b,[Zt]))),mt=nh(k,m,new yt([D,G,At,pt])),$=nh(O,g,new st(e.blockName("fa_finw"),[],[{buffer:S},{buffer:_}],[{buffer:a}],er(a,[E,R,v,O],sr("/",ms(S,[O]),ms(_,[Zt]))))),K=new yt([F,P,z,mt,$]);for(let gn of[S,A,x,b,w,_,y])K=new Hs(gn,gn.scope,K);let _t=Math.min(p,256),dt=Math.ceil(p/_t),jt;if(dt===1)jt=new wt(v,Zt,new M(p),V.THREAD_BINDING,K,"threadIdx.x");else{let gn=new Ae(e.blockName("fa_ii"),"i32"),yn=new Ae(e.blockName("fa_io"),"i32"),ts=sr("+",sr("*",yn,new M(_t)),gn),Rt=dt*_t===p?K:new Ht(new fe("lt",v,new M(p)),K),Ce=new wt(gn,Zt,new M(_t),V.THREAD_BINDING,new Sn(v,ts,Rt),"threadIdx.x");jt=new wt(yn,Zt,new M(dt),V.THREAD_BINDING,Ce,"blockIdx.z")}let Pn=new wt(R,Zt,new M(h),V.THREAD_BINDING,jt,"blockIdx.y");return new wt(E,Zt,new M(f),V.THREAD_BINDING,Pn,"blockIdx.x")})}var Zt,ms,er,sr,Vb,nh,nR=I(()=>{Jt();Xa();Xn();Zt=new M(0),ms=c((e,t)=>new H(e,t),"ld"),er=c((e,t,s)=>new Z(e,t,s),"st"),sr=c((e,t,s)=>new L(e,t,s),"mop"),Vb=c((e,...t)=>new vt(e,t),"ext"),nh=c((e,t,s,n=V.SERIAL)=>new wt(e,Zt,new M(t),n,s),"forL");c(sR,"register")});function rR(e,t,s=new Set){if(!j$.has(t.opName))return!1;if(t.opName!=="fusion")return!0;let n=t.regions[0];if(!n)return!1;let r=n.entryBlock;for(let i=0;i<t.numOperands;i++){if(t.getOperand(i)!==e)continue;let o=r.arguments[i];for(let a of o.getUsers()){let l=`${o.id}:${a.id}`;if(!s.has(l)&&(s.add(l),!rR(o,a,s)))return!1}}return!0}function W$(e){let t=[];for(let o of e.ops())t.push(o);let s=new Set(t),n=[],r=new Map,i=c(o=>{if(r.get(o)===2)return;r.set(o,1);let a=[{op:o,i:0}];for(;a.length>0;){let l=a[a.length-1],u=l.op;if(l.i<u.numOperands){let f=u.getOperand(l.i).definingOp;l.i++,f&&s.has(f)&&r.get(f)===void 0&&(r.set(f,1),a.push({op:f,i:0}));continue}r.set(u,2),n.push(u),a.pop()}},"visit");for(let o of t)i(o);return n}function rh(e,t=null){let s=new om,n=[],r=new Map;for(let m of e.args){let g=s.allocVar("arg");n.push(g),r.set(g,s.getOrAllocBuffer(m))}let i=e.getReturnOp(),o=new Set;for(let m of e.args)o.add(s.getOrAllocBuffer(m));let a=[],l=new Set;for(let m=0;m<i.numOperands;m++){let g=s.allocVar("ret");n.push(g);let y=s.getOrAllocBuffer(i.getOperand(m));if(o.has(y)||l.has(y)){let _=s.allocFreshBuffer(i.getOperand(m));r.set(g,_),a.push({src:y,dst:_})}else r.set(g,y),l.add(y)}let u=new Set;for(let m=0;m<i.numOperands;m++)u.add(i.getOperand(m));let f=[];for(let m of e.ops())Ur.has(m.opName)&&f.push(vc(s,m));for(let m of W$(e)){if(m.opName==="return"||m.opName==="yield"||Ur.has(m.opName))continue;if(m.opName==="fusion"){KN(m)?f.push(WN(s,m)):HN(s,m,f);continue}if((m.opName==="broadcast_in_dim"||m.opName==="broadcast")&&!u.has(m.getResult(0))&&m.getOperand(0).getUsers().length===1&&m.getResult(0).getUsers().every(b=>rR(m.getResult(0),b))){let b=s.getOrAllocBuffer(m.getOperand(0)),x=m.getResult(0).type.shape,A=m.getAttr("broadcast_dimensions"),S;if(A&&A.length>0)S=A;else{let E=x.length-b.shape.length;S=Array.from({length:b.shape.length},(R,v)=>v+E)}b.broadcastDims=S,s.bufferMap.set(m.getResult(0),b);continue}let g=Io(m.opName,t);if(!g)throw new Error(`No lowering rule defined for op: ${m.opName}`);let y=new Array(m.numOperands);for(let b=0;b<m.numOperands;b++)y[b]=s.getOrAllocBuffer(m.getOperand(b));let _=new Array(m.numResults);for(let b=0;b<m.numResults;b++)_[b]=s.getOrAllocBuffer(m.getResult(b));let w=g(s,m,y,_);w&&f.push(w)}for(let{src:m,dst:g}of a){let{loopVars:y,loopBinds:_,indices:w,extentNodes:b}=ve(s,m.shape,m),x=new H(m,w),A=new Z(g,w,x),S=new st(s.blockName("copy_block"),_,[{buffer:m}],[{buffer:g}],A);f.push(ke(S,y,m.shape,b))}let h=[],p=new Set;for(let m of s.shapeParams.values())p.has(m.name)||(p.add(m.name),h.push(m));for(let m of h)n.push(m);let d=new To(e.name,n,f.length===1?f[0]:new yt(f),r,h,new Map(s.shapeParams));return e._partitionTarget&&(d._partitionTarget=e._partitionTarget),d}var j$,Nc=I(()=>{Jt();Xn();am();ON();PN();$N();UN();GN();jN();YN();ZN();eR();nR();am();j$=new Set([...Object.keys(kc),"compare","select","clamp","convert","copy_to_device","dot","fusion"]);c(rR,"broadcastViewSafeForUser");CN();IN();LN();FN();zN();VN();qN();JN();XN();tR();sR();c(W$,"topologicalOps");c(rh,"lowerGraphToPrimFunc")});function iR(e){let t=new lm(e);return t.register("dot","blas",{dtypes:["f32","f64"],minElements:64}),t}function oR(e){let t=new lm(e);return t.register("dot","cublas",{dtypes:["f32","f64","f16"]}),t.register("conv","cudnn",{dtypes:["f32","f16"]}),t}var Gb,lm,aR=I(()=>{bn();Gb=class{static{c(this,"LibraryCall")}constructor(t,s,n,r={}){this.opName=t,this.libraryName=s,this.functionName=n,this.constraints=r}},lm=class{static{c(this,"LibrarySelector")}constructor(t){this.target=t,this._registry=new Map}register(t,s,n={}){let r=this._registry.get(t);r||(r=[],this._registry.set(t,r)),r.push({libraryName:s,constraints:n})}select(t,s,n){let r=this._registry.get(t);if(!r)return null;for(let i of r){if(!this._matchesConstraints(i.constraints,s,n))continue;let o=jE(t,i.libraryName,n);if(o)return new Gb(t,i.libraryName,o,i.constraints)}return null}shouldUseLibrary(t,s,n){return this.select(t,s,n)!==null}_matchesConstraints(t,s,n){if(t.minElements){let r=1;for(let i=0;i<s.length;i++)r*=s[i];if(r<t.minElements)return!1}return!(t.dtypes&&t.dtypes.indexOf(n)<0||t.maxRank&&s.length>t.maxRank)}};c(iR,"createCPULibrarySelector");c(oR,"createGPULibrarySelector")});var lR={};Xt(lR,{CPUTarget:()=>nr,CUDATarget:()=>el,TargetFeatures:()=>tl,TargetKind:()=>Te,WasmTarget:()=>sl,WebGPUTarget:()=>nl});var Te,tl,nr,el,sl,nl,hi=I(()=>{Te=Object.freeze({CPU:"cpu",CUDA:"cuda",WEBGPU:"webgpu",WASM:"wasm",ACCELERATOR:"accelerator"}),tl=class{static{c(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,s=null){return this.attrs.has(t)?this.attrs.get(t):s}hasAttr(t){return this.attrs.has(t)}withAttr(t,s){return this.attrs.set(t,s),this}isGPU(){return this.kind===Te.CUDA||this.kind===Te.WEBGPU}isWebGPU(){return this.kind===Te.WEBGPU}isCPU(){return this.kind===Te.CPU}isWasm(){return this.kind===Te.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)}},nr=c((e={})=>new tl({kind:Te.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,...e}),"CPUTarget"),el=c((e={})=>new tl({kind:Te.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,...e}),"CUDATarget"),sl=c((e={})=>new tl({kind:Te.WASM,name:"wasm_generic",vectorWidth:4,numCores:1,cacheLineSizeBytes:64,memoryBandwidthGBs:10,computeTFLOPs:.1,supportsInt8:!0,simd:!0,...e}),"WasmTarget"),nl=c((e={})=>new tl({kind:Te.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,...e}),"WebGPUTarget")});function fR(e){return Hi[0]=(e&32767)<<13,Mo[0]*=uR,Mo[0]>=65536&&(Hi[0]|=2139095040),Hi[0]|=(e&32768)<<16,Mo[0]}function hR(e){Mo[0]=e;let t=Hi[0]&2147483648,s=(Hi[0]^t)>>>0,n;if(s>=1199570944)n=s>2139095040?32256:31744;else if(s<947912704)Hi[0]=s,Mo[0]+=.5,n=Hi[0]-1056964608&65535;else{let r=s>>13&1;s=s+cR>>>0,s=s+r>>>0,n=s>>13&65535}return(n|t>>>16)&65535}function cm(e){return Hi[0]=e<<16>>>0,Mo[0]}function um(e){Mo[0]=e;let t=Hi[0];if((t>>>23&255)===255)return t>>>16&65535;let s=32767+(t>>>16&1)>>>0;return t+s>>>0>>>16&65535}function fm(e,t){return e==="f16"?hR(t):e==="bf16"?um(t):e==="i64"?typeof t=="bigint"?t:BigInt(Math.trunc(t)):t}function hm(e,t){return e==="f16"?fR(t):e==="bf16"?cm(t):e==="i64"&&typeof t=="bigint"?Number(t):t}var Mo,Hi,cR,uR,qb,pm,Rc=I(()=>{Mo=new Float32Array(1),Hi=new Uint32Array(Mo.buffer),cR=(-112<<23>>>0)+4095>>>0,uR=5192296858534828e18;c(fR,"f16ToF32");c(hR,"f32ToF16");c(cm,"bf16ToF32");c(um,"f32ToBf16");c(fm,"coerceForStorage");c(hm,"readFromStorage");qb={F16_ADD_BIAS:cR|0,F16_MAGIC_MUL:uR},pm=globalThis;pm.__mlfw_f16_to_f32=fR;pm.__mlfw_f32_to_f16=hR;pm.__mlfw_bf16_to_f32=cm;pm.__mlfw_f32_to_bf16=um});var jb,pR,dm,dR=I(()=>{Jt();bn();Rc();jb={f16:"__mlfw_f16_to_f32",bf16:"__mlfw_bf16_to_f32"},pR={f16:"__mlfw_f32_to_f16",bf16:"__mlfw_f32_to_bf16"},dm=class{static{c(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 s=t.type==="LIRFunc",n=new Set,r=[];for(let[,u]of t.bufferMap)r.push(u.name),n.add(u.name);for(let u of t.shapeParams)r.push(u.name);this._paramBuffers=n,this._readBuffers=new Set;let i,o,a,l;s?(i=t.metadata.usedBuffers,o=t.metadata.allocatedBuffers,a=t.metadata.zeroBuffers,l=t.metadata.constantBuffers):(i=new Map,o=new Set,this._scanTree(t.body,i,o,this._readBuffers),a=this._findZeroOnlyBuffers(t.body,n),l=this._constantBuffers),this._zeroBuffers=a,this._constantBuffers=l,this._localBuffers=new Set,this._primFunc=t,this._emit(`function ${t.name}(${r.join(", ")}) {`),this._indent++;for(let[u,f]of i)if(!a.has(u)&&!l.has(u)&&!n.has(u)&&!o.has(u)){let h=f.numel();if(h>0)this._emit(`const ${u} = new ${Kf(f.dtype)}(${h});`);else if(h<0){let p=[];for(let d=0;d<f.shape.length;d++)p.push(typeof f.shape[d]=="number"&&f.shape[d]>=0?String(f.shape[d]):this._resolveShapeParam(f,d));this._emit(`const ${u} = new ${Kf(f.dtype)}(${p.join(" * ")});`)}}for(let[,u]of i)n.has(u.name)||this._localBuffers.add(u.name);return this._visitNode(t.body),this._indent--,this._emit("}"),this._cleanupSource(this._lines.join(`
3
- `))}_emit(t){this._lines.push(" ".repeat(this._indent)+t)}_wrapLoad(t,s){let n=jb[t];return n?`${n}(${s})`:s}_wrapStoreVal(t,s){let n=pR[t];return n?`${n}(${s})`:t==="i64"?`BigInt(${s})`:s}_zeroLit(t){return t==="i64"?"0n":"0"}_visitNode(t){let s=t;for(;s;)switch(s.type){case"SeqNode":for(let n=0;n<s.stmts.length-1;n++)this._visitNode(s.stmts[n]);s=s.stmts[s.stmts.length-1];continue;case"AllocateNode":{let n=s.buffer,r=n.numel();r>0?this._emit(`const ${n.name} = new ${Kf(n.dtype)}(${r});`):r<0&&this._emit(`const ${n.name} = new ${Kf(n.dtype)}(${this._dynamicNumel(n)});`),s=s.body;continue}case"LetStmtNode":this._emit(`const ${s.variable.name} = ${this._exprToJS(s.value)};`),s=s.body;continue;case"ForNode":this._visitForNode(s);return;case"BlockNode":this._visitBlockNode(s);return;case"IfThenElseNode":this._visitIfThenElseStmt(s);return;case"BufferStoreNode":this._visitBufferStoreNode(s);return;case"LIRFlatStoreNode":this._visitLIRFlatStore(s);return;case"LIRBindingsNode":this._visitLIRBindings(s);return;case"LIRAccumulatorNode":this._visitLIRAccumulator(s);return;case"WhileNode":this._visitWhileNode(s);return;case"EvaluateNode":return;default:return}}_visitForNode(t){if(this._isRedundantZeroFill(t))return;let s=t.loopVar.name;if(t.extent.type==="IntImmNode"&&t.extent.value===1){this._aliases.set(s,"0"),this._visitNode(t.body);return}let n=this._exprToJS(t.extent);if(t.kind===V.UNROLLED){let i=t.extent.type==="IntImmNode"?t.extent.value:null;if(i&&i<=32&&!this._isZeroFillBody(t.body)){for(let o=0;o<i;o++)this._emit("{ const "+s+" = "+o+";"),this._indent++,this._loopStack.push(s),this._visitNode(t.body),this._loopStack.pop(),this._indent--,this._emit("}");return}}let r=this._detectReductionAcc(t);if(r){let i="_acc_"+(this._accCounter=(this._accCounter||0)+1),o=this._accTarget,a=this._accVar;this._accTarget=r,this._accVar=i,this._emit("let "+i+" = "+this._wrapLoad(r.dtype,r.bufName+"["+r.idxExpr+"]")+";"),this._emit("for (let "+s+" = 0; "+s+" < "+n+"; "+s+"++) {"),this._indent++,this._loopStack.push(s),this._visitNode(t.body),this._loopStack.pop(),this._indent--,this._emit("}"),this._emit(r.bufName+"["+r.idxExpr+"] = "+this._wrapStoreVal(r.dtype,i)+";"),this._accTarget=o,this._accVar=a;return}this._emit("for (let "+s+" = 0; "+s+" < "+n+"; "+s+"++) {"),this._indent++,this._loopStack.push(s),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 s of t.bindings){let n=this._exprToJS(s.expr);this._aliases.set(s.name,n)}this._visitNode(t.body)}_visitLIRAccumulator(t){let s=t.localName,n=this._exprToJS(t.initLoad);this._emit("let "+s+" = "+n+";");let r=this._accTarget,i=this._accVar;this._accTarget={bufName:t.flushStore.buffer.name,idxExpr:this._exprToJS(t.flushStore.offsetExpr)},this._accVar=s;let o=t.loopVar.name,a=this._exprToJS(t.extent);this._emit("for (let "+o+" = 0; "+o+" < "+a+"; "+o+"++) {"),this._indent++,this._loopStack.push(o);let l=this._exprToJS(t.body),u=t.op||"+",f;u==="max"?f="Math.max("+s+", "+l+")":u==="min"?f="Math.min("+s+", "+l+")":f="("+s+" "+u+" "+l+")",this._emit(s+" = "+f+";"),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,s)+";"),this._accTarget=r,this._accVar=i}_detectReductionAcc(t){let s=t.body;if(!s||s.type!=="BlockNode")return null;let n=s.body;if(!n||n.type!=="BufferStoreNode")return null;let r=n,i=r.value;if(!i||i.type!=="MathOpNode"||i.op!=="+")return null;let o=null;if(i.a&&i.a.type==="BufferLoadNode"&&i.a.buffer.name===r.buffer.name?o=i.a:i.b&&i.b.type==="BufferLoadNode"&&i.b.buffer.name===r.buffer.name&&(o=i.b),!o)return null;for(let f of s.iterVars)f.iterVar&&f.binding&&this._aliases.set(f.iterVar.name,this._exprToJS(f.binding));let a=this._flatIndex(r.buffer,r.indices),l=this._flatIndex(o.buffer,o.indices);if(a!==l)return null;let u=t.loopVar.name;return a.includes(u)?null:{bufName:r.buffer.name,idxExpr:a,dtype:r.buffer.dtype}}_visitBlockNode(t){for(let s of t.iterVars)if(s.iterVar&&s.binding){let n=this._exprToJS(s.binding);this._aliases.set(s.iterVar.name,n)}if(t.initBody){let s=this._loopStack.length>0?this._loopStack[this._loopStack.length-1]:null;s?(this._emit("if ("+s+" === 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 s=[],n=[{node:t,phase:0}];for(;n.length>0;){let r=n[n.length-1],i=r.node;if(!i){n.pop(),s.push("0");continue}switch(i.type){case"IntImmNode":n.pop(),s.push(String(i.value));continue;case"FloatImmNode":n.pop(),s.push(String(i.value));continue;case"VariableNode":n.pop(),s.push(this._aliases.get(i.name)||i.name);continue;case"BufferLoadNode":{if(n.pop(),this._zeroBuffers&&this._zeroBuffers.has(i.buffer.name))s.push(this._zeroLit(i.buffer.dtype));else if(this._constantBuffers&&this._constantBuffers.has(i.buffer.name)){let o=this._constantBuffers.get(i.buffer.name);s.push(i.buffer.dtype==="i64"?`BigInt(${o})`:o)}else this._accTarget&&i.buffer.name===this._accTarget.bufName&&this._flatIndex(i.buffer,i.indices)===this._accTarget.idxExpr?s.push(this._accVar):s.push(this._wrapLoad(i.buffer.dtype,i.buffer.name+"["+this._flatIndex(i.buffer,i.indices)+"]"));continue}case"LIRFlatLoadNode":{if(n.pop(),this._zeroBuffers&&this._zeroBuffers.has(i.buffer.name))s.push(this._zeroLit(i.dtype||i.buffer.dtype));else if(this._constantBuffers&&this._constantBuffers.has(i.buffer.name)){let o=String(this._constantBuffers.get(i.buffer.name));s.push((i.dtype||i.buffer.dtype)==="i64"?`BigInt(${o})`:o)}else this._accTarget&&i.buffer.name===this._accTarget.bufName&&this._exprToJS(i.offsetExpr)===this._accTarget.idxExpr?s.push(this._accVar):s.push(this._wrapLoad(i.dtype||i.buffer.dtype,i.buffer.name+"["+this._exprToJS(i.offsetExpr)+"]"));continue}case"MathOpNode":if(r.phase===0)r.phase=1,n.push({node:i.a,phase:0});else if(r.phase===1&&i.b)r.phase=2,n.push({node:i.b,phase:0});else if(n.pop(),!i.b)s.push(`(${i.op}${s.pop()})`);else{let o=s.pop(),a=s.pop();(i.op==="+"||i.op==="-")&&o==="0"?s.push(a):i.op==="+"&&a==="0"?s.push(o):i.op==="*"&&(a==="0"||o==="0")?s.push("0"):i.op==="*"&&o==="1"?s.push(a):i.op==="*"&&a==="1"?s.push(o):i.op==="%"?s.push(`((${a} % ${o} + ${o}) % ${o})`):i.op==="//"?s.push(`((${a} / ${o}) | 0)`):s.push(`(${a} ${i.op} ${o})`)}continue;case"CompareNode":if(r.phase===0)r.phase=1,n.push({node:i.a,phase:0});else if(r.phase===1)r.phase=2,n.push({node:i.b,phase:0});else{n.pop();let o=s.pop(),a=s.pop();s.push(`(${a} ${i.toJS()} ${o})`)}continue;case"IfThenElseNode":if(r.phase===0)r.phase=1,n.push({node:i.condition,phase:0});else if(r.phase===1)r.phase=2,n.push({node:i.thenBody,phase:0});else if(r.phase===2)r.phase=3,n.push({node:i.elseBody,phase:0});else{n.pop();let o=s.pop(),a=s.pop(),l=s.pop();s.push(`(${l} ? ${a} : ${o})`)}continue;case"CastNode":if(r.phase===0)r.phase=1,n.push({node:i.expr,phase:0});else{n.pop();let o=s.pop();i.toDtype==="bool"?s.push(`(${o} ? 1 : 0)`):_n(i.toDtype)?s.push(`(${o} | 0)`):jb[i.toDtype]?s.push(`${jb[i.toDtype]}(${pR[i.toDtype]}(${o}))`):s.push(`(+${o})`)}continue;case"CallExternNode":if(r.phase<i.args.length){let o=r.phase;r.phase++,n.push({node:i.args[o],phase:0})}else{n.pop();let o=[];for(let l=0;l<i.args.length;l++)o.unshift(s.pop());let a=o.join(", ");if(zE(i.externName))s.push(`Math.${i.externName}(${a})`);else if(i.externName==="rsqrt")s.push(`(1.0 / Math.sqrt(${a}))`);else if(i.externName==="fmod")s.push(`((${o[0]} % ${o[1]} + ${o[1]}) % ${o[1]})`);else if(i.externName==="exp2")s.push(`Math.pow(2, ${a})`);else if(i.externName==="erf")s.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(i.externName==="log10")s.push(`(Math.log(${a}) * ${1/Math.LN10})`);else throw new Error(`CPU codegen: unsupported extern function "${i.externName}"`)}continue;default:n.pop(),s.push("0");continue}}return s.length>0?s[0]:"0"}_dynamicNumel(t){let s=[];for(let n=0;n<t.shape.length;n++){let r=t.shape[n];typeof r=="number"&&r>=0?s.push(String(r)):s.push(this._resolveShapeParam(t,n))}return s.length===0?"1":s.join(" * ")}_flatIndex(t,s){if(s.length===0)return"0";if(s.length===1)return this._exprToJS(s[0]);let n=[];for(let r=0;r<s.length;r++){let i=this._exprToJS(s[r]);if(i==="0")continue;let o=t.strides[r];o===1?n.push(i):typeof o=="number"&&o>=0?n.push(`${i} * ${o}`):n.push(`${i} * ${this._computeDynamicStride(t,r)}`)}return n.length===0?"0":n.join(" + ")}_computeDynamicStride(t,s){let n=[];for(let r=s+1;r<t.shape.length;r++){let i=t.shape[r];typeof i=="number"&&i>=0?n.push(String(i)):n.push(this._resolveShapeParam(t,r))}return n.length===0?"1":n.join(" * ")}_resolveShapeParam(t,s){if(this._primFunc&&this._primFunc.shapeParamMap){let n=`${t.name}:${s}`,r=this._primFunc.shapeParamMap.get(n);if(r)return r.name}return"1"}_cleanupSource(t){let s=t.split(`
4
- `),n=new Array(s.length).fill(null),r=new Map;for(let o=0;o<s.length;o++){let a=s[o].match(/^\s*const (\w+) = new \w+Array\(\d+\);\s*$/);a&&(n[o]=a[1]);let l=s[o].match(/[A-Za-z_]\w*/g);if(l)for(let u of l)r.set(u,(r.get(u)||0)+1)}let i=[];for(let o=0;o<s.length;o++){if(n[o]!==null&&r.get(n[o])===1)continue;let a=s[o];if(/^\s*\}\s*$/.test(a)&&i.length>0&&/^\s*for\s*\(.*\{\s*$/.test(i[i.length-1])){i.pop();continue}i.push(a)}return i.join(`
5
- `)}_isRedundantZeroFill(t){let s=t.body;for(;s;){if(s.type==="ForNode"){if(s.extent.type==="IntImmNode"&&s.extent.value===1){s=s.body;continue}return this._isRedundantZeroFill(s)}if(s.type==="BlockNode"){s=s.body;continue}if(s.type==="BufferStoreNode"||s.type==="LIRFlatStoreNode"){let n=s.value;if(!(n.type==="FloatImmNode"&&n.value===0||n.type==="IntImmNode"&&n.value===0)||!s.buffer)return!1;let i=s.buffer.name,o=this._zeroBuffers&&this._zeroBuffers.has(i),a=this._readBuffers&&this._readBuffers.has(i);return o||!a}return!1}return!1}_isZeroFillBody(t){let s=t;for(;s;){if(s.type==="ForNode"){s=s.body;continue}if(s.type==="BlockNode"){s=s.body;continue}if(s.type==="BufferStoreNode"||s.type==="LIRFlatStoreNode"){let n=s.value;return n.type==="FloatImmNode"&&n.value===0||n.type==="IntImmNode"&&n.value===0}return!1}return!1}_scanTree(t,s,n,r){let i=[t];for(;i.length>0;){let o=i.pop();if(!(!o||typeof o!="object")){switch(o.type){case"BufferStoreNode":case"BufferLoadNode":o.buffer&&s.set(o.buffer.name,o.buffer),o.type==="BufferLoadNode"&&o.buffer&&r&&r.add(o.buffer.name);break;case"AllocateNode":o.buffer&&n.add(o.buffer.name);break}if(o.body&&i.push(o.body),o.value&&typeof o.value=="object"&&o.value.type&&i.push(o.value),o.stmts)for(let a of o.stmts)i.push(a);if(o.thenBody&&i.push(o.thenBody),o.elseBody&&i.push(o.elseBody),o.initBody&&i.push(o.initBody),o.condition&&typeof o.condition=="object"&&o.condition.type&&i.push(o.condition),o.a&&typeof o.a=="object"&&o.a.type&&i.push(o.a),o.b&&typeof o.b=="object"&&o.b.type&&i.push(o.b),o.expr&&typeof o.expr=="object"&&o.expr.type&&i.push(o.expr),o.args)for(let a of o.args)typeof a=="object"&&a!==null&&a.type&&i.push(a);if(o.indices)for(let a of o.indices)typeof a=="object"&&a!==null&&a.type&&i.push(a);if(o.reads)for(let a of o.reads)a.buffer&&s.set(a.buffer.name,a.buffer);if(o.writes)for(let a of o.writes)a.buffer&&s.set(a.buffer.name,a.buffer);if(o.iterVars)for(let a of o.iterVars)a.binding&&typeof a.binding=="object"&&a.binding.type&&i.push(a.binding)}}}_findZeroOnlyBuffers(t,s){let n=new Map,r=[t];for(;r.length>0;){let o=r.pop();if(!(!o||typeof o!="object")){if(o.type==="BufferStoreNode"){let a=o.buffer.name;s.has(a)||(n.has(a)||n.set(a,[]),n.get(a).push(o.value))}if(o.body&&r.push(o.body),o.stmts)for(let a of o.stmts)r.push(a);o.thenBody&&r.push(o.thenBody),o.elseBody&&r.push(o.elseBody),o.initBody&&r.push(o.initBody),o.condBody&&r.push(o.condBody),o.loopBody&&r.push(o.loopBody)}}let i=new Set;this._constantBuffers=new Map;for(let[o,a]of n){if(a.length===0)continue;if(a.every(f=>f.type==="FloatImmNode"&&f.value===0||f.type==="IntImmNode"&&f.value===0)){i.add(o);continue}let l=a[0];(l.type==="FloatImmNode"||l.type==="IntImmNode")&&a.every(f=>f.type===l.type&&f.value===l.value)&&this._constantBuffers.set(o,String(l.value))}return i}}});var Wb,mm,mR=I(()=>{Jt();bn();Wb=class{static{c(this,"CUDAKernel")}constructor(t,s,n,r,i,o,a,l){this.name=t,this.source=s,this.blockDim=n,this.gridDim=r,this.sharedMemBytes=i,this.params=o,this.outputIndices=a,this.scratch=l||[]}},mm=class{static{c(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 n=[],r=[],i=[],o=0;for(let[,d]of t.bufferMap)r.push(d.name),n.push(`${bb(d.dtype)} ${d.name}`),this._defaultDtype=d.dtype,this._storeBuffers.has(d.name)&&i.push(o),o++;for(let d of this._globalScratch)r.push(d.name),n.push(`${bb(d.dtype)} ${d.name}`);for(let d of t.shapeParams)r.push(d.name),n.push(`int ${d.name}`);this._emit(`__global__ void ${t.name}(${n.join(", ")}) {`),this._indent++;for(let d of this._sharedBuffers){let m=d.numel();this._emit(`__shared__ ${wr(d.dtype)} ${d.name}[${m>0?m:1}];`)}for(let d of this._promotedBufferDecls)this._emit(`__shared__ ${wr(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 l=this.target,u=[Math.min(this._blockDim[0],l.maxBlockDimX),Math.min(this._blockDim[1],l.maxBlockDimY),Math.min(this._blockDim[2],l.maxBlockDimZ)],f=[Math.min(this._gridDim[0],l.maxGridDimX),Math.min(this._gridDim[1],l.maxGridDimY),Math.min(this._gridDim[2],l.maxGridDimZ)],h=u[0]*u[1]*u[2];if(h>l.maxThreadsPerBlock)throw new Error(`[codegen] kernel '${t.name}' block ${u.join("x")} = ${h} threads exceeds maxThreadsPerBlock ${l.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)*Od(m.dtype),0);if(p>l.sharedMemoryBytes)throw new Error(`[codegen] kernel '${t.name}' shared memory ${p} bytes exceeds device limit ${l.sharedMemoryBytes}`);return new Wb(t.name,this._lines.join(`
6
- `),u,f,p,r,i,this._globalScratch)}_scanBindings(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="ForNode"&&n.kind===V.THREAD_BINDING&&n.threadTag){let r=n.extent.type==="IntImmNode"?n.extent.value:0,i=n.extent.type!=="IntImmNode",o={varName:n.loopVar.name,extent:r,isDynamic:i,extentNode:n.extent};this._threadBindings.has(n.threadTag)?this._threadBindings.get(n.threadTag).push(o):this._threadBindings.set(n.threadTag,[o]),i||this._applyBindingDim(n.threadTag,r)}if(n.type==="AllocateNode"&&n.scope==="shared"&&this._sharedBuffers.push(n.buffer),n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody)}}}_applyBindingDim(t,s){let n=t.indexOf(".");if(n<0)return;let r=t.substring(0,n),i=t.charCodeAt(n+1)-120;i<0||i>2||(r==="threadIdx"?this._blockDim[i]=Math.max(this._blockDim[i],s):r==="blockIdx"&&(this._gridDim[i]=Math.max(this._gridDim[i],s)))}_emit(t){this._lines.push(" ".repeat(this._indent)+t)}_visitNode(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n)switch(n.type){case"SeqNode":for(let r=n.stmts.length-1;r>=0;r--)s.push(n.stmts[r]);continue;case"AllocateNode":this._visitAllocateNode(n),s.push(n.body);continue;case"ForNode":this._visitForNode(n);continue;case"BlockNode":this._visitBlockNode(n);continue;case"IfThenElseNode":this._visitIfStmt(n);continue;case"LetStmtNode":this._visitLetStmtNode(n);continue;case"BufferStoreNode":this._visitBufferStoreNode(n);continue;case"SyncThreadsNode":this._emit("__syncthreads();");continue;case"LIRFlatStoreNode":this._visitLIRFlatStore(n);continue;case"LIRBindingsNode":this._visitLIRBindings(n);continue;case"LIRAccumulatorNode":this._visitLIRAccumulator(n);continue;case"WhileNode":this._visitWhileNode(n);continue;case"EvaluateNode":continue;default:continue}}}_matchFullReduction(t){let s=[],n=t;for(;n&&n.type==="ForNode";){if(n.kind!==V.SERIAL)return null;let p=n.extent.type==="IntImmNode"?n.extent.value:0;if(p<=0)return null;s.push({extC:this._exprToC(n.extent),extVal:p,varName:n.loopVar.name}),n=n.body}if(!n||n.type!=="BlockNode")return null;let r=n,i=r.body;if(!i||i.type!=="BufferStoreNode"||r.iterVars&&r.iterVars.length!==s.length||typeof i.buffer.numel!="function"||i.buffer.numel()!==1)return null;let o=i.value;if(!o||o.type!=="MathOpNode"||o.op!=="+")return null;let a=i.indices.map(p=>this._exprToC(p)).join(","),l=c(p=>p&&p.type==="BufferLoadNode"&&p.buffer===i.buffer&&p.indices.map(d=>this._exprToC(d)).join(",")===a,"isOutLoad"),u=null;if(l(o.a))u=o.b;else if(l(o.b))u=o.a;else return null;let f=new Set(s.map(p=>p.varName));if(a.split(/[^A-Za-z0-9_]/).some(p=>f.has(p)))return null;let h=s.reduce((p,d)=>p*d.extVal,1);return h<2048?null:{loops:s,block:r,store:i,valExpr:u,total:h}}_emitParallelReduction(t,s){this._blockDim=[256,1,1],this._didParallelReduce=!0;let r=wr(s.store.buffer.dtype),i=this._flatIndex(s.store.buffer,s.store.indices);this._emit(`__shared__ ${r} _redsh[256];`),this._emit(`${r} _racc = 0;`),this._emit(`for (int _rf = threadIdx.x; _rf < ${s.total}; _rf += 256) {`),this._indent++,this._emit("int _rem = _rf;");for(let o=s.loops.length-1;o>=0;o--)this._emit(`const int ${s.loops[o].varName} = _rem % ${s.loops[o].extC}; _rem /= ${s.loops[o].extC};`);for(let o of s.block.iterVars)o.iterVar&&o.binding&&this._emit(`const int ${o.iterVar.name} = ${this._exprToC(o.binding)};`);this._emit(`_racc = _racc + ${this._exprToC(s.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) ${s.store.buffer.name}[${i}] = _redsh[0];`)}_visitForNode(t){if(t.kind===V.THREAD_BINDING){let r=t.extent.type==="IntImmNode"?t.extent.value:0,i=t.threadTag,o=this._getMaxBindingExtent(i);r>0&&o>0&&r<o?(this._emit(`if (${i} < ${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 s=t.loopVar.name,n=this._exprToC(t.extent);(t.kind===V.UNROLLED||t.kind===V.VECTORIZED)&&this._emit("#pragma unroll"),this._emit(`for (int ${s} = 0; ${s} < ${n}; ${s}++) {`),this._indent++,this._visitNode(t.body),this._indent--,this._emit("}")}_visitBlockNode(t){for(let s of t.iterVars)s.iterVar&&s.binding&&this._emit(`const int ${s.iterVar.name} = ${this._exprToC(s.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 s=t.buffer.numel();s>0?this._emit(`${wr(t.buffer.dtype)} ${t.buffer.name}[${s}];`):this._emit(`${wr(t.buffer.dtype)}* ${t.buffer.name} = (${wr(t.buffer.dtype)}*)alloca(${this._dynamicNumel(t.buffer)} * sizeof(${wr(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 s=t.variable.dtype||this._defaultDtype;this._emit(`${wr(s)} ${t.variable.name} = ${this._exprToC(t.value)};`),this._visitNode(t.body)}_visitWhileNode(t){this._visitNode(t.condBody);let s=Array.isArray(t.condVar.shape)?`${t.condVar.name}[0]`:t.condVar.name;this._emit(`while (${s}) {`),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 s of t.bindings)this._emit(`const int ${s.name} = ${this._exprToC(s.expr)};`);this._visitNode(t.body)}_visitLIRAccumulator(t){let s=t.localName,n=t.dtype||this._defaultDtype;this._emit(`${wr(n)} ${s} = ${this._exprToC(t.initLoad)};`);let r=t.loopVar.name,i=this._exprToC(t.extent);this._emit(`for (int ${r} = 0; ${r} < ${i}; ${r}++) {`),this._indent++;let o=this._exprToC(t.body),a=t.op||"+",l;a==="max"||a==="min"?_n(t.dtype)?l=`((${s}) ${a==="max"?">":"<"} (${o}) ? (${s}) : (${o}))`:l=`${a==="max"?"fmaxf":"fminf"}(${s}, ${o})`:l=`(${s} ${a} ${o})`,this._emit(`${s} = ${l};`),this._indent--,this._emit("}"),this._emit(`${t.flushStore.buffer.name}[${this._exprToC(t.flushStore.offsetExpr)}] = ${s};`)}_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 s=this._exprToC(t.a);if(!t.b)return`(${t.op}${s})`;let n=this._exprToC(t.b);return t.op==="//"?`(${s} / ${n})`:`(${s} ${t.op} ${n})`}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`((${wr(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 s=xb(this._defaultDtype),n=String(t);return`${/[.eEnN]/.test(n)?n:n+".0"}${s}`}_emitExternCall(t){let s=t.args.length,n=new Array(s);for(let a=0;a<s;a++)n[a]=this._exprToC(t.args[a]);let r=n.join(", "),i=t.dtype||this._defaultDtype;if(t.externName==="rsqrt")return`${Ld("rsqrt",i)||"rsqrtf"}(${r})`;if(t.externName==="sign"){let a=n[0],l=`0.0${xb(i)}`;return`((${a} > ${l}) - (${a} < ${l}))`}if(t.externName==="min"||t.externName==="max"){if(_n(i)){let a=t.externName==="min"?"<":">";return`((${n[0]}) ${a} (${n[1]}) ? (${n[0]}) : (${n[1]}))`}return`${Ld(t.externName,i)}(${r})`}return`${Ld(t.externName,i)}(${r})`}_flatIndex(t,s){if(s.length===0)return"0";if(s.length===1)return this._exprToC(s[0]);let n=new Array(s.length);for(let r=0;r<s.length;r++){let i=this._exprToC(s[r]),o=t.strides[r];o===1?n[r]=i:typeof o=="number"&&o>=0?n[r]=`${i} * ${o}`:n[r]=`${i} * ${this._computeDynamicStride(t,r)}`}return n.join(" + ")}_computeDynamicStride(t,s){let n=[];for(let r=s+1;r<t.shape.length;r++){let i=t.shape[r];typeof i=="number"&&i>=0?n.push(String(i)):n.push(this._resolveShapeParam(t,r))}return n.length===0?"1":n.join(" * ")}_dynamicNumel(t){let s=[];for(let n=0;n<t.shape.length;n++){let r=t.shape[n];typeof r=="number"&&r>=0?s.push(String(r)):s.push(this._resolveShapeParam(t,n))}return s.length===0?"1":s.join(" * ")}_getMaxBindingExtent(t){let s=this._threadBindings.get(t);if(!s)return 0;let n=0;for(let r of s)r.extent>n&&(n=r.extent);return n}_scanStoreTargets(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n){if((n.type==="BufferStoreNode"||n.type==="LIRFlatStoreNode")&&this._storeBuffers.add(n.buffer.name),n.type==="LIRAccumulatorNode"&&n.flushStore&&this._storeBuffers.add(n.flushStore.buffer.name),n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),n.loopBody&&s.push(n.loopBody),n.condBody&&s.push(n.condBody),n.initBody&&s.push(n.initBody)}}}_emitWmmaBody(t){let{M:s,N:n,K:r,a:i,b:o,c:a}=t;this._blockDim=[32,1,1],this._gridDim=[Math.ceil(s/16),Math.ceil(n/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, ${i} + warpM * 16 * ${r} + kk, ${r});`),this._emit(`load_matrix_sync(bf, ${o} + kk * ${n} + warpN * 16, ${n});`),this._emit("mma_sync(cf, af, bf, cf);"),this._indent--,this._emit("}"),this._emit(`store_matrix_sync(${a} + warpM * 16 * ${n} + warpN * 16, cf, ${n}, mem_row_major);`)}_emitPipelinedBody(t){let{M:s,N:n,K:r,a:i,b:o,c:a,tile:l=16}=t;this._blockDim=[l,l,1],this._gridDim=[Math.ceil(n/l),Math.ceil(s/l),1];let u=l;this._emit(`__shared__ float As[2][${u}][${u}];`),this._emit(`__shared__ float Bs[2][${u}][${u}];`),this._emit(`const int row = blockIdx.y * ${u} + threadIdx.y;`),this._emit(`const int col = blockIdx.x * ${u} + threadIdx.x;`),this._emit("float acc = 0.0f;"),this._emit(`const int nTiles = ${r} / ${u};`),this._emit(`__pipeline_memcpy_async(&As[0][threadIdx.y][threadIdx.x], &${i}[row * ${r} + threadIdx.x], sizeof(float));`),this._emit(`__pipeline_memcpy_async(&Bs[0][threadIdx.y][threadIdx.x], &${o}[threadIdx.y * ${n} + 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], &${i}[row * ${r} + (t + 1) * ${u} + threadIdx.x], sizeof(float));`),this._emit(`__pipeline_memcpy_async(&Bs[nxt][threadIdx.y][threadIdx.x], &${o}[((t + 1) * ${u} + threadIdx.y) * ${n} + 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 < ${u}; kk++) acc += As[cur][threadIdx.y][kk] * Bs[cur][kk][threadIdx.x];`),this._emit("__syncthreads();"),this._indent--,this._emit("}"),this._emit(`${a}[row * ${n} + col] = acc;`)}_analyzeSharing(t){if(t.gpuWmma||t.gpuPipelined){this._needsBarriers=!1;return}if(t.gpuRegisterBlocked){this._needsBarriers=!1;return}let s=!1;for(let[,a]of this._threadBindings){let l=new Set;for(let u of a)u.extent>0&&l.add(u.extent);if(l.size>1){s=!0;break}}if(!s)return;this._needsBarriers=!0;let n=new Set;for(let[,a]of t.bufferMap)n.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(!n.has(a.buffer.name)){let l=a.buffer.numel(),u=l>0?l:this._estimateBufferSize(a.buffer);u>0&&(this._promotedBuffers.add(a.buffer.name),this._promotedBufferDecls.push({name:a.buffer.name,dtype:a.buffer.dtype,size:u}))}r.push(a.body);continue}if(a.body&&r.push(a.body),a.stmts)for(let l of a.stmts)r.push(l);a.thenBody&&r.push(a.thenBody),a.elseBody&&r.push(a.elseBody)}}let i=new Map;this._scanBufferRefs(t.body,i);let o=new Set;this._scanAllocateNodes(t.body,o);for(let[a,l]of i){if(n.has(a)||o.has(a)||this._promotedBuffers.has(a))continue;let u=l.numel(),f=u>0?u:this._estimateBufferSize(l);f>0&&(this._promotedBuffers.add(a),this._promotedBufferDecls.push({name:a,dtype:l.dtype,size:f}))}}_emitMissingLocalDecls(t){let s=new Set;for(let[,i]of t.bufferMap)s.add(i.name);let n=new Set;this._scanAllocateNodes(t.body,n);let r=new Map;this._scanBufferRefs(t.body,r);for(let[i,o]of r){if(s.has(i)||n.has(i)||this._promotedBuffers.has(i)||this._scratchNames.has(i)||this._declaredLocals.has(i))continue;let a=o.numel(),l=a>0?a:this._estimateBufferSize(o);l>0&&(this._declaredLocals.add(i),this._emit(`${wr(o.dtype)} ${i}[${l}];`))}}_collectGlobalScratch(t){if(this._threadBindings.size>0)return;let s=32768,n=new Set;for(let[,a]of t.bufferMap)n.add(a.name);let r=c((a,l)=>{if(!l||n.has(a)||this._promotedBuffers.has(a)||this._scratchNames.has(a))return;let u=typeof l.numel=="function"?l.numel():0,f=u>0?u:this._estimateBufferSize(l);f>s&&(this._scratchNames.add(a),this._globalScratch.push({name:a,dtype:l.dtype,size:f}))},"consider"),i=[t.body];for(;i.length>0;){let a=i.pop();if(a){if(a.type==="AllocateNode"&&a.scope!=="shared"&&r(a.buffer.name,a.buffer),a.body&&i.push(a.body),a.stmts)for(let l of a.stmts)i.push(l);a.thenBody&&i.push(a.thenBody),a.elseBody&&i.push(a.elseBody),a.loopBody&&i.push(a.loopBody),a.condBody&&i.push(a.condBody),a.initBody&&i.push(a.initBody)}}let o=new Map;this._scanBufferRefs(t.body,o);for(let[a,l]of o)r(a,l)}_scanAllocateNodes(t,s){let n=[t];for(;n.length>0;){let r=n.pop();if(r){if(r.type==="AllocateNode"){s.add(r.buffer.name),n.push(r.body);continue}if(r.body&&n.push(r.body),r.stmts)for(let i of r.stmts)n.push(i);r.thenBody&&n.push(r.thenBody),r.elseBody&&n.push(r.elseBody),r.loopBody&&n.push(r.loopBody),r.condBody&&n.push(r.condBody),r.initBody&&n.push(r.initBody)}}}_scanBufferRefs(t,s){let n=[t];for(;n.length>0;){let r=n.pop();if(r){if((r.type==="BufferLoadNode"||r.type==="BufferStoreNode"||r.type==="LIRFlatLoadNode"||r.type==="LIRFlatStoreNode")&&r.buffer&&s.set(r.buffer.name,r.buffer),r.type==="LIRAccumulatorNode"&&(r.flushStore&&r.flushStore.buffer&&s.set(r.flushStore.buffer.name,r.flushStore.buffer),r.initLoad&&r.initLoad.buffer&&s.set(r.initLoad.buffer.name,r.initLoad.buffer)),r.value&&n.push(r.value),r.body&&n.push(r.body),r.stmts)for(let i of r.stmts)n.push(i);if(r.thenBody&&n.push(r.thenBody),r.elseBody&&n.push(r.elseBody),r.loopBody&&n.push(r.loopBody),r.condBody&&n.push(r.condBody),r.initBody&&n.push(r.initBody),r.indices)for(let i of r.indices)n.push(i);if(r.a&&n.push(r.a),r.b&&n.push(r.b),r.condition&&n.push(r.condition),r.expr&&n.push(r.expr),r.args)for(let i of r.args)n.push(i);r.offsetExpr&&n.push(r.offsetExpr),r.extent&&n.push(r.extent)}}}_estimateBufferSize(t){let s=1;for(let n of t.shape)typeof n=="number"&&n>0?s*=n:s*=1;return s}_resolveShapeParam(t,s){if(this._primFunc&&this._primFunc.shapeParamMap){let n=`${t.name}:${s}`,r=this._primFunc.shapeParamMap.get(n);if(r)return r.name}return"1"}}});function Lo(e){return H$[e]||"f32"}function An(e){if(!e)return"f32";if(e._dtype)return e._dtype;switch(e.type){case"IntImmNode":return"i32";case"FloatImmNode":return"f32";case"LIRFlatLoadNode":return Lo(e.dtype);case"BufferLoadNode":return Lo(e.buffer.dtype);case"CastNode":return Lo(e.toDtype);case"CallExternNode":return Lo(e.dtype||"f32");case"CompareNode":return"i32";case"VariableNode":return Lo(e.dtype||"i32");case"MathOpNode":{let t=An(e.a);if(wn(t))return t;if(e.b){let s=An(e.b);if(wn(s))return s}return t}case"IfThenElseNode":return An(e.thenBody);default:return"f32"}}function Ji(e){!e||typeof e!="object"||!e.type||(e._dtype=An(e))}function gR(e){return K$.has(e)}var rl,gm,ih,oh,ym,Bc,_m,K$,H$,wm=I(()=>{bn();rl=class e{static{c(this,"LIRNode")}constructor(){this.type=this.constructor.name.replace(/^_+/,""),this._parent=null,this._parentKey=null,this._parentIdx=-1}_setChild(t,s,n=-1){(s instanceof e||s&&s._parent!==void 0)&&(s._parent=this,s._parentKey=t,s._parentIdx=n)}_setChildren(t,s){if(s)for(let n=0;n<s.length;n++){let r=s[n];(r instanceof e||r&&r._parent!==void 0)&&(r._parent=this,r._parentKey=t,r._parentIdx=n)}}replaceWith(t){let s=this._parent;return s?(this._parentIdx>=0?s[this._parentKey][this._parentIdx]=t:s[this._parentKey]=t,t&&(t instanceof e||t._parent!==void 0)&&(t._parent=s,t._parentKey=this._parentKey,t._parentIdx=this._parentIdx),this._parent=null,this._parentKey=null,this._parentIdx=-1,!0):!1}},gm=class extends rl{static{c(this,"LIRFunc")}constructor(t,s,n,r,i,o,a){super(),this.name=t,this.params=s,this.body=n,this.bufferMap=r,this.shapeParams=i,this.shapeParamMap=o,this.metadata=a,this._setChild("body",n)}},ih=class extends rl{static{c(this,"LIRFlatLoadNode")}constructor(t,s,n){super(),this.buffer=t,this.offsetExpr=s,this.dtype=n,this._setChild("offsetExpr",s)}},oh=class extends rl{static{c(this,"LIRFlatStoreNode")}constructor(t,s,n,r){super(),this.buffer=t,this.offsetExpr=s,this.value=n,this.dtype=r,this._setChild("offsetExpr",s),this._setChild("value",n)}},ym=class extends rl{static{c(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)}},Bc=class extends rl{static{c(this,"LIRBindingsNode")}constructor(t,s){super(),this.bindings=t,this.body=s,this._setChild("body",s)}},_m=class{static{c(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}},K$=new Set(["sqrt","abs","ceil","floor","min","max"]),H$={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"};c(Lo,"normalizeDtype");c(An,"inferDtype");c(Ji,"annotateDtype");c(gR,"isWasmNativeOp")});var Cc,bm,yR=I(()=>{Jt();bn();wm();Rc();Cc=new Set(["f16","bf16"]),bm=class{static{c(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 s=t.type==="LIRFunc";if(s){for(let[f,h]of t.metadata.memoryLayout.bufferOffsets)this._bufferOffsets.set(f,h);this._totalMemBytes=t.metadata.memoryLayout.totalBytes;for(let[f,h]of t.metadata.externCalls){let p=this._mathImportSig(f,h.argCount);this._imports.set(f,p)}for(let[f,h]of t.metadata.locals)this._ensureLocal(f,Pr(h))}else this._layoutBuffers(t),this._scanMathImports(t.body);this._scanParallel(t.body);let n=[];for(let[,f]of t.bufferMap)n.push(f.name);for(let f of t.shapeParams)n.push(f.name);this._emit("(module"),this._indent++;let r=Math.max(1,Math.ceil(this._totalMemBytes/65536)),i=Math.max(256,r);this._emit(`(memory (export "memory") ${r} ${i})`);for(let[f,h]of this._imports)this._emit(`(import "math" "${f}" (func $math_${f} ${h}))`);let o=[];for(let[,f]of t.bufferMap)o.push("(param i32)");let a=[];for(let f of t.shapeParams)o.push("(param i32)"),this._ensureLocal(f.name,"i32"),a.push(f.name);this._hasParallel&&(o.push("(param i32)"),o.push("(param i32)"),this._ensureLocal("_par_start","i32"),this._ensureLocal("_par_end","i32"),n.push("_par_start"),n.push("_par_end")),this._emit('(func (export "'+t.name+'") '+o.join(" ")),this._indent++,s||(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 l=[];for(let[f,h]of this._locals)l.push("(local $"+f+" "+h+")");l.length>0&&this._emit(l.join(" "));let u=t.bufferMap.size;for(let f=0;f<a.length;f++)this._emit("(local.get "+(u+f)+")"),this._emit("local.set $"+a[f]);if(this._hasParallel){let f=u+a.length;this._emit("(local.get "+f+")"),this._emit("local.set $_par_start"),this._emit("(local.get "+(f+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(`
7
- `),memoryPages:r,bufferOffsets:new Map(this._bufferOffsets),imports:this._imports,params:n,parallel:this._hasParallel?{extent:this._parallelExtent,outputIndices:this._findOutputIndices(t),poolSafe:this._isParallelSafe(t)}:null}}_isParallelSafe(t){let s=[],n=[t.body];for(;n.length>0;){let u=n.pop();if(u){if(u.type==="ForNode"&&u.kind===V.PARALLEL&&s.push(u),u.body&&n.push(u.body),u.stmts)for(let f of u.stmts)n.push(f);u.thenBody&&n.push(u.thenBody),u.elseBody&&n.push(u.elseBody),u.loopBody&&n.push(u.loopBody)}}if(s.length!==1)return!1;let r=s[0];if(!(t.body&&t.body.stmts?t.body.stmts:[t.body]).includes(r))return!1;let o=c((u,f)=>{let h=[u];for(;h.length>0;){let p=h.pop();if(p){if((p.type==="BufferStoreNode"||p.type==="LIRFlatStoreNode")&&f.add(p),p.type==="LIRAccumulatorNode"&&p.flushStore&&f.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,l=new Set;o(t.body,a),o(r.body,l);for(let u of a)if(!l.has(u))return!1;return!0}_ensureLocal(t,s){this._locals.has(t)||this._locals.set(t,s)}_emit(t){this._lines.push(" ".repeat(this._indent)+t)}_emitLoadOp(t){if(Cc.has(t)){this._emitHalfDecode(t);return}this._emit(Id(t))}_emitStoreOp(t){if(Cc.has(t)){this._emitHalfEncode(t);return}this._emit($E(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 "+qb.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 "+qb.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 s=!1;for(let[,n]of t.bufferMap)if(Cc.has(n.dtype)){s=!0;break}if(!s&&t.metadata&&t.metadata.locals){for(let[,n]of t.metadata.locals)if(Cc.has(n)){s=!0;break}}s||(s=this._treeHasHalf(t.body)),s&&(this._ensureLocal("_half_f","f32"),this._ensureLocal("_half_i","i32"),this._ensureLocal("_half_i2","i32"))}_treeHasHalf(t){let s=[t];for(;s.length>0;){let n=s.pop();if(!(!n||typeof n!="object")){if((n.type==="BufferLoadNode"||n.type==="BufferStoreNode")&&n.buffer&&Cc.has(n.buffer.dtype)||(n.type==="LIRFlatLoadNode"||n.type==="LIRFlatStoreNode")&&Cc.has(n.dtype))return!0;if(n.body&&s.push(n.body),n.value&&typeof n.value=="object"&&s.push(n.value),n.stmts)for(let r of n.stmts)s.push(r);if(n.a&&s.push(n.a),n.b&&s.push(n.b),n.expr&&s.push(n.expr),n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),n.initBody&&s.push(n.initBody),n.condition&&s.push(n.condition),n.offsetExpr&&s.push(n.offsetExpr),n.args)for(let r of n.args)s.push(r);if(n.indices)for(let r of n.indices)s.push(r)}}return!1}_visitNode(t){let s=t;for(;s;)switch(s.type){case"SeqNode":for(let n=0;n<s.stmts.length-1;n++)this._visitNode(s.stmts[n]);s=s.stmts[s.stmts.length-1];continue;case"AllocateNode":s=s.body;continue;case"LetStmtNode":{let n=this._vectorMode;if(n&&n.simd&&this._dependsOnVecVar(s.value)){let i=s.variable.name+"_vlet";this._ensureLocal(i,"v128"),n.vecLets||(n.vecLets=new Set),n.vecLets.add(s.variable.name),this._emitVecExpr(s.value),this._emit("local.set $"+i),s=s.body;continue}let r=An(s.value)||s.variable.dtype||this._defaultDtype;this._locals.set(s.variable.name,Pr(r)),this._emitCoercedTo(s.value,this._numPrefix(r)),this._emit(`local.set $${s.variable.name}`),s=s.body;continue}case"ForNode":this._visitFor(s);return;case"BlockNode":this._visitBlock(s);return;case"IfThenElseNode":this._visitIf(s);return;case"BufferStoreNode":if(this._vectorMode){this._emitVecStore(s);return}this._visitStore(s);return;case"LIRFlatStoreNode":if(this._vectorMode){this._emitVecFlatStore(s);return}this._visitLIRFlatStore(s);return;case"LIRBindingsNode":this._visitLIRBindings(s);return;case"LIRAccumulatorNode":this._visitLIRAccumulator(s);return;case"WhileNode":this._visitWhile(s);return;case"EvaluateNode":return;default:return}}_findOutputIndices(t){let s=new Set,n=[t.body];for(;n.length>0;){let o=n.pop();if(o){if((o.type==="BufferStoreNode"||o.type==="LIRFlatStoreNode")&&o.buffer&&s.add(o.buffer.name),o.type==="LIRAccumulatorNode"&&o.flushStore&&o.flushStore.buffer&&s.add(o.flushStore.buffer.name),o.body&&n.push(o.body),o.stmts)for(let a of o.stmts)n.push(a);o.thenBody&&n.push(o.thenBody),o.elseBody&&n.push(o.elseBody),o.loopBody&&n.push(o.loopBody)}}let r=[],i=0;for(let[,o]of t.bufferMap)s.has(o.name)&&r.push(i),i++;return r}_scanParallel(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="ForNode"&&n.kind===V.PARALLEL){this._hasParallel=!0,this._parallelExtent=n.extent&&n.extent.type==="IntImmNode"?n.extent.value:0;return}if(n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody)}}}_visitFor(t){let s=t.loopVar.name,n=this._constExtent(t.extent);if(t.kind===V.PARALLEL){if(n!==null&&this._parallelExtent&&n!==this._parallelExtent){this._emitForLoop(s,t.extent,t.body);return}this._emit("(local.get $_par_start)"),this._emit("local.set $"+s),this._emit("(block $break_"+s),this._indent++,this._emit("(loop $loop_"+s),this._indent++,this._emit("(local.get $"+s+")"),this._emit("(local.get $_par_end)"),this._emit("i32.ge_s"),this._emit("br_if $break_"+s),this._visitNode(t.body),this._emit("(local.get $"+s+")"),this._emit("(i32.const 1)"),this._emit("i32.add"),this._emit("local.set $"+s),this._emit("br $loop_"+s),this._indent--,this._emit(")"),this._indent--,this._emit(")");return}if(t.kind===V.VECTORIZED&&this.target.supportsSimd()&&n!==null){this._visitVectorizedFor(t);return}if((t.kind===V.UNROLLED||t.kind===V.VECTORIZED)&&n!==null&&n<=32&&!this._isZeroFillBody(t.body)){for(let i=0;i<n;i++)this._emit("(i32.const "+i+")"),this._emit("local.set $"+s),this._visitNode(t.body);return}let r=this._detectWasmAcc(t);if(r){let i="_wacc_"+(this._waccCounter=(this._waccCounter||0)+1);this._ensureLocal(i,Pr(r.buf.dtype)),this._emitAddr(r.buf,r.outerIndices),this._emitLoadOp(r.buf.dtype),this._emit("local.set $"+i),this._wasmAcc={local:i,bufName:r.buf.name,indices:r.indices},this._emitForLoop(s,t.extent,t.body),this._emitAddr(r.buf,r.outerIndices),this._emit("(local.get $"+i+")"),this._emitStoreOp(r.buf.dtype),this._wasmAcc=null;return}this._emitForLoop(s,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 s of t.bindings)this._emitExpr(s.expr),this._emit(`local.set $${s.name}`);this._visitNode(t.body)}_vecAccumOperandsUnitStride(t){let s=t.loopVar&&t.loopVar.name;if(!s)return!1;let n=c(o=>{let a=[o];for(;a.length>0;){let l=a.pop();if(!(!l||typeof l!="object")){if(l.type==="VariableNode"&&l.name===s)return!0;if(l.a&&a.push(l.a),l.b&&a.push(l.b),l.expr&&a.push(l.expr),l.args)for(let u of l.args)a.push(u);if(l.indices)for(let u of l.indices)a.push(u);l.offsetExpr&&a.push(l.offsetExpr)}}return!1},"usesVar"),r=c(o=>{let a=[o];for(;a.length>0;){let l=a.pop();if(!(!l||typeof l!="object")){if(l.type==="MathOpNode"&&l.op==="*"&&(n(l.a)||n(l.b)))return!0;if(l.a&&a.push(l.a),l.b&&a.push(l.b),l.expr&&a.push(l.expr),l.args)for(let u of l.args)a.push(u)}}return!1},"stridedMul"),i=[t.body];for(;i.length>0;){let o=i.pop();if(!(!o||typeof o!="object")){if(o.type==="BufferLoadNode"&&Array.isArray(o.indices)){for(let a=0;a<o.indices.length-1;a++)if(n(o.indices[a]))return!1}if(o.type==="LIRFlatLoadNode"&&o.offsetExpr&&r(o.offsetExpr))return!1;if(o.a&&i.push(o.a),o.b&&i.push(o.b),o.expr&&i.push(o.expr),o.args)for(let a of o.args)i.push(a);o.body&&i.push(o.body)}}return!0}_accumInstr(t,s){if(t==="*")return"mul";if(t==="max"||t==="min"){if(wn(s))return t;throw new Error(`wasm accumulator: integer ${t} reduction not supported (dtype ${s})`)}return"add"}_visitLIRAccumulator(t){let s=t.localName,n=t.dtype;this._ensureLocal(s,Pr(n));let r=this._constExtent(t.extent),i=t.op||"+",o=i==="+"&&r!==null&&t.loopKind===V.VECTORIZED&&this.target.supportsSimd()?wb(n):null,a=o?this.target.vectorWidth:0;if(o&&r>=a&&this._vecAccumOperandsUnitStride(t)){this._visitVecAccumulator(t,o,a,r);return}this._emitExpr(t.initLoad),this._emit("local.set $"+s);let l=this._wasmAcc;this._wasmAcc={local:s,bufName:t.flushStore.buffer.name};let u=t.loopVar.name;this._emit("(i32.const 0)"),this._emit("local.set $"+u),this._emit("(block $break_"+u),this._indent++,this._emit("(loop $loop_"+u),this._indent++,this._emit("(local.get $"+u+")"),this._emitExpr(t.extent),this._emit("i32.ge_s"),this._emit("br_if $break_"+u),this._emit("(local.get $"+s+")"),this._emitCoercedTo(t.body,this._numPrefix(n)),this._emit(this._numPrefix(n)+"."+this._accumInstr(i,n)),this._emit("local.set $"+s),this._emit("(local.get $"+u+")"),this._emit("(i32.const 1)"),this._emit("i32.add"),this._emit("local.set $"+u),this._emit("br $loop_"+u),this._indent--,this._emit(")"),this._indent--,this._emit(")"),this._emitFlatAddr(t.flushStore.buffer,t.flushStore.offsetExpr),this._emit("(local.get $"+s+")"),this._emitStoreOp(t.flushStore.dtype),this._wasmAcc=l}_visitVecAccumulator(t,s,n,r){let i=t.localName,o=t.dtype,a=t.loopVar.name,l=wn(o),u=l?"f32.add":"i32.add",f=Dr(o,"add"),h=Math.floor(r/n)*n,p=h,d=i+"_vec";this._ensureLocal(d,"v128"),this._emitExpr(t.initLoad),this._emit("local.set $"+i),this._emit(l?"(f32.const 0)":"(i32.const 0)"),this._emit(s.splat),this._emit("local.set $"+d);let m=this._wasmAcc;this._wasmAcc={local:i,bufName:t.flushStore.buffer.name},this._vectorMode={dtype:o,lanes:n,loopVar:a,simd:s,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(f),this._emit("local.set $"+d),this._emit("(local.get $"+a+")"),this._emit("(i32.const "+n+")"),this._emit("i32.add"),this._emit("local.set $"+a),this._emit("br $vloop_"+a),this._indent--,this._emit(")"),this._indent--,this._emit(")"),this._vectorMode=null,this._emit("(local.get $"+i+")");for(let g=0;g<n;g++)this._emit("(local.get $"+d+")"),this._emit(s.extractLane+" "+g),this._emit(u);this._emit("local.set $"+i),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 $"+i+")"),this._emitCoerced(t.body,l),this._emit(u),this._emit("local.set $"+i),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 $"+i+")"),this._emitStoreOp(t.flushStore.dtype),this._wasmAcc=m}_emitFlatAddr(t,s){let n=this._bufferOffsets.get(t.name)||0,r=Ro(t.dtype||"f32");if(!s||s.type==="IntImmNode"&&s.value===0){this._emit(`(i32.const ${n})`);return}let i=this._vectorMode,o=r+"#"+this._exprKey(s);if(i&&i.addrLocal&&this._exprKey(s)!=="?"){(!i._addrEmitted||i._addrKey!==o)&&(this._emitExpr(s),this._emit(`(i32.const ${r})`),this._emit("i32.mul"),this._emit("local.set $"+i.addrLocal),i._addrEmitted=!0,i._addrKey=o),this._emit("(local.get $"+i.addrLocal+")"),n>0&&(this._emit(`(i32.const ${n})`),this._emit("i32.add"));return}this._emitExpr(s),this._emit(`(i32.const ${r})`),this._emit("i32.mul"),n>0&&(this._emit(`(i32.const ${n})`),this._emit("i32.add"))}_emitForLoop(t,s,n){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(s),this._emit("i32.ge_s"),this._emit("br_if $break_"+t),this._loopVarStack.push(t),this._visitNode(n),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 s of t.iterVars)if(s.iterVar&&s.binding){this._emitExpr(s.binding),this._emit(`local.set $${s.iterVar.name}`);let n=this._vectorMode;n&&n.laneVars&&this._dependsOnVecVar(s.binding)&&n.laneVars.add(s.iterVar.name)}if(t.initBody){let s=this._loopVarStack.length>0?this._loopVarStack[this._loopVarStack.length-1]:null;s?(this._emit("(local.get $"+s+")"),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,s){let n=this._bufferOffsets.get(t.name)||0,r=Ro(t.dtype);if(s.length===0){this._emit(`(i32.const ${n})`);return}let i=this._vectorMode;if(i&&i.addrLocal&&s.length>0){let o=r+"#"+s.map(a=>this._exprKey(a)).join(",");(!i._addrEmitted||i._addrKey!==o)&&(this._emitFlatIndex(t,s),this._emit(`(i32.const ${r})`),this._emit("i32.mul"),this._emit("local.set $"+i.addrLocal),i._addrEmitted=!0,i._addrKey=o),this._emit("(local.get $"+i.addrLocal+")"),n>0&&(this._emit(`(i32.const ${n})`),this._emit("i32.add"));return}this._emitFlatIndex(t,s),this._emit(`(i32.const ${r})`),this._emit("i32.mul"),n>0&&(this._emit(`(i32.const ${n})`),this._emit("i32.add"))}_emitFlatIndex(t,s){if(s.length===1){this._emitExpr(s[0]);return}let n=!0;for(let r=0;r<s.length;r++){this._emitExpr(s[r]);let i=t.strides[r];typeof i=="number"&&i>=0?i!==1&&(this._emit(`(i32.const ${i})`),this._emit("i32.mul")):(this._emitDynamicStride(t,r),this._emit("i32.mul")),n||this._emit("i32.add"),n=!1}}_emitDynamicStride(t,s){let n=0;for(let r=s+1;r<t.shape.length;r++){let i=t.shape[r];if(typeof i=="number"&&i>=0)this._emit(`(i32.const ${i})`);else{let o=this._resolveShapeParam(t,r);this._emit(`(local.get $${o})`)}n>0&&this._emit("i32.mul"),n++}n===0&&this._emit("(i32.const 1)")}_resolveShapeParam(t,s){if(this._primFunc&&this._primFunc.shapeParamMap){let n=`${t.name}:${s}`,r=this._primFunc.shapeParamMap.get(n);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 s=t._dtype||An(t.thenBody),n=this._numPrefix(s);this._emitExpr(t.condition),wn(this._wasmExprDtype(t.condition))&&(this._emit("(f32.const 0)"),this._emit("f32.ne")),this._emit("(if (result "+n+")"),this._indent++,this._emit("(then"),this._indent++,this._emitCoercedTo(t.thenBody,n),this._indent--,this._emit(")"),this._emit("(else"),this._indent++,this._emitCoercedTo(t.elseBody,n),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":wn(t)?"f32":"i32"}_exprPrefix(t){return this._numPrefix(t&&t._dtype||An(t))}_convertTo(t,s){t!==s&&(s==="f64"?t==="f32"?this._emit("f64.promote_f32"):t==="i32"?this._emit("f64.convert_i32_s"):t==="i64"&&this._emit("f64.convert_i64_s"):s==="f32"?t==="f64"?this._emit("f32.demote_f64"):t==="i32"?this._emit("f32.convert_i32_s"):t==="i64"&&this._emit("f32.convert_i64_s"):s==="i32"?t==="f64"?this._emit("i32.trunc_f64_s"):t==="f32"?this._emit("i32.trunc_f32_s"):t==="i64"&&this._emit("i32.wrap_i64"):s==="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,s){this._emitExpr(t),this._convertTo(this._exprPrefix(t),s)}_emitCoerced(t,s){this._emitCoercedTo(t,s?"f32":"i32")}_emitMathOp(t){let s=this._exprPrefix(t.a),n=t.b?this._exprPrefix(t.b):s,r=this._joinPrefix(s,n);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 i=r==="f32"||r==="f64";if(!i){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(i?`${r}.div`:`${r}.div_s`);break;case"%":this._emit(`${r}.rem_s`);break;case"//":this._emit(`${r}.div_s`);break;case"<":this._emit(i?`${r}.lt`:`${r}.lt_s`);break;case">":this._emit(i?`${r}.gt`:`${r}.gt_s`);break;case"<=":this._emit(i?`${r}.le`:`${r}.le_s`);break;case">=":this._emit(i?`${r}.ge`:`${r}.ge_s`);break;default:this._emit(`${r}.add`);break}}_joinPrefix(t,s){return t==="f64"||s==="f64"?"f64":t==="f32"||s==="f32"?"f32":t==="i64"||s==="i64"?"i64":"i32"}_emitCompare(t){let s=this._exprPrefix(t.a),n=this._exprPrefix(t.b),r=this._joinPrefix(s,n),i=r==="f32"||r==="f64";this._emitCoercedTo(t.a,r),this._emitCoercedTo(t.b,r);let o={eq:"eq",ne:"ne",lt:i?"lt":"lt_s",le:i?"le":"le_s",gt:i?"gt":"gt_s",ge:i?"ge":"ge_s"};this._emit(r+"."+(o[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")&&!wn(t.dtype)){this._emitIntMinMax(t);return}if(t.externName==="abs"&&!wn(t.dtype)){this._emitIntAbs(t);return}let s=new Set(["sqrt","abs","ceil","floor","min","max","rsqrt"]),n=t.externName==="abs"||s.has(t.externName)?this._numPrefix(t.dtype)==="i32"?"f32":this._numPrefix(t.dtype):"f32";if(t.externName==="rsqrt"){this._emit("("+n+".const 1)");for(let r of t.args)this._emitCoercedTo(r,n);this._emit(n+".sqrt"),this._emit(n+".div");return}for(let r of t.args)this._emitCoercedTo(r,n);switch(t.externName){case"sqrt":this._emit(n+".sqrt");break;case"abs":this._emit(n+".abs");break;case"ceil":this._emit(n+".ceil");break;case"floor":this._emit(n+".floor");break;case"min":this._emit(n+".min");break;case"max":this._emit(n+".max");break;default:this._imports.has(t.externName)&&(this._emit(`call $math_${t.externName}`),this._numPrefix(t.dtype)==="f64"&&this._emit("f64.promote_f32"));break}}_emitIntAbs(t){let s=this._intMinMaxEmitDepth||0,n="_iabs"+s;this._intMinMaxEmitDepth=s+1,this._emitExpr(t.args[0]),this._emit("local.set $"+n),this._intMinMaxEmitDepth=s,this._emit("(i32.const 0)"),this._emit("(local.get $"+n+")"),this._emit("i32.sub"),this._emit("(local.get $"+n+")"),this._emit("(local.get $"+n+")"),this._emit("(i32.const 0)"),this._emit("i32.lt_s"),this._emit("select")}_emitIntMinMax(t){let s=this._intMinMaxEmitDepth||0,n="_immm_a"+s,r="_immm_b"+s;this._intMinMaxEmitDepth=s+1,this._emitExpr(t.args[0]),this._emit("local.set $"+n),this._emitExpr(t.args[1]),this._emit("local.set $"+r),this._intMinMaxEmitDepth=s,this._emit("(local.get $"+n+")"),this._emit("(local.get $"+r+")"),this._emit("(local.get $"+n+")"),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 s=this._joinPrefix(this._exprPrefix(t.a),this._exprPrefix(t.b));return s==="i32"||s==="i64"}_emitIntDiv(t,s){let n=this._intDivEmitDepth||0,r="_idiv_a"+n,i="_idiv_b"+n;this._intDivEmitDepth=n+1,this._emitCoercedTo(t.a,s),this._emit("local.set $"+r),this._emitCoercedTo(t.b,s),this._emit("local.set $"+i),this._intDivEmitDepth=n;let o=s==="i64"?"-9223372036854775808":"-2147483648";this._emit("(local.get $"+r+")"),this._emit("("+s+".const 1)"),this._emit("(local.get $"+i+")"),this._emit("(local.get $"+i+")"),this._emit(s+".eqz"),this._emit("(local.get $"+r+")"),this._emit("("+s+".const "+o+")"),this._emit(s+".eq"),this._emit("(local.get $"+i+")"),this._emit("("+s+".const -1)"),this._emit(s+".eq"),this._emit("i32.and"),this._emit("i32.or"),this._emit("select"),this._emit(s+".div_s"),this._emit("local.set $"+r),this._emit("("+s+".const 0)"),this._emit("(local.get $"+r+")"),this._emit("(local.get $"+i+")"),this._emit(s+".eqz"),this._emit("select")}_emitIntRem(t,s){let n=this._intDivEmitDepth||0,r="_idiv_a"+n,i="_idiv_b"+n;this._intDivEmitDepth=n+1,this._emitCoercedTo(t.a,s),this._emit("local.set $"+r),this._emitCoercedTo(t.b,s),this._emit("local.set $"+i),this._intDivEmitDepth=n,this._emit("(local.get $"+r+")"),this._emit("("+s+".const 1)"),this._emit("(local.get $"+i+")"),this._emit("(local.get $"+i+")"),this._emit(s+".eqz"),this._emit("select"),this._emit(s+".rem_s"),this._emit("local.set $"+r),this._emit("("+s+".const 0)"),this._emit("(local.get $"+r+")"),this._emit("(local.get $"+i+")"),this._emit(s+".eqz"),this._emit("select")}_prescanIntMinMax(t){let s=c((n,r,i)=>{if(!n||typeof n!="object")return;let o=r,a=i;if(n.type==="CallExternNode"&&(n.externName==="min"||n.externName==="max")&&!wn(n.dtype))this._ensureLocal("_immm_a"+r,"i32"),this._ensureLocal("_immm_b"+r,"i32"),r+1>this._intMinMaxDepth&&(this._intMinMaxDepth=r+1),o=r+1;else if(n.type==="CallExternNode"&&n.externName==="abs"&&!wn(n.dtype))this._ensureLocal("_iabs"+r,"i32"),r+1>this._intMinMaxDepth&&(this._intMinMaxDepth=r+1),o=r+1;else if(this._isIntDivNode(n)){let l=Pr(this._joinPrefix(this._exprPrefix(n.a),this._exprPrefix(n.b)));this._ensureLocal("_idiv_a"+i,l),this._ensureLocal("_idiv_b"+i,l),a=i+1}if(n.body&&s(n.body,o,a),n.value&&typeof n.value=="object"&&s(n.value,o,a),n.a&&s(n.a,o,a),n.b&&s(n.b,o,a),n.expr&&s(n.expr,o,a),n.condition&&s(n.condition,o,a),n.offsetExpr&&s(n.offsetExpr,o,a),n.thenBody&&s(n.thenBody,o,a),n.elseBody&&s(n.elseBody,o,a),n.initBody&&s(n.initBody,o,a),n.stmts)for(let l of n.stmts)s(l,o,a);if(n.args)for(let l of n.args)s(l,o,a);if(n.indices)for(let l of n.indices)s(l,o,a);if(n.bindings)for(let l of n.bindings)s(l.expr,o,a);if(n.iterVars)for(let l of n.iterVars)l.binding&&s(l.binding,o,a)},"visit");s(t,0,0)}_mathImportSig(t,s){return`${Array(s).fill("(param f32)").join(" ")} (result f32)`}_constExtent(t){return t.type==="IntImmNode"?t.value:null}_isZeroFillBody(t){let s=t;for(;s;){if(s.type==="ForNode"){s=s.body;continue}if(s.type==="BlockNode"){s=s.body;continue}if(s.type==="BufferStoreNode"||s.type==="LIRFlatStoreNode"){let n=s.value;return n.type==="FloatImmNode"&&n.value===0||n.type==="IntImmNode"&&n.value===0}return!1}return!1}_collectBindings(t,s){let n=[t];for(;n.length>0;){let r=n.pop();if(!(!r||typeof r!="object")){if(r.type==="BlockNode"&&r.iterVars)for(let i of r.iterVars)i.iterVar&&i.binding&&s.push({name:i.iterVar.name,expr:i.binding});if(r.type==="LIRBindingsNode"&&r.bindings)for(let i of r.bindings)s.push({name:i.name,expr:i.expr});if(r.body&&n.push(r.body),r.stmts)for(let i of r.stmts)n.push(i);r.thenBody&&n.push(r.thenBody),r.elseBody&&n.push(r.elseBody),r.initBody&&n.push(r.initBody),r.loopBody&&n.push(r.loopBody)}}}_computeLaneVars(t){let s=new Set([t.loopVar.name]),n=[];this._collectBindings(t.body,n);let r=c(o=>{let a=[],l=[o];for(;l.length>0;){let u=l.pop();if(!(!u||typeof u!="object")&&(u.type==="VariableNode"&&a.push(u.name),u.a&&l.push(u.a),u.b&&l.push(u.b),u.expr&&l.push(u.expr),u.args))for(let f of u.args)l.push(f)}return a},"varsIn"),i=!0;for(;i;){i=!1;for(let o of n)s.has(o.name)||r(o.expr).some(a=>s.has(a))&&(s.add(o.name),i=!0)}return s}_vecLoadsContiguous(t,s){let n=c(o=>{let a=[o];for(;a.length>0;){let l=a.pop();if(!(!l||typeof l!="object")){if(l.type==="VariableNode"&&s.has(l.name))return!0;if(l.a&&a.push(l.a),l.b&&a.push(l.b),l.expr&&a.push(l.expr),l.args)for(let u of l.args)a.push(u);if(l.indices)for(let u of l.indices)a.push(u);l.offsetExpr&&a.push(l.offsetExpr)}}return!1},"usesLane"),r=c(o=>{let a=[o];for(;a.length>0;){let l=a.pop();if(!(!l||typeof l!="object")){if(l.type==="MathOpNode"&&l.op==="*"&&(n(l.a)||n(l.b)))return!0;if(l.a&&a.push(l.a),l.b&&a.push(l.b),l.expr&&a.push(l.expr),l.args)for(let u of l.args)a.push(u)}}return!1},"stridedMul"),i=[t];for(;i.length>0;){let o=i.pop();if(!(!o||typeof o!="object")){if(o.type==="BufferLoadNode"&&Array.isArray(o.indices)){for(let a=0;a<o.indices.length-1;a++)if(n(o.indices[a]))return!1}if(o.type==="LIRFlatLoadNode"&&o.offsetExpr&&r(o.offsetExpr))return!1;if(o.a&&i.push(o.a),o.b&&i.push(o.b),o.expr&&i.push(o.expr),o.args)for(let a of o.args)i.push(a);if(o.value&&typeof o.value=="object"&&i.push(o.value),o.body&&i.push(o.body),o.stmts)for(let a of o.stmts)i.push(a);o.thenBody&&i.push(o.thenBody),o.elseBody&&i.push(o.elseBody),o.loopBody&&i.push(o.loopBody)}}return!0}_vecStoresLaneIndexed(t,s){let n=[t];for(;n.length>0;){let r=n.pop();if(!r)continue;let i=null;if(r.type==="BufferStoreNode"?i=r.indices:r.type==="LIRFlatStoreNode"&&(i=r.offsetExpr),i!=null){let o=[],a=Array.isArray(i)?[...i]:[i];for(;a.length>0;){let l=a.pop();if(!(!l||typeof l!="object")){if(l.type==="VariableNode"&&o.push(l.name),l.a&&a.push(l.a),l.b&&a.push(l.b),l.expr&&a.push(l.expr),l.args)for(let u of l.args)a.push(u);if(l.indices)for(let u of l.indices)a.push(u);l.offsetExpr&&a.push(l.offsetExpr)}}if(!o.some(l=>s.has(l)))return!1}if(r.body&&n.push(r.body),r.stmts)for(let o of r.stmts)n.push(o);r.thenBody&&n.push(r.thenBody),r.elseBody&&n.push(r.elseBody),r.loopBody&&n.push(r.loopBody)}return!0}_visitVectorizedFor(t){let s=t.loopVar.name,n=this._constExtent(t.extent),r=this._inferBodyDtype(t.body)||this._defaultDtype,i=this.target.vectorWidth,o=wb(r);if(!o||n<i||this._treeHasHalf(t.body)){this._emitForLoop(s,t.extent,t.body);return}let a=this._computeLaneVars(t);if(!this._vecStoresLaneIndexed(t.body,a)||!this._vecLoadsContiguous(t.body,a)){this._emitForLoop(s,t.extent,t.body);return}let l=Math.floor(n/i)*i,u=n-l,f=Ro(r),d=this._countBufAccesses(t.body)>=2?"_vaddr_"+s:null;if(l>0&&(this._vectorMode={dtype:r,lanes:i,loopVar:s,simd:o,addrLocal:d,laneVars:a},l===i?(this._emit("(i32.const 0)"),this._emit("local.set $"+s),this._emitVecAddrReset(),this._visitNode(t.body)):(this._emit("(i32.const 0)"),this._emit("local.set $"+s),this._emit("(block $vbreak_"+s),this._indent++,this._emit("(loop $vloop_"+s),this._indent++,this._emit("(local.get $"+s+")"),this._emit("(i32.const "+l+")"),this._emit("i32.ge_s"),this._emit("br_if $vbreak_"+s),this._emitVecAddrReset(),this._visitNode(t.body),this._emit("(local.get $"+s+")"),this._emit("(i32.const "+i+")"),this._emit("i32.add"),this._emit("local.set $"+s),this._emit("br $vloop_"+s),this._indent--,this._emit(")"),this._indent--,this._emit(")")),this._vectorMode=null),u>0)for(let m=l;m<n;m++)this._emit("(i32.const "+m+")"),this._emit("local.set $"+s),this._visitNode(t.body)}_emitVecAddrReset(){this._vectorMode&&this._vectorMode.addrLocal&&(this._vectorMode._addrEmitted=!1,this._vectorMode._addrKey=null)}_countBufAccesses(t){let s=0,n=[t];for(;n.length>0;){let r=n.pop();if(!(!r||typeof r!="object")){if((r.type==="BufferLoadNode"||r.type==="BufferStoreNode"||r.type==="LIRFlatLoadNode"||r.type==="LIRFlatStoreNode")&&s++,r.body&&n.push(r.body),r.value&&typeof r.value=="object"&&n.push(r.value),r.a&&n.push(r.a),r.b&&n.push(r.b),r.stmts)for(let i of r.stmts)n.push(i);if(r.args)for(let i of r.args)n.push(i);if(r.indices)for(let i of r.indices)n.push(i);r.thenBody&&n.push(r.thenBody),r.elseBody&&n.push(r.elseBody),r.expr&&n.push(r.expr),r.condition&&n.push(r.condition),r.offsetExpr&&n.push(r.offsetExpr)}}return s}_inferBodyDtype(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="BufferStoreNode"&&n.buffer)return n.buffer.dtype;if(n.type==="LIRFlatStoreNode")return n.dtype||this._defaultDtype;if(n.type==="BufferLoadNode"&&n.buffer)return n.buffer.dtype;if(n.type==="LIRFlatLoadNode")return n.dtype||this._defaultDtype;if(n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),n.value&&typeof n.value=="object"&&s.push(n.value)}}return null}_emitVecStore(t){let s=this._vectorMode;this._emitAddr(t.buffer,t.indices),this._emitVecExpr(t.value),this._emit(s.simd.vecStore)}_emitVecFlatStore(t){let s=this._vectorMode;this._emitFlatAddr(t.buffer,t.offsetExpr),this._emitVecExpr(t.value),this._emit(s.simd.vecStore)}_dependsOnVecVar(t){let s=this._vectorMode;if(!s)return!0;let n=s.laneVars,r=Array.isArray(t)?[...t]:[t];for(;r.length>0;){let i=r.pop();if(!(!i||typeof i!="object")){if(i.type==="VariableNode"&&(i.name===s.loopVar||n&&n.has(i.name)))return!0;if(i.a&&r.push(i.a),i.b&&r.push(i.b),i.expr&&r.push(i.expr),i.args)for(let o of i.args)r.push(o);if(i.indices)for(let o of i.indices)r.push(o);i.offsetExpr&&r.push(i.offsetExpr)}}return!1}_emitVecExpr(t){if(!t){this._emit("(i32.const 0)");return}let s=this._vectorMode,n=s.dtype;switch(t.type){case"BufferLoadNode":this._dependsOnVecVar(t.indices)?(this._emitAddr(t.buffer,t.indices),this._emit(s.simd.vecLoad)):(this._emitAddr(t.buffer,t.indices),this._emit(Id(t.buffer.dtype)),this._emit(s.simd.splat));break;case"LIRFlatLoadNode":this._dependsOnVecVar(t.offsetExpr)?(this._emitFlatAddr(t.buffer,t.offsetExpr),this._emit(s.simd.vecLoad)):(this._emitFlatAddr(t.buffer,t.offsetExpr),this._emit(Id(t.dtype)),this._emit(s.simd.splat));break;case"FloatImmNode":this._emit("(f32.const "+t.value+")"),this._emit(s.simd.splat);break;case"IntImmNode":wn(n)?(this._emit("(f32.const "+t.value+")"),this._emit(s.simd.splat)):(this._emit("(i32.const "+t.value+")"),this._emit(s.simd.splat));break;case"VariableNode":s.vecLets&&s.vecLets.has(t.name)?this._emit("(local.get $"+t.name+"_vlet)"):t.name===s.loopVar?this._emit("(local.get $"+t.name+")"):this._locals.get(t.name)==="f32"?(this._emit("(local.get $"+t.name+")"),this._emit(s.simd.splat)):(this._emit("(local.get $"+t.name+")"),wn(n)&&this._emit("f32.convert_i32_s"),this._emit(s.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),wn(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 s=this._vectorMode,n=s.dtype;if(!t.b){if(t.op==="-"){let l=Dr(n,"neg");l?(this._emitVecExpr(t.a),this._emit(l)):(this._emit("(i32.const 0)"),this._emit(s.simd.splat),this._emitVecExpr(t.a),this._emit(Dr(n,"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 i={"+":"add","-":"sub","*":"mul","/":"div"}[t.op];if(i){let l=Dr(n,i);if(l){this._emitVecExpr(t.a),this._emitVecExpr(t.b),this._emit(l);return}}let a={"<":"lt",">":"gt","<=":"le",">=":"ge"}[t.op];if(a){let l=Dr(n,a);if(l){this._emitVecExpr(t.a),this._emitVecExpr(t.b),this._emit(l);return}}this._emitExpr(t),this._emit(s.simd.splat)}_emitVecCompare(t){let s=this._vectorMode,n=s.dtype,r=Dr(n,t.direction);r?(this._emitVecExpr(t.a),this._emitVecExpr(t.b),this._emit(r)):(this._emitExpr(t),this._emit(s.simd.splat))}_emitVecCallExtern(t){let s=this._vectorMode,n=s.dtype,r=Dr(n,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 i=Dr(n,"sqrt");if(i){this._emit("(f32.const 1)"),this._emit(s.simd.splat),this._emitVecExpr(t.args[0]),this._emit(i),this._emit(Dr(n,"div"));return}}this._emitScalarizeFallback(t)}_emitVecSelect(t){let s=this._vectorMode;this._emitVecExpr(t.thenBody),this._emitVecExpr(t.elseBody),this._emitVecExpr(t.condition),this._emit(s.simd.bitselect)}_emitScalarizeFallback(t){let s=this._vectorMode,n=s.lanes,r=s.simd.extractLane,i=s.simd.replaceLane,o=s.simd.splat,a="_vtmp_"+this._vecTmpCounter++;this._ensureLocal(a,"v128");let l=[];for(let u=0;u<n;u++){let f="_vl_"+a+"_"+u;this._ensureLocal(f,Pr(s.dtype)),l.push(f)}this._emitVecExpr(t.args[0]),this._emit("local.set $"+a);for(let u=0;u<n;u++){if(this._emit("(local.get $"+a+")"),this._emit(r+" "+u),t.args.length>1){let f="_vtmp2_"+a;if(u===0){this._ensureLocal(f,"v128");let h=this._vectorMode;this._vectorMode=s,this._emitVecExpr(t.args[1]),this._vectorMode=h,this._emit("local.set $"+f)}this._emit("(local.get $"+f+")"),this._emit(r+" "+u)}this._imports.has(t.externName)&&this._emit("call $math_"+t.externName),this._emit("local.set $"+l[u])}this._emit("(local.get $"+l[n-1]+")"),this._emit(o);for(let u=n-2;u>=0;u--)this._emit("(local.get $"+l[u]+")"),this._emit(i+" "+u)}_prescanVecLocalsAll(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="ForNode"&&n.kind===V.VECTORIZED&&(this._prescanVecLocals(n.body),this._countBufAccesses(n.body)>=2&&this._ensureLocal("_vaddr_"+n.loopVar.name,"i32"),this._prescanVecLets(n)),n.type==="LIRAccumulatorNode"&&n.loopKind===V.VECTORIZED&&(this._ensureLocal(n.localName+"_vec","v128"),this._prescanVecLocals(n.body)),n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),n.initBody&&s.push(n.initBody),n.loopBody&&s.push(n.loopBody)}}}_prescanVecLets(t){let s=this._computeLaneVars(t),n=c(i=>{let o=[i];for(;o.length>0;){let a=o.pop();if(!(!a||typeof a!="object")){if(a.type==="VariableNode"&&s.has(a.name))return!0;if(a.a&&o.push(a.a),a.b&&o.push(a.b),a.expr&&o.push(a.expr),a.args)for(let l of a.args)o.push(l);if(a.indices)for(let l of a.indices)o.push(l);a.offsetExpr&&o.push(a.offsetExpr)}}return!1},"dependsOn"),r=[t.body];for(;r.length>0;){let i=r.pop();if(!(!i||typeof i!="object")){if(i.type==="LetStmtNode"&&i.variable&&n(i.value)&&this._ensureLocal(i.variable.name+"_vlet","v128"),i.body&&r.push(i.body),i.stmts)for(let o of i.stmts)r.push(o);i.thenBody&&r.push(i.thenBody),i.elseBody&&r.push(i.elseBody),i.initBody&&r.push(i.initBody)}}}_prescanVecLocals(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="CallExternNode"&&n.externName&&!Dr(this._defaultDtype,n.externName)&&n.externName!=="rsqrt"){let i="_vtmp_"+this._vecTmpCounter;this._ensureLocal(i,"v128");let o=this.target.vectorWidth;for(let a=0;a<o;a++)this._ensureLocal("_vl_"+i+"_"+a,Pr(this._defaultDtype));n.args.length>1&&this._ensureLocal("_vtmp2_"+i,"v128"),this._vecTmpCounter++}if(n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);if(n.value&&typeof n.value=="object"&&n.value.type&&s.push(n.value),n.a&&typeof n.a=="object"&&s.push(n.a),n.b&&typeof n.b=="object"&&s.push(n.b),n.args)for(let r of n.args)typeof r=="object"&&s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody)}}}_layoutBuffers(t){let s=0,n=16;this._dynamicBuffers=new Set;let r=c(o=>{s=Math.ceil(s/n)*n,this._bufferOffsets.set(o.name,s);let a=o.numel();!o.shape.some(u=>typeof u!="number"||u<0)&&a>0?s+=a*Ro(o.dtype):(this._dynamicBuffers.add(o.name),s+=65536)},"place");for(let[,o]of t.bufferMap)r(o);let i=new Map;this._collectBuffers(t.body,i);for(let[o,a]of i)this._bufferOffsets.has(o)||r(a);this._totalMemBytes=s}_collectBuffers(t,s){let n=[t];for(;n.length>0;){let r=n.pop();if(r){if((r.type==="BufferStoreNode"||r.type==="BufferLoadNode")&&r.buffer&&s.set(r.buffer.name,r.buffer),r.body&&n.push(r.body),r.stmts)for(let i of r.stmts)n.push(i);if(r.thenBody&&n.push(r.thenBody),r.elseBody&&n.push(r.elseBody),r.initBody&&n.push(r.initBody),r.value&&typeof r.value=="object"&&r.value.type&&n.push(r.value),r.reads)for(let i of r.reads)i.buffer&&s.set(i.buffer.name,i.buffer);if(r.writes)for(let i of r.writes)i.buffer&&s.set(i.buffer.name,i.buffer)}}}_scanMathImports(t){let s=[t];for(;s.length>0;){let n=s.pop();if(!(!n||typeof n!="object")){if(n.type==="CallExternNode"&&n.externName){let r=n.externName;if(r!=="sqrt"&&r!=="min"&&r!=="max"){let i=this._mathImportSig(r,n.args.length);this._imports.set(r,i)}}if(n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);if(n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),n.initBody&&s.push(n.initBody),n.value&&typeof n.value=="object"&&n.value.type&&s.push(n.value),n.a&&typeof n.a=="object"&&s.push(n.a),n.b&&typeof n.b=="object"&&s.push(n.b),n.expr&&typeof n.expr=="object"&&s.push(n.expr),n.args)for(let r of n.args)typeof r=="object"&&s.push(r);if(n.indices)for(let r of n.indices)typeof r=="object"&&s.push(r);n.condition&&typeof n.condition=="object"&&s.push(n.condition)}}}_prescanLocals(t){this._waccCounter=0;let s=[t];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="ForNode"){this._ensureLocal(n.loopVar.name,"i32");let r=this._accPatternDtype(n);r&&this._ensureLocal("_wacc_"+ ++this._waccCounter,Pr(r)),n.kind===V.VECTORIZED&&this.target.supportsSimd()&&this._prescanVecLocals(n.body)}if(n.type==="BlockNode")for(let r of n.iterVars)r.iterVar&&this._ensureLocal(r.iterVar.name,"i32");if(n.type==="LetStmtNode"&&n.variable){let r=An(n.value)||n.variable.dtype||this._defaultDtype;this._ensureLocal(n.variable.name,Pr(r))}if(n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),n.initBody&&s.push(n.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 s=this._locals.get(t.name);if(s)return s==="f32"?"f32":"i32"}return An(t)}_fixLetStmtLocals(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="LetStmtNode"&&n.variable&&n.value){let r=An(n.value);r&&this._locals.set(n.variable.name,Pr(r))}if(n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),n.initBody&&s.push(n.initBody)}}}_accPatternDtype(t){let s=t.body;if(!s||s.type!=="BlockNode")return null;let n=s.body;if(!n||n.type!=="BufferStoreNode")return null;let r=n.value;return!r||r.type!=="MathOpNode"||r.op!=="+"?null:r.a&&r.a.type==="BufferLoadNode"&&r.a.buffer.name===n.buffer.name||r.b&&r.b.type==="BufferLoadNode"&&r.b.buffer.name===n.buffer.name?n.buffer.dtype:null}_inferDtype(t){return t&&t._dtype||An(t)}_detectWasmAcc(t){let s=t.body;if(!s||s.type!=="BlockNode")return null;let n=s.body;if(!n||n.type!=="BufferStoreNode")return null;let r=n,i=r.value;if(!i||i.type!=="MathOpNode"||i.op!=="+")return null;let o=null;if(i.a&&i.a.type==="BufferLoadNode"&&i.a.buffer.name===r.buffer.name?o=i.a:i.b&&i.b.type==="BufferLoadNode"&&i.b.buffer.name===r.buffer.name&&(o=i.b),!o)return null;let a=this._indicesKey(r.buffer,r.indices),l=this._indicesKey(o.buffer,o.indices);if(a!==l)return null;let u=r.indices.map(f=>{if(f.type!=="VariableNode")return f;for(let h of s.iterVars)if(h.iterVar&&h.iterVar.name===f.name&&h.binding)return h.binding;return f});return{buf:r.buffer,indices:r.indices,outerIndices:u}}_isAccTarget(t,s){return!this._wasmAcc||t.name!==this._wasmAcc.bufName?!1:this._indicesKey(t,s)===this._indicesKey({name:this._wasmAcc.bufName,shape:t.shape,strides:t.strides},this._wasmAcc.indices)}_indicesKey(t,s){let n=[];for(let r=0;r<s.length;r++)n.push(this._exprKey(s[r]));return t.name+":"+n.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 J$,Y$,Q$,Kb,xm,Hb,_R=I(()=>{Jt();bn();J$=new Set(["!","&&","||"]),Y$=new Set(["<",">","<=",">=","==","!="]),Q$={"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"},Kb=class{static{c(this,"WebGPUKernel")}constructor(t,s,n,r,i,o,a){this.name=t,this.source=s,this.workgroupSize=n,this.dispatchSize=r,this.sharedMemBytes=i,this.params=o,this.bindings=a}},xm=class{static{c(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[S,E]of t.metadata.threadBindings){this._threadBindings.set(S,E);for(let R of E)R.isDynamic||this._applyBindingDim(S,R.extent)}this._sharedBuffers=t.metadata.sharedBuffers}else this._scanBindings(t.body);this._scanStoreTargets(t.body),this._analyzeSharing(t);let n=[],r=[],i=0,o=this._checkF16Usage(t);o&&this._emit("enable f16;"),o&&this._emit("");let a=t.bufferMap.size+(t.shapeParams.length>0?1:0),l=[...t.bufferMap.values()],u=a>6&&l.every(S=>S.numel()>0);this._packedMode=u,this._packedOffsets=null;let f=new Map;{let S=0;for(let[,E]of t.bufferMap)f.set(E.name,S++)}{let S=null;for(let[,E]of t.bufferMap)if(this._storeBuffers.has(E.name)){S=E.dtype;break}this._defaultDtype=S||(l.length?l[0].dtype:"f32")}let h={f32:"f32",i32:"i32",u32:"u32",f16:"f16"};if(u){this._packedOffsets=new Map;let S=c(v=>Math.ceil(v/4)*4,"align4"),E=new Map;for(let[,v]of t.bufferMap){n.push(v.name);let k=this._storeBuffers.has(v.name),B=Ks(v.dtype),N=(k?"w:":"r:")+B,C=E.get(N);C||(C={isWrite:k,wt:B,dtype:h[B]||v.dtype,bufs:[],off:0},E.set(N,C));let O=v.numel(),F=(k?"_pw_":"_pr_")+B;this._packedOffsets.set(v.name,{storage:F,offset:C.off}),C.bufs.push({name:v.name,offset:C.off,size:O,dtype:v.dtype,argIndex:f.get(v.name)}),C.off+=S(O)}let R=[...E.values()].sort((v,k)=>v.isWrite===k.isWrite?v.wt.localeCompare(k.wt):v.isWrite?1:-1);for(let v of R){let k=(v.isWrite?"_pw_":"_pr_")+v.wt,B=v.isWrite?"storage, read_write":"storage, read";r.push({index:i,name:k,mode:v.isWrite?"read_write":"read",packed:v.bufs,packedSize:v.off,dtype:v.dtype}),this._emit(`@group(0) @binding(${i}) var<${B}> ${k}: array<${v.wt}>;`),i++}}else for(let[,S]of t.bufferMap){n.push(S.name);let E=this._storeBuffers.has(S.name)?"read_write":"read",R=E==="read_write"?"storage, read_write":"storage, read";r.push({index:i,name:S.name,mode:E,dtype:S.dtype,argIndex:f.get(S.name)}),this._emit(`@group(0) @binding(${i}) var<${R}> ${S.name}: array<${Ks(S.dtype)}>;`),i++}if(t.shapeParams.length>0){this._emit(""),this._emit("struct ShapeParams {"),this._indent++;for(let S of t.shapeParams)n.push(S.name),this._emit(`${S.name}: u32,`);this._indent--,this._emit("}"),r.push({index:i,name:"_shapes",mode:"read"}),this._emit(`@group(0) @binding(${i}) var<uniform> _shapes: ShapeParams;`),i++}this._emit("");for(let S of this._sharedBuffers){let E=S.numel();this._emit(`var<workgroup> ${S.name}: array<${Ks(S.dtype)}, ${E>0?E:1}>;`)}for(let S of this._promotedBufferDecls)this._emit(`var<workgroup> ${S.name}: array<${Ks(S.dtype)}, ${S.size}>;`);for(let S of this._wgPoolDecls)this._emit(`var<workgroup> ${S.pool}: array<${Ks(S.dtype)}, ${S.size>0?S.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 S=0;this._workgroupSize[1]>this._workgroupSize[S]&&(S=1),this._workgroupSize[2]>this._workgroupSize[S]&&(S=2),this._workgroupSize[S]=Math.max(1,this._workgroupSize[S]>>1)}let y=this._workgroupSize[0],_=this._workgroupSize[1],w=this._workgroupSize[2];if(this._emit(`@compute @workgroup_size(${y}, ${_}, ${w})`),this._emit(`fn ${t.name}(${p.join(", ")}) {`),this._indent++,!this._serializeThreads){let S=new Set;for(let[E,R]of this._threadBindings)if(Q$[E]){for(let k of R)if(!S.has(k.varName)){let B=this._wgslBuiltinAccess(E);this._emit(`let ${k.varName}: i32 = i32(${B});`),S.add(k.varName)}}}this._assignLocalSlots(t),this._emitMissingLocalDecls(t),this._visitNode(t.body),this._indent--,this._emit("}");let b=this.target,x=[Math.min(this._workgroupSize[0],b.maxBlockDimX),Math.min(this._workgroupSize[1],b.maxBlockDimY),Math.min(this._workgroupSize[2],b.maxBlockDimZ)],A=[Math.min(this._dispatchSize[0],b.maxGridDimX),Math.min(this._dispatchSize[1],b.maxGridDimY),Math.min(this._dispatchSize[2],b.maxGridDimZ)];return new Kb(t.name,this._lines.join(`
8
- `),x,A,this._sharedBuffers.reduce((S,E)=>S+Math.max(E.sizeInBytes(),0),0),n,r)}_checkF16Usage(t){for(let[,s]of t.bufferMap)if(s.dtype==="f16")return!0;return!1}_hasBindingPrefix(t){for(let s of this._threadBindings.keys())if(s.startsWith(t))return!0;return!1}_wgslBuiltinAccess(t){let s=t.indexOf(".");if(s<0)return"_gid.x";let n=t.substring(0,s),r=t.substring(s+1);return n==="threadIdx"?`_lid.${r}`:n==="blockIdx"?`_wid.${r}`:`_gid.${r}`}_scanStoreTargets(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n){if((n.type==="BufferStoreNode"||n.type==="LIRFlatStoreNode")&&this._storeBuffers.add(n.buffer.name),n.type==="LIRAccumulatorNode"&&n.flushStore&&this._storeBuffers.add(n.flushStore.buffer.name),n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),n.loopBody&&s.push(n.loopBody),n.condBody&&s.push(n.condBody),n.initBody&&s.push(n.initBody)}}}_scanBindings(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="ForNode"&&n.kind===V.THREAD_BINDING&&n.threadTag){let r=n.extent.type==="IntImmNode"?n.extent.value:0,i=n.extent.type!=="IntImmNode",o={varName:n.loopVar.name,extent:r,isDynamic:i,extentNode:n.extent};this._threadBindings.has(n.threadTag)?this._threadBindings.get(n.threadTag).push(o):this._threadBindings.set(n.threadTag,[o]),i||this._applyBindingDim(n.threadTag,r)}if(n.type==="AllocateNode"&&n.scope==="shared"&&this._sharedBuffers.push(n.buffer),n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody)}}}_applyBindingDim(t,s){let n=t.indexOf(".");if(n<0)return;let r=t.substring(0,n),i=t.charCodeAt(n+1)-120;i<0||i>2||(r==="threadIdx"?this._workgroupSize[i]=Math.max(this._workgroupSize[i],s):r==="blockIdx"&&(this._dispatchSize[i]=Math.max(this._dispatchSize[i],s)))}_getMaxBindingExtent(t){let s=this._threadBindings.get(t);if(!s)return 0;let n=0;for(let r of s)r.extent>n&&(n=r.extent);return n}_hasRecurrence(t){let s=[t.body];for(;s.length>0;){let n=s.pop();if(!(!n||typeof n!="object")){if(n.type==="SyncThreadsNode")return!0;for(let r of["body","loopBody","condBody","initBody","thenBody","elseBody"])n[r]&&s.push(n[r]);if(n.stmts)for(let r of n.stmts)s.push(r)}}return!1}_analyzeSharing(t){let s=new Set;for(let[,h]of this._threadBindings)for(let p of h)p.extent>0&&s.add(p.extent);let n=this._threadBindings.size>0?this._findCrossThreadBuffers(t):new Set,r=this._threadBindings.size>0?this._findCrossExtentBuffers(t):new Set;this._crossThread=n,this._crossExtent=r;let i=this._hasRecurrence(t);if(!i&&s.size<=1&&n.size===0&&r.size===0)return;let o=this.target.sharedMemoryBytes||16384,a=this._sharedBuffers.reduce((h,p)=>h+Math.max(p.sizeInBytes(),0),0),l=new Set;for(let[,h]of t.bufferMap)l.add(h.name);let u=this._collectPromotionCandidates(t,l);u.sort((h,p)=>(n.has(p.name)?1:0)-(n.has(h.name)?1:0));let f=this._dispatchSize[0]*this._dispatchSize[1]*this._dispatchSize[2];if(i){let h=this.target.maxThreadsPerBlock||256,p=this._workgroupSize[0]*this._workgroupSize[1]*this._workgroupSize[2],d=this._packWorkgroupPool(t,u),m=a+d.bytes<=o;if(f===1&&p<=h&&m){this._needsBarriers=!0,this._wgPoolOffsets=d.offsets,this._wgPoolDecls=d.decls;for(let g of u)this._promotedBuffers.add(g.name);return}this._serializeThreads=!0,this._workgroupSize=[1,1,1],this._dispatchSize=[1,1,1],this._needsBarriers=!1}else this._serializeThreads=(n.size>0||r.size>0)&&f>1,this._serializeThreads&&(this._workgroupSize=[1,1,1],this._dispatchSize=[1,1,1]),this._needsBarriers=!this._serializeThreads;for(let h of u){let p=h.size*(Fr(h.dtype)||4);a+p<=o&&(this._promotedBuffers.add(h.name),this._promotedBufferDecls.push(h),a+=p)}}_collectPromotionCandidates(t,s){let n=[],r=[t.body];for(;r.length>0;){let l=r.pop();if(l){if(l.type==="AllocateNode"&&l.scope!=="shared"){if(!s.has(l.buffer.name)){let u=l.buffer.numel(),f=u>0?u:this._estimateBufferSize(l.buffer);f>0&&n.push({name:l.buffer.name,dtype:l.buffer.dtype,size:f})}r.push(l.body);continue}if(l.body&&r.push(l.body),l.stmts)for(let u of l.stmts)r.push(u);l.thenBody&&r.push(l.thenBody),l.elseBody&&r.push(l.elseBody)}}let i=new Map;this._scanBufferRefs(t.body,i);let o=new Set;this._scanAllocateNodes(t.body,o);let a=new Set(n.map(l=>l.name));for(let[l,u]of i){if(s.has(l)||o.has(l)||a.has(l))continue;let f=u.numel(),h=f>0?f:this._estimateBufferSize(u);h>0&&(n.push({name:l,dtype:u.dtype,size:h}),a.add(l))}return n}_findRecurrenceBody(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="ForNode"&&n.kind===V.RECURRENCE)return n.body;if(n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody)}}return null}_namesTouchedOutside(t,s,n){let r=new Set,i=c(o=>{if(!(!o||o===s)){(o.type==="BufferLoadNode"||o.type==="BufferStoreNode"||o.type==="LIRFlatLoadNode"||o.type==="LIRFlatStoreNode")&&o.buffer&&n.has(o.buffer.name)&&r.add(o.buffer.name),o.type==="LIRAccumulatorNode"&&(o.flushStore&&o.flushStore.buffer&&n.has(o.flushStore.buffer.name)&&r.add(o.flushStore.buffer.name),o.initLoad&&o.initLoad.buffer&&n.has(o.initLoad.buffer.name)&&r.add(o.initLoad.buffer.name));for(let a of["stmts","body","initBody","condBody","loopBody","thenBody","elseBody","value","a","b","condition","expr","offsetExpr","extent","indices","args"]){let l=o[a];if(l)if(Array.isArray(l))for(let u of l)i(u);else typeof l=="object"&&i(l)}}},"walk");return i(t),r}_packWorkgroupPool(t,s){let n=new Set(s.map(p=>p.name)),r=this._findRecurrenceBody(t.body),{minPos:i,maxPos:o}=r?this._livenessWalk(r,n):{minPos:new Map,maxPos:new Map},a=r?this._namesTouchedOutside(t.body,r,n):n,l=new Map;for(let p of s){l.has(p.dtype)||l.set(p.dtype,[]);let d=a.has(p.name)||!i.has(p.name),m=d?0:i.get(p.name),g=d?Number.MAX_SAFE_INTEGER:o.get(p.name);l.get(p.dtype).push({name:p.name,size:p.size,first:m,last:g})}let u=new Map,f=[],h=0;for(let[p,d]of l){d.sort((_,w)=>_.first-w.first||w.size-_.size);let m=[],g=`_wg_${Ks(p)}`,y=0;for(let _ of d){let w=m.filter(x=>x.first<=_.last&&_.first<=x.last).map(x=>[x.offset,x.offset+x.size]).sort((x,A)=>x[0]-A[0]),b=0;for(let[x,A]of w){if(b+_.size<=x)break;b<A&&(b=A)}m.push({offset:b,size:_.size,first:_.first,last:_.last}),u.set(_.name,{pool:g,offset:b}),b+_.size>y&&(y=b+_.size)}f.push({pool:g,dtype:p,size:y}),h+=y*(Fr(p)||4)}return{offsets:u,decls:f,bytes:h}}_findCrossThreadBuffers(t){let s=new Set;for(let[,a]of t.bufferMap)s.add(a.name);let n=new Set,r=c((a,l)=>{if(l.length===0)return!1;let u=new Set;for(let f of a)this._collectVarNames(f,u);return l.some(f=>u.has(f))},"indexUsesLoopVar"),i=c((a,l)=>{let u=new Set;this._collectVarNames(a,u);for(let f of u)if(l.includes(f))return!0;return!1},"aliasOf"),o=c((a,l)=>{if(!a)return;let u=l;if((a.type==="ForNode"&&a.kind!==V.THREAD_BINDING||a.type==="WhileNode"||a.type==="LIRAccumulatorNode")&&a.loopVar&&(u=[...l,a.loopVar.name]),a.type==="LIRBindingsNode"&&a.bindings)for(let h of a.bindings)!u.includes(h.name)&&i(h.expr,u)&&(u=[...u,h.name]);if(a.type==="BlockNode"&&a.iterVars)for(let h of a.iterVars)h.iterVar&&h.binding&&!u.includes(h.iterVar.name)&&i(h.binding,u)&&(u=[...u,h.iterVar.name]);a.type==="LetStmtNode"&&a.variable&&!u.includes(a.variable.name)&&i(a.value,u)&&(u=[...u,a.variable.name]),a.type==="BufferLoadNode"&&a.buffer&&!s.has(a.buffer.name)&&r(a.indices||[],u)&&n.add(a.buffer.name),a.type==="LIRFlatLoadNode"&&a.buffer&&!s.has(a.buffer.name)&&a.offsetExpr&&r([a.offsetExpr],u)&&n.add(a.buffer.name);for(let h of["body","loopBody","condBody","initBody","thenBody","elseBody","value","a","b","condition","expr","offsetExpr"])a[h]&&o(a[h],u);if(a.stmts)for(let h of a.stmts)o(h,u);if(a.indices)for(let h of a.indices)o(h,u);if(a.args)for(let h of a.args)o(h,u);a.initLoad&&o(a.initLoad,u),a.flushStore&&o(a.flushStore,u)},"walk");return o(t.body,[]),n}_findCrossExtentBuffers(t){let s=new Set;for(let[,f]of t.bufferMap)s.add(f.name);let n=new Map,r=new Map,i=new Set,o=c(f=>f&&(f.type==="FloatImmNode"||f.type==="IntImmNode"),"isConst"),a=c((f,h,p)=>{if(s.has(h))return;let d=f.get(h);d||(d=new Set,f.set(h,d)),d.add(p)},"record"),l=c((f,h)=>{if(!f)return;let p=h;if(f.type==="ForNode"&&f.kind===V.THREAD_BINDING){let d=f.extent&&f.extent.type==="IntImmNode"?f.extent.value:0;d>0&&(p=h*d)}f.type==="BufferStoreNode"&&f.buffer&&(a(n,f.buffer.name,p),o(f.value)||i.add(f.buffer.name)),f.type==="LIRFlatStoreNode"&&f.buffer&&(a(n,f.buffer.name,p),o(f.value)||i.add(f.buffer.name)),f.type==="LIRAccumulatorNode"&&f.flushStore&&f.flushStore.buffer&&(a(n,f.flushStore.buffer.name,p),i.add(f.flushStore.buffer.name)),f.type==="BufferLoadNode"&&f.buffer&&a(r,f.buffer.name,p),f.type==="LIRFlatLoadNode"&&f.buffer&&a(r,f.buffer.name,p);for(let d of["body","loopBody","condBody","initBody","thenBody","elseBody","value","a","b","condition","expr","offsetExpr"])f[d]&&l(f[d],p);if(f.stmts)for(let d of f.stmts)l(d,p);if(f.indices)for(let d of f.indices)l(d,p);if(f.args)for(let d of f.args)l(d,p);f.initLoad&&l(f.initLoad,p),f.flushStore&&l(f.flushStore,p)},"walk");l(t.body,1);let u=new Set;for(let[f,h]of r){if(!i.has(f))continue;let p=n.get(f);if(p){for(let d of h)if(!p.has(d)){u.add(f);break}}}return u}_collectVarNames(t,s){let n=[t];for(;n.length>0;){let r=n.pop();if(!(!r||typeof r!="object")){if(r.type==="VariableNode"){s.add(r.name);continue}for(let i of["a","b","condition","thenBody","elseBody","expr","offsetExpr"])r[i]&&n.push(r[i]);if(r.indices)for(let i of r.indices)n.push(i);if(r.args)for(let i of r.args)n.push(i)}}}_emit(t){this._lines.push(" ".repeat(this._indent)+t)}_visitNode(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n)switch(n.type){case"SeqNode":for(let r=n.stmts.length-1;r>=0;r--)s.push(n.stmts[r]);continue;case"AllocateNode":this._visitAllocateNode(n),s.push(n.body);continue;case"ForNode":this._visitForNode(n);continue;case"BlockNode":this._visitBlockNode(n);continue;case"IfThenElseNode":this._visitIfStmt(n);continue;case"LetStmtNode":this._visitLetStmtNode(n);continue;case"BufferStoreNode":this._visitBufferStoreNode(n);continue;case"LIRFlatStoreNode":this._visitLIRFlatStore(n);continue;case"LIRBindingsNode":this._visitLIRBindings(n);continue;case"LIRAccumulatorNode":this._visitLIRAccumulator(n);continue;case"WhileNode":this._visitWhileNode(n);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,l=this._exprToWGSL(t.extent);this._emit(`for (var ${a}: i32 = 0; ${a} < ${l}; ${a} = ${a} + 1) {`),this._indent++,this._visitNode(t.body),this._indent--,this._emit("}");return}let r=t.extent.type==="IntImmNode"?t.extent.value:0,i=t.threadTag,o=this._getMaxBindingExtent(i);if(r>0&&o>0&&r<o){let a=this._wgslBuiltinAccess(i);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 s=t.loopVar.name,n=this._exprToWGSL(t.extent);this._emit(`for (var ${s}: i32 = 0; ${s} < ${n}; ${s} = ${s} + 1) {`),this._indent++,this._visitNode(t.body),this._indent--,this._emit("}")}_visitBlockNode(t){for(let s of t.iterVars)s.iterVar&&s.binding&&this._emit(`let ${s.iterVar.name}: i32 = ${this._exprToWGSL(s.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}: ${Ks(t.dtype)};`):this._emit(`var ${t.name}: array<${Ks(t.dtype)}, ${t.size}>;`)}_collectLocalBuffers(t){let s=new Set;for(let[,l]of t.bufferMap)s.add(l.name);let n=new Set;for(let l of this._sharedBuffers)n.add(l.name);let r=new Map,i=c(l=>!s.has(l)&&!n.has(l)&&!this._promotedBuffers.has(l),"isLocal"),o=new Map;this._scanBufferRefs(t.body,o);for(let[l,u]of o)i(l)&&r.set(l,u);let a=[t.body];for(;a.length>0;){let l=a.pop();if(l){if(l.type==="AllocateNode"&&l.scope!=="shared"&&l.buffer&&i(l.buffer.name)&&r.set(l.buffer.name,l.buffer),l.body&&a.push(l.body),l.stmts)for(let u of l.stmts)a.push(u);l.thenBody&&a.push(l.thenBody),l.elseBody&&a.push(l.elseBody),l.loopBody&&a.push(l.loopBody),l.condBody&&a.push(l.condBody),l.initBody&&a.push(l.initBody)}}return r}_assignLocalSlots(t){let s=this._collectLocalBuffers(t);if(s.size===0)return;let{minPos:n,maxPos:r}=this._computeBufferLiveness(t,s),i=[...s.keys()].sort((d,m)=>n.get(d)-n.get(m)||r.get(d)-r.get(m));this._localSlots=new Map;let o=new Map,a=0,l=0,u=this._workgroupSize[0]*this._workgroupSize[1]*this._workgroupSize[2]*(this._dispatchSize[0]*this._dispatchSize[1]*this._dispatchSize[2]),f=this._crossThread||new Set,h=this._crossExtent||new Set,p=c((d,m)=>!this._serializeThreads&&this._threadBindings.size>0&&!f.has(d)&&!h.has(d)&&m.numel()>1&&m.numel()<=u,"scalarEligible");for(let d of i){let m=s.get(d),g=m.numel()>0?m.numel():this._estimateBufferSize(m),y=Math.max(g,1),_=n.get(d),w=r.get(d);if(p(d,m)){let S=`_s${l++}`;this._slotDecls.push({name:S,dtype:m.dtype,size:1,scalar:!0}),this._localSlots.set(d,S),this._scalarSlotNames.add(S);continue}let b=o.get(m.dtype);b||(b=new Hb((S,E)=>S.freeAt-E.freeAt),o.set(m.dtype,b));let x=null,A=b.peek();if(A&&A.freeAt<_&&(x=b.pop()),x)y>x.decl.size&&(x.decl.size=y);else{let S={name:`_lt${a++}`,dtype:m.dtype,size:y};this._slotDecls.push(S),x={decl:S}}x.freeAt=w,this._localSlots.set(d,x.decl.name),b.push(x)}}_computeBufferLiveness(t,s){return this._livenessWalk(t.body,s)}_livenessWalk(t,s){let n=new Map,r=new Map,i=new Set(["ForNode","WhileNode","LIRAccumulatorNode"]),o=["stmts","body","initBody","condBody","loopBody","thenBody","elseBody","value","a","b","condition","expr","offsetExpr","extent","indices","args"],a=0,l=[],u=null,f=0,h=c(d=>{s.has(d)&&(n.has(d)||n.set(d,a),r.set(d,a),l.length>0&&u.add(d))},"touch"),p=c(d=>{if(!d)return;a++;let m=i.has(d.type),g=!1;m&&(l.length===0&&(u=new Set,f=a,g=!0),l.push(d)),(d.type==="BufferLoadNode"||d.type==="BufferStoreNode"||d.type==="LIRFlatLoadNode"||d.type==="LIRFlatStoreNode")&&d.buffer&&h(d.buffer.name),d.type==="LIRAccumulatorNode"&&(d.flushStore&&d.flushStore.buffer&&h(d.flushStore.buffer.name),d.initLoad&&d.initLoad.buffer&&h(d.initLoad.buffer.name));for(let y of o){let _=d[y];if(_)if(Array.isArray(_))for(let w of _)p(w);else typeof _=="object"&&p(_)}if(m&&(l.pop(),g)){let y=a;for(let _ of u)n.set(_,Math.min(n.get(_),f)),r.set(_,Math.max(r.get(_),y));u=null}},"walk");return p(t),{minPos:n,maxPos:r}}_scanAllocateNodes(t,s){let n=[t];for(;n.length>0;){let r=n.pop();if(r){if(r.type==="AllocateNode"){s.add(r.buffer.name),n.push(r.body);continue}if(r.body&&n.push(r.body),r.stmts)for(let i of r.stmts)n.push(i);r.thenBody&&n.push(r.thenBody),r.elseBody&&n.push(r.elseBody),r.loopBody&&n.push(r.loopBody),r.condBody&&n.push(r.condBody),r.initBody&&n.push(r.initBody)}}}_scanBufferRefs(t,s){let n=[t];for(;n.length>0;){let r=n.pop();if(r){if((r.type==="BufferLoadNode"||r.type==="BufferStoreNode"||r.type==="LIRFlatLoadNode"||r.type==="LIRFlatStoreNode")&&r.buffer&&s.set(r.buffer.name,r.buffer),r.type==="LIRAccumulatorNode"&&(r.flushStore&&r.flushStore.buffer&&s.set(r.flushStore.buffer.name,r.flushStore.buffer),r.initLoad&&r.initLoad.buffer&&s.set(r.initLoad.buffer.name,r.initLoad.buffer)),r.value&&n.push(r.value),r.body&&n.push(r.body),r.stmts)for(let i of r.stmts)n.push(i);if(r.thenBody&&n.push(r.thenBody),r.elseBody&&n.push(r.elseBody),r.loopBody&&n.push(r.loopBody),r.condBody&&n.push(r.condBody),r.initBody&&n.push(r.initBody),r.indices)for(let i of r.indices)n.push(i);if(r.a&&n.push(r.a),r.b&&n.push(r.b),r.condition&&n.push(r.condition),r.expr&&n.push(r.expr),r.args)for(let i of r.args)n.push(i);r.offsetExpr&&n.push(r.offsetExpr),r.extent&&n.push(r.extent)}}}_estimateBufferSize(t){let s=1;for(let n of t.shape)typeof n=="number"&&n>0?s*=n:s*=1;return s}_visitAllocateNode(t){if(t.scope!=="shared"){if(this._promotedBuffers.has(t.buffer.name)||this._localSlots&&this._localSlots.has(t.buffer.name))return;let s=t.buffer.numel(),n=s>0?s:this._estimateBufferSize(t.buffer);n>0&&this._emit(`var ${t.buffer.name}: array<${Ks(t.buffer.dtype)}, ${n}>;`)}}_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 s=t.variable.dtype||this._defaultDtype,n=Ks(s),r=this._numericExpr(t.value,s);this._emit(`var ${t.variable.name}: ${n} = ${r};`),this._visitNode(t.body)}_visitWhileNode(t){this._visitNode(t.condBody);let s=Array.isArray(t.condVar.shape)?`${this._packedBufAccess(t.condVar.name,"0")} != 0`:t.condVar.name;this._emit(`while (${s}) {`),this._indent++,this._visitNode(t.loopBody),this._visitNode(t.condBody),this._indent--,this._emit("}")}_visitBufferStoreNode(t){let s=this._flatIndex(t.buffer,t.indices),n=this._packedBufAccess(t.buffer.name,s),r=this._numericExpr(t.value,t.buffer.dtype);this._emit(`${n} = ${r};`)}_visitLIRFlatStore(t){let s=this._exprToWGSL(t.offsetExpr),n=this._packedBufAccess(t.buffer.name,s),r=this._numericExpr(t.value,t.buffer.dtype);this._emit(`${n} = ${r};`)}_visitLIRBindings(t){for(let s of t.bindings)this._emit(`let ${s.name}: i32 = ${this._numericExpr(s.expr,"i32")};`);this._visitNode(t.body)}_visitLIRAccumulator(t){let s=t.localName,n=Ks(t.dtype||this._defaultDtype);this._emit(`var ${s}: ${n} = ${this._exprToWGSL(t.initLoad)};`);let r=t.loopVar.name,i=this._exprToWGSL(t.extent);this._emit(`for (var ${r}: i32 = 0; ${r} < ${i}; ${r} = ${r} + 1) {`),this._indent++,this._emit(`${s} = (${s} + ${this._exprToWGSL(t.body)});`),this._indent--,this._emit("}");let o=this._exprToWGSL(t.flushStore.offsetExpr),a=this._packedBufAccess(t.flushStore.buffer.name,o);this._emit(`${a} = ${s};`)}_isBoolExpr(t){return t?!!(t.type==="CompareNode"||t.type==="MathOpNode"&&(J$.has(t.op)||Y$.has(t.op))):!1}_numericExpr(t,s){if(this._isBoolExpr(t)){let n=s?Ks(s):"i32";return n==="f32"?`select(0.0, 1.0, ${this._exprToWGSL(t)})`:`select(${n}(0), ${n}(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 s=this._numericExpr(t.a),n=this._numericExpr(t.b);return t.op==="//"?`(${s} / ${n})`:t.op==="%"?`(${s} % ${n})`:`(${s} ${t.op} ${n})`}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`${Ks(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 s of this._primFunc.shapeParams)if(s.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 s=t.args.length,n=new Array(s);for(let o=0;o<s;o++)n[o]=this._exprToWGSL(t.args[o]);let r=n.join(", ");if(t.externName==="fmod")return`(${n[0]} % ${n[1]})`;if(t.externName==="erf")return`((select(-1.0, 1.0, ${n[0]} >= 0.0)) * (1.0 - (1.0 / (1.0 + 0.3275911 * abs(${n[0]}))) * (0.254829592 + (1.0 / (1.0 + 0.3275911 * abs(${n[0]}))) * (-0.284496736 + (1.0 / (1.0 + 0.3275911 * abs(${n[0]}))) * (1.421413741 + (1.0 / (1.0 + 0.3275911 * abs(${n[0]}))) * (-1.453152027 + (1.0 / (1.0 + 0.3275911 * abs(${n[0]}))) * 1.061405429)))) * exp(-${n[0]} * ${n[0]})))`;if(t.externName==="log10")return`(log(${n[0]}) * ${1/Math.LN10})`;let i=GE(t.externName);if(i===t.externName&&!qE(t.externName))throw new Error(`WebGPU codegen: unsupported extern function "${t.externName}"`);return`${i}(${r})`}_packedBufAccess(t,s){if(this._wgPoolOffsets&&this._wgPoolOffsets.has(t)){let n=this._wgPoolOffsets.get(t);return n.offset===0?`${n.pool}[${s}]`:`${n.pool}[${n.offset}u + u32(${s})]`}if(this._packedMode&&this._packedOffsets&&this._packedOffsets.has(t)){let n=this._packedOffsets.get(t);return n.offset===0?`${n.storage}[${s}]`:`${n.storage}[${n.offset}u + u32(${s})]`}if(this._localSlots&&this._localSlots.has(t)){let n=this._localSlots.get(t);return this._scalarSlotNames.has(n)?n:`${n}[${s}]`}return`${t}[${s}]`}_flatIndex(t,s){if(s.length===0)return"0";if(s.length===1)return this._exprToWGSL(s[0]);let n=new Array(s.length);for(let r=0;r<s.length;r++){let i=this._exprToWGSL(s[r]),o=t.strides[r];o===1?n[r]=i:typeof o=="number"&&o>=0?n[r]=`${i} * ${o}`:n[r]=`${i} * ${this._computeDynamicStride(t,r)}`}return n.join(" + ")}_computeDynamicStride(t,s){let n=[];for(let r=s+1;r<t.shape.length;r++){let i=t.shape[r];typeof i=="number"&&i>=0?n.push(String(i)):n.push(this._resolveShapeParam(t,r))}return n.length===0?"1":n.join(" * ")}_resolveShapeParam(t,s){if(this._primFunc&&this._primFunc.shapeParamMap){let n=`${t.name}:${s}`,r=this._primFunc.shapeParamMap.get(n);if(r)return`i32(_shapes.${r.name})`}return"1"}},Hb=class{static{c(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 s=this._items;s.push(t);let n=s.length-1;for(;n>0;){let r=n-1>>1;if(this._compare(s[n],s[r])>=0)break;[s[n],s[r]]=[s[r],s[n]],n=r}}pop(){let t=this._items,s=t[0],n=t.pop();if(t.length>0){t[0]=n;let r=0,i=t.length;for(;;){let o=2*r+1,a=2*r+2,l=r;if(o<i&&this._compare(t[o],t[l])<0&&(l=o),a<i&&this._compare(t[a],t[l])<0&&(l=a),l===r)break;[t[r],t[l]]=[t[l],t[r]],r=l}}return s}}});function wR(e){let{source:t,name:s,metadata:n}=e,r=[];for(let o=0;o<n.paramCount;o++)r.push(`buf_${o}`);let i=[];return i.push(t),i.push(""),i.push(`// ${s}(${r.map(o=>`/* ${o}: Float32Array */`).join(", ")});`),i.join(`
9
- `)}var bR=I(()=>{c(wR,"buildSnippet")});function ft(e){let t=[];do{let s=e&127;e>>>=7,e&&(s|=128),t.push(s)}while(e);return t}function az(e){let t=[],s=!0;for(;s;){let n=e&127;e>>=7,e===0&&!(n&64)||e===-1&&n&64?s=!1:n|=128,t.push(n)}return t}function lz(e){e=BigInt(e);let t=[],s=!0;for(;s;){let n=Number(e&0x7fn);e>>=7n,e===0n&&!(n&64)||e===-1n&&n&64?s=!1:n|=128,t.push(n)}return t}function Sm(e){let t=new TextEncoder().encode(e);return[...ft(t.length),...t]}function cz(e){let t=new ArrayBuffer(4);return new Float32Array(t)[0]=e,[...new Uint8Array(t)]}function uz(e){let t=new ArrayBuffer(8);return new Float64Array(t)[0]=e,[...new Uint8Array(t)]}function pi(e,t){for(let s=0;s<t.length;s++)e.push(t[s]);return e}function Tc(e,t){let s=[e];return pi(s,ft(t.length)),pi(s,t),s}function il(e){let t=[];pi(t,ft(e.length));for(let s of e)pi(t,s);return t}function fz(e){let t=[],s=0;for(;s<e.length;){let n=e[s];if(n<=" "){s++;continue}if(n===";"&&e[s+1]===";"){s=e.indexOf(`
10
- `,s),s<0&&(s=e.length);continue}if(n==="("&&e[s+1]===";"){s=e.indexOf(";)",s+2),s=s<0?e.length:s+2;continue}if(n==="("||n===")"){t.push(n),s++;continue}if(n==='"'){let i=s+1;for(;i<e.length&&e[i]!=='"';)i++;t.push(e.substring(s,i+1)),s=i+1;continue}let r=s;for(;r<e.length&&e[r]>" "&&e[r]!=="("&&e[r]!==")";)r++;t.push(e.substring(s,r)),s=r}return t}function hz(e){let t=0,s=c(()=>e[t++],"eat"),n=c(()=>e[t],"peek"),r=c(g=>{if(s()!==g)throw new Error("expected "+g+" at "+(t-1))},"expect");r("("),r("module");let i=[],o=1,a=256,l="",u=[],f=[],h=[],p=-1,d=-1;for(;n()!==")";){if(n()!=="("){t++;continue}t++;let g=s();if(g==="memory"){for(;n()==="(";)m();n()!==")"&&(o=parseInt(s())||1),n()!==")"&&(a=parseInt(s())||256),r(")")}else if(g==="import"){let y=s().replace(/"/g,""),_=s().replace(/"/g,"");r("("),r("func"),n().startsWith("$")&&t++;let w=[],b=[];for(;n()==="(";){t++;let x=s();if(x==="param")for(;n()!==")";){let A=s();A==="f32"?w.push(ch):A==="f64"?w.push(lh):A==="i32"?w.push(ah):A==="v128"&&w.push(uh)}else if(x==="result")for(;n()!==")";){let A=s();A==="f32"?b.push(ch):A==="f64"?b.push(lh):A==="i32"?b.push(ah):A==="v128"&&b.push(uh)}r(")")}r(")"),r(")"),i.push({module:y,name:_,params:w,results:b})}else if(g==="func"){for(;n()==="(";){let _=t;t++;let w=s();if(w==="export")l=s().replace(/"/g,""),r(")");else if(w==="param"){for(;n()!==")";){let b=s();b==="i32"?u.push(ah):b==="i64"?u.push(Qb):b==="f32"?u.push(ch):b==="f64"?u.push(lh):b==="v128"&&u.push(uh)}r(")")}else if(w==="result"){for(;n()!==")";)s();r(")")}else if(w==="local"){for(;n()!==")";){let b=s();b.startsWith("$")?h.push(b.replace("$","")):b==="i32"?f.push(ah):b==="i64"?f.push(Qb):b==="f32"?f.push(ch):b==="f64"?f.push(lh):b==="v128"&&f.push(uh)}r(")")}else{t=_;break}}p=t;let y=1;for(;y>0;){let _=s();_==="("?y++:_===")"&&y--}d=t-1}else m()}function m(){if(n()!=="("){t++;return}let g=0;do{let y=s();y==="("?g++:y===")"&&g--}while(g>0)}return c(m,"skipSExpr"),{imports:i,memMin:o,memMax:a,funcExportName:l,funcParams:u,funcLocals:f,funcLocalNames:h,bodyTokens:e.slice(p,d)}}function pz(e,t,s){let n=[],r=0,i=c(()=>e[r],"peek"),o=c(()=>e[r++],"eat"),a=[],l=-1;for(let y of t.values())y>l&&(l=y);function u(y){let _=y.replace("$","");if(t.has(_))return t.get(_);let w=parseInt(_,10);if(!isNaN(w))return w>l&&(l=w),w;let b=l+1;return l=b,t.set(_,b),b}c(u,"localIdx");function f(){return i()&&i().startsWith("$")?o().replace("$",""):""}c(f,"eatLabel");function h(y,_){let w=y.indexOf(_);return w>=0?y.substring(w+_.length):""}c(h,"extractName");function p(){for(;r<e.length&&i()!==")";){let y=i();if(y==="("){r++;let _=o();if(_==="i32.const")n.push(65),n.push(...az(parseInt(o()))),m(")");else if(_==="i64.const")n.push(66),n.push(...lz(o())),m(")");else if(_==="f32.const")n.push(67),n.push(...cz(parseFloat(o()))),m(")");else if(_==="f64.const")n.push(68),n.push(...uz(parseFloat(o()))),m(")");else if(_==="local.get")n.push(32),n.push(...ft(u(o()))),m(")");else if(_==="local.set")n.push(33),n.push(...ft(u(o()))),m(")");else if(_==="block"){let w=o().replace("$","");a.push(w),n.push(2,Jb),p(),n.push(11),a.pop(),m(")")}else if(_==="loop"){let w=o().replace("$","");a.push(w),n.push(3,Jb),p(),n.push(11),a.pop(),m(")")}else if(_==="if"){let w=Jb;if(i()==="("){let b=r;if(r++,i()==="result"){o();let x=o();w=x==="f32"?ch:x==="f64"?lh:x==="v128"?uh:x==="i64"?Qb:ah,m(")")}else r=b}for(n.push(4,w),a.push("_if");r<e.length&&i()!==")"&&i()==="(";){let b=r;r++;let x=o();if(x==="then")p(),m(")");else if(x==="else")n.push(5),p(),m(")");else{r=b;break}}n.push(11),a.pop(),m(")")}else r-=2,g();continue}if(r++,y==="br_if")n.push(13),n.push(...ft(d(f())));else if(y==="br")n.push(12),n.push(...ft(d(f())));else if(y.startsWith("call")){n.push(16);let _=h(y,"math_")||(i()&&i().startsWith("$")?h(o(),"math_"):""),w=s.get(_);if(w===void 0)throw new Error(`WAT encoder: call to unresolved import '${_}'`);n.push(...ft(w))}else y==="local.get"?(n.push(32),n.push(...ft(u(f())))):y==="local.set"?(n.push(33),n.push(...ft(u(f())))):y.endsWith(".extract_lane")||y.endsWith(".replace_lane")?(n.push(...Yb.get(y)),n.push(parseInt(o(),10))):Yb.has(y)&&n.push(...Yb.get(y))}}c(p,"emitBlock");function d(y){for(let _=a.length-1;_>=0;_--)if(a[_]===y)return a.length-1-_;return 0}c(d,"resolveBr");function m(y){if(o()!==y)throw new Error("expect "+y)}c(m,"expect");function g(){if(i()!=="("){r++;return}let y=0;do{let _=o();_==="("?y++:_===")"&&y--}while(y>0)}return c(g,"skipSExpr"),p(),n}function Am(e){let t=fz(e),s=hz(t),n=new Map,r=s.funcParams.length;for(let S=0;S<s.funcLocalNames.length;S++)n.set(s.funcLocalNames[S],r+S);let i=new Map;for(let S=0;S<s.imports.length;S++)i.set(s.imports[S].name,S);let o=[],a=new Map;function l(S,E){let R=S.join(",")+">"+E.join(",");if(a.has(R))return a.get(R);let v=o.length;return o.push({params:S,results:E}),a.set(R,v),v}c(l,"getType");for(let S of s.imports)S.typeIdx=l(S.params,S.results);let u=l(s.funcParams,[]),f=Tc(tz,il(o.map(S=>[oz,...ft(S.params.length),...S.params,...ft(S.results.length),...S.results]))),h=[];s.imports.length>0&&(h=Tc(ez,il(s.imports.map(S=>[...Sm(S.module),...Sm(S.name),0,...ft(S.typeIdx)]))));let p=Tc(sz,il([[...ft(u)]])),d=Tc(nz,il([[1,...ft(s.memMin),...ft(s.memMax)]])),m=s.imports.length,g=Tc(rz,il([[...Sm("memory"),2,...ft(0)],[...Sm(s.funcExportName),0,...ft(m)]])),y=[];if(s.funcLocals.length>0){let S=[],E=s.funcLocals[0],R=1;for(let v=1;v<s.funcLocals.length;v++)s.funcLocals[v]===E?R++:(S.push([...ft(R),E]),E=s.funcLocals[v],R=1);S.push([...ft(R),E]),pi(y,il(S))}else y.push(0);let _=pz(s.bodyTokens,n,i),w=[];pi(w,y),pi(w,_),w.push(11);let b=[];pi(b,ft(w.length)),pi(b,w);let x=Tc(iz,il([b])),A=[];for(let S of[X$,Z$,f,h,p,d,g,x])pi(A,S);return new Uint8Array(A)}var X$,Z$,tz,ez,sz,nz,rz,iz,ah,Qb,lh,ch,uh,oz,Jb,Yb,Xb=I(()=>{X$=[0,97,115,109],Z$=[1,0,0,0],tz=1,ez=2,sz=3,nz=5,rz=7,iz=10,ah=127,Qb=126,lh=124,ch=125,uh=123,oz=96,Jb=64;c(ft,"uleb");c(az,"sleb");c(lz,"slebBig");c(Sm,"encStr");c(cz,"encF32");c(uz,"encF64");c(pi,"pushAll");c(Tc,"section");c(il,"vec");Yb=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,...ft(0),4,0]],["v128.store",[253,...ft(11),4,0]],["v128.bitselect",[253,...ft(82)]],["v128.and",[253,...ft(78)]],["v128.or",[253,...ft(80)]],["v128.not",[253,...ft(77)]],["f32x4.splat",[253,...ft(19)]],["i32x4.splat",[253,...ft(17)]],["f32x4.add",[253,...ft(228)]],["f32x4.sub",[253,...ft(229)]],["f32x4.mul",[253,...ft(230)]],["f32x4.div",[253,...ft(231)]],["f32x4.neg",[253,...ft(225)]],["f32x4.abs",[253,...ft(224)]],["f32x4.sqrt",[253,...ft(227)]],["f32x4.ceil",[253,...ft(103)]],["f32x4.floor",[253,...ft(104)]],["f32x4.min",[253,...ft(232)]],["f32x4.max",[253,...ft(233)]],["f32x4.eq",[253,...ft(65)]],["f32x4.ne",[253,...ft(66)]],["f32x4.lt",[253,...ft(67)]],["f32x4.gt",[253,...ft(68)]],["f32x4.le",[253,...ft(69)]],["f32x4.ge",[253,...ft(70)]],["f32x4.extract_lane",[253,...ft(31)]],["f32x4.replace_lane",[253,...ft(32)]],["i32x4.add",[253,...ft(174)]],["i32x4.sub",[253,...ft(177)]],["i32x4.mul",[253,...ft(181)]],["i32x4.abs",[253,...ft(160)]],["i32x4.min_s",[253,...ft(182)]],["i32x4.max_s",[253,...ft(184)]],["i32x4.eq",[253,...ft(55)]],["i32x4.ne",[253,...ft(56)]],["i32x4.lt_s",[253,...ft(57)]],["i32x4.gt_s",[253,...ft(58)]],["i32x4.le_s",[253,...ft(59)]],["i32x4.ge_s",[253,...ft(60)]],["i32x4.extract_lane",[253,...ft(27)]],["i32x4.replace_lane",[253,...ft(28)]]]);c(fz,"tokenize");c(hz,"parseModule");c(pz,"encodeBody");c(Am,"encodeWat")});function xR(e){let{source:t,name:s,metadata:n}=e,{bufferOffsets:r,imports:i}=n,o=[...r.entries()],a=Am(t),l=[];if(l.push("(async () => {"),l.push(""),l.push("const binary = new Uint8Array(["+a.join(",")+"]);"),l.push(""),i&&i.size>0){l.push("const mathImports = {");for(let[f]of i)f==="fmod"?l.push(" fmod: (a, b) => a % b,"):f==="rsqrt"?l.push(" rsqrt: x => 1 / Math.sqrt(x),"):l.push(` ${f}: Math.${f},`);l.push("};"),l.push("const { instance } = await WebAssembly.instantiate(binary, { math: mathImports });")}else l.push("const { instance } = await WebAssembly.instantiate(binary);");l.push("const memory = instance.exports.memory;"),l.push("");for(let[f,h]of o)l.push(`// ${f}: new Float32Array(memory.buffer, ${h}, N).set(data);`);l.push(""),l.push(`instance.exports.${s}(${o.map(([,f])=>f).join(", ")});`),l.push("");let u=o[o.length-1];return u&&(l.push(`const result = new Float32Array(memory.buffer, ${u[1]}, N);`),l.push("console.log(result);")),l.push("})();"),l.join(`
11
- `)}var SR=I(()=>{Xb();c(xR,"buildSnippet")});function AR(e){let{source:t,name:s,metadata:n}=e,{bindings:r,dispatchSize:i}=n,o=[];o.push("(async () => {"),o.push("const adapter = await navigator.gpu.requestAdapter();"),o.push("const device = await adapter.requestDevice();"),o.push(""),o.push("const wgsl = "+JSON.stringify(t)+";"),o.push(""),o.push("const pipeline = device.createComputePipeline({"),o.push(' layout: "auto",'),o.push(" compute: { module: device.createShaderModule({ code: wgsl }), entryPoint: "+JSON.stringify(s)+" }"),o.push("});"),o.push(""),o.push("const entries = [];"),o.push("const gpuBuffers = [];"),o.push("");for(let a of r)a.name==="_shapes"?dz(o,a):a.packed?mz(o,a):a.mode==="read_write"?gz(o,a):yz(o,a),o.push("");return o.push("const bindGroup = device.createBindGroup({ layout: pipeline.getBindGroupLayout(0), entries });"),o.push("const encoder = device.createCommandEncoder();"),o.push("const pass = encoder.beginComputePass();"),o.push("pass.setPipeline(pipeline);"),o.push("pass.setBindGroup(0, bindGroup);"),o.push(`pass.dispatchWorkgroups(${i[0]}, ${i[1]}, ${i[2]});`),o.push("pass.end();"),o.push(""),_z(o,r),o.push(""),o.push("gpuBuffers.forEach(b => b.destroy());"),o.push("device.destroy();"),o.push("})();"),o.join(`
12
- `)}function dz(e,t){e.push(`// binding ${t.index}: uniform (_shapes)`),e.push("{"),e.push(" const shapeData = new Uint32Array([/* shape values */]);"),e.push(" const size = Math.max(Math.ceil(shapeData.byteLength / 16) * 16, 16);"),e.push(" const buf = device.createBuffer({ size, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });"),e.push(" device.queue.writeBuffer(buf, 0, shapeData);"),e.push(" entries.push({ binding: "+t.index+", resource: { buffer: buf } });"),e.push(" gpuBuffers.push(buf);"),e.push("}")}function mz(e,t){let s=t.mode==="read_write",n=s?"GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST":"GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST";if(e.push(`// binding ${t.index}: packed ${t.name} (${t.packed.length} buffers, ${t.packedSize} elems)`),e.push("{"),e.push(` const buf = device.createBuffer({ size: ${t.packedSize*4}, usage: ${n}, mappedAtCreation: true });`),e.push(" const mapped = new Float32Array(buf.getMappedRange());"),s)e.push(" mapped.fill(0);");else for(let r of t.packed)e.push(` // mapped.set(${r.name}_data, ${r.offset}); // size=${r.size}`);e.push(" buf.unmap();"),e.push(" entries.push({ binding: "+t.index+", resource: { buffer: buf } });"),e.push(" gpuBuffers.push(buf);"),e.push("}")}function gz(e,t){e.push(`// binding ${t.index}: output ${t.name}`),e.push("{"),e.push(" const size = N * 4; // N = output element count"),e.push(" const buf = device.createBuffer({ size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, mappedAtCreation: true });"),e.push(" new Float32Array(buf.getMappedRange()).fill(0);"),e.push(" buf.unmap();"),e.push(" entries.push({ binding: "+t.index+", resource: { buffer: buf } });"),e.push(" gpuBuffers.push(buf);"),e.push("}")}function yz(e,t){e.push(`// binding ${t.index}: input ${t.name}`),e.push("{"),e.push(` const data = ${t.name}_data; // Float32Array`),e.push(" const buf = device.createBuffer({ size: Math.max(data.byteLength, 4), usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST, mappedAtCreation: true });"),e.push(" new Float32Array(buf.getMappedRange()).set(data);"),e.push(" buf.unmap();"),e.push(" entries.push({ binding: "+t.index+", resource: { buffer: buf } });"),e.push(" gpuBuffers.push(buf);"),e.push("}")}function _z(e,t){if(t.filter(n=>n.mode==="read_write").length===0){e.push("device.queue.submit([encoder.finish()]);");return}for(let n=0;n<t.length;n++){let r=t[n];if(r.mode==="read_write")if(r.packed)for(let i of r.packed)e.push("{"),e.push(` const rb = device.createBuffer({ size: ${i.size*4}, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST });`),e.push(` encoder.copyBufferToBuffer(gpuBuffers[${n}], ${i.offset*4}, rb, 0, ${i.size*4});`),e.push(" device.queue.submit([encoder.finish()]);"),e.push(" await rb.mapAsync(GPUMapMode.READ);"),e.push(` console.log("${i.name}:", new Float32Array(rb.getMappedRange()));`),e.push(" rb.unmap(); rb.destroy();"),e.push("}");else e.push("{"),e.push(` const src = gpuBuffers[${n}];`),e.push(" const rb = device.createBuffer({ size: src.size, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST });"),e.push(" encoder.copyBufferToBuffer(src, 0, rb, 0, src.size);"),e.push(" device.queue.submit([encoder.finish()]);"),e.push(" await rb.mapAsync(GPUMapMode.READ);"),e.push(` console.log("${r.name}:", new Float32Array(rb.getMappedRange()));`),e.push(" rb.unmap(); rb.destroy();"),e.push("}")}}var vR=I(()=>{c(AR,"buildSnippet");c(dz,"emitShapeBinding");c(mz,"emitPackedBinding");c(gz,"emitOutputBinding");c(yz,"emitInputBinding");c(_z,"emitReadback")});function kR(e){let{source:t,name:s,metadata:n}=e,{blockDim:r,gridDim:i,sharedMemBytes:o,params:a}=n,l=[];return l.push(t),l.push(""),l.push("// Launch:"),l.push(`// dim3 block(${r[0]}, ${r[1]}, ${r[2]});`),l.push(`// dim3 grid(${i[0]}, ${i[1]}, ${i[2]});`),o>0?l.push(`// ${s}<<<grid, block, ${o}>>>(${a.join(", ")});`):l.push(`// ${s}<<<grid, block>>>(${a.join(", ")});`),l.join(`
13
- `)}var ER=I(()=>{c(kR,"buildSnippet")});function vm(e,t){NR.set(e,t),t.runtimeKind&&t.snippet&&RR.set(t.runtimeKind,t.snippet)}function BR(e){return NR.get(e)||null}function CR(e){return RR.get(e)||null}var NR,RR,TR=I(()=>{hi();dR();mR();yR();_R();bR();SR();vR();ER();NR=new Map,RR=new Map;c(vm,"registerCodegen");c(BR,"getCodegenEntry");c(CR,"getSnippetBuilder");vm(Te.CPU,{runtimeKind:"js",snippet:wR,compile(e,t){return{source:new dm(t).generate(e),metadata:{kind:"js",paramCount:e.params.length}}}});vm(Te.WASM,{runtimeKind:"wasm",snippet:xR,compile(e,t){let s=new bm(t).generate(e),n={kind:"wasm",memoryPages:s.memoryPages,bufferOffsets:s.bufferOffsets,imports:s.imports,params:s.params,bufferMap:e.bufferMap};return s.parallel&&(n.parallel=s.parallel),{source:s.wat,metadata:n}}});vm(Te.WEBGPU,{runtimeKind:"webgpu",snippet:AR,compile(e,t){let s=new xm(t).generate(e);return{source:s.source,metadata:{kind:"webgpu",workgroupSize:s.workgroupSize,dispatchSize:s.dispatchSize,sharedMemBytes:s.sharedMemBytes,params:s.params,bindings:s.bindings}}}});vm(Te.CUDA,{runtimeKind:"cuda",snippet:kR,compile(e,t,s){if(s&&s.matmulBackend==="cublas"&&e.cublasInfo)return{source:"",metadata:{kind:"cuda",cublas:e.cublasInfo,outputIndices:[e.cublasInfo.cIdx]}};let n=new mm(t).generate(e);return{source:n.source,metadata:{kind:"cuda",blockDim:n.blockDim,gridDim:n.gridDim,sharedMemBytes:n.sharedMemBytes,params:n.params,outputIndices:n.outputIndices,scratch:n.scratch}}}})});function IR(e){let t=[],s=[e.body];for(;s.length>0;){let m=s.pop();if(m){if(m.type==="BlockNode"){t.push(m),s.push(m.body);continue}if(m.body&&s.push(m.body),m.stmts)for(let g of m.stmts)s.push(g);m.thenBody&&s.push(m.thenBody),m.elseBody&&s.push(m.elseBody)}}let n=null;for(let m of t)if(m.name.includes("matmul"))m.reads.length>=2&&m.writes.length>=1&&(n=m);else return null;if(!n)return null;let r=n.reads[0].buffer,i=n.reads[1].buffer,o=n.writes[0].buffer;if(r.dtype!=="f32"||i.dtype!=="f32"||o.dtype!=="f32"||r.shape.length!==2||i.shape.length!==2||o.shape.length!==2)return null;let a=o.shape[0],l=o.shape[1],u=r.shape[1];if(![a,l,u].every(m=>typeof m=="number"))return null;let f=[];for(let[,m]of e.bufferMap)f.push(m.name);let h=f.indexOf(r.name),p=f.indexOf(i.name),d=f.indexOf(o.name);return h<0||p<0||d<0?null:{M:a,N:l,K:u,aIdx:h,bIdx:p,cIdx:d}}var fh,di,hh=I(()=>{aR();TR();c(IR,"detectPureMatmul");fh=class{static{c(this,"CompiledKernel")}constructor(t,s,n,r={}){this.name=t,this.source=s,this.target=n,this.metadata=r}snippet(){let t=CR(this.metadata.kind);if(!t)throw new Error(`No snippet for kind: ${this.metadata.kind}`);return t(this)}},di=class{static{c(this,"BackendPipeline")}constructor(t,s={}){this.target=t,this.matmulBackend=s.matmulBackend||"native",this.librarySelector=t.isCPU()?iR(t):t.isGPU()?oR(t):null}compile(t){let s=BR(this.target.kind);if(!s)throw new Error(`Unsupported target kind: ${this.target.kind}`);let{source:n,metadata:r}=s.compile(t,this.target,this);return new fh(t.name,n,this.target,r)}compileAll(t){return t.map(s=>this.compile(s))}}});function km(e,t){OR.set(e,t)}function MR(e){return OR.get(e)||null}var OR,Em=I(()=>{OR=new Map;c(km,"registerMeasurer");c(MR,"getMeasurer")});var LR={};Xt(LR,{runWasmParallel:()=>Az});import{Worker as wz}from"node:worker_threads";import bz from"node:os";function Sz(){if(!Zb){let e=Math.max(1,Math.min(bz.cpus().length-1,16));Zb=new t0(e)}return Zb}async function Az(e,t,s,n,r,i){let o=Sz();o._ensureWorkers();let{binary:a,bufferOffsets:l}=e,u=[...l.values()],f=Math.min(u.length,s.length),{extent:h,outputIndices:p}=r,d=new Set(p||[]),m=Math.min(o.numWorkers,Math.max(1,h)),g=Math.ceil(h/m),y=[];for(let S=0;S<f;S++){let E=s[S],v=(E instanceof Float32Array?E.length:0)/h;y.push(v>=1&&Number.isInteger(v)?v:0)}let _=u.slice(0,f);if(n)for(let S of n)_.push(S);let w=new SharedArrayBuffer(4*m),b=a.buffer.slice(a.byteOffset,a.byteOffset+a.byteLength),x=[];for(let S=0;S<m;S++){let E=S*g,R=Math.min(E+g,h);if(E>=h)break;let v=[];for(let B=0;B<f;B++){let N=s[B];N instanceof Float32Array&&v.push({offset:u[B],length:N.length,data:N.buffer.slice(0),fullLength:N.length,elemStart:0})}let k=[..._,E,R];x.push(o.dispatch(S,{wasmBinary:b,mathImportNames:i,bufferEntries:v,callArgs:k,controlBuffer:w,workerIdx:S,outputIndices:p||[]}))}let A=await Promise.all(x);for(let S=0;S<f;S++){if(!d.has(S)&&d.size>0)continue;let E=s[S];if(!(E instanceof Float32Array))continue;let R=y[S];if(R>0)for(let v of A){let k=v.outputs[S];if(!k)continue;let N=v.workerIdx*g,C=Math.min(N+g,h),O=new Float32Array(k),F=N*R,P=C*R;E.set(O.subarray(F,P),F)}else{let v=A.find(k=>k.workerIdx===0);v&&v.outputs[S]&&E.set(new Float32Array(v.outputs[S]))}}}var xz,Zb,t0,PR=I(()=>{xz=`
14
- const { parentPort } = require('node:worker_threads');
15
-
16
- const MATH = {
17
- exp: Math.exp, log: Math.log, sin: Math.sin, cos: Math.cos,
18
- tan: Math.tan, tanh: Math.tanh, pow: Math.pow,
19
- fmod: (a, b) => a % b,
20
- rsqrt: x => 1 / Math.sqrt(x),
21
- sign: Math.sign, round: Math.round,
22
- };
23
-
24
- parentPort.on('message', (msg) => {
25
- const { wasmBinary, mathImportNames, bufferEntries,
26
- callArgs, controlBuffer, workerIdx, outputIndices } = msg;
27
-
28
- const compiled = new WebAssembly.Module(new Uint8Array(wasmBinary));
29
-
30
- const mathImports = {};
31
- for (const name of mathImportNames) {
32
- mathImports[name] = MATH[name] || Math[name] || (x => x);
33
- }
34
-
35
- const instance = new WebAssembly.Instance(compiled, { math: mathImports });
36
- const wasmMem = instance.exports.memory;
37
- const fn = Object.values(instance.exports).find(v => typeof v === 'function');
38
-
39
- for (const entry of bufferEntries) {
40
- new Float32Array(wasmMem.buffer, entry.offset, entry.length).set(new Float32Array(entry.data));
41
- }
42
-
43
- fn(...callArgs);
44
-
45
- const outSet = new Set(outputIndices);
46
- const outputs = [];
47
- const transferable = [];
48
- for (let i = 0; i < bufferEntries.length; i++) {
49
- if (outSet.has(i)) {
50
- const entry = bufferEntries[i];
51
- const out = new Float32Array(entry.length);
52
- out.set(new Float32Array(wasmMem.buffer, entry.offset, entry.length));
53
- outputs.push(out.buffer);
54
- transferable.push(out.buffer);
55
- } else {
56
- outputs.push(null);
57
- }
58
- }
59
- const ctrl = new Int32Array(controlBuffer);
60
- parentPort.postMessage({ workerIdx, outputs }, transferable);
61
- Atomics.store(ctrl, workerIdx, 1);
62
- Atomics.notify(ctrl, workerIdx);
63
- });
64
- `,Zb=null;c(Sz,"getPool");t0=class{static{c(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 s=new wz(xz,{eval:!0});s.unref(),this._workers.push(s)}}dispatch(t,s){return this._ensureWorkers(),new Promise(n=>{let r=c(i=>{i.workerIdx===t&&(this._workers[t].removeListener("message",r),n(i))},"handler");this._workers[t].on("message",r),this._workers[t].postMessage(s)})}terminate(){for(let t of this._workers)t.terminate();this._workers=[]}};c(Az,"runWasmParallel")});import $R from"koffi";import{readdirSync as DR,existsSync as FR}from"fs";import{join as e0}from"path";function kz(){let e=[];process.env.CUDA_PATH&&e.push(process.env.CUDA_PATH);let t="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA";if(FR(t))for(let s of DR(t))e.push(e0(t,s));for(let s of e){let n=e0(s,"bin");if(!FR(n))continue;let r=DR(n).filter(i=>vz.test(i)&&!i.includes(".alt"));if(r.length>0)return process.env.PATH=n+";"+process.env.PATH,s0=s,r.sort().pop()}return"nvrtc64_120_0.dll"}function Lt(e,t){if(t!==0)throw new Error("CUDA driver error "+t+" in "+e)}function VR(e){let t=[0n];Yi.getProgramLogSize(e,t);let s=Number(t[0]);if(s<=1)return"";let n=new Uint8Array(s);Yi.getProgramLog(e,n);let r="";for(let i of n){if(i===0)break;r+=String.fromCharCode(i)}return r}var vz,s0,se,ol,n0,xt,Yi,zR,UR,mi=I(()=>{vz=/^nvrtc64_\d+_\d+\.dll$/,s0=null;c(kz,"resolveNvrtcName");se=$R.load("nvcuda.dll"),ol=$R.load(kz()),n0=s0?e0(s0,"include"):null,xt={init:se.func("int cuInit(uint)"),deviceGet:se.func("int cuDeviceGet(_Out_ int *dev, int ordinal)"),deviceGetAttribute:se.func("int cuDeviceGetAttribute(_Out_ int *pi, int attrib, int dev)"),primaryCtxRetain:se.func("int cuDevicePrimaryCtxRetain(_Out_ void **pctx, int dev)"),primaryCtxRelease:se.func("int cuDevicePrimaryCtxRelease_v2(int dev)"),ctxSynchronize:se.func("int cuCtxSynchronize()"),moduleLoadData:se.func("int cuModuleLoadData(_Out_ void **mod, void *image)"),moduleGetFunction:se.func("int cuModuleGetFunction(_Out_ void **func, void *mod, str name)"),memGetInfo:se.func("int cuMemGetInfo_v2(_Out_ uint64 *free, _Out_ uint64 *total)"),memAlloc:se.func("int cuMemAlloc_v2(_Out_ uint64 *dptr, size_t bytes)"),memFree:se.func("int cuMemFree_v2(uint64 dptr)"),memcpyHtoD:se.func("int cuMemcpyHtoD_v2(uint64 dst, void *src, size_t n)"),memcpyDtoH:se.func("int cuMemcpyDtoH_v2(void *dst, uint64 src, size_t n)"),memcpyDtoD:se.func("int cuMemcpyDtoD_v2(uint64 dst, uint64 src, size_t n)"),memcpyHtoDAsync:se.func("int cuMemcpyHtoDAsync_v2(uint64 dst, void *src, size_t n, void *stream)"),memcpyDtoHAsync:se.func("int cuMemcpyDtoHAsync_v2(void *dst, uint64 src, size_t n, void *stream)"),memcpyDtoDAsync:se.func("int cuMemcpyDtoDAsync_v2(uint64 dst, uint64 src, size_t n, void *stream)"),memsetD8:se.func("int cuMemsetD8_v2(uint64 dst, uint8 uc, size_t n)"),memsetD8Async:se.func("int cuMemsetD8Async(uint64 dst, uint8 uc, size_t n, void *stream)"),launchKernel:se.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:se.func("int cuStreamCreate(_Out_ void **stream, uint flags)"),streamSynchronize:se.func("int cuStreamSynchronize(void *stream)"),ctxSetCurrent:se.func("int cuCtxSetCurrent(void *ctx)"),streamBeginCapture:se.func("int cuStreamBeginCapture_v2(void *stream, int mode)"),streamEndCapture:se.func("int cuStreamEndCapture(void *stream, _Out_ void **graph)"),graphInstantiate:se.func("int cuGraphInstantiateWithFlags(_Out_ void **exec, void *graph, uint64 flags)"),graphLaunch:se.func("int cuGraphLaunch(void *exec, void *stream)"),graphExecDestroy:se.func("int cuGraphExecDestroy(void *exec)"),graphDestroy:se.func("int cuGraphDestroy(void *graph)")},Yi={createProgram:ol.func("int nvrtcCreateProgram(_Out_ void **prog, str src, str name, int n, void *h, void *inc)"),compileProgram:ol.func("int nvrtcCompileProgram(void *prog, int n, str *opts)"),destroyProgram:ol.func("int nvrtcDestroyProgram(void **prog)"),getPTXSize:ol.func("int nvrtcGetPTXSize(void *prog, _Out_ size_t *sz)"),getPTX:ol.func("int nvrtcGetPTX(void *prog, _Out_ uint8_t *ptx)"),getProgramLogSize:ol.func("int nvrtcGetProgramLogSize(void *prog, _Out_ size_t *sz)"),getProgramLog:ol.func("int nvrtcGetProgramLog(void *prog, _Out_ uint8_t *log)")},zR=75,UR=76;c(Lt,"checkCU");c(VR,"readProgramLog")});function Yt(){if(Ic)return Lt("cuCtxSetCurrent",xt.ctxSetCurrent(Ic.ctx)),Ic;Lt("cuInit",xt.init(0));let e=[0];Lt("cuDeviceGet",xt.deviceGet(e,0));let t=[0],s=[0];Lt("cuDeviceGetAttribute",xt.deviceGetAttribute(t,zR,e[0])),Lt("cuDeviceGetAttribute",xt.deviceGetAttribute(s,UR,e[0]));let n=[null];Lt("cuDevicePrimaryCtxRetain",xt.primaryCtxRetain(n,e[0])),Lt("cuCtxSetCurrent",xt.ctxSetCurrent(n[0]));let r=[null];Lt("cuStreamCreate",xt.streamCreate(r,0));let i=[0n],o=[0n];return Lt("cuMemGetInfo",xt.memGetInfo(i,o)),Ic={dev:e[0],ctx:n[0],stream:r[0],arch:"sm_"+t[0]+s[0],totalMem:Number(o[0])},process.on("exit",()=>{try{xt.primaryCtxRelease(Ic.dev)}catch{}}),Ic}var Ic,gi=I(()=>{mi();Ic=null;c(Yt,"getDevice")});function Nm(e){let t=2166136261;for(let s=0;s<e.length;s++)t^=e.charCodeAt(s),t=Math.imul(t,16777619);return(t>>>0).toString(16)}function Rm(e,t){let s=Nm(e),n=qR.get(s);if(n)return n;let{arch:r}=Yt(),i="";e.includes("__half")&&(i+=`#include <cuda_fp16.h>
65
- `),e.includes("__nv_bfloat16")&&(i+=`#include <cuda_bf16.h>
66
- `),/u?int(8|16|64)_t/.test(e)&&(i+=Nz),/mma_sync|wmma::|fragment</.test(e)&&(i+=`#include <mma.h>
67
- using namespace nvcuda::wmma;
68
- `),e.includes("__pipeline_memcpy_async")&&(i+=`#include <cuda_pipeline.h>
69
- `);let o=i+Ez+`extern "C" {
70
- `+e+`
71
- }
72
- `,a=[null];Lt("nvrtcCreateProgram",Yi.createProgram(a,o,t+".cu",0,null,null));let l=["--gpu-architecture="+r];if(n0&&l.push("--include-path="+n0),Yi.compileProgram(a[0],l.length,l)!==0){let p=VR(a[0]);throw Yi.destroyProgram(a),new Error("NVRTC compile failed for kernel "+t+`:
73
- `+p+`
74
- --- source ---
75
- `+e)}let f=[0n];Lt("nvrtcGetPTXSize",Yi.getPTXSize(a[0],f));let h=new Uint8Array(Number(f[0]));return Lt("nvrtcGetPTX",Yi.getPTX(a[0],h)),Yi.destroyProgram(a),qR.set(s,h),h}function r0(e){let t=jR.get(e);return t||(t=yi(e.source,e.name),jR.set(e,t),t)}function yi(e,t){let s=Nm(e)+":"+t,n=GR.get(s);if(n)return n;let r=Rm(e,t),i=[null];Lt("cuModuleLoadData",xt.moduleLoadData(i,r));let o=[null];Lt("cuModuleGetFunction",xt.moduleGetFunction(o,i[0],t));let a={func:o[0],module:i[0]};return GR.set(s,a),a}var GR,Ez,Nz,qR,jR,i0=I(()=>{mi();gi();GR=new Map,Ez=`#ifndef INFINITY
76
- #define INFINITY __int_as_float(0x7f800000)
77
- #endif
78
- #ifndef NAN
79
- #define NAN __int_as_float(0x7fffffff)
80
- #endif
81
- `,Nz=`typedef signed char int8_t;
82
- typedef short int16_t;
83
- typedef int int32_t;
84
- typedef long long int64_t;
85
- typedef unsigned char uint8_t;
86
- typedef unsigned short uint16_t;
87
- typedef unsigned int uint32_t;
88
- typedef unsigned long long uint64_t;
89
- `;c(Nm,"hashSource");qR=new Map;c(Rm,"compileToPTX");jR=new WeakMap;c(r0,"getProgramFor");c(yi,"getProgram")});var KR={};Xt(KR,{acquire:()=>Ee,alloc:()=>WR,copyDeviceToHost:()=>_i,copyDeviceToHostAsync:()=>o0,copyHostToDevice:()=>Po,copyHostToDeviceAsync:()=>Bm,drainPool:()=>Cm,free:()=>Qi,release:()=>Ge});function WR(e){let t=[0n];return Lt("cuMemAlloc",xt.memAlloc(t,e)),t[0]}function Po(e,t){Lt("cuMemcpyHtoD",xt.memcpyHtoD(e,t,t.byteLength))}function _i(e,t){Lt("cuMemcpyDtoH",xt.memcpyDtoH(e,t,e.byteLength))}function Bm(e,t){Lt("cuMemcpyHtoDAsync",xt.memcpyHtoDAsync(e,t,t.byteLength,Yt().stream))}function o0(e,t){Lt("cuMemcpyDtoHAsync",xt.memcpyDtoHAsync(e,t,e.byteLength,Yt().stream))}function Qi(e){xt.memFree(e)}function Ee(e){let t=ph.get(e);return t&&t.length>0?t.pop():WR(e)}function Ge(e,t){let s=ph.get(t);s||(s=[],ph.set(t,s)),s.push(e)}function Cm(){let e=0;for(let t of ph.values())for(let s of t)Qi(s),e++;return ph.clear(),e}var ph,al=I(()=>{mi();gi();c(WR,"alloc");c(Po,"copyHostToDevice");c(_i,"copyDeviceToHost");c(Bm,"copyHostToDeviceAsync");c(o0,"copyDeviceToHostAsync");c(Qi,"free");ph=new Map;c(Ee,"acquire");c(Ge,"release");c(Cm,"drainPool")});function l0(e){HR=e}function Ss(){return HR}function Tm(e){JR=e}function $s(){return JR}function Oc(e){YR=e}function QR(){return YR}function XR(e){a0=e}function c0(){a0&&a0()}var HR,a0,JR,YR,Xi=I(()=>{HR=!1,a0=null,JR=!1,YR=!1;c(l0,"setEagerDeferred");c(Ss,"isEagerDeferred");c(Tm,"setEagerCapturing");c($s,"isEagerCapturing");c(Oc,"setCudaGraphArmed");c(QR,"isCudaGraphArmed");c(XR,"setEagerFlushHook");c(c0,"eagerFlush")});function Rz(e){let t=Buffer.alloc(8);return t.writeBigUInt64LE(BigInt(e)),t}function un(e){return new Im("f32",e)}function u0(e){let t=Buffer.alloc(4);return e instanceof Im?(e.kind==="f32"?t.writeFloatLE(e.value):t.writeInt32LE(e.value|0),t):(Number.isInteger(e)?t.writeInt32LE(e|0):t.writeFloatLE(e),t)}function rr(e,t,s,n,r,i,o=!0){let{stream:a}=Yt(),l=[];for(let u of r)l.push(Rz(u));for(let u of i)l.push(u0(u));Lt("cuLaunchKernel",xt.launchKernel(e,t[0],t[1],t[2],s[0],s[1],s[2],n,a,l,null)),o&&!$s()&&Lt("cuStreamSynchronize",xt.streamSynchronize(a))}var Im,f0=I(()=>{mi();gi();Xi();c(Rz,"devicePtrParam");Im=class{static{c(this,"TypedScalar")}constructor(t,s){this.kind=t,this.value=s}};c(un,"f32");c(u0,"scalarParam");c(rr,"launch")});import sB from"koffi";import{readdirSync as ZR,existsSync as tB}from"fs";import{join as eB}from"path";function Bz(e,t){let s=[];process.env.CUDA_PATH&&s.push(process.env.CUDA_PATH);let n="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA";if(tB(n))for(let r of ZR(n))s.push(eB(n,r));for(let r of s){let i=eB(r,"bin");if(!tB(i))continue;let o=ZR(i).filter(a=>e.test(a));if(o.length>0)return process.env.PATH.includes(i)||(process.env.PATH=i+";"+process.env.PATH),o.sort().pop()}return t}function Mc(){Cz(0)}function rB(){let e=Iz();if(e!==0)throw new Error("cudaDeviceSynchronize failed: "+e)}function Lz(e){let t=[null],s=Tz(t,e);if(s!==0)throw new Error("cudaMalloc failed: "+s);return t[0]}function Om(e,t){let s=nB(e,t,t.byteLength,Oz);if(s!==0)throw new Error("cudaMemcpy H2D failed: "+s)}function p0(e,t){let s=nB(e,t,e.byteLength,Mz);if(s!==0)throw new Error("cudaMemcpy D2H failed: "+s)}function d0(e){return typeof e=="bigint"?e:typeof e=="number"?BigInt(e):sB.address(e)}function m0(e){let t=h0.get(e);return t&&t.length>0?t.pop():Lz(e)}function iB(e,t){let s=h0.get(t);s||(s=[],h0.set(t,s)),s.push(e)}var dh,Cz,Tz,nB,K7,Iz,Oz,Mz,h0,g0=I(()=>{c(Bz,"resolveLib");dh=sB.load(Bz(/^cudart64_\d+\.dll$/,"cudart64_12.dll")),Cz=dh.func("int cudaSetDevice(int d)"),Tz=dh.func("int cudaMalloc(_Out_ void **p, size_t s)"),nB=dh.func("int cudaMemcpy(void *dst, void *src, size_t n, int kind)"),K7=dh.func("int cudaFree(void *p)"),Iz=dh.func("int cudaDeviceSynchronize()"),Oz=1,Mz=2;c(Mc,"setDevice");c(rB,"devSync");c(Lz,"devAlloc");c(Om,"devH2D");c(p0,"devD2H");c(d0,"devAddr");h0=new Map;c(m0,"acquireDevice");c(iB,"releaseDevice")});var aB={};Xt(aB,{CU_STREAM_CAPTURE_MODE_GLOBAL:()=>Pz,CU_STREAM_CAPTURE_MODE_RELAXED:()=>Dz,CU_STREAM_CAPTURE_MODE_THREAD_LOCAL:()=>oB,beginEagerCapture:()=>y0,destroyEagerGraph:()=>b0,endEagerCapture:()=>Mm,isGraphCapturing:()=>Fz,replay:()=>_0,syncStream:()=>w0});function Fz(){return $s()}function y0(e=oB){let{stream:t}=Yt();Tm(!0);try{Lt("cuStreamBeginCapture",xt.streamBeginCapture(t,e))}catch(s){throw Tm(!1),s}}function Mm(){let{stream:e}=Yt(),t=[null],s=xt.streamEndCapture(e,t);Tm(!1),Lt("cuStreamEndCapture",s);let n=[null];return Lt("cuGraphInstantiateWithFlags",xt.graphInstantiate(n,t[0],0n)),{graph:t[0],exec:n[0]}}function _0(e){let{stream:t}=Yt();Lt("cuGraphLaunch",xt.graphLaunch(e,t))}function w0(){let{stream:e}=Yt();Lt("cuStreamSynchronize",xt.streamSynchronize(e))}function b0(e){e&&(e.exec&&xt.graphExecDestroy(e.exec),e.graph&&xt.graphDestroy(e.graph))}var Pz,oB,Dz,Lm=I(()=>{mi();gi();Xi();Pz=0,oB=1,Dz=2;c(Fz,"isGraphCapturing");c(y0,"beginEagerCapture");c(Mm,"endEagerCapture");c(_0,"replay");c(w0,"syncStream");c(b0,"destroyEagerGraph")});var mB={};Xt(mB,{cublasGemmBatchedDevice:()=>E0,cublasGemmDevice:()=>k0,cublasMatmul:()=>Kz,cublasMatmulDevice:()=>Vm});import hB from"koffi";import{readdirSync as lB,existsSync as cB}from"fs";import{join as uB}from"path";function pB(e,t){let s=[];process.env.CUDA_PATH&&s.push(process.env.CUDA_PATH);let n="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA";if(cB(n))for(let r of lB(n))s.push(uB(n,r));for(let r of s){let i=uB(r,"bin");if(!cB(i))continue;let o=lB(i).filter(a=>e.test(a));if(o.length>0)return process.env.PATH.includes(i)||(process.env.PATH=i+";"+process.env.PATH),o.sort().pop()}return t}function Um(){if(!mh){let e=[null],t=Uz(e);if(t!==0)throw new Error("cublasCreate failed: "+t);mh=e[0],Vz(mh,Yt().stream),Gz(mh,3)}return mh}function S0(e){let t=v0.get(e);if(t&&t.length>0)return t.pop();let s=[null];if($z(s,e))throw new Error("cudaMalloc failed");return s[0]}function A0(e,t){let s=v0.get(t);s||(s=[],v0.set(t,s)),s.push(e)}function Vm(e,t,s,n,r,i,o=!1){Fm(0);let a=Um(),f=dB(a,o?1:0,0,t,e,s,$m,r,o?s:t,n,s,zm,i,t);if(f!==0)throw new Error("cublasSgemm failed: "+f)}function k0(e,t,s,n,r,i,o,a){Fm(0);let l=Um(),d=qz(l,o?Dm:Pm,r?Dm:Pm,t,e,s,$m,i,o?s:t,n,r?e:s,zm,a,t);if(d!==0)throw new Error("cublasSgemm failed: "+d)}function E0(e,t,s,n,r,i,o,a,l,u,f,h){Fm(0);let p=Um(),d=o?Dm:Pm,m=u?Dm:Pm,g=o?t:n,_=jz(p,m,d,s,t,n,$m,a,u?n:s,BigInt(l),r,g,BigInt(i),zm,f,s,BigInt(h),e);if(_!==0)throw new Error("cublasSgemmStridedBatched failed: "+_)}function Kz(e,t,s,n,r,i,o=!1){Fm(0);let a=Um(),l=S0(n.byteLength),u=S0(r.byteLength),f=S0(i.byteLength);try{x0(l,n,n.byteLength,fB),x0(u,r,r.byteLength,fB);let d=dB(a,o?1:0,0,t,e,s,$m,u,o?s:t,l,s,zm,f,t);if(d!==0)throw new Error("cublasSgemm failed: "+d);zz(),x0(i,f,i.byteLength,Wz)}finally{A0(l,n.byteLength),A0(u,r.byteLength),A0(f,i.byteLength)}}var gh,Lc,Fm,$z,x0,r9,zz,Uz,Vz,Gz,dB,qz,jz,fB,Wz,Pm,Dm,$m,zm,mh,v0,Gm=I(()=>{gi();c(pB,"resolveLib");gh=hB.load(pB(/^cudart64_\d+\.dll$/,"cudart64_12.dll")),Lc=hB.load(pB(/^cublas64_\d+\.dll$/,"cublas64_12.dll")),Fm=gh.func("int cudaSetDevice(int d)"),$z=gh.func("int cudaMalloc(_Out_ void **p, size_t s)"),x0=gh.func("int cudaMemcpy(void *dst, void *src, size_t n, int kind)"),r9=gh.func("int cudaFree(void *p)"),zz=gh.func("int cudaDeviceSynchronize()"),Uz=Lc.func("int cublasCreate_v2(_Out_ void **h)"),Vz=Lc.func("int cublasSetStream_v2(void *h, void *streamId)"),Gz=Lc.func("int cublasSetMathMode(void *h, int mode)"),dB=Lc.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)"),qz=Lc.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)"),jz=Lc.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)"),fB=1,Wz=2,Pm=0,Dm=1,$m=new Float32Array([1]),zm=new Float32Array([0]),mh=null;c(Um,"handle");v0=new Map;c(S0,"acquire");c(A0,"release");c(Vm,"cublasMatmulDevice");c(k0,"cublasGemmDevice");c(E0,"cublasGemmBatchedDevice");c(Kz,"cublasMatmul")});function _B(e){N0=e}function wB(){return N0}function bB(e,t){let s=Nm(e)+":"+t,n=gB.get(s);if(n)return n;let r=Rm(e,t);Mc();let i=[null];Lt("cuModuleLoadData",xt.moduleLoadData(i,r));let o=[null];return Lt("cuModuleGetFunction",xt.moduleGetFunction(o,i[0],t)),gB.set(s,o[0]),o[0]}function Hz(e){let t=Buffer.alloc(8);return t.writeBigUInt64LE(e),t}function xB(e,t,s,n,r,i,o=null){let a=[];for(let l of r)a.push(Hz(l));for(let l of i)a.push(u0(l));Lt("cuLaunchKernel",xt.launchKernel(e,t[0],t[1],t[2],s[0],s[1],s[2],n,o,a,null))}function Jz(e,t,s){let n=e.inputSlots.concat(e.outputSlots),r=e.kernel.metadata;if(r.cublas){let{M:i,N:o,K:a,aIdx:l,bIdx:u,cIdx:f,transB:h}=r.cublas;Vm(i,o,a,t[n[l]],t[n[u]],t[n[f]],h)}else{let i=n.map(o=>d0(t[o]));xB(Yz(e),r.gridDim,r.blockDim,0,i,e.shapeValues||[],s)}}function Yz(e){return SB.get(e.name)}function Qz(e,t,s){Mc();let n=Yt().stream,r=new Map;for(let l of s)l.kernel.metadata.cublas||r.set(l.name,bB(l.kernel.source,l.kernel.name));SB=r;let i=new Set;for(let l of s)for(let u of l.outputSlots)i.add(u);let o=new Set(e.argSlots),a=yB.get(e);if(a)for(let l=0;l<e.numSlots;l++)t[l]&&!i.has(l)&&Om(a.dptr[l],t[l].data);else{let l=new Array(e.numSlots).fill(null);for(let u=0;u<e.numSlots;u++){let f=t[u];f&&(l[u]=m0(Math.max(f.data.byteLength,1)),i.has(u)||Om(l[u],f.data))}y0();try{for(let f of s)Jz(f,l,n);let u=Mm();a={dptr:l,graph:u.graph,exec:u.exec}}catch(u){try{Mm()}catch{}throw u}yB.set(e,a)}_0(a.exec),w0();for(let l=0;l<e.numSlots;l++)a.dptr[l]!==null&&i.has(l)&&o.has(l)&&p0(t[l].data,a.dptr[l])}async function AB(e,t,s){if(N0)return Qz(e,t,s);for(let l of s)l.kernel.metadata.cublas||Rm(l.kernel.source,l.kernel.name);Mc();let n=new Map;for(let l of s)l.kernel.metadata.cublas||n.set(l.name,bB(l.kernel.source,l.kernel.name));let r=new Set;for(let l of s)for(let u of l.outputSlots)r.add(u);let i=new Array(e.numSlots).fill(null),o=new Array(e.numSlots).fill(0);for(let l=0;l<e.numSlots;l++){let u=t[l];if(!u)continue;let f=Math.max(u.data.byteLength,1);o[l]=f,i[l]=m0(f),r.has(l)||Om(i[l],u.data)}for(let l of s){let u=l.inputSlots.concat(l.outputSlots),f=l.kernel.metadata;if(f.cublas){let{M:h,N:p,K:d,aIdx:m,bIdx:g,cIdx:y,transB:_}=f.cublas;Vm(h,p,d,i[u[m]],i[u[g]],i[u[y]],_)}else{let h=u.map(p=>d0(i[p]));xB(n.get(l.name),f.gridDim,f.blockDim,0,h,l.shapeValues||[])}}rB();let a=new Set(e.argSlots);for(let l=0;l<e.numSlots;l++)i[l]!==null&&r.has(l)&&a.has(l)&&p0(t[l].data,i[l]);for(let l=0;l<e.numSlots;l++)i[l]!==null&&iB(i[l],o[l])}var gB,yB,N0,SB,vB=I(()=>{mi();i0();g0();gi();Lm();Gm();f0();gB=new Map,yB=new WeakMap,N0=!1;c(_B,"setCudaGraphEnabled");c(wB,"isCudaGraphEnabled");c(bB,"loadFunctionOnPrimary");c(Hz,"bufferParam");c(xB,"launchOnPrimary");c(Jz,"_runStepGraphed");SB=null;c(Yz,"funcsFor");c(Qz,"runCudaPlanGraphed");c(AB,"runCudaPlan")});var EB={};Xt(EB,{clearCapturePins:()=>sU,deviceBufferDptr:()=>nU,deviceBufferForInplace:()=>qm,deviceBufferForInput:()=>wi,deviceBufferForOutput:()=>Do,downloadAndValidate:()=>B0,downloadStaticAsync:()=>iU,flushDeferred:()=>jm,hostReadHook:()=>M0,isEagerDeferred:()=>Ss,pinResident:()=>C0,releaseAllResident:()=>O0,setCudaGraphArmed:()=>Oc,setEagerDeferred:()=>l0,unpinResident:()=>eU,uploadIfStale:()=>ll,uploadStaticAsync:()=>rU});function Zz(e){let t=br.get(e);if(t)return br.delete(e),br.set(e,t),t;if(br.size>=Xz){let s=br.keys().next().value,n=br.get(s);br.delete(s),Ge(n.dptr,n.bytes)}return t={dptr:Ee(e.byteLength),bytes:e.byteLength,valid:!1},br.set(e,t),t}function ll(e){let t=Zz(e);return t.valid||(Po(t.dptr,e),t.valid=!0),t.dptr}function B0(e,t){_i(e,t);let s=br.get(e);s&&(s.valid=!0)}function C0(e){_h.add(e)}function eU(e){_h.delete(e)}function sU(){wh.clear()}function nU(e){let t=xr.get(e);return t?t.dptr:null}function rU(e,t){Bm(e,t)}function iU(e,t){o0(e,t)}function oU(){return R0||(R0=Math.floor(Yt().totalMem*tU)),R0}function T0(e,t){if($s())throw new Error("illegal device sync during CUDA graph capture");Lt("cuStreamSynchronize",xt.streamSynchronize(Yt().stream)),_i(e,t)}function aU(e){let t=oU();for(let[s,n]of xr){if(yh+e<=t)break;_h.has(s)||wh.has(s)||(n.hostStale&&T0(s,n.dptr),Ge(n.dptr,n.bytes),yh-=n.bytes,xr.delete(s))}}function I0(e){let t=xr.get(e);return t?(xr.delete(e),xr.set(e,t)):($s()||aU(e.byteLength),t={dptr:Ee(e.byteLength),bytes:e.byteLength,deviceFresh:!1,hostStale:!1},xr.set(e,t),yh+=t.bytes),$s()&&wh.add(e),t}function kB(e,t){$s()?Bm(e,t):Po(e,t)}function wi(e){let t=I0(e);return t.deviceFresh||(kB(t.dptr,e),t.deviceFresh=!0),t.dptr}function Do(e){let t=I0(e);return t.deviceFresh=!0,t.hostStale=!0,t.dptr}function qm(e){let t=I0(e);return t.deviceFresh||(kB(t.dptr,e),t.deviceFresh=!0),t.hostStale=!0,t.dptr}function jm(){for(let[e,t]of xr)_h.has(e)||wh.has(e)||(Ge(t.dptr,t.bytes),yh-=t.bytes,xr.delete(e))}function O0(){for(let[e,t]of xr)t.hostStale&&T0(e,t.dptr),Ge(t.dptr,t.bytes);xr.clear(),yh=0,_h.clear(),wh.clear();for(let e of br.values())Ge(e.dptr,e.bytes);br.clear()}function M0(e){let t=xr.get(e);if($s()){if(t&&t.hostStale)throw new Error("illegal host read of device-resident tensor during CUDA graph capture");return}let s=br.get(e);s&&(s.valid=!1),t&&(t.hostStale&&(T0(e,t.dptr),t.hostStale=!1),t.deviceFresh=!1)}var Xz,br,tU,xr,_h,wh,yh,R0,bh=I(()=>{al();gi();mi();Xi();Xz=1024,br=new Map;c(Zz,"_safeEntry");c(ll,"uploadIfStale");c(B0,"downloadAndValidate");tU=.5,xr=new Map,_h=new Set,wh=new Set,yh=0,R0=0;c(C0,"pinResident");c(eU,"unpinResident");c(sU,"clearCapturePins");c(nU,"deviceBufferDptr");c(rU,"uploadStaticAsync");c(iU,"downloadStaticAsync");c(oU,"_cap");c(T0,"_syncDownload");c(aU,"_evict");c(I0,"_defEntry");c(kB,"_upload");c(wi,"deviceBufferForInput");c(Do,"deviceBufferForOutput");c(qm,"deviceBufferForInplace");c(jm,"flushDeferred");c(O0,"releaseAllResident");c(M0,"hostReadHook");XR(jm);l0(!0)});function me(e){return cU[e]||Float32Array}function Wm(e,t){if(e===t)return e;let s=Pc.get(e)??0,n=Pc.get(t)??0;return s>=n?e:t}function Wt(e,t){let s=BB.get(e);s||(s=new Set,BB.set(e,s)),s.add(t)}function cl(e){return lU(e)}var W,lU,cU,NB,RB,Pc,BB,ir=I(()=>{It();W=Q,lU=ji,cU=Object.freeze({[W.F16]:Uint16Array,[W.BF16]:Uint16Array,[W.F32]:Float32Array,[W.F64]:Float64Array,[W.I8]:Int8Array,[W.I16]:Int16Array,[W.I32]:Int32Array,[W.I64]:BigInt64Array,[W.UI8]:Uint8Array,[W.BOOL]:Uint8Array,[W.INDEX]:Int32Array});c(me,"typedArrayCtor");NB=[W.F16,W.BF16,W.F32,W.F64],RB=[W.UI8,W.I8,W.I16,W.I32,W.I64],Pc=new Map;for(let e=0;e<NB.length;e++)Pc.set(NB[e],100+e);for(let e=0;e<RB.length;e++)Pc.set(RB[e],e);Pc.set(W.BOOL,-1);Pc.set(W.INDEX,50);c(Wm,"resultDtype");BB=new Map;c(Wt,"_addCastPair");Wt(W.BOOL,W.I8);Wt(W.BOOL,W.I16);Wt(W.BOOL,W.I32);Wt(W.BOOL,W.I64);Wt(W.BOOL,W.F16);Wt(W.BOOL,W.F32);Wt(W.BOOL,W.F64);Wt(W.UI8,W.I16);Wt(W.UI8,W.I32);Wt(W.UI8,W.I64);Wt(W.UI8,W.F16);Wt(W.UI8,W.F32);Wt(W.UI8,W.F64);Wt(W.I8,W.I16);Wt(W.I8,W.I32);Wt(W.I8,W.I64);Wt(W.I8,W.F16);Wt(W.I8,W.F32);Wt(W.I8,W.F64);Wt(W.I16,W.I32);Wt(W.I16,W.I64);Wt(W.I16,W.F32);Wt(W.I16,W.F64);Wt(W.I32,W.I64);Wt(W.I32,W.F64);Wt(W.F16,W.F32);Wt(W.F16,W.F64);Wt(W.BF16,W.F32);Wt(W.BF16,W.F64);Wt(W.BOOL,W.BF16);Wt(W.UI8,W.BF16);Wt(W.I8,W.BF16);Wt(W.F32,W.F64);c(cl,"dtypeSize")});function hl(){return CB}function TB(e){CB=e}var Ie,ul,Dc,fl,Fc,Sr,$c,CB,vn=I(()=>{Qn();Ie=Object.freeze({CPU:"cpu",GPU:"gpu",WASM:"wasm",WEBGPU:"webgpu",META:"meta",LAZY:"lazy"}),ul=class{static{c(this,"Device")}constructor(t,s=0){this.type=t,this.index=s}dispatchKey(){return oc(this.type)}equals(t){return this.type===t.type&&this.index===t.index}hash(){let t=2166136261;for(let s=0;s<this.type.length;s++)t=(t^this.type.charCodeAt(s))*16777619&2147483647;return t=(t^this.index)*16777619&2147483647,t}toString(){return this.index===0?this.type:`${this.type}:${this.index}`}},Dc=new ul(Ie.CPU),fl=new ul(Ie.GPU),Fc=new ul(Ie.WASM),Sr=new ul(Ie.WEBGPU),$c=new ul(Ie.META),CB=Dc;c(hl,"getDefaultDevice");c(TB,"setDefaultDevice")});function L0(e){return IB.get(e)||IB.get(Ie.CPU)}var Km,Hm,IB,pl,P0=I(()=>{ir();vn();Km=class{static{c(this,"CPUAllocator")}allocate(t,s){let n=me(s);return new n(t)}free(){}},Hm=class{static{c(this,"MetaAllocator")}allocate(){return null}free(){}},IB=new Map([[Ie.CPU,new Km],[Ie.WASM,new Km],[Ie.META,new Hm],[Ie.LAZY,new Hm]]);c(L0,"getAllocator");pl=class e{static{c(this,"StorageImpl")}static#t=null;static setHostReadHook(t){e.#t=t}constructor(t,s,n,r){this._data=t,this._nbytes=s,this._device=n,this._allocator=r,this._refCount=1}static allocate(t,s,n){let r=L0(n.type),i=cl(s),o=Math.max(Math.ceil(t/i),1),a=r.allocate(o,s);return new e(a,t,n,r)}static fromData(t,s){let n=t?t.byteLength:0,r=L0(s.type);return new e(t,n,s,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 e.#t&&this._data&&e.#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,s){if(t<=this._nbytes&&this._data)return;let n=cl(s),r=Math.max(Math.ceil(t/n),1),i=this._allocator.allocate(r,s);if(this._data&&i){let o=Math.min(this._data.length,i.length);for(let a=0;a<o;a++)i[a]=this._data[a]}this._allocator&&this._data&&this._allocator.free(this._data),this._data=i,this._nbytes=t}clone(){let t=L0(this._device.type),s=null;if(this._data){e.#t&&e.#t(this._data);let n=this._data.constructor;s=new n(this._data.length),s.set(this._data)}return new e(s,this._nbytes,this._device,t)}}});function As(e){let t=e.length,s=new Array(t),n=1;for(let r=t-1;r>=0;r--)s[r]=n,n*=e[r];return s}function gs(e){let t=1;for(let s=0;s<e.length;s++)t*=e[s];return t}function D0(e,t){let s=e.length;if(s===0)return!0;let n=1;for(let r=s-1;r>=0;r--){if(e[r]===0)return!0;if(e[r]!==1&&t[r]!==n)return!1;n*=e[r]}return!0}function zc(e,t){let s=e.length,n=t.length,r=Math.max(s,n),i=new Array(r);for(let o=0;o<r;o++){let a=o<s?e[s-1-o]:1,l=o<n?t[n-1-o]:1;if(a===l)i[r-1-o]=a;else if(a===1)i[r-1-o]=l;else if(l===1)i[r-1-o]=a;else return null}return i}function Jm(e,t){let s=e.length,n=t.length;if(s===1&&n===1)return[];let r=s===1?[1,e[0]]:e,i=n===1?[t[0],1]:t,o=r.length,a=i.length,l=zc(r.slice(0,o-2),i.slice(0,a-2));if(l===null)return null;let u=[...l,r[o-2],i[a-1]],f=new Set;return n===1&&f.add(u.length-1),s===1&&f.add(u.length-2),u.filter((h,p)=>!f.has(p))}function OB(e,t,s){let n=gs(e),r=-1,i=1,o=new Array(s.length);for(let a=0;a<s.length;a++)if(s[a]===-1){if(r!==-1)return null;r=a,o[a]=-1}else o[a]=s[a],i*=s[a];if(r!==-1){if(i===0||(o[r]=n/i|0,o[r]*i!==n))return null}else if(gs(o)!==n)return null;return D0(e,t)?{sizes:o,strides:As(o),needsCopy:!1}:{sizes:o,strides:As(o),needsCopy:!0}}var Vr=I(()=>{c(As,"computeStrides");c(gs,"computeNumel");c(D0,"isContiguous");c(zc,"broadcastShapes");c(Jm,"matmulOutputShape");c(OB,"inferReshape")});function MB(e,t){if(e.length<4)return!1;let s=e.length,n=1,r=s===4?uU:fU;if(!r)return!1;for(let i=s-1;i>=0;i--){let o=r[i];if(e[o]===0||t[o]!==n)return!1;n*=e[o]}return!0}var F0,uU,fU,$0=I(()=>{F0=Object.freeze({CONTIGUOUS:"contiguous",CHANNELS_LAST:"channels_last",PRESERVE:"preserve"}),uU=Object.freeze([0,2,3,1]),fU=Object.freeze([0,2,3,4,1]);c(MB,"isChannelsLast")});var Oe,Zi=I(()=>{Qn();Vr();$0();$0();Oe=class{static{c(this,"TensorImpl")}constructor(t,s,n,r,i,o){this._storage=t,this._storageOffset=s,this._sizes=Object.freeze([...n]),this._strides=Object.freeze(r?[...r]:As(n)),this._dtype=i,this._device=o,this._numel=gs(n),this._keySet=Yn.fromKey(oc(o.type)),this._autogradMeta=null,this._version=0,this._contiguousCache=null}get storage(){return this._storage}get storageOffset(){return this._storageOffset}size(t){let s=t<0?this._sizes.length+t:t;return this._sizes[s]}stride(t){let s=t<0?this._strides.length+t:t;return this._strides[s]}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===F0.CONTIGUOUS?(this._contiguousCache===null&&(this._contiguousCache=D0(this._sizes,this._strides)),this._contiguousCache):t===F0.CHANNELS_LAST?MB(this._sizes,this._strides):!1}setSizesAndStrides(t,s){this._sizes=Object.freeze([...t]),this._strides=Object.freeze(s?[...s]:As(t)),this._numel=gs(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=Yn.fromKey(oc(this._device.type));if(this._autogradMeta&&this._autogradMeta.requiresGrad){let s=oc(this._device.type);t=t.add(TE(s))}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 Ar,Uc=I(()=>{Ar=class{static{c(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,s){this._gradFn=t,this._outputNr=s??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 PB(e,t,s,n,r,i){let o=t[r];if(r===t.length-1){let l=new Array(o);if(i)for(let u=0;u<o;u++)l[u]=hm(i,e[n+u*s[r]]);else for(let u=0;u<o;u++)l[u]=e[n+u*s[r]];return l}let a=new Array(o);for(let l=0;l<o;l++)a[l]=PB(e,t,s,n+l*s[r],r+1,i);return a}function hU(){if(!z0)throw new Error("Autograd engine not initialized. Import autograd/engine.js first.");return z0}function DB(e){z0=e}var LB,Et,z0,Js=I(()=>{Zi();Uc();Rc();LB=new Set(["f16","bf16","i64"]),Et=class e{static{c(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 s=t.data;return this._impl.storageOffset===0&&this._impl.isContiguous()&&s.length===this.numel,s}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 s=this._ensureAutogradMeta();return s.requiresGrad=t,this._impl._updateKeySet(),this}retainGrad(){return this._ensureAutogradMeta().retainGrad=!0,this}detach(){let t=new Oe(this._impl.storage,this._impl.storageOffset,this._impl.sizes(),this._impl.strides(),this._impl.dtype,this._impl.device);return new e(t)}item(){if(this.numel!==1)throw new Error(`item() requires tensor with exactly 1 element, got ${this.numel}`);let s=this._impl.storage.data[this._impl.storageOffset];return LB.has(this._impl.dtype)?hm(this._impl.dtype,s):s}toArray(){let t=this.shape,s=this.strides,n=this._impl.storage.data,r=this._impl.storageOffset,i=LB.has(this._impl.dtype)?this._impl.dtype:null;return t.length===0?i?hm(i,n[r]):n[r]:PB(n,t,s,r,0,i)}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 s=0;s<t;s++)yield this._select(0,s)}_select(t,s){let n=this._impl.sizes(),r=this._impl.strides(),i=[],o=[];for(let u=0;u<n.length;u++)u!==t&&(i.push(n[u]),o.push(r[u]));let a=this._impl.storageOffset+s*r[t],l=new Oe(this._impl.storage,a,i,o,this._impl.dtype,this._impl.device);return new e(l)}_ensureAutogradMeta(){if(!this._impl.autogradMeta){let t=new Ar;t.versionAtCreation=this._impl.version,this._impl.setAutogradMeta(t)}return this._impl.autogradMeta}backward(t){let{backward:s}=hU();s(this,t)}};c(PB,"_toNestedArray");z0=null;c(hU,"require_autograd_engine");c(DB,"setAutogradEngine")});var Ys,dl=I(()=>{P0();Ys=class e{static{c(this,"Storage")}constructor(t){this._impl=t}static allocate(t,s,n){return new e(pl.allocate(t,s,n))}static fromData(t,s){return new e(pl.fromData(t,s))}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 e(this._impl.clone())}resize(t,s){this._impl.resize(t,s)}}});function FB(e,t,s,n){let r=e.length,i=s<0?r+s:s,o=n<0?r+n:n,a=[...e],l=[...t];return a[i]=e[o],a[o]=e[i],l[i]=t[o],l[o]=t[i],{sizes:a,strides:l}}function $B(e,t,s){let n=e.length,r=new Array(n),i=new Array(n);for(let o=0;o<n;o++){let a=s[o]<0?n+s[o]:s[o];r[o]=e[a],i[o]=t[a]}return{sizes:r,strides:i}}function zB(e,t,s){let n=e.length,r=s.length,i=r-n,o=new Array(r),a=new Array(r);for(let l=0;l<r;l++){let u=l-i;if(u<0)a[l]=s[l],o[l]=0;else if(e[u]===1&&s[l]!==1)a[l]=s[l],o[l]=0;else if(e[u]===s[l]||s[l]===-1)a[l]=e[u],o[l]=t[u];else throw new Error(`Cannot expand size ${e[u]} to ${s[l]} at dim ${l}`)}return{sizes:a,strides:o}}function UB(e,t,s,n,r,i){let o=e.length,a=s<0?o+s:s,l=e[a],u=n??0,f=r??l,h=i??1;u<0&&(u+=l),f<0&&(f+=l),u=Math.max(0,Math.min(u,l)),f=Math.max(0,Math.min(f,l));let p=Math.max(0,Math.ceil((f-u)/h)),d=u*t[a],m=[...e],g=[...t];return m[a]=p,g[a]=t[a]*h,{sizes:m,strides:g,offsetDelta:d}}function VB(e,t,s){let n=e.length,r=s<0?n+1+s:s,i=[...e],o=[...t],a=r<n?e[r]*t[r]:1;return i.splice(r,0,1),o.splice(r,0,a),{sizes:i,strides:o}}function U0(e,t,s){if(s!=null){let i=e.length,o=s<0?i+s:s;if(e[o]!==1)return{sizes:[...e],strides:[...t]};let a=[...e],l=[...t];return a.splice(o,1),l.splice(o,1),{sizes:a,strides:l}}let n=[],r=[];for(let i=0;i<e.length;i++)e[i]!==1&&(n.push(e[i]),r.push(t[i]));return{sizes:n,strides:r}}function GB(e,t,s,n,r){let i=e.length,o=s<0?i+s:s,a=[...e];a[o]=r;let l=n*t[o];return{sizes:a,strides:[...t],offsetDelta:l}}function qB(e,t,s,n){let r=e.length,i=s<0?r+s:s,o=n<0?e[i]+n:n,a=[],l=[];for(let f=0;f<r;f++)f!==i&&(a.push(e[f]),l.push(t[f]));let u=o*t[i];return{sizes:a,strides:l,offsetDelta:u}}var jB=I(()=>{c(FB,"computeTranspose");c($B,"computePermute");c(zB,"computeExpand");c(UB,"computeSlice");c(VB,"computeUnsqueeze");c(U0,"computeSqueeze");c(GB,"computeNarrow");c(qB,"computeSelect")});function WB(e){V0=e.GradMode,ml=e.ReshapeBackward,G0=e.TransposeBackward,q0=e.PermuteBackward,xh=e.SliceBackward,j0=e.ExpandBackward,W0=e.SelectBackward,K0=e.GradAccumulator}function to(e,t,s,...n){if(!V0||!V0.isEnabled())return;let r=e._impl.autogradMeta;if(!r||!r.requiresGrad)return;let i=new s(...n);i.saveInputMetadata(0,[...e.shape],e.dtype);let o=r.gradFn;if(o)i.setNextEdge(0,o,r.outputNr||0);else{let l=r.getGradAccumulator();!l&&K0&&(l=new K0(e),r.setGradAccumulator(l)),l&&i.setNextEdge(0,l,0)}let a=new Ar;a.setGradFn(i,0),a.requiresGrad=!0,t._impl.setAutogradMeta(a),t._impl._updateKeySet()}function eo(e,t,s){return e.tracer.recordOp(t,[e],s)}function pU(e,t){let s=t.reduce((o,a)=>o*a,1),n=1,r=-1,i=e.slice();for(let o=0;o<i.length;o++)i[o]===-1?r=o:n*=i[o];return r>=0&&(i[r]=n===0?0:s/n),i}function bi(e,t,s,n){let r=new Oe(e._impl.storage,e._impl.storageOffset+(n||0),t,s,e._impl.dtype,e._impl.device),i=e._impl.autogradMeta;return i&&r.setAutogradMeta(i),new Et(r)}function Gr(e,t){if(e.isSymbolic&&e.tracer)return eo(e,"reshape",{new_shape:pU(t,e.shape)});let s=OB(e.shape,e.strides,t);if(!s)throw new Error(`Cannot reshape tensor of shape [${e.shape}] to [${t}]`);let n;if(!s.needsCopy)n=bi(e,s.sizes,s.strides,0);else{let r=e.isContiguous?e:JB(e);n=bi(r,s.sizes,s.strides,0)}return ml&&to(e,n,ml),n}function or(e,t,s){if(e.isSymbolic&&e.tracer){let o=e.shape.length,a=t<0?o+t:t,l=s<0?o+s:s;return eo(e,"transpose",{dim0:a,dim1:l})}let{sizes:n,strides:r}=FB(e.shape,e.strides,t,s),i=bi(e,n,r,0);return G0&&to(e,i,G0,t,s),i}function J0(e,t){if(e.isSymbolic&&e.tracer){let i=e.shape.length,o=t.map(a=>a<0?i+a:a);return eo(e,"permute",{dims:o})}let{sizes:s,strides:n}=$B(e.shape,e.strides,t),r=bi(e,s,n,0);return q0&&to(e,r,q0,t),r}function KB(e,t){if(e.isSymbolic&&e.tracer){let i=e.shape.length,o=t.length-i,a=t.map((u,f)=>u===-1?e.shape[f-o]:u),l=Array.from({length:i},(u,f)=>f+o);return eo(e,"broadcast_in_dim",{result_shape:a,broadcast_dimensions:l})}let{sizes:s,strides:n}=zB(e.shape,e.strides,t),r=bi(e,s,n,0);return j0&&to(e,r,j0),r}function Sh(e,t,s,n,r){let i=e.length,o=t<0?i+t:t,a=e[o],l=s??0,u=n??a,f=r??1;l<0&&(l+=a),u<0&&(u+=a),l=Math.max(0,Math.min(l,a)),u=Math.max(0,Math.min(u,a));let h=new Array(i).fill(0),p=e.slice(),d=new Array(i).fill(1);return h[o]=l,p[o]=u,d[o]=f,{starts:h,limits:p,strides:d,d:o}}function dU(e,t,s,n,r){if(e.isSymbolic&&e.tracer){let{starts:u,limits:f,strides:h}=Sh(e.shape,t,s,n,r);return eo(e,"slice",{starts:u,limits:f,strides:h})}let{sizes:i,strides:o,offsetDelta:a}=UB(e.shape,e.strides,t,s,n,r),l=bi(e,i,o,a);if(xh){let u=Sh(e.shape,t,s,n,r);to(e,l,xh,u.d,u.starts[u.d],u.limits[u.d],u.strides[u.d])}return l}function ar(e,t){if(e.isSymbolic&&e.tracer){let i=e.shape.length,o=t<0?i+1+t:t,a=e.shape.slice();return a.splice(o,0,1),eo(e,"reshape",{new_shape:a})}let{sizes:s,strides:n}=VB(e.shape,e.strides,t),r=bi(e,s,n,0);return ml&&to(e,r,ml),r}function Ah(e,t){if(e.isSymbolic&&e.tracer){let{sizes:i}=U0(e.shape,e.strides,t);return eo(e,"reshape",{new_shape:i})}let{sizes:s,strides:n}=U0(e.shape,e.strides,t),r=bi(e,s,n,0);return ml&&to(e,r,ml),r}function vh(e,t,s,n){if(e.isSymbolic&&e.tracer){let{starts:l,limits:u,strides:f}=Sh(e.shape,t,s,s+n,1);return eo(e,"slice",{starts:l,limits:u,strides:f})}let{sizes:r,strides:i,offsetDelta:o}=GB(e.shape,e.strides,t,s,n),a=bi(e,r,i,o);if(xh){let l=Sh(e.shape,t,s,s+n,1);to(e,a,xh,l.d,l.starts[l.d],l.limits[l.d],l.strides[l.d])}return a}function zs(e,t,s){if(e.isSymbolic&&e.tracer){let a=e.shape.length,l=t<0?a+t:t,u=s<0?e.shape[l]+s:s,{starts:f,limits:h,strides:p}=Sh(e.shape,l,u,u+1,1),d=eo(e,"slice",{starts:f,limits:h,strides:p}),m=e.shape.filter((g,y)=>y!==l);return d.tracer.recordOp("reshape",[d],{new_shape:m})}let{sizes:n,strides:r,offsetDelta:i}=qB(e.shape,e.strides,t,s),o=bi(e,n,r,i);if(W0){let a=e.shape.length,l=t<0?a+t:t,u=s<0?e.shape[l]+s:s;to(e,o,W0,l,u)}return o}function HB(e){H0=e}function vr(e){let t=e._impl;return e.isContiguous&&t.storageOffset===0&&t.storage.rawData.length===e.numel?e:JB(e)}function JB(e){if(H0){let g=H0(e);if(g)return g}let t=e.shape,s=e.strides,n=e._impl.storage.data,r=e._impl.storageOffset,i=e.numel,o=As(t),a=e._impl.dtype,l=Ys.allocate(i*cl(a),a,e._impl.device),u=l.data,f=t.length,h=new Int32Array(f),p=r;for(let g=0;g<i;g++){u[g]=n[p];for(let y=f-1;y>=0&&(h[y]++,p+=s[y],!(h[y]<t[y]));y--)p-=h[y]*s[y],h[y]=0}let d=new Oe(l,0,t,o,e._impl.dtype,e._impl.device),m=e._impl.autogradMeta;return m&&d.setAutogradMeta(m),new Et(d)}function YB(e,t){let s=e.shape,n=s.length;if(t.length<n)throw new Error("repeat: reps length must be >= tensor rank");let r=t.length-n,i=r>0?[...Array(r).fill(1),...s]:s.slice(),o=[],a=[],l=[];for(let u=0;u<i.length;u++)o.push(1,i[u]),a.push(t[u],i[u]),l.push(t[u]*i[u]);return Gr(KB(Gr(e,o),a),l)}function mU(e,t){let s=e.shape.length,n=t.length<s?[...Array(s-t.length).fill(1),...t]:t;return YB(e,n)}function gl(e,t,s=0){let n=e.shape.length,r=s<0?n+s:s,i=e.shape[r],o;if(Array.isArray(t))o=t;else{o=[];for(let u=0;u<i;u+=t)o.push(Math.min(t,i-u))}let a=[],l=0;for(let u of o)a.push(vh(e,r,l,u)),l+=u;return a}function gU(e,t,s=0){let n=e.shape.length,r=s<0?n+s:s,i=e.shape[r],o=Math.ceil(i/t);return gl(e,o,r)}function QB(e){let t=e.prototype;t.reshape=function(...s){let n=s.length===1&&Array.isArray(s[0])?s[0]:s;return Gr(this,n)},t.transpose=function(s,n){return or(this,s,n)},t.permute=function(...s){let n=s.length===1&&Array.isArray(s[0])?s[0]:s;return J0(this,n)},t.expand=function(...s){let n=s.length===1&&Array.isArray(s[0])?s[0]:s;return KB(this,n)},t.slice=function(s,n,r,i){return dU(this,s,n,r,i)},t.unsqueeze=function(s){return ar(this,s)},t.squeeze=function(s){return Ah(this,s)},t.narrow=function(s,n,r){return vh(this,s,n,r)},t.select=function(s,n){return zs(this,s,n)},t.repeat=function(...s){let n=s.length===1&&Array.isArray(s[0])?s[0]:s;return YB(this,n)},t.tile=function(...s){let n=s.length===1&&Array.isArray(s[0])?s[0]:s;return mU(this,n)},t.split=function(s,n=0){return gl(this,s,n)},t.chunk=function(s,n=0){return gU(this,s,n)},t.contiguous=function(){return vr(this)},t.t=function(){if(this.ndim!==2)throw new Error("t() expects a 2D tensor");return or(this,0,1)}}var V0,ml,G0,q0,xh,j0,W0,K0,H0,vs=I(()=>{Js();Zi();dl();ir();Vr();jB();Uc();V0=null,ml=null,G0=null,q0=null,xh=null,j0=null,W0=null,K0=null;c(WB,"_initViewAutograd");c(to,"_wrapWithAutograd");c(eo,"_traceView");c(pU,"_resolveShape");c(bi,"_makeView");c(Gr,"reshape");c(or,"transpose");c(J0,"permute");c(KB,"expand");c(Sh,"_sliceAttrs");c(dU,"slice");c(ar,"unsqueeze");c(Ah,"squeeze");c(vh,"narrow");c(zs,"select");H0=null;c(HB,"setGpuContiguousHook");c(vr,"contiguous");c(JB,"_copyContiguous");c(YB,"repeat");c(mU,"tile");c(gl,"split");c(gU,"chunk");c(QB,"installViewOps")});var yU,Ym,Nt,xi=I(()=>{yU=0,Ym=class{static{c(this,"Edge")}constructor(t,s){this.node=t,this.inputNr=s}},Nt=class{static{c(this,"AutogradNode")}constructor(t){this._id=yU++,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,s){this._nextEdges.push(new Ym(t,s))}setNextEdge(t,s,n){for(;this._nextEdges.length<=t;)this._nextEdges.push(null);this._nextEdges[t]=new Ym(s,n)}saveTensor(t){this._savedTensors.push(t)}savedTensors(){return this._savedTensors}saveInputMetadata(t,s,n){this._inputMetadata[t]={shape:s,dtype:n}}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 ZB(e){let t=XB.get(e);return t||(t=Ue.findOp(e),t&&XB.set(e,t)),t}var XB,tC=I(()=>{ja();XB=new Map;c(ZB,"getHandle")});function ks(e,t){let s=t?.dtype??W.F32,n=t?.device??hl(),r=t?.requiresGrad??!1;if(ArrayBuffer.isView(e))return _U(e,t?.shape,s,n,r);if(Array.isArray(e)){let{flat:i,shape:o}=bU(e),a=t?.shape??o;return wU(i,a,s,n,r)}if(typeof e=="number")return eC(e,s,n,r);throw new Error("Unsupported data type for tensor()")}function Fo(e,t,s,n){let r=n?.device??hl(),i=n?.requiresGrad??!1,o=As(t),a=Ys.fromData(e,r),l=n?.offset??0,u=new Oe(a,l,t,o,s,r),f=new Et(u);return i&&f.requiresGrad_(!0),f}function Qm(e,t){let s=t?.dtype??W.F32,n=t?.device??hl();return eC(e,s,n,t?.requiresGrad??!1)}function eC(e,t,s,n){let r=me(t),i=new r(1);i[0]=Y0.has(t)?fm(t,e):e;let o=Ys.fromData(i,s),a=new Oe(o,0,[],[],t,s),l=new Et(a);return n&&l.requiresGrad_(!0),l}function _U(e,t,s,n,r){let i=me(s),o=new i(e.length);if(Y0.has(s))for(let p=0;p<e.length;p++)o[p]=fm(s,e[p]);else for(let p=0;p<e.length;p++)o[p]=e[p];let a=t??[e.length],l=As(a),u=Ys.fromData(o,n),f=new Oe(u,0,a,l,s,n),h=new Et(f);return r&&h.requiresGrad_(!0),h}function wU(e,t,s,n,r){let i=me(s),o=new i(e.length);if(Y0.has(s))for(let h=0;h<e.length;h++)o[h]=fm(s,e[h]);else for(let h=0;h<e.length;h++)o[h]=e[h];let a=As(t),l=Ys.fromData(o,n),u=new Oe(l,0,t,a,s,n),f=new Et(u);return r&&f.requiresGrad_(!0),f}function bU(e){let t=[],s=e;for(;Array.isArray(s);)t.push(s.length),s=s[0];let n=[];return sC(e,n,t,0),{flat:n,shape:t}}function sC(e,t,s,n){if(n===s.length-1){for(let r=0;r<e.length;r++)t.push(e[r]);return}for(let r=0;r<e.length;r++)sC(e[r],t,s,n+1)}var Y0,$o=I(()=>{Js();Zi();dl();ir();vn();Vr();Rc();c(ks,"tensor");c(Fo,"fromBuffer");c(Qm,"scalar");Y0=new Set(["f16","bf16","i64"]);c(eC,"_fromScalar");c(_U,"_fromTypedArray");c(wU,"_fromFlatArray");c(bU,"_flattenNested");c(sC,"_recurFlatten")});var Ye={};Xt(Ye,{abs:()=>zo,add:()=>ot,argmax:()=>Mh,argmin:()=>Lh,batch_norm:()=>Z0,cat:()=>Vc,ceil:()=>Bh,clamp:()=>Ch,clone:()=>Fh,conv2d:()=>tx,cos:()=>Nh,div:()=>oe,dot:()=>Dh,embedding:()=>SU,eq:()=>Si,exp:()=>lr,fill:()=>xU,floor:()=>Rh,gather:()=>Ih,ge:()=>ro,gelu:()=>_l,gt:()=>Wr,index_select:()=>io,layer_norm:()=>X0,le:()=>no,log:()=>jr,log_softmax:()=>Al,lt:()=>Sl,matmul:()=>qe,max:()=>Kr,maximum:()=>qr,mean:()=>fn,min:()=>Oh,minimum:()=>yl,mul:()=>tt,ne:()=>xl,neg:()=>kn,one_hot:()=>Th,pad:()=>Uo,pool2d:()=>Xm,pow:()=>kr,prod:()=>Ph,relu:()=>so,remainder:()=>Q0,rsqrt:()=>kh,scatter_add:()=>Vo,sigmoid:()=>Us,sign:()=>bl,silu:()=>wl,sin:()=>Eh,softmax:()=>Er,sqrt:()=>cr,stack:()=>ur,sub:()=>$t,sum:()=>he,tanh:()=>Fn,transpose_op:()=>$h,where:()=>Xs});function Qs(e,t){return e&&e._impl?e:Qm(e,{dtype:t.dtype,device:t.device})}function gt(e,...t){let s=ZB(e);if(!s)return Ue.callOp(e,...t);let n=db(t,s.schema);return Ue.dispatch(s,n,...t)}function ot(e,t){return gt("add",e,Qs(t,e))}function $t(e,t){return gt("sub",e,Qs(t,e))}function tt(e,t){return gt("mul",e,Qs(t,e))}function oe(e,t){return gt("div",e,Qs(t,e))}function kn(e){return gt("neg",e)}function kr(e,t){return gt("pow",e,Qs(t,e))}function Q0(e,t){return gt("rem",e,Qs(t,e))}function qr(e,t){return gt("maximum",e,Qs(t,e))}function yl(e,t){return gt("minimum",e,Qs(t,e))}function lr(e){return gt("exp",e)}function jr(e){return gt("log",e)}function cr(e){return gt("sqrt",e)}function kh(e){return gt("rsqrt",e)}function zo(e){return gt("abs",e)}function Eh(e){return gt("sin",e)}function Nh(e){return gt("cos",e)}function Fn(e){return gt("tanh",e)}function Us(e){return gt("sigmoid",e)}function so(e){return gt("relu",e)}function _l(e){return gt("gelu",e)}function wl(e){return gt("silu",e)}function bl(e){return gt("sign",e)}function Rh(e){return gt("floor",e)}function Bh(e){return gt("ceil",e)}function Si(e,t){return gt("eq",e,Qs(t,e))}function xl(e,t){return gt("ne",e,Qs(t,e))}function Sl(e,t){return gt("lt",e,Qs(t,e))}function no(e,t){return gt("le",e,Qs(t,e))}function Wr(e,t){return gt("gt",e,Qs(t,e))}function ro(e,t){return gt("ge",e,Qs(t,e))}function Xs(e,t,s){return gt("where",e,t,s)}function Ch(e,t,s){return gt("clamp",e,Qs(t,e),Qs(s,e))}function Uo(e,t,s,n=0){return gt("pad",e,Qs(n,e),t,s)}function Th(e,t){return gt("one_hot",e,t)}function io(e,t,s){return gt("index_select",e,s,t)}function Ih(e,t,s){return gt("gather",e,s,t)}function Vo(e,t,s,n){return gt("scatter_add",e,s,n,t)}function he(e,t,s){return gt("sum",e,t,s)}function fn(e,t,s){return gt("mean",e,t,s)}function Kr(e,t,s){return gt("max",e,t,s)}function Oh(e,t,s){return gt("min",e,t,s)}function Mh(e,t,s){return gt("argmax",e,t,s)}function Lh(e,t,s){return gt("argmin",e,t,s)}function Ph(e,t,s){return gt("prod",e,t,s)}function qe(e,t){let s=ex();if(s){let n=s(e,t);if(n!==null)return n}return gt("matmul",e,t)}function Dh(e,t){return gt("dot",e,t)}function Vc(e,t){return gt("cat",e,t)}function ur(e,t){return gt("stack",e,t)}function Fh(e){return gt("clone",e)}function xU(e,t){return gt("fill",e,t)}function $h(e,t,s){return gt("transpose",e,t,s)}function Er(e,t){return gt("softmax",e,t)}function Al(e,t){return gt("log_softmax",e,t)}function X0(e,t,s,n,r){return gt("layer_norm",e,t,s,n,r)}function Z0(e,t,s,n,r,i,o){return gt("batch_norm",e,t,s,n,r,i,o)}function tx(e,t,s,n,r,i){return gt("conv2d",e,t,s,n,r,i)}function Xm(e,t,s,n,r){return gt("pool2d",e,t,s,n,r)}function SU(e,t){return gt("embedding",e,t)}var te=I(()=>{ja();tC();$o();Nr();c(Qs,"_asTensor");c(gt,"_dispatch");c(ot,"add");c($t,"sub");c(tt,"mul");c(oe,"div");c(kn,"neg");c(kr,"pow");c(Q0,"remainder");c(qr,"maximum");c(yl,"minimum");c(lr,"exp");c(jr,"log");c(cr,"sqrt");c(kh,"rsqrt");c(zo,"abs");c(Eh,"sin");c(Nh,"cos");c(Fn,"tanh");c(Us,"sigmoid");c(so,"relu");c(_l,"gelu");c(wl,"silu");c(bl,"sign");c(Rh,"floor");c(Bh,"ceil");c(Si,"eq");c(xl,"ne");c(Sl,"lt");c(no,"le");c(Wr,"gt");c(ro,"ge");c(Xs,"where");c(Ch,"clamp");c(Uo,"pad");c(Th,"one_hot");c(io,"index_select");c(Ih,"gather");c(Vo,"scatter_add");c(he,"sum");c(fn,"mean");c(Kr,"max");c(Oh,"min");c(Mh,"argmax");c(Lh,"argmin");c(Ph,"prod");c(qe,"matmul");c(Dh,"dot");c(Vc,"cat");c(ur,"stack");c(Fh,"clone");c(xU,"fill");c($h,"transpose_op");c(Er,"softmax");c(Al,"log_softmax");c(X0,"layer_norm");c(Z0,"batch_norm");c(tx,"conv2d");c(Xm,"pool2d");c(SU,"embedding")});var nC={};Xt(nC,{arange:()=>nx,empty:()=>is,eye:()=>rx,full:()=>Kt,linspace:()=>ox,ones:()=>je,randn:()=>Uh,randperm:()=>ix,zeros:()=>Pt});function Zm(e){return{dtype:e?.dtype??W.F32,device:e?.device??hl(),requiresGrad:e?.requiresGrad??!1}}function zh(e,t,s,n){let r=As(e),o=gs(e)*cl(t),a=Ys.allocate(o,t,s),l=new Oe(a,0,e,r,t,s),u=new Et(l);return n&&u.requiresGrad_(!0),u}function is(e,t){let{dtype:s,device:n,requiresGrad:r}=Zm(t);return zh(e,s,n,r)}function sx(e,t,s){let n=is(e,t),r=n.data;if(r)return r.fill(s),n;if(n.device&&n.device.type==="meta"){let i=is(e,{dtype:t?.dtype});return i.data.fill(s),i}return n}function Pt(e,t){return sx(e,t,0)}function je(e,t){return sx(e,t,1)}function Kt(e,t,s){return sx(e,s,t)}function Uh(e,t){let s=is(e,t),n=s.data;if(n){let r=n.length;for(let i=0;i<r;i+=2){let o=Math.random()||1e-10,a=Math.random(),l=Math.sqrt(-2*Math.log(o)),u=6.283185307179586*a;n[i]=l*Math.cos(u),i+1<r&&(n[i+1]=l*Math.sin(u))}}return s}function nx(e,t,s,n){let r,i,o;t===void 0&&s===void 0?(r=0,i=e,o=1):s===void 0?(r=e,i=t,o=1):(r=e,i=t,o=s);let a=Math.max(0,Math.ceil((i-r)/o)),{dtype:l,device:u,requiresGrad:f}=Zm(n),h=zh([a],l,u,f),p=h.data;if(p)for(let d=0;d<a;d++)p[d]=r+d*o;return h}function rx(e,t,s){let n=t??e,{dtype:r,device:i,requiresGrad:o}=Zm(s),a=zh([e,n],r,i,o),l=a.data;if(l){l.fill(0);let u=Math.min(e,n);for(let f=0;f<u;f++)l[f*n+f]=1}return a}function ix(e,t){let s=t?.dtype??W.I32,n=t?.device??hl(),r=zh([e],s,n,t?.requiresGrad??!1),i=r.data;for(let o=0;o<e;o++)i[o]=o;for(let o=e-1;o>0;o--){let a=Math.floor(Math.random()*(o+1)),l=i[o];i[o]=i[a],i[a]=l}return r}function ox(e,t,s,n){let{dtype:r,device:i,requiresGrad:o}=Zm(n),a=zh([s],r,i,o),l=a.data;if(l&&s>0)if(s===1)l[0]=e;else{let u=(t-e)/(s-1);for(let f=0;f<s;f++)l[f]=e+f*u}return a}var Ne=I(()=>{Js();Zi();dl();ir();vn();Vr();c(Zm,"_defaultOpts");c(zh,"_makeTensor");c(is,"empty");c(sx,"_filled");c(Pt,"zeros");c(je,"ones");c(Kt,"full");c(Uh,"randn");c(nx,"arange");c(rx,"eye");c(ix,"randperm");c(ox,"linspace")});function ax(e,t){return e<0?t+e:e}var tg,eg,sg,ng,Gc,rg,lx=I(()=>{xi();te();Ne();vs();c(ax,"_normDim");tg=class extends Nt{static{c(this,"CatBackward")}constructor(){super(0)}apply(t){let s=t[0],n=this.opArgs(),r=s.shape.length,i=ax(n&&n.length>1?n[1]??0:0,r),o=[],a=0,l=0;for(;this.inputMetadata(l);){let f=this.inputMetadata(l).shape[i];o.push(vr(vh(s,i,a,f))),a+=f,l++}return o}},eg=class extends Nt{static{c(this,"StackBackward")}constructor(){super(0)}apply(t){let s=t[0],n=this.opArgs(),r=s.shape.length,i=ax(n&&n.length>1?n[1]??0:0,r),o=[],a=0;for(;this.inputMetadata(a);)o.push(vr(zs(s,i,a))),a++;return o}},sg=class extends Nt{static{c(this,"ClampBackward")}constructor(){super(3)}apply(t){let s=t[0],[n,r,i]=this.savedTensors(),o=Pt(s.shape,{dtype:s.dtype,device:s.device}),a=ro(n.detach(),r.detach()),l=Xs(a,s,o),u=no(n.detach(),i.detach());return[Xs(u,l,o),null,null]}},ng=class extends Nt{static{c(this,"PadBackward")}constructor(){super(2)}apply(t){let s=t[0],r=this.opArgs()[2],o=this.inputMetadata(0).shape,a=s;for(let l=0;l<o.length;l++){let u=r[l]||0;a=vh(a,l,u,o[l])}return[vr(a),null]}},Gc=class e extends Nt{static{c(this,"IndexSelectBackward")}static#t=null;static setGpuBackward(t){e.#t=t}constructor(){super(2)}apply(t){let s=t[0],[,n]=this.savedTensors(),i=this.inputMetadata(0).shape,o=i.length,a=this.opArgs(),l=ax(a&&a.length>2?a[2]??0:0,o);if(e.#t){let E=e.#t(s,n,i,l);if(E)return[E]}let u=Pt(i,{dtype:s.dtype,device:s.device}),f=u._impl.storage.data,h=u.strides,p=vr(s),d=p._impl.storage.data,m=p._impl.storageOffset,g=p.shape,y=p.strides,_=vr(n),w=_._impl.storage.data,b=_._impl.storageOffset,x=g.length,A=new Int32Array(x),S=m;for(let E=0;E<p.numel;E++){let R=0;for(let v=0;v<x;v++){let k=v===l?w[b+A[v]]:A[v];R+=k*h[v]}f[R]+=d[S];for(let v=x-1;v>=0;v--){if(A[v]++,A[v]<g[v]){S+=y[v];break}S-=(g[v]-1)*y[v],A[v]=0}}return[u,null]}},rg=class extends Nt{static{c(this,"WhereBackward")}constructor(){super(3)}apply(t){let s=t[0],[n]=this.savedTensors(),r=Pt(s.shape,{dtype:s.dtype,device:s.device}),i=Xs(n.detach(),s,r),o=Xs(n.detach(),r,s);return[null,i,o]}}});import AU from"koffi";import{readdirSync as rC,existsSync as cx}from"fs";import{join as ig}from"path";function og(e,t,s){$s()?xt.memcpyDtoDAsync(e,t,s,Yt().stream):xt.memcpyDtoD(e,t,s)}function vU(e,t){$s()?xt.memsetD8Async(e,0,t,Yt().stream):xt.memsetD8(e,0,t)}function kU(){let e="C:/Program Files/NVIDIA/CUDNN";if(cx(e))for(let t of rC(e).sort().reverse()){let s=ig(e,t,"bin");if(cx(s))for(let n of rC(s).filter(r=>r.startsWith("12.")).sort().reverse()){let r=ig(s,n,"x64");if(cx(ig(r,"cudnn64_9.dll")))return process.env.PATH.includes(r)||(process.env.PATH=r+";"+process.env.PATH),ig(r,"cudnn64_9.dll")}}return"cudnn64_9.dll"}function aC(){if(Vh!==null)return Vh;try{let e=AU.load(kU());os={create:e.func("int cudnnCreate(_Out_ void **h)"),setStream:e.func("int cudnnSetStream(void *h, void *stream)"),getErrorString:e.func("str cudnnGetErrorString(int status)"),createRNNDesc:e.func("int cudnnCreateRNNDescriptor(_Out_ void **d)"),setRNNDesc:e.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:e.func("int cudnnCreateRNNDataDescriptor(_Out_ void **d)"),setRNNDataDesc:e.func("int cudnnSetRNNDataDescriptor(void *d, int dataType, int layout, int maxSeqLength, int batchSize, int vectorSize, void *seqLengthArray, void *paddingFill)"),createTensorDesc:e.func("int cudnnCreateTensorDescriptor(_Out_ void **d)"),setTensorNdDesc:e.func("int cudnnSetTensorNdDescriptor(void *d, int dataType, int nbDims, void *dimA, void *strideA)"),createDropoutDesc:e.func("int cudnnCreateDropoutDescriptor(_Out_ void **d)"),setDropoutDesc:e.func("int cudnnSetDropoutDescriptor(void *d, void *h, float dropout, uint64 states, size_t stateSize, uint64 seed)"),dropoutGetStatesSize:e.func("int cudnnDropoutGetStatesSize(void *h, _Out_ size_t *sz)"),getRNNWeightSpaceSize:e.func("int cudnnGetRNNWeightSpaceSize(void *h, void *d, _Out_ size_t *sz)"),getRNNTempSpaceSizes:e.func("int cudnnGetRNNTempSpaceSizes(void *h, void *d, int fwdMode, void *xDesc, _Out_ size_t *work, _Out_ size_t *reserve)"),getRNNWeightParams:e.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:e.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:e.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:e.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)")},Vh=!0}catch{Vh=!1}return Vh}function lC(){return aC()}function Es(e,t){if(t!==0)throw new Error("cuDNN "+e+" failed: "+t+" ("+os.getErrorString(t)+")")}function Gh(){if(!aC())throw new Error("cuDNN not available");let e=Yt();if(Mc(),!ag){let t=vl();Es("create",os.create(t)),ag=t[0],Es("setStream",os.setStream(ag,e.stream))}return ag}function LU(){if(!ux){let e=Gh(),t=vl();Es("createDropout",os.createDropoutDesc(t));let s=[0n];Es("dropStatesSize",os.dropoutGetStatesSize(e,s));let n=Ee(Math.max(Number(s[0]),1));Es("setDropout",os.setDropoutDesc(t[0],e,0,n,s[0],0n)),ux=t[0]}return ux}function PU(e,t){let s=vl();return Es("createTensor",os.createTensorDesc(s)),Es("setTensorNd",os.setTensorNdDesc(s[0],lg,e.length,new Int32Array(e),new Int32Array(t))),s[0]}function iC(e,t,s,n){let r=vl();return Es("createRNNData",os.createRNNDataDesc(r)),Es("setRNNData",os.setRNNDataDesc(r[0],lg,IU,e,t,s,n,null)),r[0]}function DU({inputSize:e,hiddenSize:t,seqLen:s,batch:n,numLayers:r}){let i=`${e}_${t}_${s}_${n}_${r}`,o=oC.get(i);if(o)return o;let a=Gh(),l=vl();Es("createRNN",os.createRNNDesc(l)),Es("setRNN",os.setRNNDesc(l[0],EU,NU,RU,BU,CU,lg,lg,TU,e,t,t,r,LU(),0));let u=l[0],f=new Int32Array(n).fill(s),h=iC(s,n,e,f),p=iC(s,n,t,f),d=PU([r,n,t],[n*t,t,1]),m=[0n];Es("wss",os.getRNNWeightSpaceSize(a,u,m));let g=[0n],y=[0n];Es("temp",os.getRNNTempSpaceSizes(a,u,cC,h,g,y));let _=Ee(n*4);return xt.memcpyHtoD(_,f,n*4),o={rd:u,xDesc:h,yDesc:p,hDesc:d,devSeq:_,wss:m[0],wssN:Number(m[0]),workSize:g[0],workN:Math.max(Number(g[0]),1),reserveSize:y[0],reserveN:Math.max(Number(y[0]),1),inputSize:e,hiddenSize:t,seqLen:s,batch:n,numLayers:r},oC.set(i,o),o}function uC(e,t,s,n){let r=Gh(),i=vl(),o=vl();Es("cwm",os.createTensorDesc(i)),Es("cwb",os.createTensorDesc(o));let{numLayers:a,hiddenSize:l,inputSize:u,wss:f}=e;for(let h=0;h<a;h++){let p=h===0?u:l,d=s[h];for(let m=0;m<8;m++){let g=m<4,y=m%4,_=[0n],w=[0n];Es("wparam",os.getRNNWeightParams(r,e.rd,h,f,t,m,i[0],_,o[0],w));let x=(g?l*p:l*l)*4,A=l*4,S=BigInt(g?d.x2hW:d.h2hW)+BigInt(y*x),E=BigInt(g?d.x2hB:d.h2hB)+BigInt(y*A);n?(og(S,_[0],x),og(E,w[0],A)):(og(_[0],S,x),og(w[0],E,A))}}}function fC(e,t,s,n,r,i,o,a,l=!0){let u=Gh(),f=DU({...s,numLayers:t.length}),h=Ee(f.wssN);uC(f,h,t,!1);let p=Ee(f.workN),d=l?Ee(f.reserveN):0n;return Es("forward",os.rnnForward(u,f.rd,l?cC:OU,f.devSeq,f.xDesc,e,f.yDesc,i,f.hDesc,n||0n,o,f.hDesc,r||0n,a,f.wss,h,f.workSize,p,l?f.reserveSize:0n,d)),{p:f,weightSpace:h,workSpace:p,reserveSpace:d,training:l}}function hC(e){e&&(Ge(e.weightSpace,e.p.wssN),Ge(e.workSpace,e.p.workN),e.reserveSpace&&e.reserveSpace!==0n&&Ge(e.reserveSpace,e.p.reserveN))}function pC(e,t,s,n,r,i,o,a,l,u,f,h){let{p,weightSpace:d,workSpace:m,reserveSpace:g}=e,y=Gh(),_=Ee(p.wssN);vU(_,p.wssN),Es("backwardData",os.rnnBackwardData(y,p.rd,p.devSeq,p.yDesc,s,i,p.xDesc,l,p.hDesc,n||0n,o||0n,u||0n,p.hDesc,r||0n,a||0n,f||0n,p.wss,d,p.workSize,m,p.reserveSize,g)),Es("backwardWeights",os.rnnBackwardWeights(y,p.rd,MU,p.devSeq,p.xDesc,t,p.hDesc,n||0n,p.yDesc,s,p.wss,_,p.workSize,m,p.reserveSize,g)),uC(p,_,h,!0),Ge(d,p.wssN),Ge(m,p.workN),Ge(g,p.reserveN),Ge(_,p.wssN)}var os,Vh,lg,EU,NU,RU,BU,CU,TU,IU,cC,OU,MU,vl,ag,ux,oC,fx=I(()=>{mi();gi();g0();al();Xi();c(og,"d2d");c(vU,"memZero");c(kU,"resolveCudnn");Vh=null;c(aC,"ensure");c(lC,"cudnnAvailable");lg=0,EU=0,NU=2,RU=2,BU=0,CU=0,TU=2,IU=0,cC=1,OU=0,MU=0;c(Es,"ck");vl=c(()=>[null],"o2"),ag=null,ux=null;c(Gh,"handle");c(LU,"dropoutDesc");c(PU,"tensorDesc");c(iC,"dataDesc");oC=new Map;c(DU,"plan");c(uC,"packWeights");c(fC,"cudnnLSTMForward");c(hC,"releaseLSTMForward");c(pC,"cudnnLSTMBackward")});var cg={};Xt(cg,{GradMode:()=>Rr,enableGrad:()=>px,noGrad:()=>hx});function hx(e){let t=Go;Go=!1;try{return e()}finally{Go=t}}function px(e){let t=Go;Go=!0;try{return e()}finally{Go=t}}var Go,Rr,Ai=I(()=>{Go=!0,Rr={isEnabled(){return Go},setEnabled(e){Go=e}};c(hx,"noGrad");c(px,"enableGrad")});var oo,ug=I(()=>{xi();te();oo=class extends Nt{static{c(this,"GradAccumulator")}constructor(t){super(0),this._variable=typeof WeakRef<"u"?new WeakRef(t):{deref:c(()=>t,"deref")}}apply(t){let s=t[0],n=this._variable.deref();if(!n)return[];let r=n._impl.autogradMeta;return r?(r.grad===null?r.grad=s:r.grad=ot(r.grad,s),[]):[]}name(){return"GradAccumulator"}}});function $n(e){return Ss()?wi(e):ll(e)}function ki(e,t){if(Ss())return Do(e);let s=Ee(e.byteLength);return t.push([e,s]),s}function dC(e){for(let[t,s]of e)_i(t,s),Ge(s,t.byteLength)}function dx(e,t,s){let n=new Ar;n.setGradFn(e,s),n.requiresGrad=!0,t._impl.setAutogradMeta(n),t._impl._updateKeySet()}function mC(e,t,s,n=null,r=null){let{inputSize:i,hiddenSize:o,seqLen:a,batch:l}=s,u=t.length,f=[],h=vi(e),p=$n(h),d=t.map(z=>({x2hW:$n(vi(z.x2h.weight)),x2hB:$n(vi(z.x2h.bias)),h2hW:$n(vi(z.h2h.weight)),h2hB:$n(vi(z.h2h.bias))})),m=n?vi(n):null,g=r?vi(r):null,y=n?$n(m):0n,_=r?$n(g):0n,w=u*l*o,b=[u,l,o],x=new Float32Array(a*l*o),A=new Float32Array(w),S=new Float32Array(w),E=ki(x,f),R=ki(A,f),v=ki(S,f),k=[e];for(let z of t)k.push(z.x2h.weight,z.x2h.bias,z.h2h.weight,z.h2h.bias);n&&k.push(n,r);let B=!1;for(let z of k)if(z._impl.autogradMeta&&z.requiresGrad){B=!0;break}let N=Rr.isEnabled()&&B,C=fC(p,d,s,y,_,E,R,v,N);dC(f);let O=ae(x,[a,l,o],e.dtype,e.device),F=ae(A,b,e.dtype,e.device),P=ae(S,b,e.dtype,e.device);if(N){let z={xArr:h,yArr:x,hxArr:m,cxArr:g,opts:s,numLayers:u,hasInit:!!n,dtype:e.dtype,device:e.device,inputShape:[...e.shape],stateShape:b,weightShapes:t.map(U=>({x2hW:[...U.x2h.weight.shape],x2hB:[...U.x2h.bias.shape],h2hW:[...U.h2h.weight.shape],h2hB:[...U.h2h.bias.shape]}))},q=new mx(C,z,k.length);for(let U=0;U<k.length;U++){q.saveInputMetadata(U,[...k[U].shape],k[U].dtype);let D=k[U]._impl.autogradMeta;if(D&&D.requiresGrad)if(D.gradFn)q.setNextEdge(U,D.gradFn,D.outputNr||0);else{let G=D.getGradAccumulator();G||(G=new oo(k[U]),D.setGradAccumulator(G)),q.setNextEdge(U,G,0)}else q.setNextEdge(U,null,0)}dx(q,O,0),dx(q,F,1),dx(q,P,2)}else hC(C);return[O,F,P]}var vi,fg,mx,gC=I(()=>{xi();Uc();Ai();ug();Nr();bh();al();fx();vi=c(e=>gx(e),"carr"),fg=c(e=>e.reduce((t,s)=>t*s,1),"prod");c($n,"devIn");c(ki,"devOut");c(dC,"flushOut");mx=class extends Nt{static{c(this,"CudnnLSTMBackward")}constructor(t,s,n){super(n),this.fwd=t,this.info=s}apply(t){let[s,n,r]=t,i=this.info,{seqLen:o,batch:a,inputSize:l,hiddenSize:u}=i.opts,f=i.numLayers,h=f*a*u,p=[],d=$n(i.xArr),m=$n(i.yArr),g=i.hasInit?$n(i.hxArr):0n,y=i.hasInit?$n(i.cxArr):0n,_=$n(vi(s)),w=n?$n(vi(n)):0n,b=r?$n(vi(r)):0n,x=new Float32Array(o*a*l),A=ki(x,p),S=null,E=null,R=0n,v=0n;i.hasInit&&(S=new Float32Array(h),E=new Float32Array(h),R=ki(S,p),v=ki(E,p));let k=i.weightShapes.map(O=>({x2hW:new Float32Array(fg(O.x2hW)),x2hB:new Float32Array(fg(O.x2hB)),h2hW:new Float32Array(fg(O.h2hW)),h2hB:new Float32Array(fg(O.h2hB))})),B=k.map(O=>({x2hW:ki(O.x2hW,p),x2hB:ki(O.x2hB,p),h2hW:ki(O.h2hW,p),h2hB:ki(O.h2hB,p)}));pC(this.fwd,d,m,g,y,_,w,b,A,R,v,B),dC(p);let N=c((O,F)=>ae(O,F,i.dtype,i.device),"w"),C=[N(x,i.inputShape)];for(let O=0;O<k.length;O++){let F=k[O],P=i.weightShapes[O];C.push(N(F.x2hW,P.x2hW),N(F.x2hB,P.x2hB),N(F.h2hW,P.h2hW),N(F.h2hB,P.h2hB))}return i.hasInit&&C.push(N(S,i.stateShape),N(E,i.stateShape)),C}};c(dx,"attach");c(mC,"cudnnLSTMOp")});function yC(e){return Ss()?wi(e):ll(e)}function FU(e,t){if(Ss())return Do(e);let s=Ee(e.byteLength);return t.push([e,s]),s}function $U(e){for(let[t,s]of e)_i(t,s),Ge(s,t.byteLength)}function _C(e){let t=wC(e);if(t)return{data:e._impl.storage.rawData,lay:t};let s=_x();if(Ss()&&s){let r=s(e._impl.storage.rawData,e.shape,e.strides,e._impl.storageOffset,e.dtype),i=e.shape,o=i.length,a=i[o-2],l=i[o-1],u=1;for(let f=0;f<o-2;f++)u*=i[f];return{data:r,lay:{trans:!1,batchStride:a*l,batch:u,rows:a,cols:l}}}let n=vr(e);return{data:n._impl.storage.rawData,lay:wC(n)}}function wC(e){if(e.storageOffset!==0)return null;let t=e.shape,s=e.strides,n=t.length;if(n<2)return null;let r=t[n-2],i=t[n-1],o=s[n-2],a=s[n-1],l;if(o===i&&a===1)l=!1;else if(o===1&&a===r)l=!0;else return null;let u=r*i;for(let h=n-3;h>=0;h--){if(s[h]!==u)return null;u*=t[h]}let f=1;for(let h=0;h<n-2;h++)f*=t[h];return{trans:l,batchStride:r*i,batch:f,rows:r,cols:i}}function zU(e,t){let s=new Ar;s.setGradFn(e,0),s.requiresGrad=!0,t._impl.setAutogradMeta(s),t._impl._updateKeySet()}function bC(e,t){if(!e.device||e.device.type!==Ie.GPU||t.device.type!==Ie.GPU||e.dtype!=="f32"||t.dtype!=="f32")return null;let s=e.shape.length,n=t.shape.length;if(s<2||n<2||s!==n)return null;let r=e.shape[s-2],i=e.shape[s-1],o=t.shape[n-1];if(t.shape[n-2]!==i)return null;let a=_C(e),l=_C(t),u=a.lay,f=l.lay;if(!u||!f||u.batch!==f.batch)return null;let h=u.batch,p=[],d=yC(a.data),m=yC(l.data),g=new Float32Array(h*r*o),y=FU(g,p);h>1?E0(h,r,o,i,d,u.batchStride,u.trans,m,f.batchStride,f.trans,y,r*o):k0(r,o,i,d,u.trans,m,f.trans,y),$U(p);let _=[...e.shape.slice(0,s-2),r,o],w=ae(g,_,e.dtype,e.device);if(Rr.isEnabled()&&(e._impl.autogradMeta&&e.requiresGrad||t._impl.autogradMeta&&t.requiresGrad)){let b=new yx(e,t),x=[e,t];for(let A=0;A<2;A++){b.saveInputMetadata(A,[...x[A].shape],x[A].dtype);let S=x[A]._impl.autogradMeta;if(S&&S.requiresGrad)if(S.gradFn)b.setNextEdge(A,S.gradFn,S.outputNr||0);else{let E=S.getGradAccumulator();E||(E=new oo(x[A]),S.setGradAccumulator(E)),b.setNextEdge(A,E,0)}else b.setNextEdge(A,null,0)}zU(b,w)}return w}var yx,xC=I(()=>{xi();Uc();Ai();ug();Nr();vs();vn();bh();al();Gm();te();c(yC,"devIn");c(FU,"devOut");c($U,"flushOut");c(_C,"operandData");c(wC,"operandLayout");yx=class extends Nt{static{c(this,"MatmulBackward")}constructor(t,s){super(2),this.A=t,this.B=s}apply(t){let s=t[0],n=this.A,r=this.B,i=n.shape.length,o=r.shape.length,a=qe(s,r.transpose(o-2,o-1)),l=qe(n.transpose(i-2,i-1),s);return[a,l]}};c(zU,"attach");c(bC,"gpuMatmul")});var pg={};Xt(pg,{adamGraphKernelSource:()=>TC,deviceAdam:()=>IC,deviceClipGradNorm:()=>XU,deviceConcat:()=>NC,deviceContiguous:()=>bx,freeOptimizerDeviceState:()=>RC,isCudaGraphEnabled:()=>wB,measureCudaKernel:()=>AC,preloadCublas:()=>vC,releaseCudaMemory:()=>ZU,runCudaKernel:()=>VU,runCudaKernelResident:()=>wx,runCudaKernelSync:()=>kC,runCudaPlan:()=>AB,setCudaGraphEnabled:()=>_B,stepIncKernelSource:()=>CC,teardownAfterFit:()=>t2});function AC(e,t,s=[],n={}){Yt();let{func:r}=yi(e.source,e.name),i=e.metadata,o=n.warmup??5,a=n.repeat??30,l=n.minWarmupMs??25,u=n.maxWarmup??1e5,f=i.gridDim,h=i.blockDim,p=i.sharedMemBytes||0,d=t.map(g=>Math.max(g,1)),m=d.map(g=>Ee(g));try{let g=performance.now(),y=0;for(;(y<o||performance.now()-g<l)&&(rr(r,f,h,p,m,s),!(++y>=u)););let _=[];for(let w=0;w<a;w++){let b=performance.now();rr(r,f,h,p,m,s),_.push(performance.now()-b)}return _}finally{for(let g=0;g<m.length;g++)Ge(m[g],d[g])}}async function vC(){return qh||(qh=await Promise.resolve().then(()=>(Gm(),mB))),qh}function kC(e,t,s){let n=e.metadata;if(n.cublas){if(!qh)throw new Error("cuBLAS module not preloaded; call preloadCublas() before sync execution");let{M:f,N:h,K:p,aIdx:d,bIdx:m,cIdx:g,transB:y}=n.cublas;qh.cublasMatmul(f,h,p,t[d],t[m],t[g],y);return}Yt();let{func:r}=r0(e),i=[],o=[];for(let f of t)ArrayBuffer.isView(f)?i.push(f):o.push(f);if(s)for(let f of s)o.push(f);let a=n.outputIndices||i.map((f,h)=>h),l=[];for(let f of i){let h=Ee(f.byteLength);Po(h,f),l.push(h)}rr(r,n.gridDim,n.blockDim,n.sharedMemBytes||0,l,o);let u=new Set(a);for(let f=0;f<i.length;f++)u.has(f)&&_i(i[f],l[f]);for(let f=0;f<l.length;f++)Ge(l[f],i[f].byteLength)}function wx(e,t,s){let n=e.metadata;if(n.cublas)throw new Error("cuBLAS kernels are not supported on the eager device-resident path");Yt();let{func:r}=r0(e),i=[],o=[];for(let f of t)ArrayBuffer.isView(f)?i.push(f):o.push(f);if(s)for(let f of s)o.push(f);let a=n._outputSet||(n._outputSet=new Set(n.outputIndices||i.map((f,h)=>h))),l=new Array(i.length),u=UU(n.scratch);if(Ss()){for(let f=0;f<i.length;f++)l[f]=a.has(f)?Do(i[f]):wi(i[f]);rr(r,n.gridDim,n.blockDim,n.sharedMemBytes||0,[...l,...u.ptrs],o,!1),SC(u);return}for(let f=0;f<i.length;f++)l[f]=ll(i[f]);rr(r,n.gridDim,n.blockDim,n.sharedMemBytes||0,[...l,...u.ptrs],o,!1);for(let f=0;f<i.length;f++)a.has(f)&&B0(i[f],l[f]);SC(u)}function UU(e){if(!e||e.length===0)return{ptrs:[],bufs:[]};let t=[],s=[];for(let n of e){let r=n.size*me(n.dtype).BYTES_PER_ELEMENT,i=Ee(r);t.push(i),s.push([i,r])}return{ptrs:t,bufs:s}}function SC(e){for(let[t,s]of e.bufs)Ge(t,s)}async function VU(e,t,s){e.metadata.cublas&&await vC(),kC(e,t,s)}function hg(e){return GU[e]||"float"}function EC(e,t){return{gridDim:[Math.max(Math.ceil(e/256),1),1,1],blockDim:[256,1,1],sharedMemBytes:0,outputIndices:t}}function qU(e,t){return`extern "C" __global__ void ${t}(const ${e}* in, ${e}* out, int n, int rank,
90
- int s0,int s1,int s2,int s3,int s4,int s5,int s6,int s7,
91
- int t0,int t1,int t2,int t3,int t4,int t5,int t6,int t7, int off) {
92
- int i = blockIdx.x*blockDim.x + threadIdx.x; if (i >= n) return;
93
- int shp[8] = {s0,s1,s2,s3,s4,s5,s6,s7};
94
- int strd[8] = {t0,t1,t2,t3,t4,t5,t6,t7};
95
- long long src = off; int rem = i;
96
- for (int d = rank-1; d >= 0; d--) { int x = rem % shp[d]; rem /= shp[d]; src += (long long)x*strd[d]; }
97
- out[i] = in[src];
98
- }`}function bx(e,t,s,n,r){let i=hg(r),o=`gather_${r}`,a=Ei.get(o);a||(a=qU(i,o),Ei.set(o,a));let l=t.length,u=1;for(let d=0;d<l;d++)u*=t[d];let f=new(me(r))(Math.max(u,1)),h=new Array(8).fill(1),p=new Array(8).fill(0);for(let d=0;d<l;d++)h[d]=t[d],p[d]=s[d];return wx({source:a,name:o,metadata:EC(u,[1])},[e,f,u,l,...h,...p,n|0],null),f}function jU(e,t){return`extern "C" __global__ void ${t}(const ${e}* in, ${e}* out, int pre, int dk, int tail, int total, int offset) {
99
- int i = blockIdx.x*blockDim.x + threadIdx.x; int n = pre*dk*tail; if (i >= n) return;
100
- int t = i % tail; int r = i / tail; int j = r % dk; int p = r / dk;
101
- out[(long long)p*total*tail + (long long)(offset+j)*tail + t] = in[i];
102
- }`}function NC(e,t,s,n,r,i,o){let a=hg(o),l=`catcopy_${o}`,u=Ei.get(l);u||(u=jU(a,l),Ei.set(l,u));let f=e==="stack",h=r.length,p=n<0?h+n:n,d=1;for(let _=0;_<p;_++)d*=r[_];let m=1;for(let _=p+1;_<h;_++)m*=r[_];let g=r[p],y=0;for(let _=0;_<t.length;_++){let w=f?1:s[_][p],b=d*w*m;wx({source:u,name:l,metadata:EC(b,[1])},[t[_],i,d,w,m,g,y],null),y+=w}}function WU(e,t){return`extern "C" __global__ void ${t}(${e}* w, const ${e}* g, ${e}* m, ${e}* v, int n, float b1, float b2, float ob1, float ob2, float eps, float ss, float bc2s, float wd) {
103
- int i = blockIdx.x*blockDim.x + threadIdx.x; if (i >= n) return;
104
- float gi = (float)g[i] + wd * (float)w[i];
105
- float mi = b1*(float)m[i] + ob1*gi;
106
- float vi = b2*(float)v[i] + ob2*gi*gi;
107
- m[i] = (${e})mi; v[i] = (${e})vi;
108
- w[i] = (${e})((float)w[i] - ss * mi / (sqrtf(vi)/bc2s + eps));
109
- }`}function KU(e){return`extern "C" __global__ void ${e}(int* t) {
110
- if (threadIdx.x == 0 && blockIdx.x == 0) t[0] = t[0] + 1;
111
- }`}function HU(e,t){return`extern "C" __global__ void ${t}(${e}* w, const ${e}* g, ${e}* m, ${e}* v, const int* t, int n, float b1, float b2, float ob1, float ob2, float eps, float lr, float wd) {
112
- int i = blockIdx.x*blockDim.x + threadIdx.x; if (i >= n) return;
113
- int step = t[0];
114
- double bc1 = 1.0 - pow((double)b1, (double)step);
115
- double bc2 = 1.0 - pow((double)b2, (double)step);
116
- float ss = (float)((double)lr / bc1);
117
- float bc2s = (float)sqrt(bc2);
118
- float gi = (float)g[i] + wd * (float)w[i];
119
- float mi = b1*(float)m[i] + ob1*gi;
120
- float vi = b2*(float)v[i] + ob2*gi*gi;
121
- m[i] = (${e})mi; v[i] = (${e})vi;
122
- w[i] = (${e})((float)w[i] - ss * mi / (sqrtf(vi)/bc2s + eps));
123
- }`}function JU(e){return`extern "C" __global__ void ${e}(const float* g, double* acc, int n) {
124
- extern __shared__ double sdata[];
125
- int tid = threadIdx.x;
126
- int i = blockIdx.x*blockDim.x + threadIdx.x;
127
- double v = (i < n) ? (double)g[i]*(double)g[i] : 0.0;
128
- sdata[tid] = v;
129
- __syncthreads();
130
- for (int s = blockDim.x/2; s > 0; s >>= 1) { if (tid < s) sdata[tid] += sdata[tid+s]; __syncthreads(); }
131
- if (tid == 0) atomicAdd(acc, sdata[0]);
132
- }`}function YU(e){return`extern "C" __global__ void ${e}(const double* acc, const float* mp, float* coef) {
133
- if (threadIdx.x == 0 && blockIdx.x == 0) {
134
- double norm = sqrt(acc[0]);
135
- double c = (double)mp[0] / (norm + (double)mp[1]);
136
- coef[0] = c < 1.0 ? (float)c : 1.0f;
137
- }
138
- }`}function QU(e){return`extern "C" __global__ void ${e}(float* g, const float* coef, int n) {
139
- int i = blockIdx.x*blockDim.x + threadIdx.x; if (i < n) g[i] = g[i] * coef[0];
140
- }`}function XU(e,t,s=1e-6){Yt(),ao===null&&(ao=Ee(8),jh=Ee(4),Wh=Ee(8));let n=Yt().stream;$s()||Po(Wh,new Float32Array([t,s])),$s()?xt.memsetD8Async(ao,0,8,n):xt.memsetD8(ao,0,8);let r=yi(JU("clip_accum"),"clip_accum").func,i=yi(YU("clip_coef"),"clip_coef").func,o=yi(QU("clip_scale"),"clip_scale").func,a=[];for(let l of e){if(!l.grad)continue;let u=l.grad._impl.storage.rawData;if(!u)continue;let f=u.length,h=qm(u);a.push([h,f]),rr(r,[Math.ceil(f/256),1,1],[256,1,1],256*8,[h,ao],[f],!1)}if(a.length!==0){rr(i,[1,1,1],[1,1,1],0,[ao,Wh,jh],[],!1);for(let[l,u]of a)rr(o,[Math.ceil(u/256),1,1],[256,1,1],0,[l,jh],[u],!1)}}function RC(e){let t=e&&e._state;if(!(!t||typeof t.values!="function"))for(let s of t.values())s&&(s._mDev!==void 0&&(Qi(s._mDev),delete s._mDev),s._vDev!==void 0&&(Qi(s._vDev),delete s._vDev),s._tDev!==void 0&&(Qi(s._tDev),delete s._tDev))}function BC(){ao!==null&&(Qi(ao),Qi(jh),Qi(Wh),ao=null,jh=null,Wh=null)}function ZU(){return O0(),BC(),Cm()}function t2(e,t){let s=e&&e.__eagerGraphRunner;if(s){try{b0(s.captured)}catch{}e._cudaGraphPhase=s.phase,delete e.__eagerGraphRunner}if(t)for(let n of t)RC(n);return Oc(!1),jm(),BC(),Cm()}function CC(){let e="step_inc",t=Ei.get(e);return t||(t=KU(e),Ei.set(e,t)),{source:t,name:e}}function TC(e="f32"){let t=`adam_graph_${e}`,s=Ei.get(t);return s||(s=HU(hg(e),t),Ei.set(t,s)),{source:s,name:t}}function e2(e,t){Po(e,new Int32Array([t|0]))}function IC(e,t,s){if(!Ss())return!1;let n=e.dtype||"f32";if(n!=="f32")return!1;let r=e._impl.storage.rawData,i=e.grad._impl.storage.rawData;if(!r||!i)return!1;let o=r.length;Yt();let a=QR();t._mDev||(t._mDev=Ee(o*4),xt.memsetD8(t._mDev,0,o*4),t._vDev=Ee(o*4),xt.memsetD8(t._vDev,0,o*4),a&&(t._tDev=Ee(4),xt.memsetD8(t._tDev,0,4)),C0(r));let l=qm(r),u=wi(i);if($s()){if(t._tDev===void 0)throw new Error("CUDA graph Adam requires a device step counter; warmup must run before capture");let d=CC(),m=TC(n),g=yi(d.source,d.name).func,y=yi(m.source,m.name).func;return rr(g,[1,1,1],[1,1,1],0,[t._tDev],[],!1),rr(y,[Math.ceil(o/256),1,1],[256,1,1],0,[l,u,t._mDev,t._vDev,t._tDev],[o,un(s.beta1),un(s.beta2),un(s.omb1),un(s.omb2),un(s.eps),un(s.lr),un(s.wd)],!1),!0}let f=`adam_${n}`,h=Ei.get(f);h||(h=WU(hg(n),f),Ei.set(f,h));let{func:p}=yi(h,f);return rr(p,[Math.ceil(o/256),1,1],[256,1,1],0,[l,u,t._mDev,t._vDev],[o,un(s.beta1),un(s.beta2),un(s.omb1),un(s.omb2),un(s.eps),un(s.stepSize),un(s.bc2sqrt),un(s.wd)],!1),a&&t._tDev!==void 0&&e2(t._tDev,t.step),!0}function s2(e){if(!Ss()||!e.device||e.device.type!==Ie.GPU)return null;let t=bx(e._impl.storage.rawData,e.shape,e.strides,e._impl.storageOffset,e.dtype);return ae(t,[...e.shape],e.dtype,e.device)}function r2(e,t,s,n){if(!Ss()||n!==0||s.length!==2||!e.device||e.device.type!==Ie.GPU||t.dtype!=="i32")return null;let r=s[0],i=s[1],o=vr(e),a=vr(t),l=a.numel;if(o.numel!==l*i)return null;Yt();let u=new Float32Array(r*i),{func:f}=yi(n2,"index_add0"),h=Do(u),p=Yt().stream;$s()?xt.memsetD8Async(h,0,r*i*4,p):xt.memsetD8(h,0,r*i*4);let d=wi(o._impl.storage.rawData),m=wi(a._impl.storage.rawData);return rr(f,[Math.ceil(l*i/256),1,1],[256,1,1],0,[d,m,h],[l,i],!1),ae(u,[r,i],e.dtype,e.device)}var qh,GU,Ei,ao,jh,Wh,n2,dg=I(()=>{gi();i0();al();f0();vB();bh();Xi();Lm();mi();P0();Em();Nr();vs();lx();vn();ir();fx();gC();xC();pl.setHostReadHook(M0);c(AC,"measureCudaKernel");qh=null;c(vC,"preloadCublas");c(kC,"runCudaKernelSync");c(wx,"runCudaKernelResident");c(UU,"_acquireScratch");c(SC,"_releaseScratch");c(VU,"runCudaKernel");GU={f32:"float",f64:"double",i64:"long long",i32:"int",i16:"short",i8:"signed char",u8:"unsigned char",bool:"unsigned char"};c(hg,"_ctype");Ei=new Map;c(EC,"_meta");c(qU,"_gatherKernel");c(bx,"deviceContiguous");c(jU,"_catKernel");c(NC,"deviceConcat");c(WU,"_adamKernel");c(KU,"_stepIncKernel");c(HU,"_adamGraphKernel");c(JU,"_clipAccumKernel");c(YU,"_clipCoefKernel");c(QU,"_clipScaleKernel");ao=null,jh=null,Wh=null;c(XU,"deviceClipGradNorm");c(RC,"freeOptimizerDeviceState");c(BC,"freeClipScratch");c(ZU,"releaseCudaMemory");c(t2,"teardownAfterFit");c(CC,"stepIncKernelSource");c(TC,"adamGraphKernelSource");c(e2,"_setDeviceInt");c(IC,"deviceAdam");c(s2,"gpuContiguousTensor");HB(s2);n2=`extern "C" __global__ void index_add0(const float* g, const int* idx, float* out, int K, int E) {
141
- int t = blockIdx.x*blockDim.x + threadIdx.x; if (t >= K*E) return;
142
- int i = t / E, j = t % E;
143
- atomicAdd(&out[idx[i]*E + j], g[i*E + j]);
144
- }`;c(r2,"gpuIndexSelectBackward");Gc.setGpuBackward(r2);xx(bx);Sx(NC);vx(IC);kx(bC);lC()&&Ax(mC);km("cuda",AC)});var jC={};Xt(jC,{ensureWebGPUEager:()=>f2,flushWebGPUEager:()=>Qh,instantiateWebGPU:()=>a2,prewarmPipelines:()=>GC,recordWebGPUEager:()=>bg,resetDevice:()=>i2,runWebGPUKernel:()=>l2,runWebGPUPlan:()=>c2,webgpuEagerOp:()=>d2,webgpuEagerReady:()=>h2,webgpuRNN:()=>y2});function qc(e){switch(Ks(e)){case"i32":return Int32Array;case"u32":return Uint32Array;case"f16":return Uint16Array;default:return Float32Array}}function _g(e,t,s,n){if(s==="bf16")for(let r=0;r<t.length;r++)e[n+r]=cm(t[r]);else if(s==="i64")for(let r=0;r<t.length;r++)e[n+r]=Number(BigInt.asIntN(32,t[r]));else e.set(t,n)}function Cx(e,t,s,n,r){if(s==="bf16")for(let i=0;i<r;i++)e[i]=um(t[n+i]);else if(s==="i64")for(let i=0;i<r;i++)e[i]=BigInt(t[n+i]);else e.set(t.subarray(n,n+r))}function Ni(e){return Math.ceil(e/4)*4}async function Tx(){return Re||Hh||(Hh=(async()=>{let e=typeof navigator<"u"&&navigator.gpu;if(!e)try{let o=await import("webgpu");FC=o,e=o.create([]),o.globals&&(Jh=o.globals.GPUBufferUsage,gg=o.globals.GPUMapMode,yg=o.globals.GPUShaderStage)}catch{throw new Error('WebGPU not available: install the "webgpu" npm package or run in a browser with WebGPU support')}!Jh&&typeof GPUBufferUsage<"u"&&(Jh=GPUBufferUsage,gg=GPUMapMode,yg=GPUShaderStage);let t=await e.requestAdapter();if(!t)throw new Error("WebGPU: no adapter found");let s=t.limits||{},n={},r=["maxStorageBuffersPerShaderStage","maxStorageBufferBindingSize","maxBufferSize","maxBindingsPerBindGroup"];for(let o of r)s[o]!==void 0&&(n[o]=s[o]);let i=[];return t.features&&t.features.has("shader-f16")&&i.push("shader-f16"),Re=await t.requestDevice({requiredLimits:n,requiredFeatures:i}),!OC&&typeof process<"u"&&process.on&&(OC=!0,process.on("exit",()=>{Re&&(Re.destroy(),Re=null)})),Re})(),Hh)}function jc(){return Jh}function Ix(){return gg.READ}function i2(){Re&&(Re.destroy(),Re=null),Hh=null,Jh=null,gg=null,yg=null,FC=null}function o2(e){return e.name==="_shapes"?"uniform":e.mode==="read_write"?"storage":"read-only-storage"}function $C(e,t){let s=e.createShaderModule({code:t.source}),n=[];for(let o of t.metadata.bindings)n.push({binding:o.index,visibility:yg.COMPUTE,buffer:{type:o2(o)}});let r=e.createBindGroupLayout({entries:n}),i=e.createPipelineLayout({bindGroupLayouts:[r]});return{shaderModule:s,bindGroupLayout:r,pipelineLayout:i}}function zC(e,t){let{shaderModule:s,bindGroupLayout:n,pipelineLayout:r}=$C(e,t);return{pipeline:e.createComputePipeline({layout:r,compute:{module:s,entryPoint:t.name}}),bindGroupLayout:n}}function UC(e,t){let s=wg.get(t);return s||(s=zC(e,t),wg.set(t,s)),s}async function a2(e){let t=await Tx(),{pipeline:s,bindGroupLayout:n}=zC(t,e);return{device:t,pipeline:s,bindGroupLayout:n,kernel:e,workgroupSize:e.metadata.workgroupSize,dispatchSize:e.metadata.dispatchSize,bindings:e.metadata.bindings}}function VC(e){let t=new Map,s=0;for(let n of e)if(n.name!=="_shapes")if(n.packed)for(let r of n.packed)t.set(r.name,r.argIndex!==void 0?r.argIndex:s++);else t.set(n.name,n.argIndex!==void 0?n.argIndex:s++);return t}async function l2(e,t,s){let{device:n,pipeline:r,bindGroupLayout:i,bindings:o,dispatchSize:a}=e,l=jc(),u=[],f=[],h=VC(o);for(let y=0;y<o.length;y++){let _=o[y];if(_.name==="_shapes"){let k=new Uint32Array(s||[]),B=Math.max(Math.ceil(k.byteLength/16)*16,16),N=n.createBuffer({size:B,usage:l.UNIFORM|l.COPY_DST});n.queue.writeBuffer(N,0,k),u.push(N),f.push({binding:_.index,resource:{buffer:N}});continue}let w=Fr(_.dtype),b=qc(_.dtype);if(_.packed){let k=Ni(_.packedSize*w),N=_.mode==="read_write"?l.STORAGE|l.COPY_SRC|l.COPY_DST:l.STORAGE|l.COPY_DST,C=n.createBuffer({size:Math.max(k,4),usage:N,mappedAtCreation:!0}),O=new b(C.getMappedRange());for(let F of _.packed){let P=h.get(F.name),z=t[P];z&&_g(O,z,F.dtype,F.offset)}C.unmap(),u.push(C),f.push({binding:_.index,resource:{buffer:C}});continue}let x=h.get(_.name),A=t[x],S=Ni(A.length*w),R=_.mode==="read_write"?l.STORAGE|l.COPY_SRC|l.COPY_DST:l.STORAGE|l.COPY_DST,v=n.createBuffer({size:Math.max(S,4),usage:R,mappedAtCreation:!0});_g(new b(v.getMappedRange()),A,_.dtype,0),v.unmap(),u.push(v),f.push({binding:_.index,resource:{buffer:v}})}let p=n.createBindGroup({layout:i,entries:f}),d=n.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 y=0;y<o.length;y++){let _=o[y];if(_.name==="_shapes"||_.mode!=="read_write")continue;let w=u[y],b=Fr(_.dtype),x=qc(_.dtype);if(_.packed)for(let A of _.packed){let S=A.offset*b,E=Ni(A.size*b),R=n.createBuffer({size:E,usage:l.MAP_READ|l.COPY_DST});d.copyBufferToBuffer(w,S,R,0,E),g.push({readBuf:R,tensorIdx:h.get(A.name),size:A.size,dtype:A.dtype,ViewCtor:x})}else{let A=w.size,S=n.createBuffer({size:A,usage:l.MAP_READ|l.COPY_DST});d.copyBufferToBuffer(w,0,S,0,A),g.push({readBuf:S,tensorIdx:h.get(_.name),size:t[h.get(_.name)].length,dtype:_.dtype,ViewCtor:x})}}n.queue.submit([d.finish()]);for(let y of g){await y.readBuf.mapAsync(Ix());let _=new y.ViewCtor(y.readBuf.getMappedRange());Cx(t[y.tensorIdx],_,y.dtype,0,y.size),y.readBuf.unmap(),y.readBuf.destroy()}for(let y of u)y.destroy()}async function GC(e,t){if(typeof e.createComputePipelineAsync!="function")return;let s=[],n=new Set;for(let r of t){if(!r||n.has(r)||wg.has(r))continue;n.add(r);let{shaderModule:i,bindGroupLayout:o,pipelineLayout:a}=$C(e,r);s.push(e.createComputePipelineAsync({layout:a,compute:{module:i,entryPoint:r.name}}).then(l=>wg.set(r,{pipeline:l,bindGroupLayout:o}),()=>{}))}s.length>0&&await Promise.all(s)}async function c2(e,t,s){let n=await Tx(),r=jc();await GC(n,s.map(w=>w.kernel));let i=new Set;for(let w of s)for(let b of w.outputSlots)i.add(b);let o=new Array(e.numSlots).fill(null),a=new Array(e.numSlots).fill("f32");for(let w=0;w<e.numSlots;w++){let b=t[w];if(!b)continue;let x=b.dtype||"f32";a[w]=x;let A=Math.max(Ni(b.data.length*Fr(x)),4),S=!i.has(w),E=n.createBuffer({size:A,usage:r.STORAGE|r.COPY_DST|r.COPY_SRC,mappedAtCreation:S});S&&(_g(new(qc(x))(E.getMappedRange()),b.data,x,0),E.unmap()),o[w]=E}let l=32,u=[],f=[],h={encoder:n.createCommandEncoder(),pending:0},p=c(()=>{++h.pending>=l&&(n.queue.submit([h.encoder.finish()]),h.encoder=n.createCommandEncoder(),h.pending=0)},"maybeFlush"),d=c(w=>{let b=h.encoder,{pipeline:x,bindGroupLayout:A}=UC(n,w.kernel),S=w.inputSlots.concat(w.outputSlots),E=[],R=[],v=0;for(let C of w.kernel.metadata.bindings)if(C.name==="_shapes"){let O=new Uint32Array(w.shapeValues||[]),F=Math.max(Math.ceil(O.byteLength/16)*16,16),P=n.createBuffer({size:F,usage:r.UNIFORM|r.COPY_DST});n.queue.writeBuffer(P,0,O),u.push(P),E.push({binding:C.index,resource:{buffer:P}})}else if(C.packed){let O=Fr(C.dtype),F=n.createBuffer({size:Math.max(Ni(C.packedSize*O),4),usage:r.STORAGE|r.COPY_DST|r.COPY_SRC});f.push(F);let P=C.mode==="read_write";for(let z of C.packed){let q=S[z.argIndex],U=Ni(z.size*O);P?R.push({slot:q,src:F,srcOff:z.offset*O,bytes:U}):b.copyBufferToBuffer(o[q],0,F,z.offset*O,U)}E.push({binding:C.index,resource:{buffer:F}})}else E.push({binding:C.index,resource:{buffer:o[S[v++]]}});let k=n.createBindGroup({layout:A,entries:E}),B=b.beginComputePass();B.setPipeline(x),B.setBindGroup(0,k);let N=w.kernel.metadata.dispatchSize;B.dispatchWorkgroups(N[0],N[1],N[2]),B.end();for(let C of R)b.copyBufferToBuffer(C.src,C.srcOff,o[C.slot],0,C.bytes);p()},"encodeStep"),m=e.scanLoops?[...e.scanLoops].sort((w,b)=>w.loopStart-b.loopStart):e.scanLoop?[e.scanLoop]:null;if(m&&m.length){let w=0;for(let b of m){for(;w<b.loopStart;w++)d(s[w]);for(let x of b.carry)h.encoder.copyBufferToBuffer(o[x.initSlot],0,o[x.a],0,x.bytes);for(let x=0;x<b.T;x++){for(let A of b.xs)h.encoder.copyBufferToBuffer(o[A.xsSlot],x*A.stepBytes,o[A.xtSlot],0,A.stepBytes);for(let A=b.loopStart;A<b.loopEnd;A++)d(s[A]);for(let A of b.ys)h.encoder.copyBufferToBuffer(o[A.ytSlot],0,o[A.ysSlot],x*A.stepBytes,A.stepBytes);for(let A of b.carry){let S=o[A.a];o[A.a]=o[A.b],o[A.b]=S}}for(let x of b.carry)h.encoder.copyBufferToBuffer(o[x.a],0,o[x.finalSlot],0,x.bytes);w=b.loopEnd}for(;w<s.length;w++)d(s[w])}else for(let w of s)d(w);let g=h.encoder,y=new Set(e.argSlots),_=[];for(let w=0;w<e.numSlots;w++){if(!o[w]||!i.has(w)||!y.has(w))continue;let b=t[w],x=Math.max(Ni(b.data.length*Fr(a[w])),4),A=n.createBuffer({size:x,usage:r.MAP_READ|r.COPY_DST});g.copyBufferToBuffer(o[w],0,A,0,x),_.push({rb:A,dtype:a[w],size:b.data.length,dst:b.data})}n.queue.submit([g.finish()]);for(let w of _)await w.rb.mapAsync(Ix()),Cx(w.dst,new(qc(w.dtype))(w.rb.getMappedRange()),w.dtype,0,w.size),w.rb.unmap(),w.rb.destroy();for(let w of o)w&&w.destroy();for(let w of u)w.destroy();for(let w of f)w.destroy()}function qC(e){let t=Bx.get(e);if(t&&t.length)return t.pop();let s=jc();return Re.createBuffer({size:e,usage:s.STORAGE|s.COPY_SRC|s.COPY_DST})}function MC(e,t){let s=Bx.get(t);s||(s=[],Bx.set(t,s)),s.push(e)}async function f2(){return await Tx(),Hr||(Hr=Re.createCommandEncoder()),Re}function h2(){return!!Re&&!!Hr}function LC(e,t,s){let n=Yh.get(e);if(n)return n.buf;let r=jc(),i=Math.max(Ni(e.length*Fr(t)),4);if(s){let a=Re.createBuffer({size:i,usage:r.STORAGE|r.COPY_SRC|r.COPY_DST,mappedAtCreation:!0});return _g(new(qc(t))(a.getMappedRange()),e,t,0),a.unmap(),Yh.set(e,{buf:a,bytes:i,dtype:t,persistent:s}),a}let o=qC(i);return Yh.set(e,{buf:o,bytes:i,dtype:t,persistent:!1}),o}function bg(e,t,s){let{pipeline:n,bindGroupLayout:r}=UC(Re,e),{bindings:i,dispatchSize:o}=e.metadata,a=VC(i),l=jc(),u=[],f=[];for(let d of i){if(d.name==="_shapes"){let y=new Uint32Array(s||[]),_=Math.max(Math.ceil(y.byteLength/16)*16,16),w=Re.createBuffer({size:_,usage:l.UNIFORM|l.COPY_DST});Re.queue.writeBuffer(w,0,y),Nx.push(w),u.push({binding:d.index,resource:{buffer:w}});continue}if(d.packed){let y=Fr(d.dtype),_=Math.max(Ni(d.packedSize*y),4),w=qC(_);Rx.push({buf:w,bytes:_});let b=d.mode==="read_write";for(let x of d.packed){let A=LC(t[a.get(x.name)],x.dtype,!b),S=Ni(x.size*y);b?f.push({pbuf:w,off:x.offset*y,sub:A,bytes:S}):Hr.copyBufferToBuffer(A,0,w,x.offset*y,S)}u.push({binding:d.index,resource:{buffer:w}});continue}let m=t[a.get(d.name)],g=LC(m,d.dtype,d.mode!=="read_write");u.push({binding:d.index,resource:{buffer:g}})}let h=Re.createBindGroup({layout:r,entries:u}),p=Hr.beginComputePass();p.setPipeline(n),p.setBindGroup(0,h),p.dispatchWorkgroups(o[0],o[1],o[2]),p.end();for(let d of f)Hr.copyBufferToBuffer(d.pbuf,d.off,d.sub,0,d.bytes);++Ex>=u2&&(Re.queue.submit([Hr.finish()]),Hr=Re.createCommandEncoder(),Ex=0)}async function Qh(){if(!Re)return;let e=jc(),t=[];for(let[s,n]of Yh){if(n.persistent)continue;let r=Re.createBuffer({size:n.bytes,usage:e.MAP_READ|e.COPY_DST});Hr.copyBufferToBuffer(n.buf,0,r,0,n.bytes),t.push({rb:r,ha:s,buf:n.buf,bytes:n.bytes,dtype:n.dtype})}Re.queue.submit([Hr.finish()]),Hr=Re.createCommandEncoder(),Ex=0;for(let s of Nx)s.destroy();Nx.length=0,await Promise.all(t.map(s=>s.rb.mapAsync(Ix())));for(let s of t)Cx(s.ha,new(qc(s.dtype))(s.rb.getMappedRange()),s.dtype,0,s.ha.length),s.rb.unmap(),s.rb.destroy();for(let s of Rx)MC(s.buf,s.bytes);Rx.length=0;for(let s of t)MC(s.buf,s.bytes),Yh.delete(s.ha)}function p2(e,t,s,n,r){let i=`${e.join(",")}|${t.join(",")}|${s}|${n}`,o=PC.get(i);if(o)return o;let a=Ks(n),l="";for(let f=e.length-1;f>=0;f--)l+=` let idx${f} = rem % ${e[f]}u; rem = rem / ${e[f]}u; src = src + idx${f} * ${t[f]}u;
145
- `;return o={name:"contig",source:`@group(0) @binding(0) var<storage, read> inp : array<${a}>;
146
- @group(0) @binding(1) var<storage, read_write> outp : array<${a}>;
147
- @compute @workgroup_size(64)
148
- fn contig(@builtin(global_invocation_id) gid : vec3<u32>) {
149
- let i = gid.x;
150
- if (i >= ${r}u) { return; }
151
- var rem = i;
152
- var src = ${s}u;
153
- `+l+` outp[i] = inp[src];
154
- }`,metadata:{bindings:[{index:0,name:"inp",dtype:n,mode:"read"},{index:1,name:"outp",dtype:n,mode:"read_write"}],dispatchSize:[Math.ceil(r/64),1,1]}},PC.set(i,o),o}function mg(e){let t=e._impl.storage.rawData;if(e.isContiguous&&e._impl.storageOffset===0&&t.length===e.numel)return t;let s=new t.constructor(e.numel);return bg(p2(e.shape,e.strides,e._impl.storageOffset,e.dtype,e.numel),[t,s],void 0),s}function d2(e,t,s){let n=t.map(mg);n.push(s),bg(e,n,void 0)}function m2(e,t,s){let n=`lstm|${e}|${t}|${s}`,r=DC.get(n);if(r)return r;let i=4*t,o=i*e,a=i*e+i*t,l=a+i;return r={name:"rnn",source:`fn sig(x: f32) -> f32 { return 1.0 / (1.0 + exp(-x)); }
155
- @group(0) @binding(0) var<storage, read> xin : array<f32>;
156
- @group(0) @binding(1) var<storage, read> w : array<f32>;
157
- @group(0) @binding(2) var<storage, read> h0 : array<f32>;
158
- @group(0) @binding(3) var<storage, read> c0 : array<f32>;
159
- @group(0) @binding(4) var<storage, read_write> ys : array<f32>;
160
- @group(0) @binding(5) var<storage, read_write> hn : array<f32>;
161
- @group(0) @binding(6) var<storage, read_write> cn : array<f32>;
162
- var<workgroup> wh : array<f32, ${t}>;
163
- var<workgroup> wc : array<f32, ${t}>;
164
- var<workgroup> wg : array<f32, ${i}>;
165
- @compute @workgroup_size(${Kh})
166
- fn rnn(@builtin(local_invocation_id) lid : vec3<u32>) {
167
- let tid = lid.x;
168
- for (var p = tid; p < ${t}u; p = p + ${Kh}u) { wh[p] = h0[p]; wc[p] = c0[p]; }
169
- workgroupBarrier();
170
- for (var t = 0u; t < ${s}u; t = t + 1u) {
171
- for (var j = tid; j < ${i}u; j = j + ${Kh}u) {
172
- var acc = w[${a}u + j] + w[${l}u + j];
173
- let xb = t * ${e}u;
174
- let wxb = j * ${e}u;
175
- for (var e = 0u; e < ${e}u; e = e + 1u) { acc = acc + xin[xb + e] * w[wxb + e]; }
176
- let whb = ${o}u + j * ${t}u;
177
- for (var q = 0u; q < ${t}u; q = q + 1u) { acc = acc + wh[q] * w[whb + q]; }
178
- wg[j] = acc;
179
- }
180
- workgroupBarrier();
181
- for (var j = tid; j < ${t}u; j = j + ${Kh}u) {
182
- let ii = sig(wg[j]);
183
- let ff = sig(wg[${t}u + j]);
184
- let gg = tanh(wg[${2*t}u + j]);
185
- let oo = sig(wg[${3*t}u + j]);
186
- let cv = ff * wc[j] + ii * gg;
187
- wc[j] = cv;
188
- let nh = oo * tanh(cv);
189
- wh[j] = nh;
190
- ys[t * ${t}u + j] = nh;
191
- }
192
- workgroupBarrier();
193
- }
194
- for (var p = tid; p < ${t}u; p = p + ${Kh}u) { hn[p] = wh[p]; cn[p] = wc[p]; }
195
- }`,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]}},DC.set(n,r),r}function g2(e,t,s){let n=`${t}|${s}`;if(e._webgpuPacked&&e._webgpuPackedTag===n)return e._webgpuPacked;let r=4*s,i=c(h=>h.contiguous()._impl.storage.rawData,"raw"),o=i(e.x2h.weight),a=i(e.h2h.weight),l=e.x2h.bias?i(e.x2h.bias):new Float32Array(r),u=e.h2h.bias?i(e.h2h.bias):new Float32Array(r),f=new Float32Array(r*t+r*s+r+r);return f.set(o,0),f.set(a,r*t),f.set(l,r*t+r*s),f.set(u,r*t+r*s+r),e._webgpuPacked=f,e._webgpuPackedTag=n,f}function y2(e,t,s,n,r){if(s.batch!==1)return null;let i=s.hiddenSize,o=s.seqLen,a=t.length,l=mg(e),u=[],f=[];for(let d=0;d<a;d++){let m=d===0?s.inputSize:i,g=g2(t[d],m,i),y,_;n!=null&&r!=null?(y=mg(zs(n,0,d)),_=mg(zs(r,0,d))):(y=new Float32Array(i),_=new Float32Array(i));let w=new Float32Array(o*i),b=new Float32Array(i),x=new Float32Array(i);bg(m2(m,i,o),[l,g,y,_,w,b,x],void 0),l=w,u.push(b),f.push(x)}let h=ae(l,[o,1,i],"f32",Sr),p=c(d=>a===1?ae(d[0],[1,1,i],"f32",Sr):ur(d.map(m=>ae(m,[1,i],"f32",Sr)),0),"wrapState");return[h,p(u),p(f)]}var Re,Hh,Jh,gg,yg,FC,OC,wg,Hr,Ex,u2,Yh,Nx,Rx,Bx,PC,Kh,DC,xg=I(()=>{bn();Rc();Nr();vn();te();vs();Re=null,Hh=null,Jh=null,gg=null,yg=null,FC=null,OC=!1;c(qc,"wgslViewCtor");c(_g,"packTensorInto");c(Cx,"unpackTensorFrom");c(Ni,"align4");c(Tx,"ensureDevice");c(jc,"bufUsage");c(Ix,"mapModeRead");c(i2,"resetDevice");c(o2,"bindingBufferType");c($C,"pipelineParts");c(zC,"createPipeline");wg=new WeakMap;c(UC,"pipelineFor");c(a2,"instantiateWebGPU");c(VC,"buildParamIndex");c(l2,"runWebGPUKernel");c(GC,"prewarmPipelines");c(c2,"runWebGPUPlan");Hr=null,Ex=0,u2=64,Yh=new Map,Nx=[],Rx=[],Bx=new Map;c(qC,"acquireStorage");c(MC,"releaseStorage");c(f2,"ensureWebGPUEager");c(h2,"webgpuEagerReady");c(LC,"eagerBufferFor");c(bg,"recordWebGPUEager");c(Qh,"flushWebGPUEager");PC=new Map;c(p2,"contigKernel");c(mg,"webgpuEagerInput");c(d2,"webgpuEagerOp");Kh=256,DC=new Map;c(m2,"lstmKernelWGSL");c(g2,"lstmPackedWeights");c(y2,"webgpuRNN")});var zx={};Xt(zx,{getBackend:()=>Fx,hasBackend:()=>_2,preloadCudaRuntime:()=>$x,preloadWebGPU:()=>Wc,registerBackend:()=>Zh});function Zh(e,t){Dx.set(e,t)}function Fx(e){return Dx.get(e)||null}function _2(e){return Dx.has(e)}function w2(){return Ox||(Ox=Promise.resolve().then(()=>(PR(),LR))),Ox}function Px(){return Mx||(Mx=Promise.resolve().then(()=>(dg(),pg))),Mx}async function $x(){if(Xh)return Xh;let e=await Px();return e.preloadCublas&&await e.preloadCublas(),Xh=e,e}async function Wc(){if(Lx)return Lx;let e=await Promise.resolve().then(()=>(xg(),jC)),{setWebGPUEagerFn:t,setWebgpuRNN:s}=await Promise.resolve().then(()=>(Nr(),KC));return await e.ensureWebGPUEager(),t(e.webgpuEagerOp),s(e.webgpuRNN),Lx=e,e}function WC(e){let t;try{t=Am(e.source)}catch(i){throw new Error("encodeWat: "+i.message+`
196
- `+e.source)}let s;try{s=new WebAssembly.Module(t)}catch(i){throw new Error("WASM: "+i.message+`
197
- `+e.source)}let n={};if(e.metadata.imports)for(let[i]of e.metadata.imports)n[i]=b2[i]||Math[i]||(o=>o);let r=new WebAssembly.Instance(s,{math:n});return{exports:r.exports,memory:r.exports.memory,bufferOffsets:e.metadata.bufferOffsets,funcName:e.name,binary:t,parallel:e.metadata.parallel||null,mathNames:e.metadata.imports?[...e.metadata.imports.keys()]:[]}}function Sg(e,t,s,n,r){let{exports:i,memory:o,bufferOffsets:a,funcName:l}=e,u=i[l],f=[...a.values()],h=Math.min(f.length,t.length);for(let d=0;d<h;d++){let m=t[d];ArrayBuffer.isView(m)&&new m.constructor(o.buffer,f[d],m.length).set(m)}let p=f.slice(0,h);if(s)for(let d of s)p.push(d);n!==void 0&&r!==void 0&&p.push(n,r),u(...p);for(let d=0;d<h;d++){let m=t[d];ArrayBuffer.isView(m)&&m.set(new m.constructor(o.buffer,f[d],m.length))}}function x2(e,t,s=[],n={}){let r=n.warmup??5,i=n.repeat??30,o=WC(e),a=t.map(h=>new Float32Array(Math.max(1,Math.ceil(h/4)))),l=s||[],u=c(()=>{o.parallel?Sg(o,a,l,0,o.parallel.extent):Sg(o,a,l)},"once");for(let h=0;h<r;h++)u();let f=[];for(let h=0;h<i;h++){let p=performance.now();u(),f.push(performance.now()-p)}return f}var Dx,Ox,Mx,Xh,Lx,b2,tp=I(()=>{Xb();Em();Dx=new Map;c(Zh,"registerBackend");c(Fx,"getBackend");c(_2,"hasBackend");Ox=null;c(w2,"getWasmPool");Mx=null;c(Px,"getCudaRuntime");Xh=null;c($x,"preloadCudaRuntime");Lx=null;c(Wc,"preloadWebGPU");b2={exp:Math.exp,log:Math.log,sin:Math.sin,cos:Math.cos,tan:Math.tan,tanh:Math.tanh,pow:Math.pow,fmod:c((e,t)=>e%t,"fmod"),rsqrt:c(e=>1/Math.sqrt(e),"rsqrt"),sign:Math.sign,round:Math.round};c(WC,"instantiateWasm");c(Sg,"runWasmKernel");Zh("js",{instantiate(e){return new Function("return "+e.source)()},runSync(e,t,s){let n=s?[...t,...s]:t;return e(...n)},runAsync(e,t,s){let n=s?[...t,...s]:t;return e(...n)},isAsync(){return!1}});Zh("wasm",{instantiate(e){return WC(e)},runSync(e,t,s){e.parallel?Sg(e,t,s,0,e.parallel.extent):Sg(e,t,s)},async runAsync(e,t,s){if(e.parallel&&e.parallel.poolSafe){let{runWasmParallel:n}=await w2();await n(e,e.funcName,t,s,e.parallel,e.mathNames);return}this.runSync(e,t,s)},isAsync(e){return!!(e&&e.parallel&&e.parallel.poolSafe)}});c(x2,"measureWasm");km("wasm",x2);Zh("webgpu",{instantiate(e){return Wc().then(t=>t.instantiateWebGPU(e))},runSync(){throw new Error("WebGPU kernel requires async execution \u2014 use runAsync()")},async runAsync(e,t,s){let{runWebGPUKernel:n}=await Wc();await n(e,t,s)},async runPlan(e,t,s){let{runWebGPUPlan:n}=await Wc();await n(e,t,s)},isAsync(){return!0}});Zh("cuda",{instantiate(e){return{kernel:e}},runSync(e,t,s){if(!Xh)throw new Error("CUDA sync runtime not preloaded \u2014 call preloadCudaRuntime() before synchronous execution");Xh.runCudaKernelResident(e.kernel,t,s)},async runAsync(e,t,s){let{runCudaKernel:n}=await Px();await n(e.kernel,t,s)},async runPlan(e,t,s){let{runCudaPlan:n}=await Px();await n(e,t,s)},isAsync(){return!0}})});function Ux(e){return S2[e]||Float32Array}function A2(e){return e instanceof Float64Array?"f64":e instanceof Int32Array?"i32":e instanceof Int16Array?"i16":e instanceof Int8Array?"i8":e instanceof Uint16Array?"ui16":e instanceof Uint8Array?"ui8":e instanceof BigInt64Array?"i64":"f32"}function HC(e,t){if(!(!e.returnFixups||e.returnFixups.length===0))for(let s of e.returnFixups){let n=t[e.argSlots[s.pos]];if(!(!n||!n.data))if(s.kind==="copy"){let r=t[s.srcSlot];r&&r.data&&n.data.set(r.data.subarray(0,n.data.length))}else s.kind==="const"&&n.data.fill(s.value)}}var S2,Ri,Vx,Kc,Ag=I(()=>{tp();hh();S2={f16:Uint16Array,bf16:Uint16Array,f32:Float32Array,f64:Float64Array,i8:Int8Array,i16:Int16Array,i32:Int32Array,i64:BigInt64Array,ui8:Uint8Array,bool:Uint8Array,index:Int32Array};c(Ux,"typedArrayCtor");c(A2,"dtypeOfTypedArray");c(HC,"_applyReturnFixups");Ri=class e{static{c(this,"RuntimeTensor")}constructor(t,s,n,r=null){this.data=t,this.shape=s,this.dtype=n,this.strides=r||e.defaultStrides(s)}static defaultStrides(t){let s=new Array(t.length),n=1;for(let r=t.length-1;r>=0;r--)s[r]=n,n*=t[r];return s}get numel(){let t=1;for(let s=0;s<this.shape.length;s++)t*=this.shape[s];return t}get rank(){return this.shape.length}static zeros(t,s="f32"){let n=1;for(let r=0;r<t.length;r++)n*=t[r];return n=Math.max(n,1),new e(new(Ux(s))(n),t,s)}static fromArray(t,s,n="f32"){let r=Ux(n);return new e(new r(t),s,n)}get(t){let s=0;for(let n=0;n<t.length;n++)s+=t[n]*this.strides[n];return this.data[s]}set(t,s){let n=0;for(let r=0;r<t.length;r++)n+=t[r]*this.strides[r];this.data[n]=s}},Vx=class{static{c(this,"KernelRegistry")}constructor(){this._kernels=new Map}register(t,s){this._kernels.set(t,s)}get(t){return this._kernels.get(t)||null}has(t){return this._kernels.has(t)}names(){return[...this._kernels.keys()]}},Kc=class e{static{c(this,"RuntimeModule")}constructor(t){this.name=t,this.kernels=new Vx,this._instances=new Map}addCompiledKernel(t){this.kernels.register(t.name,t);let s=Fx(t.metadata.kind);if(!s)throw new Error("No runtime backend registered for kind: "+t.metadata.kind);this._instances.set(t.name,{backend:s,instance:s.instantiate(t)})}setShapeParamMap(t,s,n){this._shapeParamMaps||(this._shapeParamMaps=new Map),this._shapeParamMaps.set(t,s),n&&(this._bufferMaps||(this._bufferMaps=new Map),this._bufferMaps.set(t,n))}_prepareArgs(t,s){let n=[],r=new Map;for(let a=0;a<s.length;a++)s[a]instanceof Ri?(n.push(s[a].data),r.set(a,s[a].shape)):n.push(s[a]);let i=this._shapeParamMaps&&this._shapeParamMaps.get(t),o=null;if(i&&i.size>0){let a=this._bufferMaps&&this._bufferMaps.get(t);o=e._extractShapeParams(i,r,s,a)}return{tensorArgs:n,shapeValues:o}}run(t,...s){let n=this._instances.get(t);if(!n)throw new Error("Kernel '"+t+"' not found or not executable");if(n.instance instanceof Promise)throw new Error("Kernel '"+t+"' requires async execution \u2014 use runAsync()");let{tensorArgs:r,shapeValues:i}=this._prepareArgs(t,s);return n.backend.runSync(n.instance,r,i)}async runAsync(t,...s){let n=this._instances.get(t);if(!n)throw new Error("Kernel '"+t+"' not found or not executable");let{tensorArgs:r,shapeValues:i}=this._prepareArgs(t,s),o=await n.instance;return n.backend.runAsync(o,r,i)}isAsync(t){let s=this._instances.get(t);if(!s)return!1;let n=s.instance instanceof Promise?null:s.instance;return s.backend.isAsync(n)}async runPlanAsync(t,s){let n=new Array(t.numSlots).fill(null);for(let i=0;i<s.length;i++){let o=s[i];n[t.argSlots[i]]=o instanceof Ri?o:new Ri(o,[o.length],A2(o))}for(let i of t.intermediates){let o=1;for(let a of i.shape)o*=a;n[i.slot]=new Ri(new(Ux(i.dtype))(Math.max(o,1)),i.shape,i.dtype)}for(let i of t.steps){let o=this._instances.get(i.name);o&&o.instance instanceof Promise&&(o.instance=await o.instance)}let r=this._uniformPlanBackend(t);if(r&&r.runPlan){let i=t.steps.map(o=>{let a=[];for(let u of o.inputSlots)a.push(n[u]);for(let u of o.outputSlots)a.push(n[u]);let{shapeValues:l}=this._prepareArgs(o.name,a);return{name:o.name,inputSlots:o.inputSlots,outputSlots:o.outputSlots,kernel:this.kernels.get(o.name),shapeValues:l}});await r.runPlan(t,n,i),HC(t,n);return}for(let i of t.steps){let o=[];for(let a of i.inputSlots)o.push(n[a]);for(let a of i.outputSlots)o.push(n[a]);await this.runAsync(i.name,...o)}HC(t,n)}_uniformPlanBackend(t){let s=null;for(let n of t.steps){let r=this._instances.get(n.name);if(!r||r.instance instanceof Promise)return null;if(s===null)s=r.backend;else if(r.backend!==s)return null}return s}static _extractShapeParams(t,s,n,r){let i=new Map;if(r){let l=0;for(let[u,f]of r){let h=typeof u=="string"?u:f&&f.name;h!==void 0&&i.set(h,l),l++}}let o=new Map,a=[];for(let[l,u]of t){if(o.has(u.name))continue;o.set(u.name,!0);let f=l.lastIndexOf(":"),h=l.substring(0,f),p=parseInt(l.substring(f+1),10),d=null;if(i.has(h)){let m=s.get(i.get(h));m&&p<m.length&&m[p]>0&&(d=m[p])}if(d===null){for(let[,m]of s)if(p<m.length&&m[p]>0){d=m[p];break}}a.push(d!==null?d:1)}return a}getKernelSource(t){let s=this.kernels.get(t);return s?s.source:null}getKernelSnippet(t){let s=this.kernels.get(t);return s?s.snippet():null}listKernels(){return this.kernels.names()}serialize(){let t=[];for(let s of this.kernels.names()){let n=this.kernels.get(s);t.push({name:n.name,source:n.source,target:n.target.name,metadata:n.metadata})}return{name:this.name,kernels:t}}static deserialize(t){let s=new e(t.name);for(let n of t.kernels)s.addCompiledKernel(new fh(n.name,n.source,{name:n.target},n.metadata));return s}}});var vg,JC=I(()=>{vg=class{static{c(this,"AnalysisManager")}constructor(){this._cache=new WeakMap}getAnalysis(t,s){let n=this._cache.get(s);n||(n=new Map,this._cache.set(s,n));let r=n.get(t);if(!r||r.version!==s.version){let i=this._resolveDeps(t,s,n);r={data:t.compute(s,i),version:s.version},n.set(t,r)}return r.data}_resolveDeps(t,s,n){let r=t.dependencies;if(!r||r.length===0)return{};let i={};for(let o of r){let a=o.depKey||o.name;i[a]=this.getAnalysis(o,s)}return i}invalidate(t,s=null){let n=this._cache.get(t);if(!n)return;if(!s){this._cache.delete(t);return}let r=[];for(let i of n.keys())if(!s.has(i)&&!s.has(i.name))r.push(i);else{let o=n.get(i);o&&(o.version=t.version)}for(let i of r)n.delete(i),this._cascadeInvalidate(n,i)}_cascadeInvalidate(t,s){for(let[n]of t){let r=n.dependencies;r&&r.includes(s)&&(t.delete(n),this._cascadeInvalidate(t,n))}}invalidateFunctions(t,s=null){for(let n of t)this.invalidate(n,s)}invalidateAll(){this._cache=new WeakMap}}});function ep(e){if(typeof e.numOps=="function")return e.numOps();if(typeof e[Symbol.iterator]=="function"){let t=0;for(let s of e)typeof s.numOps=="function"&&(t+=s.numOps());return t}return-1}var Hc,kl,Gx=I(()=>{JC();hs();ps();ps();c(ep,"countOps");Hc=class{static{c(this,"FixedPointGroup")}constructor(t,s,n=8){this.name=t,this.passes=s,this.maxIterations=n}},kl=class{static{c(this,"PassManager")}constructor(){this.passes=[],this.analysisManager=new vg,this.trace=null,this.verifyHook=null,this.instruments=[]}addPass(t){this.passes.push(t)}addInstrument(t){this.instruments.push(t)}_notify(t,s,n,r){for(let i of this.instruments)typeof i[t]=="function"&&i[t](s,n,r)}setTrace(t){this.trace=t}setVerifyHook(t){this.verifyHook=t}_verifyAfter(t,s,n){if(!this.verifyHook)return null;let r=this.verifyHook(s,n);if(!r||r.length===0)return null;let i=n?s.name||"<module>":s.name;return new ws("verification",i,`pass '${t.name}' produced invalid IR: ${r.join("; ")}`,t.name)}_applyPass(t,s,n,r){this.trace&&(t.trace=this.trace),this.instruments.length&&this._notify("runBeforePass",t,s,null);let i=n.verbose,o=n.resilient,a=!1,l=!1;if(t instanceof Rd){let u=i?ep(s):-1,f=i?performance.now():0,h;try{h=t.run(s,this.analysisManager)}catch(p){if(!o)throw p;return this.analysisManager.invalidateAll(),r.push(et.FAILED),n.errors.push(new ws("graphPasses",s.name||"<module>",p.message,t.name)),{changed:a,fatal:!1}}if(r.push(h),i&&this.trace.passRun(t.name,h,performance.now()-f,u,ep(s)),h===et.CHANGED){a=!0,n.anyChanged=!0,this.analysisManager.invalidateFunctions(s,t.preservedAnalyses);let p=this._verifyAfter(t,s,!0);p&&(n.errors.push(p),o||(l=!0))}else h===et.FAILED&&(this.analysisManager.invalidateAll(),n.errors.push(new ws("graphPasses",s.name||"<module>",`pass '${t.name}' failed`,t.name)),o||(l=!0))}else if(t instanceof Mt){let u=!1;for(let f of s){if(n.failedFunctions.has(f.name))continue;let h=i?ep(f):-1,p=i?performance.now():0;if(o)try{for(let m of t.requiredAnalyses)this.analysisManager.getAnalysis(m,f);let d=t.run(f,this.analysisManager);if(i&&this.trace.passRun(t.name,d,performance.now()-p,h,ep(f)),d===et.CHANGED){u=!0,n.anyChanged=!0,f.bumpVersion(),this.analysisManager.invalidate(f,t.preservedAnalyses);let m=this._verifyAfter(t,f,!1);m&&(n.errors.push(m),n.failedFunctions.add(f.name))}else d===et.FAILED&&(this.analysisManager.invalidate(f),n.errors.push(new ws("graphPasses",f.name,`pass '${t.name}' failed`,t.name)),n.failedFunctions.add(f.name))}catch(d){n.errors.push(new ws("graphPasses",f.name,d.message,t.name)),n.failedFunctions.add(f.name)}else{for(let m of t.requiredAnalyses)this.analysisManager.getAnalysis(m,f);let d=t.run(f,this.analysisManager);if(i&&this.trace.passRun(t.name,d,performance.now()-p,h,ep(f)),d===et.CHANGED){u=!0,n.anyChanged=!0,f.bumpVersion(),this.analysisManager.invalidate(f,t.preservedAnalyses);let m=this._verifyAfter(t,f,!1);if(m){n.errors.push(m),n.failedFunctions.add(f.name),l=!0;break}}else if(d===et.FAILED){this.analysisManager.invalidate(f),n.errors.push(new ws("graphPasses",f.name,`pass '${t.name}' failed`,t.name)),n.failedFunctions.add(f.name),l=!0;break}}}r.push(u?et.CHANGED:et.UNCHANGED),a=u}return this.instruments.length&&this._notify("runAfterPass",t,s,a?et.CHANGED:et.UNCHANGED),t.trace=null,{changed:a,fatal:l}}_runGroup(t,s,n,r){let i=t.maxIterations>0?t.maxIterations:1;for(let o=0;o<i;o++){let a=!1;for(let l of t.passes){if(!n.passContext.shouldRun(l))continue;let{changed:u,fatal:f}=this._applyPass(l,s,n,r);if(f)return!0;u&&(a=!0)}if(!a)return!1}return this.trace&&this.trace.passRun(`${t.name}:max-iter`,et.UNCHANGED,0,-1,-1),!1}run(t,s={}){let n={verbose:this.trace&&this.trace.level>=at.VERBOSE,resilient:s.errorMode==="resilient",errors:[],failedFunctions:new Set,anyChanged:!1,passContext:s.passContext||new Nd},r=[];for(let i of this.passes){if(!(i instanceof Hc)&&!n.passContext.shouldRun(i))continue;if(i instanceof Hc?this._runGroup(i,t,n,r):this._applyPass(i,t,n,r).fatal)return{changed:n.anyChanged,results:r,errors:n.errors,failedFunctions:n.failedFunctions.size>0?n.failedFunctions:null}}return{changed:n.anyChanged,results:r,errors:n.errors.length>0?n.errors:null,failedFunctions:n.failedFunctions.size>0?n.failedFunctions:null}}}});function as(e,t){qx.set(e,t)}function ys(e,t,s,n){return e.broadcast(e.scalarConstant(t,s).getResult(0),n,[]).getResult(0)}var qx,Jc,jx=I(()=>{hs();li();It();ps();qx=new Map;c(as,"registerDecomposition");Jc=class extends Mt{static{c(this,"DecompositionPass")}constructor(t=null){super("DecompositionPass"),this.target=t}_shouldDecompose(t){if(!this.target)return!0;let s=this.target.getAttr?this.target.getAttr("nativeOps"):null;return!(s&&s.has(t.opName))}run(t){let s=[];for(let i of t.opsRecursive())qx.has(i.opName)&&this._shouldDecompose(i)&&s.push(i);if(s.length===0)return et.UNCHANGED;let n=new bs(t),r=[];for(let i of s){if(!i.parentBlock)continue;let o=qx.get(i.opName);n.block=i.parentBlock,n.setInsertionPoint(i),r.push(i.opName),o(i,n)}if(this.trace&&this.trace.level>=at.DEBUG){let i={};for(let o of r)i[o]=(i[o]||0)+1;this.trace.emit({type:"pass_detail",passName:this.name,decomposed:i,totalDecomposed:r.length,level:at.DEBUG})}return et.CHANGED}};as("stop_gradient",e=>{e.replaceAllResultsWith([e.getOperand(0)]),e.erase()});as("all_reduce",(e,t)=>{let s=e.getOperand(0),n=e.getAttr("mesh_axis")??0,r=e.getAttr("reduce_op")||"sum",i=s.type.shape,o=s.type.dtype,a=r==="max"?-1/0:r==="min"?1/0:r==="prod"||r==="and"?1:0,l=t.reduce(s,t.scalarConstant(a,o).getResult(0),[n],r).getResult(0),u=[];for(let h=0;h<i.length;h++)h!==n&&u.push(h);let f=t.broadcast(l,i,u).getResult(0);e.replaceAllResultsWith([f]),e.erase()});as("all_gather",(e,t)=>{let s=e.getOperand(0),n=s.type.shape,r=e.getAttr("mesh_axis")??0,i=e.getAttr("gather_dim")??1,o=n[r],a=n.filter((m,g)=>g!==r),l=i<r?i:i-1,u=[];for(let m=0;m<o;m++){let g=n.map((w,b)=>b===r?m:0),y=n.map((w,b)=>b===r?m+1:w),_=t.slice(s,g,y).getResult(0);u.push(t.reshape(_,a).getResult(0))}let f=u.length===1?u[0]:t.concat(u,l).getResult(0),h=[...n];h[i]=o*n[i];let p=[];for(let m=0;m<h.length;m++)m!==r&&p.push(m);let d=t.broadcast(f,h,p).getResult(0);e.replaceAllResultsWith([d]),e.erase()});as("softmax",(e,t)=>{let s=e.getOperand(0),n=e.getAttr("axis"),r=s.type.rank,i=s.type.dtype,o=s.type.shape,a=sm(r,n),l=t.reduce(s,t.scalarConstant(-1/0,i).getResult(0),[n],"max"),u=t.broadcast(l.getResult(0),o,a),f=t.sub(s,u.getResult(0)),h=t.exp(f.getResult(0)),p=t.reduce(h.getResult(0),t.scalarConstant(0,i).getResult(0),[n],"sum"),d=t.broadcast(p.getResult(0),o,a),m=t.div(h.getResult(0),d.getResult(0));e.replaceAllResultsWith([m.getResult(0)]),e.erase()});as("log_softmax",(e,t)=>{let s=e.getOperand(0),n=e.getAttr("axis"),r=s.type.rank,i=s.type.dtype,o=s.type.shape,a=sm(r,n),l=t.reduce(s,t.scalarConstant(-1/0,i).getResult(0),[n],"max"),u=t.broadcast(l.getResult(0),o,a),f=t.sub(s,u.getResult(0)),h=t.exp(f.getResult(0)),p=t.reduce(h.getResult(0),t.scalarConstant(0,i).getResult(0),[n],"sum"),d=t.log(p.getResult(0)),m=t.broadcast(d.getResult(0),o,a),g=t.sub(f.getResult(0),m.getResult(0));e.replaceAllResultsWith([g.getResult(0)]),e.erase()});as("sigmoid",(e,t)=>{let s=e.getOperand(0),n=s.type.dtype,r=s.type.shape,i=t.neg(s),o=t.exp(i.getResult(0)),a=t.broadcast(t.scalarConstant(1,n).getResult(0),r,[]),l=t.add(a.getResult(0),o.getResult(0)),u=t.div(a.getResult(0),l.getResult(0));e.replaceAllResultsWith([u.getResult(0)]),e.erase()});as("gelu",(e,t)=>{let s=e.getOperand(0),n=s.type.dtype,r=s.type.shape,i=t.broadcast(t.scalarConstant(1.702,n).getResult(0),r,[]),o=t.mul(i.getResult(0),s),a=t.neg(o.getResult(0)),l=t.exp(a.getResult(0)),u=t.broadcast(t.scalarConstant(1,n).getResult(0),r,[]),f=t.add(u.getResult(0),l.getResult(0)),h=t.div(u.getResult(0),f.getResult(0)),p=t.mul(s,h.getResult(0));e.replaceAllResultsWith([p.getResult(0)]),e.erase()});as("silu",(e,t)=>{let s=e.getOperand(0),n=s.type.dtype,r=s.type.shape,i=t.neg(s),o=t.exp(i.getResult(0)),a=t.broadcast(t.scalarConstant(1,n).getResult(0),r,[]),l=t.add(a.getResult(0),o.getResult(0)),u=t.div(a.getResult(0),l.getResult(0)),f=t.mul(s,u.getResult(0));e.replaceAllResultsWith([f.getResult(0)]),e.erase()});as("layer_norm",(e,t)=>{let s=e.getOperand(0),n=e.getOperand(1),r=e.getOperand(2),i=e.getAttr("axis"),o=e.getAttr("epsilon"),a=s.type.rank,l=s.type.dtype,u=s.type.shape,f=sm(a,i),h=t.reduce(s,t.scalarConstant(0,l).getResult(0),[i],"mean"),p=t.broadcast(h.getResult(0),u,f),d=t.sub(s,p.getResult(0)),m=t.mul(d.getResult(0),d.getResult(0)),g=t.reduce(m.getResult(0),t.scalarConstant(0,l).getResult(0),[i],"mean"),y=t.broadcast(t.scalarConstant(o,l).getResult(0),g.getResult(0).type.shape,[]),_=t.add(g.getResult(0),y.getResult(0)),w=t.rsqrt(_.getResult(0)),b=t.broadcast(w.getResult(0),u,f),x=t.mul(d.getResult(0),b.getResult(0)),A=t.broadcast(n,u,[i]),S=t.mul(x.getResult(0),A.getResult(0)),E=t.broadcast(r,u,[i]),R=t.add(S.getResult(0),E.getResult(0));e.replaceAllResultsWith([R.getResult(0)]),e.erase()});as("batch_norm",(e,t)=>{let s=e.getOperand(0),n=e.getOperand(1),r=e.getOperand(2),i=e.getOperand(3),o=e.getOperand(4),a=e.getAttr("axis"),l=e.getAttr("epsilon"),u=s.type.rank,f=s.type.dtype,h=s.type.shape,p=t.broadcast(t.scalarConstant(l,f).getResult(0),o.type.shape,[]),d=t.add(o,p.getResult(0)),m=t.rsqrt(d.getResult(0)),g=t.broadcast(i,h,[a]),y=t.sub(s,g.getResult(0)),_=t.broadcast(m.getResult(0),h,[a]),w=t.mul(y.getResult(0),_.getResult(0)),b=t.broadcast(n,h,[a]),x=t.mul(w.getResult(0),b.getResult(0)),A=t.broadcast(r,h,[a]),S=t.add(x.getResult(0),A.getResult(0));e.replaceAllResultsWith([S.getResult(0)]),e.erase()});as("where",(e,t)=>{let s=e.getOperand(0);if(s.type.dtype!==Q.BOOL){let r=t.broadcast(t.scalarConstant(0,s.type.dtype).getResult(0),s.type.shape,[]);s=t.compare(s,r.getResult(0),"ne").getResult(0)}let n=t.select(s,e.getOperand(1),e.getOperand(2));e.replaceAllResultsWith([n.getResult(0)]),e.erase()});as("split",(e,t)=>{let s=e.getOperand(0),n=e.getAttr("dimension"),r=e.getAttr("split_sizes"),i=s.type.shape,o=[],a=0;for(let l of r){let u=i.map((h,p)=>p===n?a:0),f=i.map((h,p)=>p===n?a+l:h);o.push(t.slice(s,u,f).getResult(0)),a+=l}e.replaceAllResultsWith(o),e.erase()});as("one_hot",(e,t)=>{let s=e.getOperand(0),n=e.getAttr("depth"),r=e.getAttr("axis")??-1,i=e.getAttr("on_value")??1,o=e.getAttr("off_value")??0,a=e.getResult(0).type,l=a.dtype,u=a.shape,f=r<0?s.type.rank+1+r:r,h=new T(u,Q.I32),p=t._inferAndBuild("iota",[],{iota_dimension:f,tensor_type:h}),d=s.type.rank,m=[];for(let A=0;A<d;A++)m.push(A<f?A:A+1);let g=t.broadcast(s,u,m),y=t.convert(g.getResult(0),Q.I32),_=t.compare(y.getResult(0),p.getResult(0),"eq"),w=t.broadcast(t.scalarConstant(i,l).getResult(0),u,[]),b=t.broadcast(t.scalarConstant(o,l).getResult(0),u,[]),x=t.select(_.getResult(0),w.getResult(0),b.getResult(0));e.replaceAllResultsWith([x.getResult(0)]),e.erase()});c(ys,"bcast");as("elu",(e,t)=>{let s=e.getOperand(0),n=e.getAttr("alpha")??1,r=s.type.dtype,i=s.type.shape,o=ys(t,0,r,i),a=t.compare(s,o,"gt").getResult(0),l=ys(t,1,r,i),u=t.exp(s).getResult(0),f=t.sub(u,l).getResult(0),h=ys(t,n,r,i),p=t.mul(h,f).getResult(0),d=t.select(a,s,p);e.replaceAllResultsWith([d.getResult(0)]),e.erase()});as("leaky_relu",(e,t)=>{let s=e.getOperand(0),n=e.getAttr("negative_slope")??.01,r=s.type.dtype,i=s.type.shape,o=ys(t,0,r,i),a=t.compare(s,o,"gt").getResult(0),l=ys(t,n,r,i),u=t.mul(l,s).getResult(0),f=t.select(a,s,u);e.replaceAllResultsWith([f.getResult(0)]),e.erase()});as("celu",(e,t)=>{let s=e.getOperand(0),n=e.getAttr("alpha")??1,r=s.type.dtype,i=s.type.shape,o=ys(t,0,r,i),a=t.maximum(s,o).getResult(0),l=ys(t,n,r,i),u=t.div(s,l).getResult(0),f=t.exp(u).getResult(0),h=ys(t,1,r,i),p=t.sub(f,h).getResult(0),d=t.mul(l,p).getResult(0),m=t.minimum(o,d).getResult(0),g=t.add(a,m);e.replaceAllResultsWith([g.getResult(0)]),e.erase()});as("selu",(e,t)=>{let s=e.getOperand(0),n=s.type.dtype,r=s.type.shape,i=1.0507009873554805,o=1.6732632423543772,a=ys(t,0,n,r),l=t.compare(s,a,"gt").getResult(0),u=ys(t,1,n,r),f=t.exp(s).getResult(0),h=t.sub(f,u).getResult(0),p=ys(t,o,n,r),d=t.mul(p,h).getResult(0),m=t.select(l,s,d).getResult(0),g=ys(t,i,n,r),y=t.mul(g,m);e.replaceAllResultsWith([y.getResult(0)]),e.erase()});as("mish",(e,t)=>{let s=e.getOperand(0),n=s.type.dtype,r=s.type.shape,i=ys(t,1,n,r),o=t.exp(s).getResult(0),a=t.add(i,o).getResult(0),l=t.log(a).getResult(0),u=t.tanh(l).getResult(0),f=t.mul(s,u);e.replaceAllResultsWith([f.getResult(0)]),e.erase()});as("hardswish",(e,t)=>{let s=e.getOperand(0),n=s.type.dtype,r=s.type.shape,i=ys(t,3,n,r),o=ys(t,0,n,r),a=ys(t,6,n,r),l=t.add(s,i).getResult(0),u=t.minimum(t.maximum(l,o).getResult(0),a).getResult(0),f=t.div(u,a).getResult(0),h=t.mul(s,f);e.replaceAllResultsWith([h.getResult(0)]),e.erase()});as("hardsigmoid",(e,t)=>{let s=e.getOperand(0),n=s.type.dtype,r=s.type.shape,i=ys(t,6,n,r),o=ys(t,.5,n,r),a=ys(t,0,n,r),l=ys(t,1,n,r),u=t.div(s,i).getResult(0),f=t.add(u,o).getResult(0),h=t.minimum(t.maximum(f,a).getResult(0),l);e.replaceAllResultsWith([h.getResult(0)]),e.erase()});as("embedding",(e,t)=>{let s=e.getOperand(0),n=e.getOperand(1),r=s.type.shape[s.type.rank-1],i=n.type.rank,o=t._inferAndBuild("gather",[s,n],{offset_dims:Array.from({length:1},(a,l)=>i+l),collapsed_slice_dims:[0],start_index_map:[0],slice_sizes:[1,r],index_vector_dim:i});e.replaceAllResultsWith([o.getResult(0)]),e.erase()})});function v2(){if(sp)return sp;sp=new ac;for(let e of rt.allOps())if(e.getCanonicalizationPatterns){let t=e.getCanonicalizationPatterns();if(t)for(let s of t)sp.add(s)}return sp}var sp,El,Wx=I(()=>{hs();jf();rs();sp=null;c(v2,"getCanonicalizationPatterns");El=class extends Mt{static{c(this,"CanonicalizePass")}constructor(){super("canonicalize")}run(t,s){let n=v2();return new lc(n).applyPatterns(t,10,this.trace)}}});var kg,Kx,Yc,YC=I(()=>{rs();He();kg=class{static{c(this,"MemoryEffect")}constructor(t,s){this.kind=t,this.value=s}},Kx=class{static{c(this,"MemoryEffectResult")}constructor(t,s){this.opEffects=t,this._valueEffects=s}hasSideEffect(t){let s=this.opEffects.get(t);return s&&s.length>0}getEffects(t){return this.opEffects.get(t)||[]}getEffectsOn(t){return this._valueEffects.get(t)||[]}getReadersOf(t){let s=this._valueEffects.get(t);if(!s)return[];let n=[];for(let r=0;r<s.length;r++)s[r].effect.kind===fs.READ&&n.push(s[r].op);return n}getWritersOf(t){let s=this._valueEffects.get(t);if(!s)return[];let n=[];for(let r=0;r<s.length;r++)s[r].effect.kind===fs.WRITE&&n.push(s[r].op);return n}},Yc=class{static get name(){return"memory_effect"}static get depKey(){return"memoryEffect"}static get dependencies(){return[]}static compute(t){let s=new Map,n=new Map,r=c((o,a,l)=>{let u=n.get(o);u||(u=[],n.set(o,u)),u.push({op:a,effect:l})},"addValueEffect"),i=typeof t.opsRecursive=="function"?t.opsRecursive():t.ops();for(let o of i){let a=rt.get(o.opName),l=[],u=fs.NONE;if(a&&a.sideEffects?u=a.sideEffects:o.hasSideEffects&&o.hasSideEffects()&&(u=fs.WRITE),u!==fs.NONE)if(a&&a.getMemoryEffects){let f=a.getMemoryEffects(o);for(let h of f)l.push(h),r(h.value,o,h)}else{if(u&fs.READ)for(let f=0;f<o.numOperands;f++){let h=new kg(fs.READ,o.getOperand(f));l.push(h),r(o.getOperand(f),o,h)}if(u&fs.WRITE)for(let f=0;f<o.numResults;f++){let h=new kg(fs.WRITE,o.getResult(f));l.push(h),r(o.getResult(f),o,h)}}s.set(o,l)}return new Kx(s,n)}}});var k2,qo,Hx=I(()=>{k2={ceildiv:"ceilDiv"},qo=class e{static{c(this,"SymInt")}constructor(t,s=null,n=[]){this.type=t,this.name=s,this.args=n}static var(t){return new e("var",t)}static const(t){return t}static add(t,s){return typeof t=="number"&&typeof s=="number"?t+s:t===0?s:s===0?t:e.equals(t,s)?e.mul(2,t):new e("add",null,[t,s])}static sub(t,s){return typeof t=="number"&&typeof s=="number"?t-s:s===0?t:e.equals(t,s)?0:new e("sub",null,[t,s])}static neg(t){return typeof t=="number"?-t:t instanceof e&&t.type==="neg"?t.args[0]:new e("neg",null,[t])}static mul(t,s){return typeof t=="number"&&typeof s=="number"?t*s:t===0||s===0?0:t===1?s:s===1?t:new e("mul",null,[t,s])}static div(t,s){if(s===0)throw new Error("SymInt.div: division by zero");return typeof t=="number"&&typeof s=="number"?Math.floor(t/s):t===0?0:s===1?t:e.equals(t,s)?1:new e("div",null,[t,s])}static mod(t,s){if(s===0)throw new Error("SymInt.mod: modulo by zero");return typeof t=="number"&&typeof s=="number"?(t%s+s)%s:t===0||s===1||e.equals(t,s)?0:new e("mod",null,[t,s])}static max(t,s){return typeof t=="number"&&typeof s=="number"?Math.max(t,s):t===s||e.equals(t,s)?t:new e("max",null,[t,s])}static min(t,s){return typeof t=="number"&&typeof s=="number"?Math.min(t,s):t===s||e.equals(t,s)?t:new e("min",null,[t,s])}static ceilDiv(t,s){return typeof t=="number"&&typeof s=="number"?Math.ceil(t/s):new e("ceildiv",null,[t,s])}static equals(t,s){if(t===s)return!0;if(typeof t=="number"||typeof s=="number"||!(t instanceof e)||!(s instanceof e)||t.type!==s.type)return!1;if(t.type==="var")return t.name===s.name;if(t.args.length!==s.args.length)return!1;if(t.type==="add"||t.type==="mul"||t.type==="max"||t.type==="min"){let n=e.equals(t.args[0],s.args[0])&&e.equals(t.args[1],s.args[1]),r=e.equals(t.args[0],s.args[1])&&e.equals(t.args[1],s.args[0]);return n||r}for(let n=0;n<t.args.length;n++)if(!e.equals(t.args[n],s.args[n]))return!1;return!0}static substitute(t,s,n){if(typeof t=="number"||!(t instanceof e))return t;if(t.type==="var")return t.name===s?n:t;let r=t.args.map(o=>e.substitute(o,s,n)),i=e[k2[t.type]||t.type];return i&&r.length===2?i(r[0],r[1]):i&&r.length===1?i(r[0]):new e(t.type,t.name,r)}static evaluate(t,s){if(typeof t=="number"||!(t instanceof e))return t;if(t.type==="var"){if(s.has(t.name))return s.get(t.name);throw new Error(`Unbound symbolic variable: ${t.name}`)}let n=t.args.map(r=>e.evaluate(r,s));switch(t.type){case"add":return n[0]+n[1];case"sub":return n[0]-n[1];case"mul":return n[0]*n[1];case"div":return Math.floor(n[0]/n[1]);case"mod":return(n[0]%n[1]+n[1])%n[1];case"max":return Math.max(n[0],n[1]);case"min":return Math.min(n[0],n[1]);case"neg":return-n[0];case"ceildiv":return Math.ceil(n[0]/n[1]);default:throw new Error(`Unknown SymInt op: ${t.type}`)}}static freeVars(t,s=new Set){if(typeof t=="number"||!(t instanceof e))return s;if(t.type==="var")return s.add(t.name),s;for(let n of t.args)e.freeVars(n,s);return s}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 Jx,jo,Eg=I(()=>{rs();It();Hx();Jx=class{static{c(this,"ShapeResult")}constructor(t){this.shapes=t}shapeOf(t){return this.shapes.get(t)||null}},jo=class{static get name(){return"shape"}static get depKey(){return"shape"}static get dependencies(){return[]}static compute(t,s){let n=new Map,r=0,i=new Map;if(s&&s.symbols.size>0)for(let[l,u]of s.symbols){let f=`${u.inputIdx}:${u.dimIdx}`;i.set(f,l)}let o=c((l,u)=>l?l.map((f,h)=>{if(f!==X)return f;if(u!==void 0){let p=i.get(`${u}:${h}`);if(p)return qo.var(p)}return qo.var(`d${r++}`)}):null,"resolveShape"),a=t.args;for(let l=0;l<a.length;l++)n.set(a[l],o(a[l].type.shape,l));for(let l of t.ops()){let u=rt.get(l.opName);if(u&&u.propagateSymbolicShapes){let f=u.propagateSymbolicShapes(l,n);if(f){for(let h=0;h<Math.min(f.length,l.numResults);h++)f[h]&&n.set(l.getResult(h),f[h]);continue}}if(u&&u.inferResultTypes){let f=l.operands.map(p=>p.type),h=u.inferResultTypes(f,l.attributes,l.results.map(p=>p.type));if(h){for(let p=0;p<Math.min(h.length,l.numResults);p++)n.set(l.getResult(p),o(h[p].shape));continue}}for(let f=0;f<l.numResults;f++)n.set(l.getResult(f),o(l.getResult(f).type.shape))}return new Jx(n)}}});var Bi,Yx=I(()=>{hs();YC();Eg();ps();Bi=class extends Mt{static{c(this,"DCEPass")}constructor(){super("dce"),this.preservedAnalyses=new Set([jo]),this.requiredAnalyses=[Yc]}run(t,s){let n=!1,r=s?s.getAnalysis(Yc,t):Yc.compute(t),i=[];for(let a of t.opsRecursive())this._isDead(a,r)&&i.push(a);let o=0;for(;i.length>0;){let a=i.pop();if(!a.parentBlock||!this._isDead(a,r))continue;let l=[];for(let u=0;u<a.numOperands;u++){let f=a.getOperand(u).definingOp;f&&f.parentBlock&&l.push(f)}a.erase(),n=!0,o++;for(let u of l)u.parentBlock&&this._isDead(u,r)&&i.push(u)}return this.trace&&this.trace.level>=at.DEBUG&&o>0&&this.trace.emit({type:"pass_detail",passName:this.name,erasedCount:o,level:at.DEBUG}),n?et.CHANGED:et.UNCHANGED}_isDead(t,s){if(t.opName==="return"||t.opName==="yield"||t.regions&&t.regions.length>0)return!1;for(let n=0;n<t.numResults;n++)if(t.getResult(n).hasUses)return!1;return!s.hasSideEffect(t)}}});var Ng,Rg,QC=I(()=>{Ng=class e{static{c(this,"ScheduleStep")}constructor(t,s){this.primitive=t,this.args=s}serialize(){return{primitive:this.primitive,args:this.args}}static deserialize(t){return new e(t.primitive,t.args)}},Rg=class e{static{c(this,"ScheduleTrace")}constructor(){this.steps=[]}record(t,s){let n=new Ng(t,s);return this.steps.push(n),n}serialize(){return this.steps.map(t=>t.serialize())}static deserialize(t){let s=new e;for(let n of t)s.steps.push(Ng.deserialize(n));return s}replay(t){for(let s of this.steps){let n=t[s.primitive];if(typeof n!="function")throw new Error(`Unknown schedule primitive: ${s.primitive}`);t._replaying=!0;try{n.call(t,...s.args)}finally{t._replaying=!1}}}get length(){return this.steps.length}clear(){this.steps.length=0}}});function ge(e,t){if(!(!e||typeof e!="object"))switch(e.type){case"VariableNode":e.name&&t.add(e.name);return;case"BufferLoadNode":if(e.indices)for(let s of e.indices)ge(s,t);return;case"BufferStoreNode":if(e.indices)for(let s of e.indices)ge(s,t);ge(e.value,t);return;case"MathOpNode":ge(e.a,t),e.b&&ge(e.b,t);return;case"CompareNode":ge(e.a,t),ge(e.b,t);return;case"CastNode":ge(e.expr,t);return;case"CallExternNode":for(let s of e.args)ge(s,t);return;case"IfThenElseNode":ge(e.condition,t),ge(e.thenBody,t),e.elseBody&&ge(e.elseBody,t);return;case"SeqNode":for(let s of e.stmts)ge(s,t);return;case"LetStmtNode":ge(e.value,t),ge(e.body,t);return;default:return}}function Bg(e,t){let s=new Set((e.writes||[]).map(r=>r.buffer&&r.buffer.name)),n=[e.body,e.initBody];for(;n.length>0;){let r=n.pop();if(!(!r||typeof r!="object")){if(r.type==="BufferStoreNode"&&r.buffer&&s.has(r.buffer.name)&&r.indices)for(let i of r.indices)ge(i,t);if(r.type==="BufferStoreNode"){r.value&&n.push(r.value);continue}if(r.type==="SeqNode"){for(let i of r.stmts)n.push(i);continue}if(r.type==="IfThenElseNode"){n.push(r.thenBody),r.elseBody&&n.push(r.elseBody);continue}if(r.type==="LetStmtNode"){n.push(r.body);continue}}}}function E2(e,t){let s=new Set;if(ge(t.body,s),t.initBody&&ge(t.initBody,s),!s.has(e))return!1;let n=new Set;return Bg(t,n),!n.has(e)}function Nl(e,t){if(!(!e||typeof e!="object"))switch(e.type){case"BlockNode":t.push(e),Nl(e.body,t),e.initBody&&Nl(e.initBody,t);return;case"ForNode":case"AllocateNode":case"LetStmtNode":Nl(e.body,t);return;case"SeqNode":for(let s of e.stmts)Nl(s,t);return;case"IfThenElseNode":Nl(e.thenBody,t),e.elseBody&&Nl(e.elseBody,t);return;default:return}}function Qx(e){let t=e.loopVar.name,s=[];Nl(e.body,s);for(let n of s)if(E2(t,n))return n.name;return null}var Xx=I(()=>{c(ge,"collectVarsUsed");c(Bg,"collectWriteIndexVars");c(E2,"loopVarIsReductionOf");c(Nl,"collectBlocksUnder");c(Qx,"loopCarriesReduction")});var Qc,Zx=I(()=>{Jt();Xx();Qc=class e{static{c(this,"ScheduleValidator")}static validate(t){let s=[],n={boundVars:new Set,threadBindings:new Map,parallelExtents:new Map,parLoops:[],innermostLoopVar:null,loopStack:[],errors:s};for(let r of t.params)n.boundVars.add(r.name);return e._visitNode(t.body,n),e._checkPartitionConsistency(n),s}static _checkPartitionConsistency(t){if(t.parallelExtents.size<=1)return;let s=[...t.parallelExtents.entries()].map(([n,r])=>`'${r}'(extent ${n})`);t.errors.push(`Ambiguous parallel partition: ${t.parallelExtents.size} distinct parallel extents [${s.join(", ")}] \u2014 runtime partitions a single axis, mismatched-extent parallel loops corrupt buffers`)}static _visitNode(t,s){if(t)switch(t.type){case"ForNode":e._visitFor(t,s);break;case"BlockNode":e._visitBlock(t,s);break;case"SeqNode":for(let n of t.stmts)e._visitNode(n,s);break;case"IfThenElseNode":e._visitExpr(t.condition,s),e._visitNode(t.thenBody,s),t.elseBody&&e._visitNode(t.elseBody,s);break;case"AllocateNode":e._visitNode(t.body,s);break;case"LetStmtNode":s.boundVars.add(t.variable.name),e._visitNode(t.body,s),s.boundVars.delete(t.variable.name);break;case"BufferStoreNode":e._validateBufferAccess(t,s);break;case"EvaluateNode":break;default:break}}static _visitFor(t,s){let n=t.loopVar.name;if(s.boundVars.has(n)&&s.errors.push(`Duplicate loop variable: ${n}`),t.kind===V.THREAD_BINDING&&(t.threadTag?(s.threadBindings.has(t.threadTag)&&s.errors.push(`Duplicate thread binding '${t.threadTag}': already bound to '${s.threadBindings.get(t.threadTag)}'`),s.threadBindings.set(t.threadTag,n)):s.errors.push(`Thread-bound loop '${n}' missing threadTag`)),t.kind===V.VECTORIZED){let o=t.extent;o.type==="IntImmNode"&&o.value<=0&&s.errors.push(`Vectorized loop '${n}' has non-positive extent ${o.value}`)}if(t.kind===V.PARALLEL){let o=t.extent;o&&o.type==="IntImmNode"&&!s.parallelExtents.has(o.value)&&s.parallelExtents.set(o.value,n)}(t.kind===V.PARALLEL||t.kind===V.THREAD_BINDING)&&e._checkNoNestedParallel(t.body,n,s);let r=t.kind===V.PARALLEL||t.kind===V.VECTORIZED;r&&s.parLoops.push({varName:n,kind:t.kind}),s.boundVars.add(n);let i=s.innermostLoopVar;s.innermostLoopVar=n,s.loopStack.push(n),e._visitNode(t.body,s),s.loopStack.pop(),s.innermostLoopVar=i,s.boundVars.delete(n),r&&s.parLoops.pop(),t.kind===V.THREAD_BINDING&&t.threadTag&&s.threadBindings.delete(t.threadTag)}static _checkNoNestedParallel(t,s,n){t&&t.type==="ForNode"&&t.kind===V.PARALLEL&&n.errors.push(`Parallel loop '${t.loopVar.name}' nested inside parallel/thread-bound loop '${s}'`)}static _visitBlock(t,s){for(let n of t.iterVars)n.iterVar&&s.boundVars.add(n.iterVar.name);if(t.initBody&&s.innermostLoopVar){let n=new Set;Bg(t,n);let r=new Set;ge(t.body,r),ge(t.initBody,r);let i=0;for(let o of s.loopStack)r.has(o)&&!n.has(o)&&i++;(n.has(s.innermostLoopVar)||i>1)&&s.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 '${s.innermostLoopVar}', ${i} enclosing reduction loops)`)}if(s.parLoops.length>0){let n=new Set;ge(t.body,n),t.initBody&&ge(t.initBody,n);let r=new Set;Bg(t,r);for(let i of s.parLoops)if(n.has(i.varName)&&!r.has(i.varName)){let o=i.kind===V.VECTORIZED?"Vectorized":"Parallel";s.errors.push(`${o} loop '${i.varName}' carries a reduction in block '${t.name}': the loop variable is read but never written, so parallel iterations race on the accumulator`)}}e._visitNode(t.body,s),t.initBody&&e._visitNode(t.initBody,s);for(let n of t.iterVars)n.iterVar&&s.boundVars.delete(n.iterVar.name)}static _validateBufferAccess(t,s){if(t.buffer?e._checkRank(t,s):s.errors.push("BufferStore with null buffer"),t.indices)for(let n of t.indices)e._visitExpr(n,s);t.value&&e._visitExpr(t.value,s)}static _checkRank(t,s){t.indices&&t.buffer.shape&&t.indices.length!==t.buffer.shape.length&&s.errors.push(`Buffer '${t.buffer.name}' rank mismatch: ${t.indices.length} indices for rank-${t.buffer.shape.length} buffer`)}static _visitExpr(t,s){if(!(!t||typeof t!="object"))switch(t.type){case"BufferLoadNode":if(t.buffer?e._checkRank(t,s):s.errors.push("BufferLoad with null buffer"),t.indices)for(let n of t.indices)e._visitExpr(n,s);break;case"BufferStoreNode":e._validateBufferAccess(t,s);break;case"MathOpNode":case"CompareNode":e._visitExpr(t.a,s),e._visitExpr(t.b,s);break;case"CastNode":e._visitExpr(t.expr,s);break;case"CallExternNode":for(let n of t.args)e._visitExpr(n,s);break;case"IfThenElseNode":e._visitExpr(t.condition,s),e._visitNode(t.thenBody,s),t.elseBody&&e._visitNode(t.elseBody,s);break}}}});var Rl,Xc,tS=I(()=>{Jt();Rl=class{static{c(this,"SRef")}constructor(t,s=null){this.node=t,this.parent=s,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=[],s=this.parent;for(;s;)t.push(s),s=s.parent;return t}loopAncestors(){let t=[],s=this.parent;for(;s;)s.isLoop&&t.push(s),s=s.parent;return t}childBlocks(){let t=[],s=[...this.children];for(;s.length>0;){let n=s.pop();if(n.isBlock)t.push(n);else for(let r of n.children)s.push(r)}return t}childLoops(){return this.children.filter(t=>t.isLoop)}},Xc=class{static{c(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,s){let n=[{node:t,parentSRef:s}];for(;n.length>0;){let{node:r,parentSRef:i}=n.pop();if(r)switch(r.type){case"ForNode":{let o=new Rl(r,i);this._nodeToSRef.set(r,o),this._loopSRefs.push(o),i&&i.children.push(o),n.push({node:r.body,parentSRef:o});break}case"BlockNode":{let o=new Rl(r,i);this._nodeToSRef.set(r,o),this._blockNameToSRef.set(r.name,o),this._blockSRefs.push(o),i&&i.children.push(o),n.push({node:r.body,parentSRef:o}),r.initBody&&n.push({node:r.initBody,parentSRef:o});break}case"SeqNode":for(let o=r.stmts.length-1;o>=0;o--)n.push({node:r.stmts[o],parentSRef:i});break;case"IfThenElseNode":r.elseBody&&n.push({node:r.elseBody,parentSRef:i}),n.push({node:r.thenBody,parentSRef:i});break;case"AllocateNode":case"LetStmtNode":n.push({node:r.body,parentSRef:i});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 s=this._blockNameToSRef.get(t);return s?s.loopAncestors().filter(n=>n.node.kind!==V.RECURRENCE).reverse():[]}replaceLoop(t,s,n){let r=this._nodeToSRef.get(t);if(!r)return;let i=r.parent,o=new Rl(s,i),a=new Rl(n,o);o.children.push(a);for(let l of r.children)l.parent=a,a.children.push(l);if(i){let l=i.children.indexOf(r);l>=0&&(i.children[l]=o)}else this.root=o;this._nodeToSRef.delete(t),this._nodeToSRef.set(s,o),this._nodeToSRef.set(n,a),this._loopSRefs.push(o,a)}reorderLoops(t,s,n){let r=t.map(u=>this._nodeToSRef.get(u)).filter(Boolean);if(r.length<2)return;let i=this._nodeToSRef.get(s),o=i?i.parent:null;if(o){let u=o.children.indexOf(i);u>=0&&(o.children[u]=r[0])}else this.root=r[0];for(let u=0;u<r.length;u++)r[u].parent=u===0?o:r[u-1],r[u].children=u<r.length-1?[r[u+1]]:[];let a=r[r.length-1],l=i?this._collectDescendantsNotIn(i,new Set(r)):[];for(let u of l)u.parent=a,a.children.push(u)}_collectDescendantsNotIn(t,s){let n=[],r=[...t.children];for(;r.length>0;){let i=r.pop();if(s.has(i))for(let o of i.children)r.push(o);else n.push(i)}return n}rebuildFrom(t){this._nodeToSRef.clear(),this._blockNameToSRef.clear(),this._loopSRefs.length=0,this._blockSRefs.length=0,this.root=this._build(t,null)}replaceNode(t,s){let n=this._nodeToSRef.get(t);if(!n)return;let r=new Rl(s,n.parent);r.children=n.children;for(let i of r.children)i.parent=r;if(n.parent){let i=n.parent.children.indexOf(n);i>=0&&(n.parent.children[i]=r)}else this.root=r;this._nodeToSRef.delete(t),this._nodeToSRef.set(s,r),s.type==="BlockNode"&&(this._blockNameToSRef.delete(t.name),this._blockNameToSRef.set(s.name,r))}}});function N2(e){let t=new Set,s=new Set;return _s(e.body,t,s),e.initBody&&_s(e.initBody,t,s),{reads:t,writes:s,accesses:R2(e)}}function XC(e,t){return e?e.map(s=>{if(s&&s.type==="VariableNode"&&t.has(s.name)){let n=t.get(s.name);return n.ext===null?null:[n.min,n.ext]}return s&&s.type==="IntImmNode"?[s.value,1]:null}):null}function R2(e){let t=[],s=new Map,n=c(r=>{if(!(!r||typeof r!="object"))switch(r.type){case"ForNode":{let i=r.extent,o=i&&i.type==="IntImmNode"?i.value:typeof i=="number"?i:null,a=r.min,l=a&&a.type==="IntImmNode"?a.value:typeof a=="number"?a:0;r.loopVar&&s.set(r.loopVar.name,{min:l,ext:o}),n(r.body);return}case"BufferLoadNode":if(r.buffer&&t.push({name:r.buffer.name,kind:"read",ranges:XC(r.indices,s)}),r.indices)for(let i of r.indices)n(i);return;case"BufferStoreNode":if(r.buffer&&t.push({name:r.buffer.name,kind:"write",ranges:XC(r.indices,s)}),r.indices)for(let i of r.indices)n(i);n(r.value);return;case"MathOpNode":n(r.a),r.b&&n(r.b);return;case"CompareNode":n(r.a),n(r.b);return;case"CallExternNode":for(let i of r.args)n(i);return;case"IfThenElseNode":n(r.condition),n(r.thenBody),r.elseBody&&n(r.elseBody);return;case"CastNode":n(r.expr);return;case"LetStmtNode":n(r.value),n(r.body);return;case"SeqNode":for(let i of r.stmts)n(i);return;case"BlockNode":r.initBody&&n(r.initBody),n(r.body);return;default:return}},"walk");return n(e.body),e.initBody&&n(e.initBody),t}function ZC(e,t){if(!e||!t||e.length!==t.length)return!0;for(let s=0;s<e.length;s++){let n=e[s],r=t[s];if(!(n===null||r===null)&&(n[0]+n[1]<=r[0]||r[0]+r[1]<=n[0]))return!1}return!0}function B2(e,t,s){for(let n of e)for(let r of t)if(n.name===r.name&&!(n.kind==="read"&&r.kind==="read")&&!(s&&!s(n,r))&&ZC(n.ranges,r.ranges))return!0;return!1}function _s(e,t,s){if(!(!e||typeof e!="object"))switch(e.type){case"BufferLoadNode":if(e.buffer&&t.add(e.buffer.name),e.indices)for(let n of e.indices)_s(n,t,s);return;case"BufferStoreNode":if(e.buffer&&s.add(e.buffer.name),e.indices)for(let n of e.indices)_s(n,t,s);_s(e.value,t,s);return;case"MathOpNode":_s(e.a,t,s),e.b&&_s(e.b,t,s);return;case"CompareNode":_s(e.a,t,s),_s(e.b,t,s);return;case"CallExternNode":for(let n of e.args)_s(n,t,s);return;case"IfThenElseNode":_s(e.condition,t,s),_s(e.thenBody,t,s),e.elseBody&&_s(e.elseBody,t,s);return;case"CastNode":_s(e.expr,t,s);return;case"LetStmtNode":_s(e.value,t,s),_s(e.body,t,s);return;case"SeqNode":for(let n of e.stmts)_s(n,t,s);return;case"ForNode":_s(e.body,t,s);return;case"BlockNode":e.initBody&&_s(e.initBody,t,s),_s(e.body,t,s);return;default:return}}var eS,sS,Cg,tT=I(()=>{eS=Object.freeze({RAW:"read-after-write",WAR:"write-after-read",WAW:"write-after-write"}),sS=class{static{c(this,"Dependency")}constructor(t,s,n,r){this.src=t,this.dst=s,this.kind=n,this.bufferName=r}};c(N2,"collectBufferAccesses");c(XC,"indexRanges");c(R2,"collectRegions");c(ZC,"rangesOverlap");c(B2,"accessesConflict");c(_s,"visitExprTree");Cg=class{static{c(this,"DependencyAnalysis")}constructor(t){this._tree=t,this._blockAccesses=new Map;for(let s of t.allBlocks())this._blockAccesses.set(s.node.name,N2(s.node))}getReads(t){let s=this._blockAccesses.get(t);return s?[...s.reads]:[]}getWrites(t){let s=this._blockAccesses.get(t);return s?[...s.writes]:[]}computeDeps(t,s){let n=this._blockAccesses.get(t),r=this._blockAccesses.get(s);if(!n||!r)return[];let i=[],o=new Set;for(let a of n.accesses)for(let l of r.accesses){if(a.name!==l.name||!ZC(a.ranges,l.ranges))continue;let u=null;if(a.kind==="write"&&l.kind==="read"?u=eS.RAW:a.kind==="write"&&l.kind==="write"?u=eS.WAW:a.kind==="read"&&l.kind==="write"&&(u=eS.WAR),!u)continue;let f=`${u}:${a.name}`;o.has(f)||(o.add(f),i.push(new sS(t,s,u,a.name)))}return i}canReorder(t,s){let n=this._blockAccesses.get(t),r=this._blockAccesses.get(s);return!n||!r?!0:!B2(n.accesses,r.accesses)}}});var nS,rS,Tg,eT=I(()=>{tS();tT();Jt();nS=class{static{c(this,"LoopBinding")}constructor(t,s,n,r,i){this.loopSRef=t,this.iterVar=s,this.extent=n,this.kind=r,this.threadTag=i}},rS=class{static{c(this,"BlockBinding")}constructor(t,s,n,r){this.blockSRef=t,this.iterVars=s,this.readBuffers=n,this.writeBuffers=r}},Tg=class{static{c(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 Xc(this.primFunc),this._depAnalysis=new Cg(this._tree),this._loopBindings=new Map,this._blockBindings=new Map;for(let t of this._tree.allLoops()){let s=t.node,n=s.extent&&s.extent.type==="IntImmNode"?s.extent.value:null;this._loopBindings.set(s.loopVar.name,new nS(t,s.loopVar.name,n,s.kind,s.threadTag))}for(let t of this._tree.allBlocks()){let s=t.node,n=[];for(let o of s.iterVars)o.iterVar&&n.push(o.iterVar.name);let r=[];for(let o of s.reads)r.push(o.buffer.name);let i=[];for(let o of s.writes)i.push(o.buffer.name);this._blockBindings.set(s.name,new rS(t,n,r,i))}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 s=this.tree.getBlockSRef(t);if(!s)throw new Error(`Block '${t}' not found in schedule state`);return s}getLoopsOf(t){return this.tree.loopsOf(t)}getReads(t){return this.depAnalysis.getReads(t)}getWrites(t){return this.depAnalysis.getWrites(t)}getDeps(t,s){return this.depAnalysis.computeDeps(t,s)}canReorder(t,s){return this.depAnalysis.canReorder(t,s)}threadBindingSummary(){this._ensureBuilt();let t={};for(let[,s]of this._loopBindings)s.kind===V.THREAD_BINDING&&s.threadTag&&(t[s.threadTag]={varName:s.iterVar,extent:s.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 le(e,t,s){if(!e||typeof e!="object")return e;if(e.type==="VariableNode"&&e.name===t)return s();switch(e.type){case"MathOpNode":e.a=le(e.a,t,s),e.b&&(e.b=le(e.b,t,s));break;case"CompareNode":e.a=le(e.a,t,s),e.b=le(e.b,t,s);break;case"BufferLoadNode":for(let n=0;n<e.indices.length;n++)e.indices[n]=le(e.indices[n],t,s);break;case"BufferStoreNode":for(let n=0;n<e.indices.length;n++)e.indices[n]=le(e.indices[n],t,s);e.value=le(e.value,t,s);break;case"CallExternNode":for(let n=0;n<e.args.length;n++)e.args[n]=le(e.args[n],t,s);break;case"CastNode":e.expr=le(e.expr,t,s);break;case"IfThenElseNode":e.condition=le(e.condition,t,s),e.thenBody=le(e.thenBody,t,s),e.elseBody&&(e.elseBody=le(e.elseBody,t,s));break;case"ForNode":e.body=le(e.body,t,s);break;case"BlockNode":e.body=le(e.body,t,s),e.initBody&&(e.initBody=le(e.initBody,t,s));for(let n=0;n<e.iterVars.length;n++)e.iterVars[n].binding&&(e.iterVars[n].binding=le(e.iterVars[n].binding,t,s));break;case"SeqNode":for(let n=0;n<e.stmts.length;n++)e.stmts[n]=le(e.stmts[n],t,s);break;case"LetStmtNode":e.value=le(e.value,t,s),e.body=le(e.body,t,s);break;case"BlockRealizeNode":e.binding&&(e.binding=le(e.binding,t,s));break}return e}function Ns(e,t,s,n){if(!e||typeof e!="object")return e;if(e.type==="BufferLoadNode"&&e.buffer&&e.buffer.name===t)return n.n++,s(e);switch(e.type){case"MathOpNode":e.a=Ns(e.a,t,s,n),e.b&&(e.b=Ns(e.b,t,s,n));break;case"CompareNode":e.a=Ns(e.a,t,s,n),e.b=Ns(e.b,t,s,n);break;case"BufferLoadNode":for(let r=0;r<e.indices.length;r++)e.indices[r]=Ns(e.indices[r],t,s,n);break;case"BufferStoreNode":for(let r=0;r<e.indices.length;r++)e.indices[r]=Ns(e.indices[r],t,s,n);e.value=Ns(e.value,t,s,n);break;case"CallExternNode":for(let r=0;r<e.args.length;r++)e.args[r]=Ns(e.args[r],t,s,n);break;case"CastNode":e.expr=Ns(e.expr,t,s,n);break;case"IfThenElseNode":e.condition=Ns(e.condition,t,s,n),e.thenBody=Ns(e.thenBody,t,s,n),e.elseBody&&(e.elseBody=Ns(e.elseBody,t,s,n));break;case"ForNode":e.body=Ns(e.body,t,s,n);break;case"BlockNode":e.body=Ns(e.body,t,s,n),e.initBody&&(e.initBody=Ns(e.initBody,t,s,n));break;case"SeqNode":for(let r=0;r<e.stmts.length;r++)e.stmts[r]=Ns(e.stmts[r],t,s,n);break;case"LetStmtNode":e.value=Ns(e.value,t,s,n),e.body=Ns(e.body,t,s,n);break}return e}function tu(e,t){if(!e||typeof e!="object")return!1;if(e.type==="BufferLoadNode"&&e.buffer&&e.buffer.name===t)return!0;for(let s of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])if(e[s]&&tu(e[s],t))return!0;if(e.args){for(let s of e.args)if(tu(s,t))return!0}if(e.indices){for(let s of e.indices)if(tu(s,t))return!0}if(e.stmts){for(let s of e.stmts)if(tu(s,t))return!0}return!1}function Ot(e){if(!e||typeof e!="object")return e;if(Array.isArray(e))return e.map(Ot);let t=Object.create(Object.getPrototypeOf(e));switch(t.type=e.type,t._parent=null,t._parentKey=null,t._parentIdx=-1,e.type){case"ForNode":t.loopVar=e.loopVar,t.min=Ot(e.min),t.extent=Ot(e.extent),t.kind=e.kind,t.body=Ot(e.body),t.threadTag=e.threadTag,t._setChild("body",t.body);break;case"BlockNode":t.name=e.name,t.iterVars=e.iterVars.map(Ot),t.reads=e.reads,t.writes=e.writes,t.body=Ot(e.body),t.initBody=e.initBody?Ot(e.initBody):null,t._setChild("body",t.body),t._setChild("initBody",t.initBody);break;case"SeqNode":t.stmts=e.stmts.map(Ot),t._setChildren("stmts",t.stmts);break;case"IfThenElseNode":t.condition=Ot(e.condition),t.thenBody=Ot(e.thenBody),t.elseBody=e.elseBody?Ot(e.elseBody):null,t._setChild("thenBody",t.thenBody),t._setChild("elseBody",t.elseBody);break;case"BufferStoreNode":t.buffer=e.buffer,t.indices=e.indices.map(Ot),t.value=Ot(e.value);break;case"BufferLoadNode":t.buffer=e.buffer,t.indices=e.indices.map(Ot);break;case"BlockRealizeNode":t.iterVar=e.iterVar,t.binding=Ot(e.binding);break;case"MathOpNode":t.op=e.op,t.a=Ot(e.a),t.b=Ot(e.b);break;case"CompareNode":t.direction=e.direction,t.a=Ot(e.a),t.b=Ot(e.b);break;case"CastNode":t.expr=Ot(e.expr),t.fromDtype=e.fromDtype,t.toDtype=e.toDtype;break;case"CallExternNode":t.externName=e.externName,t.args=e.args.map(Ot),t.dtype=e.dtype;break;default:for(let s of Object.keys(e))s==="_parent"||s==="_parentKey"||s==="_parentIdx"||(t[s]=e[s]);break}return t}function Bl(e){return e.type==="IntImmNode"?e.value:null}function Zc(e,t="int32"){return new Ae(`${e}_${T2++}`,t)}var C2,T2,En,np=I(()=>{Jt();Xa();QC();Zx();eT();tS();Xx();C2=new Set(["+","*","min","max"]);c(le,"substituteVar");c(Ns,"replaceBufferLoads");c(tu,"loadsBuffer");c(Ot,"cloneExprTree");c(Bl,"getConstExtent");T2=0;c(Zc,"freshVar");En=class{static{c(this,"Schedule")}constructor(t){this.func=t,this.trace=new Rg,this.state=new Tg(t),this._replaying=!1,this._srefTree=new Xc(t)}_rebuildSRefTree(){this._srefTree.rebuildFrom(this.func.body),this.state.invalidate()}getBlock(t){let s=this._srefTree.getBlockSRef(t);if(!s)throw new Error(`Block '${t}' not found`);return s.node}getLoops(t){return this._srefTree.loopsOf(t).map(s=>s.node)}_resolveLoop(t){if(typeof t!="string")return t;let s=null,n=c(r=>{if(!(!r||typeof r!="object"||s)){if(r.type==="ForNode"&&r.loopVar&&r.loopVar.name===t){s=r;return}if(r.body&&n(r.body),r.initBody&&n(r.initBody),r.stmts)for(let i of r.stmts)n(i);r.thenBody&&n(r.thenBody),r.elseBody&&n(r.elseBody)}},"walk");return n(this.func.body),s||t}split(t,s){t=this._resolveLoop(t);let n=Bl(t.extent);if(n===null)throw new Error(`Cannot split loop '${t.loopVar.name}' with non-constant extent`);if(s<=0||!Number.isInteger(s))throw new Error(`Split factor must be a positive integer, got ${s}`);let r=Math.ceil(n/s),i=Zc(`${t.loopVar.name}_o`),o=Zc(`${t.loopVar.name}_i`),a=t.loopVar.name,l=Ot(t.body),u=new wt(o,new M(0),new M(s),t.kind,l,t.threadTag);if(n%s!==0){let p=new L("+",new L("*",i,new M(s)),o),d=new L("<",p,new M(n)),m=new Ht(d,u.body);u.body=m,u._setChild("body",m)}let h=new wt(i,new M(0),new M(r),t.kind,u,t.threadTag);return le(u.body,a,()=>new L("+",new L("*",i,new M(s)),o)),this._replaceNode(t,h),this._rebuildSRefTree(),this._replaying||this.trace.record("split",[t.loopVar.name,s]),[h,u]}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 s=new Set(t),n=null,r=1/0,i=new Map,o=c((p,d)=>{if(p){if(p.type==="ForNode"){s.has(p)&&(i.set(p,d),d<r&&(r=d,n=p)),o(p.body,d+1);return}if(p.type==="SeqNode")for(let m of p.stmts)o(m,d);p.type==="BlockNode"&&o(p.body,d)}},"findDepths");o(this.func.body,0);let a=[...t].sort((p,d)=>i.get(p)-i.get(d)),u=a[a.length-1].body,f=this._srefTree.getSRef(n),h=f?f.parent:null;this._replaceNode(n,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]:u;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,s){if(t=this._resolveLoop(t),s=this._resolveLoop(s),t.type!=="ForNode"||s.type!=="ForNode")throw new Error("fuseLoops expects two ForNode arguments");if(t.body!==s)throw new Error("fuseLoops requires inner loop to be direct child of outer loop");let n=Bl(t.extent),r=Bl(s.extent);if(n===null||r===null)throw new Error("Cannot fuse loops with non-constant extents");let i=n*r,o=Zc(`${t.loopVar.name}_${s.loopVar.name}_fused`),a=t.loopVar.name,l=s.loopVar.name,u=new wt(o,new M(0),new M(i),t.kind,s.body);return le(u.body,a,()=>new L("/",o,new M(r))),le(u.body,l,()=>new L("%",o,new M(r))),this._replaceNode(t,u),this._srefTree.replaceNode(t,u),this.state.invalidate(),this._replaying||this.trace.record("fuseLoops",[a,l]),u}tile(t,s,n){if(s.length!==n.length)throw new Error("tile: loopIndices and tileSizes must have same length");let r=this.getLoops(t),i=s.map(h=>{if(h>=r.length)throw new Error(`tile: loop index ${h} out of range`);return r[h]}),o=[],a=[];for(let h=0;h<i.length;h++){let d=this.getLoops(t).find(y=>y.loopVar.name===i[h].loopVar.name||y===i[h]);if(!d)throw new Error(`tile: lost track of loop at index ${h}`);let[m,g]=this.split(d,n[h]);o.push(m),a.push(g)}let l=this.getLoops(t),u=[],f=[];for(let h of l)o.some(p=>p.loopVar.name===h.loopVar.name)?u.push(h):a.some(p=>p.loopVar.name===h.loopVar.name)&&f.push(h);return u.length>0&&f.length>0&&this.reorder(...u,...f),{outerLoops:u,innerLoops:f}}vectorize(t){if(t=this._resolveLoop(t),t.type!=="ForNode")throw new Error("vectorize expects ForNode");if(Bl(t.extent)===null)throw new Error("Cannot vectorize loop with non-constant extent");let n=Qx(t);if(n!==null)throw new Error(`Cannot vectorize reduction loop '${t.loopVar.name}' (loop-carried dependency in block '${n}')`);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 s=Qx(t);if(s!==null)throw new Error(`Cannot parallelize reduction loop '${t.loopVar.name}' (loop-carried dependency in block '${s}')`);t.kind=V.PARALLEL,this.state.invalidate(),this._replaying||this.trace.record("parallelize",[t.loopVar.name])}bindThread(t,s){if(t=this._resolveLoop(t),t.type!=="ForNode")throw new Error("bindThread expects ForNode");let n=["blockIdx.x","blockIdx.y","blockIdx.z","threadIdx.x","threadIdx.y","threadIdx.z"];if(!n.includes(s))throw new Error(`Invalid thread tag: ${s}. Must be one of: ${n.join(", ")}`);t.kind=V.THREAD_BINDING,t.threadTag=s,this.state.invalidate(),this._replaying||this.trace.record("bindThread",[t.loopVar.name,s])}rfactor(t,s,n){let r=this.getBlock(t),i=this.getLoops(t),o=i.find(F=>F.loopVar.name===s);if(!o)throw new Error(`rfactor: reduction loop '${s}' not found for block '${t}'`);let a=Bl(o.extent);if(a===null)throw new Error(`rfactor: reduction loop '${s}' has non-constant extent`);if(!Number.isInteger(n)||n<=1||n>=a||a%n!==0)throw new Error(`rfactor: factor ${n} must divide reduction extent ${a} with 1 < factor < ${a}`);let l=r.body;if(!l||l.type!=="BufferStoreNode"||!l.value||l.value.type!=="MathOpNode")throw new Error(`rfactor: block '${t}' body is not a single accumulating store`);let u=l.buffer,f=l.indices,h=l.value.op,p=c(F=>F&&F.type==="BufferLoadNode"&&F.buffer===u,"isAccLoad"),d;if(p(l.value.a))d=l.value.b;else if(p(l.value.b))d=l.value.a;else throw new Error(`rfactor: accumulator load not found in block '${t}' body`);if(!C2.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 M(0),g=i.filter(F=>F.loopVar.name!==s),y=a/n,_=new ee(`${u.name}_rf`,[n,...u.shape],u.dtype,u.scope),w=Zc(`${s}_rfi`),b=Zc(`${s}_rfo`),x=Zc(`${s}_rfp`),A=c(F=>[F,...f.map(Ot)],"cfIdx"),S=le(Ot(d),s,()=>new L("+",new L("*",b,new M(n)),w)),E=new Z(_,A(w),new L(h,new H(_,A(w)),S)),R=new Z(_,A(w),Ot(m)),v=new st(`${t}_rf_p`,[],r.reads.map(F=>({buffer:F.buffer})),[{buffer:_}],E,R),k=new wt(b,new M(0),new M(y),V.SERIAL,v);k=new wt(w,new M(0),new M(n),V.SERIAL,k);for(let F=g.length-1;F>=0;F--)k=new wt(g[F].loopVar,new M(0),Ot(g[F].extent),V.SERIAL,k);let B=new Z(u,f.map(Ot),new L(h,new H(u,f.map(Ot)),new H(_,A(x)))),N=new Z(u,f.map(Ot),Ot(m)),C=new st(`${t}_rf_c`,[],[{buffer:_}],[{buffer:u}],B,N),O=new wt(x,new M(0),new M(n),V.SERIAL,C);for(let F=g.length-1;F>=0;F--)O=new wt(g[F].loopVar,new M(0),Ot(g[F].extent),V.SERIAL,O);return this._replaceNode(i[0],new yt([k,O])),this._rebuildSRefTree(),this._replaying||this.trace.record("rfactor",[t,s,n]),_}decomposeReduction(t){let s=this.getBlock(t);if(!s.initBody)throw new Error(`decomposeReduction: block '${t}' has no initBody`);let n=this.getLoops(t),r=s.body;if(!r||r.type!=="BufferStoreNode")throw new Error(`decomposeReduction: block '${t}' body is not a store`);let i=r.buffer,o=new Set;for(let m of r.indices)ge(m,o);let a=n.filter(m=>o.has(m.loopVar.name));if(n.filter(m=>!o.has(m.loopVar.name)).length===0)throw new Error(`decomposeReduction: block '${t}' has no reduction loop`);let u=new Z(i,r.indices.map(Ot),Ot(s.initBody.value)),h=new st(`${t}_init`,[],[],[{buffer:i}],u);for(let m=a.length-1;m>=0;m--)h=new wt(a[m].loopVar,new M(0),Ot(a[m].extent),V.SERIAL,h);let d=new st(`${t}_upd`,[],s.reads.map(m=>({buffer:m.buffer})),[{buffer:i}],Ot(r));for(let m=n.length-1;m>=0;m--)d=new wt(n[m].loopVar,new M(0),Ot(n[m].extent),V.SERIAL,d);this._replaceNode(n[0],new yt([h,d])),this._rebuildSRefTree(),this._replaying||this.trace.record("decomposeReduction",[t])}_redirectReads(t,s,n){if(!(!t||typeof t!="object")){t.type==="BufferLoadNode"&&t.buffer===s&&(t.buffer=n);for(let r of["a","b","expr","value","condition","thenBody","elseBody","body","initBody","min","extent"])t[r]&&this._redirectReads(t[r],s,n);if(Array.isArray(t.stmts))for(let r of t.stmts)this._redirectReads(r,s,n);if(Array.isArray(t.indices))for(let r of t.indices)this._redirectReads(r,s,n);if(Array.isArray(t.args))for(let r of t.args)this._redirectReads(r,s,n)}}_redirectBuffer(t,s,n){if(!(!t||typeof t!="object")){(t.type==="BufferLoadNode"||t.type==="BufferStoreNode")&&t.buffer===s&&(t.buffer=n);for(let r of["a","b","expr","value","condition","thenBody","elseBody","body","initBody","min","extent"])t[r]&&this._redirectBuffer(t[r],s,n);if(Array.isArray(t.stmts))for(let r of t.stmts)this._redirectBuffer(r,s,n);if(Array.isArray(t.indices))for(let r of t.indices)this._redirectBuffer(r,s,n);if(Array.isArray(t.args))for(let r of t.args)this._redirectBuffer(r,s,n)}}cacheWrite(t,s,n="local"){let r=this.getBlock(t),i=this.getLoops(t);if(i.length===0)throw new Error("cacheWrite: block has no enclosing loops");let o=(r.writes||[]).find(y=>y.buffer&&y.buffer.name===s);if(!o)throw new Error(`cacheWrite: block '${t}' does not write '${s}'`);let a=o.buffer,l=new ee(`${s}_${t}_cachew`,[...a.shape],a.dtype,n);this._redirectBuffer(r.body,a,l),r.initBody&&this._redirectBuffer(r.initBody,a,l),o.buffer=l;let u=a.shape.map((y,_)=>new Ae(`${l.name}_o${_}`,"int32")),f=new Z(a,u,new H(l,u)),p=new st(`${l.name}_flush`,u.map(y=>({iterVar:y,binding:y})),[{buffer:l}],[{buffer:a}],f);for(let y=a.shape.length-1;y>=0;y--)p=new wt(u[y],new M(0),new M(a.shape[y]),V.SERIAL,p);let d=i[0],m=new yt([]),g=new Hs(l,n,m);this._replaceNode(d,g),m.stmts.push(d,p),this._rebuildSRefTree(),this._replaying||this.trace.record("cacheWrite",[t,s,n])}setScope(t,s,n){let i=(this.getBlock(t).writes||[]).find(o=>o.buffer&&o.buffer.name===s);if(!i)throw new Error(`setScope: block '${t}' does not write '${s}'`);i.buffer.scope=n,this.state.invalidate(),this._replaying||this.trace.record("setScope",[t,s,n])}storageAlign(t,s,n,r,i){let o=this.getBlock(t),a=[...o.writes||[],...o.reads||[]].find(l=>l.buffer&&l.buffer.name===s);if(!a)throw new Error(`storageAlign: block '${t}' does not access '${s}'`);if(!Number.isInteger(r)||r<=0)throw new Error("storageAlign: factor must be a positive integer");a.buffer.storageAlign={axis:n,factor:r,offset:i||0},this.state.invalidate(),this._replaying||this.trace.record("storageAlign",[t,s,n,r,i||0])}computeInline(t){let s=this.getBlock(t);if(!s)throw new Error(`computeInline: block '${t}' not found`);if(s.initBody)throw new Error("computeInline: cannot inline a reduction block (has init)");let n=null,r=c(m=>{if(!(!m||n)){if(m.type==="BufferStoreNode"){n=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(s.body),!n)throw new Error("computeInline: producer has no single store to inline");let i=n.buffer,o=n.indices.map(m=>m&&m.type==="VariableNode"?m.name:null);if(o.some(m=>m===null))throw new Error("computeInline: producer indices must be simple loop variables");if(tu(n.value,i.name))throw new Error("computeInline: producer is self-referential (recurrence), cannot inline");let a=c(m=>{if(!m||typeof m!="object")return!1;if((m.type==="BufferLoadNode"||m.type==="BufferStoreNode")&&m.indices){for(let g of m.indices)if(tu(g,i.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 '${i.name}' is used inside an index expression (indirect), cannot safely inline`);let l=n.value,u={n:0},f=o.map((m,g)=>`__inl_${t}_${g}`),h=c(m=>{let g=Ot(l);for(let y=0;y<o.length;y++){let _=f[y];g=le(g,o[y],()=>new Ae(_,"int32"))}for(let y=0;y<o.length;y++){let _=m.indices[y];g=le(g,f[y],()=>Ot(_))}return g},"makeRepl");if(Ns(this.func.body,i.name,h,u),u.n===0)throw new Error(`computeInline: buffer '${i.name}' has no consumers to inline into`);let p=this.getLoops(t),d=p.length>0?p[0]:s;this._replaceNode(d,new yt([])),this._rebuildSRefTree(),this._replaying||this.trace.record("computeInline",[t])}_relocateBlockToLoop(t,s,n){if(!this.getBlock(t))throw new Error(`computeAt: block '${t}' not found`);let i=this._resolveLoop(s);if(!i||i.type!=="ForNode")throw new Error("computeAt: target must be a loop");let o=this.getLoops(t);if(o.length!==1)throw new Error("computeAt: aligned case requires exactly one enclosing loop on the moved block");let a=o[0];if(a===i)throw new Error("computeAt: block already at target loop");let l=Bl(a.extent),u=Bl(i.extent);if(l===null||u===null||l!==u)throw new Error("computeAt: aligned case requires equal static extent on the block and target loops");let f=Ot(a.body);le(f,a.loopVar.name,()=>i.loopVar),this._replaceNode(a,new yt([]));let h=i.body;if(h&&h.type==="SeqNode")n?h.stmts.unshift(f):h.stmts.push(f);else{let p=n?new yt([f,h]):new yt([h,f]);i.body=p,i._setChild("body",p)}return this._rebuildSRefTree(),i.loopVar.name}computeAt(t,s){let n=this._relocateBlockToLoop(t,s,!0);this._replaying||this.trace.record("computeAt",[t,n])}reverseComputeAt(t,s){let n=this._relocateBlockToLoop(t,s,!1);this._replaying||this.trace.record("reverseComputeAt",[t,n])}cacheRead(t,s,n="local"){let r=this.getBlock(t),i=this.getLoops(t);if(i.length===0)throw new Error("cacheRead: block has no enclosing loops");let o=(r.reads||[]).find(y=>y.buffer&&y.buffer.name===s);if(!o)throw new Error(`cacheRead: block '${t}' does not read '${s}'`);let a=o.buffer,l=new ee(`${s}_${t}_cache`,[...a.shape],a.dtype,n),u=a.shape.map((y,_)=>new Ae(`${l.name}_i${_}`,"int32")),f=new Z(l,u,new H(a,u)),p=new st(`${l.name}_fill`,u.map(y=>({iterVar:y,binding:y})),[{buffer:a}],[{buffer:l}],f);for(let y=a.shape.length-1;y>=0;y--)p=new wt(u[y],new M(0),new M(a.shape[y]),V.SERIAL,p);this._redirectReads(r.body,a,l),r.initBody&&this._redirectReads(r.initBody,a,l),o.buffer=l;let d=i[0],m=new yt([p]),g=new Hs(l,n,m);this._replaceNode(d,g),m.stmts.push(d),this._rebuildSRefTree(),this._replaying||this.trace.record("cacheRead",[t,s,n])}fuseConsumer(t,s){let n=this.getBlock(t),r=this.getBlock(s),i=this.getLoops(t),o=this.getLoops(s);if(!n.body||n.body.type!=="BufferStoreNode")throw new Error(`fuseConsumer: producer '${t}' body is not a store`);let a=new Set;for(let m of n.body.indices)ge(m,a);let l=i.filter(m=>a.has(m.loopVar.name));if(l.length===0||o.length!==l.length)throw new Error(`fuseConsumer: producer/consumer spatial rank mismatch (${l.length} vs ${o.length})`);if(!o[0]._parent||o[0]._parent.type!=="SeqNode")throw new Error("fuseConsumer: consumer loop nest is not a direct SeqNode sibling; cannot fuse without duplicating it");let u=l[l.length-1],f=Ot(u.body),h=Ot(r.body);for(let m=0;m<o.length;m++){let g=l[m].loopVar.name;h=le(h,o[m].loopVar.name,()=>new Ae(g,"int32"))}let p=new st(`${s}_fused`,[],r.reads.map(m=>({buffer:m.buffer})),r.writes.map(m=>({buffer:m.buffer})),h),d=new yt([f,p]);for(let m=l.length-1;m>=0;m--){let g=l[m];d=new wt(g.loopVar,new M(0),Ot(g.extent),g.kind,d,g.threadTag)}this._replaceNode(i[0],d),this._removeNode(o[0]),this._rebuildSRefTree(),this._replaying||this.trace.record("fuseConsumer",[t,s])}annotate(t,s,n){if(t=this._resolveLoop(t),t.type!=="ForNode")throw new Error("annotate expects ForNode");t.annotations||(t.annotations={}),t.annotations[s]=n,this.state.invalidate(),this._replaying||this.trace.record("annotate",[t.loopVar.name,s,n])}getTrace(){return this.trace}verify(){return Qc.validate(this.func)}_replaceNode(t,s){if(t._parent){t.replaceWith(s);return}(this.func.body===t||this.func.body===void 0)&&(this.func.body=s,this.func._setChild&&this.func._setChild("body",s))}_removeNode(t){let s=t._parent;if(s&&s.type==="SeqNode"&&Array.isArray(s.stmts)){let n=s.stmts.indexOf(t);if(n>=0){s.stmts.splice(n,1),s._setChildren&&s._setChildren("stmts",s.stmts);return}}throw new Error("_removeNode: node parent is not a SeqNode; cannot remove without duplicating it")}}});function Me(e,t){let s=iS.get(e);return s||(s=new Map,I2(e.body,s,[]),iS.set(e,s)),s.get(t)||null}function sT(e){e&&iS.delete(e)}function nT(e,t){let s=[e];for(;s.length>0;){let n=s.pop();if(!(!n||typeof n!="object")){if(n.type==="VariableNode"){t.add(n.name);continue}for(let r of["a","b","condition","thenBody","elseBody","expr","value","offsetExpr","extent"])n[r]&&s.push(n[r]);if(n.indices)for(let r of n.indices)s.push(r);if(n.args)for(let r of n.args)s.push(r)}}}function rT(e){let t=new Set((e.writes||[]).map(i=>i.buffer&&i.buffer.name)),s=new Set,n=[e.body,e.initBody];for(;n.length>0;){let i=n.pop();if(i){if(i.type==="BufferStoreNode"&&i.buffer&&t.has(i.buffer.name))for(let o of i.indices)nT(o,s);if(i.body&&n.push(i.body),i.stmts)for(let o of i.stmts)n.push(o);i.thenBody&&n.push(i.thenBody),i.elseBody&&n.push(i.elseBody),i.value&&n.push(i.value)}}let r=new Set;for(let i of e.iterVars||[]){let o=new Set;if(nT(i.binding,o),!(i.iterVar&&s.has(i.iterVar.name)||[...o].some(l=>s.has(l))))for(let l of o)r.add(l)}return r}function I2(e,t,s){let n=[{node:e,loops:[...s]}];for(;n.length>0;){let{node:r,loops:i}=n.pop();if(r)if(r.type==="ForNode")n.push({node:r.body,loops:[...i,r]});else if(r.type==="BlockNode"){let o=rT(r);t.set(r.name,{loopCount:i.length,hasReduction:r.initBody!==null||o.size>0,reductionLoopVars:o,readBuffers:r.reads.map(a=>a.buffer.name),writeBuffers:r.writes.map(a=>a.buffer.name),loops:[...i]}),n.push({node:r.body,loops:i})}else if(r.type==="SeqNode")for(let o=r.stmts.length-1;o>=0;o--)n.push({node:r.stmts[o],loops:i});else r.type==="IfThenElseNode"?(r.elseBody&&n.push({node:r.elseBody,loops:i}),n.push({node:r.thenBody,loops:i})):r.type==="AllocateNode"?n.push({node:r.body,loops:i}):r.type==="LetStmtNode"&&n.push({node:r.body,loops:i})}}function iT(e){let t=0,s=[e.body];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="BlockNode"&&(t++,t>1))return!0;if(n.type==="ForNode"&&s.push(n.body),n.type==="SeqNode")for(let r of n.stmts)s.push(r)}}return!1}function mS(e){let t=[e.body];for(;t.length>0;){let s=t.pop();if(s){if(s.type==="BlockNode"&&(s.initBody!==null||rT(s).size>0))return!0;if(s.body&&t.push(s.body),s.stmts)for(let n of s.stmts)t.push(n);s.thenBody&&t.push(s.thenBody),s.elseBody&&t.push(s.elseBody),s.initBody&&t.push(s.initBody)}}return!1}function O2(e){let t=[e.body];for(;t.length>0;){let s=t.pop();if(s){if(s.type==="ForNode"&&s.kind===V.RECURRENCE)return!0;if(s.body&&t.push(s.body),s.stmts)for(let n of s.stmts)t.push(n);s.thenBody&&t.push(s.thenBody),s.elseBody&&t.push(s.elseBody),s.initBody&&t.push(s.initBody)}}return!1}function gS(e,t,s){let n=Math.min(s.maxThreadsPerBlock,256),r=t.extent;if(r.type==="IntImmNode"&&r.value>n){let[i,o]=e.split(t,n);e.bindThread(o,"threadIdx.x"),O2(e.func)||e.bindThread(i,"blockIdx.x")}else e.bindThread(t,"threadIdx.x")}function eu(e,t){return t.reductionLoopVars?t.reductionLoopVars.has(e.loopVar.name):!1}function yS(e,t){return e.body===t}function M2(e){let t=[],s=[e];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="BlockNode"&&t.push(n.name),n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),n.initBody&&s.push(n.initBody)}}return t}var Jr,iS,oS,aS,lS,cS,uS,fS,hS,pS,dS,lo,Wo=I(()=>{hi();Jt();Jr=class{static{c(this,"ScheduleRule")}constructor(t){this.name=t}matches(t,s,n){throw new Error("ScheduleRule.matches must be implemented")}apply(t,s,n){throw new Error("ScheduleRule.apply must be implemented")}},iS=new WeakMap;c(Me,"classifyBlock");c(sT,"invalidateClassifyCache");c(nT,"collectVarNames");c(rT,"computeReductionLoopVars");c(I2,"collectBlockInfo");c(iT,"hasMultipleBlocks");oS=class extends Jr{static{c(this,"ElementwiseCPURule")}constructor(){super("elementwise_cpu")}matches(t,s,n){if(n.kind!==Te.CPU)return!1;let r=Me(t,s);if(!r||r.hasReduction||r.loopCount<1||r.loops.length>0&&iT(r.loops[0]))return!1;let i=1;for(let o of r.loops){let a=o.extent&&o.extent.type==="IntImmNode"?o.extent.value:1;i*=a}return i>=n.numCores*n.vectorWidth}apply(t,s,n){let r=t.getLoops(s);if(r.length===0)return;if(r.length===1){let a=r[0].extent;if(a.type==="IntImmNode"&&a.value>=n.vectorWidth*2){let[l,u]=t.split(r[0],n.vectorWidth);t.parallelize(l),t.vectorize(u);return}t.parallelize(r[0]);return}t.parallelize(r[0]);let i=r[r.length-1],o=i.extent;if(o.type==="IntImmNode"&&o.value>=n.vectorWidth&&o.value%n.vectorWidth===0){let[a,l]=t.split(i,n.vectorWidth);t.vectorize(l)}}};c(mS,"primFuncHasReduction");c(O2,"primFuncHasRecurrence");c(gS,"bindFusedSpatialGPU");aS=class extends Jr{static{c(this,"ElementwiseGPURule")}constructor(){super("elementwise_gpu")}matches(t,s,n){if(!n.isGPU())return!1;let r=Me(t,s);return r?!r.hasReduction&&r.loopCount>=1:!1}apply(t,s,n){let r=t.getLoops(s);if(r.length===0)return;let i=r[0];for(let u=1;u<r.length;u++){let h=t.getLoops(s).find(p=>p.loopVar.name===r[u].loopVar.name);h&&yS(i,h)&&(i=t.fuseLoops(i,h))}let o=i.extent;if(o.type!=="IntImmNode"){t.bindThread(i,"threadIdx.x");return}let a=o.value,l=Math.min(n.maxThreadsPerBlock,1024);if(mS(t.func)&&a<=l){t.bindThread(i,"threadIdx.x");return}gS(t,i,n)}},lS=class extends Jr{static{c(this,"ReductionCPURule")}constructor(){super("reduction_cpu")}matches(t,s,n){if(n.kind!==Te.CPU)return!1;let r=Me(t,s);return r?r.hasReduction:!1}apply(t,s,n){let r=t.getLoops(s);if(r.length===0)return;let i=[],o=[],a=Me(t.func,s);for(let l of r)a&&eu(l,a)?o.push(l):i.push(l);i.length>0&&t.parallelize(i[0])}},cS=class extends Jr{static{c(this,"ReductionGPURule")}constructor(){super("reduction_gpu")}matches(t,s,n){if(!n.isGPU())return!1;let r=Me(t,s);return r?r.hasReduction:!1}apply(t,s,n){let r=t.getLoops(s);if(r.length===0)return;let i=[],o=Me(t.func,s);for(let l of r)(!o||!eu(l,o))&&i.push(l);if(i.length===0)return;let a=i[0];for(let l=1;l<i.length;l++){let f=t.getLoops(s).find(h=>h.loopVar.name===i[l].loopVar.name);f&&yS(a,f)&&(a=t.fuseLoops(a,f))}gS(t,a,n)}},uS=class extends Jr{static{c(this,"MatmulTiledCPURule")}constructor(){super("matmul_tiled_cpu")}matches(t,s,n){if(n.kind!==Te.CPU)return!1;let r=Me(t,s);if(!r||!r.hasReduction||!s.includes("matmul")||r.loopCount<3)return!1;let i=n.l1CacheBytes||32768,o=Math.max(8,Math.min(64,Math.floor(Math.sqrt(i/4))));return r.loops.reduce((l,u)=>{let f=u.extent&&u.extent.type==="IntImmNode"?u.extent.value:0;return f>l?f:l},0)>=o}apply(t,s,n){let r=t.getLoops(s);if(r.length<3)return;let i=n.l1CacheBytes||32768,o=Math.max(8,Math.min(64,Math.floor(Math.sqrt(i/4)))),a=[],l=[];for(let f=0;f<Math.min(2,r.length);f++){let h=r[f].extent.type==="IntImmNode"?r[f].extent.value:null;h&&h>=o&&(a.push(f),l.push(o))}if(a.length===0)return;let{outerLoops:u}=t.tile(s,a,l);u.length>0&&t.parallelize(u[0])}},fS=class extends Jr{static{c(this,"MatmulTiledGPURule")}constructor(){super("matmul_tiled_gpu")}matches(t,s,n){if(!n.isGPU())return!1;let r=Me(t,s);if(!r||!r.hasReduction||!s.includes("matmul")||r.loopCount<3)return!1;let i=n.sharedMemoryBytes||49152,a=Math.max(16,Math.min(128,Math.floor(Math.sqrt(i/8))));return r.loops.reduce((u,f)=>{let h=f.extent&&f.extent.type==="IntImmNode"?f.extent.value:0;return h>u?h:u},0)>=a}apply(t,s,n){let r=t.getLoops(s);if(r.length<3)return;let i=Me(t.func,s),o=r.filter(l=>!i||!eu(l,i));if(o.length===0)return;let a=o[0];for(let l=1;l<o.length;l++){let f=t.getLoops(s).find(h=>h.loopVar.name===o[l].loopVar.name);f&&yS(a,f)&&(a=t.fuseLoops(a,f))}gS(t,a,n)}},hS=class extends Jr{static{c(this,"ElementwiseWasmRule")}constructor(){super("elementwise_wasm")}matches(t,s,n){if(n.kind!==Te.WASM)return!1;let r=Me(t,s);return!(!r||r.hasReduction||r.loopCount<1||r.loops.length>0&&iT(r.loops[0]))}apply(t,s,n){let r=t.getLoops(s);if(r.length===0)return;let i=n.numCores||1,o=n.vectorWidth||4;if(i>1&&r.length>=1){let u=r[0].extent,f=u&&u.type==="IntImmNode"?u.value:0;if(f>=i*4){if(r.length===1&&n.supportsSimd&&n.supportsSimd()&&f>=o*2&&f%o===0){let[h,p]=t.split(r[0],o);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>=o&&p.value%o===0){let[,d]=t.split(h,o);t.vectorize(d)}}return}}let a=r[r.length-1],l=a.extent;if(l.type==="IntImmNode"&&l.value>=o*2){let[u,f]=t.split(a,o);t.vectorize(f);return}l.type==="IntImmNode"&&l.value>=o&&t.vectorize(a)}},pS=class extends Jr{static{c(this,"ReductionWasmRule")}constructor(){super("reduction_wasm")}matches(t,s,n){if(n.kind!==Te.WASM)return!1;let r=n.numCores>1,i=n.supportsSimd&&n.supportsSimd();if(!r&&!i)return!1;let o=Me(t,s);return o?o.hasReduction&&o.loopCount>=2:!1}apply(t,s,n){let r=t.getLoops(s);if(r.length<2)return;let i=Me(t.func,s),o=[],a=[];for(let l of r)!i||!eu(l,i)?o.push(l):a.push(l);if(o.length>0){let l=o[0].extent;(l&&l.type==="IntImmNode"?l.value:0)>=(n.numCores||1)*4&&t.parallelize(o[0])}if(n.supportsSimd&&n.supportsSimd()&&a.length>0){let l=a[a.length-1];(l.extent&&l.extent.type==="IntImmNode"?l.extent.value:0)>=n.vectorWidth*2&&t.vectorize(l)}}},dS=class extends Jr{static{c(this,"FallbackRule")}constructor(){super("fallback")}matches(){return!0}apply(t,s,n){let r=t.getLoops(s);r.length!==0&&n.isCPU()&&r.length>=1&&t.parallelize(r[0])}};c(eu,"isReductionLoop");c(yS,"findDirectChild");lo=class e{static{c(this,"SchedulePolicy")}constructor(t,s=null,n=null){this.target=t,this.rules=s||e.defaultRules(),this.trace=n}static defaultRules(){return[new uS,new fS,new lS,new cS,new pS,new oS,new aS,new hS,new dS]}selectRule(t,s){for(let n of this.rules)if(n.matches(t,s,this.target))return n;return null}applyToBlock(t,s){let n=this.selectRule(t.func,s);return n?(n.apply(t,s,this.target),sT(t.func),this._explain(s,n.name,`matched rule '${n.name}' for ${this.target.name}`),n.name):(this._explain(s,"none","no schedule rule matched; runs sequentially"),null)}_explain(t,s,n){this.trace&&this.trace.explainsEnabled&&this.trace.explain("schedule",t,s,n,{target:this.target.name})}applyToAllBlocks(t){sT(t.func);let s=M2(t.func.body),n=new Set,r=new Map;for(let i of s){if(n.has(i))continue;n.add(i);let o=this.applyToBlock(t,i);o&&r.set(i,o)}return r}};c(M2,"collectAllBlockNames")});function L2(e,t,s,n){let r=e;for(let i=0;i<t.length;i++)r+="|"+t[i].shape.join(",")+":"+t[i].dtype;if(s)for(let[i,o]of Object.entries(s))r+="|"+i+"="+JSON.stringify(o);return r+="|"+n.name,r}function P2(e){let t=aT.get(e);return t||(t=new Kc("jit_"+e),aT.set(e,t)),t}function F2(e){let t=1;for(let s of e.shape)t*=typeof s=="number"&&s>0?s:1;return Math.max(t,1)}function $2(e,t,s){let n=[];for(let[,r]of s.bufferMap)n.push(new(me(r.dtype))(F2(r)));e.run(t.name,...n)}function z2(e,t,s,n){try{let r=rh(e,t);if(r.shapeParams&&r.shapeParams.length>0)return null;new lo(t).applyToAllBlocks(new En(r));let i=s.compile(r);return n.addCompiledKernel(i),t.isWebGPU()||$2(n,i,r),i}catch{return null}}function V2(e,t,s){let n=t.map(a=>new T(a.shape,a.dtype)),r=e+"_jit_"+U2++,i=Yf(r,n,[],(a,l)=>{let u;if(D2[e]){let f=s?.dim,h=l[0].type.rank,p=f!=null?(Array.isArray(f)?f:[f]).map(g=>g<0?h+g:g):Array.from({length:h},(g,y)=>y),d=q2(e,l[0].type.dtype),m=a.scalarConstant(d,l[0].type.dtype);u=a.reduce(l[0],m.getResult(0),p,e)}else typeof lT[e]=="function"?u=lT[e](a,l,s):typeof a[e]=="function"?u=G2(a,e,l,s):u=a._inferAndBuild(e,l,s);a.returnOp([u.getResult(0)])}),o=i.getReturnOp();return o&&o.operands.length>0&&(i.outputTypes=Object.freeze([o.operands[0].type])),i}function G2(e,t,s,n){return s.length===1?e[t](s[0]):s.length===2?e[t](s[0],s[1]):s.length===3?e[t](s[0],s[1],s[2]):e._inferAndBuild(t,s,n)}function q2(e,t){return Md(e,t)}function cT(e,t,s,n){let r=L2(e,t,s,n),i=oT.get(r);if(i)return i;let o=V2(e,t,s),a=new cn(e+"_jit_mod");a.addFunction(o);let l=new kl;l.addPass(new Jc),l.addPass(new El),l.addPass(new Bi),l.run(a);let u=P2(n.name),f=new di(n),p=typeof n.isGPU=="function"&&n.isGPU()?z2(o,n,f,u):null;p||(p=f.compile(rh(o,n)),u.addCompiledKernel(p));let d=o.getReturnOp(),m=d&&d.operands.length>0?d.operands[0].type.dtype:null;return i={funcName:p.name,runtime:u,numInputs:t.length,outDtype:m,compiled:p},oT.set(r,i),i}var oT,aT,D2,lT,U2,uT=I(()=>{li();Ya();It();Nc();hh();Ag();Gx();jx();Wx();Yx();bn();np();Wo();ir();oT=new Map,aT=new Map;c(L2,"_cacheKey");c(P2,"_getRuntime");D2={sum:"sum",mean:"mean",max:"max",min:"min",prod:"prod"},lT={matmul:"matmul",dot:c((e,t)=>e.dot(t[0],t[1],[t[0].type.rank-1],[0]),"dot"),clone:c((e,t)=>e._inferAndBuild("add",[t[0],e.scalarConstant(0,t[0].type.dtype).getResult(0)]),"clone"),transpose:c((e,t,s)=>{let n=t[0].type.rank,r=s?.dim0??0,i=s?.dim1??1,o=Array.from({length:n},(a,l)=>l);return o[r]=i,o[i]=r,e.transpose(t[0],o)},"transpose"),softmax:c((e,t,s)=>e.softmax(t[0],s?.dim??-1),"softmax"),log_softmax:c((e,t,s)=>e.logSoftmax(t[0],s?.dim??-1),"log_softmax"),layer_norm:c((e,t,s)=>e.layernorm(t[0],t[1],t[2],s?.axis??-1,s?.eps??1e-5),"layer_norm"),batch_norm:c((e,t,s)=>e.batchnorm(t[0],t[1],t[2],t[3],t[4],s?.axis??1,s?.eps??1e-5),"batch_norm"),conv2d:c((e,t,s)=>e.conv(t[0],t[1],s?.strides??[1,1],s?.padding??[[0,0],[0,0]],{dilation:s?.dilation??[1,1],groups:s?.groups??1}),"conv2d"),pool2d:c((e,t,s)=>e.pool2d(t[0],s?.pool_type??"max",s?.kernel_size??[2,2],s?.strides??[2,2],s?.padding??[[0,0],[0,0]]),"pool2d"),embedding:c((e,t)=>e.embedding(t[0],t[1]),"embedding"),argmax:c((e,t,s)=>e.argmax(t[0],s?.dim??0,s?.keepdim??!1),"argmax"),argmin:c((e,t,s)=>e.argmin(t[0],s?.dim??0,s?.keepdim??!1),"argmin"),eq:c((e,t)=>e.compare(t[0],t[1],"eq"),"eq"),ne:c((e,t)=>e.compare(t[0],t[1],"ne"),"ne"),lt:c((e,t)=>e.compare(t[0],t[1],"lt"),"lt"),le:c((e,t)=>e.compare(t[0],t[1],"le"),"le"),gt:c((e,t)=>e.compare(t[0],t[1],"gt"),"gt"),ge:c((e,t)=>e.compare(t[0],t[1],"ge"),"ge"),clamp:c((e,t)=>e.clamp(t[1],t[0],t[2]),"clamp"),pad:c((e,t,s)=>e.pad(t[0],t[1],s.low,s.high),"pad"),one_hot:c((e,t,s)=>e.oneHot(t[0],s.depth,{dtype:Q.F32}),"one_hot"),index_select:c((e,t,s)=>e.gather(t[0],t[1],em(t[0].type,s.dim??0,t[1].type.rank)),"index_select"),gather:c((e,t,s)=>e.gatherDim(t[0],t[1],s.dim??0),"gather"),scatter_add:c((e,t,s)=>e.scatterAddDim(t[0],t[1],t[2],s.dim??0),"scatter_add"),cat:c((e,t,s)=>{let n=t[0].type.rank,r=(s?.dim??0)<0?n+(s?.dim??0):s?.dim??0;return e.concat(t,r)},"cat"),stack:c((e,t,s)=>{let n=t[0].type.rank,r=(s?.dim??0)<0?n+1+(s?.dim??0):s?.dim??0,i=t.map(o=>{let a=[...o.type.shape];return a.splice(r,0,1),e.reshape(o,a).getResult(0)});return e.concat(i,r)},"stack")};c(F2,"_bufferNumel");c($2,"_trialLaunch");c(z2,"_compileScheduledGPU");U2=0;c(V2,"_buildGraphFunc");c(G2,"_callBuilder");c(q2,"_reductionInit");c(cT,"jitCompile")});var KC={};Xt(KC,{getCudnnLSTM:()=>wS,getGpuAdamFn:()=>xS,getGpuContiguousFn:()=>_x,getGpuMatmul:()=>ex,getWebgpuRNN:()=>bS,gpuContiguousArray:()=>gx,registerJITKernels:()=>AS,setCudnnLSTM:()=>Ax,setGpuAdamFn:()=>vx,setGpuConcatFn:()=>Sx,setGpuContiguousFn:()=>xx,setGpuMatmul:()=>kx,setWebGPUEagerFn:()=>Y2,setWebgpuRNN:()=>sV,tensorToContiguous:()=>Nn,wrapResult:()=>ae});function Y2(e){fT=e}function X2(e,t){let s=[],n={},r=Q2[e],i=0;for(let o=0;o<t.length;o++){let a=t[o];if(a&&a._impl)s.push(a);else if(Array.isArray(a)&&a.length>0&&a[0]&&a[0]._impl)for(let l of a)l&&l._impl&&s.push(l);else a!=null&&(r&&i<r.length&&(n[r[i]]=a),i++)}return{tensors:s,scalars:n}}function hT(e,t,s){if(t.length===0)return[];if(new Set(["sum","mean","max","min","prod","argmax","argmin"]).has(e)){let i=t[0].shape,o=s.dim,a=s.keepdim;if(o==null)return a?i.map(()=>1):[];let l=new Set((Array.isArray(o)?o:[o]).map(f=>f<0?i.length+f:f)),u=[];for(let f=0;f<i.length;f++)l.has(f)?a&&u.push(1):u.push(i[f]);return u}if(e==="matmul")return Jm(t[0].shape,t[1].shape);if(e==="dot")return[];if(e==="transpose"){let i=[...t[0].shape],o=s.dim0??0,a=s.dim1??1,l=i[o];return i[o]=i[a],i[a]=l,i}if(e==="conv2d"){let i=t[0].shape,o=t[1].shape,a=s.strides||[1,1],l=s.padding||[[0,0],[0,0]],u=s.dilation||[1,1],f=[];for(let h=0;h<2;h++){let p=l[h][0]+l[h][1],d=(o[h+2]-1)*u[h]+1;f.push(Math.floor((i[h+2]+p-d)/a[h])+1)}return[i[0],o[0],...f]}if(e==="pool2d"){let i=t[0].shape,o=s.kernel_size||[2,2],a=s.strides||o,l=s.padding||[[0,0],[0,0]],u=[];for(let f=0;f<2;f++){let h=l[f][0]+l[f][1];u.push(Math.floor((i[f+2]+h-o[f])/a[f])+1)}return[i[0],i[1],...u]}if(e==="clamp"){let i=t[0].shape;for(let o=1;o<t.length;o++)i=zc(i,t[o].shape)||i;return i}if(e==="pad"){let i=t[0].shape,o=s.low||[],a=s.high||[];return i.map((l,u)=>l+(o[u]||0)+(a[u]||0))}if(e==="one_hot")return[...t[0].shape,s.depth];if(e==="cat"){let i=t[0].shape.length,o=(s.dim??0)<0?i+(s.dim??0):s.dim??0,a=[...t[0].shape];return a[o]=t.reduce((l,u)=>l+u.shape[o],0),a}if(e==="stack"){let i=t[0].shape.length,o=(s.dim??0)<0?i+1+(s.dim??0):s.dim??0,a=[...t[0].shape];return a.splice(o,0,t.length),a}if(e==="index_select"){let i=[...t[0].shape],o=i.length,a=(s.dim??0)<0?o+(s.dim??0):s.dim??0;return i[a]=t[1].shape.reduce((l,u)=>l*u,1),i}if(e==="gather")return[...t[1].shape];if(e==="scatter_add")return[...t[0].shape];if(e==="softmax"||e==="log_softmax")return[...t[0].shape];if(e==="layer_norm"||e==="batch_norm")return[...t[0].shape];if(e==="embedding"){let i=t[0].shape;return[...t[1].shape,i[1]]}return t.length===1?[...t[0].shape]:zc(t[0].shape,t[1].shape)||[...t[0].shape]}function Nn(e){let t=e._impl.storage.data,s=e._impl.storageOffset,n=e.numel;if(e.isContiguous&&s===0&&t.length===n)return e.data;let r=e.shape,i=e.strides,o=t.constructor,a=new o(n),l=r.length,u=new Int32Array(l),f=s;for(let h=0;h<n;h++){a[h]=t[f];for(let p=l-1;p>=0;p--){if(u[p]++,u[p]<r[p]){f+=i[p];break}f-=(r[p]-1)*i[p],u[p]=0}}return a}function ae(e,t,s,n){let r=As(t),i=Ys.fromData(e,n),o=new Oe(i,0,t,r,s,n);return new Et(o)}function Ig(e,t,s){let n=1;for(let r=t;r<s;r++)n*=e[r];return n}function Z2(e,t,s,n){let r=t.length,i=s<0?r+1+s:s,o=Ig(t,0,i),a=Ig(t,i,r),l=e.length;for(let u=0;u<o;u++)for(let f=0;f<l;f++)n.set(e[f].subarray(u*a,(u+1)*a),(u*l+f)*a)}function tV(e,t,s,n){let r=t[0].length,i=s<0?r+s:s,o=Ig(t[0],0,i),a=Ig(t[0],i+1,r),l=0;for(let u of t)l+=u[i];for(let u=0;u<o;u++){let f=u*l*a;for(let h=0;h<e.length;h++){let p=t[h][i]*a;n.set(e[h].subarray(u*p,(u+1)*p),f),f+=p}}}function eV(e,t,s){let n=hT(e,t,s),r=t[0].dtype,i=me(r),o=new i(Math.max(gs(n),1)),a=s.dim??0;if(Ss()&&_S){let u=t.map(f=>SS(f));return _S(e,u,t.map(f=>f.shape),a,n,o,r),ae(o,n,r,t[0].device)}let l=t.map(u=>Nn(u));return e==="stack"?Z2(l,t[0].shape,a,o):tV(l,t.map(u=>u.shape),a,o),ae(o,n,r,t[0].device)}function xx(e){Og=e}function _x(){return Og}function Sx(e){_S=e}function Ax(e){pT=e}function wS(){return pT}function sV(e){dT=e}function bS(){return dT}function vx(e){mT=e}function xS(){return mT}function kx(e){gT=e}function ex(){return gT}function SS(e){if(e.isContiguous&&e._impl.storageOffset===0){let t=e._impl.storage.rawData;if(t&&t.length===e.numel)return t}return Ss()&&Og?Og(e._impl.storage.rawData,e.shape,e.strides,e._impl.storageOffset,e.dtype):Nn(e)}function gx(e){return SS(e)}function nV(e,t){let s=J2[t];if(!s)return null;let n=t===it.GPU,r=t===it.CUSTOM_0,i=n&&(e==="stack"||e==="cat");return(o,...a)=>{let{tensors:l,scalars:u}=X2(e,a);if(l.length===0)throw new Error(`JIT dispatch: no tensor args for op '${e}'`);if(i)return eV(e,l,u);let f=s(),h=cT(e,l,u,f),p=hT(e,l,u),d=h.outDtype||Wm(l[0].dtype,l.length>1?l[1].dtype:l[0].dtype),m=gs(p),g=me(d),y=new g(Math.max(m,1));if(r)return fT(h.compiled,l,y),ae(y,p,d,l[0].device);let _=l.map(w=>n?SS(w):Nn(w));return _.push(y),h.runtime.run(h.funcName,..._),ae(y,p,d,l[0].device)}}function AS(){let e=Ue.listOps(),t=[it.CPU,it.GPU,it.WASM,it.CUSTOM_0];for(let s of e){let n=Ue.findOp(s);if(!n)continue;let r=n.name;for(let i of t){if(n.entry.hasKernel(i))continue;let o=nV(r,i);o&&n.entry.registerKernel(i,Dn.fromUnboxed(o))}}}var j2,W2,K2,H2,J2,fT,Q2,Og,_S,pT,dT,mT,gT,Nr=I(()=>{Qn();$f();ja();uT();Xi();hi();Js();Zi();dl();Vr();ir();J2={[it.CPU]:()=>j2??=nr(),[it.GPU]:()=>W2??=el(),[it.WASM]:()=>K2??=sl(),[it.CUSTOM_0]:()=>H2??=nl()},fT=null;c(Y2,"setWebGPUEagerFn");Q2={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"]};c(X2,"_extractTensorsAndScalars");c(hT,"_inferOutputShape");c(Nn,"tensorToContiguous");c(ae,"wrapResult");c(Ig,"_prod");c(Z2,"_hostStack");c(tV,"_hostCat");c(eV,"_runHostConcatLike");Og=null;c(xx,"setGpuContiguousFn");c(_x,"getGpuContiguousFn");_S=null;c(Sx,"setGpuConcatFn");pT=null;c(Ax,"setCudnnLSTM");c(wS,"getCudnnLSTM");dT=null;c(sV,"setWebgpuRNN");c(bS,"getWebgpuRNN");mT=null;c(vx,"setGpuAdamFn");c(xS,"getGpuAdamFn");gT=null;c(kx,"setGpuMatmul");c(ex,"getGpuMatmul");c(SS,"_gpuInputArray");c(gx,"gpuContiguousArray");c(nV,"_wrapOpForJIT");c(AS,"registerJITKernels")});var AV,de,Tl=I(()=>{Js();Zi();dl();Qn();vn();Vr();AV=Yn.fromKey(it.TRACING),de=class extends Et{static{c(this,"SymbolicTensor")}constructor(t,s,n,r,i){let o=As(s),a=Ys.allocate(0,n,$c),l=new Oe(a,0,s,o,n,$c);super(l),this._irValue=t,this._tracer=r,this._symbolicShape=i}get irValue(){return this._irValue}get tracer(){return this._tracer}get symbolicShape(){return this._symbolicShape}get dispatchKeySet(){return super.dispatchKeySet.union(AV)}get isSymbolic(){return!0}}});var MV,Ay,mI=I(()=>{It();Hx();MV={eq:c((e,t)=>e===t,"eq"),ne:c((e,t)=>e!==t,"ne"),gt:c((e,t)=>e>t,"gt"),ge:c((e,t)=>e>=t,"ge"),lt:c((e,t)=>e<t,"lt"),le:c((e,t)=>e<=t,"le")},Ay=class{static{c(this,"ShapeEnv")}constructor(){this._symbols=new Map,this._guards=[],this._bindings=new Map,this._nextId=0}allocate(t,s,n){let r=`s${this._nextId++}`;return this._symbols.set(r,{hint:n,inputIdx:t,dimIdx:s}),r}produceShapeSpec(t,s,n){let r=new Array(s.length),i=new Array(s.length);for(let o=0;o<s.length;o++)if(n&&n.has(o)){let a=this.allocate(t,o,s[o]);r[o]=X,i[o]=a}else{let a=this.allocate(t,o,s[o]);this.guardRelation(a,"eq",s[o]),r[o]=s[o],i[o]=s[o]}return{irShape:r,symShape:i}}guardRelation(t,s,n){this._guards.push({lhs:t,op:s,rhs:n})}guardDivisible(t,s){this._guards.push({type:"divisible",sym:t,divisor:s})}bindInputShapes(t){this._bindings.clear();for(let[s,n]of this._symbols)this._bindings.set(s,t[n.inputIdx].shape[n.dimIdx])}evaluateGuards(){for(let t=0;t<this._guards.length;t++){let s=this._guards[t];if(s.type==="divisible"){if(this._resolve(s.sym)%s.divisor!==0)return{passed:!1,failedGuard:s};continue}let n=this._resolve(s.lhs),r=this._resolve(s.rhs);if(!MV[s.op](n,r))return{passed:!1,failedGuard:s}}return{passed:!0,failedGuard:null}}resolveSymbolicShape(t){let s=new Array(t.length);for(let n=0;n<t.length;n++)s[n]=this._resolve(t[n]);return s}_resolve(t){return typeof t=="number"?t:typeof t=="string"?this._bindings.get(t):t instanceof qo?qo.evaluate(t,this._bindings):t}get symbols(){return this._symbols}get guards(){return this._guards}get bindings(){return this._bindings}}});function fp(e,t,s,n){let r=t[0].type.rank,i=s?.dim,o=i!=null?(Array.isArray(i)?i:[i]).map(h=>h<0?r+h:h):Array.from({length:r},(h,p)=>p),a=e.scalarConstant(Md(n,t[0].type.dtype),t[0].type.dtype),l=e.reduce(t[0],a.getResult(0),o,n);if(!s?.keepdim)return l;let u=new Set(o),f=t[0].type.shape.map((h,p)=>u.has(p)?1:h);return e.reshape(l.getResult(0),f)}function Ol(){return vy}var LV,vy,ky,hp=I(()=>{li();Ya();Co();It();rs();Tl();mI();bn();c(fp,"_traceReduce");LV={matmul:c((e,t)=>e.matmul(t[0],t[1]),"matmul"),softmax:c((e,t,s)=>e.softmax(t[0],s?.dim??-1),"softmax"),log_softmax:c((e,t,s)=>e.logSoftmax(t[0],s?.dim??-1),"log_softmax"),layer_norm:c((e,t,s)=>e.layernorm(t[0],t[1],t[2],s?.axis??-1,s?.eps??1e-5),"layer_norm"),batch_norm:c((e,t,s)=>e.batchnorm(t[0],t[1],t[2],t[3],t[4],s?.axis??1,s?.eps??1e-5),"batch_norm"),embedding:c((e,t)=>e.embedding(t[0],t[1]),"embedding"),relu:c((e,t)=>e.relu(t[0]),"relu"),sigmoid:c((e,t)=>e.sigmoid(t[0]),"sigmoid"),gelu:c((e,t)=>e.gelu(t[0]),"gelu"),silu:c((e,t)=>e.silu(t[0]),"silu"),conv2d:c((e,t,s)=>e.conv(t[0],t[1],s?.strides??[1,1],s?.padding??[[0,0],[0,0]],{dilation:s?.dilation,groups:s?.groups}),"conv2d"),pool2d:c((e,t,s)=>e.pool2d(t[0],s?.pool_type??"max",s?.kernel_size??[2,2],s?.strides??[2,2],s?.padding??[[0,0],[0,0]]),"pool2d"),maximum:c((e,t)=>e.maximum(t[0],t[1]),"maximum"),minimum:c((e,t)=>e.minimum(t[0],t[1]),"minimum"),sum:c((e,t,s)=>fp(e,t,s,"sum"),"sum"),mean:c((e,t,s)=>fp(e,t,s,"mean"),"mean"),max:c((e,t,s)=>fp(e,t,s,"max"),"max"),min:c((e,t,s)=>fp(e,t,s,"min"),"min"),prod:c((e,t,s)=>fp(e,t,s,"prod"),"prod"),argmax:c((e,t,s)=>e.argmax(t[0],s?.dim??0,s?.keepdim??!1),"argmax"),argmin:c((e,t,s)=>e.argmin(t[0],s?.dim??0,s?.keepdim??!1),"argmin"),eq:c((e,t)=>e.compare(t[0],t[1],"eq"),"eq"),ne:c((e,t)=>e.compare(t[0],t[1],"ne"),"ne"),lt:c((e,t)=>e.compare(t[0],t[1],"lt"),"lt"),le:c((e,t)=>e.compare(t[0],t[1],"le"),"le"),gt:c((e,t)=>e.compare(t[0],t[1],"gt"),"gt"),ge:c((e,t)=>e.compare(t[0],t[1],"ge"),"ge"),transpose:c((e,t,s)=>{let n=t[0].type.rank,r=s?.dim0??0,i=s?.dim1??1,o=Array.from({length:n},(a,l)=>l);return o[r]=i,o[i]=r,e.transpose(t[0],o)},"transpose"),clamp:c((e,t)=>e.clamp(t[1],t[0],t[2]),"clamp"),pad:c((e,t,s)=>e.pad(t[0],t[1],s.low,s.high),"pad"),one_hot:c((e,t,s)=>e.oneHot(t[0],s.depth,{dtype:Q.F32}),"one_hot"),index_select:c((e,t,s)=>e.gather(t[0],t[1],em(t[0].type,s?.dim??0,t[1].type.rank)),"index_select"),gather:c((e,t,s)=>e.gatherDim(t[0],t[1],s?.dim??0),"gather"),scatter_add:c((e,t,s)=>e.scatterAddDim(t[0],t[1],t[2],s?.dim??0),"scatter_add"),cat:c((e,t,s)=>{let n=t[0].type.rank,r=(s?.dim??0)<0?n+(s?.dim??0):s?.dim??0;return e.concat(t,r)},"cat"),stack:c((e,t,s)=>{let n=t[0].type.rank,r=(s?.dim??0)<0?n+1+(s?.dim??0):s?.dim??0,i=t.map(o=>{let a=[...o.type.shape];return a.splice(r,0,1),e.reshape(o,a).getResult(0)});return e.concat(i,r)},"stack"),permute:c((e,t,s)=>e.transpose(t[0],s.dims),"permute"),reshape:c((e,t,s)=>e.reshape(t[0],s.new_shape),"reshape"),slice:c((e,t,s)=>e.slice(t[0],s.starts,s.limits,s.strides),"slice")},vy=null;c(Ol,"getActiveTracer");ky=class{static{c(this,"Tracer")}constructor(t){this._name=t||"traced",this._shapeEnv=new Ay,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,s,n){let r=this._inputTypes.length,{irShape:i,symShape:o}=this._shapeEnv.produceShapeSpec(r,t,n);if(n)for(let l=0;l<o.length;l++)typeof o[l]=="string"&&this._shapeEnv.guardRelation(o[l],"gt",0);let a=new T(i,s);return this._inputTypes.push(a),this._inputSymShapes.push(o),{shape:i,dtype:s,tensorType:a}}_initGraph(){this._func=new Fs(this._name,this._inputTypes,[]),this._func.inputTypes=[...this._func.inputTypes],this._builder=new bs(this._func),this._module=new cn(this._name);let t=[],s=this._func.args;for(let n=0;n<s.length;n++){let r=s[n],i=this._inputTypes[n];r.symbolicShape=this._inputSymShapes[n];let o=new de(r,i.shape,i.dtype,this,this._inputSymShapes[n]);t.push(o)}return this._inputs=t,t}recordOp(t,s,n){let r=[];for(let f of s)f instanceof de&&r.push(f.irValue);let i,o=LV[t];o?i=o(this._builder,r,n):typeof this._builder[t]=="function"?i=this._builder[t](...r):i=this._builder._inferAndBuild(t,r,n||null);let a=i.getResult(0),l=a.type,u=this._propagateSymbolicShape(t,i,s,l);return a.symbolicShape=u,new de(a,l.shape,l.dtype,this,u)}_propagateSymbolicShape(t,s,n,r){let i=n.filter(u=>u instanceof de),o=rt.get(s.opName||t);if(o&&o.propagateSymbolicShapes){let u=new Map;for(let h of i)u.set(h.irValue,h.symbolicShape);let f=o.propagateSymbolicShapes(s,u);if(f&&f[0])return f[0]}let a=r.shape,l=new Array(a.length);for(let u=0;u<a.length;u++){if(a[u]!==X){l[u]=a[u];continue}let f=null;for(let h of i){let p=h.symbolicShape;if(!p)continue;let d=a.length-p.length,m=u-d;if(m>=0&&m<p.length&&typeof p[m]=="string"){f=p[m];break}}l[u]=f!==null?f:X}return l}scan(t,s,n){let r=c(h=>h instanceof de?h.irValue:this.captureConstant(h).irValue,"toIr"),i=t.map(r),o=s.map(r),a=this._builder.scanOp(i,o,(h,p,d)=>{let m=this._builder;this._builder=h;try{let g=c(w=>new de(w,w.type.shape,w.type.dtype,this,[...w.type.shape]),"wrap"),[y,_]=n(d.map(g),p.map(g));return[y.map(w=>w.irValue),_.map(w=>w.irValue)]}finally{this._builder=m}}),l=s.length,u=[],f=[];for(let h=0;h<a.numResults;h++){let p=a.getResult(h),d=new de(p,p.type.shape,p.type.dtype,this,[...p.type.shape]);h<l?u.push(d):f.push(d)}return[u,f]}captureConstant(t){let s=this._capturedParams.get(t);if(s)return s;if(t.shape.length===0&&t.data){let a=t.data[0],u=this._builder.scalarConstant(a,t.dtype).getResult(0),f=new de(u,[],t.dtype,this,[]);return this._capturedParams.set(t,f),f}let n=new T(t.shape,t.dtype);this._func.inputTypes.push(n);let i=this._func.entryBlock.addArgument(n),o=new de(i,t.shape,t.dtype,this,[...t.shape]);return this._capturedParams.set(t,o),this._capturedParamOrder.push(t),o}get capturedParams(){return this._capturedParamOrder}markOutput(t){t instanceof de&&(this._builder.returnOp([t.irValue]),this._outputSymShapes=[t.symbolicShape]),this._outputTypes=[new T(t.shape,t.dtype)]}markOutputs(t){let s=t.map(n=>n.irValue);this._builder.returnOp(s),this._outputTypes=t.map(n=>new T(n.shape,n.dtype)),this._outputSymShapes=t.map(n=>n instanceof de?n.symbolicShape:[...n.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(){vy=this}deactivate(){vy===this&&(vy=null)}}});function vI(e){let t=new ac;return t.add(new hc),t.add(new pc),t.add(new dc(e)),t.add(new mc),t.add(new gc(e)),t.add(new yc),t.add(new _c),t.add(new $d),t.add(new fc),t.add(new Gd),t.add(new qd),t.add(new jd),t.add(new Wd),e&&(t.add(new Vd(e)),t.add(new zd(e)),t.add(new Ud(e))),t}var ZV,t3,Iy,kI=I(()=>{hs();jf();Eg();wc();c(vI,"buildAlgebraicPatterns");ZV=vI(!1),t3=vI(!0),Iy=class extends Mt{static{c(this,"AlgebraicSimplificationPass")}constructor(t={}){super("algebraic_simplify"),this.preservedAnalyses=new Set([jo]),this.patterns=t.fastMath?t3:ZV}run(t,s){return new lc(this.patterns).applyPatterns(t,10,this.trace)}}});function EI(e){if(e3.has(e))return!0;let t=rt.get(e);return!!(t&&t.isConstant)}function s3(e,t){return!_b(t)||typeof e!="number"?!0:Number.isInteger(e)&&Number.isSafeInteger(e)}function NI(e,t,s){if(s.has(e))return s.get(e);let n=n3(e,t,s);return s.set(e,n),n}function n3(e,t,s){let n=e.definingOp;if(!n)return;if(EI(n.opName))return n.getAttr("value");if(t.has(n))return;t.add(n);let r=rt.get(n.opName);if(!r||!r.fold||r.sideEffects||r.hasTrait&&r.hasTrait(j.SIDE_EFFECT)||n.regions.length>0)return;let i=new Array(n.numOperands),o=new Array(n.numOperands);for(let a=0;a<n.numOperands;a++){let l=NI(n.getOperand(a),t,s);if(l===void 0)return;i[a]=l,o[a]=n.getOperand(a).definingOp}try{return r.fold(i,n.attributes,o)}catch{return}}var e3,Oy,RI=I(()=>{hs();li();rs();He();ps();It();e3=new Set(["constant","scalar_constant"]);c(EI,"isConstantProducer");c(s3,"isFoldResultRepresentable");c(NI,"resolveConstantValue");c(n3,"computeConstantValue");Oy=class extends Mt{static{c(this,"ConstantFoldPass")}constructor(){super("constant_fold")}run(t,s){let n=!1,r=0,i=new bs(t),o=new Map;for(let a of[...t.opsRecursive()]){if(!a.parentBlock||EI(a.opName))continue;let l=rt.get(a.opName);if(!l||a.regions.length>0||l.sideEffects||l.hasTrait&&l.hasTrait(j.SIDE_EFFECT)||l.getMemoryEffects&&l.getMemoryEffects(a).length>0||!l.fold||a.numOperands===0)continue;let u=new Array(a.numOperands),f=new Array(a.numOperands),h=!0;for(let p=0;p<a.numOperands;p++){let d=NI(a.getOperand(p),new Set,o);if(d===void 0){h=!1;break}u[p]=d,f[p]=a.getOperand(p).definingOp}if(h)try{let p=l.fold(u,a.attributes,f);if(p===void 0||!s3(p,a.getResult(0).type.dtype))continue;i.block=a.parentBlock,i.setInsertionPoint(a);let d=i.constant(p,a.getResult(0).type);a.replaceAllResultsWith([d.getResult(0)]),a.erase(),n=!0,r++}catch{}}return this.trace&&this.trace.level>=at.DEBUG&&r>0&&this.trace.emit({type:"pass_detail",passName:this.name,foldedCount:r,level:at.DEBUG}),n?et.CHANGED:et.UNCHANGED}}});var My,BI=I(()=>{hs();rs();He();Eg();ps();My=class extends Mt{static{c(this,"CSEPass")}constructor(){super("cse"),this.preservedAnalyses=new Set([jo])}run(t,s){let n=!1,r=0,i=typeof t.blocksRecursive=="function"?[...t.blocksRecursive()]:[...t.body];for(let o of i){let a=new Map;for(let l of[...o.ops()]){if(!l.parentBlock||l.regions&&l.regions.length>0)continue;let u=rt.get(l.opName);if(u&&(u.sideEffects||u.hasTrait&&u.hasTrait(j.SIDE_EFFECT))||u&&u.getMemoryEffects&&u.getMemoryEffects(l).length>0)continue;let f=l.structuralHash();if(!a.has(f)){a.set(f,[l]);continue}let h=a.get(f),p=!1;for(let d of h)if(d.parentBlock&&d.structuralEquals(l)){let m=[];for(let g=0;g<d.numResults;g++)m.push(d.getResult(g));l.replaceAllResultsWith(m),l.erase(),n=!0,r++,p=!0;break}p||h.push(l)}}return this.trace&&this.trace.level>=at.DEBUG&&r>0&&this.trace.emit({type:"pass_detail",passName:this.name,eliminated:r,level:at.DEBUG}),n?et.CHANGED:et.UNCHANGED}}});function tA(e,t){let s=ZS[e],n=ZS[t];return s===void 0||n===void 0?!1:e===zt.REDUCTION?t===zt.ELEMENTWISE:t===zt.REDUCTION?s<=ZS[zt.INJECTIVE]:!0}function Pu(e){let t=!1,s=!1,n=!1,r=!1;for(let i of e){let o=rt.get(i.opName);if(!o||o.isOpaque){n=!0;continue}o.isReduction?t=!0:o.isInjective?s=!0:o.isBroadcast?r=!0:o.isElementwise||(n=!0)}return n?zt.OPAQUE:t?zt.REDUCTION:s?zt.INJECTIVE:r?zt.BROADCAST:zt.ELEMENTWISE}function $l(e){let t=rt.get(e.opName);return!t||t.isOpaque?zt.OPAQUE:t.isReduction?zt.REDUCTION:t.isInjective?zt.INJECTIVE:t.isBroadcast?zt.BROADCAST:t.isElementwise?zt.ELEMENTWISE:zt.OPAQUE}function CI(e,t){if(e.length!==t.length)return!1;for(let s=0;s<e.length;s++)if(!(e[s]===X||t[s]===X)&&e[s]!==t[s])return!1;return!0}var zt,ZS,Lu,Ly,Du=I(()=>{It();rs();Nc();zt=Object.freeze({ELEMENTWISE:"kElementwise",BROADCAST:"kBroadcast",REDUCTION:"kReduction",INJECTIVE:"kInjective",OPAQUE:"kOpaque",HORIZONTAL:"kHorizontal"}),ZS={[zt.ELEMENTWISE]:0,[zt.BROADCAST]:1,[zt.INJECTIVE]:2,[zt.REDUCTION]:3};c(tA,"canFusePatterns");c(Pu,"classifyFusionKind");c($l,"classifyOpPattern");Lu=Object.freeze({legal:!0}),Ly=class{static{c(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 s=this._lowerableCache.get(t);return s===void 0&&(s=t==="return"||t==="yield"||Za(t)||zb(t),this._lowerableCache.set(t,s)),s}canFuse(t,s){if(!t||!s)return{legal:!1,reason:"null op"};if(t===s)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(s.regions.length>0&&s.opName!=="fusion"&&s.opName!=="reduce")return{legal:!1,reason:"consumer has control flow regions"};let n=rt.get(t.opName),r=rt.get(s.opName);if(!n)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(s.opName))return{legal:!1,reason:`consumer op '${s.opName}' has no lowering rule`};if(n.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 i=$l(t),o=$l(s);return tA(i,o)?(i===zt.REDUCTION||o===zt.REDUCTION)&&!this.allowReductionFusion?{legal:!1,reason:"reduction fusion disabled by target"}:i===zt.ELEMENTWISE&&o===zt.ELEMENTWISE?this._checkElementwisePair(t,s):(i===zt.BROADCAST||i===zt.REDUCTION)&&o===zt.ELEMENTWISE?Lu:this._checkProducerConsumerShapes(t,s):{legal:!1,reason:`cannot fuse pattern ${i} -> ${o}`}}canMergeGroups(t,s){if(t.size+s.size>this.maxFusionSize)return{legal:!1,reason:"merged group exceeds max fusion size"};let n=0;for(let r of t.ops){let i=rt.get(r.opName);if(i&&i.isReduction&&n++,i&&i.isOpaque)return{legal:!1,reason:"opaque op in merge (use EpilogueFusionPass)"}}for(let r of s.ops){let i=rt.get(r.opName);if(i&&i.isReduction&&n++,i&&i.isOpaque)return{legal:!1,reason:"opaque op in merge (use EpilogueFusionPass)"}}return n>1?{legal:!1,reason:"merged group would contain multiple reductions"}:Lu}_checkElementwisePair(t,s){let n=this._getOutputShape(t),r=this._getOutputShape(s);return n&&r&&!CI(n,r)?{legal:!1,reason:`elementwise shape mismatch: [${n}] vs [${r}]`}:Lu}_checkProducerConsumerShapes(t,s){let n=this._getOutputShape(t);if(!n)return Lu;for(let r=0;r<s.numOperands;r++)if(s.getOperand(r).definingOp===t){let i=s.getOperand(r).type;if(i instanceof T)return CI(n,i.shape)?Lu:{legal:!1,reason:`shape mismatch on data edge: [${n}] vs [${i.shape}]`}}return Lu}_getOutputShape(t){for(let s=0;s<t.numResults;s++){let n=t.getResult(s).type;if(n instanceof T)return n.shape}return null}};c(CI,"shapesCompatible")});function r3(e){for(let t=0;t<e.numResults;t++){let s=e.getResult(t).type;if(s instanceof T)return s.shape.join(",")}return null}var Fu,Py,eA=I(()=>{It();rs();Du();Nc();Fu=class{static{c(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,s){this.opSet.has(t)||(this.ops.push(t),this.opSet.add(t),this._inputValues=null,this._outputValues=null,s!==void 0&&(s<this.minTopoPos&&(this.minTopoPos=s),s>this.maxTopoPos&&(this.maxTopoPos=s)))}hasOp(t){return this.opSet.has(t)}merge(t){for(let s of t.ops)this.addOp(s);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,s=new Set;for(let n of this.ops){for(let r=0;r<n.numOperands;r++){let i=n.getOperand(r);i.definingOp&&this.opSet.has(i.definingOp)||t.has(i)||(t.add(i),this._inputValues.push(i))}for(let r=0;r<n.numResults;r++){let i=n.getResult(r);if(!s.has(i)){for(let o of i.uses())if(!this.opSet.has(o.user)){s.add(i),this._outputValues.push(i);break}}}}}getInputValues(){return this.computeIO(),this._inputValues}getOutputValues(){return this.computeIO(),this._outputValues}get size(){return this.ops.length}classifyKind(){return this.kind=Pu(this.ops),this.kind}allOpsInlineFusable(){for(let t of this.ops){let s=rt.get(t.opName);if(!s)return!1;if(!(s.isReduction||s.isConstant)&&!Za(t.opName))return!1}return!0}};c(r3,"outputShapeKey");Py=class{static{c(this,"FusionGroupBuilder")}constructor(t){this.legality=t,this._nextId=0,this._topoIndex=null}buildProducerConsumerGroups(t){this._topoIndex=new Map;let s=0;for(let o of t.ops())this._topoIndex.set(o,s++);let n=[],r=new Map;for(let o of t.ops()){let a=rt.get(o.opName);if(!a||a.isConstant||a.isTerminator||a.isReduction)continue;let l=this._topoIndex.get(o);for(let u=0;u<o.numOperands;u++){let f=o.getOperand(u).definingOp;if(!f)continue;let h=rt.get(f.opName);if(!h||h.isConstant||h.isReduction)continue;let p=r.get(o),d=r.get(f);if(p&&p===d||!this.legality.canFuse(f,o).legal)continue;let g=this._topoIndex.get(f);if(!p&&!d){let y=new Fu(this._nextId++);y.addOp(f,g),y.addOp(o,l),r.set(f,y),r.set(o,y)}else if(p&&!d)p.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(p,f)&&(p.addOp(f,g),r.set(f,p));else if(!p&&d)d.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(d,o)&&(d.addOp(o,l),r.set(o,d));else if(this.legality.canMergeGroups(p,d).legal&&!this._mergeWouldCreateCycle(p,d)){p.merge(d);for(let _ of d.ops)r.set(_,p)}}}let i=new Set;for(let o of r.values())i.has(o)||o.size<2||(i.add(o),o.classifyKind(),n.push(o));return n}_wouldCreateCycle(t,s){for(let n=0;n<s.numOperands;n++){let r=s.getOperand(n).definingOp;if(!(!r||t.hasOp(r)||this._topoIndex.get(r)<t.minTopoPos)&&this._dependsOnGroup(r,t))return!0}return!1}_dependsOnOps(t,s,n){let r=new Set,i=[t];for(r.add(t);i.length>0;){let o=i.pop();for(let a=0;a<o.numOperands;a++){let l=o.getOperand(a).definingOp;if(!l||r.has(l))continue;if(s.has(l))return!0;this._topoIndex.get(l)<n||(r.add(l),i.push(l))}}return!1}_dependsOnGroup(t,s){return this._dependsOnOps(t,s.opSet,s.minTopoPos)}_mergeWouldCreateCycle(t,s){let n=Math.min(t.minTopoPos,s.minTopoPos),r=Math.max(t.maxTopoPos,s.maxTopoPos),i=new Set([...t.opSet,...s.opSet]);for(let o of i)for(let a=0;a<o.numOperands;a++){let l=o.getOperand(a).definingOp;if(!l||i.has(l))continue;let u=this._topoIndex.get(l);if(!(u<n||u>r)&&this._dependsOnOps(l,i,n))return!0}return!1}_bucketable(t,s){return s&&!s.isConstant&&!s.isTerminator&&!s.isOpaque&&t.numResults>0&&r3(t)!==null}buildHorizontalGroups(t){let s=[...t.ops()];this._topoIndex=new Map;for(let l=0;l<s.length;l++)this._topoIndex.set(s[l],l);let n=[],r=new Map,i=this.legality.maxFusionSize||s.length,o=new Map,a=0;for(let l=0;l<s.length;l++){let u=s[l];if(r.has(u))continue;let f=rt.get(u.opName);if(!this._bucketable(u,f))continue;let h=u.getResult(0).type;a++,o.set(u,a);let p=new Fu(this._nextId++);p.addOp(u,l),r.set(u,p);let d=Math.min(s.length,l+1+i);for(let m=l+1;m<d;m++){let g=s[m],y=!1;for(let w=0;w<g.numOperands;w++){let b=g.getOperand(w).definingOp;if(b&&o.get(b)===a){y=!0;break}}if(y){o.set(g,a);continue}if(p.size>=i||r.has(g))continue;let _=rt.get(g.opName);this._bucketable(g,_)&&h.equals(g.getResult(0).type)&&(this._sharesInput(u,g)||f.isElementwise&&_.isElementwise)&&(p.addOp(g,m),r.set(g,p),o.set(g,a))}p.size>=2&&(p.kind=zt.HORIZONTAL,n.push(p))}return n}buildAllGroups(t){let s=this.buildProducerConsumerGroups(t),n=this.buildHorizontalGroups(t),r=new Map;for(let l of s)for(let u of l.ops)r.set(u,l);let i=new Set(r.keys()),o=[];for(let l of n){let u=!1;for(let f of l.ops)if(i.has(f)){u=!0;break}u||o.push(l)}for(let l of o)for(let u of l.ops)r.set(u,l);if(!this._condensedHasCycle(t,r))return[...s,...o];for(let l of o)for(let u of l.ops)r.delete(u);let a=[...s];for(let l of o){for(let u of l.ops)r.set(u,l);if(this._condensedHasCycle(t,r)){for(let u of l.ops)r.delete(u);continue}a.push(l)}return a}_condensedHasCycle(t,s){let n=c(f=>s.get(f)||f,"repOf"),r=new Map,i=new Set;for(let f of t.ops()){let h=n(f);i.add(h);for(let p=0;p<f.numOperands;p++){let d=f.getOperand(p).definingOp;if(!d)continue;let m=n(d);if(m===h)continue;i.add(m);let g=r.get(m);g||(g=new Set,r.set(m,g)),g.add(h)}}let o=0,a=1,l=2,u=new Map;for(let f of i)u.set(f,o);for(let f of i){if(u.get(f)!==o)continue;let h=[f];for(;h.length>0;){let p=h[h.length-1],d=u.get(p);if(d===o){u.set(p,a);let m=r.get(p);if(m)for(let g of m){let y=u.get(g);if(y===a)return!0;y===o&&h.push(g)}}else d===a&&u.set(p,l),h.pop()}}return!1}_sharesInput(t,s){if(t.numOperands===0||s.numOperands===0)return!1;let n=new Set;for(let r=0;r<t.numOperands;r++)n.add(t.getOperand(r));for(let r=0;r<s.numOperands;r++)if(n.has(s.getOperand(r)))return!0;return!1}_hasDependency(t,s){for(let n=0;n<t.numResults;n++){let r=t.getResult(n);for(let i=0;i<s.numOperands;i++)if(s.getOperand(i)===r)return!0}return!1}}});var $u,sA=I(()=>{It();rs();$u=class{static{c(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 s=this.estimateFLOPs(t),n=this.estimateBytes(t);return{flops:s,bytes:n,arithmeticIntensity:n>0?s/n:0}}estimateFLOPs(t){let s=rt.get(t.opName);if(s&&s.getFlops)return s.getFlops(t);let n=1;for(let r=0;r<t.numResults;r++){let i=t.getResult(r).type;if(i instanceof T){let o=i.numel();o!==X&&(n=o);break}}if(s&&s.isReduction&&t.numOperands>0){let r=t.getOperand(0).type;if(r instanceof T){let i=r.numel();if(i!==X)return i}}return n}estimateBytes(t){let s=0;for(let n=0;n<t.numOperands;n++){let r=t.getOperand(n).type;if(r instanceof T){let i=r.sizeInBytes();i!==X&&(s+=i)}}for(let n=0;n<t.numResults;n++){let r=t.getResult(n).type;if(r instanceof T){let i=r.sizeInBytes();i!==X&&(s+=i)}}return s}estimateGroupCost(t){let s=t.opSet,n=new Map,r=0,i=0,o=0,a=0,l=!1,u=!1,f=0,h=0,p=0,d=0,m=new Set;for(let x of t.ops){let A=this.estimateFLOPs(x);n.set(x,A),r+=A,i+=this.estimateBytes(x),this.libraryOps.has(x.opName)&&a++;let S=rt.get(x.opName);if(S){let E=this._outputElements(x);S.isReduction&&(l=!0,f+=E),S.isElementwise&&(u=!0,h+=E)}for(let E=0;E<x.numResults;E++){let R=x.getResult(E);m.add(R);let v=0,k=!1;for(let B of R.uses())s.has(B.user)?v++:k=!0;v>1&&(o+=(v-1)*A),(v>0||k)&&p++}for(let E=0;E<x.numOperands;E++){let R=x.getOperand(E);if(!m.has(R))continue;let v=!1;for(let k of R.uses())if(s.has(k.user)&&k.user!==x){v=!0;break}v||p--}p>d&&(d=p)}let g=t.getInputValues(),y=t.getOutputValues(),_=0;for(let x of g)if(x.type instanceof T){let A=x.type.sizeInBytes();A!==X&&(_+=A)}for(let x of y)if(x.type instanceof T){let A=x.type.sizeInBytes();A!==X&&(_+=A)}let w=0;for(let x of t.ops)for(let A=0;A<x.numResults;A++){let S=x.getResult(A),E=0;for(let R of S.uses())if(s.has(R.user)&&(E++,E>1))break;if(E>1){let R=S.type;if(R instanceof T){let v=R.sizeInBytes();v!==X&&(w+=v)}}}let b=0;return l&&u&&h>0&&f>0&&(b=Math.abs(h-f)),{unfusedFLOPs:r,unfusedBytes:i,fusedFLOPs:r+o,fusedBytes:_,recomputeCost:o,memorySaved:i-_,launchSaved:(t.size-1)*this.launchOverheadUs,registerPressure:d*this.registerBytesPerOp,sharedMemoryUsage:w,parallelismLoss:b,libraryCallLoss:a}}shouldFuse(t){if(t.size<2)return{fuse:!1,reason:"group too small"};if(this.policy&&typeof this.policy.shouldFuse=="function"){let n=this.policy.shouldFuse(t,this);if(n)return n}let s=this.estimateGroupCost(t);if(s.libraryCallLoss>0)return{fuse:!1,reason:"fusion would lose library call opportunity",cost:s};if(s.registerPressure>this.maxRegistersPerThread)return{fuse:!1,reason:`register pressure ${s.registerPressure} exceeds limit ${this.maxRegistersPerThread}`,cost:s};if(s.sharedMemoryUsage>this.maxSharedMemory)return{fuse:!1,reason:`shared memory ${s.sharedMemoryUsage} exceeds limit ${this.maxSharedMemory}`,cost:s};if(t.size>this.maxCodeSizeOps)return{fuse:!1,reason:`code size ${t.size} exceeds limit ${this.maxCodeSizeOps}`,cost:s};if(s.memorySaved<=0&&s.launchSaved<=0)return{fuse:!1,reason:"no memory or launch benefit",cost:s};if(s.parallelismLoss>0){let n=s.memorySaved+s.launchSaved*1e3;if(s.parallelismLoss>n*this.minBenefitRatio)return{fuse:!1,reason:"parallelism loss outweighs fusion benefit",cost:s}}return{fuse:!0,reason:`saves ${s.memorySaved} bytes, ${s.launchSaved}us launch`,cost:s}}_outputElements(t){for(let s=0;s<t.numResults;s++){let n=t.getResult(s).type;if(n instanceof T){let r=n.numel();if(r!==X)return r}}return 0}}});function zu(e){let t=null;for(let s of e.ops())t=s;return t&&t.opName==="yield"?t:null}function Uu(e){let t=0,s=e.regions[0]?.entryBlock;if(!s)return 0;for(let n of s.ops())n.opName!=="yield"&&t++;return t}function Vu(e){let t=0,s=e.regions[0]?.entryBlock;if(!s)return 0;for(let n of s.ops()){let r=rt.get(n.opName);r&&r.isReduction&&t++}return t}function Gu(e){let t=e.regions[0]?.entryBlock;if(!t)return!1;for(let s of t.ops())if(s.opName!=="yield"&&!Za(s.opName))return!1;return!0}function i3(e){let t=new Map,s=0;for(let n=e.firstOp;n;n=n._next)t.set(n,s++);return t}function qu(e){let t=i3(e);return(s,n)=>{let r=t.get(s),i=t.get(n);return r!==void 0&&i!==void 0&&r<i}}function ju(e,t){let s=new Array(e.numOperands);for(let n=0;n<e.numOperands;n++){let r=e.getOperand(n);s[n]=t.get(r)||r}return s}var mp=I(()=>{rs();Nc();c(zu,"getYieldOp");c(Uu,"countInnerOps");c(Vu,"countReductions");c(Gu,"allInnerOpsFusable");c(i3,"blockPositionIndex");c(qu,"makeComesBefore");c(ju,"remapOperands")});var Dy,TI=I(()=>{hs();xn();No();Du();eA();sA();mp();ps();Dy=class extends Mt{static{c(this,"FusionPass")}constructor(t={}){super("FusionPass");let s=t.target||{};this.legality=new Ly({maxFusionSize:s.maxFusionSize||t.maxFusionSize,maxSharedMemory:s.sharedMemoryBytes||t.maxSharedMemory,libraryOps:s.libraryOps||t.libraryOps,allowReductionFusion:t.allowReductionFusion}),this.costModel=new $u({memoryBandwidthGBs:s.memoryBandwidthGBs,computeTFLOPs:s.computeTFLOPs,maxRegistersPerThread:s.registersPerThread,maxSharedMemory:s.sharedMemoryBytes,libraryOps:s.libraryOps,policy:s.getAttr?s.getAttr("fusionPolicy"):null,...t.cost}),this.groupBuilder=new Py(this.legality)}run(t,s){let n=this.groupBuilder.buildAllGroups(t),r=[];for(let o of n)this._createsCycle(t,o)?this._traceDecision(o,!1,"fusing would create a dependency cycle"):r.push(o);let i=[];for(let o of r){if(!o.allOpsInlineFusable()){this._traceDecision(o,!1,"group contains ops without inline fusion support");continue}let a=this.costModel.shouldFuse(o);this._traceDecision(o,a.fuse,a.reason),a.fuse&&i.push(o)}if(i.length===0)return et.UNCHANGED;for(let o of i)this._materializeFusion(t,o);return et.CHANGED}_traceDecision(t,s,n){if(!this.trace||this.trace.level<at.DEBUG)return;let r=t.ops.map(i=>i.opName);this.trace.emit({type:"fusion_decision",passName:this.name,groupSize:t.ops.length,ops:r,anchor:r[r.length-1]||null,fuse:s,reason:n||null,level:at.DEBUG}),this.trace.explain("fusion",r.join("+"),s?"fused":"not-fused",n||null,{groupSize:r.length})}_createsCycle(t,s){let n=s.opSet,r=s.getOutputValues(),i=new Set,o=[];for(let l of r)for(let u of l.uses())n.has(u.user)||(o.push(u.user),i.add(u.user));let a=0;for(;a<o.length;){let l=o[a++];if(n.has(l))return!0;for(let u=0;u<l.numResults;u++)for(let f of l.getResult(u).uses())i.has(f.user)||(i.add(f.user),o.push(f.user))}return!1}_materializeFusion(t,s){let n=this._topologicalSort(s);if(n===null||n.length===0)return;s._inputValues=null,s._outputValues=null;let r=s.getInputValues(),i=s.getOutputValues(),o=r.map(_=>_.type),a=i.map(_=>_.type),l=new Se,u=new ns(o);l.addBlock(u);let f=new Map;for(let _=0;_<r.length;_++)f.set(r[_],u.arguments[_]);for(let _ of n){let w=[];for(let S=0;S<_.numOperands;S++){let E=_.getOperand(S),R=f.get(E);w.push(R!==void 0?R:E)}let b=_.results.map(S=>S.type),x=_.regions.length>0?_.regions.map(S=>ui(S)):null,A=new bt(_.opName,w,b,new Map(_.attributes),x);u.pushOp(A);for(let S=0;S<_.numResults;S++)f.set(_.getResult(S),A.getResult(S))}let h=i.map(_=>{let w=f.get(_);if(w===void 0)throw new Error("Fusion materialization: output value not found in valueMap");return w}),p=new bt("yield",h,[]);u.pushOp(p);let d=qu(n[0].parentBlock),m=null;for(let _ of r){let w=_.definingOp;!w||s.hasOp(w)||(!m||!d(w,m))&&(m=w)}let g=new bt("fusion",r,a,{fusion_kind:s.kind||zt.ELEMENTWISE},[l]),y=n[0].parentBlock;if(y){m?y.insertAfter(g,m):y.insertBefore(g,n[0]);for(let _=0;_<i.length;_++)i[_].replaceAllUsesWith(g.getResult(_));for(let _ of n)_.dropAllOperands(),_.parentBlock&&_.parentBlock.removeOp(_)}}_topologicalSort(t){let s=[],n=new Set,r=new Set,i=c(o=>{if(n.has(o))return!0;if(r.has(o))return!1;r.add(o);for(let a=0;a<o.numOperands;a++){let l=o.getOperand(a).definingOp;if(l&&t.hasOp(l)&&!i(l))return!1}return r.delete(o),n.add(o),s.push(o),!0},"visit");for(let o of t.ops)if(!i(o))return null;return s}}});function II(e){if(nA.has(e.opName))return!0;let t=rt.get(e.opName);return t!==null&&t.isElementwise}function a3(e,t){let s=e.getOperand(0).definingOp;return s&&t.has(s)&&!nA.has(s.opName)?e.getOperand(1).definingOp:s}function l3(e,t){if(!e.parentBlock||e.parentBlock!==t.parentBlock)return!1;let s=e.parentBlock.firstOp;for(;s;){if(s===e)return!0;if(s===t)return!1;s=s._next}return!1}function c3(e,t){for(let s of e)if(s!==t){for(let n=0;n<s.numResults;n++)for(let r of s.getResult(n).uses())if(!e.has(r.user))return!0}return!1}function u3(e,t){let s=rt.get(e.opName),n=s&&s.getAttr("epilogueTag");return n?n(e,t):"activation"}function f3(e){let t=[],s=new Set,n=new Set;function r(p){if(!(n.has(p)||p===e)){n.add(p);for(let d=0;d<p.numOperands;d++){let m=p.getOperand(d).definingOp;m&&m!==e&&!n.has(m)&&nA.has(m.opName)&&r(m)}t.push(p),s.add(p)}}c(r,"absorb");let i=[],o=e.getResult(0);for(let p of o.uses())II(p.user)&&i.push(p.user);for(;i.length>0;){let p=i.pop();if(!n.has(p)){r(p);for(let d=0;d<p.numResults;d++)for(let m of p.getResult(d).uses())!n.has(m.user)&&II(m.user)&&i.push(m.user)}}let a=[];for(let p of t)MI.has(p.opName)||OI.has(p.opName)||a.push(u3(p,s));let l=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(!s.has(g.user)){l=d,p=-1;break}if(p<0)break}}let u=new Set;for(let p=0;p<e.numOperands;p++)u.add(e.getOperand(p));let f=[],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!==e&&(g&&s.has(g)||u.has(m)||f.push(m))}return{chain:t,chainSet:s,tags:a,lastOp:l,extras:f}}var OI,MI,nA,o3,Fy,LI=I(()=>{hs();xn();rs();ps();OI=new Set(["constant","scalar_constant"]),MI=new Set(["broadcast_in_dim","broadcast"]),nA=new Set([...OI,...MI]);c(II,"isEpilogueCandidate");o3=new Map([["add",(e,t)=>t.has(a3(e,t))?"residual_add":"bias"],["sub",()=>"bias"],["mul",()=>"scale"],["maximum",()=>"relu"],["clamp",()=>"clamp"],["neg",()=>"neg"],["exp",()=>"exp"],["tanh",()=>"tanh"],["sqrt",()=>"sqrt"],["abs",()=>"abs"],["log",()=>"log"]]);c(a3,"resolveOtherOperand");for(let[e,t]of o3)rt.has(e)&&rt.registerOpAttr(e,"epilogueTag",t);c(l3,"comesBefore");c(c3,"hasEscapingUse");c(u3,"classifyTag");c(f3,"collectChainAndAnalyze");Fy=class extends Mt{static{c(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 et.UNCHANGED;let s=!1,n=[];for(let r of t.ops()){let i=rt.get(r.opName);i&&i.isOutEWiseFusable&&n.push(r)}for(let r of n){let i=f3(r);if(i.chain.length===0||i.chain.length>this.maxEpilogueOps)continue;let{chain:o,tags:a,lastOp:l,extras:u}=i,f=0;for(let w of a)w==="bias"||w==="residual_add"||w==="scale"?f++:w==="clamp"&&(f+=2);if(f!==u.length)continue;let h=[r.getOperand(0),r.getOperand(1),...u],p=l.getResult(0).type,d=new Map(r.attributes);d.set("epilogue_ops",o.map(w=>w.opName)),d.set("epilogue_tags",a),d.set("num_dot_operands",2),d.set("num_extra_inputs",u.length);let m=new bt("fused_dot_epilogue",h,[p],d),g=r.parentBlock;if(!g)continue;let y=new Set(o);if(y.add(r),c3(y,l))continue;let _=null;for(let w of h){let b=w.definingOp;!b||y.has(b)||(!_||!l3(b,_))&&(_=b)}_&&_.parentBlock===g?g.insertAfter(m,_):g.insertBefore(m,r),l.getResult(0).replaceAllUsesWith(m.getResult(0));for(let w=o.length-1;w>=0;w--)o[w].dropAllOperands(),o[w].parentBlock&&o[w].parentBlock.removeOp(o[w]);r.dropAllOperands(),r.parentBlock&&r.parentBlock.removeOp(r),s=!0}return this.trace&&this.trace.level>=at.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,dotsFound:n.length,changed:s,level:at.DEBUG}),s?et.CHANGED:et.UNCHANGED}}});var $y,PI=I(()=>{hs();xn();No();It();rs();ps();Du();mp();$y=class extends Mt{static{c(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 s=[];for(let l of t.ops())l.opName==="fusion"&&s.push(l);if(s.length<2)return et.UNCHANGED;let n=new Set(s),r=this._buildProducerConsumerEdges(s,n);if(r.length===0)return et.UNCHANGED;let i=!1,o=0,a=new Set;for(let{producer:l,consumer:u,sharedResults:f}of r)a.has(l)||a.has(u)||this._canMerge(l,u)&&this._shouldMerge(l,u,f)&&(this._mergeCreatesCycle(l,u,n)||(this._merge(l,u,f),a.add(l),a.add(u),i=!0,o++));return this.trace&&this.trace.level>=at.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:s.length,edges:r.length,mergeCount:o,level:at.DEBUG}),i?et.CHANGED:et.UNCHANGED}_buildProducerConsumerEdges(t,s){let n=[],r=new Map;for(let i of t)for(let o=0;o<i.numResults;o++){let a=i.getResult(o);for(let l of a.uses()){if(!s.has(l.user))continue;let u=l.user;if(u===i)continue;let f=`${i.id}|${u.id}`,h=r.get(f);h||(h={producer:i,consumer:u,sharedResults:new Map},r.set(f,h),n.push(h)),h.sharedResults.set(o,l.operandIndex)}}return n}_canMerge(t,s){let n=Uu(t),r=Uu(s);if(n+r>this.maxFusionSize)return!1;let i=0;return i+=Vu(t),i+=Vu(s),!(i>this.maxReductions||!Gu(t)||!Gu(s))}_shouldMerge(t,s,n){let r=0;for(let[l]of n){let u=t.getResult(l);if(u.type instanceof T){let f=u.type.sizeInBytes();f!==X&&(r+=f)}}let i=!0;for(let l=0;l<t.numResults;l++){let u=t.getResult(l);for(let f of u.uses())if(f.user!==s){i=!1;break}if(!i)break}if(i)return!0;let o=this._estimateRecomputeCost(t);return r+this.launchOverheadUs*1e3>o}_mergeCreatesCycle(t,s,n){let r=new Set,i=[];for(let a=0;a<s.numOperands;a++){let u=s.getOperand(a).definingOp;u&&u!==t&&u.opName!=="constant"&&(r.has(u)||(r.add(u),i.push(u)))}let o=0;for(;o<i.length;){let a=i[o++];if(a===t)return!0;for(let l=0;l<a.numOperands;l++){let u=a.getOperand(l).definingOp;u&&!r.has(u)&&(r.add(u),i.push(u))}}return!1}_merge(t,s,n){let r=t.regions[0].entryBlock,i=s.regions[0].entryBlock,o=zu(r),a=zu(i);if(!o||!a)return;let l=new Map;for(let[v,k]of n)l.set(k,v);let u=[],f=new Map;for(let v=0;v<t.numOperands;v++)f.set(v,u.length),u.push(t.getOperand(v));let h=new Map;for(let v=0;v<s.numOperands;v++){if(l.has(v))continue;let k=s.getOperand(v),B=-1;for(let N=0;N<u.length;N++)if(u[N]===k){B=N;break}B>=0?h.set(v,B):(h.set(v,u.length),u.push(k))}let p=u.map(v=>v.type),d=new Se,m=new ns(p);d.addBlock(m);let g=new Map;for(let v=0;v<r.arguments.length;v++)g.set(r.arguments[v],m.arguments[f.get(v)]);for(let v of r.ops()){if(v.opName==="yield")continue;let k=ju(v,g),B=v.results.map(O=>O.type),N=v.regions.length>0?v.regions.map(O=>ui(O)):null,C=new bt(v.opName,k,B,new Map(v.attributes),N);m.pushOp(C);for(let O=0;O<v.numResults;O++)g.set(v.getResult(O),C.getResult(O))}for(let v=0;v<o.numOperands;v++){let k=o.getOperand(v),B=g.get(k)||k;for(let[N,C]of l)C===v&&g.set(i.arguments[N],B)}for(let v=0;v<i.arguments.length;v++){if(l.has(v))continue;let k=h.get(v);k!==void 0&&g.set(i.arguments[v],m.arguments[k])}for(let v of i.ops()){if(v.opName==="yield")continue;let k=ju(v,g),B=v.results.map(O=>O.type),N=v.regions.length>0?v.regions.map(O=>ui(O)):null,C=new bt(v.opName,k,B,new Map(v.attributes),N);m.pushOp(C);for(let O=0;O<v.numResults;O++)g.set(v.getResult(O),C.getResult(O))}let y=[],_=[],w=[];for(let v=0;v<t.numResults;v++){let k=!1;for(let B of t.getResult(v).uses())if(B.user!==s){k=!0;break}if(k){let B=o.getOperand(v),N=g.get(B)||B;w.push({resultIdx:v,value:N}),y.push(N),_.push(t.getResult(v).type)}}for(let v=0;v<s.numResults;v++){let k=a.getOperand(v),B=g.get(k)||k;y.push(B),_.push(s.getResult(v).type)}let b=new bt("yield",y,[]);m.pushOp(b);let x=[];for(let v of m.ops())v.opName!=="yield"&&x.push(v);let A=Pu(x),S=new bt("fusion",u,_,{fusion_kind:A},[d]),E=s;if(!E.parentBlock)return;E.parentBlock.insertBefore(S,E);let R=0;for(let{resultIdx:v}of w)t.getResult(v).replaceAllUsesWith(S.getResult(R)),R++;for(let v=0;v<s.numResults;v++)s.getResult(v).replaceAllUsesWith(S.getResult(R)),R++;t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t),s.dropAllOperands(),s.parentBlock&&s.parentBlock.removeOp(s)}_estimateRecomputeCost(t){let s=0,n=t.regions[0]?.entryBlock;if(!n)return 0;for(let r of n.ops()){if(r.opName==="yield")continue;let i=rt.get(r.opName);if(i&&i.getFlops)s+=i.getFlops(r);else for(let o=0;o<r.numResults;o++){let a=r.getResult(o).type;if(a instanceof T){let l=a.numel();l!==X&&(s+=l);break}}}return s}}});function h3(e,t){let s=Math.min(e.id,t.id),n=Math.max(e.id,t.id);return`${s}|${n}`}var zy,DI=I(()=>{hs();xn();No();It();Du();ps();mp();zy=class extends Mt{static{c(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 s=[];for(let a of t.ops())a.opName==="fusion"&&s.push(a);if(s.length<2)return et.UNCHANGED;let n=this._findCandidates(s);if(n.length===0)return et.UNCHANGED;let r=!1,i=0,o=new Set;for(let{left:a,right:l,sharedInputs:u,sharedBytes:f}of n)o.has(a)||o.has(l)||this._canMerge(a,l)&&(this._mergeCreatesCycle(a,l)||(this._mergeMultiOutput(a,l,u),o.add(a),o.add(l),r=!0,i++));return this.trace&&this.trace.level>=at.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:s.length,candidates:n.length,mergeCount:i,level:at.DEBUG}),r?et.CHANGED:et.UNCHANGED}_findCandidates(t){let s=new Map;for(let i of t)for(let o=0;o<i.numOperands;o++){let l=i.getOperand(o).id,u=s.get(l);u||(u=[],s.set(l,u)),u.push(i)}let n=new Map;for(let[i,o]of s)if(!(o.length<2))for(let a=0;a<o.length;a++)for(let l=a+1;l<o.length;l++){let u=o[a],f=o[l];if(u===f)continue;let h=h3(u,f),p=n.get(h);p||(p={left:u,right:f,sharedInputs:new Set,sharedBytes:0},n.set(h,p)),p.sharedInputs.add(i)}let r=[];for(let i of n.values()){let o=0;for(let a of i.sharedInputs){let l=s.get(a);if(!l||l.length===0)continue;let u=l[0];for(let f=0;f<u.numOperands;f++){let h=u.getOperand(f);if(h.id===a&&h.type instanceof T){let p=h.type.sizeInBytes();p!==X&&(o+=p);break}}}i.sharedBytes=o,o>=this.minSharedInputBytes&&r.push(i)}return r.sort((i,o)=>o.sharedBytes-i.sharedBytes),r}_canMerge(t,s){if(this._hasProducerConsumerEdge(t,s)||this._hasProducerConsumerEdge(s,t))return!1;let n=Uu(t),r=Uu(s);if(n+r>this.maxFusionSize||t.numResults+s.numResults>this.maxOutputs)return!1;let o=0;return o+=Vu(t),o+=Vu(s),!(o>this.maxReductions||!Gu(t)||!Gu(s))}_mergeCreatesCycle(t,s){let n=new Set,r=[];for(let o=0;o<t.numResults;o++){let a=t.getResult(o);for(let l of a.uses())l.user!==s&&!n.has(l.user)&&(n.add(l.user),r.push(l.user))}let i=0;for(;i<r.length;){let o=r[i++];if(o===s)return!0;for(let a=0;a<o.numResults;a++)for(let l of o.getResult(a).uses())n.has(l.user)||(n.add(l.user),r.push(l.user))}n.clear(),r.length=0;for(let o=0;o<s.numResults;o++){let a=s.getResult(o);for(let l of a.uses())l.user!==t&&!n.has(l.user)&&(n.add(l.user),r.push(l.user))}for(i=0;i<r.length;){let o=r[i++];if(o===t)return!0;for(let a=0;a<o.numResults;a++)for(let l of o.getResult(a).uses())n.has(l.user)||(n.add(l.user),r.push(l.user))}return!1}_mergeMultiOutput(t,s,n){let r=t.regions[0].entryBlock,i=s.regions[0].entryBlock,o=zu(r),a=zu(i);if(!o||!a)return;let l=[],u=new Map,f=new Map;for(let k=0;k<t.numOperands;k++){let B=t.getOperand(k),N=B.id,C=u.get(N);C===void 0&&(C=l.length,u.set(N,C),l.push(B)),f.set(k,C)}let h=new Map;for(let k=0;k<s.numOperands;k++){let B=s.getOperand(k),N=B.id,C=u.get(N);C===void 0&&(C=l.length,u.set(N,C),l.push(B)),h.set(k,C)}let p=l.map(k=>k.type),d=new Se,m=new ns(p);d.addBlock(m);let g=new Map;for(let k=0;k<r.arguments.length;k++)g.set(r.arguments[k],m.arguments[f.get(k)]);for(let k of r.ops()){if(k.opName==="yield")continue;let B=ju(k,g),N=k.results.map(O=>O.type),C=new bt(k.opName,B,N,new Map(k.attributes));m.pushOp(C);for(let O=0;O<k.numResults;O++)g.set(k.getResult(O),C.getResult(O))}for(let k=0;k<i.arguments.length;k++)g.set(i.arguments[k],m.arguments[h.get(k)]);for(let k of i.ops()){if(k.opName==="yield")continue;let B=ju(k,g),N=k.results.map(O=>O.type),C=new bt(k.opName,B,N,new Map(k.attributes));m.pushOp(C);for(let O=0;O<k.numResults;O++)g.set(k.getResult(O),C.getResult(O))}let y=[],_=[];for(let k=0;k<o.numOperands;k++){let B=o.getOperand(k);y.push(g.get(B)||B),_.push(t.getResult(k).type)}for(let k=0;k<a.numOperands;k++){let B=a.getOperand(k);y.push(g.get(B)||B),_.push(s.getResult(k).type)}let w=new bt("yield",y,[]);m.pushOp(w);let b=[];for(let k of m.ops())k.opName!=="yield"&&b.push(k);let x=Pu(b),A=new bt("fusion",l,_,{fusion_kind:x},[d]),S=t.parentBlock;if(!S)return;let E=qu(S),R=null;for(let k of l){let B=k.definingOp;!B||B===t||B===s||(!R||!E(B,R))&&(R=B)}R&&R.parentBlock===S?S.insertAfter(A,R):S.insertBefore(A,t);for(let k=0;k<t.numResults;k++)t.getResult(k).replaceAllUsesWith(A.getResult(k));let v=t.numResults;for(let k=0;k<s.numResults;k++)s.getResult(k).replaceAllUsesWith(A.getResult(v+k));t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t),s.dropAllOperands(),s.parentBlock&&s.parentBlock.removeOp(s)}_hasProducerConsumerEdge(t,s){for(let n=0;n<t.numResults;n++)for(let r of t.getResult(n).uses())if(r.user===s)return!0;return!1}};c(h3,"pairKey")});var rA,ne,Ci=I(()=>{rA=class{static{c(this,"UseDefResult")}constructor(t,s,n,r,i){this.topologicalOrder=t,this.valueToOp=s,this.opUsers=n,this.depth=r,this.height=i}},ne=class{static get name(){return"use_def"}static get depKey(){return"useDef"}static get dependencies(){return[]}static compute(t){let s=[],n=new Map,r=new Map,i=new Set,o=new Set,a=c(f=>{if(i.has(f))return;o.add(f);let h=[{op:f,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&&!i.has(m)){if(o.has(m))throw new Error("Cycle detected in UseDefAnalysis");o.add(m),h.push({op:m,i:0})}continue}o.delete(d),i.add(d),s.push(d),h.pop()}},"visit");for(let f of t.ops()){for(let h=0;h<f.numResults;h++)n.set(f.getResult(h),f);r.set(f,new Set)}let l=new Map,u=new Map;for(let f of t.ops())a(f);for(let f of s){let h=0;for(let p=0;p<f.numOperands;p++){let d=f.getOperand(p).definingOp;if(d){r.get(d).add(f);let m=l.get(d)||0;m+1>h&&(h=m+1)}}l.set(f,h)}for(let f=s.length-1;f>=0;f--){let h=s[f],p=0,d=r.get(h)||new Set;for(let m of d){let g=u.get(m)||0;g+1>p&&(p=g+1)}u.set(h,p)}return new rA(s,n,r,l,u)}}});var iA,Uy,FI=I(()=>{Ci();iA=class{static{c(this,"DominanceResult")}constructor(t){this.idom=t}postDominates(t,s){let n=s;for(;n;){if(n===t)return!0;n=this.idom.get(n)}return!1}immediatePDom(t){return this.idom.get(t)||null}pathToPDom(t){let s=[],n=this.idom.get(t);for(;n&&(s.push(n),n!==this.idom.get(n));)n=this.idom.get(n);return s}},Uy=class{static get name(){return"post_dominance"}static get depKey(){return"postDominance"}static get dependencies(){return[ne]}static compute(t,s={}){let r=(s.useDef||ne.compute(t)).topologicalOrder,i=new Map;for(let m of r)i.set(m,[]);for(let m of r)for(let g=0;g<m.numResults;g++){let y=m.getResult(g);for(let _ of y.uses()){let w=_.user;i.has(w)&&i.get(m).push(w)}}let o=new Set;for(let m of r)(m.opName==="return"||m.opName==="yield")&&o.add(m);let a={opName:"__pdom_root__"},l=Math.max(1,Math.ceil(Math.log2(r.length+2))+1),u=new Map,f=new Map,h=new Map;f.set(a,0),h.set(a,new Array(l).fill(a));let p=c((m,g)=>{u.set(m,g),f.set(m,f.get(g)+1);let y=new Array(l);y[0]=g;for(let _=1;_<l;_++)y[_]=h.get(y[_-1])[_-1];h.set(m,y)},"link"),d=c((m,g)=>{if(f.get(m)<f.get(g)){let _=m;m=g,g=_}let y=f.get(m)-f.get(g);for(let _=0;_<l;_++)y>>_&1&&(m=h.get(m)[_]);if(m===g)return m;for(let _=l-1;_>=0;_--)h.get(m)[_]!==h.get(g)[_]&&(m=h.get(m)[_],g=h.get(g)[_]);return h.get(m)[0]},"lca");for(let m=r.length-1;m>=0;m--){let g=r[m];if(o.has(g)){p(g,a);continue}let y=null;for(let _ of i.get(g))u.has(_)&&(y=y===null?_:d(y,_));y!==null&&p(g,y)}for(let[m,g]of u)g===a&&u.delete(m);return new iA(u,r)}}});var $I,Vy,zI=I(()=>{hs();xn();No();rs();Du();eA();sA();FI();ps();Ci();mp();$I=new Set(["return","yield","constant","scalar_constant"]),Vy=class extends Mt{static{c(this,"DominatorFusionPass")}constructor(t={}){super("DominatorFusionPass"),this.requiredAnalyses=[ne];let s=t.target||{};this.maxFusionSize=s.maxFusionSize||t.maxFusionSize||512,this.maxReductions=t.maxReductions||1,this.libraryOps=s.libraryOps||t.libraryOps||new Set,this.costModel=new $u({memoryBandwidthGBs:s.memoryBandwidthGBs,computeTFLOPs:s.computeTFLOPs,maxRegistersPerThread:s.registersPerThread,maxSharedMemory:s.sharedMemoryBytes,libraryOps:s.libraryOps,...t.cost})}run(t,s){let n=s?s.getAnalysis(ne,t):ne.compute(t),r=Uy.compute(t,{useDef:n}),i=n.topologicalOrder,o=this._buildGroups(i,r);if(o.length===0)return et.UNCHANGED;let a=[];for(let l of o){if(!l.allOpsInlineFusable()){this._explain(l,!1,"group contains ops without inline fusion support");continue}let u=this.costModel.shouldFuse(l);this._explain(l,u.fuse,u.reason),u.fuse&&a.push(l)}if(a.length===0)return et.UNCHANGED;for(let l of a)this._materialize(t,l);return this.trace&&this.trace.level>=at.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,groupsBuilt:o.length,groupsFused:a.length,level:at.DEBUG}),et.CHANGED}_explain(t,s,n){if(!this.trace||!this.trace.explainsEnabled)return;let r=t.ops.map(i=>i.opName);this.trace.explain("fusion",r.join("+"),s?"fused":"not-fused",n||null,{groupSize:r.length,strategy:"dominator"})}_buildGroups(t,s){let n=new Map,r=[],i=0;for(let u=t.length-1;u>=0;u--){let f=t[u];if($I.has(f.opName))continue;let h=$l(f);if(h===zt.OPAQUE||this.libraryOps.has(f.opName))continue;let p=s.immediatePDom(f);if(!p||$I.has(p.opName))continue;let d=$l(p);if(d===zt.OPAQUE||this.libraryOps.has(p.opName)||!this._canFusePatterns(h,d)||!this._pathAllFusable(f,p,t,s))continue;let m=n.get(p);if(m){if(m.size+1>this.maxFusionSize||!this._checkReductionLimit(m,f))continue;m.addOp(f),n.set(f,m),this._absorbIntermediates(f,p,m,n,t,s)}else{let g=n.get(f);if(g){if(g.size+1>this.maxFusionSize||!this._checkReductionLimit(g,p))continue;g.addOp(p),n.set(p,g),this._absorbIntermediates(f,p,g,n,t,s)}else{let y=new Fu(i++);if(y.addOp(f),y.addOp(p),!this._checkGroupReductions(y))continue;n.set(f,y),n.set(p,y),r.push(y),this._absorbIntermediates(f,p,y,n,t,s)}}}let o=new Set,a=[];for(let u of r)o.has(u)||u.size<2||(o.add(u),u.classifyKind(),a.push(u));for(let u of n.values())o.has(u)||u.size<2||(o.add(u),u.classifyKind(),a.push(u));let l=[];for(let u of a){if(!this._checkGroupReductions(u)){this._explain(u,!1,`group exceeds the ${this.maxReductions}-reduction limit`);continue}if(this._createsCycle(u)){this._explain(u,!1,"fusing would create a dependency cycle");continue}l.push(u)}return l}_createsCycle(t){let s=new Set;for(let i of t.getInputValues())i.definingOp&&!t.hasOp(i.definingOp)&&s.add(i.definingOp);if(s.size===0)return!1;let n=new Set,r=[];for(let i of t.getOutputValues())for(let o of i.uses())t.hasOp(o.user)||r.push(o.user);for(;r.length>0;){let i=r.pop();if(!n.has(i)){if(n.add(i),s.has(i))return!0;for(let o=0;o<i.numResults;o++)for(let a of i.getResult(o).uses())t.hasOp(a.user)||r.push(a.user)}}return!1}_canFusePatterns(t,s){return tA(t,s)}_pathAllFusable(t,s,n,r){let i=new Set,o=[t];for(i.add(t),i.add(s);o.length>0;){let a=o.pop();for(let l=0;l<a.numResults;l++)for(let u of a.getResult(l).uses()){let f=u.user;if(f===s||i.has(f)||!r.postDominates(s,f))continue;if(i.add(f),$l(f)===zt.OPAQUE||this.libraryOps.has(f.opName))return!1;o.push(f)}}return!0}_absorbIntermediates(t,s,n,r,i,o){let a=new Set,l=[t];for(a.add(t),a.add(s);l.length>0;){let u=l.pop();for(let f=0;f<u.numResults;f++)for(let h of u.getResult(f).uses()){let p=h.user;p===s||a.has(p)||!o.postDominates(s,p)||(a.add(p),$l(p)===zt.OPAQUE||this.libraryOps.has(p.opName))||(n.size<this.maxFusionSize&&this._checkReductionLimit(n,p)&&(n.addOp(p),r.set(p,n)),l.push(p))}}}_checkGroupReductions(t){let s=0;for(let n of t.ops){let r=rt.get(n.opName);r&&r.isReduction&&s++}return s<=this.maxReductions}_checkReductionLimit(t,s){let n=rt.get(s.opName);if(!n||!n.isReduction)return!0;let r=0;for(let i of t.ops){let o=rt.get(i.opName);o&&o.isReduction&&r++}return r<this.maxReductions}_materialize(t,s){let n=this._topoSort(s);if(!n||n.length===0)return;let r=s.getInputValues(),i=s.getOutputValues(),o=r.map(y=>y.type),a=i.map(y=>y.type),l=new Se,u=new ns(o);l.addBlock(u);let f=new Map;for(let y=0;y<r.length;y++)f.set(r[y],u.arguments[y]);for(let y of n){let _=[];for(let A=0;A<y.numOperands;A++){let S=y.getOperand(A),E=f.get(S);_.push(E!==void 0?E:S)}let w=y.results.map(A=>A.type),b=y.regions.length>0?y.regions.map(A=>ui(A)):null,x=new bt(y.opName,_,w,new Map(y.attributes),b);u.pushOp(x);for(let A=0;A<y.numResults;A++)f.set(y.getResult(A),x.getResult(A))}let h=i.map(y=>{let _=f.get(y);if(_===void 0)throw new Error("DominatorFusion: output not found in valueMap");return _});u.pushOp(new bt("yield",h,[]));let p=new bt("fusion",r,a,{fusion_kind:s.kind||zt.ELEMENTWISE},[l]),d=n[0].parentBlock;if(!d)return;let m=qu(d),g=null;for(let y of r){let _=y.definingOp;!_||s.hasOp(_)||(!g||!m(_,g))&&(g=_)}g&&g.parentBlock===d?d.insertAfter(p,g):d.insertBefore(p,n[0]);for(let y=0;y<i.length;y++)i[y].replaceAllUsesWith(p.getResult(y));for(let y of n)y.dropAllOperands(),y.parentBlock&&y.parentBlock.removeOp(y)}_topoSort(t){let s=[],n=new Set,r=new Set,i=c(o=>{if(n.has(o))return!0;if(r.has(o))return!1;r.add(o);for(let a=0;a<o.numOperands;a++){let l=o.getOperand(a).definingOp;if(l&&t.hasOp(l)&&!i(l))return!1}return r.delete(o),n.add(o),s.push(o),!0},"visit");for(let o of t.ops)if(!i(o))return null;return s}}});function p3(e,t){return e===t?!0:!e||!t?!1:typeof e.equals=="function"?e.equals(t):!1}var ma,Gy,UI=I(()=>{It();ma=class{static{c(this,"LayoutPreference")}constructor(t,s,n=0){this.inputs=t,this.outputs=s,this.cost=n}},Gy=class{static{c(this,"LayoutPolicy")}constructor(t){this.target=t,this._rules=new Map,this._initDefaultRules()}registerRule(t,s){this._rules.set(t,s)}getPreference(t){let s=this._rules.get(t.opName);return s?s(t,this.target):null}estimateConversionCost(t,s,n){if(!(n instanceof T)||p3(t,s))return 0;let r=n.numel();return r<0?1024:r*2}estimateBenefit(t,s,n){if(!(s instanceof T))return 0;let r=s.numel();if(r<0)return 0;let i=t.opName;if(i==="dot"||i==="conv"||i==="matmul")return r*4*n;if(i==="reduce")return r*2*n;let o=this.target.cacheLineBytes||64;return r*4<=o*4?0:Math.floor(r*.5)}_initDefaultRules(){this._rules.set("conv",(t,s)=>{let r=t.getOperand(0).type?.rank||4;if(s.preferredConvLayout)return new ma([s.preferredConvLayout,null],[s.preferredConvLayout]);if(s.isGPU()&&r===4){let i=new Ve([0,2,3,1]);return new ma([i,null],[i])}if(s.isCPU()&&r===4){let i=new Ve([0,2,3,1]);return new ma([i,null],[i])}return null}),this._rules.set("dot",(t,s)=>{let n=t.getOperand(0).type,r=t.getOperand(1).type;if(!n||!r)return null;let i=Ve.rowMajor(n.rank);if(s.isCPU()&&r.rank===2){let a=Ve.columnMajor(r.rank);return new ma([i,a],[i])}let o=Ve.rowMajor(r.rank);return new ma([i,o],[i])}),this._rules.set("reduce",(t,s)=>{let n=t.getResult(0).type;return n?new ma([null],[Ve.rowMajor(n.rank)]):null})}};c(p3,"layoutEquals")});function VI(e,t){let s=new Map,n=null,r=0;for(let o=0;o<e.numOperands;o++){let a=e.getOperand(o),l=t.get(a);if(!l)continue;let u=l.hash?l.hash():0,f=(s.get(u)||0)+1;s.set(u,f),f>r&&(r=f,n=l)}if(n)return n;let i=e.getResult(0);return i&&i.type instanceof T?Ve.rowMajor(i.type.rank):Ve.rowMajor(1)}function d3(e,t){return e===t?!0:!e||!t?!1:typeof e.equals=="function"?e.equals(t):!1}function GI(e){if(e instanceof Ve)return e;if(e&&typeof e.toLayout=="function")try{return e.toLayout()}catch{}return e&&e.baseOrder&&!e.isBlocked?.()?new Ve(e.baseOrder):e&&e.order?new Ve(e.order):null}var oA,qy,qI=I(()=>{Ci();It();rs();He();oA=class{static{c(this,"LayoutAnalysisResult")}constructor(t,s,n){this.assignments=t,this.conversions=s,this.totalCost=n}},qy=class{static get name(){return"layout"}static get depKey(){return"layout"}static get dependencies(){return[ne]}static compute(t,s,n){let r=s.useDef,i=new Map,o=r.topologicalOrder;for(let u of t.args)u.type instanceof T&&i.set(u,u.type.layout||Ve.rowMajor(u.type.rank));for(let u=0;u<o.length;u++){let f=o[u];if(f.opName==="return"||f.opName==="yield")continue;let h=n?n.getPreference(f):null;if(h&&h.outputs.length>0){for(let m=0;m<f.numResults;m++){let g=f.getResult(m);if(!(g.type instanceof T))continue;let y=h.outputs[m]||null;y?i.set(g,y):i.set(g,VI(f,i))}continue}let p=rt.get(f.opName),d=p&&p.hasTrait(j.ELEMENTWISE);for(let m=0;m<f.numResults;m++){let g=f.getResult(m);g.type instanceof T&&(d?i.set(g,VI(f,i)):i.set(g,Ve.rowMajor(g.type.rank)))}}let a=[],l=0;for(let u=0;u<o.length;u++){let f=o[u],h=n?n.getPreference(f):null;if(h)for(let p=0;p<f.numOperands;p++){let d=f.getOperand(p);if(!(d.type instanceof T))continue;let m=i.get(d);if(!m)continue;let g=h.inputs[p];if(!g||d3(m,g))continue;let y=GI(m),_=GI(g);!y||!_||y.equals(_)||(a.push({value:d,consumer:f,operandIdx:p,from:y,to:_}),n&&(l+=n.estimateConversionCost(y,_,d.type)))}}return new oA(i,a,l)}};c(VI,"resolveFromInputs");c(d3,"layoutEquals");c(GI,"toPlainLayout")});function m3(e,t,s){let n=e.id,r=t.hash?t.hash():0,i=s.hash?s.hash():0;return`${n}:${r}:${i}`}var jy,jI=I(()=>{hs();xn();It();UI();qI();Ci();ps();jy=class extends Mt{static{c(this,"LayoutTransformPass")}constructor(t={}){super("LayoutTransformPass"),this.target=t.target||null,this._policy=null}run(t,s){if(!this.target)return et.UNCHANGED;this._policy||(this._policy=new Gy(this.target));let n=s?s.getAnalysis(ne,t):ne.compute(t),r=qy.compute(t,{useDef:n},this._policy);if(r.conversions.length===0)return et.UNCHANGED;let i=new Map;for(let o=0;o<r.conversions.length;o++){let a=r.conversions[o],{value:l,consumer:u,operandIdx:f,from:h,to:p}=a,d=m3(l,h,p),m=i.get(d);if(!m){let g=this._policy.estimateConversionCost(h,p,l.type),y=l.uses?[...l.uses()].length:1,_=this._policy.estimateBenefit(u,l.type,y);if(g>_)continue;let w=h instanceof Ve?h.order:Array.from({length:l.type.rank},(E,R)=>R),b=p instanceof Ve?p.order:Array.from({length:l.type.rank},(E,R)=>R),x=new T(l.type.shape,l.type.dtype,p),A=new bt("layout_transform",[l],[x],{src_layout:[...w],dst_layout:[...b]}),S=l.definingOp;S&&S.parentBlock?S.parentBlock.insertAfter(A,S):u.parentBlock&&u.parentBlock.insertBefore(A,u),m=A.getResult(0),i.set(d,m)}u.replaceOperand(f,m)}return this.trace&&this.trace.level>=at.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,conversions:r.conversions.length,uniqueTransforms:i.size,level:at.DEBUG}),et.CHANGED}};c(m3,"valueLayoutKey")});function WI(e,t){if(e===t)return!0;if(typeof e=="number"&&typeof t=="number")return e===t;if(typeof e!=typeof t||e.length!==t.length)return!1;for(let s=0;s<e.length;s++)if(e[s]!==t[s])return!1;return!0}function KI(e){let t=0;for(let s=0;s<e.length;s++)t=(t<<5)-t+e.charCodeAt(s)&2147483647;return t}function _3(e){if(typeof e=="number"){let s=new Float64Array([e]),n=new Uint32Array(s.buffer);return(n[0]^n[1])&2147483647}let t=e.length;for(let s=0;s<Math.min(e.length,8);s++){let n=new Float64Array([e[s]]),r=new Uint32Array(n.buffer);t=(t^(r[0]^r[1]))*16777619&2147483647}return t}var Un,g3,y3,Vn,aA=I(()=>{It();Un=Object.freeze({PER_TENSOR_SYMMETRIC:"per_tensor_symmetric",PER_TENSOR_ASYMMETRIC:"per_tensor_asymmetric",PER_CHANNEL:"per_channel",PER_GROUP:"per_group"}),g3=new Set(Object.values(Un)),y3=new Set([Q.I8,Q.UI8]),Vn=class e{static{c(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||Q.I8,this.numBits=t.numBits||ji(this.dtype)*8,this._hash=null}clampRange(){if(this.isSymmetric()){let n=2**(this.numBits-1)-1;return[-n,n]}if(this.dtype===Q.UI8)return[0,2**this.numBits-1];let t=-(2**(this.numBits-1)),s=2**(this.numBits-1)-1;return[t,s]}quantize(t){let[s,n]=this.clampRange(),r=Math.round(t/this.getScalarScale()+this.getScalarZeroPoint());return Math.max(s,Math.min(n,r))}dequantize(t){return(t-this.getScalarZeroPoint())*this.getScalarScale()}quantizeArray(t){let[s,n]=this.clampRange(),r=new Array(t.length);if(this.isPerChannel())throw new Error("Use quantizeArrayPerChannel(floatArr, shape) for per-channel quantization");let i=this.getScalarScale(),o=this.getScalarZeroPoint();for(let a=0;a<t.length;a++)r[a]=Math.max(s,Math.min(n,Math.round(t[a]/i+o)));return r}dequantizeArray(t){let s=new Array(t.length);if(this.isPerChannel())throw new Error("Use dequantizeArrayPerChannel(intArr, shape) for per-channel dequantization");let n=this.getScalarScale(),r=this.getScalarZeroPoint();for(let i=0;i<t.length;i++)s[i]=(t[i]-r)*n;return s}_channelStride(t){let s=1;for(let n=this.axis+1;n<t.length;n++)s*=t[n];return s}quantizeArrayPerChannel(t,s){let[n,r]=this.clampRange(),i=this._channelStride(s),o=s[this.axis],a=new Array(t.length);for(let l=0;l<t.length;l++){let u=Math.floor(l/i)%o,f=this.getScaleForChannel(u),h=this.getZeroPointForChannel(u);a[l]=Math.max(n,Math.min(r,Math.round(t[l]/f+h)))}return a}dequantizeArrayPerChannel(t,s){let n=this._channelStride(s),r=s[this.axis],i=new Array(t.length);for(let o=0;o<t.length;o++){let a=Math.floor(o/n)%r;i[o]=(t[o]-this.getZeroPointForChannel(a))*this.getScaleForChannel(a)}return i}quantizeArrayPerGroup(t){let[s,n]=this.clampRange(),r=new Array(t.length);for(let i=0;i<t.length;i++){let o=Math.floor(i/this.groupSize),a=this.scale[o],l=this.zeroPoint[o];r[i]=Math.max(s,Math.min(n,Math.round(t[i]/a+l)))}return r}dequantizeArrayPerGroup(t){let s=new Array(t.length);for(let n=0;n<t.length;n++){let r=Math.floor(n/this.groupSize);s[n]=(t[n]-this.zeroPoint[r])*this.scale[r]}return s}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===Un.PER_CHANNEL}isPerGroup(){return this.scheme===Un.PER_GROUP}isSymmetric(){return this.scheme===Un.PER_TENSOR_SYMMETRIC}equals(t){return this===t?!0:!(!(t instanceof e)||this.scheme!==t.scheme||this.dtype!==t.dtype||this.numBits!==t.numBits||this.axis!==t.axis||this.groupSize!==t.groupSize||!WI(this.scale,t.scale)||!WI(this.zeroPoint,t.zeroPoint))}hash(){if(this._hash!==null)return this._hash;let t=2166136261;return t=(t^KI(this.scheme))*16777619&2147483647,t=(t^KI(this.dtype))*16777619&2147483647,t=(t^this.numBits)*16777619&2147483647,t=(t^_3(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 e(t)}static fromRange(t,s,n,r=Q.I8,i=8){if(n===Un.PER_TENSOR_SYMMETRIC){let p=Math.max(Math.abs(t),Math.abs(s)),d=2**(i-1)-1,m=p/d||1e-10;return new e({scheme:n,scale:m,zeroPoint:0,dtype:r,numBits:i})}let[o,a]=r===Q.UI8?[0,2**i-1]:[-(2**(i-1)),2**(i-1)-1],u=(s-t||1e-10)/(a-o),f=Math.round(o-t/u),h=Math.max(o,Math.min(a,f));return new e({scheme:n,scale:u,zeroPoint:h,dtype:r,numBits:i})}static fromRangePerChannel(t,s,n,r=Q.I8,i=8){let o=t.length,a=new Float64Array(o),l=new Int32Array(o),u=2**(i-1)-1;for(let f=0;f<o;f++){let h=Math.max(Math.abs(t[f]),Math.abs(s[f]));a[f]=h/u||1e-10,l[f]=0}return new e({scheme:Un.PER_CHANNEL,scale:a,zeroPoint:l,axis:n,dtype:r,numBits:i})}static defaultForActivation(t,s=Q.I8,n=8){return e.fromRange(-6,6,t,s,n)}static fromConstantArray(t,s,n=Q.I8,r=8){let i=1/0,o=-1/0;for(let a=0;a<t.length;a++)t[a]<i&&(i=t[a]),t[a]>o&&(o=t[a]);return isFinite(i)||(i=-1),isFinite(o)||(o=1),i===o&&(i-=.5,o+=.5),e.fromRange(i,o,s,n,r)}static fromConstantArrayPerChannel(t,s,n,r=Q.I8,i=8){let o=s[n],a=1;for(let f=n+1;f<s.length;f++)a*=s[f];let l=new Array(o).fill(1/0),u=new Array(o).fill(-1/0);for(let f=0;f<t.length;f++){let h=Math.floor(f/a)%o,p=t[f];p<l[h]&&(l[h]=p),p>u[h]&&(u[h]=p)}for(let f=0;f<o;f++)isFinite(l[f])||(l[f]=-1),isFinite(u[f])||(u[f]=1),l[f]===u[f]&&(l[f]-=.5,u[f]+=.5);return e.fromRangePerChannel(l,u,n,r,i)}static fromConstantArrayPerGroup(t,s,n=Q.I8,r=4){let i=Math.ceil(t.length/s),o=new Float64Array(i),a=new Int32Array(i),l=2**(r-1)-1;for(let u=0;u<i;u++){let f=u*s,h=Math.min(f+s,t.length),p=0;for(let d=f;d<h;d++){let m=Math.abs(t[d]);m>p&&(p=m)}o[u]=p/l||1e-10,a[u]=0}return new e({scheme:Un.PER_GROUP,scale:o,zeroPoint:a,groupSize:s,dtype:n,numBits:r})}static isQuantizableDtype(t){return y3.has(t)}static isValidScheme(t){return g3.has(t)}};c(WI,"scaleEquals");c(KI,"hashStr");c(_3,"hashScaleValue")});function S3(e){for(let t=0;t<e.numOperands;t++){let s=e.getOperand(t).definingOp;if(s&&s.opName==="constant")return!0}return!1}function A3(e,t){for(let s=0;s<e.numOperands;s++)if(!t.has(e.getOperand(s)))return!1;return e.numOperands>0}function v3(e,t,s){for(let n=0;n<e.numOperands;n++){let r=e.getOperand(n);if(!t.has(r)&&(!(r.type instanceof T)||!ss(r.type.dtype)))return!1}return e.numOperands>0}var w3,b3,x3,Wy,Ky,HI=I(()=>{hs();xn();It();rs();He();Ci();aA();ps();w3=new Set(["softmax","sqrt","div","rsqrt","log","exp","tanh"]),b3=new Set(["dot","conv","add","mul","sub"]),x3=new Map([["dot","quantized_dot"],["conv","quantized_conv"]]);for(let[e,t]of x3)rt.has(e)&&rt.registerOpAttr(e,"quantizedVariant",t);Wy=class{static{c(this,"QuantizationConfig")}constructor(t={}){this.scheme=t.scheme||Un.PER_TENSOR_SYMMETRIC,this.calibration=t.calibration||null,this.targetDtype=t.targetDtype||Q.I8,this.excludeOps=t.excludeOps||w3,this.quantizableOps=t.quantizableOps||b3,this.sensitivityThreshold=t.sensitivityThreshold||0,this.sensitivityResult=t.sensitivityResult||null,this.weightOnly=t.weightOnly||!1,this.target=t.target||null}},Ky=class extends Mt{static{c(this,"QuantizationPass")}constructor(t={}){super("QuantizationPass"),this.requiredAnalyses=[ne],this.config=t instanceof Wy?t:new Wy(t)}run(t,s){let r=(s?s.getAnalysis(ne,t):ne.compute(t)).topologicalOrder,i=new Set,o=this.config,a=!1;if(o.target&&!o.target.supportsInt8)return et.UNCHANGED;for(let u=0;u<r.length;u++){let f=r[u];if(f.opName==="return"||f.opName==="yield")continue;if(o.excludeOps.has(f.opName)||!o.quantizableOps.has(f.opName)){for(let g=0;g<f.numOperands;g++)i.has(f.getOperand(g))&&(a=this._insertDequantBefore(f,g,f.getOperand(g),o)||a);continue}if(o.sensitivityResult&&o.sensitivityThreshold>0&&o.sensitivityResult.isSensitive(f,o.sensitivityThreshold)||o.weightOnly&&!S3(f))continue;let h=rt.get(f.opName),p=h?h.getAttr("quantizedVariant"):null;if(p&&v3(f,i,o)){o.scheme===Un.PER_CHANNEL&&this._canPerChannelDot(f,i)?a=this._replacePerChannelDot(f,o)||a:a=this._replaceWithNativeQuantized(f,p,i,o)||a;continue}let d=rt.get(f.opName);if(d&&d.hasTrait(j.ELEMENTWISE)&&A3(f,i)){for(let g=0;g<f.numResults;g++)i.add(f.getResult(g));continue}a=this._insertDequantQuantBoundary(f,i,o)||a}let l=t.getReturnOp();if(l)for(let u=0;u<l.numOperands;u++){let f=l.getOperand(u);i.has(f)&&(a=this._insertDequantBefore(l,u,f,o)||a)}return this.trace&&this.trace.level>=at.DEBUG&&a&&this.trace.emit({type:"pass_detail",passName:this.name,opsProcessed:r.length,changed:a,level:at.DEBUG}),a?et.CHANGED:et.UNCHANGED}_getQuantParams(t,s){let n=ji(s.targetDtype)*8;if(s.calibration&&s.calibration.hasData(t))return s.calibration.getQuantParams(t,s.scheme,s.targetDtype);let r=t.definingOp;if(r&&r.opName==="constant"){let i=r.getAttr("value");if(typeof i=="number")return Vn.fromRange(-Math.abs(i)||-1,Math.abs(i)||1,s.scheme,s.targetDtype,n);if(i&&typeof i.length=="number")return Vn.fromConstantArray(i,s.scheme,s.targetDtype,n)}return t.type instanceof T&&ss(t.type.dtype)?Vn.defaultForActivation(s.scheme,s.targetDtype,n):null}_insertQuantizeAfter(t,s,n){let r=t.getResult(s),i=this._getQuantParams(r,n);if(!i)return null;let o=new T(r.type.shape,n.targetDtype),a=new bt("quantize",[r],[o],{scale:i.getScalarScale(),zero_point:i.getScalarZeroPoint(),scheme:n.scheme,target_dtype:n.targetDtype,...i.axis!==null?{axis:i.axis}:{}});return t.parentBlock&&t.parentBlock.insertAfter(a,t),a.getResult(0)}_insertDequantBefore(t,s,n,r){let i=this._getQuantParams(n,r);if(!i)return!1;let o=Q.F32,a=new T(n.type.shape,o),l=new bt("dequantize",[n],[a],{scale:i.getScalarScale(),zero_point:i.getScalarZeroPoint(),scheme:r.scheme,target_dtype:o});return t.parentBlock&&(t.parentBlock.insertBefore(l,t),t.replaceOperand(s,l.getResult(0))),!0}_insertDequantQuantBoundary(t,s,n){let r=!1;for(let i=0;i<t.numOperands;i++){let o=t.getOperand(i);s.has(o)&&(r=this._insertDequantBefore(t,i,o,n)||r)}for(let i=0;i<t.numResults;i++){let o=t.getResult(i);if(!(o.type instanceof T)||!ss(o.type.dtype))continue;let a=this._insertQuantizeAfter(t,i,n);if(a){let l=[...o.uses()];for(let u of l)u.user!==a.definingOp&&(u.user.replaceOperand(u.operandIndex,a),s.add(a));r=!0}}return r}_replaceWithNativeQuantized(t,s,n,r){let i=[],o={};for(let[h,p]of t.attributes||[])o[h]=p;for(let h=0;h<t.numOperands;h++){let p=t.getOperand(h);if(n.has(p)){i.push(p);let _=this._getQuantParams(p,r);if(_){let w=h===0?"lhs":"rhs";if(s==="quantized_dot")o[`${w}_scale`]=_.getScalarScale(),o[`${w}_zero_point`]=_.getScalarZeroPoint();else{let b=h===0?"input":"kernel";o[`${b}_scale`]=_.getScalarScale(),o[`${b}_zero_point`]=_.getScalarZeroPoint()}}continue}let d=this._getQuantParams(p,r);if(!d)return!1;let m=new T(p.type.shape,r.targetDtype),g=new bt("quantize",[p],[m],{scale:d.getScalarScale(),zero_point:d.getScalarZeroPoint(),scheme:r.scheme,target_dtype:r.targetDtype});t.parentBlock&&t.parentBlock.insertBefore(g,t),i.push(g.getResult(0));let y=h===0?s==="quantized_dot"?"lhs":"input":s==="quantized_dot"?"rhs":"kernel";o[`${y}_scale`]=d.getScalarScale(),o[`${y}_zero_point`]=d.getScalarZeroPoint()}let a=o.lhs_scale||o.input_scale||1,l=o.rhs_scale||o.kernel_scale||1;o.output_scale=a*l,o.output_zero_point=0;let u=new T(t.getResult(0).type.shape,Q.I32),f=new bt(s,i,[u],o);if(t.parentBlock){t.parentBlock.insertBefore(f,t);let h=new T(t.getResult(0).type.shape,t.getResult(0).type.dtype),p=o.output_scale||1,d=new bt("dequantize",[f.getResult(0)],[h],{scale:p,zero_point:o.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,s){if(t.opName!=="dot")return!1;let n=t.getOperand(0),r=t.getOperand(1);if(s.has(n)||s.has(r)||!(n.type instanceof T)||n.type.shape.length!==2||!(r.type instanceof T)||r.type.shape.length!==2)return!1;let i=r.definingOp;if(!i||i.opName!=="constant")return!1;let o=i.getAttr("value");if(!o||typeof o=="number"||typeof o.length!="number")return!1;let a=t.getAttr("rhs_contracting")||[],l=t.getAttr("lhs_contracting")||[];return!(a.length!==1||l.length!==1||(t.getAttr("rhs_batch")||[]).length!==0||(t.getAttr("lhs_batch")||[]).length!==0)}_activationParams(t,s){let n=ji(s.targetDtype)*8,r=Un.PER_TENSOR_SYMMETRIC;return s.calibration&&s.calibration.hasData(t)?s.calibration.getQuantParams(t,r,s.targetDtype):Vn.defaultForActivation(r,s.targetDtype,n)}_replacePerChannelDot(t,s){let n=t.getOperand(0),r=t.getOperand(1),i=r.type.shape,o=r.definingOp.getAttr("value"),a=ji(s.targetDtype)*8,u=t.getAttr("rhs_contracting")[0]===0?1:0,f=Vn.fromConstantArrayPerChannel([...o],i,u,s.targetDtype,a),h=f.quantizeArrayPerChannel([...o],i),p=this._activationParams(n,s),d=p.getScalarScale(),m=p.getScalarZeroPoint(),g=t.parentBlock;if(!g)return!1;let y=new T(i,s.targetDtype),_=new bt("constant",[],[y],{value:h,tensor_type:y});g.insertBefore(_,t);let w=new T(n.type.shape,s.targetDtype),b=new bt("quantize",[n],[w],{scale:d,zero_point:m,scheme:Un.PER_TENSOR_SYMMETRIC,target_dtype:s.targetDtype});g.insertBefore(b,t);let x=t.getResult(0).type.shape,A={};for(let[U,D]of t.attributes||[])A[U]=D;A.lhs_scale=d,A.lhs_zero_point=m,A.rhs_scale=1,A.rhs_zero_point=0,A.output_scale=1,A.output_zero_point=0;let S=new T(x,Q.I32),E=new bt("quantized_dot",[b.getResult(0),_.getResult(0)],[S],A);g.insertBefore(E,t);let R=new T(x,Q.F32),v=new bt("convert",[E.getResult(0)],[R],{target_dtype:Q.F32});g.insertBefore(v,t);let k=i[u],B=new Array(k);for(let U=0;U<k;U++)B[U]=d*f.getScaleForChannel(U);let N=new Set(t.getAttr("lhs_contracting")||[]),C=0;for(let U=0;U<n.type.shape.length;U++)N.has(U)||C++;let O=C,F=new T([k],Q.F32),P=new bt("constant",[],[F],{value:B,tensor_type:F});g.insertBefore(P,t);let z=new bt("broadcast_in_dim",[P.getResult(0)],[R],{broadcast_dimensions:[O],result_shape:x});g.insertBefore(z,t);let q=new bt("mul",[v.getResult(0),z.getResult(0)],[R],{});return g.insertBefore(q,t),t.replaceAllResultsWith([q.getResult(0)]),t.erase(),!0}};c(S3,"hasConstantOperand");c(A3,"allOperandsQuantized");c(v3,"allOperandsCanQuantize")});var lA,cA,Hy,JI=I(()=>{lA=class{static{c(this,"BufferInterval")}constructor(t,s,n,r){this.buffer=t,this.firstUse=s,this.lastUse=n,this.scope=r}get size(){return this.buffer.sizeInBytes()}overlaps(t){return this.firstUse<=t.lastUse&&t.firstUse<=this.lastUse}},cA=class{static{c(this,"BufferLivenessResult")}constructor(t,s,n){this.intervals=t,this.stmtOrder=s,this.paramBuffers=n}isParam(t){return this.paramBuffers.has(t)}getTemporaries(){let t=[];for(let[s,n]of this.intervals)this.paramBuffers.has(s)||t.push(n);return t}interfere(t,s){let n=this.intervals.get(t),r=this.intervals.get(s);return!n||!r?!1:n.overlaps(r)}},Hy=class{static{c(this,"BufferLiveness")}static analyze(t){let s=new Map,n=[],r=0,i=new Set;for(let[,p]of t.bufferMap)i.add(p);let o=[],a=c(p=>{if(!p)return;let d=s.get(p);d?(r<d.firstUse&&(d.firstUse=r),r>d.lastUse&&(d.lastUse=r)):(d=new lA(p,r,r,p.scope),s.set(p,d)),o.push(p)},"touch"),l=c(p=>{if(!(!p||typeof p!="object")){if(p.type==="BufferLoadNode"){a(p.buffer);return}if(p.type==="CallExternNode"){for(let d of p.args)l(d);return}p.a&&l(p.a),p.b&&l(p.b),p.expr&&l(p.expr),p.cond&&l(p.cond),p.condition&&l(p.condition),p.thenBody&&l(p.thenBody),p.elseBody&&l(p.elseBody)}},"touchExpr"),u=c(p=>{if(p){if(p.type==="BufferStoreNode"){a(p.buffer),l(p.value);for(let d of p.indices)l(d)}else if(p.type==="BufferLoadNode"){a(p.buffer);for(let d of p.indices)l(d)}else if(p.type==="SeqNode")for(let d of p.stmts)u(d)}},"touchBody"),f=c((p,d)=>{for(let m=p;m<o.length;m++){let g=s.get(o[m]);g&&d>g.lastUse&&(g.lastUse=d)}},"extendRegion"),h=c(p=>{if(p)switch(p.type){case"SeqNode":for(let d of p.stmts)h(d);break;case"ForNode":{let d=r,m=o.length;h(p.body);let g=r>d?r-1:d;f(m,g);break}case"BlockNode":n.push({idx:r,node:p});for(let d of p.reads)a(d.buffer);for(let d of p.writes)a(d.buffer);u(p.body),p.initBody&&u(p.initBody),r++;break;case"AllocateNode":a(p.buffer),h(p.body);break;case"IfThenElseNode":{let d=r,m=o.length;h(p.thenBody),p.elseBody&&h(p.elseBody);let g=r>d?r-1:d;f(m,g);break}case"LetStmtNode":h(p.body);break;default:u(p);break}},"walk");return h(t.body),new cA(s,n,i)}}});function k3(e,t){if(e.shape.length!==t.shape.length)return!1;for(let s=0;s<e.shape.length;s++)if(e.shape[s]!==t.shape[s])return!1;return!0}function E3(e,t){let s=[e];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="BlockNode"&&t.push(n),n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),n.initBody&&s.push(n.initBody)}}}var uA,Jy,YI=I(()=>{uA=class{static{c(this,"InplaceCandidate")}constructor(t,s,n){this.srcBuffer=t,this.dstBuffer=s,this.reason=n}},Jy=class{static{c(this,"InplaceAnalysis")}static analyze(t,s,n=new Set){let r=[],i=[];E3(t.body,i);let o=new Map;for(let u of s.stmtOrder)o.set(u.node,u.idx);let a=new Map;for(let u of s.stmtOrder)for(let f of u.node.reads){let h=a.get(f.buffer);(h===void 0||u.idx>h)&&a.set(f.buffer,u.idx)}let l=new Set;for(let u of i){let f=o.get(u);if(f!==void 0)for(let h of u.writes){let p=h.buffer;if(!(s.isParam(p)&&!n.has(p)))for(let d of u.reads){let m=d.buffer;if(m===p||l.has(m)||s.isParam(m)&&!n.has(m)||!k3(m,p)||m.dtype!==p.dtype||m.scope!==p.scope)continue;let g=s.intervals.get(m),y=s.intervals.get(p);if(!(!g||!y)&&g.lastUse<=y.firstUse){let _=a.get(m);if(_===void 0||_<=f){r.push(new uA(m,p,`${m.name} last used at ${g.lastUse}, ${p.name} first used at ${y.firstUse}`)),l.add(m);break}}}}}return r}};c(k3,"shapesMatch");c(E3,"collectBlocks")});function QI(e){return{best:null,consider(t,s,n){return s<n?null:e==="best-fit"?((this.best===null||s<this.best.gap)&&(this.best={offset:t,gap:s}),null):t},result(t){return this.best!==null?this.best.offset:t}}}var fA,hA,Yy,XI=I(()=>{c(QI,"gapSelector");fA=class{static{c(this,"MemoryBlock")}constructor(t,s,n){this.offset=t,this.size=s,this.buffer=n}get end(){return this.offset+this.size}overlaps(t){return this.offset<t.end&&t.offset<this.end}},hA=class{static{c(this,"MemoryPool")}constructor(t,s=64,n="best-fit"){this.scope=t,this.alignment=s,this.strategy=n,this.blocks=[],this.peakUsage=0}allocate(t,s){let n=this._align(t),r=this._findFreeOffset(n);return this.placeAt(r,n,s)}placeAt(t,s,n){let r=this._align(s),i=new fA(t,r,n);this.blocks.push(i);let o=t+r;return o>this.peakUsage&&(this.peakUsage=o),i}_align(t){return Math.ceil(t/this.alignment)*this.alignment}_findFreeOffset(t){let s=this.blocks.slice().sort((i,o)=>i.offset-o.offset),n=0,r=QI(this.strategy);for(let i of s){let o=this._align(n),a=i.offset-o,l=r.consider(o,a,t);if(l!==null)return l;i.end>n&&(n=i.end)}return r.result(this._align(n))}fragmentation(){if(this.peakUsage===0)return 0;let t=this.blocks.reduce((s,n)=>s+n.size,0);return Math.max(0,1-t/this.peakUsage)}release(t){let s=this.blocks.indexOf(t);s>=0&&this.blocks.splice(s,1)}},Yy=class{static{c(this,"BufferAssignment")}constructor(){this.assignments=new Map,this.pools=new Map,this.inplaceMap=new Map}assign(t,s=[],n=64,r="best-fit"){for(let f of s)this.inplaceMap.set(f.dstBuffer,f.srcBuffer);let i=new Map;for(let f of t)i.set(f.buffer,f);let o=new Map;for(let f of t)o.set(f.buffer,f.lastUse);let a=new Map;for(let[f,h]of this.inplaceMap)!i.has(f)||!i.has(h)||(a.has(h)||a.set(h,[]),a.get(h).push(f));if(a.size>0){let f=new Map;for(let h of t){let p=h.buffer;if(f.get(p)===1)continue;let d=[p];for(;d.length;){let m=d[d.length-1],g=a.get(m);if(f.get(m)===void 0){if(f.set(m,0),g)for(let y of g)f.get(y)===void 0&&d.push(y);continue}if(d.pop(),f.get(m)!==1){if(g){let y=o.get(m);for(let _ of g){let w=o.get(_);w>y&&(y=w)}o.set(m,y)}f.set(m,1)}}}}let l=[...t].sort((f,h)=>{let p=f.size,d=h.size,m=p>0,g=d>0;if(m&&g){let y=d-p;if(y!==0)return y}else if(m!==g)return m?-1:1;return f.firstUse-h.firstUse}),u=new Map;for(let f of l){let h=f.buffer;if(this.inplaceMap.has(h)){let b=this.inplaceMap.get(h),x=this.assignments.get(b);if(x){this.assignments.set(h,{offset:x.offset,size:f.size,scope:f.scope,pool:x.pool,inplaceOf:b});continue}}let p=f.size;if(p===0)continue;if(p<0){this.assignments.set(h,{offset:0,size:0,scope:f.scope,pool:f.scope,inplaceOf:null,isDynamic:!0});continue}let d=f.scope,m=this.pools.get(d);m||(m=new hA(d,n),this.pools.set(d,m)),u.has(d)||u.set(d,[]);let g=u.get(d),y=o.get(h)??f.lastUse,_=this._interferenceOffset(g,f.firstUse,y,m._align(p),n,r),w=m.placeAt(_,p,h);g.push({firstUse:f.firstUse,lastUseEff:y,offset:w.offset,size:w.size}),this.assignments.set(h,{offset:w.offset,size:w.size,scope:d,pool:d,inplaceOf:null})}return this}_interferenceOffset(t,s,n,r,i,o){let a=[];for(let f of t)f.firstUse<=n&&s<=f.lastUseEff&&a.push([f.offset,f.offset+f.size]);a.sort((f,h)=>f[0]-h[0]);let l=0,u=QI(o);for(let[f,h]of a){let p=Math.ceil(l/i)*i,d=f-p,m=u.consider(p,d,r);if(m!==null)return m;h>l&&(l=h)}return u.result(Math.ceil(l/i)*i)}getOffset(t){let s=this.assignments.get(t);return s?s.offset:-1}getAssignment(t){return this.assignments.get(t)||null}peakMemory(t=null){if(t){let n=this.pools.get(t);return n?n.peakUsage:0}let s=0;for(let[,n]of this.pools)s+=n.peakUsage;return s}}});var pA,Qy,ZI=I(()=>{JI();YI();XI();Jt();pA=class{static{c(this,"MemoryPlan")}constructor(t,s,n){this.assignment=t,this.liveness=s,this.inplaceCandidates=n}peakMemory(t=null){return this.assignment.peakMemory(t)}getReport(){let t=new Map;for(let[r,i]of this.assignment.pools)t.set(r,{peakUsage:i.peakUsage,numBuffers:0,numReused:0});for(let[r,i]of this.assignment.assignments){let o=t.get(i.scope);o&&(o.numBuffers++,i.inplaceOf&&o.numReused++)}let s=this.liveness.getTemporaries().length,n=this.inplaceCandidates.length;return{peakMemory:this.assignment.peakMemory(),scopeBreakdown:t,totalTemporaries:s,totalInplace:n,assignments:this.assignment.assignments}}},Qy=class{static{c(this,"MemoryPlanner")}constructor(t={}){this.alignment=t.alignment||64,this.enableInplace=t.enableInplace!==!1,this.allocStrategy=t.allocStrategy||"best-fit"}plan(t){let s=Hy.analyze(t),n=s.getTemporaries(),r=[];this.enableInplace&&(r=Jy.analyze(t,s));let i=new Yy;return i.assign(n,r,this.alignment,this.allocStrategy),new pA(i,s,r)}planAndRewrite(t){let s=this.plan(t);return{func:this._insertAllocations(t,s),plan:s}}_insertAllocations(t,s){let n=s.liveness.getTemporaries();if(n.length===0)return t;let r=[...n].sort((o,a)=>a.firstUse-o.firstUse),i=t.body;for(let o of r){let a=o.buffer,l=s.assignment.getAssignment(a);l&&(l.inplaceOf||(i=new Hs(a,l.isDynamic?"dynamic":l.scope,i)))}return t.body=i,t._setChild("body",i),t}}});var Xy,dA,zl,mA=I(()=>{Jt();Xy=["iterCount","depth","parallelLoops","vectorizedLoops","unrolledLoops","threadBoundLoops","serialLoops","threadBlockSize","gridSize","underReduction","numMathOps","numExternCalls","numReads","numWrites","stride1Accesses","stridedAccesses","reuseCount","touchedBytes","arithmeticIntensity","vectorized","parallelized","innermostExtent"],dA=class{static{c(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}},zl=class e{static{c(this,"FeatureExtractor")}static extract(t){let s={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};e._visitIterative(t.body,s);let n=s.totalBufferBytes,r=s.numMathOps+s.numExternCalls;return s.arithmeticIntensity=n>0?r/n:0,new dA(s)}static extractStatements(t){let s=[],n=[],r=0,i=[{node:t.body,action:"enter"}];for(;i.length>0;){let{node:o,action:a}=i.pop();if(o){if(a==="leaveFor"){n.pop();continue}if(a==="leaveBlock"){r--;continue}switch(o.type){case"ForNode":n.push(o),i.push({node:null,action:"leaveFor"}),i.push({node:o.body,action:"enter"});break;case"BlockNode":o.initBody&&(r++,i.push({node:null,action:"leaveBlock"}),i.push({node:o.initBody,action:"enter"})),i.push({node:o.body,action:"enter"});break;case"SeqNode":for(let l=o.stmts.length-1;l>=0;l--)i.push({node:o.stmts[l],action:"enter"});break;case"AllocateNode":case"LetStmtNode":i.push({node:o.body,action:"enter"});break;case"IfThenElseNode":o.elseBody&&i.push({node:o.elseBody,action:"enter"}),i.push({node:o.thenBody,action:"enter"});break;case"BufferStoreNode":s.push(e._statementVector(o,n,r));break}}}return s}static _statementVector(t,s,n){let r=1,i=0,o=0,a=0,l=0,u=0,f=1,h=1;for(let S of s){let E=S.extent&&S.extent.type==="IntImmNode"?S.extent.value:1;switch(r*=E,S.kind){case V.PARALLEL:i++;break;case V.VECTORIZED:o++;break;case V.UNROLLED:a++;break;case V.THREAD_BINDING:l++,S.threadTag&&S.threadTag.startsWith("threadIdx")?f*=E:S.threadTag&&S.threadTag.startsWith("blockIdx")&&(h*=E);break;default:u++;break}}let p={math:0,extern:0};e._countExpr(t.value,p);let d=s.map(S=>S.loopVar.name),m=[{buffer:t.buffer,indices:t.indices}];e._collectLoads(t.value,m);let g=0,y=0,_=0,w=0;for(let S of m){let E=S.buffer&&S.buffer.sizeInBytes?S.buffer.sizeInBytes():0;E>0&&(w+=E);let R=S.indices&&S.indices.length>0?S.indices[S.indices.length-1]:null;R&&R.type==="VariableNode"?g++:y++;let v=new Set;if(S.indices)for(let k of S.indices)e._collectVars(k,v);for(let k of d)v.has(k)||_++}let b=s.length>0?s[s.length-1]:null,x=b&&b.extent&&b.extent.type==="IntImmNode"?b.extent.value:0,A={iterCount:r,depth:s.length,parallelLoops:i,vectorizedLoops:o,unrolledLoops:a,threadBoundLoops:l,serialLoops:u,threadBlockSize:f,gridSize:h,underReduction:n>0?1:0,numMathOps:p.math,numExternCalls:p.extern,numReads:m.length-1,numWrites:1,stride1Accesses:g,stridedAccesses:y,reuseCount:_,touchedBytes:w,arithmeticIntensity:w>0?(p.math+p.extern)/w:0,vectorized:o>0?1:0,parallelized:i+l>0?1:0,innermostExtent:x};return Xy.map(S=>A[S]||0)}static _countExpr(t,s){if(!(!t||typeof t!="object"))switch(t.type){case"MathOpNode":s.math++,e._countExpr(t.a,s),t.b&&e._countExpr(t.b,s);break;case"CompareNode":s.math++,e._countExpr(t.a,s),e._countExpr(t.b,s);break;case"CallExternNode":s.extern++;for(let n of t.args)e._countExpr(n,s);break;default:break}}static _collectLoads(t,s){if(!(!t||typeof t!="object")){if(t.type==="BufferLoadNode"){s.push({buffer:t.buffer,indices:t.indices});return}if(t.a&&e._collectLoads(t.a,s),t.b&&e._collectLoads(t.b,s),t.args)for(let n of t.args)e._collectLoads(n,s)}}static _collectVars(t,s){if(!(!t||typeof t!="object")){if(t.type==="VariableNode"){s.add(t.name);return}if(t.a&&e._collectVars(t.a,s),t.b&&e._collectVars(t.b,s),t.args)for(let n of t.args)e._collectVars(n,s);if(t.indices)for(let n of t.indices)e._collectVars(n,s)}}static _visitIterative(t,s){let n=[{node:t,action:"enter"}];for(;n.length>0;){let{node:r,action:i}=n.pop();if(r){if(i==="leave_for"){s.currentDepth--;continue}switch(r.type){case"ForNode":{s.numLoops++,s.currentDepth++,s.currentDepth>s.maxLoopDepth&&(s.maxLoopDepth=s.currentDepth);let o=r.extent.type==="IntImmNode"?r.extent.value:1;switch(s.loopExtents.push(o),s.numLoops===1&&(s.outermostExtent=o),s.innermostExtent=o,r.kind){case V.PARALLEL:s.numParallelLoops++;break;case V.VECTORIZED:s.numVectorizedLoops++;break;case V.UNROLLED:s.numUnrolledLoops++;break;case V.THREAD_BINDING:s.numThreadBound++,r.threadTag&&r.threadTag.startsWith("threadIdx")?s.threadBlockSize*=o:r.threadTag&&r.threadTag.startsWith("blockIdx")&&(s.gridSize*=o);break;default:s.numSerialLoops++;break}s.totalIterations*=o,n.push({node:null,action:"leave_for"}),n.push({node:r.body,action:"enter"});break}case"BlockNode":s.numBlocks++,r.initBody&&(s.hasReduction=!0,s.reductionDepth=s.currentDepth);for(let o of r.reads)e._visitBuffer(o.buffer,s);for(let o of r.writes)e._visitBuffer(o.buffer,s);n.push({node:r.body,action:"enter"}),r.initBody&&n.push({node:r.initBody,action:"enter"});break;case"SeqNode":for(let o=r.stmts.length-1;o>=0;o--)n.push({node:r.stmts[o],action:"enter"});break;case"AllocateNode":e._visitBuffer(r.buffer,s),n.push({node:r.body,action:"enter"});break;case"IfThenElseNode":r.elseBody&&n.push({node:r.elseBody,action:"enter"}),n.push({node:r.thenBody,action:"enter"});break;case"LetStmtNode":n.push({node:r.body,action:"enter"});break;case"BufferStoreNode":s.numBufferWrites++,e._visitBuffer(r.buffer,s),e._checkStride(r.buffer,r.indices,s),e._visitExpr(r.value,s);break}}}}static _visitBuffer(t,s){if(!t||s.buffersSeen.has(t))return;s.buffersSeen.add(t);let n=t.sizeInBytes();n>0&&(s.totalBufferBytes+=n)}static _visitExpr(t,s){if(t)switch(t.type){case"MathOpNode":s.numMathOps++,e._visitExpr(t.a,s),t.b&&e._visitExpr(t.b,s);break;case"CallExternNode":s.numExternCalls++;for(let n of t.args)e._visitExpr(n,s);break;case"BufferLoadNode":s.numBufferReads++,e._checkStride(t.buffer,t.indices,s);break;case"CompareNode":s.numMathOps++,e._visitExpr(t.a,s),e._visitExpr(t.b,s);break;default:break}}static _checkStride(t,s,n){if(!t||!s||s.length===0)return;let r=s[s.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"?n.strideOneAccesses++:n.nonStrideOneAccesses++}}});function tO(e,t){if(e.length===0)return 0;let s=0;for(let n of e)s+=t[n];return s/e.length}function gA(e,t,s,n,r,i){let o=s.length;if(n>=r||o<i*2)return{leaf:tO(s,t)};let a=e[0].length,l=0,u=0;for(let d of s){let m=t[d];l+=m,u+=m*m}let f=null,h=null;for(let d=0;d<a;d++){let m=s.slice().sort((_,w)=>e[_][d]-e[w][d]),g=0,y=0;for(let _=0;_<m.length-1;_++){let w=t[m[_]];g+=w,y+=w*w;let b=e[m[_]][d],x=e[m[_+1]][d];if(b===x)continue;let A=_+1,S=o-A;if(A<i||S<i)continue;let E=l-g,R=u-y,v=y-g*g/A+(R-E*E/S);(!f||v<f.cost)&&(f={f:d,thr:(b+x)/2,sIdx:_,cost:v},h=m)}}if(!f)return{leaf:tO(s,t)};let p=h;return{f:f.f,thr:f.thr,left:gA(e,t,p.slice(0,f.sIdx+1),n+1,r,i),right:gA(e,t,p.slice(f.sIdx+1),n+1,r,i)}}function eO(e,t){for(;e&&e.leaf===void 0;)e=t[e.f]<=e.thr?e.left:e.right;return e?e.leaf:0}var gp,sO=I(()=>{c(tO,"leafValue");c(gA,"buildTree");c(eO,"predictTree");gp=class e{static{c(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,s){let n=t.length;if(n===0)return;let r=0;for(let l of s)r+=l;r/=n;let i=new Array(n).fill(r),o=[];for(let l=0;l<n;l++)o.push(l);let a=[];for(let l=0;l<this.numTrees;l++){let u=new Array(n);for(let h=0;h<n;h++)u[h]=s[h]-i[h];let f=gA(t,u,o,0,this.maxDepth,this.minSamples);for(let h=0;h<n;h++)i[h]+=this.lr*eO(f,t[h]);a.push(f)}this.trees=a,this.base=r}predict(t){if(!this.trees)return 0;let s=this.base;for(let n of this.trees)s+=this.lr*eO(n,t);return s}serialize(){return{trees:this.trees,base:this.base,lr:this.lr}}static deserialize(t){let s=new e({lr:t.lr});return s.trees=t.trees,s.base=t.base,s}}});function nO(e){let t=e[0].length,s=new Array(t+1).fill(0);for(let n of e)for(let r=0;r<t;r++){let i=n[r]||0;B3.has(r)?i>s[r]&&(s[r]=i):s[r]+=i}for(let n of C3)n<t&&(s[n]/=e.length);return s[t]=e.length,s}var N3,R3,B3,C3,yA,T3,Zy,Wu,Ku,_A=I(()=>{mA();sO();N3=new Set(["depth","threadBlockSize","gridSize","underReduction","vectorized","parallelized","innermostExtent"]),R3=new Set(["arithmeticIntensity"]),B3=new Set(Xy.map((e,t)=>N3.has(e)?t:-1).filter(e=>e>=0)),C3=new Set(Xy.map((e,t)=>R3.has(e)?t:-1).filter(e=>e>=0));c(nO,"aggregateStatements");yA=class{static{c(this,"CostEstimate")}constructor(t,s){this.score=t,this.breakdown=s}},T3={parallelism:2,vectorization:1.5,memoryCoalescing:2,occupancy:1,arithmeticIntensity:1,loopOverhead:-.5,codeSize:-.3},Zy=class{static{c(this,"AnalyticalCostModel")}constructor(t,s={}){this.target=t,this._weights={...T3,...t&&t.costModelWeights?t.costModelWeights:{},...s.weights||{}}}estimate(t){let s=zl.extract(t);return this.estimateFromFeatures(s)}score(t){return this.estimate(t).score}estimateFromFeatures(t){let s=this._scoreParallelism(t),n=this._scoreVectorization(t),r=this._scoreMemoryAccess(t),i=this._scoreOccupancy(t),o=this._scoreIntensity(t),a=this._scoreOverhead(t),l=this._scoreCodeSize(t),u=this._weights,f=u.parallelism*s+u.vectorization*n+u.memoryCoalescing*r+u.occupancy*i+u.arithmeticIntensity*o+u.loopOverhead*a+u.codeSize*l;return new yA(f,{parallelism:s,vectorization:n,memoryCoalescing:r,occupancy:i,arithmeticIntensity:o,loopOverhead:a,codeSize:l})}_scoreParallelism(t){if(this.target.isGPU()){let n=t.threadBlockSize*t.gridSize,r=this.target.maxParallelism();return Math.min(1,n/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 s=t.strideOneAccesses+t.nonStrideOneAccesses;return s===0?1:t.strideOneAccesses/s}_scoreOccupancy(t){if(!this.target.isGPU())return 1;if(t.threadBlockSize===0)return 0;let s=this.target.warpSize,n=Math.ceil(t.threadBlockSize/s),r=Math.floor(this.target.maxThreadsPerBlock/s);return Math.min(1,n/r)}_scoreIntensity(t){let s=this.target.supportsFloat16?1.5:1;return Math.min(1,t.arithmeticIntensity*10*s)}_scoreOverhead(t){return t.numSerialLoops/Math.max(t.numLoops,1)}_scoreCodeSize(t){return Math.min(1,(t.numMathOps+t.numExternCalls)/256)}compare(t,s){return this.estimate(t).score-this.estimate(s).score}},Wu=class e{static{c(this,"LearnedCostModel")}constructor(t=null,s={}){this.opts={numTrees:s.numTrees??60,maxDepth:s.maxDepth??3,lr:s.lr??.1,minSamples:s.minSamples??1},this._gbt=t?gp.deserialize(t):null,this._X=[],this._Y=[]}addSample(t,s){!t||t.length===0||Number.isFinite(s)&&(this._X.push(nO(t)),this._Y.push(s))}train(){if(this._X.length===0)return;let t=new gp(this.opts);t.fit(this._X,this._Y),this._gbt=t}predict(t){return!this._gbt||!t||t.length===0?0:this._gbt.predict(nO(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 e(t&&t.gbt?t.gbt:null)}},Ku=class{static{c(this,"GuidedCostModel")}constructor(t,s){this.analytical=t,this.learned=s}score(t){return this.learned&&this.learned.trained?this.learned.predict(zl.extractStatements(t)):this.analytical.score(t)}}});function*rO(e){let t=[e];for(;t.length>0;){let s=t.pop();if(s){if(yield s,s.body&&t.push(s.body),s.stmts)for(let n of s.stmts)t.push(n);s.thenBody&&t.push(s.thenBody),s.elseBody&&t.push(s.elseBody),s.initBody&&t.push(s.initBody)}}}function iO(e,t){for(let s of rO(e))if(s.type==="BlockNode"&&s.name===t)return s;return null}function yp(e){let t=[];for(let s of rO(e))s.type==="BlockNode"&&t.push(s.name);return t}function t_(e,t){let s=Me(e,t);if(!s)return{spatial:0,reduction:0,reads:0,hasReduction:!1};let n=0,r=0;for(let i of s.loops)s.reductionLoopVars.has(i.loopVar.name)?r++:n++;return{spatial:n,reduction:r,reads:s.readBuffers.length,hasReduction:s.hasReduction}}var e_=I(()=>{Wo();c(rO,"walkStmts");c(iO,"findBlock");c(yp,"collectAllBlockNames");c(t_,"analyzeBlockStructure")});function aO(e){return e.isGPU()?O3:I3}function wA(e){let t=0,s=0;for(let[n,r]of e.order)n==="S"?t=Math.max(t,r+1):s=Math.max(s,r+1);return{spatialLevels:t,reductionLevels:s}}var I3,O3,oO,bA=I(()=>{I3={name:"mlt_cpu",order:[["S",0],["S",1],["S",2],["S",3],["R",0]],roles:{S0:"parallelize",S3:"vectorize"}},O3={name:"mlt_gpu",order:[["S",0],["S",1],["S",2],["R",0]],roles:{S0:"blockIdx",S1:"threadIdx",S2:"unroll"}},oO={name:"ssrsrs_cpu",order:[["S",0],["S",1],["R",0],["S",2],["R",1],["S",3]],roles:{S0:"parallelize",S3:"vectorize",R1:"unroll"}};c(aO,"getTileStructure");c(wA,"levelCounts")});var Cr,Gn,s_=I(()=>{Cr=class{static{c(this,"SearchVariable")}constructor(t,s){this.name=t,this.candidates=s}sample(t){return this.candidates[t(this.candidates.length)]}},Gn=class{static{c(this,"ScheduleSketch")}constructor(t,s,n){this.name=t,this.variables=s,this._apply=n}instantiate(t){return(s,n,r)=>{this._apply(s,n,r,t)}}sampleParams(t){let s={};for(let n of this.variables)s[n.name]=n.sample(t);return s}}});function M3(e){let t=[];for(let s=1;s*s<=e;s++){if(e%s!==0)continue;t.push(s);let n=e/s;n!==s&&t.push(n)}return t.sort((s,n)=>s-n),t}function L3(e,t){let s=Math.min(e.length,t.length);for(let n=0;n<s;n++)if(e[n]!==t[n])return e[n]-t[n];return e.length-t.length}function P3(e,t){let s=new Set,n=[];e.sort(L3);for(let o of e){let a=o.join(",");s.has(a)||(s.add(a),n.push(o))}if(n.length<=t)return n;let r=new Set,i=[];for(let o=0;o<t;o++){let a=Math.floor(o*(n.length-1)/(t-1));r.has(a)||(r.add(a),i.push(n[a]))}return i}function xA(e,t,s=48){if(!Number.isFinite(e)||e<1||t<=1)return[[e]];let n=[],r=s*8,i=c((o,a,l)=>{if(!(n.length>=r)){if(a===t-1){n.push([...l,o]);return}for(let u of M3(o))if(l.push(u),i(o/u,a+1,l),l.pop(),n.length>=r)return}},"rec");return i(e,0,[]),P3(n,s)}var lO=I(()=>{c(M3,"divisorsOf");c(L3,"cmpTuple");c(P3,"selectDiverse");c(xA,"enumerateFactorizations")});function n_(e){return e.extent&&e.extent.type==="IntImmNode"?e.extent.value:null}function uO(e,t,s,n){let r=[],i=s;for(let o=0;o<n.length-1;o++){let a=e.getLoops(t).find(h=>h.loopVar.name===i);if(!a)return null;let l=1;for(let h=o+1;h<n.length;h++)l*=n[h];let[u,f]=e.split(a,l);r.push(u.loopVar.name),i=f.loopVar.name}return r.push(i),r}function D3(e,t,s,n,r){let i=c(o=>e.getLoops(t).find(a=>a.loopVar.name===o),"find");for(let[o,a]of s.order){let l=s.roles[o+a];if(!l)continue;let u=o==="S"?n:r;if(l==="parallelize"){let f=u[0]&&a<u[0].length?i(u[0][a]):null;f&&e.parallelize(f)}else if(l==="vectorize"){let f=u[u.length-1],h=f&&a<f.length?i(f[a]):null;h&&e.vectorize(h)}else if(l==="unroll")for(let f of u){let h=a<f.length?i(f[a]):null;h&&e.unroll(h)}else(l==="blockIdx"||l==="threadIdx")&&u.forEach((f,h)=>{if(h>=cO.length)return;let p=a<f.length?i(f[a]):null;p&&e.bindThread(p,`${l}.${cO[h]}`)})}}function fO(e,t,s,n,r,i){let o=[];for(let h=0;h<n.length;h++){let p=uO(e,t,n[h],i[`s${h}`]);if(!p)return;o.push(p)}let a=[];for(let h=0;h<r.length;h++){let p=uO(e,t,r[h],i[`r${h}`]);if(!p)return;a.push(p)}let l=[];for(let[h,p]of s.order){let d=h==="S"?o:a;for(let m of d)p<m.length&&l.push(m[p])}let u=new Map(e.getLoops(t).map(h=>[h.loopVar.name,h])),f=l.map(h=>u.get(h)).filter(Boolean);f.length>=2&&e.reorder(...f),D3(e,t,s,o,a)}function hO(e){let t=e.loops.filter(n=>!e.reductionLoopVars.has(n.loopVar.name)),s=e.loops.filter(n=>e.reductionLoopVars.has(n.loopVar.name));return{spatialLoops:t,reductionLoops:s}}function pO(e,t,s,n){let r=[];return e.forEach((i,o)=>r.push(new Cr(`s${o}`,xA(n_(i),s)))),t.forEach((i,o)=>r.push(new Cr(`r${o}`,xA(n_(i),n)))),r}function dO(e,t){let{spatialLevels:s,reductionLevels:n}=wA(t),{spatialLoops:r,reductionLoops:i}=hO(e);if(r.length===0)return null;for(let u of[...r,...i])if(n_(u)===null)return null;let o=pO(r,i,s,n),a=r.map(u=>u.loopVar.name),l=i.map(u=>u.loopVar.name);return new Gn(t.name,o,(u,f,h,p)=>{fO(u,f,t,a,l,p)})}function mO(e,t){let{spatialLevels:s,reductionLevels:n}=wA(t),{spatialLoops:r,reductionLoops:i}=hO(e);if(r.length===0||i.length===0)return null;for(let u of[...r,...i])if(n_(u)===null)return null;let o=pO(r,i,s,n),a=r.map(u=>u.loopVar.name),l=i.map(u=>u.loopVar.name);return new Gn(t.name,o,(u,f,h,p)=>{u.decomposeReduction(f),fO(u,`${f}_upd`,t,a,l,p)})}var cO,gO=I(()=>{s_();lO();bA();cO=["x","y","z"];c(n_,"staticExtent");c(uO,"multiLevelSplit");c(D3,"applyRoles");c(fO,"tileBlock");c(hO,"splitLoops");c(pO,"tilingVariables");c(dO,"createMultiLevelTilingSketch");c(mO,"createSSRSRSTilingSketch")});function _O(e){return Math.min(e&&e.maxThreadsPerBlock||256,256)}function $3(e){let t=new Set;for(let s=2;s*s<=e;s++){if(e%s!==0)continue;t.add(s);let n=e/s;n>1&&n<e&&t.add(n)}return[...t].sort((s,n)=>s-n)}function wO(e){return new Gn("fused",[],(t,s,n,r)=>{t.fuseConsumer(s,e);let i=t.getLoops(s);i.length>0&&t.parallelize(i[0])})}function bO(e){let t=e.loops.filter(o=>e.reductionLoopVars.has(o.loopVar.name));if(t.length!==1)return null;let s=t[0],n=s.extent&&s.extent.type==="IntImmNode"?s.extent.value:null;if(n===null)return null;let r=$3(n);if(r.length===0)return null;let i=s.loopVar.name;return new Gn("rfactor",[new Cr("rf_factor",r)],(o,a,l,u)=>{o.rfactor(a,i,u.rf_factor);let f=o.getLoops(`${a}_rf_p`);f.length>0&&o.parallelize(f[0])})}function xO(){return new Gn("elementwise_cpu",[new Cr("vector_width",F3)],(e,t,s,n)=>{let r=e.getLoops(t);if(r.length===0)return;if(r.length===1){let a=r[0].extent;if(a.type==="IntImmNode"&&a.value>=n.vector_width*2){let[l,u]=e.split(r[0],n.vector_width);e.parallelize(l),e.vectorize(u)}else e.parallelize(r[0]);return}e.parallelize(r[0]);let i=r[r.length-1],o=i.extent;if(o.type==="IntImmNode"&&o.value>=n.vector_width){let[,a]=e.split(i,n.vector_width);e.vectorize(a)}})}function SO(){return new Gn("elementwise_gpu",[new Cr("block_size",yO)],(e,t,s,n)=>{let r=e.getLoops(t);if(r.length===0)return;let i=r[0];for(let f=1;f<r.length;f++){let p=e.getLoops(t).find(d=>d.loopVar.name===r[f].loopVar.name);p&&i.body===p&&(i=e.fuseLoops(i,p))}let o=i.extent;if(o.type!=="IntImmNode"){e.bindThread(i,"threadIdx.x");return}let a=o.value,l=Math.min(s.maxThreadsPerBlock,1024);if(mS(e.func)&&a<=l){e.bindThread(i,"threadIdx.x");return}let u=Math.min(n.block_size,_O(s));if(a>u){let[f,h]=e.split(i,u);e.bindThread(f,"blockIdx.x"),e.bindThread(h,"threadIdx.x")}else e.bindThread(i,"threadIdx.x")})}function AO(){return new Gn("reduction_cpu",[],(e,t,s,n)=>{let r=e.getLoops(t);r.length>0&&e.parallelize(r[0])})}function vO(){return new Gn("reduction_gpu",[new Cr("block_size",yO)],(e,t,s,n)=>{let r=e.getLoops(t);if(r.length===0)return;let i=Me(e.func,t),o=r.filter(f=>!i||!eu(f,i));if(o.length===0)return;let a=o[0];for(let f=1;f<o.length;f++){let p=e.getLoops(t).find(d=>d.loopVar.name===o[f].loopVar.name);p&&a.body===p&&(a=e.fuseLoops(a,p))}let l=a.extent;if(l.type!=="IntImmNode"){e.bindThread(a,"threadIdx.x");return}let u=Math.min(n.block_size,_O(s));if(l.value>u){let[f,h]=e.split(a,u);e.bindThread(f,"blockIdx.x"),e.bindThread(h,"threadIdx.x")}else e.bindThread(a,"threadIdx.x")})}var yO,F3,kO=I(()=>{Wo();s_();yO=[32,64,128,256,512,1024],F3=[1,2,4,8,16];c(_O,"gpuThreadCap");c($3,"reductionDivisors");c(wO,"createFusedTilingSketch");c(bO,"createRfactorSketch");c(xO,"createElementwiseCPUSketch");c(SO,"createElementwiseGPUSketch");c(AO,"createReductionCPUSketch");c(vO,"createReductionGPUSketch")});function kA(e,t){let s=[];for(let n=1;n<=t;n*=2)n>=e&&s.push(n);return s}function CO(e,t){if(!Me(e,t))return null;let n=iO(e.body,t);if(!n||n.reads.length<2||n.writes.length<1)return null;let r=n.reads[0].buffer,i=n.reads[1].buffer,o=n.writes[0].buffer;if(!r||!i||!o||r.shape.length!==2||i.shape.length!==2||o.shape.length!==2)return null;let a=o.shape[0],l=o.shape[1],u=r.shape[1];return![a,l,u].every(f=>typeof f=="number"&&f>0)||i.shape[0]!==u||i.shape[1]!==l||r.dtype!=="f32"||i.dtype!=="f32"||o.dtype!=="f32"?null:{A:r,B:i,C:o,M:a,N:l,K:u}}function z3(e,t,s=32){let n=e.maxThreadsPerBlock||1024,r=e.warpSize||32,i=e.sharedMemoryBytes||49152,o=e.registersPerThread||255,a=4,{M:l,N:u,K:f}=t,h=kA(2,8),p=kA(2,r/2),d=kA(r/4,r/2),m=2*r,g=Math.min(n,8*r),y=4*r,_=new Set,w=[];for(let b of h)for(let x of h)if(!(b*x>y)&&!(b*x+b+x+r>o))for(let A of p)for(let S of p){let E=S*A;if(E<m||E>g||E%r!==0)continue;let R=b*A,v=x*S;if(!(R>l||v>u||R>y||v>y))for(let k of d){if(k>f||(R*k+k*v)*a>i)continue;let N=`${R}_${v}_${k}_${b}_${x}`;_.has(N)||(_.add(N),w.push({BM:R,BN:v,BK:k,TM:b,TN:x,threads:E}))}}return w.sort((b,x)=>RO(x,r)-RO(b,r)),w.slice(0,s)}function RO(e,t){let s=e.TM*e.TN,n=-Math.abs(e.TM-e.TN),r=-Math.abs(Math.log2(e.BM)-Math.log2(e.BN)),i=e.threads>=4*t&&e.threads<=8*t?1:0,o=e.BK===t/4?1:0;return i*100+s*4+n*6+r*4+o}function U3(e,t){let{A:s,B:n,C:r,M:i,N:o,K:a}=e,{BM:l,BN:u,BK:f,TM:h,TN:p}=t,d=u/p,m=l/h,g=d*m,y=Math.ceil(a/f),_=l*f,w=f*u,b=Math.ceil(_/g),x=Math.ceil(w/g),A=i%l!==0,S=o%u!==0,E=a%f!==0,R=_%g!==0,v=w%g!==0,k=new ee("rb_As",[_],"f32","shared"),B=new ee("rb_Bs",[w],"f32","shared"),N=new ee("rb_acc",[h*p],"f32","local"),C=new ee("rb_af",[h],"f32","local"),O=new ee("rb_bf",[p],"f32","local"),F=Qe("rb_bx"),P=Qe("rb_by"),z=Qe("rb_tx"),q=Qe("rb_ty"),U=Qe("rb_tid"),D=Qe("rb_brow"),G=Qe("rb_bcol"),At=Qe("rb_k0"),pt=c((ln,jF)=>Zs(qn(ln,qt(p)),jF),"accIdx"),mt=Qe("rb_im"),$=Qe("rb_in"),K=go(mt,h,go($,p,new Z(N,[pt(mt,$)],SA()))),_t=Qe("rb_la"),dt=Qe("rb_aidx"),jt=Zs(D,EO(dt,qt(f))),Pn=Zs(At,NO(dt,qt(f))),gn=new H(s,[jt,Pn]);if(A||E){let ln=A?Yr(jt,qt(i)):null;E&&(ln=ln?AA(ln,Yr(Pn,qt(a))):Yr(Pn,qt(a))),gn=new Ht(ln,gn,SA())}let yn=new Z(k,[dt],gn);R&&(yn=new Ht(Yr(dt,qt(_)),yn));let ts=vA(_t,b,new Sn(dt,Zs(U,qn(_t,qt(g))),yn)),Rt=Qe("rb_lb"),Ce=Qe("rb_bidx"),Vi=Zs(At,EO(Ce,qt(u))),es=Zs(G,NO(Ce,qt(u))),Bt=new H(n,[Vi,es]);if(E||S){let ln=E?Yr(Vi,qt(a)):null;S&&(ln=ln?AA(ln,Yr(es,qt(o))):Yr(es,qt(o))),Bt=new Ht(ln,Bt,SA())}let xe=new Z(B,[Ce],Bt);v&&(xe=new Ht(Yr(Ce,qt(w)),xe));let yr=vA(Rt,x,new Sn(Ce,Zs(U,qn(Rt,qt(g))),xe)),ai=Qe("rb_kk"),Eo=Qe("rb_fi"),Gi=Qe("rb_fj"),ic=go(Eo,h,new Z(C,[Eo],new H(k,[Zs(qn(Zs(qn(q,qt(h)),Eo),qt(f)),ai)]))),vE=go(Gi,p,new Z(O,[Gi],new H(B,[Zs(qn(ai,qt(u)),Zs(qn(z,qt(p)),Gi))]))),Ls=Qe("rb_mi"),Ga=Qe("rb_ni"),_r=go(Ls,h,go(Ga,p,new Z(N,[pt(Ls,Ga)],Zs(new H(N,[pt(Ls,Ga)]),qn(new H(C,[Ls]),new H(O,[Ga])))))),sb=go(ai,f,new yt([ic,vE,_r])),md=Qe("rb_kt"),nb=new Sn(At,qn(md,qt(f)),new yt([ts,yr,new Qa,sb,new Qa])),gd=vA(md,y,nb),Df=Qe("rb_wm"),Ff=Qe("rb_wn"),yd=Zs(Zs(D,qn(q,qt(h))),Df),rb=Zs(Zs(G,qn(z,qt(p))),Ff),ib=new Z(r,[yd,rb],new H(N,[pt(Df,Ff)]));if(A||S){let ln=A?Yr(yd,qt(i)):null;S&&(ln=ln?AA(ln,Yr(rb,qt(o))):Yr(rb,qt(o))),ib=new Ht(ln,ib)}let FF=go(Df,h,go(Ff,p,ib)),$F=new yt([K,gd,FF]),zF=new Hs(N,"local",new Hs(C,"local",new Hs(O,"local",$F))),UF=new Sn(U,Zs(qn(q,qt(d)),z),new Sn(D,qn(P,qt(l)),new Sn(G,qn(F,qt(u)),zF))),VF=Math.ceil(o/u),GF=Math.ceil(i/l),qF=r_(P,"blockIdx.y",GF,r_(F,"blockIdx.x",VF,r_(q,"threadIdx.y",m,r_(z,"threadIdx.x",d,UF))));return new Hs(k,"shared",new Hs(B,"shared",qF))}function V3(e){let t=new Cr("config_index",e.map((n,r)=>r)),s=new Gn("matmul_register_block_gpu",[t],(n,r,i,o)=>{let a=CO(n.func,r);if(!a)return;let l=e[o.config_index];if(!l)return;let u=U3(a,l);n.func.body=u,n.func._setChild&&n.func._setChild("body",u),n.func.gpuRegisterBlocked=!0});return s.configs=e,s.enumerate=()=>e.map((n,r)=>({config_index:r})),s}function G3(e){let t=yp(e.body),s=null;for(let r of t){let i=t_(e,r);if(i.hasReduction&&i.spatial===2&&i.reads>=2){if(s)return null;s=r}}if(!s)return null;let n=CO(e,s);if(!n)return null;for(let r of t){if(r===s)continue;let i=Me(e,r);if(!i||i.hasReduction||i.readBuffers.length>0)return null;for(let o of i.writeBuffers)if(o!==n.C.name)return null}return{reductionBlock:s,dims:n}}function TO(e,t,s){let n=G3(e);if(!n)return null;let r=BO.get(e);if(r===void 0){let i=z3(s,n.dims);r=i.length>0?V3(i):null,BO.set(e,r)}return r?t===n.reductionBlock?[r]:[]:null}var qt,SA,Qe,Zs,qn,EO,NO,Yr,AA,vA,go,r_,BO,IO=I(()=>{Jt();Xa();Wo();s_();e_();qt=c(e=>new M(e),"I"),SA=c(()=>new ut(0),"FZERO"),Qe=c(e=>new Ae(e,"i32"),"IV"),Zs=c((e,t)=>ds("+",e,t),"ADD"),qn=c((e,t)=>ds("*",e,t),"MUL"),EO=c((e,t)=>ds("//",e,t),"DIV"),NO=c((e,t)=>ds("%",e,t),"MOD"),Yr=c((e,t)=>new fe("lt",e,t),"LT"),AA=c((e,t)=>new L("&&",e,t),"AND"),vA=c((e,t,s)=>new wt(e,qt(0),qt(t),V.SERIAL,s),"forS"),go=c((e,t,s)=>new wt(e,qt(0),qt(t),V.UNROLLED,s),"forU"),r_=c((e,t,s,n)=>new wt(e,qt(0),qt(s),V.THREAD_BINDING,n,t),"forT");c(kA,"pow2Range");c(CO,"matmulTileDims");c(z3,"enumerateRegisterBlockConfigs");c(RO,"goodness");c(U3,"buildRegisterBlockedMatmul");c(V3,"createMatmulRegisterBlockGPUSketch");c(G3,"analyzePureMatmul");BO=new WeakMap;c(TO,"richMatmulSketches")});function q3(e,t){let s=[e];for(;s.length;){let n=s.pop();if(!(!n||typeof n!="object")&&(n.type==="BlockNode"&&t(n),n.body&&s.push(n.body),n.initBody&&s.push(n.initBody),n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),Array.isArray(n.stmts)))for(let r of n.stmts)s.push(r)}}function i_(e){let t=[];q3(e.body,n=>{t.push({name:n.name,node:n,reads:(n.reads||[]).map(r=>r.buffer&&r.buffer.name).filter(Boolean),writes:(n.writes||[]).map(r=>r.buffer&&r.buffer.name).filter(Boolean)})});let s=new Map;for(let n of t)for(let r of n.reads)s.has(r)||s.set(r,[]),s.get(r).push(n.name);return{blocks:t,consumersByBuf:s,byName:new Map(t.map(n=>[n.name,n]))}}function EA(e){let t=[];for(let s of e){if(!s||s.type!=="VariableNode")return null;t.push(s.name)}return t}function _p(e,t,s){if(!e||typeof e!="object")return s;e.type==="BufferLoadNode"&&e.buffer&&e.buffer.name===t&&s.push(e);for(let n of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])e[n]&&_p(e[n],t,s);if(Array.isArray(e.args))for(let n of e.args)_p(n,t,s);if(Array.isArray(e.indices))for(let n of e.indices)_p(n,t,s);if(Array.isArray(e.stmts))for(let n of e.stmts)_p(n,t,s);return s}function Hu(e,t){if(!e||typeof e!="object")return t;e.type==="BufferLoadNode"&&e.buffer&&e.buffer.name&&t.add(e.buffer.name);for(let s of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])e[s]&&Hu(e[s],t);if(Array.isArray(e.args))for(let s of e.args)Hu(s,t);if(Array.isArray(e.indices))for(let s of e.indices)Hu(s,t);if(Array.isArray(e.stmts))for(let s of e.stmts)Hu(s,t);return t}function o_(e,t,s,n){let r=t.byName.get(s);if(!r||r.writes.length!==1)return null;let i=r.writes[0],o=[...new Set((t.consumersByBuf.get(i)||[]).filter(b=>b!==s))];if(o.length!==1)return null;let a=o[0],l=n(e,s),u=n(e,a);if(!l||!u||u.hasReduction)return null;let f=t.byName.get(a);if(!f||f.writes.length!==1||f.writes[0]===i)return null;let h=new Set(r.reads);if(Hu(r.node.body,h),Hu(r.node.initBody,h),h.has(f.writes[0]))return null;let p=r.node.body&&r.node.body.type==="BufferStoreNode"?r.node.body:null,d=f.node.body&&f.node.body.type==="BufferStoreNode"?f.node.body:null;if(!p||!d)return null;let m=l.loops.filter(b=>!l.reductionLoopVars.has(b.loopVar.name)).map(b=>b.loopVar.name),g=EA(p.indices);if(!g||g.join(",")!==m.join(","))return null;let y=u.loops.map(b=>b.loopVar.name);if(y.length!==m.length)return null;let _=EA(d.indices);if(!_||_.join(",")!==y.join(","))return null;let w=_p(d.value,i,[]);if(w.length===0)return null;for(let b of w){let x=EA(b.indices);if(!x||x.join(",")!==_.join(","))return null}return a}var NA=I(()=>{c(q3,"walkBlocks");c(i_,"buildBlockDAG");c(EA,"indexVarNames");c(_p,"collectLoads");c(Hu,"collectLoadedBuffers");c(o_,"findFusibleConsumer")});function RA(e){return e.isGPU()?vO():AO()}function j3(e){return e.isGPU()?SO():xO()}function W3(e,t,s,n){let r=Me(e,t);if(!r)return[RA(s)];let i=[],o=dO(r,aO(s));if(o&&i.push(o),s.kind===Te.CPU){let a=mO(r,oO);a&&i.push(a);let l=bO(r);l&&i.push(l);let u=n?o_(e,n,t,Me):null;u&&i.push(wO(u))}return i.push(RA(s)),i}function CA(e,{priority:t=100}={}){BA.push({matches:e.matches,derive:e.derive,priority:t}),BA.sort((s,n)=>s.priority-n.priority)}function OO(e,t,s,n={}){if(n.richGpu&&s.isGPU()){let o=TO(e,t,s);if(o!==null)return o}if(s.kind!==Te.CPU&&!s.isGPU())return[];let r=t_(e,t),i=i_(e);for(let o of BA)if(o.matches(r,s))return o.derive(e,t,s,i);return[]}var BA,MO=I(()=>{hi();Wo();e_();bA();gO();kO();IO();NA();c(RA,"reductionSketch");c(j3,"elementwiseSketch");c(W3,"deriveMultiLevel");BA=[];c(CA,"registerSketchRule");CA({matches:c(e=>e.hasReduction&&e.spatial>=1&&e.reads>=2,"matches"),derive:W3},{priority:10});CA({matches:c(e=>e.hasReduction,"matches"),derive:c((e,t,s)=>[RA(s)],"derive")},{priority:20});CA({matches:c(()=>!0,"matches"),derive:c((e,t,s)=>[j3(s)],"derive")},{priority:30});c(OO,"deriveSketches")});function TA(e,t,s,n,r={}){return OO(e,t,s,r)}var LO=I(()=>{MO();c(TA,"getSketchesForBlock")});function a_(e){let t=new Map,s=[e];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="BlockNode"&&t.set(n.name,n),n.body&&s.push(n.body),n.stmts)for(let r of n.stmts)s.push(r);n.thenBody&&s.push(n.thenBody),n.elseBody&&s.push(n.elseBody),n.initBody&&s.push(n.initBody)}}return t}function l_(e,t,s,n=null){let r=n||a_(e.body),i=r.get(t)||null,o=[t];if(i){let a=[];for(let u of i.reads)a.push(`${u.buffer.shape.join("x")}:${u.buffer.dtype}`);for(let u of i.writes)a.push(`${u.buffer.shape.join("x")}:${u.buffer.dtype}`);o.push(a.join(","));let l=[];if(Xe(i.body,l),i.initBody&&Xe(i.initBody,l),o.push(l.join(";")),i.writes.length===1){let u=i.writes[0].buffer.name,f=[];for(let h of K3(r).get(u)||[]){if(h===i)continue;let p=[];Xe(h.body,p),h.initBody&&Xe(h.initBody,p),f.push(`${h.name}#${p.join(";")}`)}f.length>0&&(f.sort(),o.push(`consumers:${f.join("|")}`))}}return o.push(s.name),o.push(s.kind),H3(o.join("|"))}function K3(e){if(e.__readersByBuffer)return e.__readersByBuffer;let t=new Map;for(let s of e.values()){if(!s.reads)continue;let n=new Set;for(let r of s.reads)r.buffer&&n.add(r.buffer.name);for(let r of n){let i=t.get(r);i||(i=[],t.set(r,i)),i.push(s)}}return e.__readersByBuffer=t,t}function Xe(e,t){if(!(!e||typeof e!="object"))switch(e.type){case"BufferStoreNode":if(t.push("store"),e.indices)for(let s of e.indices)Xe(s,t);Xe(e.value,t);return;case"BufferLoadNode":if(t.push(`load:${e.buffer.name}`),e.indices)for(let s of e.indices)Xe(s,t);return;case"MathOpNode":t.push(`math:${e.op}`),Xe(e.a,t),e.b&&Xe(e.b,t);return;case"CallExternNode":t.push(`call:${e.externName}`);for(let s of e.args)Xe(s,t);return;case"CompareNode":t.push(`cmp:${e.direction}`),Xe(e.a,t),Xe(e.b,t);return;case"CastNode":t.push(`cast:${e.fromDtype}->${e.toDtype}`),Xe(e.expr,t);return;case"IfThenElseNode":Xe(e.condition,t),Xe(e.thenBody,t),e.elseBody&&Xe(e.elseBody,t);return;case"SeqNode":for(let s of e.stmts)Xe(s,t);return;case"ForNode":Xe(e.body,t);return;case"BlockNode":e.initBody&&Xe(e.initBody,t),Xe(e.body,t);return;case"LetStmtNode":Xe(e.value,t),Xe(e.body,t);return;default:return}}function H3(e){let t=2166136261;for(let s=0;s<e.length;s++)t^=e.charCodeAt(s),t=t*16777619>>>0;return t.toString(16).padStart(8,"0")}var IA=I(()=>{c(a_,"buildBlockMap");c(l_,"computeWorkloadKey");c(K3,"readersByBuffer");c(Xe,"collectBlockOps");c(H3,"fnv1a")});var PO,wp,bp,DO=I(()=>{IA();PO="mlfw-codegen-1",wp=class{static{c(this,"TuningRecord")}constructor(t,s,n,r,i,o){this.workloadKey=t,this.sketchName=s,this.params=n,this.score=r,this.traceData=i,this.version=o,this.timestamp=Date.now()}},bp=class e{static{c(this,"TuningDatabase")}constructor(t=1){this.version=t,this._records=new Map}computeWorkloadKey(t,s,n,r=null){return l_(t,s,n,r)}store(t,s){let n=this._records.get(t);n||(n=[],this._records.set(t,n)),n.push(s),n.sort((r,i)=>i.score-r.score),n.length>10&&(n.length=10)}lookup(t){let s=this._records.get(t);return!s||s.length===0?null:s[0]}lookupTopK(t,s=5){let n=this._records.get(t);return n?n.slice(0,s):[]}has(t){return this._records.has(t)&&this._records.get(t).length>0}get size(){let t=0;for(let[,s]of this._records)t+=s.length;return t}serialize(){let t=[];for(let[s,n]of this._records)for(let r of n)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:PO,entries:t}}static deserialize(t){let s=new e(t.version);if(t.codegenVersion!==void 0&&t.codegenVersion!==PO)return s;for(let n of t.entries){let r=new wp(n.workloadKey,n.sketchName,n.params,n.score,n.traceData,n.version);r.timestamp=n.timestamp,s.store(n.workloadKey,r)}return s}saveToFile(t,s){return s.writeFile(t,JSON.stringify(this.serialize())),t}static loadFromFile(t,s){return s.exists(t)?e.deserialize(JSON.parse(s.readFile(t))):new e}clear(){this._records.clear()}}});function ga(e,t){if(!e)return 0;if(e.type==="ForNode"){let s=e.extent&&e.extent.type==="IntImmNode"?e.extent.value:1,n=e.threadTag?t:t*s;return ga(e.body,n)}if(e.type==="BlockNode")return Math.max(t,ga(e.body,t),e.initBody?ga(e.initBody,t):0);if(e.type==="SeqNode"){let s=t;for(let n of e.stmts)s=Math.max(s,ga(n,t));return s}return e.type==="IfThenElseNode"?Math.max(ga(e.thenBody,t),e.elseBody?ga(e.elseBody,t):t):e.type==="AllocateNode"||e.type==="LetStmtNode"?ga(e.body,t):t}function FO(e,t=.1){let s=e.slice().sort((g,y)=>g-y),n=s.length;if(n===0)return{median:0,min:0,trimmedMean:0,cv:0};let r=s[n>>1],i=s[0],o=Math.floor(n*t),a=o,l=n-o>a?n-o:n,u=0,f=0;for(let g=a;g<l;g++)u+=s[g],f++;let h=f>0?u/f:r,p=0;for(let g=a;g<l;g++){let y=s[g]-h;p+=y*y}let d=f>1?Math.sqrt(p/(f-1)):0,m=h>0?d/h:0;return{median:r,min:i,trimmedMean:h,cv:m}}var J3,c_,u_,$O=I(()=>{hh();J3=1e6;c(ga,"maxSerialTripCount");c_=class{static{c(this,"BenchmarkResult")}constructor(t,s,n,r,i=null,o=0){this.medianMs=t,this.minMs=s,this.trimmedMeanMs=i??t,this.cv=o,this.samples=n,this._totalBytes=r}get throughputGBs(){return this.minMs<=0||!this._totalBytes?0:this._totalBytes/(this.minMs*1e6)}};c(FO,"robustStats");u_=class{static{c(this,"BenchmarkRunner")}constructor(t,s={}){this.target=t,this.warmup=s.warmup??3,this.repeat=s.repeat??10,this.minRepeatMs=s.minRepeatMs??0,this.maxCv=s.maxCv??0,this.maxReMeasures=s.maxReMeasures??1,this.measurer=s.measurer||null,this._bufferCache=new Map}_getOrAllocBuffers(t){let s=0,n=[];for(let[,o]of t.bufferMap){let a=Math.max(o.numel(),1);n.push(a);let l=o.sizeInBytes();l>0&&(s+=l)}let r=n.join(","),i=this._bufferCache.get(r);i||(i=n.map(o=>new Float32Array(o)),this._bufferCache.set(r,i));for(let o of i)for(let a=0;a<o.length;a++)o[a]=Math.random()*2-1;return{buffers:i,totalBytes:s}}run(t){if(!t||!t.body)return null;if(!this.target.isCPU())return this.measurer?this._runMeasured(t):null;let s=new di(this.target),n;try{n=s.compile(t)}catch{return null}if(n.metadata.kind!=="js")return null;let r;try{r=new Function("return "+n.source)()}catch{return null}let{buffers:i,totalBytes:o}=this._getOrAllocBuffers(t);for(let u=0;u<this.warmup;u++)try{r(...i)}catch{return null}let a=[],l=null;for(let u=0;u<=this.maxReMeasures&&(this._collect(r,i,a),l=FO(a),!(this.maxCv<=0||l.cv<=this.maxCv));u++);return new c_(l.median,l.min,a,o,l.trimmedMean,l.cv)}_runMeasured(t){if(t.shapeParams&&t.shapeParams.length>0||ga(t.body,1)>J3)return null;let s;try{s=new di(this.target).compile(t)}catch{return null}let n=[],r=0;for(let[,a]of t.bufferMap){let l=Math.max(a.sizeInBytes(),1);n.push(l),r+=l}let i;try{i=this.measurer(s,n,[],{warmup:this.warmup,repeat:this.repeat})}catch{return null}if(!i||i.length===0)return null;let o=FO(i);return new c_(o.median,o.min,i,r,o.trimmedMean,o.cv)}_collect(t,s,n){let r=0,i=this.repeat*3;for(let o=0;o<i&&(o<this.repeat||r<this.minRepeatMs);o++){let a=performance.now();t(...s);let l=performance.now()-a;n.push(l),r+=l}}}});var f_,zO=I(()=>{f_=class{static{c(this,"Deadline")}constructor(t=1/0,s=null){this._clock=s||(()=>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 yo(e){let t=c(s=>{if(!s||typeof s!="object")return s;if(Array.isArray(s))return s.map(t);let n=Object.create(Object.getPrototypeOf(s));switch(n.type=s.type,n._parent=null,n._parentKey=null,n._parentIdx=-1,s.type){case"PrimFunc":n.name=s.name,n.params=s.params,n.body=t(s.body),n.bufferMap=new Map(s.bufferMap),n.shapeParams=s.shapeParams,n.shapeParamMap=s.shapeParamMap instanceof Map?new Map(s.shapeParamMap):s.shapeParamMap,n._setChild("body",n.body);break;case"ForNode":n.loopVar=s.loopVar,n.min=t(s.min),n.extent=t(s.extent),n.kind=s.kind,n.body=t(s.body),n.threadTag=s.threadTag,n._setChild("body",n.body);break;case"BlockNode":n.name=s.name,n.iterVars=s.iterVars.map(t),n.reads=s.reads,n.writes=s.writes,n.body=t(s.body),n.initBody=t(s.initBody),n._setChild("body",n.body),n._setChild("initBody",n.initBody);break;case"SeqNode":n.stmts=s.stmts.map(t),n._setChildren("stmts",n.stmts);break;case"AllocateNode":n.buffer=s.buffer,n.scope=s.scope,n.body=t(s.body),n._setChild("body",n.body);break;case"LetStmtNode":n.variable=s.variable,n.value=t(s.value),n.body=t(s.body),n._setChild("body",n.body);break;case"IfThenElseNode":n.condition=t(s.condition),n.thenBody=t(s.thenBody),n.elseBody=t(s.elseBody),n._setChild("thenBody",n.thenBody),n._setChild("elseBody",n.elseBody);break;case"WhileNode":n.condVar=s.condVar,n.condBody=t(s.condBody),n.loopBody=t(s.loopBody),n._setChild("condBody",n.condBody),n._setChild("loopBody",n.loopBody);break;default:for(let r of Object.keys(s)){if(r==="_parent"||r==="_parentKey"||r==="_parentIdx")continue;let i=s[r];i instanceof Map?n[r]=new Map(i):Array.isArray(i)?n[r]=i.map(t):typeof i=="object"&&i!==null&&i.type?n[r]=t(i):n[r]=i}break}return n},"cloneNode");return t(e)}function OA(e){let t=c(s=>{if(!s||typeof s!="object")return s;if(Array.isArray(s))return s.map(t);let n=Object.create(Object.getPrototypeOf(s));n.type=s.type,n._parent=null,n._parentKey=null,n._parentIdx=-1;for(let r of Object.keys(s)){if(r==="_parent"||r==="_parentKey"||r==="_parentIdx")continue;let i=s[r];i instanceof Map?n[r]=new Map(i):Array.isArray(i)?n[r]=i.map(t):typeof i=="object"&&i!==null&&i.type?n[r]=t(i):n[r]=i}return n._setChild&&(n.body&&n._setChild("body",n.body),n.initBody&&n._setChild("initBody",n.initBody),n.thenBody&&n._setChild("thenBody",n.thenBody),n.elseBody&&n._setChild("elseBody",n.elseBody)),n._setChildren&&n.stmts&&n._setChildren("stmts",n.stmts),n},"cloneNode");return t(e)}function UO(e,t,s){let n=s.get(t);if(!n)return null;let r=[],i=n._parent;for(;i&&i!==e;)i.type==="ForNode"&&r.push(i),i=i._parent;r.reverse();let o=OA(n);for(let u=r.length-1;u>=0;u--){let f=r[u],h=new wt(f.loopVar,OA(f.min),OA(f.extent),f.kind,o,f.threadTag);h._setChild("body",o),o=h}let a=new Map;for(let u of n.reads)a.set(u.buffer.name,u.buffer);for(let u of n.writes)a.set(u.buffer.name,u.buffer);let l=[];for(let u of e.params)a.has(u.name)&&l.push(u);return new To("__tune_"+t,l,o,a,[])}var MA=I(()=>{Jt();c(yo,"clonePrimFunc");c(OA,"cloneBlockSubtree");c(UO,"extractBlockMini")});function Q3(e,t,s){let n={...e};for(let r of t.variables)s.rngFloat()<s.mutationRate&&(n[r.name]=r.sample(i=>s.rng(i)));return n}function VO(e={}){return e.strategy==="random"?new LA({numTrials:e.numTrials,seed:e.seed,deadline:e.deadline}):new PA({populationSize:e.populationSize,numGenerations:e.numGenerations,mutationRate:e.mutationRate,eliteRatio:e.eliteRatio,seed:e.seed,deadline:e.deadline})}var h_,LA,PA,Y3,GO=I(()=>{h_=class{static{c(this,"SearchCandidate")}constructor(t,s,n){this.sketchName=t,this.params=s,this.score=n}},LA=class{static{c(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,s){let n=[];for(let r of t){if(this._expired())break;for(let i=0;i<this.numTrials&&!this._expired();i++){let o=r.sampleParams(l=>this._rng(l)),a=s(r,o);a&&n.push(new h_(r.name,o,a.score))}}return n.sort((r,i)=>i.score-r.score),{candidates:n,population:null}}},PA=class{static{c(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,s,n=null){let r=n&&n.length?n:this._initPopulation(t),i=new Map,o=c((l,u)=>{let f=l.name+"|"+JSON.stringify(u);if(i.has(f))return i.get(f);let h=s(l,u);return i.set(f,h),h},"evalMemo");for(let l=0;l<this.numGenerations&&!this._expired();l++){let u=[];for(let d of r){let m=o(d.sketch,d.params);m&&u.push({...d,score:m.score})}if(u.sort((d,m)=>m.score-d.score),u.length===0)break;let f=Math.max(1,Math.floor(u.length*this.eliteRatio)),h=u.slice(0,f),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 l of r){let u=o(l.sketch,l.params);u&&a.push(new h_(l.sketch.name,l.params,u.score))}return a.sort((l,u)=>u.score-l.score),{candidates:a,population:r}}_initPopulation(t){let s=[];for(let n=0;n<this.populationSize;n++){let r=t[this._rng(t.length)],i=r.sampleParams(o=>this._rng(o));s.push({sketch:r,params:i})}return s}_crossover(t,s){if(t.sketch!==s.sketch)return{...t.params};let n={};for(let r of t.sketch.variables)n[r.name]=this._rngFloat()<.5?t.params[r.name]:s.params[r.name];return n}_mutate(t,s){let n={rngFloat:c(()=>this._rngFloat(),"rngFloat"),rng:c(o=>this._rng(o),"rng"),mutationRate:this.mutationRate},r=[Q3,...Y3],i=s;for(let o of r)i=o(i,t,n);return{sketch:t,params:i}}},Y3=[];c(Q3,"defaultResampleMutator");c(VO,"createSearchStrategy")});function d_(e){let t={"threadIdx.x":1,"threadIdx.y":1,"threadIdx.z":1},s=new Set,n=c(r=>{if(!(!r||typeof r!="object"||s.has(r))){if(s.add(r),r.type==="ForNode"&&t[r.threadTag]!==void 0){let i=r.extent&&r.extent.type==="IntImmNode"?r.extent.value:1;i>t[r.threadTag]&&(t[r.threadTag]=i)}for(let i in r){let o=r[i];Array.isArray(o)?o.forEach(n):o&&typeof o=="object"&&n(o)}}},"visit");return n(e.body),X3.reduce((r,i)=>r*t[i],1)}var X3,p_,qO=I(()=>{np();Zx();mA();MA();GO();_A();X3=["threadIdx.x","threadIdx.y","threadIdx.z"];c(d_,"gpuThreadBlockSize");p_=class{static{c(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 s=this.sketches.some(i=>i.name==="fused");s?(this.learnedModel=new Wu,this.costModel=new Ku(t.costModel.analytical,this.learnedModel)):(this.costModel=t.costModel,this.learnedModel=t.learnedModel);let n=s?null:UO(t.primFunc,t.blockName,t.blockMap);this.evalFunc=n||t.primFunc,this.evalBlockName=t.blockName,this.sketchByName=new Map;for(let i of this.sketches)this.sketchByName.set(i.name,i);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=VO({...this.config,deadline:this.deadline}),this.population=null,this._best=null,this.plateaued=!1}runRound(){let t=this._best?this._best.measuredScore:-1/0,s=this._produceCandidates();if(s.length===0)return this.plateaued=!0,0;if(this.benchmarkRunner)this._measureAndLearn(s);else{let r=s[0];this._consider({sketchName:r.sketchName,params:r.params,score:r.score,measuredScore:r.score}),this.plateaued=!0}let n=this._best?this._best.measuredScore:-1/0;return Math.max(0,n-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 s=new En(yo(this.primFunc));return t.instantiate(this._best.params)(s,this.blockName,this.target),s.trace.serialize()}catch{return null}}_produceCandidates(){if(this.enumSketch){let r=[];for(let i of this.enumParams){let o=this._evaluate(this.enumSketch,i);o&&r.push({sketchName:this.enumSketch.name,params:i,score:o.score})}return r.sort((i,o)=>o.score-i.score),r}let t=c((r,i)=>this._evaluate(r,i),"evaluator"),{candidates:s,population:n}=this.strategy.search(this.sketches,t,this.population);return this.population=n,s}_evaluate(t,s){try{let n=yo(this.evalFunc),r=new En(n);if(t.instantiate(s)(r,this.evalBlockName,this.target),Qc.validate(n).length>0)return null;let o=this.target.maxThreadsPerBlock;return this.target.isGPU&&this.target.isGPU()&&o&&d_(n)>o?null:{score:this.costModel.score(n)}}catch{return null}}_measureAndLearn(t){let s=t.slice(0,this.config.topKForBenchmark);for(let n of s){if(this.deadline&&this.deadline.expired)break;let r=this._measure(n);if(!r)continue;let i=-r.result.medianMs;this.learnedModel.addSample(r.features,i),this._consider({sketchName:n.sketchName,params:n.params,score:i,measuredScore:i,medianMs:r.result.medianMs,minMs:r.result.minMs})}this.learnedModel.train()}_measure(t){let s=this.sketchByName.get(t.sketchName);if(!s)return null;let n,r;try{n=yo(this.primFunc),s.instantiate(t.params)(new En(n),this.blockName,this.target),r=yo(this.evalFunc),s.instantiate(t.params)(new En(r),this.evalBlockName,this.target)}catch{return null}let i=this.benchmarkRunner.run(n);return i?{result:i,features:zl.extractStatements(r)}:null}_consider(t){(!this._best||t.measuredScore>this._best.measuredScore)&&(this._best=t)}}});var DA,m_,jO=I(()=>{DA=class{static{c(this,"GradientSchedulerPolicy")}pick(t){let s=t.filter(o=>!o.plateaued);if(s.length===0)return null;let n=s.filter(o=>o.rounds===0);if(n.length>0)return n[0];let r=null,i=-1/0;for(let o of s){let a=o.gainEwma!==void 0?o.gainEwma:o.lastGain,l=o.weight*a;l>i&&(i=l,r=o)}return r}},m_=class{static{c(this,"TaskScheduler")}constructor(t=null){this.policy=t||new DA}run(t,s,n={}){let r=n.maxRoundsPerTask??8,i=n.plateauPatience??2,o=n.gainEwmaAlpha??.5;for(let a of t)a.rounds=0,a.lastGain=0,a.gainEwma=0,a.stale=0,a.plateaued=!1;for(;!(s&&s.expired);){let a=this.policy.pick(t);if(!a)break;let l=a.session.runRound();a.rounds++,a.lastGain=l,a.gainEwma=o*l+(1-o)*a.gainEwma,l<=0?a.stale++:a.stale=0,(a.session.plateaued||a.stale>=i||a.rounds>=r)&&(a.plateaued=!0)}}}});function Z3(e){if(e.isCPU())return null;let t=MR(e.kind);if(!t)throw new Error("hardwareMeasure requested for target '"+e.kind+"' but no measurer is registered for it; the corresponding runtime must be loaded (Node: import '#io/cuda_runtime') before compiling");return t}var g_,y_,WO=I(()=>{np();_A();LO();DO();$O();zO();IA();e_();NA();Wo();qO();MA();Jt();jO();Em();c(Z3,"resolveMeasurer");g_=class{static{c(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}},y_=class{static{c(this,"Autotuner")}constructor(t,s={}){this.target=t,this.config=s instanceof g_?s:new g_(s),this.config.hardwareMeasure&&(this.config.measurer=Z3(t)),this.analyticalModel=new Zy(t),this.learnedModel=new Wu,this.costModel=new Ku(this.analyticalModel,this.learnedModel),this.db=this.config.tuningDB instanceof bp?this.config.tuningDB:new bp,this.benchmarkRunner=this.config.enableBenchmark?new u_(t,{warmup:this.config.benchmarkWarmup,repeat:this.config.benchmarkRepeat,maxCv:this.config.benchmarkMaxCv,measurer:this.config.measurer}):null,this.scheduler=new m_(this.config.schedulerPolicy)}tune(t,s=null){let n=s?[s]:yp(t.body),r=a_(t.body),i=new f_(this.config.timeBudgetMs,this.config.clock),o=new Map,a=new Map;for(let f of n){let h=l_(t,f,this.target,r);a.set(f,h);let p=o.get(h);if(p){p.weight++;continue}if(this.config.useTuningDB&&this.db.has(h)){o.set(h,{key:h,kind:"cache",cached:this.db.lookup(h),weight:1});continue}let d=TA(t,f,this.target,r,{richGpu:this.config.richGpu??!!this.config.measurer});if(d.length===0){o.set(h,{key:h,kind:"empty",weight:1});continue}let m=new p_({target:this.target,primFunc:t,blockName:f,blockMap:r,sketches:d,costModel:this.costModel,learnedModel:this.learnedModel,benchmarkRunner:this.benchmarkRunner,config:this.config,deadline:i});o.set(h,{key:h,kind:"session",session:m,weight:1})}let l=[...o.values()].filter(f=>f.kind==="session");l.length>0&&this.scheduler.run(l,i,this.config);let u=new Map;for(let f of n){let h=o.get(a.get(f));if(h.kind==="cache"){u.set(f,{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 wp(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}u.set(f,{sketchName:p.sketchName,params:p.params,score:p.score,fromCache:!1,medianMs:p.medianMs,minMs:p.minMs})}}return u}tuneAndApply(t,s=null){let n=this.tune(t,s);if(n.size>0){let r=this._applyBestSchedule(t,n);if(r)return{func:r.func,results:n,applied:!0}}return{func:t,results:n,applied:!1}}_applyBestSchedule(t,s){let n=this._buildTunedSchedule(t,s);if(n&&this._scheduleIsValid(n))return this._adoptSchedule(t,n),{func:t};let r=this._buildDefaultSchedule(t);return r?(this._adoptSchedule(t,r),{func:t}):n?(this._adoptSchedule(t,n),{func:t}):null}_buildTunedSchedule(t,s){try{let n=yo(t),r=new En(n),i=a_(n.body),o=i_(n),a=new Set,l=[];for(let f of s)if(f[1].sketchName==="fused"){let h=o_(n,o,f[0],Me);h&&a.add(h),l.unshift(f)}else l.push(f);let u=new Set;for(let[f,h]of l)if(!(a.has(f)||u.has(h))&&(u.add(h),!(!h.sketchName||!h.params)))try{let d=TA(n,f,this.target,i,{richGpu:this.config.richGpu??!!this.config.measurer}).find(m=>m.name===h.sketchName);d&&this._fitsThreadBlock(n,f,d,h.params)&&d.instantiate(h.params)(r,f,this.target)}catch{continue}return this._scheduleResidualBlocks(r,a),n}catch{return null}}_scheduleResidualBlocks(t,s){let n=null;for(let r of yp(t.func.body))if(!(s.has(r)||this._blockIsParallelized(t,r))){n||(n=new lo(this.target));try{n.applyToBlock(t,r)}catch{}}}_blockIsParallelized(t,s){let n;try{n=t.getLoops(s)}catch{return!0}for(let r of n)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:d_(t)<=this.target.maxThreadsPerBlock}_buildDefaultSchedule(t){try{let s=yo(t);return new lo(this.target).applyToAllBlocks(new En(s)),s}catch{return null}}_adoptSchedule(t,s){Object.assign(t,s),t._setChild("body",t.body)}_fitsThreadBlock(t,s,n,r){if(!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock)return!0;try{let i=yo(t);return n.instantiate(r)(new En(i),s,this.target),d_(i)<=this.target.maxThreadsPerBlock}catch{return!1}}}});var __,KO=I(()=>{Jt();__=class{static{c(this,"TensorVerifier")}constructor(){this.errors=[],this.boundVars=new Set}verify(t){if(this.errors=[],this.boundVars.clear(),!(t instanceof To))return this.errors.push("Expected PrimFunc at root"),this.errors;for(let s of t.params)this.boundVars.add(s.name);if(t.shapeParams)for(let s of t.shapeParams)this.boundVars.add(s.name);return this.visit(t.body),this.errors}visit(t){let s=[t];for(;s.length>0;){let n=s.pop();if(n)switch(n.type){case"SeqNode":for(let r=n.stmts.length-1;r>=0;r--)s.push(n.stmts[r]);break;case"ForNode":this.boundVars.has(n.loopVar.name)&&this.errors.push(`Loop variable ${n.loopVar.name} already bound`),this.boundVars.add(n.loopVar.name),s.push({type:"_unbind",name:n.loopVar.name}),s.push(n.body);break;case"BlockNode":for(let r of n.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=n.iterVars.length-1;r>=0;r--)n.iterVars[r].iterVar&&s.push({type:"_unbind",name:n.iterVars[r].iterVar.name});s.push(n.body),n.initBody&&s.push(n.initBody);break;case"AllocateNode":n.buffer||this.errors.push("Allocate missing buffer"),s.push(n.body);break;case"LetStmtNode":this._visitExpr(n.value),this.boundVars.add(n.variable.name),s.push({type:"_unbind",name:n.variable.name}),s.push(n.body);break;case"IfThenElseNode":this._visitExpr(n.condition),n.elseBody&&s.push(n.elseBody),s.push(n.thenBody);break;case"WhileNode":n.condVar||this.errors.push("WhileNode missing condition variable"),s.push(n.loopBody),s.push(n.condBody);break;case"BufferStoreNode":if(n.buffer||this.errors.push("BufferStore missing buffer"),(!n.indices||n.indices.length!==n.buffer.shape.length)&&this.errors.push(`BufferStore rank mismatch for ${n.buffer?n.buffer.name:"unknown"}`),n.indices)for(let r of n.indices)this._visitExpr(r);this._visitExpr(n.value);break;case"EvaluateNode":this._visitExpr(n.value);break;case"_unbind":this.boundVars.delete(n.name);break;default:this._visitExpr(n);break}}}_visitExpr(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 s of t.indices)this._visitExpr(s);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 s of t.args)this._visitExpr(s);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 FA(e){let t=[];e.functionCount===0&&t.push(new Le("Module has no functions"));for(let s of e)xp(s,t);return t}function xp(e,t=[]){if(!e.entryBlock)return t.push(new Le("Function has no entry block",null,e)),t;e.entryBlock.arguments.length!==e.inputTypes.length&&t.push(new Le(`Entry block has ${e.entryBlock.arguments.length} args, expected ${e.inputTypes.length}`,null,e));for(let r=0;r<e.entryBlock.arguments.length;r++){let i=e.entryBlock.arguments[r],o=e.inputTypes[r];o&&!i.type.equals(o)&&t.push(new Le(`Block arg ${r} type ${Ws(i.type)} != expected ${Ws(o)}`,null,e))}let s=new Set;for(let r of e.entryBlock.arguments)s.add(r);HO(e.body,s);for(let r of e.body)JO(r,e,s,t);let n=e.getReturnOp();if(!n)t.push(new Le("Missing return op",null,e));else{n.numOperands!==e.outputTypes.length&&t.push(new Le(`Return has ${n.numOperands} operands, function declares ${e.outputTypes.length} outputs`,n,e));for(let r=0;r<Math.min(n.numOperands,e.outputTypes.length);r++){let i=n.getOperand(r).type,o=e.outputTypes[r];o instanceof T&&i instanceof T&&(i.dtype!==o.dtype&&t.push(new Le(`Return operand ${r} dtype ${i.dtype} != expected ${o.dtype}`,n,e)),i.shapeCompatible(o)||t.push(new Le(`Return operand ${r} shape incompatible: ${Ws(i)} vs ${Ws(o)}`,n,e)))}}return t}function HO(e,t){for(let s of e){for(let n of s.arguments)t.add(n);for(let n of s){for(let r=0;r<n.numResults;r++)t.add(n.getResult(r));for(let r of n.regions)HO(r,t)}}}function JO(e,t,s,n){for(let r of e)tG(r,t,s,n);if(e.size>0){let r=e.lastOp;if(e.parentRegion&&e.parentRegion.parentOp){let i=e.parentRegion.parentOp.opName,o=rt.get(i);o&&o.hasRegions&&(r.isTerminator()||n.push(new Le(`Block in ${i} region must end with terminator, got '${r.opName}'`,r,t)))}}}function tG(e,t,s,n){for(let i=0;i<e.numOperands;i++){let o=e.getOperand(i);if(!o){n.push(new Le(`Operand ${i} is null`,e,t));continue}if(!(o instanceof Ka)){n.push(new Le(`Operand ${i} is not a Value`,e,t));continue}s.has(o)||n.push(new Le(`Operand ${i} used before definition`,e,t))}for(let i=0;i<e.numResults;i++){let o=e.getResult(i);o||n.push(new Le(`Result ${i} is null`,e,t)),o&&o.definingOp!==e&&n.push(new Le(`Result ${i} definingOp mismatch`,e,t))}let r=rt.get(e.opName);if(!r){!["return","yield"].includes(e.opName)&&!rt.has(e.opName)&&n.push(new Le(`Unknown op '${e.opName}'`,e,t));return}r.numOperands>=0&&e.numOperands!==r.numOperands&&n.push(new Le(`'${e.opName}' expects ${r.numOperands} operands, got ${e.numOperands}`,e,t)),r.numResults>=0&&e.numResults!==r.numResults&&n.push(new Le(`'${e.opName}' expects ${r.numResults} results, got ${e.numResults}`,e,t));for(let i of r.attrs)i.required&&!e.hasAttr(i.name)&&n.push(new Le(`'${e.opName}' missing required attribute '${i.name}'`,e,t));if(r.hasRegions&&r.numRegions>0&&e.numRegions!==r.numRegions&&n.push(new Le(`'${e.opName}' expects ${r.numRegions} regions, got ${e.numRegions}`,e,t)),r.verify){let i=r.verify(e);if(i)for(let o of i)n.push(new Le(o,e,t))}if(r.inferResultTypes&&e.numResults>0){let i=[];for(let a=0;a<e.numOperands;a++)i.push(e.getOperand(a).type);let o=r.inferResultTypes(i,e.attributes,e.results.map(a=>a.type));if(o)for(let a=0;a<Math.min(o.length,e.numResults);a++){let l=e.getResult(a).type,u=o[a];u instanceof T&&l instanceof T&&(l.dtype!==u.dtype&&n.push(new Le(`Result ${a} dtype ${l.dtype} != inferred ${u.dtype}`,e,t)),l.shapeCompatible(u)||n.push(new Le(`Result ${a} shape ${Ws(l)} incompatible with inferred ${Ws(u)}`,e,t)))}}for(let i of e.regions){let o=new Set(s);for(let a of i){for(let l of a.arguments)o.add(l);JO(a,t,o,n)}}}var Le,YO=I(()=>{It();vd();rs();Le=class{static{c(this,"VerificationError")}constructor(t,s=null,n=null){this.message=t,this.op=s,this.func=n}toString(){let t="";return this.func&&(t+=`[${this.func.name}] `),this.op&&(t+=`op '${this.op.opName}' (id=${this.op.id}): `),t+this.message}};c(FA,"verifyModule");c(xp,"verifyFunction");c(HO,"collectDefinedValues");c(JO,"verifyBlock");c(tG,"verifyOperation")});var w_,$A,b_,zA,QO=I(()=>{aA();It();w_=class{static{c(this,"ValueObserver")}constructor(){this.min=1/0,this.max=-1/0,this.count=0,this.histogram=null}update(t){for(let s=0;s<t.length;s++){let n=t[s];n<this.min&&(this.min=n),n>this.max&&(this.max=n)}this.count+=t.length,this.histogram&&this.histogram.update(t)}enableHistogram(t=2048){this.histogram=new $A(t)}},$A=class{static{c(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 s=this.rangeMax-this.rangeMin;if(s<=0)return;for(let r=0;r<t.length;r++){let i=t[r];if(i<this.rangeMin||i>this.rangeMax){this._expandAndRebucket(t);return}}let n=this.numBins/s;for(let r=0;r<t.length;r++){let i=Math.min(this.numBins-1,Math.floor((t[r]-this.rangeMin)*n));this.bins[i]++}this.totalCount+=t.length}_initRange(t){let s=1/0,n=-1/0;for(let r=0;r<t.length;r++)t[r]<s&&(s=t[r]),t[r]>n&&(n=t[r]);s===n&&(n=s+1),this.rangeMin=s,this.rangeMax=n,this.initialized=!0}_expandAndRebucket(t){let s=this.rangeMin,n=this.rangeMax;for(let f=0;f<t.length;f++)t[f]<s&&(s=t[f]),t[f]>n&&(n=t[f]);let r=new Float64Array(this.bins),i=this.rangeMin,a=this.rangeMax-i;this.rangeMin=s,this.rangeMax=n;let l=n-s,u=this.numBins/l;if(this.bins.fill(0),a>0){let f=a/this.numBins;for(let h=0;h<this.numBins;h++){if(r[h]===0)continue;let p=i+(h+.5)*f,d=Math.min(this.numBins-1,Math.floor((p-s)*u));this.bins[d]+=r[h]}}for(let f=0;f<t.length;f++){let h=Math.min(this.numBins-1,Math.floor((t[f]-s)*u));this.bins[h]++}this.totalCount+=t.length}computePercentileThreshold(t){let s=this.totalCount*t,n=0,r=(this.rangeMax-this.rangeMin)/this.numBins;for(let i=0;i<this.numBins;i++)if(n+=this.bins[i],n>=s)return this.rangeMin+(i+1)*r;return this.rangeMax}computeEntropyThreshold(t){if(this.totalCount===0)return this.rangeMax;let s=new Float64Array(this.numBins);for(let o=0;o<this.numBins;o++)s[o]=this.bins[o]/this.totalCount;let n=this.numBins,r=1/0;for(let o=t;o<=this.numBins;o++){let a=o/t,l=new Float64Array(o);for(let f=0;f<t;f++){let h=Math.floor(f*a),p=Math.min(o,Math.floor((f+1)*a)),d=0;for(let g=h;g<p;g++)d+=s[g];let m=p-h;if(m>0){let g=d/m;for(let y=h;y<p;y++)l[y]=g}}let u=0;for(let f=0;f<o;f++)s[f]>0&&l[f]>0&&(u+=s[f]*Math.log(s[f]/l[f]));u<r&&(r=u,n=o)}let i=(this.rangeMax-this.rangeMin)/this.numBins;return this.rangeMin+n*i}},b_=class{static{c(this,"CalibrationCollector")}constructor(t="minmax"){this.observers=new Map,this.mode=t}attach(t){for(let s of t.ops())for(let n=0;n<s.numResults;n++){let r=s.getResult(n);if(r.type instanceof T&&ss(r.type.dtype)){let i=new w_;(this.mode==="entropy"||this.mode==="percentile")&&i.enableHistogram(),this.observers.set(r,i)}}for(let s of t.args)if(s.type instanceof T&&ss(s.type.dtype)){let n=new w_;(this.mode==="entropy"||this.mode==="percentile")&&n.enableHistogram(),this.observers.set(s,n)}}observe(t,s){let n=this.observers.get(t);n&&n.update(s)}getResult(){return new zA(this.observers,this.mode)}},zA=class{static{c(this,"CalibrationResult")}constructor(t,s){this._observers=t,this._mode=s}getRange(t){let s=this._observers.get(t);return!s||s.count===0?null:{min:s.min,max:s.max}}getQuantParams(t,s,n){let r=this.getRange(t);if(!r)return null;let i=s===Un.PER_TENSOR_ASYMMETRIC;if(this._mode==="percentile"&&this._observers.get(t).histogram){if(i)return Vn.fromRange(r.min,r.max,s,n);let a=this._observers.get(t).histogram.computePercentileThreshold(.999);return Vn.fromRange(-a,a,s,n)}if(this._mode==="entropy"&&this._observers.get(t).histogram){if(i)return Vn.fromRange(r.min,r.max,s,n);let o=this._observers.get(t).histogram,a=n==="ui8"?256:255,l=o.computeEntropyThreshold(a);return Vn.fromRange(-l,l,s,n)}return Vn.fromRange(r.min,r.max,s,n)}hasData(t){let s=this._observers.get(t);return s&&s.count>0}values(){return this._observers.keys()}}});var UA,x_,XO=I(()=>{Ci();It();UA=class{static{c(this,"LivenessResult")}constructor(t,s,n,r,i,o,a){this.liveIn=t,this.liveOut=s,this.intervals=n,this.opIndex=r,this.peakPressure=i,this.peakOp=o,this.pressureAtOp=a}interfere(t,s){if(t===s)return!0;let n=this.intervals.get(t),r=this.intervals.get(s);return n&&r?n.start<=r.end&&r.start<=n.end:!1}liveAtOp(t){return this.liveIn.get(t)||new Set}intervalOf(t){return this.intervals.get(t)||null}},x_=class e{static get name(){return"liveness"}static get depKey(){return"liveness"}static get dependencies(){return[ne]}static buildIntervals(t,s){let n=new Map;for(let i=0;i<s.length;i++)n.set(s[i],i);let r=new Map;for(let i of t.args)r.set(i,{start:-1,end:-1});for(let i=0;i<s.length;i++){let o=s[i];for(let a=0;a<o.numResults;a++)r.set(o.getResult(a),{start:i,end:i})}for(let i=0;i<s.length;i++){let o=s[i];for(let a=0;a<o.numOperands;a++){let l=o.getOperand(a),u=r.get(l);u&&u.end<i&&(u.end=i)}}return{intervals:r,opIndex:n}}static compute(t,s={}){let n=s.useDef||ne.compute(t),r=n.topologicalOrder,{intervals:i,opIndex:o}=e.buildIntervals(t,r),a=new Map,l=new Map;for(let d of r)a.set(d,new Set),l.set(d,new Set);let u=!0;for(;u;){u=!1;for(let d=r.length-1;d>=0;d--){let m=r[d],g=l.get(m),y=g.size,_=n.opUsers.get(m);if(_)for(let x of _){let A=a.get(x);if(A)for(let S of A)g.add(S)}let w=a.get(m),b=w.size;for(let x of g)w.add(x);for(let x=0;x<m.numResults;x++)w.delete(m.getResult(x));for(let x=0;x<m.numOperands;x++)w.add(m.getOperand(x));(g.size!==y||w.size!==b)&&(u=!0)}}for(let[d,m]of o){let g=a.get(d);for(let y of g){let _=i.get(y);_&&_.end<m&&(_.end=m)}}let f=0,h=null,p=new Map;for(let d=0;d<r.length;d++){let m=r[d],g=a.get(m),y=0;for(let _ of g)if(_.type instanceof T){let w=_.type.sizeInBytes();w!==X&&(y+=w)}p.set(m,y),y>f&&(f=y,h=m)}return new UA(a,l,i,o,f,h,p)}}});var eG,S_,A_,ZO=I(()=>{hs();xn();It();rs();ps();Ci();XO();eG=new Set(["return","yield","constant","scalar_constant"]),S_=class{static{c(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}},A_=class extends Mt{static{c(this,"RematerializationPass")}constructor(t={}){super("RematerializationPass"),this.config=t instanceof S_?t:new S_(t)}run(t,s){if(this.config.memoryBudget===1/0)return et.UNCHANGED;let n=!1,r=0,i=0;for(;r<this.config.maxIterations;){let o=ne.compute(t),{peakPressure:a,candidates:l}=this._analyzeIntervalPressure(t,o);if(i=a,a<=this.config.memoryBudget||l.length===0)break;l.sort((u,f)=>f.score-u.score),this._rematerialize(t,l[0]),n=!0,r++}return this.trace&&this.trace.level>=at.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,iterations:r,peakPressure:i,budget:this.config.memoryBudget,changed:n,level:at.DEBUG}),n?et.CHANGED:et.UNCHANGED}_analyzeIntervalPressure(t,s){let n=s.topologicalOrder,{intervals:r}=x_.buildIntervals(t,n),i=[];for(let[d,m]of r){if(!(d.type instanceof T))continue;let g=d.type.sizeInBytes();g===X||g<=0||(i.push({idx:m.start,delta:g,value:d}),i.push({idx:m.end+1,delta:-g,value:null}))}i.sort((d,m)=>d.idx-m.idx||d.delta-m.delta);let o=0,a=0,l=0,u=new Set,f=new Set,h=0;for(let d=-1;d<=n.length;d++){for(;h<i.length&&i[h].idx<=d;)o+=i[h].delta,i[h].delta>0&&i[h].value&&f.add(i[h].value),i[h].delta<0&&i[h].value&&f.delete(i[h].value),h++;if(o>a){a=o,l=d,u.clear();for(let m of f)u.add(m)}}let p=[];for(let d of u){if(!this._canRematerialize(d))continue;let m=d.definingOp,g=this._computeMemorySaved(d);if(g<=0)continue;let y=this._estimateRecomputeCost(m);y>=this.config.maxRecomputeCost||y!==0&&p.push({value:d,definingOp:m,memorySaved:g,recomputeCost:y,score:g/y})}return{peakPressure:a,peakIdx:l,candidates:p}}_canRematerialize(t){if(t.isBlockArgument())return!1;let s=t.definingOp;if(!s||eG.has(s.opName)||this.config.excludeOps.has(s.opName)||s.regions.length>0||s.hasSideEffects()||t.useCount<=1)return!1;for(let n=0;n<s.numOperands;n++){let r=s.getOperand(n);if(r.definingOp&&r.definingOp.hasSideEffects())return!1}return!0}_computeMemorySaved(t){if(!(t.type instanceof T))return 0;let s=t.type.sizeInBytes();return s===X?0:s}_operandExtensionCost(t,s,n){let r=0;for(let i=0;i<t.numOperands;i++){let o=t.getOperand(i),a=0;for(let l of o.uses()){let u=n.get(l.user)||0;u>a&&(a=u)}if(s>a&&o.type instanceof T){let l=o.type.sizeInBytes();l!==X&&(r+=l)}}return r}_estimateRecomputeCost(t){let s=rt.get(t.opName);if(!s||s.isOpaque)return 1/0;if(s.getFlops){let n=s.getFlops(t);if(n>0)return n}if(s.isElementwise||s.isBroadcast||s.isView){let n=0;for(let r=0;r<t.numResults;r++){let i=t.getResult(r).type;if(i instanceof T){let o=i.numel();if(o!==X){n=o;break}}}return n||1}if(s.isReduction)for(let n=0;n<t.numOperands;n++){let r=t.getOperand(n).type;if(r instanceof T){let i=r.numel();if(i!==X)return i}}return 1/0}_rematerialize(t,s){let{value:n,definingOp:r}=s,i=[];for(let h of n.uses())i.push({user:h.user,operandIndex:h.operandIndex});if(i.length<=1)return;let o=new Map,a=0;for(let h of t.ops())o.set(h,a++);i.sort((h,p)=>(o.get(h.user)||0)-(o.get(p.user)||0));let l=o.get(i[i.length-1].user)||0,u=this._operandExtensionCost(r,l,o),f=this._computeMemorySaved(n);if(!(u>=f))for(let h=1;h<i.length;h++){let p=i[h],d=p.user;if(!d.parentBlock)continue;let m=new bt(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(n.resultIndex))}}}});function Ul(e){let t=new Set(e),s=[],n=new Map,r=c(i=>{let o=n.get(i);if(o!==2){if(o===1)throw new Error("partition topo sort: cycle detected");n.set(i,1);for(let a=0;a<i.numOperands;a++){let l=i.getOperand(a).definingOp;l&&t.has(l)&&r(l)}n.set(i,2),s.push(i)}},"visit");for(let i of e)r(i);return s}function Sp(e,{labelOf:t,sameLabel:s=c((o,a)=>o===a,"sameLabel"),canMerge:n=c(()=>!0,"canMerge"),onAttach:r=c(()=>{},"onAttach"),sort:i=Ul}){let o=i(e),a=new Map,l=new Map,u=[],f=0,h=c((m,g)=>{if(m===g)return!0;let y=[g],_=new Set;for(;y.length>0;){let w=y.pop();if(w===m)return!0;if(_.has(w))continue;_.add(w);let b=l.get(w);if(b)for(let x of b)y.push(x)}return!1},"isUpstreamOf"),p=c(m=>{let g=new Set;for(let y=0;y<m.numOperands;y++){let _=m.getOperand(y).definingOp;if(!_)continue;let w=a.get(_);w&&g.add(w)}return g},"operandParts"),d=c((m,g)=>{for(let y of p(m)){if(y===g)continue;let _=l.get(g);_||(_=new Set,l.set(g,_)),_.add(y)}},"recordEdges");for(let m of o){let g=t(m);if(g==null)continue;let y=!1;for(let _=0;_<m.numOperands;_++){let w=m.getOperand(_).definingOp;if(!w)continue;let b=a.get(w);if(!b||!s(b.label,g)||!n(b,m,g))continue;let x=!1;for(let A of p(m))if(A!==b&&h(b,A)){x=!0;break}if(!x){b.ops.push(m),b.opSet.add(m),r(b,m),a.set(m,b),d(m,b),y=!0;break}}if(!y){let _={id:f++,label:g,ops:[m],opSet:new Set([m])};r(_,m),u.push(_),a.set(m,_),d(m,_)}}return{partitions:u,opToPart:a,preds:l}}function v_(e,t,{pullConstants:s=!1,isConstant:n=c(()=>!1,"isConstant")}={}){let r=[],i=new Set,o=[],a=new Set,l=[],u=new Set;for(let f of t){for(let h=0;h<f.numOperands;h++){let p=f.getOperand(h),d=p.definingOp;if(!(d&&e.has(d))){if(s&&d&&n(d)){u.has(d)||(u.add(d),l.push(d));continue}i.has(p)||(i.add(p),r.push(p))}}for(let h=0;h<f.numResults;h++){let p=f.getResult(h);if(a.has(p))continue;let d=!1;for(let m of p.uses())if(!e.has(m.user)){d=!0;break}d&&(a.add(p),o.push(p))}}return{inputs:r,outputs:o,constDefs:l}}function VA(e,t){let s=new Map,n=new Map;for(let o of e)s.set(o,0),n.set(o,[]);for(let o of e){let a=t.get(o);if(a)for(let l of a)n.has(l)&&(n.get(l).push(o),s.set(o,s.get(o)+1))}let r=[];for(let o of e)s.get(o)===0&&r.push(o);let i=[];for(;r.length>0;){let o=r.shift();i.push(o);for(let a of n.get(o)){let l=s.get(a)-1;s.set(a,l),l===0&&r.push(a)}}return i.length===e.length?i:null}var k_=I(()=>{c(Ul,"topoSortOps");c(Sp,"buildPartitions");c(v_,"computePartitionIO");c(VA,"topoSortPartitions")});function qA(e){let t=0;for(let s=0;s<e.numResults;s++){let n=e.getResult(s).type;n instanceof T&&n.isFullyStatic&&(t+=n.sizeInBytes())}return t}var GA,Ju,jA,E_,t1=I(()=>{rs();It();k_();GA=class{static{c(this,"Partition")}constructor(t,s){this.id=t,this.target=s,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+=qA(t))}hasOp(t){return this.opSet.has(t)}merge(t){for(let s of t.ops)this.addOp(s)}computeIO(){if(this._inputValues&&this._outputValues)return;this._inputValues=[],this._outputValues=[];let t=new Set,s=new Set;for(let n of this.ops){for(let r=0;r<n.numOperands;r++){let i=n.getOperand(r);i.definingOp&&this.opSet.has(i.definingOp)||t.has(i)||(t.add(i),this._inputValues.push(i))}for(let r=0;r<n.numResults;r++){let i=n.getResult(r);if(!s.has(i)){for(let o of i.uses())if(!this.opSet.has(o.user)){s.add(i),this._outputValues.push(i);break}}}}}getInputValues(){return this.computeIO(),this._inputValues}getOutputValues(){return this.computeIO(),this._outputValues}get size(){return this.ops.length}get memoryBytes(){return this._memoryBytes}};c(qA,"estimateOpMemory");Ju=class{static{c(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||{}}}},jA=class{static{c(this,"PartitionResult")}constructor(t,s,n){this.partitions=t,this.opToPartition=s,this.transferEdges=n}getPartition(t){return this.opToPartition.get(t)||null}getPartitionsForTarget(t){return this.partitions.filter(s=>s.target===t||s.target.name===t.name)}get numPartitions(){return this.partitions.length}},E_=class{static{c(this,"GraphPartitioner")}constructor(t){this.config=t instanceof Ju?t:new Ju(t),this._supportCache=new Map,this._buildSupportMap()}partition(t){let s=this._collectPartitionableOps(t),n=this._assignTargets(s),r=this._buildPartitions(s,n),i=this._mergeSmallPartitions(r,n),o=this._computeTransferEdges(i),a=new Map;for(let l of i)for(let u of l.ops)a.set(u,l);return new jA(i,a,o)}_buildSupportMap(){for(let t of this.config.targets){let s=new Set;for(let n of rt.names())this._targetSupportsOp(t,n)&&s.add(n);this._supportCache.set(t,s)}}_targetSupportsOp(t,s){if(t.hasLibraryOp(s))return!0;let n=rt.get(s);return n?n.isConstant||n.isTerminator?!0:t.isGPU()?n.isElementwise||n.isReduction||n.isBroadcast||n.isInjective||s==="dot"||s==="conv"||s==="batch_dot"||s==="softmax"||s==="layer_norm"||s==="batch_norm":t.isCPU()?!0:t.isWasm()?!n.isOpaque&&s!=="custom_call":!1:!1}_collectPartitionableOps(t){let s=[];for(let n of t.ops()){let r=rt.get(n.opName);!r||r.isTerminator||s.push(n)}return s}_assignTargets(t){let s=new Map;for(let n of t){let r=n.getAttr("device");if(r){let a=this._resolveDeviceAttr(r);if(a){s.set(n,a);continue}}let i=this.config.opTargetOverrides.get(n.opName);if(i){s.set(n,i);continue}let o=this._selectBestTarget(n);s.set(n,o)}return s}_resolveDeviceAttr(t){return typeof t=="string"?this.config.targets.find(s=>s.name===t||s.kind===t)||null:t}_selectBestTarget(t){let s=this.config.defaultTarget||this.config.targets[0],n=-1/0;for(let r of this.config.targets){let i=this._supportCache.get(r);if(!i||!i.has(t.opName))continue;let o=this._scoreTargetForOp(r,t);o>n&&(n=o,s=r)}return s}_scoreTargetForOp(t,s){let n=rt.get(s.opName);if(!n)return 0;let r=0;if(t.hasLibraryOp(s.opName)&&(r+=100),n.isReduction||n.isElementwise){let i=0;for(let o=0;o<s.numOperands;o++){let a=s.getOperand(o).type;a instanceof T&&a.isFullyStatic&&(i+=a.numel())}t.isGPU()&&i>1024?r+=50:t.isCPU()&&i<=1024&&(r+=30)}return r+=t.computeTFLOPs*10,r}_buildPartitions(t,s){let{partitions:n}=Sp(t,{sort:c(o=>this._topologicalSort(o),"sort"),labelOf:c(o=>s.get(o),"labelOf"),sameLabel:c((o,a)=>o===a||o.name===a.name,"sameLabel"),canMerge:c((o,a,l)=>this._fitsMemoryLimit(o,a,l),"canMerge"),onAttach:c((o,a)=>{o.memoryBytes=(o.memoryBytes||0)+qA(a)},"onAttach")}),r=new Map;for(let o of n){let a=new GA(o.id,o.label);for(let l of o.ops)a.addOp(l);r.has(o.label.name)||r.set(o.label.name,[]),r.get(o.label.name).push(a)}let i=[];for(let o of r.values())for(let a of o)i.push(a);return i}_fitsMemoryLimit(t,s,n){let r=this.config.memoryLimits.get(n.name);if(!r)return!0;let i=qA(s);return t.memoryBytes+i<=r}_mergeSmallPartitions(t,s){if(t.length<=1)return t;let n=new Map;for(let f of t)for(let h of f.ops)n.set(h,f);let r=c(f=>{let h=new Set;for(let p of f.ops)for(let d=0;d<p.numResults;d++)for(let m of p.getResult(d).uses()){let g=n.get(m.user);g&&g!==f&&h.add(g)}return h},"directSuccs"),i=c((f,h)=>{let p=new Set([f]),d=[...r(f)].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"),o=c((f,h)=>i(f,h)||i(h,f),"mergeCreatesCycle"),a=[],l=new Set;for(let f=0;f<t.length;f++){if(l.has(f))continue;let h=t[f];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(f===m||l.has(m))continue;let g=t[m];if(g.target.name!==h.target.name||o(h,g))continue;let y=this._mergeScore(h,g);y>d&&(d=y,p=m)}if(p>=0){t[p].merge(h);for(let m of h.ops)n.set(m,t[p]);l.add(f)}else a.push(h)}let u=new Set;for(let f of a)for(let h of f.ops)u.add(h);for(let f of t){let h=!1;for(let p of f.ops)if(!u.has(p)){h=!0;break}if(h){a.push(f);for(let p of f.ops)u.add(p)}}return a}_mergeScore(t,s){let n=0,r=new Set;for(let i of t.ops)for(let o=0;o<i.numResults;o++)r.add(i.getResult(o));for(let i of s.ops)for(let o=0;o<i.numOperands;o++)r.has(i.getOperand(o))&&n++;return n}_computeTransferEdges(t){let s=[],n=new Set,r=new Map;for(let i of t)for(let o of i.ops)r.set(o,i);for(let i of t)for(let o of i.ops)for(let a=0;a<o.numOperands;a++){let l=o.getOperand(a).definingOp;if(!l)continue;let u=r.get(l);if(!u||u===i)continue;let f=o.getOperand(a),h=`${u.id}|${i.id}|${f.id}`;n.has(h)||(n.add(h),s.push({src:u,dst:i,value:f,sizeBytes:f.type instanceof T&&f.type.isFullyStatic?f.type.sizeInBytes():0}))}return s}_topologicalSort(t){let s=new Set(t),n=new Map,r=new Map;for(let a of t)n.set(a,0),r.set(a,[]);for(let a of t)for(let l=0;l<a.numOperands;l++){let u=a.getOperand(l).definingOp;u&&s.has(u)&&(r.get(u).push(a),n.set(a,n.get(a)+1))}let i=[];for(let a of t)n.get(a)===0&&i.push(a);let o=[];for(;i.length>0;){let a=i.shift();o.push(a);for(let l of r.get(a)){let u=n.get(l)-1;n.set(l,u),u===0&&i.push(l)}}return o}}});var N_,R_,e1=I(()=>{hs();xn();Co();t1();k_();ps();N_=class extends Mt{static{c(this,"GraphPartitionPass")}constructor(t={}){super("GraphPartitionPass"),this.partitionerConfig=new Ju(t),this.partitionResult=null}run(t,s){if(this.partitionerConfig.targets.length<2)return et.UNCHANGED;let n=new E_(this.partitionerConfig);return this.partitionResult=n.partition(t),this.partitionResult.numPartitions<=1?et.UNCHANGED:(this._annotateOps(),this._insertTransferOps(t),this.trace&&this.trace.level>=at.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,numPartitions:this.partitionResult.numPartitions,transferEdges:this.partitionResult.transferEdges.length,level:at.DEBUG}),t.bumpVersion(),et.CHANGED)}_annotateOps(){for(let t of this.partitionResult.partitions)for(let s of t.ops)s.setAttr("partition_id",t.id),s.setAttr("partition_target",t.target.name)}_insertTransferOps(t){let s=t.entryBlock,n=this._buildOrderIndex(s),{useMap:r,firstInPart:i}=this._buildInsertionIndex(s);for(let o of this.partitionResult.transferEdges){let a=o.value,l=o.src.target.name,u=o.dst.target.name;if(l===u)continue;let f=new bt("copy_to_device",[a],[a.type],{src_device:l,dst_device:u});f.setAttr("partition_id",o.dst.id),f.setAttr("partition_target",u);let h=r.get(o.dst),p=h&&h.get(a)||i.get(o.dst)||null,d=a.definingOp;if(p&&d&&n.has(d)&&n.has(p)&&n.get(d)>=n.get(p))s.insertAfter(f,d);else if(p)s.insertBefore(f,p);else if(d&&n.has(d))s.insertAfter(f,d);else{let g=t.getReturnOp();g?s.insertBefore(f,g):s.pushOp(f)}let m=f.getResult(0);for(let g of o.dst.ops)for(let y=0;y<g.numOperands;y++)g.getOperand(y)===a&&g.replaceOperand(y,m)}}_buildInsertionIndex(t){let s=[...new Set(this.partitionResult.transferEdges.map(i=>i.dst))],n=new Map,r=new Map;for(let i of s)n.set(i,new Map);for(let i of t.ops())for(let o of s){if(!o.hasOp(i))continue;r.has(o)||r.set(o,i);let a=n.get(o);for(let l=0;l<i.numOperands;l++){let u=i.getOperand(l);a.has(u)||a.set(u,i)}}return{useMap:n,firstInPart:r}}_buildOrderIndex(t){let s=new Map,n=0;for(let r of t.ops())s.set(r,n++);return s}},R_=class extends Mt{static{c(this,"PartitionMaterializationPass")}constructor(t={}){super("PartitionMaterializationPass"),this.targets=t.targets||[]}run(t,s){let n=this._collectPartitions(t);if(n.size<=1)return et.UNCHANGED;let r=this._getModule(t);if(!r)return et.UNCHANGED;let i=this._materializePartitions(t,n);for(let o of i)r.addFunction(o);return this._rewriteOriginalFunction(t,i,n),this.trace&&this.trace.level>=at.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,partitions:n.size,subFunctions:i.length,level:at.DEBUG}),t.bumpVersion(),et.CHANGED}_collectPartitions(t){let s=new Map;for(let n of t.ops()){let r=n.getAttr("partition_id");r!==void 0&&(s.has(r)||s.set(r,{id:r,target:n.getAttr("partition_target"),ops:[]}),s.get(r).ops.push(n))}return s}_getModule(t){return t._module||null}_materializePartitions(t,s){let n=[];for(let[r,i]of s){let o=new Set(i.ops),{inputs:a,outputs:l}=v_(o,i.ops),u=a.map(_=>_.type),f=l.map(_=>_.type),h=`${t.name}_partition_${r}`,p=new Fs(h,u,f);p._partitionTarget=i.target;let d=new Map;for(let _=0;_<a.length;_++)d.set(a[_],p.args[_]);let m=Ul(i.ops);for(let _ of m){let w=[];for(let S=0;S<_.numOperands;S++){let E=d.get(_.getOperand(S));E?w.push(E):w.push(_.getOperand(S))}let b=[];for(let S=0;S<_.numResults;S++)b.push(_.getResult(S).type);let x=_.regions.length>0?_.regions.map(S=>ui(S)):null,A=new bt(_.opName,w,b,new Map(_.attributes),x);p.entryBlock.pushOp(A);for(let S=0;S<_.numResults;S++)d.set(_.getResult(S),A.getResult(S))}let g=l.map(_=>d.get(_)||_),y=new bt("return",g,[]);p.entryBlock.pushOp(y),n.push(p)}return n}_rewriteOriginalFunction(t,s,n){for(let r of s)t.setAttr?.(`sub_${r.name}`,r._partitionTarget)}}});function Ti(e){return sG.has(e.opName)}function s1(e){let t=0;for(let s=0;s<e.numResults;s++){let n=e.getResult(s).type;if(!n||!n.isFullyStatic)continue;let r=n.sizeInBytes();r>t&&(t=r)}return t}function rG(e){let t=e.definingOp;if(t&&t.opName==="broadcast"){let s=t.getOperand(0);t=s&&s.definingOp}if(t&&Ti(t)){let s=t.getAttr("value");if(typeof s=="number")return s}return 0}function WA(e){if(e.opName!=="dot"&&e.opName!=="cublas_gemm")return null;let t=e.getOperand(0).type,s=e.getOperand(1).type,n=e.getResult(0).type;if(!t||!s||!n||t.dtype!=="f32"||s.dtype!=="f32"||n.dtype!=="f32")return null;let r=e.getAttr("lhs_batch")||[],i=e.getAttr("rhs_batch")||[];if(r.length>0||i.length>0)return null;let o=e.getAttr("lhs_contracting")||[],a=e.getAttr("rhs_contracting")||[];if(o.length!==1||a.length!==1||s.rank!==2||o[0]!==t.rank-1||a[0]!==0&&a[0]!==1||!t.isFullyStatic||!s.isFullyStatic||!n.isFullyStatic)return null;let l=e.getOperand(0).definingOp,u=e.getOperand(1).definingOp;if(l&&Ti(l)||u&&Ti(u))return null;let f=a[0]===1,h=t.shape[t.rank-1];if((f?s.shape[1]:s.shape[0])!==h)return null;let d=1;for(let g=0;g<t.rank-1;g++)d*=t.shape[g];let m=f?s.shape[0]:s.shape[1];return d<=0||m<=0||h<=0?null:{M:d,N:m,K:h,transB:f}}function n1(e){return{labelOf:c(t=>e.get(t),"labelOf"),canMerge:c((t,s)=>Math.max(t.maxBuf||0,s1(s))<=nG,"canMerge"),onAttach:c((t,s)=>{t.maxBuf=Math.max(t.maxBuf||0,s1(s))},"onAttach")}}function C_(e,t,s){let n=e.opSet,r=Ul(e.ops),{inputs:i,outputs:o,constDefs:a}=v_(n,r,{pullConstants:!0,isConstant:Ti});for(let p of i)if(!p.type||!p.type.isFullyStatic)return null;for(let p of o)if(!p.type||!p.type.isFullyStatic)return null;let l=new Fs(t,i.map(p=>p.type),o.map(p=>p.type)),u=new Map;for(let p=0;p<i.length;p++)u.set(i[p],l.args[p]);for(let p of a)l.entryBlock.pushOp(p.clone(u));for(let p of r)l.entryBlock.pushOp(p.clone(u));let f=o.map(p=>u.get(p));if(f.some(p=>p===void 0))return null;l.entryBlock.pushOp(new bt("return",f,[]));let h=e.ops.length===1&&s.has(e.ops[0])?e.ops[0]:null;return{part:e,subFunc:l,inputs:i,outputs:o,dotOp:h}}function B_(e){if(iG.has(e.opName))return!0;if(e.regions)for(let t of e.regions){let s=t.entryBlock;if(s){for(let n of s.ops())if(B_(n))return!0}}return!1}function oG(e){let t=0;for(let s=0;s<e.numResults;s++){let n=e.getResult(s).type;if(!n||!n.isFullyStatic)continue;let r=1;for(let i of n.shape)r*=i;r>t&&(t=r)}return t}function r1(e,t=256){if(e.functionCount!==1)return!1;let s=e.functions().next().value,n=new Map,r=c(i=>{if(!i)return 0;let o=n.get(i);if(o!==void 0)return o;n.set(i,0);let a=B_(i)?oG(i):0;for(let l=0;l<i.numOperands;l++){let u=r(i.getOperand(l).definingOp);u>a&&(a=u)}return n.set(i,a),a},"maxBoundaryInSubtree");for(let i of s.ops())if(!(Ap.has(i.opName)||!B_(i))){for(let o=0;o<i.numOperands;o++)if(r(i.getOperand(o).definingOp)>t)return!0}return!1}function i1(e,t,s){let n=new Map,r=0,i=c(d=>{let m=n.get(d);return m===void 0&&(m=r++,n.set(d,m)),m},"getSlot");for(let d of e.args)i(d);for(let d of s)for(let m of d.outputs)i(m);let o=[];for(let d of e.args)o.push(i(d));let a=[],l=new Set;for(let d=0;d<t.numOperands;d++){let m=t.getOperand(d),g=o.length,y=m.isBlockArgument&&m.isBlockArgument(),_=m.definingOp&&Ti(m.definingOp);if(!y&&!_&&n.has(m)){let w=n.get(m);if(!l.has(w)){l.add(w),o.push(w);continue}o.push(r++),a.push({pos:g,kind:"copy",srcSlot:w});continue}if(y){o.push(r++),a.push({pos:g,kind:"copy",srcSlot:i(m)});continue}if(_){o.push(r++),a.push({pos:g,kind:"const",value:rG(m)});continue}return null}let u=[];for(let d of s){let m=[];for(let y of d.inputs){let _=n.get(y);if(_===void 0)return null;m.push(_)}let g=d.outputs.map(y=>n.get(y));u.push({name:d.subFunc.name,inputSlots:m,outputSlots:g})}let f=new Set(o),h=[],p=new Set;for(let[d,m]of n)if(!(f.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:o,intermediates:h,steps:u,returnFixups:a}}}function Yu(e,t=2){if(e.functionCount!==1)return null;let s=e.functions().next().value,n=s.getReturnOp();if(!n)return null;let r=[],i=new Map,o=0;for(let g of s.ops())Ap.has(g.opName)||Ti(g)||(B_(g)?i.set(g,"boundary#"+o++):i.set(g,"native"),r.push(g));if(o<t||r.length===0)return null;let{partitions:a,preds:l}=Sp(r,n1(i));if(a.length<2)return null;let u=VA(a,l);if(!u)return null;let f=s.name,h=[],p=new Map,d=0;for(let g of u){let y=C_(g,f+"_p"+d++,p);if(!y)return null;h.push(y)}let m=i1(s,n,h);if(!m)return null;e.removeFunction(s.name);for(let g of h)e.addFunction(g.subFunc);return{plan:m.plan}}function o1(e){if(e.functionCount!==1)return null;let t=e.functions().next().value,s=t.getReturnOp();if(!s)return null;let n=[],r=new Map,i=new Map,o=0;for(let g of t.ops()){if(Ap.has(g.opName)||Ti(g))continue;let y=WA(g);y?(r.set(g,"cublas#"+o),i.set(g,y),o++):r.set(g,"native"),n.push(g)}if(o===0||n.length===0)return null;let{partitions:a,preds:l}=Sp(n,n1(r));if(a.length<2)return null;let u=VA(a,l);if(!u)return null;let f=t.name,h=[],p=0;for(let g of u){let y=C_(g,f+"_p"+p++,i);if(!y)return null;h.push(y)}let d=i1(t,s,h);if(!d)return null;let m=new Map;for(let g of h){if(!g.dotOp)continue;let y=i.get(g.dotOp),_=g.inputs.indexOf(g.dotOp.getOperand(0)),w=g.inputs.indexOf(g.dotOp.getOperand(1)),b=g.outputs.indexOf(g.dotOp.getResult(0));_<0||w<0||b<0||m.set(g.subFunc.name,{M:y.M,N:y.N,K:y.K,transB:y.transB,aIdx:_,bIdx:w,cIdx:g.inputs.length+b})}if(m.size===0)return null;e.removeFunction(t.name);for(let g of h)e.addFunction(g.subFunc);return{plan:d.plan,cublasInfos:m}}var sG,Ap,nG,iG,T_=I(()=>{Co();xn();k_();sG=new Set(["constant","scalar_constant"]),Ap=new Set(["return","yield"]);c(Ti,"isConstantOp");nG=32*1024;c(s1,"maxResultBytes");c(rG,"constScalarOf");c(WA,"cublasDotInfo");c(n1,"bufferLimitedConfig");c(C_,"materializePartition");iG=new Set(["dot","cublas_gemm","reduce"]);c(B_,"containsBoundaryOp");c(oG,"maxBoundaryResultBytes");c(r1,"hasDependentBoundaries");c(i1,"buildExecutionPlan");c(Yu,"splitGraphForNative");c(o1,"splitGraphForCublas")});function l1(e){return a1.push(e),e}var a1,I_,c1=I(()=>{hs();a1=[];c(l1,"registerBackendOpRewrite");I_=class extends Mt{static{c(this,"BackendOpRewritePass")}constructor(t={}){super(t.name||"BackendOpRewritePass"),this.config=t,this.rewrites=t.rewrites||a1}run(t){let s=!1;for(let n of[...t.ops()]){let r=n.parentBlock;if(r){for(let i of this.rewrites)if(i.match(n,this.config)){i.build(n,r,this.config),s=!0;break}}}return s?et.CHANGED:et.UNCHANGED}}});var aG,O_,u1=I(()=>{xn();T_();c1();aG=l1({name:"dot->cublas_gemm",match:c(e=>e.opName==="dot"&&e.numOperands===2&&WA(e)!==null,"match"),build:c((e,t)=>{let s=e.results.map(r=>r.type),n=new bt("cublas_gemm",[e.getOperand(0),e.getOperand(1)],s,new Map(e.attributes),null);t.insertBefore(n,e),e.replaceAllResultsWith(n.results),e.erase()},"build")}),O_=class extends I_{static{c(this,"CublasRewritePass")}constructor(t={}){super({...t,name:"CublasRewritePass",rewrites:[aG]})}}});function vp(e){let t=1;for(let s of e){if(typeof s!="number"||s<0)return-1;t*=s}return t}function kp(e){return e==="f16"||e==="i16"?2:e==="i8"||e==="u8"?1:4}function lG(e,t,s){let n=s.sharedMemoryBytes||16384,r=0;for(let l of t.entryBlock.ops())if(l.opName!=="yield")for(let u=0;u<l.numResults;u++){let f=l.getResult(u).type;if(!f||!f.shape)continue;let h=vp(f.shape);if(h<0)return!0;let p=h*kp(f.dtype);p>r&&(r=p)}let i=e.getAttr("num_carry"),o=e.getAttr("num_xs"),a=0;for(let l=0;l<i;l++){let u=e.getOperand(o+l).type,f=u&&u.shape?vp(u.shape):-1;f>0&&(a+=f*kp(u.dtype))}return 3*r+2*a>n}function cG(e,t){let n=e.regions[0].entryBlock,r=e.getAttr("num_carry"),i=[],o=null;for(let w of n.ops()){if(w.opName==="yield"){o=w;continue}i.push(w)}if(!o)return null;let a=new Set(i),l=new Set(n.arguments),u=[],f=new Set,h=[],p=new Set;for(let w of i)for(let b=0;b<w.numOperands;b++){let x=w.getOperand(b),A=x.definingOp;if(!l.has(x)&&!(A&&a.has(A))){if(A&&Ti(A)){p.has(A)||(p.add(A),h.push(A));continue}f.has(x)||(f.add(x),u.push(x))}}let d=[...n.arguments,...u];for(let w of d)if(!w.type||!w.type.isFullyStatic)return null;let m=[];for(let w=0;w<o.numOperands;w++)m.push(o.getOperand(w));let g=new Fs(t,d.map(w=>w.type),m.map(w=>w.type)),y=new Map;for(let w=0;w<d.length;w++)y.set(d[w],g.args[w]);for(let w of h)g.entryBlock.pushOp(w.clone(y));for(let w of i)g.entryBlock.pushOp(w.clone(y));let _=m.map(w=>y.get(w));return _.some(w=>w===void 0)?null:(g.entryBlock.pushOp(new bt("return",_,[])),{bodyFunc:g,captured:u,numCarry:r,numYs:m.length-r})}function uG(e,t,s,n,r){let{getSlot:i,newSlot:o,steps:a,addedFuncs:l}=n,u=new cn(e.name+"_mod");u.addFunction(e);let f=Yu(u,r);if(f){let h=f.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=c(x=>{let A=p+x;return d.has(A)?d.get(A):h.argSlots[A]},"retSlot"),y=new Map;for(let x=0;x<p;x++)y.set(h.argSlots[x],i(t[x]));for(let x of h.intermediates)y.set(x.slot,o(x.shape,x.dtype));for(let x=0;x<s.length;x++){let A=g(x);y.has(A)||y.set(A,i(s[x]))}let _=c(x=>{let A=y.get(x);return A===void 0?null:A},"mapSlot"),w=[],b=!0;for(let x of h.steps){let A=x.inputSlots.map(_),S=x.outputSlots.map(_);if(A.includes(null)||S.includes(null)){b=!1;break}w.push({name:x.name,inputSlots:A,outputSlots:S})}if(b){for(let x of w)a.push(x);for(let x of u.functions())l.push(x);return!0}}}return a.push({name:e.name,inputSlots:t.map(i),outputSlots:s.map(i)}),l.push(e),!0}function f1(e,t,s){let n=C_({ops:e,opSet:new Set(e)},t,new Map);return n?uG(n.subFunc,n.inputs,n.outputs,s,1):!1}function fG(e,t,s){let{getSlot:n,newSlot:r,steps:i,scanLoops:o,addedFuncs:a}=s,l=e.regions[0];if(!l||!l.entryBlock)return!1;let u=cG(e,t);if(!u)return!1;let{bodyFunc:f,captured:h,numCarry:p,numYs:d}=u,m=e.getAttr("num_xs"),g=new cn(t+"_mod");g.addFunction(f);let y=Yu(g,2),_=y?y.plan:null,w=[],b=[];for(let D=0;D<p;D++){let G=e.getOperand(m+D).type;w.push(G.shape),b.push(G.dtype)}let x=[],A=[];for(let D=0;D<m;D++){let G=e.getOperand(D).type;x.push(G.shape.slice(1)),A.push(G.dtype)}let S=[],E=[];for(let D=0;D<d;D++){let G=e.getResult(p+D).type;S.push(G.shape.slice(1)),E.push(G.dtype)}let R=w.map((D,G)=>r(D,b[G])),v=w.map((D,G)=>r(D,b[G])),k=x.map((D,G)=>r(D,A[G])),B=[],N=[],C=[],O=[];for(let D=0;D<p;D++)N.push(n(e.getOperand(m+D)));for(let D=0;D<p;D++)C.push(n(e.getResult(D)));for(let D of h)O.push(n(D));let F=[],P=[];for(let D=0;D<m;D++)F.push(n(e.getOperand(D)));for(let D=0;D<d;D++)P.push(n(e.getResult(p+D)));let z=i.length;if(!_)B=S.map((D,G)=>r(D,E[G])),i.push({name:f.name,inputSlots:[...k,...R,...O],outputSlots:[...v,...B]}),a.push(f);else{let D=m+p+h.length,G=new Map;for(let $ of _.returnFixups||[]){if($.kind!=="copy")return!1;G.set($.pos,$.srcSlot)}let At=c($=>{let K=D+$;return G.has(K)?G.get(K):_.argSlots[K]},"bodyReturnSlot"),pt=new Map;for(let $=0;$<m;$++)pt.set(_.argSlots[$],k[$]);for(let $=0;$<p;$++)pt.set(_.argSlots[m+$],R[$]);for(let $=0;$<h.length;$++)pt.set(_.argSlots[m+p+$],O[$]);for(let $ of _.intermediates)pt.set($.slot,r($.shape,$.dtype));for(let $=0;$<p;$++){let K=At($);pt.has(K)||pt.set(K,v[$])}for(let $=0;$<d;$++){let K=At(p+$),_t=pt.get(K);_t===void 0&&(_t=r(S[$],E[$]),pt.set(K,_t)),B.push(_t)}let mt=c($=>{let K=pt.get($);return K===void 0?null:K},"mapSlot");for(let $ of _.steps){let K=$.inputSlots.map(mt),_t=$.outputSlots.map(mt);if(K.includes(null)||_t.includes(null))return!1;i.push({name:$.name,inputSlots:K,outputSlots:_t})}for(let $ of g.functions())a.push($)}let q=i.length,U=e.getOperand(0).type.shape[0];return typeof U!="number"||U<0?!1:(o.push({T:U,loopStart:z,loopEnd:q,carry:w.map((D,G)=>({a:R[G],b:v[G],initSlot:N[G],finalSlot:C[G],bytes:vp(D)*kp(b[G])})),xs:k.map((D,G)=>({xtSlot:D,xsSlot:F[G],stepBytes:vp(x[G])*kp(A[G])})),ys:B.map((D,G)=>({ytSlot:D,ysSlot:P[G],stepBytes:vp(S[G])*kp(E[G])}))}),!0)}function KA(e,t,s=!1){if(!t||typeof t.isWebGPU!="function"||!t.isWebGPU()||e.functionCount!==1)return null;let n=e.functions().next().value,r=n.getReturnOp();if(!r)return null;let i=[];for(let N of n.ops())N.opName==="scan"&&i.push(N);if(i.length===0)return null;let o=new Set,a=c(N=>{if(!(!N||o.has(N))){o.add(N);for(let C=0;C<N.numOperands;C++)a(N.getOperand(C).definingOp)}},"visit");for(let N=0;N<r.numOperands;N++)a(r.getOperand(N).definingOp);for(let N of i)if(!o.has(N))return null;if(!s){let N=!1;for(let C of i){let O=C.regions[0];if(O&&O.entryBlock&&lG(C,O,t)){N=!0;break}}if(!N)return null}let l=[];for(let N of o)Ti(N)||Ap.has(N.opName)||l.push(N);let u=Ul(l),f=new Map,h=0,p=c(N=>{let C=f.get(N);return C===void 0&&(C=h++,f.set(N,C)),C},"getSlot");for(let N of n.args)p(N);let d=[],m=c((N,C)=>{let O=h++;return d.push({slot:O,shape:[...N],dtype:C}),O},"newSlot"),g=[],y=[],_=[],w={getSlot:p,newSlot:m,steps:g,scanLoops:y,addedFuncs:_},b=new Set(i),x=[],A=0,S=0;for(let N of u)if(b.has(N)){if(x.length&&!f1(x,n.name+"_seg"+A++,w)||(x=[],!fG(N,n.name+"_scan"+S++,w)))return null}else x.push(N);if(x.length&&!f1(x,n.name+"_seg"+A++,w))return null;let E=n.args.map(p),R=[],v=new Set;for(let N=0;N<r.numOperands;N++){let C=r.getOperand(N),O=E.length;if(f.has(C)&&!(C.isBlockArgument&&C.isBlockArgument())){let F=p(C);v.has(F)?(E.push(h++),R.push({pos:O,kind:"copy",srcSlot:F})):(v.add(F),E.push(F))}else if(C.isBlockArgument&&C.isBlockArgument())E.push(h++),R.push({pos:O,kind:"copy",srcSlot:p(C)});else return null}let k=new Set(E),B=new Set;for(let[N,C]of f)if(!(k.has(C)||B.has(C))){if(B.add(C),!N.type||!N.type.isFullyStatic)return null;d.some(O=>O.slot===C)||d.push({slot:C,shape:[...N.type.shape],dtype:N.type.dtype})}e.removeFunction(n.name);for(let N of _)e.addFunction(N);return{plan:{numSlots:h,argSlots:E,intermediates:d,steps:g,returnFixups:R,scanLoops:y}}}var h1=I(()=>{xn();Co();Ya();T_();c(vp,"numel");c(kp,"dtypeBytes");c(lG,"isScanOversized");c(cG,"buildScanBodyFunction");c(uG,"inlineNativeSplit");c(f1,"emitSegment");c(fG,"emitScanLoop");c(KA,"splitGraphForScan")});function JA(e){HA.push(e),HA.sort((t,s)=>(t.priority??100)-(s.priority??100))}function hG(e){for(let t of HA)if(t.applies(e))return t;return null}function p1(e,t){let s=hG(t);return s?s.run(e,t):null}var HA,d1=I(()=>{T_();h1();HA=[];c(JA,"registerGraphSplitStrategy");c(hG,"selectGraphSplitStrategy");c(p1,"splitGraph");JA({name:"cublas",priority:10,applies:c(e=>e.config.matmulBackend==="cublas","applies"),run:c(e=>o1(e),"run")});JA({name:"cuda-native-matmul-chain",priority:20,applies:c(e=>e.cudaMatmulChain,"applies"),run:c(e=>Yu(e),"run")});JA({name:"webgpu",priority:30,applies:c(e=>e.isWebGPU,"applies"),run:c((e,t)=>{let s=KA(e,t.target);return!s&&r1(e,t.target.maxThreadsPerBlock||256)&&(s=KA(e,t.target,!0),s||(s=Yu(e,2))),s},"run")})});function YA(e,t,s){return pG.filter(n=>n.phase===e).sort((n,r)=>n.priority-r.priority).map(n=>n.factory(t,s)).filter(Boolean)}var pG,m1=I(()=>{pG=[];c(YA,"graphPassesForPhase")});function dG(e){let t=[...e.entries()];return t.sort((s,n)=>s[0]<n[0]?-1:s[0]>n[0]?1:0),t}function M_(e){return e==null?"null":typeof e=="number"?Number.isFinite(e)?String(e):e===1/0?"inf":e===-1/0?"-inf":"nan":typeof e=="boolean"?String(e):typeof e=="string"?`"${e}"`:e instanceof T?Ws(e):Array.isArray(e)?e.length===0?"[]":Array.isArray(e[0])?"["+e.map(t=>M_(t)).join(", ")+"]":"["+e.map(t=>M_(t)).join(", ")+"]":typeof e=="object"&&e.constructor===Object?"{"+Object.entries(e).sort((s,n)=>s[0].localeCompare(n[0])).map(([s,n])=>`${s}: ${M_(n)}`).join(", ")+"}":String(e)}function g1(e){return new Ep().printModule(e)}var Ep,QA=I(()=>{It();Ep=class{static{c(this,"IRPrinter")}constructor(t={}){this.indent=0,this.indentStr=t.indentStr||" ",this.valueNames=new Map,this._nextValueId=0}printModule(t){let s=[];s.push(`module @${t.name} {`),this.indent++;let n=!0;for(let r of t)n||s.push(""),n=!1,this.printFunction(r,s);return this.indent--,s.push("}"),s.join(`
198
- `)}printFunction(t,s=null){let n=s===null;n&&(s=[]),this.valueNames.clear(),this._nextValueId=0;let r=t.args.map((o,a)=>`${this._nameValue(o)}: ${Ws(o.type)}`),i=t.outputTypes.map(o=>Ws(o)).join(", ");s.push(`${this._indentPrefix()}func @${t.name}(${r.join(", ")}) -> (${i}) {`),this.indent++;for(let o of t.body)this.printBlock(o,s,o===t.entryBlock);return this.indent--,s.push(`${this._indentPrefix()}}`),n?s.join(`
199
- `):void 0}printBlock(t,s,n=!1){if(!n&&t.arguments.length>0){let r=t.arguments.map(i=>`${this._nameValue(i)}: ${Ws(i.type)}`);s.push(`${this._indentPrefix()}^bb(${r.join(", ")}):`)}for(let r of t)this.printOperation(r,s)}printOperation(t,s=null){let n=s===null;n&&(s=[]);let r=this._indentPrefix(),i=r;if(t.numResults>0){let o=[];for(let a=0;a<t.numResults;a++)o.push(this._nameValue(t.getResult(a)));i+=o.join(", ")+" = "}if(i+=t.opName,t.numOperands>0){let o=[];for(let a=0;a<t.numOperands;a++)o.push(this._valueName(t.getOperand(a)));i+="("+o.join(", ")+")"}else i+="()";if(t.attributes.size>0){let o=[];for(let[a,l]of dG(t.attributes))o.push(`${a} = ${M_(l)}`);i+=" {"+o.join(", ")+"}"}if(t.numResults>0){let o=[];for(let a=0;a<t.numResults;a++)o.push(Ws(t.getResult(a).type));i+=" : "+o.join(", ")}s.push(i);for(let o=0;o<t.regions.length;o++){let a=t.regions[o];s.push(`${r}{`),this.indent++;for(let l of a){if(l.arguments.length>0){let u=l.arguments.map(f=>`${this._nameValue(f)}: ${Ws(f.type)}`);s.push(`${this._indentPrefix()}^bb(${u.join(", ")}):`)}for(let u of l)this.printOperation(u,s)}this.indent--,s.push(`${r}}`)}return n?s.join(`
200
- `):void 0}_nameValue(t){if(this.valueNames.has(t))return this.valueNames.get(t);let s=`%${this._nextValueId++}`;return this.valueNames.set(t,s),s}_valueName(t){return this.valueNames.has(t)?this.valueNames.get(t):"%?"}_indentPrefix(){let t="";for(let s=0;s<this.indent;s++)t+=this.indentStr;return t}};c(dG,"sortedEntries");c(M_,"formatAttrValue");c(g1,"printModule")});function ZA(e){return new XA().print(e)}var XA,y1=I(()=>{XA=class{static{c(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(`
201
- `+" ".repeat(this.indent))}visit(t){if(!t)return;let s="visit"+t.type;this[s]?this[s](t):this.push(`[UnknownNode: ${t.type}]`)}visitPrimFunc(t){this.push(`prim_func ${t.name}(${t.params.map(s=>s.name).join(", ")}) {`),this.indent++,this.newline();for(let[s,n]of t.bufferMap)this.push(`${n.name} = buffer_map(${s.name}, shape=[${n.shape.join(",")}], dtype=${n.dtype})`),this.newline();this.visit(t.body),this.indent--,this.newline(),this.push("}")}visitSeqNode(t){for(let s=0;s<t.stmts.length;s++)this.visit(t.stmts[s]),s<t.stmts.length-1&&this.newline()}visitForNode(t){let s=t.kind==="serial"?"":`@${t.kind} `,n=t.threadTag?`[${t.threadTag}] `:"";this.push(`for ${t.loopVar.name} in 0..`),this.visit(t.extent),this.push(` ${s}${n}{`),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 s of t.iterVars)this.push(`bind ${s.iterVar.name} = `),this.visit(s.binding),this.newline();t.reads.length>0&&(this.push("reads(["),this.push(t.reads.map(s=>`${s.buffer.name}[...]`).join(", ")),this.push("])"),this.newline()),t.writes.length>0&&(this.push("writes(["),this.push(t.writes.map(s=>`${s.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 s=0;s<t.indices.length;s++)this.visit(t.indices[s]),s<t.indices.length-1&&this.push(", ");this.push("] = "),this.visit(t.value)}visitBufferLoadNode(t){this.push(`${t.buffer.name}[`);for(let s=0;s<t.indices.length;s++)this.visit(t.indices[s]),s<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 s=0;s<t.args.length;s++)this.visit(t.args[s]),s<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(")")}};c(ZA,"printTensorIR")});function _1(e,t){let s=new _m;for(let[,n]of e.bufferMap)s.paramBuffers.add(n.name);return mG(e.body,s,t),yG(e,s,t),_G(e.body,s),s}function mG(e,t,s){let n=[e];for(;n.length>0;){let r=n.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 i=r.extent&&r.extent.type==="IntImmNode"?r.extent.value:0,o=!r.extent||r.extent.type!=="IntImmNode",a={varName:r.loopVar.name,extent:i,isDynamic:o,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,Lo(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&&!gR(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 i of r.reads)i.buffer&&t.usedBuffers.set(i.buffer.name,i.buffer);if(r.writes)for(let i of r.writes)i.buffer&&t.usedBuffers.set(i.buffer.name,i.buffer);if(r.iterVars)for(let i of r.iterVars)i.iterVar&&t.locals.set(i.iterVar.name,Lo(i.iterVar.dtype)),i.binding&&typeof i.binding=="object"&&i.binding.type&&n.push(i.binding);break}if(r.body&&n.push(r.body),r.value&&typeof r.value=="object"&&r.value.type&&n.push(r.value),r.stmts)for(let i of r.stmts)n.push(i);if(r.thenBody&&n.push(r.thenBody),r.elseBody&&n.push(r.elseBody),r.initBody&&n.push(r.initBody),r.condBody&&n.push(r.condBody),r.loopBody&&n.push(r.loopBody),r.condition&&typeof r.condition=="object"&&r.condition.type&&n.push(r.condition),r.a&&typeof r.a=="object"&&r.a.type&&n.push(r.a),r.b&&typeof r.b=="object"&&r.b.type&&n.push(r.b),r.expr&&typeof r.expr=="object"&&r.expr.type&&n.push(r.expr),r.args)for(let i of r.args)typeof i=="object"&&i!==null&&i.type&&n.push(i);if(r.indices)for(let i of r.indices)typeof i=="object"&&i!==null&&i.type&&n.push(i)}}}function yG(e,t,s){let n=t.memoryLayout.alignment,r=0,i=c(o=>{let a=o.shape.some(f=>typeof f!="number"||f<0),l=o.numel();if(!a&&l>=0)return l*Ro(o.dtype);let u=1;for(let f of o.shape)u*=typeof f=="number"&&f>0?f:1;return Math.max(gG,u*Ro(o.dtype))},"bufBytes");for(let[,o]of e.bufferMap)r=Math.ceil(r/n)*n,t.memoryLayout.bufferOffsets.set(o.name,r),r+=i(o);for(let[o,a]of t.usedBuffers)t.memoryLayout.bufferOffsets.has(o)||(r=Math.ceil(r/n)*n,t.memoryLayout.bufferOffsets.set(o,r),r+=i(a));t.memoryLayout.totalBytes=r}function _G(e,t){let s=new Map,n=[e];for(;n.length>0;){let r=n.pop();if(!(!r||typeof r!="object")){if(r.type==="BufferStoreNode"&&r.buffer){let i=r.buffer.name;t.paramBuffers.has(i)||(s.has(i)||s.set(i,[]),s.get(i).push(r.value))}if(r.body&&n.push(r.body),r.value&&typeof r.value=="object"&&r.value.type&&n.push(r.value),r.stmts)for(let i of r.stmts)n.push(i);r.thenBody&&n.push(r.thenBody),r.elseBody&&n.push(r.elseBody),r.initBody&&n.push(r.initBody),r.condBody&&n.push(r.condBody),r.loopBody&&n.push(r.loopBody)}}for(let[r,i]of s)if(i.every(a=>a&&a.type==="IntImmNode"&&a.value===0||a&&a.type==="FloatImmNode"&&a.value===0)&&t.zeroBuffers.add(r),i.length===1&&i[0]){let a=i[0];(a.type==="IntImmNode"||a.type==="FloatImmNode")&&t.constantBuffers.set(r,a.value)}}var gG,w1=I(()=>{wm();bn();Jt();c(_1,"scanMetadata");c(mG,"walkTree");gG=65536;c(yG,"computeMemoryLayout");c(_G,"detectZeroBuffers")});function Np(e,t,s){let n=typeof e.offset=="number"?e.offset:0;if(t.length===0)return new M(n);if(t.length===1)return n===0?t[0]:new L("+",t[0],new M(n));let r=[];n!==0&&r.push(new M(n));for(let i=0;i<t.length;i++){let o=t[i];if(o.type==="IntImmNode"&&o.value===0)continue;let a=e.strides[i];if(a===1)r.push(o);else if(typeof a=="number"&&a>=0)r.push(new L("*",o,new M(a)));else{let l=wG(e,i,s);r.push(new L("*",o,l))}}return r.length===0?new M(0):r.reduce((i,o)=>new L("+",i,o))}function wG(e,t,s){let n=[];for(let r=t+1;r<e.shape.length;r++){let i=e.shape[r];typeof i=="number"&&i>=0?n.push(new M(i)):n.push(bG(e,r,s))}return n.length===0?new M(1):n.reduce((r,i)=>new L("*",r,i))}function bG(e,t,s){if(s){let n=`${e.name}:${t}`,r=s.get(n);if(r)return new Ae(r.name,"index")}return new M(1)}var b1=I(()=>{Jt();c(Np,"flattenIndex");c(wG,"computeDynamicStride");c(bG,"resolveShapeParam")});function x1(e,t){let s=_1(e,t),n={shapeParamMap:e.shapeParamMap,accCounter:0,metadata:s},r=Tr(e.body,n);return new gm(e.name,e.params,r,e.bufferMap,e.shapeParams,e.shapeParamMap,s)}function Tr(e,t){if(!e||typeof e!="object")return e;switch(e.type){case"ForNode":return xG(e,t);case"BlockNode":return SG(e,t);case"SeqNode":return AG(e,t);case"BufferStoreNode":return vG(e,t);case"LetStmtNode":return kG(e,t);case"AllocateNode":return EG(e,t);case"IfThenElseNode":return NG(e,t);case"WhileNode":return RG(e,t);case"EvaluateNode":return new nm(Rn(e.value,t));case"SyncThreadsNode":return e;default:return e}}function xG(e,t){let s=CG(e);if(s)return TG(e,s,t);let n=Tr(e.body,t);return new wt(e.loopVar,e.min,e.extent,e.kind,n,e.threadTag)}function SG(e,t){let s=[];for(let i of e.iterVars)i.iterVar&&i.binding&&s.push({name:i.iterVar.name,dtype:i.iterVar.dtype,expr:Rn(i.binding,t)});let n=Tr(e.body,t),r=e.initBody?Tr(e.initBody,t):null;return s.length>0?r?(n=new yt([new Bc(s,r),new Bc(s,n)]),r=null):n=new Bc(s,n):r&&(n=new yt([r,n])),n}function AG(e,t){let s=[];for(let n of e.stmts)s.push(Tr(n,t));return new yt(s)}function vG(e,t){let s=Np(e.buffer,e.indices,t.shapeParamMap),n=Rn(e.value,t),r=e.buffer.dtype||An(e.value);return new oh(e.buffer,s,n,r)}function kG(e,t){return new Sn(e.variable,Rn(e.value,t),Tr(e.body,t))}function EG(e,t){return new Hs(e.buffer,e.scope,Tr(e.body,t))}function NG(e,t){return new Ht(Rn(e.condition,t),Tr(e.thenBody,t),e.elseBody?Tr(e.elseBody,t):null)}function RG(e,t){return new xc(e.condVar,Tr(e.condBody,t),Tr(e.loopBody,t))}function Rn(e,t){if(!e||typeof e!="object"||!e.type)return e;switch(e.type){case"BufferLoadNode":{let s=Np(e.buffer,e.indices,t.shapeParamMap),n=new ih(e.buffer,s,e.buffer.dtype);return Ji(n),n}case"MathOpNode":{let s=Rn(e.a,t),n=e.b?Rn(e.b,t):null,r={...e,a:s,b:n};return Object.setPrototypeOf(r,Object.getPrototypeOf(e)),Ji(r),r}case"CompareNode":{let s=Rn(e.a,t),n=Rn(e.b,t),r={...e,a:s,b:n};return Object.setPrototypeOf(r,Object.getPrototypeOf(e)),Ji(r),r}case"CastNode":{let s=Rn(e.expr,t),n={...e,expr:s};return Object.setPrototypeOf(n,Object.getPrototypeOf(e)),Ji(n),n}case"CallExternNode":{let s=e.args.map(r=>Rn(r,t)),n={...e,args:s};return Object.setPrototypeOf(n,Object.getPrototypeOf(e)),Ji(n),n}case"IfThenElseNode":{let s=Rn(e.condition,t),n=Rn(e.thenBody,t),r=e.elseBody?Rn(e.elseBody,t):null,i={...e,condition:s,thenBody:n,elseBody:r};return Object.setPrototypeOf(i,Object.getPrototypeOf(e)),Ji(i),i}default:return Ji(e),e}}function CG(e){let t=e.body;if(!t||t.type!=="BlockNode")return null;let s=t.body;if(!s||s.type!=="BufferStoreNode")return null;let n=s,r=n.value;if(!r||r.type!=="MathOpNode"||!BG.has(r.op))return null;let i=null,o=null;if(r.a&&r.a.type==="BufferLoadNode"&&r.a.buffer.name===n.buffer.name?(i=r.a,o=r.b):r.b&&r.b.type==="BufferLoadNode"&&r.b.buffer.name===n.buffer.name&&(i=r.b,o=r.a),!i)return null;let a=tv(n.indices),l=tv(i.indices);if(a!==l||a.includes("?"))return null;let u=n.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}),f=e.loopVar.name,h=tv(u);return h.includes("?")||h.includes("$"+f)?null:{store:n,loadSide:i,valueSide:o,outerIndices:u,block:t,op:n.value.op}}function TG(e,t,s){let n=`_acc_${s.accCounter++}`,r=An(t.loadSide);s.metadata.locals.set(n,r);let i=new Map;for(let d of t.block.iterVars)d.iterVar&&d.binding&&i.set(d.iterVar.name,d.binding);let o=i.size>0?jn(t.valueSide,i):t.valueSide,a=Np(t.store.buffer,t.outerIndices,s.shapeParamMap),l=new ih(t.store.buffer,a,t.store.buffer.dtype);Ji(l);let u=Rn(o,s),f=Np(t.store.buffer,t.outerIndices,s.shapeParamMap),h=new oh(t.store.buffer,f,null,t.store.buffer.dtype),p=t.block.initBody?i.size>0?Rp(t.block.initBody,i):t.block.initBody:null;return new ym({localName:n,dtype:r,op:t.op,initLoad:l,loopVar:e.loopVar,extent:e.extent,loopKind:e.kind,body:u,flushStore:h,initBody:p?Tr(p,s):null})}function jn(e,t){if(!e||typeof e!="object"||!e.type)return e;if(e.type==="VariableNode"&&t.has(e.name))return t.get(e.name);if(e.type==="BufferLoadNode"){let s=e.indices.map(i=>jn(i,t));if(!s.some((i,o)=>i!==e.indices[o]))return e;let r={...e,indices:s};return Object.setPrototypeOf(r,Object.getPrototypeOf(e)),r}if(e.type==="MathOpNode"){let s=jn(e.a,t),n=e.b?jn(e.b,t):null;if(s===e.a&&n===e.b)return e;let r={...e,a:s,b:n};return Object.setPrototypeOf(r,Object.getPrototypeOf(e)),r}if(e.type==="CompareNode"){let s=jn(e.a,t),n=jn(e.b,t);if(s===e.a&&n===e.b)return e;let r={...e,a:s,b:n};return Object.setPrototypeOf(r,Object.getPrototypeOf(e)),r}if(e.type==="CastNode"){let s=jn(e.expr,t);if(s===e.expr)return e;let n={...e,expr:s};return Object.setPrototypeOf(n,Object.getPrototypeOf(e)),n}if(e.type==="CallExternNode"){let s=e.args.map(i=>jn(i,t));if(!s.some((i,o)=>i!==e.args[o]))return e;let r={...e,args:s};return Object.setPrototypeOf(r,Object.getPrototypeOf(e)),r}if(e.type==="IfThenElseNode"){let s=jn(e.condition,t),n=jn(e.thenBody,t),r=e.elseBody?jn(e.elseBody,t):null;if(s===e.condition&&n===e.thenBody&&r===e.elseBody)return e;let i={...e,condition:s,thenBody:n,elseBody:r};return Object.setPrototypeOf(i,Object.getPrototypeOf(e)),i}return e}function Rp(e,t){if(!e||typeof e!="object"||!e.type)return e;switch(e.type){case"BufferStoreNode":{let s=e.indices.map(i=>jn(i,t)),n=jn(e.value,t);if(s.every((i,o)=>i===e.indices[o])&&n===e.value)return e;let r={...e,indices:s,value:n};return Object.setPrototypeOf(r,Object.getPrototypeOf(e)),r}case"SeqNode":{let s=e.stmts.map(r=>Rp(r,t)),n={...e,stmts:s};return Object.setPrototypeOf(n,Object.getPrototypeOf(e)),n}case"ForNode":{let s=Rp(e.body,t);if(s===e.body)return e;let n={...e,body:s};return Object.setPrototypeOf(n,Object.getPrototypeOf(e)),n}case"IfThenElseNode":{let s=jn(e.condition,t),n=Rp(e.thenBody,t),r=e.elseBody?Rp(e.elseBody,t):null,i={...e,condition:s,thenBody:n,elseBody:r};return Object.setPrototypeOf(i,Object.getPrototypeOf(e)),i}default:return e}}function tv(e){return e.map(ev).join(",")}function ev(e){return e?e.type==="VariableNode"?"$"+e.name:e.type==="IntImmNode"?String(e.value):e.type==="MathOpNode"?"("+ev(e.a)+e.op+(e.b?ev(e.b):"")+")":"?":"?"}var BG,S1=I(()=>{wm();w1();b1();Jt();c(x1,"lowerToLIR");c(Tr,"lowerStmt");c(xG,"lowerForNode");c(SG,"lowerBlockNode");c(AG,"lowerSeqNode");c(vG,"lowerBufferStore");c(kG,"lowerLetStmt");c(EG,"lowerAllocate");c(NG,"lowerIfThenElse");c(RG,"lowerWhile");c(Rn,"lowerExpr");BG=new Set(["+","*","max","min"]);c(CG,"detectAccumulator");c(TG,"lowerAccumulator");c(jn,"substituteVars");c(Rp,"substituteVarsStmt");c(tv,"indicesKey");c(ev,"exprKey")});function A1(e){let t=[],s={errors:t,boundVars:new Set,path:[],bufferNames:new Set};if(!e||e.type!=="LIRFunc")return t.push(new Ii("root must be LIRFunc")),t;for(let[,n]of e.bufferMap)s.bufferNames.add(n.name);for(let n of e.shapeParams)s.boundVars.add(n.name);if(e.metadata)for(let[n]of e.metadata.locals)s.boundVars.add(n);return Ir(e.body,s),t}function Ir(e,t){if(!(!e||typeof e!="object")){switch(t.path.push(e.type),e.type){case"ForNode":IG(e,t);break;case"SeqNode":for(let s of e.stmts)Ir(s,t);break;case"LIRFlatStoreNode":OG(e,t);break;case"LIRAccumulatorNode":MG(e,t);break;case"LIRBindingsNode":LG(e,t);break;case"LetStmtNode":{Rs(e.value,t);let s=t.boundVars.has(e.variable.name);t.boundVars.add(e.variable.name),Ir(e.body,t),s||t.boundVars.delete(e.variable.name);break}case"AllocateNode":{let s=e.buffer?t.bufferNames.has(e.buffer.name):!0;e.buffer&&t.bufferNames.add(e.buffer.name),Ir(e.body,t),e.buffer&&!s&&t.bufferNames.delete(e.buffer.name);break}case"IfThenElseNode":Rs(e.condition,t),Ir(e.thenBody,t),e.elseBody&&Ir(e.elseBody,t);break;case"WhileNode":Ir(e.condBody,t),Ir(e.loopBody,t);break;case"EvaluateNode":Rs(e.value,t);break;default:break}t.path.pop()}}function IG(e,t){e.extent?Rs(e.extent,t):t.errors.push(new Ii("ForNode missing extent",[...t.path]));let s=!0;e.loopVar?(s=t.boundVars.has(e.loopVar.name),t.boundVars.add(e.loopVar.name)):t.errors.push(new Ii("ForNode missing loopVar",[...t.path])),Ir(e.body,t),e.loopVar&&!s&&t.boundVars.delete(e.loopVar.name)}function OG(e,t){e.buffer||t.errors.push(new Ii("LIRFlatStoreNode missing buffer",[...t.path])),Rs(e.offsetExpr,t),Rs(e.value,t)}function MG(e,t){e.localName||t.errors.push(new Ii("LIRAccumulatorNode missing localName",[...t.path])),e.dtype||t.errors.push(new Ii("LIRAccumulatorNode missing dtype",[...t.path]));let s=e.localName?t.boundVars.has(e.localName):!0;e.localName&&t.boundVars.add(e.localName),Rs(e.initLoad,t),Rs(e.body,t),Ir(e.flushStore,t),e.initBody&&Ir(e.initBody,t),e.localName&&!s&&t.boundVars.delete(e.localName)}function LG(e,t){let s=[];for(let n of e.bindings)Rs(n.expr,t),t.boundVars.has(n.name)||s.push(n.name),t.boundVars.add(n.name);Ir(e.body,t);for(let n of s)t.boundVars.delete(n)}function Rs(e,t){if(!(!e||typeof e!="object"||!e.type))switch(e.type){case"LIRFlatLoadNode":e.buffer||t.errors.push(new Ii("LIRFlatLoadNode missing buffer",[...t.path])),Rs(e.offsetExpr,t);break;case"MathOpNode":Rs(e.a,t),e.b&&Rs(e.b,t);break;case"CompareNode":Rs(e.a,t),Rs(e.b,t);break;case"CastNode":Rs(e.expr,t);break;case"CallExternNode":if(e.args)for(let s of e.args)Rs(s,t);break;case"IfThenElseNode":Rs(e.condition,t),Rs(e.thenBody,t),e.elseBody&&Rs(e.elseBody,t);break;case"VariableNode":e.name!==void 0&&!t.boundVars.has(e.name)&&t.errors.push(new Ii(`unbound variable '${e.name}'`,[...t.path]));break;case"IntImmNode":case"FloatImmNode":break;default:break}}var Ii,v1=I(()=>{Ii=class{static{c(this,"LIRVerificationError")}constructor(t,s){this.message=t,this.nodePath=s||[]}toString(){let t=this.nodePath.length>0?` at ${this.nodePath.join(" > ")}`:"";return`LIR verification: ${this.message}${t}`}};c(A1,"verifyLIR");c(Ir,"verifyStmt");c(IG,"verifyForNode");c(OG,"verifyFlatStore");c(MG,"verifyAccumulator");c(LG,"verifyBindings");c(Rs,"verifyExpr")});function L_(e){return e&&typeof e=="object"?e:{}}function sv(e,...t){let s=new Set(t),n={};for(let r of Object.keys(e))s.has(r)||(n[r]=e[r]);return n}function k1(e,t,s={}){return new _o({target:t,...s}).compileFunction(e)}var P_,nv,_o,D_=I(()=>{Ya();Gx();Wx();kI();RI();BI();Yx();TI();LI();PI();DI();zI();jI();HI();Nc();np();Wo();ZI();hh();Ag();WO();KO();YO();QO();jx();ZO();e1();u1();d1();m1();ps();QA();y1();S1();v1();ps();c(L_,"spread");c(sv,"omit");P_=class{static{c(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 s=t.fusion||{};this.fusion={enabled:s.enabled??t.enableFusion??!0,strategy:s.strategy??t.fusionStrategy??"xla",epilogue:s.epilogue??t.enableEpilogueFusion,...L_(t.fusionConfig),...sv(s,"enabled","strategy","epilogue")};let n=t.scheduling||{},r=this.target&&typeof this.target.isWebGPU=="function"&&this.target.isWebGPU();this.scheduling={enabled:n.enabled??t.enableSchedule??r,autotune:n.autotune??t.enableAutotune??!1,...L_(t.autotuneConfig),...sv(n,"enabled","autotune")},this.matmulBackend=t.matmulBackend||"native";let i=t.quantization||{};this.quantization={enabled:i.enabled??t.enableQuantization??!1,...L_(t.quantizationConfig),...sv(i,"enabled")};let o=t.optimization||{};this.optimization={layout:o.layout??t.enableLayoutOptimization??!1,rematerialization:o.rematerialization??t.enableRematerialization??!1,rematConfig:o.rematConfig??t.rematerializationConfig??{},fastMath:o.fastMath??t.fastMath??!1,maxSimplifyIterations:o.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 l=t.partition||{};this.partition={enabled:l.enabled??!1,targets:l.targets||[],defaultTarget:l.defaultTarget||null,opTargetOverrides:l.opTargetOverrides||new Map,memoryLimits:l.memoryLimits||new Map,minPartitionSize:l.minPartitionSize||1,costWeights:l.costWeights||{}},this.passContext=t.passContext||null;let u=t.trace||{};this.trace={level:u.level??at.SILENT,sink:u.sink??null,irSnapshot:{afterGraphPasses:!1,afterLowering:!1,afterScheduling:!1,...L_(u.irSnapshot)}}}get usePartition(){return this.partition.enabled&&this.partition.targets.length>=2}},nv=class{static{c(this,"CompilationResult")}constructor(t,s,n){this.module=t,this.trace=s,this.errors=n||[]}get succeeded(){return this.errors.length===0}get failedFunctions(){let t=new Set;for(let s of this.errors)s.funcName&&t.add(s.funcName);return t}run(t,...s){return this.module.run(t,...s)}async runAsync(t,...s){return this.module.runAsync(t,...s)}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()}},_o=class{static{c(this,"Compiler")}constructor(t){if(this.config=t instanceof P_?t:new P_(t),!this.config.target)throw new Error("Compiler requires a target")}compile(t){let s=new Bd(this.config.trace),n=this.config.errorMode==="resilient",r=[],i=new Set,o=performance.now();s.phaseStart("compile"),this.config.verify&&this._verifyGraph(t,"before graph passes",s,r,i,n);let a=this._runGraphPasses(t,s,r,i,n);this.config.usePartition&&this._runPartitioning(t,s);let l=typeof this.config.target.isWebGPU=="function"&&this.config.target.isWebGPU(),u=p1(t,{config:this.config,target:this.config.target,cudaMatmulChain:a,isWebGPU:l});this.config.verify&&this._verifyGraph(t,"after graph passes",s,r,i,n);let f=this._lowerAll(t,s,r,i,n);if(this.config.matmulBackend==="cublas")for(let d of f)d.cublasInfo=u&&u.cublasInfos?u.cublasInfos.get(d.name)||null:IR(d);this._scheduleAll(f,s,r,i,n),this.config.verifyMode==="full"&&this._verifyAll(f,r,i,n),this._planMemory(f,s,r,i,n),this.config.verify&&this._verifyAll(f,r,i,n);let h=this._lowerToLIR(f,s,r,i,n),p=this._codegen(h,s,r,i,n);if(u&&(p.executionPlan=u.plan),s.phaseEnd("compile",performance.now()-o),!n&&r.length>0)throw new Error(r[0].toString());return new nv(p,s,r)}compileFunction(t){let s=new cn("single");return s.addFunction(t),this.compile(s)}calibrate(t,s="minmax"){let n=new b_(s);for(let r of t)n.attach(r);return n}_runGraphPasses(t,s,n,r,i){let o=new kl;for(let m of YA("pre",this.config,this.config.target))o.addPass(m);o.addPass(new Jc(this.config.target)),o.addPass(new Hc("canonicalize",[new El,new Iy({fastMath:this.config.optimization.fastMath}),new Oy,new My,new Bi],this.config.optimization.maxSimplifyIterations)),this.config.optimization.layout&&this.config.target&&(o.addPass(new jy({target:this.config.target})),o.addPass(new Bi)),this.config.quantization.enabled&&(o.addPass(new Ky({...this.config.quantization,target:this.config.target})),o.addPass(new El),o.addPass(new Bi));let a=0;for(let m of t)for(let g of m.ops())g.opName==="dot"&&a++;let l=this.config.target,u=(l.getAttr&&l.getAttr("matmulChainThreshold"))??(l.kind==="cuda"?2:1/0),f=a>=u;if(this.config.matmulBackend!=="cublas"&&!f&&(this.config.fusion.epilogue!==void 0?this.config.fusion.epilogue:this.config.target&&this.config.target.enableEpilogueFusion)&&(o.addPass(new Fy({target:this.config.target})),o.addPass(new Bi)),this.config.fusion.enabled){let m=this.config.fusion;m.strategy==="dominator"?o.addPass(new Vy({target:this.config.target,...m})):(o.addPass(new Dy({target:this.config.target,cost:{launchOverheadUs:5},...m})),o.addPass(new $y({maxFusionSize:this.config.target?.maxFusionSize,...m})),o.addPass(new zy({maxFusionSize:this.config.target?.maxFusionSize,...m}))),o.addPass(new Bi)}if(this.config.matmulBackend==="cublas"&&o.addPass(new O_),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),o.addPass(new A_(m))}for(let m of YA("post",this.config,this.config.target))o.addPass(m);o.setTrace(s),this.config.verifyMode==="full"&&o.setVerifyHook((m,g)=>(g?FA(m):xp(m)).map(_=>_.toString())),s.phaseStart("graphPasses");let p=performance.now(),d=o.run(t,{errorMode:i?"resilient":"strict",passContext:this.config.passContext});if(d.errors){for(let m of d.errors)n.push(m),s.errorEvent(m.phase,m.funcName,m.message,m.passName);if(d.failedFunctions)for(let m of d.failedFunctions)r.add(m)}if(s.phaseEnd("graphPasses",performance.now()-p),s.shouldSnapshot("afterGraphPasses")){let m=new Ep;s.irDump("afterGraphPasses",m.printModule(t))}return f}_runPartitioning(t,s){let n=new kl;n.addPass(new N_(this.config.partition)),n.addPass(new R_({targets:this.config.partition.targets})),n.setTrace(s),s.phaseStart("partition");let r=performance.now();n.run(t),s.phaseEnd("partition",performance.now()-r)}_lowerAll(t,s,n,r,i){s.phaseStart("lowering");let o=performance.now(),a=[];for(let l of t)if(!r.has(l.name))try{let u=performance.now(),f=rh(l,this.config.target);s.functionEvent("lowering",l.name,{durationMs:performance.now()-u}),a.push(f),s.shouldSnapshot("afterLowering")&&s.irDump("afterLowering:"+l.name,ZA(f))}catch(u){let f=new ws("lowering",l.name,u.message);if(n.push(f),r.add(l.name),s.errorEvent("lowering",l.name,u.message),!i)break}return s.phaseEnd("lowering",performance.now()-o),a}_scheduleAll(t,s,n,r,i){s.phaseStart("scheduling");let o=performance.now(),a=this.config.scheduling;if(a.autotune){let l=new y_(this.config.target,a);for(let u of t)if(!r.has(u.name)&&!u.cublasInfo)try{let f=performance.now(),h=l.tuneAndApply(u),p=performance.now()-f,d=0,m=0;if(h&&h.results){m=h.results.size;for(let[g,y]of h.results)y.fromCache&&d++,s.explainsEnabled&&s.explain("schedule",g,y.sketchName,`autotuned: best of search${y.fromCache?" (cached)":""}, score ${y.score!=null?y.score.toFixed(3):"n/a"}`,{target:this.config.target.name,params:y.params})}s.autotuneStats(u.name,{durationMs:p,blockCount:m,applied:!!(h&&h.applied),cacheHits:d})}catch(f){let h=new ws("scheduling",u.name,f.message);if(n.push(h),r.add(u.name),s.errorEvent("scheduling",u.name,f.message),!i)break}}else if(a.enabled){let l=new lo(this.config.target,null,s);for(let u of t)if(!r.has(u.name)&&!u.cublasInfo)try{let f=performance.now(),h=new En(u);l.applyToAllBlocks(h),s.functionEvent("scheduling",u.name,{durationMs:performance.now()-f})}catch(f){let h=new ws("scheduling",u.name,f.message);if(n.push(h),r.add(u.name),s.errorEvent("scheduling",u.name,f.message),!i)break}}if(s.phaseEnd("scheduling",performance.now()-o),s.shouldSnapshot("afterScheduling"))for(let l of t)r.has(l.name)||s.irDump("afterScheduling:"+l.name,ZA(l))}_planMemory(t,s,n,r,i){s.phaseStart("memoryPlanning");let o=performance.now(),a=this.config.memory.alignment||this.config.target?.cacheLineSizeBytes||64,l=new Qy({alignment:a,enableInplace:this.config.memory.inplaceReuse,allocStrategy:this.config.memory.allocStrategy});for(let u of t)if(!r.has(u.name)&&!u.gpuRegisterBlocked)try{let f=performance.now(),{plan:h}=l.planAndRewrite(u),p=h.getReport();s.memoryStats(u.name,{durationMs:performance.now()-f,peakMemory:p.peakMemory,totalTemporaries:p.totalTemporaries,totalInplace:p.totalInplace})}catch(f){let h=new ws("memoryPlanning",u.name,f.message);if(n.push(h),r.add(u.name),s.errorEvent("memoryPlanning",u.name,f.message),!i)break}s.phaseEnd("memoryPlanning",performance.now()-o)}_verifyGraph(t,s,n,r,i,o){if(o){for(let l of t){if(i.has(l.name))continue;let u=xp?xp(l):[];if(u.length>0){let f=u.map(h=>h.toString()).join("; ");r.push(new ws("verification",l.name,f)),i.add(l.name),n.errorEvent("verification",l.name,f)}}return}let a=FA(t);if(a.length>0)throw new Error("Graph verification failed ("+s+"): "+a.map(l=>l.toString()).join("; "))}_verifyAll(t,s,n,r){let i=new __;for(let o of t){if(n.has(o.name))continue;let a=i.verify(o);if(a.length>0){let l=a.join("; ");if(r)s.push(new ws("verification",o.name,l)),n.add(o.name);else throw new Error("TensorIR verification failed for "+o.name+": "+l)}}}_lowerToLIR(t,s,n,r,i){s.phaseStart("lirLowering");let o=performance.now(),a=[];for(let l of t)if(!r.has(l.name))try{let u=performance.now(),f=x1(l,this.config.target);if(l.cublasInfo&&(f.cublasInfo=l.cublasInfo),l.gpuRegisterBlocked&&(f.gpuRegisterBlocked=!0),this.config.verifyMode==="full"){let h=A1(f);if(h.length>0)throw new Error("LIR verification failed: "+h.map(p=>p.toString()).join("; "))}s.functionEvent("lirLowering",l.name,{durationMs:performance.now()-u}),a.push(f)}catch(u){let f=new ws("lirLowering",l.name,u.message);if(n.push(f),r.add(l.name),s.errorEvent("lirLowering",l.name,u.message),!i)break}return s.phaseEnd("lirLowering",performance.now()-o),a}_codegen(t,s,n,r,i){s.phaseStart("codegen");let o=performance.now(),a=new Kc("compiled"),l=this.config.usePartition,u={matmulBackend:this.config.matmulBackend},f=new Map,h=c(d=>(f.has(d.name)||f.set(d.name,new di(d,u)),f.get(d.name)),"getBackend"),p=l?null:new di(this.config.target,u);for(let d of t)if(!r.has(d.name))try{let m=performance.now(),g;if(l){let _=d._partitionTarget,w=_?this.config.partition.targets.find(b=>b.name===_):this.config.target;g=h(w||this.config.target)}else g=p;let y=g.compile(d);a.addCompiledKernel(y),d.shapeParamMap&&d.shapeParamMap.size>0&&a.setShapeParamMap(d.name,d.shapeParamMap,d.bufferMap),s.codegenStats(d.name,{durationMs:performance.now()-m,sourceSize:y.source.length,targetName:y.target.name})}catch(m){let g=new ws("codegen",d.name,m.message);if(n.push(g),r.add(d.name),s.errorEvent("codegen",d.name,m.message),!i)break}return s.phaseEnd("codegen",performance.now()-o),a}};c(k1,"compileGraph")});function DG(e){return(t,...s)=>{let n=Ol();if(!n)throw new Error(`TRACING dispatch key active but no tracer is set for op '${e}'`);let r=[],i={},o=PG[e],a=0,l=c(f=>f instanceof de||f&&f._impl,"isTensor"),u=c(f=>{f instanceof de||f&&f._impl&&f.isSymbolic?r.push(f):f&&f._impl&&r.push(n.captureConstant(f))},"pushTensor");for(let f of s)if(Array.isArray(f)&&f.length>0&&l(f[0]))for(let h of f)u(h);else l(f)?u(f):o&&(f!=null&&a<o.length&&(i[o[a]]=f),a++);return n.recordOp(e,r,i)}}function E1(){let e=Ue.listOps();for(let t of e){let s=Ue.findOp(t);if(!s)continue;let n=s.name,r=Dn.fromUnboxed(DG(n));s.entry.registerKernel(it.TRACING,r)}}var PG,N1=I(()=>{Qn();$f();ja();hp();Tl();PG={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"]};c(DG,"_tracingKernel");c(E1,"registerTracingDispatch")});function $G(e){return e&&e.shape&&e.shape.length>=2&&FG.has(e.dtype)}function R1(e,t,s=$G){let n=e.graph.functions().next().value;if(!n)return e;let r=n.entryBlock,i=e.numUserInputs,o=e.capturedParams,a=[];for(let h=0;h<o.length;h++){let p=i+h,d=r.getArgument(p);d&&s(o[h],h,d)&&a.push({j:h,argIndex:p,arg:d,param:o[h]})}if(a.length===0)return e;for(let h of a){let p=t(h.param),d=h.arg.type,m=new bt("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 l=new Set(a.map(h=>h.argIndex));r.removeArguments(l),n.inputTypes=Object.freeze(n.inputTypes.filter((h,p)=>!l.has(p)));let u=new Set(a.map(h=>h.j)),f=o.filter((h,p)=>!u.has(p));return{...e,capturedParams:f}}var FG,B1=I(()=>{xn();FG=new Set(["f16","f32","f64"]);c($G,"defaultWeightPredicate");c(R1,"foldWeightParams")});function zG(){C1||(E1(),C1=!0)}function UG(e,t){if(!e)return new Array(t.length).fill(null);let s=new Array(t.length);for(let n=0;n<t.length;n++){let r=e[n];if(r===!0){let i=new Set;for(let o=0;o<t[n].shape.length;o++)i.add(o);s[n]=i}else r instanceof Set?s[n]=r:s[n]=null}return s}function U_(e,t,s){zG();let n=s?.name||e.name||"traced",r=new ky(n),i=UG(s?.dynamicShapes,t);for(let h=0;h<t.length;h++)r.createInput(t[h].shape,t[h].dtype,i[h]);let o=t.length,a=r._initGraph();function l(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:o,outputTypes:d.outputTypes,shapeEnv:r.shapeEnv,outputSymShapes:r.outputSymShapes}}c(l,"_finalize"),r.activate();let u=Yn.fromKey(it.TRACING),f=LE(u,()=>e(...a));if(f&&typeof f.then=="function")return f.then(h=>l(h),h=>{throw r.deactivate(),h});try{return l(f)}catch(h){throw r.deactivate(),h}}function ov(e,t,s){let n=U_(e,t,s);return n&&typeof n.then=="function"?n.then(r=>r.graph):n.graph}function VG(e,t,s){let n=e.result.listKernels();if(n.length===0)throw new Error("No kernels compiled");let r=n[0],i=t.length>0?t[0].device:"cpu",o=new Array(t.length);for(let g=0;g<t.length;g++)o[g]=Nn(t[g]);let a=e.capturedParams,l=new Array(a.length);for(let g=0;g<a.length;g++)l[g]=Nn(a[g]);let u=e.outputTypes,f=e.outputSymShapes,h=new Array(u.length),p=new Array(u.length);for(let g=0;g<u.length;g++){let y=f&&s?s.resolveSymbolicShape(f[g]):u[g].shape,_=u[g].dtype,w=gs(y),b=me(_);h[g]=new b(Math.max(w,1)),p[g]=y}let d=new Array(o.length+l.length+h.length),m=0;for(let g=0;g<o.length;g++)d[m++]=new Ri(o[g],t[g].shape,t[g].dtype);for(let g=0;g<l.length;g++)d[m++]=new Ri(l[g],a[g].shape,a[g].dtype);for(let g=0;g<h.length;g++)d[m++]=new Ri(h[g],p[g],u[g].dtype);return{funcName:r,device:i,outputTypes:u,outputArrays:h,outputShapes:p,allArgs:d}}function iv(e,t,s,n){if(t.length===1)return ae(s[0],n[0],t[0].dtype,e);let r=new Array(t.length);for(let i=0;i<t.length;i++)r[i]=ae(s[i],n[i],t[i].dtype,e);return r}function GG(e,t,s){let{funcName:n,device:r,outputTypes:i,outputArrays:o,outputShapes:a,allArgs:l}=VG(e,t,s),u=e.result.module.executionPlan;return u?e.result.module.runPlanAsync(u,l).then(()=>iv(r,i,o,a)):e.result.isAsync(n)?e.result.runAsync(n,...l).then(()=>iv(r,i,o,a)):(e.result.run(n,...l),iv(r,i,o,a))}function V_(e,t,s){if(s?.backward)return _compileWithBackward(e,t,s);let n=s?.target??nr(),r={target:n,verify:!1,...s},i=s?.dynamic_shapes||null,o=s?.shapeBuckets||null,a=s?.foldWeights??s?.quantization?.foldWeights??!1,l=[];function u(w,b,x){if(!w||typeof w!="object"||w.repro)return w;try{w.repro={name:e.constructor?.name||"compiled",phase:x,target:n?.name,inputs:(b||[]).map(A=>({shape:A.shape,dtype:A.dtype})),config:{fusion:r.fusion,scheduling:r.scheduling,optimization:r.optimization,quantization:r.quantization,dynamicShapes:!!i}}}catch{return w}return w}c(u,"_attachRepro");function f(w){let b=a?R1(w,Nn):w;return{result:new _o(r).compile(b.graph),graph:b.graph,capturedParams:b.capturedParams,numUserInputs:b.numUserInputs,outputTypes:b.outputTypes,shapeEnv:b.shapeEnv,outputSymShapes:b.outputSymShapes}}c(f,"_finalize");function h(w,b){try{let x=U_((...A)=>e.forward(...A),w,{name:e.constructor.name||"compiled",dynamicShapes:b});return x&&typeof x.then=="function"?x.then(f,A=>{throw u(A,w,"compile")}):f(x)}catch(x){throw u(x,w,"compile")}}c(h,"_compileWith");function p(w){return h(w,i)}c(p,"_compile");function d(w){return w.map((b,x)=>({shape:b,dtype:t[x].dtype}))}c(d,"_bucketInputs");function m(w){for(let b=0;b<l.length;b++){let x=l[b];x.shapeEnv.bindInputShapes(w);let{passed:A}=x.shapeEnv.evaluateGuards();if(A)return x}return null}c(m,"_findCachedEntry");function g(w,b){try{let x=GG(w,b,w.shapeEnv);return x&&typeof x.then=="function"?x.then(void 0,A=>{throw u(A,b,"run")}):x}catch(x){throw u(x,b,"run")}}c(g,"_execute");function y(...w){let b=m(w);if(!b){let x=p(w);if(x&&typeof x.then=="function")return x.then(A=>(l.push(A),A.shapeEnv.bindInputShapes(w),g(A,w)));b=x,l.push(b),b.shapeEnv.bindInputShapes(w)}return g(b,w)}c(y,"compiledForward");let _=null;if(t){let w=[];if(o)for(let b of o)w.push(h(d(b),null));if(w.push(p(t)),w.some(b=>b&&typeof b.then=="function"))_=Promise.all(w).then(b=>{for(let x of b)l.push(x)});else for(let b of w)l.push(b)}return y.original=e,y.graph=w=>ov((...x)=>e.forward(...x),w||t,{name:e.constructor.name||"compiled",dynamicShapes:i}),y.source=()=>{if(l.length===0)return null;let w=l[0],b=w.result.listKernels();return b.length>0?w.result.getSource(b[0]):null},y.kernels=()=>l.length===0?[]:l[0].result.listKernels(),y.snippet=()=>{if(l.length===0)return null;let w=l[0],b=w.result.listKernels();return b.length>0?w.result.getSnippet(b[0]):null},y.result=()=>l.length>0?l[0].result:null,y._ready=_,y}var C1,G_=I(()=>{hp();N1();Qn();fb();D_();hi();Nr();Ag();ir();Vr();B1();C1=!1;c(zG,"_ensureTracing");c(UG,"_normalizeDynamicShapes");c(U_,"_traceCore");c(ov,"trace");c(VG,"_prepareExecution");c(iv,"_wrapOutputs");c(GG,"executeCompiled");c(V_,"compile")});var Xr,Bp=I(()=>{Xr=class{static{c(this,"GradAccumulator")}constructor(t){this._builder=t,this._grads=new Map}accumulate(t,s){if(!s)return;let n=this._grads.get(t);if(!n)this._grads.set(t,s);else{let r=this._builder.add(n,s).getResult(0);this._grads.set(t,r)}}get(t){return this._grads.get(t)||null}has(t){return this._grads.has(t)}}});function lt(e,t){av.set(e,t)}function Vl(e){return av.get(e)||null}function q_(e){let t=av.get(e);if(t)return t;if(lv.has(e))return null;throw new Error(`autodiff: op '${e}' is on the gradient path but has no VJP rule and is not a registered gradient barrier. Register one with registerVJPRule('${e}', ...) or registerGradientBarrier('${e}').`)}function cv(e,t){T1.set(e,t)}function I1(e){return T1.get(e)||null}function uv(e){lv.add(e)}function j_(e){return lv.has(e)}var av,lv,T1,Mr=I(()=>{av=new Map,lv=new Set;c(lt,"registerVJPRule");c(Vl,"getVJPRule");c(q_,"requireVJPRuleOrBarrier");T1=new Map;c(cv,"registerRegionVJP");c(I1,"getRegionVJP");c(uv,"registerGradientBarrier");c(j_,"isGradientBarrier")});function Cp(e){let t=new Set(e.arguments.map(r=>r.id));for(let r of e.ops())for(let i of r.results)t.add(i.id);let s=new Set,n=[];for(let r of e.ops())for(let i of r.operands)t.has(i.id)||s.has(i.id)||i.definingOp&&i.definingOp.opName==="constant"||(s.add(i.id),n.push(i));return n}function W_(e,t){let s=e.scalarConstant(0,t.type.dtype).getResult(0);return e.broadcast(s,t.type.shape,[]).getResult(0)}function O1(e,t,s){let n=t.type.shape,r=n.map((a,l)=>l===0?s:0),i=n.map((a,l)=>l===0?s+1:a),o=e.slice(t,r,i).getResult(0);return e.reshape(o,n.slice(1)).getResult(0)}function qG(e,t,s){let n=s.slice(1),r=t.map(i=>e.reshape(i,[1,...n]).getResult(0));return r.length===1?e.reshape(r[0],s).getResult(0):e.concat(r,0).getResult(0)}function K_(e,t,s,n,r,i,o=new Map){let a=new Map;for(let g=0;g<t.arguments.length;g++)a.set(t.arguments[g].id,s[g]);for(let[g,y]of n)a.set(g,y);let l=[],u=null;for(let g of t.ops())g.opName==="yield"?u=g:l.push(g);let f=c(g=>{if(a.has(g.id))return a.get(g.id);let y=g.definingOp;if(y&&y.opName==="constant"){let _=o.get(g.id);return _===void 0&&(_=e._buildOp("constant",[],[g.type],new Map(y.attributes),null).getResult(0),o.set(g.id,_)),a.set(g.id,_),_}return g},"matOperand");for(let g of l){let y=g.operands.map(f),_=e._buildOp(g.opName,y,g.results.map(w=>w.type),new Map(g.attributes),null);for(let w=0;w<g.numResults;w++)a.set(g.getResult(w).id,_.getResult(w))}let h=u.operands.map(g=>a.get(g.id));if(i)return{forwardYields:h};let p=new Xr(e);for(let g=0;g<u.operands.length;g++)r[g]&&p.accumulate(u.operands[g].id,r[g]);for(let g=l.length-1;g>=0;g--){let y=l[g];if(y.opName==="constant")continue;let _=y.results.map(A=>p.get(A.id));if(_.every(A=>A===null))continue;let w=q_(y.opName);if(!w)continue;let b={builder:e,op:y,operands:y.operands.map(f),results:y.results.map(A=>a.get(A.id)),gradOutputs:_,attrs:y.attributes},x=w(b);if(x)for(let A=0;A<y.numOperands;A++)x[A]&&p.accumulate(y.getOperand(A).id,hv(e,x[A],y.getOperand(A).type.shape))}let d=t.arguments.map(g=>p.get(g.id)),m=new Map;for(let g of n.keys())m.set(g,p.get(g));return{forwardYields:h,gradArgs:d,gradFree:m}}function fv(e,t){for(let s of e.ops())if(jG.has(s.opName))throw new Error(`${t} VJP does not support nested control flow ('${s.opName}') in the body`)}function WG(e,t,s,n,r){let i=e.regions[0].blocks[0],o=e.regions[1].blocks[0];fv(i,"if"),fv(o,"if");let a=n(e.getOperand(0)),l=[];for(let y=0;y<e.numResults;y++)l.push(t.get(e.getResult(y).id));let u=Cp(i),f=Cp(o),h=new Map(u.map(y=>[y.id,n(y)])),p=new Map(f.map(y=>[y.id,n(y)])),{gradFree:d}=K_(s,i,[],h,l,!1),{gradFree:m}=K_(s,o,[],p,l,!1),g=new Map;for(let y of u)g.set(y.id,y);for(let y of f)g.set(y.id,y);for(let[y,_]of g){if(!r.has(y))continue;let w=d.get(y),b=m.get(y);if(!w&&!b)continue;let x=W_(s,_),A=s.broadcast(a,_.type.shape,[]).getResult(0);t.accumulate(y,s.select(A,w??x,b??x).getResult(0))}}function KG(e,t){if(!e||t<=1)return null;if(e==="sqrt"||e===!0)return Math.max(1,Math.ceil(Math.sqrt(t)));if(typeof e=="number"&&e>=1){let s=Math.floor(e);return s>=t?null:s}return null}function HG(e,t,s,n,r,i=null){let o=e.regions[0].blocks[0];fv(o,"scan");let a=e.getAttr("num_carry"),l=e.getAttr("num_xs"),u=e.numResults-a,f=[];for(let k=0;k<l;k++)f.push(e.getOperand(k));let h=[];for(let k=0;k<a;k++)h.push(e.getOperand(l+k));let p=f[0].type.shape[0],d=Cp(o),m=f.map(n),g=h.map(n),y=new Map(d.map(k=>[k.id,n(k)])),_=new Map,w=c(k=>m.map(B=>O1(s,B,k)),"sliceX"),b=c((k,B)=>K_(s,o,[...k,...B],y,null,!0,_).forwardYields.slice(0,a),"stepForward"),x=[];for(let k=0;k<u;k++)x.push(t.get(e.getResult(a+k).id));let A=[];for(let k=0;k<a;k++){let B=t.get(e.getResult(k).id);A.push(B??W_(s,g[k]))}let S=new Map,E=m.map(()=>new Array(p)),R=c((k,B,N)=>{let C=[...B,...N],O=x.map(q=>q===null?null:O1(s,q,k)),F=[...A,...O],{gradArgs:P,gradFree:z}=K_(s,o,C,y,F,!1,_);for(let q=0;q<l;q++)E[q][k]=P[q]??W_(s,B[q]);A=[];for(let q=0;q<a;q++)A.push(P[l+q]??W_(s,N[q]));for(let[q,U]of z){if(!U)continue;let D=S.get(q);S.set(q,D?s.add(D,U).getResult(0):U)}},"backwardStep"),v=KG(i,p);if(v){let k=Math.ceil(p/v),B=new Array(k),N=g;B[0]=N;for(let C=0;C<p;C++){N=b(w(C),N);let O=(C+1)/v;Number.isInteger(O)&&O<k&&(B[O]=N)}for(let C=k-1;C>=0;C--){let O=C*v,F=Math.min(O+v,p),P=[],z=[B[C]],q=B[C];for(let U=O;U<F;U++){let D=w(U);P.push(D),q=b(D,q),z.push(q)}for(let U=F-1;U>=O;U--)R(U,P[U-O],z[U-O])}}else{let k=g,B=[k],N=[];for(let C=0;C<p;C++){let O=w(C);N.push(O),k=b(O,k),B.push(k)}for(let C=p-1;C>=0;C--)R(C,N[C],B[C])}for(let k=0;k<l;k++)r.has(f[k].id)&&t.accumulate(f[k].id,qG(s,E[k],m[k].type.shape));for(let k=0;k<a;k++)r.has(h[k].id)&&t.accumulate(h[k].id,A[k]);for(let[k,B]of S)B&&r.has(k)&&t.accumulate(k,B)}var jG,M1=I(()=>{Bp();Mr();Tp();cv("scan",(e,t)=>HG(e,t.accumulator,t.builder,t.materialize,t.needsGrad,t.scanCheckpoint));cv("if",(e,t)=>WG(e,t.accumulator,t.builder,t.materialize,t.needsGrad));c(Cp,"regionFreeVars");c(W_,"zeroLike");c(O1,"sliceStep");c(qG,"stackSteps");c(K_,"diffBodyStep");jG=new Set(["scan","while","if"]);c(fv,"assertNoNestedControlFlow");c(WG,"buildCondBackward");c(KG,"resolveSegmentLength");c(HG,"buildScanBackward")});function Op(e,{accumulator:t,builder:s,needsGrad:n,resolveValue:r,handleRegionOp:i=null}){for(let o=e.length-1;o>=0;o--){let a=e[o];if(a.opName==="return"||a.opName==="constant"||!a.results.some(m=>n.has(m.id)))continue;let u=[];for(let m=0;m<a.numResults;m++)u.push(t.get(a.getResult(m).id));if(u.every(m=>m===null)||i&&i(a))continue;let f=q_(a.opName);if(!f)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=f({builder:s,op:a,operands:h,results:p,gradOutputs:u,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);n.has(g.id)&&t.accumulate(g.id,hv(s,d[m],g.type.shape))}}}function L1(e){let t=[];for(let s of e.regions)s.blocks[0]&&t.push(...Cp(s.blocks[0]));return t}function hv(e,t,s){let n=t.type.shape;if(n.length===s.length&&n.every((l,u)=>l===s[u]))return t;let r=n.length-s.length,i=[];for(let l=0;l<r;l++)i.push(l);for(let l=0;l<s.length;l++)s[l]===1&&n[r+l]!==1&&i.push(r+l);let o=t;if(i.length>0){let l=e.scalarConstant(0,t.type.dtype).getResult(0);o=e.reduce(t,l,i,"sum").getResult(0)}let a=o.type.shape;return a.length===s.length&&a.every((l,u)=>l===s[u])||(o=e.reshape(o,s).getResult(0)),o}var Ip,Mp,Tp=I(()=>{Co();li();Ci();Bp();Mr();M1();Ip=new Set(["scan","if"]);c(Op,"backpropOps");c(L1,"regionControlFlowFreeVars");c(hv,"reduceGradToOperandShape");Mp=class{static{c(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 n=ne.compute(t).topologicalOrder,r=t.getReturnOp();if(!r)throw new Error("Forward function has no return op");let i=r.operands,o=t.args,a=this._computeGradReachability(t,n),{savedValues:l,savedValueIndices:u}=this._identifySavedValues(n,a,o),f=i.map(S=>S.type),h=l.map(S=>S.type),p=[...f,...h],d=[];for(let S=0;S<o.length;S++)a.has(o[S].id)&&d.push(o[S].type);let m=new Fs(`backward_${t.name}`,p,d),g=new bs(m),y=m.args,_=y.slice(0,f.length),w=y.slice(f.length),b=new Map;for(let S=0;S<l.length;S++)b.set(l[S].id,w[S]);for(let S=0;S<o.length;S++)u.has(o[S].id)&&b.set(o[S].id,w[u.get(o[S].id)]);let x=new Xr(g);for(let S=0;S<i.length;S++){let E=i[S];x.accumulate(E.id,_[S])}Op(n,{accumulator:x,builder:g,needsGrad:a,resolveValue:c(S=>this._materialize(S,b,g),"resolveValue"),handleRegionOp:c(S=>{let E=I1(S.opName);return E?(E(S,{accumulator:x,builder:g,materialize:c(R=>this._materialize(R,b,g),"materialize"),needsGrad:a,scanCheckpoint:this._scanCheckpoint}),!0):!1},"handleRegionOp")});let A=[];for(let S=0;S<o.length;S++)if(a.has(o[S].id)){let E=x.get(o[S].id);if(E)A.push(E);else{let R=g.scalarConstant(0,o[S].type.dtype).getResult(0),v=g.broadcast(R,o[S].type.shape,[]).getResult(0);A.push(v)}}return g.returnOp(A),{backwardFunc:m,savedValues:l,gradInputIndices:this._getGradInputIndices(o,a)}}_materialize(t,s,n){if(s.has(t.id))return s.get(t.id);if(!t.definingOp)return t;let r=new Set([t.id]),i=[{val:t,i:0}];for(;i.length>0;){let o=i[i.length-1],a=o.val,l=a.definingOp;if(s.has(a.id)||!l){r.delete(a.id),i.pop();continue}if(o.i<l.numOperands){let p=l.getOperand(o.i);o.i++,p.definingOp&&!s.has(p.id)&&!r.has(p.id)&&(r.add(p.id),i.push({val:p,i:0}));continue}let u=new Array(l.numOperands);for(let p=0;p<l.numOperands;p++){let d=l.getOperand(p);u[p]=s.has(d.id)?s.get(d.id):d}let f=l.results.map(p=>p.type),h=n._buildOp(l.opName,u,f,new Map(l.attributes),null);for(let p=0;p<l.numResults;p++)s.set(l.getResult(p).id,h.getResult(p));r.delete(a.id),i.pop()}return s.has(t.id)?s.get(t.id):t}_computeGradReachability(t,s){let n=new Set,r=t.getReturnOp();for(let i of r.operands)n.add(i.id);for(let i=s.length-1;i>=0;i--){let o=s[i];if(!(o.opName==="return"||!o.results.some(l=>n.has(l.id)))){if(Ip.has(o.opName)){for(let l=0;l<o.numOperands;l++)n.add(o.getOperand(l).id);for(let l of L1(o))n.add(l.id);continue}if(Vl(o.opName)&&!j_(o.opName))for(let l=0;l<o.numOperands;l++)n.add(o.getOperand(l).id)}}return n}_identifySavedValues(t,s,n){let r=[],i=new Map,o=new Set(n.map(h=>h.id));for(let h of t)if(!(h.opName==="return"||h.opName==="constant"||!Vl(h.opName)||!h.results.some(m=>s.has(m.id)))){for(let m=0;m<h.numOperands;m++){let g=h.getOperand(m);o.has(g.id)&&!i.has(g.id)&&(i.set(g.id,r.length),r.push(g))}for(let m=0;m<h.numResults;m++){let g=h.getResult(m);s.has(g.id)&&!i.has(g.id)&&this._shouldSaveResult(h)&&(i.set(g.id,r.length),r.push(g))}}let a=new Set(i.keys()),l=new Map(n.map(h=>[h.id,h])),u=new Set,f=c(h=>{if(a.has(h.id)||u.has(h.id))return;u.add(h.id);let p=h.definingOp;if(!p){o.has(h.id)&&!i.has(h.id)&&(i.set(h.id,r.length),r.push(l.get(h.id)));return}for(let d=0;d<p.numOperands;d++)f(p.getOperand(d))},"collect");for(let h of t)if(!(h.opName==="return"||h.opName==="constant")&&h.results.some(p=>s.has(p.id))){if(Ip.has(h.opName)){for(let p of h.operands)f(p);for(let p of L1(h))f(p);continue}if(Vl(h.opName)){for(let p=0;p<h.numOperands;p++)f(h.getOperand(p));for(let p=0;p<h.numResults;p++)f(h.getResult(p))}}return{savedValues:r,savedValueIndices:i}}_shouldSaveResult(t){if(this._rematPolicy)return!this._rematPolicy.shouldRematerialize(t);let s=t.opName;return!new Set(["neg","abs","sign","floor","ceil"]).has(s)}_getGradInputIndices(t,s){let n=[];for(let r=0;r<t.length;r++)s.has(t[r].id)&&n.push(r);return n}_buildCheckpointed(t){let n=ne.compute(t).topologicalOrder,r=t.getReturnOp();if(!r)throw new Error("Forward function has no return op");let i=r.operands,o=t.args,a=this._computeGradReachability(t,n),l=this._checkpointPolicy.segment(n,t);for(let k of l)for(let B of k.ops)if(Ip.has(B.opName))throw new Error(`Checkpointed backward does not support region control-flow op '${B.opName}'; build the backward without a checkpointPolicy, which differentiates scan/if via buildScanBackward/buildCondBackward.`);let u=new Set,f=[],h=new Map,p=new Set(o.map(k=>k.id));for(let k of o)a.has(k.id)&&!u.has(k.id)&&(u.add(k.id),h.set(k.id,f.length),f.push(k));for(let k of l){for(let B of k.boundaryInputs)if(!p.has(B)&&!u.has(B)){u.add(B);let N=this._findValue(n,B);N&&(h.set(B,f.length),f.push(N))}for(let B of k.boundaryOutputs)if(!u.has(B)){u.add(B);let N=this._findValue(n,B);N&&(h.set(B,f.length),f.push(N))}}for(let k of i)u.has(k.id)||(u.add(k.id),h.set(k.id,f.length),f.push(k));let d=i.map(k=>k.type),m=f.map(k=>k.type),g=[...d,...m],y=[];for(let k=0;k<o.length;k++)a.has(o[k].id)&&y.push(o[k].type);let _=new Fs(`backward_${t.name}`,g,y),w=new bs(_),b=_.args,x=b.slice(0,d.length),A=b.slice(d.length),S=new Map;for(let k=0;k<f.length;k++)S.set(f[k].id,A[k]);let E=new Xr(w);for(let k=0;k<i.length;k++)E.accumulate(i[k].id,x[k]);let R=new Map;for(let k of n)if(k.opName==="constant"){let B=k.getResult(0).type,N=w._buildOp("constant",[],[B],new Map(k.attributes),null);R.set(k.getResult(0).id,N.getResult(0))}for(let k=l.length-1;k>=0;k--){let B=l[k],N=new Map;for(let C of B.ops){let O=new Array(C.numOperands);for(let z=0;z<C.numOperands;z++){let q=C.getOperand(z),U=N.get(q.id)||S.get(q.id)||R.get(q.id);O[z]=U||q}let F=C.results.map(z=>z.type),P=w._buildOp(C.opName,O,F,new Map(C.attributes),null);for(let z=0;z<C.numResults;z++)N.set(C.getResult(z).id,P.getResult(z))}Op(B.ops,{accumulator:E,builder:w,needsGrad:a,resolveValue:c(C=>N.get(C.id)||S.get(C.id)||R.get(C.id)||C,"resolveValue")})}let v=[];for(let k=0;k<o.length;k++)if(a.has(o[k].id)){let B=E.get(o[k].id);if(B)v.push(B);else{let N=w.scalarConstant(0,o[k].type.dtype).getResult(0),C=w.broadcast(N,o[k].type.shape,[]).getResult(0);v.push(C)}}return w.returnOp(v),{backwardFunc:_,savedValues:f,gradInputIndices:this._getGradInputIndices(o,a)}}_findValue(t,s){for(let n of t)for(let r=0;r<n.numResults;r++)if(n.getResult(r).id===s)return n.getResult(r);return null}}});var Gl,H_=I(()=>{Gl=class{static{c(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 s=t.numResults>0?t.getResult(0).type:null;return!s||!s.shape||s.numel()>this._sizeThreshold?!1:this._isElementwise(t)}_isElementwise(t){if(t.numOperands===0||t.numResults===0)return!1;let s=t.getResult(0).type.shape;for(let n=0;n<t.numOperands;n++){let r=t.getOperand(n).type;if(!r||!r.shape||r.shape.length!==s.length)return!1;for(let i=0;i<s.length;i++)if(r.shape[i]!==s[i])return!1}return!0}}});var Lp,pv=I(()=>{Co();li();Ci();Bp();Mr();H_();Tp();Lp=class{static{c(this,"JointGraphBuilder")}constructor(t={}){this._rematPolicy=t.rematPolicy||new Gl(t.remat||{}),this._checkpointPolicy=t.checkpointPolicy||null}build(t){if(this._checkpointPolicy)return this._buildCheckpointed(t);let n=ne.compute(t).topologicalOrder;this._assertNoRegionControlFlow(n);let r=t.getReturnOp();if(!r)throw new Error("Forward function has no return op");let i=r.operands,o=t.args,a=i.map(x=>x.type),l=[...t.inputTypes,...a],u=[...t.outputTypes,...t.inputTypes],f=new Fs(`joint_${t.name}`,l,u),h=new bs(f),p=f.args,d=p.slice(0,t.inputTypes.length),m=p.slice(t.inputTypes.length),g=new Map;for(let x=0;x<o.length;x++)g.set(o[x].id,d[x]);for(let x of n){if(x.opName==="return")continue;let A=new Array(x.numOperands);for(let R=0;R<x.numOperands;R++){let v=x.getOperand(R);A[R]=g.get(v.id)||v}let S=x.results.map(R=>R.type),E=h._buildOp(x.opName,A,S,new Map(x.attributes),null);for(let R=0;R<x.numResults;R++)g.set(x.getResult(R).id,E.getResult(R))}let y=i.map(x=>g.get(x.id)),_=this._computeGradReachability(t,n),w=new Xr(h);for(let x=0;x<i.length;x++)w.accumulate(i[x].id,m[x]);Op(n,{accumulator:w,builder:h,needsGrad:_,resolveValue:c(x=>g.get(x.id)||x,"resolveValue")});let b=[];for(let x=0;x<o.length;x++){let A=w.get(o[x].id);if(A)b.push(A);else{let S=h.scalarConstant(0,o[x].type.dtype).getResult(0),E=h.broadcast(S,o[x].type.shape,[]).getResult(0);b.push(E)}}return h.returnOp([...y,...b]),{jointFunc:f,numForwardOutputs:i.length,numGradInputs:b.length}}_computeGradReachability(t,s){let n=new Set,r=t.getReturnOp();for(let i of r.operands)n.add(i.id);for(let i=s.length-1;i>=0;i--){let o=s[i];if(!(o.opName==="return"||!o.results.some(l=>n.has(l.id)))&&Vl(o.opName)&&!j_(o.opName))for(let l=0;l<o.numOperands;l++)n.add(o.getOperand(l).id)}return n}_assertNoRegionControlFlow(t){for(let s of t)if(Ip.has(s.opName))throw new Error(`JointGraphBuilder does not support region control-flow op '${s.opName}'; use BackwardGraphBuilder (separate mode) without a checkpointPolicy, which differentiates scan/if.`)}_buildCheckpointed(t){let n=ne.compute(t).topologicalOrder;this._assertNoRegionControlFlow(n);let r=t.getReturnOp();if(!r)throw new Error("Forward function has no return op");let i=r.operands,o=t.args,a=i.map(A=>A.type),l=[...t.inputTypes,...a],u=[...t.outputTypes,...t.inputTypes],f=new Fs(`joint_${t.name}`,l,u),h=new bs(f),p=f.args,d=p.slice(0,t.inputTypes.length),m=p.slice(t.inputTypes.length),g=new Map;for(let A=0;A<o.length;A++)g.set(o[A].id,d[A]);for(let A of n){if(A.opName==="return")continue;let S=new Array(A.numOperands);for(let v=0;v<A.numOperands;v++){let k=A.getOperand(v);S[v]=g.get(k.id)||k}let E=A.results.map(v=>v.type),R=h._buildOp(A.opName,S,E,new Map(A.attributes),null);for(let v=0;v<A.numResults;v++)g.set(A.getResult(v).id,R.getResult(v))}let y=i.map(A=>g.get(A.id)),_=this._computeGradReachability(t,n),w=this._checkpointPolicy.segment(n,t),b=new Xr(h);for(let A=0;A<i.length;A++)b.accumulate(i[A].id,m[A]);for(let A=w.length-1;A>=0;A--){let S=w[A],E=new Map;for(let R of S.ops){let v=new Array(R.numOperands);for(let N=0;N<R.numOperands;N++){let C=R.getOperand(N);v[N]=E.get(C.id)||g.get(C.id)||C}let k=R.results.map(N=>N.type),B=h._buildOp(R.opName,v,k,new Map(R.attributes),null);for(let N=0;N<R.numResults;N++)E.set(R.getResult(N).id,B.getResult(N))}Op(S.ops,{accumulator:b,builder:h,needsGrad:_,resolveValue:c(R=>E.get(R.id)||g.get(R.id)||R,"resolveValue")})}let x=[];for(let A=0;A<o.length;A++){let S=b.get(o[A].id);if(S)x.push(S);else{let E=h.scalarConstant(0,o[A].type.dtype).getResult(0),R=h.broadcast(E,o[A].type.shape,[]).getResult(0);x.push(R)}}return h.returnOp([...y,...x]),{jointFunc:f,numForwardOutputs:i.length,numGradInputs:x.length}}}});var P1=I(()=>{});function D1(e,t){let s=e.gradOutputs[0],[n,r]=e.operands,i=e.builder.scalarConstant(0,n.type.dtype).getResult(0),o=e.builder.broadcast(i,n.type.shape,[]).getResult(0),a=e.builder.compare(n,r,t).getResult(0),l=e.builder.select(a,s,o).getResult(0),u=e.builder.select(a,o,s).getResult(0);return[l,u]}function F1(e){let t=e.gradOutputs[0],[s,n]=e.operands,r=e.builder.scalarConstant(0,n.type.dtype).getResult(0),i=e.builder.broadcast(r,t.type.shape,[]).getResult(0),o=e.builder.where(s,t,i).getResult(0),a=e.builder.where(s,i,t).getResult(0);return[null,o,a]}var $1=I(()=>{Mr();lt("add",e=>{let t=e.gradOutputs[0];return[t,t]});lt("sub",e=>{let t=e.gradOutputs[0],s=e.builder.neg(t).getResult(0);return[t,s]});c(D1,"_minMaxVJP");lt("maximum",e=>D1(e,"ge"));lt("minimum",e=>D1(e,"le"));lt("clamp",e=>{let t=e.gradOutputs[0],[s,n,r]=e.operands,i=e.builder.scalarConstant(0,n.type.dtype).getResult(0),o=e.builder.broadcast(i,n.type.shape,[]).getResult(0),a=e.builder.compare(n,s,"ge").getResult(0),l=e.builder.where(a,t,o).getResult(0),u=e.builder.compare(n,r,"le").getResult(0);return[null,e.builder.where(u,l,o).getResult(0),null]});c(F1,"_whereVJP");lt("where",F1);lt("select",F1);lt("mul",e=>{let t=e.gradOutputs[0],[s,n]=e.operands,r=e.builder.mul(t,n).getResult(0),i=e.builder.mul(t,s).getResult(0);return[r,i]});lt("div",e=>{let t=e.gradOutputs[0],[s,n]=e.operands,r=e.builder.div(t,n).getResult(0),i=e.builder.mul(n,n).getResult(0),o=e.builder.neg(s).getResult(0),a=e.builder.mul(t,o).getResult(0),l=e.builder.div(a,i).getResult(0);return[r,l]});lt("neg",e=>{let t=e.gradOutputs[0];return[e.builder.neg(t).getResult(0)]});lt("pow",e=>{let t=e.gradOutputs[0],[s,n]=e.operands,r=e.builder.scalarConstant(1,s.type.dtype).getResult(0),i=e.builder.broadcast(r,s.type.shape,[]).getResult(0),o=e.builder.sub(n,i).getResult(0),a=e.builder.pow(s,o).getResult(0),l=e.builder.mul(n,a).getResult(0),u=e.builder.mul(t,l).getResult(0),f=e.builder.pow(s,n).getResult(0),h=e.builder.log(s).getResult(0),p=e.builder.mul(f,h).getResult(0),d=e.builder.mul(t,p).getResult(0);return[u,d]})});var z1=I(()=>{Mr();lt("exp",e=>{let t=e.gradOutputs[0],s=e.results[0];return[e.builder.mul(t,s).getResult(0)]});lt("log",e=>{let t=e.gradOutputs[0],[s]=e.operands;return[e.builder.div(t,s).getResult(0)]});lt("sqrt",e=>{let t=e.gradOutputs[0],s=e.results[0],n=e.builder.scalarConstant(2,s.type.dtype).getResult(0),r=e.builder.broadcast(n,s.type.shape,[]).getResult(0),i=e.builder.mul(r,s).getResult(0);return[e.builder.div(t,i).getResult(0)]});lt("tanh",e=>{let t=e.gradOutputs[0],s=e.results[0],n=e.builder.mul(s,s).getResult(0),r=e.builder.scalarConstant(1,s.type.dtype).getResult(0),i=e.builder.broadcast(r,s.type.shape,[]).getResult(0),o=e.builder.sub(i,n).getResult(0);return[e.builder.mul(t,o).getResult(0)]});lt("sigmoid",e=>{let t=e.gradOutputs[0],s=e.results[0],n=e.builder.scalarConstant(1,s.type.dtype).getResult(0),r=e.builder.broadcast(n,s.type.shape,[]).getResult(0),i=e.builder.sub(r,s).getResult(0),o=e.builder.mul(s,i).getResult(0);return[e.builder.mul(t,o).getResult(0)]});lt("relu",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=e.builder.scalarConstant(0,s.type.dtype).getResult(0),r=e.builder.broadcast(n,s.type.shape,[]).getResult(0),i=e.builder.compare(s,r,"gt").getResult(0);return[e.builder.select(i,t,r).getResult(0)]});lt("gelu",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.dtype,r=s.type.shape,i=e.builder.broadcast(e.builder.scalarConstant(1.702,n).getResult(0),r,[]).getResult(0),o=e.builder.broadcast(e.builder.scalarConstant(1,n).getResult(0),r,[]).getResult(0),a=e.builder.mul(i,s).getResult(0),l=e.builder.sigmoid(a).getResult(0),u=e.builder.sub(o,l).getResult(0),f=e.builder.mul(a,u).getResult(0),h=e.builder.add(o,f).getResult(0),p=e.builder.mul(l,h).getResult(0);return[e.builder.mul(t,p).getResult(0)]});lt("silu",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.dtype,r=s.type.shape,i=e.builder.sigmoid(s).getResult(0),o=e.builder.scalarConstant(1,n).getResult(0),a=e.builder.broadcast(o,r,[]).getResult(0),l=e.builder.sub(a,i).getResult(0),u=e.builder.mul(s,l).getResult(0),f=e.builder.add(i,e.builder.mul(i,u).getResult(0)).getResult(0);return[e.builder.mul(t,f).getResult(0)]});lt("sin",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=e.builder.cos(s).getResult(0);return[e.builder.mul(t,n).getResult(0)]});lt("cos",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=e.builder.sin(s).getResult(0),r=e.builder.neg(n).getResult(0);return[e.builder.mul(t,r).getResult(0)]});lt("abs",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=e.builder.sign(s).getResult(0);return[e.builder.mul(t,n).getResult(0)]});lt("erf",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.dtype,r=s.type.shape,i=e.builder.broadcast(e.builder.scalarConstant(2/Math.sqrt(Math.PI),n).getResult(0),r,[]).getResult(0),o=e.builder.mul(s,s).getResult(0),a=e.builder.neg(o).getResult(0),l=e.builder.exp(a).getResult(0),u=e.builder.mul(i,l).getResult(0);return[e.builder.mul(t,u).getResult(0)]});lt("log2",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.dtype,r=s.type.shape,i=e.builder.broadcast(e.builder.scalarConstant(Math.LN2,n).getResult(0),r,[]).getResult(0),o=e.builder.mul(s,i).getResult(0);return[e.builder.div(t,o).getResult(0)]});lt("log10",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.dtype,r=s.type.shape,i=e.builder.broadcast(e.builder.scalarConstant(Math.LN10,n).getResult(0),r,[]).getResult(0),o=e.builder.mul(s,i).getResult(0);return[e.builder.div(t,o).getResult(0)]});lt("exp2",e=>{let t=e.gradOutputs[0],s=e.results[0],n=s.type.dtype,r=s.type.shape,i=e.builder.broadcast(e.builder.scalarConstant(Math.LN2,n).getResult(0),r,[]).getResult(0),o=e.builder.mul(s,i).getResult(0);return[e.builder.mul(t,o).getResult(0)]});lt("square",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.dtype,r=s.type.shape,i=e.builder.broadcast(e.builder.scalarConstant(2,n).getResult(0),r,[]).getResult(0),o=e.builder.mul(i,s).getResult(0);return[e.builder.mul(t,o).getResult(0)]});lt("reciprocal",e=>{let t=e.gradOutputs[0],s=e.results[0],n=e.builder.mul(s,s).getResult(0),r=e.builder.neg(n).getResult(0);return[e.builder.mul(t,r).getResult(0)]});lt("rsqrt",e=>{let t=e.gradOutputs[0],s=e.results[0],n=e.builder.mul(e.builder.mul(s,s).getResult(0),s).getResult(0),r=e.builder.broadcast(e.builder.scalarConstant(-.5,s.type.dtype).getResult(0),s.type.shape,[]).getResult(0),i=e.builder.mul(r,n).getResult(0);return[e.builder.mul(t,i).getResult(0)]})});var U1=I(()=>{Mr();lt("dot",e=>{let t=e.gradOutputs[0],[s,n]=e.operands,r=e.op.getAttr("lhs_contracting"),i=e.op.getAttr("rhs_contracting"),o=e.op.getAttr("lhs_batch")||[],a=e.op.getAttr("rhs_batch")||[],l=s.type.rank,u=n.type.rank,f=t.type.rank,h=[];for(let _=0;_<l;_++)!r.includes(_)&&!o.includes(_)&&h.push(_);let p=[];for(let _=0;_<u;_++)!i.includes(_)&&!a.includes(_)&&p.push(_);let d=[];for(let _=o.length;_<o.length+p.length;_++)d.push(_+h.length);let m=[];for(let _=o.length;_<o.length+h.length;_++)m.push(_);let g=e.builder.dot(t,n,d,p,Array.from({length:o.length},(_,w)=>w),a).getResult(0),y=e.builder.dot(s,t,h,m,o,Array.from({length:o.length},(_,w)=>w)).getResult(0);return[g,y]});lt("matmul",e=>{let t=e.gradOutputs[0],[s,n]=e.operands,r=c(u=>{let f=Array.from({length:u},(h,p)=>p);return f[u-2]=u-1,f[u-1]=u-2,f},"swapLastTwo"),i=e.builder.transpose(n,r(n.type.rank)).getResult(0),o=e.builder.matmul(t,i).getResult(0),a=e.builder.transpose(s,r(s.type.rank)).getResult(0),l=e.builder.matmul(a,t).getResult(0);return[o,l]});lt("conv",e=>{let t=e.gradOutputs[0],[s,n]=e.operands,r=e.builder,i=e.op.getAttr("strides"),o=e.op.getAttr("padding"),a=e.op.getAttr("dilation")||i.map(()=>1),l=e.op.getAttr("groups")||1,u=e.op.getAttr("input_layout"),f=e.op.getAttr("kernel_layout");if(!(i.length===2&&i.every(R=>R===1)&&a.every(R=>R===1)&&l===1&&u==="NCHW"&&f==="OIHW"))throw new Error("conv VJP supports only 2D stride-1 dilation-1 groups-1 NCHW/OIHW conv");let p=n.type.shape,d=p[2],m=p[3],[g,y]=o,_=r.reverse(r.transpose(n,[1,0,2,3]).getResult(0),[2,3]).getResult(0),w=[[d-1-g[0],d-1-g[1]],[m-1-y[0],m-1-y[1]]],b=r.conv(t,_,[1,1],w).getResult(0),x=r.transpose(s,[1,0,2,3]).getResult(0),A=r.transpose(t,[1,0,2,3]).getResult(0),S=r.conv(x,A,[1,1],[g,y]).getResult(0),E=r.transpose(S,[1,0,2,3]).getResult(0);return[b,E]})});var V1=I(()=>{Mr();lt("reduce",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=e.op.getAttr("reduce_type"),r=e.op.getAttr("dimensions"),i=s.type.shape,o=s.type.dtype,a=new Set(r),l=i.map((h,p)=>a.has(p)?1:h),u=i.map((h,p)=>p),f=e.builder.reshape(t,l).getResult(0);if(n==="sum")return[e.builder.broadcast(f,i,u).getResult(0),null];if(n==="mean"){let h=1;for(let y=0;y<i.length;y++)a.has(y)&&(h*=i[y]);let p=e.builder.broadcast(f,i,u).getResult(0),d=e.builder.scalarConstant(h,o).getResult(0),m=e.builder.broadcast(d,i,[]).getResult(0);return[e.builder.div(p,m).getResult(0),null]}if(n==="max"||n==="min"){let h=e.builder.reshape(e.results[0],l).getResult(0),p=e.builder.broadcast(h,i,u).getResult(0),d=e.builder.compare(s,p,"eq").getResult(0),m=e.builder.convert(d,o).getResult(0),g=e.builder.broadcast(f,i,u).getResult(0);return[e.builder.mul(g,m).getResult(0),null]}throw n==="prod"?new Error("reduce VJP for reduce_type='prod' is not implemented; provide a gradient rule or avoid differentiating reduce_prod"):new Error(`reduce VJP: unsupported reduce_type '${n}' on the gradient path (would silently drop the gradient)`)})});var G1=I(()=>{Mr();lt("reshape",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.shape;return[e.builder.reshape(t,n).getResult(0)]});lt("reverse",e=>[e.builder.reverse(e.gradOutputs[0],e.op.getAttr("dimensions")).getResult(0)]);lt("transpose",e=>{let t=e.gradOutputs[0],s=e.op.getAttr("permutation"),n=new Array(s.length);for(let r=0;r<s.length;r++)n[s[r]]=r;return[e.builder.transpose(t,n).getResult(0)]});lt("broadcast_in_dim",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=e.op.getAttr("broadcast_dimensions"),r=s.type.shape,i=e.results[0].type.shape,o=s.type.dtype,a=[];for(let h=0;h<i.length;h++)if(!n.includes(h))a.push(h);else{let p=n.indexOf(h);r[p]===1&&i[h]!==1&&a.push(h)}if(a.length===0)return[t];let l=e.builder.scalarConstant(0,o).getResult(0),u=e.builder.reduce(t,l,a,"sum").getResult(0),f=u.type.shape;return f.length!==r.length||!f.every((h,p)=>h===r[p])?[e.builder.reshape(u,r).getResult(0)]:[u]});lt("slice",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=e.op.getAttr("starts"),r=s.type.shape,i=t.type.shape,o=e.op.getAttr("strides")||r.map(()=>1),a=s.type.dtype,l=[...n],u=new Array(r.length),f=new Array(r.length);for(let p=0;p<r.length;p++)f[p]=o[p]-1,u[p]=r[p]-n[p]-(i[p]-1)*o[p]-1;let h=e.builder.scalarConstant(0,a).getResult(0);return[e.builder.pad(t,h,l,u,f).getResult(0)]});lt("concat",e=>{let t=e.gradOutputs[0],s=e.op.getAttr("dimension"),n=e.operands,r=[],i=0;for(let o=0;o<n.length;o++){let a=n[o].type.shape,l=new Array(a.length).fill(0),u=[...t.type.shape];l[s]=i,u[s]=i+a[s],r.push(e.builder.slice(t,l,u).getResult(0)),i+=a[s]}return r});lt("gather",e=>{let t=e.gradOutputs[0],[s,n]=e.operands,r=e.builder.scalarConstant(0,s.type.dtype).getResult(0),i=e.builder.broadcast(r,s.type.shape,[]).getResult(0);return[e.builder.scatterAdd(i,n,t,{updateWindowDims:e.op.getAttr("offset_dims"),insertedWindowDims:e.op.getAttr("collapsed_slice_dims"),scatterDimsToOperandDims:e.op.getAttr("start_index_map"),indexVectorDim:e.op.getAttr("index_vector_dim")}).getResult(0),null]});lt("scatter",e=>{let t=e.gradOutputs[0],[s,n]=e.operands,r=e.op.getAttr("inserted_window_dims"),i=s.type.shape.map((a,l)=>r.includes(l)?1:a),o=e.builder.gather(t,n,{offsetDims:e.op.getAttr("update_window_dims"),collapsedSliceDims:r,startIndexMap:e.op.getAttr("scatter_dims_to_operand_dims"),indexVectorDim:e.op.getAttr("index_vector_dim"),sliceSizes:i}).getResult(0);return[t,null,o]});lt("pad",e=>{let t=e.gradOutputs[0],s=e.op.getAttr("low"),n=e.op.getAttr("high"),[r]=e.operands,i=r.type.shape,o=[...s],a=new Array(i.length);for(let l=0;l<i.length;l++)a[l]=s[l]+i[l];return[e.builder.slice(t,o,a).getResult(0),null]})});var q1=I(()=>{Mr();lt("softmax",e=>{let t=e.gradOutputs[0],s=e.results[0],n=e.op.getAttr("axis"),r=s.type.dtype,i=s.type.shape,o=e.builder.mul(t,s).getResult(0),a=e.builder.scalarConstant(0,r).getResult(0),l=e.builder.reduce(o,a,[n],"sum").getResult(0),u=[];for(let p=0;p<i.length;p++)p!==n&&u.push(p);let f=e.builder.broadcast(l,i,u).getResult(0),h=e.builder.sub(t,f).getResult(0);return[e.builder.mul(s,h).getResult(0)]});lt("log_softmax",e=>{let t=e.gradOutputs[0],s=e.results[0],[n]=e.operands,r=e.op.getAttr("axis"),i=s.type.dtype,o=s.type.shape,a=e.builder.exp(s).getResult(0),l=e.builder.scalarConstant(0,i).getResult(0),u=e.builder.reduce(t,l,[r],"sum").getResult(0),f=[];for(let d=0;d<o.length;d++)d!==r&&f.push(d);let h=e.builder.broadcast(u,o,f).getResult(0),p=e.builder.mul(a,h).getResult(0);return[e.builder.sub(t,p).getResult(0)]});lt("layer_norm",e=>{let t=e.gradOutputs[0],[s,n]=e.operands,r=e.op.getAttr("axis"),i=e.op.getAttr("epsilon"),o=s.type.dtype,a=s.type.shape,l=[r<0?a.length+r:r],u=a[l[0]],f=[];for(let mt=0;mt<a.length;mt++)l.includes(mt)||f.push(mt);let h=e.builder.scalarConstant(0,o).getResult(0),p=e.builder.reduce(s,h,l,"mean").getResult(0),d=e.builder.broadcast(p,a,f).getResult(0),m=e.builder.sub(s,d).getResult(0),g=e.builder.mul(m,m).getResult(0),y=e.builder.reduce(g,h,l,"mean").getResult(0),_=e.builder.scalarConstant(i,o).getResult(0),w=e.builder.broadcast(_,y.type.shape,[]).getResult(0),b=e.builder.add(y,w).getResult(0),x=e.builder.rsqrt(b).getResult(0),A=e.builder.broadcast(x,a,f).getResult(0),S=[];for(let mt=0;mt<a.length;mt++)l.includes(mt)&&S.push(mt);let E=e.builder.broadcast(n,a,S).getResult(0),R=e.builder.mul(t,E).getResult(0),v=e.builder.scalarConstant(u,o).getResult(0),k=e.builder.broadcast(v,a,[]).getResult(0),B=e.builder.mul(k,R).getResult(0),N=e.builder.reduce(R,h,l,"sum").getResult(0),C=e.builder.broadcast(N,a,f).getResult(0),O=e.builder.mul(m,A).getResult(0),F=e.builder.mul(R,O).getResult(0),P=e.builder.reduce(F,h,l,"sum").getResult(0),z=e.builder.broadcast(P,a,f).getResult(0),q=e.builder.mul(O,z).getResult(0),U=e.builder.sub(B,e.builder.add(C,q).getResult(0)).getResult(0),D=e.builder.div(A,k).getResult(0),G=e.builder.mul(D,U).getResult(0),At=e.builder.reduce(e.builder.mul(t,O).getResult(0),h,f,"sum").getResult(0),pt=e.builder.reduce(t,h,f,"sum").getResult(0);return[G,At,pt]});lt("scaled_dot_product_attention",e=>{let t=e.gradOutputs[0],[s,n,r]=e.operands,i=e.op.getAttr("scale");if(e.op.getAttr("causal"))throw new Error("causal scaled_dot_product_attention VJP not supported");let o=e.builder,a=s.type.dtype,l=s.type.rank,u=[];for(let k=0;k<l;k++)u.push(k);u[l-2]=l-1,u[l-1]=l-2;let f=c(k=>o.transpose(k,u).getResult(0),"lastT"),h=c(k=>o.broadcast(o.scalarConstant(i,a).getResult(0),k,[]).getResult(0),"scaleBrTo"),p=o.matmul(s,f(n)).getResult(0),d=o.mul(p,h(p.type.shape)).getResult(0),m=o.softmax(d,l-1).getResult(0),g=o.matmul(f(m),t).getResult(0),y=o.matmul(t,f(r)).getResult(0),_=o.scalarConstant(0,a).getResult(0),w=o.mul(y,m).getResult(0),b=o.reduce(w,_,[l-1],"sum").getResult(0),x=[];for(let k=0;k<l-1;k++)x.push(k);let A=o.broadcast(b,m.type.shape,x).getResult(0),S=o.mul(m,o.sub(y,A).getResult(0)).getResult(0),E=o.mul(S,h(S.type.shape)).getResult(0),R=o.matmul(E,n).getResult(0),v=o.matmul(f(E),s).getResult(0);return[R,v,g]});lt("pool2d",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=e.builder,r=e.op.getAttr("pool_type"),i=e.op.getAttr("kernel_size"),o=e.op.getAttr("strides"),a=e.op.getAttr("padding"),l=e.op.getAttr("layout")||"NCHW",u=a.every(E=>E[0]===0&&E[1]===0),f=o[0]===i[0]&&o[1]===i[1];if(l!=="NCHW"||!u||!f||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,y]=i,_=s.type.shape,w=c(E=>n.reshape(n.broadcast(E,[h,p,d,g,m,y],[0,1,2,4]).getResult(0),_).getResult(0),"upsample"),b=w(t);if(r==="avg"){let E=n.broadcast(n.scalarConstant(g*y,s.type.dtype).getResult(0),_,[]).getResult(0);return[n.div(b,E).getResult(0)]}let x=w(e.results[0]),A=n.compare(s,x,"eq").getResult(0),S=n.broadcast(n.scalarConstant(0,s.type.dtype).getResult(0),_,[]).getResult(0);return[n.select(A,b,S).getResult(0)]});lt("batch_norm",e=>{let t=e.gradOutputs[0],[s,n,,r,i]=e.operands,o=e.op.getAttr("axis"),a=e.op.getAttr("epsilon"),l=s.type.dtype,u=s.type.shape,f=e.builder,h=[];for(let C=0;C<u.length;C++)C!==o&&h.push(C);let p=f.scalarConstant(0,l).getResult(0),d=f.broadcast(f.scalarConstant(a,l).getResult(0),i.type.shape,[]).getResult(0),m=f.rsqrt(f.add(i,d).getResult(0)).getResult(0),g=f.broadcast(m,u,[o]).getResult(0),y=f.broadcast(r,u,[o]).getResult(0),_=f.broadcast(n,u,[o]).getResult(0),w=f.sub(s,y).getResult(0),b=f.mul(w,g).getResult(0),x=f.mul(t,_).getResult(0),A=f.mul(x,g).getResult(0),S=f.reduce(f.mul(t,b).getResult(0),p,h,"sum").getResult(0),E=f.reduce(t,p,h,"sum").getResult(0),R=f.neg(f.reduce(A,p,h,"sum").getResult(0)).getResult(0),v=f.mul(f.mul(g,g).getResult(0),g).getResult(0),k=f.broadcast(f.scalarConstant(-.5,l).getResult(0),u,[]).getResult(0),B=f.mul(f.mul(f.mul(x,w).getResult(0),v).getResult(0),k).getResult(0),N=f.reduce(B,p,h,"sum").getResult(0);return[A,S,E,R,N]});lt("elu",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=e.results[0],r=s.type.dtype,i=s.type.shape,o=e.op.getAttr("alpha")??1,a=e.builder.broadcast(e.builder.scalarConstant(0,r).getResult(0),i,[]).getResult(0),l=e.builder.broadcast(e.builder.scalarConstant(1,r).getResult(0),i,[]).getResult(0),u=e.builder.broadcast(e.builder.scalarConstant(o,r).getResult(0),i,[]).getResult(0),f=e.builder.compare(s,a,"gt").getResult(0),h=e.builder.add(n,u).getResult(0),p=e.builder.select(f,l,h).getResult(0);return[e.builder.mul(t,p).getResult(0)]});lt("leaky_relu",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.dtype,r=s.type.shape,i=e.op.getAttr("negative_slope")??.01,o=e.builder.broadcast(e.builder.scalarConstant(0,n).getResult(0),r,[]).getResult(0),a=e.builder.broadcast(e.builder.scalarConstant(1,n).getResult(0),r,[]).getResult(0),l=e.builder.broadcast(e.builder.scalarConstant(i,n).getResult(0),r,[]).getResult(0),u=e.builder.compare(s,o,"gt").getResult(0),f=e.builder.select(u,a,l).getResult(0);return[e.builder.mul(t,f).getResult(0)]});lt("celu",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.dtype,r=s.type.shape,i=e.op.getAttr("alpha")??1,o=e.builder.broadcast(e.builder.scalarConstant(0,n).getResult(0),r,[]).getResult(0),a=e.builder.broadcast(e.builder.scalarConstant(1,n).getResult(0),r,[]).getResult(0),l=e.builder.compare(s,o,"gt").getResult(0),u=e.builder.broadcast(e.builder.scalarConstant(i,n).getResult(0),r,[]).getResult(0),f=e.builder.div(s,u).getResult(0),h=e.builder.exp(f).getResult(0),p=e.builder.select(l,a,h).getResult(0);return[e.builder.mul(t,p).getResult(0)]});lt("selu",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.dtype,r=s.type.shape,i=1.0507009873554805,o=1.6732632423543772,a=e.builder.broadcast(e.builder.scalarConstant(0,n).getResult(0),r,[]).getResult(0),l=e.builder.broadcast(e.builder.scalarConstant(i,n).getResult(0),r,[]).getResult(0),u=e.builder.compare(s,a,"gt").getResult(0),f=e.builder.broadcast(e.builder.scalarConstant(o,n).getResult(0),r,[]).getResult(0),h=e.builder.exp(s).getResult(0),p=e.builder.mul(f,h).getResult(0),d=e.builder.select(u,l,e.builder.mul(l,p).getResult(0)).getResult(0);return[e.builder.mul(t,d).getResult(0)]});lt("hardswish",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.dtype,r=s.type.shape,i=e.builder.broadcast(e.builder.scalarConstant(-3,n).getResult(0),r,[]).getResult(0),o=e.builder.broadcast(e.builder.scalarConstant(3,n).getResult(0),r,[]).getResult(0),a=e.builder.broadcast(e.builder.scalarConstant(0,n).getResult(0),r,[]).getResult(0),l=e.builder.broadcast(e.builder.scalarConstant(1,n).getResult(0),r,[]).getResult(0),u=e.builder.broadcast(e.builder.scalarConstant(2,n).getResult(0),r,[]).getResult(0),f=e.builder.broadcast(e.builder.scalarConstant(6,n).getResult(0),r,[]).getResult(0),h=e.builder.compare(s,i,"le").getResult(0),p=e.builder.compare(s,o,"ge").getResult(0),d=e.builder.add(e.builder.mul(u,s).getResult(0),o).getResult(0),m=e.builder.div(d,f).getResult(0),g=e.builder.select(h,a,e.builder.select(p,l,m).getResult(0)).getResult(0);return[e.builder.mul(t,g).getResult(0)]});lt("hardsigmoid",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.dtype,r=s.type.shape,i=e.builder.broadcast(e.builder.scalarConstant(-3,n).getResult(0),r,[]).getResult(0),o=e.builder.broadcast(e.builder.scalarConstant(3,n).getResult(0),r,[]).getResult(0),a=e.builder.broadcast(e.builder.scalarConstant(0,n).getResult(0),r,[]).getResult(0),l=e.builder.broadcast(e.builder.scalarConstant(1/6,n).getResult(0),r,[]).getResult(0),u=e.builder.compare(s,i,"le").getResult(0),f=e.builder.compare(s,o,"ge").getResult(0),h=e.builder.select(u,a,e.builder.select(f,a,l).getResult(0)).getResult(0);return[e.builder.mul(t,h).getResult(0)]});lt("embedding",e=>{let t=e.gradOutputs[0],[s,n]=e.operands,r=s.type.shape,i=s.type.dtype,o=n.type.rank,a=n.type.shape,l=e.builder.scalarConstant(0,i).getResult(0),u=e.builder.broadcast(l,r,[]).getResult(0);return[e.builder.scatter(u,n,t,{updateWindowDims:[o],insertedWindowDims:[0],scatterDimsToOperandDims:[0],indexVectorDim:o}).getResult(0),null]})});var j1=I(()=>{Mr();uv("stop_gradient");lt("stop_gradient",()=>[null]);for(let e of["compare","logical_not","argmax","argmin","iota"])uv(e)});var W1=I(()=>{Mr();Bp();Tp();pv();H_();P1();$1();z1();U1();V1();G1();q1();j1()});var K1={};Xt(K1,{compileWithBackward:()=>dv});function dv(e,t,s={}){let n=s.target??nr(),r=s.mode||"separate",i=s.rematPolicy||new Gl(s.remat||{}),o={target:n,verify:!1,...s,backward:void 0,mode:void 0,rematPolicy:void 0,remat:void 0},a=s.dynamic_shapes||null,l=[],u=null,f=null,h=null;function p(v){let k=U_((...N)=>e.forward(...N),v,{name:e.constructor.name||"compiled",dynamicShapes:a}),B=c(N=>{let C=N.graph.functions().next().value,O=r==="joint"?m(C,N,i):d(C,N,i);return O.shapeEnv=N.shapeEnv,O.outputSymShapes=N.outputSymShapes,O},"finish");return k&&typeof k.then=="function"?k.then(B):B(k)}c(p,"_compile");function d(v,k,B){let N=new Mp({rematPolicy:B}),{backwardFunc:C,savedValues:O,gradInputIndices:F}=N.build(v),P=v.getReturnOp(),z=[...P.operands],q=z.length,U=new Map(v.args.map((dt,jt)=>[dt.id,jt])),D=new Map(z.map((dt,jt)=>[dt.id,jt])),G=[],At=new Map;for(let dt of O)U.has(dt.id)||D.has(dt.id)||At.has(dt.id)||(At.set(dt.id,q+G.length),G.push(dt));G.length>0&&(P.erase(),new bs(v).returnOp([...z,...G]),v.outputTypes=Object.freeze([...z.map(dt=>dt.type),...G.map(dt=>dt.type)]));let pt=O.map(dt=>U.has(dt.id)?{kind:"arg",index:U.get(dt.id)}:D.has(dt.id)?{kind:"output",index:D.get(dt.id)}:{kind:"output",index:At.get(dt.id)}),mt=new cn("forward");mt.addFunction(v);let $=new _o(o).compile(mt),K=new cn("backward");K.addFunction(C);let _t=new _o(o).compile(K);return{mode:"separate",fwdResult:$,bwdResult:_t,forwardFunc:v,backwardFunc:C,savedValues:O,savedSources:pt,numRealOutputs:q,gradInputIndices:F,capturedParams:k.capturedParams,numUserInputs:k.numUserInputs,outputTypes:k.outputTypes}}c(d,"_compileSeparate");function m(v,k,B){let N=new Lp({rematPolicy:B}),{jointFunc:C,numForwardOutputs:O,numGradInputs:F}=N.build(v),P=new cn("joint");return P.addFunction(C),{mode:"joint",result:new _o(o).compile(P),jointFunc:C,numForwardOutputs:O,numGradInputs:F,capturedParams:k.capturedParams,numUserInputs:k.numUserInputs,outputTypes:k.outputTypes,inputTypes:v.inputTypes}}c(m,"_compileJoint");function g(v,k){return v.outputSymShapes&&v.shapeEnv?v.shapeEnv.resolveSymbolicShape(v.outputSymShapes[k]):v.outputTypes[k].shape}c(g,"_resolveOutputShape");function y(v,k,B){let N=v.module||v;return N.executionPlan?N.runPlanAsync(N.executionPlan,B):v.isAsync(k)?v.runAsync(k,...B):(v.run(k,...B),null)}c(y,"_runK");function _(v,k){let N=v.fwdResult.listKernels()[0],C=k.length>0?k[0].device:"cpu",O=k.map(mt=>Nn(mt)),P=v.capturedParams.map(mt=>Nn(mt)),z=v.forwardFunc.outputTypes,q=v.numRealOutputs,U=new Array(z.length),D=new Array(z.length);for(let mt=0;mt<z.length;mt++){let $=mt<q?g(v,mt):z[mt].shape,K=z[mt].dtype,_t=gs($),dt=me(K);U[mt]=new dt(Math.max(_t,1)),D[mt]=$}let G=[...O,...P,...U],At=c(()=>({results:q===1?ae(U[0],D[0],z[0].dtype,C):Array.from({length:q},($,K)=>ae(U[K],D[K],z[K].dtype,C)),inputArrays:O,paramArrays:P,outputArrays:U,device:C}),"build"),pt=y(v.fwdResult,N,G);return pt?pt.then(At):At()}c(_,"_executeSeparateForward");function w(v,k,B){let C=v.bwdResult.listKernels()[0],O=k.map(K=>Nn(K)),F=v.savedValues,P=v.savedSources,z=[...B.inputArrays,...B.paramArrays],q=new Array(F.length);for(let K=0;K<F.length;K++){let _t=P[K];q[K]=_t.kind==="arg"?z[_t.index]:B.outputArrays[_t.index]}let U=v.backwardFunc,D=U.outputTypes.length,G=new Array(D),At=new Array(D);for(let K=0;K<D;K++){let _t=U.outputTypes[K].shape,dt=U.outputTypes[K].dtype,jt=gs(_t),Pn=me(dt);G[K]=new Pn(Math.max(jt,1)),At[K]=_t}let pt=[...O,...q,...G],mt=c(()=>G.map((K,_t)=>ae(K,At[_t],U.outputTypes[_t].dtype,B.device)),"build"),$=y(v.bwdResult,C,pt);return $?$.then(mt):mt()}c(w,"_executeSeparateBackward");function b(v){for(let k=0;k<l.length;k++){let B=l[k];B.shapeEnv.bindInputShapes(v);let{passed:N}=B.shapeEnv.evaluateGuards();if(N)return B}return null}c(b,"_findCachedEntry");function x(v,k){if(f=v,v.mode==="joint")return E(v,k);let B=_(v,k);return B&&B.then?B.then(N=>(u=N,N.results)):(u=B,B.results)}c(x,"_forwardWith");function A(v){let k=b(v);if(k)return x(k,v);let B=p(v);return B&&typeof B.then=="function"?B.then(N=>(l.push(N),N.shapeEnv.bindInputShapes(v),x(N,v))):(l.push(B),B.shapeEnv.bindInputShapes(v),x(B,v))}c(A,"_runForward");function S(...v){return h?h.then(()=>A(v)):A(v)}c(S,"compiledForward");function E(v,k){let N=v.result.listKernels()[0],C=k.length>0?k[0].device:"cpu",O=k.map($=>Nn($)),P=v.capturedParams.map($=>Nn($)),z=v.jointFunc,q=z.outputTypes.length,U=new Array(q),D=new Array(q);for(let $=0;$<q;$++){let K=z.outputTypes[$].shape,_t=z.outputTypes[$].dtype,dt=gs(K),jt=me(_t);U[$]=new jt(Math.max(dt,1)),D[$]=K}let G=new Array(v.numForwardOutputs);for(let $=0;$<v.numForwardOutputs;$++){let K=v.outputTypes[$],_t=gs(K.shape),dt=me(K.dtype);G[$]=new dt(Math.max(_t,1))}u={inputArrays:O,paramArrays:P,gradOutputArrays:G,outputArrays:U,outputShapes:D,device:C,compiled:v};let At=[...O,...P,...G,...U],pt=c(()=>{let $=[];for(let K=0;K<v.numForwardOutputs;K++)$.push(ae(U[K],D[K],z.outputTypes[K].dtype,C));return $.length===1?$[0]:$},"build"),mt=y(v.result,N,At);return mt?mt.then(pt):pt()}c(E,"_executeJointForward"),S.backward=function(...v){if(!f||!u)throw new Error("Must run forward before backward");return f.mode==="joint"?R(f,v,u):w(f,v,u)};function R(v,k,B){let{inputArrays:N,paramArrays:C,outputArrays:O,outputShapes:F,device:P}=B,z=k.map($=>Nn($));for(let $=0;$<z.length;$++)B.gradOutputArrays[$].set(z[$]);let q=v.jointFunc,U=q.outputTypes.length,D=new Array(U),G=new Array(U);for(let $=0;$<U;$++){let K=q.outputTypes[$].shape,_t=q.outputTypes[$].dtype,dt=gs(K),jt=me(_t);D[$]=new jt(Math.max(dt,1)),G[$]=K}let At=[...N,...C,...z,...D],pt=c(()=>{let $=[];for(let K=v.numForwardOutputs;K<U;K++)$.push(ae(D[K],G[K],q.outputTypes[K].dtype,P));return $},"build"),mt=y(v.result,v.result.listKernels()[0],At);return mt?mt.then(pt):pt()}if(c(R,"_executeJointBackward"),S.original=e,S.backwardGraph=()=>{if(l.length===0)return null;let v=l[0];return v.mode==="joint"?v.jointFunc:v.backwardFunc},S.forwardGraph=()=>{if(l.length===0)return null;let v=l[0];return v.mode==="joint"?v.jointFunc:v.forwardFunc},S.capturedParams=()=>l.length?l[0].capturedParams:[],t){let v=p(t);v&&typeof v.then=="function"?h=v.then(k=>(l.push(k),h=null,k)):l.push(v)}return S}var mv=I(()=>{G_();D_();hi();Ya();Tp();li();pv();H_();W1();Nr();ir();Vr();c(dv,"compileWithBackward")});import MF from"node:fs";var mf={};Xt(mf,{Accuracy:()=>ka,Adam:()=>ca,AdamW:()=>ua,AdaptiveAvgPool2d:()=>gu,AvgPool2d:()=>mu,BCELoss:()=>bu,BatchNorm1d:()=>Il,BatchNorm2d:()=>pu,BatchSampler:()=>ia,CPUTarget:()=>nr,CSVLogger:()=>Aa,CUDATarget:()=>el,Callback:()=>Gs,ConfusionMatrix:()=>Ba,ConsoleLogger:()=>Mi,Conv1d:()=>Jo,Conv2d:()=>Ho,CosineAnnealingLR:()=>pa,CrossEntropyLoss:()=>_u,DataLoader:()=>aa,Dataset:()=>ea,Dropout:()=>pn,ELU:()=>hu,EarlyStopping:()=>wa,Embedding:()=>xu,F:()=>HS,F1Score:()=>Ra,Flatten:()=>Eu,GELU:()=>au,GRU:()=>Au,GRUCell:()=>Ml,GradientAccumulationScheduler:()=>Sa,LRScheduler:()=>fa,LSTM:()=>vu,LSTMCell:()=>Ll,LayerNorm:()=>Br,LeakyReLU:()=>fu,LearningRateMonitor:()=>ba,LightningModule:()=>ya,Linear:()=>ls,LogSoftmax:()=>Qo,Logger:()=>Oi,MSELoss:()=>yu,MapDataset:()=>Bu,MaxPool2d:()=>du,MeanMetric:()=>pf,Metric:()=>tn,MetricCollection:()=>va,ModelCheckpoint:()=>ti,Module:()=>nt,ModuleDict:()=>ku,ModuleList:()=>uo,MultiheadAttention:()=>fo,NLLLoss:()=>wu,Optimizer:()=>zn,Parameter:()=>pe,PositionalEncoding:()=>Ru,Precision:()=>Ea,ProgressCallback:()=>Zr,RandomSampler:()=>ra,ReLU:()=>ou,Recall:()=>Na,ReduceLROnPlateau:()=>da,SGD:()=>la,Sampler:()=>ho,Sequential:()=>Xo,SequentialSampler:()=>na,SiLU:()=>lu,Sigmoid:()=>cu,Softmax:()=>Yo,StepLR:()=>ha,SumMetric:()=>df,Tanh:()=>uu,Tensor:()=>Et,TensorDataset:()=>sa,Timer:()=>xa,Tokenizer:()=>mo,Trainer:()=>_a,Transformer:()=>Nu,TransformerDecoder:()=>Dl,TransformerDecoderLayer:()=>ta,TransformerEncoder:()=>Pl,TransformerEncoderLayer:()=>Zo,Vocab:()=>po,WasmTarget:()=>sl,WebGPUTarget:()=>nl,abs:()=>zo,add:()=>ot,applyCheckpoint:()=>hf,arange:()=>nx,argmax:()=>Mh,argmin:()=>Lh,argsort:()=>hy,cat:()=>Vc,ceil:()=>Bh,clamp:()=>Ch,clipGradNorm_:()=>Ou,clipGradValue_:()=>Mu,clone:()=>Fh,compile:()=>V_,compileWithBackward:()=>dv,cos:()=>Nh,cumsum:()=>uy,data:()=>YS,defaultCollate:()=>oa,div:()=>oe,dot:()=>Dh,empty:()=>is,emptyLike:()=>iI,enableGrad:()=>px,eq:()=>Si,exp:()=>lr,eye:()=>rx,flip:()=>cy,floor:()=>Rh,fromBuffer:()=>Fo,full:()=>Kt,fullLike:()=>lI,gather:()=>Ih,ge:()=>ro,gelu:()=>_l,gt:()=>Wr,index_select:()=>io,init:()=>ap,le:()=>no,lightning:()=>xv,linspace:()=>ox,loadCheckpoint:()=>ff,log:()=>jr,log_softmax:()=>Al,lt:()=>Sl,matmul:()=>qe,max:()=>Kr,maximum:()=>qr,mean:()=>fn,min:()=>Oh,minimum:()=>yl,mul:()=>tt,ne:()=>xl,neg:()=>kn,nn:()=>JS,noGrad:()=>hx,one_hot:()=>Th,ones:()=>je,onesLike:()=>aI,optim:()=>rv,pad:()=>Uo,pow:()=>kr,prod:()=>Ph,randn:()=>Uh,randnLike:()=>cI,randperm:()=>ix,relu:()=>so,roll:()=>ly,rsqrt:()=>kh,scalar:()=>Qm,scan:()=>Su,scatter:()=>ay,scatter_add:()=>Vo,sigmoid:()=>Us,sign:()=>bl,silu:()=>wl,sin:()=>Eh,softmax:()=>Er,sort:()=>fy,sqrt:()=>cr,stack:()=>ur,sub:()=>$t,sum:()=>he,tanh:()=>Fn,tensor:()=>ks,tokenizer:()=>XS,topk:()=>py,trace:()=>ov,where:()=>Xs,zeros:()=>Pt,zerosLike:()=>oI});cb();Qn();Nr();Js();Zi();dl();vn();Vr();ir();function su(e,t){let s=As(e),n=Ys.allocate(0,t,$c),r=new Oe(n,0,e,s,t,$c);return new Et(r)}c(su,"_metaTensor");function Ko(e,t,s){let n=zc(t.shape,s.shape);if(!n)throw new Error(`Incompatible shapes: [${t.shape}] vs [${s.shape}]`);let r=Wm(t.dtype,s.dtype);return su(n,r)}c(Ko,"_metaBinary");function hn(e,t){return su([...t.shape],t.dtype)}c(hn,"_metaUnary");function rp(e,t,s,n){let r=[...t.shape],i=s!=null?Array.isArray(s)?s:[s]:Array.from({length:r.length},(l,u)=>u);if(i.length===r.length||s===void 0)return su(n?r.map(()=>1):[],t.dtype);let o=[],a=new Set(i.map(l=>l<0?r.length+l:l));for(let l=0;l<r.length;l++)a.has(l)?n&&o.push(1):o.push(r[l]);return su(o,t.dtype)}c(rp,"_metaReduction");var yT=Ko,_T=Ko,wT=Ko,bT=Ko,xT=Ko,ST=Ko,AT=Ko,vT=Ko,kT=hn,ET=hn,NT=hn,RT=hn,BT=hn,CT=hn,TT=hn,IT=hn,OT=hn,MT=hn,LT=hn,PT=hn,DT=hn,FT=hn,$T=hn,zT=hn,UT=rp,VT=rp,GT=rp,qT=rp,jT=rp;function WT(e,t,s){let n=Jm(t.shape,s.shape);if(n===null)throw new Error("metaMatmul: unsupported shapes");return su(n,t.dtype)}c(WT,"metaMatmul");function KT(e,t){return su([...t.shape],t.dtype)}c(KT,"metaClone");var rV=["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"],iV=it.META,oV={add:yT,sub:_T,mul:wT,div:bT,pow:xT,rem:ST,maximum:AT,minimum:vT,neg:kT,exp:ET,log:NT,sqrt:RT,rsqrt:BT,abs:CT,sin:TT,cos:IT,tanh:OT,sigmoid:MT,relu:LT,gelu:PT,silu:DT,sign:FT,floor:$T,ceil:zT,sum:UT,mean:VT,max:GT,min:qT,prod:jT,matmul:WT,clone:KT},HT=!1;function JT(){if(HT)return;HT=!0;let e=new zf("mlc","DEF");for(let s of rV)e.def(s);let t=new zf("mlc","IMPL");for(let[s,n]of Object.entries(oV))t.impl(s,iV,n);AS()}c(JT,"registerNativeOps");Qn();$f();ja();Ai();xi();te();Ne();var Mg=class extends Nt{static{c(this,"AddBackward")}constructor(){super(2)}apply(t){let s=t[0];return[s,s]}},Lg=class extends Nt{static{c(this,"SubBackward")}constructor(){super(2)}apply(t){let s=t[0];return[s,kn(s)]}},Pg=class extends Nt{static{c(this,"MulBackward")}constructor(){super(2)}apply(t){let s=t[0],[n,r]=this.savedTensors();return[tt(s,r.detach()),tt(s,n.detach())]}},Dg=class extends Nt{static{c(this,"DivBackward")}constructor(){super(2)}apply(t){let s=t[0],[n,r]=this.savedTensors(),i=oe(s,r.detach()),o=kn(oe(tt(s,n.detach()),tt(r.detach(),r.detach())));return[i,o]}},Fg=class extends Nt{static{c(this,"NegBackward")}constructor(){super(1)}apply(t){let s=t[0];return[kn(s)]}},$g=class extends Nt{static{c(this,"PowBackward")}constructor(){super(2)}apply(t){let s=t[0],[n,r]=this.savedTensors(),i=je(r.shape,{dtype:r.dtype,device:r.device}),o=tt(s,tt(r.detach(),kr(n.detach(),$t(r.detach(),i)))),a=tt(s,tt(kr(n.detach(),r.detach()),jr(n.detach())));return[o,a]}};xi();te();Ne();var zg=class extends Nt{static{c(this,"ExpBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors();return[tt(t[0],lr(s.detach()))]}},Ug=class extends Nt{static{c(this,"LogBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors();return[oe(t[0],s.detach())]}},Vg=class extends Nt{static{c(this,"SqrtBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors(),n=s.detach(),r=cr(n),i=Kt(n.shape,2,{dtype:n.dtype,device:n.device});return[oe(t[0],tt(i,r))]}},Gg=class extends Nt{static{c(this,"TanhBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors(),n=s.detach(),r=Fn(n),i=je(n.shape,{dtype:n.dtype,device:n.device});return[tt(t[0],$t(i,tt(r,r)))]}},qg=class extends Nt{static{c(this,"SigmoidBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors(),n=s.detach(),r=Us(n),i=je(n.shape,{dtype:n.dtype,device:n.device});return[tt(t[0],tt(r,$t(i,r)))]}},jg=class extends Nt{static{c(this,"SoftmaxBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors(),n=this.opArgs(),r=n&&n.length>1&&n[1]!=null?n[1]:-1,i=t[0],o=Er(s.detach(),r),a=he(tt(i,o),r,!0);return[tt(o,$t(i,a))]}},Wg=class extends Nt{static{c(this,"LogSoftmaxBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors(),n=this.opArgs(),r=n&&n.length>1&&n[1]!=null?n[1]:-1,i=t[0],o=Er(s.detach(),r),a=he(i,r,!0);return[$t(i,tt(o,a))]}},Kg=class extends Nt{static{c(this,"ReluBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors(),n=s.detach();return[tt(t[0],bl(so(n)))]}},Hg=class extends Nt{static{c(this,"GeluBackward")}constructor(){super(1)}apply(t){let s=t[0],[n]=this.savedTensors(),r=n.detach(),i=r.shape,o=r.dtype,a=r.device,l=Kt(i,1.702,{dtype:o,device:a}),u=je(i,{dtype:o,device:a}),f=tt(l,r),h=Us(f),p=tt(h,ot(u,tt(f,$t(u,h))));return[tt(s,p)]}},Jg=class extends Nt{static{c(this,"SiluBackward")}constructor(){super(1)}apply(t){let s=t[0],[n]=this.savedTensors(),r=n.detach(),i=Us(r),o=je(r.shape,{dtype:r.dtype,device:r.device}),a=tt(i,ot(o,tt(r,$t(o,i))));return[tt(s,a)]}};xi();te();Ne();vs();function YT(e,t){if(e==null){let n=[];for(let r=0;r<t;r++)n.push(r);return n}return(Array.isArray(e)?e:[e]).map(n=>n<0?n+t:n).sort((n,r)=>n-r)}c(YT,"_normalizeDims");function QT(e,t,s,n){let r=e;if(!n)for(let o of s)r=ar(r,o);let i=Pt(t,{dtype:r.dtype,device:r.device});return ot(i,r)}c(QT,"_unreduce");var Yg=class extends Nt{static{c(this,"SumBackward")}constructor(){super(1)}apply(t){let s=t[0],r=this.inputMetadata(0).shape,i=this.opArgs(),o=i?i[1]:void 0,a=i?i[2]:!1,l=YT(o,r.length);return[QT(s,r,l,a)]}},Qg=class extends Nt{static{c(this,"MeanBackward")}constructor(){super(1)}apply(t){let s=t[0],r=this.inputMetadata(0).shape,i=this.opArgs(),o=i?i[1]:void 0,a=i?i[2]:!1,l=YT(o,r.length),u=1;for(let p of l)u*=r[p];let f=QT(s,r,l,a),h=Kt(r,u,{dtype:s.dtype,device:s.device});return[oe(f,h)]}};xi();te();vs();function XT(e,t){let s=e;for(;s.ndim>t.length;)s=he(s,0,!1);for(let n=0;n<t.length;n++)t[n]===1&&s.shape[n]!==1&&(s=he(s,n,!0));return s}c(XT,"_sumToShape");var Xg=class extends Nt{static{c(this,"MatmulBackward")}constructor(){super(2)}apply(t){let s=t[0],[n,r]=this.savedTensors(),i=n.detach(),o=r.detach(),a=i.ndim,l=o.ndim;if(a===1&&l===1)return[tt(s,o),tt(s,i)];if(a===2&&l===1){let h=ar(s,1),p=ar(o,0);return[qe(h,p),Ah(qe(or(i,0,1),h),1)]}if(a===1&&l===2){let h=qe(s,or(o,0,1)),p=qe(ar(i,1),ar(s,0));return[h,p]}let u=qe(s,or(o,l-2,l-1)),f=qe(or(i,a-2,a-1),s);return u=XT(u,i.shape),f=XT(f,o.shape),[u,f]}},Zg=class extends Nt{static{c(this,"DotBackward")}constructor(){super(2)}apply(t){let s=t[0],[n,r]=this.savedTensors();return[tt(s,r.detach()),tt(s,n.detach())]}};lx();var vS=new Map;function ye(e,t){vS.set(e,t)}c(ye,"_register");ye("add",()=>new Mg);ye("sub",()=>new Lg);ye("mul",()=>new Pg);ye("div",()=>new Dg);ye("neg",()=>new Fg);ye("pow",()=>new $g);ye("exp",()=>new zg);ye("log",()=>new Ug);ye("sqrt",()=>new Vg);ye("tanh",()=>new Gg);ye("sigmoid",()=>new qg);ye("relu",()=>new Kg);ye("softmax",()=>new jg);ye("log_softmax",()=>new Wg);ye("gelu",()=>new Hg);ye("silu",()=>new Jg);ye("sum",()=>new Yg);ye("mean",()=>new Qg);ye("matmul",()=>new Xg);ye("dot",()=>new Zg);ye("cat",()=>new tg);ye("stack",()=>new eg);ye("clamp",()=>new sg);ye("pad",()=>new ng);ye("index_select",()=>new Gc);ye("where",()=>new rg);function ZT(e){let t=vS.get(e);return t?t():null}c(ZT,"getGradFn");function tI(e){return vS.has(e)}c(tI,"hasGradFn");ug();Uc();Zi();Js();vn();Xi();Js();Ne();te();function eI(e,t){let s=e.gradFn;if(!s)throw new Error("Cannot call backward on a tensor that does not require grad");let n=t;if(!n){if(e.numel!==1)throw new Error("grad must be specified for non-scalar tensors");n=je(e.shape,{dtype:e.dtype,device:e.device})}let r=new Map;lV(s,r,new Set);let o=new Map;o.set(s.id,[n]);let a=[];for(let[u,f]of r)f===0&&a.push(u);let l=[];for(;a.length>0;){let u=a.pop();l.push(u);for(let f of u.nextEdges){if(!f||!f.node)continue;let h=f.node,p=r.get(h)-1;r.set(h,p),p===0&&a.push(h)}}for(let u of l){let f=o.get(u.id);if(!f)continue;let h=u.apply(f),p=h;if(h&&(p=h.map((m,g)=>{if(!m)return m;let y=u.inputMetadata(g);return y?aV(m,y.shape):m})),u.releaseVariables(),!p)continue;let d=u.nextEdges;for(let m=0;m<d.length;m++){let g=d[m];if(!g||!g.node||m>=p.length||!p[m])continue;let y=g.node.id,_=o.get(y);if(_)_[g.inputNr]=_[g.inputNr]?ot(_[g.inputNr],p[m]):p[m];else{let w=[];w[g.inputNr]=p[m],o.set(y,w)}}}}c(eI,"backward");function aV(e,t){let s=e.shape;if(s.length===t.length&&s.every((i,o)=>i===t[o]))return e;let n=e,r=s.length-t.length;for(let i=0;i<r;i++)n=he(n,0,!1);for(let i=t.length-1;i>=0;i--)t[i]===1&&n.shape[i]!==1&&(n=he(n,i,!0));return n}c(aV,"_reduceBroadcastGrad");function lV(e,t,s){let n=[e];for(t.set(e,0);n.length>0;){let r=n.shift();if(!s.has(r.id)){s.add(r.id);for(let i of r.nextEdges){if(!i||!i.node)continue;let o=i.node;t.has(o)||t.set(o,0),t.set(o,t.get(o)+1),s.has(o.id)||n.push(o)}}}}c(lV,"_countDeps");vs();xi();te();Ne();vs();var ty=class extends Nt{static{c(this,"ReshapeBackward")}constructor(){super(1)}apply(t){let s=this.inputMetadata(0);return[Gr(t[0],s.shape)]}},ey=class extends Nt{static{c(this,"TransposeBackward")}constructor(t,s){super(1),this._dim0=t,this._dim1=s}apply(t){return[or(t[0],this._dim0,this._dim1)]}},sy=class extends Nt{static{c(this,"SliceBackward")}constructor(t,s,n,r){super(1),this._dim=t,this._start=s,this._end=n,this._step=r}apply(t){let s=t[0],n=this.inputMetadata(0),r=this._dim,i=n.shape[r],o=this._step||1;if(o===1){let _=n.shape.map(()=>0),w=n.shape.map(()=>0);return _[r]=this._start,w[r]=i-this._end,[Uo(s,_,w,0)]}let a=Pt(n.shape,{dtype:s.dtype,device:s.device}),l=a._impl.storage.data,u=s._impl.storage.data,f=s._impl.storageOffset,h=s.shape,p=s.strides,d=a.strides,m=h.length,g=new Int32Array(m),y=f;for(let _=0;_<s.numel;_++){let w=0;for(let b=0;b<m;b++){let x=b===r?this._start+g[b]*o:g[b];w+=x*d[b]}l[w]+=u[y];for(let b=m-1;b>=0;b--){if(g[b]++,g[b]<h[b]){y+=p[b];break}y-=(h[b]-1)*p[b],g[b]=0}}return[a]}},ny=class extends Nt{static{c(this,"SelectBackward")}constructor(t,s){super(1),this._dim=t,this._index=s}apply(t){let s=t[0],n=this.inputMetadata(0),r=this._dim,i=n.shape[r],o=ar(s,r),a=n.shape.map(()=>0),l=n.shape.map(()=>0);return a[r]=this._index,l[r]=i-1-this._index,[Uo(o,a,l,0)]}},ry=class extends Nt{static{c(this,"ExpandBackward")}constructor(){super(1)}apply(t){let s=t[0],r=this.inputMetadata(0).shape,i=s.shape,o=[],a=i.length-r.length;for(let u=0;u<a;u++)o.push(u);for(let u=0;u<r.length;u++)r[u]===1&&i[u+a]!==1&&o.push(u+a);let l=s;return o.length>0&&(l=he(s,o,!0)),[Gr(l,r)]}},iy=class extends Nt{static{c(this,"PermuteBackward")}constructor(t){super(1),this._dims=t}apply(t){let s=new Array(this._dims.length);for(let n=0;n<this._dims.length;n++)s[this._dims[n]]=n;return[J0(t[0],s)]}};DB({backward:eI});WB({GradMode:Rr,ReshapeBackward:ty,TransposeBackward:ey,PermuteBackward:iy,SliceBackward:sy,ExpandBackward:ry,SelectBackward:ny,GradAccumulator:oo});function cV(e){let t=e._impl;if(Ss()&&t.device&&t.device.type===Ie.GPU){t.storage.retain();let r=new Oe(t.storage,t.storageOffset,t.sizes(),t.strides(),t.dtype,t.device);return new Et(r)}let s=t.storage.clone(),n=new Oe(s,t.storageOffset,t.sizes(),t.strides(),t.dtype,t.device);return new Et(n)}c(cV,"_snapshotTensor");function uV(e){for(let t=0;t<e.length;t++){let s=e[t];if(s&&s._impl&&s.requiresGrad)return!0;if(Array.isArray(s)){for(let n=0;n<s.length;n++)if(s[n]&&s[n]._impl&&s[n].requiresGrad)return!0}}return!1}c(uV,"_anyRequiresGrad");function fV(e){let t=[];for(let s=0;s<e.length;s++){let n=e[s];if(n&&n._impl)t.push(n);else if(Array.isArray(n))for(let r=0;r<n.length;r++)n[r]&&n[r]._impl&&t.push(n[r])}return t}c(fV,"_extractTensors");function hV(e){let t=e._impl.autogradMeta;if(!t)return null;let s=t.getGradAccumulator();return s||(s=new oo(e),t.setGradAccumulator(s)),s}c(hV,"_getOrCreateAccumulator");function pV(e,t){let s=Ue.dispatch.bind(Ue);return(n,...r)=>{if(!Rr.isEnabled()||!uV(r)){let u=n.subtract(Vf);return Ue.redispatch(t,u,...r)}let i=ZT(e);if(!i){let u=n.subtract(Vf);return Ue.redispatch(t,u,...r)}let o=fV(r);i.setOpArgs(r);for(let u=0;u<o.length;u++)i.saveTensor(cV(o[u])),i.saveInputMetadata(u,[...o[u].shape],o[u].dtype);for(let u=0;u<o.length;u++){let f=o[u];if(f.requiresGrad){let h=f.gradFn;if(h){let p=f._impl.autogradMeta?f._impl.autogradMeta.outputNr:0;i.setNextEdge(u,h,p)}else{let p=hV(f);p&&i.setNextEdge(u,p,0)}}}let a=n.subtract(Vf),l=Ue.redispatch(t,a,...r);if(l&&l._impl){l._impl.autogradMeta||l._impl.setAutogradMeta(new Ar);let u=l._impl.autogradMeta;u.setGradFn(i,0),u.requiresGrad=!0,l._impl._updateKeySet()}return l}}c(pV,"wrapWithAutograd");function dV(e){return(t,...s)=>{let n=t.subtract(Vf);return Ue.redispatch(e,n,...s)}}c(dV,"_makePassthrough");function sI(){let e=Ue.listOps(),t=[it.AUTOGRAD,it.AUTOGRAD_CPU,it.AUTOGRAD_GPU,it.AUTOGRAD_WASM];for(let s of e){let n=Ue.findOp(s);if(!n)continue;let r=n.name,i=tI(r)?pV(r,n):dV(n),o=Dn.fromUnboxed(i);for(let a of t)n.entry.registerKernel(a,o)}}c(sI,"registerAutogradKernels");te();te();Ne();$o();function ay(e,t,s,n){let r=Pt(e.shape,{dtype:e.dtype}),i=Vo(r,t,s,n),o=Vo(r,t,s,je(n.shape,{dtype:e.dtype}));return Xs(Wr(o,Pt(o.shape,{dtype:o.dtype})),i,e)}c(ay,"scatter");function nu(e,t){return e<0?t+e:e}c(nu,"_normDim");function ly(e,t,s=0){let n=e.shape.length,r=nu(s,n),i=e.shape[r],o=(t%i+i)%i;return o===0?e:Vc([e.narrow(r,i-o,o),e.narrow(r,0,i-o)],r)}c(ly,"roll");function cy(e,t){let s=Array.isArray(t)?t:[t],n=e.shape.length,r=e;for(let i of s){let o=nu(i,n),a=r.shape[o],l=new Array(a);for(let u=0;u<a;u++)l[u]=a-1-u;r=io(r,o,ks(l,{dtype:"i32"}))}return r}c(cy,"flip");function uy(e,t=0){let s=e.shape.length,n=nu(t,s),r=e.shape[n],i=e;for(let o=1;o<r;o*=2){let a=[...i.shape];a[n]=o;let l=Vc([Pt(a,{dtype:i.dtype}),i.narrow(n,0,r-o)],n);i=ot(i,l)}return i}c(uy,"cumsum");function mV(e){let t=1;for(;t<e;)t<<=1;return t}c(mV,"_nextPow2");function oy(e,t,s){let n=e.shape.length,r=n-1,i=e.shape[r],o=mV(i),a=t?-1/0:1/0,l=e;if(o>i){let h=new Array(n).fill(0),p=new Array(n).fill(0);p[r]=o-i,l=Uo(e,h,p,a)}let u=new Array(n).fill(1);u[r]=o;let f=null;if(s){let h=new Array(o);for(let p=0;p<o;p++)h[p]=p;f=ks(h,{dtype:"i32"}).reshape(u)}for(let h=2;h<=o;h<<=1)for(let p=h>>1;p>=1;p>>=1){let d=new Array(o),m=new Array(o);for(let x=0;x<o;x++){let A=x^p;d[x]=A;let S=(x&h)===0,E=t?!S:S;A>x?m[x]=E?1:0:m[x]=E?0:1}let g=ks(d,{dtype:"i32"}),y=io(l,r,g),_=yl(l,y),w=qr(l,y),b=ks(m,{dtype:"f32"}).reshape(u);if(s){let x=io(f,r,g),A=Xs(b,Si(_,l),Si(w,l));f=Xs(A,f,x)}l=Xs(b,_,w)}return o>i&&(l=l.narrow(r,0,i),s&&(f=f.narrow(r,0,i))),s?{values:l,indices:f}:l}c(oy,"_bitonicLastDim");function fy(e,t=-1,s=!1){let n=e.shape.length,r=nu(t,n);if(r===n-1)return oy(e,s,!1);let i=e.transpose(r,n-1);return oy(i,s,!1).transpose(r,n-1)}c(fy,"sort");function nI(e,t,s){let n=e.shape.length,r=nu(t,n);if(r===n-1)return oy(e,s,!0);let i=e.transpose(r,n-1),o=oy(i,s,!0);return{values:o.values.transpose(r,n-1),indices:o.indices.transpose(r,n-1)}}c(nI,"_sortWithIndices");function hy(e,t=-1,s=!1){return nI(e,t,s).indices}c(hy,"argsort");function py(e,t,s=-1,n=!0){let r=e.shape.length,i=nu(s,r),{values:o,indices:a}=nI(e,i,n);return[o.narrow(i,0,t),a.narrow(i,0,t)]}c(py,"topk");vs();$o();function rI(e){let t=e.prototype;t.to=function(s){if(this.device.equals(s))return this;let r=this.contiguous().data.slice(0,this.numel);return Fo(r,this.shape,this.dtype,{device:s})},t.add=function(s){return ot(this,s)},t.sub=function(s){return $t(this,s)},t.mul=function(s){return tt(this,s)},t.div=function(s){return oe(this,s)},t.neg=function(){return kn(this)},t.pow=function(s){return kr(this,s)},t.remainder=function(s){return Q0(this,s)},t.maximum=function(s){return qr(this,s)},t.minimum=function(s){return yl(this,s)},t.exp=function(){return lr(this)},t.log=function(){return jr(this)},t.sqrt=function(){return cr(this)},t.rsqrt=function(){return kh(this)},t.abs=function(){return zo(this)},t.sin=function(){return Eh(this)},t.cos=function(){return Nh(this)},t.tanh=function(){return Fn(this)},t.sigmoid=function(){return Us(this)},t.relu=function(){return so(this)},t.gelu=function(){return _l(this)},t.silu=function(){return wl(this)},t.sign=function(){return bl(this)},t.floor=function(){return Rh(this)},t.ceil=function(){return Bh(this)},t.eq=function(s){return Si(this,s)},t.ne=function(s){return xl(this,s)},t.lt=function(s){return Sl(this,s)},t.le=function(s){return no(this,s)},t.gt=function(s){return Wr(this,s)},t.ge=function(s){return ro(this,s)},t.sum=function(s,n){return he(this,s,n)},t.mean=function(s,n){return fn(this,s,n)},t.max=function(s,n){return Kr(this,s,n)},t.min=function(s,n){return Oh(this,s,n)},t.argmax=function(s,n){return Mh(this,s,n)},t.argmin=function(s,n){return Lh(this,s,n)},t.prod=function(s,n){return Ph(this,s,n)},t.matmul=function(s){return qe(this,s)},t.dot=function(s){return Dh(this,s)},t.mm=function(s){return qe(this,s)},t.softmax=function(s=-1){return Er(this,s)},t.log_softmax=function(s=-1){return Al(this,s)},t.requires_grad=function(s=!0){return this.requiresGrad_(s)},t.clone=function(){return Fh(this)},t.roll=function(s,n=0){return ly(this,s,n)},t.flip=function(s){return cy(this,s)},t.cumsum=function(s=0){return uy(this,s)},t.sort=function(s=-1,n=!1){return fy(this,s,n)},t.topk=function(s,n=-1,r=!0){return py(this,s,n,r)},t.argsort=function(s=-1,n=!1){return hy(this,s,n)},t.gather=function(s,n){return Ih(this,s,n)},t.scatter_add=function(s,n,r){return Vo(this,s,n,r)},t.scatter=function(s,n,r){return ay(this,s,n,r)},QB(e)}c(rI,"installOps");Js();Js();Ne();Ne();function ip(e,t){return{dtype:t?.dtype??e.dtype,device:t?.device??e.device,requiresGrad:t?.requiresGrad??!1}}c(ip,"_likeOpts");function iI(e,t){return is(e.shape,ip(e,t))}c(iI,"emptyLike");function oI(e,t){return Pt(e.shape,ip(e,t))}c(oI,"zerosLike");function aI(e,t){return je(e.shape,ip(e,t))}c(aI,"onesLike");function lI(e,t,s){return Kt(e.shape,t,ip(e,s))}c(lI,"fullLike");function cI(e,t){return Uh(e.shape,ip(e,t))}c(cI,"randnLike");$o();te();Ai();var JS={};Xt(JS,{AdaptiveAvgPool2d:()=>gu,AvgPool2d:()=>mu,BCELoss:()=>bu,BatchNorm1d:()=>Il,BatchNorm2d:()=>pu,Conv1d:()=>Jo,Conv2d:()=>Ho,CrossEntropyLoss:()=>_u,Dropout:()=>pn,ELU:()=>hu,Embedding:()=>xu,F:()=>HS,Flatten:()=>Eu,GELU:()=>au,GRU:()=>Au,GRUCell:()=>Ml,GroupNorm:()=>Sy,LSTM:()=>vu,LSTMCell:()=>Ll,LayerNorm:()=>Br,LeakyReLU:()=>fu,Linear:()=>ls,LogSoftmax:()=>Qo,MSELoss:()=>yu,MaxPool2d:()=>du,Module:()=>nt,ModuleDict:()=>ku,ModuleList:()=>uo,MultiheadAttention:()=>fo,NLLLoss:()=>wu,Parameter:()=>pe,PositionalEncoding:()=>Ru,ReLU:()=>ou,Sequential:()=>Xo,SiLU:()=>lu,Sigmoid:()=>cu,Softmax:()=>Yo,Tanh:()=>uu,Transformer:()=>Nu,TransformerDecoder:()=>Dl,TransformerDecoderLayer:()=>ta,TransformerEncoder:()=>Pl,TransformerEncoderLayer:()=>Zo,functional:()=>iu,init:()=>ap});Js();var pe=class extends Et{static{c(this,"Parameter")}constructor(t,s=!0){t instanceof Et?super(t._impl):super(t),s&&this.requiresGrad_(!0)}get isParameter(){return!0}};var nt=class e{static{c(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,s){if(s!==null&&!(s instanceof pe))throw new Error("Expected Parameter instance");this._parameters.set(t,s)}registerBuffer(t,s){this._buffers.set(t,s)}registerModule(t,s){if(s!==null&&!(s instanceof e))throw new Error("Expected Module instance");this._modules.set(t,s)}*parameters(t=!0){this._autoDetect();for(let[,s]of this._parameters)s!==null&&(yield s);if(t)for(let[,s]of this._modules)s!==null&&(yield*s.parameters(!0))}*namedParameters(t="",s=!0){this._autoDetect();let n=t?t+".":"";for(let[r,i]of this._parameters)i!==null&&(yield[n+r,i]);if(s)for(let[r,i]of this._modules)i!==null&&(yield*i.namedParameters(n+r,!0))}*buffers(t=!0){for(let[,s]of this._buffers)s!==null&&(yield s);if(t)for(let[,s]of this._modules)s!==null&&(yield*s.buffers(!0))}*children(){this._autoDetect();for(let[,t]of this._modules)t!==null&&(yield t)}*namedChildren(){this._autoDetect();for(let[t,s]of this._modules)s!==null&&(yield[t,s])}*modules(){yield this,this._autoDetect();for(let[,t]of this._modules)t!==null&&(yield*t.modules())}*namedModules(t=""){yield[t,this],this._autoDetect();let s=t?t+".":"";for(let[n,r]of this._modules)r!==null&&(yield*r.namedModules(s+n))}stateDict(t=""){this._autoDetect();let s=new Map,n=t?t+".":"";for(let[r,i]of this._parameters)i!==null&&s.set(n+r,i);for(let[r,i]of this._buffers)i!==null&&s.set(n+r,i);for(let[r,i]of this._modules)if(i!==null)for(let[o,a]of i.stateDict(n+r))s.set(o,a);return s}loadStateDict(t){this._autoDetect();for(let[s,n]of this._parameters){let r=s;if(t.has(r)&&n!==null){let i=t.get(r),o=n._impl.storage.data,a=i._impl?i._impl.storage.data:i.data;for(let l=0;l<o.length;l++)o[l]=a[l]}}for(let[s,n]of this._modules)if(n!==null){let r=new Map,i=s+".";for(let[o,a]of t)o.startsWith(i)&&r.set(o.substring(i.length),a);r.size>0&&n.loadStateDict(r)}}train(t=!0){this._training=t;for(let[,s]of this._modules)s!==null&&s.train(t);return this}eval(){return this.train(!1)}get training(){return this._training}to(t){this._autoDetect();for(let[s,n]of this._parameters)n!==null&&typeof n.to=="function"&&(this._parameters.set(s,new pe(n.to(t),n.requiresGrad)),this[s]=this._parameters.get(s));for(let[s,n]of this._buffers)n!==null&&typeof n.to=="function"&&(this._buffers.set(s,n.to(t)),this[s]=this._buffers.get(s));for(let[,s]of this._modules)s!==null&&s.to(t);return this}apply(t){for(let[,s]of this._modules)s!==null&&s.apply(t);return t(this),this}zeroGrad(){for(let t of this.parameters())if(t.grad){let s=t.grad._impl.storage.data;s&&s.fill(0)}return this}_autoDetect(){if(this._detected)return;this._detected=!0;let t=Object.keys(this);for(let s of t){if(s.startsWith("_"))continue;let n=this[s];n instanceof pe&&!this._parameters.has(s)?this._parameters.set(s,n):n instanceof e&&!this._modules.has(s)&&this._modules.set(s,n)}}toString(){return this._buildRepr("")}_buildRepr(t){this._autoDetect();let s=this.constructor.name;if(this._modules.size===0)return s+"()";let n=[s+"("];for(let[r,i]of this._modules)i!==null&&n.push(t+" ("+r+"): "+i._buildRepr(t+" "));return n.push(t+")"),n.join(`
202
- `)}};var ap={};Xt(ap,{_calculateFanInFanOut:()=>co,constant_:()=>wV,kaiming_normal_:()=>SV,kaiming_uniform_:()=>ru,normal_:()=>op,ones_:()=>_V,uniform_:()=>Cl,xavier_normal_:()=>xV,xavier_uniform_:()=>bV,zeros_:()=>yV});function co(e){let t=e.shape,s=t.length;if(s<2)throw new Error("Fan in/out requires at least 2D tensor");let n=t[1],r=t[0],i=1;for(let o=2;o<s;o++)i*=t[o];return{fanIn:n*i,fanOut:r*i}}c(co,"_calculateFanInFanOut");function Cl(e,t=0,s=1){let n=e._impl.storage.data;if(!n)return e;let r=s-t;for(let i=0;i<n.length;i++)n[i]=t+Math.random()*r;return e._impl.bumpVersion(),e}c(Cl,"uniform_");function op(e,t=0,s=1){let n=e._impl.storage.data;if(!n)return e;for(let r=0;r<n.length;r+=2){let i=Math.random()||1e-10,o=Math.random(),a=Math.sqrt(-2*Math.log(i)),l=6.283185307179586*o;n[r]=t+s*a*Math.cos(l),r+1<n.length&&(n[r+1]=t+s*a*Math.sin(l))}return e._impl.bumpVersion(),e}c(op,"normal_");function yV(e){let t=e._impl.storage.data;return t&&t.fill(0),e._impl.bumpVersion(),e}c(yV,"zeros_");function _V(e){let t=e._impl.storage.data;return t&&t.fill(1),e._impl.bumpVersion(),e}c(_V,"ones_");function wV(e,t){let s=e._impl.storage.data;return s&&s.fill(t),e._impl.bumpVersion(),e}c(wV,"constant_");function bV(e,t=1){let{fanIn:s,fanOut:n}=co(e),r=t*Math.sqrt(2/(s+n)),i=Math.sqrt(3)*r;return Cl(e,-i,i)}c(bV,"xavier_uniform_");function xV(e,t=1){let{fanIn:s,fanOut:n}=co(e),r=t*Math.sqrt(2/(s+n));return op(e,0,r)}c(xV,"xavier_normal_");function ru(e,t=0,s="fan_in",n="leaky_relu"){let{fanIn:r,fanOut:i}=co(e),o=s==="fan_in"?r:i,l=uI(n,t)/Math.sqrt(o),u=Math.sqrt(3)*l;return Cl(e,-u,u)}c(ru,"kaiming_uniform_");function SV(e,t=0,s="fan_in",n="leaky_relu"){let{fanIn:r,fanOut:i}=co(e),o=s==="fan_in"?r:i,l=uI(n,t)/Math.sqrt(o);return op(e,0,l)}c(SV,"kaiming_normal_");function uI(e,t=.01){switch(e){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}}c(uI,"_calculateGain");var iu={};Xt(iu,{elu:()=>TS,gelu:()=>cp,leaky_relu:()=>CS,log_softmax:()=>BS,relu:()=>lp,sigmoid:()=>ES,silu:()=>kS,softmax:()=>RS,tanh:()=>NS});te();Ne();function lp(e){return so(e)}c(lp,"relu");function cp(e){return _l(e)}c(cp,"gelu");function kS(e){return wl(e)}c(kS,"silu");function ES(e){return Us(e)}c(ES,"sigmoid");function NS(e){return Fn(e)}c(NS,"tanh");function RS(e,t=-1){return Er(e,t)}c(RS,"softmax");function BS(e,t=-1){return Al(e,t)}c(BS,"log_softmax");function CS(e,t=.01){let s=tt(e,Kt(e.shape,t,{dtype:e.dtype,device:e.device}));return qr(e,s)}c(CS,"leaky_relu");function TS(e,t=1){let s=Kt(e.shape,0,{dtype:e.dtype,device:e.device}),n=Kt(e.shape,1,{dtype:e.dtype,device:e.device}),r=Kt(e.shape,t,{dtype:e.dtype,device:e.device}),i=Wr(e,s),o=tt(r,$t(lr(e),n));return Xs(i,e,o)}c(TS,"elu");var my={};Xt(my,{batch_norm:()=>MS,group_norm:()=>OS,layer_norm:()=>IS});te();Ne();Tl();function IS(e,t,s,n,r=1e-5){if((e instanceof de||e.isSymbolic)&&s&&n){let m=e.ndim-t.length;return X0(e,s,n,m,r)}let i=e.ndim-t.length,o=[];for(let m=i;m<e.ndim;m++)o.push(m);let a=e;for(let m=o.length-1;m>=0;m--)a=fn(a,o[m],!0);let l=$t(e,a),f=tt(l,l);for(let m=o.length-1;m>=0;m--)f=fn(f,o[m],!0);let h=Kt([],r),p=oe(Kt([],1),cr(ot(f,h))),d=tt(l,p);return s&&(d=tt(d,s)),n&&(d=ot(d,n)),d}c(IS,"layer_norm");function OS(e,t,s,n,r=1e-5){let i=e.shape,o=i[0],a=i[1],l=i.slice(2),u=e.reshape([o,t,a/t,...l]),f=[];for(let b=2;b<u.ndim;b++)f.push(b);let h=u;for(let b=f.length-1;b>=0;b--)h=fn(h,f[b],!0);let p=$t(u,h),m=tt(p,p);for(let b=f.length-1;b>=0;b--)m=fn(m,f[b],!0);let g=Kt([],r),y=oe(Kt([],1),cr(ot(m,g))),_=tt(p,y).reshape(i),w=[1,a,...l.map(()=>1)];return s&&(_=tt(_,s.reshape(w))),n&&(_=ot(_,n.reshape(w))),_}c(OS,"group_norm");var dy=1;function vV(e,t){let s=new Array(e).fill(1);return s[dy]=t,s}c(vV,"channelShape");function fI(e,t){let s=e;for(let n=t.length-1;n>=0;n--)s=fn(s,t[n],!0);return s}c(fI,"reduceMeanOver");function hI(e,t,s){let n=e.data;if(!n)return;let r=t.reshape([e.shape[0]]).toArray();for(let i=0;i<n.length;i++)n[i]=n[i]*(1-s)+r[i]*s}c(hI,"blendRunning");function MS(e,t,s,n,r,i=!0,o=1e-5,a=.1){let l=e instanceof de||e.isSymbolic;if(!i)return Z0(e,n,r,t,s,dy,o);let u=[];for(let y=0;y<e.ndim;y++)y!==dy&&u.push(y);let f=fI(e,u),h=$t(e,f),p=fI(tt(h,h),u),d=oe(Kt([],1),cr(ot(p,Kt([],o)))),m=tt(h,d),g=vV(e.ndim,e.shape[dy]);return n&&(m=tt(m,n.reshape(g))),r&&(m=ot(m,r.reshape(g))),l||(t&&hI(t,f,a),s&&hI(s,p,a)),m}c(MS,"batch_norm");var PS={};Xt(PS,{linear:()=>LS});te();vs();Tl();function LS(e,t,s){let n;e instanceof de||e.isSymbolic?n=$h(t,0,1):n=or(t,0,1);let r=qe(e,n);return s?ot(r,s):r}c(LS,"linear");var yy={};Xt(yy,{conv1d:()=>DS,conv2d:()=>gy});te();vs();function gy(e,t,s,n=[1,1],r=[[0,0],[0,0]],i=[1,1],o=1){let a=Array.isArray(n)?n:[n,n],l=kV(r),u=Array.isArray(i)?i:[i,i],f=tx(e,t,a,l,u,o);if(s){let h=Gr(s,[1,s.shape[0],1,1]);return ot(f,h)}return f}c(gy,"conv2d");function DS(e,t,s,n=1,r=0,i=1,o=1){let a=ar(e,2),l=ar(t,2),u=[1,Array.isArray(n)?n[0]:n],f=[[0,0],EV(r)],h=[1,Array.isArray(i)?i[0]:i],p=gy(a,l,null,u,f,h,o),d=Ah(p,2);if(s){let m=Gr(s,[1,s.shape[0],1]);return ot(d,m)}return d}c(DS,"conv1d");function kV(e){return typeof e=="number"?[[e,e],[e,e]]:Array.isArray(e)&&typeof e[0]=="number"?e.map(t=>[t,t]):e}c(kV,"_normalizePadding");function EV(e){return typeof e=="number"?[e,e]:e}c(EV,"_normalizePadding1d");var wy={};Xt(wy,{adaptive_avg_pool2d:()=>$S,avg_pool2d:()=>_y,max_pool2d:()=>FS});te();function FS(e,t,s,n=[[0,0],[0,0]]){let r=Array.isArray(t)?t:[t,t],i=s?Array.isArray(s)?s:[s,s]:r,o=pI(n);return Xm(e,"max",r,i,o)}c(FS,"max_pool2d");function _y(e,t,s,n=[[0,0],[0,0]]){let r=Array.isArray(t)?t:[t,t],i=s?Array.isArray(s)?s:[s,s]:r,o=pI(n);return Xm(e,"avg",r,i,o)}c(_y,"avg_pool2d");function $S(e,t){let[s,n]=Array.isArray(t)?t:[t,t],r=e.shape[2],i=e.shape[3],o=Math.floor(r/s),a=Math.floor(i/n);return _y(e,[o,a],[o,a],[[0,0],[0,0]])}c($S,"adaptive_avg_pool2d");function pI(e){if(typeof e=="number")return[[e,e],[e,e]];if(Array.isArray(e)&&typeof e[0]=="number"){let t=e.map(s=>[s,s]);return t.length===1?[t[0],t[0]]:t}return e}c(pI,"_normalizePadding");var zS={};Xt(zS,{dropout:()=>up});te();Ne();function up(e,t=.5,s=!0){if(!s||t===0)return e;if(t===1)return Kt(e.shape,0,{dtype:e.dtype,device:e.device});let n=NV(e.shape,1-t,e.dtype,e.device),r=Kt(e.shape,1/(1-t),{dtype:e.dtype,device:e.device});return tt(tt(e,n),r)}c(up,"dropout");function NV(e,t,s,n){let r=is(e,{dtype:s,device:n}),i=r._impl.storage.data;if(i)for(let o=0;o<i.length;o++)i[o]=Math.random()<t?1:0;return r}c(NV,"_bernoulliMask");var xy={};Xt(xy,{binary_cross_entropy:()=>GS,cross_entropy:()=>VS,mse_loss:()=>US,nll_loss:()=>by});te();Ne();vs();Vr();function US(e,t,s="mean"){let n=$t(e,t),r=tt(n,n);return dI(r,s)}c(US,"mse_loss");function RV(e,t){let s=BV(e,t),n=$t(e,s),r=lr(n),i=CV(r,t),o=jr(i);return $t(n,o)}c(RV,"_logSoftmaxAutograd");function BV(e,t){let s=t<0?e.ndim+t:t;return Kr(e,s,!0)}c(BV,"_dimMax");function CV(e,t){let s=t<0?e.ndim+t:t;return he(e,s,!0)}c(CV,"_dimSum");function by(e,t,s="mean",n=null){let r=e.ndim-1,i=e.shape[r],o=Th(t,i),a=he(tt(e,o),r),l=a,u=null;if(n!==null){let h=zs(o,r,n),p=ot(kn(h),1);l=tt(a,p),u=he(p)}let f=kn(he(l));if(s==="sum")return f;if(s==="mean")return u!==null?oe(f,u):oe(f,gs(t.shape));throw new Error(`nll_loss: unknown reduction '${s}'`)}c(by,"nll_loss");function VS(e,t,s="mean",n=null){let r=RV(e,-1);return by(r,t,s,n)}c(VS,"cross_entropy");function GS(e,t,s="mean"){let n=Kt(e.shape,1e-7,{dtype:e.dtype,device:e.device}),r=Kt(e.shape,1,{dtype:e.dtype,device:e.device}),i=jr(ot(e,n)),o=jr(ot($t(r,e),n)),a=kn(ot(tt(t,i),tt($t(r,t),o)));return dI(a,s)}c(GS,"binary_cross_entropy");function dI(e,t){if(t==="mean")return fn(e);if(t==="sum")return he(e);if(t==="none")return e;throw new Error(`Unknown reduction: ${t}`)}c(dI,"_reduce");var jS={};Xt(jS,{embedding:()=>qS});te();function qS(e,t){let s=e.shape[e.shape.length-1],n=t.shape.reduce((i,o)=>i*o,1);return io(e,0,t.reshape([n])).reshape([...t.shape,s])}c(qS,"embedding");var KS={};Xt(KS,{scaled_dot_product_attention:()=>WS});te();vs();Tl();Ne();function TV(e,t){if(e instanceof de||e.isSymbolic)return Er(e,t);let s=t<0?e.ndim+t:t,n=Kr(e,s,!0).detach(),r=$t(e,n),i=lr(r),o=he(i,s,!0);return oe(i,o)}c(TV,"_softmax");function IV(e){let t=e.ndim;return e instanceof de||e.isSymbolic?$h(e,t-2,t-1):or(e,t-2,t-1)}c(IV,"_transposeLastTwo");function OV(e,t){let s=is([e,t]),n=s._impl.storage.data,r=t-e;for(let i=0;i<e;i++)for(let o=0;o<t;o++)n[i*t+o]=o<=i+r?0:-1/0;return s}c(OV,"_generateCausalMask");function WS(e,t,s,n=null,r=0,i=!1,o=!1){let a=e.shape[e.ndim-1],l=e.shape[e.ndim-2],u=t.shape[t.ndim-2],f=Kt([],1/Math.sqrt(a)),h=IV(t),p=qe(e,h);p=tt(p,f),i&&(p=ot(p,OV(l,u))),n&&(p=ot(p,n));let d=TV(p,-1);return r>0&&o&&(d=up(d,r,!0)),qe(d,s)}c(WS,"scaled_dot_product_attention");Ne();var ls=class extends nt{static{c(this,"Linear")}constructor(t,s,n=!0){super(),this.inFeatures=t,this.outFeatures=s,this.weight=new pe(is([s,t])),this.bias=n?new pe(Pt([s])):null,this._resetParameters()}_resetParameters(){if(ru(this.weight,Math.sqrt(5)),this.bias){let{fanIn:t}=co(this.weight),s=1/Math.sqrt(t);Cl(this.bias,-s,s)}}forward(t){return LS(t,this.weight,this.bias)}};Ne();var Ho=class extends nt{static{c(this,"Conv2d")}constructor(t,s,n,r={}){super(),this.inChannels=t,this.outChannels=s,this.kernelSize=Array.isArray(n)?n:[n,n],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 i=this.kernelSize[0],o=this.kernelSize[1];this.weight=new pe(is([s,t/this.groups,i,o])),this.bias=r.bias!==!1?new pe(Pt([s])):null,this._resetParameters()}_resetParameters(){if(ru(this.weight,Math.sqrt(5)),this.bias){let{fanIn:t}=co(this.weight),s=1/Math.sqrt(t);Cl(this.bias,-s,s)}}forward(t){return gy(t,this.weight,this.bias,this.stride,this.padding,this.dilation,this.groups)}},Jo=class extends nt{static{c(this,"Conv1d")}constructor(t,s,n,r={}){super(),this.inChannels=t,this.outChannels=s,this.kernelSize=Array.isArray(n)?n[0]:n,this.stride=r.stride??1,this.padding=r.padding??0,this.dilation=r.dilation??1,this.groups=r.groups??1,this.weight=new pe(is([s,t/this.groups,this.kernelSize])),this.bias=r.bias!==!1?new pe(Pt([s])):null,ru(this.weight,Math.sqrt(5))}forward(t){return DS(t,this.weight,this.bias,this.stride,this.padding,this.dilation,this.groups)}};var ou=class extends nt{static{c(this,"ReLU")}forward(t){return lp(t)}},au=class extends nt{static{c(this,"GELU")}forward(t){return cp(t)}},lu=class extends nt{static{c(this,"SiLU")}forward(t){return kS(t)}},cu=class extends nt{static{c(this,"Sigmoid")}forward(t){return ES(t)}},uu=class extends nt{static{c(this,"Tanh")}forward(t){return NS(t)}},fu=class extends nt{static{c(this,"LeakyReLU")}constructor(t=.01){super(),this.negativeSlope=t}forward(t){return CS(t,this.negativeSlope)}},hu=class extends nt{static{c(this,"ELU")}constructor(t=1){super(),this.alpha=t}forward(t){return TS(t,this.alpha)}},Yo=class extends nt{static{c(this,"Softmax")}constructor(t=-1){super(),this.dim=t}forward(t){return RS(t,this.dim)}},Qo=class extends nt{static{c(this,"LogSoftmax")}constructor(t=-1){super(),this.dim=t}forward(t){return BS(t,this.dim)}};Ne();var Br=class extends nt{static{c(this,"LayerNorm")}constructor(t,s=1e-5,n=!0){super(),this.normalizedShape=Array.isArray(t)?t:[t],this.eps=s,this.weight=n?new pe(je(this.normalizedShape)):null,this.bias=n?new pe(Pt(this.normalizedShape)):null}forward(t){return IS(t,this.normalizedShape,this.weight,this.bias,this.eps)}},Sy=class extends nt{static{c(this,"GroupNorm")}constructor(t,s,n=1e-5,r=!0){super(),this.numGroups=t,this.numChannels=s,this.eps=n,this.weight=r?new pe(je([s])):null,this.bias=r?new pe(Pt([s])):null}forward(t){return OS(t,this.numGroups,this.weight,this.bias,this.eps)}},Il=class extends nt{static{c(this,"BatchNorm1d")}constructor(t,s=1e-5,n=!0){super(),this.numFeatures=t,this.eps=s,this.weight=n?new pe(je([t])):null,this.bias=n?new pe(Pt([t])):null,this.runningMean=Pt([t]),this.runningVar=je([t]),this.registerBuffer("runningMean",this.runningMean),this.registerBuffer("runningVar",this.runningVar)}forward(t){return MS(t,this.runningMean,this.runningVar,this.weight,this.bias,this.training,this.eps)}},pu=class extends Il{static{c(this,"BatchNorm2d")}constructor(t,s=1e-5,n=!0){super(t,s,n)}};var du=class extends nt{static{c(this,"MaxPool2d")}constructor(t,s,n=0){super(),this.kernelSize=Array.isArray(t)?t:[t,t],this.stride=s?Array.isArray(s)?s:[s,s]:this.kernelSize,this.padding=n}forward(t){return FS(t,this.kernelSize,this.stride,this.padding)}},mu=class extends nt{static{c(this,"AvgPool2d")}constructor(t,s,n=0){super(),this.kernelSize=Array.isArray(t)?t:[t,t],this.stride=s?Array.isArray(s)?s:[s,s]:this.kernelSize,this.padding=n}forward(t){return _y(t,this.kernelSize,this.stride,this.padding)}},gu=class extends nt{static{c(this,"AdaptiveAvgPool2d")}constructor(t){super(),this.outputSize=Array.isArray(t)?t:[t,t]}forward(t){return $S(t,this.outputSize)}};var pn=class extends nt{static{c(this,"Dropout")}constructor(t=.5){super(),this.p=t}forward(t){return up(t,this.p,this.training)}};var yu=class extends nt{static{c(this,"MSELoss")}constructor(t="mean"){super(),this.reduction=t}forward(t,s){return US(t,s,this.reduction)}},_u=class extends nt{static{c(this,"CrossEntropyLoss")}constructor(t="mean",s=-100){super(),this.reduction=t,this.ignoreIndex=s}forward(t,s){return VS(t,s,this.reduction,this.ignoreIndex===-100?null:this.ignoreIndex)}},wu=class extends nt{static{c(this,"NLLLoss")}constructor(t="mean",s=-100){super(),this.reduction=t,this.ignoreIndex=s}forward(t,s){return by(t,s,this.reduction,this.ignoreIndex===-100?null:this.ignoreIndex)}},bu=class extends nt{static{c(this,"BCELoss")}constructor(t="mean"){super(),this.reduction=t}forward(t,s){return GS(t,s,this.reduction)}};Ne();var xu=class extends nt{static{c(this,"Embedding")}constructor(t,s){super(),this.numEmbeddings=t,this.embeddingDim=s,this.weight=new pe(is([t,s])),op(this.weight)}forward(t){return qS(this.weight,t)}};Ne();te();vs();hp();vs();te();function PV(e){if(Array.isArray(e[0])){let t=new Array(e[0].length);for(let s=0;s<t.length;s++)t[s]=ur(e.map(n=>n[s]),0);return t}return ur(e,0)}c(PV,"stackSteps");function Su(e,t,s){let n=Array.isArray(t),r=Array.isArray(s),i=r?s:[s],o=n?t:[t],a=Ol();if(!a){let p=t,d=[],m=i[0].shape[0];for(let g=0;g<m;g++){let y=r?i.map(b=>zs(b,0,g)):zs(i[0],0,g),[_,w]=e(p,y);p=_,d.push(w)}return[p,PV(d)]}let l=!1,u=c((p,d)=>{let[m,g]=e(n?p:p[0],r?d:d[0]);return l=Array.isArray(g),[n?m:[m],l?g:[g]]},"step"),[f,h]=a.scan(i,o,u);return[n?f:f[0],l?h:h[0]]}c(Su,"scan");var Ml=class extends nt{static{c(this,"GRUCell")}constructor(t,s,n=!0){super(),this.inputSize=t,this.hiddenSize=s,this.x2h=new ls(t,3*s,n),this.h2h=new ls(s,3*s,n)}forward(t,s=null){let n=s!==null?s:Pt([t.shape[0],this.hiddenSize],{device:t.device}),r=this.x2h.forward(t),i=this.h2h.forward(n),[o,a,l]=gl(r,this.hiddenSize,-1),[u,f,h]=gl(i,this.hiddenSize,-1),p=Us(ot(o,u)),d=Us(ot(a,f)),m=Fn(ot(l,tt(p,h)));return ot(m,tt(d,$t(n,m)))}},Au=class extends nt{static{c(this,"GRU")}constructor(t,s,n=1,r=!1,i=!0){super(),this.inputSize=t,this.hiddenSize=s,this.numLayers=n,this.batchFirst=r,this.cells=[];for(let o=0;o<n;o++){let a=new Ml(o===0?t:s,s,i);this.cells.push(a),this.registerModule("cell_"+o,a)}}forward(t,s=null){let n=this.batchFirst?t.transpose(0,1):t,r=n.shape[1],i=n,o=[];for(let l=0;l<this.numLayers;l++){let u=s!==null?zs(s,0,l):Pt([r,this.hiddenSize],{device:n.device}),f=this.cells[l],[h,p]=Su((d,m)=>{let g=f.forward(m,d);return[g,g]},u,i);o.push(h),i=p}let a=i;return this.batchFirst&&(a=a.transpose(0,1)),[a,ur(o,0)]}};Ne();te();vs();hp();Nr();vn();var Ll=class extends nt{static{c(this,"LSTMCell")}constructor(t,s,n=!0){super(),this.inputSize=t,this.hiddenSize=s,this.x2h=new ls(t,4*s,n),this.h2h=new ls(s,4*s,n)}forward(t,s=null){let n=s!==null?s[0]:Pt([t.shape[0],this.hiddenSize],{device:t.device}),r=s!==null?s[1]:Pt([t.shape[0],this.hiddenSize],{device:t.device}),i=ot(this.x2h.forward(t),this.h2h.forward(n)),[o,a,l,u]=gl(i,this.hiddenSize,-1),f=Us(o),h=Us(a),p=Fn(l),d=Us(u),m=ot(tt(h,r),tt(f,p));return[tt(d,Fn(m)),m]}},vu=class extends nt{static{c(this,"LSTM")}constructor(t,s,n=1,r=!1,i=!0){super(),this.inputSize=t,this.hiddenSize=s,this.numLayers=n,this.batchFirst=r,this.cells=[];for(let o=0;o<n;o++){let a=new Ll(o===0?t:s,s,i);this.cells.push(a),this.registerModule("cell_"+o,a)}}forward(t,s=null){let n=wS();if(n&&t.device.type===Ie.GPU&&!Ol()){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]=n(h,this.cells,p,s?s[0]:null,s?s[1]:null);return[this.batchFirst?d.transpose(0,1):d,[m,g]]}let r=bS();if(r&&t.device.type===Ie.WEBGPU&&!Ol()){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,s?s[0]:null,s?s[1]:null);if(d){let[m,g,y]=d;return[this.batchFirst?m.transpose(0,1):m,[g,y]]}}let i=this.batchFirst?t.transpose(0,1):t,o=i.shape[1],a=i,l=[],u=[];for(let h=0;h<this.numLayers;h++){let p=s!==null?zs(s[0],0,h):Pt([o,this.hiddenSize],{device:i.device}),d=s!==null?zs(s[1],0,h):Pt([o,this.hiddenSize],{device:i.device}),m=this.cells[h],[[g,y],_]=Su((w,b)=>{let[x,A]=m.forward(b,w);return[[x,A],x]},[p,d],a);l.push(g),u.push(y),a=_}let f=a;return this.batchFirst&&(f=f.transpose(0,1)),[f,[ur(l,0),ur(u,0)]]}};var Xo=class extends nt{static{c(this,"Sequential")}constructor(...t){super();for(let s=0;s<t.length;s++)this[String(s)]=t[s],this.registerModule(String(s),t[s]);this._length=t.length}forward(t){let s=t;for(let n=0;n<this._length;n++)s=this[String(n)].forward(s);return s}get length(){return this._length}*[Symbol.iterator](){for(let t=0;t<this._length;t++)yield this[String(t)]}push(t){let s=this._length;return this[String(s)]=t,this.registerModule(String(s),t),this._length++,this}},uo=class extends nt{static{c(this,"ModuleList")}constructor(t){if(super(),this._list=[],t)for(let s=0;s<t.length;s++)this._list.push(t[s]),this.registerModule(String(s),t[s])}get length(){return this._list.length}get(t){return this._list[t]}push(t){let s=this._list.length;return this._list.push(t),this.registerModule(String(s),t),this}*[Symbol.iterator](){for(let t of this._list)yield t}forward(){throw new Error("ModuleList does not implement forward()")}},ku=class extends nt{static{c(this,"ModuleDict")}constructor(t){if(super(),this._dict=new Map,t)for(let[s,n]of Object.entries(t))this._dict.set(s,n),this.registerModule(s,n)}get(t){return this._dict.get(t)}set(t,s){return this._dict.set(t,s),this.registerModule(t,s),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()")}};vs();var Eu=class extends nt{static{c(this,"Flatten")}constructor(t=1,s=-1){super(),this.startDim=t,this.endDim=s}forward(t){let s=t.shape,n=s.length,r=this.startDim<0?n+this.startDim:this.startDim,i=this.endDim<0?n+this.endDim:this.endDim,o=1;for(let l=r;l<=i;l++)o*=s[l];let a=[];for(let l=0;l<r;l++)a.push(s[l]);a.push(o);for(let l=i+1;l<n;l++)a.push(s[l]);return Gr(t,a)}};te();Ne();Ne();function gI(e){return e==="gelu"?cp:lp}c(gI,"_getActivation");var fo=class extends nt{static{c(this,"MultiheadAttention")}constructor(t,s,n=0,r=!0,i=null,o=null,a=!0){super(),this.embedDim=t,this.numHeads=s,this.headDim=Math.floor(t/s),this.batchFirst=a,this.dropout=n,this.qProj=new ls(t,t,r),this.kProj=new ls(i??t,t,r),this.vProj=new ls(o??t,t,r),this.outProj=new ls(t,t,r)}forward(t,s,n,r=null,i=null,o=!1){this.batchFirst||(t=t.transpose(0,1),s=s.transpose(0,1),n=n.transpose(0,1));let a=t.shape[0],l=t.shape[1],u=s.shape[1],f=this.qProj.forward(t),h=this.kProj.forward(s),p=this.vProj.forward(n);if(f=f.reshape([a,l,this.numHeads,this.headDim]).permute([0,2,1,3]),h=h.reshape([a,u,this.numHeads,this.headDim]).permute([0,2,1,3]),p=p.reshape([a,u,this.numHeads,this.headDim]).permute([0,2,1,3]),i){let g=Kt(i.shape,-1/0),y=Kt(i.shape,0),_=Xs(i,g,y);_=_.unsqueeze(1).unsqueeze(2),r=r?ot(r,_):_}let d=WS(f,h,p,r,this.dropout,o,this.training);d=d.permute([0,2,1,3]).reshape([a,l,this.embedDim]);let m=this.outProj.forward(d);return this.batchFirst||(m=m.transpose(0,1)),m}},Zo=class extends nt{static{c(this,"TransformerEncoderLayer")}constructor(t,s,n=2048,r=.1,i="relu",o=1e-5,a=!0,l=!1){super(),this.selfAttn=new fo(t,s,r,!0,null,null,a),this.linear1=new ls(t,n),this.linear2=new ls(n,t),this.norm1=new Br(t,o),this.norm2=new Br(t,o),this.dropout1=new pn(r),this.dropout2=new pn(r),this.dropoutFFN=new pn(r),this._activation=gI(i),this._activationName=i,this.normFirst=l,this._dModel=t,this._nhead=s,this._dimFeedforward=n,this._dropout=r,this._layerNormEps=o,this._batchFirst=a}forward(t,s=null,n=null,r=!1){return this.normFirst?this._forwardPreNorm(t,s,n,r):this._forwardPostNorm(t,s,n,r)}_forwardPostNorm(t,s,n,r){let i=this.selfAttn.forward(t,t,t,s,n,r);i=this.norm1.forward(ot(t,this.dropout1.forward(i)));let o=this._activation(this.linear1.forward(i));return o=this.linear2.forward(this.dropoutFFN.forward(o)),i=this.norm2.forward(ot(i,this.dropout2.forward(o))),i}_forwardPreNorm(t,s,n,r){let i=this.norm1.forward(t),o=this.selfAttn.forward(i,i,i,s,n,r);o=ot(t,this.dropout1.forward(o));let a=this._activation(this.linear1.forward(this.norm2.forward(o)));return a=this.linear2.forward(this.dropoutFFN.forward(a)),o=ot(o,this.dropout2.forward(a)),o}},ta=class extends nt{static{c(this,"TransformerDecoderLayer")}constructor(t,s,n=2048,r=.1,i="relu",o=1e-5,a=!0,l=!1){super(),this.selfAttn=new fo(t,s,r,!0,null,null,a),this.crossAttn=new fo(t,s,r,!0,null,null,a),this.linear1=new ls(t,n),this.linear2=new ls(n,t),this.norm1=new Br(t,o),this.norm2=new Br(t,o),this.norm3=new Br(t,o),this.dropout1=new pn(r),this.dropout2=new pn(r),this.dropout3=new pn(r),this.dropoutFFN=new pn(r),this._activation=gI(i),this._activationName=i,this.normFirst=l,this._dModel=t,this._nhead=s,this._dimFeedforward=n,this._dropout=r,this._layerNormEps=o,this._batchFirst=a}forward(t,s,n=null,r=null,i=null,o=null,a=!1){return this.normFirst?this._forwardPreNorm(t,s,n,r,i,o,a):this._forwardPostNorm(t,s,n,r,i,o,a)}_forwardPostNorm(t,s,n,r,i,o,a){let l=this.selfAttn.forward(t,t,t,n,i,a);l=this.norm1.forward(ot(t,this.dropout1.forward(l)));let u=this.crossAttn.forward(l,s,s,r,o);l=this.norm2.forward(ot(l,this.dropout2.forward(u)));let f=this._activation(this.linear1.forward(l));return f=this.linear2.forward(this.dropoutFFN.forward(f)),l=this.norm3.forward(ot(l,this.dropout3.forward(f))),l}_forwardPreNorm(t,s,n,r,i,o,a){let l=this.norm1.forward(t),u=this.selfAttn.forward(l,l,l,n,i,a);u=ot(t,this.dropout1.forward(u));let f=this.norm2.forward(u),h=this.crossAttn.forward(f,s,s,r,o);u=ot(u,this.dropout2.forward(h));let p=this._activation(this.linear1.forward(this.norm3.forward(u)));return p=this.linear2.forward(this.dropoutFFN.forward(p)),u=ot(u,this.dropout3.forward(p)),u}};function DV(e){return new Zo(e._dModel,e._nhead,e._dimFeedforward,e._dropout,e._activationName,e._layerNormEps,e._batchFirst,e.normFirst)}c(DV,"_cloneEncoderLayer");function FV(e){return new ta(e._dModel,e._nhead,e._dimFeedforward,e._dropout,e._activationName,e._layerNormEps,e._batchFirst,e.normFirst)}c(FV,"_cloneDecoderLayer");var Pl=class extends nt{static{c(this,"TransformerEncoder")}constructor(t,s,n=null){super(),this.layers=new uo(Array.from({length:s},()=>DV(t))),this.norm=n}forward(t,s=null,n=null,r=!1){let i=t;for(let o of this.layers)i=o.forward(i,s,n,r);return this.norm&&(i=this.norm.forward(i)),i}},Dl=class extends nt{static{c(this,"TransformerDecoder")}constructor(t,s,n=null){super(),this.layers=new uo(Array.from({length:s},()=>FV(t))),this.norm=n}forward(t,s,n=null,r=null,i=null,o=null,a=!1){let l=t;for(let u of this.layers)l=u.forward(l,s,n,r,i,o,a);return this.norm&&(l=this.norm.forward(l)),l}},Nu=class extends nt{static{c(this,"Transformer")}constructor({dModel:t=512,nhead:s=8,numEncoderLayers:n=6,numDecoderLayers:r=6,dimFeedforward:i=2048,dropout:o=.1,activation:a="relu",batchFirst:l=!0,normFirst:u=!1,layerNormEps:f=1e-5}={}){super();let h=new Zo(t,s,i,o,a,f,l,u),p=new ta(t,s,i,o,a,f,l,u);this.encoder=new Pl(h,n),this.decoder=new Dl(p,r),this.dModel=t}forward(t,s,n=null,r=null,i=null,o=null,a=null,l=null){let u=this.encoder.forward(t,n,o);return this.decoder.forward(s,u,r,i,a,l)}static generateSquareSubsequentMask(t){let s=is([t,t]),n=s._impl.storage.data;for(let r=0;r<t;r++)for(let i=0;i<t;i++)n[r*t+i]=i<=r?0:-1/0;return s}};te();Ne();var Ru=class extends nt{static{c(this,"PositionalEncoding")}constructor(t,s=5e3,n=.1){super(),this.dropoutLayer=new pn(n);let r=Pt([1,s,t]),i=r._impl.storage.data;for(let o=0;o<s;o++)for(let a=0;a<t;a+=2){let l=o*Math.exp(-(a*Math.log(1e4))/t);i[o*t+a]=Math.sin(l),a+1<t&&(i[o*t+a+1]=Math.cos(l))}this.pe=r,this.registerBuffer("pe",r)}forward(t){let s=t.shape[1],n=this.pe.narrow(1,0,s);return this.dropoutLayer.forward(ot(t,n))}};var HS={...iu,...my,...PS,...yy,...wy,...zS,...xy,...jS,...KS};var YS={};Xt(YS,{BatchSampler:()=>ia,DataLoader:()=>aa,Dataset:()=>ea,MapDataset:()=>Bu,RandomSampler:()=>ra,Sampler:()=>ho,SequentialSampler:()=>na,TensorDataset:()=>sa,defaultCollate:()=>oa});vs();var ea=class{static{c(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 s=0;s<t;s++)yield this.get(s)}},sa=class extends ea{static{c(this,"TensorDataset")}constructor(...t){if(super(),t.length===0)throw new Error("TensorDataset requires at least one tensor");let s=t[0].shape[0];for(let n=1;n<t.length;n++)if(t[n].shape[0]!==s)throw new Error(`Size mismatch at dim 0: tensor 0 has ${s}, tensor ${n} has ${t[n].shape[0]}`);this._tensors=t,this._length=s}get length(){return this._length}get(t){let s=new Array(this._tensors.length);for(let n=0;n<this._tensors.length;n++)s[n]=zs(this._tensors[n],0,t);return s}},Bu=class extends ea{static{c(this,"MapDataset")}constructor(t,s){super(),this._dataset=t,this._transform=s}get length(){return this._dataset.length}get(t){return this._transform(this._dataset.get(t))}};var ho=class{static{c(this,"Sampler")}*[Symbol.iterator](){throw new Error("Subclass must implement [Symbol.iterator]()")}},na=class extends ho{static{c(this,"SequentialSampler")}constructor(t){super(),this._dataSource=t}*[Symbol.iterator](){let t=this._dataSource.length;for(let s=0;s<t;s++)yield s}},ra=class extends ho{static{c(this,"RandomSampler")}constructor(t){super(),this._dataSource=t}*[Symbol.iterator](){let t=this._dataSource.length,s=new Int32Array(t);for(let n=0;n<t;n++)s[n]=n;for(let n=t-1;n>0;n--){let r=Math.floor(Math.random()*(n+1)),i=s[n];s[n]=s[r],s[r]=i}for(let n=0;n<t;n++)yield s[n]}},ia=class extends ho{static{c(this,"BatchSampler")}constructor(t,s,n=!1){super(),this._sampler=t,this._batchSize=s,this._dropLast=n}*[Symbol.iterator](){let t=[];for(let s of this._sampler)t.push(s),t.length===this._batchSize&&(yield t,t=[]);t.length>0&&!this._dropLast&&(yield t)}};Js();$o();ir();function $V(e,t=0){let s=e.length,n=e[0].shape,r=e[0].dtype,i=[...n];i.splice(t,0,s);let o=n.reduce((u,f)=>u*f,1),a=me(r),l=new a(s*o);for(let u=0;u<s;u++){let f=e[u],h=f.data,p=f._impl?f._impl.storageOffset:0;if(f.isContiguous)l.set(h.subarray(p,p+o),u*o);else{let d=f.shape,m=f.strides,g=f._impl.storage.data,y=d.length,_=new Int32Array(y),w=p;for(let b=0;b<o;b++){l[u*o+b]=g[w];for(let x=y-1;x>=0;x--){if(_[x]++,_[x]<d[x]){w+=m[x];break}w-=(d[x]-1)*m[x],_[x]=0}}}}return Fo(l,i,r)}c($V,"stackTensors");function oa(e){let t=e[0];if(t instanceof Et)return $V(e,0);if(typeof t=="number")return ks(e);if(Array.isArray(t)){let s=new Array(t.length);for(let n=0;n<t.length;n++){let r=new Array(e.length);for(let i=0;i<e.length;i++)r[i]=e[i][n];s[n]=oa(r)}return s}if(t!==null&&typeof t=="object"&&t.constructor===Object){let s=Object.keys(t),n={};for(let r=0;r<s.length;r++){let i=s[r],o=new Array(e.length);for(let a=0;a<e.length;a++)o[a]=e[a][i];n[i]=oa(o)}return n}throw new Error(`defaultCollate: unsupported element type ${typeof t}`)}c(oa,"defaultCollate");var aa=class{static{c(this,"DataLoader")}constructor(t,s={}){if(this._dataset=t,this._collate=s.collate??oa,s.batchSampler!=null){if(s.batchSize!=null||s.shuffle||s.sampler!=null||s.dropLast)throw new Error("batchSampler is mutually exclusive with batchSize, shuffle, sampler, and dropLast");this._batchSampler=s.batchSampler,this._batchSize=null}else{let n=s.batchSize??1,r=s.dropLast??!1;this._batchSize=n;let i;if(s.sampler!=null){if(s.shuffle)throw new Error("sampler and shuffle are mutually exclusive");i=s.sampler}else i=s.shuffle?new ra(t):new na(t);this._batchSampler=new ia(i,n,r)}}get dataset(){return this._dataset}get length(){let t=this._dataset.length;if(this._batchSize==null){let s=0;for(let n of this._batchSampler)s++;return s}return this._batchSampler._dropLast?Math.floor(t/this._batchSize):Math.ceil(t/this._batchSize)}*[Symbol.iterator](){for(let t of this._batchSampler){let s=new Array(t.length);for(let n=0;n<t.length;n++)s[n]=this._dataset.get(t[n]);yield this._collate(s)}}};var XS={};Xt(XS,{DEFAULT_SPECIALS:()=>Ty,TOKENIZER_FORMAT:()=>Tu,TOKENIZER_VERSION:()=>Iu,Tokenizer:()=>mo,Vocab:()=>po});var po=class e{static{c(this,"Vocab")}constructor(t=[]){this._tokenToId=new Map,this._idToToken=[];for(let s of t)this.add(s)}add(t){let s=this._tokenToId.get(t);return s===void 0&&(s=this._idToToken.length,this._tokenToId.set(t,s),this._idToToken.push(t)),s}getId(t,s=-1){let n=this._tokenToId.get(t);return n===void 0?s:n}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 s=new e,n=new Set;for(let r of t){if(typeof r!="string")throw new Error("mlfw tokenizer: vocab entries must be strings");if(n.has(r))throw new Error(`mlfw tokenizer: duplicate vocab token '${r}'`);n.add(r),s.add(r)}return s}};var Ey=class e{static{c(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 e({lowercase:t.lowercase??!1})}};var Ny=class e{static{c(this,"CharStrategy")}constructor({lowercase:t=!1}={}){this._lowercase=t}fit(){}segment(t){let s=this._lowercase?String(t).toLowerCase():String(t);return Array.from(s)}detokenize(t){return t.join("")}toJSON(){return{lowercase:this._lowercase}}static fromJSON(t={}){return new e({lowercase:t.lowercase??!1})}};function Ry(e,t){return e+""+t}c(Ry,"pairKey");var By=class e{static{c(this,"BpeStrategy")}constructor({numMerges:t=1e3,lowercase:s=!1,endOfWord:n="</w>"}={}){this._numMerges=t,this._lowercase=s,this._eow=n,this._ranks=new Map}_pretokenize(t){return(this._lowercase?String(t).toLowerCase():String(t)).split(/\s+/).filter(Boolean)}_baseSymbols(t){let s=Array.from(t);return s.push(this._eow),s}fit(t){let s=new Map;for(let l of t)for(let u of this._pretokenize(l))s.set(u,(s.get(u)||0)+1);let n=new Map;for(let l of s.keys())n.set(l,this._baseSymbols(l));let r=new Map,i=new Map,o=c(l=>{let u=n.get(l),f=s.get(l);for(let h=0;h+1<u.length;h++){let p=Ry(u[h],u[h+1]);r.set(p,(r.get(p)||0)+f);let d=i.get(p);d||(d=new Set,i.set(p,d)),d.add(l)}},"addWordPairs"),a=c(l=>{let u=n.get(l),f=s.get(l);for(let h=0;h+1<u.length;h++){let p=Ry(u[h],u[h+1]),d=(r.get(p)||0)-f;d<=0?r.delete(p):r.set(p,d);let m=i.get(p);m&&m.delete(l)}},"removeWordPairs");for(let l of s.keys())o(l);this._ranks=new Map;for(let l=0;l<this._numMerges;l++){let u=null,f=0;for(let[y,_]of r)_>f&&(f=_,u=y);if(u===null||f<=0)break;let h=u.indexOf(""),p=u.slice(0,h),d=u.slice(h+1),m=p+d;this._ranks.set(u,l);let g=i.get(u);if(!g||g.size===0)break;for(let y of[...g]){a(y);let _=n.get(y),w=[];for(let b=0;b<_.length;b++)b+1<_.length&&_[b]===p&&_[b+1]===d?(w.push(m),b++):w.push(_[b]);n.set(y,w),o(y)}}}_encodeWord(t){let s=this._baseSymbols(t);for(;s.length>1;){let n=1/0,r=-1;for(let i=0;i+1<s.length;i++){let o=this._ranks.get(Ry(s[i],s[i+1]));o!==void 0&&o<n&&(n=o,r=i)}if(r<0)break;s=s.slice(0,r).concat(s[r]+s[r+1],s.slice(r+2))}return s}segment(t){let s=[];for(let n of this._pretokenize(t))for(let r of this._encodeWord(n))s.push(r);return s}detokenize(t){let s="";for(let n of t)s+=n;return s.split(this._eow).join(" ").trim()}toJSON(){let t=[...this._ranks.entries()].map(([s])=>{let n=s.indexOf("");return[s.slice(0,n),s.slice(n+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 s=new e({numMerges:t.numMerges??t.merges.length,lowercase:t.lowercase??!1,endOfWord:t.endOfWord??"</w>"});s._ranks=new Map;for(let n=0;n<t.merges.length;n++){let r=t.merges[n];if(!Array.isArray(r)||r.length!==2||r.some(i=>typeof i!="string"))throw new Error("mlfw tokenizer: bpe merges must be string pairs");s._ranks.set(Ry(r[0],r[1]),n)}return s}};$o();import{writeFileSync as yI,appendFileSync as zV,readFileSync as _I,existsSync as UV,mkdirSync as VV,readdirSync as GV,unlinkSync as qV,renameSync as jV}from"node:fs";import{resolve as pp}from"node:path";var _e={readFile(e){return _I(pp(e),"utf8")},readBinary(e){return _I(pp(e))},writeFile(e,t){yI(e,t)},writeBinary(e,t){yI(pp(e),t)},appendFile(e,t){zV(e,t)},exists(e){return UV(e)},mkdir(e){VV(e,{recursive:!0})},readdir(e){return GV(e)},remove(e){qV(e)},rename(e,t){jV(pp(e),pp(t))}};var Tu="mlfw-tokenizer",Iu=1,Ty=Object.freeze({pad:"<pad>",unk:"<unk>",bos:"<bos>",eos:"<eos>"}),wI=`${Tu}-v${Iu}`,Cy={word:Ey,char:Ny,bpe:By},dp=Object.freeze(["pad","unk","bos","eos"]),mo=class e{static{c(this,"Tokenizer")}constructor(t={}){let s=t.mode??"word",n=Cy[s];if(!n)throw new Error(`Unknown tokenizer mode '${s}'. Available: ${Object.keys(Cy).join(", ")}`);this._mode=s,this._specials=QS(t.specialTokens),bI(this._specials),this._maxVocab=t.vocabSize??null,xI(this._maxVocab,this._specials),this._strategy=new n(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 s=Array.isArray(t)?t:[t];this._strategy.fit(s,{vocabSize:this._maxVocab});let n=new po(Object.values(this._specials));if(this._maxVocab==null)for(let r of s)for(let i of this._strategy.segment(r))n.add(i);else{let r=new Map;for(let a of s)for(let l of this._strategy.segment(a))r.set(l,(r.get(l)||0)+1);let i=[...r.entries()].sort((a,l)=>l[1]-a[1]),o=this._maxVocab-n.size;for(let a=0;a<i.length&&a<o;a++)n.add(i[a][0])}return this._vocab=n,this}toJSON(){this._ensureFit();let t=this._strategy.toJSON();return{format:Tu,version:Iu,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 s=t+".tmp";_e.writeFile(s,KV(this.toJSON())),_e.rename(s,t)}static load(t){if(typeof t!="string")throw new Error("mlfw tokenizer: load(path) requires a file path string");let s=_e.readFile(t),n=typeof s=="string"?s:new TextDecoder().decode(s);return e.fromJSON(HV(n))}static fromJSON(t){WV(t);let s=Cy[t.mode],n=new e({mode:t.mode,vocabSize:t.config.vocabSize,specialTokens:t.specialTokens});n._strategy=s.fromJSON(t.strategy),n._vocab=po.fromTokens(t.vocab);for(let r of Object.values(n._specials))if(!n._vocab.has(r))throw new Error(`mlfw tokenizer: special token '${r}' is missing from vocab`);return n}encode(t,s={}){this._ensureFit();let n=[];s.addBos&&n.push(this.bosId);let r=this.unkId;for(let i of this._strategy.segment(t))n.push(this._vocab.getId(i,r));return s.addEos&&n.push(this.eosId),n}decode(t,s={}){this._ensureFit();let n=s.skipSpecial??!0,r=new Set(Object.values(this._specials).map(o=>this._vocab.getId(o))),i=[];for(let o of t){if(n&&r.has(o))continue;let a=this._vocab.getToken(o);a!==void 0&&i.push(a)}return this._strategy.detokenize(i)}encodeBatch(t,s={}){this._ensureFit();let r=(Array.isArray(t)?t:[t]).map(u=>this.encode(u,s)),i=s.maxLen??r.reduce((u,f)=>Math.max(u,f.length),0),o=s.padId??this.padId,a=r.length,l=new Int32Array(a*i).fill(o);for(let u=0;u<a;u++){let f=r[u],h=Math.min(f.length,i);for(let p=0;p<h;p++)l[u*i+p]=f[p]}return ks(l,{shape:[a,i],dtype:"i32"})}};function QS(e){if(Array.isArray(e)){let t={...Ty};for(let s=0;s<e.length&&s<dp.length;s++)t[dp[s]]=e[s];return t}return{...Ty,...e??{}}}c(QS,"normalizeSpecials");function bI(e){let t=new Set;for(let s of dp){let n=e[s];if(typeof n!="string"||n.length===0)throw new Error(`mlfw tokenizer: special token '${s}' must be a non-empty string`);if(t.has(n))throw new Error(`mlfw tokenizer: duplicate special token '${n}'`);t.add(n)}}c(bI,"validateSpecials");function xI(e,t){if(e!=null&&(!Number.isInteger(e)||e<Object.keys(t).length))throw new Error(`mlfw tokenizer: vocabSize must be an integer >= ${Object.keys(t).length}`)}c(xI,"validateVocabSize");function WV(e){if(!e||typeof e!="object")throw new Error("mlfw tokenizer: artifact must be an object");if(e.format!==Tu)throw new Error("mlfw tokenizer: unrecognized tokenizer format");if(e.version!==Iu)throw new Error(`mlfw tokenizer: unsupported tokenizer version ${e.version}`);if(!Cy[e.mode])throw new Error(`mlfw tokenizer: unknown tokenizer mode '${e.mode}'`);if(!e.config||typeof e.config!="object")throw new Error("mlfw tokenizer: config must be an object");if(bI(QS(e.specialTokens)),xI(e.config.vocabSize,QS(e.specialTokens)),!Array.isArray(e.vocab))throw new Error("mlfw tokenizer: vocab must be an array");if(!e.strategy||typeof e.strategy!="object")throw new Error("mlfw tokenizer: strategy must be an object")}c(WV,"validateTokenizerData");function KV(e){let t=[wI,Fl("m",[e.mode]),Fl("z",[e.config.vocabSize==null?"":String(e.config.vocabSize)]),Fl("s",dp.map(s=>e.specialTokens[s]))];if(e.mode==="bpe"){let s=[];for(let n of e.strategy.merges)s.push(n[0],n[1]);t.push(Fl("g",[String(e.strategy.lowercase?1:0),String(e.strategy.numMerges),e.strategy.endOfWord])),t.push(Fl("r",s))}else t.push(Fl("g",[String(e.strategy.lowercase?1:0)]));return t.push(Fl("v",e.vocab)),t.join(`
203
- `)}c(KV,"serializeTokenizer");function HV(e){if(!e.startsWith(wI))throw new Error("mlfw tokenizer: unrecognized tokenizer format");let t=new Map;for(let u of e.split(/\r?\n/).slice(1)){if(!u)continue;let f=u.indexOf(" ");if(f<0)throw new Error("mlfw tokenizer: malformed tokenizer artifact");t.set(u.slice(0,f),JV(u.slice(f+1)))}let s=Cu(t,"m")[0],n=Cu(t,"z")[0],r=Cu(t,"s"),i=Cu(t,"g"),o=Cu(t,"v"),a=Object.fromEntries(dp.map((u,f)=>[u,r[f]])),l={lowercase:i[0]==="1"};if(s==="bpe"){let u=Cu(t,"r");if(u.length%2!==0)throw new Error("mlfw tokenizer: bpe merges must be string pairs");l.numMerges=Number(i[1]),l.endOfWord=i[2],l.merges=[];for(let f=0;f<u.length;f+=2)l.merges.push([u[f],u[f+1]])}return{format:Tu,version:Iu,mode:s,config:{vocabSize:n===""?null:Number(n),...l},specialTokens:a,vocab:o,strategy:l}}c(HV,"parseTokenizer");function Fl(e,t){return`${e} ${t.map(YV).join(" ")}`}c(Fl,"record");function JV(e){return e.split(" ").map(QV)}c(JV,"parseRecord");function YV(e){return String(e).replace(/\\/g,"\\\\").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r")}c(YV,"escapeField");function QV(e){let t="";for(let s=0;s<e.length;s++){let n=e[s];if(n!=="\\"){t+=n;continue}let r=e[++s];r==="t"?t+=" ":r==="n"?t+=`
204
- `:r==="r"?t+="\r":r==="\\"?t+="\\":t+=r??""}return t}c(QV,"unescapeField");function Cu(e,t){let s=e.get(t);if(!s)throw new Error(`mlfw tokenizer: missing '${t}' record`);return s}c(Cu,"requiredField");var rv={};Xt(rv,{Adam:()=>ca,AdamW:()=>ua,CosineAnnealingLR:()=>pa,FusedAdam:()=>$_,FusedOptimizer:()=>Xu,FusedSGD:()=>F_,GradScaler:()=>z_,LRScheduler:()=>fa,Optimizer:()=>zn,ReduceLROnPlateau:()=>da,SGD:()=>la,StepLR:()=>ha,clipGradNorm_:()=>Ou,clipGradValue_:()=>Mu});var zn=class{static{c(this,"Optimizer")}constructor(t,s){this._defaults={...s},this._paramGroups=[],this._state=new Map,this._paramIndex=new WeakMap,this._nextId=0;let n=XV(t);for(let r of n)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 s of this._paramGroups)for(let n of s.params)n.grad!==null&&(t?n.grad=null:n.grad._impl.storage.data.fill(0))}stateDict(){let t=new Map;for(let[s,n]of this._state){let r={};for(let i of Object.keys(n)){let o=n[i];r[i]=SI(o)?new o.constructor(o):o}t.set(s,r)}return{state:t,paramGroups:this._paramGroups.map(s=>{let n={};for(let r of Object.keys(s)){if(r==="params")continue;let i=s[r];n[r]=Array.isArray(i)?[...i]:i}return n})}}loadStateDict(t){for(let s=0;s<this._paramGroups.length;s++){let n=t.paramGroups[s];for(let r of Object.keys(n))this._paramGroups[s][r]=Array.isArray(n[r])?[...n[r]]:n[r]}this._state=new Map;for(let[s,n]of t.state){let r={};for(let i of Object.keys(n)){let o=n[i];r[i]=SI(o)?new o.constructor(o):o}this._state.set(s,r)}}_addParamGroup(t){let s={...this._defaults};for(let r of Object.keys(t))r!=="params"&&(s[r]=t[r]);let n=Array.isArray(t.params)?t.params:[...t.params];for(let r of n){if(this._paramIndex.has(r))throw new Error("Parameter appears in more than one parameter group");this._paramIndex.set(r,this._nextId++)}s.params=n,this._paramGroups.push(s)}_getParamId(t){return this._paramIndex.get(t)}_getState(t){let s=this._paramIndex.get(t),n=this._state.get(s);return n||(n={},this._state.set(s,n)),n}};function XV(e){if(e==null)throw new Error("Optimizer requires at least one parameter");let t=Array.isArray(e)?e:[...e];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}]}c(XV,"normalizeParamGroups");function SI(e){return e instanceof Float32Array||e instanceof Float64Array||e instanceof Int32Array||e instanceof Int16Array||e instanceof Int8Array||e instanceof Uint8Array||e instanceof Uint16Array||e instanceof Uint32Array}c(SI,"isTypedArray");var la=class extends zn{static{c(this,"SGD")}constructor(t,{lr:s=.01,momentum:n=0,dampening:r=0,weightDecay:i=0,nesterov:o=!1}={}){if(o&&(n===0||r!==0))throw new Error("Nesterov momentum requires momentum > 0 and dampening = 0");super(t,{lr:s,momentum:n,dampening:r,weightDecay:i,nesterov:o})}step(){for(let t of this._paramGroups){let{lr:s,momentum:n,dampening:r,weightDecay:i,nesterov:o}=t;for(let a of t.params){if(a.grad===null)continue;let l=a._impl.storage.data,u=a.grad._impl.storage.data,f=l.length,h=this._getState(a);if(n===0)if(i===0)for(let p=0;p<f;p++)l[p]-=s*u[p];else for(let p=0;p<f;p++)l[p]-=s*(u[p]+i*l[p]);else{let p=h.momentumBuffer;if(p)for(let d=0;d<f;d++){let m=i!==0?u[d]+i*l[d]:u[d];p[d]=n*p[d]+(1-r)*m}else{p=new l.constructor(f);for(let d=0;d<f;d++)p[d]=i!==0?u[d]+i*l[d]:u[d];h.momentumBuffer=p}if(o)for(let d=0;d<f;d++){let m=i!==0?u[d]+i*l[d]:u[d];l[d]-=s*(m+n*p[d])}else for(let d=0;d<f;d++)l[d]-=s*p[d]}a._impl.bumpVersion()}}}};Nr();vn();var ca=class extends zn{static{c(this,"Adam")}constructor(t,{lr:s=.001,betas:n=[.9,.999],eps:r=1e-8,weightDecay:i=0,amsgrad:o=!1}={}){super(t,{lr:s,betas:[...n],eps:r,weightDecay:i,amsgrad:o})}step(){let t=xS();for(let s of this._paramGroups){let{lr:n,betas:r,eps:i,weightDecay:o,amsgrad:a}=s,[l,u]=r;for(let f of s.params){if(f.grad===null)continue;let h=this._getState(f);if(t&&!a&&f.device&&f.device.type===Ie.GPU){h.step=(h.step||0)+1;let S=h.step,E=1-Math.pow(l,S),R=1-Math.pow(u,S);if(t(f,h,{beta1:l,beta2:u,omb1:1-l,omb2:1-u,eps:i,stepSize:n/E,bc2sqrt:Math.sqrt(R),wd:o,lr:n})){f._impl.bumpVersion();continue}}let p=f._impl.storage.data,d=f.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,y=h.expAvg,_=h.expAvgSq,w=1-Math.pow(l,g),b=1-Math.pow(u,g),x=n/w,A=Math.sqrt(b);if(a){let S=h.maxExpAvgSq;for(let E=0;E<m;E++){let R=o!==0?d[E]+o*p[E]:d[E];y[E]=l*y[E]+(1-l)*R,_[E]=u*_[E]+(1-u)*R*R,_[E]>S[E]&&(S[E]=_[E]),p[E]-=x*y[E]/(Math.sqrt(S[E])/A+i)}}else for(let S=0;S<m;S++){let E=o!==0?d[S]+o*p[S]:d[S];y[S]=l*y[S]+(1-l)*E,_[S]=u*_[S]+(1-u)*E*E,p[S]-=x*y[S]/(Math.sqrt(_[S])/A+i)}f._impl.bumpVersion()}}}};var ua=class extends zn{static{c(this,"AdamW")}constructor(t,{lr:s=.001,betas:n=[.9,.999],eps:r=1e-8,weightDecay:i=.01,amsgrad:o=!1}={}){super(t,{lr:s,betas:[...n],eps:r,weightDecay:i,amsgrad:o})}step(){for(let t of this._paramGroups){let{lr:s,betas:n,eps:r,weightDecay:i,amsgrad:o}=t,[a,l]=n;for(let u of t.params){if(u.grad===null)continue;let f=u._impl.storage.data,h=u.grad._impl.storage.data,p=f.length,d=this._getState(u);d.step===void 0&&(d.step=0,d.expAvg=new f.constructor(p),d.expAvgSq=new f.constructor(p),o&&(d.maxExpAvgSq=new f.constructor(p))),d.step++;let m=d.step,g=d.expAvg,y=d.expAvgSq,_=1-Math.pow(a,m),w=1-Math.pow(l,m),b=s/_,x=Math.sqrt(w);if(i!==0){let A=1-s*i;for(let S=0;S<p;S++)f[S]*=A}if(o){let A=d.maxExpAvgSq;for(let S=0;S<p;S++)g[S]=a*g[S]+(1-a)*h[S],y[S]=l*y[S]+(1-l)*h[S]*h[S],y[S]>A[S]&&(A[S]=y[S]),f[S]-=b*g[S]/(Math.sqrt(A[S])/x+r)}else for(let A=0;A<p;A++)g[A]=a*g[A]+(1-a)*h[A],y[A]=l*y[A]+(1-l)*h[A]*h[A],f[A]-=b*g[A]/(Math.sqrt(y[A])/x+r);u._impl.bumpVersion()}}}};var fa=class{static{c(this,"LRScheduler")}constructor(t,s=-1){this._optimizer=t,this._baseLRs=t.paramGroups.map(n=>n.lr),this._lastEpoch=s,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 s=this._optimizer.paramGroups;for(let n=0;n<s.length;n++)s[n].lr=t[n]}},ha=class extends fa{static{c(this,"StepLR")}constructor(t,s,n=.1,r=-1){super(t,r),this._stepSize=s,this._gamma=n,this._init()}getLR(){let t=Math.pow(this._gamma,Math.floor(this._lastEpoch/this._stepSize));return this._baseLRs.map(s=>s*t)}},pa=class extends fa{static{c(this,"CosineAnnealingLR")}constructor(t,s,n=0,r=-1){super(t,r),this._tMax=s,this._etaMin=n,this._init()}getLR(){let t=(1+Math.cos(Math.PI*this._lastEpoch/this._tMax))/2;return this._baseLRs.map(s=>this._etaMin+(s-this._etaMin)*t)}},da=class{static{c(this,"ReduceLROnPlateau")}constructor(t,{mode:s="min",factor:n=.1,patience:r=10,threshold:i=1e-4,thresholdMode:o="rel",cooldown:a=0,minLR:l=0,eps:u=1e-8}={}){this._optimizer=t,this._mode=s,this._factor=n,this._patience=r,this._threshold=i,this._thresholdMode=o,this._cooldown=a,this._minLR=l,this._eps=u,this._best=s==="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 s=Math.max(t.lr*this._factor,this._minLR);t.lr-s>this._eps&&(t.lr=s)}}};te();function AI(e){return(Array.isArray(e)?e:[...e]).filter(s=>s.grad!==null)}c(AI,"_gradParams");function Ou(e,t,s=2){let n=AI(e);if(n.length===0)return 0;let r;if(s===2){let o=null;for(let a of n){let l=he(tt(a.grad,a.grad));o=o===null?l:ot(o,l)}r=Math.sqrt(o.item())}else if(s===1/0){let o=null;for(let a of n){let l=Kr(zo(a.grad));o=o===null?l:qr(o,l)}r=o.item()}else{let o=null;for(let a of n){let l=he(kr(zo(a.grad),s));o=o===null?l:ot(o,l)}r=Math.pow(o.item(),1/s)}let i=t/(r+1e-6);if(i<1)for(let o of n)o.grad=tt(o.grad,i);return r}c(Ou,"clipGradNorm_");function Mu(e,t){for(let s of AI(e))s.grad=Ch(s.grad,-t,t)}c(Mu,"clipGradValue_");li();It();D_();hi();var Qr=Q.F32,Pe=c(e=>new T([e],Qr),"VEC"),Qu=new T([],Qr);function Or(e,t,s){return e.broadcast(t,[s],[]).getResult(0)}c(Or,"bcast");var Xu=class extends zn{static{c(this,"FusedOptimizer")}constructor(t,s,n=null){super(t,s),this._target=n||nr(),this._kernels=new Map}_kernel(t){let s=this._kernels.get(t);if(!s){let n=this._buildGraph(t);s=k1(n,this._target,{fusion:{enabled:!0}}),this._kernels.set(t,s)}return s}_buildGraph(t){throw new Error(`${this.constructor.name}._buildGraph() not implemented`)}},F_=class extends Xu{static{c(this,"FusedSGD")}constructor(t,{lr:s=.01,momentum:n=0,dampening:r=0,weightDecay:i=0,nesterov:o=!1}={},a=null){if(o&&(n===0||r!==0))throw new Error("Nesterov momentum requires momentum > 0 and dampening = 0");super(t,{lr:s,momentum:n,dampening:r,weightDecay:i,nesterov:o},a)}_buildGraph(t){let{momentum:s,dampening:n,weightDecay:r,nesterov:i}=this._defaults,o=s!==0,a=o?[Pe(t),Pe(t),Pe(t),Qu]:[Pe(t),Pe(t),Qu],l=o?[Pe(t),Pe(t)]:[Pe(t)];return Yf("sgd_update",a,l,(f,h)=>{let p=h[0],d=h[1],m=o?h[3]:h[2],g=Or(f,m,t),y=d;if(r!==0){let E=f.scalarConstant(r,Qr).getResult(0);y=f.add(d,f.mul(Or(f,E,t),p).getResult(0)).getResult(0)}if(!o){let E=f.sub(p,f.mul(g,y).getResult(0)).getResult(0);f.returnOp([E]);return}let _=h[2],w=Or(f,f.scalarConstant(s,Qr).getResult(0),t),b=Or(f,f.scalarConstant(1-n,Qr).getResult(0),t),x=f.add(f.mul(w,_).getResult(0),f.mul(b,y).getResult(0)).getResult(0),A=x;i&&(A=f.add(y,f.mul(w,x).getResult(0)).getResult(0));let S=f.sub(p,f.mul(g,A).getResult(0)).getResult(0);f.returnOp([S,x])})}step(){for(let t of this._paramGroups){let{lr:s,momentum:n}=t,r=n!==0,i=new Float32Array([s]);for(let o of t.params){if(o.grad===null)continue;let a=o._impl.storage.data,l=o.grad._impl.storage.data,u=a.length,f=this._kernel(u);if(r){let h=this._getState(o),p=h.momentumBuffer;p||(p=new a.constructor(u),h.momentumBuffer=p),f.run("sgd_update",a,l,p,i,a,p)}else f.run("sgd_update",a,l,i,a);o._impl.bumpVersion()}}}},$_=class extends Xu{static{c(this,"FusedAdam")}constructor(t,{lr:s=.001,betas:n=[.9,.999],eps:r=1e-8,weightDecay:i=0,amsgrad:o=!1}={},a=null){super(t,{lr:s,betas:[...n],eps:r,weightDecay:i,amsgrad:o},a)}_buildGraph(t){let{betas:s,eps:n,weightDecay:r,amsgrad:i}=this._defaults,[o,a]=s,l=i?[Pe(t),Pe(t),Pe(t),Pe(t),Pe(t),Qu,Qu]:[Pe(t),Pe(t),Pe(t),Pe(t),Qu,Qu],u=i?[Pe(t),Pe(t),Pe(t),Pe(t)]:[Pe(t),Pe(t),Pe(t)];return Yf("adam_update",l,u,(f,h)=>{let p=h[0],d=h[1],m=h[2],g=h[3],y=i?h[4]:null,_=i?h[5]:h[4],w=i?h[6]:h[5],b=d;if(r!==0){let z=Or(f,f.scalarConstant(r,Qr).getResult(0),t);b=f.add(d,f.mul(z,p).getResult(0)).getResult(0)}let x=Or(f,f.scalarConstant(o,Qr).getResult(0),t),A=Or(f,f.scalarConstant(a,Qr).getResult(0),t),S=Or(f,f.scalarConstant(1-o,Qr).getResult(0),t),E=Or(f,f.scalarConstant(1-a,Qr).getResult(0),t),R=Or(f,f.scalarConstant(n,Qr).getResult(0),t),v=f.add(f.mul(x,m).getResult(0),f.mul(S,b).getResult(0)).getResult(0),k=f.mul(b,b).getResult(0),B=f.add(f.mul(A,g).getResult(0),f.mul(E,k).getResult(0)).getResult(0),N=B,C=null;i&&(C=f.maximum(y,B).getResult(0),N=C);let O=f.add(f.div(f.sqrt(N).getResult(0),Or(f,w,t)).getResult(0),R).getResult(0),F=f.mul(Or(f,_,t),f.div(v,O).getResult(0)).getResult(0),P=f.sub(p,F).getResult(0);f.returnOp(i?[P,v,B,C]:[P,v,B])})}step(){for(let t of this._paramGroups){let{lr:s,betas:n,eps:r,weightDecay:i,amsgrad:o}=t,[a,l]=n;for(let u of t.params){if(u.grad===null)continue;let f=u._impl.storage.data,h=u.grad._impl.storage.data,p=f.length,d=this._getState(u);d.step===void 0&&(d.step=0,d.expAvg=new f.constructor(p),d.expAvgSq=new f.constructor(p),o&&(d.maxExpAvgSq=new f.constructor(p))),d.step++;let m=d.step,g=1-Math.pow(a,m),y=1-Math.pow(l,m),_=new Float32Array([s/g]),w=new Float32Array([Math.sqrt(y)]),b=this._kernel(p),x=d.expAvg,A=d.expAvgSq;if(o){let S=d.maxExpAvgSq;b.run("adam_update",f,h,x,A,S,_,w,f,x,A,S)}else b.run("adam_update",f,h,x,A,_,w,f,x,A);u._impl.bumpVersion()}}}};var z_=class{static{c(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 s=t._impl.storage.data;for(let n=0;n<s.length;n++)s[n]*=this._scale;return t._impl.bumpVersion&&t._impl.bumpVersion(),t}unscale_(t){if(!this.enabled)return!1;let s=1/this._scale,n=!1;for(let r of t.paramGroups)for(let i of r.params){if(i.grad===null||i.grad===void 0)continue;let o=i.grad._impl.storage.data;for(let a=0;a<o.length;a++){let l=o[a]*s;Number.isFinite(l)||(n=!0),o[a]=l}}return this._unscaled.add(t),n&&(this._foundInf=!0),n}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)}};G_();mv();hi();var xv={};Xt(xv,{Accuracy:()=>ka,CSVLogger:()=>Aa,Callback:()=>Gs,CallbackConnector:()=>sf,ConfusionMatrix:()=>Ba,ConsoleLogger:()=>Mi,EarlyStopping:()=>wa,EvaluationLoop:()=>lf,F1Score:()=>Ra,FitLoop:()=>af,GradientAccumulationScheduler:()=>Sa,HOOKS:()=>Y1,LearningRateMonitor:()=>ba,LightningModule:()=>ya,Logger:()=>Oi,LoggerConnector:()=>nf,MeanMetric:()=>pf,Metric:()=>tn,MetricAccumulator:()=>Zu,MetricCollection:()=>va,ModelCheckpoint:()=>ti,Precision:()=>Ea,PredictionLoop:()=>cf,ProgressCallback:()=>Zr,Recall:()=>Na,SingleDeviceStrategy:()=>ef,Stage:()=>Vs,SumMetric:()=>df,Timer:()=>xa,Trainer:()=>_a,TrainerState:()=>tf,TrainingLoop:()=>rf,ValidationLoop:()=>of,applyCheckpoint:()=>hf,deserializeCheckpoint:()=>Q_,loadCheckpoint:()=>ff,serializeCheckpoint:()=>uf});var ya=class extends nt{static{c(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,s){throw new Error(`${this.constructor.name}.trainingStep() not implemented`)}validationStep(t,s){}testStep(t,s){}predictStep(t,s){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,s,{onStep:n=null,onEpoch:r=null,reduceFx:i="mean",progBar:o=!1}={}){let a=this._trainer?this._trainer.state.stage:"training",l=a==="training",u=a!=="training";this._logBuffer.set(t,{value:s,onStep:n!==null?n:l,onEpoch:r!==null?r:u,reduceFx:i,progBar:o})}logDict(t,s={}){for(let n of Object.keys(t))this.log(n,t[n],s)}manualBackward(t){this._trainer&&this._trainer.strategy?this._trainer.strategy.backward(t):t.backward()}};function J1(e){if(!e)throw new Error("configureOptimizers() returned null/undefined");if(Array.isArray(e)){let t=[],s=[];for(let n=0;n<e.length;n++){let r=e[n];r.optimizer?(t.push(r.optimizer),s.push(H1(r.lrScheduler))):(t.push(r),s.push(null))}return{optimizers:t,schedulerConfigs:s}}if(e.optimizer)return{optimizers:[e.optimizer],schedulerConfigs:[H1(e.lrScheduler)]};if(typeof e.step=="function")return{optimizers:[e],schedulerConfigs:[null]};throw new Error("configureOptimizers() returned an unrecognized format")}c(J1,"parseOptimizersConfig");function H1(e){return e?e.scheduler?{scheduler:e.scheduler,interval:e.interval||"epoch",frequency:e.frequency||1,monitor:e.monitor||null}:typeof e.step=="function"?{scheduler:e,interval:"epoch",frequency:1,monitor:null}:null:null}c(H1,"normalizeSchedulerConfig");var Vs=Object.freeze({IDLE:"idle",TRAINING:"training",VALIDATING:"validating",TESTING:"testing",PREDICTING:"predicting"}),JG=Object.freeze({mean:c(e=>e.count===0?0:e.sum/e.count,"mean"),sum:c(e=>e.sum,"sum"),min:c(e=>e.min,"min"),max:c(e=>e.max,"max"),last:c(e=>e.last,"last")});function YG(){return{sum:0,count:0,min:1/0,max:-1/0,last:0}}c(YG,"createAccumulator");function QG(e,t){let s=typeof t=="number"?t:t.item();e.sum+=s,e.count+=1,s<e.min&&(e.min=s),s>e.max&&(e.max=s),e.last=s}c(QG,"updateAccumulator");var Zu=class{static{c(this,"MetricAccumulator")}constructor(){this._accumulators=new Map,this._reduceFns=new Map}update(t,s,n="mean"){this._accumulators.has(t)||(this._accumulators.set(t,YG()),this._reduceFns.set(t,n)),QG(this._accumulators.get(t),s)}compute(t){let s=this._accumulators.get(t);if(!s)return;let n=JG[this._reduceFns.get(t)];return n(s)}computeAll(){let t={};for(let[s]of this._accumulators)t[s]=this.compute(s);return t}reset(){this._accumulators.clear(),this._reduceFns.clear()}has(t){return this._accumulators.has(t)}get size(){return this._accumulators.size}},tf=class{static{c(this,"TrainerState")}constructor(){this.stage=Vs.IDLE,this.epoch=0,this.globalStep=0,this.maxEpochs=0,this.maxSteps=-1,this.shouldStop=!1,this.stepMetrics=new Zu,this.epochMetrics=new Zu}resetEpochMetrics(){this.epochMetrics.reset()}resetStepMetrics(){this.stepMetrics.reset()}},ef=class{static{c(this,"SingleDeviceStrategy")}constructor(){this.device=null}setup(t,s){this.device=s,(s.type==="gpu"||s.type==="webgpu")&&typeof t.to=="function"&&t.to(s)}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 s=new Array(t.length);for(let n=0;n<t.length;n++)s[n]=this.toDevice(t[n]);return s}return t}backward(t){t.backward()}optimizerStep(t){t.step()}};var Y1=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"}),sf=class{static{c(this,"CallbackConnector")}constructor(t=[]){this._callbacks=t}get callbacks(){return this._callbacks}add(t){this._callbacks.push(t)}dispatch(t,...s){for(let n=0;n<this._callbacks.length;n++){let r=this._callbacks[n][t];r&&r.call(this._callbacks[n],...s)}}remove(t){let s=this._callbacks.indexOf(t);s!==-1&&this._callbacks.splice(s,1)}},nf=class{static{c(this,"LoggerConnector")}constructor(t=[],s=null){this._loggers=Array.isArray(t)?t:[t],this._state=s}drain(t){let s=t._logBuffer;if(s.size!==0){for(let[n,r]of s){let{value:i,onStep:o,onEpoch:a,reduceFx:l,progBar:u}=r;if(a&&this._state.epochMetrics.update(n,i,l),o&&this._state.stepMetrics.update(n,i,l),u){this._state._progBarMetrics||(this._state._progBarMetrics=new Map);let f=typeof i=="number"?i:i.item();this._state._progBarMetrics.set(n,f)}}s.clear()}}flushStepMetrics(t){let s=this._state.stepMetrics.computeAll();if(Object.keys(s).length===0)return s;for(let n=0;n<this._loggers.length;n++)this._loggers[n].logMetrics(s,t);return this._state.stepMetrics.reset(),s}flushEpochMetrics(t){let s=this._state.epochMetrics.computeAll();if(Object.keys(s).length===0)return s;for(let n=0;n<this._loggers.length;n++)this._loggers[n].logMetrics(s,t);return this._state.epochMetrics.reset(),s}logHyperparams(t){for(let s=0;s<this._loggers.length;s++)this._loggers[s].logHyperparams(t)}};te();Xi();Ai();var rf=class{static{c(this,"TrainingLoop")}async run(t,s,n,r,i){let o=n.state,a=n.callbackConnector,l=n.loggerConnector,u=n.strategy,f=n.accumulateGradBatches,h=XG(n.limitTrainBatches,s.length);o.numTrainingBatches=h,o.stage=Vs.TRAINING,t.train(),Rr.setEnabled(!0),t.onTrainEpochStart(),a.dispatch("onTrainEpochStart",n,t);let p=0;for(let m of s){if(p>=h||o.shouldStop)break;if(o.maxSteps>0&&o.globalStep>=o.maxSteps){o.shouldStop=!0;break}let g=u.toDevice(m);a.dispatch("onTrainBatchStart",n,t,g,p);let y;t.automaticOptimization?y=await this._automaticStep(t,g,p,n,r,i,u,f,a):y=await Promise.resolve(t.trainingStep(g,p)),l.drain(t),o.globalStep%n.logEveryNSteps===0&&l.flushStepMetrics(o.globalStep),a.dispatch("onTrainBatchEnd",n,t,y,g,p),c0(),o.globalStep++,p++}c0();let d=l.flushEpochMetrics(o.globalStep);return this._stepEpochSchedulers(i,o.epoch),t.onTrainEpochEnd(),a.dispatch("onTrainEpochEnd",n,t),d}async _automaticStep(t,s,n,r,i,o,a,l,u){if(r.compile)return this._compiledStep(t,s,n,r,i,o,l);if(r.cudaGraph)return this._graphedStep(t,s,r,i,o,a);let f=await Promise.resolve(t.trainingStep(s,n)),h=f,p=f;if(f&&typeof f=="object"&&!f.backward&&(h=f.loss,p=f),l>1&&(h=oe(h,l)),u.dispatch("onBeforeBackward",r,t,h),a.backward(h),u.dispatch("onAfterBackward",r,t),(n+1)%l===0){for(let m=0;m<i.length;m++)this._clipGradients(t,r),u.dispatch("onBeforeOptimizerStep",r,t,i[m]),a.optimizerStep(i[m]),u.dispatch("onBeforeZeroGrad",r,t,i[m]),i[m].zeroGrad();this._stepStepSchedulers(o,r.state.globalStep)}return p}async _compiledStep(t,s,n,r,i,o,a){let l=Array.isArray(s)?s:[s],u=c((...y)=>t.trainingStep(Array.isArray(s)?y:y[0],0),"callForward"),f;if(t.__compiledTrainStep)f=t.__compiledTrainStep(...l),f&&f.then&&(f=await f);else{let{compileWithBackward:y}=await Promise.resolve().then(()=>(mv(),K1)),{CPUTarget:_,CUDATarget:w,WebGPUTarget:b}=await Promise.resolve().then(()=>(hi(),lR)),x=t._device&&t._device.type,A=x==="webgpu"?b():x==="gpu"?w():_();t.__compiledTrainStep=y({forward:u},l,{target:A,mode:r.compileMode});let S=t.log;t.log=()=>{};try{f=t.__compiledTrainStep(...l),f&&f.then&&(f=await f)}finally{t.log=S}}let h=t.__compiledTrainStep,p=h.capturedParams(),{ones:d}=await Promise.resolve().then(()=>(Ne(),nC)),m=h.backward(d(f.shape));m&&m.then&&(m=await m);let g=m.length-p.length;for(let y=0;y<p.length;y++){let _=m[g+y];_&&(p[y].grad=_)}if((n+1)%a===0){for(let y=0;y<i.length;y++)this._clipGradients(t,r),i[y].step(),i[y].zeroGrad();this._stepStepSchedulers(o,r.state.globalStep)}return t.log&&t.log("train_loss",f),f}async _eagerTrainStepCore(t,s,n,r,i){let o=await Promise.resolve(t.trainingStep(s,0)),a=o;if(o&&typeof o=="object"&&!o.backward&&(a=o.loss),r.backward(a),i&&i.gradientClipVal){let{deviceClipGradNorm:l}=await Promise.resolve().then(()=>(dg(),pg));l([...t.parameters()],i.gradientClipVal)}for(let l=0;l<n.length;l++)r.optimizerStep(n[l]),n[l].zeroGrad();return a}async _graphedStep(t,s,n,r,i,o){let a=await Promise.resolve().then(()=>(Lm(),aB)),l=await Promise.resolve().then(()=>(bh(),EB)),u=await Promise.resolve().then(()=>(al(),KR)),f=t.__eagerGraphRunner;if(f||(f=t.__eagerGraphRunner={phase:"warmup",seen:0},l.setEagerDeferred(!0),Oc(!0)),f.phase==="disabled")return this._eagerTrainStepCore(t,s,r,o,n);if(f.phase==="warmup"&&f.seen<n.cudaGraphWarmupSteps)return f.seen++,this._eagerTrainStepCore(t,s,r,o,n);let h=gv(s);if(f.phase==="warmup"){f.inputs=h.map(y=>{let _=y._impl.storage.rawData;return l.deviceBufferForInput(_),l.pinResident(_),{dptr:l.deviceBufferDptr(_)}});let d=t.log,m;try{a.beginEagerCapture(),t.log=()=>{};try{m=await this._eagerTrainStepCore(t,s,r,o,n)}finally{t.log=d}f.captured=a.endEagerCapture()}catch(y){try{a.endEagerCapture()}catch{}if(t.log=d,l.clearCapturePins(),f.phase="disabled",f.captureError=y&&y.message,process.env.MLFW_DEBUG_CUDAGRAPH)throw y;return this._eagerTrainStepCore(t,s,r,o,n)}f.exec=f.captured.exec,f.lossDptr=l.deviceBufferDptr(m._impl.storage.rawData),f.lossScratch=new Float32Array(1),f.phase="replay",a.replay(f.exec),a.syncStream(),u.copyDeviceToHost(f.lossScratch,f.lossDptr);let g=f.lossScratch[0];return this._logGraphLoss(n,g),this._stepStepSchedulers(i,n.state.globalStep),g}for(let d=0;d<f.inputs.length&&d<h.length;d++)f.inputs[d].dptr&&u.copyHostToDeviceAsync(f.inputs[d].dptr,h[d]._impl.storage.rawData);a.replay(f.exec),a.syncStream(),u.copyDeviceToHost(f.lossScratch,f.lossDptr);let p=f.lossScratch[0];return this._logGraphLoss(n,p),this._stepStepSchedulers(i,n.state.globalStep),p}_logGraphLoss(t,s){t.state.stepMetrics.update("train_loss",s),t.state.epochMetrics.update("train_loss",s),t.state._progBarMetrics||(t.state._progBarMetrics=new Map),t.state._progBarMetrics.set("train_loss",s)}_clipGradients(t,s){if(!s.gradientClipVal)return;let n=[...t.parameters()];s.gradientClipAlgorithm==="norm"?Ou(n,s.gradientClipVal):Mu(n,s.gradientClipVal)}_stepStepSchedulers(t,s){if(t)for(let n=0;n<t.length;n++){let r=t[n];!r||r.interval!=="step"||r.monitor||(s+1)%r.frequency===0&&r.scheduler.step()}}_stepEpochSchedulers(t,s){if(t)for(let n=0;n<t.length;n++){let r=t[n];!r||r.interval!=="epoch"||r.monitor||(s+1)%r.frequency===0&&r.scheduler.step()}}};function gv(e,t=[]){if(e==null)return t;if(e.shape!==void 0&&typeof e.contiguous=="function")return t.push(e),t;if(Array.isArray(e)){for(let s of e)gv(s,t);return t}if(typeof e=="object"){for(let s of Object.keys(e))gv(e[s],t);return t}return t}c(gv,"_flattenTensors");function XG(e,t){return e==null?t:typeof e=="number"?e>0&&e<=1?Math.max(1,Math.round(e*t)):Math.min(e,t):t}c(XG,"resolveLimit");var of=class{static{c(this,"ValidationLoop")}async run(t,s,n,r){let i=n.state,o=n.callbackConnector,a=n.loggerConnector,l=i.stage;i.stage=Vs.VALIDATING,t.eval(),t.onValidationEpochStart(),o.dispatch("onValidationStart",n,t),o.dispatch("onValidationEpochStart",n,t);let u=t4(n.limitValBatches,s.length);i.numValBatches=u;let f=0;await ZG(async()=>{for(let p of s){if(f>=u)break;o.dispatch("onValidationBatchStart",n,t,p,f);let d=await Promise.resolve(t.validationStep(p,f));a.drain(t),o.dispatch("onValidationBatchEnd",n,t,d,p,f),f++}});let h=a.flushEpochMetrics(i.globalStep);return this._stepPlateauSchedulers(r,h),t.onValidationEpochEnd(),o.dispatch("onValidationEpochEnd",n,t),o.dispatch("onValidationEnd",n,t),t.train(),i.stage=l,h}_stepPlateauSchedulers(t,s){if(t)for(let n=0;n<t.length;n++){let r=t[n];if(!r||!r.monitor)continue;let i=r.scheduler;if(typeof i.step=="function"&&i.step.length>0){let o=s[r.monitor];o!==void 0&&i.step(o)}}}};async function ZG(e){let{GradMode:t}=await Promise.resolve().then(()=>(Ai(),cg)),s=t.isEnabled();t.setEnabled(!1);try{await e()}finally{t.setEnabled(s)}}c(ZG,"noGradAsync");function t4(e,t){return e==null?t:typeof e=="number"?e>0&&e<=1?Math.max(1,Math.round(e*t)):Math.min(e,t):t}c(t4,"resolveLimit");var af=class{static{c(this,"FitLoop")}constructor(){this._trainingLoop=new rf,this._validationLoop=new of}get trainingLoop(){return this._trainingLoop}get validationLoop(){return this._validationLoop}async run(t,s,n,r,i,o){let a=r.state,l=r.callbackConnector;l.dispatch("onTrainStart",r,t);for(let u=0;u<a.maxEpochs&&!(a.shouldStop||a.maxSteps>0&&a.globalStep>=a.maxSteps||(a.epoch=u,a.resetEpochMetrics(),await this._trainingLoop.run(t,s,r,i,o),a.shouldStop));u++)n&&this._shouldRunValidation(u,r)&&await this._validationLoop.run(t,n,r,o);l.dispatch("onTrainEnd",r,t)}_shouldRunValidation(t,s){let n=s.checkValEveryNEpoch;return(t+1)%n===0}};var lf=class{static{c(this,"EvaluationLoop")}async run(t,s,n){let r=n.state,i=n.callbackConnector,o=n.loggerConnector,a=r.stage;r.stage=Vs.TESTING,t.eval(),t.onTestEpochStart(),i.dispatch("onTestStart",n,t),i.dispatch("onTestEpochStart",n,t);let l=s4(n.limitTestBatches,s.length),u=0;await e4(async()=>{for(let h of s){if(u>=l)break;i.dispatch("onTestBatchStart",n,t,h,u);let p=await Promise.resolve(t.testStep(h,u));o.drain(t),i.dispatch("onTestBatchEnd",n,t,p,h,u),u++}});let f=o.flushEpochMetrics(r.globalStep);return t.onTestEpochEnd(),i.dispatch("onTestEpochEnd",n,t),i.dispatch("onTestEnd",n,t),t.train(),r.stage=a,f}};async function e4(e){let{GradMode:t}=await Promise.resolve().then(()=>(Ai(),cg)),s=t.isEnabled();t.setEnabled(!1);try{await e()}finally{t.setEnabled(s)}}c(e4,"noGradAsync");function s4(e,t){return e==null?t:typeof e=="number"?e>0&&e<=1?Math.max(1,Math.round(e*t)):Math.min(e,t):t}c(s4,"resolveLimit");var cf=class{static{c(this,"PredictionLoop")}async run(t,s,n){let r=n.state,i=n.callbackConnector,o=n.strategy,a=r.stage;r.stage=Vs.PREDICTING,t.eval(),i.dispatch("onPredictStart",n,t);let l=[],u=r4(n.limitTestBatches,s.length),f=0;return await n4(async()=>{for(let h of s){if(f>=u)break;let p=o.toDevice(h);i.dispatch("onPredictBatchStart",n,t,p,f);let d=await Promise.resolve(t.predictStep(p,f));await n._flushEagerInference(),l.push(d),i.dispatch("onPredictBatchEnd",n,t,d,p,f),f++}}),i.dispatch("onPredictEnd",n,t),t.train(),r.stage=a,l}};async function n4(e){let{GradMode:t}=await Promise.resolve().then(()=>(Ai(),cg)),s=t.isEnabled();t.setEnabled(!1);try{await e()}finally{t.setEnabled(s)}}c(n4,"noGradAsync");function r4(e,t){return e==null?t:typeof e=="number"?e>0&&e<=1?Math.max(1,Math.round(e*t)):Math.min(e,t):t}c(r4,"resolveLimit");var Oi=class{static{c(this,"Logger")}constructor({name:t="default",version:s=0}={}){this._name=t,this._version=s}get name(){return this._name}get version(){return this._version}logMetrics(t,s){throw new Error(`${this.constructor.name}.logMetrics() not implemented`)}logHyperparams(t){throw new Error(`${this.constructor.name}.logHyperparams() not implemented`)}finalize(){}};var Mi=class extends Oi{static{c(this,"ConsoleLogger")}constructor(t={}){super(t),this._logFrequency=t.logFrequency||1,this._callCount=0}logMetrics(t,s){if(this._callCount++,this._callCount%this._logFrequency!==0)return;let n=[`[step ${s}]`],r=Object.keys(t).sort();for(let i=0;i<r.length;i++){let o=t[r[i]];n.push(`${r[i]}: ${i4(o)}`)}console.log(n.join(" | "))}logHyperparams(t){let s=["[hyperparams]"],n=Object.keys(t).sort();for(let r=0;r<n.length;r++)s.push(`${n[r]}: ${t[n[r]]}`);console.log(s.join(" | "))}};function i4(e){return typeof e!="number"||Number.isInteger(e)?String(e):Math.abs(e)<.001&&e!==0?e.toExponential(3):e.toFixed(4)}c(i4,"formatNumber");var Gs=class{static{c(this,"Callback")}setup(t,s,n){}teardown(t,s,n){}onFitStart(t,s){}onFitEnd(t,s){}onTrainStart(t,s){}onTrainEnd(t,s){}onTrainEpochStart(t,s){}onTrainEpochEnd(t,s){}onTrainBatchStart(t,s,n,r){}onTrainBatchEnd(t,s,n,r,i){}onValidationStart(t,s){}onValidationEnd(t,s){}onValidationEpochStart(t,s){}onValidationEpochEnd(t,s){}onValidationBatchStart(t,s,n,r){}onValidationBatchEnd(t,s,n,r,i){}onTestStart(t,s){}onTestEnd(t,s){}onTestBatchStart(t,s,n,r){}onTestBatchEnd(t,s,n,r,i){}onPredictStart(t,s){}onPredictEnd(t,s){}onPredictBatchStart(t,s,n,r){}onPredictBatchEnd(t,s,n,r,i){}onBeforeBackward(t,s,n){}onAfterBackward(t,s){}onBeforeOptimizerStep(t,s,n){}onBeforeZeroGrad(t,s,n){}onSaveCheckpoint(t,s,n){}onLoadCheckpoint(t,s,n){}};var o4=["\u258F","\u258E","\u258D","\u258C","\u258B","\u258A","\u2589"],Zr=class extends Gs{static{c(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,s){this._trainBatchCount=0,this._epochStartTime=Date.now();let n=this._trainTotal(t);n&&this._render("Epoch",t.state.epoch+1,t.state.maxEpochs,0,n,t.state)}onTrainBatchEnd(t,s,n,r,i){this._trainBatchCount++;let o=this._trainTotal(t);o&&this._render("Epoch",t.state.epoch+1,t.state.maxEpochs,this._trainBatchCount,o,t.state)}onTrainEpochEnd(t,s){let n=this._trainTotal(t);n&&this._render("Epoch",t.state.epoch+1,t.state.maxEpochs,n,n,t.state)}onTrainEnd(t,s){this._active&&process.stdout.write(`
205
- `),this._active=!1,this._lastLen=0}onValidationEpochStart(t,s){this._valBatchCount=0,this._epochStartTime=Date.now()}onValidationBatchEnd(t,s,n,r,i){this._valBatchCount++;let o=this._valTotal(t);o&&this._render("Validation",null,null,this._valBatchCount,o,t.state)}onValidationEnd(t,s){let n=this._valTotal(t);n&&this._render("Validation",null,null,n,n,t.state)}_trainTotal(t){return t.state.numTrainingBatches??Q1(t.limitTrainBatches)}_valTotal(t){return t.state.numValBatches??Q1(t.limitValBatches)}_render(t,s,n,r,i,o){let a=i>0?Math.min(r/i,1):0,l=String(Math.round(a*100)).padStart(3," "),u=this._bar(a),f=s!==null?`${t} ${s}/${n}`:t,h=(Date.now()-this._epochStartTime)/1e3,p=h>0?r/h:0,d=p>0?(i-r)/p:0,m=`${X1(h)}<${X1(d)}, ${p.toFixed(2)}it/s`,g=this._formatProgBarMetrics(o),y=`${f}: ${l}%|${u}| ${r}/${i} [${m}${g}]`,_=Math.max(0,this._lastLen-y.length);process.stdout.write("\r"+y+" ".repeat(_)),this._lastLen=y.length,this._active=!0}_bar(t){let s=this._barLength,n=t*s,r=Math.floor(n),i=Math.round((n-r)*8);if(i===8&&(r+=1,i=0),r>=s)return"\u2588".repeat(s);let o=i>0?o4[i-1]:"",a=s-r-(o?1:0);return"\u2588".repeat(r)+o+" ".repeat(a)}_formatProgBarMetrics(t){let s=t._progBarMetrics;if(!s||s.size===0)return"";let n=[];for(let[r,i]of s)n.push(`${r}=${a4(i)}`);return", "+n.join(", ")}};function Q1(e){return e==null?null:typeof e=="number"&&e>1?e:null}c(Q1,"resolveTotal");function X1(e){(!Number.isFinite(e)||e<0)&&(e=0);let t=Math.round(e),s=Math.floor(t/60),n=t%60;return`${String(s).padStart(2,"0")}:${String(n).padStart(2,"0")}`}c(X1,"fmtTime");function a4(e){return typeof e!="number"||Number.isInteger(e)?String(e):e.toFixed(4)}c(a4,"formatNum");function wo(...e){return e.filter(t=>t!=null&&t!=="").join("/").replace(/\/+/g,"/")}c(wo,"joinPath");Js();$o();ir();var tM="mlfw-ckpt-v1",Pp=8,l4={Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array};function c4(e){return ArrayBuffer.isView(e)&&!(e instanceof DataView)}c(c4,"isTypedArray");function yv(e){return new Uint8Array(e.buffer,e.byteOffset,e.byteLength)}c(yv,"byteView");function uf(e){let t=[],s=J_(e,t),n=0;for(let l of t)l.meta.offset=n,l.meta.length=l.bytes.length,n+=l.bytes.length;let r={format:tM,tree:s,buffers:t.map(l=>l.meta)},i=new TextEncoder().encode(JSON.stringify(r)),o=new Uint8Array(Pp+i.length+n);new DataView(o.buffer).setBigUint64(0,BigInt(i.length),!0),o.set(i,Pp);let a=Pp+i.length;for(let l of t)o.set(l.bytes,a),a+=l.bytes.length;return o}c(uf,"serializeCheckpoint");function Q_(e){let t=e instanceof Uint8Array?e:new Uint8Array(e),s=new DataView(t.buffer,t.byteOffset,t.byteLength),n=Number(s.getBigUint64(0,!0)),r=Pp+n,i=JSON.parse(new TextDecoder().decode(t.subarray(Pp,r)));if(i.format!==tM)throw new Error("mlfw: unrecognized checkpoint format: "+i.format);let o=i.buffers.map(a=>u4(a,t,r));return Y_(i.tree,o)}c(Q_,"deserializeCheckpoint");function J_(e,t){if(e instanceof Et)return Z1(t,{kind:"tensor",dtype:e.dtype,shape:e.shape},yv(e._impl.storage.data));if(c4(e))return Z1(t,{kind:"array",arrayType:e.constructor.name},yv(e));if(e instanceof Map){let s=[];for(let[n,r]of e)s.push([n,J_(r,t)]);return{$map:s}}if(Array.isArray(e))return e.map(s=>J_(s,t));if(e&&typeof e=="object"){let s={};for(let n of Object.keys(e))s[n]=J_(e[n],t);return s}return e}c(J_,"encodeNode");function Z1(e,t,s){let n=e.length;return e.push({meta:t,bytes:s}),{$buf:n}}c(Z1,"pushBuffer");function u4(e,t,s){let n=s+e.offset,r=t.subarray(n,n+e.length),i=e.kind==="tensor"?me(e.dtype):l4[e.arrayType];if(!i)throw new Error("mlfw: unknown typed array in checkpoint: "+e.arrayType);let o=new i(e.length/i.BYTES_PER_ELEMENT);return yv(o).set(r),e.kind==="tensor"?Fo(o,e.shape,e.dtype):o}c(u4,"decodeBuffer");function Y_(e,t){if(e===null||typeof e!="object")return e;if(Array.isArray(e))return e.map(n=>Y_(n,t));if("$buf"in e)return t[e.$buf];if("$map"in e){let n=new Map;for(let[r,i]of e.$map)n.set(r,Y_(i,t));return n}let s={};for(let n of Object.keys(e))s[n]=Y_(e[n],t);return s}c(Y_,"decodeNode");var _v=".ckpt",ti=class extends Gs{static{c(this,"ModelCheckpoint")}constructor({dirpath:t="./lightning_logs/checkpoints",filename:s="epoch={epoch}-step={step}",monitor:n=null,mode:r="min",saveTopK:i=1,saveLast:o=!0,everyNEpochs:a=1}={}){super(),this._dirpath=t,this._filename=s,this._monitor=n,this._mode=r,this._saveTopK=i,this._saveLast=o,this._everyNEpochs=a,this._bestK=[],this._recent=[],this._compareFn=r==="min"?(l,u)=>l-u:(l,u)=>u-l,this._bestModelPath=null,this._lastModelPath=null}get bestModelPath(){return this._bestModelPath}get lastModelPath(){return this._lastModelPath}get bestKModels(){return this._bestK}onTrainEpochEnd(t,s){let n=t.state;if((n.epoch+1)%this._everyNEpochs!==0)return;this._ensureDir();let r=this._fillTemplate(n);if(this._saveLast){let f=wo(this._dirpath,"last"+_v);this._saveCheckpoint(s,t,f),this._lastModelPath=f}if(!this._monitor){let f=wo(this._dirpath,r+_v);if(this._saveCheckpoint(s,t,f),this._saveTopK>=0)for(this._recent.push(f);this._recent.length>this._saveTopK;)this._tryDelete(this._recent.shift());return}let o=n.epochMetrics.computeAll()[this._monitor];if(o===void 0)return;let a=wo(this._dirpath,r+_v),l={score:o,path:a};if(this._saveTopK<0){this._saveCheckpoint(s,t,a),this._bestK.push(l),this._updateBest();return}let u=this._findInsertIndex(o);if(u<this._saveTopK){if(this._saveCheckpoint(s,t,a),this._bestK.splice(u,0,l),this._bestK.length>this._saveTopK){let f=this._bestK.pop();this._tryDelete(f.path)}this._updateBest()}}_findInsertIndex(t){let s=0,n=this._bestK.length;for(;s<n;){let r=s+n>>>1;this._compareFn(t,this._bestK[r].score)<0?n=r:s=r+1}return s}_updateBest(){this._bestK.length>0&&(this._bestModelPath=this._bestK[0].path)}_saveCheckpoint(t,s,n){let r={epoch:s.state.epoch,globalStep:s.state.globalStep,modelState:t.stateDict()},i=t._currentOptimizers;i&&i.length>0&&(r.optimizerStates=i.map(a=>a.stateDict())),s.callbackConnector.dispatch("onSaveCheckpoint",s,t,r);let o=n+".tmp";_e.writeBinary(o,uf(r)),_e.rename(o,n)}_fillTemplate(t){return this._filename.replace("{epoch}",t.epoch).replace("{step}",t.globalStep)}_ensureDir(){_e.exists(this._dirpath)||_e.mkdir(this._dirpath)}_tryDelete(t){try{_e.remove(t)}catch{}}};function ff(e){return Q_(_e.readBinary(e))}c(ff,"loadCheckpoint");function hf(e,t,s=[]){if(e.modelState&&t.loadStateDict(e.modelState),e.optimizerStates){let n=Math.min(s.length,e.optimizerStates.length);for(let r=0;r<n;r++)s[r].loadStateDict(e.optimizerStates[r])}return e}c(hf,"applyCheckpoint");vn();var _a=class{static{c(this,"Trainer")}constructor({maxEpochs:t=10,maxSteps:s=-1,accelerator:n="auto",precision:r="f32",callbacks:i=[],logger:o=!0,enableCheckpointing:a=!1,enableProgress:l=!0,gradientClipVal:u=null,gradientClipAlgorithm:f="norm",accumulateGradBatches:h=1,limitTrainBatches:p=null,limitValBatches:d=null,limitTestBatches:m=null,valCheckInterval:g=1,checkValEveryNEpoch:y=1,logEveryNSteps:_=50,deterministic:w=!1,fastDevRun:b=!1,defaultRootDir:x="./lightning_logs",compile:A=!1,compileMode:S="separate",cudaGraph:E=!1,cudaGraphWarmupSteps:R=3}={}){if(this._state=new tf,this._state.maxEpochs=t,this._state.maxSteps=s,this._compile=A,this._compileMode=S,this._cudaGraph=E,this._cudaGraphWarmupSteps=R,this._accelerator=n,this._precision=r,this._gradientClipVal=u,this._gradientClipAlgorithm=f,this._accumulateGradBatches=h,this._limitTrainBatches=p,this._limitValBatches=d,this._limitTestBatches=m,this._valCheckInterval=g,this._checkValEveryNEpoch=y,this._logEveryNSteps=_,this._deterministic=w,this._defaultRootDir=x,b!==!1){let k=typeof b=="number"?b:1;this._limitTrainBatches=k,this._limitValBatches=k,this._limitTestBatches=k,this._state.maxEpochs=1}this._loggers=this._resolveLoggers(o),this._strategy=new ef,this._fitLoop=new af,this._evaluationLoop=new lf,this._predictionLoop=new cf;let v=[...i];l&&!v.some(k=>k instanceof Zr)&&v.push(new Zr),a&&!v.some(k=>k instanceof ti)&&v.push(new ti({dirpath:x})),this._callbackConnector=new sf(v),this._loggerConnector=new nf(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,s,n=null){this._model=t,t._trainer=this;let r=this._resolveDevice();this._guardEagerWebGPU(r,"fit",n!=null),this._guardCudaGraph(r,n!=null),t._device=r,await this._prepareDevice(r),this._strategy.setup(t,r);let{optimizers:i,schedulerConfigs:o}=J1(await Promise.resolve(t.configureOptimizers()));if(this._cudaGraph&&o&&o.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=i,this._loggerConnector.logHyperparams(this._extractHyperparams(t,i)),this._callbackConnector.dispatch("setup",this,t,Vs.TRAINING),this._callbackConnector.dispatch("onFitStart",this,t),this._state.shouldStop=!1,await this._fitLoop.run(t,s,n,this,i,o),r===fl){let{teardownAfterFit:a}=await Promise.resolve().then(()=>(dg(),pg));a(t,i)}this._callbackConnector.dispatch("onFitEnd",this,t),this._callbackConnector.dispatch("teardown",this,t,Vs.TRAINING);for(let a=0;a<this._loggers.length;a++)this._loggers[a].finalize()}async validate(t,s){t._trainer=this,this._model=t;let n=this._resolveDevice();this._guardEagerWebGPU(n,"validate"),t._device=n,await this._prepareDevice(n),this._strategy.setup(t,n),this._callbackConnector.dispatch("setup",this,t,Vs.VALIDATING);let r=await this._fitLoop.validationLoop.run(t,s,this,null);this._callbackConnector.dispatch("teardown",this,t,Vs.VALIDATING);for(let i=0;i<this._loggers.length;i++)this._loggers[i].finalize();return r}async test(t,s){t._trainer=this,this._model=t;let n=this._resolveDevice();this._guardEagerWebGPU(n,"test"),t._device=n,await this._prepareDevice(n),this._strategy.setup(t,n),this._callbackConnector.dispatch("setup",this,t,Vs.TESTING);let r=await this._evaluationLoop.run(t,s,this);this._callbackConnector.dispatch("teardown",this,t,Vs.TESTING);for(let i=0;i<this._loggers.length;i++)this._loggers[i].finalize();return r}async predict(t,s){t._trainer=this,this._model=t;let n=this._resolveDevice();return t._device=n,await this._prepareDevice(n),this._strategy.setup(t,n),await this._predictionLoop.run(t,s,this)}_resolveDevice(){return this._accelerator==="gpu"?fl:this._accelerator==="wasm"?Fc:this._accelerator==="webgpu"?Sr:this._accelerator==="cpu"?Dc:Dc}_guardEagerWebGPU(t,s,n=!1){if(t.type==="webgpu"){if(s==="fit"){if(!this._compile)throw new Error('Trainer(accelerator="webgpu"): eager WebGPU is inference-only (CUSTOM_0 dispatch has no autograd key). Pass compile=true to train on WebGPU, or use predict() for eager inference.');if(n)throw new Error(`Trainer(accelerator="webgpu"): in-fit validation is unsupported \u2014 validationStep runs eagerly and reads scalar metrics via .item(), which WebGPU's asynchronous readback cannot serve, and there is no compiled validation path. Call fit() without a valLoader on WebGPU.`);return}throw new Error(`Trainer(accelerator="webgpu"): ${s}() 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,s=!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(s)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:s}=await Promise.resolve().then(()=>(tp(),zx));await s()}else if(t.type==="webgpu"){let{preloadWebGPU:s}=await Promise.resolve().then(()=>(tp(),zx));this._webgpuMod=await s()}}async _flushEagerInference(){this._webgpuMod&&await this._webgpuMod.flushWebGPUEager()}_resolveLoggers(t){return t===!1||t===null?[]:t===!0?[new Mi]:Array.isArray(t)?t:[t]}_extractHyperparams(t,s){let n={maxEpochs:this._state.maxEpochs,maxSteps:this._state.maxSteps,accelerator:this._accelerator,precision:this._precision,accumulateGradBatches:this._accumulateGradBatches};for(let r=0;r<s.length;r++){let i=s[r],o=i.defaults,a=s.length>1?`optimizer_${r}_`:"";n[a+"optimizer"]=i.constructor.name,o.lr!==void 0&&(n[a+"lr"]=o.lr),o.weightDecay!==void 0&&(n[a+"weight_decay"]=o.weightDecay)}return n}};var wa=class extends Gs{static{c(this,"EarlyStopping")}constructor({monitor:t="val_loss",patience:s=3,mode:n="min",minDelta:r=0,checkOnTrainEpochEnd:i=!1}={}){super(),this._monitor=t,this._patience=s,this._mode=n,this._minDelta=r,this._checkOnTrainEpochEnd=i,this._waitCount=0,this._bestScore=null,this._compareFn=n==="min"?(o,a)=>o<a-r:(o,a)=>o>a+r}get monitor(){return this._monitor}get patience(){return this._patience}get bestScore(){return this._bestScore}get waitCount(){return this._waitCount}onValidationEnd(t,s){this._checkOnTrainEpochEnd||this._check(t)}onTrainEpochEnd(t,s){this._checkOnTrainEpochEnd&&this._check(t)}_check(t){let n=t.state.epochMetrics.computeAll()[this._monitor];if(n!==void 0){if(this._bestScore===null||this._compareFn(n,this._bestScore)){this._bestScore=n,this._waitCount=0;return}this._waitCount++,this._waitCount>=this._patience&&(t.shouldStop=!0)}}reset(){this._waitCount=0,this._bestScore=null}};var ba=class extends Gs{static{c(this,"LearningRateMonitor")}constructor({logMomentum:t=!1}={}){super(),this._logMomentum=t,this._lrHistory={}}get lrHistory(){return this._lrHistory}onTrainBatchStart(t,s,n,r){let i=s._currentOptimizers;if(i)for(let o=0;o<i.length;o++){let a=i[o].paramGroups;for(let l=0;l<a.length;l++){let u=i.length>1||a.length>1?`lr_opt${o}_group${l}`:"lr",f=a[l].lr;if(s.log(u,f,{onStep:!0,onEpoch:!1,progBar:!1}),this._lrHistory[u]||(this._lrHistory[u]=[]),this._lrHistory[u].push({step:t.state.globalStep,lr:f}),this._logMomentum&&a[l].momentum!==void 0){let h=u.replace("lr","momentum");s.log(h,a[l].momentum,{onStep:!0,onEpoch:!1})}}}}};var xa=class extends Gs{static{c(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,s){this._fitStartTime=performance.now()}onFitEnd(t,s){this._totalTrainingTime=(performance.now()-this._fitStartTime)/1e3}onTrainEpochStart(t,s){this._epochStartTime=performance.now()}onTrainEpochEnd(t,s){this._epochDurations.push((performance.now()-this._epochStartTime)/1e3)}onValidationStart(t,s){this._valStartTime=performance.now()}onValidationEnd(t,s){this._validationDurations.push((performance.now()-this._valStartTime)/1e3)}};var Sa=class extends Gs{static{c(this,"GradientAccumulationScheduler")}constructor({scheduling:t}){super(),this._scheduling=new Map;let s=Object.entries(t);for(let n=0;n<s.length;n++)this._scheduling.set(Number(s[n][0]),s[n][1]);this._sortedEpochs=[...this._scheduling.keys()].sort((n,r)=>n-r)}onTrainEpochStart(t,s){let n=t.state.epoch;this._scheduling.has(n)&&(t.accumulateGradBatches=this._scheduling.get(n))}getCurrentAccumulation(t){let s=1;for(let n=0;n<this._sortedEpochs.length&&this._sortedEpochs[n]<=t;n++)s=this._scheduling.get(this._sortedEpochs[n]);return s}};var Aa=class extends Oi{static{c(this,"CSVLogger")}constructor({saveDir:t="./lightning_logs",name:s="default",version:n=null,flushInterval:r=10}={}){super({name:s,version:n||0}),this._saveDir=t,this._flushInterval=r,this._columns=[],this._columnSet=new Set,this._buffer=[],this._filePath=null,this._headerWritten=!1,this._version=n}get logDir(){let t=this._version!==null?this._version:this._resolveVersion();return wo(this._saveDir,this._name,`version_${t}`)}logMetrics(t,s){let n={step:s},r=Object.keys(t);for(let i=0;i<r.length;i++){let o=r[i];n[o]=t[o],this._columnSet.has(o)||(this._columnSet.add(o),this._columns.push(o),this._headerWritten=!1)}this._buffer.push(n),this._buffer.length>=this._flushInterval&&this._flush()}logHyperparams(t){this._ensureDir();let s=wo(this.logDir,"hparams.json");_e.writeFile(s,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(",");_e.writeFile(t,r+`
206
- `),this._headerWritten=!0}let s=["step",...this._columns],n=[];for(let r=0;r<this._buffer.length;r++){let i=this._buffer[r],o=[];for(let a=0;a<s.length;a++){let l=i[s[a]];o.push(l!==void 0?String(l):"")}n.push(o.join(","))}_e.appendFile(t,n.join(`
207
- `)+`
208
- `),this._buffer.length=0}_getFilePath(){return this._filePath||(this._filePath=wo(this.logDir,"metrics.csv")),this._filePath}_ensureDir(){let t=this.logDir;_e.exists(t)||_e.mkdir(t)}_resolveVersion(){if(this._version!==null)return this._version;let t=wo(this._saveDir,this._name);if(!_e.exists(t))return this._version=0,0;let s=-1;try{let n=_e.readdir(t);for(let r=0;r<n.length;r++){let i=n[r].match(/^version_(\d+)$/);if(i){let o=parseInt(i[1],10);o>s&&(s=o)}}}catch{}return this._version=s+1,this._version}};var tn=class{static{c(this,"Metric")}constructor(){this._computed=null}update(t,s){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,s){return this.update(t,s),this._computed=this.compute(),this._computed}get value(){return this._computed}};var pf=class extends tn{static{c(this,"MeanMetric")}constructor(){super(),this._sum=0,this._count=0}update(t,s=1){let n=typeof t=="number"?t:t.item();this._sum+=n*s,this._count+=s}compute(){return this._count===0?0:this._sum/this._count}reset(){super.reset(),this._sum=0,this._count=0}},df=class extends tn{static{c(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 va=class{static{c(this,"MetricCollection")}constructor(t={}){this._metrics=new Map;let s=Object.keys(t);for(let n=0;n<s.length;n++)this._metrics.set(s[n],t[s[n]])}add(t,s){return this._metrics.set(t,s),this}update(t,s){for(let[,n]of this._metrics)n.update(t,s)}compute(){let t={};for(let[s,n]of this._metrics)t[s]=n.compute();return t}reset(){for(let[,t]of this._metrics)t.reset()}forward(t,s){return this.update(t,s),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 ka=class extends tn{static{c(this,"Accuracy")}constructor({task:t="multiclass",numClasses:s=null,topK:n=1,threshold:r=.5}={}){super(),this._task=t,this._numClasses=s,this._topK=n,this._threshold=r,this._correct=0,this._total=0}update(t,s){let n=t._impl.storage.data,r=s._impl.storage.data;this._task==="binary"?this._updateBinary(n,r):this._task==="multiclass"?this._updateMulticlass(t,s):this._task==="multilabel"&&this._updateMultilabel(n,r)}compute(){return this._total===0?0:this._correct/this._total}reset(){super.reset(),this._correct=0,this._total=0}_updateBinary(t,s){let n=s.length;for(let r=0;r<n;r++)(t[r]>=this._threshold?1:0)===s[r]&&this._correct++;this._total+=n}_updateMulticlass(t,s){let n=t.shape;if(n.length<2){let l=t._impl.storage.data,u=s._impl.storage.data,f=u.length;for(let h=0;h<f;h++)Math.round(l[h])===u[h]&&this._correct++;this._total+=f;return}let r=n[0],i=n[1],o=t._impl.storage.data,a=s._impl.storage.data;if(this._topK===1)for(let l=0;l<r;l++){let u=0,f=o[l*i];for(let h=1;h<i;h++){let p=o[l*i+h];p>f&&(f=p,u=h)}u===a[l]&&this._correct++}else for(let l=0;l<r;l++){let u=f4(o,l*i,i,this._topK);for(let f=0;f<u.length;f++)if(u[f]===a[l]){this._correct++;break}}this._total+=r}_updateMultilabel(t,s){let n=s.length;for(let r=0;r<n;r++)(t[r]>=this._threshold?1:0)===s[r]&&this._correct++;this._total+=n}};function f4(e,t,s,n){let r=[];for(let i=0;i<s;i++){let o=e[t+i];r.length<n?(r.push({val:o,idx:i}),r.length===n&&h4(r)):o>r[0].val&&(r[0]={val:o,idx:i},eM(r,0))}return r.map(i=>i.idx)}c(f4,"topKIndices");function h4(e){for(let t=(e.length>>>1)-1;t>=0;t--)eM(e,t)}c(h4,"heapify");function eM(e,t){let s=e.length;for(;;){let n=t,r=2*t+1,i=2*t+2;if(r<s&&e[r].val<e[n].val&&(n=r),i<s&&e[i].val<e[n].val&&(n=i),n===t)break;let o=e[t];e[t]=e[n],e[n]=o,t=n}}c(eM,"siftDown");var Ea=class extends tn{static{c(this,"Precision")}constructor({task:t="multiclass",numClasses:s=2,average:n="macro"}={}){super(),this._task=t,this._numClasses=s,this._average=n,this._tp=new Int32Array(s),this._fp=new Int32Array(s),this._support=new Int32Array(s)}update(t,s){wv(t,s,this._tp,this._fp,null,this._support,this._task,this._numClasses)}compute(){return d4(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)}},Na=class extends tn{static{c(this,"Recall")}constructor({task:t="multiclass",numClasses:s=2,average:n="macro"}={}){super(),this._task=t,this._numClasses=s,this._average=n,this._tp=new Int32Array(s),this._fn=new Int32Array(s),this._support=new Int32Array(s)}update(t,s){wv(t,s,this._tp,null,this._fn,this._support,this._task,this._numClasses)}compute(){return m4(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)}},Ra=class extends tn{static{c(this,"F1Score")}constructor({task:t="multiclass",numClasses:s=2,average:n="macro"}={}){super(),this._task=t,this._numClasses=s,this._average=n,this._tp=new Int32Array(s),this._fp=new Int32Array(s),this._fn=new Int32Array(s),this._support=new Int32Array(s)}update(t,s){wv(t,s,this._tp,this._fp,this._fn,this._support,this._task,this._numClasses)}compute(){let t=sM(this._tp,this._fp,this._numClasses),s=nM(this._tp,this._fn,this._numClasses),n=new Float64Array(this._numClasses);for(let r=0;r<this._numClasses;r++){let i=t[r]+s[r];n[r]=i>0?2*t[r]*s[r]/i:0}return bv(n,this._support,this._numClasses,this._average)}reset(){super.reset(),this._tp.fill(0),this._fp.fill(0),this._fn.fill(0),this._support.fill(0)}};function wv(e,t,s,n,r,i,o,a){let l=e._impl.storage.data,u=t._impl.storage.data,f=u.length,h=e.shape.length>=2&&e.shape[1]>1;for(let p=0;p<f;p++){let d=u[p]|0,m;o==="binary"?m=l[p]>=.5?1:0:h?m=p4(l,p,a):m=l[p]|0,i[d]++,m===d?s&&s[d]++:(n&&n[m]++,r&&r[d]++)}}c(wv,"updateConfusionCounters");function p4(e,t,s){let n=0,r=e[t*s];for(let i=1;i<s;i++){let o=e[t*s+i];o>r&&(r=o,n=i)}return n}c(p4,"argmaxRow");function sM(e,t,s){let n=new Float64Array(s);for(let r=0;r<s;r++){let i=e[r]+t[r];n[r]=i>0?e[r]/i:0}return n}c(sM,"computePrecisionPerClass");function nM(e,t,s){let n=new Float64Array(s);for(let r=0;r<s;r++){let i=e[r]+t[r];n[r]=i>0?e[r]/i:0}return n}c(nM,"computeRecallPerClass");function d4(e,t,s,n,r){let i=sM(e,t,n);return bv(i,s,n,r)}c(d4,"computePrecision");function m4(e,t,s,n,r){let i=nM(e,t,n);return bv(i,s,n,r)}c(m4,"computeRecall");function bv(e,t,s,n){if(n==="none")return[...e];if(n==="micro"){let o=0,a=0;for(let l=0;l<s;l++)o+=e[l]*t[l],a+=t[l];return a>0?o/a:0}if(n==="weighted"){let o=0,a=0;for(let l=0;l<s;l++)a+=e[l]*t[l],o+=t[l];return o>0?a/o:0}let r=0,i=0;for(let o=0;o<s;o++)t[o]>0&&(i+=e[o],r++);return r>0?i/r:0}c(bv,"aggregate");var Ba=class extends tn{static{c(this,"ConfusionMatrix")}constructor({numClasses:t}){super(),this._numClasses=t,this._matrix=new Int32Array(t*t)}update(t,s){let n=t._impl.storage.data,r=s._impl.storage.data,i=r.length,o=this._numClasses,a=t.shape.length>=2&&t.shape[1]>1;for(let l=0;l<i;l++){let u=r[l]|0,f;a?f=g4(n,l,o):f=n[l]|0,this._matrix[u*o+f]++}}compute(){let t=this._numClasses,s=[];for(let n=0;n<t;n++){let r=new Array(t);for(let i=0;i<t;i++)r[i]=this._matrix[n*t+i];s.push(r)}return s}reset(){super.reset(),this._matrix.fill(0)}};function g4(e,t,s){let n=0,r=e[t*s];for(let i=1;i<s;i++){let o=e[t*s+i];o>r&&(r=o,n=i)}return n}c(g4,"argmaxRow");JT();sI();rI(Et);Js();vn();tp();xg();Ai();Tl();G_();ps();var gf=class extends Error{static{c(this,"LangSyntaxError")}constructor(t,s,n){super(`${t} at ${s}:${n}`),this.name="LangSyntaxError",this.line=s,this.column=n}};function rM(e){return _4(y4(e))}c(rM,"tokenize");function y4(e){let t=[],s=0,n=1,r=1,i=c((a,l,u=n,f=r)=>{t.push({type:a,value:l,line:u,column:f,endLine:n,endColumn:r})},"push"),o=c(()=>{let a=e[s++];return a===`
209
- `?(n++,r=1):r++,a},"advance");for(;s<e.length;){let a=e[s];if(a===" "||a===" "||a==="\r"){o();continue}if(a===`
210
- `||a===";"){let p=n,d=r;o(),i("newline",`
211
- `,p,d);continue}if(a==="#"||a==="/"&&e[s+1]==="/"){for(;s<e.length&&e[s]!==`
212
- `;)o();continue}if(/[A-Za-z_]/.test(a)){let p=n,d=r,m="";for(;s<e.length&&/[A-Za-z0-9_]/.test(e[s]);)m+=o();i("identifier",m,p,d);continue}if(/[0-9]/.test(a)||a==="."&&/[0-9]/.test(e[s+1])){let p=n,d=r,m="";for(;s<e.length&&/[0-9.]/.test(e[s]);)m+=o();let g=m.includes(".");if(/[eE]/.test(e[s]||""))for(g=!0,m+=o(),/[+-]/.test(e[s]||"")&&(m+=o());/[0-9]/.test(e[s]||"");)m+=o();i("number",Number(m),p,d),t[t.length-1].float=g;continue}if(a==='"'||a==="'"){let p=n,d=r,m=o(),g="";for(;s<e.length&&e[s]!==m;)if(e[s]==="\\"){o();let y=o();g+=y==="n"?`
213
- `:y==="t"?" ":y}else g+=o();if(e[s]!==m)throw new gf("Unterminated string",p,d);o(),i("string",g,p,d);continue}let l=n,u=r;if(e.slice(s,s+3)==="**="){o(),o(),o(),i("symbol","**=",l,u);continue}let h=e.slice(s,s+2);if(["**","==","!=","<=",">=","+=","-=","*=","/=","@=","->"].includes(h)){o(),o(),i("symbol",h,l,u);continue}if("()[],.=:+-*/@<>{}|".includes(a)){o(),i("symbol",a,l,u);continue}throw new gf(`Unexpected character '${a}'`,n,r)}return i("eof",null),t}c(y4,"tokenizeRaw");function _4(e){let t=[],s=[0],n=0,r=!1;for(let i=0;i<e.length;i++){let o=e[i];if(o.type==="eof"){for(;s.length>1;)s.pop(),t.push({type:"dedent",value:null,line:o.line,column:o.column});t.push(o);break}if(o.type!=="string"&&((o.value==="("||o.value==="["||o.value==="{")&&n++,(o.value===")"||o.value==="]"||o.value==="}")&&n--),o.type==="newline"){if(n>0)continue;let a=i+1;for(;a<e.length&&e[a].type==="newline";)a++;if(a>=e.length||e[a].type==="eof"){for(t.push(o);s.length>1;)s.pop(),t.push({type:"dedent",value:null,line:o.line,column:o.column});i=a-1,r=!1;continue}if(e[a].type==="symbol"&&e[a].value==="."){i=a-1;continue}let l=e[a].column,u=s[s.length-1];if(t.push(o),r&&l>u)s.push(l),t.push({type:"indent",value:null,line:e[a].line,column:e[a].column});else if(l<u)for(;s.length>1&&s[s.length-1]>l;)s.pop(),t.push({type:"dedent",value:null,line:e[a].line,column:e[a].column});i=a-1,r=!1;continue}r=o.value===":",t.push(o)}return t}c(_4,"addIndentation");var w4={or:1,and:2,"==":3,"!=":3,"<":3,"<=":3,">":3,">=":3,"+":4,"-":4,"*":5,"/":5,"@":5,"**":6},yf={"+=":"+","-=":"-","*=":"*","/=":"/","**=":"**","@=":"@"};function X_(e){return new Sv(rM(e)).parseProgram()}c(X_,"parse");var Sv=class{static{c(this,"Parser")}constructor(t){this.tokens=t,this.pos=0}parseProgram(t=null){let s=[];for(this.skipLines();!this.at("eof")&&!(t&&(this.atValue(t)||this.at(t)));)s.push(this.parseStatement()),this.skipLines();return{type:"Program",body:s}}parseStatement(){let t=this.current();if(this.atIdentifier("model")&&this.peek(1).type==="identifier")return this.parseModel();if(this.atIdentifier("forward"))return this.parseForward();if(this.atIdentifier("train")&&this.peek(1).value!=="="&&!yf[this.peek(1).value])return this.parseTrain();if(this.atIdentifier("validate")&&this.peek(1).value!=="="&&!yf[this.peek(1).value])return this.parseValidate();if(this.atIdentifier("optimizer")&&this.peek(1).value===":")return this.parseOptimizer();if(this.atIdentifier("fn"))return this.parseFunctionDeclaration();if(this.atIdentifier("if"))return this.parseIf();if(this.atIdentifier("for"))return this.parseFor();if(this.atIdentifier("while"))return this.parseWhile();if(this.atIdentifier("break"))return this.next(),this.locate({type:"Break"},t);if(this.atIdentifier("continue"))return this.next(),this.locate({type:"Continue"},t);if(this.atIdentifier("return"))return this.next(),this.locate({type:"Return",value:this.parseExpression()},t);if(this.at("identifier")){let n=this.peek(1).value;if(n===","){let r=this.tryParseDestructureAssign();if(r)return r}if(n==="="){let r=this.next().value;return this.expectValue("="),this.locate({type:"Assign",name:r,value:this.parseExpression()},t)}if(n===":"){let r=this.next().value;this.expectValue(":");let i=this.parseTypeAnnotation();return this.expectValue("="),this.locate({type:"Assign",name:r,annotation:i,value:this.parseExpression()},t)}if(yf[n]!==void 0){let r=this.next().value,i=this.next(),o=yf[i.value];return this.locate({type:"CompoundAssign",name:r,op:o,value:this.parseExpression()},t)}}let s=this.parseExpression();if(s.type==="Index"&&this.atValue("="))return this.next(),this.locate({type:"IndexAssign",object:s.object,items:s.items,op:null,value:this.parseExpression()},t);if(s.type==="Index"&&yf[this.current().value]!==void 0){let n=yf[this.next().value];return this.locate({type:"IndexAssign",object:s.object,items:s.items,op:n,value:this.parseExpression()},t)}return this.locate({type:"ExpressionStatement",expression:s},t)}parseBlock(){if(this.expectValue(":"),!this.at("newline")&&!this.at("indent")&&!this.at("eof"))return[this.parseStatement()];this.skipLines(),this.expect("indent");let t=this.parseProgram("dedent").body;return this.expect("dedent"),t}parseIf(){let t=this.expectIdentifier("if"),s=this.parseExpression(),n=this.parseBlock(),r=[];for(this.skipLines();this.atIdentifier("else")&&this.peek(1).type==="identifier"&&this.peek(1).value==="if";){this.next(),this.next();let o=this.parseExpression(),a=this.parseBlock();this.skipLines(),r.push({condition:o,body:a})}let i=null;return this.atIdentifier("else")&&(this.next(),i=this.parseBlock()),this.locate({type:"If",condition:s,body:n,elifs:r,elseBody:i},t)}parseFor(){let t=this.expectIdentifier("for"),s=this.expect("identifier").value;this.expectIdentifier("in");let n=this.parseExpression(),r=this.parseBlock();return this.locate({type:"For",variable:s,iterable:n,body:r},t)}parseWhile(){let t=this.expectIdentifier("while"),s=this.parseExpression(),n=this.parseBlock();return this.locate({type:"While",condition:s,body:n},t)}parseFunctionDeclaration(){let t=this.expectIdentifier("fn"),s=this.expect("identifier").value,n=[],r=this.parseNameList(n),i=this.matchValue("->")?this.parseTypeAnnotation():null,o=this.parseBlock();return this.locate({type:"FunctionDeclaration",name:s,params:r,paramTypes:n,returnType:i,body:o},t)}parseModel(){let t=this.expectIdentifier("model"),s=this.expect("identifier").value,n=[],r=this.parseNameList(n),i=this.parseBlock();return this.locate({type:"ModelDeclaration",name:s,params:r,paramTypes:n,body:i},t)}parseForward(){let t=this.expectIdentifier("forward"),s=[],n=this.parseBlockParams(s),r=this.matchValue("->")?this.parseTypeAnnotation():null,i=this.parseBlock();return this.locate({type:"ForwardDeclaration",params:n,paramTypes:s,returnType:r,body:i},t)}parseTrain(){let t=this.expectIdentifier("train"),s=[],n=this.parseBlockParams(s),r=this.parseBlock();return this.locate({type:"TrainDeclaration",params:n,paramTypes:s,body:r},t)}parseValidate(){let t=this.expectIdentifier("validate"),s=[],n=this.parseBlockParams(s),r=this.parseBlock();return this.locate({type:"ValidateDeclaration",params:n,paramTypes:s,body:r},t)}parseOptimizer(){let t=this.expectIdentifier("optimizer"),s=this.parseBlock();return this.locate({type:"OptimizerDeclaration",body:s},t)}tryParseDestructureAssign(){let t=this.pos,s=this.current(),n=[this.next().value];for(;this.matchValue(",");){if(!this.at("identifier"))return this.pos=t,null;n.push(this.next().value)}return this.atValue("=")?(this.next(),this.locate({type:"DestructureAssign",names:n,value:this.parseExpression()},s)):(this.pos=t,null)}parseNameList(t=null){if(!this.matchValue("("))return[];let s=[];if(!this.atValue(")"))do{s.push(this.expect("identifier").value);let n=this.matchValue(":")?this.parseTypeAnnotation():null;if(t&&t.push(n),!this.matchValue(","))break}while(!this.atValue(")"));return this.expectValue(")"),s}parseBlockParams(t){if(this.atValue("("))return this.parseNameList(t);let s=[];for(;!this.atValue(":")&&!this.atValue("->")&&(s.push(this.expect("identifier").value),t.push(null),!(!this.matchValue(",")||this.atValue(":")||this.atValue("->"))););return s}parseTypeAnnotation(){return this.parseUnionType()}parseUnionType(){let t=this.current(),s=this.parsePostfixType();if(!this.atValue("|"))return s;let n=[s];for(;this.matchValue("|");)n.push(this.parsePostfixType());return this.locate({kind:"UnionType",members:n},t)}parsePostfixType(){let t=this.parsePrimaryType();for(;this.atValue("[")&&this.peek(1).value==="]";){let s=this.next();this.expectValue("]"),t=this.locate({kind:"ArrayType",element:t},s)}return t}parsePrimaryType(){if(this.atIdentifier("fn"))return this.parseFunctionType();let t=this.expect("identifier"),s=t.value;if(!this.matchValue("<"))return this.locate({kind:"NameType",name:s},t);let n=[this.parseTypeAnnotation()];for(;this.matchValue(",");)n.push(this.parseTypeAnnotation());return this.expectGenericClose(),this.locate({kind:"GenericType",name:s,args:n},t)}expectGenericClose(){if(this.matchValue(">"))return;let t=this.current();if(t.type!=="string"&&t.value===">="){t.value="=",t.column+=1;return}throw this.error("Expected '>'")}parseFunctionType(){let t=this.expectIdentifier("fn");this.expectValue("(");let s=[];if(!this.atValue(")"))for(s.push(this.parseTypeAnnotation());this.matchValue(",");)s.push(this.parseTypeAnnotation());this.expectValue(")"),this.expectValue("->");let n=this.parseTypeAnnotation();return this.locate({kind:"FunctionType",params:s,ret:n},t)}parseExpression(t=0){let s=this.parsePrefix();for(;;){if(this.atValue("(")){s=this.parseCall(s);continue}if(this.atValue(".")){let a=this.next();s=this.locate({type:"Member",object:s,property:this.expect("identifier").value},a);continue}if(this.atValue("[")){s=this.parseIndex(s);continue}let n=this.current().value,r=w4[n];if(r===void 0||r<t)break;let i=this.next(),o=this.parseExpression(r+(n==="**"?0:1));s=this.locate({type:"Binary",op:n,left:s,right:o},i)}return s}parsePrefix(){let t=this.current();if(t.type==="symbol"&&(t.value==="-"||t.value==="+"))return this.next(),this.locate({type:"Unary",op:t.value,value:this.parseExpression(7)},t);if(t.type==="identifier"&&t.value==="not")return this.next(),this.locate({type:"Unary",op:"not",value:this.parseExpression(7)},t);if(t.type==="number"||t.type==="string"){this.next();let s={type:"Literal",value:t.value};return t.type==="number"&&(s.isFloat=t.float===!0),this.locate(s,t)}if(t.type==="identifier")return this.next(),t.value==="true"?this.locate({type:"Literal",value:!0},t):t.value==="false"?this.locate({type:"Literal",value:!1},t):t.value==="null"?this.locate({type:"Literal",value:null},t):this.locate({type:"Identifier",name:t.value},t);if(this.matchValue("(")){let s=this.parseExpression();return this.expectValue(")"),s}if(this.matchValue("[")){if(this.skipLines(),this.atValue("]"))return this.next(),this.locate({type:"Array",elements:[]},t);let s=this.parseExpression();if(this.skipLines(),this.atIdentifier("for")){this.next();let r=this.expect("identifier").value;this.expectIdentifier("in");let i=this.parseExpression();this.skipLines();let o=null;return this.atIdentifier("if")&&(this.next(),o=this.parseExpression(),this.skipLines()),this.expectValue("]"),this.locate({type:"ListComprehension",expr:s,variable:r,iterable:i,condition:o},t)}let n=[s];for(this.skipLines();this.matchValue(",")&&(this.skipLines(),!this.atValue("]"));)n.push(this.parseExpression()),this.skipLines();return this.expectValue("]"),this.locate({type:"Array",elements:n},t)}if(this.matchValue("{")){let s=[];if(this.skipLines(),!this.atValue("}"))do{if(this.skipLines(),this.atValue("}"))break;let n=this.parseExpression();this.expectValue(":");let r=this.parseExpression();s.push({key:n,value:r}),this.skipLines()}while(this.matchValue(","));return this.skipLines(),this.expectValue("}"),this.locate({type:"Dict",entries:s},t)}throw this.error(`Expected expression, got '${t.value??t.type}'`)}parseCall(t){let s=this.expectValue("("),n=[];if(this.skipLines(),!this.atValue(")"))do{if(this.skipLines(),this.atValue(")"))break;if(this.at("identifier")&&this.peek(1).value==="="){let r=this.next().value;this.next(),n.push({name:r,value:this.parseExpression()})}else n.push({name:null,value:this.parseExpression()});this.skipLines()}while(this.matchValue(","));return this.skipLines(),this.expectValue(")"),this.locate({type:"Call",callee:t,args:n},s)}parseIndex(t){let s=this.expectValue("["),n=[];if(this.atValue("]"))throw this.error("Expected index expression");do{if(this.skipLines(),this.atValue("]"))break;n.push(this.parseIndexItem()),this.skipLines()}while(this.matchValue(","));return this.expectValue("]"),this.locate({type:"Index",object:t,items:n},s)}parseIndexItem(){let t=this.current(),s=null;if(!this.atValue(":")&&!this.atValue(",")&&!this.atValue("]")&&(s=this.parseExpression()),!this.matchValue(":")){if(!s)throw this.error("Expected index expression");return s}let n=null,r=null;return!this.atValue(":")&&!this.atValue(",")&&!this.atValue("]")&&(n=this.parseExpression()),this.matchValue(":")&&!this.atValue(",")&&!this.atValue("]")&&(r=this.parseExpression()),this.locate({type:"Slice",start:s,end:n,step:r},t)}skipLines(){for(;this.at("newline");)this.next()}current(){return this.tokens[this.pos]}peek(t){return this.tokens[this.pos+t]||this.tokens[this.tokens.length-1]}next(){return this.tokens[this.pos++]}at(t){return this.current().type===t}atValue(t){let s=this.current();return s.type!=="string"&&s.value===t}atIdentifier(t){return this.at("identifier")&&this.atValue(t)}matchValue(t){return this.atValue(t)?(this.next(),!0):!1}expect(t){if(!this.at(t))throw this.error(`Expected ${t}`);return this.next()}expectValue(t){if(!this.atValue(t))throw this.error(`Expected '${t}'`);return this.next()}expectIdentifier(t){if(!this.atIdentifier(t))throw this.error(`Expected '${t}'`);return this.next()}error(t){let s=this.current();return new gf(t,s.line,s.column)}locate(t,s){return t.line=s.line,t.column=s.column,t}};Js();function iM(e){if(e instanceof Et){if(e.ndim===0&&e.data)return String(e.item());if(e.numel<=64&&e.data)return JSON.stringify(e.toArray());let t=String(e.device),s=t==="cpu"?"":`, device=${t}`;return`Tensor(shape=[${e.shape.join(", ")}], dtype=${e.dtype}${s})`}return ql(e)}c(iM,"formatValueCompact");function ql(e){if(e instanceof Et){let t=String(e.device),s=t==="cpu"?"":`, device=${t}`;if(e.ndim===0&&e.data)return`Tensor(${e.item()}, dtype=${e.dtype}${s})`;let n=`Tensor(shape=[${e.shape.join(", ")}], dtype=${e.dtype}${s})`;return e.numel<=64&&e.data?`${n}
214
- ${JSON.stringify(e.toArray())}`:n}if(e instanceof nt||e instanceof Ca)return e.toString();if(typeof e=="function"&&e._isCompiled){let t=e._compiledView;return t?t.toString():"<compiled model (lazy)>"}return typeof e=="function"?`<fn ${e._langName||e.name||"anonymous"}>`:Array.isArray(e)?JSON.stringify(e):e===void 0?"":typeof e=="string"?e:e&&e.constructor===Object?JSON.stringify(e):String(e)}c(ql,"formatValue");var Ca=class{static{c(this,"CompiledProgramView")}constructor(t){Object.assign(this,t)}toString(){let t=this.result.listKernels(),s=this.events.filter(n=>n.type==="phase"&&n.action==="end").reduce((n,r)=>n+(r.durationMs||0),0);return`Compiled(target=${this.target}, kernels=${t.length}, trace_events=${this.events.length}, ${s.toFixed(2)} ms)`}};function Dp(e){let t=["Compile trace"];for(let s of e)if(s.type==="phase"&&s.action==="end")t.push(` ${s.phase.padEnd(16)} ${s.durationMs.toFixed(2)} ms`);else if(s.type==="pass"){let n=s.opCountBefore>=0?` ${s.opCountBefore} -> ${s.opCountAfter} ops`:"";t.push(` ${s.passName.padEnd(24)} ${s.changed?"changed":"unchanged"}${n}`)}else s.type==="codegen"?t.push(` codegen:${s.funcName.padEnd(12)} ${s.sourceSize} bytes`):s.type==="memory"?t.push(` memory:${s.funcName.padEnd(13)} peak ${s.peakMemory} bytes`):s.type==="ir_snapshot"?t.push(`
215
- [${s.label}]
216
- ${s.text}`):s.type==="error"&&t.push(` ERROR ${s.phase}: ${s.message}`);return t.join(`
217
- `)}c(Dp,"formatTrace");te();Js();vn();xg();QA();function b4(e,t=","){let s=x4(e);if(s.length===0)throw new Error("CSV file is empty");let n=oM(s[0],t),r=n.length,i=[];for(let o=1;o<s.length;o++){let a=oM(s[o],t);if(a.length!==r)throw new Error(`Row ${o+1} has ${a.length} fields, expected ${r}`);let l={};for(let u=0;u<r;u++)l[n[u]]=S4(a[u]);i.push(l)}return{headers:n,rows:i}}c(b4,"parseCsvRows");function aM(e,t=","){let s=_e.readFile(e);return b4(s,t)}c(aM,"loadCsvRows");function x4(e){let t=[],s="",n=!1;for(let i=0;i<e.length;i++){let o=e[i];if(o==='"')n=!n,s+=o;else if((o===`
218
- `||o==="\r")&&!n){o==="\r"&&e[i+1]===`
219
- `&&i++;let a=s.trim();a.length>0&&t.push(a),s=""}else s+=o}let r=s.trim();return r.length>0&&t.push(r),t}c(x4,"splitLines");function oM(e,t){let s=[],n="",r=!1;for(let i=0;i<e.length;i++){let o=e[i];r?o==='"'?e[i+1]==='"'?(n+='"',i++):r=!1:n+=o:o==='"'?r=!0:o===t?(s.push(n.trim()),n=""):n+=o}return s.push(n.trim()),s}c(oM,"parseRow");function S4(e){if(e===""||e==="null"||e==="NULL"||e==="NA"||e==="NaN")return null;if(e==="true"||e==="True"||e==="TRUE")return 1;if(e==="false"||e==="False"||e==="FALSE")return 0;let t=Number(e);return Number.isNaN(t)?e:t}c(S4,"coerceValue");import{Worker as nj}from"worker_threads";import{fileURLToPath as rj}from"url";import{dirname as ij,join as oj}from"path";import{Worker as gj}from"worker_threads";import{promises as yj}from"fs";import{createServer as Tj,request as Ij,Agent as Oj}from"http";import Af from"fs";import BH from"os";import Dk from"path";import{promises as RL}from"fs";import BL from"fs";import MH from"path";import gw from"fs";import{promises as yw}from"fs";import PH from"path";import{readFile as $H}from"fs/promises";import{fileURLToPath as zH}from"url";import{dirname as UH,join as JD}from"path";var A4=Object.defineProperty,v4=Object.getOwnPropertyNames,J=c((e,t)=>c(function(){return e&&(t=(0,e[v4(e)[0]])(e=0)),t},"__init"),"__esm"),us=c((e,t)=>{for(var s in t)A4(e,s,{get:t[s],enumerable:!0})},"__export");function Nf(e){let t=IL[e];if(!t)throw new Error(`No TypedArray for type ${e}`);return t}c(Nf,"typedArrayCtorFor");function k4(e,t){return new(Nf(e))(t)}c(k4,"typedArrayFor");function Rf(e){return OL[e]??0}c(Rf,"byteWidthFor");function ri(e){return TL.has(e)}c(ri,"isFixedWidth");function vw(e){return e==="INT32"||e==="INT64"||e==="FLOAT64"||e==="DECIMAL"}c(vw,"isNumeric");function lM(e){return e==="DATE"||e==="TIMESTAMP"}c(lM,"isTemporal");function E4(e,t){return!!(e===t||vw(e)&&vw(t)||lM(e)&&lM(t))}c(E4,"isComparable");function rd(e,t,s){let n=Date.UTC(e,t-1,s);return Math.floor(n/864e5)}c(rd,"dateToEpochDays");function CL(e){let t=e*864e5,s=new Date(t);return{year:s.getUTCFullYear(),month:s.getUTCMonth()+1,day:s.getUTCDate()}}c(CL,"epochDaysToDate");function N4(e,t,s,n,r,i,o=0){return Date.UTC(e,t-1,s,n,r,i,o)}c(N4,"timestampToEpochMs");function R4(e){let t=new Date(e);return{year:t.getUTCFullYear(),month:t.getUTCMonth()+1,day:t.getUTCDate(),hour:t.getUTCHours(),minute:t.getUTCMinutes(),second:t.getUTCSeconds(),ms:t.getUTCMilliseconds()}}c(R4,"epochMsToTimestamp");var cM,TL,IL,OL,ML,we=J({"src/storage/data-type.ts"(){"use strict";cM=(e=>(e.BOOLEAN="BOOLEAN",e.INT32="INT32",e.INT64="INT64",e.FLOAT64="FLOAT64",e.DECIMAL="DECIMAL",e.VARCHAR="VARCHAR",e.DATE="DATE",e.TIMESTAMP="TIMESTAMP",e))(cM||{}),TL=new Set(["BOOLEAN","INT32","INT64","FLOAT64","DECIMAL","DATE","TIMESTAMP"]),IL={BOOLEAN:Uint8Array,INT32:Int32Array,INT64:BigInt64Array,FLOAT64:Float64Array,DECIMAL:BigInt64Array,DATE:Int32Array,TIMESTAMP:BigInt64Array},OL={BOOLEAN:1,INT32:4,INT64:8,FLOAT64:8,DECIMAL:8,DATE:4,TIMESTAMP:8},ML=100}});function La(e,t,s,n,r=0){return{kind:"BoundColumnRef",tableAlias:e,columnName:t,columnIndex:s,dataType:n,depth:r,isCorrelated:r>0}}c(La,"BoundColumnRef");function ce(e,t){return{kind:"BoundLiteral",value:e,dataType:t}}c(ce,"BoundLiteral");function Cn(e,t,s,n){return{kind:"BoundBinary",op:e,left:t,right:s,resultType:n}}c(Cn,"BoundBinary");function Zv(e,t,s){return{kind:"BoundUnary",op:e,operand:t,resultType:s}}c(Zv,"BoundUnary");function tk(e,t,s){return{kind:"BoundFunction",name:e,args:t,resultType:s}}c(tk,"BoundFunction");function zw(e,t,s,n){return{kind:"BoundAggregate",name:e,args:t,distinct:s,resultType:n}}c(zw,"BoundAggregate");function B4(e,t,s,n){return{kind:"BoundCase",operand:e,whenClauses:t,elseExpr:s,resultType:n}}c(B4,"BoundCase");function LL(e,t){return{kind:"BoundCast",expr:e,targetType:t,dataType:t}}c(LL,"BoundCast");function PL(e,t,s,n){return{kind:"BoundBetween",expr:e,low:t,high:s,negated:n,resultType:"BOOLEAN"}}c(PL,"BoundBetween");function kw(e,t,s){return{kind:"BoundInList",expr:e,list:t,negated:s,resultType:"BOOLEAN"}}c(kw,"BoundInList");function DL(e,t,s){return{kind:"BoundLike",expr:e,pattern:t,negated:s,resultType:"BOOLEAN"}}c(DL,"BoundLike");function ek(e,t){return{kind:"BoundIsNull",expr:e,negated:t,resultType:"BOOLEAN"}}c(ek,"BoundIsNull");function uM(e,t){return{kind:"BoundSubquery",plan:e,subqueryType:t}}c(uM,"BoundSubquery");function C4(e,t){return{kind:"BoundExists",plan:e,negated:t,resultType:"BOOLEAN"}}c(C4,"BoundExists");function T4(e,t){return{kind:"BoundExtract",field:e,source:t,resultType:"INT32"}}c(T4,"BoundExtract");function I4(e,t){return{kind:"BoundInterval",value:e,unit:t,resultType:"INT32"}}c(I4,"BoundInterval");function O4(e,t,s,n,r){return{kind:"BoundWindow",name:e,args:t,partitionBy:s,orderBy:n,resultType:r}}c(O4,"BoundWindow");function nn(e){return e&&(e.resultType||e.dataType)||null}c(nn,"getExprType");var Vt=J({"src/binder/expression-binder.ts"(){"use strict";we()}});function sk(e,t,s){return{type:"Scan",table:e,columns:t,alias:s||e}}c(sk,"LogicalScan");function mn(e,t){return{type:"Filter",condition:e,children:[t]}}c(mn,"LogicalFilter");function Ew(e,t){return{type:"Project",expressions:e,children:[t]}}c(Ew,"LogicalProject");function Lr(e,t,s,n,r="HASH"){return{type:"Join",joinType:e,condition:t,children:[s,n],physicalStrategy:r}}c(Lr,"LogicalJoin");function id(e,t,s,n="HASH"){return{type:"Aggregate",groupBy:e,aggregates:t,children:[s],physicalStrategy:n}}c(id,"LogicalAggregate");function Nw(e,t){return{type:"Sort",orderKeys:e,children:[t]}}c(Nw,"LogicalSort");function FL(e,t,s){return{type:"Limit",count:e,offset:t||0,children:[s]}}c(FL,"LogicalLimit");function $L(e){return{type:"Distinct",children:[e]}}c($L,"LogicalDistinct");function zL(e,t,s){return{type:"Union",all:!!s,children:[e,t]}}c(zL,"LogicalUnion");function M4(e,t){return{type:"CTEScan",cteName:e,cteId:t,children:[]}}c(M4,"LogicalCTEScan");function Z_(e,t,s,n,r){return{type:"DependentJoin",correlatedColumns:s,subqueryType:n,condition:r,children:[e,t]}}c(Z_,"LogicalDependentJoin");function L4(e,t,s,n,r,i,o,a,l,u,f){return{type:"IndexScan",table:e,alias:t||e,indexName:s,columnName:n,scanType:r,scanKey:i,scanLow:o,scanHigh:a,lowInc:l,highInc:u,columns:f}}c(L4,"LogicalIndexScan");function P4(e,t){return{type:"Window",windowExprs:e,children:[t]}}c(P4,"LogicalWindow");function D4(e){return{type:"Materialize",children:[e]}}c(D4,"LogicalMaterialize");function F4(e,t,s,n){return{type:"Exchange",exchangeType:e,partitionKeys:t||[],partitionCount:s||0,children:[n]}}c(F4,"LogicalExchange");function $4(e,t,s){return{type:"PartialAggregate",groupBy:e,aggregates:t,children:[s],physicalStrategy:"HASH"}}c($4,"LogicalPartialAggregate");function z4(e,t,s,n){return{type:"FinalAggregate",groupBy:e,aggregates:t,partialAggregates:s,children:[n],physicalStrategy:"HASH"}}c(z4,"LogicalFinalAggregate");function fM(e,t,s){return{type:"MergeExchange",orderKeys:e,limit:t||null,children:[s]}}c(fM,"LogicalMergeExchange");function Ta(e,t){return{type:"ExchangeReceive",sourceFragmentIds:e,schema:t||[],children:[]}}c(Ta,"LogicalExchangeReceive");function U4(){return{type:"SingleRow",children:[]}}c(U4,"LogicalSingleRow");function cs(e){return e.children||[]}c(cs,"getChildren");function Tn(e,t){return{...e,children:t}}c(Tn,"setChildren");function UL(e,t=0){let n=`${" ".repeat(t)}${e.type}`;switch(e.type){case"Scan":n+=`(${e.table}${e.alias!==e.table?` AS ${e.alias}`:""})`;break;case"Join":n+=`(${e.joinType})`;break;case"Limit":n+=`(${e.count}${e.offset?`, offset=${e.offset}`:""})`;break;case"CTEScan":n+=`(${e.cteName})`;break;case"CTEAnchor":n+=`(${e.cteName})`;break;case"DependentJoin":n+=`(${e.subqueryType})`;break;case"Window":n+=`(${e.windowExprs.map(r=>r.name).join(", ")})`;break;case"TopN":n+=`(${e.count}${e.offset?`, offset=${e.offset}`:""})`;break;case"IndexScan":n+=`(${e.table} using ${e.indexName})`;break;case"Exchange":n+=`(${e.exchangeType})`;break;case"PartialAggregate":n+="(partial)";break;case"FinalAggregate":n+="(final)";break;case"MergeExchange":n+=`(${e.limit?`limit=${e.limit}`:"all"})`;break;case"ExchangeReceive":n+=`(fragments=[${e.sourceFragmentIds.join(",")}])`;break}n+=`
220
- `;for(let r of cs(e))n+=UL(r,t+1);return n}c(UL,"planToString");var Rw,Ft=J({"src/planner/logical-plan.ts"(){"use strict";Rw=(e=>(e.INNER="INNER",e.LEFT="LEFT",e.RIGHT="RIGHT",e.FULL="FULL",e.SEMI="SEMI",e.ANTI="ANTI",e.MARK="MARK",e.SINGLE="SINGLE",e.CROSS="CROSS",e))(Rw||{})}});function Fa(e){return Math.ceil(e/32)}c(Fa,"bitmapWordCount");function Av(e){return new Uint32Array(Fa(e))}c(Av,"createBitmap");function Yl(e,t){e[t>>>5]|=1<<(t&31)}c(Yl,"setBit");function Uw(e,t){e[t>>>5]&=~(1<<(t&31))}c(Uw,"clearBit");function Kl(e,t){return(e[t>>>5]&1<<(t&31))!==0}c(Kl,"testBit");function V4(e,t,s){for(let n=t;n<s;n++)Yl(e,n)}c(V4,"setBitRange");var vv,Zl=J({"src/utils/bitmap.ts"(){"use strict";vv=new Uint8Array(256);for(let e=0;e<256;e++)vv[e]=vv[e>>1]+(e&1)}});function G4(e,t){let s=Vw?.[e];return s!==void 0?parseInt(s,10):t}c(G4,"getEnvInt");function q4(e,t){let s=Vw?.[e];return s!==void 0?parseFloat(s):t}c(q4,"getEnvFloat");function j4(e,t){let s=Vw?.[e];return s===void 0?t:s==="1"||s.toLowerCase()==="true"}c(j4,"getEnvFlag");function W4(){return globalThis.navigator?.hardwareConcurrency??VL}c(W4,"getCpuCount");var VL,Vw,K4=J({"src/runtime/platform.ts"(){"use strict";VL=1,Vw=typeof process<"u"&&process.env?process.env:null}}),GL={};us(GL,{Config:c(()=>ht,"Config")});var Ut,hM,tw,pM,ht,Fe=J({"src/config.ts"(){"use strict";be(),K4(),Ut=G4,hM=j4,tw=q4,pM=c(()=>{let e=Ut("QE_PARALLEL_WORKERS",0);return e>0?e:Math.max(1,W4()-1)},"resolveWorkerCount"),ht={memoryLimit:Ut("QE_MEMORY_LIMIT",2e5),hashJoinPartitions:Ut("QE_HASH_JOIN_PARTITIONS",16),flushBatchSize:Ut("QE_FLUSH_BATCH_SIZE",We),bufferPoolPages:Ut("QE_BUFFER_POOL_PAGES",50),wasmMinChunkSize:Ut("QE_WASM_MIN_CHUNK",4096),sinkQueueCapacity:Ut("QE_SINK_QUEUE_CAPACITY",8),btreeOrder:Ut("QE_BTREE_ORDER",128),indexScanSelectivityThreshold:tw("QE_INDEX_SELECTIVITY_THRESHOLD",.3),dependentJoinConcurrency:Ut("QE_DEPENDENT_JOIN_CONCURRENCY",1),parallelWorkers:pM(),parallelThreshold:Ut("QE_PARALLEL_THRESHOLD",1e4),parallelAggThreshold:Ut("QE_PARALLEL_AGG_THRESHOLD",5e4),aggMorselRows:Ut("QE_AGG_MORSEL_ROWS",16384),sabColumns:hM("QE_SAB_COLUMNS",!1),sabArenaSegmentBytes:Ut("QE_SAB_ARENA_SEGMENT_BYTES",1<<20),parallelAggMemoryBytes:Ut("QE_PARALLEL_AGG_MEMORY_BYTES",1<<28),parallelCombineMinGroups:Ut("QE_PARALLEL_COMBINE_MIN_GROUPS",8192),aggSpillGroups:Ut("QE_AGG_SPILL_GROUPS",1<<17),vectorGroupRange:Ut("QE_VECTOR_GROUP_RANGE",1<<21),parallelJoinThreshold:Ut("QE_PARALLEL_JOIN_THRESHOLD",5e4),transportMaxBuffers:Ut("QE_TRANSPORT_MAX_BUFFERS",64),aggRadixMultiplier:Ut("QE_AGG_RADIX_MULTIPLIER",2),regionSize:Ut("QE_WASM_REGION_SIZE",16*1024*1024),morselSize:Ut("QE_MORSEL_SIZE",262144),clusterPort:Ut("QE_CLUSTER_PORT",9400),coordinatorSchemaSampleRows:Ut("QE_COORD_SCHEMA_SAMPLE_ROWS",1e3),heartbeatIntervalMs:Ut("QE_HEARTBEAT_INTERVAL",3e3),heartbeatTimeoutMs:Ut("QE_HEARTBEAT_TIMEOUT",1e4),defaultPartitionCount:Ut("QE_PARTITION_COUNT",16),broadcastThreshold:Ut("QE_BROADCAST_THRESHOLD",1e4),exchangeBatchSize:Ut("QE_EXCHANGE_BATCH_SIZE",4096),exchangeBufferCapacity:Ut("QE_EXCHANGE_BUFFER_CAPACITY",8),fragmentRetryLimit:Ut("QE_FRAGMENT_RETRY_LIMIT",3),coordinatorTimeoutMs:Ut("QE_COORDINATOR_TIMEOUT",3e5),codecCompression:Ut("QE_CODEC_COMPRESSION",0),distributedWorkers:Ut("QE_DISTRIBUTED_WORKERS",0),phiAccrualWindowSize:Ut("QE_PHI_WINDOW_SIZE",100),phiAccrualThreshold:tw("QE_PHI_THRESHOLD",8),networkCostPerByte:tw("QE_NETWORK_COST_PER_BYTE",.001)}}});function H4(e=0){return ht.sabColumns?e>0?new od(e):new od:$a}c(H4,"columnAllocator");function jl(e){return!!e&&e.buffer instanceof SharedArrayBuffer}c(jl,"isSharedView");var kv,dM,$a,od,tc=J({"src/storage/sab-arena.ts"(){"use strict";Fe(),kv=8,dM=class{static{c(this,"HeapAllocator")}get shared(){return!1}acquire(e,t){return new e(t)}},$a=new dM,od=class{static{c(this,"SabArena")}nextSegmentBytes;segments;constructor(e=ht.sabArenaSegmentBytes){this.nextSegmentBytes=Math.max(kv,e|0),this.segments=[]}get shared(){return!0}acquire(e,t){let s=t*e.BYTES_PER_ELEMENT,n=Math.max(e.BYTES_PER_ELEMENT,kv),r=this._segmentFor(s,n),i=r.offset+n-1&~(n-1);return r.offset=i+s,new e(r.sab,i,t)}_segmentFor(e,t){let s=this.segments[this.segments.length-1];if(s&&(s.offset+t-1&~(t-1))+e<=s.sab.byteLength)return s;let n=Math.max(this.nextSegmentBytes,e+t);this.nextSegmentBytes=n*2;let r={sab:new SharedArrayBuffer(n),offset:0};return this.segments.push(r),r}totalBytes(){let e=0;for(let t of this.segments)e+=t.sab.byteLength;return e}}}}),mM,gM,Qt,Ts=J({"src/storage/column.ts"(){"use strict";we(),Zl(),tc(),mM=2048,gM=4096,Qt=class nk{static{c(this,"_Column")}dataType;capacity;length;allocator;data;offsets;stringBytes;stringBytesUsed;nullBitmap;hasNulls;constructor(t,s=mM,n=$a){this.dataType=t,this.capacity=s,this.length=0,this.allocator=n,ri(t)?this.data=n.acquire(Nf(t),s):(this.offsets=n.acquire(Uint32Array,s+1),this.stringBytes=n.acquire(Uint8Array,gM),this.stringBytesUsed=0),this.nullBitmap=n.acquire(Uint32Array,Fa(s)),this.hasNulls=!1}static fromParts({dataType:t,data:s,offsets:n,stringBytes:r,stringBytesUsed:i,nullBitmap:o,length:a,hasNulls:l,allocator:u}){let f=Object.create(nk.prototype);return f.dataType=t,f.capacity=a,f.length=a,f.allocator=u||$a,s!==void 0&&(f.data=s),n!==void 0&&(f.offsets=n,f.stringBytes=r,f.stringBytesUsed=i),f.nullBitmap=o,f.hasNulls=l,f}get(t){if(this.hasNulls&&!Kl(this.nullBitmap,t))return null;if(this.dataType==="VARCHAR")return this._getString(t);let s=this.data[t];return this.dataType==="BOOLEAN"?s!==0:s}set(t,s){if(s==null){this._setNull(t);return}Yl(this.nullBitmap,t),this.dataType==="VARCHAR"?this._setString(t,s):this.dataType==="BOOLEAN"?this.data[t]=s?1:0:this.data[t]=s,t>=this.length&&(this.length=t+1)}append(t){this.length>=this.capacity&&this._grow(),this.set(this.length,t)}appendBatch(t){for(let s=0;s<t.length;s++)this.append(t[s])}isNull(t){return this.hasNulls&&!Kl(this.nullBitmap,t)}slice(t,s){let n=s-t,r=new nk(this.dataType,n,this.allocator);if(this.dataType==="VARCHAR")for(let i=0;i<n;i++)r.set(i,this.get(t+i));else{if(this.data){let i=this.data.subarray(t,s);r.data.set(i)}for(let i=0;i<n;i++)Kl(this.nullBitmap,t+i)&&Yl(r.nullBitmap,i)}return r.length=n,r.hasNulls=this.hasNulls,r}_setNull(t){this.hasNulls=!0,Uw(this.nullBitmap,t),this.dataType==="VARCHAR"&&(this.offsets[t+1]=this.offsets[t]),t>=this.length&&(this.length=t+1)}_getString(t){let s=this.offsets[t],n=this.offsets[t+1];if(s===n)return"";let r=this.stringBytes.subarray(s,n);return new TextDecoder().decode(r)}_setString(t,s){let n=new TextEncoder().encode(s);for(;this.stringBytesUsed+n.length>this.stringBytes.length;)this._growStringBuffer();this.offsets[t]=this.stringBytesUsed,this.stringBytes.set(n,this.stringBytesUsed),this.stringBytesUsed+=n.length,this.offsets[t+1]=this.stringBytesUsed}_grow(){let t=this.capacity*2;if(ri(this.dataType)){let n=this.allocator.acquire(Nf(this.dataType),t);n.set(this.data),this.data=n}else{let n=this.allocator.acquire(Uint32Array,t+1);n.set(this.offsets),this.offsets=n}let s=this.allocator.acquire(Uint32Array,Fa(t));s.set(this.nullBitmap),this.nullBitmap=s,this.capacity=t}_growStringBuffer(){let t=this.allocator.acquire(Uint8Array,this.stringBytes.length*2);t.set(this.stringBytes),this.stringBytes=t}}}}),yM,Ev,pr,Gw=J({"src/storage/dictionary-column.ts"(){"use strict";we(),Zl(),tc(),yM=2048,Ev=65535,pr=class rk{static{c(this,"_DictionaryColumn")}dataType;capacity;length;allocator;_dictionary;reverseDict;indices;nullBitmap;hasNulls;constructor(t=yM,s=$a){this.dataType="VARCHAR",this.capacity=t,this.length=0,this.allocator=s,this._dictionary=new Map,this.reverseDict=[],this.indices=s.acquire(Uint16Array,t),this.nullBitmap=s.acquire(Uint32Array,Fa(t)),this.hasNulls=!1}static fromParts({indices:t,reverseDict:s,nullBitmap:n,length:r,hasNulls:i,allocator:o}){let a=Object.create(rk.prototype);return a.dataType="VARCHAR",a.capacity=r,a.length=r,a.allocator=o||$a,a._dictionary=null,a.reverseDict=s,a.indices=t,a.nullBitmap=n,a.hasNulls=i,a}get dictionary(){return this._dictionary===null&&(this._dictionary=new Map(this.reverseDict.map((t,s)=>[t,s]))),this._dictionary}set dictionary(t){this._dictionary=t}get(t){if(this.hasNulls&&!Kl(this.nullBitmap,t))return null;let s=this.indices[t];return this.reverseDict[s]}set(t,s){if(s==null){this._setNull(t);return}Yl(this.nullBitmap,t);let n=s,r=this.dictionary.get(n);if(r===void 0){if(r=this.reverseDict.length,r>Ev)throw new Error(`Dictionary capacity exceeded ${Ev} values per chunk`);this.dictionary.set(n,r),this.reverseDict.push(n)}this.indices[t]=r,t>=this.length&&(this.length=t+1)}append(t){this.length>=this.capacity&&this._grow(),this.set(this.length,t)}appendBatch(t){for(let s=0;s<t.length;s++)this.append(t[s])}isNull(t){return this.hasNulls&&!Kl(this.nullBitmap,t)}slice(t,s){let n=s-t,r=new rk(n,this.allocator);r.dictionary=this.dictionary,r.reverseDict=this.reverseDict;let i=this.indices.subarray(t,s);r.indices.set(i);for(let o=0;o<n;o++)Kl(this.nullBitmap,t+o)&&Yl(r.nullBitmap,o);return r.length=n,r.hasNulls=this.hasNulls,r}_setNull(t){this.hasNulls=!0,Uw(this.nullBitmap,t),t>=this.length&&(this.length=t+1)}_grow(){let t=this.capacity*2,s=this.allocator.acquire(Uint16Array,t);s.set(this.indices),this.indices=s;let n=this.allocator.acquire(Uint32Array,Fa(t));n.set(this.nullBitmap),this.nullBitmap=n,this.capacity=t}}}}),We,kt,be=J({"src/storage/chunk.ts"(){"use strict";Ts(),Gw(),we(),We=2048,kt=class _w{static{c(this,"_DataChunk")}columns;size;selectionVector;_cachedColumnCount;constructor(t,s=0){this.columns=t,this.size=s,this.selectionVector=null,this._cachedColumnCount=t.length}static fromSchema(t,s=We){let n=t.map(({dataType:r})=>r==="VARCHAR"?new pr(s):new Qt(r,s));return new _w(n,0)}getColumn(t){return this.columns[t]}columnCount(){return this.columns.length}getValue(t,s){let n=this.selectionVector?this.selectionVector[t]:t;return this.columns[s].get(n)}activeRowIndex(t){return this.selectionVector?this.selectionVector[t]:t}setSelectionVector(t,s){this.selectionVector=t,this.size=s}clearSelectionVector(){this.selectionVector=null}appendRow(t){let s=this.size;for(let n=0;n<t.length;n++)this.columns[n].set(s,t[n]);this.size++}project(t){let s=t.map(r=>this.columns[r]),n=new _w(s,this.size);return n.selectionVector=this.selectionVector,n}flatten(){if(!this.selectionVector)return this;let t=this.columns.map(s=>{let n=new Qt(s.dataType,this.size);for(let r=0;r<this.size;r++)n.set(r,s.get(this.selectionVector[r]));return n.length=this.size,n});return new _w(t,this.size)}reset(){this.size=0,this.selectionVector=null;for(let t of this.columns)t.length=0}toRows(){let t=[];for(let s=0;s<this.size;s++){let n=[];for(let r=0;r<this.columns.length;r++)n.push(this.getValue(s,r));t.push(n)}return t}}}}),Be,$e=J({"src/optimizer/pass.ts"(){"use strict";Be=class{static{c(this,"OptimizationPass")}get name(){throw new Error("Subclass must implement name")}apply(e,t){throw new Error("Subclass must implement apply()")}}}}),ze,Ke=J({"src/planner/plan-visitor.ts"(){"use strict";Ft(),ze=class{static{c(this,"PlanRewriter")}rewrite(e){switch(e.type){case"Scan":return this.rewriteScan?this.rewriteScan(e):this.rewriteDefault(e);case"Filter":return this.rewriteFilter?this.rewriteFilter(e):this.rewriteDefault(e);case"Project":return this.rewriteProject?this.rewriteProject(e):this.rewriteDefault(e);case"Join":return this.rewriteJoin?this.rewriteJoin(e):this.rewriteDefault(e);case"Aggregate":return this.rewriteAggregate?this.rewriteAggregate(e):this.rewriteDefault(e);case"Sort":return this.rewriteSort?this.rewriteSort(e):this.rewriteDefault(e);case"Limit":return this.rewriteLimit?this.rewriteLimit(e):this.rewriteDefault(e);case"Distinct":return this.rewriteDistinct?this.rewriteDistinct(e):this.rewriteDefault(e);case"Union":return this.rewriteUnion?this.rewriteUnion(e):this.rewriteDefault(e);case"CTEScan":return this.rewriteCTEScan?this.rewriteCTEScan(e):this.rewriteDefault(e);case"CTEAnchor":return this.rewriteCTEAnchor?this.rewriteCTEAnchor(e):this.rewriteDefault(e);case"DependentJoin":return this.rewriteDependentJoin?this.rewriteDependentJoin(e):this.rewriteDefault(e);case"Materialize":return this.rewriteMaterialize?this.rewriteMaterialize(e):this.rewriteDefault(e);case"Empty":return this.rewriteEmpty?this.rewriteEmpty(e):this.rewriteDefault(e);case"TopN":return this.rewriteTopN?this.rewriteTopN(e):this.rewriteDefault(e);case"IndexScan":return this.rewriteIndexScan?this.rewriteIndexScan(e):this.rewriteDefault(e);case"Window":return this.rewriteWindow?this.rewriteWindow(e):this.rewriteDefault(e);case"Exchange":return this.rewriteExchange?this.rewriteExchange(e):this.rewriteDefault(e);case"PartialAggregate":return this.rewritePartialAggregate?this.rewritePartialAggregate(e):this.rewriteDefault(e);case"FinalAggregate":return this.rewriteFinalAggregate?this.rewriteFinalAggregate(e):this.rewriteDefault(e);case"MergeExchange":return this.rewriteMergeExchange?this.rewriteMergeExchange(e):this.rewriteDefault(e);case"ExchangeReceive":return this.rewriteExchangeReceive?this.rewriteExchangeReceive(e):this.rewriteDefault(e);case"SingleRow":return this.rewriteSingleRow?this.rewriteSingleRow(e):this.rewriteDefault(e);default:return this.rewriteDefault(e)}}rewriteDefault(e){return this.rewriteChildren(e)}rewriteChildren(e){let t=cs(e);if(t.length===0)return e;let s=t.map(r=>this.rewrite(r));return s.some((r,i)=>r!==t[i])?Tn(e,s):e}}}});function Dt(e,t){if(!e)return()=>null;switch(e.kind){case"BoundColumnRef":{let s=Q4(e,t);return(n,r)=>n.columns[s]?.get(r)??null}case"BoundLiteral":return()=>e.value;case"BoundBinary":{let s=Dt(e.left,t),n=Dt(e.right,t);return J4(e.op,s,n)}case"BoundUnary":{let s=Dt(e.operand,t);return e.op==="-"?(n,r)=>{let i=s(n,r);return i==null?null:-i}:e.op==="NOT"?(n,r)=>{let i=s(n,r);return i==null?null:!i}:s}case"BoundBetween":{let s=Dt(e.expr,t),n=Dt(e.low,t),r=Dt(e.high,t),i=e.negated;return(o,a)=>{let l=s(o,a);if(l==null)return null;let u=n(o,a),f=r(o,a),h=u==null?null:l>=u,p=f==null?null:l<=f,d;return h===!1||p===!1?d=!1:h===null||p===null?d=null:d=!0,i?d===null?null:!d:d}}case"BoundInList":{let s=Dt(e.expr,t),n=e.negated;if(Array.isArray(e.list)){let r=e.list,i=c((l,u)=>{if(l==null)return null;let f=u(l);return f?!0:f===null?null:!1},"test");if(r.every(l=>l.kind==="BoundLiteral")){let l=r.some(h=>h.value===null||h.value===void 0),u=new Set(r.filter(h=>h.value!=null).map(h=>wM(h.value))),f=c(h=>u.has(wM(h))?!0:l?null:!1,"has2");return(h,p)=>{let d=i(s(h,p),f);return n?d===null?null:!d:d}}let o=r.map(l=>Dt(l,t)),a=c((l,u,f)=>{let h=!1;for(let p of o){let d=p(u,f);if(d==null){h=!0;continue}if(d==l)return!0}return h?null:!1},"has");return(l,u)=>{let f=s(l,u);if(f==null)return null;let h=a(f,l,u),p=h?!0:h===null?null:!1;return n?p===null?null:!p:p}}return()=>!0}case"BoundLike":{let s=Dt(e.expr,t),n=Dt(e.pattern,t),r=e.negated,i=new Map,o=[];return(a,l)=>{let u=s(a,l),f=n(a,l);if(u==null||f===null||f===void 0)return null;let h=String(f),p=i.get(h);p||(p=X4(h),i.size>=jL&&i.delete(o.shift()),i.set(h,p),o.push(h));let d=p.test(String(u));return r?!d:d}}case"BoundIsNull":{let s=Dt(e.expr,t);return e.negated?(n,r)=>s(n,r)!==null&&s(n,r)!==void 0:(n,r)=>s(n,r)===null||s(n,r)===void 0}case"BoundCase":{let s=e.whenClauses.map(r=>({cond:Dt(r.condition,t),result:Dt(r.result,t)})),n=e.elseExpr?Dt(e.elseExpr,t):()=>null;return(r,i)=>{for(let o of s)if(o.cond(r,i))return o.result(r,i);return n(r,i)}}case"BoundCast":{let s=Dt(e.expr,t),n=e.targetType;return(r,i)=>t6(s(r,i),n)}case"BoundExtract":{let s=Dt(e.source,t),n=nn(e.source),r=e.field;return(i,o)=>{let a=s(i,o);if(a===null)return null;if(n==="TIMESTAMP"||typeof a=="bigint"||a>1e5){let u=R4(typeof a=="bigint"?Number(a):a);switch(r){case"YEAR":return u.year;case"MONTH":return u.month;case"DAY":return u.day;case"HOUR":return u.hour;case"MINUTE":return u.minute;case"SECOND":return u.second;default:return null}}let l=CL(a);switch(r){case"YEAR":return l.year;case"MONTH":return l.month;case"DAY":return l.day;default:return null}}}case"BoundFunction":{let s=e.args.map(n=>Dt(n,t));return Y4(e.name,s)}case"BoundAggregate":{let s=qL(e);if(t&&t.has(s)){let n=t.get(s);return(r,i)=>r.columns[n]?.get(i)??null}return e.args.length>0?Dt(e.args[0],t):()=>null}case"BoundInterval":return()=>({value:e.value,unit:e.unit,_isInterval:!0});case"BoundWindow":{let s=Z4(e);if(t&&t.has(s)){let n=t.get(s);return(r,i)=>r.columns[n]?.get(i)??null}return()=>null}default:return()=>null}}c(Dt,"compileExpression");function Nv(e,t,s){if(s==="DAY")return e+t;let n=CL(e);if(s==="YEAR")return rd(n.year+t,n.month,Math.min(n.day,_M(n.year+t,n.month)));if(s==="MONTH"){let r=n.month+t,i=n.year;for(;r>12;)r-=12,i++;for(;r<1;)r+=12,i--;return rd(i,r,Math.min(n.day,_M(i,r)))}return e+t}c(Nv,"addInterval");function _M(e,t){return new Date(e,t,0).getDate()}c(_M,"daysInMonth");function sn(e){return typeof e=="bigint"?Number(e):e}c(sn,"toNum");function wM(e){return typeof e=="bigint"?Number(e):e}c(wM,"normalizeComparable");function Fp(e,t,s){return s(sn(e),sn(t))}c(Fp,"numOp");function J4(e,t,s){switch(e){case"=":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:sn(i)==sn(o)};case"<>":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:sn(i)!=sn(o)};case"<":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:sn(i)<sn(o)};case">":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:sn(i)>sn(o)};case"<=":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:sn(i)<=sn(o)};case">=":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:sn(i)>=sn(o)};case"AND":return(n,r)=>{let i=t(n,r),o=s(n,r);return i===!1||o===!1?!1:i==null||o==null?null:!0};case"OR":return(n,r)=>{let i=t(n,r),o=s(n,r);return i===!0||o===!0?!0:i==null||o==null?null:!1};case"+":return(n,r)=>{let i=t(n,r),o=s(n,r);return i===null||o===null?null:o?._isInterval?Nv(sn(i),o.value,o.unit):i?._isInterval?Nv(sn(o),i.value,i.unit):Fp(i,o,(a,l)=>a+l)};case"-":return(n,r)=>{let i=t(n,r),o=s(n,r);return i===null||o===null?null:o?._isInterval?Nv(sn(i),-o.value,o.unit):Fp(i,o,(a,l)=>a-l)};case"*":return(n,r)=>{let i=t(n,r),o=s(n,r);return i!==null&&o!==null?Fp(i,o,(a,l)=>a*l):null};case"/":return(n,r)=>{let i=t(n,r),o=s(n,r);return i!==null&&o!==null&&o!==0?Fp(i,o,(a,l)=>a/l):null};case"%":return(n,r)=>{let i=t(n,r),o=s(n,r);return i!==null&&o!==null?Fp(i,o,(a,l)=>a%l):null};case"||":return(n,r)=>{let i=t(n,r),o=s(n,r);return i!==null&&o!==null?String(i)+String(o):null};default:return()=>null}}c(J4,"compileBinaryOp");function Y4(e,t){switch(e.toUpperCase()){case"SUBSTRING":{let[s,n,r]=t;return(i,o)=>{let a=s(i,o);if(a===null)return null;let l=n(i,o)-1;return r?String(a).substring(l,l+r(i,o)):String(a).substring(l)}}case"TRIM":return(s,n)=>{let r=t[0](s,n);return r!==null?String(r).trim():null};case"UPPER":return(s,n)=>{let r=t[0](s,n);return r!==null?String(r).toUpperCase():null};case"LOWER":return(s,n)=>{let r=t[0](s,n);return r!==null?String(r).toLowerCase():null};case"ABS":return(s,n)=>{let r=t[0](s,n);return r!==null?Math.abs(r):null};case"ROUND":return(s,n)=>{let r=t[0](s,n),i=t[1]?t[1](s,n):0;if(r===null)return null;let o=Math.pow(10,i);return Math.round(r*o)/o};case"COALESCE":return(s,n)=>{for(let r of t){let i=r(s,n);if(i!=null)return i}return null};case"NULLIF":return(s,n)=>{let r=t[0](s,n),i=t[1](s,n);return r==i?null:r};case"SQRT":return(s,n)=>{let r=t[0](s,n);return r!==null?Math.sqrt(r):null};case"LENGTH":return(s,n)=>{let r=t[0](s,n);return r!==null?String(r).length:null};case"REPLACE":return(s,n)=>{let r=t[0](s,n),i=t[1](s,n),o=t[2](s,n);return r===null||i===null||o===null?null:String(r).split(String(i)).join(String(o))};default:return()=>null}}c(Y4,"compileFunction");function Q4(e,t){if(t){let s=`${e.tableAlias}.${e.columnName}`.toUpperCase();if(t.has(s))return t.get(s);let n=`${e.columnName}`.toUpperCase();if(t.has(n))return t.get(n)}return e.columnIndex}c(Q4,"resolveColumnIndex");function X4(e){let t="^";for(let s of e)s==="%"?t+=".*":s==="_"?t+=".":".+*?^${}()|[]\\".includes(s)?t+="\\"+s:t+=s;return t+="$",new RegExp(t,"i")}c(X4,"likeToRegex");function qL(e){let t=e.name?.toUpperCase()||"AGG",s=e.distinct?"_DISTINCT":"";if(e.args.length===0)return`__AGG__${t}${s}`;let n=e.args.map(r=>{if(r.kind==="BoundColumnRef"){let i=r;return`${i.tableAlias}.${i.columnName}`.toUpperCase()}return JSON.stringify(r).slice(0,30)}).join(",");return`__AGG__${t}${s}(${n})`}c(qL,"aggExprKey");function Z4(e){let t=e.name?.toUpperCase()||"WIN",s=(e.args||[]).map(r=>{if(r.kind==="BoundColumnRef"){let i=r;return`${i.tableAlias}.${i.columnName}`.toUpperCase()}return JSON.stringify(r).slice(0,30)}).join(","),n=(e.partitionBy||[]).map(r=>{if(r.kind==="BoundColumnRef"){let i=r;return`${i.tableAlias}.${i.columnName}`.toUpperCase()}return""}).join(",");return`__WIN__${t}(${s})[${n}]`}c(Z4,"windowExprKey");function t6(e,t){if(e===null)return null;switch(t){case"INT32":return parseInt(e,10)|0;case"INT64":return BigInt(parseInt(e,10));case"FLOAT64":return parseFloat(e);case"VARCHAR":return String(typeof e=="bigint"?Number(e):e);case"BOOLEAN":return!!e;case"TIMESTAMP":return typeof e=="string"?new Date(e).getTime():Number(e);case"DATE":{if(typeof e=="string"){let[s,n,r]=e.split("-").map(Number);return rd(s,n,r)}return e}default:return e}}c(t6,"castValue");var jL,ad=J({"src/execution/expression-eval.ts"(){"use strict";Vt(),we(),jL=256}});function e6(e,t,s,n){let r=new Uint32Array(Math.min(t,n)),i=0,o=0,a=0;for(;i<t&&o<n;){let l=e[i],u=s[o];l===u?(r[a++]=l,i++,o++):l<u?i++:o++}return{data:r,count:a}}c(e6,"intersectSorted");function s6(e,t,s,n){let r=new Uint32Array(t+n),i=0,o=0,a=0;for(;i<t&&o<n;){let l=e[i],u=s[o];l===u?(r[a++]=l,i++,o++):l<u?(r[a++]=l,i++):(r[a++]=u,o++)}for(;i<t;)r[a++]=e[i++];for(;o<n;)r[a++]=s[o++];return{data:r,count:a}}c(s6,"unionSorted");var Rv,zk,WL=J({"src/execution/operators/filter.ts"(){"use strict";be(),Vt(),we(),Rv={"=":"filterEq","<":"filterLt",">":"filterGt","<=":"filterLe",">=":"filterGe"},zk=class{static{c(this,"FilterOperator")}predicate;evaluator;columnMapping;parallelDispatch;constructor(e,t,s,n){this.predicate=e,this.evaluator=t,this.columnMapping=s||null,this.parallelDispatch=n||null}async init(){}async process(e){if(e.size===0)return new kt(e.columns,0);if(this.parallelDispatch){let s=this._analyze(this.predicate);if(s){let n=await this._executeParallel(e,s);if(n)return n}}return this._executeFallback(e)}_analyze(e){if(!e)return null;if(e.kind==="BoundComparison"||e.kind==="BoundBinary"){let t=this._analyzeLogical(e);return t||this._analyzeComparison(e)}return e.kind==="BoundBetween"?this._analyzeBetween(e):null}_analyzeComparison(e){let t=e.op;if(!Rv[t])return null;let{columnRef:s,literal:n}=this._extractColumnAndLiteral(e);if(!s||n===null)return null;let r=s.dataType;if(!ri(r))return null;let i=Rv[t];return this.parallelDispatch.canParallelize(i,r,1)?{type:"simple",operation:i,dataType:r,columnIndex:s.columnIndex,value:n}:null}_analyzeBetween(e){let t=e;if(!t.expr||t.expr.kind!=="BoundColumnRef")return null;let s=t.expr;if(!ri(s.dataType))return null;let n=this._extractLiteralValue(t.low),r=this._extractLiteralValue(t.high);return n===null||r===null?null:{type:"between",operation:"filterBetween",dataType:s.dataType,columnIndex:s.columnIndex,low:n,high:r}}_analyzeLogical(e){let t=e.op;if(t!=="AND"&&t!=="OR")return null;let s=e,n=this._analyze(s.left),r=this._analyze(s.right);return!n||!r?null:{type:t==="AND"?"and":"or",left:n,right:r}}_extractColumnAndLiteral(e){let t=null,s=null,n=e;return n.left?.kind==="BoundColumnRef"&&n.right?.kind==="BoundLiteral"?(t=n.left,s=n.right.value):n.right?.kind==="BoundColumnRef"&&n.left?.kind==="BoundLiteral"&&(t=n.right,s=n.left.value),{columnRef:t,literal:s}}_extractLiteralValue(e){return!e||e.kind!=="BoundLiteral"?null:e.value}async _executeParallel(e,t){return t.type==="simple"?this._executeSimple(e,t):t.type==="between"?this._executeBetween(e,t):t.type==="and"?this._executeAnd(e,t):t.type==="or"?this._executeOr(e,t):null}async _executeSimple(e,t){let s=e.columns[t.columnIndex],n=this._getColumnData(e,s);if(!n)return null;let r=await this.parallelDispatch.filterParallel(n,n.length,t.operation,t.dataType,{value:t.value});if(!r)return null;let i=this._dropNullRows(s,r.selectionVector,r.matchCount);return this._applySelectionVector(e,r.selectionVector,i)}_dropNullRows(e,t,s){if(!e.hasNulls)return s;let n=0;for(let r=0;r<s;r++)e.isNull(t[r])||(t[n++]=t[r]);return n}async _executeBetween(e,t){let s=e.columns[t.columnIndex],n=this._getColumnData(e,s);if(!n)return null;let r=await this.parallelDispatch.filterParallel(n,n.length,t.operation,t.dataType,{low:t.low,high:t.high});if(!r)return null;let i=this._dropNullRows(s,r.selectionVector,r.matchCount);return this._applySelectionVector(e,r.selectionVector,i)}async _executeAnd(e,t){let s=await this._executeParallel(e,t.left);if(!s||s.size===0)return new kt(e.columns,0);let n=await this._executeParallel(e,t.right);if(!n||n.size===0)return new kt(e.columns,0);let r=s.selectionVector,i=n.selectionVector;if(!r||!i)return null;let o=e6(r,s.size,i,n.size);return this._applySelectionVector(e,o.data,o.count)}async _executeOr(e,t){let s=await this._executeParallel(e,t.left),n=await this._executeParallel(e,t.right);if(!s&&!n)return null;if(!s)return n;if(!n)return s;let r=s.selectionVector,i=n.selectionVector;if(!r&&!i)return null;if(!r)return n;if(!i)return s;let o=s6(r,s.size,i,n.size);return this._applySelectionVector(e,o.data,o.count)}_getColumnData(e,t){if(e.selectionVector)return null;let s=t.data;return s?s.subarray(0,t.length):null}_applySelectionVector(e,t,s){if(s===0)return new kt(e.columns,0);if(s===e.size&&!e.selectionVector)return e;let n=new kt(e.columns,s);return n.setSelectionVector(t.length===s?t:t.subarray(0,s),s),n}_executeFallback(e){let t=e.size,s=new Uint32Array(t),n=0;if(e.selectionVector){let i=e.selectionVector;for(let o=0;o<t;o++){let a=i[o];this.evaluator(e,a)&&(s[n++]=a)}}else for(let i=0;i<t;i++)this.evaluator(e,i)&&(s[n++]=i);if(n===0)return new kt(e.columns,0);if(n===t)return e;let r=new kt(e.columns,n);return n>64?r.setSelectionVector(s.subarray(0,n),n):r.setSelectionVector(s.slice(0,n),n),r}}}}),KL={};us(KL,{WasmDispatch:c(()=>ik,"WasmDispatch"),globalDispatch:c(()=>ct,"globalDispatch")});var ik,ct,ld=J({"src/wasm/dispatch.ts"(){"use strict";ik=class{static{c(this,"WasmDispatch")}kernels;wasmInstance;memory;constructor(){this.kernels=new Map,this.wasmInstance=null,this.memory=null}register(e,t,s){let n=`${e}:${t}`;this.kernels.set(n,s)}lookup(e,t){let s=`${e}:${t}`;return this.kernels.get(s)||null}has(e,t){return this.kernels.has(`${e}:${t}`)}setInstance(e,t){this.wasmInstance=e,this.memory=t}listKernels(){return[...this.kernels.keys()]}},ct=new ik}});function td(e){return e?e.kind==="BoundColumnRef"?HL.has(e.dataType):e.kind==="BoundLiteral"?typeof e.value=="number":e.kind==="BoundBinary"&&Uk.has(e.op)?td(e.left)&&td(e.right):e.kind==="BoundUnary"&&e.op==="-"?td(e.operand):!1:!1}c(td,"isVectorizableExpr");function n6(e,t,s){let n=t.selectionVector,r=e.data;if(e.dataType==="DECIMAL"){let i=new Float64Array(s);if(n)for(let o=0;o<s;o++)i[o]=Number(r[n[o]])/ok;else for(let o=0;o<s;o++)i[o]=Number(r[o])/ok;return i}if(JL.has(e.dataType)){if(n){let i=new Int32Array(s);for(let o=0;o<s;o++)i[o]=r[n[o]];return i}return r.subarray(0,s)}if(e.dataType==="FLOAT64"){if(n){let i=new Float64Array(s);for(let o=0;o<s;o++)i[o]=r[n[o]];return i}return r.subarray(0,s)}return null}c(n6,"extractColumnF64");async function ed(e,t,s,n){if(xo.kernels.size===0)return null;if(e.kind==="BoundColumnRef"){let r=r6(e,s);if(r<0)return null;let i=t.columns[r];if(!i||!i.data)return null;let o=n6(i,t,n);if(!o)return null;if(o instanceof Int32Array){let a=xo.lookup("widenI32ToF64","INT32");return a?await a(o):null}return o}if(e.kind==="BoundLiteral")return Number(e.value);if(e.kind==="BoundUnary"&&e.op==="-"){let r=await ed(e.operand,t,s,n);if(r===null)return null;if(typeof r=="number")return-r;let i=xo.lookup("negF64","FLOAT64");return i?await i(r):null}if(e.kind==="BoundBinary"&&Uk.has(e.op)){let r=await ed(e.left,t,s,n);if(r===null)return null;let i=await ed(e.right,t,s,n);if(i===null)return null;let o=r instanceof Float64Array||r instanceof Int32Array,a=i instanceof Float64Array||i instanceof Int32Array;if(o&&a){let l={"+":"vecAddF64","-":"vecSubF64","*":"vecMulF64","/":"vecDivF64"}[e.op],u=xo.lookup(l,"FLOAT64");return u?await u(r,i):null}if(o&&!a){let l={"+":"scalarAddF64","-":"scalarSubF64","*":"scalarMulF64","/":"scalarDivF64"}[e.op],u=xo.lookup(l,"FLOAT64");return u?await u(r,Number(i)):null}if(!o&&a){if(e.op==="+"||e.op==="*"){let l=e.op==="+"?"scalarAddF64":"scalarMulF64",u=xo.lookup(l,"FLOAT64");return u?await u(i,Number(r)):null}if(e.op==="-"){let l=xo.lookup("scalarSubRevF64","FLOAT64");return l?await l(Number(r),i):null}if(e.op==="/"){let l=xo.lookup("scalarDivRevF64","FLOAT64");return l?await l(Number(r),i):null}}}return null}c(ed,"evalVectorized");function r6(e,t){if(t){let s=`${e.tableAlias}.${e.columnName}`.toUpperCase();if(t.has(s))return t.get(s);let n=e.columnName.toUpperCase();if(t.has(n))return t.get(n)}return e.columnIndex>=0?e.columnIndex:-1}c(r6,"resolveColIndex");var HL,JL,Uk,ok,xo,YL=J({"src/execution/wasm-expr-eval.ts"(){"use strict";Vt(),ld(),HL=new Set(["INT32","FLOAT64","DATE","DECIMAL"]),JL=new Set(["INT32","DATE"]),Uk=new Set(["+","-","*","/"]),ok=100,xo=ct}});function i6(e,t){if(t){let s=`${e.tableAlias}.${e.columnName}`.toUpperCase();if(t.has(s))return t.get(s);let n=`${e.columnName}`.toUpperCase();if(t.has(n))return t.get(n)}return e.columnIndex}c(i6,"resolveColumnIndex2");function QL(e,t,s,n){if(!e||typeof e!="object")return;if(e.kind==="BoundColumnRef"){let i=t.columns[i6(e,s)];i&&i.hasNulls&&n.push(i);return}let r=e;for(let i of["left","right","operand","expr"]){let o=r[i];o&&QL(o,t,s,n)}}c(QL,"collectNullableColumns");function o6(e,t,s,n){let r=[];if(QL(t,s,n,r),r.length===0)return;let i=s.size,o=!1;for(let a=0;a<i;a++){let l=s.activeRowIndex(a),u=!1;for(let f of r)if(f.isNull(l)){u=!0;break}u?(Uw(e.nullBitmap,a),o=!0):Yl(e.nullBitmap,a)}e.hasNulls=o}c(o6,"applyNullMask");async function a6(e,t,s){if(!td(e)||t.size<ht.wasmMinChunkSize)return null;let n=await ed(e,t,s,t.size);if(n===null||typeof n=="number")return null;let r=new Qt("FLOAT64",t.size);return r.data.set(n),r.length=t.size,o6(r,e,t,s),r}c(a6,"tryWasmProject");var Vk,XL=J({"src/execution/operators/projection.ts"(){"use strict";Ts(),we(),be(),Vt(),YL(),Fe(),Zl(),Vk=class{static{c(this,"ProjectionOperator")}expressions;evaluators;resultTypes;columnMapping;parallelDispatch;colRefIndices;constructor(e,t,s=null,n=null,r){this.expressions=e,this.evaluators=t,this.resultTypes=s,this.columnMapping=n,this.parallelDispatch=r||null,this.colRefIndices=e.map(i=>i?.kind==="BoundColumnRef"?this._resolveColIdx(i):-1)}async init(){}async process(e){if(e.size===0)return new kt([],0);let t=[],s=!!e.selectionVector;for(let n=0;n<this.evaluators.length;n++){let r=this.colRefIndices[n],i=this.expressions[n],o=this.resultTypes?this.resultTypes[n]:i?.dataType||i?.resultType||"VARCHAR";if(r>=0&&!s){let u=e.columns[r];if(u){t.push(u);continue}}if(this.expressions[n]&&!this.parallelDispatch){let u=await a6(this.expressions[n],e,this.columnMapping);if(u){t.push(u);continue}}let a=this.evaluators[n],l=new Qt(o,e.size||1);for(let u=0;u<e.size;u++){let f=e.activeRowIndex(u),h=a(e,f);l.set(u,typeof h=="bigint"&&o!=="INT64"?Number(h):h)}l.length=e.size,t.push(l)}return new kt(t,e.size)}_resolveColIdx(e){if(this.columnMapping){let t=`${e.tableAlias}.${e.columnName}`.toUpperCase();if(this.columnMapping.has(t))return this.columnMapping.get(t);let s=e.columnName.toUpperCase();if(this.columnMapping.has(s))return this.columnMapping.get(s)}return e.columnIndex>=0?e.columnIndex:-1}}}});function ZL(e){if(typeof e=="number"){ak[0]=e;let t=lk[0]^Math.imul(lk[1],2654435761);return t=Math.imul(t^t>>>16,2246822507),(t^t>>>13)>>>0}if(typeof e=="string"){let t=2166136261;for(let s=0;s<e.length;s++)t^=e.charCodeAt(s),t=Math.imul(t,16777619);return t>>>0}return typeof e=="bigint"?ZL(Number(BigInt.asIntN(53,e))):typeof e=="boolean"&&e?1:0}c(ZL,"hashValue");var ak,lk,tP=J({"src/utils/hash.ts"(){"use strict";ak=new Float64Array(1),lk=new Uint32Array(ak.buffer)}});function l6(e){return e==null?0:ZL(e)}c(l6,"hashGroupKey");function vf(e,t=!1){if(t&&e.toUpperCase()==="COUNT")return()=>new aP;switch(e.toUpperCase()){case"SUM":return()=>new eP;case"COUNT":return()=>new sP;case"COUNT_STAR":return()=>new nP;case"AVG":return()=>new ck;case"AVG_PARTIAL":return()=>new ck;case"AVG_FINAL":return()=>new rP;case"MIN":return()=>new iP;case"MAX":return()=>new oP;default:throw new Error(`Unknown aggregate: ${e}`)}}c(vf,"getAccumulatorFactory");var cd,ew,eP,sP,nP,ck,rP,iP,oP,aP,lP=J({"src/execution/operators/hash-aggregate.ts"(){"use strict";Ts(),be(),we(),ld(),tP(),cd=class{static{c(this,"HashAggregateOperator")}groupByExtractors;groupByTypes;aggregateDefs;hasCachedValues;groups;groupKeys;constructor(e,t,s){this.groupByExtractors=e,this.groupByTypes=t,this.aggregateDefs=s,this.hasCachedValues=s.some(n=>n.valueKey),this.groups=new Map,this.groupKeys=[]}async init(){}async consume(e){if(this.groupByExtractors.length===0&&e.size>0&&ct&&ct.kernels.size>0&&await this._tryWasmUngrouped(e))return;let t=e.size,s=!!e.selectionVector,n=e.selectionVector,r=this.groupByExtractors.length,i=this.aggregateDefs.length,o=new Array(r);for(let u=0;u<r;u++){o[u]=new Array(t);let f=this.groupByExtractors[u];for(let h=0;h<t;h++){let p=s?n[h]:h;o[u][h]=f(e,p)}}let a=new Array(i),l=new Set;for(let u=0;u<i;u++){let f=this.aggregateDefs[u];if(this.hasCachedValues&&f.valueKey&&l.has(f.valueKey)){for(let h=0;h<u;h++)if(this.aggregateDefs[h].valueKey===f.valueKey){a[u]=a[h];break}}else{a[u]=new Array(t);for(let h=0;h<t;h++){let p=s?n[h]:h;a[u][h]=f.extractValue(e,p)}f.valueKey&&l.add(f.valueKey)}}for(let u=0;u<t;u++){let f;if(r===0)f=ew;else if(r===1)f=o[0][u];else{let p=new Array(r);for(let d=0;d<r;d++){let m=o[d][u];p[d]=typeof m=="bigint"?m.toString():String(m)}f=p.join("|")}let h=this.groups.get(f);if(!h){let p=new Array(r);for(let d=0;d<r;d++)p[d]=o[d][u];h={groupValues:p,accumulators:this.aggregateDefs.map(d=>d.createAccumulator())},this.groups.set(f,h)}for(let p=0;p<i;p++)h.accumulators[p].add(a[p][u])}}async finalize(){if(this.groups.size===0){if(this.groupByExtractors.length===0){let o=this.aggregateDefs.map(a=>{let l=new Qt(a.resultType,1),u=a.createAccumulator();return l.set(0,u.result()),l.length=1,l});return[new kt(o,1)]}return[]}let t=this.groupByExtractors.length,s=this.aggregateDefs.length,n=t+s,r=[],i=Array.from(this.groups.values());for(let o=0;o<i.length;o+=We){let l=Math.min(o+We,i.length)-o,u=new Array(n);for(let f=0;f<t;f++)u[f]=new Qt(this.groupByTypes[f]||"VARCHAR",l);for(let f=0;f<s;f++)u[t+f]=new Qt(this.aggregateDefs[f].resultType,l);for(let f=0;f<l;f++){let h=i[o+f];for(let p=0;p<t;p++){let d=h.groupValues[p];u[p].set(f,typeof d=="bigint"?Number(d):d)}for(let p=0;p<s;p++)u[t+p].set(f,h.accumulators[p].result())}for(let f of u)f.length=l;r.push(new kt(u,l))}return r}exportPartials(e){let t=e-1,s=Array.from({length:e},()=>[]);for(let[n,r]of this.groups)s[l6(n)&t].push({key:n,groupValues:r.groupValues,states:r.accumulators.map(i=>i.exportState())});return s}absorbPartials(e){let t=this.aggregateDefs.length;for(let s of e){let n=this.groups.get(s.key);n||(n={groupValues:s.groupValues,accumulators:this.aggregateDefs.map(r=>r.createAccumulator())},this.groups.set(s.key,n));for(let r=0;r<t;r++)n.accumulators[r].mergeState(s.states[r])}}_resolveWasmAggKernel(e){let t=e.name?.toUpperCase();if(!t)return null;if(t==="SUM"&&e.resultType==="FLOAT64"){if(ct.has("sumF64","FLOAT64"))return{kernelKey:"sumF64",dataType:"FLOAT64",kind:"SUM"};if(ct.has("sumI32","INT32"))return{kernelKey:"sumI32",dataType:"INT32",kind:"SUM"}}if(t==="MIN"){if(e.resultType==="FLOAT64"&&ct.has("minF64","FLOAT64"))return{kernelKey:"minF64",dataType:"FLOAT64",kind:"MIN"};if(e.resultType==="INT32"&&ct.has("minI32","INT32"))return{kernelKey:"minI32",dataType:"INT32",kind:"MIN"}}if(t==="MAX"){if(e.resultType==="FLOAT64"&&ct.has("maxF64","FLOAT64"))return{kernelKey:"maxF64",dataType:"FLOAT64",kind:"MAX"};if(e.resultType==="INT32"&&ct.has("maxI32","INT32"))return{kernelKey:"maxI32",dataType:"INT32",kind:"MAX"}}return t==="COUNT"?{kernelKey:"countBits",dataType:"UINT8",kind:"COUNT"}:t==="COUNT_STAR"?{kernelKey:null,dataType:null,kind:"COUNT_STAR"}:t==="AVG"&&e.resultType==="FLOAT64"&&ct.has("sumF64","FLOAT64")?{kernelKey:"sumF64",dataType:"FLOAT64",kind:"AVG"}:null}async _tryWasmUngrouped(e){let t=e.size,s=new Array(this.aggregateDefs.length);for(let r=0;r<this.aggregateDefs.length;r++){let i=this.aggregateDefs[r],o=this._resolveWasmAggKernel(i);if(!o)return!1;if(o.kind==="COUNT_STAR"){s[r]={kind:"count",n:t};continue}if(o.kind==="COUNT"){if(i._wasmColIndex===void 0||i._wasmColIndex===null)return!1;let p=e.columns[i._wasmColIndex];if(!p)return!1;if(!p.hasNulls)s[r]={kind:"count",n:t};else{let d=ct.lookup("countBits","UINT8");if(!d)return!1;s[r]={kind:"count",n:await d(p.nullBitmap,t)}}continue}if(i._wasmColIndex===void 0||i._wasmColIndex===null)return!1;let a=e.columns[i._wasmColIndex];if(!a||!a.data||a.hasNulls)return!1;let l=a.dataType;if(!(o.dataType==="FLOAT64"&&l==="FLOAT64"||o.dataType==="INT32"&&(l==="INT32"||l==="DATE")))return!1;let f=ct.lookup(o.kernelKey,o.dataType);if(!f)return!1;let h=await f(a.data.subarray(0,t));s[r]=o.kind==="AVG"?{kind:"avg",sum:h,n:t}:{kind:"value",result:h}}let n=this.groups.get(ew);n||(n={groupValues:[],accumulators:this.aggregateDefs.map(r=>r.createAccumulator())},this.groups.set(ew,n));for(let r=0;r<s.length;r++){let i=s[r],o=n.accumulators[r];i.kind==="count"?o.count+=i.n:i.kind==="avg"?(o.sum+=i.sum,o.count+=i.n):o.add(i.result)}return!0}},ew="__ALL__",eP=class{static{c(this,"SumAccumulator")}sum;hasValue;constructor(){this.sum=0,this.hasValue=!1}add(e){e!=null&&(this.sum+=Number(e),this.hasValue=!0)}result(){return this.hasValue?this.sum:null}exportState(){return this.hasValue?this.sum:null}mergeState(e){e!=null&&(this.sum+=e,this.hasValue=!0)}},sP=class{static{c(this,"CountAccumulator")}count;constructor(){this.count=0}add(e){e!=null&&this.count++}result(){return this.count}exportState(){return this.count}mergeState(e){this.count+=e}},nP=class{static{c(this,"CountStarAccumulator")}count;constructor(){this.count=0}add(){this.count++}result(){return this.count}exportState(){return this.count}mergeState(e){this.count+=e}},ck=class{static{c(this,"AvgAccumulator")}sum;count;constructor(){this.sum=0,this.count=0}add(e){e!=null&&(this.sum+=Number(e),this.count++)}result(){return this.count>0?this.sum/this.count:null}exportState(){return{sum:this.sum,count:this.count}}mergeState(e){this.sum+=e.sum,this.count+=e.count}},rP=class{static{c(this,"AvgFinalAccumulator")}sum;count;constructor(){this.sum=0,this.count=0}add(e){if(!e)return;let t=e,s=t[0],n=t[1];s!=null&&n!==null&&n!==void 0&&(this.sum+=Number(s),this.count+=Number(n))}result(){return this.count>0?this.sum/this.count:null}exportState(){return{sum:this.sum,count:this.count}}mergeState(e){this.sum+=e.sum,this.count+=e.count}},iP=class{static{c(this,"MinAccumulator")}min;constructor(){this.min=null}add(e){e!=null&&(this.min===null||e<this.min)&&(this.min=e)}result(){return this.min}exportState(){return this.min}mergeState(e){e!=null&&(this.min===null||e<this.min)&&(this.min=e)}},oP=class{static{c(this,"MaxAccumulator")}max;constructor(){this.max=null}add(e){e!=null&&(this.max===null||e>this.max)&&(this.max=e)}result(){return this.max}exportState(){return this.max}mergeState(e){e!=null&&(this.max===null||e>this.max)&&(this.max=e)}},aP=class{static{c(this,"CountDistinctAccumulator")}values;constructor(){this.values=new Set}add(e){e!=null&&this.values.add(typeof e=="bigint"?Number(e):e)}result(){return this.values.size}exportState(){return Array.from(this.values)}mergeState(e){for(let t of e)this.values.add(t)}}}});function qw(e){return e==="DECIMAL"||e==="INT64"?"FLOAT64":e}c(qw,"normalizeExecType");function cP(e){let t=(e.name||"").toUpperCase();return t==="COUNT"||t==="COUNT_STAR"?"INT32":"FLOAT64"}c(cP,"normalizeAggResultType");function sd(e){if(!e||typeof e!="object")return String(e);switch(e.kind){case"BoundColumnRef":return`COL:${e.tableAlias||""}.${e.columnName}`;case"BoundLiteral":return`LIT:${String(e.value)}`;case"BoundBinary":return`BIN:${e.op}:${sd(e.left)}:${sd(e.right)}`;case"BoundUnary":return`UNARY:${e.op}:${sd(e.operand)}`;case"BoundCase":return`CASE:${JSON.stringify(e)}`;default:return JSON.stringify(e)}}c(sd,"expressionCacheKey");function vo(e){let t=new Map;for(let s=0;s<e.length;s++){let n=e[s],r=`${n.tableAlias||""}.${n.name}`.toUpperCase();t.set(r,s),t.has(n.name.toUpperCase())||t.set(n.name.toUpperCase(),s)}return t}c(vo,"schemaMappingOf");function Gk(e){return e.map((t,s)=>{let n=t;return{name:n?.outputName||n?.alias||n?.name||n?.columnName||`col${s}`,dataType:qw(n?.dataType||n?.resultType||"VARCHAR"),tableAlias:""}})}c(Gk,"projectionSchemaOf");function Pa(e,t=[]){if(!e||typeof e!="object")return t;if(Array.isArray(e)){for(let s of e)Pa(s,t);return t}if(e.kind==="BoundColumnRef")return t.push(e),t;for(let s of Object.values(e))Pa(s,t);return t}c(Pa,"collectColumnRefs");function qk(e,t){let s=`${e.tableAlias||""}.${e.columnName}`.toUpperCase();if(t.has(s))return t.get(s);let n=String(e.columnName).toUpperCase();if(t.has(n))return t.get(n)}c(qk,"resolveRef");function Bw(e,t){for(let s of Pa(e))if(qk(s,t)===void 0)return!1;return!0}c(Bw,"exprResolvable");function uP(e,t){let s=new Map;for(let n of e){if(!n.args||n.args.length===0)continue;let r=sd(n.args[0]);s.set(r,(s.get(r)||0)+1)}return e.map(n=>{let r=n.args&&n.args.length>0,i=r?Dt(n.args[0],t):()=>1,o=r?sd(n.args[0]):null,a;if(r&&n.args[0].kind==="BoundColumnRef"){let l=qk(n.args[0],t);l!==void 0&&(a=l)}return{name:n.name,valueKey:o&&(s.get(o)??0)>1?o:null,resultType:cP(n),createAccumulator:vf(n.name,n.distinct),extractValue:c((l,u)=>{let f=i(l,u);return typeof f=="bigint"?Number(f):f},"extractValue"),_wasmColIndex:a,_sourceExpr:r?n.args[0]:null,_columnMapping:t}})}c(uP,"buildAggregateDefs");function fP(e){let t=[],s=e;for(;s;)if(s.type==="Filter")t.push({kind:"filter",condition:s.condition}),s=s.children[0];else if(s.type==="Project")t.push({kind:"project",expressions:s.expressions}),s=s.children[0];else return s.type==="Scan"?(t.reverse(),{table:s.table,alias:s.alias||s.table,scanColumns:s.columns,stages:t}):null;return null}c(fP,"extractStageChain");function c6(e){return fP((e.children||[])[0]??null)}c(c6,"extractAggregateFragment");function Cw(e,t){let s=e;for(let n of t)n.kind==="project"&&(s=Gk(n.expressions));return s}c(Cw,"stagedSchemaOf");function uk(e,t){let s=e,n=vo(s);for(let r of t)if(r.kind==="filter"){if(!Bw(r.condition,n))return!1}else{if(!Bw(r.expressions,n))return!1;s=Gk(r.expressions),n=vo(s)}return!0}c(uk,"stagesResolvable");function u6(e,t){let s=e,n=vo(s),r=[];for(let i of t)if(i.kind==="filter"){let o=Dt(i.condition,n);r.push(new zk(i.condition,o,n,null))}else{let o=i.expressions.map(l=>Dt(l,n)),a=i.expressions.map(l=>{let u=l;return qw(u?.dataType||u?.resultType||"VARCHAR")});r.push(new Vk(i.expressions,o,a,n,null)),s=Gk(i.expressions),n=vo(s)}return{operators:r,schema:s,mapping:n}}c(u6,"instantiateStages");function f6(e,t,s){let n=s.map(p=>({name:p.name,dataType:p.dataType,tableAlias:e.alias})),r=[],i=!1;for(let p of e.stages)if(p.kind==="filter")Pa(p.condition,r);else{Pa(p.expressions,r),i=!0;break}if(!i){Pa(t.groupBy||[],r);for(let p of t.aggregates)Pa(p.args||[],r)}let o=vo(n),a=new Set;for(let p of r){let d=qk(p,o);if(d===void 0)return null;a.add(d)}let l=Array.from(a).sort((p,d)=>p-d),f={baseSchema:l.map(p=>n[p]),stages:e.stages,groupBy:t.groupBy||[],aggregates:t.aggregates.map(p=>({name:p.name,distinct:!!p.distinct,args:p.args||[]}))};if(!h6(f))return null;let h=Math.ceil(l.length/8);for(let p of l)h+=Rf(n[p].dataType)||Uint16Array.BYTES_PER_ELEMENT;return{spec:f,columnIndexes:l,estimatedRowBytes:h}}c(f6,"buildFragmentSpec");function h6(e){if(!uk(e.baseSchema,e.stages))return!1;let t=vo(Cw(e.baseSchema,e.stages));for(let s of e.groupBy)if(!Bw(s,t))return!1;for(let s of e.aggregates)if(!Bw(s.args,t))return!1;return!0}c(h6,"validateFragmentSpec");function p6(e){let{operators:t,mapping:s}=u6(e.baseSchema,e.stages),n=e.groupBy.map(o=>Dt(o,s)),r=e.groupBy.map(o=>{let a=o;return qw(a?.dataType||a?.resultType||"VARCHAR")}),i=uP(e.aggregates,s);return{operators:t,aggregate:new cd(n,r,i)}}c(p6,"instantiateFragment");function sw(e){return p6(e).aggregate}c(sw,"instantiateAggregate");function bM(e,t){let s=`${e.tableAlias}.${e.columnName}`.toUpperCase();if(t.has(s))return t.get(s);let n=`${e.columnName}`.toUpperCase();return t.has(n)?t.get(n):e.columnIndex}c(bM,"execResolvedIndex");function Bv(e,t,s){for(let n of Pa(e)){let r=bM(n,t),i=bM(n,s);if(r==null||r!==i)return!1}return!0}c(Bv,"refsResolveIdentically");function d6(e){return e.map(t=>({name:t.name,dataType:t.dataType,tableAlias:t.tableAlias||""}))}c(d6,"plainSchemaOf");function m6(e,t){if(e.length!==t.length)return!1;for(let s=0;s<e.length;s++)if(e[s].name.toUpperCase()!==t[s].name.toUpperCase()||e[s].dataType!==t[s].dataType||(e[s].tableAlias||"").toUpperCase()!==(t[s].tableAlias||"").toUpperCase())return!1;return!0}c(m6,"schemasEqual");function g6({build:e,probe:t,buildKeys:s,probeKeys:n,residualCondition:r,joinType:i,buildPreserved:o,uniqueKeys:a,buildMapping:l,probeMapping:u,combinedMapping:f}){if(!uk(e.baseSchema,e.stages)||!uk(t.baseSchema,t.stages))return null;let h=vo(Cw(e.baseSchema,e.stages)),p=vo(Cw(t.baseSchema,t.stages)),d=vo([...e.schema,...t.schema]);return!Bv(s,l,h)||!Bv(n,u,p)||r&&!Bv(r,f,d)?null:{build:e,probe:t,buildKeys:s,probeKeys:n,residualCondition:r||null,joinType:i,buildPreserved:!!o,uniqueKeys:!!a,buildColCount:e.schema.length,probeColCount:t.schema.length}}c(g6,"buildJoinSpec");var jw=J({"src/execution/fragment-spec.ts"(){"use strict";Ft(),Vt(),we(),ad(),WL(),XL(),lP()}}),Ww,jk=J({"src/execution/result-sink.ts"(){"use strict";Fe(),Ww=class{static{c(this,"ResultSink")}_streaming;_capacity;_queue;_head;_tail;_count;_totalRows;_done;_error;_producerResolve;_consumerResolve;_collected;constructor(e=!1){this._streaming=e,this._capacity=ht.sinkQueueCapacity,this._queue=new Array(this._capacity),this._head=0,this._tail=0,this._count=0,this._totalRows=0,this._done=!1,this._error=null,this._producerResolve=null,this._consumerResolve=null,this._collected=[]}async init(){}async consume(e){if(!(!e||e.size===0)){if(this._error)throw this._error;if(this._totalRows+=e.size,!this._streaming){this._collected.push(e);return}if(this._count===this._capacity&&await new Promise(t=>{this._producerResolve=t}),this._error)throw this._error;if(this._queue[this._tail]=e,this._tail=(this._tail+1)%this._capacity,this._count++,this._consumerResolve){let t=this._consumerResolve;this._consumerResolve=null,t()}}}async finalize(){if(this._done=!0,this._consumerResolve){let e=this._consumerResolve;this._consumerResolve=null,e()}}error(e){if(this._error=e,this._done=!0,this._consumerResolve){let t=this._consumerResolve;this._consumerResolve=null,t()}if(this._producerResolve){let t=this._producerResolve;this._producerResolve=null,t()}}get totalRows(){return this._totalRows}get chunks(){return this._collected}_dequeue(){let e=this._queue[this._head];if(this._queue[this._head]=void 0,this._head=(this._head+1)%this._capacity,this._count--,this._producerResolve){let t=this._producerResolve;this._producerResolve=null,t()}return e}[Symbol.asyncIterator](){if(!this._streaming){let t=0,s=this._collected;return{async next(){return t<s.length?{done:!1,value:s[t++]}:{done:!0,value:void 0}}}}let e=this;return{async next(){for(;e._count===0;){if(e._error)throw e._error;if(e._done)return{done:!0,value:void 0};await new Promise(t=>{e._consumerResolve=t})}if(e._error)throw e._error;return{done:!1,value:e._dequeue()}}}}async collect(){let e=[];for await(let t of this)e.push(t);return e}}}}),Wk={};us(Wk,{CancelToken:c(()=>Kk,"CancelToken"),PipelineGraph:c(()=>Bf,"PipelineGraph")});var Bf,Kk,ud=J({"src/execution/pipeline.ts"(){"use strict";Bf=class{static{c(this,"PipelineGraph")}pipelines;nextId;constructor(){this.pipelines=new Map,this.nextId=1}createPipeline(e){let t=this.nextId++;return this.pipelines.set(t,{id:t,sink:e,source:null,dependencies:new Set,dependents:new Set,state:"PENDING",cancelled:!1}),t}addDependency(e,t){let s=this.pipelines.get(e),n=this.pipelines.get(t);s.dependencies.add(t),n.dependents.add(e)}setSource(e,t){let s=this.pipelines.get(e);s.source=t}getReadyPipelines(){let e=[];for(let[t,s]of this.pipelines.entries())s.state==="PENDING"&&s.dependencies.size===0&&e.push(s);return e}markPipelineDone(e){let t=this.pipelines.get(e);t.state="DONE";for(let s of t.dependents)this.pipelines.get(s).dependencies.delete(e)}cancelPipeline(e){let t=this.pipelines.get(e);!t||t.cancelled||(t.cancelled=!0)}isCancelled(e){let t=this.pipelines.get(e);return t?t.cancelled:!1}},Kk=class{static{c(this,"CancelToken")}cancelled;constructor(){this.cancelled=!1}cancel(){this.cancelled=!0}get isCancelled(){return this.cancelled}}}}),Hk={};us(Hk,{TaskScheduler:c(()=>Cf,"TaskScheduler")});var Cf,Kw=J({"src/execution/scheduler.ts"(){"use strict";Cf=class{static{c(this,"TaskScheduler")}concurrency;constructor(e=4){this.concurrency=e}async schedule(e){let t=!0;for(;t;){let s=e.getReadyPipelines();if(s.length===0){let n=0;for(let r of e.pipelines.values())r.state==="PENDING"&&n++;if(n>0)throw new Error("Pipeline deadlock detected: pending pipelines with unresolved dependencies.");break}for(let n of s)n.state="RUNNING";await this.executePipelines(s,e);for(let n of s)e.markPipelineDone(n.id)}}async executePipelines(e,t){let s=[];for(let n of e)n.source&&s.push(this.runPipelineSource(n,t));await Promise.all(s)}async runPipelineSource(e,t){let s=e.source();for await(let n of s)if(e.cancelled)break}}}}),hP,pP,dP,Jk=J({"src/storage/storage-constants.ts"(){"use strict";hP=".qdat",pP=".spill",dP="mem://query_engine"}}),mP,y6=J({"src/storage/temp-space/memory-temp-space.ts"(){"use strict";Jk(),mP=class{static{c(this,"MemoryTempSpace")}rootDir;counters;constructor(e={}){this.rootDir=e.baseDir||dP,this.counters=new Map}allocate(e,t){let s=this.counters.get(e)||0;return this.counters.set(e,s+1),`${this.rootDir}/${e}/${t}_${s}`}getRoot(){return this.rootDir}cleanup(){this.counters.clear()}}}}),gP,_6=J({"src/storage/page-store/memory-page-store.ts"(){"use strict";gP=class{static{c(this,"MemoryPageStore")}pages;constructor(){this.pages=new Map}async write(e,t){this.pages.set(e,t)}async read(e){return this.pages.get(e)??null}clear(){this.pages.clear()}}}});function $p(e,t,s,n){return new Uint8Array(s.buffer,s.byteOffset,n).set(e.subarray(t,t+n)),t+n}c($p,"copyBytesInto");function w6(e){let t=6;for(let s of e.columns){let n=s instanceof pr;if(t+=7,s.hasNulls){let r=Math.ceil(s.length/32);t+=4+r*4}if(n){let r=s;t+=r.length*2,t+=4;for(let i=0;i<r.reverseDict.length;i++)t+=4+Buffer.byteLength(r.reverseDict[i],"utf8")}else if(ri(s.dataType))t+=s.length*Rf(s.dataType);else{let r=s;t+=4+(r.length+1)*4+r.stringBytesUsed}}return t}c(w6,"computeSize");var xM,Cv,Tv,SM,kf,yP=J({"src/storage/serializer.ts"(){"use strict";Ts(),Gw(),be(),we(),tc(),Zl(),xM=0,Cv=1,Tv={BOOLEAN:0,INT32:1,INT64:2,FLOAT64:3,DECIMAL:4,VARCHAR:5,DATE:6,TIMESTAMP:7},SM=Object.fromEntries(Object.entries(Tv).map(([e,t])=>[t,e])),kf=class{static{c(this,"ChunkSerializer")}static serialize(e){let t=w6(e),s=Buffer.allocUnsafe(t),n=0;s.writeUInt32LE(e.size,n),n+=4,s.writeUInt16LE(e.columns.length,n),n+=2;for(let r of e.columns){let i=r instanceof pr;if(s.writeUInt8(i?Cv:xM,n),n+=1,s.writeUInt8(Tv[r.dataType],n),n+=1,s.writeUInt32LE(r.length,n),n+=4,s.writeUInt8(r.hasNulls?1:0,n),n+=1,r.hasNulls){let o=Math.ceil(r.length/32);s.writeUInt32LE(o,n),n+=4;let a=o*4;Buffer.from(r.nullBitmap.buffer,r.nullBitmap.byteOffset,a).copy(s,n),n+=a}if(i){let o=r,a=o.length*2;Buffer.from(o.indices.buffer,o.indices.byteOffset,a).copy(s,n),n+=a;let l=o.reverseDict.length;s.writeUInt32LE(l,n),n+=4;for(let u=0;u<l;u++){let f=Buffer.from(o.reverseDict[u],"utf8");s.writeUInt32LE(f.length,n),n+=4,f.copy(s,n),n+=f.length}}else if(ri(r.dataType)){let o=r,a=Rf(o.dataType),l=o.length*a;Buffer.from(o.data.buffer,o.data.byteOffset,l).copy(s,n),n+=l}else{let o=r;s.writeUInt32LE(o.stringBytesUsed,n),n+=4;let a=(o.length+1)*4;Buffer.from(o.offsets.buffer,o.offsets.byteOffset,a).copy(s,n),n+=a,Buffer.from(o.stringBytes.buffer,o.stringBytes.byteOffset,o.stringBytesUsed).copy(s,n),n+=o.stringBytesUsed}}return s}static deserialize(e,t=$a){let s=0,n=e.readUInt32LE(s);s+=4;let r=e.readUInt16LE(s);s+=2;let i=[];for(let o=0;o<r;o++){let a=e.readUInt8(s);s+=1;let l=e.readUInt8(s);s+=1;let u=e.readUInt32LE(s);s+=4;let f=e.readUInt8(s)!==0;s+=1;let h=SM[l],p;if(f){let d=e.readUInt32LE(s);s+=4,p=t.acquire(Uint32Array,d),s=$p(e,s,p,d*4)}else p=t.acquire(Uint32Array,Fa(Math.max(u,1)));if(a===Cv){let d=t.acquire(Uint16Array,u);s=$p(e,s,d,u*2);let m=e.readUInt32LE(s);s+=4;let g=[];for(let y=0;y<m;y++){let _=e.readUInt32LE(s);s+=4,g.push(e.toString("utf8",s,s+_)),s+=_}i.push(pr.fromParts({indices:d,reverseDict:g,nullBitmap:p,length:u,hasNulls:f,allocator:t}))}else if(ri(h)){let d=t.acquire(Nf(h),u);s=$p(e,s,d,u*Rf(h)),i.push(Qt.fromParts({dataType:h,data:d,nullBitmap:p,length:u,hasNulls:f,allocator:t}))}else{let d=e.readUInt32LE(s);s+=4;let m=t.acquire(Uint32Array,u+1);s=$p(e,s,m,(u+1)*4);let g=t.acquire(Uint8Array,d);s=$p(e,s,g,d),i.push(Qt.fromParts({dataType:h,offsets:m,stringBytes:g,stringBytesUsed:d,nullBitmap:p,length:u,hasNulls:f,allocator:t}))}}return new kt(i,n)}}}}),Iv,Yk,_P=J({"src/storage/spill-manager/spill-manager.ts"(){"use strict";yP(),Iv=4,Yk=class{static{c(this,"SpillManager")}storage;constructor(e){this.storage=e}async appendChunk(e,t){if(!t||t.size===0)return;let s=kf.serialize(t),n=Buffer.allocUnsafe(Iv);n.writeUInt32LE(s.length,0),await this.storage.append(e,Buffer.concat([n,s]))}async*readChunks(e){let t=await this.storage.read(e);if(!t)return;let s=0;for(;s<t.length;){let n=t.readUInt32LE(s);s+=Iv;let r=t.subarray(s,s+n);yield kf.deserialize(r),s+=n}}async clearPartition(e){await this.storage.remove(e)}async clearAll(){await this.storage.removeAll()}hasSpilled(e){return this.storage.exists(e)}}}}),wP,b6=J({"src/storage/spill-manager/memory-storage.ts"(){"use strict";wP=class{static{c(this,"MemoryStorage")}store;constructor(){this.store=new Map}async append(e,t){this.store.has(e)||this.store.set(e,[]),this.store.get(e).push(Buffer.from(t))}async read(e){let t=this.store.get(e);return!t||t.length===0?null:Buffer.concat(t)}exists(e){let t=this.store.get(e);return!!t&&t.length>0}async remove(e){this.store.delete(e)}async removeAll(){this.store.clear()}}}}),bP,x6=J({"src/storage/backend/memory-storage-backend.ts"(){"use strict";y6(),_6(),_P(),b6(),bP=class{static{c(this,"MemoryStorageBackend")}options;constructor(e={}){this.options=e}createTempSpace(){return new mP(this.options)}createPageStore(){return new gP}createSpillManager(){return new Yk(new wP)}}}}),xP,S6=J({"src/execution/operators/scan.ts"(){"use strict";xP=class{static{c(this,"ScanOperator")}table;projectedColumns;constructor(e,t){this.table=e,this.projectedColumns=t||null}async init(){}async*scan(){for await(let e of this.table.scan())this.projectedColumns?yield e.project(this.projectedColumns):yield e}estimatedRows(){return this.table.rowCount()}}}}),SP,A6=J({"src/execution/operators/index-scan.ts"(){"use strict";Ts(),be(),Fe(),SP=class{static{c(this,"IndexScanOperator")}btreeIndex;table;scanType;scanKey;scanLow;scanHigh;lowInc;highInc;projectedColumns;constructor(e,t,s,n,r,i,o,a,l){this.btreeIndex=e,this.table=t,this.scanType=s,this.scanKey=n,this.scanLow=r,this.scanHigh=i,this.lowInc=o,this.highInc=a,this.projectedColumns=l}async*scan(){let e;if(this.scanType==="point"?e=this.btreeIndex.search(this.scanKey):e=[...this.btreeIndex.range(this.scanLow,this.scanHigh,this.lowInc,this.highInc)],e.length===0)return;let t=new Map;for(let i of e){let o=t.get(i.pageId);o||(o=[],t.set(i.pageId,o)),o.push(i.rowIndex)}let s=this.table.getSchema(),n=this.projectedColumns?this.projectedColumns.map(i=>s[i]):s,r=[];for(let[i,o]of t){let a=await this.table.bufferPool.fetchPage(i,!0);for(let l of o){let u=[];if(this.projectedColumns)for(let f of this.projectedColumns)u.push(a.columns[f].get(l));else for(let f=0;f<a.columns.length;f++)u.push(a.columns[f].get(l));r.push(u),r.length>=ht.flushBatchSize&&(yield this._buildChunk(r,n),r=[])}}r.length>0&&(yield this._buildChunk(r,n))}_buildChunk(e,t){let s=t.length,n=new Array(s);for(let r=0;r<s;r++){let i=new Qt(t[r].dataType,e.length);for(let o=0;o<e.length;o++)i.set(o,e[o][r]);i.length=e.length,n[r]=i}return new kt(n,e.length)}}}});async function v6(e,t){let s=e.catalog.getTableStorage(t.table);if(!s)throw new Error(`No storage for table: ${t.table}`);let n=s.getSchema(),r=e.resolveProjectedColumnIndexes(n,t.columns),o=(r?r.map(l=>n[l]):n).map(l=>({...l,tableAlias:t.alias||t.table})),a=e.buildSchemaMapping(o,t.alias||t.table);return{schema:o,columnMapping:a,register:c((l,u,f)=>{let h=new xP(s,r),p=c(async function*(){for await(let d of h.scan()){if(f.cancelToken?.isCancelled)break;await f.consume(d),yield d}f.finalize&&await f.finalize()},"source");l.setSource(u,p)},"register")}}c(v6,"buildScan");async function k6(e,t){let s=e.catalog.getTableStorage(t.table);if(!s)throw new Error(`No storage for table: ${t.table}`);let n=e.catalog.getIndexForColumn(t.table,t.columnName);if(!n)throw new Error(`No index for ${t.table}.${t.columnName}`);let r=s.getSchema(),i=e.resolveProjectedColumnIndexes(r,t.columns),a=(i?i.map(u=>r[u]):r).map(u=>({...u,tableAlias:t.alias||t.table})),l=e.buildSchemaMapping(a,t.alias||t.table);return{schema:a,columnMapping:l,register:c((u,f,h)=>{let p=new SP(n,s,t.scanType,t.scanKey,t.scanLow,t.scanHigh,t.lowInc,t.highInc,i),d=c(async function*(){for await(let m of p.scan()){if(h.cancelToken?.isCancelled)break;await h.consume(m),yield m}h.finalize&&await h.finalize()},"source");u.setSource(f,d)},"register")}}c(k6,"buildIndexScan");async function E6(e,t){return{schema:[],columnMapping:new Map,register:c((s,n,r)=>{let i=c(async function*(){let o=new kt([],1);await r.consume(o),yield o,r.finalize&&await r.finalize()},"source");s.setSource(n,i)},"register")}}c(E6,"buildSingleRow");async function N6(e,t){let s=await e.buildPipeline(t.children[0]);return{schema:s.schema,columnMapping:s.columnMapping,register:c((n,r,i)=>{let o=c(async function*(){},"source");n.setSource(r,o)},"register")}}c(N6,"buildEmpty");var R6=J({"src/execution/builders/source-builders.ts"(){"use strict";S6(),A6(),be()}}),AP,B6=J({"src/utils/priority-queue.ts"(){"use strict";AP=class{static{c(this,"PriorityQueue")}_heap;_comparator;constructor(e=(t,s)=>t-s){this._heap=[],this._comparator=e}get size(){return this._heap.length}isEmpty(){return this.size===0}peek(){return this._heap[0]}push(e){this._heap.push(e),this._siftUp()}pop(){if(this.isEmpty())return null;let e=this.peek(),t=this.size-1;return t>0&&(this._heap[0]=this._heap[t]),this._heap.pop(),this._siftDown(),e}_parent(e){return(e+1>>>1)-1}_left(e){return(e<<1)+1}_right(e){return e+1<<1}_siftUp(){let e=this.size-1;for(;e>0&&this._compare(e,this._parent(e));)this._swap(e,this._parent(e)),e=this._parent(e)}_siftDown(){let e=0;for(;this._left(e)<this.size&&this._compare(this._left(e),e)||this._right(e)<this.size&&this._compare(this._right(e),e);){let t=this._right(e)<this.size&&this._compare(this._right(e),this._left(e))?this._right(e):this._left(e);this._swap(e,t),e=t}}_compare(e,t){return this._comparator(this._heap[e],this._heap[t])<0}_swap(e,t){let s=this._heap[e];this._heap[e]=this._heap[t],this._heap[t]=s}}}}),Qk,vP,C6=J({"src/execution/operators/sort.ts"(){"use strict";Ts(),be(),B6(),Fe(),Qk=class{static{c(this,"SortOperator")}keyExtractors;limit;offset;topN;rows;schema;spillManager;runCount;constructor(e,t,s,n){this.keyExtractors=e,this.limit=t??null,this.offset=s||0,this.topN=this.limit!==null?this.limit+this.offset:null,this.rows=[],this.schema=null,this.spillManager=n,this.runCount=0}async init(){}async consume(e){this.schema||(this.schema=e.columns.map(s=>s.dataType));let t=new Array(e.size);for(let s=0;s<e.size;s++){let n=e.activeRowIndex(s),r=new Array(e.columns.length);for(let o=0;o<e.columns.length;o++)r[o]=e.columns[o].get(n);let i=new Array(this.keyExtractors.length);for(let o=0;o<this.keyExtractors.length;o++)i[o]=this.keyExtractors[o].eval(e,n);t[s]={row:r,sortKeys:i}}this.rows.push(...t),this.topN&&this.runCount===0&&this.rows.length>this.topN*4&&(this.rows.sort((s,n)=>this.compareRows(s,n)),this.rows.length=this.topN),this.rows.length>=ht.memoryLimit&&await this.spillCurrentRun()}async spillCurrentRun(){if(this.rows.length===0)return;this.rows.sort((t,s)=>this.compareRows(t,s)),this.topN&&(this.rows.length=Math.min(this.rows.length,this.topN));let e=this.rowsToChunk(this.rows);await this.spillManager.appendChunk(`run_${this.runCount}`,e),this.runCount++,this.rows=[]}async finalize(){if(this.runCount===0){if(this.rows.sort((l,u)=>this.compareRows(l,u)),this.topN&&(this.rows.length=Math.min(this.rows.length,this.topN)),this.offset>0&&(this.rows=this.rows.slice(this.offset)),this.rows.length===0)return[];let a=this.rowsToChunk(this.rows);return await this.spillManager.clearAll(),[a]}this.rows.length>0&&await this.spillCurrentRun();let e=[];for(let a=0;a<this.runCount;a++)e.push(this.spillManager.readChunks(`run_${a}`));let t=new AP((a,l)=>this.compareRows(a.item,l.item)),s=new Array(this.runCount);for(let a=0;a<this.runCount;a++){let l=e[a],u=await l.next();!u.done&&u.value.size>0&&(s[a]={iter:l,chunk:u.value,index:0,chunkItems:this.chunkToItems(u.value)},t.push({item:s[a].chunkItems[0],runIndex:a}),s[a].index=1)}let n=[],r=[],i=0,o=0;for(;!t.isEmpty()&&!(this.topN&&i>=this.topN);){let{item:a,runIndex:l}=t.pop();i++,o<this.offset?o++:r.push(a),r.length>=ht.flushBatchSize&&(n.push(this.rowsToChunk(r)),r=[]);let u=s[l];if(u.index<u.chunkItems.length)t.push({item:u.chunkItems[u.index],runIndex:l}),u.index++;else{let f=await u.iter.next();!f.done&&f.value.size>0&&(u.chunk=f.value,u.index=0,u.chunkItems=this.chunkToItems(u.chunk),t.push({item:u.chunkItems[u.index],runIndex:l}),u.index++)}}return r.length>0&&n.push(this.rowsToChunk(r)),await this.spillManager.clearAll(),n}chunkToItems(e){let t=new Array(e.size);for(let s=0;s<e.size;s++){let n=e.activeRowIndex(s),r=new Array(e.columns.length);for(let o=0;o<e.columns.length;o++)r[o]=e.columns[o].get(n);let i=new Array(this.keyExtractors.length);for(let o=0;o<this.keyExtractors.length;o++)i[o]=this.keyExtractors[o].eval(e,n);t[s]={row:r,sortKeys:i}}return t}rowsToChunk(e){if(e.length===0)return new kt([],0);let t=e[0].row.length,s=new Array(t);for(let n=0;n<t;n++){let r=new Qt(this.schema?.[n]||"VARCHAR",e.length);for(let i=0;i<e.length;i++)r.set(i,e[i].row[n]);r.length=e.length,s[n]=r}return new kt(s,e.length)}compareRows(e,t){for(let s=0;s<this.keyExtractors.length;s++){let n=e.sortKeys[s],r=t.sortKeys[s];if(typeof n=="bigint"&&(n=Number(n)),typeof r=="bigint"&&(r=Number(r)),n===null&&r!==null)return 1;if(n!==null&&r===null)return-1;if(!(n===null&&r===null)){if(n<r)return this.keyExtractors[s].direction==="ASC"?-1:1;if(n>r)return this.keyExtractors[s].direction==="ASC"?1:-1}}return 0}},vP=class{static{c(this,"LimitOperator")}limit;offset;seen;emitted;chunks;schema;done;constructor(e,t=0){this.limit=e,this.offset=t,this.seen=0,this.emitted=0,this.chunks=[],this.schema=null,this.done=!1}async init(){}async consume(e){if(this.done)return;this.schema||(this.schema=e.columns.map(a=>a.dataType));let t=this.seen,s=this.seen+e.size;if(this.seen=s,s<=this.offset)return;let n=Math.max(0,this.offset-t),r=this.limit-this.emitted;if(r<=0){this.done=!0;return}let o=Math.min(e.size,n+r)-n;if(!(o<=0)){if(n===0&&o===e.size&&!e.selectionVector)this.chunks.push(e);else{let a=new Uint32Array(o);for(let u=0;u<o;u++)a[u]=e.activeRowIndex(n+u);let l=new kt(e.columns,o);l.setSelectionVector(a,o),this.chunks.push(l)}this.emitted+=o,this.emitted>=this.limit&&(this.done=!0)}}async finalize(){return this.chunks.length===0?[]:this.chunks.map(e=>e.selectionVector?e.flatten():e)}}}}),kP,T6=J({"src/execution/operators/distinct.ts"(){"use strict";be(),kP=class{static{c(this,"DistinctOperator")}seen;schema;_legacyChunks;constructor(){this.seen=new Set,this.schema=null}async init(){}async process(e){this.schema||(this.schema=e.columns.map(r=>r.dataType));let t=new Uint32Array(e.size),s=0;for(let r=0;r<e.size;r++){let i=e.activeRowIndex(r),o="";for(let a=0;a<e.columns.length;a++)a>0&&(o+="|"),o+=String(e.columns[a].get(i));this.seen.has(o)||(this.seen.add(o),t[s++]=i)}if(s===0)return new kt(e.columns,0);if(s===e.size)return e;let n=new kt(e.columns,s);return n.setSelectionVector(t.slice(0,s),s),n}async consume(e){this._legacyChunks||(this._legacyChunks=[]);let t=await this.process(e);t.size>0&&this._legacyChunks.push(t.selectionVector?t.flatten():t)}async finalize(){return this._legacyChunks||[]}}}}),EP,I6=J({"src/execution/operators/union.ts"(){"use strict";be(),EP=class{static{c(this,"UnionOperator")}isAll;seen;schema;_legacyChunks;constructor(e){this.isAll=e,this.seen=e?null:new Set,this.schema=null}async init(){}async process(e){if(this.isAll)return e;this.schema||(this.schema=e.columns.map(r=>r.dataType));let t=new Uint32Array(e.size),s=0;for(let r=0;r<e.size;r++){let i=e.activeRowIndex(r),o="";for(let a=0;a<e.columns.length;a++)a>0&&(o+="|"),o+=String(e.columns[a].get(i));this.seen.has(o)||(this.seen.add(o),t[s++]=i)}if(s===0)return new kt(e.columns,0);if(s===e.size)return e;let n=new kt(e.columns,s);return n.setSelectionVector(t.slice(0,s),s),n}async consume(e){this._legacyChunks||(this._legacyChunks=[]);let t=await this.process(e);t.size>0&&this._legacyChunks.push(t.selectionVector?t.flatten():t)}async finalize(){return this._legacyChunks||[]}}}}),NP,O6=J({"src/execution/operators/window.ts"(){"use strict";be(),Ts(),NP=class{static{c(this,"WindowOperator")}windowExprs;childSchema;childColumnMapping;compileExpression;constructor(e,t,s,n){this.windowExprs=e,this.childSchema=t,this.childColumnMapping=s,this.compileExpression=n}async execute(e){let t=[];for(let r of e)for(let i=0;i<r.size;i++){let o=[];for(let a=0;a<r.columns.length;a++)o.push(r.columns[a].get(r.activeRowIndex?r.activeRowIndex(i):i));t.push(o)}if(t.length===0)return[];let s=[];for(let r of this.windowExprs)s.push(this.computeWindow(r,t,e));let n=[];for(let r=0;r<this.childSchema.length;r++){let i=new Qt(this.childSchema[r].dataType,t.length);for(let o=0;o<t.length;o++)i.set(o,t[o][r]);i.length=t.length,n.push(i)}for(let r=0;r<s.length;r++){let i=this.windowExprs[r].resultType||"INT64",o=new Qt(i==="INT64"?"FLOAT64":i,t.length);for(let a=0;a<t.length;a++)o.set(a,s[r][a]);o.length=t.length,n.push(o)}return[new kt(n,t.length)]}computeWindow(e,t,s){let n=e.partitionBy.map(u=>this.compileExpression(u,this.childColumnMapping)),r=e.orderBy.map(u=>({eval:this.compileExpression(u.expr,this.childColumnMapping),direction:u.direction||"ASC"})),i=c((u,f)=>{let h=0;for(let p of s){if(u<h+p.size)return f(p,u-h);h+=p.size}return null},"getVal"),o=this.partitionRows(t,n,s);if(r.length>0)for(let u of o)u.sort((f,h)=>{for(let p of r){let d=i(f,p.eval),m=i(h,p.eval),g=d==null,y=m==null;if(g&&y)continue;if(g)return 1;if(y)return-1;let _=this.compareValues(d,m);if(_!==0)return p.direction==="DESC"?-_:_}return 0});let a=new Array(t.length),l=e.name.toUpperCase();for(let u of o)switch(l){case"ROW_NUMBER":for(let f=0;f<u.length;f++)a[u[f]]=f+1;break;case"RANK":{let f=1;for(let h=0;h<u.length;h++)h>0&&!this.sameOrderKey(u[h],u[h-1],r,s)&&(f=h+1),a[u[h]]=f;break}case"DENSE_RANK":{let f=1;for(let h=0;h<u.length;h++)h>0&&!this.sameOrderKey(u[h],u[h-1],r,s)&&f++,a[u[h]]=f;break}case"LAG":{let f=e.args.length>0?this.compileExpression(e.args[0],this.childColumnMapping):null,h=e.args.length>1?e.args[1].value:1,p=e.args.length>2?e.args[2].value:null;for(let d=0;d<u.length;d++){let m=d-h;m>=0&&m<u.length?a[u[d]]=f?i(u[m],f):null:a[u[d]]=p}break}case"LEAD":{let f=e.args.length>0?this.compileExpression(e.args[0],this.childColumnMapping):null,h=e.args.length>1?e.args[1].value:1,p=e.args.length>2?e.args[2].value:null;for(let d=0;d<u.length;d++){let m=d+h;m>=0&&m<u.length?a[u[d]]=f?i(u[m],f):null:a[u[d]]=p}break}case"SUM":{let f=this.compileExpression(e.args[0],this.childColumnMapping);if(r.length===0){let h=0,p=0;for(let m=0;m<u.length;m++){let g=i(u[m],f);g!=null&&(h+=typeof g=="bigint"?Number(g):g,p++)}let d=p===0?null:h;for(let m=0;m<u.length;m++)a[u[m]]=d}else{let h=0,p=0;for(let d=0;d<u.length;d++){let m=i(u[d],f);m!=null&&(h+=typeof m=="bigint"?Number(m):m,p++),a[u[d]]=p===0?null:h}}break}case"AVG":{let f=this.compileExpression(e.args[0],this.childColumnMapping);if(r.length===0){let h=0,p=0;for(let m=0;m<u.length;m++){let g=i(u[m],f);g!=null&&(h+=typeof g=="bigint"?Number(g):g,p++)}let d=p===0?null:h/p;for(let m=0;m<u.length;m++)a[u[m]]=d}else{let h=0,p=0;for(let d=0;d<u.length;d++){let m=i(u[d],f);m!=null&&(h+=typeof m=="bigint"?Number(m):m,p++),a[u[d]]=p===0?null:h/p}}break}case"COUNT":case"COUNT_STAR":{let f=e.args.length>0?this.compileExpression(e.args[0],this.childColumnMapping):null;if(r.length===0){let h=0;for(let p=0;p<u.length;p++)f?i(u[p],f)!==null&&h++:h++;for(let p=0;p<u.length;p++)a[u[p]]=h}else{let h=0;for(let p=0;p<u.length;p++)f?i(u[p],f)!==null&&h++:h++,a[u[p]]=h}break}case"MIN":{let f=this.compileExpression(e.args[0],this.childColumnMapping);if(r.length===0){let h=null;for(let p=0;p<u.length;p++){let d=i(u[p],f);d!==null&&(h===null||d<h)&&(h=d)}for(let p=0;p<u.length;p++)a[u[p]]=h}else{let h=null;for(let p=0;p<u.length;p++){let d=i(u[p],f);d!==null&&(h===null||d<h)&&(h=d),a[u[p]]=h}}break}case"MAX":{let f=this.compileExpression(e.args[0],this.childColumnMapping);if(r.length===0){let h=null;for(let p=0;p<u.length;p++){let d=i(u[p],f);d!==null&&(h===null||d>h)&&(h=d)}for(let p=0;p<u.length;p++)a[u[p]]=h}else{let h=null;for(let p=0;p<u.length;p++){let d=i(u[p],f);d!==null&&(h===null||d>h)&&(h=d),a[u[p]]=h}}break}default:for(let f=0;f<u.length;f++)a[u[f]]=null}return a}partitionRows(e,t,s){if(t.length===0)return[e.map((i,o)=>o)];let n=c((i,o)=>{let a=0;for(let l of s){if(i<a+l.size)return o(l,i-a);a+=l.size}return null},"getVal"),r=new Map;for(let i=0;i<e.length;i++){let o=t.map(a=>{let l=n(i,a);return typeof l=="bigint"?Number(l):l}).join("|");r.has(o)||r.set(o,[]),r.get(o).push(i)}return[...r.values()]}sameOrderKey(e,t,s,n){let r=c((i,o)=>{let a=0;for(let l of n){if(i<a+l.size)return o(l,i-a);a+=l.size}return null},"getVal");for(let i of s){let o=r(e,i.eval),a=r(t,i.eval);if(this.compareValues(o,a)!==0)return!1}return!0}compareValues(e,t){let s=typeof e=="bigint"?Number(e):e,n=typeof t=="bigint"?Number(t):t;return s===null&&n===null?0:s===null?1:n===null||s<n?-1:s>n?1:0}}}});function Tw(...e){let t=new Map,s=0;for(let n of e)for(let r of n){let i=`${r.tableAlias}.${r.name}`.toUpperCase();t.set(i,s),t.has(r.name.toUpperCase())||t.set(r.name.toUpperCase(),s),s++}return t}c(Tw,"combinedMappingOf");function Ql(e,t,s){let n=[],r={consume:c(async o=>{n.push(o)},"consume"),finalize:c(async()=>{},"finalize")},i=e.createPipeline(r);return s.register(e,i,r),e.addDependency(t,i),n}c(Ql,"registerBufferedChild");var Xk=J({"src/execution/builders/builder-utils.ts"(){"use strict"}});async function M6(e,t){let s=await e.buildPipeline(t.children[0]),n=Dt(t.condition,s.columnMapping),r=e.parallelDispatch;return{schema:s.schema,columnMapping:s.columnMapping,register:c((i,o,a)=>{let l=new zk(t.condition,n,s.columnMapping,r),u={get cancelToken(){return a.cancelToken},async consume(f){if(this.cancelToken?.isCancelled)return;let h=await l.process(f);h&&h.size>0&&await a.consume(h)},async finalize(){a.finalize&&await a.finalize()}};s.register(i,o,u)},"register")}}c(M6,"buildFilter");async function L6(e,t){let s=await e.buildPipeline(t.children[0]),n=t.expressions.map(a=>Dt(a,s.columnMapping)),r=t.expressions.map(a=>{let l=a;return e.normalizeExecType(l.dataType||l.resultType||"VARCHAR")}),i=t.expressions.map((a,l)=>{let u=a;return{name:u.outputName||u.alias||u.name||u.columnName||`col${l}`,dataType:e.normalizeExecType(u.dataType||u.resultType||"VARCHAR"),tableAlias:""}}),o=e.buildSchemaMapping(i,"");return{schema:i,columnMapping:o,register:c((a,l,u)=>{let f=new Vk(t.expressions,n,r,s.columnMapping,e.parallelDispatch),h={get cancelToken(){return u.cancelToken},async consume(p){if(this.cancelToken?.isCancelled)return;let d=await f.process(p);await u.consume(d)},async finalize(){u.finalize&&await u.finalize()}};s.register(a,l,h)},"register")}}c(L6,"buildProject");async function P6(e,t){let s=await e.buildPipeline(t.children[0]),n=t.orderKeys.map(r=>({eval:Dt(r.expr,s.columnMapping),direction:r.direction||"ASC"}));return{schema:s.schema,columnMapping:s.columnMapping,register:c((r,i,o)=>{let a=e.tempManager.allocate("spill","sort"),l=new Qk(n,t.limit??null,t.offset||0,e.storageBackend.createSpillManager(a)),u={async consume(p){await l.consume(p)},async finalize(){}},f=r.createPipeline(u);s.register(r,f,u),r.addDependency(i,f);let h=c(async function*(){let p=await l.finalize();for(let d of p)await o.consume(d),yield d;o.finalize&&await o.finalize()},"source");r.setSource(i,h)},"register")}}c(P6,"buildSort");async function D6(e,t){let s=await e.buildPipeline(t.children[0]),n=t.orderKeys.map(r=>({eval:Dt(r.expr,s.columnMapping),direction:r.direction||"ASC"}));return{schema:s.schema,columnMapping:s.columnMapping,register:c((r,i,o)=>{let a=e.tempManager.allocate("spill","topn"),l=new Qk(n,t.count,t.offset||0,e.storageBackend.createSpillManager(a)),u={async consume(p){await l.consume(p)},async finalize(){}},f=r.createPipeline(u);s.register(r,f,u),r.addDependency(i,f);let h=c(async function*(){let p=await l.finalize();for(let d of p)await o.consume(d),yield d;o.finalize&&await o.finalize()},"source");r.setSource(i,h)},"register")}}c(D6,"buildTopN");async function F6(e,t){let s=await e.buildPipeline(t.children[0]),n=t.count,r=t.offset||0;return{schema:s.schema,columnMapping:s.columnMapping,register:c((i,o,a)=>{let l=new vP(n,r),u=new Kk,f={async consume(h){if(u.isCancelled)return;await l.consume(h);let p=await l.finalize();for(let d of p)d.size>0&&await a.consume(d);l.chunks=[],l.done&&u.cancel()},async finalize(){let h=await l.finalize();for(let p of h)p.size>0&&await a.consume(p);a.finalize&&await a.finalize()},cancelToken:u};s.register(i,o,f)},"register")}}c(F6,"buildLimit");async function $6(e,t){let s=await e.buildPipeline(t.children[0]);return{schema:s.schema,columnMapping:s.columnMapping,register:c((n,r,i)=>{let o=new kP,a={async consume(l){let u=await o.process(l);u&&u.size>0&&await i.consume(u)},async finalize(){i.finalize&&await i.finalize()}};s.register(n,r,a)},"register")}}c($6,"buildDistinct");async function z6(e,t){let s=await e.buildPipeline(t.children[0]),n=await e.buildPipeline(t.children[1]);return{schema:s.schema,columnMapping:s.columnMapping,register:c((r,i,o)=>{if(t.all){let a=r.createPipeline(o),l=r.createPipeline(o);s.register(r,a,o),n.register(r,l,o),r.addDependency(i,a),r.addDependency(i,l);let u=c(async function*(){},"source");r.setSource(i,u)}else{let a=new EP(!1),l={async consume(p){let d=await a.process(p);d&&d.size>0&&await o.consume(d)},async finalize(){}},u=r.createPipeline(l),f=r.createPipeline(l);s.register(r,u,l),n.register(r,f,l),r.addDependency(f,u),r.addDependency(i,f);let h=c(async function*(){o.finalize&&await o.finalize()},"source");r.setSource(i,h)}},"register")}}c(z6,"buildUnion");async function U6(e,t){let s=await e.buildPipeline(t.children[0]),n=t.windowExprs,r=[...s.schema,...n.map((a,l)=>({name:`__window_${l}`,dataType:e.normalizeExecType(a.resultType||"FLOAT64"),tableAlias:""}))],i=new Map,o=0;for(let a of r){let l=a.tableAlias?`${a.tableAlias}.${a.name}`.toUpperCase():a.name.toUpperCase();i.set(l,o),i.has(a.name.toUpperCase())||i.set(a.name.toUpperCase(),o),o++}for(let a=0;a<n.length;a++){let l=V6(n[a]);i.set(l,s.schema.length+a)}return{schema:r,columnMapping:i,register:c((a,l,u)=>{let f=Ql(a,l,s),h=c(async function*(){let d=await new NP(n,s.schema,s.columnMapping,Dt).execute(f);for(let m of d)await u.consume(m),yield m;u.finalize&&await u.finalize()},"source");a.setSource(l,h)},"register")}}c(U6,"buildWindow");function V6(e){let t=e.name?.toUpperCase()||"WIN",s=(e.args||[]).map(r=>r.kind==="BoundColumnRef"?`${r.tableAlias}.${r.columnName}`.toUpperCase():JSON.stringify(r).slice(0,30)).join(","),n=(e.partitionBy||[]).map(r=>r.kind==="BoundColumnRef"?`${r.tableAlias}.${r.columnName}`.toUpperCase():"").join(",");return`__WIN__${t}(${s})[${n}]`}c(V6,"windowExprKey2");var G6=J({"src/execution/builders/pipeline-builders.ts"(){"use strict";ad(),WL(),XL(),C6(),T6(),I6(),O6(),ud(),Vt(),Xk()}});function fk(e){return e?e.kind==="BoundBinary"&&e.op==="AND"?[...fk(e.left),...fk(e.right)]:[e]:[]}c(fk,"splitAnd");function Da(e,t){let s=`${t.tableAlias||""}.${t.columnName}`.toUpperCase();if(e.has(s))return!0;let n=t.columnName.toUpperCase();return e.has(n)}c(Da,"hasColumn");function jp(e,t,s){if(!e)return null;if(e.kind==="BoundBinary"&&e.op==="OR"){let n=jp(e.left,t,s),r=jp(e.right,t,s);return!n||!r?null:n.buildKey.tableAlias===r.buildKey.tableAlias&&n.buildKey.columnName===r.buildKey.columnName&&n.probeKey.tableAlias===r.probeKey.tableAlias&&n.probeKey.columnName===r.probeKey.columnName?n:null}if(e.kind==="BoundBinary"&&e.op==="AND"){let n=jp(e.left,t,s);return n||jp(e.right,t,s)}if(e.kind==="BoundBinary"&&e.op==="="&&e.left?.kind==="BoundColumnRef"&&e.right?.kind==="BoundColumnRef"){if(Da(t,e.left)&&Da(s,e.right))return{buildKey:e.left,probeKey:e.right};if(Da(t,e.right)&&Da(s,e.left))return{buildKey:e.right,probeKey:e.left}}return null}c(jp,"findCommonEquiJoinKeys");function q6(e,t,s){if(!e)return{buildKeys:[],probeKeys:[],residualCondition:null};let n=[],r=[],i=[],o=fk(e);for(let l of o){let u=!1;l.kind==="BoundBinary"&&l.op==="="&&l.left?.kind==="BoundColumnRef"&&l.right?.kind==="BoundColumnRef"&&(Da(t,l.left)&&Da(s,l.right)?(n.push(l.left),r.push(l.right),u=!0):Da(t,l.right)&&Da(s,l.left)&&(n.push(l.right),r.push(l.left),u=!0)),u||i.push(l)}if(n.length===0){let l=jp(e,t,s);l&&l.buildKey?(n.push(l.buildKey),r.push(l.probeKey),i.length=0,i.push(e)):(n.push({kind:"BoundLiteral",value:1,dataType:null}),r.push({kind:"BoundLiteral",value:1,dataType:null}))}let a=null;return i.length>0&&(a=i.reduce((l,u)=>({kind:"BoundBinary",op:"AND",left:l,right:u,resultType:"BOOLEAN"}))),{buildKeys:n,probeKeys:r,residualCondition:a}}c(q6,"extractJoinKeys");var j6=J({"src/execution/join-utils.ts"(){"use strict";Vt()}});function AM(e,t,s){if(e.length===1){let r=e[0](t,s);return r==null?null:typeof r=="bigint"?Number(r):r}let n=new Array(e.length);for(let r=0;r<e.length;r++){let i=e[r](t,s);if(i==null)return null;n[r]=typeof i=="bigint"?Number(i):i}return n.join("|")}c(AM,"joinKeyOf");function W6(e){let t=new Array(e),s={row:null,columns:t,setRow(n){this.row=n}};for(let n=0;n<e;n++)t[n]={get:c(()=>s.row[n],"get")};return s}c(W6,"createCombinedRowAdapter");function RP(e,t,s){let{joinType:n,buildColCount:r,probeColCount:i,conditionEvaluator:o,hasNullKey:a,onMatched:l}=s,u=o?W6(r+i):null,f=[];for(let h of e){let{row:p,key:d}=h;if(d===null){(n==="LEFT"||n==="FULL"||n==="ANTI"||n==="SINGLE")&&f.push(new Array(r).fill(null).concat(p));continue}let m=t(d),g=!1;if(m)for(let y of m){let _=y.row;if(u){let w=_.concat(p);if(u.setRow(w),!o(u,0))continue}if(g=!0,l&&l(y),n==="SEMI")break;if(n==="ANTI")break;if(n==="SINGLE"){f.push(_.concat(p));break}else{if(n==="MARK")break;f.push(_.concat(p))}}g?n==="SEMI"?f.push(p):n==="MARK"&&f.push(p.concat([!0])):n==="LEFT"||n==="FULL"||n==="SINGLE"?f.push(new Array(r).fill(null).concat(p)):n==="ANTI"?f.push(p):n==="MARK"&&f.push(p.concat([a?null:!1]))}return f}c(RP,"probeJoinRows");function K6(e){return e==="LEFT"||e==="FULL"||e==="SINGLE"||e==="ANTI"||e==="MARK"}c(K6,"emitsOnUnmatchedProbe");function hk(e,{joinType:t,buildColCount:s,buildSchema:n,probeSchema:r},i=$a){if(e.length===0)return new kt([],0);let o=t==="SEMI"||t==="ANTI",a=t==="MARK",l=e[0].length,u=[];for(let f=0;f<l;f++){let h=e.find(g=>g[f]!==null)?.[f],p="VARCHAR";h!==void 0&&(p=typeof h=="bigint"?"DECIMAL":typeof h=="number"?Number.isInteger(h)?"INT32":"FLOAT64":typeof h=="boolean"?"BOOLEAN":"VARCHAR");let d=p;o?d=r?.[f]||p:a&&f===l-1?d="BOOLEAN":f<s?d=n?.[f]||p:d=r?.[f-s]||p;let m=new Qt(d,e.length,i);for(let g=0;g<e.length;g++)m.set(g,e[g][f]);m.length=e.length,u.push(m)}return new kt(u,e.length)}c(hk,"buildJoinOutputChunk");var BP=J({"src/execution/operators/join-core.ts"(){"use strict";Ts(),be(),Ft(),tP(),tc()}});function H6(e){let t=0;for(let s=0;s<e.length;s++)t=(t<<5)-t+e.charCodeAt(s),t|=0;return Math.abs(t)}c(H6,"hashString");function vM(e){return H6(String(e))%ht.hashJoinPartitions}c(vM,"getPartition");var CP,TP,J6=J({"src/execution/operators/hash-join.ts"(){"use strict";Ts(),be(),Ft(),Fe(),BP(),CP=class{static{c(this,"HashJoinBuild")}keyExtractors;joinType;uniqueKeys;buildPreserved;hashTable;buildSchema;hasNullKey;nullKeyRows;spillManager;partitions;totalRowsInRAM;matchedSet;constructor(e,t,s,n,r=!1){this.keyExtractors=e,this.joinType=t||"INNER",this.uniqueKeys=!!s,this.buildPreserved=!!r,this.hashTable=new Map,this.buildSchema=null,this.hasNullKey=!1,this.nullKeyRows=[],this.spillManager=n,this.partitions=Array.from({length:ht.hashJoinPartitions},()=>({rows:[],spilled:!1})),this.totalRowsInRAM=0,this.matchedSet=new Set}async init(){}async consume(e){this.buildSchema||(this.buildSchema=e.columns.map(n=>n.dataType));let t=e.selectionVector?e.flatten():e,s=new Array(t.size);for(let n=0;n<t.size;n++){let r=new Array(t.columns.length);for(let i=0;i<t.columns.length;i++)r[i]=t.columns[i].get(n);s[n]=r}for(let n=0;n<t.size;n++){let r=this.buildKey(t,n);if(r===null){this.hasNullKey=!0,this.buildPreserved&&this.nullKeyRows.push(s[n]);continue}let i=vM(r),o=this.partitions[i];o.rows.push({row:s[n],key:r}),o.spilled||this.totalRowsInRAM++,o.spilled&&o.rows.length>=ht.flushBatchSize&&await this.flushPartition(i)}if(this.totalRowsInRAM>ht.memoryLimit){let n=-1,r=0;for(let i=0;i<ht.hashJoinPartitions;i++)!this.partitions[i].spilled&&this.partitions[i].rows.length>r&&(r=this.partitions[i].rows.length,n=i);n!==-1&&(this.partitions[n].spilled=!0,this.totalRowsInRAM-=this.partitions[n].rows.length,await this.flushPartition(n))}}async flushPartition(e){let t=this.partitions[e];if(t.rows.length===0)return;let s=this.rowsToChunk(t.rows.map(n=>n.row));await this.spillManager.appendChunk(`build_${e}`,s),t.rows=[]}rowsToChunk(e){if(e.length===0)return new kt([],0);let t=e[0].length,s=new Array(t);for(let n=0;n<t;n++){let r=new Qt(this.buildSchema?.[n]||"VARCHAR",e.length);for(let i=0;i<e.length;i++)r.set(i,e[i][n]);r.length=e.length,s[n]=r}return new kt(s,e.length)}async finalize(){for(let e=0;e<ht.hashJoinPartitions;e++){let t=this.partitions[e];if(t.spilled&&t.rows.length>0&&await this.flushPartition(e),!t.spilled)for(let s=0;s<t.rows.length;s++){let n=t.rows[s],r=this.hashTable.get(n.key);this.uniqueKeys&&r||(r||(r=[],this.hashTable.set(n.key,r)),r.push({row:n.row,pIdx:e,rIdx:s}))}}}markMatched(e){this.matchedSet.add(`${e.pIdx}_${e.rIdx}`)}emitUnmatched(e){let t=[];for(let s=0;s<ht.hashJoinPartitions;s++){let n=this.partitions[s];if(!n.spilled){for(let r=0;r<n.rows.length;r++)if(!this.matchedSet.has(`${s}_${r}`)){let i=[...n.rows[r].row];for(let o=0;o<e;o++)i.push(null);t.push(i)}}}for(let s=0;s<this.nullKeyRows.length;s++){let n=[...this.nullKeyRows[s]];for(let r=0;r<e;r++)n.push(null);t.push(n)}return t}probe(e){return this.hashTable.get(e)||null}buildKey(e,t){return AM(this.keyExtractors,e,t)}},TP=class{static{c(this,"HashJoinProbe")}buildSide;probeKeyExtractors;buildColCount;probeColCount;joinType;conditionEvaluator;spillBuffers;probeSchema;constructor(e,t,s,n,r="INNER",i=null){this.buildSide=e,this.probeKeyExtractors=t,this.buildColCount=s,this.probeColCount=n,this.joinType=r,this.conditionEvaluator=i,this.spillBuffers=Array.from({length:ht.hashJoinPartitions},()=>[]),this.probeSchema=null}async init(){}async process(e){this.probeSchema||(this.probeSchema=e.columns.map(n=>n.dataType));let t=e.selectionVector?e.flatten():e,s=[];for(let n=0;n<t.size;n++){let r=this.extractProbeKey(t,n),i=new Array(t.columns.length);for(let a=0;a<t.columns.length;a++)i[a]=t.columns[a].get(n);if(r===null){s.push({row:i,key:null});continue}let o=vM(r);this.buildSide.partitions[o].spilled?(this.spillBuffers[o].push({row:i,key:r}),this.spillBuffers[o].length>=ht.flushBatchSize&&await this.flushProbePartition(o)):s.push({row:i,key:r})}return s.length>0?this.executeInMemoryJoin(s):null}async flushProbePartition(e){let t=this.spillBuffers[e];if(t.length===0)return;let s=this.rowsToProbeChunk(t.map(n=>n.row));await this.buildSide.spillManager.appendChunk(`probe_${e}`,s),this.spillBuffers[e]=[]}rowsToProbeChunk(e){if(e.length===0)return new kt([],0);let t=e[0].length,s=new Array(t);for(let n=0;n<t;n++){let r=new Qt(this.probeSchema?.[n]||"VARCHAR",e.length);for(let i=0;i<e.length;i++)r.set(i,e[i][n]);r.length=e.length,s[n]=r}return new kt(s,e.length)}executeInMemoryJoin(e){let t=RP(e,s=>this.buildSide.probe(s),{joinType:this.joinType,buildColCount:this.buildColCount,probeColCount:this.probeColCount,conditionEvaluator:this.conditionEvaluator,hasNullKey:this.buildSide.hasNullKey,onMatched:c(s=>this.buildSide.markMatched(s),"onMatched")});return this.buildOutputChunk(t)}async finalize(e){for(let t=0;t<ht.hashJoinPartitions;t++)this.spillBuffers[t].length>0&&await this.flushProbePartition(t);for(let t=0;t<ht.hashJoinPartitions;t++){let s=this.buildSide.partitions[t];if(!s.spilled)continue;this.buildSide.hashTable.clear(),s.rows=[];let n=this.buildSide.spillManager.readChunks(`build_${t}`);for await(let i of n)for(let o=0;o<i.size;o++){let a=this.buildSide.buildKey(i,o),l=new Array(i.columns.length);for(let h=0;h<i.columns.length;h++)l[h]=i.columns[h].get(o);let u=s.rows.length;s.rows.push({row:l,key:a});let f=this.buildSide.hashTable.get(a);this.buildSide.uniqueKeys&&f||(f||(f=[],this.buildSide.hashTable.set(a,f)),f.push({row:l,pIdx:t,rIdx:u}))}let r=this.buildSide.spillManager.readChunks(`probe_${t}`);for await(let i of r){let o=[];for(let l=0;l<i.size;l++){let u=this.extractProbeKey(i,l),f=new Array(i.columns.length);for(let h=0;h<i.columns.length;h++)f[h]=i.columns[h].get(l);o.push({row:f,key:u})}let a=this.executeInMemoryJoin(o);a&&a.size>0&&await e.consume(a)}}await this.buildSide.spillManager.clearAll()}extractProbeKey(e,t){return AM(this.probeKeyExtractors,e,t)}buildOutputChunk(e){return hk(e,{joinType:this.joinType,buildColCount:this.buildColCount,buildSchema:this.buildSide.buildSchema??void 0,probeSchema:this.probeSchema??void 0})}}}});function nw(e){return e==null?!0:Array.isArray(e)&&e.some(t=>t==null)}c(nw,"isNullKey");var IP,Y6=J({"src/execution/operators/merge-join.ts"(){"use strict";be(),Ts(),Ft(),we(),IP=class{static{c(this,"MergeJoinOperator")}buildChunks;probeChunks;buildKeyExtractors;probeKeyExtractors;buildColCount;probeColCount;joinType;conditionEvaluator;constructor(e,t,s,n,r,i,o="INNER",a=null){this.buildChunks=e,this.probeChunks=t,this.buildKeyExtractors=s,this.probeKeyExtractors=n,this.buildColCount=r,this.probeColCount=i,this.joinType=o,this.conditionEvaluator=a}async execute(){let e=this.joinType==="SEMI"||this.joinType==="ANTI",t=this.joinType==="MARK",s=this._flattenAndExtractKeys(this.buildChunks,this.buildKeyExtractors),n=this._flattenAndExtractKeys(this.probeChunks,this.probeKeyExtractors),r=s.filter(g=>!nw(g.key)),i=n.filter(g=>!nw(g.key)),o=s.filter(g=>nw(g.key)),a=n.filter(g=>nw(g.key)),l=o.length>0?null:!1;r.sort((g,y)=>this._compareKeys(g.key,y.key)),i.sort((g,y)=>this._compareKeys(g.key,y.key));let u=[],f=this.conditionEvaluator?this.createAdapter():null,h=this.conditionEvaluator,p=0,d=0;for(;p<r.length&&d<i.length;){let g=r[p],y=i[d],_=this._compareKeys(g.key,y.key);if(_<0)(this.joinType==="LEFT"||this.joinType==="FULL")&&u.push(this._combineRowWithNulls(g,!0)),p++;else if(_>0)e&&this.joinType==="ANTI"?u.push(this._extractProbeRow(y)):t?u.push(this._extractProbeRow(y).concat([l])):(this.joinType==="RIGHT"||this.joinType==="FULL")&&u.push(this._combineRowWithNulls(y,!1)),d++;else{let w=p;for(;w<r.length&&this._compareKeys(g.key,r[w].key)===0;)w++;let b=d;for(;b<i.length&&this._compareKeys(y.key,i[b].key)===0;)b++;if(e||t)for(let x=d;x<b;x++){let A=!1;for(let S=p;S<w;S++){if(f){let E=this._combineRow(r[S],i[x]);if(f.setRow(E),!h(f,0))continue}A=!0;break}this.joinType==="SEMI"&&A?u.push(this._extractProbeRow(i[x])):this.joinType==="ANTI"&&!A?u.push(this._extractProbeRow(i[x])):t&&u.push(this._extractProbeRow(i[x]).concat([A?!0:l]))}else for(let x=p;x<w;x++){let A=!1;for(let S=d;S<b;S++){let E=this._combineRow(r[x],i[S]);f&&(f.setRow(E),!h(f,0))||(u.push(E),A=!0)}!A&&(this.joinType==="LEFT"||this.joinType==="FULL")&&u.push(this._combineRowWithNulls(r[x],!0))}p=w,d=b}}for(;p<r.length;)(this.joinType==="LEFT"||this.joinType==="FULL")&&u.push(this._combineRowWithNulls(r[p],!0)),p++;for(;d<i.length;)e&&this.joinType==="ANTI"?u.push(this._extractProbeRow(i[d])):t?u.push(this._extractProbeRow(i[d]).concat([l])):(this.joinType==="RIGHT"||this.joinType==="FULL")&&u.push(this._combineRowWithNulls(i[d],!1)),d++;for(let g of o)(this.joinType==="LEFT"||this.joinType==="FULL")&&u.push(this._combineRowWithNulls(g,!0));for(let g of a)e&&this.joinType==="ANTI"?u.push(this._extractProbeRow(g)):t?u.push(this._extractProbeRow(g).concat([null])):(this.joinType==="RIGHT"||this.joinType==="FULL")&&u.push(this._combineRowWithNulls(g,!1));if(u.length===0)return[];let m=this._buildOutputChunk(u);return m?[m]:[]}_flattenAndExtractKeys(e,t){let s=[];for(let n of e)for(let r=0;r<n.size;r++){let i=n.activeRowIndex(r),o=t.length===1?t[0](n,i):t.map(a=>a(n,i));s.push({chunk:n,idx:i,key:o})}return s}_compareKeys(e,t){if(Array.isArray(e)){let r=e,i=t;for(let o=0;o<r.length;o++){let a=typeof r[o]=="bigint"?Number(r[o]):r[o],l=typeof i[o]=="bigint"?Number(i[o]):i[o];if(a<l)return-1;if(a>l)return 1}return 0}let s=typeof e=="bigint"?Number(e):e,n=typeof t=="bigint"?Number(t):t;return s<n?-1:s>n?1:0}_combineRow(e,t){let s=[];for(let n=0;n<e.chunk.columns.length;n++)s.push(e.chunk.columns[n].get(e.idx));for(let n=0;n<t.chunk.columns.length;n++)s.push(t.chunk.columns[n].get(t.idx));return s}_extractProbeRow(e){let t=[];for(let s=0;s<e.chunk.columns.length;s++)t.push(e.chunk.columns[s].get(e.idx));return t}_combineRowWithNulls(e,t){let s=[];if(t){for(let n=0;n<e.chunk.columns.length;n++)s.push(e.chunk.columns[n].get(e.idx));for(let n=0;n<this.probeColCount;n++)s.push(null)}else{for(let n=0;n<this.buildColCount;n++)s.push(null);for(let n=0;n<e.chunk.columns.length;n++)s.push(e.chunk.columns[n].get(e.idx))}return s}_buildOutputChunk(e){if(e.length===0)return null;let t=this.joinType==="SEMI"||this.joinType==="ANTI",s=this.joinType==="MARK",n=t?this.probeColCount:s?this.probeColCount+1:this.buildColCount+this.probeColCount,r=[];for(let i=0;i<n;i++){let o="VARCHAR";t?this.probeChunks.length>0&&i<this.probeChunks[0].columns.length&&(o=this.probeChunks[0].columns[i].dataType):s?i<this.probeColCount&&this.probeChunks.length>0&&i<this.probeChunks[0].columns.length?o=this.probeChunks[0].columns[i].dataType:i===this.probeColCount&&(o="BOOLEAN"):i<this.buildColCount&&this.buildChunks.length>0?o=this.buildChunks[0].columns[i].dataType:i>=this.buildColCount&&this.probeChunks.length>0&&(o=this.probeChunks[0].columns[i-this.buildColCount].dataType),r.push(new Qt(o,e.length))}for(let i=0;i<e.length;i++){let o=e[i];for(let a=0;a<n;a++)r[a].set(i,o[a])}for(let i of r)i.length=e.length;return new kt(r,e.length)}createAdapter(){let e=this.buildColCount+this.probeColCount,t=new Array(e),s={row:null,columns:t,setRow(n){this.row=n}};for(let n=0;n<e;n++)t[n]={get:c(()=>s.row[n],"get")};return s}}}}),OP,Q6=J({"src/execution/operators/nested-loop-join.ts"(){"use strict";be(),Ts(),Ft(),OP=class{static{c(this,"NestedLoopJoinOperator")}outerChunks;innerChunks;outerColCount;innerColCount;joinType;conditionEvaluator;constructor(e,t,s,n,r="INNER",i=null){this.outerChunks=e,this.innerChunks=t,this.outerColCount=s,this.innerColCount=n,this.joinType=r,this.conditionEvaluator=i}async execute(){let e=this._flattenChunks(this.outerChunks),t=this._flattenChunks(this.innerChunks),s=[],n=this.conditionEvaluator?this._createAdapter():null,r=this.joinType==="FULL"||this.joinType==="RIGHT"?new Uint8Array(t.length):null;for(let i=0;i<e.length;i++){let o=e[i],a=!1;for(let l=0;l<t.length;l++){let u=t[l],f=this._combineRow(o,u);if(!(n&&(n.setRow(f),!this.conditionEvaluator(n.chunk,0)))){if(a=!0,r&&(r[l]=1),this.joinType==="SEMI"){s.push(this._extractOuter(o));break}if(this.joinType==="ANTI"||this.joinType==="MARK")break;s.push(f)}}a?this.joinType==="MARK"&&s.push(this._outerWithMark(o,!0)):this.joinType==="LEFT"||this.joinType==="FULL"?s.push(this._outerWithNulls(o)):this.joinType==="ANTI"?s.push(this._extractOuter(o)):this.joinType==="MARK"&&s.push(this._outerWithMark(o,!1))}if(r)for(let i=0;i<t.length;i++)r[i]||s.push(this._innerWithNulls(t[i]));return s.length===0?[]:[this._buildOutputChunk(s)]}_flattenChunks(e){let t=[];for(let s of e)for(let n=0;n<s.size;n++){let r=s.activeRowIndex(n),i=new Array(s.columns.length);for(let o=0;o<s.columns.length;o++)i[o]=s.columns[o].get(r);t.push(i)}return t}_combineRow(e,t){let s=new Array(this.outerColCount+this.innerColCount);for(let n=0;n<this.outerColCount;n++)s[n]=e[n];for(let n=0;n<this.innerColCount;n++)s[this.outerColCount+n]=t[n];return s}_extractOuter(e){return e.slice(0,this.outerColCount)}_outerWithNulls(e){let t=new Array(this.outerColCount+this.innerColCount);for(let s=0;s<this.outerColCount;s++)t[s]=e[s];for(let s=0;s<this.innerColCount;s++)t[this.outerColCount+s]=null;return t}_innerWithNulls(e){let t=new Array(this.outerColCount+this.innerColCount);for(let s=0;s<this.outerColCount;s++)t[s]=null;for(let s=0;s<this.innerColCount;s++)t[this.outerColCount+s]=e[s];return t}_outerWithMark(e,t){let s=new Array(this.outerColCount+1);for(let n=0;n<this.outerColCount;n++)s[n]=e[n];return s[this.outerColCount]=t,s}_buildOutputChunk(e){let t=e[0].length,s=new Array(t);for(let n=0;n<t;n++){let r="VARCHAR";if(this.joinType==="MARK"&&n===t-1)r="BOOLEAN";else if(this.joinType==="SEMI"||this.joinType==="ANTI")n<this.outerColCount&&this.outerChunks.length>0&&this.outerChunks[0].columns[n]&&(r=this.outerChunks[0].columns[n].dataType);else if(n<this.outerColCount&&this.outerChunks.length>0&&this.outerChunks[0].columns[n])r=this.outerChunks[0].columns[n].dataType;else if(n>=this.outerColCount&&this.innerChunks.length>0){let o=n-this.outerColCount;this.innerChunks[0].columns[o]&&(r=this.innerChunks[0].columns[o].dataType)}let i=new Qt(r,e.length);for(let o=0;o<e.length;o++)i.set(o,e[o][n]);i.length=e.length,s[n]=i}return new kt(s,e.length)}_createAdapter(){let e=this.outerColCount+this.innerColCount,t=new Array(e),s=null;for(let r=0;r<e;r++){let i=r;t[r]={get:c(()=>s[i],"get")}}return{chunk:{columns:t},setRow(r){s=r}}}}}});async function X6(e,t){let s=await e.buildPipeline(t.children[0]),n=await e.buildPipeline(t.children[1]),r=t.joinType==="SEMI"||t.joinType==="ANTI",i=t.joinType==="MARK",o,a,l,u;r||i||t._buildSide==="right"?(o=n,a=s,l=t.children[1],u=t.children[0]):(o=s,a=n,l=t.children[0],u=t.children[1]);let f=[...o.schema,...a.schema],h=Tw(o.schema,a.schema),{buildKeys:p,probeKeys:d,residualCondition:m}=q6(t.condition,o.columnMapping,a.columnMapping),g=t.joinType==="FULL"||t.joinType==="LEFT"&&l===t.children[0],y=m?Dt(m,h):null,_=i?[...s.schema,{name:t.markColumn||"__mark",dataType:"BOOLEAN",tableAlias:""}]:null,w=r?s.schema:i?_:f,b=r?s.columnMapping:i?e.buildSchemaMapping(_,""):h;if(t.physicalStrategy==="MERGE")return Z6(t,{left:s,right:n,buildInput:o,probeInput:a,buildKeys:p,probeKeys:d,conditionEvaluator:y,resultSchema:w,resultMapping:b});if(t.physicalStrategy==="NESTED_LOOP")return tq(t,{left:s,right:n,buildInput:o,probeInput:a,buildKeys:p,probeKeys:d,conditionEvaluator:y,resultSchema:w,resultMapping:b,isSemiAnti:r,isMark:i,markSchema:_});let x=e.tempManager.allocate("spill","join"),A=c(()=>new CP(p.map(v=>Dt(v,o.columnMapping)),t.joinType,!!t._dedupeBuild&&!y,e.storageBackend.createSpillManager(x),g),"makeBuildSide"),S=c(v=>new TP(v,d.map(k=>Dt(k,a.columnMapping)),o.schema.length,a.schema.length,t.joinType,y),"makeProbeOp"),E={schema:w,columnMapping:b,register:c((v,k,B)=>{let N=A(),C={async consume(z){await N.consume(z)},async finalize(){await N.finalize()}},O=v.createPipeline(C);o.register(v,O,C);let F=S(N),P={get cancelToken(){return B.cancelToken},async consume(z){if(this.cancelToken?.isCancelled)return;let q=await F.process(z);q&&q.size>0&&await B.consume(q)},async finalize(){if(g){let z=N.emitUnmatched(a.schema.length);z.length>0&&await B.consume(F.buildOutputChunk(z))}F.finalize&&await F.finalize(B),B.finalize&&await B.finalize()}};v.addDependency(k,O),a.register(v,k,P)},"register")},R=e._prepareParallelJoin(t,o,a,l,u,p,d,m,h);return R?{schema:w,columnMapping:b,register:c((v,k,B)=>{let N=c((F,P)=>F.storage?null:Ql(v,k,P),"registerSide"),C=N(R.buildSide,o),O=N(R.probeSide,a);v.setSource(k,async function*(){let F=c(async(K,_t)=>{if(!K.storage)return _t;let dt=[];for await(let jt of K.storage.scan())dt.push(jt);return dt},"collect"),P=await F(R.buildSide,C),z=await F(R.probeSide,O),q=c(K=>K.reduce((_t,dt)=>_t+dt.size,0),"countRows"),U=q(P),D=q(z),G=U+D>=ht.parallelJoinThreshold&&U<=ht.memoryLimit,At=c(async function*(){let _t=C!==null&&O!==null?await e._runBufferedSerialJoin(A,S,P,z,t,a.schema.length):await e._executeSubPipeline(E);for(let dt of _t)!dt||dt.size===0||(await B.consume(dt),yield dt)},"emitSerial");if(!G){yield*At(),B.finalize&&await B.finalize();return}let pt=c((K,_t)=>{if(K.storage)return K.spec.schema.map(jt=>jt.dataType);let dt=_t.find(jt=>jt.size>0);return dt?dt.columns.map(jt=>jt.dataType):K.spec.schema.map(jt=>jt.dataType)},"typesOf"),mt={build:pt(R.buildSide,P),probe:pt(R.probeSide,z)},$=!1;try{let K=e.fragmentPool.runJoinStream(R.spec,{chunks:P,columnIndexes:R.buildSide.columnIndexes},{chunks:z,columnIndexes:R.probeSide.columnIndexes},mt);for await(let _t of K)_t.size!==0&&(await B.consume(_t),yield _t,$=!0)}catch(K){if($)throw K;yield*At()}B.finalize&&await B.finalize()})},"register")}:E}c(X6,"buildJoin");function Z6(e,t){let{left:s,right:n,buildInput:r,probeInput:i,conditionEvaluator:o}=t,a=r,l=i,u=t.buildKeys,f=t.probeKeys,h=t.resultSchema,p=t.resultMapping;return e.joinType==="LEFT"&&r!==s?(a=s,l=n,u=t.probeKeys,f=t.buildKeys,h=[...s.schema,...n.schema],p=Tw(s.schema,n.schema)):e.joinType==="RIGHT"&&r!==n&&(a=n,l=s,u=t.probeKeys,f=t.buildKeys,h=[...n.schema,...s.schema],p=Tw(n.schema,s.schema)),{schema:h,columnMapping:p,register:c((d,m,g)=>{let y=Ql(d,m,s),_=Ql(d,m,n);d.setSource(m,async function*(){let b=await new IP(a===s?y:_,l===s?y:_,u.map(x=>Dt(x,a.columnMapping)),f.map(x=>Dt(x,l.columnMapping)),a.schema.length,l.schema.length,e.joinType,o).execute();for(let x of b)await g.consume(x),yield x;g.finalize&&await g.finalize()})},"register")}}c(Z6,"buildMergeJoin");function tq(e,t){let{left:s,right:n,buildInput:r,probeInput:i,isSemiAnti:o,isMark:a,markSchema:l}=t,u=r===s?r:i,f=r===s?i:r,h=Tw(u.schema,f.schema),p=e.condition?Dt(e.condition,h):null,d=[...u.schema,...f.schema],m=o?s.columnMapping:h;return{schema:o?s.schema:a?l:d,columnMapping:m,register:c((y,_,w)=>{let b=Ql(y,_,s),x=Ql(y,_,n);y.setSource(_,async function*(){let S=await new OP(u===s?b:x,f===s?b:x,u.schema.length,f.schema.length,e.joinType,p).execute();for(let E of S)await w.consume(E),yield E;w.finalize&&await w.finalize()})},"register")}}c(tq,"buildNestedLoopJoin");function eq(e,t,s,n,r,i,o,a,l,u){if(!e.fragmentPool||o.length===0||t.joinType==="CROSS"||e._estimatePlanRows(t)<ht.parallelJoinThreshold)return null;let f=kM(e,r,s),h=kM(e,i,n),p=t.joinType==="FULL"||t.joinType==="LEFT"&&r===t.children[0],d=g6({build:f.spec,probe:h.spec,buildKeys:o,probeKeys:a,residualCondition:l,joinType:t.joinType,buildPreserved:p,uniqueKeys:!!t._dedupeBuild&&!l,buildMapping:s.columnMapping,probeMapping:n.columnMapping,combinedMapping:u});return d?{spec:d,buildSide:f,probeSide:h}:null}c(eq,"prepareParallelJoin");function kM(e,t,s){let n=d6(s.schema),r={spec:{schema:n,baseSchema:n,stages:[]},storage:null,columnIndexes:null},i=t?fP(t):null;if(!i)return r;let o=e.catalog.getTableStorage(i.table);if(!o||typeof o.scan!="function")return r;let a=o.getSchema(),u=e.resolveProjectedColumnIndexes(a,i.scanColumns)||a.map((h,p)=>p),f=u.map(h=>({name:a[h].name,dataType:a[h].dataType,tableAlias:i.alias}));return m6(Cw(f,i.stages),n)?{spec:{schema:n,baseSchema:f,stages:i.stages},storage:o,columnIndexes:u}:r}c(kM,"prepareJoinSide");async function sq(e,t,s,n,r,i){let o=e();for(let u of s)await o.consume(u);await o.finalize();let a=t(o),l=[];for(let u of n){let f=await a.process(u);f&&f.size>0&&l.push(f)}if(r.joinType==="LEFT"||r.joinType==="FULL"){let u=o.emitUnmatched(i);u.length>0&&l.push(a.buildOutputChunk(u))}return a.finalize&&await a.finalize({consume:c(async u=>{l.push(u)},"consume")}),l}c(sq,"runBufferedSerialJoin");var nq=J({"src/execution/builders/join-builder.ts"(){"use strict";Ft(),ad(),j6(),J6(),Y6(),Q6(),jw(),Fe(),Xk(),we()}}),MP,rq=J({"src/execution/operators/stream-aggregate.ts"(){"use strict";Ts(),be(),we(),ld(),YL(),MP=class{static{c(this,"StreamAggregateOperator")}groupByExtractors;groupByTypes;aggregateDefs;hasCachedValues;constructor(e,t,s){this.groupByExtractors=e,this.groupByTypes=t,this.aggregateDefs=s,this.hasCachedValues=s.some(n=>n.valueKey)}async init(){}_resolveWasmAggKernel(e){let t=e.name?.toUpperCase();if(!t)return null;if(t==="SUM"&&e.resultType==="FLOAT64"){if(ct.has("sumF64","FLOAT64"))return{kernelKey:"sumF64",dataType:"FLOAT64",kind:"SUM"};if(ct.has("sumI32","INT32"))return{kernelKey:"sumI32",dataType:"INT32",kind:"SUM"}}if(t==="MIN"){if(e.resultType==="FLOAT64"&&ct.has("minF64","FLOAT64"))return{kernelKey:"minF64",dataType:"FLOAT64",kind:"MIN"};if(e.resultType==="INT32"&&ct.has("minI32","INT32"))return{kernelKey:"minI32",dataType:"INT32",kind:"MIN"}}if(t==="MAX"){if(e.resultType==="FLOAT64"&&ct.has("maxF64","FLOAT64"))return{kernelKey:"maxF64",dataType:"FLOAT64",kind:"MAX"};if(e.resultType==="INT32"&&ct.has("maxI32","INT32"))return{kernelKey:"maxI32",dataType:"INT32",kind:"MAX"}}return t==="COUNT"?{kernelKey:"countBits",dataType:"UINT8",kind:"COUNT"}:t==="COUNT_STAR"?{kernelKey:null,dataType:null,kind:"COUNT_STAR"}:t==="AVG"&&e.resultType==="FLOAT64"&&ct.has("sumF64","FLOAT64")?{kernelKey:"sumF64",dataType:"FLOAT64",kind:"AVG"}:null}async _tryWasmUngrouped(e){if(this.groupByExtractors.length!==0||!ct||ct.kernels.size===0)return null;let t=this.aggregateDefs.map(r=>r.createAccumulator());for(let r of e){if(r.selectionVector||r.size===0)return null;for(let i=0;i<this.aggregateDefs.length;i++){let o=this.aggregateDefs[i],a=this._resolveWasmAggKernel(o);if(!a)return null;let l=t[i];if(a.kind==="COUNT_STAR"){l.count+=r.size;continue}if(a.kind==="COUNT"){if(!o._wasmColIndex&&o._wasmColIndex!==0)return null;let h=r.columns[o._wasmColIndex];if(!h)return null;if(!h.hasNulls)l.count+=r.size;else{let p=ct.lookup("countBits","UINT8");if(!p)return null;let d=await p(h.nullBitmap,r.size);l.count+=d}continue}if(a.kind==="AVG"){if(!o._wasmColIndex&&o._wasmColIndex!==0)return null;let h=r.columns[o._wasmColIndex];if(!h||!h.data||h.dataType!=="FLOAT64")return null;let p=h.data.subarray(0,r.size),d=ct.lookup(a.kernelKey,a.dataType);if(!d)return null;let m=await d(p),g;if(!h.hasNulls)g=r.size;else{let y=ct.lookup("countBits","UINT8");if(!y)return null;g=await y(h.nullBitmap,r.size)}l.sum+=m,l.count+=g;continue}let u=null;if(o._wasmColIndex!==void 0&&o._wasmColIndex!==null){let h=r.columns[o._wasmColIndex];if(h&&h.data){let p=h.dataType;(a.dataType==="FLOAT64"&&p==="FLOAT64"||a.dataType==="INT32"&&(p==="INT32"||p==="DATE"))&&(u=h.data.subarray(0,r.size))}}if(!u&&o._sourceExpr&&td(o._sourceExpr)){let h=await ed(o._sourceExpr,r,o._columnMapping,r.size);h instanceof Float64Array&&(u=h)}if(!u)return null;let f=ct.lookup(a.kernelKey,a.dataType);if(!f)return null;l.add(await f(u))}}let s=t.map(r=>r.result()),n=[];for(let r=0;r<this.aggregateDefs.length;r++){let i=new Qt(this.aggregateDefs[r].resultType,1);i.set(0,typeof s[r]=="bigint"?Number(s[r]):s[r]),i.length=1,n.push(i)}return[new kt(n,1)]}async execute(e){let t=await this._tryWasmUngrouped(e);if(t!==null)return t;let s=[],n=null,r=null,i=null;for(let l of e)for(let u=0;u<l.size;u++){let f=l.activeRowIndex(u),h=this.extractGroupKey(l,f);if(n!==h){if(i!==null){let p=[...r];for(let d=0;d<i.length;d++)p.push(i[d].result());s.push(p)}n=h,r=this.groupByExtractors.map(p=>p(l,f)),i=this.aggregateDefs.map(p=>p.createAccumulator())}if(i!==null){let p=this.hasCachedValues?Object.create(null):null;for(let d=0;d<this.aggregateDefs.length;d++){let m=this.aggregateDefs[d],g;p&&m.valueKey?Object.prototype.hasOwnProperty.call(p,m.valueKey)?g=p[m.valueKey]:(g=m.extractValue(l,f),p[m.valueKey]=g):g=m.extractValue(l,f),i[d].add(g)}}}if(i!==null){let l=[...r];for(let u=0;u<i.length;u++)l.push(i[u].result());s.push(l)}else if(this.groupByExtractors.length===0){let l=this.aggregateDefs.map(u=>u.createAccumulator());s.push(l.map(u=>u.result()))}if(s.length===0)return[];let o=this.groupByExtractors.length+this.aggregateDefs.length,a=[];for(let l=0;l<this.groupByExtractors.length;l++)a.push(new Qt(this.groupByTypes[l]||"VARCHAR",s.length));for(let l=0;l<this.aggregateDefs.length;l++)a.push(new Qt(this.aggregateDefs[l].resultType,s.length));for(let l=0;l<s.length;l++){let u=s[l];for(let f=0;f<o;f++)a[f].set(l,typeof u[f]=="bigint"?Number(u[f]):u[f])}for(let l of a)l.length=s.length;return[new kt(a,s.length)]}extractGroupKey(e,t){return this.groupByExtractors.length===0?"__ALL__":this.groupByExtractors.map(s=>{let n=s(e,t);return typeof n=="bigint"?n.toString():String(n)}).join("|")}}}});async function iq(e,t){let s=await e.buildPipeline(t.children[0]),n=(t.groupBy||[]).map(f=>Dt(f,s.columnMapping)),r=(t.groupBy||[]).map(f=>e.normalizeExecType(f?.dataType||f?.resultType||"VARCHAR")),i=uP(t.aggregates,s.columnMapping),o=[...(t.groupBy||[]).map((f,h)=>({name:f?.columnName||`group${h}`,dataType:r[h],tableAlias:f?.tableAlias||""})),...t.aggregates.map((f,h)=>({name:f.outputName||(f.name||"").toLowerCase(),dataType:e.normalizeAggResultType(f),tableAlias:""}))],a=Zk(o,t.groupBy||[],t.aggregates);if(t.physicalStrategy==="STREAM")return{schema:o,columnMapping:a,register:c((f,h,p)=>{let d=Ql(f,h,s);f.setSource(h,async function*(){let g=await new MP(n,r,i).execute(d);for(let y of g)await p.consume(y),yield y;p.finalize&&await p.finalize()})},"register")};let l={schema:o,columnMapping:a,register:c((f,h,p)=>{let d=new cd(n,r,i),m={async consume(y){await d.consume(y)},async finalize(){}},g=f.createPipeline(m);s.register(f,g,m),f.addDependency(h,g),f.setSource(h,async function*(){let y=await d.finalize();for(let _ of y)await p.consume(_),yield _;p.finalize&&await p.finalize()})},"register")},u=oq(e,t);return u?{schema:o,columnMapping:a,register:c((f,h,p)=>{let d=u.storage.rowCount(),m=d*u.estimatedRowBytes<=ht.parallelAggMemoryBytes;if(d<ht.parallelAggThreshold||!m){l.register(f,h,p);return}f.setSource(h,async function*(){let g=null;try{let y=[];for await(let _ of u.storage.scan())y.push(_);g=await e.fragmentPool.runAggregate(u.spec,u.columnIndexes,y,{spillDir:e.tempManager.allocate("spill","pagg")})}catch{g=null}g===null&&(g=await e._executeSubPipeline(l));for(let y of g)await p.consume(y),yield y;p.finalize&&await p.finalize()})},"register")}:l}c(iq,"buildAggregate");function oq(e,t){if(!e.fragmentPool)return null;let s=c6(t);if(!s)return null;let n=e.catalog.getTableStorage(s.table);if(!n||typeof n.scan!="function")return null;let r=f6(s,t,n.getSchema());return r?{storage:n,...r}:null}c(oq,"prepareParallelAggregate");function Zk(e,t,s){let n=new Map,r=0;for(let o of e){let a=o.tableAlias?`${o.tableAlias}.${o.name}`.toUpperCase():o.name.toUpperCase();n.set(a,r),n.set(o.name.toUpperCase(),r),r++}let i=t.length;for(let o=0;o<s.length;o++)n.set(qL(s[o]),i+o);return n}c(Zk,"aggregateSchemaMapping");async function aq(e,t){let s=await e.buildPipeline(t.children[0]),n=(t.groupBy||[]).map(f=>Dt(f,s.columnMapping)),r=(t.groupBy||[]).map(f=>e.normalizeExecType(f?.dataType||f?.resultType||"VARCHAR")),i=[],o=[],a=[];for(let f of t.aggregates){let h=(f.func||f.name||"").toUpperCase(),p=f.args&&f.args.length>0?Dt(f.args[0],s.columnMapping):()=>1,d=c((m,g)=>{let y=p(m,g);return typeof y=="bigint"?Number(y):y},"extract");if(h==="AVG_PARTIAL"){i.push({name:"SUM",resultType:"FLOAT64",createAccumulator:vf("SUM"),extractValue:d}),i.push({name:"COUNT",resultType:"FLOAT64",createAccumulator:vf("COUNT"),extractValue:d}),o.push({name:"_avg_sum",dataType:"FLOAT64",tableAlias:""}),o.push({name:"_avg_count",dataType:"FLOAT64",tableAlias:""}),a.push({func:"SUM",args:f.args},{func:"COUNT",args:f.args});continue}i.push({name:f.func||f.name,resultType:e.normalizeAggResultType(f),createAccumulator:vf(f.func||f.name,f.distinct),extractValue:d}),o.push({name:(f.func||f.name||"").toLowerCase(),dataType:e.normalizeAggResultType(f),tableAlias:""}),a.push(f)}let l=[...(t.groupBy||[]).map((f,h)=>({name:f?.columnName||`group${h}`,dataType:r[h],tableAlias:f?.tableAlias||""})),...o],u=Zk(l,t.groupBy||[],a);return{schema:l,columnMapping:u,register:LP(s,()=>new cd(n,r,i))}}c(aq,"buildPartialAggregate");async function lq(e,t){let s=await e.buildPipeline(t.children[0]),n=(t.groupBy||[]).length,r=(t.groupBy||[]).map((m,g)=>{let y=g;return(_,w)=>_.columns[y]?.get(w)??null}),i=(t.groupBy||[]).map(m=>e.normalizeExecType(m?.dataType||m?.resultType||"VARCHAR")),o=t.aggregates,a=t.partialAggregates||o,l=c(m=>(m.func||m.name||"").toUpperCase()==="AVG_PARTIAL"?2:1,"partialWidth"),u=[],f=n;for(let m=0;m<o.length;m++)u.push(f),f+=l(a[m]||o[m]);let h=o.map((m,g)=>{let y=(m.func||m.name||"").toUpperCase(),_=u[g];return y==="AVG_FINAL"?{name:"AVG",resultType:e.normalizeAggResultType(m),createAccumulator:vf("AVG_FINAL",!1),extractValue:c((w,b)=>{let x=w.columns[_]?.get(b),A=w.columns[_+1]?.get(b);return[typeof x=="bigint"?Number(x):x,typeof A=="bigint"?Number(A):A]},"extractValue")}:{name:y,resultType:e.normalizeAggResultType(m),createAccumulator:vf(y,!1),extractValue:c((w,b)=>{let x=w.columns[_]?.get(b);return typeof x=="bigint"?Number(x):x},"extractValue")}}),p=[...(t.groupBy||[]).map((m,g)=>({name:m?.columnName||`group${g}`,dataType:i[g],tableAlias:m?.tableAlias||""})),...o.map(m=>({name:(m.name||m.func||"").toLowerCase(),dataType:e.normalizeAggResultType(m),tableAlias:""}))],d=Zk(p,t.groupBy||[],o);return{schema:p,columnMapping:d,register:LP(s,()=>new cd(r,i,h))}}c(lq,"buildFinalAggregate");function LP(e,t){return(s,n,r)=>{let i=t(),o={async consume(l){await i.consume(l)},async finalize(){}},a=s.createPipeline(o);e.register(s,a,o),s.addDependency(n,a),s.setSource(n,async function*(){let l=await i.finalize();for(let u of l)await r.consume(u),yield u;r.finalize&&await r.finalize()})}}c(LP,"registerHashAggregate");var cq=J({"src/execution/builders/aggregate-builder.ts"(){"use strict";Ft(),ad(),lP(),rq(),jw(),Fe(),Xk(),we()}}),pk,uq=J({"src/execution/operators/dependent-join.ts"(){"use strict";Ts(),be(),we(),pk=class{static{c(this,"DependentJoinOperator")}subqueryType;outerSchema;resultRows;resultSchema;constructor(e,t){this.subqueryType=e,this.outerSchema=t,this.resultRows=[],this.resultSchema=this.subqueryType==="SCALAR"?[...t,{name:"_scalar",dataType:"FLOAT64",tableAlias:""}]:t}async processOuterRow(e,t){let s=[];for(let n of t)for(let r=0;r<n.size;r++){let i=[];for(let o=0;o<n.columns.length;o++)i.push(n.columns[o].get(n.activeRowIndex(r)));s.push(i)}if(this.subqueryType==="EXISTS")s.length>0&&this.resultRows.push(e);else if(this.subqueryType==="NOT_EXISTS")s.length===0&&this.resultRows.push(e);else if(this.subqueryType==="SCALAR"){let n=s.length>0?s[0][0]:null;this.resultRows.push([...e,n])}else this.subqueryType==="IN"?s.length>0&&this.resultRows.push(e):this.subqueryType==="NOT_IN"?s.length===0&&this.resultRows.push(e):this.resultRows.push(e)}async finalize(){if(this.resultRows.length===0)return[];let e=this.resultSchema.length,t=[];for(let s=0;s<e;s++){let n=new Qt(this.resultSchema[s].dataType||"VARCHAR",this.resultRows.length);for(let r=0;r<this.resultRows.length;r++)n.set(r,this.resultRows[r][s]);n.length=this.resultRows.length,t.push(n)}return[new kt(t,this.resultRows.length)]}}}});async function fq(e,t){let s=await e.buildPipeline(t.children[0]);e.cteDefinitions.set(t.cteName.toUpperCase(),t.children[0]);let n=await e.buildPipeline(t.children[1]);return{schema:n.schema,columnMapping:n.columnMapping,register:c((r,i,o)=>{let a=[],l={consume:c(async f=>{a.push(f)},"consume")},u=r.createPipeline(l);s.register(r,u,l),l.finalize=async()=>{e.cteResults.set(t.cteName.toUpperCase(),{chunks:a,schema:s.schema,columnMapping:s.columnMapping})},r.addDependency(i,u),n.register(r,i,o)},"register")}}c(fq,"buildCTEAnchor");async function hq(e,t){let s=e.findCTEPlan(t.cteName);if(!s)throw new Error(`CTE not found: ${t.cteName}`);let n=await e.buildPipeline(s);return{schema:n.schema,columnMapping:n.columnMapping,register:c((r,i,o)=>{let a=c(async function*(){let l=e.cteResults.get(t.cteName.toUpperCase());if(!l){let f=[],h={async consume(g){f.push(g)},async finalize(){}},p=new Bf,d=p.createPipeline(h);n.register(p,d,h),await new Cf().schedule(p),l={chunks:f,schema:n.schema,columnMapping:n.columnMapping},e.cteResults.set(t.cteName.toUpperCase(),l)}let u=l.chunks.map(f=>{let h=f.columns.map(p=>{let d=new Qt(p.dataType,f.size);for(let m=0;m<f.size;m++)d.set(m,p.get(f.activeRowIndex(m)));return d.length=f.size,d});return new kt(h,f.size)});for(let f of u)await o.consume(f),yield f},"source");r.setSource(i,a)},"register")}}c(hq,"buildCTEScan");async function pq(e,t){let s=await e.buildPipeline(t.children[0]);return{schema:s.schema,columnMapping:s.columnMapping,register:c((n,r,i)=>{s.register(n,r,i)},"register")}}c(pq,"buildMaterialize");async function dq(e,t){let s=await e.buildPipeline(t.children[0]),n=new pk(t.subqueryType,s.schema);return{schema:n.resultSchema,columnMapping:e.buildSchemaMapping(n.resultSchema,""),register:c((r,i,o)=>{let a=[],l={consume:c(async h=>{a.push(h)},"consume")},u=r.createPipeline(l);s.register(r,u,l),r.addDependency(i,u);let f=c(async function*(){let h=new pk(t.subqueryType,s.schema),p=(t.correlatedColumns||[]).length>0,d=null;for(let g of a){let y=g.toRows();for(let _ of y){if(!p&&d!==null){await h.processOuterRow(_,d);continue}let w=await e.buildPipeline(t.children[1]),b=[],x=new Bf,A={consume:c(async E=>{b.push(E)},"consume")},S=x.createPipeline(A);w.register(x,S,A),await new Cf(ht.dependentJoinConcurrency).schedule(x),p||(d=b),await h.processOuterRow(_,b)}}let m=await h.finalize();for(let g of m)await o.consume(g),yield g},"source");r.setSource(i,f)},"register")}}c(dq,"buildDependentJoin");var mq=J({"src/execution/builders/cte-builders.ts"(){"use strict";uq(),Ts(),be(),ud(),Kw(),Fe()}}),PP={};us(PP,{ExchangeType:c(()=>re,"ExchangeType"),Fragment:c(()=>Pi,"Fragment"),FragmentPlan:c(()=>tE,"FragmentPlan"),FragmentState:c(()=>qs,"FragmentState"),resetFragmentIdCounter:c(()=>DP,"resetFragmentIdCounter")});function DP(){dk=1}c(DP,"resetFragmentIdCounter");var re,qs,dk,Pi,tE,Tf=J({"src/distributed/planner/fragment.ts"(){"use strict";re={HASH_SHUFFLE:"hash_shuffle",BROADCAST:"broadcast",GATHER:"gather",PASSTHROUGH:"passthrough"},qs={PENDING:"pending",DISPATCHED:"dispatched",RUNNING:"running",COMPLETED:"completed",FAILED:"failed",CANCELLED:"cancelled"},dk=1,Pi=class{static{c(this,"Fragment")}fragmentId;planRoot;targetNodes;exchangeInputs;outputPartitioning;estimatedCardinality;state;retryCount;error;assignedNode;constructor({planRoot:e,targetNodes:t,exchangeInputs:s,outputPartitioning:n,estimatedCardinality:r}){this.fragmentId=dk++,this.planRoot=e,this.targetNodes=t||[],this.exchangeInputs=s||[],this.outputPartitioning=n||null,this.estimatedCardinality=r||0,this.state=qs.PENDING,this.retryCount=0,this.error=null,this.assignedNode=null}isLeaf(){return this.exchangeInputs.length===0}isRoot(){return this.outputPartitioning===null}markDispatched(e){this.state=qs.DISPATCHED,this.assignedNode=e}markRunning(){this.state=qs.RUNNING}markCompleted(){this.state=qs.COMPLETED}markFailed(e){this.state=qs.FAILED,this.error=e,this.retryCount++}markCancelled(){this.state=qs.CANCELLED}canRetry(e){return this.retryCount<e}toJSON(){return{fragmentId:this.fragmentId,planRoot:this.planRoot,targetNodes:this.targetNodes,exchangeInputs:this.exchangeInputs,outputPartitioning:this.outputPartitioning,estimatedCardinality:this.estimatedCardinality,state:this.state}}},tE=class{static{c(this,"FragmentPlan")}fragments;rootFragmentId;_fragmentMap;constructor(e,t){this.fragments=e,this.rootFragmentId=t,this._fragmentMap=new Map;for(let s of e)this._fragmentMap.set(s.fragmentId,s)}getFragment(e){return this._fragmentMap.get(e)||null}getRootFragment(){return this._fragmentMap.get(this.rootFragmentId)||null}getLeafFragments(){return this.fragments.filter(e=>e.isLeaf())}topologicalOrder(){let e=new Map,t=new Map;for(let r of this.fragments)e.set(r.fragmentId,0),t.set(r.fragmentId,[]);for(let r of this.fragments)for(let i of r.exchangeInputs)t.get(i.sourceFragmentId).push(r.fragmentId),e.set(r.fragmentId,e.get(r.fragmentId)+1);let s=[];for(let[r,i]of e)i===0&&s.push(r);let n=[];for(;s.length>0;){let r=s.shift();n.push(r);for(let i of t.get(r)){let o=e.get(i)-1;e.set(i,o),o===0&&s.push(i)}}return n.map(r=>this._fragmentMap.get(r))}getReadyFragments(){return this.fragments.filter(e=>e.state!==qs.PENDING?!1:e.exchangeInputs.every(t=>{let s=this._fragmentMap.get(t.sourceFragmentId);return s&&s.state===qs.COMPLETED}))}allCompleted(){return this.fragments.every(e=>e.state===qs.COMPLETED)}hasFailed(){return this.fragments.some(e=>e.state===qs.FAILED)}}}}),Ov,EM,Mv,zp,Iw,FP=J({"src/distributed/transport/chunk-codec.ts"(){"use strict";we(),Ts(),Gw(),be(),Zl(),Ov={BOOLEAN:0,INT32:1,INT64:2,FLOAT64:3,DECIMAL:4,VARCHAR:5,DATE:6,TIMESTAMP:7},EM=Object.fromEntries(Object.entries(Ov).map(([e,t])=>[t,e])),Mv=1363493704,zp=6,Iw=class{static{c(this,"ChunkCodec")}encode(e){let t=e.selectionVector?e.flatten():e,s=t.columns.length,n=t.size,r=new Array(s),i=0;for(let h=0;h<s;h++){let p=t.columns[h],d=this._encodeColumn(p,n);r[h]=d,i+=d.length}let o=10+s*zp,a=o+i,l=Buffer.allocUnsafe(a);l.writeUInt32LE(Mv,0),l.writeUInt16LE(s,4),l.writeUInt32LE(n,6);let u=10;for(let h=0;h<s;h++){let p=t.columns[h],d=Ov[p.dataType],m=p instanceof pr?1:0;l.writeUInt8(d,u),l.writeUInt8(m,u+1),l.writeUInt32LE(r[h].length,u+2),u+=zp}let f=o;for(let h=0;h<s;h++)r[h].copy(l,f),f+=r[h].length;return l}decode(e){if(e.readUInt32LE(0)!==Mv)throw new Error("Invalid chunk codec magic number");let s=e.readUInt16LE(4),n=e.readUInt32LE(6),r=new Array(s),i=10;for(let l=0;l<s;l++){let u=e.readUInt8(i),f=e.readUInt8(i+1),h=e.readUInt32LE(i+2);r[l]={dataType:EM[u],isDictionary:f===1,payloadSize:h},i+=zp}let o=new Array(s),a=10+s*zp;for(let l=0;l<s;l++){let u=r[l],f=e.subarray(a,a+u.payloadSize);o[l]=this._decodeColumn(f,u.dataType,u.isDictionary,n),a+=u.payloadSize}return new kt(o,n)}_encodeColumn(e,t){return e instanceof pr?this._encodeDictionaryColumn(e,t):this._encodeRegularColumn(e,t)}_encodeRegularColumn(e,t){if(e.dataType==="VARCHAR")return this._encodeVarcharColumn(e,t);let s=Math.ceil(t/32),n=s*4,r=Rf(e.dataType),i=t*r,o=Buffer.allocUnsafe(1+n+i);o.writeUInt8(e.hasNulls?1:0,0);for(let u=0;u<s;u++)o.writeUInt32LE(u<e.nullBitmap.length?e.nullBitmap[u]:0,1+u*4);let a=1+n,l=new Uint8Array(e.data.buffer,e.data.byteOffset,i);for(let u=0;u<i;u++)o[a+u]=l[u];return o}_encodeVarcharColumn(e,t){let s=Math.ceil(t/32),n=s*4,r=(t+1)*4,i=e.stringBytesUsed,o=Buffer.allocUnsafe(1+n+r+i);o.writeUInt8(e.hasNulls?1:0,0);for(let l=0;l<s;l++)o.writeUInt32LE(l<e.nullBitmap.length?e.nullBitmap[l]:0,1+l*4);let a=1+n;for(let l=0;l<=t;l++)o.writeUInt32LE(e.offsets[l],a),a+=4;for(let l=0;l<i;l++)o[a+l]=e.stringBytes[l];return o}_encodeDictionaryColumn(e,t){let s=Math.ceil(t/32),n=s*4,r=e.reverseDict.length,i=new TextEncoder,o=e.reverseDict.map(h=>i.encode(h)),a=4;for(let h of o)a+=4+h.length;let l=t*2,u=Buffer.allocUnsafe(1+n+a+l);u.writeUInt8(e.hasNulls?1:0,0);for(let h=0;h<s;h++)u.writeUInt32LE(h<e.nullBitmap.length?e.nullBitmap[h]:0,1+h*4);let f=1+n;u.writeUInt32LE(r,f),f+=4;for(let h of o){u.writeUInt32LE(h.length,f),f+=4;for(let p=0;p<h.length;p++)u[f+p]=h[p];f+=h.length}for(let h=0;h<t;h++)u.writeUInt16LE(e.indices[h],f),f+=2;return u}_decodeColumn(e,t,s,n){return s?this._decodeDictionaryColumn(e,n):t==="VARCHAR"?this._decodeVarcharColumn(e,n):this._decodeFixedColumn(e,t,n)}_decodeFixedColumn(e,t,s){let n=new Qt(t,s),r=Math.ceil(s/32),i=r*4;n.hasNulls=e.readUInt8(0)===1;let o=Av(s);for(let p=0;p<r;p++)o[p]=e.readUInt32LE(1+p*4);n.nullBitmap=o;let a=Rf(t),l=1+i,u=e.subarray(l,l+s*a),f=k4(t,0).constructor,h=new ArrayBuffer(s*a);return new Uint8Array(h).set(u),n.data=new f(h),n.length=s,n}_decodeVarcharColumn(e,t){let s=new Qt("VARCHAR",t),n=Math.ceil(t/32),r=n*4;s.hasNulls=e.readUInt8(0)===1;let i=Av(t);for(let u=0;u<n;u++)i[u]=e.readUInt32LE(1+u*4);s.nullBitmap=i;let o=1+r,a=new Uint32Array(t+1);for(let u=0;u<=t;u++)a[u]=e.readUInt32LE(o),o+=4;s.offsets=a;let l=a[t];s.stringBytes=new Uint8Array(l);for(let u=0;u<l;u++)s.stringBytes[u]=e[o+u];return s.stringBytesUsed=l,s.length=t,s}_decodeDictionaryColumn(e,t){let s=new pr(t),n=Math.ceil(t/32),r=n*4;s.hasNulls=e.readUInt8(0)===1;let i=Av(t);for(let p=0;p<n;p++)i[p]=e.readUInt32LE(1+p*4);s.nullBitmap=i;let o=1+r,a=e.readUInt32LE(o);o+=4;let l=new TextDecoder,u=new Array(a),f=new Map;for(let p=0;p<a;p++){let d=e.readUInt32LE(o);o+=4;let m=l.decode(e.subarray(o,o+d));u[p]=m,f.set(m,p),o+=d}s.reverseDict=u,s.dictionary=f;let h=new Uint16Array(t);for(let p=0;p<t;p++)h[p]=e.readUInt16LE(o),o+=2;return s.indices=h,s.length=t,s}}}});function gq(e,t){let s=typeof e=="string"?e:String(e),n=t>>>0,r=s.length,i=0;for(;i+4<=r;){let a=s.charCodeAt(i)&255|(s.charCodeAt(i+1)&255)<<8|(s.charCodeAt(i+2)&255)<<16|(s.charCodeAt(i+3)&255)<<24;a=Math.imul(a,3432918353),a=a<<15|a>>>17,a=Math.imul(a,461845907),n^=a,n=n<<13|n>>>19,n=Math.imul(n,5)+3864292196,i+=4}let o=0;switch(r-i){case 3:o^=(s.charCodeAt(i+2)&255)<<16;case 2:o^=(s.charCodeAt(i+1)&255)<<8;case 1:o^=s.charCodeAt(i)&255,o=Math.imul(o,3432918353),o=o<<15|o>>>17,o=Math.imul(o,461845907),n^=o}return n^=r,n^=n>>>16,n=Math.imul(n,2246822507),n^=n>>>13,n=Math.imul(n,3266489909),n^=n>>>16,n>>>0}c(gq,"murmur3");var Oa,eE=J({"src/distributed/partition/partition-strategy.ts"(){"use strict";Ts(),be(),Oa={HASH:"hash",RANGE:"range",ROUND_ROBIN:"round_robin"}}}),mk={};us(mk,{ExchangeReceiver:c(()=>nE,"ExchangeReceiver"),ExchangeSender:c(()=>sE,"ExchangeSender")});var sE,nE,Ow=J({"src/distributed/execution/exchange-operator.ts"(){"use strict";Fe(),Tf(),FP(),be(),Ts(),eE(),sE=class{static{c(this,"ExchangeSender")}_transport;_targetNodes;_exchangeType;_keyExtractors;_partitionCount;_channelId;_codec;_batchSize;_closed;constructor(e,t,s={}){this._transport=e,this._targetNodes=t,this._exchangeType=s.exchangeType||re.GATHER,this._keyExtractors=s.keyExtractors||[],this._partitionCount=s.partitionCount||t.length,this._channelId=s.channelId||`exchange-${Date.now()}`,this._codec=new Iw,this._batchSize=ht.exchangeBatchSize,this._closed=!1}get channelId(){return this._channelId}async init(){}async consume(e){if(!(this._closed||!e||e.size===0))switch(this._exchangeType){case re.GATHER:await this._sendGather(e);break;case re.BROADCAST:await this._sendBroadcast(e);break;case re.HASH_SHUFFLE:await this._sendShuffle(e);break;default:await this._sendGather(e)}}async finalize(){this._closed=!0;let e=this._codec.encode(new kt([],0)),t=this._targetNodes.map(s=>this._transport.sendChunk(s,this._channelId,e));await Promise.all(t)}async _sendGather(e){let t=this._codec.encode(e),s=this._targetNodes[0];await this._transport.sendChunk(s,this._channelId,t)}async _sendBroadcast(e){let t=this._codec.encode(e),s=this._targetNodes.map(n=>this._transport.sendChunk(n,this._channelId,t));await Promise.all(s)}async _sendShuffle(e){let t=this._partitionByKey(e),s=[];for(let[n,r]of t){let i=n%this._targetNodes.length,o=this._targetNodes[i],a=this._codec.encode(r);s.push(this._transport.sendChunk(o,this._channelId,a))}await Promise.all(s)}_partitionByKey(e){let t=e.size,s=this._partitionCount,n=new Map;for(let i=0;i<t;i++){let o=e.selectionVector?e.selectionVector[i]:i,l=this._keyExtractors.map(h=>h(e,o)).join("|"),u=gq(l,2538058380)%s,f=n.get(u);f||(f=[],n.set(u,f)),f.push(o)}let r=new Map;for(let[i,o]of n){let a=e.columns.map(l=>{let u=new Qt(l.dataType,o.length);for(let f=0;f<o.length;f++)u.set(f,l.get(o[f]));return u.length=o.length,u});r.set(i,new kt(a,o.length))}return r}},nE=class{static{c(this,"ExchangeReceiver")}_transport;_sourceNodes;_channelId;_codec;_bufferCapacity;_buffer;_completedSources;_resolveWaiter;_done;_error;_listening;constructor(e,t,s={}){this._transport=e,this._sourceNodes=t,this._channelId=s.channelId||`exchange-${Date.now()}`,this._codec=new Iw,this._bufferCapacity=ht.exchangeBufferCapacity,this._buffer=[],this._completedSources=new Set,this._resolveWaiter=null,this._done=!1,this._error=null,this._listening=!1}get channelId(){return this._channelId}async init(){this._listening||(this._listening=!0,this._transport.onChunkReceived(this._channelId,(e,t)=>{this._handleChunk(e,t)}))}async*generate(){for(;;){if(this._buffer.length>0){yield this._buffer.shift();continue}if(this._error)throw this._error;if(this._done)break;if(this._completedSources.size>=this._sourceNodes.length){this._done=!0;break}await new Promise(e=>{this._resolveWaiter=e})}}cleanup(){this._done=!0,this._transport.removeChunkListener(this._channelId),this._resolveWaiter&&(this._resolveWaiter(),this._resolveWaiter=null)}_handleChunk(e,t){try{let s=this._codec.decode(t);if(s.size===0&&s.columns.length===0){this._completedSources.add(e),this._completedSources.size>=this._sourceNodes.length&&(this._done=!0),this._wake();return}this._buffer.push(s),this._wake()}catch(s){this._error=s,this._wake()}}_wake(){if(this._resolveWaiter){let e=this._resolveWaiter;this._resolveWaiter=null,e()}}}}}),$P={};us($P,{MergeExchangeOperator:c(()=>zP,"MergeExchangeOperator"),MinHeap:c(()=>gk,"MinHeap")});var zP,gk,yq=J({"src/distributed/execution/merge-exchange.ts"(){"use strict";Fe(),be(),Ts(),we(),Zl(),FP(),zP=class{static{c(this,"MergeExchangeOperator")}_transport;_sourceNodes;_orderKeys;_limit;_channelId;_codec;_buffers;_exhausted;_resolveWaiter;_error;_listening;constructor(e,t,s={}){this._transport=e,this._sourceNodes=t,this._orderKeys=s.orderKeys||[],this._limit=s.limit||null,this._channelId=s.channelId||`merge-exchange-${Date.now()}`,this._codec=new Iw,this._buffers=new Map,this._exhausted=new Set,this._resolveWaiter=null,this._error=null,this._listening=!1}get channelId(){return this._channelId}async init(){if(!this._listening){this._listening=!0;for(let e of this._sourceNodes)this._buffers.set(e,[]);this._transport.onChunkReceived(this._channelId,(e,t)=>{this._handleChunk(e,t)})}}async*generate(){let e=new gk(this._compareEntries.bind(this)),t=0;await this._ensureAllStreamsHaveData();for(let[r]of this._buffers){let i=this._nextEntry(r);i&&e.push(i)}let s=null,n=0;for(;e.size>0&&!(this._limit&&t>=this._limit);){if(this._error)throw this._error;let r=e.pop(),i=this._computeRunLength(r,e),o=this._limit?Math.min(i,this._limit-t):i,a=0;for(;a<o;){s||(s=r.chunk.columns.map(p=>new Qt(p.dataType,We)));let u=We-n,f=Math.min(o-a,u),h=r.rowIdx+a;if(this._bulkCopy(r.chunk,h,s,n,f),n+=f,a+=f,t+=f,n>=We){for(let p of s)p.length=n;yield new kt(s,n),s=null,n=0}}let l=r.rowIdx+o;if(l<r.chunk.size)r.chunk._readIdx=l+1,e.push({nodeId:r.nodeId,chunk:r.chunk,rowIdx:l});else{r.chunk._readIdx=r.chunk.size;let u=this._nextEntry(r.nodeId);if(u)e.push(u);else if(!this._exhausted.has(r.nodeId)){await this._waitForData(r.nodeId);let f=this._nextEntry(r.nodeId);f&&e.push(f)}}}if(n>0&&s){for(let r of s)r.length=n;yield new kt(s,n)}}_computeRunLength(e,t){let s=e.chunk.size-e.rowIdx;if(t.size===0)return s;let n=t.peek(),r=1;for(let i=e.rowIdx+1;i<e.chunk.size&&!(this._compareRowAgainst(e.chunk,i,n)>0);i++)r++;return r}_compareRowAgainst(e,t,s){for(let n of this._orderKeys){let r=n.columnIndex!==void 0?n.columnIndex:0,i=e.columns[r]?.get(t),o=s.chunk.columns[r]?.get(s.rowIdx);if(!(i===null&&o===null)){if(i===null)return n.direction==="DESC"?-1:1;if(o===null||i<o)return n.direction==="DESC"?1:-1;if(i>o)return n.direction==="DESC"?-1:1}}return 0}_bulkCopy(e,t,s,n,r){for(let i=0;i<e.columns.length;i++){let o=e.columns[i],a=s[i];if(ri(o.dataType)&&o.data)a.data.set(o.data.subarray(t,t+r),n);else{for(let l=0;l<r;l++)a.set(n+l,o.get(t+l));continue}if(o.hasNulls){a.hasNulls=!0;for(let l=0;l<r;l++)Kl(o.nullBitmap,t+l)?Yl(a.nullBitmap,n+l):Uw(a.nullBitmap,n+l)}else V4(a.nullBitmap,n,n+r)}}cleanup(){this._transport.removeChunkListener(this._channelId)}_handleChunk(e,t){try{let s=this._codec.decode(t);if(s.size===0&&s.columns.length===0){this._exhausted.add(e),this._wake();return}let n=this._buffers.get(e);n&&(n.push(s),this._wake())}catch(s){this._error=s,this._wake()}}_nextEntry(e){let t=this._buffers.get(e);if(!t||t.length===0)return null;let s=t[0];if(s._readIdx===void 0&&(s._readIdx=0),s._readIdx>=s.size)return t.shift(),this._nextEntry(e);let n={nodeId:e,chunk:s,rowIdx:s._readIdx};return s._readIdx++,n}async _ensureAllStreamsHaveData(){for(let e of this._sourceNodes)this._buffers.get(e).length===0&&!this._exhausted.has(e)&&await this._waitForData(e)}async _waitForData(e){let t=ht.coordinatorTimeoutMs,s=Date.now();for(;;){let n=this._buffers.get(e);if(n&&n.length>0||this._exhausted.has(e))return;if(Date.now()-s>t)throw new Error(`Timeout waiting for data from node ${e}`);await new Promise(r=>{this._resolveWaiter=r,setTimeout(r,100)})}}_wake(){if(this._resolveWaiter){let e=this._resolveWaiter;this._resolveWaiter=null,e()}}_compareEntries(e,t){for(let s of this._orderKeys){let n=s.columnIndex!==void 0?s.columnIndex:0,r=e.chunk.columns[n]?.get(e.rowIdx),i=t.chunk.columns[n]?.get(t.rowIdx);if(!(r===null&&i===null)){if(r===null)return s.direction==="DESC"?-1:1;if(i===null||r<i)return s.direction==="DESC"?1:-1;if(r>i)return s.direction==="DESC"?-1:1}}return 0}},gk=class{static{c(this,"MinHeap")}_data;_compare;constructor(e){this._data=[],this._compare=e}get size(){return this._data.length}push(e){this._data.push(e),this._bubbleUp(this._data.length-1)}pop(){if(this._data.length===0)return null;let e=this._data[0],t=this._data.pop();return this._data.length>0&&(this._data[0]=t,this._sinkDown(0)),e}peek(){return this._data[0]||null}_bubbleUp(e){for(;e>0;){let t=e-1>>>1;if(this._compare(this._data[e],this._data[t])>=0)break;this._swap(e,t),e=t}}_sinkDown(e){let t=this._data.length;for(;;){let s=e,n=2*e+1,r=2*e+2;if(n<t&&this._compare(this._data[n],this._data[s])<0&&(s=n),r<t&&this._compare(this._data[r],this._data[s])<0&&(s=r),s===e)break;this._swap(e,s),e=s}}_swap(e,t){let s=this._data[e];this._data[e]=this._data[t],this._data[t]=s}}}});function _q(e,t){return!e||e.length===0?[]:e.map(s=>{let n=Dt(s,t);return(r,i)=>n(r,i)})}c(_q,"buildKeyExtractors");async function wq(e,t){let s=await e.buildPipeline(t.children[0]);if(e._distributedContext){let{transport:n,sourceNodes:r,channelId:i,exchangeType:o}=e._distributedContext.getExchangeConfig(t);if(e._distributedContext.role==="receiver"){let{ExchangeReceiver:f}=await Promise.resolve().then(()=>(Ow(),mk)),h=new f(n,r,{channelId:i});return await h.init(),{schema:s.schema,columnMapping:s.columnMapping,register:c((p,d,m)=>{p.setSource(d,async function*(){for await(let g of h.generate())await m.consume(g),yield g;h.cleanup(),m.finalize&&await m.finalize()})},"register")}}let{ExchangeSender:l}=await Promise.resolve().then(()=>(Ow(),mk)),u=new l(n,t._targetNodes||[],{exchangeType:o||t.exchangeType,channelId:i,partitionCount:t.partitionCount,keyExtractors:_q(t.partitionKeys,s.columnMapping)});return{schema:s.schema,columnMapping:s.columnMapping,register:c((f,h,p)=>{let d={get cancelToken(){return p.cancelToken},async consume(m){await u.consume(m),await p.consume(m)},async finalize(){await u.finalize(),p.finalize&&await p.finalize()}};s.register(f,h,d)},"register")}}return{schema:s.schema,columnMapping:s.columnMapping,register:c((n,r,i)=>{s.register(n,r,i)},"register")}}c(wq,"buildExchange");async function bq(e,t){let s=await e.buildPipeline(t.children[0]);if(e._distributedContext){let{transport:n,sourceNodes:r,channelId:i}=e._distributedContext.getMergeExchangeConfig(t),{MergeExchangeOperator:o}=await Promise.resolve().then(()=>(yq(),$P)),a=new o(n,r,{orderKeys:t.orderKeys,limit:t.limit,channelId:i});return await a.init(),{schema:s.schema,columnMapping:s.columnMapping,register:c((l,u,f)=>{l.setSource(u,async function*(){for await(let h of a.generate())await f.consume(h),yield h;a.cleanup(),f.finalize&&await f.finalize()})},"register")}}return{schema:s.schema,columnMapping:s.columnMapping,register:c((n,r,i)=>{s.register(n,r,i)},"register")}}c(bq,"buildMergeExchange");async function xq(e,t){let s=e._exchangeReceivers,n=t.sourceFragmentIds||[],r=[];if(s)for(let a of n){let l=s.get(a);l&&r.push(l)}let i=t.schema||[],o=new Map;return i.forEach((a,l)=>{a.tableAlias&&o.set(`${a.tableAlias}.${a.name}`.toUpperCase(),l),o.set(a.name.toUpperCase(),l)}),{schema:i,columnMapping:o,register:c((a,l,u)=>{a.setSource(l,async function*(){for(let f of r){for await(let h of f.generate())h&&h.size>0&&(await u.consume(h),yield h);f.cleanup()}u.finalize&&await u.finalize()})},"register")}}c(xq,"buildExchangeReceive");var Sq=J({"src/execution/builders/exchange-builders.ts"(){"use strict";ad()}}),NM,rE,UP=J({"src/execution/query-executor.ts"(){"use strict";Ft(),jw(),jk(),ud(),Kw(),Fe(),x6(),R6(),G6(),nq(),cq(),mq(),Sq(),NM={Scan:v6,IndexScan:k6,Filter:M6,Project:L6,Join:X6,Aggregate:iq,Sort:P6,Limit:F6,Distinct:$6,Union:z6,CTEAnchor:fq,CTEScan:hq,Materialize:pq,DependentJoin:dq,TopN:D6,Window:U6,Empty:N6,SingleRow:E6,Exchange:wq,PartialAggregate:aq,FinalAggregate:lq,MergeExchange:bq,ExchangeReceive:xq},rE=class{static{c(this,"QueryExecutor")}catalog;tempManager;storageBackend;cteResults;cteDefinitions;workerPool;parallelDispatch;fragmentPool;_distributedContext;constructor(e,t,s=null){this.catalog=e,this.tempManager=t,this.storageBackend=s??new bP,this.cteResults=new Map,this.cteDefinitions=new Map,this.workerPool=null,this.parallelDispatch=null,this.fragmentPool=null,this._distributedContext=null}setParallelContext(e,t,s=null){this.workerPool=e,this.parallelDispatch=t,this.fragmentPool=s}setDistributedContext(e){this._distributedContext=e}_shouldParallelize(e){return!!(this.workerPool&&this.parallelDispatch&&e.rowCount()>=ht.parallelThreshold)}async execute(e,t,s=!1){let n=await this.executePlan(e,s),r=t.map(i=>i.name);return{sink:n,columnNames:r}}async executeStreaming(e,t){return this.execute(e,t,!0)}async executePlan(e,t=!1){this.cteResults.clear();let s=new Bf,n=new Ww(t);await n.init();let r=s.createPipeline(n);(await this.buildPipeline(e)).register(s,r,n);let o=new Cf;return t?(o.schedule(s).catch(l=>n.error(l)),n):(await o.schedule(s),n)}async buildPipeline(e){let t=NM[e.type];if(!t)throw new Error(`Unsupported plan node: ${e.type}`);return t(this,e)}resolveProjectedColumnIndexes(e,t){if(!t||t.length===0||t.length>=e.length)return null;let s=[];for(let n of t){let r=e.findIndex(i=>i.name.toUpperCase()===n.name.toUpperCase());if(r<0)return null;s.push(r)}return s}buildSchemaMapping(e,t){let s=new Map;for(let n=0;n<e.length;n++){let r=e[n],o=`${r.tableAlias||t||""}.${r.name}`.toUpperCase();s.set(o,n),s.has(r.name.toUpperCase())||s.set(r.name.toUpperCase(),n)}return s}findCTEPlan(e){let t=e.toUpperCase();return this.cteDefinitions?.get(t)||null}_estimatePlanRows(e){let t=0,s=[e];for(;s.length>0;){let n=s.pop();if(n){if(n.type==="Scan"||n.type==="IndexScan"){let r=this.catalog.getTableStorage(n.table);r&&(t+=r.rowCount())}for(let r of n.children||[])s.push(r)}}return t}async _executeSubPipeline(e){let t=[],s={consume:c(async i=>{t.push(i)},"consume"),finalize:c(async()=>{},"finalize")},n=new Bf,r=n.createPipeline(s);return e.register(n,r,s),await new Cf().schedule(n),t}_prepareParallelJoin(e,t,s,n,r,i,o,a,l){return eq(this,e,t,s,n,r,i,o,a,l)}_runBufferedSerialJoin(e,t,s,n,r,i){return sq(e,t,s,n,r,i)}normalizeExecType(e){return qw(e)}normalizeAggResultType(e){return cP(e)}}}}),bf,VP=J({"src/execution/query-result.ts"(){"use strict";bf=class{static{c(this,"QueryResult")}_columnNames;_sink;constructor(e,t){this._columnNames=e,this._sink=t}get columns(){return this._columnNames}async toArray(){let e=[];for await(let t of this._sink)for(let s=0;s<t.size;s++){let n=t.activeRowIndex(s),r={};for(let i=0;i<this._columnNames.length;i++){let o=t.columns[i].get(n);typeof o=="bigint"&&(o=Number(o)),r[this._columnNames[i]]=o}e.push(r)}return e}async*[Symbol.asyncIterator](){for await(let e of this._sink)for(let t=0;t<e.size;t++){let s=e.activeRowIndex(t),n={};for(let r=0;r<this._columnNames.length;r++){let i=e.columns[r].get(s);typeof i=="bigint"&&(i=Number(i)),n[this._columnNames[r]]=i}yield n}}async*chunks(){for await(let e of this._sink)yield e}}}}),ww={};us(ww,{formatExpression:c(()=>hr,"formatExpression"),formatPlan:c(()=>GP,"formatPlan")});function hr(e){if(!e)return"";switch(e.kind){case"BoundColumnRef":return e.tableAlias?`${e.tableAlias}.${e.columnName}`:e.columnName;case"BoundLiteral":return typeof e.value=="string"?`'${e.value}'`:String(e.value);case"BoundBinary":return`(${hr(e.left)} ${e.op} ${hr(e.right)})`;case"BoundUnary":return`${e.op} ${hr(e.operand)}`;case"BoundFunction":return`${e.name}(${e.args.map(hr).join(", ")})`;case"BoundAggregate":return`${e.name}(${e.args.map(hr).join(", ")})`;default:return e.kind?`<${e.kind}>`:JSON.stringify(e)}}c(hr,"formatExpression");function GP(e,t=0){let n=`${" ".repeat(t)}-> ${Aq(e)}
221
- `;if(e.children&&e.children.length>0)for(let r of e.children)n+=GP(r,t+1);return n}c(GP,"formatPlan");function Aq(e){switch(e.type){case"Scan":return`Seq Scan on ${e.table}${e.alias?" as "+e.alias:""}`;case"Filter":return`Filter (condition: ${hr(e.condition)})`;case"Project":return`Project (${e.expressions.map(n=>hr(n)).join(", ")})`;case"Join":{let n=e.joinType==="INNER"?"":`${e.joinType} `,r="";e.physicalStrategy==="HASH"?r="Hash ":e.physicalStrategy==="MERGE"&&(r="Merge ");let i=`${r}${n}Join`;return e.condition&&(i+=` (condition: ${hr(e.condition)})`),i}case"Aggregate":{let n="";e.physicalStrategy==="HASH"?n="Hash ":e.physicalStrategy==="STREAM"?n="Stream ":e.physicalStrategy==="PERFECT_HASH"?n="Perfect Hash ":e.physicalStrategy==="UNGROUPED"&&(n="Ungrouped ");let r=`${n}Aggregate`;return e.groupBy&&e.groupBy.length>0&&(r+=` (group by: ${e.groupBy.map(i=>hr(i)).join(", ")})`),e.aggregates&&e.aggregates.length>0&&(r+=` (aggs: ${e.aggregates.map(i=>`${i.name}(${i.args.map(o=>hr(o)).join(", ")})`).join(", ")})`),r}case"Sort":return`Sort (${e.orderKeys.map(n=>`${hr(n.expr)} ${n.direction||"ASC"}`).join(", ")})`;case"Limit":return`Limit (count: ${e.count}${e.offset?", offset: "+e.offset:""})`;case"TopN":{let n=e.orderKeys.map(r=>`${hr(r.expr)} ${r.direction||"ASC"}`).join(", ");return`Top-N (count: ${e.count}${e.offset?", offset: "+e.offset:""}, order: ${n})`}case"Distinct":return"Distinct";case"Union":return`Union${e.all?" All":""}`;case"CTEAnchor":return`CTE Anchor (${e.cteName})`;case"CTEScan":return`CTE Scan (${e.cteName})`;case"Materialize":return"Materialize";case"DependentJoin":return`Dependent Join (${e.subqueryType})`;case"IndexScan":{let n=`Index Scan using ${e.indexName} on ${e.table}`;return e.alias!==e.table&&(n+=` as ${e.alias}`),e.scanType==="point"?n+=` (key: ${e.scanKey})`:n+=` (range: ${e.scanLow??"-\u221E"} to ${e.scanHigh??"\u221E"})`,n}case"Empty":return"Empty (short-circuit)";case"Window":return`Window (${e.windowExprs.map(r=>r.name).join(", ")})`;default:return`${e.type}`}}c(Aq,"formatNode");var Lv=J({"src/planner/plan-formatter.ts"(){"use strict";Ft(),Vt()}}),Pv,rw,qP,vq=J({"src/wasm/region-allocator.ts"(){"use strict";Pv=16,rw=c(e=>e+Pv-1&~(Pv-1),"align"),qP=class{static{c(this,"RegionAllocator")}memory;regionSize;regions;totalAllocated;stagingOffset;dataOffset;dataBaseOffset;constructor(e,t){this.memory=e,this.regionSize=t,this.regions=[],this.totalAllocated=0,this.stagingOffset=0,this.dataOffset=0,this.dataBaseOffset=0}addRegion(){let e=this.regions.length,t=this.totalAllocated,s=this.regionSize;return this._ensureMemory(t+s),this.regions.push({id:e,start:t,capacity:s,offset:0}),this.totalAllocated=t+s,e}alloc(e,t){let s=this.regions[e];if(!s)throw new RangeError(`Region ${e} does not exist`);let n=rw(s.offset),r=n+t;r>s.capacity&&this._growRegion(e,r);let i=s.start+n;return s.offset=n+t,i}reset(e){let t=this.regions[e];if(!t)throw new RangeError(`Region ${e} does not exist`);t.offset=0}getRegionBounds(e){let t=this.regions[e];if(!t)throw new RangeError(`Region ${e} does not exist`);return{start:t.start,end:t.start+t.capacity}}getRegionUsage(e){let t=this.regions[e];if(!t)throw new RangeError(`Region ${e} does not exist`);return{used:t.offset,capacity:t.capacity,free:t.capacity-t.offset}}regionCount(){return this.regions.length}_growRegion(e,t){let s=this.regions[e];if(!(e===this.regions.length-1))throw new RangeError(`Region ${e} exhausted (${s.capacity} bytes) and cannot grow \u2014 only the last region is growable`);let r=s.capacity;for(;r<t;)r*=2;this._ensureMemory(s.start+r);let i=r-s.capacity;s.capacity=r,this.totalAllocated+=i}allocData(e){this.dataBaseOffset===0&&(this.dataBaseOffset=this.totalAllocated);let t=rw(this.dataOffset),s=this.dataBaseOffset+t,n=s+e;return this._ensureMemory(n),this.dataOffset=t+e,s}resetData(){this.dataOffset=0}allocStaging(e){let t=this.dataBaseOffset>0?this.dataBaseOffset+this.dataOffset:this.totalAllocated,s=rw(this.stagingOffset),n=t+s,r=n+e;return this._ensureMemory(r),this.stagingOffset=s+e,n}resetStaging(){this.stagingOffset=0}_ensureMemory(e){let t=this.memory.buffer.byteLength;if(e<=t)return;let n=Math.ceil((e-t)/65536);this.memory.grow(n)}}}}),RM,jP,WP,iw,ow,BM,KP,kq=J({"src/wasm/wasm-loader-base.ts"(){"use strict";vq(),RM=65536,jP=256,WP=16384,iw=16,ow=4,BM=8,KP=class{static{c(this,"WasmLoaderBase")}memory;instances;modules;moduleBytes;bumpOffset;regionAllocator;shared;constructor(){this.memory=null,this.instances=new Map,this.modules=new Map,this.moduleBytes=new Map,this.bumpOffset=0,this.regionAllocator=null,this.shared=!1}initRegions(e){if(!this.memory)throw new Error("Memory not initialized \u2014 call init() first");return this.regionAllocator=new qP(this.memory,e),this.regionAllocator}async fetchModuleBytes(e){throw new Error("fetchModuleBytes must be implemented by a platform loader")}async loadModule(e){if(this.instances.has(e))return this.instances.get(e);this.memory||await this.init();let t=await this.fetchModuleBytes(e);this.moduleBytes.set(e,t);let s=await WebAssembly.compile(t);this.modules.set(e,s);let r=WebAssembly.Module.imports(s).some(o=>o.name==="memory"),i=await WebAssembly.instantiate(s,{env:r?{memory:this.memory}:{}});return!r&&i.exports.memory&&(this.memory=i.exports.memory),this.instances.set(e,i),i}getModule(e){return this.modules.get(e)||null}getModuleBytes(e){return this.moduleBytes.get(e)||null}alloc(e){let t=this.bumpOffset+iw-1&~(iw-1),s=e+iw,n=t+s,r=this.memory.buffer.byteLength;if(n>r){let i=Math.ceil((n-r)/RM);this.memory.grow(i)}return this.bumpOffset=n,t}reset(){this.bumpOffset=0}getBuffer(){return this.memory.buffer}isShared(){return this.shared}isWasmBacked(e){return e.buffer===this.memory.buffer}allocTypedArray(e,t){if(!this.regionAllocator)return null;let s=e.BYTES_PER_ELEMENT,n=this.regionAllocator.allocData(t*s);return new e(this.memory.buffer,n,t)}resolveDataPtr(e,t){if(this.isWasmBacked(e))return e.byteOffset;let s=e.length*t,n=this.alloc(s);return t===ow?this.writeI32Array(e,n):this.writeF64Array(e,n),n}writeI32Array(e,t){new Int32Array(this.memory.buffer,t,e.length).set(e)}writeF64Array(e,t){new Float64Array(this.memory.buffer,t,e.length).set(e)}readI32Array(e,t){return new Int32Array(this.memory.buffer.slice(e,e+t*ow))}readF64Array(e,t){return new Float64Array(this.memory.buffer.slice(e,e+t*BM))}readF64(e){return new Float64Array(this.memory.buffer,e,1)[0]}readU32Array(e,t){return new Uint32Array(this.memory.buffer.slice(e,e+t*ow))}}}}),yk={};us(yk,{WasmLoader:c(()=>iE,"WasmLoader"),configureWasmSource:c(()=>HP,"configureWasmSource"),getGlobalLoader:c(()=>fd,"getGlobalLoader"),resetGlobalLoader:c(()=>Eq,"resetGlobalLoader")});function HP(e){Mw=e}c(HP,"configureWasmSource");async function fd(e={}){if(!xf){if(!Mw)throw new Error("WASM byte source not configured \u2014 call configureWasmSource() first");xf=new iE(Mw),await xf.init(e)}return xf}c(fd,"getGlobalLoader");function Eq(){xf=null}c(Eq,"resetGlobalLoader");var iE,xf,Mw,Xl=J({"src/wasm/loader.ts"(){"use strict";kq(),iE=class extends KP{static{c(this,"WasmLoader")}constructor(e){super(),this.byteSource=e}async init(e={}){this.shared=!0,this.memory=new WebAssembly.Memory({initial:jP,maximum:WP,shared:!0}),this.bumpOffset=0}async fetchModuleBytes(e){return this.byteSource(e)}},xf=null,Mw=null}});async function Hw(){if(Wp)return Wp;let t=await fd();return Wp={loader:t,instance:await t.loadModule("core")},Wp}c(Hw,"getCoreInstance");async function CM(e,t){let{loader:s,instance:n}=await Hw();s.reset();let r=e.length,i=s.resolveDataPtr(e,4),o=s.alloc(r*4),a=n.exports.filterEqI32(i,o,r,t);return s.readU32Array(o,a)}c(CM,"wasmFilterEqI32");async function TM(e,t){let{loader:s,instance:n}=await Hw();s.reset();let r=e.length,i=s.resolveDataPtr(e,4),o=s.alloc(r*4),a=n.exports.filterLtI32(i,o,r,t);return s.readU32Array(o,a)}c(TM,"wasmFilterLtI32");async function IM(e,t){let{loader:s,instance:n}=await Hw();s.reset();let r=e.length,i=s.resolveDataPtr(e,4),o=s.alloc(r*4),a=n.exports.filterGtI32(i,o,r,t);return s.readU32Array(o,a)}c(IM,"wasmFilterGtI32");async function OM(e,t,s){let{loader:n,instance:r}=await Hw();n.reset();let i=e.length,o=n.resolveDataPtr(e,4),a=n.alloc(i*4),l=r.exports.filterBetweenI32(o,a,i,t,s);return n.readU32Array(a,l)}c(OM,"wasmFilterBetweenI32");var Wp,Nq=J({"src/wasm/kernels/wasm-filter.ts"(){"use strict";Xl(),Wp=null}});async function za(){if(Kp)return Kp;let e=await fd();return Kp={loader:e,instance:await e.loadModule("core")},Kp}c(za,"getCoreInstance2");async function Rq(e,t){let{loader:s,instance:n}=await za();s.reset();let r=e.length,i=s.resolveDataPtr(e,8),o=s.alloc(r*4),a=n.exports.filterEqF64(i,o,r,t);return s.readU32Array(o,a)}c(Rq,"wasmFilterEqF64");async function Bq(e,t){let{loader:s,instance:n}=await za();s.reset();let r=e.length,i=s.resolveDataPtr(e,8),o=s.alloc(r*4),a=n.exports.filterLtF64(i,o,r,t);return s.readU32Array(o,a)}c(Bq,"wasmFilterLtF64");async function Cq(e,t){let{loader:s,instance:n}=await za();s.reset();let r=e.length,i=s.resolveDataPtr(e,8),o=s.alloc(r*4),a=n.exports.filterGtF64(i,o,r,t);return s.readU32Array(o,a)}c(Cq,"wasmFilterGtF64");async function Tq(e,t){let{loader:s,instance:n}=await za();s.reset();let r=e.length,i=s.resolveDataPtr(e,8),o=s.alloc(r*4),a=n.exports.filterLeF64(i,o,r,t);return s.readU32Array(o,a)}c(Tq,"wasmFilterLeF64");async function Iq(e,t){let{loader:s,instance:n}=await za();s.reset();let r=e.length,i=s.resolveDataPtr(e,8),o=s.alloc(r*4),a=n.exports.filterGeF64(i,o,r,t);return s.readU32Array(o,a)}c(Iq,"wasmFilterGeF64");async function Oq(e,t,s){let{loader:n,instance:r}=await za();n.reset();let i=e.length,o=n.resolveDataPtr(e,8),a=n.alloc(i*4),l=r.exports.filterBetweenF64(o,a,i,t,s);return n.readU32Array(a,l)}c(Oq,"wasmFilterBetweenF64");async function MM(e,t){let{loader:s,instance:n}=await za();s.reset();let r=e.length,i=s.resolveDataPtr(e,4),o=s.alloc(r*4),a=n.exports.filterLeI32(i,o,r,t);return s.readU32Array(o,a)}c(MM,"wasmFilterLeI32");async function LM(e,t){let{loader:s,instance:n}=await za();s.reset();let r=e.length,i=s.resolveDataPtr(e,4),o=s.alloc(r*4),a=n.exports.filterGeI32(i,o,r,t);return s.readU32Array(o,a)}c(LM,"wasmFilterGeI32");var Kp,Mq=J({"src/wasm/kernels/wasm-filter-f64.ts"(){"use strict";Xl(),Kp=null}});async function If(){if(Hp)return Hp;let e=await fd();return Hp={loader:e,instance:await e.loadModule("core")},Hp}c(If,"getCoreInstance3");async function Lq(e){let{loader:t,instance:s}=await If();t.reset();let n=t.resolveDataPtr(e,4);return s.exports.sumI32(n,e.length)}c(Lq,"wasmSumI32");async function Pq(e){let{loader:t,instance:s}=await If();t.reset();let n=t.resolveDataPtr(e,8);return s.exports.sumF64(n,e.length)}c(Pq,"wasmSumF64");async function Dq(e){let{loader:t,instance:s}=await If();t.reset();let n=t.resolveDataPtr(e,4);return s.exports.minI32(n,e.length)}c(Dq,"wasmMinI32");async function Fq(e){let{loader:t,instance:s}=await If();t.reset();let n=t.resolveDataPtr(e,4);return s.exports.maxI32(n,e.length)}c(Fq,"wasmMaxI32");async function $q(e){let{loader:t,instance:s}=await If();t.reset();let n=t.resolveDataPtr(e,8);return s.exports.minF64(n,e.length)}c($q,"wasmMinF64");async function zq(e){let{loader:t,instance:s}=await If();t.reset();let n=t.resolveDataPtr(e,8);return s.exports.maxF64(n,e.length)}c(zq,"wasmMaxF64");var Hp,Uq=J({"src/wasm/kernels/wasm-aggregate.ts"(){"use strict";Xl(),Hp=null}});async function mr(){if(Jp)return Jp;let e=await fd();return Jp={loader:e,instance:await e.loadModule("core")},Jp}c(mr,"getCoreInstance4");async function Vq(e,t){let{loader:s,instance:n}=await mr();s.reset();let r=e.length,i=s.alloc(r*8),o=s.alloc(r*8),a=s.alloc(r*8);return s.writeF64Array(e,i),s.writeF64Array(t,o),n.exports.vecAddF64(i,o,a,r),s.readF64Array(a,r)}c(Vq,"wasmVecAddF64");async function Gq(e,t){let{loader:s,instance:n}=await mr();s.reset();let r=e.length,i=s.alloc(r*8),o=s.alloc(r*8),a=s.alloc(r*8);return s.writeF64Array(e,i),s.writeF64Array(t,o),n.exports.vecSubF64(i,o,a,r),s.readF64Array(a,r)}c(Gq,"wasmVecSubF64");async function qq(e,t){let{loader:s,instance:n}=await mr();s.reset();let r=e.length,i=s.alloc(r*8),o=s.alloc(r*8),a=s.alloc(r*8);return s.writeF64Array(e,i),s.writeF64Array(t,o),n.exports.vecMulF64(i,o,a,r),s.readF64Array(a,r)}c(qq,"wasmVecMulF64");async function jq(e,t){let{loader:s,instance:n}=await mr();s.reset();let r=e.length,i=s.alloc(r*8),o=s.alloc(r*8),a=s.alloc(r*8);return s.writeF64Array(e,i),s.writeF64Array(t,o),n.exports.vecDivF64(i,o,a,r),s.readF64Array(a,r)}c(jq,"wasmVecDivF64");async function Wq(e,t){let{loader:s,instance:n}=await mr();s.reset();let r=e.length,i=s.alloc(r*8),o=s.alloc(r*8);return s.writeF64Array(e,i),n.exports.scalarAddF64(i,t,o,r),s.readF64Array(o,r)}c(Wq,"wasmScalarAddF64");async function Kq(e,t){let{loader:s,instance:n}=await mr();s.reset();let r=e.length,i=s.alloc(r*8),o=s.alloc(r*8);return s.writeF64Array(e,i),n.exports.scalarSubF64(i,t,o,r),s.readF64Array(o,r)}c(Kq,"wasmScalarSubF64");async function Hq(e,t){let{loader:s,instance:n}=await mr();s.reset();let r=e.length,i=s.alloc(r*8),o=s.alloc(r*8);return s.writeF64Array(e,i),n.exports.scalarMulF64(i,t,o,r),s.readF64Array(o,r)}c(Hq,"wasmScalarMulF64");async function Jq(e,t){let{loader:s,instance:n}=await mr();s.reset();let r=e.length,i=s.alloc(r*8),o=s.alloc(r*8);return s.writeF64Array(e,i),n.exports.scalarDivF64(i,t,o,r),s.readF64Array(o,r)}c(Jq,"wasmScalarDivF64");async function Yq(e,t){let{loader:s,instance:n}=await mr();s.reset();let r=t.length,i=s.alloc(r*8),o=s.alloc(r*8);return s.writeF64Array(t,i),n.exports.scalarSubRevF64(e,i,o,r),s.readF64Array(o,r)}c(Yq,"wasmScalarSubRevF64");async function Qq(e,t){let{loader:s,instance:n}=await mr();s.reset();let r=t.length,i=s.alloc(r*8),o=s.alloc(r*8);return s.writeF64Array(t,i),n.exports.scalarDivRevF64(e,i,o,r),s.readF64Array(o,r)}c(Qq,"wasmScalarDivRevF64");async function Xq(e){let{loader:t,instance:s}=await mr();t.reset();let n=e.length,r=t.alloc(n*4),i=t.alloc(n*8);return t.writeI32Array(e,r),s.exports.widenI32ToF64(r,i,n),t.readF64Array(i,n)}c(Xq,"wasmWidenI32ToF64");async function Zq(e){let{loader:t,instance:s}=await mr();t.reset();let n=e.length,r=t.alloc(n*8),i=t.alloc(n*8);return t.writeF64Array(e,r),s.exports.negF64(r,i,n),t.readF64Array(i,n)}c(Zq,"wasmNegF64");async function tj(e,t){let{loader:s,instance:n}=await mr();s.reset();let i=Math.ceil(t/32)*4,o=s.alloc(i);return new Uint8Array(s.getBuffer(),o,i).set(new Uint8Array(e.buffer,e.byteOffset,i)),n.exports.countBitmapBits(o,t)}c(tj,"wasmCountBits");var Jp,ej=J({"src/wasm/kernels/wasm-arithmetic.ts"(){"use strict";Xl(),Jp=null}}),_k={};us(_k,{registerAllKernels:c(()=>sj,"registerAllKernels")});function sj(){ct.register("filterEq","INT32",CM),ct.register("filterLt","INT32",TM),ct.register("filterGt","INT32",IM),ct.register("filterLe","INT32",MM),ct.register("filterGe","INT32",LM),ct.register("filterBetween","INT32",OM),ct.register("filterEq","FLOAT64",Rq),ct.register("filterLt","FLOAT64",Bq),ct.register("filterGt","FLOAT64",Cq),ct.register("filterLe","FLOAT64",Tq),ct.register("filterGe","FLOAT64",Iq),ct.register("filterBetween","FLOAT64",Oq),ct.register("filterEq","DATE",CM),ct.register("filterLt","DATE",TM),ct.register("filterGt","DATE",IM),ct.register("filterLe","DATE",MM),ct.register("filterGe","DATE",LM),ct.register("filterBetween","DATE",OM),ct.register("sumI32","INT32",Lq),ct.register("sumF64","FLOAT64",Pq),ct.register("minI32","INT32",Dq),ct.register("maxI32","INT32",Fq),ct.register("minF64","FLOAT64",$q),ct.register("maxF64","FLOAT64",zq),ct.register("vecAddF64","FLOAT64",Vq),ct.register("vecSubF64","FLOAT64",Gq),ct.register("vecMulF64","FLOAT64",qq),ct.register("vecDivF64","FLOAT64",jq),ct.register("scalarAddF64","FLOAT64",Wq),ct.register("scalarSubF64","FLOAT64",Kq),ct.register("scalarMulF64","FLOAT64",Hq),ct.register("scalarDivF64","FLOAT64",Jq),ct.register("scalarSubRevF64","FLOAT64",Yq),ct.register("scalarDivRevF64","FLOAT64",Qq),ct.register("widenI32ToF64","INT32",Xq),ct.register("negF64","FLOAT64",Zq),ct.register("countBits","UINT8",tj)}c(sj,"registerAllKernels");var PM=J({"src/wasm/register-kernels.ts"(){"use strict";ld(),Nq(),Mq(),Uq(),ej()}}),JP={};us(JP,{WorkerPool:c(()=>YP,"WorkerPool")});var DM,FM,Wn,YP,aj=J({"src/parallel/worker-pool.ts"(){"use strict";DM=ij(rj(import.meta.url)),FM=oj(DM,"worker-thread.js"),Wn={IDLE:0,BUSY:1,TERMINATED:2},YP=class{static{c(this,"WorkerPool")}maxWorkers;wasmModule;wasmMemory;regionAllocator;workers;taskQueue;taskIdCounter;pendingTasks;initialized;constructor({maxWorkers:e,wasmModule:t,wasmMemory:s,regionAllocator:n}){this.maxWorkers=e,this.wasmModule=t,this.wasmMemory=s,this.regionAllocator=n,this.workers=new Map,this.taskQueue=[],this.taskIdCounter=0,this.pendingTasks=new Map,this.initialized=!1}async init(){if(this.initialized)return;let e=[];for(let t=0;t<this.maxWorkers;t++)e.push(this._spawnWorker(t));await Promise.all(e),this.initialized=!0}async _spawnWorker(e){let t=this.regionAllocator.addRegion(),s=this.regionAllocator.getRegionBounds(t),n={workerId:e,wasmModule:this.wasmModule,wasmMemory:this.wasmMemory,regionId:t,regionStart:s.start,regionCapacity:s.end-s.start},r=new nj(FM,{workerData:n});return new Promise((i,o)=>{let a={worker:r,id:e,regionId:t,state:Wn.IDLE};r.on("message",l=>{if(l.type==="ready"){this.workers.set(e,a),i();return}if(l.type==="result"){let u=this.pendingTasks.get(l.taskId);u&&(this.pendingTasks.delete(l.taskId),a.state=Wn.IDLE,u.resolve(l.data),this._drainQueue());return}if(l.type==="error"){let u=this.pendingTasks.get(l.taskId);u&&(this.pendingTasks.delete(l.taskId),a.state=Wn.IDLE,u.reject(new Error(l.error)),this._drainQueue())}}),r.on("error",l=>{a.state=Wn.TERMINATED;for(let[u,f]of this.pendingTasks)f.workerId===e&&(this.pendingTasks.delete(u),f.reject(l));o(l)})})}execute(e){return Promise.all(e.map(t=>this._submitTask(t)))}executeOnWorker(e,t){return this._submitTask(t,e)}_submitTask(e,t){let s=this.taskIdCounter++;return new Promise((n,r)=>{let i={taskId:s,task:e,resolve:n,reject:r,targetWorkerId:t,workerId:null};if(t!==void 0){let a=this.workers.get(t);if(!a||a.state===Wn.TERMINATED){r(new Error(`Worker ${t} not available`));return}if(a.state===Wn.IDLE){this._dispatch(a,s,e,i);return}this.taskQueue.push(i),this.pendingTasks.set(s,i);return}let o=this._findIdleWorker();if(o){this._dispatch(o,s,e,i);return}this.taskQueue.push(i),this.pendingTasks.set(s,i)})}_dispatch(e,t,s,n){e.state=Wn.BUSY,n.workerId=e.id,this.pendingTasks.set(t,n);let r={taskId:t,...s};e.worker.postMessage(r)}_drainQueue(){for(;this.taskQueue.length>0;){let e=this.taskQueue[0],t;if(e.targetWorkerId!==void 0){if(t=this.workers.get(e.targetWorkerId),!t||t.state!==Wn.IDLE)return}else if(t=this._findIdleWorker(),!t)return;this.taskQueue.shift(),this._dispatch(t,e.taskId,e.task,e)}}_findIdleWorker(){for(let e of this.workers.values())if(e.state===Wn.IDLE)return e;return null}activeWorkerCount(){let e=0;for(let t of this.workers.values())t.state!==Wn.TERMINATED&&e++;return e}idleWorkerCount(){let e=0;for(let t of this.workers.values())t.state===Wn.IDLE&&e++;return e}async resize(e){let t=this.activeWorkerCount();if(e>t){let s=[];for(let n=t;n<e;n++)s.push(this._spawnWorker(n));await Promise.all(s),this.maxWorkers=e}else if(e<t){let s=[];for(let[n,r]of this.workers){if(s.length>=t-e)break;r.state===Wn.IDLE&&s.push(n)}for(let n of s)await this._terminateWorker(n);this.maxWorkers=e}}async _terminateWorker(e){let t=this.workers.get(e);t&&(t.state=Wn.TERMINATED,await t.worker.terminate(),this.workers.delete(e))}async shutdown(){let e=[];for(let t of[...this.workers.keys()])e.push(this._terminateWorker(t));await Promise.all(e);for(let[,t]of this.pendingTasks)t.reject(new Error("Worker pool shut down"));this.pendingTasks.clear(),this.taskQueue.length=0,this.initialized=!1}}}}),QP=J({"src/parallel/worker-messages.ts"(){"use strict"}}),XP={};us(XP,{ParallelDispatch:c(()=>ZP,"ParallelDispatch")});var Dv,Fv,$M,zM,Up,ZP,lj=J({"src/parallel/parallel-dispatch.ts"(){"use strict";Fe(),QP(),Dv=new Set(["INT32","FLOAT64","DATE"]),Fv=new Set(["filterEq","filterLt","filterGt","filterLe","filterGe","filterBetween"]),$M=new Set(["sum","min","max"]),zM=new Set(["scalarAddF64","scalarSubF64","scalarMulF64","scalarDivF64","scalarSubRevF64","scalarDivRevF64","vecAddF64","vecSubF64","vecMulF64","vecDivF64","negF64","widenI32ToF64"]),Up={INT32:4,FLOAT64:8,DATE:4},ZP=class{static{c(this,"ParallelDispatch")}workerPool;regionAllocator;globalDispatch;constructor(e,t,s){this.workerPool=e,this.regionAllocator=t,this.globalDispatch=s}canParallelize(e,t,s){return!this.workerPool||s<ht.parallelThreshold||!Dv.has(t)?!1:!!(Fv.has(e)||$M.has(e))}async filterParallel(e,t,s,n,r){if(!this.canParallelize(s,n,t))return this._filterFallback(s,n,e,r);let i=Up[n],o=this._resolveDataPtr(e,t,i),a=this.workerPool.activeWorkerCount(),u=this._splitRange(t,a).map(h=>({type:"filter",dataOffset:o+h.start*i,count:h.length,operation:s,dataType:n,baseIndex:h.start,...r})),f=await this.workerPool.execute(u);return this._gatherSelectionVectors(f,t)}async aggregateParallel(e,t,s,n){if(!this.canParallelize(s,n,t))return this._aggregateFallback(s,n,e);let r=Up[n],i=this._resolveDataPtr(e,t,r),o=this.workerPool.activeWorkerCount(),l=this._splitRange(t,o).map(f=>({type:"aggregate",dataOffset:i+f.start*r,count:f.length,aggType:s,dataType:n})),u=await this.workerPool.execute(l);return this._mergeAggregates(u,s)}async compoundFilterParallel(e,t,s,n,r){if(!s.every(p=>Fv.has(p.operation))||!this.canParallelize(s[0].operation,r,t))return this._compoundFilterFallback(e,t,s,n,r);let o=Up[r],a=this._resolveDataPtr(e,t,o),l=this.workerPool.activeWorkerCount(),f=this._splitRange(t,l).map(p=>({type:"filter_compound",dataOffset:a+p.start*o,count:p.length,filters:s,combineOp:n,dataType:r,baseIndex:p.start})),h=await this.workerPool.execute(f);return this._gatherSelectionVectors(h,t)}async pipelineParallel(e,t,s,n){let r=Up[s];if(!Dv.has(s)||!this.workerPool||t<ht.parallelThreshold)return this._pipelineFallback(e,t,s,n);let i=this._resolveDataPtr(e,t,r),o=this.workerPool.activeWorkerCount(),l=this._splitRange(t,o).map(h=>({type:"pipeline",dataOffset:i+h.start*r,count:h.length,dataType:s,baseIndex:h.start,stages:n})),u=await this.workerPool.execute(l);return u.some(h=>h.aggregates)?this._mergePipelineAggregates(u):this._gatherSelectionVectors(u,t)}async projectParallel(e,t,s,n={}){if(!this.workerPool||t<ht.parallelThreshold||!zM.has(s))return null;let r=e instanceof Float64Array?8:4;this.regionAllocator.resetStaging();let i=this.regionAllocator.allocStaging(t*r);r===4?new Int32Array(this.regionAllocator.memory.buffer,i,t).set(e.subarray(0,t)):new Float64Array(this.regionAllocator.memory.buffer,i,t).set(e.subarray(0,t));let o;if(n.dataB){let h=n.dataB instanceof Float64Array?8:4;o=this.regionAllocator.allocStaging(t*h),h===4?new Int32Array(this.regionAllocator.memory.buffer,o,t).set(n.dataB.subarray(0,t)):new Float64Array(this.regionAllocator.memory.buffer,o,t).set(n.dataB.subarray(0,t))}let a=this.workerPool.activeWorkerCount(),u=this._splitRange(t,a).map(h=>{let p={type:"project",dataOffset:i+h.start*r,count:h.length,op:s,scalar:n.scalar};if(o!==void 0){let d=n.dataB instanceof Float64Array?8:4;p.dataBOffset=o+h.start*d}return p}),f=await this.workerPool.execute(u);return this._gatherProjectResults(f,t)}_gatherProjectResults(e,t){let s=new Float64Array(t),n=0;for(let r of e)r.resultData&&s.set(r.resultData,n),n+=r.count;return s}_resolveDataPtr(e,t,s){if(e.buffer===this.regionAllocator.memory.buffer)return e.byteOffset;this.regionAllocator.resetStaging();let n=this.regionAllocator.allocStaging(t*s),r=this.regionAllocator.memory;return s===4?new Int32Array(r.buffer,n,t).set(e.subarray(0,t)):new Float64Array(r.buffer,n,t).set(e.subarray(0,t)),n}_splitRange(e,t){let s=Math.max(t,Math.ceil(e/ht.morselSize)),n=Math.ceil(e/s),r=[],i=0;for(;i<e;){let o=Math.min(n,e-i);r.push({start:i,length:o}),i+=o}return r}_gatherSelectionVectors(e,t){let s=0;for(let i of e)s+=i.matchCount;let n=new Uint32Array(s),r=0;for(let i of e)i.matchCount>0&&i.selectionVector&&(n.set(i.selectionVector,r),r+=i.matchCount);return{selectionVector:n,matchCount:s}}_mergeAggregates(e,t){if(e.length===0)return null;if(t==="sum"||t==="count"){let s=0,n=0;for(let r of e)s+=r.result,n+=r.count;return{result:s,count:n}}if(t==="min"){let s=e[0].result,n=0;for(let r of e)r.result<s&&(s=r.result),n+=r.count;return{result:s,count:n}}if(t==="max"){let s=e[0].result,n=0;for(let r of e)r.result>s&&(s=r.result),n+=r.count;return{result:s,count:n}}return null}_mergePipelineAggregates(e){let t=new Map;for(let n of e)if(n.aggregates)for(let r of n.aggregates)t.has(r.aggType)||t.set(r.aggType,[]),t.get(r.aggType).push(r);let s={};for(let[n,r]of t)s[n]=this._mergeAggregates(r,n);return{aggregates:s}}async _compoundFilterFallback(e,t,s,n,r){let i=[];for(let l of s){let u=await this._filterFallback(l.operation,r,e,l);u&&i.push(u.selectionVector)}if(i.length===0)return{selectionVector:new Uint32Array(0),matchCount:0};let o=i[0],a=n==="or"?this._unionSorted:this._intersectSorted;for(let l=1;l<i.length;l++)o=a(o,i[l]);return{selectionVector:o,matchCount:o.length}}async _pipelineFallback(e,t,s,n){let r=null,i={};for(let o of n)if(o.kind==="filter"){let a=await this._filterFallback(o.operation,s,e,o);if(!a)continue;r!==null?r=this._intersectSorted(r,a.selectionVector):r=a.selectionVector}else if(o.kind==="aggregate"){let a=`${o.aggType}${s==="INT32"?"I32":"F64"}`,l=this.globalDispatch.lookup(a,s);if(!l)continue;if(r!==null){let u=this._gatherByIndices(e,r,s),f=await l(u);i[o.aggType]={result:f,count:r.length}}else{let u=await l(e);i[o.aggType]={result:u,count:t}}}else o.kind==="count"&&(i.count={result:r!==null?r.length:t,count:r!==null?r.length:t});return Object.keys(i).length>0?{aggregates:i}:{selectionVector:r||new Uint32Array(0),matchCount:r?r.length:0}}_gatherByIndices(e,t,s){let n=s==="FLOAT64"?Float64Array:Int32Array,r=new n(t.length);for(let i=0;i<t.length;i++)r[i]=e[t[i]];return r}_intersectSorted(e,t){let s=new Uint32Array(Math.min(e.length,t.length)),n=0,r=0,i=0;for(;n<e.length&&r<t.length;)e[n]===t[r]?(s[i++]=e[n],n++,r++):e[n]<t[r]?n++:r++;return s.subarray(0,i)}_unionSorted(e,t){let s=new Uint32Array(e.length+t.length),n=0,r=0,i=0;for(;n<e.length&&r<t.length;)e[n]===t[r]?(s[i++]=e[n],n++,r++):e[n]<t[r]?(s[i++]=e[n],n++):(s[i++]=t[r],r++);for(;n<e.length;)s[i++]=e[n++];for(;r<t.length;)s[i++]=t[r++];return s.subarray(0,i)}async _filterFallback(e,t,s,n){let r=this.globalDispatch.lookup(e,t);if(!r)return null;if(e==="filterBetween"){let o=await r(s,n.low,n.high);return{selectionVector:o,matchCount:o.length}}let i=await r(s,n.value);return{selectionVector:i,matchCount:i.length}}async _aggregateFallback(e,t,s){let n=`${e}${t==="INT32"?"I32":"F64"}`,r=this.globalDispatch.lookup(n,t);return r?{result:await r(s),count:s.length}:null}}}}),bw,cj=J({"src/parallel/morsel-scheduler.ts"(){"use strict";bw=class tD{static{c(this,"_MorselScheduler")}totalUnits;unitsPerMorsel;counter;constructor(t,s){this.totalUnits=t,this.unitsPerMorsel=Math.max(1,s|0),this.counter=new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT))}static attach({counterBuffer:t,totalUnits:s,unitsPerMorsel:n}){let r=Object.create(tD.prototype);return r.totalUnits=s,r.unitsPerMorsel=n,r.counter=new Int32Array(t),r}descriptor(){return{counterBuffer:this.counter.buffer,totalUnits:this.totalUnits,unitsPerMorsel:this.unitsPerMorsel}}next(){let t=Atomics.add(this.counter,0,this.unitsPerMorsel);return t>=this.totalUnits?null:{start:t,end:Math.min(t+this.unitsPerMorsel,this.totalUnits)}}*drain(){let t;for(;(t=this.next())!==null;)yield t}}}});function eD(e){return e instanceof pr?jl(e.indices)&&jl(e.nullBitmap):ri(e.dataType)?jl(e.data)&&jl(e.nullBitmap):jl(e.offsets)&&jl(e.stringBytes)&&jl(e.nullBitmap)}c(eD,"columnIsShared");function uj(e,t){for(let s of t)if(!eD(e.columns[s]))return!1;return!0}c(uj,"chunkIsShared");function fj(e,t,s){let n=Fa(Math.max(t,1)),r=s.acquire(Uint32Array,n);if(r.set(e.nullBitmap.subarray(0,Math.min(n,e.nullBitmap.length))),e instanceof pr){let a=s.acquire(Uint16Array,t);return a.set(e.indices.subarray(0,t)),pr.fromParts({indices:a,reverseDict:e.reverseDict,nullBitmap:r,length:t,hasNulls:e.hasNulls,allocator:s})}if(ri(e.dataType)){let a=s.acquire(Nf(e.dataType),t);return a.set(e.data.subarray(0,t)),Qt.fromParts({dataType:e.dataType,data:a,nullBitmap:r,length:t,hasNulls:e.hasNulls,allocator:s})}let i=s.acquire(Uint32Array,t+1);i.set(e.offsets.subarray(0,t+1));let o=s.acquire(Uint8Array,e.stringBytesUsed);return o.set(e.stringBytes.subarray(0,e.stringBytesUsed)),Qt.fromParts({dataType:e.dataType,offsets:i,stringBytes:o,stringBytesUsed:e.stringBytesUsed,nullBitmap:r,length:t,hasNulls:e.hasNulls,allocator:s})}c(fj,"copyColumn");function UM(e,t,s,n=!1){let r=e.selectionVector?e.flatten():e;if(!n&&uj(r,t))return r;let i=new Set(t),o=r.columns.map((a,l)=>i.has(l)&&(n||!eD(a))?fj(a,r.size,s):a);return new kt(o,r.size)}c(UM,"shareChunk");function hj(e,t){let s=t.map(a=>nD.encode(a)),n=0;for(let a of s)n+=a.length;let r=e.acquire(Uint32Array,Math.max(s.length,1));for(let a=0;a<s.length;a++)r[a]=s[a].length;let i=e.acquire(Uint8Array,Math.max(n,1)),o=0;for(let a of s)i.set(a,o),o+=a.length;return{lengths:r,count:s.length,blob:i,totalBytes:n}}c(hj,"writeDictBlob");function pj(e,t,s){let n=new Array(t),r=0;for(let i=0;i<t;i++){let o=e[i];n[i]=o===0?"":rD.decode(s.subarray(r,r+o)),r+=o}return n}c(pj,"readDictBlob");function VM(e,t,s,n=new Map){let r=[],i=new Map,o=c(f=>{let h=i.get(f);return h===void 0&&(h=r.length,r.push(f),i.set(f,h)),h},"refBuffer"),a=t.length,l=new Uint32Array(e.length*a*St.WORDS),u=new Uint32Array(e.length);for(let f=0;f<e.length;f++){let h=e[f];u[f]=h.size;for(let p=0;p<a;p++){let d=h.columns[t[p]],m=(f*a+p)*St.WORDS,g=Math.min(Fa(Math.max(h.size,1)),d.nullBitmap.length);if(l[m+St.HAS_NULLS]=d.hasNulls?1:0,l[m+St.NULL_BUF]=o(d.nullBitmap.buffer),l[m+St.NULL_OFF]=d.nullBitmap.byteOffset,l[m+St.NULL_WORDS]=g,d instanceof pr){let y=n.get(d.reverseDict);y||(y=hj(s,d.reverseDict),n.set(d.reverseDict,y)),l[m+St.KIND]=bk,l[m+St.TYPE_ID]=xw.get("VARCHAR"),l[m+St.DATA_BUF]=o(d.indices.buffer),l[m+St.DATA_OFF]=d.indices.byteOffset,l[m+St.DATA_LEN]=h.size,l[m+St.AUX_BUF]=o(y.lengths.buffer),l[m+St.AUX_OFF]=y.lengths.byteOffset,l[m+St.AUX_LEN]=y.count,l[m+St.AUX2_BUF]=o(y.blob.buffer),l[m+St.AUX2_OFF]=y.blob.byteOffset,l[m+St.AUX2_LEN]=y.totalBytes}else ri(d.dataType)?(l[m+St.KIND]=wk,l[m+St.TYPE_ID]=xw.get(d.dataType),l[m+St.DATA_BUF]=o(d.data.buffer),l[m+St.DATA_OFF]=d.data.byteOffset,l[m+St.DATA_LEN]=h.size):(l[m+St.KIND]=sD,l[m+St.TYPE_ID]=xw.get(d.dataType),l[m+St.DATA_BUF]=o(d.offsets.buffer),l[m+St.DATA_OFF]=d.offsets.byteOffset,l[m+St.DATA_LEN]=h.size+1,l[m+St.AUX_BUF]=o(d.stringBytes.buffer),l[m+St.AUX_OFF]=d.stringBytes.byteOffset,l[m+St.AUX_LEN]=d.stringBytesUsed)}}return{buffers:r,meta:l,sizes:u,colCount:a}}c(VM,"encodeChunkSet");function dj(e){let t=0;for(let s of e.buffers)t+=s.byteLength;return t}c(dj,"transportBytes");var wk,sD,bk,$v,xw,St,nD,rD,iD,mj=J({"src/parallel/chunk-transport.ts"(){"use strict";Ts(),Gw(),be(),we(),tc(),Zl(),wk=0,sD=1,bk=2,$v=["BOOLEAN","INT32","INT64","FLOAT64","DECIMAL","VARCHAR","DATE","TIMESTAMP"],xw=new Map($v.map((e,t)=>[e,t])),St={KIND:0,TYPE_ID:1,HAS_NULLS:2,DATA_BUF:3,DATA_OFF:4,DATA_LEN:5,NULL_BUF:6,NULL_OFF:7,NULL_WORDS:8,AUX_BUF:9,AUX_OFF:10,AUX_LEN:11,AUX2_BUF:12,AUX2_OFF:13,AUX2_LEN:14,WORDS:16},nD=new TextEncoder,rD=new TextDecoder,iD=class{static{c(this,"ChunkSetReader")}buffers;meta;sizes;colCount;cache;constructor({buffers:e,meta:t,sizes:s,colCount:n}){this.buffers=e,this.meta=t,this.sizes=s,this.colCount=n,this.cache=new Map}get count(){return this.sizes.length}chunk(e){let t=this.cache.get(e);return t||(t=this._decode(e),this.cache.set(e,t)),t}_decode(e){let t=this.sizes[e],s=new Array(this.colCount);for(let n=0;n<this.colCount;n++){let r=(e*this.colCount+n)*St.WORDS,i=this.meta,o=i[r+St.HAS_NULLS]===1,a=new Uint32Array(this.buffers[i[r+St.NULL_BUF]],i[r+St.NULL_OFF],i[r+St.NULL_WORDS]),l=i[r+St.KIND],u=$v[i[r+St.TYPE_ID]],f=this.buffers[i[r+St.DATA_BUF]],h=i[r+St.DATA_OFF],p=i[r+St.DATA_LEN];if(l===bk){let d=new Uint32Array(this.buffers[i[r+St.AUX_BUF]],i[r+St.AUX_OFF],Math.max(i[r+St.AUX_LEN],1)),m=new Uint8Array(this.buffers[i[r+St.AUX2_BUF]],i[r+St.AUX2_OFF],Math.max(i[r+St.AUX2_LEN],1));s[n]=pr.fromParts({indices:new Uint16Array(f,h,p),reverseDict:pj(d,i[r+St.AUX_LEN],m),nullBitmap:a,length:t,hasNulls:o})}else l===wk?s[n]=Qt.fromParts({dataType:u,data:new(Nf(u))(f,h,p),nullBitmap:a,length:t,hasNulls:o}):s[n]=Qt.fromParts({dataType:u,offsets:new Uint32Array(f,h,p),stringBytes:new Uint8Array(this.buffers[i[r+St.AUX_BUF]],i[r+St.AUX_OFF],i[r+St.AUX_LEN]),stringBytesUsed:i[r+St.AUX_LEN],nullBitmap:a,length:t,hasNulls:o})}return new kt(s,t)}}}}),oD={};us(oD,{FragmentPool:c(()=>aD,"FragmentPool")});async function*_j(e){let t=[],s=null;for(let r of e)r.then(i=>{t.push({value:i}),s?.()},i=>{t.push({error:i}),s?.()});let n=0;for(;n<e.length;)for(t.length===0&&(await new Promise(r=>{s=r}),s=null);t.length>0;){let r=t.shift();if(n++,r.error)throw r.error;yield r.value}}c(_j,"completionOrder");function GM(e,t,s){let n=new Map,r=e.map(o=>UM(o,t,s)),i=VM(r,t,s,n);return i.buffers.length>ht.transportMaxBuffers&&(r=e.map(o=>UM(o,t,s,!0)),i=VM(r,t,s,n)),{shared:r,encoded:i}}c(GM,"encodeForTransport");function qM(e){let t=0;for(let r of e)t+=r.length;let s=new Uint32Array(t),n=0;for(let r of e)s.set(r,n),n+=r.length;return s}c(qM,"concatRefs");function jM(e){let t=1;for(;t<e;)t<<=1;return t}c(jM,"nextPowerOfTwo");var aD,wj=J({"src/parallel/fragment-pool.ts"(){"use strict";Fe(),be(),tc(),cj(),mj(),jw(),BP(),QP(),aD=class{static{c(this,"FragmentPool")}workerCount;morselRows;workers;pending;nextReqId;closed;spillSeq;stats;constructor(e=ht.parallelWorkers,t=ht.aggMorselRows){this.workerCount=Math.max(1,e),this.morselRows=Math.max(1,t),this.workers=[],this.pending=new Map,this.nextReqId=1,this.closed=!1}_ensure(){if(this.closed)throw new Error("FragmentPool is closed");for(;this.workers.length<this.workerCount;)this.workers.push(this._spawn(this.workers.length))}_spawn(e){let t=new gj(new URL("./fragment-worker.js",import.meta.url)),s={worker:t,slot:e};return t.on("message",n=>this._onMessage(n)),t.on("error",n=>this._failAll(n)),t.on("exit",n=>{this.closed||(this._failAll(new Error(`Fragment worker exited with code ${n}`)),this.workers[e]=this._spawn(e))}),t.unref(),s}_updateRefs(){let e=this.pending.size>0;for(let t of this.workers)e?t.worker.ref():t.worker.unref()}_settle(e,t){this.pending.delete(e),this._updateRefs(),t()}_onMessage(e){let t=this.pending.get(e.reqId);if(t){if(e.ok===!1){this._settle(e.reqId,()=>t.reject(new Error(e.error)));return}t.replies.push(e),t.replies.length===t.expected&&this._settle(e.reqId,()=>t.resolve(t.replies))}}_failAll(e){for(let[t,s]of this.pending)this._settle(t,()=>s.reject(e))}_request(e,t){let s=this.nextReqId++;return new Promise((n,r)=>{this.pending.set(s,{expected:1,replies:[],resolve:c(i=>n(i[0]),"resolve"),reject:r}),this._updateRefs(),e.worker.postMessage({...t,reqId:s})})}_broadcast(e){let t=this.nextReqId++;return new Promise((s,n)=>{this.pending.set(t,{expected:this.workers.length,replies:[],resolve:s,reject:n}),this._updateRefs();for(let r of this.workers)r.worker.postMessage({...e,reqId:t})})}async runAggregate(e,t,s,n={}){let{byteLimit:r=ht.parallelAggMemoryBytes,spillDir:i=null}=n,o=s.filter(g=>g.size>0);if(o.length===0)return sw(e).finalize();this._ensure();let a=new od,{shared:l,encoded:u}=GM(o,t,a);if(r>0&&dj(u)>r)return null;let f=Math.max(1,Math.round(this.morselRows/We)),h=new bw(l.length,f),p=jM(this.workerCount*Math.max(1,ht.aggRadixMultiplier));this.spillSeq=(this.spillSeq||0)+1;let d=await this._broadcast({type:"aggregate",spec:e,chunks:u,scheduler:h.descriptor(),partitionCount:p,spill:i?{dir:i,tag:`agg${this.spillSeq}`,groupLimit:ht.aggSpillGroups}:null}),m=[];for(let g of d)for(let y of g.spillFiles||[])m.push(y);this.stats={spillFiles:m.length};try{return await this._combineAndFinalize(e,d.map(g=>g.partitions),m,p)}finally{m.length>0&&await Promise.allSettled(m.map(g=>yj.rm(g,{force:!0})))}}async _combineAndFinalize(e,t,s,n){let r=0,i=[];for(let u=0;u<n;u++){let f=[];for(let h of t){let p=h[u];p&&p.length>0&&(f.push(p),r+=p.length)}i.push(f)}if(!(s.length>0||r>=ht.parallelCombineMinGroups&&this.workers.length>1)){let u=sw(e);for(let f of i)for(let h of f)u.absorbPartials(h);return u.finalize()}let a=await Promise.all(i.map((u,f)=>{let h=s.map(p=>({file:p,partition:f}));return u.length===0&&h.length===0?Promise.resolve([]):u.length===1&&h.length===0?Promise.resolve(u[0]):this._request(this.workers[f%this.workers.length],{type:"combine",spec:e,partials:u,spillRefs:h}).then(p=>p.partials)})),l=[];for(let u of a){if(u.length===0)continue;let f=sw(e);f.absorbPartials(u),l.push(...await f.finalize())}return l.length===0?sw(e).finalize():l}async*runJoinStream(e,t,s,n){this._ensure();let r=new od,i=c(w=>{let b=w.chunks.filter(A=>A.size>0),x=w.columnIndexes??(b.length>0?b[0].columns.map((A,S)=>S):[]);return GM(b,x,r)},"prepare"),{shared:o,encoded:a}=i(t),{shared:l,encoded:u}=i(s),f=Math.max(1,Math.round(this.morselRows/We)),h=new bw(o.length,f),p=new bw(l.length,f),d=jM(this.workerCount*Math.max(1,ht.aggRadixMultiplier)),m=await this._broadcast({type:"joinPartition",spec:e,buildChunks:a,probeChunks:u,buildScheduler:h.descriptor(),probeScheduler:p.descriptor(),partitionCount:d}),g=!1;for(let w of m)w.buildNullCount>0&&(g=!0);let y=[];for(let w of m)for(let b of w.probeNullRows)y.push({row:b,key:null});if(y.length>0){let w=RP(y,()=>null,{joinType:e.joinType,buildColCount:e.buildColCount,probeColCount:e.probeColCount,conditionEvaluator:null,hasNullKey:g});for(let b=0;b<w.length;b+=We)yield hk(w.slice(b,b+We),{joinType:e.joinType,buildColCount:e.buildColCount,buildSchema:n.build,probeSchema:n.probe})}if(e.buildPreserved){let w=[];for(let b of m)for(let x of b.buildNullRows||[])w.push(x.concat(new Array(e.probeColCount).fill(null)));for(let b=0;b<w.length;b+=We)yield hk(w.slice(b,b+We),{joinType:e.joinType,buildColCount:e.buildColCount,buildSchema:n.build,probeSchema:n.probe})}let _=[];for(let w=0;w<d;w++){let b=qM(m.map(R=>R.buildPartitions[w])),x=qM(m.map(R=>R.probePartitions[w])),A=x.length>0&&(b.length>0||K6(e.joinType)),S=b.length>0&&e.buildPreserved;if(!A&&!S)continue;let E=this.workers[_.length%this.workers.length];_.push(this._request(E,{type:"joinProbe",spec:e,buildChunks:a,probeChunks:u,buildRefs:b,probeRefs:A?x:new Uint32Array(0),hasNullKey:g,outputTypes:n}))}for await(let w of _j(_)){if(!w.chunks)continue;let b=new iD(w.chunks);for(let x=0;x<b.count;x++){let A=b.chunk(x);A.size>0&&(yield A)}}}async close(){this.closed=!0;let e=this.workers;this.workers=[],this._failAll(new Error("FragmentPool closed")),await Promise.all(e.map(t=>t.worker.terminate()))}}}}),lD={};us(lD,{NodeDescriptor:c(()=>cD,"NodeDescriptor"),NodeRole:c(()=>Wl,"NodeRole"),NodeStatus:c(()=>rn,"NodeStatus")});var Wl,rn,cD,oE=J({"src/distributed/cluster/node-descriptor.ts"(){"use strict";Wl={COORDINATOR:"coordinator",WORKER:"worker",HYBRID:"hybrid"},rn={ALIVE:"alive",SUSPECT:"suspect",DEAD:"dead"},cD=class uD{static{c(this,"_NodeDescriptor")}nodeId;host;port;role;capacity;status;partitions;constructor({nodeId:t,host:s,port:n,role:r,capacity:i,status:o}){this.nodeId=t,this.host=s,this.port=n,this.role=r||Wl.WORKER,this.capacity=i||{cores:1,memoryMb:512},this.status=o||rn.ALIVE,this.partitions=new Set}get address(){return`${this.host}:${this.port}`}isAlive(){return this.status===rn.ALIVE}canExecuteFragments(){return this.status!==rn.DEAD&&(this.role===Wl.WORKER||this.role===Wl.HYBRID)}canCoordinate(){return this.role===Wl.COORDINATOR||this.role===Wl.HYBRID}assignPartition(t){this.partitions.add(t)}removePartition(t){this.partitions.delete(t)}hasPartition(t){return this.partitions.has(t)}toJSON(){return{nodeId:this.nodeId,host:this.host,port:this.port,role:this.role,capacity:this.capacity,status:this.status,partitions:[...this.partitions]}}static fromJSON(t){let s=new uD(t);if(t.partitions)for(let n of t.partitions)s.partitions.add(n);return s}}}}),fD,WM,bj=J({"src/distributed/cluster/heartbeat-monitor.ts"(){"use strict";Fe(),oE(),fD=class{static{c(this,"HeartbeatMonitor")}_windowSize;_threshold;_interval;_windows;_timer;_sendFn;_statusCallback;constructor(e={}){this._windowSize=e.windowSize||ht.phiAccrualWindowSize,this._threshold=e.threshold||ht.phiAccrualThreshold,this._interval=e.intervalMs||ht.heartbeatIntervalMs,this._windows=new Map,this._timer=null,this._sendFn=null,this._statusCallback=null}onSend(e){this._sendFn=e}onStatusChange(e){this._statusCallback=e}start(){this._timer||(this._timer=setInterval(()=>this._tick(),this._interval),this._timer.unref&&this._timer.unref())}stop(){this._timer&&(clearInterval(this._timer),this._timer=null)}recordHeartbeat(e,t){let s=this._windows.get(e);s||(s=new WM(this._windowSize),this._windows.set(e,s)),s.record(t)}phi(e,t){let s=this._windows.get(e);return!s||s.size<2?0:s.phi(t)}getStatus(e,t){let s=this.phi(e,t);return s>=this._threshold*2?rn.DEAD:s>=this._threshold?rn.SUSPECT:rn.ALIVE}removeNode(e){this._windows.delete(e)}_tick(){this._sendFn&&this._sendFn(),this._evaluateAll()}_evaluateAll(){if(!this._statusCallback)return;let e=Date.now();for(let[t,s]of this._windows){if(s.size<2)continue;let n=this.getStatus(t,e);this._statusCallback(t,n)}}},WM=class{static{c(this,"ArrivalWindow")}_maxSize;_buffer;_head;_count;_lastTimestamp;_meanAccum;_varianceAccum;constructor(e){this._maxSize=e,this._buffer=new Float64Array(e),this._head=0,this._count=0,this._lastTimestamp=0,this._meanAccum=0,this._varianceAccum=0}get size(){return this._count}record(e){if(this._lastTimestamp>0){let t=e-this._lastTimestamp;this._addInterval(t)}this._lastTimestamp=e}phi(e){if(this._count<1)return 0;let t=e-this._lastTimestamp,s=this._meanAccum/this._count,n=this._count>1?this._varianceAccum/(this._count-1):s*s,r=Math.sqrt(n);return r===0?t>s*2?16:0:this._computePhi(t,s,r)}_addInterval(e){if(this._count>=this._maxSize){let t=this._head%this._maxSize,s=this._buffer[t];this._removeFromStats(s),this._buffer[t]=e,this._head=(this._head+1)%this._maxSize,this._addToStats(e)}else{let t=(this._head+this._count)%this._maxSize;this._buffer[t]=e,this._count++,this._addToStats(e)}}_addToStats(e){this._meanAccum+=e,this._varianceAccum+=e*e}_removeFromStats(e){this._meanAccum-=e,this._varianceAccum-=e*e}_computePhi(e,t,s){let n=(e-t)/s,i=1-1/(1+Math.exp(-n*1.5976));return i>0?-Math.log10(i):16}}}}),hD={};us(hD,{ClusterManager:c(()=>pD,"ClusterManager")});var pD,xj=J({"src/distributed/cluster/cluster-manager.ts"(){"use strict";oE(),bj(),Fe(),pD=class{static{c(this,"ClusterManager")}_localNode;_nodeMap;_failureCallbacks;_joinCallbacks;_heartbeat;constructor(e,t={}){this._localNode=e,this._nodeMap=new Map,this._nodeMap.set(e.nodeId,e),this._failureCallbacks=[],this._joinCallbacks=[],this._heartbeat=new fD({windowSize:t.phiWindowSize||ht.phiAccrualWindowSize,threshold:t.phiThreshold||ht.phiAccrualThreshold,intervalMs:t.heartbeatIntervalMs||ht.heartbeatIntervalMs}),this._heartbeat.onStatusChange((s,n)=>{this._handleStatusChange(s,n)})}get localNode(){return this._localNode}get nodeCount(){return this._nodeMap.size}addNode(e){let t=this._nodeMap.get(e.nodeId);if(t)return t.host=e.host,t.port=e.port,t.role=e.role,t.capacity=e.capacity,t.status=rn.ALIVE,t;e.status=rn.ALIVE,this._nodeMap.set(e.nodeId,e);for(let s of this._joinCallbacks)s(e);return e}removeNode(e){return e===this._localNode.nodeId?!1:(this._heartbeat.removeNode(e),this._nodeMap.delete(e))}getNode(e){return this._nodeMap.get(e)||null}getAliveNodes(){let e=[];for(let t of this._nodeMap.values())t.status!==rn.DEAD&&e.push(t);return e}getWorkerNodes(){let e=[];for(let t of this._nodeMap.values())t.canExecuteFragments()&&e.push(t);return e}getNodesForPartitions(e){let t=new Map;for(let s of e)for(let n of this._nodeMap.values())if(n.hasPartition(s)&&n.status!==rn.DEAD){t.set(s,n);break}return t}getNodesByPartition(e,t){let s=new Map;for(let n of t){let r=`${e}:${n}`;for(let i of this._nodeMap.values())if(i.hasPartition(r)&&i.status!==rn.DEAD){let o=s.get(i.nodeId);o||(o={node:i,partitionIds:[]},s.set(i.nodeId,o)),o.partitionIds.push(n);break}}return s}recordHeartbeat(e,t){let s=this._nodeMap.get(e);s&&(s.status===rn.DEAD&&(s.status=rn.ALIVE),this._heartbeat.recordHeartbeat(e,t))}onNodeFailure(e){this._failureCallbacks.push(e)}onNodeJoin(e){this._joinCallbacks.push(e)}startMonitoring(){this._heartbeat.start()}stopMonitoring(){this._heartbeat.stop()}_handleStatusChange(e,t){let s=this._nodeMap.get(e);if(!s)return;let n=s.status;if(n!==t&&(s.status=t,t===rn.DEAD&&n!==rn.DEAD))for(let r of this._failureCallbacks)r(s)}assignPartition(e,t){let s=this._nodeMap.get(e);s&&s.assignPartition(t)}snapshot(){let e=[];for(let t of this._nodeMap.values())e.push(t.toJSON());return{localNodeId:this._localNode.nodeId,nodes:e}}}}}),dD={};us(dD,{PartitionMap:c(()=>mD,"PartitionMap")});var mD,Sj=J({"src/distributed/partition/partition-map.ts"(){"use strict";eE(),mD=class{static{c(this,"PartitionMap")}_tables;constructor(){this._tables=new Map}registerTable(e,t,s,n){let r=e.toUpperCase(),i=new Map;if(n)for(let[o,a]of n)i.set(o,Array.isArray(a)?[...a]:[a]);this._tables.set(r,{strategy:t,partitionCount:s,partitionKey:t._partitionKey||null,placements:i})}getTableInfo(e){return this._tables.get(e.toUpperCase())||null}getPartitionCount(e){let t=this.getTableInfo(e);return t?t.partitionCount:0}getPartitionsForNode(e,t){let s=this.getTableInfo(e);if(!s)return[];let n=[];for(let[r,i]of s.placements)i.includes(t)&&n.push(r);return n}getNodesForPartition(e,t){let s=this.getTableInfo(e);return s?s.placements.get(t)||[]:[]}getAllPartitionIds(e){let t=this.getTableInfo(e);return t?Array.from({length:t.partitionCount},(s,n)=>n):[]}isColocated(e,t){let s=this.getTableInfo(e),n=this.getTableInfo(t);if(!s||!n||s.strategy.type!==n.strategy.type||s.strategy.type!==Oa.HASH||s.partitionCount!==n.partitionCount)return!1;for(let r=0;r<s.partitionCount;r++){let i=s.placements.get(r)||[],o=n.placements.get(r)||[];if(!(i.length===0||o.length===0)&&i[0]!==o[0])return!1}return!0}rebalance(e,t){let s=this.getTableInfo(e);if(!s)return new Map;if(t.length===0)return new Map;let r=new Map,i=this._buildHashRing(t,s.partitionCount);for(let o=0;o<s.partitionCount;o++){let a=i.get(o);r.set(o,[a])}return s.placements=r,r}_buildHashRing(e,t){let s=new Map,n=[...e].sort();for(let r=0;r<t;r++){let i=r%n.length;s.set(r,n[i])}return s}computeMovements(e,t,s){let n=[];for(let[r,i]of s){let a=(t.get(r)||[])[0],l=i[0];a&&l&&a!==l&&n.push({partitionId:r,from:a,to:l})}return n}snapshot(){let e={};for(let[t,s]of this._tables)e[t]={strategyType:s.strategy.type,partitionCount:s.partitionCount,placements:Object.fromEntries(s.placements)};return e}}}}),gD={};us(gD,{DistributionAwareJoin:c(()=>yD,"DistributionAwareJoin"),DistributionStrategy:c(()=>on,"DistributionStrategy")});var on,yD,KM,aE=J({"src/distributed/optimizer/distribution-aware-join.ts"(){"use strict";$e(),Ft(),Ke(),Vt(),Fe(),on={COLOCATED:"colocated",BROADCAST_LEFT:"broadcast_left",BROADCAST_RIGHT:"broadcast_right",SHUFFLE:"shuffle"},yD=class extends Be{static{c(this,"DistributionAwareJoin")}_partitionMap;_statisticsMap;_costPerByte;constructor(e,t=new Map){super(),this._partitionMap=e,this._statisticsMap=t,this._costPerByte=ht.networkCostPerByte}get name(){return"DistributionAwareJoin"}apply(e){return new KM(this._partitionMap,this._statisticsMap,this._costPerByte).rewrite(e)}},KM=class extends ze{static{c(this,"DistributionAwareJoinRewriter")}_partitionMap;_statisticsMap;_costPerByte;constructor(e,t,s){super(),this._partitionMap=e,this._statisticsMap=t,this._costPerByte=s}rewriteJoin(e){let t=this.rewriteChildren(e),s=this._extractJoinKeys(t.condition);if(s.leftKeys.length===0)return t._distributionStrategy=on.SHUFFLE,t;let n=this._findScanTable(t.children[0]),r=this._findScanTable(t.children[1]);if(n&&r&&this._areColocated(n,r,s))return t._distributionStrategy=on.COLOCATED,t;let i=t.children[0]._cardinality||this._estimateCardinality(t.children[0]),o=t.children[1]._cardinality||this._estimateCardinality(t.children[1]),a=this._estimateRowWidth(t.children[0]),l=this._estimateRowWidth(t.children[1]),u=this._estimateNodeCount(),f=(i*a+o*l)*this._costPerByte,h=i*a*this._costPerByte*u,p=o*l*this._costPerByte*u,d=[{strategy:on.SHUFFLE,cost:f},{strategy:on.BROADCAST_LEFT,cost:h},{strategy:on.BROADCAST_RIGHT,cost:p}];return i>ht.broadcastThreshold&&(d[1].cost=1/0),o>ht.broadcastThreshold&&(d[2].cost=1/0),this._isRestrictedJoinType(t.joinType)&&(d[1].cost=1/0),d.sort((m,g)=>m.cost-g.cost),t._distributionStrategy=d[0].strategy,t._distributionCost=d[0].cost,t}_areColocated(e,t,s){if(!this._partitionMap)return!1;let n=this._partitionMap.getTableInfo(e),r=this._partitionMap.getTableInfo(t);if(!n||!r||!this._partitionMap.isColocated(e,t)||!n.partitionKey||!r.partitionKey)return!1;let i=n.partitionKey.toUpperCase(),o=r.partitionKey.toUpperCase();for(let a=0;a<s.leftKeys.length;a++){let l=s.leftKeys[a].toUpperCase(),u=s.rightKeys[a].toUpperCase();if(l.endsWith(i)&&u.endsWith(o)||l.endsWith(o)&&u.endsWith(i))return!0}return!1}_extractJoinKeys(e){let t=[],s=[];if(!e)return{leftKeys:t,rightKeys:s};let n=this._splitAnd(e);for(let r of n)r.kind==="BoundBinary"&&r.op==="="&&r.left?.kind==="BoundColumnRef"&&r.right?.kind==="BoundColumnRef"&&(t.push(`${r.left.tableAlias||""}.${r.left.columnName}`.toUpperCase()),s.push(`${r.right.tableAlias||""}.${r.right.columnName}`.toUpperCase()));return{leftKeys:t,rightKeys:s}}_splitAnd(e){return e?e.kind==="BoundBinary"&&e.op==="AND"?[...this._splitAnd(e.left),...this._splitAnd(e.right)]:[e]:[]}_findScanTable(e){return e.type==="Scan"||e.type==="IndexScan"?e.table:e.children&&e.children.length>0?this._findScanTable(e.children[0]):null}_estimateCardinality(e){return e._cardinality?e._cardinality:e.type==="Scan"&&this._statisticsMap.has(e.table)?this._statisticsMap.get(e.table).rowCount:1e3}_estimateRowWidth(e){let t=this._findScanTable(e);return t&&this._statisticsMap.has(t)&&this._statisticsMap.get(t).avgRowWidth||64}_estimateNodeCount(){return Math.max(2,ht.defaultPartitionCount)}_isRestrictedJoinType(e){return e==="LEFT"||e==="SEMI"||e==="ANTI"}}}}),_D={};us(_D,{DECOMPOSABLE_FUNCTIONS:c(()=>Yp,"DECOMPOSABLE_FUNCTIONS"),PartialAggregatePass:c(()=>wD,"PartialAggregatePass")});var Yp,wD,HM,Aj=J({"src/distributed/optimizer/partial-aggregate.ts"(){"use strict";$e(),Ft(),Ke(),Tf(),Yp=new Map([["SUM",{partial:"SUM",final:"SUM"}],["COUNT",{partial:"COUNT",final:"SUM"}],["COUNT_STAR",{partial:"COUNT_STAR",final:"SUM"}],["MIN",{partial:"MIN",final:"MIN"}],["MAX",{partial:"MAX",final:"MAX"}],["AVG",{partial:"AVG_PARTIAL",final:"AVG_FINAL"}]]),wD=class extends Be{static{c(this,"PartialAggregatePass")}get name(){return"PartialAggregate"}apply(e){return e._distributed?new HM().rewrite(e):e}},HM=class extends ze{static{c(this,"PartialAggregateRewriter")}rewriteAggregate(e){let t=this.rewriteChildren(e);if(!this._canDecompose(t.aggregates))return t;let s=this._buildPartialAggregates(t.aggregates),n=this._buildFinalAggregates(t.aggregates),r=$4(t.groupBy,s,t.children[0]);r._cardinality=t._cardinality;let i=(t.groupBy||[]).map(l=>l),o=F4(re.HASH_SHUFFLE,i,0,r),a=z4(t.groupBy,n,s,o);return a._cardinality=t._cardinality,a}_canDecompose(e){return!e||e.length===0?!1:e.every(t=>{let s=this._normalizeFuncName(t);return Yp.has(s)})}_normalizeFuncName(e){return e.isCountStar?"COUNT_STAR":(e.func||e.name||"").toUpperCase()}_buildPartialAggregates(e){return e.map((t,s)=>{let n=this._normalizeFuncName(t),r=Yp.get(n);return n==="AVG"?{...t,func:r.partial,_originalIndex:s,_emitColumns:["_sum","_count"]}:{...t,func:r.partial,_originalIndex:s}})}_buildFinalAggregates(e){return e.map((t,s)=>{let n=this._normalizeFuncName(t),r=Yp.get(n);return n==="AVG"?{...t,func:r.final,_originalIndex:s,_inputColumns:["_sum","_count"]}:{...t,func:r.final,_originalIndex:s}})}}}}),bD={};us(bD,{DistributedSortPass:c(()=>xD,"DistributedSortPass")});var xD,JM,vj=J({"src/distributed/optimizer/distributed-sort.ts"(){"use strict";$e(),Ft(),Ke(),xD=class extends Be{static{c(this,"DistributedSortPass")}get name(){return"DistributedSort"}apply(e){return e._distributed?new JM().rewrite(e):e}},JM=class extends ze{static{c(this,"DistributedSortRewriter")}rewriteSort(e){let t=this.rewriteChildren(e);if(this._hasExchangeBelow(t)){let s=Nw(t.orderKeys,t.children[0]);s._cardinality=t._cardinality;let n=fM(t.orderKeys,null,s);return n._cardinality=t._cardinality,n}return t}rewriteTopN(e){let t=this.rewriteChildren(e);if(this._hasExchangeBelow(t)){let s={...t,children:[t.children[0]]},n=fM(t.orderKeys,t.count,s);return n._cardinality=Math.min(t.count||1/0,t._cardinality||1/0),{type:"Limit",count:t.count,offset:t.offset||0,children:[n]}}return t}_hasExchangeBelow(e){let t=[...e.children||[]];for(;t.length>0;){let s=t.pop();if(s.type==="Exchange"||s.type==="PartialAggregate")return!0;if(s.children)for(let n of s.children)t.push(n)}return!1}}}}),SD,kj=J({"src/distributed/planner/exchange-placement.ts"(){"use strict";Tf(),aE(),Fe(),SD=class{static{c(this,"ExchangePlacement")}_partitionMap;_statisticsMap;constructor(e,t){this._partitionMap=e,this._statisticsMap=t||new Map}determineJoinExchange(e){switch(e._distributionStrategy||on.SHUFFLE){case on.COLOCATED:return{left:{type:re.PASSTHROUGH},right:{type:re.PASSTHROUGH}};case on.BROADCAST_LEFT:return{left:{type:re.BROADCAST},right:{type:re.PASSTHROUGH}};case on.BROADCAST_RIGHT:return{left:{type:re.PASSTHROUGH},right:{type:re.BROADCAST}};case on.SHUFFLE:default:{let s=this._extractShuffleKeys(e);return{left:{type:re.HASH_SHUFFLE,keys:s.leftKeys,partitionCount:ht.defaultPartitionCount},right:{type:re.HASH_SHUFFLE,keys:s.rightKeys,partitionCount:ht.defaultPartitionCount}}}}}determineAggregateExchange(e){let t=e.groupBy||[];return t.length===0?{type:re.GATHER}:{type:re.HASH_SHUFFLE,keys:t,partitionCount:ht.defaultPartitionCount}}determineSortExchange(e){return{type:re.GATHER,ordered:!0,orderKeys:e.orderKeys}}_extractShuffleKeys(e){let t=[],s=[],n=e.condition;if(!n)return{leftKeys:t,rightKeys:s};let r=this._splitAnd(n);for(let i of r)i.op==="="&&i.left?.kind==="ColumnRef"&&i.right?.kind==="ColumnRef"&&(t.push(i.left),s.push(i.right));return{leftKeys:t,rightKeys:s}}_splitAnd(e){return e?e.op==="AND"?[...this._splitAnd(e.left),...this._splitAnd(e.right)]:[e]:[]}}}}),AD,Ej=J({"src/distributed/partition/partition-pruner.ts"(){"use strict";Vt(),eE(),AD=class{static{c(this,"PartitionPruner")}prune(e,t,s){let n=s.getTableInfo(e);if(!n)return this._allPartitions(0);let r=this._allPartitions(n.partitionCount);return!t||!n.partitionKey?r:this._prunePredicate(t,n)||r}_prunePredicate(e,t){if(!e)return null;if(e.kind==="BoundBinary"&&e.op==="AND"){let s=this._prunePredicate(e.left,t),n=this._prunePredicate(e.right,t);return s&&n?this._intersect(s,n):s||n}if(e.kind==="BoundBinary"&&e.op==="OR"){let s=this._prunePredicate(e.left,t),n=this._prunePredicate(e.right,t);return s&&n?this._union(s,n):null}return e.kind==="BoundBinary"&&e.op==="="?this._pruneEquality(e,t):e.kind==="BoundBinary"&&(e.op==="<"||e.op==="<="||e.op===">"||e.op===">=")?this._pruneRange(e,t):e.kind==="BoundBetween"?this._pruneBetween(e,t):e.kind==="BoundInList"?this._pruneInList(e,t):null}_pruneEquality(e,t){let s=this._extractPartitionColumn(e,t);if(!s)return null;let n=s.side==="left"?e.right:e.left;if(n.kind!=="BoundLiteral")return null;if(t.strategy.type===Oa.HASH){let r=t.strategy.partitionFor(n.value,t.partitionCount);return new Set([r])}if(t.strategy.type===Oa.RANGE){let r=t.strategy.partitionFor(n.value);return new Set([r])}return null}_pruneRange(e,t){if(t.strategy.type!==Oa.RANGE)return null;let s=this._extractPartitionColumn(e,t);if(!s)return null;let n=s.side==="left"?e.right:e.left;if(n.kind!=="BoundLiteral")return null;let i=t.strategy.boundaries.length+1,o=n.value,a=new Set,u=s.side==="left"?e.op:this._flipOp(e.op);if(u==="<"||u==="<="){let f=t.strategy.partitionFor(o);for(let h=0;h<=Math.min(f,i-1);h++)a.add(h)}else{let f=t.strategy.partitionFor(o);for(let h=f;h<i;h++)a.add(h)}return a}_pruneBetween(e,t){if(t.strategy.type!==Oa.RANGE||e.kind!=="BoundBetween"||!e.expr||e.expr.kind!=="BoundColumnRef"||!this._isPartitionKey(e.expr,t)||!e.low||!e.high||e.low.kind!=="BoundLiteral"||e.high.kind!=="BoundLiteral")return null;let s=t.strategy.partitionFor(e.low.value),n=t.strategy.partitionFor(e.high.value),r=new Set;for(let i=s;i<=n;i++)r.add(i);return r}_pruneInList(e,t){if(e.kind!=="BoundInList"||!e.expr||e.expr.kind!=="BoundColumnRef"||!this._isPartitionKey(e.expr,t))return null;let s=e.values;if(!s||!s.every(r=>r.kind==="BoundLiteral"))return null;let n=new Set;for(let r of s)t.strategy.type===Oa.HASH?n.add(t.strategy.partitionFor(r.value,t.partitionCount)):t.strategy.type===Oa.RANGE&&n.add(t.strategy.partitionFor(r.value));return n}_extractPartitionColumn(e,t){let s=e.left,n=e.right;return s&&s.kind==="BoundColumnRef"&&this._isPartitionKey(s,t)?{col:s,side:"left"}:n&&n.kind==="BoundColumnRef"&&this._isPartitionKey(n,t)?{col:n,side:"right"}:null}_isPartitionKey(e,t){return e.columnName.toUpperCase()===t.partitionKey.toUpperCase()}_flipOp(e){switch(e){case"<":return">";case"<=":return">=";case">":return"<";case">=":return"<=";default:return e}}_intersect(e,t){let s=new Set;for(let n of e)t.has(n)&&s.add(n);return s}_union(e,t){let s=new Set(e);for(let n of t)s.add(n);return s}_allPartitions(e){let t=new Set;for(let s=0;s<e;s++)t.add(s);return t}}}}),vD,Nj=J({"src/distributed/planner/distributed-planner.ts"(){"use strict";Ft(),Tf(),aE(),kj(),Ej(),vD=class{static{c(this,"DistributedPlanner")}_partitionMap;_clusterManager;_exchangePlacement;_pruner;_catalog;_fragments;constructor(e,t,s,n=null){this._partitionMap=e,this._clusterManager=t,this._exchangePlacement=new SD(e,s),this._pruner=new AD,this._catalog=n,this._fragments=[]}fragmentize(e){DP(),this._fragments=[];let t=this._clusterManager.localNode.nodeId,s={coordinatorId:t},n=this._processNode(e,s),r=this._placeGathers(n.plan),i=r.plan,o=(n.exchangeInputs||[]).concat(r.exchangeInputs),a=new Pi({planRoot:i,targetNodes:[t],exchangeInputs:o,outputPartitioning:null,estimatedCardinality:e._cardinality||0});return this._fragments.push(a),new tE(this._fragments,a.fragmentId)}_placeGathers(e){if(e.type==="Join"&&(e._distributionStrategy===on.BROADCAST_LEFT||e._distributionStrategy===on.BROADCAST_RIGHT)){let r=this._buildBroadcastJoin(e);if(r)return r}if(e.type==="Join"&&this._isShufflePushableJoin(e)){let r=this._buildShuffleJoin(e);if(r)return r}if(e.type==="Join"&&this._isColocatedPushableJoin(e)){let r=this._findPartitionedScan(e),i=this._pruner.prune(r.table,null,this._partitionMap),o=this._selectNodesForPartitions(r.table,i,this._clusterManager.getWorkerNodes());if(o.length>0){let a=[],l=[];for(let u of o){let f=new Pi({planRoot:e,targetNodes:[u],exchangeInputs:[],outputPartitioning:{exchangeType:"gather",partitionCount:o.length},estimatedCardinality:Math.ceil((e._cardinality||0)/o.length)});this._fragments.push(f),a.push(f.fragmentId),l.push({sourceFragmentId:f.fragmentId,exchangeType:re.GATHER})}return{plan:Ta(a,this._joinOutputSchema(e)),exchangeInputs:l}}}if(this._isPushableSubtree(e)){let r=this._findPartitionedScan(e),i=this._pruner.prune(r.table,null,this._partitionMap),o=this._selectNodesForPartitions(r.table,i,this._clusterManager.getWorkerNodes());if(o.length===0)return{plan:e,exchangeInputs:[]};let a=[],l=[];for(let u of o){let f=new Pi({planRoot:e,targetNodes:[u],exchangeInputs:[],outputPartitioning:{exchangeType:"gather",partitionCount:o.length},estimatedCardinality:Math.ceil((e._cardinality||0)/o.length)});this._fragments.push(f),a.push(f.fragmentId),l.push({sourceFragmentId:f.fragmentId,exchangeType:re.GATHER})}return{plan:Ta(a,this._deriveSubtreeSchema(e)),exchangeInputs:l}}let t=cs(e);if(t.length===0)return{plan:e,exchangeInputs:[]};let s=[],n=[];for(let r of t){let i=this._placeGathers(r);s.push(i.plan),n=n.concat(i.exchangeInputs)}return{plan:Tn(e,s),exchangeInputs:n}}_isPushableSubtree(e){let t=new Set(["Scan","IndexScan","Filter","Project"]),s=!1,n=[e];for(;n.length>0;){let r=n.pop();if(!r||!t.has(r.type))return!1;(r.type==="Scan"||r.type==="IndexScan")&&this._partitionMap.getTableInfo(r.table)&&(s=!0);for(let i of cs(r))n.push(i)}return s}_findPartitionedScan(e){let t=[e];for(;t.length>0;){let s=t.pop();if((s.type==="Scan"||s.type==="IndexScan")&&this._partitionMap.getTableInfo(s.table))return s;for(let n of cs(s))t.push(n)}return null}_buildBroadcastJoin(e){if(e.joinType!=="INNER")return null;let t=e._distributionStrategy===on.BROADCAST_LEFT,s=e.children[0],n=e.children[1];if(!this._isScanFilterSubtree(s)||!this._isScanFilterSubtree(n))return null;let r=t?s:n,i=t?n:s,o=this._findPartitionedScan(r),a=this._findPartitionedScan(i),l=this._clusterManager.getWorkerNodes(),u=this._selectNodesForPartitions(o.table,this._pruner.prune(o.table,null,this._partitionMap),l),f=this._selectNodesForPartitions(a.table,this._pruner.prune(a.table,null,this._partitionMap),l);if(u.length===0||f.length===0)return null;let h=[];for(let _ of u){let w=new Pi({planRoot:r,targetNodes:[_],exchangeInputs:[],outputPartitioning:{exchangeType:"broadcast",targetNodes:f},estimatedCardinality:0});this._fragments.push(w),h.push(w.fragmentId)}let p=h.map(_=>({sourceFragmentId:_,exchangeType:re.BROADCAST})),d=Ta(h,this._deriveSubtreeSchema(r)),m=Tn(e,t?[d,i]:[i,d]),g=[],y=[];for(let _ of f){let w=new Pi({planRoot:m,targetNodes:[_],exchangeInputs:p,outputPartitioning:{exchangeType:"gather",partitionCount:f.length},estimatedCardinality:0});this._fragments.push(w),y.push(w.fragmentId),g.push({sourceFragmentId:w.fragmentId,exchangeType:re.GATHER})}return{plan:Ta(y,this._joinOutputSchema(e)),exchangeInputs:g}}_isShufflePushableJoin(e){if(e._distributionStrategy!==on.SHUFFLE)return!1;let t=e.joinType;return t!=="INNER"&&t!=="LEFT"&&t!=="RIGHT"&&t!=="FULL"?!1:this._isScanFilterSubtree(e.children[0])&&this._isScanFilterSubtree(e.children[1])}_buildShuffleJoin(e){let t=e.children[0],s=e.children[1],n=this._deriveSubtreeSchema(t),r=this._deriveSubtreeSchema(s),i=this._extractShuffleKeyIndices(e.condition,n,r);if(!i||i.leftIdx.length===0)return null;let o=this._findPartitionedScan(t),a=this._findPartitionedScan(s),l=this._clusterManager.getWorkerNodes(),u=this._selectNodesForPartitions(o.table,this._pruner.prune(o.table,null,this._partitionMap),l),f=this._selectNodesForPartitions(a.table,this._pruner.prune(a.table,null,this._partitionMap),l);if(u.length===0||f.length===0)return null;let h=[...new Set([...u,...f])],p=h.length,d=c((A,S,E)=>{let R=[];for(let v of S){let k=new Pi({planRoot:A,targetNodes:[v],exchangeInputs:[],outputPartitioning:{exchangeType:"hash_shuffle",partitionCount:p,targetNodes:h,keyColumns:E},estimatedCardinality:0});this._fragments.push(k),R.push(k.fragmentId)}return R},"mkShuffleFrags"),m=d(t,u,i.leftIdx),g=d(s,f,i.rightIdx),y=m.map(A=>({sourceFragmentId:A,exchangeType:re.HASH_SHUFFLE})),_=g.map(A=>({sourceFragmentId:A,exchangeType:re.HASH_SHUFFLE})),w=Tn(e,[Ta(m,n),Ta(g,r)]),b=[],x=[];for(let A of h){let S=new Pi({planRoot:w,targetNodes:[A],exchangeInputs:[...y,..._],outputPartitioning:{exchangeType:"gather",partitionCount:p},estimatedCardinality:0});this._fragments.push(S),x.push(S.fragmentId),b.push({sourceFragmentId:S.fragmentId,exchangeType:re.GATHER})}return{plan:Ta(x,this._joinOutputSchema(e)),exchangeInputs:b}}_extractShuffleKeyIndices(e,t,s){if(!e)return null;let n=[],r=[],i=this._splitAnd(e);for(let o of i){if(o.op!=="=")continue;let a=o.left,l=o.right;if(a?.kind!=="BoundColumnRef"||l?.kind!=="BoundColumnRef")continue;let u=this._colIndexInSchema(a,t),f=this._colIndexInSchema(l,s);if((u<0||f<0)&&(u=this._colIndexInSchema(l,t),f=this._colIndexInSchema(a,s)),u<0||f<0)return null;n.push(u),r.push(f)}return n.length>0?{leftIdx:n,rightIdx:r}:null}_splitAnd(e){return e?e.op==="AND"?[...this._splitAnd(e.left),...this._splitAnd(e.right)]:[e]:[]}_colIndexInSchema(e,t){let s=(e.tableAlias||"").toUpperCase(),n=(e.columnName||e.name||"").toUpperCase();for(let r=0;r<t.length;r++)if(t[r].name.toUpperCase()===n&&(t[r].tableAlias||"").toUpperCase()===s)return r;if(s)return-1;for(let r=0;r<t.length;r++)if(t[r].name.toUpperCase()===n)return r;return-1}_isColocatedPushableJoin(e){if(e._distributionStrategy!==on.COLOCATED)return!1;let t=e.joinType;return t!=="INNER"&&t!=="LEFT"&&t!=="RIGHT"&&t!=="FULL"?!1:this._isScanFilterSubtree(e.children[0])&&this._isScanFilterSubtree(e.children[1])}_isScanFilterSubtree(e){let t=new Set(["Scan","IndexScan","Filter"]),s=!1,n=[e];for(;n.length>0;){let r=n.pop();if(!r||!t.has(r.type))return!1;(r.type==="Scan"||r.type==="IndexScan")&&this._partitionMap.getTableInfo(r.table)&&(s=!0);for(let i of cs(r))n.push(i)}return s}_joinOutputSchema(e){let t=e._buildSide==="right"?e.children[1]:e.children[0],s=e._buildSide==="right"?e.children[0]:e.children[1];return[...this._deriveSubtreeSchema(t),...this._deriveSubtreeSchema(s)]}_deriveSubtreeSchema(e){return e.type==="Filter"?this._deriveSubtreeSchema(e.children[0]):e.type==="Project"?e.expressions.map((t,s)=>({name:t?.outputName||t?.alias||t?.name||t?.columnName||`col${s}`,dataType:t?.dataType||t?.resultType||"VARCHAR",tableAlias:""})):this._scanOutputSchema(e)}_scanOutputSchema(e){if(!this._catalog)return[];let t=this._catalog.getTableStorage(e.table);if(!t||typeof t.getSchema!="function")return[];let s=t.getSchema(),n=e.alias||e.table,r=s;return e.columns&&e.columns.length>0&&e.columns.length<s.length&&(r=e.columns.map(i=>s.find(o=>o.name.toUpperCase()===(i.name||i).toUpperCase())).filter(Boolean)),r.map(i=>({name:i.name,dataType:i.dataType,tableAlias:n}))}_processNode(e,t){switch(e.type){case"Scan":case"IndexScan":return this._processScan(e,t);case"Join":return this._processJoin(e,t);case"PartialAggregate":return this._processPartialAggregate(e,t);case"FinalAggregate":return this._processFinalAggregate(e,t);case"Exchange":return this._processExchange(e,t);case"MergeExchange":return this._processMergeExchange(e,t);default:return this._processGeneric(e,t)}}_processScan(e,t){let s=e.table;if(!this._partitionMap.getTableInfo(s))return{plan:e,exchangeInputs:[],workerNodes:[]};let r=this._findFilterAbove(e),i=this._pruner.prune(s,r,this._partitionMap),o=this._clusterManager.getWorkerNodes(),a=this._selectNodesForPartitions(s,i,o);return{plan:e,exchangeInputs:[],workerNodes:a}}_processJoin(e,t){let s=this._processNode(e.children[0],t),n=this._processNode(e.children[1],t),r=this._exchangePlacement.determineJoinExchange(e),i=[...s.exchangeInputs,...n.exchangeInputs];if(r.left.type!==re.PASSTHROUGH)for(let a of s.exchangeInputs)a.exchangeType=r.left.type,r.left.keys&&(a.keys=r.left.keys);if(r.right.type!==re.PASSTHROUGH)for(let a of n.exchangeInputs)a.exchangeType=r.right.type,r.right.keys&&(a.keys=r.right.keys);return{plan:{...e,children:[s.plan,n.plan]},exchangeInputs:i,workerNodes:[]}}_processPartialAggregate(e,t){let s=this._processNode(e.children[0],t);return{plan:{...e,children:[s.plan]},exchangeInputs:s.exchangeInputs,workerNodes:s.workerNodes||[]}}_processFinalAggregate(e,t){let s=this._processNode(e.children[0],t);return{plan:{...e,children:[s.plan]},exchangeInputs:s.exchangeInputs,workerNodes:[]}}_processExchange(e,t){let s=this._processNode(e.children[0],t),n=s.workerNodes||[];if(n.length===0)return{plan:s.plan,exchangeInputs:s.exchangeInputs,workerNodes:[]};let r=s.plan,i=[],o=[];for(let l of n){let u=new Pi({planRoot:r,targetNodes:[l],exchangeInputs:[],outputPartitioning:{exchangeType:e.exchangeType||"gather",partitionCount:n.length},estimatedCardinality:Math.ceil((e._cardinality||0)/n.length)});this._fragments.push(u),o.push(u.fragmentId),i.push({sourceFragmentId:u.fragmentId,exchangeType:re.GATHER})}return{plan:Ta(o,[]),exchangeInputs:i,workerNodes:[]}}_processMergeExchange(e,t){let s=this._processNode(e.children[0],t),n=s.exchangeInputs.map(i=>({...i,exchangeType:re.GATHER,ordered:!0,orderKeys:e.orderKeys}));return{plan:{...e,children:[s.plan]},exchangeInputs:n,workerNodes:[]}}_processGeneric(e,t){let s=cs(e);if(s.length===0)return{plan:e,exchangeInputs:[],workerNodes:[]};let n=s.map(l=>this._processNode(l,t)),r=[],i=[],o=[];for(let l of n)i.push(l.plan),r.push(...l.exchangeInputs),l.workerNodes&&l.workerNodes.length>0&&(o=l.workerNodes);return{plan:{...e,children:i},exchangeInputs:r,workerNodes:o}}_findFilterAbove(e){return null}_selectNodesForPartitions(e,t,s){if(s.length===0)throw new Error(`Table "${e}" is partitioned but no workers are available to scan it`);let n=new Set;for(let r of t){let i=this._partitionMap.getNodesForPartition(e,r);i.length>0&&n.add(i[0])}return n.size===0?s.map(r=>r.nodeId):[...n]}}}}),kD,Rj=J({"src/distributed/execution/fragment-executor.ts"(){"use strict";UP(),jk(),Ow(),Ow(),kD=class{static{c(this,"FragmentExecutor")}_catalog;_tempManager;_transport;_activeFragments;_localExecutor;constructor(e,t,s,n,r={}){this._catalog=e,this._tempManager=t,this._transport=s,this._activeFragments=new Map,this._localExecutor=new rE(e,t,n)}async execute(e,t){let s=e.fragmentId,n={cancelled:!1};this._activeFragments.set(s,{fragment:e,cancelToken:n});try{e.markRunning();let r=await this._setupReceivers(e),i=t?await this._setupSender(t):null,o=new Ww(!1);if(await o.init(),await this._executePlan(e.planRoot,o,r,n),i&&!n.cancelled){for(let a of o.chunks)await i.consume(a);await i.finalize()}return e.markCompleted(),{sink:o,sender:i,receivers:r}}catch(r){throw e.markFailed(r),r}finally{this._activeFragments.delete(s),this._cleanupReceivers(e)}}async cancel(e){let t=this._activeFragments.get(e);t&&(t.cancelToken.cancelled=!0,t.fragment.markCancelled())}isRunning(e){return this._activeFragments.has(e)}async _executePlan(e,t,s,n){let r=this._localExecutor;r._exchangeReceivers=s;let i=await r.buildPipeline(e);r._exchangeReceivers=null;let{PipelineGraph:o}=await Promise.resolve().then(()=>(ud(),Wk)),{TaskScheduler:a}=await Promise.resolve().then(()=>(Kw(),Hk)),l=new o,u=l.createPipeline(t);i.register(l,u,t),await new a().schedule(l)}async _setupReceivers(e){let t=new Map;for(let s of e.exchangeInputs){let n=`frag-${s.sourceFragmentId}-output`,r=new nE(this._transport,["_source_"],{channelId:n});await r.init(),t.set(s.sourceFragmentId,r)}return t}async _setupSender(e){let t=e.keyExtractors||[];e.keyColumns&&e.keyColumns.length>0&&(t=e.keyColumns.map(n=>(r,i)=>r.columns[n].get(i)));let s=new sE(this._transport,e.targetNodes,{exchangeType:e.exchangeType,keyExtractors:t,partitionCount:e.partitionCount,channelId:e.channelId});return await s.init(),s}_cleanupReceivers(e){for(let t of e.exchangeInputs){let s=`frag-${t.sourceFragmentId}-to-${e.fragmentId}`;this._transport.removeChunkListener(s)}}}}}),ED={};us(ED,{QueryCoordinator:c(()=>ND,"QueryCoordinator")});var ND,Bj=J({"src/distributed/execution/coordinator.ts"(){"use strict";Fe(),Tf(),Nj(),Rj(),jk(),VP(),ND=class{static{c(this,"QueryCoordinator")}_engine;_clusterManager;_partitionMap;_transport;_activeQueries;_fragmentExecutor;_nextQueryId;constructor(e,t,s,n){this._engine=e,this._clusterManager=t,this._partitionMap=s,this._transport=n,this._activeQueries=new Map,this._fragmentExecutor=new kD(e.catalog,e.tempManager,n,e.storageBackend),this._nextQueryId=1,this._setupFragmentHandler()}async execute(e){let t=this._nextQueryId++,s=Date.now(),n=ht.coordinatorTimeoutMs;try{let r=await this._engine.compile(e);if(r.ddl)return this._engine.executeDDL(r.ddl);let{plan:i,outputColumns:o,cteMap:a}=r;i._distributed=!0;let l=this._reoptimize(i),f=new vD(this._partitionMap,this._clusterManager,this._engine.precomputedStats,this._engine.catalog).fragmentize(l);this._activeQueries.set(t,{fragmentPlan:f,startTime:s,status:"running"});let h=f.getRootFragment(),p=await this._fragmentExecutor._setupReceivers(h);await this._executeFragmentPlan(f,t,n);let d=await this._executeRootFragmentWithReceivers(h,p),m=o.map(y=>y.name);return{rows:await new bf(m,d.sink).toArray(),columns:m}}finally{this._activeQueries.delete(t)}}async cancel(e){let t=this._activeQueries.get(e);if(!t)return;t.status="cancelled";let{fragmentPlan:s}=t,n=s.fragments.filter(r=>r.state===qs.RUNNING||r.state===qs.DISPATCHED).map(r=>this._cancelFragment(r));await Promise.all(n)}async _executeFragmentPlan(e,t,s){let r=e.topologicalOrder().filter(o=>o!==e.getRootFragment()),i=this._groupByDependencyLevel(r,e);for(let o of i){if(Date.now()-this._activeQueries.get(t).startTime>s)throw new Error(`Query ${t} timed out after ${s}ms`);if(this._activeQueries.get(t)?.status==="cancelled")throw new Error(`Query ${t} was cancelled`);await Promise.all(o.map(l=>this._dispatchFragment(l,e)))}}_groupByDependencyLevel(e,t){let s=new Set,n=[],r=new Set(e.map(i=>i.fragmentId));for(;r.size>0;){let i=[];for(let o of e){if(!r.has(o.fragmentId))continue;(o.exchangeInputs||[]).map(u=>u.sourceFragmentId).every(u=>s.has(u)||!r.has(u))&&i.push(o)}if(i.length===0)break;for(let o of i)r.delete(o.fragmentId),s.add(o.fragmentId);n.push(i)}return n}async _dispatchFragment(e,t){let s=ht.fragmentRetryLimit;for(;;){let n=this._selectTargetNode(e);e.markDispatched(n);try{n===this._clusterManager.localNode.nodeId?await this._executeLocalFragment(e,t):await this._executeRemoteFragment(e,n);return}catch(r){if(e.markFailed(r),!e.canRetry(s))throw new Error(`Fragment ${e.fragmentId} failed after ${s} retries: ${r.message}`);let i=n,o=this._clusterManager.getWorkerNodes().filter(a=>a.nodeId!==i);if(o.length===0)throw new Error(`No available nodes to retry fragment ${e.fragmentId}`);e.state=qs.PENDING,e.targetNodes=o.map(a=>a.nodeId)}}}async _executeLocalFragment(e,t){let s=this._buildOutputConfig(e);await this._fragmentExecutor.execute(e,s)}async _executeRemoteFragment(e,t){let s=e.toJSON();s.coordinatorNodeId=this._clusterManager.localNode.nodeId,await this._transport.sendFragment(t,s),await this._waitForFragmentCompletion(e)}async _executeRootFragment(e){return this._fragmentExecutor.execute(e,null)}async _executeRootFragmentWithReceivers(e,t){let s=e.fragmentId,n={cancelled:!1};e.markRunning();let r=new Ww(!1);await r.init();let i=this._fragmentExecutor._localExecutor;i._exchangeReceivers=t;let o=await i.buildPipeline(e.planRoot);i._exchangeReceivers=null;let{PipelineGraph:a}=await Promise.resolve().then(()=>(ud(),Wk)),{TaskScheduler:l}=await Promise.resolve().then(()=>(Kw(),Hk)),u=new a,f=u.createPipeline(r);return o.register(u,f,r),await new l().schedule(u),e.markCompleted(),{sink:r}}async _waitForFragmentCompletion(e){let t=ht.coordinatorTimeoutMs,s=Date.now();for(;e.state!==qs.COMPLETED&&e.state!==qs.FAILED;){if(Date.now()-s>t)throw new Error(`Fragment ${e.fragmentId} timed out`);await new Promise(n=>setTimeout(n,50))}if(e.state===qs.FAILED)throw e.error||new Error(`Fragment ${e.fragmentId} failed`)}async _cancelFragment(e){if(e.markCancelled(),e.assignedNode)try{await this._transport.sendControl(e.assignedNode,{type:"cancel_fragment",fragmentId:e.fragmentId})}catch{}}_selectTargetNode(e){if(e.targetNodes.length===1)return e.targetNodes[0];let t=e.targetNodes.filter(s=>{let n=this._clusterManager.getNode(s);return n&&n.canExecuteFragments()});return t.length===0?this._clusterManager.localNode.nodeId:t[e.fragmentId%t.length]}_buildOutputConfig(e,t=this._clusterManager.localNode.nodeId){if(!e.outputPartitioning)return null;let s=e.outputPartitioning;return{targetNodes:s.targetNodes&&s.targetNodes.length>0?s.targetNodes:[t],exchangeType:s.exchangeType||"gather",partitionCount:s.partitionCount,keyColumns:s.keyColumns,channelId:`frag-${e.fragmentId}-output`}}_reoptimize(e){return this._engine.optimize(e)}_setupFragmentHandler(){this._transport.onFragmentReceived(async e=>{let t=e.coordinatorNodeId||"coordinator";try{let{Fragment:s}=await Promise.resolve().then(()=>(Tf(),PP)),n=Object.assign(new s({planRoot:e.planRoot}),e);await this._fragmentExecutor.execute(n,this._buildOutputConfig(n,t)),await this._transport.sendControl(t,{type:"fragment_completed",fragmentId:e.fragmentId,nodeId:this._clusterManager.localNode.nodeId})}catch(s){try{await this._transport.sendControl(t,{type:"fragment_failed",fragmentId:e.fragmentId,error:s.message})}catch{}}}),this._transport.onControlMessage(e=>{e.type==="fragment_completed"?this._handleFragmentCompleted(e.fragmentId):e.type==="fragment_failed"&&this._handleFragmentFailed(e.fragmentId,e.error)})}_handleFragmentCompleted(e){for(let t of this._activeQueries.values()){let s=t.fragmentPlan.fragments.find(n=>n.fragmentId===e);if(s){s.markCompleted();return}}}_handleFragmentFailed(e,t){for(let s of this._activeQueries.values()){let n=s.fragmentPlan.fragments.find(r=>r.fragmentId===e);if(n){n.markFailed(new Error(t||"Remote execution failed"));return}}}getActiveQueryCount(){return this._activeQueries.size}getQueryStatus(e){let t=this._activeQueries.get(e);return t?{queryId:e,status:t.status,elapsed:Date.now()-t.startTime,fragments:t.fragmentPlan.fragments.map(s=>({fragmentId:s.fragmentId,state:s.state,assignedNode:s.assignedNode}))}:null}}}}),RD,Cj=J({"src/distributed/transport/transport.ts"(){"use strict";RD=class{static{c(this,"Transport")}async start(){throw new Error("Transport subclass must implement start()")}async stop(){throw new Error("Transport subclass must implement stop()")}async sendChunk(e,t,s){throw new Error("Transport subclass must implement sendChunk()")}onChunkReceived(e,t){throw new Error("Transport subclass must implement onChunkReceived()")}removeChunkListener(e){throw new Error("Transport subclass must implement removeChunkListener()")}async sendFragment(e,t){throw new Error("Transport subclass must implement sendFragment()")}async sendControl(e,t){throw new Error("Transport subclass must implement sendControl()")}onControlMessage(e){throw new Error("Transport subclass must implement onControlMessage()")}onFragmentReceived(e){throw new Error("Transport subclass must implement onFragmentReceived()")}registerNode(e,t,s){throw new Error("Transport subclass must implement registerNode()")}}}}),BD={};us(BD,{HttpTransport:c(()=>CD,"HttpTransport")});var CD,Mj=J({"src/distributed/transport/http-transport.ts"(){"use strict";Cj(),Fe(),CD=class extends RD{static{c(this,"HttpTransport")}_port;_nodeId;_server;_nodes;_chunkListeners;_pendingChunks;_controlCallbacks;_fragmentCallbacks;_registerCallbacks;_heartbeatCallbacks;_agents;constructor(e={}){super(),this._port=e.port||ht.clusterPort,this._nodeId=e.nodeId||null,this._server=null,this._nodes=new Map,this._chunkListeners=new Map,this._pendingChunks=new Map,this._controlCallbacks=[],this._fragmentCallbacks=[],this._registerCallbacks=[],this._heartbeatCallbacks=[],this._agents=new Map}async start(){return new Promise((e,t)=>{this._server=Tj((s,n)=>this._handleRequest(s,n)),this._server.on("error",t),this._server.listen(this._port,()=>e())})}async stop(){if(this._server){for(let e of this._agents.values())e.destroy();return this._agents.clear(),new Promise(e=>{this._server.close(()=>e())})}}registerNode(e,t,s){this._nodes.set(e,{host:t,port:s})}async sendChunk(e,t,s){let n=this._nodes.get(e);if(!n)throw new Error(`Unknown node: ${e}`);let r={"Content-Type":"application/octet-stream","Content-Length":s.length};this._nodeId&&(r["x-source-node"]=this._nodeId),await this._post(n,`/exchange/${encodeURIComponent(t)}`,s,r)}onChunkReceived(e,t){this._chunkListeners.set(e,t);let s=this._pendingChunks.get(e);if(s){this._pendingChunks.delete(e);for(let{sourceNodeId:n,data:r}of s)t(n,r)}}removeChunkListener(e){this._chunkListeners.delete(e),this._pendingChunks.delete(e)}async sendFragment(e,t){let s=this._nodes.get(e);if(!s)throw new Error(`Unknown node: ${e}`);let n=Buffer.from(JSON.stringify(t));await this._post(s,"/fragment/execute",n,{"Content-Type":"application/json","Content-Length":n.length})}async sendControl(e,t){let s=this._nodes.get(e);if(!s)throw new Error(`Unknown node: ${e}`);let n=Buffer.from(JSON.stringify(t));await this._post(s,"/control",n,{"Content-Type":"application/json","Content-Length":n.length})}async sendRegister(e,t){let s=this._nodes.get(e);if(!s)throw new Error(`Unknown node: ${e}`);let n=Buffer.from(JSON.stringify(t));await this._post(s,"/register",n,{"Content-Type":"application/json","Content-Length":n.length})}onControlMessage(e){this._controlCallbacks.push(e)}onFragmentReceived(e){this._fragmentCallbacks.push(e)}onRegister(e){this._registerCallbacks.push(e)}onHeartbeat(e){this._heartbeatCallbacks.push(e)}async sendHeartbeat(e,t){let s=this._nodes.get(e);if(!s)return;let n=Buffer.from(JSON.stringify(t));await this._post(s,"/heartbeat",n,{"Content-Type":"application/json","Content-Length":n.length})}_handleRequest(e,t){let n=new URL(e.url,`http://${e.headers.host}`).pathname;if(e.method==="GET"&&n==="/status"){t.writeHead(200,{"Content-Type":"application/json"});let r={status:"alive",nodeId:this._nodeId};t.end(JSON.stringify(r));return}if(e.method==="POST"){this._collectBody(e,r=>{if(n.startsWith("/exchange/")){let i=decodeURIComponent(n.slice(10));this._handleExchangeData(i,r,e,t)}else n==="/fragment/execute"?this._handleFragmentExecute(r,t):n==="/control"?this._handleControl(r,t):n==="/heartbeat"?this._handleHeartbeat(r,t):n==="/register"?this._handleRegister(r,t):(t.writeHead(404),t.end())});return}t.writeHead(404),t.end()}_handleExchangeData(e,t,s,n){let r=s.headers["x-source-node"]||"unknown",i=this._chunkListeners.get(e);if(i)i(r,t);else{let o=this._pendingChunks.get(e);o||(o=[],this._pendingChunks.set(e,o)),o.push({sourceNodeId:r,data:t})}n.writeHead(200),n.end()}_handleFragmentExecute(e,t){try{let s=JSON.parse(e.toString());for(let n of this._fragmentCallbacks)n(s);t.writeHead(202),t.end()}catch(s){t.writeHead(400),t.end(s.message)}}_handleControl(e,t){try{let s=JSON.parse(e.toString());for(let n of this._controlCallbacks)n(s);t.writeHead(200),t.end()}catch(s){t.writeHead(400),t.end(s.message)}}_handleHeartbeat(e,t){try{let n=JSON.parse(e.toString());for(let r of this._heartbeatCallbacks)r(n)}catch{}t.writeHead(200);let s={ts:Date.now()};t.end(JSON.stringify(s))}_handleRegister(e,t){try{let s=JSON.parse(e.toString());for(let r of this._registerCallbacks)r(s);t.writeHead(200);let n={ok:!0};t.end(JSON.stringify(n))}catch(s){t.writeHead(400),t.end(s.message)}}_collectBody(e,t){let s=[];e.on("data",n=>s.push(n)),e.on("end",()=>t(Buffer.concat(s)))}async _post(e,t,s,n){return new Promise((r,i)=>{let o={hostname:e.host,port:e.port,path:t,method:"POST",headers:n||{"Content-Type":"application/octet-stream","Content-Length":s.length},agent:this._getAgent(e)},a=Ij(o,l=>{let u=[];l.on("data",f=>u.push(f)),l.on("end",()=>r(Buffer.concat(u)))});a.on("error",i),a.end(s)})}_getAgent(e){let t=`${e.host}:${e.port}`,s=this._agents.get(t);return s||(s=new Oj({keepAlive:!0,maxSockets:4}),this._agents.set(t,s)),s}}}}),xk=(e=>(e.IDENT="IDENT",e.NUMBER="NUMBER",e.STRING="STRING",e.COMMA="COMMA",e.DOT="DOT",e.STAR="STAR",e.LPAREN="LPAREN",e.RPAREN="RPAREN",e.EQ="EQ",e.NEQ="NEQ",e.LT="LT",e.GT="GT",e.LTE="LTE",e.GTE="GTE",e.PLUS="PLUS",e.MINUS="MINUS",e.SLASH="SLASH",e.PERCENT="PERCENT",e.SEMICOLON="SEMICOLON",e.CONCAT="CONCAT",e.COLON="COLON",e.EOF="EOF",e.EXPLAIN="EXPLAIN",e.SELECT="SELECT",e.FROM="FROM",e.WHERE="WHERE",e.AND="AND",e.OR="OR",e.NOT="NOT",e.AS="AS",e.ON="ON",e.JOIN="JOIN",e.INNER="INNER",e.LEFT="LEFT",e.RIGHT="RIGHT",e.FULL="FULL",e.OUTER="OUTER",e.CROSS="CROSS",e.IN="IN",e.EXISTS="EXISTS",e.BETWEEN="BETWEEN",e.LIKE="LIKE",e.IS="IS",e.NULL="NULL",e.TRUE="TRUE",e.FALSE="FALSE",e.CASE="CASE",e.WHEN="WHEN",e.THEN="THEN",e.ELSE="ELSE",e.END="END",e.DISTINCT="DISTINCT",e.ALL="ALL",e.GROUP="GROUP",e.BY="BY",e.HAVING="HAVING",e.ORDER="ORDER",e.ASC="ASC",e.DESC="DESC",e.LIMIT="LIMIT",e.OFFSET="OFFSET",e.UNION="UNION",e.EXCEPT="EXCEPT",e.INTERSECT="INTERSECT",e.WITH="WITH",e.CAST="CAST",e.INTERVAL="INTERVAL",e.EXTRACT="EXTRACT",e.SUBSTRING="SUBSTRING",e.TRIM="TRIM",e.YEAR="YEAR",e.MONTH="MONTH",e.DAY="DAY",e.DATE="DATE",e.SUM="SUM",e.AVG="AVG",e.COUNT="COUNT",e.MIN="MIN",e.MAX="MAX",e.CREATE="CREATE",e.VIEW="VIEW",e.NULLS="NULLS",e.FIRST="FIRST",e.LAST="LAST",e.FETCH="FETCH",e.NEXT="NEXT",e.ROWS="ROWS",e.ONLY="ONLY",e.SOME="SOME",e.ANY="ANY",e.LEADING="LEADING",e.TRAILING="TRAILING",e.BOTH="BOTH",e.FOR="FOR",e.TIMESTAMP="TIMESTAMP",e.HOUR="HOUR",e.MINUTE="MINUTE",e.SECOND="SECOND",e.OVER="OVER",e.PARTITION="PARTITION",e.RANGE="RANGE",e.UNBOUNDED="UNBOUNDED",e.PRECEDING="PRECEDING",e.FOLLOWING="FOLLOWING",e.CURRENT="CURRENT",e.ROW="ROW",e.NATURAL="NATURAL",e.USING="USING",e.TABLE="TABLE",e.DROP="DROP",e.IF="IF",e.ANALYZE="ANALYZE",e))(xk||{}),Lj=new Set(["IDENT","NUMBER","STRING","COMMA","DOT","STAR","LPAREN","RPAREN","EQ","NEQ","LT","GT","LTE","GTE","PLUS","MINUS","SLASH","PERCENT","SEMICOLON","CONCAT","COLON","EOF"]),TD=new Map;for(let e of Object.keys(xk))Lj.has(e)||TD.set(e,xk[e]);var De=class{static{c(this,"Token")}type;value;position;constructor(e,t,s){this.type=e,this.value=t,this.position=s}},Pj=class{static{c(this,"Lexer")}input;pos;tokens;constructor(e){this.input=e,this.pos=0,this.tokens=[],this._tokenize()}_tokenize(){for(;this.pos<this.input.length&&(this._skipWhitespaceAndComments(),!(this.pos>=this.input.length));){let e=this.pos,t=this.input[this.pos];t==="'"?this.tokens.push(this._readString(e)):this._isDigit(t)?this.tokens.push(this._readNumber(e)):this._isIdentStart(t)?this.tokens.push(this._readIdentOrKeyword(e)):this.tokens.push(this._readSymbol(e))}this.tokens.push(new De("EOF","",this.pos))}_skipWhitespaceAndComments(){for(;this.pos<this.input.length;){let e=this.input[this.pos];if(e===" "||e===" "||e===`
222
- `||e==="\r"){this.pos++;continue}if(e==="-"&&this.pos+1<this.input.length&&this.input[this.pos+1]==="-"){for(;this.pos<this.input.length&&this.input[this.pos]!==`
223
- `;)this.pos++;continue}break}}_readString(e){this.pos++;let t="";for(;this.pos<this.input.length;)if(this.input[this.pos]==="'")if(this.pos+1<this.input.length&&this.input[this.pos+1]==="'")t+="'",this.pos+=2;else return this.pos++,new De("STRING",t,e);else t+=this.input[this.pos],this.pos++;throw new Error(`Unterminated string at position ${e}`)}_readNumber(e){for(;this.pos<this.input.length&&this._isDigit(this.input[this.pos]);)this.pos++;if(this.pos<this.input.length&&this.input[this.pos]===".")for(this.pos++;this.pos<this.input.length&&this._isDigit(this.input[this.pos]);)this.pos++;return new De("NUMBER",this.input.slice(e,this.pos),e)}_readIdentOrKeyword(e){for(;this.pos<this.input.length&&this._isIdentPart(this.input[this.pos]);)this.pos++;let t=this.input.slice(e,this.pos),s=t.toUpperCase(),n=TD.get(s);return n?new De(n,s,e):new De("IDENT",t,e)}_readSymbol(e){let t=this.input[this.pos];switch(this.pos++,t){case",":return new De("COMMA",",",e);case".":return new De("DOT",".",e);case"*":return new De("STAR","*",e);case"(":return new De("LPAREN","(",e);case")":return new De("RPAREN",")",e);case"+":return new De("PLUS","+",e);case"-":return new De("MINUS","-",e);case"/":return new De("SLASH","/",e);case"%":return new De("PERCENT","%",e);case";":return new De("SEMICOLON",";",e);case":":return new De("COLON",":",e);case"=":return new De("EQ","=",e);case"<":if(this.pos<this.input.length){if(this.input[this.pos]==="=")return this.pos++,new De("LTE","<=",e);if(this.input[this.pos]===">")return this.pos++,new De("NEQ","<>",e)}return new De("LT","<",e);case">":return this.pos<this.input.length&&this.input[this.pos]==="="?(this.pos++,new De("GTE",">=",e)):new De("GT",">",e);case"!":if(this.pos<this.input.length&&this.input[this.pos]==="=")return this.pos++,new De("NEQ","!=",e);throw new Error(`Unexpected character '!' at position ${e}`);case"|":if(this.pos<this.input.length&&this.input[this.pos]==="|")return this.pos++,new De("CONCAT","||",e);throw new Error(`Unexpected character '|' at position ${e}`);default:throw new Error(`Unexpected character '${t}' at position ${e}`)}}_isDigit(e){return e>="0"&&e<="9"}_isIdentStart(e){return e>="a"&&e<="z"||e>="A"&&e<="Z"||e==="_"}_isIdentPart(e){return this._isIdentStart(e)||this._isDigit(e)}};function Dj(e){return{kind:"ExplainStmt",query:e}}c(Dj,"ExplainStmt");function Fj({withClause:e=null,distinct:t=!1,selectItems:s,from:n=null,where:r=null,groupBy:i=null,having:o=null,orderBy:a=null,limit:l=null,offset:u=null}){return{kind:"SelectStmt",withClause:e,distinct:t,selectItems:s,from:n,where:r,groupBy:i,having:o,orderBy:a,limit:l,offset:u}}c(Fj,"SelectStmt");function $j(e,t,s,n=!1){return{kind:"SetOp",op:e,left:t,right:s,all:n}}c($j,"SetOp");function zj(e){return{kind:"WithClause",ctes:e}}c(zj,"WithClause");function Uj(e,t,s=null){return{kind:"CTE",name:e,query:t,columnAliases:s}}c(Uj,"CTE");function aw(e,t=null){return{kind:"SelectItem",expr:e,alias:t}}c(aw,"SelectItem");function Vp(e=null){return{kind:"AllColumns",table:e}}c(Vp,"AllColumns");function Vj(e,t=null){return{kind:"TableRef",name:e,alias:t||e}}c(Vj,"TableRef");function zv(e,t,s,n=null){return{kind:"JoinRef",left:e,right:t,joinType:s,condition:n}}c(zv,"JoinRef");function Gj(e,t){return{kind:"SubqueryRef",query:e,alias:t}}c(Gj,"SubqueryRef");function YM(e,t=null){return{kind:"ColumnRef",name:e,table:t}}c(YM,"ColumnRef");function bo(e,t=null){return{kind:"Literal",value:e,dataType:t}}c(bo,"Literal");function _f(e,t,s){return{kind:"BinaryExpr",op:e,left:t,right:s}}c(_f,"BinaryExpr");function Uv(e,t){return{kind:"UnaryExpr",op:e,operand:t}}c(Uv,"UnaryExpr");function qj(e,t,s,n=!1){return{kind:"BetweenExpr",expr:e,low:t,high:s,negated:n}}c(qj,"BetweenExpr");function QM(e,t,s=!1){return{kind:"InExpr",expr:e,list:t,negated:s}}c(QM,"InExpr");function jj(e,t,s=!1){return{kind:"LikeExpr",expr:e,pattern:t,negated:s}}c(jj,"LikeExpr");function Wj(e,t=!1){return{kind:"IsNullExpr",expr:e,negated:t}}c(Wj,"IsNullExpr");function XM(e,t=!1){return{kind:"ExistsExpr",query:e,negated:t}}c(XM,"ExistsExpr");function ZM(e){return{kind:"SubqueryExpr",query:e}}c(ZM,"SubqueryExpr");function tL(e,t){return{kind:"FunctionCall",name:e,args:t}}c(tL,"FunctionCall");function eL(e,t,s=!1){return{kind:"AggregateCall",name:e,args:t,distinct:s}}c(eL,"AggregateCall");function Kj(e,t,s=null){return{kind:"CaseExpr",operand:e,whenClauses:t,elseExpr:s}}c(Kj,"CaseExpr");function Hj(e,t){return{kind:"WhenClause",condition:e,result:t}}c(Hj,"WhenClause");function Jj(e,t){return{kind:"CastExpr",expr:e,targetType:t}}c(Jj,"CastExpr");function Yj(e,t){return{kind:"ExtractExpr",field:e,source:t}}c(Yj,"ExtractExpr");function Qj(e,t,s=null){return{kind:"SubstringExpr",expr:e,from:t,length:s}}c(Qj,"SubstringExpr");function Xj(e,t){return{kind:"IntervalExpr",value:e,unit:t}}c(Xj,"IntervalExpr");function Zj(e,t="ASC",s=null){return{kind:"OrderKey",expr:e,direction:t,nullOrder:s}}c(Zj,"OrderKey");function tW(e,t=[]){return{kind:"TypeName",name:e,params:t}}c(tW,"TypeName");function eW(e=[],t=[]){return{kind:"WindowSpec",partitionBy:e,orderBy:t}}c(eW,"WindowSpec");function sW(e,t,s){return{kind:"WindowCall",name:e,args:t,windowSpec:s}}c(sW,"WindowCall");function sL(e,t,s=!1,n=null){return{kind:"CreateTableStmt",name:e,columns:t,ifNotExists:s,as:n}}c(sL,"CreateTableStmt");function nW(e,t=!1){return{kind:"DropTableStmt",name:e,ifExists:t}}c(nW,"DropTableStmt");function rW(e,t){return{kind:"ColumnDef",name:e,typeName:t}}c(rW,"ColumnDef");function iW(e){return{kind:"ExplainAnalyzeStmt",query:e}}c(iW,"ExplainAnalyzeStmt");var ID=class{static{c(this,"Parser")}tokens;pos;constructor(e){let t=new Pj(e);this.tokens=t.tokens,this.pos=0}parse(){let e=this.parseStatement();return!this.isAt("EOF")&&!this.isAt("SEMICOLON")&&this.error(`Unexpected token ${this.peek().type}`),e}parseStatement(){if(this.isAt("CREATE"))return this.parseCreateTable();if(this.isAt("DROP"))return this.parseDropTable();let e=!1,t=!1;this.isAt("EXPLAIN")&&(this.advance(),e=!0,this.isAt("ANALYZE")&&(this.advance(),t=!0));let s=this.parseQueryExpr();return t?iW(s):e?Dj(s):s}parseCreateTable(){this.advance(),this.expect("TABLE");let e=!1;this.isAt("IF")&&(this.advance(),this.expect("NOT"),this.expect("EXISTS"),e=!0);let t=this.expectIdent();if(this.tryConsume("AS")){let n=this.parseQueryExpr();return sL(t,null,e,n)}this.expect("LPAREN");let s=[];do{let n=this.expectIdent(),r=this.parseTypeName();s.push(rW(n,r))}while(this.tryConsume("COMMA"));return this.expect("RPAREN"),sL(t,s,e)}parseDropTable(){this.advance(),this.expect("TABLE");let e=!1;this.isAt("IF")&&(this.advance(),this.expect("EXISTS"),e=!0);let t=this.expectIdent();return nW(t,e)}parseQueryExpr(){let e=this.parseSelectStmt();for(;this.isAt("UNION")||this.isAt("EXCEPT")||this.isAt("INTERSECT");){let t=this.advance().type,s=this.tryConsume("ALL");s||this.tryConsume("DISTINCT");let n=this.parseSelectStmt();e=$j(t,e,n,s)}return e}parseSelectStmt(){let e=null;if(this.isAt("WITH")&&(e=this.parseWithClause()),this.isAt("LPAREN")){this.advance();let f=this.parseQueryExpr();return this.expect("RPAREN"),e&&(f.kind==="SelectStmt"?(f.withClause&&this.error("Multiple WITH clauses are not allowed"),f.withClause=e):this.error("WITH clause cannot precede a parenthesized set operation")),f}let t=!1,s;this.isAt("FROM")?s=[aw(Vp())]:(this.expect("SELECT"),t=this.tryConsume("DISTINCT"),t||this.tryConsume("ALL"),s=this.parseSelectItems());let n=null;this.tryConsume("FROM")&&(n=this.parseFromClause());let r=null;this.tryConsume("WHERE")&&(r=this.parseExpression());let i=null;this.tryConsume("GROUP")&&(this.expect("BY"),i=this.parseExpressionList());let o=null;this.tryConsume("HAVING")&&(o=this.parseExpression());let a=null;this.isAt("ORDER")&&(this.advance(),this.expect("BY"),a=this.parseOrderByList());let l=null,u=null;return this.tryConsume("LIMIT")&&(l=this.parseExpression(),this.tryConsume("OFFSET")&&(u=this.parseExpression())),this.tryConsume("FETCH")&&(this.tryConsume("FIRST")||this.tryConsume("NEXT"),l=this.parseExpression(),this.tryConsume("ROWS"),this.expect("ONLY")),Fj({withClause:e,distinct:!!t,selectItems:s,from:n,where:r,groupBy:i,having:o,orderBy:a,limit:l,offset:u})}parseWithClause(){this.advance();let e=[];do e.push(this.parseCTE());while(this.tryConsume("COMMA"));return zj(e)}parseCTE(){let e=this.expectIdent(),t=null;if(this.isAt("LPAREN")&&!this.isLookaheadSelect()){this.advance(),t=[];do t.push(this.expectIdent());while(this.tryConsume("COMMA"));this.expect("RPAREN")}this.expect("AS"),this.expect("LPAREN");let s=this.parseQueryExpr();return this.expect("RPAREN"),Uj(e,s,t)}parseSelectItems(){let e=[];do e.push(this.parseSelectItem());while(this.tryConsume("COMMA"));return e}parseSelectItem(){if(this.isAt("STAR"))return this.advance(),aw(Vp());if(this.isAt("IDENT")&&this.peekAhead(1)?.type==="DOT"&&this.peekAhead(2)?.type==="STAR"){let s=this.advance().value;return this.advance(),this.advance(),aw(Vp(s))}let e=this.parseExpression(),t=null;return this.tryConsume("AS")?t=this.expectIdent():this.isAt("IDENT")&&(t=this.expectIdent()),aw(e,t)}parseFromClause(){let e=this.parseTableRef();for(;this.isJoinKeyword();)e=this.parseJoin(e);for(;this.tryConsume("COMMA");){let t=this.parseTableRef();e=zv(e,t,"CROSS")}return e}parseTableRef(){if(this.isAt("LPAREN")){if(this.isSubqueryStart())return this.parseSubqueryRef();this.advance();let s=this.parseFromClause();return this.expect("RPAREN"),s}let e=this.expectIdent(),t=e;return this.tryConsume("AS")?t=this.expectIdent():this.isAt("IDENT")&&!this.isJoinKeyword()&&!this.isClauseKeyword()&&(t=this.expectIdent()),Vj(e,t)}parseSubqueryRef(){this.expect("LPAREN");let e=this.parseQueryExpr();this.expect("RPAREN");let t=null;return this.tryConsume("AS")?t=this.expectIdent():this.isAt("IDENT")&&(t=this.expectIdent()),Gj(e,t)}parseJoin(e){let t="INNER",s=!1;this.tryConsume("NATURAL")&&(s=!0),this.tryConsume("LEFT")?(this.tryConsume("OUTER"),t="LEFT"):this.tryConsume("RIGHT")?(this.tryConsume("OUTER"),t="RIGHT"):this.tryConsume("FULL")?(this.tryConsume("OUTER"),t="FULL"):this.tryConsume("CROSS")?t="CROSS":this.tryConsume("INNER")&&(t="INNER"),this.expect("JOIN");let n=this.parseTableRef(),r=null,i=null;if(s){let a=zv(e,n,t,null);return a.natural=!0,a}if(t!=="CROSS"){if(this.tryConsume("ON"))r=this.parseExpression();else if(this.tryConsume("USING")){this.expect("LPAREN"),i=[];do i.push(this.expectIdent());while(this.tryConsume("COMMA"));this.expect("RPAREN")}}let o=zv(e,n,t,r);return i&&(o.usingColumns=i),o}parseExpression(){return this.parseOr()}parseOr(){let e=this.parseAnd();for(;this.isAt("OR");){this.advance();let t=this.parseAnd();e=_f("OR",e,t)}return e}parseAnd(){let e=this.parseNot();for(;this.isAt("AND");){this.advance();let t=this.parseNot();e=_f("AND",e,t)}return e}parseNot(){if(this.isAt("NOT")){this.advance();let e=this.parseNot();return Uv("NOT",e)}return this.parseComparison()}parseComparison(){if(this.isAt("EXISTS"))return this.parseExists();let e=this.parseAddition();if(this.isAt("NOT")){let s=this.pos;if(this.advance(),this.isAt("BETWEEN"))return this.parseBetween(e,!0);if(this.isAt("IN"))return this.parseIn(e,!0);if(this.isAt("LIKE"))return this.parseLike(e,!0);this.pos=s}if(this.isAt("BETWEEN"))return this.parseBetween(e,!1);if(this.isAt("IN"))return this.parseIn(e,!1);if(this.isAt("LIKE"))return this.parseLike(e,!1);if(this.isAt("IS")){this.advance();let s=this.tryConsume("NOT");return this.expect("NULL"),Wj(e,!!s)}let t={EQ:"=",NEQ:"<>",LT:"<",GT:">",LTE:"<=",GTE:">="};if(t[this.peek().type]){let s=t[this.advance().type];if(this.isAt("ALL")||this.isAt("SOME")||this.isAt("ANY")){let r=this.advance().type;this.expect("LPAREN");let i=this.parseQueryExpr();return this.expect("RPAREN"),_f(s,e,{kind:"QuantifiedSubquery",quantifier:r,query:i})}let n=this.parseAddition();return _f(s,e,n)}return e}parseBetween(e,t){this.advance();let s=this.parseAddition();this.expect("AND");let n=this.parseAddition();return qj(e,s,n,t)}parseIn(e,t){if(this.advance(),this.expect("LPAREN"),this.isAt("SELECT")||this.isAt("WITH")||this.isAt("FROM")){let n=this.parseQueryExpr();return this.expect("RPAREN"),QM(e,ZM(n),t)}let s=this.parseExpressionList();return this.expect("RPAREN"),QM(e,s,t)}parseLike(e,t){this.advance();let s=this.parseAddition();return jj(e,s,t)}parseExists(){this.advance(),this.expect("LPAREN");let e=this.parseQueryExpr();return this.expect("RPAREN"),XM(e,!1)}parseAddition(){let e=this.parseMultiplication();for(;this.isAt("PLUS")||this.isAt("MINUS")||this.isAt("CONCAT");){let t=this.advance().value,s=this.parseMultiplication();e=_f(t,e,s)}return e}parseMultiplication(){let e=this.parseUnary();for(;this.isAt("STAR")||this.isAt("SLASH")||this.isAt("PERCENT");){let t=this.advance().value,s=this.parseUnary();e=_f(t,e,s)}return e}parseUnary(){if(this.isAt("MINUS")){this.advance();let e=this.parseUnary();return Uv("-",e)}return this.isAt("PLUS")?(this.advance(),this.parseUnary()):this.parsePrimary()}parsePrimary(){let e=this.peek();if(e.type==="NUMBER")return this.advance(),bo(e.value.includes(".")?parseFloat(e.value):parseInt(e.value,10));if(e.type==="STRING")return this.advance(),bo(e.value,"VARCHAR");if(e.type==="NULL")return this.advance(),bo(null);if(e.type==="TRUE")return this.advance(),bo(!0,"BOOLEAN");if(e.type==="FALSE")return this.advance(),bo(!1,"BOOLEAN");if(e.type==="DATE"){this.advance();let t=this.expect("STRING");return bo(t.value,"DATE")}if(e.type==="TIMESTAMP"){this.advance();let t=this.expect("STRING");return bo(t.value,"TIMESTAMP")}if(e.type==="INTERVAL")return this.parseInterval();if(e.type==="CASE")return this.parseCaseExpr();if(e.type==="CAST")return this.parseCast();if(e.type==="EXTRACT")return this.parseExtract();if(e.type==="SUBSTRING")return this.parseSubstringFn();if(e.type==="TRIM")return this.parseTrim();if(e.type==="EXISTS")return this.parseExists();if(e.type==="NOT"){if(this.advance(),this.isAt("EXISTS")){this.advance(),this.expect("LPAREN");let s=this.parseQueryExpr();return this.expect("RPAREN"),XM(s,!0)}let t=this.parsePrimary();return Uv("NOT",t)}if(e.type==="LPAREN"){if(this.isSubqueryStart()){this.advance();let s=this.parseQueryExpr();return this.expect("RPAREN"),ZM(s)}this.advance();let t=this.parseExpression();return this.expect("RPAREN"),t}if(e.type==="STAR")return this.advance(),Vp();if(this.isAggregateKeyword(e.type))return this.parseAggregateCall();if(e.type==="COLON"){this.advance();let t=this.expect("NUMBER");return bo(`:${t.value}`,"PARAM")}if(e.type==="IDENT"){let t=this.advance().value;if(this.isAt("LPAREN"))return this.parseFunctionCallNamed(t);if(this.isAt("DOT")){if(this.advance(),this.isAt("STAR"))return this.advance(),Vp(t);let s=this.expectIdent();return YM(s,t)}return YM(t)}this.error(`Unexpected token ${e.type} (${e.value})`)}parseAggregateCall(){let e=this.advance().value;if(this.expect("LPAREN"),e==="COUNT"&&this.isAt("STAR"))return this.advance(),this.expect("RPAREN"),this.isAt("OVER")?this.parseWindowCall("COUNT_STAR",[]):eL("COUNT_STAR",[],!1);let t=this.tryConsume("DISTINCT"),s=this.parseExpressionList();return this.expect("RPAREN"),this.isAt("OVER")?this.parseWindowCall(e,s):eL(e,s,!!t)}parseFunctionCallNamed(e){this.expect("LPAREN");let t=[];return this.isAt("RPAREN")||(t=this.parseExpressionList()),this.expect("RPAREN"),this.isAt("OVER")?this.parseWindowCall(e,t):tL(e,t)}parseWindowCall(e,t){this.expect("OVER"),this.expect("LPAREN");let s=[];this.tryConsume("PARTITION")&&(this.expect("BY"),s=this.parseExpressionList());let n=[];this.isAt("ORDER")&&(this.advance(),this.expect("BY"),n=this.parseOrderByList()),this.expect("RPAREN");let r=eW(s,n);return sW(e,t,r)}parseCaseExpr(){this.advance();let e=null;this.isAt("WHEN")||(e=this.parseExpression());let t=[];for(;this.tryConsume("WHEN");){let n=this.parseExpression();this.expect("THEN");let r=this.parseExpression();t.push(Hj(n,r))}let s=null;return this.tryConsume("ELSE")&&(s=this.parseExpression()),this.expect("END"),Kj(e,t,s)}parseCast(){this.advance(),this.expect("LPAREN");let e=this.parseExpression();this.expect("AS");let t=this.parseTypeName();return this.expect("RPAREN"),Jj(e,t)}parseExtract(){this.advance(),this.expect("LPAREN");let e=this.peek().value;this.advance(),this.expect("FROM");let t=this.parseExpression();return this.expect("RPAREN"),Yj(e.toUpperCase(),t)}parseSubstringFn(){this.advance(),this.expect("LPAREN");let e=this.parseExpression(),t,s=null;return this.isAtKeyword("FROM")?(this.advance(),t=this.parseExpression(),this.isAtKeyword("FOR")&&(this.advance(),s=this.parseExpression())):(this.expect("COMMA"),t=this.parseExpression(),this.tryConsume("COMMA")&&(s=this.parseExpression())),this.expect("RPAREN"),Qj(e,t,s)}parseTrim(){this.advance(),this.expect("LPAREN");let e=null;(this.isAt("LEADING")||this.isAt("TRAILING")||this.isAt("BOTH"))&&(e=this.advance().value);let t=null;!this.isAt("FROM")&&e!==null&&(this.isAt("FROM")||(t=this.parseExpression())),e!==null&&this.tryConsume("FROM");let s=this.parseExpression();this.expect("RPAREN");let n=t?[s,bo(e),t]:[s];return tL("TRIM",n)}parseInterval(){this.advance();let e=this.expect("STRING"),t=this.expectIdent().toUpperCase();return Xj(e.value,t)}parseTypeName(){let e=this.expectIdent().toUpperCase(),t=[];if(this.tryConsume("LPAREN")){do t.push(parseInt(this.expect("NUMBER").value,10));while(this.tryConsume("COMMA"));this.expect("RPAREN")}return tW(e,t)}parseExpressionList(){let e=[];do e.push(this.parseExpression());while(this.tryConsume("COMMA"));return e}parseOrderByList(){let e=[];do{let t=this.parseExpression(),s="ASC";this.tryConsume("ASC")?s="ASC":this.tryConsume("DESC")&&(s="DESC");let n=null;this.tryConsume("NULLS")&&(this.tryConsume("FIRST")?n="FIRST":this.tryConsume("LAST")&&(n="LAST")),e.push(Zj(t,s,n))}while(this.tryConsume("COMMA"));return e}peek(){return this.tokens[this.pos]}peekAhead(e){return this.tokens[this.pos+e]||null}advance(){return this.tokens[this.pos++]}isAt(e){return this.peek().type===e}tryConsume(e){return this.isAt(e)?(this.advance(),!0):!1}expect(e){return this.isAt(e)||this.error(`Expected ${e}, got ${this.peek().type} (${this.peek().value})`),this.advance()}expectIdent(){let e=this.peek();if(e.type==="IDENT")return this.advance(),e.value;if(this.isNonReservedKeyword(e.type))return this.advance(),e.value;this.error(`Expected identifier, got ${e.type} (${e.value})`)}isNonReservedKeyword(e){return["YEAR","MONTH","DAY","DATE","FIRST","LAST","NULLS","VIEW","ROWS","ONLY","NEXT","FETCH","SOME","ANY","LEADING","TRAILING","BOTH","SUM","AVG","COUNT","MIN","MAX","SUBSTRING","EXTRACT","TRIM","OFFSET","FOR","ASC","DESC","LEFT","RIGHT","FULL","INNER","OUTER","CROSS","JOIN","CREATE","INTERVAL","UNION","ALL","TIMESTAMP","HOUR","MINUTE","SECOND","OVER","PARTITION","RANGE","UNBOUNDED","PRECEDING","FOLLOWING","CURRENT","ROW","NATURAL","USING","TABLE","DROP","IF","ANALYZE"].includes(e)}isAggregateKeyword(e){return["SUM","AVG","COUNT","MIN","MAX"].includes(e)}isJoinKeyword(){let e=this.peek().type;return e==="JOIN"||e==="INNER"||e==="LEFT"||e==="RIGHT"||e==="FULL"||e==="CROSS"||e==="NATURAL"}isClauseKeyword(){let e=this.peek().type;return e==="WHERE"||e==="GROUP"||e==="HAVING"||e==="ORDER"||e==="LIMIT"||e==="UNION"||e==="EXCEPT"||e==="INTERSECT"||e==="ON"||e==="FETCH"}isSubqueryStart(){let e=0;for(let t=this.pos;t<this.tokens.length&&(this.tokens[t].type==="LPAREN"&&e++,this.tokens[t].type==="RPAREN"&&e--,!(e===0&&t>this.pos));t++)if(e===1&&(this.tokens[t].type==="SELECT"||this.tokens[t].type==="WITH"||this.tokens[t].type==="FROM"))return!0;return!1}isLookaheadSelect(){let e=1;for(let t=this.pos+1;t<this.tokens.length&&(this.tokens[t].type==="LPAREN"&&e++,!(this.tokens[t].type==="RPAREN"&&(e--,e===0)));t++)if(e===1&&this.tokens[t].type==="SELECT")return!0;return!1}isAtKeyword(e){let t=this.peek(),s=e.toUpperCase();return t.type===s||t.value===s}error(e){let t=this.peek();throw new Error(`Parse error at position ${t.position}: ${e}`)}};function oW(e){return new ID(e).parse()}c(oW,"parse");function aW(e){let t=new ID(e),s=t.parseExpression();return!t.isAt("EOF")&&!t.isAt("SEMICOLON")&&t.error(`Unexpected token ${t.peek().type}`),s}c(aW,"parseExpression");we();var OD=class MD{static{c(this,"_BinderScope")}parent;tables;columns;correlatedRefs;constructor(t=null){this.parent=t,this.tables=new Map,this.columns=new Map,this.correlatedRefs=[]}addTable(t,s){this.tables.set(t.toUpperCase(),s)}addColumn(t,s){let n=t.toUpperCase();this.columns.has(n)||this.columns.set(n,[]),this.columns.get(n).push(s)}resolveTable(t){let s=t.toUpperCase(),n=this.tables.get(s);if(n)return{table:n,depth:0};if(this.parent){let r=this.parent.resolveTable(s);if(r)return{table:r.table,depth:r.depth+1}}return null}resolveColumn(t,s=null){let n=t.toUpperCase();if(s){let o=s.toUpperCase(),a=this.resolveTable(s);if(!a)return null;let{table:l,depth:u}=a,f=l.columns.find(p=>p.name.toUpperCase()===n);if(!f)return null;let h=l.columns.findIndex(p=>p.name.toUpperCase()===n);return{tableAlias:o,tableName:l.originalName||o,column:f,columnIndex:h,depth:u}}let r=null,i=0;for(let[o,a]of this.tables){let l=a.columns.findIndex(u=>u.name.toUpperCase()===n);if(l>=0){if(r)throw new Error(`Ambiguous column reference: ${t}`);r={tableAlias:o,tableName:a.originalName||o,column:a.columns[l],columnIndex:l,depth:0}}}if(r)return r;if(this.parent){let o=this.parent.resolveColumn(t);if(o)return{...o,depth:o.depth+1}}return null}getAllColumns(){let t=[];for(let[s,n]of this.tables)for(let r=0;r<n.columns.length;r++)t.push({tableAlias:s,tableName:n.originalName||s,column:n.columns[r],columnIndex:r});return t}getTableColumns(t){let s=t.toUpperCase(),n=this.tables.get(s);return n?n.columns.map((r,i)=>({tableAlias:s,tableName:n.originalName||s,column:r,columnIndex:i})):null}child(){return new MD(this)}};Vt();var Sk=class{static{c(this,"Binder")}catalog;functionRegistry;cteScopes;aggregatesFound;constructor(e,t){this.catalog=e,this.functionRegistry=t,this.cteScopes=new Map,this.aggregatesFound=[]}bind(e){let t=new OD;return this.bindQuery(e,t)}bindQuery(e,t){return e.kind==="SetOp"?this.bindSetOp(e,t):this.bindSelect(e,t)}bindSetOp(e,t){let s=this.bindQuery(e.left,t),n=this.bindQuery(e.right,t);return{type:"SetOp",op:e.op,all:e.all,left:s,right:n,outputColumns:s.outputColumns}}bindSelect(e,t){e.withClause&&this.bindWithClause(e.withClause,t);let s=t.child(),n=null;e.from&&(n=this.bindFrom(e.from,s));let r=this.aggregatesFound;this.aggregatesFound=[];let i=[],o=this.bindSelectItems(e.selectItems,s),a=null;e.where&&(a=this.bindExpression(e.where,s));let l=[...this.aggregatesFound],u=new Map;for(let g of o){let y=g.alias||g.inferredName;y&&u.set(y.toUpperCase(),g.expr)}let f=null;e.groupBy&&(f=e.groupBy.map(g=>{if(g.kind==="ColumnRef"&&!g.table){let y=u.get(g.name.toUpperCase());if(y)return y}return this.bindExpression(g,s)}));let h=null;e.having&&(h=this.bindExpression(e.having,s),l.push(...this.aggregatesFound.slice(l.length))),this.aggregatesFound=r;let p=null;e.orderBy&&(p=e.orderBy.map(g=>{if(g.expr.kind==="ColumnRef"&&!g.expr.table){let y=u.get(g.expr.name.toUpperCase());if(y)return{expr:y,direction:g.direction,nullOrder:g.nullOrder}}return{expr:this.bindExpression(g.expr,s),direction:g.direction,nullOrder:g.nullOrder}}));let d=null;e.limit&&(d=this.bindExpression(e.limit,s));let m=null;return e.offset&&(m=this.bindExpression(e.offset,s)),i=o.map((g,y)=>({name:g.alias||g.inferredName||`col${y}`,expr:g.expr,dataType:nn(g.expr)})),{type:"BoundSelect",plan:n,selectItems:o,where:a,groupBy:f,aggregates:l,having:h,orderBy:p,limit:d,offset:m,distinct:e.distinct,outputColumns:i}}bindWithClause(e,t){for(let s of e.ctes){let n=t.child(),r=this.bindQuery(s.query,n),i=r.outputColumns.map((o,a)=>({name:s.columnAliases?s.columnAliases[a]:o.name,dataType:o.dataType}));this.cteScopes.set(s.name.toUpperCase(),{name:s.name,columns:i,bound:r})}}bindFrom(e,t){switch(e.kind){case"TableRef":return this.bindTableRef(e,t);case"JoinRef":return this.bindJoinRef(e,t);case"SubqueryRef":return this.bindSubqueryRef(e,t);default:throw new Error(`Unknown FROM node kind: ${e.kind}`)}}bindTableRef(e,t){let s=e.name.toUpperCase(),n=this.cteScopes.get(s);if(n)return t.addTable(e.alias,{originalName:n.name,columns:n.columns,isCTE:!0}),{type:"CTERef",cteName:n.name,alias:e.alias.toUpperCase(),columns:n.columns,query:n.bound};let r=this.catalog.getTable(e.name);if(!r)throw new Error(`Unknown table: ${e.name}`);return t.addTable(e.alias,{originalName:r.name,columns:r.columns}),{type:"TableRef",tableName:r.name,alias:e.alias.toUpperCase(),columns:r.columns}}bindJoinRef(e,t){let s=this.bindFrom(e.left,t),n=this.bindFrom(e.right,t),r=null;return e.natural?r=this.buildNaturalJoinCondition(s,n,t):e.usingColumns?r=this.buildUsingCondition(e.usingColumns,s,n,t):e.condition&&(r=this.bindExpression(e.condition,t)),{type:"JoinRef",joinType:e.joinType,left:s,right:n,condition:r}}buildNaturalJoinCondition(e,t,s){let n=this.getRefColumnNames(e),r=this.getRefColumnNames(t),i=n.filter(o=>r.includes(o));return i.length===0?null:this.buildUsingCondition(i,e,t,s)}buildUsingCondition(e,t,s,n){let r=null;for(let i of e){let o=n.resolveColumn(i,this.getRefAlias(t)),a=n.resolveColumn(i,this.getRefAlias(s));if(!o||!a)throw new Error(`USING column not found: ${i}`);let l=Cn("=",La(o.tableAlias,o.column.name,o.columnIndex,o.column.dataType),La(a.tableAlias,a.column.name,a.columnIndex,a.column.dataType),"BOOLEAN");r=r?Cn("AND",r,l,"BOOLEAN"):l}return r}getRefColumnNames(e){return(e.columns||[]).map(s=>s.name.toUpperCase())}getRefAlias(e){return e.alias||e.tableName||e.cteName||null}bindSubqueryRef(e,t){let s=t.child(),n=this.bindQuery(e.query,s),r=e.alias||"_subquery";return t.addTable(r,{originalName:r,columns:n.outputColumns.map(i=>({name:i.name,dataType:i.dataType}))}),{type:"SubqueryRef",alias:r.toUpperCase(),query:n,columns:n.outputColumns}}bindSelectItems(e,t){let s=[];for(let n of e)if(n.expr.kind==="AllColumns"){let r=this.expandStar(n.expr,t);s.push(...r)}else{let r=this.bindExpression(n.expr,t);s.push({expr:r,alias:n.alias,inferredName:this.inferColumnName(n.expr)})}return s}expandStar(e,t){if(e.table){let s=t.getTableColumns(e.table);if(!s)throw new Error(`Unknown table for star: ${e.table}`);return s.map(n=>({expr:La(n.tableAlias,n.column.name,n.columnIndex,n.column.dataType),alias:null,inferredName:n.column.name}))}return t.getAllColumns().map(s=>({expr:La(s.tableAlias,s.column.name,s.columnIndex,s.column.dataType),alias:null,inferredName:s.column.name}))}bindExpression(e,t){if(!e)return null;switch(e.kind){case"ColumnRef":return this.bindColumnRef(e,t);case"Literal":return this.bindLiteral(e);case"BinaryExpr":return this.bindBinaryExpr(e,t);case"UnaryExpr":return this.bindUnaryExpr(e,t);case"AggregateCall":return this.bindAggregateCall(e,t);case"FunctionCall":return this.bindFunctionCall(e,t);case"CaseExpr":return this.bindCaseExpr(e,t);case"CastExpr":return this.bindCastExpr(e,t);case"BetweenExpr":return PL(this.bindExpression(e.expr,t),this.bindExpression(e.low,t),this.bindExpression(e.high,t),e.negated);case"InExpr":return this.bindInExpr(e,t);case"LikeExpr":return DL(this.bindExpression(e.expr,t),this.bindExpression(e.pattern,t),e.negated);case"IsNullExpr":return ek(this.bindExpression(e.expr,t),e.negated);case"ExistsExpr":return this.bindExistsExpr(e,t);case"SubqueryExpr":return this.bindSubqueryExpr(e,t);case"ExtractExpr":return T4(e.field,this.bindExpression(e.source,t));case"SubstringExpr":return tk("SUBSTRING",[this.bindExpression(e.expr,t),this.bindExpression(e.from,t),e.length?this.bindExpression(e.length,t):null].filter(Boolean),"VARCHAR");case"IntervalExpr":return I4(parseInt(e.value,10),e.unit);case"WindowCall":return this.bindWindowCall(e,t);case"AllColumns":throw new Error("Star expression in unexpected position");default:throw new Error(`Unhandled expression kind: ${e.kind}`)}}bindWindowCall(e,t){let s=e.args.map(o=>this.bindExpression(o,t)),n=e.windowSpec.partitionBy.map(o=>this.bindExpression(o,t)),r=e.windowSpec.orderBy.map(o=>({expr:this.bindExpression(o.expr,t),direction:o.direction,nullOrder:o.nullOrder})),i=this.inferWindowType(e.name,s);return O4(e.name.toUpperCase(),s,n,r,i)}bindColumnRef(e,t){let s=t.resolveColumn(e.name,e.table);if(!s)throw new Error(`Unknown column: ${e.table?`${e.table}.`:""}${e.name}`);return La(s.tableAlias,s.column.name,s.columnIndex,s.column.dataType,s.depth)}bindLiteral(e){if(e.value===null)return ce(null,null);if(e.dataType==="DATE"){let[t,s,n]=e.value.split("-").map(Number);return ce(rd(t,s,n),"DATE")}if(e.dataType==="TIMESTAMP"){let t=e.value.split(/[T ]/),[s,n,r]=t[0].split("-").map(Number),i=0,o=0,a=0,l=0;if(t[1]){let u=t[1].split(":");if(i=Number(u[0])||0,o=Number(u[1])||0,u[2]){let f=u[2].split(".");a=Number(f[0])||0,l=f[1]?Number(f[1].padEnd(3,"0").slice(0,3)):0}}return ce(N4(s,n,r,i,o,a,l),"TIMESTAMP")}return e.dataType==="BOOLEAN"?ce(e.value,"BOOLEAN"):e.dataType==="VARCHAR"?ce(e.value,"VARCHAR"):typeof e.value=="number"?Number.isInteger(e.value)?ce(e.value,"INT32"):ce(e.value,"FLOAT64"):ce(e.value,"VARCHAR")}bindBinaryExpr(e,t){let s=this.bindExpression(e.left,t),n=this.bindExpression(e.right,t),r=e.op;if(["=","<>","<",">","<=",">="].includes(r)||["AND","OR"].includes(r))return Cn(r,s,n,"BOOLEAN");if(["||"].includes(r))return Cn(r,s,n,"VARCHAR");let i=this.inferArithmeticType(nn(s),nn(n));return Cn(r,s,n,i)}bindUnaryExpr(e,t){let s=this.bindExpression(e.operand,t);return e.op==="NOT"?Zv("NOT",s,"BOOLEAN"):Zv(e.op,s,nn(s))}bindAggregateCall(e,t){let s=e.args.map(i=>this.bindExpression(i,t)),n=this.inferAggregateType(e.name,s),r=zw(e.name,s,e.distinct,n);return this.aggregatesFound.push(r),r}bindFunctionCall(e,t){let s=e.args.map(r=>this.bindExpression(r,t)),n=this.inferFunctionType(e.name,s);return tk(e.name.toUpperCase(),s,n)}bindCaseExpr(e,t){let s=e.operand?this.bindExpression(e.operand,t):null,n=e.whenClauses.map(o=>({condition:this.bindExpression(o.condition,t),result:this.bindExpression(o.result,t)})),r=e.elseExpr?this.bindExpression(e.elseExpr,t):null,i=nn(n[0]?.result)||"VARCHAR";return B4(s,n,r,i)}bindCastExpr(e,t){let s=this.bindExpression(e.expr,t),n=this.resolveTypeName(e.targetType);return LL(s,n)}bindInExpr(e,t){let s=this.bindExpression(e.expr,t),n=e.list;if(!Array.isArray(n)&&n.kind==="SubqueryExpr"){let i=t.child(),o=this.bindQuery(n.query,i);return kw(s,uM(o,"IN"),e.negated)}let r=n.map(i=>this.bindExpression(i,t));return kw(s,r,e.negated)}bindExistsExpr(e,t){let s=t.child(),n=this.bindQuery(e.query,s);return C4(n,e.negated)}bindSubqueryExpr(e,t){let s=t.child(),n=this.bindQuery(e.query,s);return uM(n,"SCALAR")}inferColumnName(e){return e.kind==="ColumnRef"?e.name:e.kind==="AggregateCall"||e.kind==="FunctionCall"?e.name.toLowerCase():null}inferArithmeticType(e,t){return e==="FLOAT64"||t==="FLOAT64"?"FLOAT64":e==="DECIMAL"||t==="DECIMAL"?"DECIMAL":e==="INT64"||t==="INT64"?"INT64":e==="DATE"?"DATE":"INT32"}inferAggregateType(e,t){switch(e.toUpperCase()){case"COUNT":case"COUNT_STAR":return"INT64";case"SUM":return t[0]&&nn(t[0])||"FLOAT64";case"AVG":return"FLOAT64";case"MIN":case"MAX":return t[0]&&nn(t[0])||"FLOAT64";default:return"FLOAT64"}}inferFunctionType(e,t){switch(e.toUpperCase()){case"SUBSTRING":case"TRIM":case"UPPER":case"LOWER":case"REPLACE":return"VARCHAR";case"EXTRACT":return"INT32";case"LENGTH":return"INT32";case"ABS":case"ROUND":case"SQRT":return t[0]?nn(t[0]):"FLOAT64";case"COALESCE":case"NULLIF":{for(let s of t){let n=nn(s);if(n)return n}return null}default:return"VARCHAR"}}inferWindowType(e,t){switch(e.toUpperCase()){case"ROW_NUMBER":case"RANK":case"DENSE_RANK":return"INT64";case"LAG":case"LEAD":return t[0]?nn(t[0]):"VARCHAR";case"SUM":case"AVG":case"MIN":case"MAX":case"COUNT":case"COUNT_STAR":return this.inferAggregateType(e,t);default:return"FLOAT64"}}resolveTypeName(e){let t=e.name.toUpperCase();return{INTEGER:"INT32",INT:"INT32",INT32:"INT32",BIGINT:"INT64",INT64:"INT64",FLOAT:"FLOAT64",DOUBLE:"FLOAT64",REAL:"FLOAT64",DECIMAL:"DECIMAL",NUMERIC:"DECIMAL",VARCHAR:"VARCHAR",TEXT:"VARCHAR",CHAR:"VARCHAR",DATE:"DATE",TIMESTAMP:"TIMESTAMP",DATETIME:"TIMESTAMP",BOOLEAN:"BOOLEAN",BOOL:"BOOLEAN"}[t]||"VARCHAR"}};Ft();Vt();var lW=0;function nL(e){let t=e.alias||e.inferredName;return t?{...e.expr,outputName:t}:e.expr}c(nL,"projectionExpr");var cW=class{static{c(this,"LogicalPlanner")}cteMap;constructor(){this.cteMap=new Map}plan(e){return this.planQuery(e)}planQuery(e){return e.type==="SetOp"?this.planSetOp(e):this.planSelect(e)}planSetOp(e){let t=this.planQuery(e.left),s=this.planQuery(e.right);return zL(t,s,e.all)}planSelect(e){let t=null;if(e.plan?t=this.planFrom(e.plan):t=U4(),e.where){let{expr:n,subqueryJoins:r}=this.extractSubqueries(e.where,t);for(let i of r)t=i(t);n&&(t=mn(n,t))}if((e.aggregates.length>0||e.groupBy)&&(t=id(e.groupBy||[],e.aggregates,t)),e.having){let{expr:n,subqueryJoins:r}=this.extractSubqueries(e.having,t);for(let i of r)t=i(t);n&&(t=mn(n,t))}for(let n=0;n<e.selectItems.length;n++){let{expr:r,subqueryJoins:i}=this.extractSubqueries(e.selectItems[n].expr,t);for(let o of i)t=o(t);e.selectItems[n]={...e.selectItems[n],expr:r}}let s=[];for(let n of e.selectItems)this._collectWindows(n.expr,s);if(s.length>0&&(t=P4(s,t)),e.distinct){let n=e.selectItems.map(nL);t=Ew(n,t),t=$L(t),e.orderBy&&(t=Nw(e.orderBy,t))}else{e.orderBy&&(t=Nw(e.orderBy,t));let n=e.selectItems.map(nL);t=Ew(n,t)}if(e.limit){let n=e.limit.value,r=e.offset?e.offset.value:0;t=FL(n,r,t)}return t}planFrom(e){switch(e.type){case"TableRef":return sk(e.tableName,e.columns,e.alias);case"CTERef":{let t=lW++,s=e.query.prebuiltPlan??this.planQuery(e.query);return this.cteMap.set(e.cteName.toUpperCase(),s),M4(e.cteName,t)}case"JoinRef":{let t=this.planFrom(e.left),s=this.planFrom(e.right),n=e.joinType==="CROSS"?"CROSS":Rw[e.joinType];return Lr(n,e.condition,t,s)}case"SubqueryRef":return this.planQuery(e.query);default:throw new Error(`Unknown from type: ${e.type}`)}}extractSubqueries(e,t){let s=[];return{expr:this.walkAndReplace(e,r=>{if(r.kind==="BoundUnary"&&r.op==="NOT"&&r.operand?.kind==="BoundExists"){let i=this.planQuery(r.operand.plan),o=this.findCorrelatedRefs(r.operand.plan);return s.push(a=>Z_(a,i,o,"NOT_EXISTS",null)),null}if(r.kind==="BoundExists"){let i=this.planQuery(r.plan),o=this.findCorrelatedRefs(r.plan),a=r.negated?"NOT_EXISTS":"EXISTS";return s.push(l=>Z_(l,i,o,a,null)),null}if(r.kind==="BoundInList"&&!Array.isArray(r.list)&&r.list.kind==="BoundSubquery"){let i=this.planQuery(r.list.plan),o=this.findCorrelatedRefs(r.list.plan),a=r.negated?"NOT_IN":"IN";return s.push(l=>Z_(l,i,o,a,r.expr)),null}if(r.kind==="BoundSubquery"&&r.subqueryType==="SCALAR"){let i=this.planQuery(r.plan),o=this.findCorrelatedRefs(r.plan);return s.push(a=>Z_(a,i,o,"SCALAR",null)),{kind:"BoundColumnRef",tableAlias:"",columnName:"_scalar",columnIndex:-1,dataType:"FLOAT64",depth:0,isCorrelated:!1}}return r}),subqueryJoins:s}}walkAndReplace(e,t){if(!e)return null;let s=t(e);if(s!==e)return s;switch(e.kind){case"BoundBinary":return{...e,left:this.walkAndReplace(e.left,t),right:this.walkAndReplace(e.right,t)};case"BoundUnary":return{...e,operand:this.walkAndReplace(e.operand,t)};case"BoundCase":return{...e,operand:e.operand?this.walkAndReplace(e.operand,t):null,whenClauses:e.whenClauses.map(n=>({condition:this.walkAndReplace(n.condition,t),result:this.walkAndReplace(n.result,t)})),elseExpr:e.elseExpr?this.walkAndReplace(e.elseExpr,t):null};case"BoundBetween":return{...e,expr:this.walkAndReplace(e.expr,t),low:this.walkAndReplace(e.low,t),high:this.walkAndReplace(e.high,t)};default:return e}}_collectWindows(e,t){if(e)switch(e.kind){case"BoundWindow":t.push(e);return;case"BoundBinary":this._collectWindows(e.left,t),this._collectWindows(e.right,t);return;case"BoundUnary":this._collectWindows(e.operand,t);return;case"BoundFunction":case"BoundAggregate":for(let s of e.args)this._collectWindows(s,t);return;case"BoundCase":e.operand&&this._collectWindows(e.operand,t);for(let s of e.whenClauses)this._collectWindows(s.condition,t),this._collectWindows(s.result,t);e.elseExpr&&this._collectWindows(e.elseExpr,t);return}}findCorrelatedRefs(e){let t=[];return this._scanQuery(e,t),t}_scanQuery(e,t){if(e.type==="SetOp"){this._scanQuery(e.left,t),this._scanQuery(e.right,t);return}e.plan&&this._scanFrom(e.plan,t),e.where&&this._scanExpr(e.where,t),e.having&&this._scanExpr(e.having,t);for(let s of e.groupBy||[])this._scanExpr(s,t);for(let s of e.aggregates)this._scanExpr(s,t);for(let s of e.selectItems)this._scanExpr(s.expr,t);for(let s of e.orderBy||[])this._scanExpr(s.expr,t);e.limit&&this._scanExpr(e.limit,t),e.offset&&this._scanExpr(e.offset,t)}_scanFrom(e,t){switch(e.type){case"JoinRef":this._scanFrom(e.left,t),this._scanFrom(e.right,t),e.condition&&this._scanExpr(e.condition,t);return;case"SubqueryRef":this._scanQuery(e.query,t);return;case"CTERef":this._scanQuery(e.query,t);return}}_scanExpr(e,t){if(e)switch(e.kind){case"BoundColumnRef":e.isCorrelated&&t.push(e);return;case"BoundBinary":this._scanExpr(e.left,t),this._scanExpr(e.right,t);return;case"BoundUnary":this._scanExpr(e.operand,t);return;case"BoundFunction":case"BoundAggregate":for(let s of e.args)this._scanExpr(s,t);return;case"BoundCase":e.operand&&this._scanExpr(e.operand,t);for(let s of e.whenClauses)this._scanExpr(s.condition,t),this._scanExpr(s.result,t);e.elseExpr&&this._scanExpr(e.elseExpr,t);return;case"BoundCast":this._scanExpr(e.expr,t);return;case"BoundBetween":this._scanExpr(e.expr,t),this._scanExpr(e.low,t),this._scanExpr(e.high,t);return;case"BoundInList":if(this._scanExpr(e.expr,t),Array.isArray(e.list))for(let s of e.list)this._scanExpr(s,t);else this._scanExpr(e.list,t);return;case"BoundLike":this._scanExpr(e.expr,t),this._scanExpr(e.pattern,t);return;case"BoundIsNull":this._scanExpr(e.expr,t);return;case"BoundExtract":this._scanExpr(e.source,t);return;case"BoundWindow":for(let s of e.args)this._scanExpr(s,t);for(let s of e.partitionBy)this._scanExpr(s,t);for(let s of e.orderBy)this._scanExpr(s.expr,t);return;case"BoundSubquery":this._scanQuery(e.plan,t);return;case"BoundExists":this._scanQuery(e.plan,t);return}}};function uW(e){let t=new cW,s=t.plan(e);return s._cteMap=t.cteMap,s}c(uW,"createLogicalPlan");var fW=class{static{c(this,"FunctionRegistry")}functions;constructor(){this.functions=new Map,this._registerBuiltins()}register(e,t){this.functions.set(e.toUpperCase(),t)}lookup(e){return this.functions.get(e.toUpperCase())||null}has(e){return this.functions.has(e.toUpperCase())}isAggregate(e){return this.lookup(e)?.type==="AGGREGATE"}isScalar(e){return this.lookup(e)?.type==="SCALAR"}_registerBuiltins(){let e=c((s,n=1,r=1)=>({name:s.toUpperCase(),type:"AGGREGATE",minArgs:n,maxArgs:r}),"agg"),t=c((s,n,r)=>({name:s.toUpperCase(),type:"SCALAR",minArgs:n,maxArgs:r??n}),"scalar");this.register("SUM",e("SUM")),this.register("AVG",e("AVG")),this.register("COUNT",e("COUNT",0,1)),this.register("MIN",e("MIN")),this.register("MAX",e("MAX")),this.register("COUNT_STAR",e("COUNT_STAR",0,0)),this.register("SUBSTRING",t("SUBSTRING",2,3)),this.register("EXTRACT",t("EXTRACT",2,2)),this.register("TRIM",t("TRIM",1,1)),this.register("UPPER",t("UPPER",1,1)),this.register("LOWER",t("LOWER",1,1)),this.register("CAST",t("CAST",2,2)),this.register("COALESCE",t("COALESCE",1,1/0)),this.register("NULLIF",t("NULLIF",2,2)),this.register("ABS",t("ABS",1,1)),this.register("ROUND",t("ROUND",1,2)),this.register("SQRT",t("SQRT",1,1)),this.register("LENGTH",t("LENGTH",1,1)),this.register("REPLACE",t("REPLACE",3,3))}},hW=new fW;we();be();var pW=class{static{c(this,"LRUCache")}_maxSize;_map;_head;_tail;constructor(e){this._maxSize=e,this._map=new Map,this._head=null,this._tail=null}_detach(e){e.prev?e.prev.next=e.next:this._head=e.next,e.next?e.next.prev=e.prev:this._tail=e.prev,e.prev=null,e.next=null}_prepend(e){e.next=this._head,e.prev=null,this._head&&(this._head.prev=e),this._head=e,this._tail||(this._tail=e)}_moveToHead(e){e!==this._head&&(this._detach(e),this._prepend(e))}_evictTail(){let e=this._tail;return e?(this._map.delete(e.key),this._detach(e),e.key):null}get(e){let t=this._map.get(e);if(t)return this._moveToHead(t),t.value}set(e,t){let s=this._map.get(e);if(s)return s.value=t,this._moveToHead(s),null;let n={key:e,value:t,prev:null,next:null};return this._map.set(e,n),this._prepend(n),this._map.size>this._maxSize?this._evictTail():null}has(e){return this._map.has(e)}delete(e){let t=this._map.get(e);return t?(this._detach(t),this._map.delete(e),!0):!1}clear(){this._map.clear(),this._head=null,this._tail=null}get size(){return this._map.size}},dW=class{static{c(this,"BufferPoolManager")}maxPages;cache;pageStore;constructor(e,t){this.maxPages=e,this.cache=new pW(e),this.pageStore=t}clear(){this.cache.clear(),this.pageStore.clear()}async writePage(e,t){await this.pageStore.write(e,t)}async readPage(e){return this.pageStore.read(e)}async fetchPage(e,t=!1){let s=this.cache.get(e);if(s!==void 0)return s;let n=await this.readPage(e);return t||this.cache.set(e,n),n}};Fe();var Vv=class{static{c(this,"Table")}name;schema;pageIds;_rowCount;bufferPool;activeChunk;indexes;constructor(e,t,s){this.name=e,this.schema=t,this.pageIds=[],this._rowCount=0,this.bufferPool=new dW(ht.bufferPoolPages,s),this.activeChunk=null,this.indexes=[]}getSchema(){return this.schema}getColumnIndex(e){let t=e.toUpperCase();return this.schema.findIndex(s=>s.name.toUpperCase()===t)}getColumn(e){return this.schema.find(t=>t.name.toUpperCase()===e.toUpperCase())}rowCount(){return this._rowCount+(this.activeChunk?this.activeChunk.size:0)}registerIndex(e,t){this.indexes.push({columnIndex:e,btree:t})}async addChunk(e){let t=`${this.name}_page_${this.pageIds.length}`;this.pageIds.push(t),this._rowCount+=e.size,await this.bufferPool.writePage(t,e);for(let s of this.indexes)for(let n=0;n<e.size;n++){let r=e.columns[s.columnIndex].get(n);r!=null&&s.btree.insert(r,{pageId:t,rowIndex:n})}}async insertRows(e){this.activeChunk||(this.activeChunk=this._createChunk());for(let t of e)this.activeChunk.size>=We&&(await this.addChunk(this.activeChunk),this.activeChunk=this._createChunk()),this.activeChunk.appendRow(t)}async flush(){this.activeChunk&&this.activeChunk.size>0&&(await this.addChunk(this.activeChunk),this.activeChunk=null)}async*scan(){await this.flush();for(let e of this.pageIds)yield await this.bufferPool.fetchPage(e,!0)}async scanAll(){await this.flush();let e=[];for(let t of this.pageIds)e.push(await this.bufferPool.fetchPage(t,!1));return e}getStatistics(){return null}_createChunk(){return kt.fromSchema(this.schema,We)}},mW=class{static{c(this,"Optimizer")}passes;constructor(){this.passes=[]}registerPass(e){return this.passes.push(e),this}removePass(e){return this.passes=this.passes.filter(t=>t.name!==e),this}insertPassBefore(e,t){let s=this.passes.findIndex(n=>n.name===e);return s===-1?this.passes.push(t):this.passes.splice(s,0,t),this}insertPassAfter(e,t){let s=this.passes.findIndex(n=>n.name===e);return s===-1?this.passes.push(t):this.passes.splice(s+1,0,t),this}optimize(e,t={}){let s=e;for(let n of this.passes)s=n.apply(s,t);return s}listPasses(){return this.passes.map(e=>e.name)}};$e();Ft();Ke();Vt();$e();Ft();Ke();Vt();var lw=class extends Be{static{c(this,"PredicatePushdown")}get name(){return"PredicatePushdown"}apply(e){return new gW().rewrite(e)}},gW=class extends ze{static{c(this,"PushdownRewriter")}rewriteJoin(e){let t=this.rewriteChildren(e);return yW(t)}rewriteFilter(e){let t=this.rewrite(e.children[0]),s=In(e.condition);return Hl(s,t)}rewriteDefault(e){return this.rewriteChildren(e)}};function yW(e){if(!e.condition)return e;let t=Lw(e.children[1]),s=[],n=[];for(let o of In(e.condition)){let a=lE(o),l=a.length>0&&a.every(u=>Pw(u,t));e.joinType==="LEFT"&&l?s.push(o):n.push(o)}if(s.length===0)return e;let r=e.children[1];return s.length>0&&(r=Hl(s,r)),{...Lr(e.joinType==="CROSS"&&n.length>0?"INNER":e.joinType,Cs(n),e.children[0],r),..._W(e)}}c(yW,"pushJoinConditionPredicates");function _W(e){let t={},s=e;s.markColumn&&(t.markColumn=s.markColumn);for(let n of Object.keys(s))n.startsWith("_")&&(t[n]=s[n]);return t}c(_W,"copyJoinProperties");function Hl(e,t){if(t.type==="Join")return bW(e,t);if(t.type==="Filter"){let n=[...In(t.condition),...e],r=t.children[0];return Hl(n,r)}if(t.type==="Aggregate"&&t.groupBy&&t.groupBy.length>0){let s=new Set;for(let i of t.groupBy)i.kind==="BoundColumnRef"&&s.add(`${(i.tableAlias||"").toUpperCase()}.${(i.columnName||"").toUpperCase()}`);let n=[],r=[];for(let i of e){let o=lE(i);o.length>0&&!SW(i)&&o.every(a=>s.has(`${a.tableAlias}.${a.columnName}`))?n.push(i):r.push(i)}if(n.length>0){let i=Hl(n,t.children[0]),o={...t,children:[i]};return r.length===0?o:mn(Cs(r),o)}}if(t.type==="Project"){let s=[],n=[];for(let r of e)wW(r,t)?s.push(r):n.push(r);if(s.length>0){let r=Hl(s,t.children[0]),i={...t,children:[r]};return n.length===0?i:mn(Cs(n),i)}}return e.length===0?t:mn(Cs(e),t)}c(Hl,"pushPredicates");function wW(e,t){let s=new Set;dn(e,r=>{r.kind==="BoundColumnRef"&&s.add({tableAlias:(r.tableAlias||"").toUpperCase(),columnName:(r.columnName||"").toUpperCase()})});let n=Lw(t.children[0]);for(let r of s)if(!Pw(r,n))return!1;return!0}c(wW,"canPushThroughProject");function bW(e,t){let s=Lw(t.children[0]),n=Lw(t.children[1]),r=[],i=[],o=[],a=[];for(let d of e){let m=lE(d),g=m.every(_=>Pw(_,s)),y=m.every(_=>Pw(_,n));t.joinType==="INNER"||t.joinType==="CROSS"?g?r.push(d):y?i.push(d):o.push(d):t.joinType==="LEFT"?g?r.push(d):y&&xW(d)?(i.push(d),t={...t,joinType:"INNER"}):a.push(d):t.joinType==="SEMI"||t.joinType==="ANTI"||t.joinType==="MARK"?g?r.push(d):y?i.push(d):a.push(d):a.push(d)}let l=t.children[0],u=t.children[1];r.length>0&&(l=Hl(r,l)),i.length>0&&(u=Hl(i,u));let f=t.condition;if(o.length>0){let d=f?[f,...o]:o;f=Cs(d)}let h=Lr(t.joinType==="CROSS"&&f?"INNER":t.joinType,f,l,u),p=t.markColumn;return p&&(h.markColumn=p),a.length>0&&(h=mn(Cs(a),h)),h}c(bW,"pushIntoJoin");function xW(e){return e.kind==="BoundBinary"?["=","<>","<",">","<=",">="].includes(e.op):!(e.kind==="BoundIsNull"&&!e.negated)}c(xW,"rejectsNulls");function In(e){return e?e.kind==="BoundBinary"&&e.op==="AND"?[...In(e.left),...In(e.right)]:[e]:[]}c(In,"splitConjuncts");function Cs(e){return e.length===0?null:e.length===1?e[0]:e.reduce((t,s)=>({kind:"BoundBinary",op:"AND",left:t,right:s,resultType:"BOOLEAN"}))}c(Cs,"combineConjuncts");function Lw(e){let t={aliases:new Set,columns:new Set};return Ak(e,t),t.aliases.delete(""),t.columns.delete(""),t}c(Lw,"collectPlanRefs");function Ak(e,t){if(e){if(e.type==="Scan"){t.aliases.add(e.alias?.toUpperCase()||e.table?.toUpperCase());for(let s of e.columns||[])t.columns.add((s.name||s.columnName||"").toUpperCase());return}if(e.type==="CTEScan"){t.aliases.add((e.alias||e.cteName||"").toUpperCase());return}if(e.type==="Project"){for(let s of e.expressions||[]){let n=s;t.columns.add((n.outputName||n.alias||n.name||n.columnName||"").toUpperCase())}return}if(e.type==="Aggregate"){for(let s of e.groupBy||[]){let n=s;t.columns.add((n.outputName||n.alias||n.name||n.columnName||"").toUpperCase())}for(let s of e.aggregates||[]){let n=s;t.columns.add((n.outputName||n.alias||n.name||"").toUpperCase())}return}if(e.type==="Join"||e.type==="Union"){for(let s of cs(e))Ak(s,t);return}e.children?.[0]&&Ak(e.children[0],t)}}c(Ak,"addOutputRefs");function Pw(e,t){return e.tableAlias?t.aliases.has(e.tableAlias):t.columns.has(e.columnName)}c(Pw,"refBelongsToPlan");function SW(e){let t=!1;return dn(e,s=>{s.kind==="BoundAggregate"&&(t=!0)}),t}c(SW,"containsAggregate");function lE(e){let t=new Set;return dn(e,s=>{s.kind==="BoundColumnRef"&&t.add(`${(s.tableAlias||"").toUpperCase()}.${(s.columnName||"").toUpperCase()}`)}),[...t].map(s=>{let[n,r]=s.split(".");return{tableAlias:n,columnName:r}})}c(lE,"collectTableRefs");function dn(e,t){if(!e||typeof e!="object")return;t(e);let s=e;if(s.left&&dn(s.left,t),s.right&&dn(s.right,t),s.operand&&dn(s.operand,t),s.expr&&dn(s.expr,t),s.low&&dn(s.low,t),s.high&&dn(s.high,t),s.args)for(let n of s.args)dn(n,t);if(s.whenClauses)for(let n of s.whenClauses)dn(n.condition,t),dn(n.result,t);if(s.elseExpr&&dn(s.elseExpr,t),s.list&&Array.isArray(s.list))for(let n of s.list)dn(n,t);s.pattern&&dn(s.pattern,t),s.source&&dn(s.source,t)}c(dn,"_walkExpr");var AW=class extends Be{static{c(this,"SubqueryUnnesting")}get name(){return"SubqueryUnnesting"}apply(e){let t=e,s=!0;for(;s;){let n=new vW,r=n.rewrite(t);s=n.didChange,t=r}return t}},vW=class extends ze{static{c(this,"UnnestingRewriter")}didChange;markId;constructor(){super(),this.didChange=!1,this.markId=0}rewriteDependentJoin(e){this.didChange=!0;let t=this.rewrite(e.children[0]),s=this.rewrite(e.children[1]),n=e.correlatedColumns||[];switch(e.subqueryType){case"EXISTS":return this.unnestExists(t,s,n);case"NOT_EXISTS":return this.unnestNotExists(t,s,n);case"IN":return this.unnestIn(t,s,n,e.condition);case"NOT_IN":return this.unnestNotIn(t,s,n,e.condition);case"SCALAR":return this.unnestScalar(t,s,n);default:return Tn(e,[t,s])}}unnestExists(e,t,s){let{cleanedPlan:n,joinCondition:r}=this.extractCorrelation(t,s);return Lr("SEMI",r,e,this.removeProjection(n))}unnestNotExists(e,t,s){let{cleanedPlan:n,joinCondition:r}=this.extractCorrelation(t,s);return Lr("ANTI",r,e,this.removeProjection(n))}unnestIn(e,t,s,n){let{cleanedPlan:r,joinCondition:i}=this.extractCorrelation(t,s),o=[];if(i&&o.push(i),n){let a=this.getSubqueryOutputRef(t);a&&o.push({kind:"BoundBinary",op:"=",left:n,right:a,resultType:"BOOLEAN"})}return Lr("SEMI",Cs(o),e,this.exposeCorrelationColumns(r))}unnestNotIn(e,t,s,n){let{cleanedPlan:r,joinCondition:i}=this.extractCorrelation(t,s),o=[];if(i&&o.push(i),n){let f=this.getSubqueryOutputRef(t);f&&o.push({kind:"BoundBinary",op:"=",left:n,right:f,resultType:"BOOLEAN"})}let a=`__mark_${this.markId++}`,l={kind:"BoundColumnRef",tableAlias:"",columnName:a,columnIndex:-1,dataType:"BOOLEAN",depth:0,isCorrelated:!1},u={...Lr("MARK",Cs(o),e,this.exposeCorrelationColumns(r)),markColumn:a};return mn({kind:"BoundBinary",op:"=",left:l,right:{kind:"BoundLiteral",value:!1,dataType:"BOOLEAN"},resultType:"BOOLEAN"},u)}unnestScalar(e,t,s){let{cleanedPlan:n,joinCondition:r,correlatedPredicates:i}=this.extractCorrelation(t,s),o=this.getSubqueryOutputRef(t);if(s.length>0&&this.hasAggregate(t)){let a=this.getInnerCorrelationExprs(i,s),l=this.removeProjection(n),u=this.addGroupBy(l,a),f=this.projectScalarOutput(u,a,o);return Lr("LEFT",r,e,f)}return Lr("SINGLE",r,e,this.projectScalarOutput(n,[],o))}extractCorrelation(e,t){let s=[],n=this.removeCorrelatedPredicates(e,t,s),r=s.map(i=>this.rewriteCorrelatedPredicate(i,t));return{cleanedPlan:n,joinCondition:Cs(r),correlatedPredicates:s}}removeCorrelatedPredicates(e,t,s){if(!e)return e;if(e.type==="Filter"){let o=this.removeCorrelatedPredicates(e.children[0],t,s),{correlatedPreds:a,localPreds:l}=this.partitionPredicates(e.condition,t);return s.push(...a),l.length===0?o:mn(Cs(l),o)}if(e.type==="Project"){let o=this.removeCorrelatedPredicates(e.children[0],t,s);return Tn(e,[o])}let n=cs(e),r=n.map(o=>this.removeCorrelatedPredicates(o,t,s));return r.some((o,a)=>o!==n[a])?Tn(e,r):e}partitionPredicates(e,t){let s=[],n=[],r=this.splitAnd(e);for(let i of r)this.hasCorrelatedRef(i,t)?s.push(i):n.push(i);return{correlatedPreds:s,localPreds:n}}hasCorrelatedRef(e,t){if(!e||typeof e!="object")return!1;let s=e;if(s.kind==="BoundColumnRef"&&s.isCorrelated)return!0;if(s.kind==="BoundColumnRef")return t.some(n=>n.tableAlias===s.tableAlias&&n.columnName===s.columnName);for(let n of Object.values(e))if(Array.isArray(n)){for(let r of n)if(this.hasCorrelatedRef(r,t))return!0}else if(n&&typeof n=="object"&&this.hasCorrelatedRef(n,t))return!0;return!1}rewriteCorrelatedPredicate(e,t){return this.rewriteExprRefs(e,t)}rewriteExprRefs(e,t){if(!e||typeof e!="object")return e;if(e.kind==="BoundColumnRef"&&e.isCorrelated)return{...e,depth:0,isCorrelated:!1};if(e.kind==="BoundColumnRef"&&t.find(n=>n.tableAlias===e.tableAlias&&n.columnName===e.columnName))return{...e,depth:0,isCorrelated:!1};switch(e.kind){case"BoundBinary":return{...e,left:this.rewriteExprRefs(e.left,t),right:this.rewriteExprRefs(e.right,t)};case"BoundUnary":return{...e,operand:this.rewriteExprRefs(e.operand,t)};case"BoundFunction":case"BoundAggregate":return{...e,args:e.args.map(s=>this.rewriteExprRefs(s,t))};case"BoundCase":return{...e,operand:e.operand?this.rewriteExprRefs(e.operand,t):null,whenClauses:e.whenClauses.map(s=>({condition:this.rewriteExprRefs(s.condition,t),result:this.rewriteExprRefs(s.result,t)})),elseExpr:e.elseExpr?this.rewriteExprRefs(e.elseExpr,t):null};case"BoundCast":return{...e,expr:this.rewriteExprRefs(e.expr,t)};case"BoundBetween":return{...e,expr:this.rewriteExprRefs(e.expr,t),low:this.rewriteExprRefs(e.low,t),high:this.rewriteExprRefs(e.high,t)};case"BoundInList":return{...e,expr:this.rewriteExprRefs(e.expr,t),list:Array.isArray(e.list)?e.list.map(s=>this.rewriteExprRefs(s,t)):this.rewriteExprRefs(e.list,t)};case"BoundLike":return{...e,expr:this.rewriteExprRefs(e.expr,t),pattern:this.rewriteExprRefs(e.pattern,t)};case"BoundIsNull":return{...e,expr:this.rewriteExprRefs(e.expr,t)};case"BoundExtract":return{...e,source:this.rewriteExprRefs(e.source,t)};case"BoundWindow":return{...e,args:e.args.map(s=>this.rewriteExprRefs(s,t)),partitionBy:e.partitionBy.map(s=>this.rewriteExprRefs(s,t)),orderBy:e.orderBy.map(s=>({...s,expr:this.rewriteExprRefs(s.expr,t)}))};default:return e}}splitAnd(e){return e?e.kind==="BoundBinary"&&e.op==="AND"?[...this.splitAnd(e.left),...this.splitAnd(e.right)]:[e]:[]}getSubqueryOutputRef(e){let t=e;for(;t;){if(t.type==="Project"&&t.expressions?.length>0)return t.expressions[0];t=t.children?.[0]}return null}getInnerCorrelationExprs(e,t){let s=[],n=new Set;for(let r of e){if(r.kind!=="BoundBinary"||r.op!=="=")continue;let i=this.hasCorrelatedRef(r.left,t),o=this.hasCorrelatedRef(r.right,t),a=null;if(i&&!o?a=r.right:o&&!i&&(a=r.left),!a)continue;let l=this.rewriteExprRefs(a,t),u=JSON.stringify(l);n.has(u)||(n.add(u),s.push(l))}return s}projectScalarOutput(e,t,s){if(!s)return e;let n={...s,outputName:"_scalar"};return{type:"Project",expressions:[...t,n],children:[e]}}hasAggregate(e){if(!e)return!1;if(e.type==="Aggregate")return!0;for(let t of cs(e))if(this.hasAggregate(t))return!0;return!1}removeProjection(e){return e.type==="Project"?e.children[0]:e}exposeCorrelationColumns(e){return e.type==="Project"&&(e.expressions||[]).every(t=>t.kind==="BoundColumnRef")?e.children[0]:e}addGroupBy(e,t){if(e.type==="Aggregate")return{...e,groupBy:[...t,...e.groupBy||[]]};let n=cs(e).map(r=>this.addGroupBy(r,t));return Tn(e,n)}};$e();Ft();Ke();var kW=class extends Be{static{c(this,"CTEOptimization")}get name(){return"CTEOptimization"}apply(e){let t=EW(e);return new NW(t).rewrite(e)}};function EW(e){let t=new Map;return LD(e,s=>{if(s.type==="CTEScan"){let n=s.cteName.toUpperCase();t.set(n,(t.get(n)||0)+1)}}),t}c(EW,"countCTERefs");var NW=class extends ze{static{c(this,"CTERewriter")}refCounts;ctePlans;constructor(e){super(),this.refCounts=e,this.ctePlans=new Map}rewriteCTEAnchor(e){let t=this.rewrite(e.children[0]),s=this.rewrite(e.children[1]),n=e.cteName.toUpperCase();return(this.refCounts.get(n)||0)<=1?(this.ctePlans.set(n,t),s):(this.ctePlans.set(n,D4(t)),s)}rewriteCTEScan(e){let t=e.cteName.toUpperCase(),s=this.ctePlans.get(t);return s||e}};function LD(e,t){if(e){t(e);for(let s of cs(e))LD(s,t)}}c(LD,"_walkPlan");$e();Ft();Vt();var RW=class extends Be{static{c(this,"ProjectionPushdown")}get name(){return"ProjectionPushdown"}apply(e){return ni(e,null)}};function ni(e,t){if(!e)return e;switch(e.type){case"Scan":return BW(e,t);case"Project":return CW(e,t);case"Filter":return nd(e,t?PD(t,e.condition):null);case"Join":return OW(e,t);case"Aggregate":return TW(e);case"Sort":return IW(e,t);case"Limit":case"Materialize":return nd(e,t);case"DependentJoin":return MW(e,t);case"Union":case"CTEAnchor":return vk(e,null);case"Distinct":return nd(e,null);default:return vk(e,t)}}c(ni,"pruneColumns");function BW(e,t){if(e.type!=="Scan"||!t||t.size===0)return e;let s=kk(e),n=e.columns.filter(r=>PW(t,s.aliases,r.name));return n.length>0&&n.length<e.columns.length?{...e,columns:n}:e}c(BW,"pruneScan");function CW(e,t){if(e.type!=="Project")return e;let s=new Set;for(let[r,i]of(e.expressions||[]).entries())(!t||LW(i,t,r))&&ko(i,s);let n=ni(e.children[0],s);return n!==e.children[0]?Tn(e,[n]):e}c(CW,"pruneProject");function TW(e){if(e.type!=="Aggregate")return e;let t=new Set;for(let n of e.groupBy||[])ko(n,t);for(let n of e.aggregates||[])for(let r of n.args||[])ko(r,t);let s=ni(e.children[0],t);return s!==e.children[0]?Tn(e,[s]):e}c(TW,"pruneAggregate");function IW(e,t){if(e.type!=="Sort")return e;if(!t)return nd(e,null);let s=Jw(t);for(let n of e.orderKeys||[])s=PD(s,n.expr);return nd(e,s)}c(IW,"pruneSort");function OW(e,t){if(e.type!=="Join")return e;let s=e.children[0],n=e.children[1];if(!t){let h=ni(s,null),p=ni(n,null);return h!==s||p!==n?Tn(e,[h,p]):e}let r=kk(s),i=kk(n),o=Jw(t);e.condition&&ko(e.condition,o);let a=rL(o,r),l=rL(o,i),u=ni(s,a),f=ni(n,l);return u!==s||f!==n?Tn(e,[u,f]):e}c(OW,"pruneJoin");function MW(e,t){if(e.type!=="DependentJoin")return e;let s=Jw(t);e.condition&&ko(e.condition,s);for(let o of e.correlatedColumns||[])ko(o,s);let n=e.children||[];if(n.length!==2)return vk(e,s);let r=ni(n[0],s),i=ni(n[1],null);return r!==n[0]||i!==n[1]?Tn(e,[r,i]):e}c(MW,"pruneDependentJoin");function nd(e,t){let s=e.children?.[0];if(!s)return e;let n=ni(s,t);return n!==s?Tn(e,[n]):e}c(nd,"pruneUnary");function vk(e,t){let s=cs(e),n=s.map(i=>ni(i,t));return n.some((i,o)=>i!==s[o])?Tn(e,n):e}c(vk,"pruneChildren");function PD(e,t){let s=Jw(e);return ko(t,s),s}c(PD,"addExprRefs");function Jw(e){return e?new Set(e):new Set}c(Jw,"copyRefs");function ko(e,t){if(!e||typeof e!="object")return;let s=e;if(s.kind==="BoundColumnRef"){t.add(iL(s.tableAlias,s.columnName)),Number.isInteger(s.columnIndex)&&s.columnIndex>=0&&t.add(iL(s.tableAlias,`#${s.columnIndex}`));return}for(let n of Object.values(e))if(Array.isArray(n))for(let r of n)ko(r,t);else n&&typeof n=="object"&&ko(n,t)}c(ko,"collectExprColumns");function kk(e){let t={aliases:new Set,columns:new Set};return Sw(e,t),t.aliases.delete(""),t.columns.delete(""),t}c(kk,"collectPlanRefs2");function Sw(e,t){if(e){if(e.type==="Scan"){t.aliases.add((e.alias||e.table||"").toUpperCase());for(let s of e.columns||[])t.columns.add((s.name||s.columnName||"").toUpperCase());return}if(e.type==="CTEScan"){t.aliases.add((e.alias||e.cteName||"").toUpperCase());return}if(e.type==="Project"){for(let s of e.expressions||[])t.columns.add(Aw(s));for(let s of cs(e))Sw(s,t);return}if(e.type==="Aggregate"){for(let s of e.groupBy||[])t.columns.add(Aw(s));for(let s of e.aggregates||[])t.columns.add(Aw(s));return}if(e.type==="Join"||e.type==="Union"){for(let s of cs(e))Sw(s,t);return}e.children?.[0]&&Sw(e.children[0],t)}}c(Sw,"addOutputRefs2");function Aw(e){let t=e;return(t?.outputName||t?.alias||t?.name||t?.columnName||"").toUpperCase()}c(Aw,"outputName");function LW(e,t,s){let n=Aw(e);if(!n&&s===void 0)return!0;for(let r of t){let{columnName:i}=cE(r);if(n&&i===n||s!==void 0&&i===`#${s}`)return!0}return!1}c(LW,"outputNeeded");function rL(e,t){let s=new Set;for(let n of e||[]){let r=cE(n);DW(r,t)&&s.add(n)}return s}c(rL,"filterRefsForPlan");function PW(e,t,s){let n=(s||"").toUpperCase();for(let r of e){let i=cE(r);if(i.columnName===n&&(!i.tableAlias||i.tableAlias==="."||t.has(i.tableAlias)))return!0}return!1}c(PW,"refSetNeedsColumn");function DW(e,t){return e.tableAlias&&e.tableAlias!=="."?t.aliases.has(e.tableAlias):t.columns.has(e.columnName)}c(DW,"refBelongsToPlan2");function iL(e,t){return`${(e||"").toUpperCase()}.${(t||"").toUpperCase()}`}c(iL,"refKey");function cE(e){let t=e.indexOf(".");return t<0?{tableAlias:"",columnName:e.toUpperCase()}:{tableAlias:e.slice(0,t).toUpperCase(),columnName:e.slice(t+1).toUpperCase()}}c(cE,"parseRef");$e();Ft();Ke();Vt();var FW=class{static{c(this,"HyperEdge")}leftMask;rightMask;predicate;constructor(e,t,s){this.leftMask=e,this.rightMask=t,this.predicate=s}},$W=class{static{c(this,"HyperGraph")}relations;relationIndex;edges;adjacency;constructor(){this.relations=[],this.relationIndex=new Map,this.edges=[],this.adjacency=[]}addRelation(e,t,s){let n=this.relations.length;if(n>=30)return-1;let r=1<<n;return this.relations.push({id:n,name:e,plan:t,cardinality:s,mask:r}),this.relationIndex.set(e.toUpperCase(),n),this.adjacency.push(0),n}addEdge(e,t,s){let n=0;for(let i of e){let o=this.relationIndex.get(i.toUpperCase());o!==void 0&&(n|=1<<o)}let r=0;for(let i of t){let o=this.relationIndex.get(i.toUpperCase());o!==void 0&&(r|=1<<o)}if(!(n===0||r===0)){this.edges.push(new FW(n,r,s));for(let i=0;i<this.relations.length;i++){let o=1<<i;n&o&&(this.adjacency[i]|=r),r&o&&(this.adjacency[i]|=n)}}}getNeighborhood(e){let t=0;for(let s=0;s<this.relations.length;s++)e&1<<s&&(t|=this.adjacency[s]);return t&~e}isConnected(e){if(e===0)return!1;let t=UW(e),s=t,n=t;for(;n!==0;){let r=0;for(let i=0;i<this.relations.length;i++){if(!(n&1<<i))continue;let o=this.adjacency[i]&e&~s;r|=o,s|=o}n=r}return s===e}findJoinPredicates(e,t){let s=[],n=new Set;for(let r of this.edges){let i=r.leftMask|r.rightMask,o=e|t;if((i&o)!==i)continue;let a=(r.leftMask&e)!==0&&(r.rightMask&t)!==0,l=(r.leftMask&t)!==0&&(r.rightMask&e)!==0;(a||l)&&!n.has(r)&&(n.add(r),s.push(r.predicate))}return s}get size(){return this.relations.length}get fullMask(){return(1<<this.relations.length)-1}};function zW(e,t,s){let n=new $W;for(let r of e){let i=s.estimatePlan?s.estimatePlan(r.plan):s.estimateScan(r.name);if(n.addRelation(r.alias||r.name,r.plan,i)===-1)return n}for(let r of t){let i=Gv(r);if(i.size<2)continue;let o=[],a=[];if(r.kind==="BoundBinary"&&(o=[...Gv(r.left)],a=[...Gv(r.right)]),o.length>0&&a.length>0)n.addEdge(o,a,r);else{let l=[...i];n.addEdge([l[0]],l.slice(1),r)}}return n}c(zW,"buildHyperGraph");function Gv(e){let t=new Set;return Qp(e,s=>{s.kind==="BoundColumnRef"&&s.tableAlias&&t.add(s.tableAlias.toUpperCase())}),t}c(Gv,"collectColumnTableRefs");function Qp(e,t){if(e)switch(t(e),e.kind){case"BoundBinary":Qp(e.left,t),Qp(e.right,t);return;case"BoundUnary":Qp(e.operand,t);return;case"BoundFunction":case"BoundAggregate":for(let s of e.args)Qp(s,t);return}}c(Qp,"_walkExpr2");function UW(e){return e&-e}c(UW,"lowestBit");function VW(e){let t=0;for(;e;)t+=e&1,e>>>=1;return t}c(VW,"popcount");Vt();var GW=class{static{c(this,"DPhypEnumerator")}graph;costModel;cardEstimator;dp;constructor(e,t,s){this.graph=e,this.costModel=t,this.cardEstimator=s,this.dp=new Map}solve(){for(let t of this.graph.relations){let s=this.costModel.scanCost(t.cardinality);this.dp.set(t.mask,{plan:t.plan,cardinality:t.cardinality,totalCost:s,mask:t.mask})}let e=this.graph.size;for(let t=2;t<=e;t++)this.enumerateSize(t);return this.dp.get(this.graph.fullMask)||null}enumerateSize(e){let t=this.graph.fullMask;for(let s=1;s<=t;s++)VW(s)===e&&(this.dp.has(s)||this.graph.isConnected(s)&&this.enumerateCsgCmpPairs(s))}enumerateCsgCmpPairs(e){for(let t=e-1&e;t>0;t=t-1&e){let s=e&~t;if(s===0||t>s||!this.dp.has(t)||!this.dp.has(s)||!this.graph.isConnected(t)||!this.graph.isConnected(s))continue;let n=this.graph.findJoinPredicates(t,s);n.length!==0&&this.emitPair(t,s,e,n)}}emitPair(e,t,s,n){let r=this.dp.get(e),i=this.dp.get(t),o=qW(n);this.tryJoinOrder(r,i,s,o),this.tryJoinOrder(i,r,s,o)}tryJoinOrder(e,t,s,n){let r=this.cardEstimator.estimateJoin(e.cardinality,t.cardinality,n),i=e.cardinality,o=t.cardinality,a=e.totalCost+t.totalCost+this.costModel.hashJoinCost(i,o,r),l=this.dp.get(s);(!l||a<l.totalCost)&&this.dp.set(s,{plan:{type:"HashJoin",buildSide:e.plan,probeSide:t.plan,condition:n,buildCard:e.cardinality,probeCard:t.cardinality},cardinality:r,totalCost:a,mask:s})}};function qW(e){if(e.length===0)return null;if(e.length===1)return e[0];let t=[],s=new Set;for(let n of e){let r=Ek(n);s.has(r)||(s.add(r),t.push(n))}return t.length===1?t[0]:t.reduce((n,r)=>({kind:"BoundBinary",op:"AND",left:n,right:r,resultType:"BOOLEAN"}))}c(qW,"combinePredicates");function Ek(e){return e?e.kind==="BoundColumnRef"?`${e.tableAlias}.${e.columnName}`:e.kind==="BoundBinary"?`${Ek(e.left)}${e.op}${Ek(e.right)}`:e.kind==="BoundLiteral"?String(e.value):JSON.stringify(e).slice(0,50):""}c(Ek,"predicateKey");function jW(e,t,s){return new GW(e,t,s).solve()}c(jW,"runDPhyp");var DD=class{static{c(this,"DefaultCostModel")}C_HASH_BUILD;C_HASH_PROBE;C_COMPARE;C_SCAN;C_FILTER;C_OUTPUT;C_MEMORY;C_IO;SPILL_THRESHOLD;C_CROSS;constructor(e={}){this.C_HASH_BUILD=e.hashBuildCost??1.5,this.C_HASH_PROBE=e.hashProbeCost??1,this.C_COMPARE=e.compareCost??.3,this.C_SCAN=e.scanCost??.1,this.C_FILTER=e.filterCost??.2,this.C_OUTPUT=e.outputCost??.3,this.C_MEMORY=e.memoryCost??.05,this.C_IO=e.ioCost??5,this.SPILL_THRESHOLD=e.spillThreshold??2e5,this.C_CROSS=e.crossJoinPenalty??1e3}hashJoinCost(e,t,s=null){let n=e*this.C_HASH_BUILD+t*this.C_HASH_PROBE,r=e*this.C_MEMORY,i=(s||Math.max(e,t))*this.C_OUTPUT,o=e>this.SPILL_THRESHOLD?(e+t)*this.C_IO:0;return n+r+i+o}mergeJoinCost(e,t,s=null){let n=(e+t)*this.C_COMPARE,r=(s||Math.max(e,t))*this.C_OUTPUT;return n+r}sortMergeJoinCost(e,t,s=null){return this.sortCost(e)+this.sortCost(t)+this.mergeJoinCost(e,t,s)}nestedLoopJoinCost(e,t){return e*t*this.C_COMPARE}crossJoinCost(e,t){return e*t*this.C_CROSS}sortCost(e){return e<=1?0:e*Math.log2(e)*this.C_COMPARE}topNSortCost(e,t){if(e<=1||t<=0)return 0;let s=Math.min(t,e);return e*Math.log2(Math.max(2,s))*this.C_COMPARE}scanCost(e){return e*this.C_SCAN}filterCost(e){return e*this.C_FILTER}aggregateCost(e){return this.hashAggregateCost(e)}hashAggregateCost(e,t=null){let s=t||Math.max(1,Math.sqrt(e));return e*this.C_HASH_BUILD+s*this.C_MEMORY}streamAggregateCost(e){return e*this.C_SCAN}totalJoinCost(e,t,s,n,r){return e.totalCost+t.totalCost+this.hashJoinCost(s,n,r)}cheaperJoinCost(e,t,s,n,r,i=0){let o=Math.min(e,t),a=Math.max(e,t),l=this.hashJoinCost(o,a,r),u=s?0:this.sortCost(e),f=n?0:this.sortCost(t),h=u+f+this.mergeJoinCost(e,t,r)-i;return{hashCost:l,mergeCost:h,preferMerge:h<l}}};Vt();Ft();var Ze=1e-4,cw=.5,qv=100,jv=1e3,uE=class{static{c(this,"DefaultCardinalityEstimator")}stats;constructor(e){this.stats=e}estimateScan(e){let t=this.stats.get(e.toUpperCase());return t?t.rowCount:jv}estimatePlan(e){if(!e)return jv;switch(e.type){case"Scan":return this.estimateScan(e.table);case"Filter":return this.estimateFilter(this.estimatePlan(e.children[0]),e.condition);case"Project":case"Sort":case"Distinct":case"Materialize":return this.estimatePlan(e.children?.[0]);case"Limit":{let t=this.estimatePlan(e.children?.[0]);return Math.min(e.count||t,t)}case"Join":{let t=this.estimatePlan(e.children[0]),s=this.estimatePlan(e.children[1]);return e.joinType==="SEMI"?this.estimateSemiJoin(t,s,e.condition):e.joinType==="ANTI"?this.estimateAntiJoin(t,s,e.condition):e.joinType==="MARK"?t:e.joinType==="LEFT"?this.estimateLeftJoin(t,s,e.condition):e.joinType==="CROSS"?t*s:this.estimateJoin(t,s,e.condition)}case"Aggregate":return this.estimateAggregate(this.estimatePlan(e.children[0]),e.groupBy?.length||0,e.groupBy);case"Empty":return 0;default:return e.children?.length?this.estimatePlan(e.children[0]):jv}}estimateFilter(e,t){let s=this.estimateSelectivity(t);return Math.max(1,Math.round(e*s))}estimateJoin(e,t,s){if(!s)return e*t;let n=this.extractEquiPredicates(s);if(n.length===0){let i=this.estimateSelectivity(s);return Math.max(1,Math.round(e*t*i))}let r=1;for(let i of n)r*=this.estimateEquiJoinSelectivity(i.left,i.right,e,t);return Math.max(1,Math.round(e*t*r))}estimateEquiJoinSelectivity(e,t,s,n){let r=this.getColumnStats(e),i=this.getColumnStats(t),o=Math.min(this.getColumnNdv(e),s),a=Math.min(this.getColumnNdv(t),n),l=r?.mcv,u=i?.mcv,f=l?.values?.length&&u?.values?.length,h=this._histogramJoinCollision(r,i);if(!f&&h===null)return 1/Math.max(o,a,1);let p=0,d=0,m=0,g=0,y=0;if(f){let E=new Map;for(let R=0;R<u.values.length;R++)E.set(u.values[R],u.frequencies[R]);for(let R=0;R<l.values.length;R++){d+=l.frequencies[R];let v=E.get(l.values[R]);v!==void 0&&(p+=l.frequencies[R]*v)}for(let R=0;R<u.frequencies.length;R++)m+=u.frequencies[R];g=l.values.length,y=u.values.length}let _=Math.max(0,1-(r?.nullFraction||0)-d),w=Math.max(0,1-(i?.nullFraction||0)-m),b=Math.max(1,o-g),x=Math.max(1,a-y),A=h!==null?h:1/Math.max(b,x),S=_*w*A;return Math.max(Ze,Math.min(1,p+S))}_histogramJoinCollision(e,t){let s=e?.histogram,n=t?.histogram;if(!s?.bucketDistincts||!n?.bucketDistincts||!s.totalCount||!n.totalCount)return null;let r=si(e.min),i=si(t.min);return r===null||i===null?null:WW(s,r,n,i)}estimateLeftJoin(e,t,s){let n=this.estimateJoin(e,t,s);return Math.max(e,n)}estimateSemiJoin(e,t,s){if(!s)return Math.round(e*.5);let n=this.extractEquiPredicates(s);if(n.length>0){let r=1;for(let i of n)r=Math.min(r,this.estimateSemiJoinSelectivity(i.left,i.right));return Math.max(1,Math.round(e*r))}return Math.max(1,Math.round(e*.5))}estimateSemiJoinSelectivity(e,t){let s=this.getColumnNdv(e),n=this.getColumnNdv(t),r=this.getColumnStats(e),i=this.getColumnStats(t),o=r?.mcv,a=i?.mcv;if(!o?.values?.length||!a?.values?.length)return Math.min(1,n/Math.max(s,1));let l=new Set(a.values),u=0,f=0;for(let g=0;g<o.values.length;g++)f+=o.frequencies[g],l.has(o.values[g])&&(u+=o.frequencies[g]);let h=Math.max(0,1-(r?.nullFraction||0)-f),p=Math.max(1,s-o.values.length),d=Math.max(1,n-a.values.length),m=h*Math.min(1,d/p);return Math.max(Ze,Math.min(1,u+m))}estimateAntiJoin(e,t,s){let n=this.estimateSemiJoin(e,t,s);return Math.max(1,e-n)}estimateAggregate(e,t,s=[]){if(t===0)return 1;let n=1,r=(s||[]).map(i=>this.getColumnNdv(i)).sort((i,o)=>o-i);for(let i=0;i<r.length;i++)i===0?n=r[i]:n*=Math.max(1,Math.sqrt(r[i]));return n>1?Math.max(1,Math.min(e,Math.round(n))):Math.min(e,Math.pow(10,t))}estimateSelectivity(e){if(!e)return 1;switch(e.kind){case"BoundBinary":{if(e.op==="AND"){let t=this.estimateSelectivity(e.left),s=this.estimateSelectivity(e.right),n=t*s,r=Math.min(t,s),i=this.lookupCorrelation(e.left,e.right),o=n*(1-i)+r*i;return Math.max(Ze,Math.min(r,o))}if(e.op==="OR"){let t=this.estimateSelectivity(e.left),s=this.estimateSelectivity(e.right);return Math.min(1,t+s-t*s)}if(e.op==="=")return this.estimateEqualitySelectivity(e);if(["<",">","<=",">="].includes(e.op))return this.estimateRangeSelectivity(e);if(e.op==="<>"){let t=this.estimateEqualitySelectivity({...e,op:"="});return Math.max(Ze,1-t)}return .5}case"BoundBetween":return this.estimateBetweenSelectivity(e);case"BoundLike":return this.estimateLikeSelectivity(e);case"BoundInList":return this.estimateInListSelectivity(e);case"BoundIsNull":return this.estimateIsNullSelectivity(e);case"BoundUnary":return e.op==="NOT"?Math.max(Ze,1-this.estimateSelectivity(e.operand)):.5;case"BoundExists":return .5;default:return .5}}estimateEqualitySelectivity(e){let t=null,s=null;if(e.left?.kind==="BoundColumnRef"&&e.right?.kind==="BoundLiteral")t=e.left,s=e.right;else if(e.right?.kind==="BoundColumnRef"&&e.left?.kind==="BoundLiteral")t=e.right,s=e.left;else if(e.left?.kind==="BoundColumnRef"&&e.right?.kind==="BoundColumnRef"){let o=this.getColumnNdv(e.left),a=this.getColumnNdv(e.right);return 1/Math.max(o,a,1)}else return .1;let n=this.getColumnStats(t);if(!n)return .1;if(n.mcv){let o=String(s.value),a=n.mcv.values.indexOf(o);if(a>=0)return n.mcv.frequencies[a]*(1-(n.nullFraction||0))}let r=n.ndv||qv,i=n.nullFraction||0;return Math.max(Ze,(1-i)/r)}estimateRangeSelectivity(e){let t=e.left?.kind==="BoundColumnRef"?e.left:e.right,s=e.left?.kind==="BoundLiteral"?e.left:e.right;if(t?.kind!=="BoundColumnRef"||s?.kind!=="BoundLiteral")return .33;let n=this.getColumnStats(t);if(!n)return .33;if(n.histogram){let f=e.left===t?["<","<="].includes(e.op):[">",">="].includes(e.op),h=n.histogram.estimateLessThan(s.value),p=f?h:1-h;return Math.max(Ze,p*(1-(n.nullFraction||0)))}let r=si(n.min),i=si(n.max),o=si(s.value);if(r===null||i===null||o===null||i<=r)return .33;let a=(o-r)/(i-r),l=Math.max(0,Math.min(1,a)),u=e.left===t?["<","<="].includes(e.op):[">",">="].includes(e.op);return Math.max(Ze,(u?l:1-l)*(1-(n.nullFraction||0)))}estimateBetweenSelectivity(e){let t=this.getColumnStats(e.expr);if(!t)return .25;if(t.histogram&&e.low?.kind==="BoundLiteral"&&e.high?.kind==="BoundLiteral"){let l=t.histogram.estimateRange(e.low.value,e.high.value),u=e.negated?1-l:l;return Math.max(Ze,u*(1-(t.nullFraction||0)))}let s=si(t.min),n=si(t.max),r=si(e.low?.kind==="BoundLiteral"?e.low.value:void 0),i=si(e.high?.kind==="BoundLiteral"?e.high.value:void 0);if(s===null||n===null||r===null||i===null||n<=s)return .25;let a=Math.max(0,Math.min(n,i)-Math.max(s,r))/(n-s);return Math.max(Ze,Math.min(1,e.negated?1-a:a))}estimateInListSelectivity(e){if(Array.isArray(e.list)){let t=this.getColumnNdv(e.expr),s=this.getColumnStats(e.expr),n=s?.nullFraction??0;if(s?.mcv){let i=0,o=0;for(let h of e.list)if(h.kind==="BoundLiteral"){let p=s.mcv.values.indexOf(String(h.value));p>=0&&(i++,o+=s.mcv.frequencies[p])}let a=e.list.length-i,l=Math.max(1,t-s.mcv.values.length),u=a/l,f=Math.min(1,(o+u)*(1-n));return e.negated?Math.max(Ze,1-f):Math.max(Ze,f)}let r=Math.min(1,e.list.length/Math.max(t,1))*(1-n);return e.negated?Math.max(Ze,1-r):Math.max(Ze,r)}return e.negated?.7:.3}estimateIsNullSelectivity(e){let s=this.getColumnStats(e.expr)?.nullFraction??.05;return e.negated?Math.max(Ze,1-s):Math.max(Ze,s)}estimateLikeSelectivity(e){let t=e.pattern.kind==="BoundLiteral"?e.pattern.value:void 0;if(typeof t!="string")return this.likeFallback("unknown");let s=this.getColumnNdv(e.expr),r=this.getColumnStats(e.expr)?.avgLength;if(!t.includes("%")&&!t.includes("_"))return s>0?1/s:this.likeFallback("exact");if(this.isPrefixPattern(t)){let i=t.length-1;if(r&&r>0){let o=Math.min(i/r,1);return Math.max(Ze,Math.pow(s,-o))}return Math.max(Ze,1/Math.pow(s,Math.min(1,i/4)))}if(this.isContainsPattern(t)){let i=t.slice(1,-1);if(r&&r>0){let o=Math.min(1,i.length/r),a=1/Math.max(Math.sqrt(s),1);return Math.max(Ze,a*(1-o*.5))}return Math.max(Ze,1/Math.max(Math.sqrt(s),1))}if(this.isSuffixPattern(t)){let i=t.length-1;return r&&r>0?Math.max(Ze,1/Math.pow(s,Math.min(1,i/r))):Math.max(Ze,1/Math.max(Math.sqrt(s),1))}return this.likeFallback("complex")}isPrefixPattern(e){return e.endsWith("%")&&!e.slice(0,-1).includes("%")&&!e.includes("_")}isContainsPattern(e){return e.startsWith("%")&&e.endsWith("%")&&!e.slice(1,-1).includes("%")}isSuffixPattern(e){return e.startsWith("%")&&!e.slice(1).includes("%")&&!e.includes("_")}likeFallback(e){return{exact:.1,unknown:.1,complex:.15}[e]??.1}lookupCorrelation(e,t){let s=this.extractSingleColumn(e),n=this.extractSingleColumn(t);if(!s||!n||s.tableAlias?.toUpperCase()!==n.tableAlias?.toUpperCase())return cw;let r=this.stats.get(s.tableAlias.toUpperCase());if(!r?.getCorrelation)return cw;let i=r.getCorrelation(s.columnName,n.columnName);return i!==null?Math.abs(i):cw}extractSingleColumn(e){if(e?.kind==="BoundColumnRef")return e;if(e?.kind==="BoundBinary"&&["=","<",">","<=",">=","<>"].includes(e.op)){if(e.left?.kind==="BoundColumnRef")return e.left;if(e.right?.kind==="BoundColumnRef")return e.right}return e?.kind==="BoundBetween"&&e.expr?.kind==="BoundColumnRef"||e?.kind==="BoundLike"&&e.expr?.kind==="BoundColumnRef"||e?.kind==="BoundInList"&&e.expr?.kind==="BoundColumnRef"?e.expr:null}getColumnNdv(e){return e?.kind!=="BoundColumnRef"?qv:this.getColumnStats(e)?.ndv||qv}getColumnStats(e){if(!e||e.kind!=="BoundColumnRef")return null;let t=e.columnName?.toUpperCase(),s=this.stats.get(e.tableAlias?.toUpperCase());if(s?.columnStats?.has(t))return s.columnStats.get(t)??null;for(let n of this.stats.values())if(n.columnStats?.has(t))return n.columnStats.get(t)??null;return null}extractEquiPredicates(e){let t=[];return this._collectEqui(e,t),t}_collectEqui(e,t){if(e){if(e.kind==="BoundBinary"&&e.op==="AND"){this._collectEqui(e.left,t),this._collectEqui(e.right,t);return}e.kind==="BoundBinary"&&e.op==="="&&e.left?.kind==="BoundColumnRef"&&e.right?.kind==="BoundColumnRef"&&t.push({left:e.left,right:e.right})}}};function si(e){return e==null?null:typeof e=="bigint"?Number(e):typeof e=="number"?e:null}c(si,"toNumber");function oL(e,t){let s=new Array(e.numBuckets),n=new Array(e.numBuckets);for(let r=0;r<e.numBuckets;r++)s[r]=r===0?t:si(e.boundaries[r-1]),n[r]=si(e.boundaries[r]);return{lows:s,highs:n}}c(oL,"bucketRanges");function aL(e,t){let{highs:s}=e,n=0,r=s.length-1,i=-1;for(;n<=r;){let o=n+r>>>1;s[o]>=t?(i=o,r=o-1):n=o+1}return i>=0&&t>=e.lows[i]?i:-1}c(aL,"findBucket");function lL(e,t,s){let n=e.highs[t]-e.lows[t];return n>0?Math.min(1,s/n):1}c(lL,"segFraction");function WW(e,t,s,n){let r=oL(e,t),i=oL(s,n),o=Math.max(r.lows[0],i.lows[0]),a=Math.min(r.highs[e.numBuckets-1],i.highs[s.numBuckets-1]);if(a<=o)return 0;let l=new Set([o,a]);for(let d=0;d<e.numBuckets;d++)r.highs[d]>o&&r.highs[d]<a&&l.add(r.highs[d]);for(let d=0;d<s.numBuckets;d++)i.highs[d]>o&&i.highs[d]<a&&l.add(i.highs[d]);let u=[...l].sort((d,m)=>d-m),f=e.totalCount,h=s.totalCount,p=0;for(let d=0;d<u.length-1;d++){let m=u[d],g=u[d+1],y=g-m,_=aL(r,(m+g)/2),w=aL(i,(m+g)/2);if(_<0||w<0)continue;let b=lL(r,_,y),x=lL(i,w,y),A=e.bucketCounts[_]*b,S=s.bucketCounts[w]*x,E=Math.max(1,e.bucketDistincts[_]*b),R=Math.max(1,s.bucketDistincts[w]*x);p+=A/f*(S/h)/Math.max(E,R)}return Math.max(0,Math.min(1,p))}c(WW,"histogramJoinCollision");Vt();var KW=class extends Be{static{c(this,"JoinReorder")}statisticsMap;costModel;cardEstimator;constructor(e=new Map,t=null,s=null){super(),this.statisticsMap=e,this.costModel=t||new DD,this.cardEstimator=s||new uE(this.statisticsMap)}get name(){return"JoinReorder"}apply(e){return new HW(this.costModel,this.cardEstimator).rewrite(e)}},HW=class extends ze{static{c(this,"JoinReorderRewriter")}costModel;cardEstimator;constructor(e,t){super(),this.costModel=e,this.cardEstimator=t}rewriteJoin(e){let t=this.rewriteChildren(e);return this.isInnerJoinTree(t)?this.reorderJoinTree(t):this.isNonInnerJoin(t)?this.reorderNonInnerJoin(t):t}rewriteDefault(e){let t=this.rewriteChildren(e);return this.isInnerJoinTree(t)?this.reorderJoinTree(t):t}isInnerJoinTree(e){return!(e.type!=="Join"||e.joinType!=="INNER"&&e.joinType!=="CROSS")}isNonInnerJoin(e){return e.type!=="Join"?!1:e.joinType==="SEMI"||e.joinType==="ANTI"||e.joinType==="LEFT"||e.joinType==="MARK"}reorderNonInnerJoin(e){let t=e.children[0],s=e.children[1];return this.isInnerJoinTree(t)&&(t=this.reorderJoinTree(t)),this.isInnerJoinTree(s)&&(s=this.reorderJoinTree(s)),{...e,children:[t,s]}}reorderJoinTree(e){let t=[],s=[],n=[];if(this.flattenJoinTree(e,t,s,n),t.length<2)return e;let r=zW(t,s,this.cardEstimator);if(r.size<2)return e;let i=jW(r,this.costModel,this.cardEstimator);if(!i)return e;let o=this.reconstructPlan(i.plan);return n.length>0&&(o=mn(Cs(n),o)),o}flattenJoinTree(e,t,s,n){if(e.type==="Join"&&(e.joinType==="INNER"||e.joinType==="CROSS")){if(this.flattenJoinTree(e.children[0],t,s,n),this.flattenJoinTree(e.children[1],t,s,n),e.condition){let i=In(e.condition);for(let o of i){let a=this.collectTableRefs(o);a.size>=2?s.push(o):(a.size,n.push(o))}}return}if(e.type==="Filter"){let i=In(e.condition),o=e.children[0];if(o.type==="Join"&&(o.joinType==="INNER"||o.joinType==="CROSS")){for(let a of i)this.collectTableRefs(a).size>=2?s.push(a):n.push(a);this.flattenJoinTree(o,t,s,n);return}}if(e.type==="Filter"&&e.children[0].type==="Scan"){let i=e.children[0];t.push({name:i.table,alias:i.alias||i.table,plan:e});return}if(e.type==="Scan"){t.push({name:e.table,alias:e.alias||e.table,plan:e});return}let r=this.inferAlias(e);t.push({name:r,alias:r,plan:e})}collectTableRefs(e){let t=new Set;return this._walkExpr(e,s=>{s.kind==="BoundColumnRef"&&s.tableAlias&&t.add(s.tableAlias.toUpperCase())}),t}_walkExpr(e,t){if(e)switch(t(e),e.kind){case"BoundBinary":this._walkExpr(e.left,t),this._walkExpr(e.right,t);return;case"BoundUnary":this._walkExpr(e.operand,t);return;case"BoundFunction":case"BoundAggregate":for(let s of e.args)this._walkExpr(s,t);return}}reconstructPlan(e){if(!e)return e;if(e.type==="HashJoin"){let t=this.reconstructPlan(e.buildSide),s=this.reconstructPlan(e.probeSide);return Lr("INNER",e.condition,t,s)}return e}inferAlias(e){if(e.type==="Scan"||e.type==="IndexScan"){if(e.alias)return e.alias;if(e.table)return e.table}let t=this.findFirstScan(e);return t?.alias||t?.table||`_rel_${Math.random().toString(36).slice(2,6)}`}findFirstScan(e){if(!e)return null;if(e.type==="Scan")return e;for(let t of cs(e)){let s=this.findFirstScan(t);if(s)return s}return null}};$e();Ft();Ke();Vt();var ei=1e3,JW=class extends Be{static{c(this,"PhysicalDesign")}statisticsMap;costModel;cardEstimator;constructor(e=new Map,t=null,s=null){super(),this.statisticsMap=e,this.costModel=t||new DD,this.cardEstimator=s||new uE(this.statisticsMap)}get name(){return"PhysicalDesign"}apply(e){return new YW(this.costModel,this.cardEstimator).rewrite(e)}},YW=class extends ze{static{c(this,"PhysicalDesignRewriter")}costModel;cardEstimator;parentMap;constructor(e,t){super(),this.costModel=e,this.cardEstimator=t,this.parentMap=null}rewrite(e){return this.parentMap=QW(e),super.rewrite(e)}rewriteDefault(e){let t=this.rewriteChildren(e);return t._cardinality=this.estimateNodeCardinality(t),t._sortedBy=this.inferSortOrder(t),t}rewriteJoin(e){let t=e,s=this.rewriteChildren(e);s._cardinality=this.estimateNodeCardinality(s);let n=s.children[0],r=s.children[1],i=n._cardinality||ei,o=r._cardinality||ei;if(s.joinType!=="CROSS"&&s.condition){let h=this.extractEquiJoinKeys(s.condition);if(h.leftKeys.length>0&&h.rightKeys.length>0){let p=this.isSortedBy(n._sortedBy,h.leftKeys),d=this.isSortedBy(r._sortedBy,h.rightKeys),m=this.estimateDownstreamSortSaving(t,h.leftKeys,h.rightKeys,s._cardinality);if(this.costModel.cheaperJoinCost(i,o,p,d,s._cardinality,m).preferMerge)return s.physicalStrategy="MERGE",s._sortedBy=[...h.leftKeys,...h.rightKeys],s._requiresSort={left:!p,right:!d},this.assignBuildSide(s,i,o),s}}this.assignBuildSide(s,i,o);let a=s._buildSide==="left"?i:o,l=s._buildSide==="left"?o:i,u=this.costModel.nestedLoopJoinCost(a,l),f=this.costModel.hashJoinCost(a,l,s._cardinality);return u<f?(s.physicalStrategy="NESTED_LOOP",s._sortedBy=[],s):(this.isSpecialJoinType(s.joinType)&&this.isPureEquiJoin(s.condition)&&(s._dedupeBuild=!0),this.assignBuildSide(s,i,o),s.physicalStrategy="HASH",s._sortedBy=[],s)}assignBuildSide(e,t,s){switch(e.joinType){case"LEFT":case"SEMI":case"ANTI":case"MARK":e._buildSide="right";break;case"RIGHT":e._buildSide="left";break;case"INNER":e._buildSide=s<t?"right":"left";break}}isSpecialJoinType(e){return e==="SEMI"||e==="ANTI"||e==="MARK"}rewriteAggregate(e){let t=this.rewriteChildren(e);t._cardinality=this.estimateNodeCardinality(t);let s=t.children[0];if(t.groupBy&&t.groupBy.length>0){let n=t.groupBy.map(i=>this.getColumnKey(i));if(this.isSortedByPrefix(s._sortedBy,n)){let i=this.costModel.hashAggregateCost(s._cardinality);if(this.costModel.streamAggregateCost(s._cardinality)<=i)return t.physicalStrategy="STREAM",t._sortedBy=[...s._sortedBy],t}}return!t.groupBy||t.groupBy.length===0?(t.physicalStrategy="UNGROUPED",t._sortedBy=[],t):this.canUsePerfectHashAggregate(t,s)?(t.physicalStrategy="PERFECT_HASH",t._sortedBy=[],t):(t.physicalStrategy="HASH",t._sortedBy=[],t)}rewriteSort(e){let t=this.rewriteChildren(e),s=t.children[0]._cardinality||ei;return t.limit?(t._cardinality=Math.min(t.limit,s),t._cost=this.costModel.topNSortCost(s,t.limit)):(t._cardinality=s,t._cost=this.costModel.sortCost(s)),t._sortedBy=t.orderKeys.map(n=>({key:this.getColumnKey(n.expr),direction:(n.direction||"ASC").toUpperCase()})).filter(n=>!!n.key),t}inferSortOrder(e){return e.type==="Sort"?e.orderKeys.map(t=>({key:this.getColumnKey(t.expr),direction:(t.direction||"ASC").toUpperCase()})).filter(t=>!!t.key):e.type==="IndexScan"?[`${(e.alias||e.table||"").toUpperCase()}.${(e.columnName||"").toUpperCase()}`]:(e.type==="Filter"||e.type==="Project"||e.type==="Limit")&&e.children&&e.children.length>0?e.children[0]._sortedBy||[]:[]}getColumnKey(e){return e&&e.kind==="BoundColumnRef"?`${e.tableAlias||""}.${e.columnName}`.toUpperCase():null}columnMatches(e,t){let s=e&&typeof e=="object"?e.key:e,n=t&&typeof t=="object"?t.key:t;return!s||!n?!1:s===n?!0:s.split(".").pop()===n.split(".").pop()}isSortedBy(e,t){if(!e||e.length===0||t.length===0)return!1;for(let s=0;s<t.length;s++)if(!this.columnMatches(e[s],t[s]))return!1;return!0}isSortedByPrefix(e,t){if(!e||e.length<t.length||t.length===0)return!1;let s=e.slice(0,t.length);for(let n of t)if(!s.some(r=>this.columnMatches(r,n)))return!1;return!0}estimateNodeCardinality(e){if(e.type==="Scan"||e.type==="IndexScan")return this.cardEstimator.estimateScan(e.table);if(e.type==="Filter"){let t=e.children[0]._cardinality||ei;return this.cardEstimator.estimateFilter(t,e.condition)}if(e.type==="Join"){let t=e.children[0]._cardinality||ei,s=e.children[1]._cardinality||ei;return e.joinType==="SEMI"?this.cardEstimator.estimateSemiJoin(t,s,e.condition):e.joinType==="ANTI"?this.cardEstimator.estimateAntiJoin(t,s,e.condition):e.joinType==="MARK"?t:e.joinType==="LEFT"?this.cardEstimator.estimateLeftJoin?this.cardEstimator.estimateLeftJoin(t,s,e.condition):Math.max(t,this.cardEstimator.estimateJoin(t,s,e.condition)):e.joinType==="CROSS"?t*s:this.cardEstimator.estimateJoin(t,s,e.condition)}if(e.type==="Aggregate"){let t=e.children[0]._cardinality||ei;return this.cardEstimator.estimateAggregate(t,e.groupBy?.length||0,e.groupBy||[])}if(e.type==="Limit"){let t=e.children[0]._cardinality||ei;return Math.min(e.count||t,t)}if(e.type==="Distinct"){let t=e.children[0]._cardinality||ei;return Math.max(1,Math.round(Math.sqrt(t)))}return e.children&&e.children.length>0?e.children[0]._cardinality||ei:1e3}extractEquiJoinKeys(e){let t=[],s=[],n=this.splitAnd(e);for(let r of n)r.kind==="BoundBinary"&&r.op==="="&&r.left?.kind==="BoundColumnRef"&&r.right?.kind==="BoundColumnRef"&&(t.push(this.getColumnKey(r.left)),s.push(this.getColumnKey(r.right)));return{leftKeys:t,rightKeys:s}}splitAnd(e){return e?e.kind==="BoundBinary"&&e.op==="AND"?[...this.splitAnd(e.left),...this.splitAnd(e.right)]:[e]:[]}canUsePerfectHashAggregate(e,t){if(!e.groupBy||e.groupBy.length===0||!e.groupBy.every(r=>r.kind==="BoundColumnRef"))return!1;let s=e.groupBy.map(r=>this.cardEstimator.getColumnStats?.(r)||null);if(!s.every(Boolean))return!1;let n=1;for(let r of s){if(!r.ndv||r.ndv<=0)return!1;n*=r.ndv}return n>256?!1:s.every(r=>this.hasCompactDomain(r))}hasCompactDomain(e){let t=e.ndv||0;if(t<=0||t>256)return!1;let s=cL(e.min),n=cL(e.max);if(s!==null&&n!==null&&Number.isInteger(s)&&Number.isInteger(n)){let r=n-s+1;return r>0&&r<=4096}return t<=4}estimateDownstreamSortSaving(e,t,s,n){let r=this.parentMap?.get(e);if(!r)return 0;let i=this.findDownstreamSort(r,e);if(!i||!i.orderKeys)return 0;let o=i.orderKeys.map(u=>this.getColumnKey(u.expr)).filter(u=>!!u);if(o.length===0)return 0;let a=[...t,...s];if(!this.isSortedByPrefix(a,o))return 0;let l=n||ei;return i.limit?this.costModel.topNSortCost(l,i.limit):this.costModel.sortCost(l)}findDownstreamSort(e,t){if(!e)return null;if(e.type==="Sort")return e;if(e.type==="Project"||e.type==="Filter"||e.type==="Limit"){let s=this.parentMap?.get(e);return s?this.findDownstreamSort(s,e):null}return null}isPureEquiJoin(e){if(!e)return!1;let t=this.splitAnd(e);return t.length>0&&t.every(s=>s.kind==="BoundBinary"&&s.op==="="&&s.left?.kind==="BoundColumnRef"&&s.right?.kind==="BoundColumnRef")}};function QW(e){let t=new Map,s=[e];for(;s.length>0;){let n=s.shift();if(n.children)for(let r of n.children)t.set(r,n),s.push(r)}return t}c(QW,"buildParentMap");function cL(e){return e==null?null:typeof e=="bigint"?Number(e):typeof e=="number"?e:null}c(cL,"toNumber2");UP();VP();$e();Ke();Ft();Vt();var XW=class extends Be{static{c(this,"ExpressionSimplifier")}get name(){return"ExpressionSimplifier"}apply(e){return new ZW().rewrite(e)}},ZW=class extends ze{static{c(this,"SimplifierRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]),s=fr(e.condition);return s&&s.kind==="BoundLiteral"&&s.value===!0?t:s&&s.kind==="BoundLiteral"&&s.value===!1?{type:"Empty",children:[t]}:s!==e.condition||t!==e.children[0]?{...e,condition:s,children:[t]}:e}rewriteProject(e){let t=this.rewrite(e.children[0]),s=!1,n=e.expressions.map(r=>{let i=fr(r);return i!==r&&(s=!0),i});return s||t!==e.children[0]?{...e,expressions:n,children:[t]}:e}rewriteJoin(e){let t=e.children.map(r=>this.rewrite(r)),s=t.some((r,i)=>r!==e.children[i]),n=e.condition;return e.condition&&(n=fr(e.condition),n!==e.condition&&(s=!0)),s?{...e,condition:n,children:t}:e}};function fr(e){if(!e)return e;switch(e.kind){case"BoundBinary":{let t=fr(e.left),s=fr(e.right),n=e.op.toUpperCase();if(n==="AND"){if(en(t,!1)||en(s,!1))return ce(!1,"BOOLEAN");if(en(t,!0))return s;if(en(s,!0)||uL(t,s))return t}else if(n==="OR"){if(en(t,!0)||en(s,!0))return ce(!0,"BOOLEAN");if(en(t,!1))return s;if(en(s,!1)||uL(t,s))return t;let r=tK(t,s);if(r)return fr(r)}else{if(n==="+"||n==="-"){if(en(s,0))return t;if(n==="+"&&en(t,0))return s}if(n==="*"){if(en(s,1))return t;if(en(t,1))return s}if(n==="/"&&en(s,1))return t;if(t.kind==="BoundLiteral"&&s.kind==="BoundLiteral"){let r=t.value,i=s.value,o=e.dataType??null;if(r!==null&&i!==null)try{if(n==="+")return ce(r+i,typeof(r+i)=="number"?"FLOAT64":o);if(n==="-")return ce(r-i,typeof(r-i)=="number"?"FLOAT64":o);if(n==="*")return ce(r*i,typeof(r*i)=="number"?"FLOAT64":o);if(n==="/")return ce(r/i,"FLOAT64");if(n==="=")return ce(r===i,"BOOLEAN");if(n==="!=")return ce(r!==i,"BOOLEAN");if(n===">")return ce(r>i,"BOOLEAN");if(n===">=")return ce(r>=i,"BOOLEAN");if(n==="<")return ce(r<i,"BOOLEAN");if(n==="<=")return ce(r<=i,"BOOLEAN")}catch{}}}return t!==e.left||s!==e.right?{...e,left:t,right:s}:e}case"BoundUnary":{let t=fr(e.operand);if(e.op.toUpperCase()==="NOT"){if(en(t,!0))return ce(!1,"BOOLEAN");if(en(t,!1))return ce(!0,"BOOLEAN");if(t.kind==="BoundUnary"&&t.op.toUpperCase()==="NOT")return t.operand}return t!==e.operand?{...e,operand:t}:e}case"BoundFunction":{let t=e.args.map(n=>fr(n));return t.some((n,r)=>n!==e.args[r])?{...e,args:t}:e}case"BoundCase":{let t=[],s=!1;for(let r of e.whenClauses){let i=fr(r.condition),o=fr(r.result);if((i!==r.condition||o!==r.result)&&(s=!0),en(i,!1)){s=!0;continue}if(en(i,!0))return o;t.push({condition:i,result:o})}let n=e.elseExpr?fr(e.elseExpr):null;return n!==e.elseExpr&&(s=!0),t.length===0&&n?n:t.length===0&&!n?ce(null,e.resultType):s?{...e,whenClauses:t,elseExpr:n}:e}case"BoundCast":{let t=fr(e.expr);if(t.kind==="BoundLiteral"&&t.value!==null){let s=eK(t.value,e.targetType);if(s!==void 0)return ce(s,e.targetType)}return t!==e.expr?{...e,expr:t}:e}case"BoundExtract":{let t=fr(e.source),s=t.kind==="BoundLiteral"?t.value:null;if(s instanceof Date){let n=sK(s,e.field);if(n!==null)return ce(n,"INT32")}return t!==e.source?{...e,source:t}:e}}return e}c(fr,"simplifyExpression");function en(e,t){return!!e&&e.kind==="BoundLiteral"&&e.value===t}c(en,"isLiteral");function uL(e,t){return e===t?!0:!e||!t||e.kind!==t.kind?!1:e.kind==="BoundColumnRef"&&t.kind==="BoundColumnRef"?e.tableAlias===t.tableAlias&&e.columnName===t.columnName:e.kind==="BoundLiteral"&&t.kind==="BoundLiteral"?e.value===t.value:Bn(e)===Bn(t)}c(uL,"exprEquals");function tK(e,t){let s=Dw(e),n=Dw(t),r=new Map(n.map(p=>[Bn(p),p])),i=[],o=[],a=new Set;for(let p of s){let d=Bn(p);r.has(d)?(i.push(p),a.add(d)):o.push(p)}if(i.length===0)return null;let l=n.filter(p=>!a.has(Bn(p))),u=Wv(o)||ce(!0,"BOOLEAN"),f=Wv(l)||ce(!0,"BOOLEAN"),h={kind:"BoundBinary",op:"OR",left:u,right:f,resultType:"BOOLEAN"};return Wv([...i,h])}c(tK,"factorCommonConjuncts");function Dw(e){return e?e.kind==="BoundBinary"&&e.op.toUpperCase()==="AND"?[...Dw(e.left),...Dw(e.right)]:[e]:[]}c(Dw,"splitConjuncts2");function Wv(e){return e.length===0?null:e.reduce((t,s)=>t?{kind:"BoundBinary",op:"AND",left:t,right:s,resultType:"BOOLEAN"}:s,null)}c(Wv,"combineConjuncts2");function eK(e,t){let s=(t||"").toUpperCase();try{if(s.includes("INT"))return Math.trunc(Number(e));if(s.includes("FLOAT")||s.includes("DOUBLE")||s.includes("DECIMAL")||s.includes("NUMERIC"))return Number(e);if(s.includes("VARCHAR")||s.includes("TEXT")||s.includes("CHAR"))return String(e);if(s.includes("BOOL"))return!!e}catch{}}c(eK,"foldCast");function sK(e,t){let s=(t||"").toUpperCase();return s==="YEAR"?e.getFullYear():s==="MONTH"?e.getMonth()+1:s==="DAY"?e.getDate():s==="HOUR"?e.getHours():s==="MINUTE"?e.getMinutes():s==="SECOND"?e.getSeconds():null}c(sK,"extractFromDate");function Bn(e){if(!e||typeof e!="object")return String(e);switch(e.kind){case"BoundColumnRef":return`COL:${e.tableAlias||""}.${e.columnName}`;case"BoundLiteral":return`LIT:${String(e.value)}`;case"BoundBinary":return`BIN:${e.op}:${Bn(e.left)}:${Bn(e.right)}`;case"BoundLike":return`LIKE:${e.negated}:${Bn(e.expr)}:${Bn(e.pattern)}`;case"BoundInList":return`IN:${e.negated}:${Bn(e.expr)}:${Array.isArray(e.list)?e.list.map(Bn).join(","):Bn(e.list)}`;case"BoundBetween":return`BETWEEN:${e.negated}:${Bn(e.expr)}:${Bn(e.low)}:${Bn(e.high)}`;default:return JSON.stringify(e)}}c(Bn,"exprKey");$e();Ke();Ft();Vt();var nK=class extends Be{static{c(this,"OuterToInnerJoin")}get name(){return"OuterToInnerJoin"}apply(e){return new rK().rewrite(e)}},rK=class extends ze{static{c(this,"OuterToInnerRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]);if(t.type==="Join"&&(t.joinType==="LEFT"||t.joinType==="FULL"||t.joinType==="RIGHT"||t.joinType==="SINGLE")){let s=fL(t.children[0]),n=fL(t.children[1]),r=Rk(e.condition),i=!1,o=!1;for(let l of r)hL(l,n)&&(i=!0),hL(l,s)&&(o=!0);let a=t.joinType;t.joinType==="LEFT"&&i||t.joinType==="SINGLE"&&i||t.joinType==="RIGHT"&&o?a="INNER":t.joinType==="FULL"&&(o&&i?a="INNER":o?a="LEFT":i&&(a="RIGHT")),a!==t.joinType&&(t={...t,joinType:a})}return t!==e.children[0]?{...e,children:[t]}:e}};function fL(e){let t={aliases:new Set,columns:new Set};return Nk(e,t),t.aliases.delete(""),t.columns.delete(""),t}c(fL,"getPlanRefs");function Nk(e,t){if(e){if(e.type==="Scan"){t.aliases.add((e.alias||e.table||"").toUpperCase());for(let s of e.columns||[])t.columns.add((s.name||s.columnName||"").toUpperCase());return}if(e.type==="CTEScan"){t.aliases.add((e.alias||e.cteName||"").toUpperCase());return}if(e.type==="Project"){for(let s of e.expressions||[]){let n=s;t.columns.add((n.outputName||n.alias||n.name||n.columnName||"").toUpperCase())}return}if(e.type==="Aggregate"){for(let s of e.groupBy||[]){let n=s;t.columns.add((n.outputName||n.alias||n.name||n.columnName||"").toUpperCase())}for(let s of e.aggregates||[]){let n=s;t.columns.add((n.outputName||n.alias||n.name||"").toUpperCase())}return}if(e.type==="Join"||e.type==="Union"){for(let s of cs(e))Nk(s,t);return}e.children?.[0]&&Nk(e.children[0],t)}}c(Nk,"addOutputRefs3");function Rk(e){return e?e.kind==="BoundBinary"&&e.op.toUpperCase()==="AND"?[...Rk(e.left),...Rk(e.right)]:[e]:[]}c(Rk,"splitConjuncts3");function hL(e,t){let s=Xp(e,t);return s===!1||s===null}c(hL,"isNullRejecting");function Xp(e,t){if(e){switch(e.kind){case"BoundLiteral":return e.value;case"BoundColumnRef":return e.tableAlias&&t.aliases.has(e.tableAlias.toUpperCase())||!e.tableAlias&&t.columns.has((e.columnName||"").toUpperCase())?null:"UNKNOWN";case"BoundBinary":{let s=Xp(e.left,t),n=Xp(e.right,t),r=e.op.toUpperCase();return r==="AND"?s===!1||n===!1?!1:s===null||n===null?null:s==="UNKNOWN"||n==="UNKNOWN"?"UNKNOWN":!0:r==="OR"?s===!0||n===!0?!0:s==="UNKNOWN"||n==="UNKNOWN"?"UNKNOWN":s===null&&n===null?null:!1:s===null||n===null?null:"UNKNOWN"}case"BoundUnary":return Xp(e.operand,t)===null?null:"UNKNOWN";case"BoundFunction":case"BoundExtract":case"BoundCast":case"BoundInterval":case"BoundCase":case"BoundAggregate":return"UNKNOWN";case"BoundIsNull":return Xp(e.operand,t)===null?!0:"UNKNOWN"}return"UNKNOWN"}}c(Xp,"evaluateWithNulls");$e();Ke();Ft();var iK=class extends Be{static{c(this,"LimitPushdown")}get name(){return"LimitPushdown"}apply(e){return new oK().rewrite(e)}},oK=class extends ze{static{c(this,"LimitPushdownRewriter")}rewriteLimit(e){let t=this.rewrite(e.children[0]);if(t.type==="Project"){let s={...e,children:[t.children[0]]},n={...t,children:[s]},r=this.rewrite(s);return{...n,children:[r]}}if(t.type==="Union"&&t.all){let s={...e,children:[t.children[0]]},n={...e,children:[t.children[1]]},r={...t,children:[this.rewrite(s),this.rewrite(n)]};return{...e,children:[r]}}if(t.type==="Sort"){let s={...t,limit:e.count,offset:e.offset||0};return{...e,children:[s]}}if(t.type==="Aggregate"&&t.groupBy&&t.groupBy.length>0){let s={...t,_limitHint:e.count+(e.offset||0)};return{...e,children:[s]}}return t!==e.children[0]?{...e,children:[t]}:e}};$e();Ke();Ft();Vt();var aK=class extends Be{static{c(this,"HavingPushdown")}get name(){return"HavingPushdown"}apply(e){return new lK().rewrite(e)}},lK=class extends ze{static{c(this,"HavingPushdownRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]);if(t.type==="Aggregate"){let s=Bk(e.condition),n=[],r=[];for(let i of s)So(i)?r.push(i):n.push(i);if(n.length>0){let i=t.children[0],a={type:"Filter",condition:pL(n),children:[i]};return t={...t,children:[a]},r.length===0?t:{...e,condition:pL(r),children:[t]}}}return t!==e.children[0]?{...e,children:[t]}:e}};function Bk(e){return e?e.kind==="BoundBinary"&&e.op?.toUpperCase()==="AND"?[...Bk(e.left),...Bk(e.right)]:[e]:[]}c(Bk,"splitConjuncts4");function pL(e){if(!e||e.length===0)return null;let t=e[0];for(let s=1;s<e.length;s++)t={kind:"BoundBinary",op:"AND",left:t,right:e[s],resultType:"BOOLEAN"};return t}c(pL,"combineConjuncts3");function So(e){if(!e)return!1;if(e.kind==="BoundAggregate")return!0;if(e.kind==="BoundBinary")return So(e.left)||So(e.right);if(e.kind==="BoundUnary")return So(e.operand);if(e.kind==="BoundFunction"||e.kind==="BoundCase"){if(e.kind==="BoundFunction"&&e.args)return e.args.some(So);if(e.kind==="BoundCase"&&e.whenClauses){for(let t of e.whenClauses)if(So(t.condition)||So(t.result))return!0}if(e.kind==="BoundCase"&&e.operand&&So(e.operand)||e.kind==="BoundCase"&&e.elseExpr&&So(e.elseExpr))return!0}return!1}c(So,"containsAggregate2");$e();Ke();Ft();Vt();var cK=class extends Be{static{c(this,"EmptyPropagation")}get name(){return"EmptyPropagation"}apply(e){return new uK().rewrite(e)}},uK=class extends ze{static{c(this,"EmptyPropagationRewriter")}rewriteDefault(e){let t=this.rewriteChildren(e),s=t.children;return s&&s.length===1&&s[0].type==="Empty"?t.type==="Aggregate"&&(!t.groupBy||t.groupBy.length===0)?t:s[0]:t}rewriteFilter(e){let t=this.rewriteChildren(e);return t.children[0].type==="Empty"?t.children[0]:t.condition&&t.condition.kind==="BoundLiteral"&&t.condition.value===!1?{type:"Empty",children:[t.children[0]]}:t}rewriteLimit(e){let t=this.rewriteChildren(e);return t.children[0].type==="Empty"?t.children[0]:t.count===0?{type:"Empty",children:[t.children[0]]}:t}rewriteJoin(e){let t=this.rewriteChildren(e),s=t.children[0],n=t.children[1],r=s.type==="Empty",i=n.type==="Empty";if(t.joinType==="INNER"||t.joinType==="CROSS"){if(r||i)return{type:"Empty",children:[t]}}else if(t.joinType==="LEFT"){if(r)return{type:"Empty",children:[t]};if(i)return t}else if(t.joinType==="FULL"&&r&&i)return{type:"Empty",children:[t]};return t}rewriteUnion(e){let t=this.rewriteChildren(e),s=t.children[0],n=t.children[1],r=s.type==="Empty",i=n.type==="Empty";return r&&i?{type:"Empty",children:[t]}:r?n:i?s:t}};$e();Ke();Ft();Vt();var fK=class extends Be{static{c(this,"NodeMerge")}get name(){return"NodeMerge"}apply(e){return new hK().rewrite(e)}},hK=class extends ze{static{c(this,"NodeMergeRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]);if(t.type==="Filter"){let s={kind:"BoundBinary",op:"AND",left:e.condition,right:t.condition,resultType:"BOOLEAN"};return t=t.children[0],{...e,condition:s,children:[t]}}return t!==e.children[0]?{...e,children:[t]}:e}rewriteProject(e){let t=this.rewrite(e.children[0]);return t.type==="Project"&&pK(e.expressions,t.expressions)?{...e,children:[t.children[0]]}:t!==e.children[0]?{...e,children:[t]}:e}rewriteLimit(e){let t=this.rewrite(e.children[0]);if(t.type==="Limit"){let s=Math.min(e.count,t.count);return{...e,count:s,children:[t.children[0]]}}return t!==e.children[0]?{...e,children:[t]}:e}};function pK(e,t){return!Array.isArray(e)||!Array.isArray(t)||e.length!==t.length?!1:e.every((s,n)=>Ck(s,t[n]))}c(pK,"sameProjectExpressions");function Ck(e,t){if(e===t)return!0;if(!e||!t)return!1;if(typeof e!="object"||typeof t!="object")return e===t;if(Array.isArray(e)||Array.isArray(t))return!Array.isArray(e)||!Array.isArray(t)||e.length!==t.length?!1:e.every((o,a)=>Ck(o,t[a]));let s=e,n=t,r=Object.keys(s).filter(dL).sort(),i=Object.keys(n).filter(dL).sort();if(r.length!==i.length)return!1;for(let o=0;o<r.length;o++)if(r[o]!==i[o]||!Ck(s[r[o]],n[i[o]]))return!1;return!0}c(Ck,"exprEqualsIgnoringOutput");function dL(e){return e!=="outputName"&&e!=="alias"}c(dL,"isSemanticKey");$e();Ft();Ke();Vt();var dK=class extends Be{static{c(this,"PredicateInference")}get name(){return"PredicateInference"}apply(e){return new mK().rewrite(e)}},mK=class extends ze{static{c(this,"InferenceRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]),s=In(e.condition),n=mL(s);if(n.length===0)return t!==e.children[0]?{...e,children:[t]}:e;let r=[...s,...n];return mn(Cs(r),t)}rewriteJoin(e){let t=this.rewriteChildren(e);if(t.joinType!=="INNER"||!t.condition)return t;let s=In(t.condition),n=yL(t.children[0]),r=yL(t.children[1]),i=[...s];_L(t.children[0],i),_L(t.children[1],i);let o=mL(i);if(o.length===0)return t;let a=[],l=[];for(let h of o){let p=xK(h);if(p.length===0)continue;let d=p.every(g=>n.has(g)),m=p.every(g=>r.has(g));d?a.push(h):m&&l.push(h)}let u=t.children[0],f=t.children[1];return a.length>0&&(u=mn(Cs(a),u)),l.length>0&&(f=mn(Cs(l),f)),u!==t.children[0]||f!==t.children[1]?{...t,children:[u,f]}:t}};function mL(e){let t=new Map,s=new Map,n=new Set(e.map(Ao)),r=[];for(let i of e){for(let o of gK(i,n))r.push(o),n.add(Ao(o));for(let o of yK(i,n))r.push(o),n.add(Ao(o))}for(let i of e){if(i.kind!=="BoundBinary"||i.op!=="=")continue;let o=i.left?.kind==="BoundColumnRef",a=i.right?.kind==="BoundColumnRef",l=i.left?.kind==="BoundLiteral",u=i.right?.kind==="BoundLiteral";if(o&&u)s.set(Di(i.left),i.right);else if(a&&l)s.set(Di(i.right),i.left);else if(o&&a){let f=Di(i.left),h=Di(i.right);t.has(f)||t.set(f,new Set),t.has(h)||t.set(h,new Set),t.get(f).add(h),t.get(h).add(f)}}for(let[i,o]of s){let a=t.get(i);if(a)for(let l of a){if(s.has(l))continue;let u=fw(e,l);if(!u)continue;let f=Cn("=",u,o,"BOOLEAN"),h=Ao(f);n.has(h)||(r.push(f),n.add(h),s.set(l,o))}}for(let[i,o]of t){let a=s.get(i);if(a)for(let l of o){if(s.has(l))continue;let u=fw(e,l);if(!u)continue;let f=Cn("=",u,a,"BOOLEAN"),h=Ao(f);n.has(h)||(r.push(f),n.add(h))}}for(let i of e){if(i.kind!=="BoundBinary"||!["<",">","<=",">=","<>"].includes(i.op))continue;let o=i.left?.kind==="BoundColumnRef",a=i.right?.kind==="BoundColumnRef",l=i.left?.kind==="BoundLiteral",u=i.right?.kind==="BoundLiteral";if(o&&u){let f=t.get(Di(i.left));if(f)for(let h of f){let p=fw(e,h);if(!p)continue;let d=Cn(i.op,p,i.right,"BOOLEAN"),m=Ao(d);n.has(m)||(r.push(d),n.add(m))}}else if(a&&l){let f=t.get(Di(i.right));if(f)for(let h of f){let p=fw(e,h);if(!p)continue;let d=Cn(i.op,i.left,p,"BOOLEAN"),m=Ao(d);n.has(m)||(r.push(d),n.add(m))}}}return r}c(mL,"inferNewPredicates");function gK(e,t){if(!e||e.kind!=="BoundBinary"||e.op!=="OR")return[];let s=$w(e);if(s.length<2)return[];let n=s.map(o=>FD(o)),r=[],i=n[0];for(let[o,a]of i){if(!n.every(h=>h.has(o)))continue;let l=new Map(a.literals||[]);for(let h=1;h<n.length;h++){let p=n[h].get(o);if(!p.literals||p.literals.size===0){l.clear();break}for(let[d,m]of p.literals)l.set(d,m)}if(l.size<2)continue;let u=kw(a.col,[...l.values()],!1),f=Ao(u);t.has(f)||r.push(u)}return r}c(gK,"inferInListsFromOr");function yK(e,t){if(!e||e.kind!=="BoundBinary"||e.op!=="OR")return[];let s=$w(e);if(s.length<2)return[];let n=s.map(o=>FD(o)),r=[],i=n[0];for(let[o,a]of i){if(!n.every(d=>d.has(o)))continue;let l=n.map(d=>d.get(o));if(!l.every(d=>d.lower&&d.upper))continue;let u=l.reduce((d,m)=>Fw(m.lower.literal,d.literal)<0?m.lower:d,l[0].lower),f=l.reduce((d,m)=>Fw(m.upper.literal,d.literal)>0?m.upper:d,l[0].upper),h=Cn(u.op,a.col,u.literal,"BOOLEAN"),p=Cn(f.op,a.col,f.literal,"BOOLEAN");for(let d of[h,p]){let m=Ao(d);t.has(m)||r.push(d)}}return r}c(yK,"inferRangePredicatesFromOr");function FD(e){let t=new Map;for(let s of In(e))_K(t,s),wK(t,s),bK(t,s);return t}c(FD,"collectBranchConstraints");function Sf(e,t){let s=Di(t);return e.has(s)||e.set(s,{col:t,literals:new Map,lower:null,upper:null}),e.get(s)}c(Sf,"ensureConstraint");function _K(e,t){if(t.kind!=="BoundBinary"||t.op!=="=")return;let s=t.left?.kind==="BoundColumnRef",n=t.right?.kind==="BoundColumnRef",r=t.left?.kind==="BoundLiteral",i=t.right?.kind==="BoundLiteral",o=null,a=null;s&&i?(o=t.left,a=t.right):n&&r&&(o=t.right,a=t.left),!(!o||!a)&&Sf(e,o).literals.set($D(a),a)}c(_K,"addEqualityConstraint");function wK(e,t){if(t.kind!=="BoundInList"||t.negated||!Array.isArray(t.list)||t.expr?.kind!=="BoundColumnRef"||!t.list.every(n=>n.kind==="BoundLiteral"))return;let s=Sf(e,t.expr);for(let n of t.list)s.literals.set($D(n),n)}c(wK,"addInListConstraint");function bK(e,t){if(t.kind==="BoundBetween"&&!t.negated&&t.expr?.kind==="BoundColumnRef"){t.low?.kind==="BoundLiteral"&&Kv(Sf(e,t.expr),">=",t.low),t.high?.kind==="BoundLiteral"&&Hv(Sf(e,t.expr),"<=",t.high);return}if(t.kind!=="BoundBinary")return;let s=t.left?.kind==="BoundColumnRef",n=t.right?.kind==="BoundColumnRef",r=t.left?.kind==="BoundLiteral",i=t.right?.kind==="BoundLiteral";if(s&&i){let o=Sf(e,t.left);(t.op===">="||t.op===">")&&Kv(o,t.op,t.right),(t.op==="<="||t.op==="<")&&Hv(o,t.op,t.right)}else if(n&&r){let o=Sf(e,t.right);(t.op==="<="||t.op==="<")&&Kv(o,gL(t.op),t.left),(t.op===">="||t.op===">")&&Hv(o,gL(t.op),t.left)}}c(bK,"addRangeConstraint");function Kv(e,t,s){(!e.lower||Fw(s,e.lower.literal)>0)&&(e.lower={op:t,literal:s})}c(Kv,"setLower");function Hv(e,t,s){(!e.upper||Fw(s,e.upper.literal)<0)&&(e.upper={op:t,literal:s})}c(Hv,"setUpper");function gL(e){return e==="<="?">=":e==="<"?">":e===">="?"<=":e===">"?"<":e}c(gL,"flipRangeOp");function Fw(e,t){let s=e.value,n=t.value;return typeof s=="number"&&typeof n=="number"?s-n:String(s).localeCompare(String(n))}c(Fw,"compareLiteral");function $w(e){return e?e.kind==="BoundBinary"&&e.op==="OR"?[...$w(e.left),...$w(e.right)]:[e]:[]}c($w,"splitOr");function Di(e){let t=e;return`${(t.tableAlias||"").toUpperCase()}.${(t.columnName||"").toUpperCase()}`}c(Di,"colKey");function Ao(e){return e.kind==="BoundBinary"?`${e.op}:${uw(e.left)}:${uw(e.right)}`:e.kind==="BoundInList"?`IN:${uw(e.expr)}:${e.list.map(uw).join(",")}:${e.negated}`:JSON.stringify(e).slice(0,80)}c(Ao,"predKey");function uw(e){return e?e.kind==="BoundColumnRef"?Di(e):e.kind==="BoundLiteral"?`LIT:${e.value}`:JSON.stringify(e).slice(0,40):"null"}c(uw,"exprKey2");function $D(e){let t=e;return`${t.dataType||""}:${String(t.value)}`}c($D,"literalKey");function fw(e,t){for(let s of e)if(s.kind==="BoundBinary"){if(s.left?.kind==="BoundColumnRef"&&Di(s.left)===t)return s.left;if(s.right?.kind==="BoundColumnRef"&&Di(s.right)===t)return s.right}return null}c(fw,"findColExpr");function yL(e){let t=new Set;function s(n){if(n){n.type==="Scan"&&t.add((n.alias||n.table).toUpperCase());for(let r of cs(n))s(r)}}return c(s,"walk"),s(e),t}c(yL,"collectTables");function xK(e){let t=[];return Kn(e,s=>{s.kind==="BoundColumnRef"&&s.tableAlias&&t.push(s.tableAlias.toUpperCase())}),t}c(xK,"collectTableRefs2");function _L(e,t){e&&e.type==="Filter"&&t.push(...In(e.condition))}c(_L,"collectFiltersAbove");function Kn(e,t){if(!e||typeof e!="object")return;t(e);let s=e;if(s.left&&Kn(s.left,t),s.right&&Kn(s.right,t),s.operand&&Kn(s.operand,t),s.expr&&Kn(s.expr,t),s.low&&Kn(s.low,t),s.high&&Kn(s.high,t),s.args)for(let n of s.args)Kn(n,t);if(Array.isArray(s.list))for(let n of s.list)Kn(n,t);if(s.pattern&&Kn(s.pattern,t),s.source&&Kn(s.source,t),s.whenClauses)for(let n of s.whenClauses)Kn(n.condition,t),Kn(n.result,t);s.elseExpr&&Kn(s.elseExpr,t)}c(Kn,"walkExpr");$e();Ke();Vt();var SK=class extends Be{static{c(this,"SortElimination")}get name(){return"SortElimination"}apply(e){return new AK().rewrite(e)}},AK=class extends ze{static{c(this,"SortEliminationRewriter")}rewriteSort(e){let t=this.rewrite(e.children[0]);if(!t._sortedBy||t._sortedBy.length===0)return t!==e.children[0]?{...e,children:[t]}:e;let s=e.orderKeys.map(i=>({key:vK(i.expr),direction:i.direction||"ASC"}));if(s.some(i=>!i.key))return t!==e.children[0]?{...e,children:[t]}:e;let n=t._sortedBy,r=!0;for(let i=0;i<s.length;i++){if(i>=n.length){r=!1;break}let o=n[i],a=typeof o=="object"?o.key:o,l=typeof o=="object"&&o.direction||"ASC";if(!kK(a,s[i].key)){r=!1;break}if(l.toUpperCase()!==s[i].direction.toUpperCase()){r=!1;break}}return r?t:t!==e.children[0]?{...e,children:[t]}:e}};function vK(e){return e&&e.kind==="BoundColumnRef"?`${e.tableAlias||""}.${e.columnName}`.toUpperCase():null}c(vK,"getColumnKey");function kK(e,t){if(!e||!t)return!1;if(e===t)return!0;let s=e.split(".").pop(),n=t.split(".").pop();return s===n}c(kK,"columnMatches");$e();Ke();Ft();Vt();var EK=class extends Be{static{c(this,"JoinElimination")}get name(){return"JoinElimination"}apply(e){return new RK().rewrite(e)}},NK=new Set(["Project","Aggregate"]),RK=class extends ze{static{c(this,"JoinEliminationRewriter")}rewriteDefault(e){let t=this.rewriteChildren(e);return NK.has(t.type)&&BK(t)?CK(t):t}};function BK(e){return e.children?e.children.some(t=>t.type==="Join"&&t.joinType==="LEFT"):!1}c(BK,"hasLeftJoinChild");function CK(e){let t=e.children||[],s=t.map(r=>{if(r.type!=="Join"||r.joinType!=="LEFT")return r;let i=IK(r.children[1]),o=LK(r.children[1]),a=new Set;return TK(e,a),OK(i,a)||MK(o,a)?r:r.children[0]});return s.some((r,i)=>r!==t[i])?{...e,children:s}:e}c(CK,"tryEliminateLeftJoin");function TK(e,t){let s=c(n=>{if(!(!n||typeof n!="object")){if(n.kind==="BoundColumnRef"){t.add(`${(n.tableAlias||"").toUpperCase()}.${(n.columnName||"").toUpperCase()}`);return}for(let r of Object.values(n))if(Array.isArray(r))for(let i of r)s(i);else r&&typeof r=="object"&&s(r)}},"collectExpr");switch(e.type){case"Project":for(let n of e.expressions)s(n);break;case"Filter":e.condition&&s(e.condition);break;case"Aggregate":if(e.groupBy)for(let n of e.groupBy)s(n);for(let n of e.aggregates)for(let r of n.args||[])s(r);break;case"Sort":for(let n of e.orderKeys)s(n.expr);break;case"Distinct":break}}c(TK,"collectNodeExprColumns");function IK(e){let t=new Set;function s(n){if(n){n.type==="Scan"&&t.add((n.alias||n.table).toUpperCase());for(let r of cs(n))s(r)}}return c(s,"walk"),s(e),t}c(IK,"collectTableAliases");function OK(e,t){for(let s of t){let n=s.split(".")[0];if(e.has(n))return!0}return!1}c(OK,"hasAnyColumnUsed");function MK(e,t){for(let s of t){let n=s.split(".")[1];if(n&&e.has(n))return!0}return!1}c(MK,"hasAnyNameUsed");function LK(e){let t=new Set,s=c(i=>{let o=(i||"").toUpperCase();o&&t.add(o)},"add"),n=c(i=>s(i?.outputName||i?.alias||i?.name||i?.columnName),"outputName3");function r(i){if(i)switch(i.type){case"Scan":for(let o of i.columns||[])s(o.name||o.columnName);return;case"Project":for(let o of i.expressions||[])n(o);return;case"Aggregate":for(let o of i.groupBy||[])n(o);for(let o of i.aggregates||[])n(o);return;default:for(let o of cs(i))r(o)}}return c(r,"walk"),r(e),t}c(LK,"collectOutputNames");$e();Ke();Ft();Vt();var PK=class extends Be{static{c(this,"PredicateDedup")}get name(){return"PredicateDedup"}apply(e){return new DK().rewrite(e)}},DK=class extends ze{static{c(this,"DedupRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]),s=In(e.condition),n=wL(s);return n.length===0?t:n.length===s.length&&t===e.children[0]?e:mn(Cs(n),t)}rewriteJoin(e){let t=this.rewriteChildren(e);if(!t.condition)return t;let s=In(t.condition),n=wL(s);return n.length===s.length?t:n.length===0?{...t,condition:null}:{...t,condition:Cs(n)}}};function wL(e){let t=new Set,s=[];for(let n of e){let r=Bs(n);t.has(r)||(t.add(r),s.push(n))}return s}c(wL,"dedup");function Bs(e){if(!e||typeof e!="object")return String(e);switch(e.kind){case"BoundColumnRef":return`COL:${(e.tableAlias||"").toUpperCase()}.${(e.columnName||"").toUpperCase()}`;case"BoundLiteral":return`LIT:${String(e.value)}:${e.dataType||""}`;case"BoundBinary":{let t=Bs(e.left),s=Bs(e.right),n=e.op;return["=","<>","AND","OR","+","*"].includes(n)?`BIN:${n}:${t<s?t:s}:${t<s?s:t}`:`BIN:${n}:${t}:${s}`}case"BoundUnary":return`UNARY:${e.op}:${Bs(e.operand)}`;case"BoundLike":return`LIKE:${e.negated}:${Bs(e.expr)}:${Bs(e.pattern)}`;case"BoundInList":return`IN:${e.negated}:${Bs(e.expr)}:${Array.isArray(e.list)?e.list.map(Bs).join(","):Bs(e.list)}`;case"BoundBetween":return`BTW:${e.negated}:${Bs(e.expr)}:${Bs(e.low)}:${Bs(e.high)}`;case"BoundIsNull":return`ISNULL:${e.negated}:${Bs(e.expr)}`;case"BoundAggregate":return`AGG:${e.name}:${e.distinct}:${e.args.map(Bs).join(",")}`;case"BoundFunction":return`FN:${e.name}:${e.args.map(Bs).join(",")}`;case"BoundExtract":return`EXT:${e.field}:${Bs(e.source)}`;case"BoundCase":return`CASE:${e.whenClauses.map(t=>`${Bs(t.condition)}:${Bs(t.result)}`).join(";")}:${Bs(e.elseExpr)}`;default:return JSON.stringify(e).slice(0,100)}}c(Bs,"exprKey3");$e();Ft();Ke();Vt();var FK=class extends Be{static{c(this,"JoinResidualSplit")}get name(){return"JoinResidualSplit"}apply(e){return new $K().rewrite(e)}},$K=class extends ze{static{c(this,"JoinResidualSplitRewriter")}rewriteJoin(e){let t=this.rewriteChildren(e);if(t.joinType!=="INNER"||!t.condition)return t;let s=bL(t.children[0]),n=bL(t.children[1]),r=[],i=[];for(let a of In(t.condition))UK(a,s,n)?i.push(a):r.push(a);if(i.length===0||r.length===0)return t;let o=Lr(t.joinType,Cs(r),t.children[0],t.children[1],t.physicalStrategy);return mn(Cs(i),zK(o,t))}};function zK(e,t){let s={...e},n=t;for(let r of Object.keys(n))r.startsWith("_")&&(s[r]=n[r]);return n.markColumn&&(s.markColumn=n.markColumn),s}c(zK,"copyJoinMetadata");function UK(e,t,s){if(!e||e.kind!=="BoundBinary"||e.op!=="OR")return!1;let n=VK(e);return n.some(r=>xL(r,t))&&n.some(r=>xL(r,s))}c(UK,"isCrossSideOr");function VK(e){let t=[];return Hn(e,s=>{s.kind==="BoundColumnRef"&&t.push({tableAlias:(s.tableAlias||"").toUpperCase(),columnName:(s.columnName||"").toUpperCase()})}),t}c(VK,"collectExprRefs");function bL(e){let t={aliases:new Set,columns:new Set};return Tk(e,t),t.aliases.delete(""),t.columns.delete(""),t}c(bL,"collectPlanRefs3");function Tk(e,t){if(e){if(e.type==="Scan"){t.aliases.add((e.alias||e.table||"").toUpperCase());for(let s of e.columns||[])t.columns.add((s.name||s.columnName||"").toUpperCase());return}if(e.type==="CTEScan"){t.aliases.add((e.alias||e.cteName||"").toUpperCase());return}if(e.type==="Project"){for(let s of e.expressions||[])t.columns.add(Jv(s));return}if(e.type==="Aggregate"){for(let s of e.groupBy||[])t.columns.add(Jv(s));for(let s of e.aggregates||[])t.columns.add(Jv(s));return}if(e.type==="Join"||e.type==="Union"){for(let s of cs(e))Tk(s,t);return}e.children?.[0]&&Tk(e.children[0],t)}}c(Tk,"addOutputRefs4");function Jv(e){return(e?.outputName||e?.alias||e?.name||e?.columnName||"").toUpperCase()}c(Jv,"outputName2");function xL(e,t){return e.tableAlias?t.aliases.has(e.tableAlias):t.columns.has(e.columnName)}c(xL,"refBelongsToPlan3");function Hn(e,t){if(!e||typeof e!="object")return;t(e);let s=e;if(s.left&&Hn(s.left,t),s.right&&Hn(s.right,t),s.operand&&Hn(s.operand,t),s.expr&&Hn(s.expr,t),s.low&&Hn(s.low,t),s.high&&Hn(s.high,t),s.args)for(let n of s.args)Hn(n,t);if(s.whenClauses)for(let n of s.whenClauses)Hn(n.condition,t),Hn(n.result,t);if(s.elseExpr&&Hn(s.elseExpr,t),s.list&&Array.isArray(s.list))for(let n of s.list)Hn(n,t);s.pattern&&Hn(s.pattern,t),s.source&&Hn(s.source,t)}c(Hn,"walkExpr2");$e();Ke();Ft();var GK=class extends Be{static{c(this,"TopNFusion")}get name(){return"TopNFusion"}apply(e){return new qK().rewrite(e)}},qK=class extends ze{static{c(this,"TopNFusionRewriter")}rewriteLimit(e){let t=this.rewrite(e.children[0]);if(t.type==="Sort")return{type:"TopN",orderKeys:t.orderKeys,count:e.count,offset:e.offset||0,children:t.children,_sortedBy:t._sortedBy,_cardinality:Math.min(e.count,t._cardinality||1/0)};if(t.type==="Project"&&t.children[0]?.type==="Sort"){let s=t.children[0],n={type:"TopN",orderKeys:s.orderKeys,count:e.count,offset:e.offset||0,children:s.children,_sortedBy:s._sortedBy,_cardinality:Math.min(e.count,s._cardinality||1/0)};return{...t,children:[n]}}return t!==e.children[0]?{...e,children:[t]}:e}};$e();Ke();Ft();Vt();Fe();var jK=class extends Be{static{c(this,"IndexSelection")}catalog;statistics;constructor(e,t){super(),this.catalog=e,this.statistics=t}get name(){return"IndexSelection"}apply(e){return new WK(this.catalog,this.statistics).rewrite(e)}},WK=class extends ze{static{c(this,"IndexSelectionRewriter")}catalog;statistics;constructor(e,t){super(),this.catalog=e,this.statistics=t}rewriteFilter(e){let t=this.rewrite(e.children[0]);if(t.type!=="Scan")return{...e,children:[t]};let s=t.table,n=t.alias||s,r=In(e.condition),i=new Map,o=[];for(let b=0;b<r.length;b++){let x=this._analyzeConjunct(r[b],n);if(!x){o.push({idx:b,indexed:!1});continue}o.push({idx:b,indexed:!0,column:x.column}),i.has(x.column)||i.set(x.column,{point:null,low:null,high:null,lowInc:!1,highInc:!1,conjunctIndices:[]});let A=i.get(x.column);A.conjunctIndices.push(b),x.type==="eq"?A.point=x.value:x.type==="gt"||x.type==="gte"?(A.low=x.value,A.lowInc=x.type==="gte"):(x.type==="lt"||x.type==="lte")&&(A.high=x.value,A.highInc=x.type==="lte")}let a=null,l=null;for(let[b,x]of i)if(this.catalog.getIndexForColumn(s,b)){if(this.statistics){let S=this.statistics.get(s.toUpperCase());if(S){let E=S.getColumnStats(b);if(E&&E.ndv>0){let R;if(x.point!==null?R=1/E.ndv:R=this._estimateRangeSelectivity(E,x),R>ht.indexScanSelectivityThreshold)continue}}}a=b,l=x;break}if(!a||!l)return{...e,children:[t]};let u=new Set(l.conjunctIndices),f=r.filter((b,x)=>!u.has(x)),h,p,d,m,g,y,_=`idx_${s}_${a}`.toUpperCase();l.point!==null?(h="point",p=l.point,d=null,m=null,g=!0,y=!0):(h="range",p=null,d=l.low,m=l.high,g=l.lowInc,y=l.highInc);let w=L4(s,n,_,a,h,p,d,m,g,y,t.columns);return f.length>0?mn(Cs(f),w):w}_estimateRangeSelectivity(e,t){let s=hw(e.min),n=hw(e.max);if(s===null||n===null||n<=s)return .33;let r=t.low!==null?hw(t.low):s,i=t.high!==null?hw(t.high):n;r===null&&(r=s),i===null&&(i=n);let o=Math.max(0,Math.min(n,i)-Math.max(s,r));return Math.max(1e-4,o/(n-s))}_analyzeConjunct(e,t){if(e.kind!=="BoundBinary")return null;let s=e.op;if(s!=="="&&s!==">"&&s!==">="&&s!=="<"&&s!=="<=")return null;let n,r,i=!1;if(e.left.kind==="BoundColumnRef"&&e.right.kind==="BoundLiteral")n=e.left,r=e.right;else if(e.right.kind==="BoundColumnRef"&&e.left.kind==="BoundLiteral")n=e.right,r=e.left,i=!0;else return null;if(n.kind!=="BoundColumnRef"||r.kind!=="BoundLiteral"||n.tableAlias&&n.tableAlias.toUpperCase()!==t.toUpperCase())return null;let o=n.columnName.toUpperCase(),a=r.value,l;return s==="="?l="eq":s===">"?l=i?"lt":"gt":s===">="?l=i?"lte":"gte":s==="<"?l=i?"gt":"lt":l=i?"gte":"lte",{column:o,value:a,type:l}}};function hw(e){return e==null?null:typeof e=="bigint"?Number(e):typeof e=="number"?e:null}c(hw,"toNumber3");Fe();var pw=class{static{c(this,"BTreeNode")}isLeaf;keys;children;values;next;constructor(e){this.isLeaf=e,this.keys=[],this.children=[],this.values=[],this.next=null}},KK=class{static{c(this,"BTreeIndex")}dataType;root;order;constructor(e){this.dataType=e,this.root=new pw(!0),this.order=ht.btreeOrder}_compare(e,t){return e===t?0:e==null?-1:t==null?1:(typeof e=="bigint"&&(e=Number(e)),typeof t=="bigint"&&(t=Number(t)),e<t?-1:e>t?1:0)}_findIndex(e,t){let s=0,n=e.length;for(;s<n;){let r=s+n>>>1;this._compare(e[r],t)<0?s=r+1:n=r}return s}insert(e,t){let s=this._insertInternal(this.root,e,t);if(s){let n=new pw(!1);n.keys=[s.key],n.children=[this.root,s.right],this.root=n}}_insertInternal(e,t,s){if(e.isLeaf){let o=this._findIndex(e.keys,t);return o<e.keys.length&&this._compare(e.keys[o],t)===0?(e.values[o].push(s),null):(e.keys.splice(o,0,t),e.values.splice(o,0,[s]),e.keys.length>=this.order?this._splitLeaf(e):null)}let n=this._findChildIndex(e,t),r=this._insertInternal(e.children[n],t,s);if(!r)return null;let i=this._findIndex(e.keys,r.key);return e.keys.splice(i,0,r.key),e.children.splice(i+1,0,r.right),e.keys.length>=this.order?this._splitInternal(e):null}_findChildIndex(e,t){let s=0,n=e.keys.length;for(;s<n;){let r=s+n>>>1;this._compare(e.keys[r],t)<=0?s=r+1:n=r}return s}_splitLeaf(e){let t=Math.ceil(e.keys.length/2),s=e.keys.splice(t),n=e.values.splice(t),r=new pw(!0);return r.keys=s,r.values=n,r.next=e.next,e.next=r,{key:s[0],right:r}}_splitInternal(e){let t=Math.floor(e.keys.length/2),s=e.keys[t],n=e.keys.splice(t+1);e.keys.splice(t,1);let r=e.children.splice(t+1),i=new pw(!1);return i.keys=n,i.children=r,{key:s,right:i}}search(e){let t=this.root;for(;!t.isLeaf;){let n=this._findChildIndex(t,e);t=t.children[n]}let s=this._findIndex(t.keys,e);return s<t.keys.length&&this._compare(t.keys[s],e)===0?t.values[s]:[]}*range(e,t,s,n){let r=this.root;if(e!=null)for(;!r.isLeaf;){let l=this._findChildIndex(r,e);r=r.children[l]}else for(;!r.isLeaf;)r=r.children[0];let i=0;e!=null&&(i=this._findIndex(r.keys,e),!s&&i<r.keys.length&&this._compare(r.keys[i],e)===0&&i++);let o=r,a=i;for(;o;){for(;a<o.keys.length;){let l=o.keys[a];if(t!=null){let u=this._compare(l,t);if(u>0||u===0&&!n)return}for(let u of o.values[a])yield u;a++}o=o.next,a=0}}};$e();Ke();Vt();var HK=class extends Be{static{c(this,"FilterOrdering")}cardEstimator;constructor(e=new Map){super(),this.cardEstimator=new uE(e)}get name(){return"FilterOrdering"}apply(e){return new JK(this.cardEstimator).rewrite(e)}},JK=class extends ze{static{c(this,"FilterOrderingRewriter")}cardEstimator;constructor(e){super(),this.cardEstimator=e}rewriteFilter(e){let t=this.rewriteChildren(e),s=Ik(t.condition);if(s.length<2)return t;let n=s.map(i=>({pred:i,selectivity:this.cardEstimator.estimateSelectivity(i)}));n.sort((i,o)=>i.selectivity-o.selectivity);let r=YK(n.map(i=>i.pred));return{...t,condition:r}}};function Ik(e){return e?e.kind==="BoundBinary"&&e.op==="AND"?[...Ik(e.left),...Ik(e.right)]:[e]:[]}c(Ik,"splitConjuncts5");function YK(e){return e.length===0?null:e.length===1?e[0]:e.reduce((t,s)=>({kind:"BoundBinary",op:"AND",left:t,right:s,resultType:"BOOLEAN"}))}c(YK,"combineConjuncts4");$e();Ft();Ke();Vt();var Yv=new Map([["SUM",{partial:"SUM",final:"SUM"}],["COUNT",{partial:"COUNT",final:"SUM"}],["MIN",{partial:"MIN",final:"MIN"}],["MAX",{partial:"MAX",final:"MAX"}]]),QK=class extends Be{static{c(this,"AggregatePushdown")}get name(){return"AggregatePushdown"}apply(e){return new XK().rewrite(e)}},XK=class extends ze{static{c(this,"AggregatePushdownRewriter")}rewriteAggregate(e){let t=this.rewriteChildren(e),s=t.children[0];if(s.type!=="Join"||s.joinType!=="INNER"||!t.groupBy||t.groupBy.length===0||!t.aggregates||t.aggregates.length===0)return t;let n=t.aggregates;if(!this.allAggregatesDecomposable(n))return t;let r=this.collectPlanTableRefs(s.children[0]),i=this.collectPlanTableRefs(s.children[1]),o=this.classifyColumns(t.groupBy,r,i);if(o==="both"||o==="none")return t;let a=this.classifyAggInputs(n,r,i);if(a==="both"||a!==o&&a!=="none")return t;let u=o==="left"?0:1,f=this.buildPartialAggregates(n),h=id([...t.groupBy],f,s.children[u]),p=[...s.children];p[u]=h;let d={...s,children:p},m=this.buildFinalAggregates(n,f);return id(t.groupBy,m,d)}allAggregatesDecomposable(e){return e.every(t=>{if(t.distinct)return!1;let s=(t.func||t.functionName||"").toUpperCase();return Yv.has(s)})}buildPartialAggregates(e){return e.map((t,s)=>{let n=(t.func||t.functionName||"").toUpperCase(),r=Yv.get(n);return{...t,func:r.partial,functionName:r.partial,outputName:t.outputName||`_partial_${s}`,_isPartial:!0}})}buildFinalAggregates(e,t){return e.map((s,n)=>{let r=(s.func||s.functionName||"").toUpperCase(),i=Yv.get(r),o={kind:"BoundColumnRef",columnName:t[n].outputName,tableAlias:null};return{...s,func:i.final,functionName:i.final,args:[o],_isFinal:!0}})}classifyColumns(e,t,s){let n=!1,r=!1;for(let i of e){if(i.kind!=="BoundColumnRef")return"both";let o=(i.tableAlias||"").toUpperCase();if(t.has(o))n=!0;else if(s.has(o))r=!0;else return"none"}return n&&r?"both":n?"left":r?"right":"none"}classifyAggInputs(e,t,s){let n=!1,r=!1;for(let i of e)if(!((i.func||i.functionName||"").toUpperCase()==="COUNT"&&(!i.args||i.args.length===0||this.isCountStar(i))))for(let a of i.args||[])this.walkExprRefs(a,l=>{t.has(l)?n=!0:s.has(l)&&(r=!0)});return n&&r?"both":n?"left":r?"right":"none"}isCountStar(e){return!e.args||e.args.length===0?!0:e.args.length===1&&e.args[0]?.kind==="BoundLiteral"}walkExprRefs(e,t){if(!e||typeof e!="object")return;e.kind==="BoundColumnRef"&&e.tableAlias&&t(e.tableAlias.toUpperCase());let s=e;if(s.left&&this.walkExprRefs(s.left,t),s.right&&this.walkExprRefs(s.right,t),s.operand&&this.walkExprRefs(s.operand,t),s.args)for(let n of s.args)this.walkExprRefs(n,t)}collectPlanTableRefs(e){let t=new Set;return this._collectRefs(e,t),t}_collectRefs(e,t){if(e){if(e.type==="Scan"){t.add((e.alias||e.table||"").toUpperCase());return}if(e.children)for(let s of e.children)this._collectRefs(s,t)}}};we();var ZK=64,tH=10,eH=class{static{c(this,"ColumnStatistics")}ndv;min;max;nullFraction;histogram;mcv;avgWidth;avgLength;constructor({ndv:e=0,min:t=null,max:s=null,nullFraction:n=0,histogram:r=null,mcv:i=null,avgWidth:o=8,avgLength:a=null}={}){this.ndv=e,this.min=t,this.max=s,this.nullFraction=n,this.histogram=r,this.mcv=i,this.avgWidth=o,this.avgLength=a}},sH=class{static{c(this,"TableStatistics")}rowCount;columnStats;correlations;constructor(e,t=new Map,s=new Map){this.rowCount=e,this.columnStats=t,this.correlations=s}getColumnStats(e){return this.columnStats.get(e.toUpperCase())||null}setColumnStats(e,t){this.columnStats.set(e.toUpperCase(),t)}_correlationKey(e,t){let s=e.toUpperCase(),n=t.toUpperCase();return s<n?`${s}:${n}`:`${n}:${s}`}getCorrelation(e,t){return this.correlations.get(this._correlationKey(e,t))??null}setCorrelation(e,t,s){this.correlations.set(this._correlationKey(e,t),s)}get avgRowWidth(){let e=0;for(let t of this.columnStats.values())e+=t.avgWidth||8;return e||64}},nH=class{static{c(this,"EquiDepthHistogram")}boundaries;numBuckets;numRows;rowsPerBucket;bucketCounts;bucketDistincts;totalCount;constructor(e,t,s=null,n=null){this.boundaries=e,this.numBuckets=e.length,this.numRows=t,this.rowsPerBucket=t/Math.max(this.numBuckets,1),this.bucketCounts=s,this.bucketDistincts=n,this.totalCount=s?s.reduce((r,i)=>r+i,0):null}bucketInfo(){return{boundaries:this.boundaries,bucketCounts:this.bucketCounts,bucketDistincts:this.bucketDistincts}}estimateLessThan(e){if(this.numBuckets===0)return .5;let t=Ma(e);if(t===null)return .5;let s=0,n=this.numBuckets-1;for(;s<=n;){let l=s+n>>>1;Ma(this.boundaries[l])<t?s=l+1:n=l-1}if(s>=this.numBuckets)return 1;if(s===0){let l=Ma(this.boundaries[0]),u=this.numBuckets>0?Ma(this.boundaries[0]):l,f=l>0?Math.max(0,t)/l:.5;return Math.max(0,Math.min(1,f/this.numBuckets))}let r=Ma(this.boundaries[s-1]),o=Ma(this.boundaries[s])-r,a=o>0?(t-r)/o:.5;return Math.max(0,Math.min(1,(s+Math.max(0,Math.min(1,a)))/this.numBuckets))}estimateRange(e,t){let s=this.estimateLessThan(t+(typeof t=="number"?.5:0)),n=this.estimateLessThan(e);return Math.max(.001,s-n)}},zD=class Zp{static{c(this,"_StatisticsCollector")}static async collect(t){let s=t.rowCount(),n=new Map;for(let i of t.getSchema()){let o=await Zp._collectColumn(t,i,s);n.set(i.name.toUpperCase(),o)}let r=await Zp._computeCorrelations(t,n);return new sH(s,n,r)}static async _collectColumn(t,s,n){let r=[],i=new Map,o=0,a=null,l=null,u=0,f=0,h=0,p=t.getColumnIndex(s.name),d=["INT32","INT64","FLOAT64","DECIMAL","DATE"].includes(s.dataType),m=["VARCHAR","TEXT","STRING"].includes(s.dataType);for await(let A of t.scan())for(let S=0;S<A.size;S++){let E=A.getValue(S,p);if(E==null){o++;continue}let R=typeof E=="bigint"?Number(E):E;r.push(R);let v=String(R);i.set(v,(i.get(v)||0)+1),(a===null||E<a)&&(a=E),(l===null||E>l)&&(l=E),typeof E=="string"?(u+=E.length*2,f+=E.length,h++):typeof E=="bigint"?u+=8:u+=4}let g=new Set(r.map(String)).size,y=n>0?o/n:0,_=r.length>0?Math.ceil(u/r.length):8,w=h>0?f/h:null,b=null;d&&r.length>0&&(b=Zp._buildHistogram(r,n));let x=null;return i.size>0&&(x=Zp._buildMCV(i,r.length)),new eH({ndv:g,min:a,max:l,nullFraction:y,histogram:b,mcv:x,avgWidth:_,avgLength:w})}static _buildHistogram(t,s){let n=[...t].sort((f,h)=>{let p=typeof f=="bigint"?Number(f):f,d=typeof h=="bigint"?Number(h):h;return p-d}),r=Math.min(ZK,Math.max(1,Math.floor(n.length/4))),i=[],o=Math.max(1,Math.floor(n.length/r));for(let f=1;f<=r;f++){let h=Math.min(f*o-1,n.length-1);i.push(n[h])}let a=new Array(r).fill(0),l=new Array(r).fill(0),u=c(f=>Math.min(Math.floor(f/o),r-1),"bucketOf");for(let f=0;f<n.length;f++){let h=u(f);a[h]++,(f===0||u(f-1)!==h||Ma(n[f])!==Ma(n[f-1]))&&l[h]++}return new nH(i,s,a,l)}static _buildMCV(t,s){let n=[...t.entries()].sort((r,i)=>i[1]-r[1]).slice(0,tH);return n.length===0?null:{values:n.map(r=>r[0]),frequencies:n.map(r=>r[1]/s)}}static _computeCorrelations},Qv=1e3,rH=.3;zD._computeCorrelations=async function(e,t){let s=new Map,n=[],r=e.getSchema();for(let f of r)["INT32","INT64","FLOAT64","DECIMAL"].includes(f.dataType)&&n.push({name:f.name.toUpperCase(),idx:e.getColumnIndex(f.name)});if(n.length<2)return s;let i=new Map;for(let f of n)i.set(f.name,[]);let o=0,a=e.rowCount(),l=Math.min(1,Qv/Math.max(a,1)),u=0;for await(let f of e.scan()){for(let h=0;h<f.size&&o<Qv;h++){if(u++,l<1&&u*l%1>=l)continue;let p=!1;for(let d of n){let m=f.getValue(h,d.idx);if(m==null){p=!0;break}}if(!p){for(let d of n){let m=f.getValue(h,d.idx);i.get(d.name).push(typeof m=="bigint"?Number(m):m)}o++}}if(o>=Qv)break}if(o<2)return s;for(let f=0;f<n.length;f++)for(let h=f+1;h<n.length;h++){let p=n[f].name,d=n[h].name,m=i.get(p),g=i.get(d),y=iH(m,g);if(Math.abs(y)>=rH){let _=p<d?`${p}:${d}`:`${d}:${p}`;s.set(_,y)}}return s};function iH(e,t){let s=e.length;if(s<2)return 0;let n=0,r=0;for(let h=0;h<s;h++)n+=e[h],r+=t[h];let i=n/s,o=r/s,a=0,l=0,u=0;for(let h=0;h<s;h++){let p=e[h]-i,d=t[h]-o;a+=p*d,l+=p*p,u+=d*d}let f=Math.sqrt(l*u);return f>0?a/f:0}c(iH,"pearsonCorrelation");function Ma(e){return e==null?null:typeof e=="bigint"?Number(e):typeof e=="number"?e:null}c(Ma,"toNum2");var oH=class{static{c(this,"StatisticsCache")}catalog;cache;versions;constructor(e){this.catalog=e,this.cache=new Map,this.versions=new Map}get(e){let t=e.toUpperCase(),s=this.cache.get(t);if(s&&s.version===this.getVersion(t))return s.stats}has(e){return this.get(e)!==void 0}set(e,t){let s=e.toUpperCase();this.cache.set(s,{stats:t,version:this.getVersion(s)})}async ensure(e){let t=e.toUpperCase(),s=this.get(t);if(s)return s;let n=this.catalog.getTableStorage(t);if(!n)return;let r=await zD.collect(n);return this.set(t,r),r}async ensureAll(){for(let e of this.catalog.listTables())await this.ensure(e)}invalidate(e){let t=e.toUpperCase();this.versions.set(t,this.getVersion(t)+1)}invalidateAll(){for(let e of this.cache.keys())this.invalidate(e)}getVersion(e){return this.versions.get(e)||0}get size(){let e=0;for(let[t,s]of this.cache)s.version===this.getVersion(t)&&e++;return e}*values(){for(let[e,t]of this.cache)t.version===this.getVersion(e)&&(yield t.stats)}*entries(){for(let[e,t]of this.cache)t.version===this.getVersion(e)&&(yield[e,t.stats])}[Symbol.iterator](){return this.entries()}toMap(){let e=new Map;for(let[t,s]of this)e.set(t,s);return e}};Ft();Ft();Vt();we();var aH=class extends Error{static{c(this,"UnknownColumnError")}},lH=class extends Error{static{c(this,"AmbiguousColumnError")}},Jl=class{static{c(this,"DFField")}name;dataType;index;tableAlias;constructor(e,t,s,n){this.name=e,this.dataType=t,this.index=s,this.tableAlias=n||""}};function Gp(e){return e.map((t,s)=>new Jl(t.name,t.dataType,s,t.tableAlias))}c(Gp,"reindex");var Ef=class Ia{static{c(this,"_DFSchema")}_fields;constructor(t){this._fields=t}static fromStorageSchema(t,s){return new Ia(t.map((n,r)=>new Jl(n.name,n.dataType,r,s)))}static fromFields(t){return new Ia(Gp(t))}get fields(){return this._fields}get length(){return this._fields.length}field(t){return this._fields[t]}names(){return this._fields.map(t=>t.name)}resolve(t,s=null){let n=t.toUpperCase(),r=s?s.toUpperCase():null,i=null;for(let o of this._fields)if(o.name.toUpperCase()===n&&!(r&&o.tableAlias.toUpperCase()!==r)){if(i)throw new lH(`Ambiguous column reference: ${t}`);i=o}if(!i){let o=s?`${s}.`:"";throw new aH(`Unknown column: ${o}${t}`)}return i}has(t,s=null){try{return this.resolve(t,s),!0}catch{return!1}}project(t){return new Ia(Gp(t.map(s=>new Jl(s.name,s.dataType,0,s.tableAlias||""))))}drop(t){let s=new Set(t.map(r=>r.toUpperCase())),n=this._fields.filter(r=>!s.has(r.name.toUpperCase()));return new Ia(Gp(n))}append(t){return new Ia(Gp([...this._fields,...t.fields]))}requalify(t){return new Ia(this._fields.map(s=>new Jl(s.name,s.dataType,s.index,t)))}static aggregateOutput(t,s){return new Ia(Gp([...t,...s]))}};Vt();we();we();var SL={INT32:1,INT64:2,DECIMAL:3,FLOAT64:4};function UD(e){return e==null?null:typeof e=="boolean"?"BOOLEAN":typeof e=="bigint"?"INT64":typeof e=="number"?Number.isInteger(e)?"INT32":"FLOAT64":e instanceof Date?"TIMESTAMP":"VARCHAR"}c(UD,"inferValueType");function cH(e,t){return e===null?t:t===null||e===t?e:vw(e)&&vw(t)?SL[e]>=SL[t]?e:t:"VARCHAR"}c(cH,"reconcileTypes");function Ok(e){let t=null;for(let s of e)t=cH(t,UD(s));return t===null?"VARCHAR":t}c(Ok,"inferColumnType");function Mk(e,t){if(e==null)return null;switch(t){case"BOOLEAN":return e;case"INT32":return Number(e);case"FLOAT64":return Number(e);case"INT64":return BigInt(e);case"DECIMAL":return BigInt(Math.round(Number(e)*ML));case"DATE":return e instanceof Date?rd(e.getUTCFullYear(),e.getUTCMonth()+1,e.getUTCDate()):Number(e);case"TIMESTAMP":return e instanceof Date?BigInt(e.getTime()):BigInt(e);case"VARCHAR":return String(e);default:return e}}c(Mk,"coerceForColumn");we();function dw(e,t){return e==="FLOAT64"||t==="FLOAT64"?"FLOAT64":e==="DECIMAL"||t==="DECIMAL"?"DECIMAL":e==="INT64"||t==="INT64"?"INT64":e==="DATE"?"DATE":"INT32"}c(dw,"inferArithmeticType");function wf(){return"BOOLEAN"}c(wf,"inferComparisonType");function AL(){return"BOOLEAN"}c(AL,"inferLogicalType");function uH(e,t){switch(e.toUpperCase()){case"COUNT":case"COUNT_STAR":return"INT64";case"AVG":return"FLOAT64";case"SUM":case"MIN":case"MAX":return t||"FLOAT64";default:return"FLOAT64"}}c(uH,"inferAggregateResultType");Vt();function fH(e){let t=new Map;for(let s of e.fields){let n=s.tableAlias||"";t.has(n)||t.set(n,[]),t.get(n).push({name:s.name,dataType:s.dataType})}return t}c(fH,"groupByAlias");function hH(e){return e?e.kind==="BoundColumnRef"?e.columnName:e.kind==="BoundAggregate"||e.kind==="BoundFunction"?e.name.toLowerCase():null:null}c(hH,"deriveOutputName");function pH(e,t,s,n){let r=aW(e),i=new OD;for(let[l,u]of fH(t))i.addTable(l,{originalName:l,columns:u});let a=new Sk(s,n).bindExpression(r,i);return{expr:a,dataType:nn(a),outputName:hH(a)}}c(pH,"bindScalarSql");function dH(e){return e?e.kind==="BoundColumnRef"?e.columnName:e.kind==="BoundAggregate"?e.name.toLowerCase():null:null}c(dH,"deriveName");var On=class Li{static{c(this,"_Col")}_build;_name;_alias;constructor(t,s=null){this._build=t,this._name=s,this._alias=null}alias(t){let s=new Li(this._build,this._name);return s._alias=t,s}as(t){return this.alias(t)}bind(t,s){let n=this._build(t,s),r=this._alias||this._name||dH(n);return r&&(n.outputName=r),{expr:n,outputName:r,dataType:nn(n)}}_binary(t,s,n){let r=mw(s);return new Li((i,o)=>{let a=this._build(i,o),l=r._build(i,o);return Cn(t,a,l,n(nn(a),nn(l)))})}add(t){return this._binary("+",t,dw)}sub(t){return this._binary("-",t,dw)}mul(t){return this._binary("*",t,dw)}div(t){return this._binary("/",t,dw)}eq(t){return this._binary("=",t,wf)}ne(t){return this._binary("<>",t,wf)}lt(t){return this._binary("<",t,wf)}le(t){return this._binary("<=",t,wf)}gt(t){return this._binary(">",t,wf)}ge(t){return this._binary(">=",t,wf)}and(t){return this._binary("AND",t,AL)}or(t){return this._binary("OR",t,AL)}not(){return new Li((t,s)=>Zv("NOT",this._build(t,s),"BOOLEAN"))}isNull(){return new Li((t,s)=>ek(this._build(t,s),!1))}isNotNull(){return new Li((t,s)=>ek(this._build(t,s),!0))}like(t){return new Li((s,n)=>DL(this._build(s,n),Yw(t)._build(s,n),!1))}between(t,s){let n=mw(t),r=mw(s);return new Li((i,o)=>PL(this._build(i,o),n._build(i,o),r._build(i,o),!1))}isin(...t){let s=t.map(mw);return new Li((n,r)=>kw(this._build(n,r),s.map(i=>i._build(n,r)),!1))}cast(t){return new Li((s,n)=>LL(this._build(s,n),t))}};function mw(e){return e instanceof On?e:Yw(e)}c(mw,"toCol");function Fi(e){let t=e.indexOf("."),s=t>=0?e.slice(0,t):null,n=t>=0?e.slice(t+1):e;return new On(i=>{let o=i.resolve(n,s);return La(o.tableAlias,o.name,o.index,o.dataType)},n)}c(Fi,"col");function Yw(e){return new On(()=>ce(e,UD(e)))}c(Yw,"lit");function Qw(e){return new On((t,s)=>pH(e,t,s.catalog,s.functionRegistry).expr)}c(Qw,"expr");function hd(e){return t=>{let s=t instanceof On?t:Fi(t);return new On((r,i)=>{let o=s._build(r,i);return zw(e,[o],!1,uH(e,nn(o)))},e.toLowerCase())}}c(hd,"aggregate");var VD=hd("SUM"),GD=hd("AVG"),qD=hd("MIN"),jD=hd("MAX"),WD=hd("COUNT");function KD(){return new On(()=>zw("COUNT_STAR",[],!1,"INT64"),"count")}c(KD,"countStar");var mH="__l",gH="__r",yH="self";function qp(e){return new On(()=>La(e.tableAlias,e.name,e.index,e.dataType),e.name)}c(qp,"fieldCol");function _H(e,t){return e===t?e:e==="VARCHAR"?t:e}c(_H,"reconcileKeyType");function wH(e,t,s){return new On((n,r)=>tk("COALESCE",[Fi(e).bind(n,r).expr,Fi(t).bind(n,r).expr],s))}c(wH,"coalesceCol");function Xv(e){return e instanceof On?e:Fi(e)}c(Xv,"selectArg");function bH(e){return e instanceof On?e:Qw(e)}c(bH,"predicateArg");function xH(e){let t=new Map;return e.map(s=>{let n=s.toUpperCase(),r=t.get(n)||0;return t.set(n,r+1),r===0?s:`${s}_${r}`})}c(xH,"uniquify");var dr=class Lk{static{c(this,"_DataFrame")}_engine;_plan;_schema;_cteMap;constructor(t,s,n,r=null){this._engine=t,this._plan=s,this._schema=n,this._cteMap=r}_ctx(){return{catalog:this._engine.catalog,functionRegistry:this._engine.functionRegistry}}_derive(t,s,n=null){return new Lk(this._engine,t,s,kL(this._cteMap,n))}columns(){return this._schema.names()}schema(){return this._schema}explain(){return UL(this._plan)}sql(t){let s=this._schema.fields.map(n=>({name:n.name,dataType:n.dataType}));return this._engine.sql(t,{frames:[{name:yH,columns:s,plan:this._plan,cteMap:this._cteMap}]})}select(...t){let s=this._ctx(),n=t.map(o=>Xv(o).bind(this._schema,s)),r=n.map(o=>o.expr),i=n.map((o,a)=>new Jl(o.outputName||`col${a}`,o.dataType,a,""));return this._derive(Ew(r,this._plan),Ef.fromFields(i))}filter(t){let{expr:s,dataType:n}=bH(t).bind(this._schema,this._ctx());if(n!=="BOOLEAN")throw new TypeError(`filter condition must be boolean, got ${n}`);return this._derive(mn(s,this._plan),this._schema)}where(t){return this.filter(t)}withColumn(t,s){let n=(s instanceof On?s:Qw(s)).alias(t),r=this._schema.fields.findIndex(o=>o.name.toUpperCase()===t.toUpperCase()),i=r>=0?this._schema.fields.map((o,a)=>a===r?n:qp(o)):[...this._schema.fields.map(qp),n];return this.select(...i)}drop(...t){for(let r of t)this._schema.resolve(r);let s=new Set(t.map(r=>r.toUpperCase())),n=this._schema.fields.filter(r=>!s.has(r.name.toUpperCase())).map(qp);return this.select(...n)}groupBy(...t){let s=this._ctx(),n=t.map(r=>Xv(r).bind(this._schema,s));return new AH(this._engine,this._plan,this._schema,n,this._cteMap)}orderBy(...t){let s=this._ctx(),n=t.map(r=>{let i=r&&typeof r=="object"&&!(r instanceof On)?r:{col:r,desc:!1},{expr:o}=Xv(i.col).bind(this._schema,s);return{expr:o,direction:i.desc?"DESC":"ASC"}});return this._derive(Nw(n,this._plan),this._schema)}sort(...t){return this.orderBy(...t)}limit(t,s=0){return this._derive(FL(t,s,this._plan),this._schema)}distinct(){return this._derive($L(this._plan),this._schema)}union(t){return this._union(t,!0)}unionAll(t){return this._union(t,!0)}_union(t,s){if(this._schema.length!==t._schema.length)throw new TypeError(`union requires equal column counts: ${this._schema.length} vs ${t._schema.length}`);for(let r=0;r<this._schema.length;r++){let i=this._schema.field(r).dataType,o=t._schema.field(r).dataType;if(!E4(i,o))throw new TypeError(`union column ${r} type mismatch: ${i} vs ${o}`)}let n=zL(this._plan,t._plan,s);return this._derive(n,Ef.fromFields(this._schema.fields),t._cteMap)}join(t,s,n="INNER"){let r=Array.isArray(s)?s:[s],i=Rw[n.toUpperCase()];if(!i)throw new Error(`Unknown join type: ${n}`);let o=`${mH}${this._engine._nextDfId()}_`,a=`${gH}${this._engine._nextDfId()}_`,l=this.select(...this._schema.fields.map(S=>qp(S).alias(`${o}${S.name}`))),u=t.select(...t._schema.fields.map(S=>qp(S).alias(`${a}${S.name}`))),f=this._ctx(),h=null;for(let S of r){let E=this._schema.resolve(S),R=t._schema.resolve(S),v=Cn("=",Fi(`${o}${E.name}`).bind(l._schema,f).expr,Fi(`${a}${R.name}`).bind(u._schema,f).expr,"BOOLEAN");h=h?Cn("AND",h,v,"BOOLEAN"):v}let p=i==="RIGHT",d=p?"LEFT":i,m=p?[u._plan,l._plan]:[l._plan,u._plan],g=Lr(d,h,m[0],m[1]),y=new Lk(this._engine,g,l._schema.append(u._schema),kL(this._cteMap,t._cteMap)),_=new Set(r.map(S=>S.toUpperCase())),w=new Map(r.map(S=>{let E=this._schema.resolve(S),R=t._schema.resolve(S);return[S.toUpperCase(),{left:`${o}${E.name}`,right:`${a}${R.name}`,dataType:_H(E.dataType,R.dataType)}]})),b=[],x=[];for(let S of this._schema.fields){let E=w.get(S.name.toUpperCase());b.push(E?wH(E.left,E.right,E.dataType):Fi(`${o}${S.name}`)),x.push(S.name)}for(let S of t._schema.fields)_.has(S.name.toUpperCase())||(b.push(Fi(`${a}${S.name}`)),x.push(S.name));let A=xH(x);return y.select(...b.map((S,E)=>S.alias(A[E])))}_outputColumns(){return this._schema.fields.map(t=>({name:t.name,expr:null,dataType:t.dataType}))}async collect(){return(await this._engine._runPlan(this._plan,this._outputColumns(),!1,this._cteMap)).toArray()}toArray(){return this.collect()}async count(){let t=zw("COUNT_STAR",[],!1,"INT64");t.outputName="count";let s=id([],[t],this._plan),n=[{name:"count",expr:null,dataType:"INT64"}],i=await(await this._engine._runPlan(s,n,!1,this._cteMap)).toArray();return i.length>0?Number(i[0].count):0}async*chunks(){let t=await this._engine._runPlan(this._plan,this._outputColumns(),!0,this._cteMap);for await(let s of t.chunks())yield s}async show(t=20){let s=await this.limit(t).collect(),n=this._schema.names(),r=n.map((u,f)=>{let h=u.length;for(let p of s){let d=vL(p[n[f]]);d.length>h&&(h=d.length)}return h}),i=c(u=>u.map((f,h)=>f.padEnd(r[h])).join(" | "),"renderRow"),o=r.map(u=>"-".repeat(u)).join("-+-"),a=[i(n),o];for(let u of s)a.push(i(n.map(f=>vL(u[f]))));let l=a.join(`
224
- `);return console.log(l),l}};function vL(e){return e==null?"NULL":String(e)}c(vL,"formatCell");function kL(e,t){if(!e&&!t)return null;let s=new Map;if(e)for(let[n,r]of e)s.set(n,r);if(t)for(let[n,r]of t)s.set(n,r);return s}c(kL,"mergeCteMaps");function SH(e,t,s){let n=e.kind==="BoundColumnRef"?e.columnName:`group${s}`,r=e.tableAlias||"";return{ref:La(r,n,s,nn(e)),outputName:t.outputName||n,dataType:t.dataType}}c(SH,"aggregateGroupField");var AH=class{static{c(this,"GroupedData")}_engine;_childPlan;_childSchema;_groupBounds;_cteMap;constructor(e,t,s,n,r){this._engine=e,this._childPlan=t,this._childSchema=s,this._groupBounds=n,this._cteMap=r}agg(...e){let t={catalog:this._engine.catalog,functionRegistry:this._engine.functionRegistry},s=e.map(p=>p.bind(this._childSchema,t)),n=this._groupBounds.map(p=>p.expr),r=s.map(p=>p.expr),i=id(n,r,this._childPlan),o=[],a=[];for(let p=0;p<this._groupBounds.length;p++){let d=SH(n[p],this._groupBounds[p],p);d.ref.outputName=d.outputName,o.push(d.ref),a.push(new Jl(d.outputName,d.dataType,p,""))}let l=[],u=[];for(let p=0;p<s.length;p++){let d=s[p],m=d.outputName||r[p].name.toLowerCase();d.expr.outputName=m,l.push(d.expr),u.push(new Jl(m,d.dataType,this._groupBounds.length+p,""))}let f=Ew([...o,...l],i),h=Ef.aggregateOutput(a,u);return new dr(this._engine,f,h,this._cteMap)}};be();function EL(e,t){let s=[],n=kt.fromSchema(e,We);for(let r of t)n.size>=We&&(s.push(n),n=kt.fromSchema(e,We)),n.appendRow(r);return(n.size>0||s.length===0)&&s.push(n),s}c(EL,"buildChunks");var pd=class Pk{static{c(this,"_InMemoryRelation")}schema;chunks;_rowCount;constructor(t,s){this.schema=t,this.chunks=s,this._rowCount=s.reduce((n,r)=>n+r.size,0)}getSchema(){return this.schema}rowCount(){return this._rowCount}getColumnIndex(t){let s=t.toUpperCase();return this.schema.findIndex(n=>n.name.toUpperCase()===s)}async*scan(){for(let t of this.chunks)yield t}static fromRows(t,s=null){let n=s?s.map(l=>l.name):t.length>0?Object.keys(t[0]):[],r=Array.isArray(t[0])?(l,u)=>l[u]:(l,u)=>l[n[u]],i=n.map((l,u)=>t.map(f=>r(f,u))),o=n.map((l,u)=>({name:l,dataType:s?s[u].dataType:Ok(i[u])})),a=t.map(l=>o.map((u,f)=>Mk(r(l,f),u.dataType)));return new Pk(o,EL(o,a))}static fromColumns(t,s=null){let n=Object.keys(t),r=n.length>0?t[n[0]].length:0,i=new Map((s||[]).map(l=>[l.name,l.dataType])),o=n.map(l=>({name:l,dataType:i.has(l)?i.get(l):Ok(t[l])})),a=[];for(let l=0;l<r;l++)a.push(o.map(u=>Mk(t[u.name][l],u.dataType)));return new Pk(o,EL(o,a))}static builder(t=null){return new vH(t)}},vH=class{static{c(this,"RelationBuilder")}_declaredSchema;schema;_names;_extractByIndex;_chunks;_chunk;_finished;constructor(e=null){this._declaredSchema=e,this.schema=null,this._names=null,this._extractByIndex=!1,this._chunks=[],this._chunk=null,this._finished=!1}_initFrom(e){let t=this._declaredSchema;if(this._names=t?t.map(s=>s.name):e.length>0?Object.keys(e[0]):[],this._extractByIndex=Array.isArray(e[0]),t)this.schema=t.map(s=>({name:s.name,dataType:s.dataType}));else{let s=this._extractByIndex?(n,r)=>n[r]:(n,r)=>n[this._names[r]];this.schema=this._names.map((n,r)=>({name:n,dataType:Ok(e.map(i=>s(i,r)))}))}this._chunk=kt.fromSchema(this.schema,We)}appendRows(e){if(this._finished)throw new Error("RelationBuilder: appendRows after finish()");if(!e||e.length===0)return this;this.schema===null&&this._initFrom(e);let t=this.schema,s=this._names,n=this._extractByIndex?(r,i)=>r[i]:(r,i)=>r[s[i]];for(let r of e)this._chunk.size>=We&&(this._chunks.push(this._chunk),this._chunk=kt.fromSchema(t,We)),this._chunk.appendRow(t.map((i,o)=>Mk(n(r,o),i.dataType)));return this}finish(){if(this._finished)throw new Error("RelationBuilder: finish() called twice");this._finished=!0,this.schema===null&&(this.schema=this._declaredSchema?this._declaredSchema.map(t=>({name:t.name,dataType:t.dataType})):[],this._chunk=kt.fromSchema(this.schema,We)),(this._chunk.size>0||this._chunks.length===0)&&this._chunks.push(this._chunk);let e=new pd(this.schema,this._chunks);return this._chunk=null,this._chunks=[],e}},kH="__DF",HD=null;function EH(e){HD=e}c(EH,"setDefaultStorageBackend");function NH(e,t){return e==null?null:t==="INT64"||t==="DECIMAL"||t==="TIMESTAMP"?typeof e=="bigint"?e:BigInt(Math.round(Number(e))):typeof e=="bigint"?t==="VARCHAR"?e.toString():Number(e):e}c(NH,"coerceForStorage");var RH=class{static{c(this,"QueryEngine")}catalog;functionRegistry;storageBackend;tempManager;executor;wasmEnabled;_dfIdCounter;precomputedStats;statsCache;optimizer;_statsCollected;_distributedPasses;_activeCancel;workerPool;fragmentPool;parallelEnabled;distributed;constructor(e,t={}){this.catalog=e,this.functionRegistry=hW;let s=t.storageBackend??HD?.(t);if(!s)throw new Error("QueryEngine requires a storageBackend (none provided and no default registered)");this.storageBackend=s,this.tempManager=s.createTempSpace(),this.executor=new rE(e,this.tempManager,s),this.wasmEnabled=!1,this._dfIdCounter=0,this.precomputedStats=t.statistics||null,this.statsCache=new oH(e),this.optimizer=this.createOptimizer(this.precomputedStats)}_nextDfId(){return this._dfIdCounter++}close(){this.tempManager.cleanup()}async collectStatistics(){await this.statsCache.ensureAll();let e=this.statsCache.toMap();return e.size>0?e:null}createOptimizer(e){let t=e||new Map,s=new mW;return s.registerPass(new XW),s.registerPass(new AW),s.registerPass(new aK),s.registerPass(new kW),s.registerPass(new lw),s.registerPass(new dK),s.registerPass(new lw),s.registerPass(new nK),s.registerPass(new lw),s.registerPass(new QK),e&&(s.registerPass(new KW(e)),s.registerPass(new lw)),s.registerPass(new EK),s.registerPass(new RW),s.registerPass(new iK),s.registerPass(new cK),s.registerPass(new fK),s.registerPass(new PK),s.registerPass(new HK(t)),s.registerPass(new jK(this.catalog,e)),s.registerPass(new FK),s.registerPass(new JW(t)),s.registerPass(new SK),s.registerPass(new GK),s}parseSQL(e){return oW(e)}bind(e){return new Sk(this.catalog,this.functionRegistry).bind(e)}plan(e){return uW(e)}optimize(e){return this.optimizer.optimize(e)}async compile(e){let t=this.parseSQL(e),s=!1,n=!1,r=t;if(t.kind==="ExplainStmt")s=!0,r=t.query;else if(t.kind==="ExplainAnalyzeStmt")s=!0,n=!0,r=t.query;else if(t.kind==="CreateTableStmt"||t.kind==="DropTableStmt")return{ddl:t};let i=this.bind(r),o=this.plan(i),a=o._cteMap||new Map;await this._ensureStatistics();let l=this.optimize(o);return a=this.optimizeCTEMap(a),{plan:l,outputColumns:i.outputColumns,cteMap:a,isExplain:s,isAnalyze:n}}optimizeCTEMap(e){if(!e||e.size===0)return e;let t=new Map;for(let[s,n]of e)t.set(s,this.optimize(n));return t}async _ensureStatistics(){if(!this.precomputedStats&&!this._statsCollected){let e=await this.collectStatistics();if(e&&(this.optimizer=this.createOptimizer(e),this._statsCollected=!0,this._distributedPasses))for(let{method:t,args:s}of this._distributedPasses)this.optimizer[t](...s)}}table(e){let t=this.catalog.getTable(e);if(!t)throw new Error(`Unknown table: ${e}`);let s=sk(t.name,t.columns,t.name),n=Ef.fromStorageSchema(t.columns,t.name);return new dr(this,s,n)}createDataFrame(e,t=null){let s=pd.fromRows(e,t),n=`${kH}${this._nextDfId()}`,r=s.getSchema();this.catalog.registerTable(n,r),this.catalog.registerTableStorage(n,s);let i=sk(n,r,n),o=Ef.fromStorageSchema(r,n);return new dr(this,i,o)}sql(e,t={}){let s=this.parseSQL(e);if(s.kind==="CreateTableStmt"||s.kind==="DropTableStmt"||s.kind==="ExplainStmt"||s.kind==="ExplainAnalyzeStmt")throw new Error("sql() supports query statements only");let n=new Sk(this.catalog,this.functionRegistry);for(let l of t.frames||[])n.cteScopes.set(l.name.toUpperCase(),{name:l.name,columns:l.columns,bound:{prebuiltPlan:l.plan}});let r=n.bind(s),i=this.plan(r),o=i._cteMap||null;for(let l of t.frames||[])if(l.cteMap){o=o||new Map;for(let[u,f]of l.cteMap)o.has(u)||o.set(u,f)}let a=Ef.fromFields(r.outputColumns.map((l,u)=>({name:l.name,dataType:l.dataType,index:u,tableAlias:""})));return new dr(this,i,a,o)}async _runPlan(e,t,s=!1,n=null){await this._ensureStatistics();let r=this.optimize(e);this.executor.cteDefinitions=this.optimizeCTEMap(n||new Map);let{sink:i,columnNames:o}=await this.executor.execute(r,t,s);return new bf(o,i)}async run(e){let t=await this.compile(e);if(t.ddl)return this.executeDDL(t.ddl);let{plan:s,outputColumns:n,cteMap:r,isExplain:i,isAnalyze:o}=t;if(i&&!o){let{formatPlan:a}=await Promise.resolve().then(()=>(Lv(),ww));return{rows:[{EXPLAIN_PLAN:a(s)}],columns:["EXPLAIN_PLAN"]}}if(o){let{formatPlan:a}=await Promise.resolve().then(()=>(Lv(),ww)),l=a(s),u=performance.now();this.executor.cteDefinitions=r;let{sink:f,columnNames:h}=await this.executor.execute(s,n),d=await new bf(h,f).toArray(),m=(performance.now()-u).toFixed(2);return{rows:[{EXPLAIN_ANALYZE:`${l}
225
- Execution Time: ${m} ms
226
- Rows Returned: ${d.length}`}],columns:["EXPLAIN_ANALYZE"]}}this._activeCancel=new AbortController;try{this.executor.cteDefinitions=r;let{sink:a,columnNames:l}=await this.executor.execute(s,n);return{rows:await new bf(l,a).toArray(),columns:l}}finally{this._activeCancel=null}}cancel(){this._activeCancel&&this._activeCancel.abort()}async executeDDL(e){if(e.kind==="CreateTableStmt")return this.executeCreateTable(e);if(e.kind==="DropTableStmt")return this.executeDropTable(e);throw new Error(`Unknown DDL: ${e.kind}`)}async executeCreateTable(e){let t=e.name.toUpperCase();if(this.catalog.getTable(t)){if(e.ifNotExists)return{rows:[],columns:[],message:`Table ${t} already exists`};throw new Error(`Table ${t} already exists`)}if(e.as)return this.createTableAsSelect(t,e.as);let s=c(o=>({INTEGER:"INT32",INT:"INT32",INT32:"INT32",BIGINT:"INT64",INT64:"INT64",FLOAT:"FLOAT64",DOUBLE:"FLOAT64",REAL:"FLOAT64",DECIMAL:"DECIMAL",NUMERIC:"DECIMAL",VARCHAR:"VARCHAR",TEXT:"VARCHAR",CHAR:"VARCHAR",DATE:"DATE",TIMESTAMP:"TIMESTAMP",DATETIME:"TIMESTAMP",BOOLEAN:"BOOLEAN",BOOL:"BOOLEAN"})[o.name.toUpperCase()]||"VARCHAR","resolveType"),n=(e.columns??[]).map(o=>({name:o.name.toUpperCase(),dataType:s(o.typeName)})),r=this.storageBackend.createPageStore(this.tempManager.allocate("buffer",t)),i=new Vv(t,n,r);return this.catalog.registerTable(t,n),this.catalog.registerTableStorage(t,i),{rows:[],columns:[],message:`Table ${t} created`}}async createTableAsSelect(e,t){let s=this.bind(t),n=this.plan(s),r=n._cteMap||new Map,i=await this._runPlan(n,s.outputColumns,!1,r),o=i.columns,a=new Set;for(let d of o){let m=d.toUpperCase();if(a.has(m))throw new Error(`Duplicate column name "${d}" in CREATE TABLE ... AS \u2014 use an explicit alias`);a.add(m)}let l=c(()=>o.map((d,m)=>({name:d.toUpperCase(),dataType:s.outputColumns[m]?.dataType??"VARCHAR"})),"fallbackTypes"),u=this.storageBackend.createPageStore(this.tempManager.allocate("buffer",e)),f=null,h=null,p=0;for await(let d of i.chunks()){if(f||(h=o.map((g,y)=>({name:g.toUpperCase(),dataType:d.columns[y].dataType})),f=new Vv(e,h,u)),d.size===0)continue;let m=d.toRows();for(let g of m)for(let y=0;y<g.length;y++)g[y]=NH(g[y],h[y].dataType);await f.insertRows(m),p+=d.size}return f||(h=l(),f=new Vv(e,h,u)),await f.flush(),this.catalog.registerTable(e,h),this.catalog.registerTableStorage(e,f),{rows:[],columns:[],message:`Table ${e} created with ${p} rows`}}executeDropTable(e){let t=e.name.toUpperCase();if(!this.catalog.getTable(t)){if(e.ifExists)return{rows:[],columns:[],message:`Table ${t} does not exist`};throw new Error(`Table ${t} does not exist`)}return this.catalog.dropTable(t),{rows:[],columns:[],message:`Table ${t} dropped`}}async stream(e){let t=await this.compile(e);if(t.ddl)return this.executeDDL(t.ddl);let{plan:s,outputColumns:n,cteMap:r,isExplain:i,isAnalyze:o}=t;if(i&&!o){let{formatPlan:u}=await Promise.resolve().then(()=>(Lv(),ww));return{rows:[{EXPLAIN_PLAN:u(s)}],columns:["EXPLAIN_PLAN"]}}this.executor.cteDefinitions=r;let{sink:a,columnNames:l}=await this.executor.execute(s,n,!0);return new bf(l,a)}async buildIndexes(){for(let e of this.catalog.listTables()){let t=this.catalog.getTable(e);if(!t.primaryKey||t.primaryKey.length===0)continue;let s=this.catalog.getTableStorage(e);if(s)for(let n of t.primaryKey){let r=t.columns.findIndex(a=>a.name.toUpperCase()===n.toUpperCase());if(r<0)continue;let i=t.columns[r],o=new KK(i.dataType);await s.flush();for(let a=0;a<s.pageIds.length;a++){let l=s.pageIds[a],u=await s.bufferPool.fetchPage(l,!0);for(let f=0;f<u.size;f++){let h=u.columns[r].get(f);h!=null&&o.insert(h,{pageId:l,rowIndex:f})}}this.catalog.registerIndex(e,n,o),s.registerIndex(r,o)}}}async enableWasm(){try{let{getGlobalLoader:e}=await Promise.resolve().then(()=>(Xl(),yk));await(await e()).loadModule("core");let{registerAllKernels:s}=await Promise.resolve().then(()=>(PM(),_k));s(),this.wasmEnabled=!0}catch{this.wasmEnabled=!1}}async enableParallel(){let{Config:e}=await Promise.resolve().then(()=>(Fe(),GL));if(e.parallelWorkers<=1)return!1;try{let{getGlobalLoader:t}=await Promise.resolve().then(()=>(Xl(),yk)),s=await t({shared:!0}),n=await s.loadModule("core"),r=s.initRegions(e.regionSize),{registerAllKernels:i}=await Promise.resolve().then(()=>(PM(),_k));i(),this.wasmEnabled=!0;let o=s.getModule("core"),{WorkerPool:a}=await Promise.resolve().then(()=>(aj(),JP)),l=new a({maxWorkers:e.parallelWorkers,wasmModule:o,wasmMemory:s.memory,regionAllocator:r});await l.init();let{globalDispatch:u}=await Promise.resolve().then(()=>(ld(),KL)),{ParallelDispatch:f}=await Promise.resolve().then(()=>(lj(),XP)),h=new f(l,r,u),{FragmentPool:p}=await Promise.resolve().then(()=>(wj(),oD)),d=new p(e.parallelWorkers,e.aggMorselRows);return this.executor.setParallelContext(l,h,d),this.workerPool=l,this.fragmentPool=d,this.parallelEnabled=!0,!0}catch{return this.parallelEnabled=!1,!1}}async enableDistributed(e={}){let{NodeDescriptor:t,NodeRole:s}=await Promise.resolve().then(()=>(oE(),lD)),{ClusterManager:n}=await Promise.resolve().then(()=>(xj(),hD)),{PartitionMap:r}=await Promise.resolve().then(()=>(Sj(),dD)),{DistributionAwareJoin:i}=await Promise.resolve().then(()=>(aE(),gD)),{PartialAggregatePass:o}=await Promise.resolve().then(()=>(Aj(),_D)),{DistributedSortPass:a}=await Promise.resolve().then(()=>(vj(),bD)),{QueryCoordinator:l}=await Promise.resolve().then(()=>(Bj(),ED)),u=new t({nodeId:e.nodeId||`node-${Date.now()}`,host:e.host||"127.0.0.1",port:e.port||9400,role:e.role||s.HYBRID,capacity:e.capacity}),f=new n(u),h=new r,p=this.precomputedStats||new Map;this.optimizer.insertPassAfter("PhysicalDesign",new i(h,p)),this.optimizer.registerPass(new o),this.optimizer.registerPass(new a),this._distributedPasses=[{method:"insertPassAfter",args:["PhysicalDesign",new i(h,p)]},{method:"registerPass",args:[new o]},{method:"registerPass",args:[new a]}];let d=e.transport||null;if(!d){let{HttpTransport:g}=await Promise.resolve().then(()=>(Mj(),BD));d=new g({port:u.port})}let m=new l(this,f,h,d);return this.distributed={clusterManager:f,partitionMap:h,transport:d,coordinator:m,localNode:u},m}async shutdown(){this.workerPool&&(await this.workerPool.shutdown(),this.workerPool=null),this.fragmentPool&&(await this.fragmentPool.close(),this.fragmentPool=null),this.distributed?.transport&&await this.distributed.transport.stop(),this.tempManager.cleanup()}},Fk="query_engine",$k=new Set,NL=!1;function CH(){if(NL)return;NL=!0;let e=c(()=>{for(let t of $k)t.cleanup()},"cleanupAll");process.on("exit",e);for(let t of["SIGINT","SIGTERM"])process.on(t,()=>{e(),process.exit()})}c(CH,"registerProcessHooks");function TH(e){try{return process.kill(e,0),!0}catch{return!1}}c(TH,"isProcessAlive");function IH(e){let t;try{t=Af.readdirSync(e,{withFileTypes:!0})}catch{return}let s=new RegExp(`^${Fk}_(\\d+)_`);for(let n of t){if(!n.isDirectory()||!n.name.startsWith(Fk+"_"))continue;let r=Dk.join(e,n.name),i=n.name.match(s);if(i){let o=parseInt(i[1],10);if(TH(o))continue}try{Af.rmSync(r,{recursive:!0,force:!0})}catch{}}}c(IH,"reapStaleDirectories");var OH=class{static{c(this,"TempDirectoryManager")}baseDir;rootName;rootDir;counters;initialized;constructor(e={}){this.baseDir=e.baseDir||BH.tmpdir();let t=Math.random().toString(36).substring(2,10);this.rootName=`${Fk}_${process.pid}_${t}`,this.rootDir=Dk.join(this.baseDir,this.rootName),this.counters=new Map,this.initialized=!1,$k.add(this),CH(),IH(this.baseDir)}allocate(e,t){this.initialized||(Af.mkdirSync(this.rootDir,{recursive:!0}),this.initialized=!0);let s=this.counters.get(e)||0;this.counters.set(e,s+1);let n=`${t}_${s}`,r=Dk.join(this.rootDir,e,n);return Af.mkdirSync(r,{recursive:!0}),r}getRootDir(){return this.rootDir}cleanup(){$k.delete(this),this.initialized&&Af.existsSync(this.rootDir)&&(Af.rmSync(this.rootDir,{recursive:!0,force:!0}),this.initialized=!1)}};yP();Jk();var LH=class{static{c(this,"FilePageStore")}basePath;allocatorFactory;constructor(e,t=null){this.basePath=e,this.allocatorFactory=t}getPagePath(e){return MH.join(this.basePath,`${e}${hP}`)}async write(e,t){let s=kf.serialize(t);await RL.writeFile(this.getPagePath(e),s)}async read(e){let t=await RL.readFile(this.getPagePath(e));return this.allocatorFactory?kf.deserialize(t,this.allocatorFactory(t.length)):kf.deserialize(t)}clear(){BL.existsSync(this.basePath)&&BL.rmSync(this.basePath,{recursive:!0,force:!0})}};_P();Jk();var DH=class{static{c(this,"FsStorage")}basePath;constructor(e){this.basePath=e}filePath(e){return PH.join(this.basePath,`${e}${pP}`)}async append(e,t){await yw.appendFile(this.filePath(e),t)}async read(e){let t=this.filePath(e);return gw.existsSync(t)?yw.readFile(t):null}exists(e){return gw.existsSync(this.filePath(e))}async remove(e){let t=this.filePath(e);gw.existsSync(t)&&await yw.unlink(t)}async removeAll(){gw.existsSync(this.basePath)&&await yw.rm(this.basePath,{recursive:!0,force:!0})}};tc();var FH=class{static{c(this,"NodeStorageBackend")}options;constructor(e={}){this.options=e}createTempSpace(){return new OH(this.options.tempDir?{baseDir:this.options.tempDir}:{})}createPageStore(e){return new LH(e,H4)}createSpillManager(e){return new Yk(new DH(e))}};Xl();var VH=UH(zH(import.meta.url)),GH=JD(VH,"../../wasm"),qH=".wasm";function jH(e){return $H(JD(GH,`${e}${qH}`))}c(jH,"nodeByteSource");var WH=class{static{c(this,"Catalog")}tables;tableStorage;indexes;partitionInfo;constructor(){this.tables=new Map,this.tableStorage=new Map,this.indexes=new Map,this.partitionInfo=new Map}registerTable(e,t,s={}){let n=e.toUpperCase();this.tables.set(n,{name:n,columns:t,primaryKey:s.primaryKey||[],foreignKeys:s.foreignKeys||[]})}registerTableStorage(e,t){this.tableStorage.set(e.toUpperCase(),t)}getTable(e){return this.tables.get(e.toUpperCase())||null}getTableStorage(e){return this.tableStorage.get(e.toUpperCase())||null}getColumn(e,t){let s=this.getTable(e);if(!s)return null;let n=t.toUpperCase();return s.columns.find(r=>r.name.toUpperCase()===n)||null}getColumnIndex(e,t){let s=this.getTable(e);if(!s)return-1;let n=t.toUpperCase();return s.columns.findIndex(r=>r.name.toUpperCase()===n)}dropTable(e){let t=e.toUpperCase();this.tables.delete(t),this.tableStorage.delete(t);for(let[s,n]of this.indexes)n.tableName===t&&this.indexes.delete(s)}hasTable(e){return this.tables.has(e.toUpperCase())}listTables(){return Array.from(this.tables.keys())}registerIndex(e,t,s){let n=`${e.toUpperCase()}.${t.toUpperCase()}`;this.indexes.set(n,{tableName:e.toUpperCase(),columnName:t.toUpperCase(),btree:s})}getIndexForColumn(e,t){let s=`${e.toUpperCase()}.${t.toUpperCase()}`,n=this.indexes.get(s);return n?n.btree:null}getIndexesForTable(e){let t=e.toUpperCase(),s=[];for(let n of this.indexes.values())n.tableName===t&&s.push(n);return s}registerPartitionInfo(e,t,s,n){this.partitionInfo.set(e.toUpperCase(),{strategy:t,partitionCount:s,partitionKey:n})}getPartitionInfo(e){return this.partitionInfo.get(e.toUpperCase())||null}};we();EH(e=>new FH(e));HP(jH);function YD(e={}){let t=e.catalog||new WH;return new RH(t,e)}c(YD,"createEngine");var hE=["tensor","zeros","ones","empty","full","randn","arange","eye","linspace","randperm","zerosLike","onesLike","emptyLike","fullLike","randnLike"],pE=["where","cat","stack"],dE=["sum","min","max"],Xw=["Linear","ReLU","GELU","SiLU","Sigmoid","Tanh","LeakyReLU","ELU","Softmax","LogSoftmax","Flatten","Dropout","LayerNorm","BatchNorm1d","BatchNorm2d","Conv1d","Conv2d","MaxPool2d","AvgPool2d","AdaptiveAvgPool2d","Embedding","GRU","GRUCell","LSTM","LSTMCell","CrossEntropyLoss","MSELoss","NLLLoss","BCELoss"],QD=null;function ec(){return QD??(QD=YD())}c(ec,"engine");var tF=0;function eF(e){let t=ec(),s=pd.fromColumns(e),n=`__upload${tF++}`;return t.catalog.registerTable(n,s.getSchema()),t.catalog.registerTableStorage(n,s),n}c(eF,"registerColumnsAsTable");function sF(e){let t=ec(),s=e.toUpperCase();t.catalog.tables?.delete(s),t.catalog.tableStorage?.delete(s)}c(sF,"dropTable");function nF(e){return ec().sql(`SELECT * FROM ${eF(e)}`)}c(nF,"createDataFrameFromColumns");var sc=new Map;function rF(e,t){sc.set(e,eF(t))}c(rF,"setUploadedCsv");function iF(e){let t=pd.builder();return{appendRows(s){s&&s.length&&t.appendRows(s)},finish(){let s=t.finish(),n=ec(),r=`__upload${tF++}`;n.catalog.registerTable(r,s.getSchema()),n.catalog.registerTableStorage(r,s);let i=sc.get(e);i&&sF(i),sc.set(e,r)}}}c(iF,"beginUploadedCsv");function oF(e){let t=sc.get(e);t&&sF(t),sc.delete(e)}c(oF,"removeUploadedCsv");var KH={sum:VD,min:qD,max:jD};function HH(e){return typeof e=="string"||e instanceof On}c(HH,"isColumnArg");dr.prototype.toString=function(){return`DataFrame(${this.columns().join(", ")})`};dr.prototype.head=function(e=5){return this.limit(e)};async function JH(e,t){let s=t.length>0?e.select(...t):e,n=s.columns(),r=await s.collect(),i=n.length,o=r.length,a=new Float32Array(o*i),l=0;for(let u=0;u<o;u++)for(let f=0;f<i;f++){let h=r[u][n[f]];if(typeof h!="number")throw new Error(`Column '${n[f]}' contains non-numeric value '${h}' at row ${u}. Use encode() for categorical columns.`);a[l++]=h}return ks(a,{shape:[o,i]})}c(JH,"dfToTensor");async function YH(e,t,s){let n=t??e.columns()[0],r=await e.collect(),i=new Map,o=s?[...s]:[];for(let l=0;l<o.length;l++)i.set(String(o[l]),l);let a=new Float32Array(r.length);for(let l=0;l<r.length;l++){let u=String(r[l][n]),f=i.get(u);if(f===void 0){if(s)throw new Error(`Unknown class '${r[l][n]}' not present in fitted classes`);f=o.length,i.set(u,f),o.push(r[l][n])}a[l]=f}return[ks(a,{shape:[a.length]}),o]}c(YH,"dfEncode");dr.prototype.toTensor=function(...e){return JH(this,e)};dr.prototype.to_tensor=dr.prototype.toTensor;dr.prototype.to_array=function(){return this.toArray()};dr.prototype.encode=function(e,...t){let n=an(t).classes??t[0]??null;return YH(this,e,n)};function aF(e,t){if(!e||typeof e.stateDict!="function")throw new Error("save() requires a model");if(typeof t!="string")throw new Error("save() requires a file path string");let s=t+".tmp";_e.writeBinary(s,uf({modelState:e.stateDict()})),_e.rename(s,t)}c(aF,"saveModelCheckpoint");function Zw(e,t){for(let s of hE)t(s,(...n)=>XD(mf[s],n));for(let s of pE)t(s,(...n)=>XD(mf[s]??Ye[s],n));for(let s of dE)t(s,n=>{if(!HH(n))throw new Error(`${s}() expects a DataFrame column; call tensor.${s}() for tensors`);return KH[s](n)});for(let s of Xw)t(s,(...n)=>QH(mf[s],n));t("Sequential",(...s)=>new Xo(...s)),t("range",(...s)=>{let n=0,r,i=1;s.length===1?r=s[0]:s.length===2?(n=s[0],r=s[1]):(n=s[0],r=s[1],i=s[2]);let o=[];if(i>0)for(let a=n;a<r;a+=i)o.push(a);else if(i<0)for(let a=n;a>r;a+=i)o.push(a);else throw new Error("range() step cannot be zero");return o}),t("print",async(...s)=>{let n=s.length>0&&s[s.length-1]?.__named?s.pop():null;s.some(a=>a instanceof Et&&a.device===Sr)&&await Qh();let r=n?.sep??" ",i=s.length>1,o=s.map(a=>i?iM(a):ql(a)).join(r);e.output(o)}),t("trace",s=>{let n=s?._isCompiled?s._compiledView:s instanceof Ca?s:null;if(!n?.events)throw new Error("trace() expects a compiled program");let r=Dp(n.events);return e.output(r),r}),t("graph",s=>{let n=s?._isCompiled?s._compiledView?.graph:s instanceof Ca?s.graph:s,r=g1(n);return e.output(r),r}),t("compile",(...s)=>e.compile(...s)),t("cpu","cpu"),t("gpu","gpu"),t("wasm","wasm"),t("webgpu","webgpu");for(let s of["f16","f32","f64","i32","i64","bool"])t(s,s);t("Tokenizer",(...s)=>Mn(mo,s)),t("load_tokenizer",s=>mo.load(s)),t("TensorDataset",(...s)=>new sa(...s)),t("DataLoader",(...s)=>{let n=an(s);return delete n.__named,new aa(s[0],Of(n))}),t("SGD",(...s)=>fE(la,s)),t("Adam",(...s)=>fE(ca,s)),t("AdamW",(...s)=>fE(ua,s)),t("StepLR",(...s)=>ZD(ha,s,["optimizer","stepSize","gamma"])),t("CosineAnnealingLR",(...s)=>ZD(pa,s,["optimizer","tMax","etaMin"])),t("ReduceLROnPlateau",(...s)=>{let n=an(s);return delete n.__named,new da(s[0],Of(n))}),t("Trainer",(...s)=>{let n=an(s);return delete n.__named,new _a(Of(n))}),t("EarlyStopping",(...s)=>Mn(wa,s)),t("ModelCheckpoint",(...s)=>Mn(ti,s)),t("ProgressCallback",(...s)=>Mn(Zr,s)),t("LearningRateMonitor",(...s)=>Mn(ba,s)),t("Timer",(...s)=>Mn(xa,s)),t("GradientAccumulationScheduler",(...s)=>Mn(Sa,s)),t("ConsoleLogger",(...s)=>Mn(Mi,s)),t("CSVLogger",(...s)=>Mn(Aa,s)),t("Accuracy",(...s)=>Mn(ka,s)),t("Precision",(...s)=>Mn(Ea,s)),t("Recall",(...s)=>Mn(Na,s)),t("F1Score",(...s)=>Mn(Ra,s)),t("ConfusionMatrix",(...s)=>Mn(Ba,s)),t("MetricCollection",(...s)=>Mn(va,s)),t("load_model",(s,n)=>{if(!s||typeof s.loadStateDict!="function")throw new Error("load_model() requires a model as the first argument");if(typeof n!="string")throw new Error("load_model() requires a file path string");return hf(ff(n),s),s}),t("read_text",s=>{if(typeof s!="string")throw new Error("read_text() requires a file path string");let n=_e.readFile(s);return typeof n=="string"?n:new TextDecoder().decode(n)}),t("load_json",s=>{if(typeof s!="string")throw new Error("load_json() requires a file path string");let n=_e.readFile(s),r=typeof n=="string"?n:new TextDecoder().decode(n),i=c(o=>{if(Array.isArray(o))return o.map(i);if(o&&typeof o=="object"){let a=new Map;for(let l of Object.keys(o))a.set(l,i(o[l]));return a}return o},"toTera");return i(JSON.parse(r))}),t("optim_config",(...s)=>{let n=an(s);delete n.__named;let r=s[0]??n.optimizer;if(!r)throw new Error("optim_config() requires an optimizer");let i={optimizer:r},o=n.lr_scheduler??n.lrScheduler;return o&&(i.lrScheduler=o),i}),t("DataFrame",(...s)=>{let n=an(s);delete n.__named;let r=Object.keys(n);if(r.length===0)throw new Error("DataFrame() requires named column arrays, e.g. DataFrame(name=[...], age=[...])");let i=n[r[0]].length,o=[];for(let a=0;a<i;a++){let l={};for(let u of r)l[u]=n[u][a];o.push(l)}return ec().createDataFrame(o)}),t("col",s=>Fi(s)),t("lit",s=>Yw(s)),t("expr",s=>Qw(s)),t("avg",s=>GD(s)),t("count",s=>WD(s)),t("countStar",()=>KD()),t("load_csv",(...s)=>{let n=an(s);delete n.__named;let r=s[0];if(typeof r!="string")throw new Error("load_csv() requires a file path string");if(sc.has(r))return ec().sql(`SELECT * FROM ${sc.get(r)}`);let i=n.separator??n.sep??",",{rows:o}=aM(r,i);return ec().createDataFrame(o)}),t("encode",(...s)=>{let n=s[0];if(n instanceof dr)return n.encode(...s.slice(1));if(Array.isArray(n)){let r=new Map,i=[],o=new Float32Array(n.length);for(let a=0;a<n.length;a++){let l=String(n[a]),u=r.get(l);u===void 0&&(u=i.length,r.set(l,u),i.push(n[a])),o[a]=u}return[ks(o,{shape:[o.length]}),i]}throw new Error("encode() expects a DataFrame or array")}),t("decode",(...s)=>{let n=s[0],r=s[1];if(!Array.isArray(r))throw new Error("decode() expects (indices, classes) \u2014 classes must be an array");if(n instanceof Et)return n.contiguous().data.reduce((i,o)=>{let a=Math.round(o);return i.push(a>=0&&a<r.length?r[a]:`<unknown:${a}>`),i},[]);if(typeof n=="number"){let i=Math.round(n);return i>=0&&i<r.length?r[i]:`<unknown:${i}>`}throw new Error("decode() expects a tensor or number as first argument")}),t("normalize",(...s)=>{let n=s[0];if(!(n instanceof Et))throw new Error("normalize() expects a tensor");let r=an(s);delete r.__named;let i=r.axis??0,o=fn(n,i,!0),a=$t(n,o),l=fn(tt(a,a),i,!0),u=cr(ot(l,ks(1e-8)));return oe(a,u)}),t("train_test_split",(...s)=>{let n=an(s);delete n.__named;let r=s[0],i=n.test_size??n.ratio??.2;if(r instanceof Et){let o=r.shape[0],a=Math.round(o*(1-i));return[r.slice(0,0,a),r.slice(0,a,o)]}throw new Error("train_test_split() expects a tensor")})}c(Zw,"installBuiltins");function an(e){let t=e[e.length-1];return t&&t.__named?e.pop():{}}c(an,"takeNamed");function XD(e,t){let s=an(t);return Object.keys(s).length===0?e(...t):(delete s.__named,"grad"in s&&(s.requiresGrad=s.grad,delete s.grad),"axis"in s&&(t.push(s.axis),delete s.axis),typeof s.device=="string"&&(s.device=lF[s.device]??s.device),e(...t,s))}c(XD,"callWithOptions");var lF={cpu:Dc,gpu:fl,wasm:Fc,webgpu:Sr};function cF(e){return typeof e=="string"?lF[e]??e:e}c(cF,"resolveDeviceName");function QH(e,t){let s=an(t);return delete s.__named,e===Yo||e===Qo?new e(s.axis??t[0]??-1):e===Jo||e===Ho?new e(...t,s):new e(...t,...Object.values(s))}c(QH,"constructWithNamed");function XH(e){return e.replace(/_([a-z])/g,(t,s)=>s.toUpperCase())}c(XH,"snakeToCamel");function Of(e){let t={};for(let s of Object.keys(e))t[XH(s)]=e[s];return t}c(Of,"snakeNamedToCamel");function Mn(e,t){let s=an(t);delete s.__named;let n=Of(s);return t.length>0?new e(...t,n):Object.keys(n).length>0?new e(n):new e}c(Mn,"constructWithSnakeCase");function fE(e,t){let s=an(t);delete s.__named;let n=t[0];if(!n)throw new Error(`${e.name}() requires params as first argument`);return new e(n,Of(s))}c(fE,"constructOptimizer");function ZD(e,t,s){let n=an(t);delete n.__named;let r=Of(n),i=[];for(let o=0;o<s.length;o++){let a=s[o];if(o<t.length)i.push(t[o]);else if(r[a]!==void 0)i.push(r[a]);else break}return new e(...i)}c(ZD,"constructScheduler");var ZH={tensor:[{name:"data"},{name:"opts",isOptional:!0}],zeros:[{name:"shape"},{name:"opts",isOptional:!0}],ones:[{name:"shape"},{name:"opts",isOptional:!0}],empty:[{name:"shape"},{name:"opts",isOptional:!0}],full:[{name:"shape"},{name:"value"},{name:"opts",isOptional:!0}],randn:[{name:"shape"},{name:"opts",isOptional:!0}],arange:[{name:"start"},{name:"end",isOptional:!0},{name:"step",isOptional:!0},{name:"opts",isOptional:!0}],eye:[{name:"n"},{name:"m",isOptional:!0},{name:"opts",isOptional:!0}],linspace:[{name:"start"},{name:"end"},{name:"steps"},{name:"opts",isOptional:!0}],zerosLike:[{name:"tensor"}],onesLike:[{name:"tensor"}],emptyLike:[{name:"tensor"}],fullLike:[{name:"tensor"},{name:"value"}],randnLike:[{name:"tensor"}]},t8={Linear:[{name:"inFeatures"},{name:"outFeatures"},{name:"bias",defaultValue:"true",isOptional:!0}],Conv1d:[{name:"inChannels"},{name:"outChannels"},{name:"kernelSize"},{name:"stride",defaultValue:"1",isOptional:!0},{name:"padding",defaultValue:"0",isOptional:!0}],Conv2d:[{name:"inChannels"},{name:"outChannels"},{name:"kernelSize"},{name:"stride",defaultValue:"1",isOptional:!0},{name:"padding",defaultValue:"0",isOptional:!0}],LayerNorm:[{name:"normalizedShape"},{name:"eps",defaultValue:"1e-5",isOptional:!0}],BatchNorm1d:[{name:"numFeatures"},{name:"eps",defaultValue:"1e-5",isOptional:!0},{name:"momentum",defaultValue:"0.1",isOptional:!0}],BatchNorm2d:[{name:"numFeatures"},{name:"eps",defaultValue:"1e-5",isOptional:!0},{name:"momentum",defaultValue:"0.1",isOptional:!0}],Dropout:[{name:"p",defaultValue:"0.5",isOptional:!0}],Embedding:[{name:"numEmbeddings"},{name:"embeddingDim"},{name:"paddingIdx",isOptional:!0}],MaxPool2d:[{name:"kernelSize"},{name:"stride",isOptional:!0},{name:"padding",defaultValue:"0",isOptional:!0}],AvgPool2d:[{name:"kernelSize"},{name:"stride",isOptional:!0},{name:"padding",defaultValue:"0",isOptional:!0}],AdaptiveAvgPool2d:[{name:"outputSize"}],LeakyReLU:[{name:"negativeSlope",defaultValue:"0.01",isOptional:!0}],ELU:[{name:"alpha",defaultValue:"1.0",isOptional:!0}],Softmax:[{name:"dim",defaultValue:"-1",isOptional:!0}],LogSoftmax:[{name:"dim",defaultValue:"-1",isOptional:!0}],Flatten:[{name:"startDim",defaultValue:"1",isOptional:!0},{name:"endDim",defaultValue:"-1",isOptional:!0}]},e8={TensorDataset:[{name:"...tensors"}],DataLoader:[{name:"dataset"},{name:"batch_size",defaultValue:"1",isOptional:!0},{name:"shuffle",defaultValue:"false",isOptional:!0},{name:"drop_last",defaultValue:"false",isOptional:!0}],SGD:[{name:"params"},{name:"lr",defaultValue:"0.01",isOptional:!0},{name:"momentum",defaultValue:"0",isOptional:!0},{name:"weight_decay",defaultValue:"0",isOptional:!0}],Adam:[{name:"params"},{name:"lr",defaultValue:"0.001",isOptional:!0},{name:"betas",isOptional:!0},{name:"weight_decay",defaultValue:"0",isOptional:!0}],AdamW:[{name:"params"},{name:"lr",defaultValue:"0.001",isOptional:!0},{name:"betas",isOptional:!0},{name:"weight_decay",defaultValue:"0.01",isOptional:!0}],StepLR:[{name:"optimizer"},{name:"step_size"},{name:"gamma",defaultValue:"0.1",isOptional:!0}],CosineAnnealingLR:[{name:"optimizer"},{name:"t_max"},{name:"eta_min",defaultValue:"0",isOptional:!0}],ReduceLROnPlateau:[{name:"optimizer"},{name:"mode",defaultValue:'"min"',isOptional:!0},{name:"patience",defaultValue:"10",isOptional:!0},{name:"factor",defaultValue:"0.1",isOptional:!0}],Trainer:[{name:"max_epochs",defaultValue:"10",isOptional:!0},{name:"accelerator",defaultValue:'"auto"',isOptional:!0},{name:"callbacks",isOptional:!0},{name:"logger",defaultValue:"true",isOptional:!0},{name:"compile",defaultValue:"false",isOptional:!0},{name:"compile_mode",defaultValue:'"separate"',isOptional:!0}],EarlyStopping:[{name:"monitor"},{name:"patience",defaultValue:"3",isOptional:!0},{name:"mode",defaultValue:'"min"',isOptional:!0}],ModelCheckpoint:[{name:"monitor",isOptional:!0},{name:"save_top_k",defaultValue:"1",isOptional:!0},{name:"mode",defaultValue:'"min"',isOptional:!0}],ProgressCallback:[],LearningRateMonitor:[],Timer:[],GradientAccumulationScheduler:[{name:"scheduling"}],ConsoleLogger:[],CSVLogger:[{name:"save_dir",isOptional:!0},{name:"name",isOptional:!0}],Accuracy:[{name:"task",defaultValue:'"binary"',isOptional:!0},{name:"num_classes",isOptional:!0},{name:"top_k",defaultValue:"1",isOptional:!0}],Precision:[{name:"task",defaultValue:'"binary"',isOptional:!0},{name:"num_classes",isOptional:!0},{name:"average",defaultValue:'"macro"',isOptional:!0}],Recall:[{name:"task",defaultValue:'"binary"',isOptional:!0},{name:"num_classes",isOptional:!0},{name:"average",defaultValue:'"macro"',isOptional:!0}],F1Score:[{name:"task",defaultValue:'"binary"',isOptional:!0},{name:"num_classes",isOptional:!0},{name:"average",defaultValue:'"macro"',isOptional:!0}],ConfusionMatrix:[{name:"num_classes"}],MetricCollection:[{name:"...metrics"}],optim_config:[{name:"optimizer"},{name:"lr_scheduler",isOptional:!0}],load_csv:[{name:"path"},{name:"separator",defaultValue:'","',isOptional:!0}],read_text:[{name:"path"}],load_json:[{name:"path"}],DataFrame:[{name:"columns"}],col:[{name:"name"}],lit:[{name:"value"}],expr:[{name:"sql"}],avg:[{name:"column"}],count:[{name:"column"}],countStar:[],encode:[{name:"data"}],decode:[{name:"indices"},{name:"classes"}],normalize:[{name:"tensor"},{name:"axis",defaultValue:"0",isOptional:!0}],train_test_split:[{name:"data"},{name:"test_size",defaultValue:"0.2",isOptional:!0}]},s8={reshape:[{name:"tensor"},{name:"shape"}],transpose:[{name:"tensor"},{name:"dim0"},{name:"dim1"}],permute:[{name:"tensor"},{name:"dims"}],expand:[{name:"tensor"},{name:"shape"}],slice:[{name:"tensor"},{name:"dim"},{name:"start"},{name:"end"},{name:"step",defaultValue:"1",isOptional:!0}],unsqueeze:[{name:"tensor"},{name:"dim"}],squeeze:[{name:"tensor"},{name:"dim"}],narrow:[{name:"tensor"},{name:"dim"},{name:"start"},{name:"length"}],select:[{name:"tensor"},{name:"dim"},{name:"index"}],contiguous:[{name:"tensor"}],detach:[{name:"tensor"}],requires_grad:[{name:"tensor"},{name:"flag",defaultValue:"true",isOptional:!0}],grad:[{name:"tensor"}],backward:[{name:"tensor"},{name:"gradient",isOptional:!0}],range:[{name:"start"},{name:"stop",isOptional:!0},{name:"step",isOptional:!0}],shape:[{name:"tensor"}],dtype:[{name:"tensor"}],print:[{name:"value"}],trace:[{name:"compiled"}],graph:[{name:"compiled"}],compile:[{name:"model"},{name:"input",isOptional:!0},{name:"target",defaultValue:"cpu",isOptional:!0},{name:"fusion",isOptional:!0},{name:"scheduling",isOptional:!0},{name:"autotune",isOptional:!0},{name:"quantization",isOptional:!0},{name:"layout",isOptional:!0},{name:"rematerialization",isOptional:!0},{name:"inplaceReuse",isOptional:!0},{name:"partition",isOptional:!0},{name:"debug",isOptional:!0},{name:"snippet",isOptional:!0},{name:"verify",defaultValue:"true",isOptional:!0},{name:"epilogue",isOptional:!0},{name:"fusionStrategy",defaultValue:"xla",isOptional:!0},{name:"numTrials",defaultValue:"64",isOptional:!0},{name:"timeBudgetMs",defaultValue:"30000",isOptional:!0}],Sequential:[{name:"...modules"}],sum:[{name:"input"},{name:"axis",isOptional:!0},{name:"keep",isOptional:!0}],mean:[{name:"input"},{name:"axis",isOptional:!0},{name:"keep",isOptional:!0}],max:[{name:"input"},{name:"axis",isOptional:!0},{name:"keep",isOptional:!0}],min:[{name:"input"},{name:"axis",isOptional:!0},{name:"keep",isOptional:!0}],argmax:[{name:"input"},{name:"axis",isOptional:!0},{name:"keep",isOptional:!0}],argmin:[{name:"input"},{name:"axis",isOptional:!0},{name:"keep",isOptional:!0}],prod:[{name:"input"},{name:"axis",isOptional:!0},{name:"keep",isOptional:!0}]};function uF(e){for(let[t,s]of Object.entries(ZH))e.register(t,s);for(let[t,s]of Object.entries(t8))e.register(t,s);for(let[t,s]of Object.entries(s8))e.register(t,s);for(let[t,s]of Object.entries(e8))e.register(t,s)}c(uF,"installSignatures");function fF(e,t){return e<0?e+t:e}c(fF,"normalizeIndex");function n8(e,t){if(e.length===0)throw new Error("pop from empty list");let s=fF(t===void 0?e.length-1:t,e.length);if(s<0||s>=e.length)throw new Error(`pop index ${t} is out of bounds for list of length ${e.length}`);return e.splice(s,1)[0]}c(n8,"popList");function r8(e,t,s){let n=fF(t,e.length);return n<0&&(n=0),n>e.length&&(n=e.length),e.splice(n,0,s),null}c(r8,"insertList");function i8(e,t){let s=e.indexOf(t);return s!==-1&&e.splice(s,1),null}c(i8,"removeList");function o8(e,t){let s=0;for(let n of e)n===t&&(s+=1);return s}c(o8,"countList");var a8={append:c((e,t)=>(e.push(t),null),"append"),extend:c((e,t)=>{for(let s of t)e.push(s);return null},"extend"),insert:r8,pop:n8,remove:i8,index:c((e,t)=>e.indexOf(t),"index"),count:o8,contains:c((e,t)=>e.indexOf(t)!==-1,"contains"),reverse:c(e=>(e.reverse(),null),"reverse"),clear:c(e=>(e.length=0,null),"clear"),copy:c(e=>e.slice(),"copy")};function l8(e,t){if(t===void 0){let s=e.trim();return s===""?[]:s.split(/\s+/)}return e.split(t)}c(l8,"splitString");var c8={upper:c(e=>e.toUpperCase(),"upper"),lower:c(e=>e.toLowerCase(),"lower"),strip:c(e=>e.trim(),"strip"),lstrip:c(e=>e.replace(/^\s+/,""),"lstrip"),rstrip:c(e=>e.replace(/\s+$/,""),"rstrip"),split:l8,join:c((e,t)=>t.join(e),"join"),replace:c((e,t,s)=>e.split(t).join(s),"replace"),starts_with:c((e,t)=>e.startsWith(t),"starts_with"),ends_with:c((e,t)=>e.endsWith(t),"ends_with"),find:c((e,t)=>e.indexOf(t),"find"),contains:c((e,t)=>e.includes(t),"contains")},u8={keys:c(e=>[...e.keys()],"keys"),values:c(e=>[...e.values()],"values"),items:c(e=>[...e.entries()].map(([t,s])=>[t,s]),"items"),get:c((e,t,s)=>e.has(t)?e.get(t):s===void 0?null:s,"get"),has:c((e,t)=>e.has(t),"has"),remove:c((e,t)=>(e.delete(t),null),"remove")};function hF(e,t){let s;if(Array.isArray(e))s=a8;else if(typeof e=="string")s=c8;else if(e instanceof Map)s=u8;else return;let n=s[t];if(n)return(...r)=>n(e,...r)}c(hF,"lookupCollectionMethod");ja();var tb=class{static{c(this,"SignatureRegistry")}constructor(){this._signatures=new Map}register(t,s){this._signatures.set(t,{params:s})}lookup(t){if(this._signatures.has(t))return this._signatures.get(t);let s=Ue.findOp(t);return s?.schema?this._fromSchema(s.schema):null}_fromSchema(t){let s=[];for(let n of t.args)n.isOut||s.push({name:n.name||n.kind,kind:n.kind,defaultValue:n.defaultValue,isOptional:n.defaultValue!=null});return{params:s}}};var Ua=class{static{c(this,"Environment")}constructor(t=null){this.parent=t,this.values=new Map}define(t,s){return this.values.set(t,s),s}set(t,s){if(this.values.has(t))return this.values.set(t,s),s;if(this.parent)return this.parent.set(t,s);throw new Error(`Cannot assign to undefined variable '${t}'`)}get(t){if(this.values.has(t))return this.values.get(t);if(this.parent)return this.parent.get(t);throw new Error(`Unknown name '${t}'`)}},Mf=class{static{c(this,"TeraRuntime")}constructor({output:t=console.log}={}){this.output=t,TB(Fc),this.global=new Ua,this.signatureRegistry=new tb,this._installBuiltins(),uF(this.signatureRegistry)}registerDataFrame(t,s){let n=nF(s);return this.global.define(t,n),n}registerUploadedCsv(t,s){rF(t,s)}beginUploadedCsv(t){return iF(t)}removeUploadedCsv(t){oF(t)}async execute(t){try{return await this.evaluateProgram(X_(t),this.global)}catch(s){throw s.line!==void 0?s:new eb(s.message,this.currentNode?.line??1,this.currentNode?.column??1,s)}}getCompletionNames(){return[...this.global.values.keys()]}getVariable(t){try{return this.global.get(t)}catch{return}}registerGlobal(t,s){if(typeof t!="string"||!/^[A-Za-z_]\w*$/.test(t))throw new Error("Global name must be a valid identifier");return this.global.define(t,s)}async evaluateProgram(t,s){let n;for(let r of t.body){let i=await this.evaluateStatement(r,s);if(i&&(i.__return||i.__break||i.__continue))return i;n=i}return n}async evaluateStatement(t,s){return this.withNode(t,async()=>{if(t.type==="Assign")return s.define(t.name,await this.evaluateExpression(t.value,s));if(t.type==="CompoundAssign"){let n=s.get(t.name),r=await this.evaluateExpression(t.value,s);return s.set(t.name,this.applyBinary(t.op,n,r))}if(t.type==="If")return await this.evaluateIf(t,s);if(t.type==="For")return await this.evaluateFor(t,s);if(t.type==="While")return await this.evaluateWhile(t,s);if(t.type==="Break")return{__break:!0};if(t.type==="Continue")return{__continue:!0};if(t.type==="ExpressionStatement")return await this.evaluateExpression(t.expression,s);if(t.type==="Return")return{__return:!0,value:await this.evaluateExpression(t.value,s)};if(t.type==="FunctionDeclaration")return this.defineFunction(t,s);if(t.type==="ModelDeclaration")return this.defineModel(t,s);if(t.type==="ForwardDeclaration")throw new Error("forward can only appear inside model");if(t.type==="TrainDeclaration")throw new Error("train can only appear inside model");if(t.type==="ValidateDeclaration")throw new Error("validate can only appear inside model");if(t.type==="OptimizerDeclaration")throw new Error("optimizer can only appear inside model");if(t.type==="DestructureAssign")return await this.evaluateDestructure(t,s);if(t.type==="IndexAssign")return await this.evaluateIndexAssign(t,s);throw new Error(`Unsupported statement ${t.type}`)})}async evaluateExpression(t,s){return this.withNode(t,async()=>{if(t.type==="Literal")return t.value;if(t.type==="Identifier")return s.get(t.name);if(t.type==="Array"){let n=[];for(let r of t.elements)n.push(await this.evaluateExpression(r,s));return n}if(t.type==="Dict"){let n=new Map;for(let r of t.entries)n.set(await this.evaluateExpression(r.key,s),await this.evaluateExpression(r.value,s));return n}if(t.type==="ListComprehension")return await this.evaluateComprehension(t,s);if(t.type==="Unary"){let n=await this.evaluateExpression(t.value,s);return t.op==="-"?this.applyUnaryMinus(n):t.op==="not"?this.applyUnaryNot(n):n}if(t.type==="Binary"){if(t.op==="and"||t.op==="or"){let n=await this.evaluateExpression(t.left,s);if(!$i(n))return t.op==="and"?n&&await this.evaluateExpression(t.right,s):n||await this.evaluateExpression(t.right,s);let r=await this.evaluateExpression(t.right,s);return this.applyBinary(t.op,n,r)}return this.applyBinary(t.op,await this.evaluateExpression(t.left,s),await this.evaluateExpression(t.right,s))}if(t.type==="Member"){let n=await this.evaluateExpression(t.object,s),r=hF(n,t.property);if(r)return r;let i=n[t.property];return typeof i!="function"?i:t.property==="to"&&($i(n)||n instanceof nt)?h8(n,i):(t.property==="eval"||t.property==="train")&&n instanceof nt?p8(n,i,t.property):$i(n)?n.device===Sr&&f8.has(t.property)?d8(n,i):dF(n,i):i.bind(n)}if(t.type==="Index")return await this.evaluateIndex(t,s);if(t.type==="Call")return await this.evaluateCall(t,s);throw new Error(`Unsupported expression ${t.type}`)})}async evaluateCall(t,s){let n=await this.evaluateExpression(t.callee,s),r=[],i={};for(let a of t.args){let l=await this.evaluateExpression(a.value,s);a.name?i[a.name]=l:r.push(l)}Object.keys(i).length>0&&r.push({__named:!0,...i});let o;if(n instanceof nt)o=n.forward(...r);else if(n&&typeof n.forward=="function"&&typeof n!="function")o=n.forward(...r);else{if(typeof n!="function")throw new Error("Value is not callable");o=n(...r)}return o&&typeof o.then=="function"&&(o=await o),o}applyUnaryMinus(t){return $i(t)?kn(t):-t}applyUnaryNot(t){if($i(t)){let s=je(t.shape,{dtype:t.dtype,device:t.device});return $t(s,t)}return!t}applyBinary(t,s,n){if(!($i(s)||$i(n))){if(t==="+"&&Array.isArray(s)&&Array.isArray(n))return s.concat(n);if(t==="+")return s+n;if(t==="-")return s-n;if(t==="*")return s*n;if(t==="/")return s/n;if(t==="**")return s**n;if(t==="==")return s===n;if(t==="!=")return s!==n;if(t==="<")return s<n;if(t==="<=")return s<=n;if(t===">")return s>n;if(t===">=")return s>=n;if(t==="and")return s&&n;if(t==="or")return s||n}[s,n]=m8(s,n);let i={"+":ot,"-":$t,"*":tt,"/":oe,"**":kr,"@":qe,"==":Si,"!=":xl,"<":Sl,"<=":no,">":Wr,">=":ro,and:tt,or:c((o,a)=>$t(ot(o,a),tt(o,a)),"or")}[t];if(!i)throw new Error(`Unsupported operator '${t}'`);return i(s,n)}isTruthy(t){if($i(t)){if(t.numel!==1)throw new Error("Condition tensor must be a scalar (single element)");return!!t.item()}return!!t}async evaluateIf(t,s){if(this.isTruthy(await this.evaluateExpression(t.condition,s)))return await this.evaluateProgram({type:"Program",body:t.body},s);for(let n of t.elifs)if(this.isTruthy(await this.evaluateExpression(n.condition,s)))return await this.evaluateProgram({type:"Program",body:n.body},s);if(t.elseBody)return await this.evaluateProgram({type:"Program",body:t.elseBody},s)}async evaluateFor(t,s){let n=await this.evaluateExpression(t.iterable,s),r=Array.isArray(n)?n:n instanceof Map?[...n.keys()]:null;if(!r)throw new Error("for...in expects an array or map");let i;for(let o of r){s.define(t.variable,o);let a=await this.evaluateProgram({type:"Program",body:t.body},s);if(a&&a.__return)return a;if(a&&a.__break)break;a&&a.__continue||(i=a)}return i}async evaluateWhile(t,s){let n;for(;this.isTruthy(await this.evaluateExpression(t.condition,s));){let r=await this.evaluateProgram({type:"Program",body:t.body},s);if(r&&r.__return)return r;if(r&&r.__break)break;r&&r.__continue||(n=r)}return n}defineFunction(t,s){let n=this,r=c(async(...i)=>{let o=new Ua(s);t.params.forEach((l,u)=>o.define(l,i[u]));let a=await n.evaluateProgram({type:"Program",body:t.body},o);return a&&a.__return?a.value:a},"func");return r._langName=t.name,s.define(t.name,r),this.signatureRegistry.register(t.name,t.params.map(i=>({name:i}))),r}async evaluateDestructure(t,s){let n=await this.evaluateExpression(t.value,s);if(!Array.isArray(n))throw new Error("Destructuring requires an array value");if(n.length<t.names.length)throw new Error(`Not enough values to unpack (expected ${t.names.length}, got ${n.length})`);for(let r=0;r<t.names.length;r++)s.define(t.names[r],n[r]);return n}defineModel(t,s){let n=this,r=t.body.find(m=>m.type==="ForwardDeclaration");if(!r)throw new Error(`Model ${t.name} needs a forward block`);let i=t.body.find(m=>m.type==="TrainDeclaration"),o=t.body.find(m=>m.type==="ValidateDeclaration"),a=t.body.find(m=>m.type==="OptimizerDeclaration"),l=!!(i||o||a),u=new Set(["ForwardDeclaration","TrainDeclaration","ValidateDeclaration","OptimizerDeclaration"]),f=t.body.filter(m=>!u.has(m.type)),h=t.name,p=l?ya:nt,d=c(async(...m)=>{let g=an(m),y=new Ua(s);t.params.forEach((b,x)=>y.define(b,g[b]??m[x]));class _ extends p{static{c(this,"LangModel")}constructor(){super(),this._langName=h}async forward(...x){let A=new Ua(y);for(let E of f)E.type==="Assign"&&A.define(E.name,this[E.name]);r.params.forEach((E,R)=>A.define(E,x[R]));let S=await n.evaluateProgram({type:"Program",body:r.body},A);return S&&S.__return?S.value:S}toString(){return`${this._langName}${super.toString().slice(this.constructor.name.length)}`}}_.prototype.save=function(b){aF(this,b)},i&&(_.prototype.trainingStep=async function(b,x){let A=mE(this,y,f,h);pF(A,this),i.params[0]&&A.define(i.params[0],b),i.params[1]&&A.define(i.params[1],x);let S=await n.evaluateProgram({type:"Program",body:i.body},A);return S&&S.__return?S.value:S}),o&&(_.prototype.validationStep=async function(b,x){let A=mE(this,y,f,h);pF(A,this),o.params[0]&&A.define(o.params[0],b),o.params[1]&&A.define(o.params[1],x);let S=await n.evaluateProgram({type:"Program",body:o.body},A);return S&&S.__return?S.value:S}),a&&(_.prototype.configureOptimizers=async function(){let b=mE(this,y,f,h),x=await n.evaluateProgram({type:"Program",body:a.body},b);return x&&x.__return?x.value:x});let w=new _;for(let b of f){let x=await n.evaluateStatement(b,y);b.type==="Assign"&&(w[b.name]=x)}return w},"factory");return d._langName=h,s.define(h,d),this.signatureRegistry.register(h,t.params.map(m=>({name:m}))),d}_installBuiltins(){Zw(this,c((s,n)=>this.global.define(s,n),"define")),this.global.define("log",()=>{throw new Error("log() can only be called inside a train or validate block")})}async evaluateIndex(t,s){let n=await this.evaluateExpression(t.object,s);if(Array.isArray(n))return this.indexArray(n,t.items,s);if(n instanceof Map)return this.indexMap(n,t.items,s);if(typeof n=="string")return this.indexString(n,t.items,s);if(!(n instanceof Et))throw new Error("Indexing currently expects a Tensor, array, map, or string");let r=0;for(let i of t.items){if(r>=n.ndim)throw new Error(`Too many indices for tensor with ${n.ndim} dimensions`);if(i.type==="Slice"){let o=i.start?await this.evaluateExpression(i.start,s):0,a=i.end?await this.evaluateExpression(i.end,s):n.shape[r],l=i.step?await this.evaluateExpression(i.step,s):1;if(![o,a,l].every(Number.isInteger))throw new Error("Slice bounds must be integers");if(l<=0)throw new Error("Slice step must be a positive integer");n=n.slice(r,o,a,l),r++}else{let o=await this.evaluateExpression(i,s);if(!Number.isInteger(o))throw new Error("Tensor index must be an integer");if(o<0&&(o+=n.shape[r]),o<0||o>=n.shape[r])throw new Error(`Index ${o} is out of bounds for dimension ${r} with size ${n.shape[r]}`);if(n=n.select(r,o),n.ndim===0)return n.item()}}return n}async indexArray(t,s,n){let r=t;for(let i of s){if(!Array.isArray(r))throw new Error("Too many indices for array");if(i.type==="Slice"){let o=r.length,a=i.start?await this.evaluateExpression(i.start,n):0,l=i.end?await this.evaluateExpression(i.end,n):o,u=i.step?await this.evaluateExpression(i.step,n):1;if(![a,l,u].every(Number.isInteger))throw new Error("Slice bounds must be integers");if(u<=0)throw new Error("Slice step must be a positive integer");a<0&&(a+=o),l<0&&(l+=o);let f=[];for(let h=Math.max(0,a);h<Math.min(o,l);h+=u)f.push(r[h]);r=f}else{let o=await this.evaluateExpression(i,n);if(!Number.isInteger(o))throw new Error("Array index must be an integer");if(o<0&&(o+=r.length),o<0||o>=r.length)throw new Error(`Index ${o} is out of bounds for array of length ${r.length}`);r=r[o]}}return r}async indexMap(t,s,n){let r=t;for(let i of s){if(!(r instanceof Map))throw new Error("Too many indices for map");if(i.type==="Slice")throw new Error("Cannot slice a map");let o=await this.evaluateExpression(i,n);r=r.has(o)?r.get(o):null}return r}async indexString(t,s,n){let r=t;for(let i of s){if(typeof r!="string")throw new Error("Too many indices for string");if(i.type==="Slice"){let o=r.length,a=i.start?await this.evaluateExpression(i.start,n):0,l=i.end?await this.evaluateExpression(i.end,n):o,u=i.step?await this.evaluateExpression(i.step,n):1;if(![a,l,u].every(Number.isInteger))throw new Error("Slice bounds must be integers");if(u<=0)throw new Error("Slice step must be a positive integer");a<0&&(a+=o),l<0&&(l+=o);let f="";for(let h=Math.max(0,a);h<Math.min(o,l);h+=u)f+=r[h];r=f}else{let o=await this.evaluateExpression(i,n);if(!Number.isInteger(o))throw new Error("String index must be an integer");if(o<0&&(o+=r.length),o<0||o>=r.length)throw new Error(`Index ${o} is out of bounds for string of length ${r.length}`);r=r[o]}}return r}async evaluateComprehension(t,s){let n=await this.evaluateExpression(t.iterable,s),r=Array.isArray(n)?n:n instanceof Map?[...n.keys()]:null;if(!r)throw new Error("Comprehension expects an array or map");let i=new Ua(s),o=[];for(let a of r)i.define(t.variable,a),!(t.condition&&!this.isTruthy(await this.evaluateExpression(t.condition,i)))&&o.push(await this.evaluateExpression(t.expr,i));return o}async evaluateIndexAssign(t,s){let n=await this.evaluateExpression(t.object,s),r=[];for(let a of t.items){if(a.type==="Slice")throw new Error("Slice assignment is not supported");r.push(await this.evaluateExpression(a,s))}for(let a=0;a<r.length-1;a++)n=this.readContainer(n,r[a]);let i=r[r.length-1],o=await this.evaluateExpression(t.value,s);return t.op&&(o=this.applyBinary(t.op,this.readContainer(n,i),o)),this.writeContainer(n,i,o),o}readContainer(t,s){if(t instanceof Map)return t.has(s)?t.get(s):null;if(Array.isArray(t)){let n=Number.isInteger(s)&&s<0?s+t.length:s;if(!Number.isInteger(n))throw new Error("Array index must be an integer");return t[n]}throw new Error("Cannot index into this value")}writeContainer(t,s,n){if(t instanceof Map){t.set(s,n);return}if(Array.isArray(t)){if(!Number.isInteger(s))throw new Error("Array index must be an integer");let r=s<0?s+t.length:s;if(r<0||r>t.length)throw new Error(`Index ${s} is out of bounds for assignment to array of length ${t.length}`);t[r]=n;return}throw new Error("Cannot assign into this value")}async withNode(t,s){let n=this.currentNode;this.currentNode=t;try{return await s()}catch(r){throw r.line!==void 0?r:new eb(r.message,t.line??1,t.column??1,r)}finally{this.currentNode=n}}compile(t,...s){if(!(t instanceof nt))throw new Error("compile() currently expects a model. Example: compile(model, input=x)");let n=an(s),r=n.input??s[0],i=r!=null?Array.isArray(r)?r:[r]:null;if(i&&i.some(d=>!(d instanceof Et)))throw new Error("compile() input must be a tensor, for example compile(model, input=x)");let o=n.target??"wasm",a=o==="gpu"?el():o==="wasm"?sl():o==="webgpu"?nl():nr(),l=n.debug??!1,u=n.snippet??!1,f=[],h={target:a,verify:n.verify??!0,fusion:{enabled:n.fusion??!1,epilogue:n.epilogue??!1,strategy:n.fusionStrategy??"xla"},scheduling:{enabled:n.scheduling??!1,autotune:n.autotune??!1,numTrials:n.numTrials??64,timeBudgetMs:n.timeBudgetMs??3e4},quantization:{enabled:n.quantization??!1},optimization:{layout:n.layout??!1,rematerialization:n.rematerialization??!1},memory:{inplaceReuse:n.inplaceReuse??!1},partition:{enabled:n.partition??!1,targets:[]}};l&&(h.trace={level:at.DEBUG,sink:c(d=>f.push(d),"sink"),irSnapshot:{afterGraphPasses:!0,afterLowering:!0,afterScheduling:!0}});let p=V_(t,i,h);if(p._isCompiled=!0,p._langName="compiled",Object.defineProperty(p,"_compiledView",{get:c(()=>new Ca({model:t,inputs:i,graph:p.graph(i),result:p.result(),events:f,target:o}),"get")}),p._ready){let d=this;return p._ready.then(()=>(l&&d.output(Dp(f)),u&&d.output(p.snippet()),p))}return i&&(l&&this.output(Dp(f)),u&&this.output(p.snippet())),p}};function $i(e){return e instanceof Et||e instanceof de}c($i,"isTensorValue");var f8=new Set(["item","toArray","tolist"]);function h8(e,t){return async(...s)=>{let n=s[s.length-1],r;if(n&&n.__named?(s.pop(),r=n.device):s.length&&(r=s[s.length-1]),r=cF(r),r===Sr){if(typeof navigator>"u"||!navigator.gpu)throw new Error("webgpu device requires a browser environment (navigator.gpu); it is not available in the node CLI");await Wc()}else r===fl&&await $x();return t.call(e,r)}}c(h8,"makeDeviceMove");function p8(e,t,s){return(...n)=>{let r=t.apply(e,n);return Rr.setEnabled(s==="train"?n.length===0?!0:!!n[0]:!1),r}}c(p8,"makeTrainModeToggle");function d8(e,t){return async(...s)=>(await Qh(),dF(e,t)(...s))}c(d8,"makeWebgpuHostRead");function dF(e,t){return(...s)=>{let n=s[s.length-1];if(n&&n.__named){s.pop();let r=n.axis??n.dim,i=n.keep??n.keepdim;r!==void 0&&s.push(r),i!==void 0&&s.push(i)}return t.apply(e,s)}}c(dF,"bindTensorMethod");function m8(e,t){let s=e instanceof Et?e:t instanceof Et?t:null;if(!s)return[e,t];let n={dtype:s.dtype,device:s.device};return $i(e)||(e=ks(e,n)),$i(t)||(t=ks(t,n)),[e,t]}c(m8,"promoteScalars");function mE(e,t,s,n){let r=new Ua(t);r.define(n,e);for(let i of s)i.type==="Assign"&&r.define(i.name,e[i.name]);return r}c(mE,"buildStepEnv");function pF(e,t){e.define("log",(s,n,...r)=>{let i=r.length>0&&r[r.length-1]&&r[r.length-1].__named?r.pop():{};delete i.__named;let o=n;o&&typeof o=="object"&&typeof o.compute=="function"&&!(o instanceof Et)&&(o=o.compute());let a={};i.on_step!==void 0&&(a.onStep=i.on_step),i.on_epoch!==void 0&&(a.onEpoch=i.on_epoch),i.prog_bar!==void 0&&(a.progBar=i.prog_bar),i.reduce_fx!==void 0&&(a.reduceFx=i.reduce_fx),t.log(s,o,a)})}c(pF,"bindLog");var eb=class extends Error{static{c(this,"LangRuntimeError")}constructor(t,s,n,r){super(`${t} at ${s}:${n}`,{cause:r}),this.name="LangRuntimeError",this.line=s,this.column=n}};var Tt=Object.freeze({kind:"any"}),$St=Object.freeze({kind:"unknown"}),zi=Object.freeze({kind:"number",num:"int"}),Va=Object.freeze({kind:"number",num:"float"}),ii=zi,Os=Object.freeze({kind:"string"}),oi=Object.freeze({kind:"bool"}),gE=Object.freeze({kind:"null"}),Ui=Object.freeze({kind:"none"}),Ms=Object.freeze({kind:"tensor"});function js(e){return{kind:"list",element:e??Tt}}c(js,"listType");function yE(e,t){return{kind:"dict",key:e??Tt,value:t??Tt}}c(yE,"dictType");function nc(e,t,s=!1,n=null,r=null){let i=e??[];return{kind:"function",params:i,ret:t??Tt,variadic:s,required:n??i.length,names:r}}c(nc,"functionType");function gr(e=""){return{kind:"module",name:e}}c(gr,"moduleType");function Is(e){return!e||e.kind==="any"||e.kind==="unknown"}c(Is,"isAny");function Gt(e){switch(e.kind){case"list":{let t=Gt(e.element);return e.element.kind==="union"||e.element.kind==="function"?`(${t})[]`:`${t}[]`}case"dict":return`Record<${Gt(e.key)}, ${Gt(e.value)}>`;case"number":return e.num==="float"?"float":"int";case"bool":return"boolean";case"module":return e.name||"Module";case"tensor":return"Tensor";case"function":return`(${e.params.map(Gt).join(", ")}${e.variadic?", ...":""}) -> ${Gt(e.ret)}`;case"union":return e.members.map(Gt).join(" | ");default:return e.kind}}c(Gt,"typeToString");function _E(e){let t=[];for(let r of e)r.kind==="union"?t.push(...r.members):t.push(r);if(t.some(Is))return Tt;let s=new Map;for(let r of t){let i=Gt(r);s.has(i)||s.set(i,r)}let n=[...s.values()];return n.length===1?n[0]:{kind:"union",members:n}}c(_E,"unionType");function Lf(e,t){return e?t?Is(e)||Is(t)?Tt:e.kind==="number"&&t.kind==="number"?e.num==="float"||t.num==="float"?Va:zi:Gt(e)===Gt(t)?e:_E([e,t]):e:t}c(Lf,"join");function g8(e,t){let s=Math.min(e.params.length,t.params.length);for(let n=0;n<s;n++)if(!Ln(t.params[n],e.params[n]))return!1;return Ln(e.ret,t.ret)}c(g8,"functionAssignable");function Ln(e,t){if(Is(e)||Is(t))return!0;if(t.kind==="union")return t.members.some(s=>Ln(e,s));if(e.kind==="union")return e.members.every(s=>Ln(s,t));if(e.kind==="null"||e.kind==="none")return t.kind===e.kind;if(e.kind!==t.kind)return!1;switch(e.kind){case"list":return Ln(e.element,t.element);case"dict":return Ln(e.key,t.key)&&Ln(e.value,t.value);case"module":return!e.name||!t.name||e.name===t.name;case"function":return g8(e,t);default:return!0}}c(Ln,"isAssignable");var y8=new Set(["LSTM","GRU","LSTMCell","GRUCell"]);function mF(e){return y8.has(e)?js(Ms):Ms}c(mF,"moduleCallReturn");function gF(e){return e?e.endsWith("[]")?js(gF(e.slice(0,-2))):e==="Tensor"?Ms:e==="int"?zi:e==="float"?Va:e==="string"||e==="str"?Os:e==="boolean"||e==="bool"?oi:e==="none"?Ui:gr(e):Tt}c(gF,"resolveReturn");function _8(e){return Array.isArray(e)?e:e?.methods??[]}c(_8,"methodsOf");function yF(e){let t=new Map,s=c((n,r)=>{let i=t.get(n);for(let o of r)o.returns&&(i||(i=new Map,t.set(n,i)),i.set(o.name,gF(o.returns)))},"record");for(let[n,r]of Object.entries(e.pseudoTypes??{}))s(n,_8(r));for(let n of e.builtins??[])s(n.name,n.methods??[]);return t}c(yF,"buildMethodReturns");var w8=["cpu","gpu","wasm","webgpu","f16","f32","f64","i32","i64","bool"],_F=new Map([...Xw,"Sequential"].map(e=>[e,mF(e)]));function b8(){return new Map([["len",ii],["range",js(ii)],["shape",js(ii)],["dtype",Os],["read_text",Os],["trace",Os],["graph",Os],["print",Ui],["backward",Ui],["load_csv",gr("DataFrame")],["DataFrame",gr("DataFrame")],["Tokenizer",gr("Tokenizer")],["load_tokenizer",gr("Tokenizer")]])}c(b8,"returnOverrides");function x8(e){return nc([],e,!0,0)}c(x8,"builtinFunction");function wF(){let e=new Map,t=b8(),s=c((n,r)=>e.set(n,x8(t.get(n)??r)),"setFn");for(let n of hE)s(n,Ms);for(let n of pE)s(n,Ms);for(let n of dE)s(n,Tt);for(let n of Xw)s(n,gr(n));for(let n of t.keys())e.has(n)||s(n,Tt);for(let n of w8)e.set(n,Os);return e}c(wF,"buildBuiltinTypes");var wE=null;function bF(){if(wE)return wE;let e=new Set;return Zw({output(){},compile(){}},s=>e.add(s)),wE=e,e}c(bF,"collectBuiltinNames");var bE=class extends Error{static{c(this,"LangTypeError")}constructor(t,s,n){super(`${t} at ${s}:${n}`),this.name="TypeError",this.line=s,this.column=n}},xF={int:zi,float:Va,string:Os,boolean:oi,Tensor:Ms,tensor:Ms,none:Ui,null:gE},QSt=Object.freeze([...Object.keys(xF),"Record"]),SF=Object.freeze(["chart"]),S8=new Map([["log","log() can only be called inside a train or validate block"]]),A8={shape:js(ii),ndim:ii,numel:ii,dtype:Os,device:Os,requiresGrad:oi,grad:Ms};function v8(e,t){switch(e){case"append":case"extend":case"insert":case"remove":case"reverse":case"clear":return Ui;case"pop":return t.element;case"index":case"count":return zi;case"contains":return oi;case"copy":return js(t.element);default:return null}}c(v8,"listMethodResult");function k8(e){switch(e){case"upper":case"lower":case"strip":case"lstrip":case"rstrip":case"replace":case"join":return Os;case"split":return js(Os);case"starts_with":case"ends_with":case"contains":return oi;case"find":return zi;default:return null}}c(k8,"stringMethodResult");function E8(e,t){switch(e){case"keys":return js(t.key);case"values":return js(t.value);case"items":return js(Tt);case"get":return t.value;case"has":return oi;case"remove":return Ui;default:return null}}c(E8,"dictMethodResult");var N8=new Set(["==","!=","<","<=",">",">="]),R8=new Set(["-","*","/","**"]),B8=new Set(["number","string","bool","list","dict","tensor","null","none"]),rc=class{static{c(this,"TypeEnv")}constructor(t=null){this.parent=t,this.values=new Map}define(t,s){return this.values.set(t,s),s}lookup(t){if(this.values.has(t))return this.values.get(t);if(this.parent)return this.parent.lookup(t)}},xE=class{static{c(this,"TypeChecker")}constructor({builtinNames:t,builtinTypes:s,methodReturns:n,moduleCalls:r}){this.diagnostics=[],this.reserved=new Set(t),this.inferred=new Map,this.modelReturns=new Map,this.modelFields=new Map,this.methodReturns=n??new Map,this.moduleCalls=r??new Map,this.root=new rc;for(let i of t)this.root.define(i,s.get(i)??Tt);this.exprHandlers={Literal:c(i=>this.inferLiteral(i),"Literal"),Identifier:c((i,o)=>this.inferIdentifier(i,o),"Identifier"),Array:c((i,o)=>this.inferArray(i,o),"Array"),Dict:c((i,o)=>this.inferDict(i,o),"Dict"),ListComprehension:c((i,o)=>this.inferComprehension(i,o),"ListComprehension"),Unary:c((i,o)=>this.inferUnary(i,o),"Unary"),Binary:c((i,o)=>this.inferBinary(i,o),"Binary"),Member:c((i,o)=>this.inferMember(i,o),"Member"),Index:c((i,o)=>this.inferIndex(i,o),"Index"),Call:c((i,o)=>this.inferCall(i,o),"Call")},this.stmtHandlers={Assign:c((i,o)=>this.checkAssign(i,o),"Assign"),CompoundAssign:c((i,o)=>this.checkCompoundAssign(i,o),"CompoundAssign"),DestructureAssign:c((i,o)=>this.checkDestructure(i,o),"DestructureAssign"),IndexAssign:c((i,o)=>this.checkIndexAssign(i,o),"IndexAssign"),If:c((i,o,a)=>this.checkIf(i,o,a),"If"),For:c((i,o,a)=>this.checkFor(i,o,a),"For"),While:c((i,o,a)=>this.checkWhile(i,o,a),"While"),Return:c((i,o,a)=>this.checkReturn(i,o,a),"Return"),ExpressionStatement:c((i,o)=>this.infer(i.expression,o),"ExpressionStatement"),FunctionDeclaration:c((i,o)=>this.checkFunction(i,o),"FunctionDeclaration"),ModelDeclaration:c((i,o)=>this.checkModel(i,o),"ModelDeclaration")}}run(t){return this.checkBlock(t.body,this.root,{returns:[],declaredReturn:Tt}),this.diagnostics}report(t,s){this.diagnostics.push(new bE(t,s?.line??1,s?.column??1))}declare(t,s,n,r){return this.reserved.has(s)&&this.report(`cannot redefine built-in '${s}'`,r),r?.line!=null&&!Is(n)&&this.inferred.set(`${s}:${r.line}`,Gt(n)),t.define(s,n)}checkBlock(t,s,n){for(let r of t)this.checkStatement(r,s,n)}checkStatement(t,s,n){let r=this.stmtHandlers[t.type];r&&r(t,s,n)}infer(t,s){let n=this.exprHandlers[t.type];return n?n(t,s):Tt}isTensor(t){return t&&t.kind==="tensor"}resolveTypeNode(t){return t?t.kind==="ArrayType"?js(this.resolveTypeNode(t.element)):t.kind==="UnionType"?_E(t.members.map(s=>this.resolveTypeNode(s))):t.kind==="FunctionType"?nc(t.params.map(s=>this.resolveTypeNode(s)),this.resolveTypeNode(t.ret),!1,t.params.length):t.kind==="GenericType"?t.name==="Record"?yE(this.resolveTypeNode(t.args[0]),this.resolveTypeNode(t.args[1])):t.name==="Tensor"||t.name==="tensor"?Ms:gr(t.name):xF[t.name]??gr(t.name):Tt}elementOf(t){return Is(t)?Tt:t.kind==="list"?t.element:t.kind==="dict"?t.key:t.kind==="string"?Os:Tt}inferLiteral(t){let s=t.value;return typeof s=="number"?t.isFloat?Va:zi:typeof s=="string"?Os:typeof s=="boolean"?oi:s===null?gE:Tt}inferIdentifier(t,s){let n=s.lookup(t.name);return n===void 0?(this.report(S8.get(t.name)??`undefined name '${t.name}'`,t),Tt):n}inferArray(t,s){let n=null;for(let r of t.elements){let i=this.infer(r,s);n=n===null?i:Lf(n,i)}return js(n??Tt)}inferDict(t,s){let n=null,r=null;for(let i of t.entries){let o=this.infer(i.key,s),a=this.infer(i.value,s);n=n===null?o:Lf(n,o),r=r===null?a:Lf(r,a)}return yE(n??Tt,r??Tt)}inferComprehension(t,s){let n=this.infer(t.iterable,s),r=new rc(s);return this.declare(r,t.variable,this.elementOf(n),t),t.condition&&this.infer(t.condition,r),js(this.infer(t.expr,r))}inferUnary(t,s){let n=this.infer(t.value,s);return t.op==="not"?this.isTensor(n)?Ms:oi:this.isTensor(n)?Ms:Is(n)?Tt:n.kind==="number"?n:(this.report(`operator '${t.op}' cannot be applied to ${Gt(n)}`,t),Tt)}inferBinary(t,s){let n=this.infer(t.left,s),r=this.infer(t.right,s);return this.binaryResult(t.op,n,r,t)}binaryResult(t,s,n,r){let i=this.isTensor(s)||this.isTensor(n);if(t==="and"||t==="or")return i?Ms:Lf(s,n);if(N8.has(t))return i?Ms:oi;if(Is(s)||Is(n))return Tt;if(t==="@")return i||this.report(`operator '@' requires tensors, got ${Gt(s)} and ${Gt(n)}`,r),Ms;if(i)return Ms;if(s.kind==="number"&&n.kind==="number")return t==="/"?Va:s.num==="float"||n.num==="float"?Va:zi;if(t==="+"){if(s.kind==="string"&&n.kind==="string")return Os;if(s.kind==="list"&&n.kind==="list")return js(Lf(s.element,n.element))}return(t==="+"||R8.has(t))&&this.report(`operator '${t}' cannot be applied to ${Gt(s)} and ${Gt(n)}`,r),Tt}inferMember(t,s){return this.memberType(this.infer(t.object,s),t.property)}memberType(t,s){if(Is(t))return Tt;if(t.kind==="tensor")return A8[s]??Tt;if(t.kind==="module"){let n=this.modelFields.get(t.name);if(n&&n.has(s))return n.get(s);let r=this.methodReturns.get(t.name)?.get(s);if(r)return r;if(this.modelFields.has(t.name)){let i=this.methodReturns.get("Model")?.get(s);if(i)return i}}return(t.kind==="list"||t.kind==="string")&&s==="length"?ii:Tt}typeNameOf(t){return t?t.kind==="tensor"?"Tensor":t.kind==="module"?t.name||null:t.kind==="function"&&t.ret?.kind==="module"&&t.ret.name||null:null}methodResult(t,s){let n=this.typeNameOf(t),r=n&&this.methodReturns.get(n)?.get(s);if(!r&&t.kind==="module"&&this.modelFields.has(n)&&(r=this.methodReturns.get("Model")?.get(s)),r)return r;if(t.kind==="list"){let i=v8(s,t);if(i)return i}if(t.kind==="string"){let i=k8(s);if(i)return i}if(t.kind==="dict"){let i=E8(s,t);if(i)return i}return this.isTensor(t)?this.tensorMethodResult(s):null}tensorMethodResult(t){return t==="item"?ii:t==="toArray"||t==="tolist"?js(ii):Ms}requireNumberIndex(t,s){Is(t)||this.isTensor(t)||t.kind==="number"||this.report(`index must be a number, got ${Gt(t)}`,s)}checkIndexItems(t,s,n){for(let r of t){if(r.type==="Slice"){for(let o of[r.start,r.end,r.step])o&&this.requireNumberIndex(this.infer(o,n),o);continue}let i=this.infer(r,n);Is(s)||(s.kind==="list"||s.kind==="string"?this.requireNumberIndex(i,r):s.kind==="dict"&&!Ln(i,s.key)&&this.report(`index of type ${Gt(i)} is not assignable to key type ${Gt(s.key)}`,r))}}inferIndex(t,s){let n=this.infer(t.object,s);return this.checkIndexItems(t.items,n,s),Is(n)?Tt:n.kind==="list"?n.element:n.kind==="dict"?n.value:n.kind==="string"?Os:Tt}inferCall(t,s){let n=[],r=[];for(let i of t.args){let o=this.infer(i.value,s);i.name?r.push({name:i.name,type:o,node:i.value}):n.push({type:o,node:i.value})}if(t.callee.type==="Member"){let i=this.infer(t.callee.object,s),o=this.methodResult(i,t.callee.property);return o!==null?o:this.finishCall(this.memberType(i,t.callee.property),n,r,t)}return this.finishCall(this.infer(t.callee,s),n,r,t)}finishCall(t,s,n,r){if(t.kind==="module"){if(this.modelReturns.has(t.name))return this.modelReturns.get(t.name);if(this.moduleCalls.has(t.name))return this.moduleCalls.get(t.name)}return t.kind!=="function"?(B8.has(t.kind)&&this.report(`${Gt(t)} is not callable`,r.callee),Tt):(n.length===0?this.checkPositionalCall(t,s,r):t.names&&this.checkNamedCall(t,s,n,r),t.ret)}checkPositionalCall(t,s,n){let r=t.required,i=t.variadic?1/0:t.params.length;if(s.length<r||s.length>i){let o=r===i?`${r}`:i===1/0?`at least ${r}`:`${r}-${i}`;this.report(`expected ${o} argument(s), got ${s.length}`,n);return}for(let o=0;o<s.length&&o<t.params.length;o++)Ln(s[o].type,t.params[o])||this.report(`argument ${o+1} expects ${Gt(t.params[o])}, got ${Gt(s[o].type)}`,s[o].node)}checkNamedCall(t,s,n,r){let{names:i,params:o,required:a}=t;if(s.length>o.length){this.report(`expected at most ${o.length} argument(s), got ${s.length} positional`,r);return}let l=new Array(o.length).fill(null);for(let u=0;u<s.length;u++)l[u]=s[u];for(let u of n){let f=i.indexOf(u.name);if(f===-1){this.report(`unknown argument '${u.name}'`,u.node);continue}if(l[f]){this.report(`argument '${u.name}' specified more than once`,u.node);continue}l[f]=u}for(let u=0;u<o.length;u++){if(!l[u]){u<a&&this.report(`missing required argument '${i[u]}'`,r);continue}Ln(l[u].type,o[u])||this.report(`argument '${i[u]}' expects ${Gt(o[u])}, got ${Gt(l[u].type)}`,l[u].node)}}checkAssign(t,s){let n=this.infer(t.value,s);if(t.annotation){let r=this.resolveTypeNode(t.annotation);Ln(n,r)||this.report(`cannot assign ${Gt(n)} to '${t.name}: ${Gt(r)}'`,t),this.declare(s,t.name,r,t)}else this.declare(s,t.name,n,t)}checkCompoundAssign(t,s){let n=s.lookup(t.name);n===void 0&&this.report(`undefined name '${t.name}'`,t);let r=this.infer(t.value,s);this.declare(s,t.name,this.binaryResult(t.op,n??Tt,r,t),t)}checkDestructure(t,s){let n=this.infer(t.value,s);!Is(n)&&n.kind!=="list"&&!this.isTensor(n)&&this.report(`cannot destructure ${Gt(n)}`,t);let r=n.kind==="list"?n.element:Tt;for(let i of t.names)this.declare(s,i,r,t)}checkIndexAssign(t,s){let n=this.infer(t.object,s);this.checkIndexItems(t.items,n,s);let r=this.infer(t.value,s);Is(n)||(n.kind==="list"&&!Ln(r,n.element)?this.report(`cannot assign ${Gt(r)} to element of ${Gt(n)}`,t):n.kind==="dict"&&!Ln(r,n.value)&&this.report(`cannot assign ${Gt(r)} to value of ${Gt(n)}`,t))}checkIf(t,s,n){this.infer(t.condition,s),this.checkBlock(t.body,s,n);for(let r of t.elifs)this.infer(r.condition,s),this.checkBlock(r.body,s,n);t.elseBody&&this.checkBlock(t.elseBody,s,n)}checkFor(t,s,n){let r=this.infer(t.iterable,s);this.declare(s,t.variable,this.elementOf(r),t),this.checkBlock(t.body,s,n)}checkWhile(t,s,n){this.infer(t.condition,s),this.checkBlock(t.body,s,n)}checkReturn(t,s,n){n.returns.push({type:this.infer(t.value,s),node:t})}resolveParams(t,s,n,r,i){return t.map((o,a)=>{let l=s?.[a];if(this.reserved.has(o)&&this.report(`parameter '${o}' of ${n} cannot redefine built-in '${o}'`,l??r),!l)return this.report(`parameter '${o}' of ${n} needs a type annotation`,r),Tt;let u=this.resolveTypeNode(l);return Is(u)||this.inferred.set(`${o}:${l.line??r.line}`,Gt(u)),u})}checkReturnTypes(t,s){for(let n of t)Ln(n.type,s)||this.report(`return type ${Gt(n.type)} is not assignable to declared ${Gt(s)}`,n.node)}allowsImplicitReturn(t){return Is(t)||t.kind==="none"?!0:t.kind==="union"?t.members.some(s=>s.kind==="none"):!1}blockReturns(t){for(let s of t)if(s.type==="Return"||s.type==="If"&&this.ifReturns(s))return!0;return!1}ifReturns(t){if(!t.elseBody||!this.blockReturns(t.body))return!1;for(let s of t.elifs)if(!this.blockReturns(s.body))return!1;return this.blockReturns(t.elseBody)}checkAllPathsReturn(t,s,n,r){this.allowsImplicitReturn(s)||this.blockReturns(t)||this.report(`${r} may not return a value on all paths; expected ${Gt(s)}`,n)}checkFunction(t,s){let n=this.resolveParams(t.params,t.paramTypes,`'${t.name}'`,t,s);t.returnType||this.report(`function '${t.name}' needs a return type annotation`,t);let r=t.returnType?this.resolveTypeNode(t.returnType):Tt;this.declare(s,t.name,nc(n,r,!1,t.params.length,t.params),t);let i=new rc(s);t.params.forEach((a,l)=>i.define(a,n[l]));let o={returns:[],declaredReturn:r};this.checkBlock(t.body,i,o),t.returnType&&(this.checkReturnTypes(o.returns,r),this.checkAllPathsReturn(t.body,r,t,`function '${t.name}'`))}checkModel(t,s){let n=this.resolveParams(t.params,t.paramTypes,`model '${t.name}'`,t,s);this.declare(s,t.name,nc(n,gr(t.name),!1,t.params.length,t.params),t);let r=t.body.find(l=>l.type==="ForwardDeclaration");r&&this.modelReturns.set(t.name,r.returnType?this.resolveTypeNode(r.returnType):Tt);let i=new rc(s);t.params.forEach((l,u)=>i.define(l,n[u]));let o=new Set(["ForwardDeclaration","TrainDeclaration","ValidateDeclaration","OptimizerDeclaration"]);for(let l of t.body)o.has(l.type)||this.checkStatement(l,i,{returns:[],declaredReturn:Tt});let a=new Map;for(let l of t.body)l.type==="Assign"&&a.set(l.name,i.lookup(l.name)??Tt);this.modelFields.set(t.name,a);for(let l of t.body)l.type==="ForwardDeclaration"?this.checkModelBlock(l,i,t.name,!1):l.type==="TrainDeclaration"||l.type==="ValidateDeclaration"?this.checkModelBlock(l,i,t.name,!0):l.type==="OptimizerDeclaration"&&this.checkBlock(l.body,this.stepEnv(i,t.name),{returns:[],declaredReturn:Tt})}stepEnv(t,s){let n=new rc(t);return n.define(s,gr(s)),n}checkModelBlock(t,s,n,r){let i=this.resolveParams(t.params,t.paramTypes,`'${n}'`,t,s),o=this.stepEnv(s,n);t.params.forEach((u,f)=>o.define(u,i[f])),r&&o.define("log",nc([Os,Tt],Ui,!0,1));let a=t.returnType?this.resolveTypeNode(t.returnType):Tt,l={returns:[],declaredReturn:a};this.checkBlock(t.body,o,l),t.returnType&&(this.checkReturnTypes(l.returns,a),this.checkAllPathsReturn(t.body,a,t,`'${n}'`))}};function AF(e,t){return new xE(t).run(e)}c(AF,"typecheck");function C8(e){return{builtinNames:new Set([...bF(),...SF]),builtinTypes:wF(),methodReturns:e,moduleCalls:_F}}c(C8,"buildEnv");function SE(e,{methodReturns:t}={}){return{diagnostics:AF(X_(e),C8(t))}}c(SE,"checkSource");function Pf(e,t,s=null){let n=s?`${s}:${e.line??1}:${e.column??1}`:`${e.line??1}:${e.column??1}`,i=t.split(/\r?\n/)[(e.line??1)-1],o=`${e.name||"Error"}: ${e.message}`;if(i===void 0)return o;let a=`${" ".repeat(Math.max(0,(e.column??1)-1))}^`;return`${o}
227
- --> ${n}
228
- |
229
- ${String(e.line??1).padStart(3)}| ${i}
230
- | ${a}`}c(Pf,"formatDiagnostic");import L8 from"terminal-kit";var kF=`Tera \u2014 Tensor Algebra
231
-
232
- Try:
233
- x = tensor([[1, 2], [3, 4]])
234
- w = randn([2, 3])
235
- y = (x @ w).relu()
236
-
237
- Type help for examples. Type exit to quit.`,T8={main:`Getting started:
238
- x = tensor([[1, 2], [3, 4]])
239
- x.shape
240
- y = x * 2 + 1
241
- x[:, 0]
242
-
243
- Neural network:
244
- model = Sequential(
245
- Linear(2, 4),
246
- ReLU(),
247
- Linear(4, 1),
248
- )
249
- output = model(x)
250
-
251
- Compiler:
252
- compiled = compile(model, input=x)
253
- y = compiled(x)
254
- trace(compiled)
255
-
256
- More:
257
- help tensor
258
- help model
259
- help fn
260
- help control
261
- help compile
262
- examples`,tensor:`Tensor examples:
263
- x = tensor([[1, 2], [3, 4]])
264
- z = zeros([2, 3])
265
- r = randn([4, 8])
266
-
267
- x.shape
268
- x + 1
269
- x * 2
270
- x @ randn([2, 3])
271
- x.mean(axis=0)
272
- x.relu()
273
- x[:, 1]
274
- x[0:2]
275
- x.reshape([4])
276
- x.transpose(0, 1)
277
-
278
- Autograd:
279
- x = tensor([2], grad=true)
280
- y = (x * x).sum()
281
- y.backward()
282
- x.grad`,model:`Neural network examples:
283
- model = Sequential(
284
- Linear(2, 4),
285
- ReLU(),
286
- Linear(4, 1),
287
- )
288
-
289
- x = randn([8, 2])
290
- output = model(x)
291
-
292
- Custom model:
293
- model MLP(input: int, hidden: int, output: int):
294
- fc1 = Linear(input, hidden)
295
- fc2 = Linear(hidden, output)
296
-
297
- forward (x: Tensor) -> Tensor:
298
- x = fc1(x).relu()
299
- return fc2(x)
300
-
301
-
302
- net = MLP(2, 4, 1)
303
- x = randn([8, 2])
304
- output = net(x)`,fn:`Function examples:
305
- fn double(x: int) -> int:
306
- return x * 2
307
- double(5)
308
-
309
- fn to_prob(x: Tensor) -> Tensor:
310
- return x / x.sum()
311
- to_prob(tensor([3, 1, 2]))
312
-
313
- Closure (captures outer variables):
314
- scale = 10
315
- fn scaled_add(a: int, b: int) -> int:
316
- return (a + b) * scale
317
-
318
- Multi-statement:
319
- fn describe(x: Tensor) -> Tensor:
320
- print(x.shape)
321
- print(x.dtype)
322
- return x.mean()`,control:`Conditionals:
323
- if x > 0:
324
- print("positive")
325
- else if x == 0:
326
- print("zero")
327
- else:
328
- print("negative")
329
-
330
- For loop:
331
- for i in range(5):
332
- print(i)
333
- for x in [1, 2, 3]:
334
- total += x
335
-
336
- While loop:
337
- x = 10
338
- while x > 0:
339
- x -= 1
340
-
341
- Break and continue:
342
- for i in range(100):
343
- if i > 10: break
344
- if i == 5: continue
345
- print(i)`,compile:`Compile and execute a neural network:
346
- m = Sequential(Linear(2, 4), ReLU(), Linear(4, 1))
347
- x = randn([8, 2])
348
-
349
- compiled = compile(m, input=x)
350
- y = compiled(x) # execute compiled model
351
-
352
- Lazy compile (no input needed):
353
- compiled = compile(m)
354
- y = compiled(x) # first call compiles + executes
355
-
356
- Options (off by default):
357
- compile(m, input=x, target=gpu)
358
- compile(m, input=x, fusion=true)
359
- compile(m, input=x, scheduling=true, autotune=true)
360
- compile(m, input=x, quantization=true)
361
- compile(m, input=x, layout=true, rematerialization=true)
362
- compile(m, input=x, inplaceReuse=true)
363
- compile(m, input=x, partition=true)
364
-
365
- Debug & inspect:
366
- compile(m, input=x, debug=true)
367
- compile(m, input=x, snippet=true)
368
-
369
- Targets: cpu (default), gpu, wasm, webgpu`},I8={tensor:`x = tensor([[1, 2], [3, 4]])
370
- w = randn([2, 3])
371
- y = (x @ w).relu()
372
- y`,linear:`model = Sequential(
373
- Linear(2, 4),
374
- ReLU(),
375
- Linear(4, 1),
376
- )
377
- x = randn([8, 2])
378
- model(x)`,custom:`model MLP(input: int, hidden: int, output: int):
379
- fc1 = Linear(input, hidden)
380
- fc2 = Linear(hidden, output)
381
-
382
- forward (x: Tensor) -> Tensor:
383
- x = fc1(x).relu()
384
- return fc2(x)
385
-
386
- model = MLP(2, 4, 1)
387
- x = randn([8, 2])
388
- model(x)`,compile:`model = Sequential(Linear(2, 4), ReLU(), Linear(4, 1))
389
- x = randn([8, 2])
390
- compiled = compile(model, input=x)
391
- y = compiled(x)
392
- trace(compiled)`};function vF(e="main"){return T8[e]||`Unknown help topic '${e}'. Try: help tensor, help model, or help compile.`}c(vF,"getHelp");function O8(){return`Available examples:
393
- tensor Basic tensor operations
394
- linear Sequential neural network
395
- custom Custom model with forward
396
- compile Compile and inspect a model
397
-
398
- Show an example:
399
- example tensor`}c(O8,"listExamples");function M8(e){return I8[e]||`Unknown example '${e}'. Try: examples`}c(M8,"getExample");function EF(e){let t=e.trim();return t==="help"?vF():t.startsWith("help ")?vF(t.slice(5).trim()):t==="examples"?O8():t.startsWith("example ")?M8(t.slice(8).trim()):null}c(EF,"handleReplCommand");function NF(e){let t=0,s=null,n=0,r=-1;for(let a=e.length-1;a>=0;a--){let l=e[a];if(s){l===s&&(a===0||e[a-1]!=="\\")&&(s=null);continue}if(l==='"'||l==="'"){s=l;continue}if(l===")"||l==="]"){t++;continue}if(l==="("||l==="["){if(t>0){t--;continue}if(l==="("){r=a;break}continue}l===","&&t===0&&n++}if(r<0)return null;let i=e.substring(0,r),o=i.match(/([A-Za-z_][A-Za-z0-9_]*)$/);if(!o){let a=i.match(/[A-Za-z_][A-Za-z0-9_]*\.([A-Za-z_][A-Za-z0-9_]*)$/);return a?{functionName:a[1],argIndex:n}:null}return{functionName:o[1],argIndex:n}}c(NF,"parseCallContext");var AE=new Set(["model","forward","return","true","false","null","and","or","not","fn","if","else","for","in","while","break","continue"]),P8=new Set(["tensor","zeros","ones","empty","full","randn","arange","eye","linspace","zerosLike","onesLike","emptyLike","fullLike","randnLike","add","sub","mul","div","neg","pow","remainder","maximum","minimum","exp","log","sqrt","rsqrt","abs","sin","cos","tanh","sigmoid","relu","gelu","silu","sign","floor","ceil","eq","ne","lt","le","gt","ge","where","matmul","dot","cat","stack","clone","softmax","log_softmax","sum","mean","max","min","argmax","argmin","prod","reshape","transpose","permute","expand","slice","unsqueeze","squeeze","narrow","select","contiguous","detach","requires_grad","grad","backward","range","len","shape","dtype","print","trace","graph","compile"]),D8=["help","help tensor","help model","help fn","help control","help compile","examples","example tensor","example linear","example custom","example compile","exit","quit"];async function CF({term:e=L8.terminal}={}){let t=c(a=>e.noFormat(String(a)+`
400
- `),"write"),s=new Mf({output:t}),n=[],r="",i=0,o=!1;for(e(kF+`
401
- `);;){e(i>0?"^K... ^:":"^Kmlfw> ^:");let a=e.inputField({history:n,autoComplete:c(h=>F8(h,s,r),"autoComplete"),autoCompleteHint:!0,autoCompleteMenu:{style:e.brightBlack,selectedStyle:e.bgCyan.black},cancelable:!0,autoClosePairs:{"(":")","[":"]",'"':'"',"'":"'"},tokenRegExp:/#[^\n]*|"(?:\\.|[^"])*"|'(?:\\.|[^'])*'|\b\d+(?:\.\d+)?\b|\b[A-Za-z_]\w*\b|\*\*=?|==|!=|<=|>=|[+\-*\/@]=|[^\s]/g,tokenHook:z8}),l=c(h=>{h==="CTRL_C"&&(o=!0,a.stop())},"onKey"),u=c(()=>{o=!0,a.stop()},"onSigint");e.on("key",l),process.once("SIGINT",u);let f;try{f=await a.promise}finally{e.removeListener("key",l),process.removeListener("SIGINT",u)}if(e(`
402
- `),o){RF(e),e.brightBlack(`^C
403
- `),process.exitCode=130;break}if(f===void 0||!r&&(f.trim()==="exit"||f.trim()==="quit")){e.brightBlack(`Bye.
404
- `);break}if(f.trim()&&n.push(f),!r){let h=EF(f);if(h!==null){t(h);continue}}if(r+=f+`
405
- `,f.trim().endsWith(":")?i=1:i>0&&f.trim()===""?i=0:i+=V8(f),!(i>0)){try{let h=await s.execute(r),p=ql(h);p&&t(p)}catch(h){e.red.noFormat(`${Pf(h,r)}
406
- `)}r="",i=0}}return RF(e),{runtime:s,history:n}}c(CF,"startRepl");function RF(e){typeof e.grabInput=="function"&&e.grabInput(!1),typeof e.hideCursor=="function"&&e.hideCursor(!1),typeof e.styleReset=="function"&&e.styleReset()}c(RF,"shutdownTerminal");function F8(e,t,s=""){let n=NF(e);if(n){let h=t.signatureRegistry.lookup(n.functionName);if(h){let p=U8(e,h);if(p!==e)return p}}let r=e.match(/([A-Za-z_][A-Za-z0-9_]*)\.([A-Za-z_][A-Za-z0-9_]*)?$/);if(r){let h=t.getVariable(r[1]);if(h!=null&&typeof h=="object"){let p=r[2]||"",d=e.slice(0,r.index)+r[1]+".",m=Object.keys(h).filter(y=>!y.startsWith("_")&&y!=="constructor").filter(y=>y.startsWith(p)).sort();if(m.length===0)return e;if(m.length===1)return d+m[0];let g=BF(m);return g.length>p.length?d+g:(m.prefix=d,m)}return e}let i=e.match(/[A-Za-z_][A-Za-z0-9_]*$/);if(!i)return e;let o=e.slice(0,i.index),a=i[0],l=$8(s),u=[...new Set([...D8,...AE,...t.getCompletionNames(),...l])].filter(h=>h.startsWith(a)).sort();if(u.length===0)return e;if(u.length===1)return o+u[0];let f=BF(u);return f.length>a.length?o+f:(u.prefix=o,u)}c(F8,"completeInput");function $8(e){if(!e)return[];let t=[];for(let s of e.matchAll(/\b([A-Za-z_][A-Za-z0-9_]*)\s*=/g)){let n=s[1];AE.has(n)||t.push(n)}for(let s of e.matchAll(/\b(?:fn|model)\s+([A-Za-z_][A-Za-z0-9_]*)/g))t.push(s[1]);for(let s of e.matchAll(/\bforward\s+([A-Za-z_][A-Za-z0-9_]*)/g))t.push(s[1]);return t}c($8,"extractBufferNames");function BF(e){let t=e[0]||"";for(let s of e.slice(1))for(;!s.startsWith(t);)t=t.slice(0,-1);return t}c(BF,"commonPrefix");function z8(e,t,s,n){return e.startsWith("#")?n.brightBlack:e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'")?n.green:/^\d+(?:\.\d+)?$/.test(e)?n.magenta:AE.has(e)?n.brightBlue:P8.has(e)?n.yellow:/^(?:\*\*=?|==|!=|<=|>=|[+\-*\/@][=]?|[=<>])$/.test(e)?n.brightBlack:/^[A-Z]/.test(e)?n.cyan:n}c(z8,"tokenHook");function U8(e,t){let s=e.match(/.*[,(]\s*(.*)$/s);if(!s)return e;let n=s[1];if(!n||!/^[A-Za-z_][A-Za-z0-9_]*$/.test(n))return e;for(let r of t.params){let i=r.defaultValue!=null?`${r.name}=${r.defaultValue}`:r.name;if(i.startsWith(n)&&i!==n)return e+i.slice(n.length)}return e}c(U8,"formatParamHint");function V8(e){let t=0,s=null;for(let n=0;n<e.length;n++){let r=e[n];s?r==="\\"?n++:r===s&&(s=null):r==='"'||r==="'"?s=r:r==="("||r==="["?t++:(r===")"||r==="]")&&t--}return t}c(V8,"bracketDelta");var dd;function LF(){if(dd!==void 0)return dd;try{let e=new URL("../../vscode-ext/language-data.json",import.meta.url);dd=yF(JSON.parse(MF.readFileSync(e,"utf8")))}catch{dd=null}return dd}c(LF,"methodReturns");var TF=`Usage:
407
- mlfw Start the Tera REPL
408
- mlfw check <file> Type-check a Tera file without executing
409
- mlfw <file> Execute a Tera file (.tera)`;function PF(e,t,s,n){for(let r of e)n(Pf(r,t,s));return e.length>0}c(PF,"reportDiagnostics");async function DF(e,{stdout:t=console.log,stderr:s=console.error,readFile:n=c(a=>MF.readFileSync(a,"utf8"),"readFile"),repl:r=CF,stdinIsTTY:i=process.stdin.isTTY,readStdin:o=c(()=>G8(process.stdin),"readStdin")}={}){let[a,l,...u]=e;if(!a||a==="repl")return l||u.length?OF(s,"repl does not accept arguments"):i?(await r(),0):await IF(await o(),{stdout:t,stderr:s,filename:"<stdin>",stripExit:!0});if(a==="--help"||a==="-h"||a==="help")return t(TF),0;let f=a==="run"||a==="check"?a:"run",h=f===a?l:a;if(!h||u.length||f!==a&&l)return OF(s,`Invalid arguments.
410
-
411
- ${TF}`);let p;try{if(p=n(h),f==="check"){let{diagnostics:d}=SE(p,{methodReturns:LF()});return PF(d,p,h,s)?1:(t(`${h}: OK`),0)}return await IF(p,{stdout:t,stderr:s,filename:h})}catch(d){return s(p===void 0?`${d.name||"Error"}: ${d.message}`:Pf(d,p,h)),1}}c(DF,"runCli");async function IF(e,{stdout:t=console.log,stderr:s=console.error,filename:n=null,stripExit:r=!1}={}){r&&(e=e.replace(/(?:^|\n)\s*(?:exit|quit)\s*;?\s*$/u,""));try{let{diagnostics:i}=SE(e,{methodReturns:LF()});if(PF(i,e,n,s))return 1;let o=await new Mf({output:t}).execute(e),a=ql(o);return a&&t(a),0}catch(i){return s(Pf(i,e,n)),1}}c(IF,"executeSource");function OF(e,t){return e(t),1}c(OF,"fail");async function G8(e){let t="";for await(let s of e)t+=s;return t}c(G8,"readStream");process.exitCode=await DF(process.argv.slice(2));