@slexisvn/mlfw 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.js +284 -0
- package/dist/index.d.ts +3552 -0
- package/dist/index.node.js +428 -0
- package/package.json +12 -24
- package/dist/cli.js +0 -411
- package/dist/index.js +0 -207
- package/patches/terminal-kit+3.1.2.patch +0 -140
package/dist/cli.js
DELETED
|
@@ -1,411 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
var Cd=Object.defineProperty;var KF=Object.getOwnPropertyDescriptor;var HF=Object.getOwnPropertyNames;var YF=Object.prototype.hasOwnProperty;var c=(e,t)=>Cd(e,"name",{value:t,configurable:!0});var P=(e,t)=>()=>(e&&(t=e(e=0)),t);var ne=(e,t)=>{for(var s in t)Cd(e,s,{get:t[s],enumerable:!0})},JF=(e,t,s,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of HF(t))!YF.call(e,r)&&r!==s&&Cd(e,r,{get:()=>t[r],enumerable:!(n=KF(t,r))||n.enumerable});return e};var XF=e=>JF(Cd({},"__esModule",{value:!0}),e);var MN={};ne(MN,{ArgKind:()=>Vs,OperatorSchema:()=>Od,SchemaArg:()=>Id,parseSchema:()=>Sb});function Sb(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?ZF(p):[],m=i.length>0?tU(i):[{kind:Vs.TENSOR}];return new Od(s,l,u,d,m)}function ZF(e){return BN(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=bb.get(u)||Vs.SCALAR;return new Id(f,h,o,a)})}function tU(e){let t=e.trim();if(t.startsWith("(")&&t.endsWith(")")){let n=t.substring(1,t.length-1);return BN(n,",").map(i=>{let o=i.trim();return{kind:bb.get(o)||Vs.TENSOR}})}return[{kind:bb.get(t)||Vs.TENSOR}]}function BN(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 Vs,QF,Id,Od,bb,xb=P(()=>{Vs=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([Vs.TENSOR,Vs.TENSOR_LIST]),Id=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)}},Od=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}},bb=new Map([["Tensor",Vs.TENSOR],["Scalar",Vs.SCALAR],["int",Vs.INT],["float",Vs.FLOAT],["bool",Vs.BOOL],["int[]",Vs.INT_LIST],["Tensor[]",Vs.TENSOR_LIST],["Dtype",Vs.DTYPE],["Device",Vs.DEVICE],["str",Vs.STRING],["MemoryFormat",Vs.MEMORY_FORMAT]]);c(Sb,"parseSchema");c(ZF,"_parseArgs");c(tU,"_parseReturns");c(BN,"_splitTopLevel")});function LN(e){return e instanceof sr?e:e&&typeof e=="object"&&e._impl?sr.tensor(e):typeof e=="number"?Number.isInteger(e)?sr.int(e):sr.float(e):typeof e=="boolean"?sr.bool(e):typeof e=="string"?sr.string(e):Array.isArray(e)?e.length>0&&e[0]&&e[0]._impl?sr.tensorList(e):sr.intList(e):(e==null,sr.none())}var Gs,sr,qn,Jf=P(()=>{Gs=Object.freeze({TENSOR:0,INT:1,FLOAT:2,BOOL:3,INT_LIST:4,TENSOR_LIST:5,STRING:6,NONE:7,DEVICE:8,DTYPE:9}),sr=class e{static{c(this,"IValue")}constructor(t,s){this.tag=t,this.value=s}static tensor(t){return new e(Gs.TENSOR,t)}static int(t){return new e(Gs.INT,t)}static float(t){return new e(Gs.FLOAT,t)}static bool(t){return new e(Gs.BOOL,t)}static intList(t){return new e(Gs.INT_LIST,t)}static tensorList(t){return new e(Gs.TENSOR_LIST,t)}static string(t){return new e(Gs.STRING,t)}static none(){return new e(Gs.NONE,null)}static device(t){return new e(Gs.DEVICE,t)}static dtype(t){return new e(Gs.DTYPE,t)}isTensor(){return this.tag===Gs.TENSOR}isInt(){return this.tag===Gs.INT}isFloat(){return this.tag===Gs.FLOAT}isBool(){return this.tag===Gs.BOOL}isIntList(){return this.tag===Gs.INT_LIST}isTensorList(){return this.tag===Gs.TENSOR_LIST}isString(){return this.tag===Gs.STRING}isNone(){return this.tag===Gs.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}},qn=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=>LN(i)),r=this._boxed(t,n);return Array.isArray(r)?r.length===1?r[0].value:r.map(i=>i.value):r instanceof sr?r.value:r}_callUnboxedAsBoxed(t,s){let n=s.map(i=>i.value),r=this._unboxed(t,...n);return r==null?[]:[LN(r)]}};c(LN,"_toIValue")});function PN(e){Xi=e}var Xi,Xf,Ab=P(()=>{xb();Jf();Xi=null;c(PN,"_setDispatcher");Xf=class{static{c(this,"Library")}constructor(t,s){this._namespace=t,this._kind=s,this._registrations=[]}def(t){let s=Sb(t,this._namespace);return Xi&&Xi.registerOp(s),this._registrations.push({type:"def",schema:s}),this}impl(t,s,n){let r=n instanceof qn?n:qn.fromUnboxed(n);return Xi&&Xi.registerKernel(`${this._namespace}::${t}`,s,r),this._registrations.push({type:"impl",name:t,key:s,kernelFn:r}),this}implBoxed(t,s,n){let r=qn.fromBoxed(n);return Xi&&Xi.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 qn?s:qn.fromBoxed(s);return Xi&&Xi.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 yc(e){let t=eU[e];if(t===void 0)throw new Error(`No backend key for device: ${e}`);return t}function UN(e){return sU[e]??ft.AUTOGRAD}function DN(e){return e=e-(e>>1&1431655765),e=(e&858993459)+(e>>2&858993459),(e+(e>>4)&252645135)*16843009>>24}var ft,FN,eU,sU,nr,Qf,eY,Zf,sY,rr=P(()=>{ft=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}),FN=new Array(ft.NUM_KEYS).fill(null);for(let[e,t]of Object.entries(ft))e!=="NUM_KEYS"&&t<ft.NUM_KEYS&&(FN[t]=e);eU=Object.freeze({cpu:ft.CPU,gpu:ft.GPU,wasm:ft.WASM,webgpu:ft.CUSTOM_0,meta:ft.META,lazy:ft.LAZY}),sU=Object.freeze({[ft.CPU]:ft.AUTOGRAD_CPU,[ft.GPU]:ft.AUTOGRAD_GPU,[ft.WASM]:ft.AUTOGRAD_WASM});c(yc,"backendKeyForDevice");c(UN,"autogradKeyForBackend");nr=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 DN(this._lo)+DN(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(FN[s]||String(s));return`DispatchKeySet(${t.join(", ")})`}};c(DN,"_popcount32");Qf=new nr(0,0),eY=nr.fromKeys(ft.CPU,ft.GPU,ft.WASM,ft.META,ft.LAZY,ft.CUSTOM_0,ft.CUSTOM_1,ft.CUSTOM_2,ft.CUSTOM_3),Zf=nr.fromKeys(ft.AUTOGRAD,ft.AUTOGRAD_CPU,ft.AUTOGRAD_GPU,ft.AUTOGRAD_WASM),sY=nr.fromKeys(ft.BATCHED,ft.VMAP,ft.FUNCTIONALIZE,ft.AUTOCAST,ft.AUTOGRAD,ft.AUTOGRAD_CPU,ft.AUTOGRAD_GPU,ft.AUTOGRAD_WASM,ft.TRACING)});var Bd,$N=P(()=>{rr();Bd=class{static{c(this,"OperatorEntry")}constructor(t){this._schema=t,this._kernels=new Array(ft.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 Md,zN=P(()=>{Md=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 Ld,VN=P(()=>{rr();Ld=class{static{c(this,"FallbackTable")}constructor(){this._kernels=new Array(ft.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 GN(e,t){Ja.push(null,e);let s;try{s=t()}catch(n){throw Ja.pop(),n}return s&&typeof s.then=="function"?s.then(n=>(Ja.pop(),n),n=>{throw Ja.pop(),n}):(Ja.pop(),s)}var Eb,Ja,Tb=P(()=>{rr();Eb=class{static{c(this,"GuardStack")}constructor(){this._frames=[]}push(t,s){this._frames.push({exclude:t||Qf,include:s||Qf})}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}},Ja=new Eb;c(GN,"withIncludedKeys")});function Rb(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=Rb(e,t[s]);return e}function vb(e,t){let s=Qf;if(t){let n=t.tensorArgIndices;for(let r=0;r<n.length;r++){let i=n[r];i<e.length&&(s=Rb(s,e[i]))}}else for(let n=0;n<e.length;n++)s=Rb(s,e[n]);return s}var Nb,We,Xa=P(()=>{rr();$N();zN();VN();Tb();Ab();Nb=class{static{c(this,"Dispatcher")}constructor(){this._entries=new Map,this._handles=new Map,this._fallbacks=new Ld}registerOp(t){let s=t.key();if(this._handles.has(s))return this._handles.get(s);let n=new Bd(t);this._entries.set(s,n);let r=new Md(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}=(xb(),XF(MN)),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=Ja.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=vb(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(Rb,"_unionArg");c(vb,"computeKeySet");We=new Nb;PN(We)});function Qi(e){let t=qN[e];if(t===void 0)throw new Error(`Unknown dtype: ${e}`);return t}function as(e){return nU.has(e)}function Ob(e){return rU.has(e)}function iU(e,t){return e===t?e:e===1?t:t===1?e:e===nt?t===nt?nt:t:t===nt?e:null}function Ib(e,t){return e===t?!0:e&&typeof e.equals=="function"?e.equals(t):!1}function Qs(e){if(e instanceof M)return`tensor<${e.shape.map(s=>s===nt?"?":String(s)).join("x")}x${e.dtype}>`;if(e instanceof Qa)return`tuple<${e.elements.map(Qs).join(", ")}>`;if(e instanceof kb)return"token";if(e instanceof Cb){let t=e.inputs.map(Qs).join(", "),s=e.outputs.map(Qs).join(", ");return`(${t}) -> (${s})`}return"unknown"}var st,qN,nU,rU,nt,Ke,M,Qa,kb,Cb,Ft=P(()=>{st=Object.freeze({F16:"f16",BF16:"bf16",F32:"f32",F64:"f64",I8:"i8",I16:"i16",I32:"i32",I64:"i64",UI8:"ui8",BOOL:"bool",INDEX:"index"}),qN=Object.freeze({[st.F16]:2,[st.BF16]:2,[st.F32]:4,[st.F64]:8,[st.I8]:1,[st.I16]:2,[st.I32]:4,[st.I64]:8,[st.UI8]:1,[st.BOOL]:1,[st.INDEX]:4}),nU=new Set([st.F16,st.BF16,st.F32,st.F64]),rU=new Set([st.I8,st.I16,st.I32,st.I64,st.UI8]),nt=-1;c(Qi,"scalarBytes");c(as,"isFloatType");c(Ob,"isIntType");Ke=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]===nt?r=nt:r!==nt&&(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(iU,"broadcastDim");M=class e{static{c(this,"TensorType")}constructor(t,s,n=null){this.shape=Object.freeze([...t]),this.dtype=s,this.layout=n||Ke.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===nt)}numel(){if(this.hasDynamic)return nt;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===nt?nt:t*qN[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===nt||r===nt)&&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=iU(i,a[l]),i===null))return null}n[r]=i}return n}static broadcastCompatible(...t){return e.broadcastShape(...t)!==null}},Qa=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(!Ib(this.elements[s],t.elements[s]))return!1;return!0}},kb=class e{static{c(this,"TokenType")}equals(t){return t instanceof e}},Cb=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(!Ib(this.inputs[s],t.inputs[s]))return!1;for(let s=0;s<this.outputs.length;s++)if(!Ib(this.outputs[s],t.outputs[s]))return!1;return!0}};c(Ib,"typeEquals");c(Qs,"typeToString")});var oU,th,Za,eh,Pd=P(()=>{oU=0,th=class{static{c(this,"UseLink")}constructor(t,s){this.user=t,this.operandIndex=s,this.prev=null,this.next=null}},Za=class{static{c(this,"Value")}constructor(t,s=null,n=0){this.type=t,this.definingOp=s,this.resultIndex=n,this.id=oU++,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}},eh=class extends Za{static{c(this,"BlockArgument")}constructor(t,s,n){super(t,null,0),this.ownerBlock=s,this.argIndex=n}isBlockArgument(){return!0}}});var ls,Re,Mo=P(()=>{Pd();ls=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 eh(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 eh(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()}},Re=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 ys,J,et,Dd,Ze=P(()=>{ys=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"}),et=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||ys.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!==ys.NONE}},Dd=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 ot,Fd,Ud,zt,$d,_s=P(()=>{ot=Object.freeze({UNCHANGED:0,CHANGED:1,FAILED:2}),Fd=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")}},Ud=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)}},zt=class extends Fd{static{c(this,"FunctionPass")}run(t,s){throw new Error("Not implemented")}},$d=class extends Fd{static{c(this,"ModulePass")}run(t,s){throw new Error("Not implemented")}}});var dt,Ns,aU,zd,ws=P(()=>{_s();dt=Object.freeze({SILENT:0,INFO:1,VERBOSE:2,DEBUG:3}),Ns=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}},aU=c(()=>{},"NOOP"),zd=class{static{c(this,"TraceLog")}constructor(t={}){this.level=t.level??dt.SILENT,this.sink=typeof t.sink=="function"?t.sink:aU,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:dt.INFO})}phaseEnd(t,s){this.emit({type:"phase",action:"end",phase:t,durationMs:s,level:dt.INFO})}passRun(t,s,n,r,i){this.emit({type:"pass",passName:t,changed:s===ot.CHANGED,durationMs:n,opCountBefore:r,opCountAfter:i,level:dt.VERBOSE})}functionEvent(t,s,n){this.emit({type:"function",phase:t,funcName:s,...n,level:dt.INFO})}irDump(t,s){this.emit({type:"ir_snapshot",label:t,text:s,level:dt.DEBUG})}memoryStats(t,s){this.emit({type:"memory",funcName:t,...s,level:dt.VERBOSE})}autotuneStats(t,s){this.emit({type:"autotune",funcName:t,...s,level:dt.VERBOSE})}codegenStats(t,s){this.emit({type:"codegen",funcName:t,...s,level:dt.VERBOSE})}errorEvent(t,s,n,r){this.emit({type:"error",phase:t,funcName:s,message:n,passName:r||null,level:dt.INFO})}explain(t,s,n,r,i){this.emit({type:"explain",category:t,subject:s,decision:n,reason:r,...i||{},level:dt.DEBUG})}get explainsEnabled(){return this.level>=dt.DEBUG}shouldSnapshot(t){return this.level>=dt.DEBUG&&!!this.irSnapshot[t]}}});var me,_c,wc,sh=P(()=>{_s();di();ws();me=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}},_c=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}},wc=class{static{c(this,"PatternApplicator")}constructor(t){this.patternSet=t}applyPatterns(t,s=10,n=null){let r=new Rs(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,S=[];for(let x=0;x<d.numResults;x++)for(let N of d.getResult(x).getUsers())S.push(N);for(let x=0;x<d.numOperands;x++){let N=d.getOperand(x).definingOp;N&&S.push(N)}if(r.block=y,r.setInsertionPoint(d),!g.rewrite(d,r))continue;i++;for(let x of S)u(x);let b=_?_._next:y._head,A=y._size+2;for(;b&&b!==w&&A-- >0;)u(b),b=b._next;u(d);break}}return n&&(p&&n.level>=dt.INFO&&n.emit({type:"pass_detail",passName:"PatternApplicator",message:`pattern rewriting hit safety budget (${f}) without converging`,totalRewrites:i,level:dt.INFO}),n.level>=dt.DEBUG&&i>0&&n.emit({type:"pass_detail",passName:"PatternApplicator",totalRewrites:i,patternCount:this.patternSet.patterns.length,level:dt.DEBUG})),i>0?ot.CHANGED:ot.UNCHANGED}}});function Gr(e){return(nh[e]||Gd).wasm}function qd(e){return(nh[e]||Gd).load}function KN(e){return(nh[e]||Gd).store}function Lo(e){return(nh[e]||Gd).bytes}function Bb(e){return WN[e]||null}function qr(e,t){let s=WN[e];return s&&s[t]||null}function Po(e){return jN[e]||lU}function rh(e){return Po(e).js}function Nr(e){return Po(e).c}function Mb(e){return Po(e).cPtr}function Lb(e){return Po(e).suffix}function cU(e){return Po(e).mathSuffix}function jd(e){return Po(e).bytes}function Tn(e){return Po(e).isFloat}function En(e){return Po(e).isInt}function Wd(e,t){return e==="sum"||e==="mean"?0:e==="prod"?1:e==="max"?En(t)?(Vd[t]||Vd.i32)[0]:-1/0:e==="min"?En(t)?(Vd[t]||Vd.i32)[1]:1/0:0}function Kd(e,t){let s=uU[e];return s?s+cU(t):e}function HN(e){return fU.has(e)}function Zs(e){return(Pb[e]||YN).wgsl}function jr(e){return(Pb[e]||YN).bytes}function XN(e){return JN[e]||e}function QN(e){return e in JN}function ZN(e,t,s){let n=hU[e];if(!n)return null;let r=n[t];return r&&r[s]||null}var jN,nh,Gd,WN,lU,Vd,uU,fU,Pb,YN,JN,hU,Nn=P(()=>{jN={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}},nh={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}},Gd=nh.f32,WN={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(Gr,"wasmType");c(qd,"wasmLoad");c(KN,"wasmStore");c(Lo,"wasmBytes");c(Bb,"wasmSimdEntry");c(qr,"wasmVecOp");lU=jN.f32;c(Po,"dtypeInfo");c(rh,"jsTypedArray");c(Nr,"cType");c(Mb,"cPtrType");c(Lb,"cLiteralSuffix");c(cU,"cMathFuncSuffix");c(jd,"dtypeBytes");c(Tn,"isDtypeFloat");c(En,"isDtypeInt");Vd={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(Wd,"reduceInitValue");uU={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(Kd,"cMathFunc");fU=new Set(["exp","log","sqrt","tanh","abs","ceil","floor","sin","cos","max","min","pow","round","sign","log2","log10"]);c(HN,"isJSMathFunc");Pb={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}},YN=Pb.f32;c(Zs,"wgslType");c(jr,"wgslBytes");JN={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(XN,"wgslMathFunc");c(QN,"hasWgslMathFunc");hU={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(ZN,"libraryFunc")});function Sc(e,t){let s={};return e.match(t,s)?s:null}var Hd,Db,Fb,bc,Wr,tR=P(()=>{Hd=class{static{c(this,"DFPattern")}match(t,s){return!1}},Db=class extends Hd{static{c(this,"AnyPattern")}match(){return!0}},Fb=class extends Hd{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}},bc=c(()=>new Db,"wildcard"),Wr=c((e,...t)=>new Fb(e,t),"isOp");c(Sc,"matchPattern")});function mi(e,t){return e&&e.opName==="constant"&&e.getAttr("value")===t}function $b(e){return new Ub(e)}var pU,dU,mU,gU,yU,Yd,xc,Jd,Xd,Ub,Ac,Ec,Tc,Nc,Rc,vc,kc,Qd,Zd,tm,em,sm,nm,rm,im,om,am,Cc=P(()=>{sh();Ft();Nn();tR();pU=Wr("transpose",Wr("transpose",bc())),dU=Wr("reshape",Wr("reshape",bc())),mU=Wr("neg",Wr("neg",bc())),gU=Wr("exp",Wr("log",bc())),yU=Wr("log",Wr("exp",bc()));c(mi,"isConstantVal");Yd=class extends me{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 M&&n instanceof M&&s.shapeEquals(n)}rewrite(t,s){return t.replaceAllResultsWith([t.getOperand(0)]),t.erase(),!0}},xc=class extends me{static{c(this,"ReshapeReshape")}constructor(){super("reshape_reshape",10),this.rootOpName="reshape"}match(t){return Sc(dU,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}},Jd=class extends me{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}},Xd=class extends me{static{c(this,"TransposeTranspose")}constructor(){super("transpose_transpose",10),this.rootOpName="transpose"}match(t){return Sc(pU,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}},Ub=class extends me{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($b,"commutativeConstantRightFor");Ac=class extends me{static{c(this,"AddZero")}constructor(){super("add_zero",5),this.rootOpName="add"}match(t){return mi(t.getOperand(1).definingOp,0)||mi(t.getOperand(0).definingOp,0)}rewrite(t,s){let n=mi(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}},Ec=class extends me{static{c(this,"SubZero")}constructor(){super("sub_zero",5),this.rootOpName="sub"}match(t){return mi(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}},Tc=class extends me{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:En(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}},Nc=class extends me{static{c(this,"MulOne")}constructor(){super("mul_one",5),this.rootOpName="mul"}match(t){return mi(t.getOperand(1).definingOp,1)||mi(t.getOperand(0).definingOp,1)}rewrite(t,s){let n=mi(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}},Rc=class extends me{static{c(this,"MulZero")}constructor(t=!1){super("mul_zero",5),this.rootOpName="mul",this.fastMath=t}match(t){return!En(t.getResult(0).type.dtype)&&!this.fastMath?!1:mi(t.getOperand(1).definingOp,0)||mi(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}},vc=class extends me{static{c(this,"DivOne")}constructor(){super("div_one",5),this.rootOpName="div"}match(t){return mi(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}},kc=class extends me{static{c(this,"DoubleNeg")}constructor(){super("double_neg",5),this.rootOpName="neg"}match(t){return Sc(mU,t)!==null}rewrite(t,s){let n=t.getOperand(0).definingOp.getOperand(0);return t.replaceAllResultsWith([n]),t.erase(),!0}},Qd=class extends me{static{c(this,"ExpLog")}constructor(t=!1){super("exp_log",5),this.rootOpName="exp",this.fastMath=t}match(t){return this.fastMath&&Sc(gU,t)!==null}rewrite(t,s){return t.replaceAllResultsWith([t.getOperand(0).definingOp.getOperand(0)]),t.erase(),!0}},Zd=class extends me{static{c(this,"LogExp")}constructor(t=!1){super("log_exp",5),this.rootOpName="log",this.fastMath=t}match(t){return this.fastMath&&Sc(yU,t)!==null}rewrite(t,s){return t.replaceAllResultsWith([t.getOperand(0).definingOp.getOperand(0)]),t.erase(),!0}},tm=class extends me{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}},em=class extends me{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}},sm=class extends me{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}},nm=class extends me{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}},rm=class extends me{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}},im=class extends me{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}},om=class extends me{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}},am=class extends me{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 tl(e){if(e.length!==2)return null;let t=e[0],s=e[1];if(!(t instanceof M)||!(s instanceof M)||t.dtype!==s.dtype)return null;let n=M.broadcastShape(t.shape,s.shape);return n?[new M(n,t.dtype)]:null}function lm(e){if(e.length!==1)return null;let t=e[0];return t instanceof M?[new M(t.shape,t.dtype)]:null}function eR(e){if(e.length!==2)return null;let t=e[0],s=e[1];if(!(t instanceof M)||!(s instanceof M))return null;let n=M.broadcastShape(t.shape,s.shape);return n?[new M(n,st.BOOL)]:null}function zb(e){if(e.length!==1)return null;let t=e[0];return!(t instanceof M)||!as(t.dtype)?null:[new M(t.shape,t.dtype)]}function el(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 M||t.push(`${e.opName} operand 0 is not tensor`),n instanceof M||t.push(`${e.opName} operand 1 is not tensor`),s instanceof M&&n instanceof M&&s.dtype!==n.dtype&&t.push(`${e.opName} dtype mismatch: ${s.dtype} vs ${n.dtype}`),t}function oh(e){let t=[];return e.numOperands!==1?(t.push(`${e.opName} expects 1 operand, got ${e.numOperands}`),t):(e.getOperand(0).type instanceof M||t.push(`${e.opName} operand is not tensor`),t)}function Vb(e){let t=oh(e);if(t.length===0){let s=e.getOperand(0).type.dtype;as(s)||t.push(`${e.opName} requires float type, got ${s}`)}return t}var Ic,Gb,cm=P(()=>{Ft();Ze();c(tl,"inferBinaryElementwise");c(lm,"inferUnaryElementwise");c(eR,"inferCompare");c(zb,"inferUnaryFloat");c(el,"verifyBinaryElementwise");c(oh,"verifyUnaryElementwise");c(Vb,"verifyUnaryFloat");Ic=[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],Gb=[...Ic,J.COMMUTATIVE,J.ASSOCIATIVE]});function um(e){return t=>{if(!(typeof t[0]!="number"||typeof t[1]!="number"))return e(t[0],t[1])}}function _U(e){return t=>typeof t[0]=="number"?e(t[0]):void 0}function sR(e){e.register(new et({name:"add",numOperands:2,numResults:1,traits:Gb,inferResultTypes:tl,verify:el,getCanonicalizationPatterns(){return[$b("add"),new Ac]},fold:um((t,s)=>t+s)})),e.register(new et({name:"mul",numOperands:2,numResults:1,traits:Gb,inferResultTypes:tl,verify:el,getCanonicalizationPatterns(){return[$b("mul"),new Nc,new Rc]},fold:um((t,s)=>t*s)})),e.register(new et({name:"sub",numOperands:2,numResults:1,traits:Ic,inferResultTypes:tl,verify:el,getCanonicalizationPatterns(){return[new Ec,new Tc]},fold:um((t,s)=>t-s)})),e.register(new et({name:"div",numOperands:2,numResults:1,traits:Ic,inferResultTypes:tl,verify:el,getCanonicalizationPatterns(){return[new vc]},fold:um((t,s)=>t/s)}));for(let t of["rem","pow"])e.register(new et({name:t,numOperands:2,numResults:1,traits:Ic,inferResultTypes:tl,verify:el}));e.register(new et({name:"neg",numOperands:1,numResults:1,traits:[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:lm,verify:oh,getCanonicalizationPatterns(){return[new kc]},fold:_U(t=>-t)}));for(let t of["maximum","minimum"])e.register(new et({name:t,numOperands:2,numResults:1,traits:[...Ic,J.COMMUTATIVE],inferResultTypes:tl,verify:el}))}var nR=P(()=>{Ze();Cc();cm();c(um,"scalarBinaryFold");c(_U,"scalarUnaryFold");c(sR,"register")});function rR(e){for(let t of["abs","floor","ceil","round","sign","square","reciprocal"])e.register(new et({name:t,numOperands:1,numResults:1,traits:[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:lm,verify:oh}));e.register(new et({name:"exp",numOperands:1,numResults:1,traits:[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:zb,fold(t){return typeof t[0]=="number"?Math.exp(t[0]):void 0},verify:Vb}));for(let t of["log","sqrt","rsqrt","tanh","sin","cos","erf","log2","log10","exp2"])e.register(new et({name:t,numOperands:1,numResults:1,traits:[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:zb,verify:Vb}))}var iR=P(()=>{Ze();cm();c(rR,"register")});function wU(e){if(e.length!==1)return null;let t=e[0];return!(t instanceof M)||t.dtype!==st.BOOL?null:[new M(t.shape,st.BOOL)]}function bU(e){if(e.length!==2)return null;let[t,s]=e;if(!(t instanceof M)||t.dtype!==st.BOOL||!(s instanceof M)||s.dtype!==st.BOOL)return null;let n=M.broadcastShape(t.shape,s.shape);return n?[new M(n,st.BOOL)]:null}function oR(e){e.register(new et({name:"compare",numOperands:2,numResults:1,attrs:[{name:"direction",type:"string",required:!0}],traits:[J.ELEMENTWISE],inferResultTypes:eR,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");SU.has(n)||s.push(`compare invalid direction: ${n}`)}return s}})),e.register(new et({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 M)||s.dtype!==st.BOOL||!(n instanceof M)||!(r instanceof M)||n.dtype!==r.dtype)return null;let i=M.broadcastShape(s.shape,n.shape,r.shape);return i?[new M(i,n.dtype)]:null}})),e.register(new et({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 M)||!(n instanceof M)||!(r instanceof M)||n.dtype!==r.dtype)return null;let i=M.broadcastShape(s.shape,n.shape,r.shape);return i?[new M(i,n.dtype)]:null}})),e.register(new et({name:"logical_not",numOperands:1,numResults:1,traits:[J.ELEMENTWISE],inferResultTypes:wU}));for(let t of["logical_and","logical_or"])e.register(new et({name:t,numOperands:2,numResults:1,traits:[J.ELEMENTWISE],inferResultTypes:bU}));e.register(new et({name:"clamp",numOperands:3,numResults:1,traits:[J.ELEMENTWISE],inferResultTypes(t){if(t.length!==3)return null;let s=t[1];return s instanceof M?[new M(s.shape,s.dtype)]:null}}))}var SU,aR=P(()=>{Ze();Ft();cm();c(wU,"inferUnaryBool");c(bU,"inferBinaryBool");SU=new Set(["eq","ne","lt","le","gt","ge"]);c(oR,"register")});function lR(e){e.register(new et({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 et({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 et({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 M))return null;let r=s.get?s.get("result_shape"):s.result_shape;return r?[new M(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===nt?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 M&&i.shape[a]!==nt&&i.shape[a]!==1&&r[n[a]]!==nt&&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 et({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 M))return null;let r=s.get?s.get("new_shape"):s.new_shape;return r?[new M(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 M&&n.isFullyStatic){let i=r.filter(o=>o===nt).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 Yd,new xc]},fold(t){return t[0]}})),e.register(new et({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 M))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 M(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 Jd]},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 M?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 M&&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 et({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 M))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 M(a,n.dtype)]}})),e.register(new et({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 M))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 M)||a.dtype!==n.dtype||a.rank!==n.rank)return null;i[r]===nt||a.shape[r]===nt?i[r]=nt:i[r]+=a.shape[r]}return[new M(i,n.dtype)]}})),e.register(new et({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 M))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]===nt?a.push(nt):a.push(r[l]+n.shape[l]+(n.shape[l]-1)*o[l]+i[l]);return[new M(a,n.dtype)]}})),e.register(new et({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 M)||!(r instanceof M))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 M(p,n.dtype)]}})),e.register(new et({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 M?[new M(s.shape,s.dtype)]:null}})),e.register(new et({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 M))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 M(a,n.dtype)})}}))}var cR=P(()=>{Ze();Ft();Cc();c(lR,"register")});function uR(e){e.register(new et({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 M))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 M(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");xU.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 M))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 M(a,st.I32)]}c(t,"inferArgReduceTypes"),e.register(new et({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 et({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 xU,fR=P(()=>{Ze();Ft();xU=new Set(["sum","max","min","prod","mean","and","or"]);c(uR,"register")});function hR(e){e.register(new et({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 M)||!(n instanceof M))return 0;let r=t.getAttr("lhs_contracting")||[],i=1;for(let l of r)s.shape[l]!==nt&&(i*=s.shape[l]);let o=t.getResult(0).type;if(!(o instanceof M))return 0;let a=o.numel();return a===nt?0:2*a*i},inferResultTypes:fm,getCanonicalizationPatterns(){return[new om]},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 M&&r instanceof M){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]!==nt&&r.shape[u]!==nt&&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 et({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:fm,getFlops(t){let s=t.getOperand(0).type,n=t.getResult(0).type;if(!(s instanceof M)||!(n instanceof M))return 0;let r=1;for(let o of t.getAttr("lhs_contracting")||[])s.shape[o]!==nt&&(r*=s.shape[o]);let i=n.numel();return i===nt?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 et({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 M)||!(n instanceof M))return 0;let r=s.numel(),i=n.numel();return r===nt||i===nt?0:2*r*i/(n.shape[0]||1)},inferResultTypes:qb,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 fm(e,t,s){if(e.length!==2)return null;let n=e[0],r=e[1];if(!(n instanceof M)||!(r instanceof M))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 M(f,i.outputDtype||n.dtype)]}function qb(e,t,s){if(e.length!==2)return null;let n=e[0],r=e[1];if(!(n instanceof M)||!(r instanceof M))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===nt||g===nt)p.push(nt);else{let _=(g-1)*l[d]+1;p.push(Math.floor((m+y-_)/o[d])+1)}}return[new M([f,h,...p],i.outputDtype||n.dtype)]}var jb=P(()=>{Ze();Ft();Cc();c(hR,"register");c(fm,"inferDotResultTypes");c(qb,"inferConvResultTypes")});function pR(e){e.register(new et({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 et({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 et({name:"tuple",numOperands:-1,numResults:1,inferResultTypes(t){return[new Qa(t)]}})),e.register(new et({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 et({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 M))return null;let r=s.get?s.get("target_dtype"):s.target_dtype;return[new M(n.shape,r)]}})),e.register(new et({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 M))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)||st.F32,a=[...n.shape],l=i<0?a.length+1+i:i;return a.splice(l,0,r),[new M(a,o)]}}))}var dR=P(()=>{Ze();Ft();c(pR,"register")});function mR(e){e.register(new et({name:"return",numOperands:-1,numResults:0,traits:[J.TERMINATOR]})),e.register(new et({name:"yield",numOperands:-1,numResults:0,traits:[J.TERMINATOR]})),e.register(new et({name:"if",numOperands:1,numResults:-1,hasRegions:!0,numRegions:2,sideEffects:ys.CONTROL,inferResultTypes(t,s,n){return n||null}})),e.register(new et({name:"while",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:2,sideEffects:ys.CONTROL,inferResultTypes(t){return[...t]}})),e.register(new et({name:"scan",numOperands:-1,numResults:-1,hasRegions:!0,numRegions:1,sideEffects:ys.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 et({name:"custom_call",numOperands:-1,numResults:-1,attrs:[{name:"call_target_name",type:"string",required:!0},{name:"backend_config",type:"any",required:!1}],sideEffects:ys.WRITE,inferResultTypes(t,s,n){return n||null}})),e.register(new et({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 et({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 gR=P(()=>{Ze();c(mR,"register")});function yR(e){e.register(new et({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 M))return null;let r=s.get?s.get("dst_layout"):s.dst_layout;return r?[new M(n.shape,n.dtype,new Ke(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 M&&(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 im,new am]}}))}var _R=P(()=>{Ze();Ft();Cc();c(yR,"register")});function AU(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 hm,wR=P(()=>{sh();Ft();c(AU,"quantParamsMatch");hm=class extends me{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:AU(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 M&&i instanceof M&&r.dtype===i.dtype?(t.replaceAllResultsWith([n]),t.erase(),!0):!1}}});function bR(e){e.register(new et({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 M))return null;let r=s.get?s.get("target_dtype"):s.target_dtype;return r?[new M(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 M&&!as(n.dtype)&&s.push(`quantize input must be float, got ${n.dtype}`);let r=t.getAttr("target_dtype");r&&!Wb.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 hm]}})),e.register(new et({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 M))return null;let r=s.get?s.get("target_dtype"):s.target_dtype;return r?[new M(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 M&&!TU.has(n.dtype)&&s.push("dequantize input must be i8, ui8, or i32, got "+n.dtype);let r=t.getAttr("target_dtype");return r&&!as(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 et({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 M)||!(n instanceof M))return 0;let r=t.getAttr("lhs_contracting")||[],i=1;for(let l of r)s.shape[l]!==nt&&(i*=s.shape[l]);let o=t.getResult(0).type;if(!(o instanceof M))return 0;let a=o.numel();return a===nt?0:2*a*i},inferResultTypes(t,s){return fm(t,s,{outputDtype:st.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 M&&!Wb.has(n.dtype)&&s.push(`quantized_dot lhs must be i8/ui8, got ${n.dtype}`),r instanceof M&&!Wb.has(r.dtype)&&s.push(`quantized_dot rhs must be i8/ui8, got ${r.dtype}`),s}})),e.register(new et({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 qb(t,s,{outputDtype:st.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 Wb,TU,SR=P(()=>{Ze();Ft();wR();jb();Wb=new Set([st.I8,st.UI8]),TU=new Set([st.I8,st.UI8,st.I32]);c(bR,"register")});function NU(e){if(e.length<1)return null;let t=e[0];return t instanceof M?[new M(t.shape,t.dtype)]:null}function Kr(e){if(e.length<1)return null;let t=e[0];return!(t instanceof M)||!as(t.dtype)?null:[new M(t.shape,t.dtype)]}function Zi(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 M&&!as(s.dtype)&&t.push(`${e.opName} requires float input, got ${s.dtype}`),t}function xR(e){e.register(new et({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:NU})),e.register(new et({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 M))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 M(o,n.dtype)]}})),e.register(new et({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 M)||!(n instanceof M)?null:[new M([...s.shape.slice(0,s.rank-1),n.shape[n.rank-1]],s.dtype)]}})),e.register(new et({name:"softmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0}],inferResultTypes:Kr,verify:Zi})),e.register(new et({name:"log_softmax",numOperands:1,numResults:1,attrs:[{name:"axis",type:"number",required:!0}],inferResultTypes:Kr,verify:Zi})),e.register(new et({name:"gelu",numOperands:1,numResults:1,traits:[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Kr,verify:Zi})),e.register(new et({name:"sigmoid",numOperands:1,numResults:1,traits:[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Kr,verify:Zi})),e.register(new et({name:"silu",numOperands:1,numResults:1,traits:[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Kr,verify:Zi})),e.register(new et({name:"layer_norm",numOperands:3,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"epsilon",type:"number",required:!0}],inferResultTypes:Kr,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 M&&!as(n.dtype)&&s.push(`layer_norm requires float input, got ${n.dtype}`),s}})),e.register(new et({name:"batch_norm",numOperands:5,numResults:1,attrs:[{name:"axis",type:"number",required:!0},{name:"epsilon",type:"number",required:!0}],inferResultTypes:Kr,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 M&&!as(n.dtype)&&s.push(`batch_norm requires float input, got ${n.dtype}`),s}}));for(let t of["selu","mish","hardswish","hardsigmoid"])e.register(new et({name:t,numOperands:1,numResults:1,traits:[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Kr,verify:Zi}));e.register(new et({name:"elu",numOperands:1,numResults:1,attrs:[{name:"alpha",type:"number"}],traits:[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Kr,verify:Zi})),e.register(new et({name:"leaky_relu",numOperands:1,numResults:1,attrs:[{name:"negative_slope",type:"number"}],traits:[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Kr,verify:Zi})),e.register(new et({name:"celu",numOperands:1,numResults:1,attrs:[{name:"alpha",type:"number"}],traits:[J.ELEMENTWISE,J.SAME_OPERAND_AND_RESULT_TYPE],inferResultTypes:Kr,verify:Zi})),e.register(new et({name:"embedding",numOperands:2,numResults:1,inferResultTypes(t){if(t.length<2)return null;let s=t[0],n=t[1];if(!(s instanceof M)||!(n instanceof M))return null;let r=[...n.shape,s.shape[s.rank-1]];return[new M(r,s.dtype)]}}))}var AR=P(()=>{Ze();Ft();c(NU,"inferSameAsInput");c(Kr,"inferSameAsInputFloat");c(Zi,"verifyUnaryFloat");c(xR,"register")});function ER(e){e.register(new et({name:"copy_to_device",numOperands:1,numResults:1,attrs:[{name:"src_device",type:"string",required:!0},{name:"dst_device",type:"string",required:!0}],sideEffects:ys.READ|ys.WRITE,traits:[J.INJECTIVE],inferResultTypes(t){return t.length!==1?null:[t[0]]},getFlops(){return 0}}))}var TR=P(()=>{Ze();c(ER,"register")});function NR(e,t,s,n,r,i){let o=e+n+r-t;return(i?Math.ceil(o/s):Math.floor(o/s))+1}function RR(e){e.register(new et({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 M)||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=NR(n.shape[2],r[0],i[0],o[0][0],o[0][1],a),u=NR(n.shape[3],r[1],i[1],o[1][0],o[1][1],a);return[new M([n.shape[0],n.shape[1],l,u],n.dtype)]}}))}var vR=P(()=>{Ze();Ft();c(NR,"computePoolOutputDim");c(RR,"register")});function kR(e){e.register(new et({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 M)||n.rank!==4)return null;let r=s.get?s.get("output_size"):s.output_size;return[new M([n.shape[0],n.shape[1],r[0],r[1]],n.dtype)]}}))}var CR=P(()=>{Ze();Ft();c(kR,"register")});function vU(e=RU){let t=new Dd;for(let s of e)s(t);return t}var RU,ut,cs=P(()=>{Ze();nR();iR();aR();cR();fR();jb();dR();gR();_R();SR();AR();TR();vR();CR();RU=[sR,rR,oR,lR,uR,hR,pR,mR,yR,bR,xR,ER,RR,kR];c(vU,"buildRegistry");ut=vU()});function IR(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^IR(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 OR(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(!OR(e[s],t[s]))return!1;return!0}return typeof e=="object"&&e!==null&&typeof e.equals=="function"?e.equals(t):!1}function gi(e,t=new Map){let s=new Re;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 kU,Nt,Rn=P(()=>{Pd();Mo();cs();kU=0,Nt=class e{static{c(this,"Operation")}constructor(t,s,n,r=null,i=null){if(this.opName=t,this.id=kU++,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 th(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 Za(n[o],this,o);if(this.regions=[],i)for(let o=0;o<i.length;o++){let a=i[o]instanceof Re?i[o]:new Re;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 th(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=ut.get(this.opName);return!!(t&&t.isTerminator)}hasSideEffects(){let t=ut.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=>gi(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^IR(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)||!OR(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(IR,"hashAttrValue");c(OR,"attrValueEquals");c(gi,"cloneRegion")});function*BR(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*BR(n)}function*MR(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*MR(n)}}var qs,Do=P(()=>{Mo();c(BR,"opsInRegions");c(MR,"blocksInRegions");qs=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 Re;let r=new ls(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*BR(t)}*blocksRecursive(){for(let t of this.body){yield t;for(let s of t)yield*MR(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 dn,sl=P(()=>{dn=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 LR(e){return e?e instanceof Qa?`tuple(${e.types.map(LR).join(", ")})`:e.shape!==void 0&&e.dtype!==void 0?`[${e.shape.join(",")}]:${e.dtype}`:String(e):"?"}function pm(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 CU(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 dm(e,t){let s=[];for(let n=0;n<e;n++)n!==t&&s.push(n);return s}function IU(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 ah(e,t,s,n){let r=new qs(e,t,s),i=new Rs(r);return n(i,r.args),r}var Rs,di=P(()=>{Ft();Rn();Mo();Do();sl();cs();c(LR,"describeType");c(pm,"indexSelectGatherOpts");c(CU,"bcastBatchDims");Rs=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 Nt(t,s,n,r,i);return this._insert(o)}_inferAndBuild(t,s,n=null,r=null,i=null){let o=ut.get(t),a=i;if(!a&&o&&o.inferResultTypes){let l=s.map(f=>f.type),u=n instanceof Map?n:IU(n);a=o.inferResultTypes(l,u,i)}if(!a){let l=s.map(f=>LR(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=st.F32){let n=new M([],s);return this.constant(t,n)}tensorConstant(t,s,n=st.F32){let r=new M(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 M([],t.type.dtype),o=new Re,a=new ls([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=CU(g,h),_=y.length,w=this._broadcastBatch(i,g,y),S=this._broadcastBatch(o,h,y),b=Array.from({length:_},(A,x)=>x);p=this.dot(w,S,[_+1],[_],b,b)}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 Re,o=new ls(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 Re,o=new ls([]);i.addBlock(o);let a=new Re,l=new ls([]);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 Re,o=new ls(r);i.addBlock(o);let a=new Re,l=new ls(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 Re,a=new ls([...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 M([],t.type.dtype),o=new Re;return o.addBlock(new ls([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 M(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(dm,"broadcastDimsExcluding");c(IU,"toMap");c(ah,"buildFunction")});function bs(e,t,s){if(s==null)return new $(e,t,s);let n=t instanceof U,r=s instanceof U;if(n&&r){let i=t.value,o=s.value;switch(e){case"+":return new U(i+o);case"-":return new U(i-o);case"*":return new U(i*o);case"//":if(o!==0)return new U(Math.trunc(i/o));break;case"%":if(o!==0)return new U((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 U(0);if(e==="//"&&i===1)return t;if(e==="%"&&i===1)return new U(0)}if(n){let i=t.value;if(e==="+"&&i===0||e==="*"&&i===1)return s;if(e==="*"&&i===0)return new U(0)}return new $(e,t,s)}var K,ts,Fo,Tt,lt,Kb,lh,rt,Z,Zt,vn,tn,At,Oc,mm,nl,Ct,$,ge,ue,ve,U,gt,te=P(()=>{K=Object.freeze({SERIAL:"serial",PARALLEL:"parallel",VECTORIZED:"vectorized",UNROLLED:"unrolled",THREAD_BINDING:"thread_binding",RECURRENCE:"recurrence"}),ts=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}},Fo=class extends ts{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)}},Tt=class extends ts{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)}},lt=class extends ts{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)}},Kb=Object.freeze({DATA_PAR:"DataPar",COMM_REDUCE:"CommReduce"}),lh=class extends ts{static{c(this,"BlockRealizeNode")}constructor(t,s,n=Kb.DATA_PAR){super(),this.iterVar=t,this.binding=s,this.kind=n}},rt=class extends ts{static{c(this,"BufferStoreNode")}constructor(t,s,n){super(),this.buffer=t,this.indices=s,this.value=n}},Z=class extends ts{static{c(this,"BufferLoadNode")}constructor(t,s){super(),this.buffer=t,this.indices=s}},Zt=class extends ts{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)}},vn=class extends ts{static{c(this,"LetStmtNode")}constructor(t,s,n){super(),this.variable=t,this.value=s,this.body=n,this._setChild("body",n)}},tn=class extends ts{static{c(this,"AllocateNode")}constructor(t,s,n){super(),this.buffer=t,this.scope=s,this.body=n,this._setChild("body",n)}},At=class extends ts{static{c(this,"SeqNode")}constructor(t){super(),this.stmts=t,this._setChildren("stmts",t)}},Oc=class extends ts{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)}},mm=class extends ts{static{c(this,"EvaluateNode")}constructor(t){super(),this.value=t}},nl=class extends ts{static{c(this,"SyncThreadsNode")}constructor(){super()}},Ct=class extends ts{static{c(this,"CallExternNode")}constructor(t,s,n){super(),this.externName=t,this.args=s,this.dtype=n}},$=class extends ts{static{c(this,"MathOpNode")}constructor(t,s,n=null){super(),this.op=t,this.a=s,this.b=n}},ge=class e extends ts{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}},ue=class extends ts{static{c(this,"CastNode")}constructor(t,s,n){super(),this.expr=t,this.fromDtype=s,this.toDtype=n}},ve=class extends ts{static{c(this,"VariableNode")}constructor(t,s){super(),this.name=t,this.dtype=s}},U=class extends ts{static{c(this,"IntImmNode")}constructor(t){super(),this.value=t}},gt=class extends ts{static{c(this,"FloatImmNode")}constructor(t){super(),this.value=t}};c(bs,"mathOp")});var Bc,Hb=P(()=>{Bc=Object.freeze({GLOBAL:"global",SHARED:"shared",LOCAL:"local",REGISTER:"register"})});var oe,rl=P(()=>{Nn();oe=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*jd(this.dtype)}}});function Jb(e,{name:t,compute:s,plevel:n=10,targetKind:r=null}){let i=Yb.get(e);return i||(i=new ch(e),Yb.set(e,i)),i.addImplementation(t,s,n,r),i}function ym(e,t=null){let s=Yb.get(e);if(!s)return null;let n=t?t.kind:null,r=new ch(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 Xb(e,t=null){let s=ym(e,t);return s?s.best():null}var gm,ch,Yb,Qb=P(()=>{gm=class{static{c(this,"OpImplementation")}constructor(t,s,n=10,r=null){this.name=t,this.compute=s,this.plevel=n,this.targetKind=r}},ch=class{static{c(this,"OpStrategy")}constructor(t){this.opName=t,this.implementations=[]}addImplementation(t,s,n=10,r=null){let i=new gm(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}},Yb=new Map;c(Jb,"registerOpStrategy");c(ym,"getOpStrategy");c(Xb,"selectImplementation")});function t0(e,t){return ym(e,t)?!0:Hr.has(e)}function Pt(e,t,s=OU){Jb(e,{name:`${e}.generic`,compute:t,plevel:s,targetKind:null})}function Uo(e,t){let s=Xb(e,t);return s?s.compute:void 0}function ke(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 fh(e,t,s){let n=e;for(let r=t.length-1;r>=0;r--)n=new Tt(t[r],new U(0),s[r],K.SERIAL,n);return n}function BU(e,t,s){let n=e;for(let r=t.length-1;r>=0;r--)n=new Tt(t[r],new U(0),new U(s[r]),K.SERIAL,n);return n}function Ce(e,t,s,n){return n?fh(e,t,n):BU(e,t,s)}function vs(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 lh(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 fh(f,o,u)}}}function to(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 U(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 U(0):s[i+a];return o}function Mc(e){let t=new Array(e.length);for(let s=0;s<e.length;s++)t[s]={buffer:e[s]};return t}function uh(){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 Zb(e){let t=new Array(e.length);for(let s=0;s<e.length;s++)t[s]=e[s].iterVar;return t}function yi(e,t,s,n,r){let i=n[0],{loopVars:o,loopBinds:a,indices:l,extentNodes:u}=ke(e,i.shape,i),f=new Array(s.length);for(let m=0;m<s.length;m++){let g=to(s[m],i,l);f[m]=new Z(s[m],g)}let h=r(t,f,i.dtype),p=new rt(i,l,h),d=new lt(e.blockName(`${t.opName}_block`),a,Mc(s),[{buffer:i}],p);return Ce(d,o,i.shape,u)}function Lc(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 oe(`buf_${e.varCounter++}`,y,g,Bc.GLOBAL);_.broadcastDims=Array.from({length:r.length},(S,b)=>b),e.bufferMap.set(s,_);let w=En(g)?new U(n):new gt(n);return new rt(_,y.map(()=>new U(0)),w)}let i=e.getOrAllocBuffer(s),o=En(i.dtype),a=c(g=>o?new U(g):new gt(g),"imm");if(n&&typeof n!="number"&&typeof n.length=="number"){if(i.shape.length===0)return new rt(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 S=new Array(i.shape.length);for(let b=0;b<i.shape.length;b++)S[b]=new U(Math.floor(w/g[b])%i.shape[b]);_.push(new rt(i,S,a(n[w])))}return new At(_)}let l=a(typeof n=="number"?n:0);if(i.shape.length===0)return new rt(i,[],l);let{loopVars:u,loopBinds:f,indices:h,extentNodes:p}=ke(e,i.shape,i),d=new rt(i,h,l),m=new lt(e.blockName(`${t.opName}_block`),f,[],[{buffer:i}],d);return Ce(m,u,i.shape,p)}function $o(e){let t={};for(let s=0;s<e.length;s++)t[e[s]]=s;return t}function PR(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 U(0):t[s[r]];return n}function hh(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 z=0;z<l.length;z++)!f.has(z)&&!h.has(z)&&m.push(z);let g=[];for(let z=0;z<u.length;z++)!p.has(z)&&!d.has(z)&&g.push(z);let y=e.allocVarArray("b",o.length),_=e.allocVarArray("ls",m.length),w=e.allocVarArray("rs",g.length),S=e.allocVarArray("c",r.length),b=e.allocBindArray("vb",y),A=e.allocBindArray("vls",_),x=e.allocBindArray("vrs",w),N=e.allocBindArray("vc",S),I=uh(Zb(b),Zb(A),Zb(x)),E=new Array(l.length);for(let z=0;z<o.length;z++)E[o[z]]=b[z].iterVar;for(let z=0;z<m.length;z++)E[m[z]]=A[z].iterVar;for(let z=0;z<r.length;z++)E[r[z]]=N[z].iterVar;let T=new Array(u.length);for(let z=0;z<a.length;z++)T[a[z]]=b[z].iterVar;for(let z=0;z<g.length;z++)T[g[z]]=x[z].iterVar;for(let z=0;z<i.length;z++)T[i[z]]=N[z].iterVar;let B=PR(s,E),k=PR(n,T),O=uh(b,A,x,N),D=[{vars:y,dims:o,shape:l,buf:s},{vars:_,dims:m,shape:l,buf:s},{vars:w,dims:g,shape:u,buf:n},{vars:S,dims:r,shape:l,buf:s}];function q(z){let j=z;for(let Y=D.length-1;Y>=0;Y--){let{vars:W,dims:G,shape:H,buf:kt}=D[Y];for(let wt=W.length-1;wt>=0;wt--){let bt=kt.broadcastDims?new U(H[G[wt]]):e.extentNode(H[G[wt]],kt,G[wt]);j=new Tt(W[wt],new U(0),bt,K.SERIAL,j)}}return j}return c(q,"wrapAccBody"),{outIdx:I,lhsIdx:B,rhsIdx:k,allIvs:O,wrapAccBody:q}}var Hr,OU,_m,ir=P(()=>{Ft();Hb();rl();Nn();te();Qb();Qb();Hr=new Set(["constant","scalar_constant"]),OU=10;c(t0,"hasLoweringRule");c(Pt,"registerLoweringRule");c(Uo,"getLoweringRule");_m=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 ve(`${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 oe(`buf_${this.varCounter++}`,r,i,Bc.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 oe(`buf_${this.varCounter++}`,n,r,Bc.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]===nt&&this.extentNode(nt,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!==nt?new U(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]===nt?this._shapeParamVar(s,r):new U(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 lh(this.allocVar(`${t}${r}`),s[r]);return n}};c(ke,"makeLoopNest");c(fh,"wrapLoopsWithNodes");c(BU,"wrapLoops");c(Ce,"wrapInLoops");c(vs,"buildSpatialNest");c(to,"computeBroadcastIndices");c(Mc,"bufRefs");c(uh,"concatIterVars");c(Zb,"extractIterVars");c(yi,"lowerPointwise");c(Lc,"lowerConstant");c($o,"parseLayout");c(PR,"physicalDotIndices");c(hh,"buildDotGeometry")});function e0(e,t,s){let n=Pc[e];return n?DR[e]?DR[e](t):t.length===2&&MU.has(n)?new $(n,t[0],t[1]):t.length===2&&LU.has(n)?new $(n,t[0],t[1]):t.length===1&&PU.has(n)?new $(n,t[0]):t.length===1&&n==="-"?new $("-",t[0]):new Ct(n,t,s):null}function FR(){for(let e of Object.keys(Pc))Pt(e,(t,s,n,r)=>yi(t,s,n,r,(i,o,a)=>e0(i.opName,o,a)));Pt("compare",(e,t,s,n)=>yi(e,t,s,n,(r,i)=>new ge(r.getAttr("direction")||"eq",i[0],i[1]))),Pt("select",(e,t,s,n)=>yi(e,t,s,n,(r,i)=>new Zt(i[0],i[1],i[2]))),Pt("clamp",(e,t,s,n)=>yi(e,t,s,n,(r,i,o)=>new Ct("min",[new Ct("max",[i[1],i[0]],o),i[2]],o))),Pt("convert",(e,t,s,n)=>yi(e,t,s,n,(r,i)=>new ue(i[0],s[0].dtype,n[0].dtype))),Pt("copy_to_device",(e,t,s,n)=>yi(e,t,s,n,(r,i)=>i[0]))}var MU,LU,PU,DR,Pc,wm=P(()=>{te();ir();MU=new Set(["+","-","*","/"]),LU=new Set(["&&","||"]),PU=new Set(["!"]),DR={square:c(e=>new $("*",e[0],e[0]),"square"),reciprocal:c(e=>new $("/",new gt(1),e[0]),"reciprocal")},Pc={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(e0,"buildElementwiseExpr");c(FR,"register")});function $R(){Pt("broadcast_in_dim",UR),Pt("broadcast",UR),Pt("transpose",(e,t,s,n)=>{let r=t.getAttr("permutation"),i=s[0],o=n[0],{loopVars:a,loopBinds:l,indices:u,extentNodes:f}=ke(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 rt(o,u,new Z(i,h)),d=new lt(e.blockName("transpose_block"),l,[{buffer:i}],[{buffer:o}],p);return Ce(d,a,o.shape,f)}),Pt("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}=ke(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 $("-",new $("-",f[m],new U(1)),u[m]):u[m];let p=new rt(o,u,new Z(i,h)),d=new lt(e.blockName("reverse_block"),l,[{buffer:i}],[{buffer:o}],p);return Ce(d,a,o.shape,f)}),Pt("reshape",(e,t,s,n)=>{let r=s[0],i=n[0],{loopVars:o,loopBinds:a,indices:l,extentNodes:u}=ke(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=bs("+",m,bs("*",l[_],new U(g)));f=new Array(r.shape.length);let y=m;for(let _=r.shape.length-1;_>=0;_--)_===0?f[_]=y:(f[_]=bs("%",y,new U(r.shape[_])),y=bs("//",y,new U(r.shape[_])))}let p=new rt(i,l,new Z(r,f)),d=new lt(e.blockName("reshape_block"),a,[{buffer:r}],[{buffer:i}],p);return Ce(d,o,i.shape,u)}),Pt("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}=ke(e,i.shape,i),p=new Array(r.shape.length);for(let g=0;g<r.shape.length;g++){let y=new U(o[g]);a[g]===1?p[g]=new $("+",y,f[g]):p[g]=new $("+",y,new $("*",f[g],new U(a[g])))}let d=new rt(i,f,new Z(r,p)),m=new lt(e.blockName("slice_block"),u,[{buffer:r}],[{buffer:i}],d);return Ce(m,l,i.shape,h)}),Pt("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}=ke(e,o.shape,o),d=new Array(r.shape.length),m=new U(1);for(let b=0;b<r.shape.length;b++){let A=new $("+",h[b],new U(-a[b]));if(l[b]>0){let I=l[b]+1,E=new $("%",A,new U(I)),T=new ge("eq",E,new U(0));m=new $("*",m,T),d[b]=new $("//",A,new U(I))}else d[b]=A;let x=new ge("ge",d[b],new U(0)),N=new ge("lt",d[b],new U(r.shape[b]));m=new $("*",m,new $("*",x,N))}let g=new Z(r,d),y=new Z(i,[]),_=new Zt(m,g,y),w=new rt(o,h,_),S=new lt(e.blockName("pad_block"),f,[{buffer:r},{buffer:i}],[{buffer:o}],w);return Ce(S,u,o.shape,p)}),Pt("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}=ke(e,u.shape,u),m=new Array(u.shape.length);for(let _=0;_<u.shape.length;_++)m[_]=_===i&&a>0?new $("+",p[_],new U(a)):p[_];let g=new rt(r,m,new Z(u,p)),y=new lt(e.blockName("concat"),h,[{buffer:u}],[{buffer:r}],g);o.push(Ce(y,f,u.shape,d)),a+=u.shape[i]}return o.length===1?o[0]:new At(o)}),Pt("iota",(e,t,s,n)=>{let r=n[0],i=t.getAttr("iota_dimension"),{loopVars:o,loopBinds:a,indices:l,extentNodes:u}=ke(e,r.shape,r),f=new ue(l[i],"index",r.dtype),h=new rt(r,l,f),p=new lt(e.blockName("iota_block"),a,[],[{buffer:r}],h);return Ce(p,o,r.shape,u)})}function UR(e,t,s,n){let r=s[0],i=n[0],o=t.getAttr("broadcast_dimensions")||[],{loopVars:a,loopBinds:l,indices:u,extentNodes:f}=ke(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 U(0):u[g]}let p=new rt(i,u,new Z(r,h)),d=new lt(e.blockName("broadcast_block"),l,[{buffer:r}],[{buffer:i}],p);return Ce(d,a,i.shape,f)}var zR=P(()=>{te();ir();c($R,"register");c(UR,"lowerBroadcast")});function UU(e,t){if(En(e)){let s=t?DU[e]??-2147483648:FU[e]??2147483647;return new U(s)}return new gt(t?-1/0:1/0)}function GR(){Pt("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 O=0;O<i.shape.length;O++)(f.has(O)?p:h).push(O);let d=vs(t,"si",h,i.shape,i),m=new rt(a,d.indices,new Z(o,[])),g=new lt(t.blockName("reduce_init"),d.ivs,[{buffer:o}],[{buffer:a}],m),y=h.length>0?d.wrap(g):g,_=vs(t,"sa",h,i.shape,i),w=t.allocVarArray("r",p.length),S=t.allocBindArray("rv",w);for(let O of S)O.kind=Kb.COMM_REDUCE;let b=new Array(i.shape.length);for(let O=0;O<h.length;O++)b[h[O]]=_.ivs[O].iterVar;for(let O=0;O<p.length;O++)b[p[O]]=S[O].iterVar;let A=new Z(a,_.indices),x=new Z(i,b),N=VR[u]||VR.sum,I=new rt(a,_.indices,N(A,x,a.dtype)),E=new Array(p.length);for(let O=0;O<p.length;O++)E[O]=t.extentNode(i.shape[p[O]],i,p[O]);let T=new lt(t.blockName("reduce_acc"),uh(_.ivs,S),[{buffer:i}],[{buffer:a}],I),B=fh(T,w,E);B=_.wrap(B);let k=[y,B];if(u==="mean"){let O=1,D=[];for(let G=0;G<p.length;G++){let H=i.shape[p[G]];H===nt?D.push(t.extentNode(nt,i,p[G])):O*=H}let q=vs(t,"sm",h,i.shape,i),z=new Z(a,q.indices),j;if(D.length===0)j=new $("*",z,new gt(1/O));else{let G=new U(O);for(let H of D)G=new $("*",G,H);j=new $("/",z,G)}let Y=new rt(a,q.indices,j),W=new lt(t.blockName("mean_div"),q.ivs,[{buffer:a}],[{buffer:a}],Y);k.push(h.length>0?q.wrap(W):W)}return new At(k)});function e(t,s){Pt(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 H=0;H<a.shape.length;H++)h.has(H)||p.push(H);let m=c(H=>{if(!f)return H.indices;let kt=new Array(a.shape.length);for(let wt=0;wt<p.length;wt++)kt[p[wt]]=H.indices[wt];return kt[d]=new U(0),kt},"outIndicesFor"),g=new oe("_argval_"+n.varCounter,p.map(H=>a.shape[H]),a.dtype,"global");n.varCounter++;let y=vs(n,"ai",p,a.shape,a),_=new rt(g,y.indices,UU(a.dtype,s==="gt")),w=new rt(l,m(y),new U(0)),S=new lt(n.blockName("arg_init"),y.ivs,[],[{buffer:g},{buffer:l}],new At([_,w])),b=p.length>0?y.wrap(S):S,A=vs(n,"as",p,a.shape,a),x=n.allocVar("ar"),N=n.allocBindArray("arv",[x]),I=new Array(a.shape.length);for(let H=0;H<p.length;H++)I[p[H]]=A.ivs[H].iterVar;I[d]=N[0].iterVar;let E=new Z(a,I),T=new Z(g,A.indices),B=new ge(s,E,T),k=new Zt(B,E,T),O=m(A),D=new Z(l,O),q=new Zt(B,N[0].iterVar,D),z=new rt(l,O,q),j=new rt(g,A.indices,k),Y=new lt(n.blockName("arg_acc"),uh(A.ivs,N),[{buffer:a},{buffer:g}],[{buffer:g},{buffer:l}],new At([z,j])),W=n.extentNode(a.shape[d],a,d),G=fh(Y,[x],[W]);return G=A.wrap(G),new At([b,G])})}c(e,"registerArgReduce"),e("argmax","gt"),e("argmin","lt")}var DU,FU,VR,qR=P(()=>{te();rl();Ft();ir();Nn();DU={i8:-128,i16:-32768,i32:-2147483648,i64:-2147483648,ui8:0,ui16:0,ui32:0,bool:0},FU={i8:127,i16:32767,i32:2147483647,i64:2147483647,ui8:255,ui16:65535,ui32:4294967295,bool:1};c(UU,"argReduceSentinel");VR={sum:c((e,t)=>new $("+",e,t),"sum"),mean:c((e,t)=>new $("+",e,t),"mean"),prod:c((e,t)=>new $("*",e,t),"prod"),max:c((e,t,s)=>new Ct("max",[e,t],s),"max"),min:c((e,t,s)=>new Ct("min",[e,t],s),"min")};c(GR,"register")});function jR(e,t){return as(t)?new ue(e,t,st.I32):e}function WR(){Pt("dot",(e,t,s,n)=>{let r=s[0],i=s[1],o=n[0],a=hh(e,t,r,i),l=vs(e,"di",Array.from({length:o.shape.length},(y,_)=>_),o.shape,o),u=new rt(o,l.indices,new gt(0)),f=new lt(e.blockName("matmul_init"),l.ivs,[],[{buffer:o}],u),h=l.wrap(f),p=new $("+",new Z(o,a.outIdx),new $("*",new Z(r,a.lhsIdx),new Z(i,a.rhsIdx))),d=new rt(o,a.outIdx,p),m=new lt(e.blockName("matmul"),a.allIvs,[{buffer:r},{buffer:i}],[{buffer:o}],d),g=a.wrapAccBody(m);return new At([h,g])}),Pt("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=$o(t.getAttr("input_layout")),p=$o(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=vs(e,"ci",Array.from({length:_.length},(Mt,Le)=>Le),_,o),S=new rt(o,w.indices,new gt(0)),b=new lt(e.blockName("conv_init"),w.ivs,[],[{buffer:o}],S),A=w.wrap(b),x=e.allocVar("cn"),N=e.allocVar("coc"),I=e.allocVar("cic"),E=e.allocVarArray("co",d),T=e.allocVarArray("ck",d),B=[x,N,...E,I,...T],k=e.allocBindArray("cv",B),O=k[0].iterVar,D=k[1].iterVar,q=k.slice(2,2+d),z=k[2+d].iterVar,j=k.slice(3+d),Y=new Array(_.length);Y[h.N]=O,Y[h.C]=D;let W=Object.keys(h).filter(Mt=>Mt!=="N"&&Mt!=="C").sort();for(let Mt=0;Mt<d;Mt++)Y[h[W[Mt]]]=q[Mt].iterVar;let G=new Array(r.shape.length);G[h.N]=O;let H=Math.floor(g/f);f>1?G[h.C]=new $("+",new $("*",new $("//",D,new U(H)),new U(y)),z):G[h.C]=z;let kt=new Array(i.shape.length);kt[p.O]=D,kt[p.I]=z;let wt=null;for(let Mt=0;Mt<d;Mt++){let Le=W[Mt],Yi=Le.toUpperCase(),os=bs("+",bs("*",q[Mt].iterVar,new U(a[Mt])),bs("+",bs("*",j[Mt].iterVar,new U(u[Mt])),new U(-l[Mt][0])));if(G[h[Le]]=os,kt[p[Yi]]=j[Mt].iterVar,l[Mt][0]!==0||l[Mt][1]!==0){let Lt=new ge("ge",os,new U(0)),Ne=new ge("lt",os,new U(r.shape[h[Le]])),Er=new $("*",Lt,Ne);wt=wt?new $("*",wt,Er):Er}}let bt=new Z(r,G),V=new Z(i,kt),Q=new Z(o,Y),xt=new $("*",bt,V),Et=wt?new Zt(wt,xt,new gt(0)):xt,je=new $("+",Q,Et),Gn=new rt(o,Y,je),xn=new lt(e.blockName("conv_acc"),k,[{buffer:r},{buffer:i}],[{buffer:o}],Gn),An=new Array(d);for(let Mt=0;Mt<d;Mt++){let Le=W[Mt].toUpperCase();An[Mt]=i.shape[p[Le]]}let is=xn;for(let Mt=d-1;Mt>=0;Mt--){let Le=W[Mt].toUpperCase();is=new Tt(T[Mt],new U(0),e.extentNode(An[Mt],i,p[Le]),K.SERIAL,is)}is=new Tt(I,new U(0),e.extentNode(y,i,p.I),K.SERIAL,is);for(let Mt=d-1;Mt>=0;Mt--){let Le=h[W[Mt]];is=new Tt(E[Mt],new U(0),e.extentNode(_[Le],o,Le),K.SERIAL,is)}return is=new Tt(N,new U(0),e.extentNode(g,i,p.O),K.SERIAL,is),is=new Tt(x,new U(0),e.extentNode(m,r,h.N),K.SERIAL,is),new At([A,is])}),Pt("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}=ke(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 U(0):_[A]=y[w++];for(let A=0;A<u.length;A++){let x=new Array(i.shape.length),N=0;for(let T=0;T<i.shape.length;T++)T===f?x[T]=new U(A):x[T]=g[N++];let I=jR(new Z(i,x),i.dtype),E=u[A];_[E]=new $("+",_[E],I)}let S=new rt(o,d,new Z(r,_)),b=new lt(e.blockName("gather_block"),p,[{buffer:r},{buffer:i}],[{buffer:o}],S);return Ce(b,h,o.shape,m)}),Pt("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=ke(e,r.shape,r),d=new rt(a,p.indices,new Z(r,p.indices)),m=new lt(e.blockName("scatter_copy"),p.loopBinds,[{buffer:r}],[{buffer:a}],d),g=Ce(m,p.loopVars,r.shape,p.extentNodes),{loopVars:y,loopBinds:_,indices:w,extentNodes:S}=ke(e,o.shape,o),b=[],A=[];for(let D=0;D<o.shape.length;D++)h.has(D)?A.push(w[D]):b.push(w[D]);let x=new Array(r.shape.length),N=0;for(let D=0;D<r.shape.length;D++)l.has(D)?x[D]=new U(0):x[D]=A[N++];for(let D=0;D<u.length;D++){let q=new Array(i.shape.length),z=0;for(let W=0;W<i.shape.length;W++)W===f?q[W]=new U(D):q[W]=b[z++];let j=jR(new Z(i,q),i.dtype),Y=u[D];x[Y]=new $("+",x[Y],j)}let I=new Z(o,w),E=new Z(a,x),T=new $("+",E,I),B=new rt(a,x,T),k=new lt(e.blockName("scatter_update"),_,[{buffer:o},{buffer:i}],[{buffer:a}],B),O=Ce(k,y,o.shape,S);return new At([g,O])}),Pt("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=hh(e,t,i,o),h=vs(e,"ei",Array.from({length:l.shape.length},(B,k)=>k),l.shape,l),p=new rt(l,h.indices,new gt(0)),d=new lt(e.blockName("matmul_init"),h.ivs,[],[{buffer:l}],p),m=h.wrap(d),g=new $("+",new Z(l,f.outIdx),new $("*",new Z(i,f.lhsIdx),new Z(o,f.rhsIdx))),y=new rt(l,f.outIdx,g),_=new lt(e.blockName("matmul_acc"),f.allIvs,[{buffer:i},{buffer:o}],[{buffer:l}],y),w=f.wrapAccBody(_);if(u.length===0)return new At([m,w]);let S=vs(e,"ep",Array.from({length:l.shape.length},(B,k)=>k),l.shape,l),b=S.indices,A=new Z(l,b),x={v:0};for(let B of u){let k=or.get(B);k&&(A=k(A,a,x,l,b))}let N=Mc([l,...a]),I=new rt(l,b,A),E=new lt(e.blockName("epilogue"),S.ivs,N,[{buffer:l}],I),T=S.wrap(E);return new At([m,w,T])}),Pt("cublas_gemm",Uo("dot"))}var or,KR=P(()=>{te();Ft();ir();c(jR,"asIndexValue");or=new Map;or.set("bias",(e,t,s,n,r)=>{let i=t[s.v++];return new $("+",e,new Z(i,to(i,n,r)))});or.set("residual_add",(e,t,s,n,r)=>{let i=t[s.v++];return new $("+",e,new Z(i,to(i,n,r)))});or.set("scale",(e,t,s,n,r)=>{let i=t[s.v++];return new $("*",e,new Z(i,to(i,n,r)))});or.set("relu",(e,t,s,n)=>new Ct("max",[e,new gt(0)],n.dtype));or.set("clamp",(e,t,s,n,r)=>{let i=t[s.v++],o=t[s.v++];return new Ct("min",[new Ct("max",[e,new Z(i,to(i,n,r))],n.dtype),new Z(o,to(o,n,r))],n.dtype)});or.set("neg",e=>new $("-",e));or.set("exp",(e,t,s,n)=>new Ct("exp",[e],n.dtype));or.set("tanh",(e,t,s,n)=>new Ct("tanh",[e],n.dtype));or.set("sqrt",(e,t,s,n)=>new Ct("sqrt",[e],n.dtype));or.set("abs",(e,t,s,n)=>new Ct("abs",[e],n.dtype));or.set("log",(e,t,s,n)=>new Ct("log",[e],n.dtype));or.set("activation",e=>e);c(WR,"register")});function Dc(e,t,s){let{loopVars:n,loopBinds:r,indices:i,extentNodes:o}=ke(e,s.shape,s),a=new rt(s,i,new Z(t,i)),l=new lt(e.blockName("cf_copy"),r,[{buffer:t}],[{buffer:s}],a);return Ce(l,n,s.shape,o)}function $U(e,t,s,n){let{loopVars:r,loopBinds:i,indices:o,extentNodes:a}=ke(e,s.shape,s),l=new rt(s,o,new Z(t,[n,...o])),u=new lt(e.blockName("scan_in"),i,[{buffer:t}],[{buffer:s}],l);return Ce(u,r,s.shape,a)}function zU(e,t,s,n){let{loopVars:r,loopBinds:i,indices:o,extentNodes:a}=ke(e,t.shape,t),l=new rt(s,[n,...o],new Z(t,o)),u=new lt(e.blockName("scan_out"),i,[{buffer:t}],[{buffer:s}],l);return Ce(u,r,t.shape,a)}function ph(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(Hr.has(o.opName)){i.push(Lc(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 oe?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=Uo(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 HR(){Pt("if",(e,t,s,n)=>{let r=s[0],i=new Z(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=ph(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(Dc(e,m,l[d]))}let h=f.length===1?f[0]:new At(f),p=null;if(a&&a.entryBlock){let d=ph(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(Dc(e,y,l[g]))}m.length>0&&(p=m.length===1?m[0]:new At(m))}return new Zt(i,h,p)}),Pt("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(Dc(e,s[y],_))}for(let y=0;y<t.numResults;y++)e.bufferMap.set(t.getResult(y),o[y]);let l=new oe(`_wcond_${e.varCounter++}`,[],"bool",Bc.GLOBAL),u=ph(e,r,o),f=u.stmts.slice(),h=u.yieldBuffers[0];f.push(new rt(l,[],new Z(h,[])));let p=f.length===1?f[0]:new At(f),d=ph(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(Dc(e,_,o[y]))}let g=m.length===1?m[0]:new At(m);return new At([...a,new Oc(l,p,g)])}),Pt("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 S=n[w]||e.getOrAllocBuffer({type:t.getResult(w).type});l[w]=S,u.push(Dc(e,a[w],S)),e.bufferMap.set(t.getResult(w),S)}let f=n.length-i,h=new Array(f);for(let w=0;w<f;w++){let S=n[i+w]||e.getOrAllocBuffer({type:t.getResult(i+w).type});h[w]=S,e.bufferMap.set(t.getResult(i+w),S)}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 S=e.getOrAllocBuffer({type:{shape:o[w].shape.slice(1),dtype:o[w].dtype}});g[w]=S,m.push($U(e,o[w],S,p))}let y=ph(e,t.regions[0],[...g,...l]);for(let w of y.stmts)m.push(w);for(let w=0;w<i;w++){let S=y.yieldBuffers[w];S&&S!==l[w]&&m.push(Dc(e,S,l[w]))}for(let w=0;w<f;w++)m.push(zU(e,y.yieldBuffers[i+w],h[w],p));m.push(new nl);let _=m.length===1?m[0]:new At(m);return new At([...u,new Tt(p,new U(0),d,K.RECURRENCE,_)])})}var YR=P(()=>{te();rl();Hb();ir();c(Dc,"copyBuffer");c($U,"sliceCopyIn");c(zU,"sliceCopyOut");c(ph,"lowerRegionBody");c(HR,"register")});function JR(){Pt("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}=ke(e,i.shape,i),p=new rt(i,f,new Z(r,f)),d=new lt(e.blockName("layout_transform_block"),u,[{buffer:r}],[{buffer:i}],p);return Ce(d,l,i.shape,h)})}var XR=P(()=>{te();ir();c(JR,"register")});function QR(){Pt("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 yi(e,t,s,n,(h,p)=>{let d=new $("/",p[0],new gt(r)),m=i!==0?new $("+",d,new gt(i)):d,g=new Ct("round",[m],"f32"),y=new Ct("min",[new Ct("max",[g,new gt(u)],"f32"),new gt(f)],"f32");return new ue(y,"f32",o)})}),Pt("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 yi(e,t,s,n,(l,u)=>{let f=new ue(u[0],o,a),h=i!==0?new $("-",f,new gt(i)):f;return new $("*",h,new gt(r))})}),Pt("quantized_dot",(e,t,s,n)=>{let r=s[0],i=s[1],o=n[0],a=hh(e,t,r,i),l=vs(e,"qdi",Array.from({length:o.shape.length},(N,I)=>I),o.shape,o),u=new rt(o,l.indices,new U(0)),f=new lt(e.blockName("qmatmul_init"),l.ivs,[],[{buffer:o}],u),h=l.wrap(f),p=new ue(new Z(r,a.lhsIdx),r.dtype,"i32"),d=new ue(new Z(i,a.rhsIdx),i.dtype,"i32"),m=t.getAttr("lhs_zero_point")||0,g=t.getAttr("rhs_zero_point")||0,y=m!==0?new $("-",p,new U(m)):p,_=g!==0?new $("-",d,new U(g)):d,w=new $("*",y,_),S=new $("+",new Z(o,a.outIdx),w),b=new rt(o,a.outIdx,S),A=new lt(e.blockName("qmatmul_acc"),a.allIvs,[{buffer:r},{buffer:i}],[{buffer:o}],b),x=a.wrapAccBody(A);return new At([h,x])}),Pt("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=$o(t.getAttr("input_layout")),p=$o(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,S=t.getAttr("kernel_zero_point")||0,b=vs(e,"qci",Array.from({length:_.length},(Lt,Ne)=>Ne),_,o),A=new rt(o,b.indices,new U(0)),x=new lt(e.blockName("qconv_init"),b.ivs,[],[{buffer:o}],A),N=b.wrap(x),I=e.allocVar("qcn"),E=e.allocVar("qcoc"),T=e.allocVar("qcic"),B=e.allocVarArray("qco",d),k=e.allocVarArray("qck",d),O=[I,E,...B,T,...k],D=e.allocBindArray("qcv",O),q=D[0].iterVar,z=D[1].iterVar,j=D.slice(2,2+d),Y=D[2+d].iterVar,W=D.slice(3+d),G=new Array(_.length);G[h.N]=q,G[h.C]=z;let H=Object.keys(h).filter(Lt=>Lt!=="N"&&Lt!=="C").sort();for(let Lt=0;Lt<d;Lt++)G[h[H[Lt]]]=j[Lt].iterVar;let kt=new Array(r.shape.length);kt[h.N]=q;let wt=Math.floor(m/f);f>1?kt[h.C]=new $("+",new $("*",new $("//",z,new U(wt)),new U(g)),Y):kt[h.C]=Y;let bt=new Array(i.shape.length);bt[p.O]=z,bt[p.I]=Y;let V=null;for(let Lt=0;Lt<d;Lt++){let Ne=H[Lt],Er=Ne.toUpperCase(),pi=bs("+",bs("*",j[Lt].iterVar,new U(a[Lt])),bs("+",bs("*",W[Lt].iterVar,new U(u[Lt])),new U(-l[Lt][0])));if(kt[h[Ne]]=pi,bt[p[Er]]=W[Lt].iterVar,l[Lt][0]!==0||l[Lt][1]!==0){let Bo=new ge("ge",pi,new U(0)),Ji=new ge("lt",pi,new U(r.shape[h[Ne]])),gc=new $("*",Bo,Ji);V=V?new $("*",V,gc):gc}}let Q=new ue(new Z(r,kt),r.dtype,"i32"),xt=new ue(new Z(i,bt),i.dtype,"i32"),Et=w!==0?new $("-",Q,new U(w)):Q,je=S!==0?new $("-",xt,new U(S)):xt,Gn=new $("*",Et,je),xn=V?new Zt(V,Gn,new U(0)):Gn,An=new Z(o,G),is=new $("+",An,xn),Mt=new rt(o,G,is),Le=new lt(e.blockName("qconv_acc"),D,[{buffer:r},{buffer:i}],[{buffer:o}],Mt),Yi=new Array(d);for(let Lt=0;Lt<d;Lt++){let Ne=H[Lt].toUpperCase();Yi[Lt]=i.shape[p[Ne]]}let os=Le;for(let Lt=d-1;Lt>=0;Lt--){let Ne=H[Lt].toUpperCase();os=new Tt(k[Lt],new U(0),e.extentNode(Yi[Lt],i,p[Ne]),K.SERIAL,os)}os=new Tt(T,new U(0),e.extentNode(g,i,p.I),K.SERIAL,os);for(let Lt=d-1;Lt>=0;Lt--){let Ne=h[H[Lt]];os=new Tt(B[Lt],new U(0),e.extentNode(_[Ne],o,Ne),K.SERIAL,os)}return os=new Tt(E,new U(0),e.extentNode(m,i,p.O),K.SERIAL,os),os=new Tt(I,new U(0),e.extentNode(y,r,h.N),K.SERIAL,os),new At([N,os])})}var ZR=P(()=>{te();ir();c(QR,"register")});function il(e){return ar.has(e)}function VU(){for(let e of Object.keys(Pc))ar.set(e,(t,s,n)=>e0(t.opName,s,n));ar.set("compare",(e,t)=>new ge(e.getAttr("direction")||"eq",t[0],t[1])),ar.set("select",(e,t)=>new Zt(t[0],t[1],t[2])),ar.set("clamp",(e,t,s)=>new Ct("min",[new Ct("max",[t[1],t[0]],s),t[2]],s)),ar.set("convert",(e,t)=>new ue(t[0],e.getOperand(0).type.dtype,e.getAttr("target_dtype")||e.getResult(0).type.dtype)),ar.set("broadcast_in_dim",(e,t)=>t[0]),ar.set("broadcast",(e,t)=>t[0]),ar.set("iota",()=>{throw new Error("iota fusion must be handled by the index-aware path in lowerFusion")}),ar.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 $("/",t[0],new gt(s)),u=new $("+",l,new gt(n)),f=new Ct("round",[u],"f32"),h=new Ct("min",[new Ct("max",[f,new gt(o)],"f32"),new gt(a)],"f32");return new ue(h,"f32",r)}),ar.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 ue(t[0],r,i),a=new $("-",o,new gt(n));return new $("*",a,new gt(s))})}function tv(e,t){let s=t.numOperands,n=t.numResults,r=new Array(s);for(let E=0;E<s;E++)r[E]=e.getOrAllocBuffer(t.getOperand(E));let i=new Array(n);for(let E=0;E<n;E++)i[E]=e.getOrAllocBuffer(t.getResult(E));let o=i[0],{loopVars:a,loopBinds:l,indices:u,extentNodes:f}=ke(e,o.shape,o),h=new Map,p=t.regions[0].entryBlock,d=new Map,m=[...p.ops()];for(let E=m.length-1;E>=0;E--){let T=m[E];if(T.opName==="broadcast_in_dim"||T.opName==="broadcast"){let B=T.getAttr("broadcast_dimensions");B&&B.length>0&&d.set(T.getOperand(0),B);continue}if(!(T.opName==="yield"||Hr.has(T.opName)))for(let B=0;B<T.numResults;B++){let k=d.get(T.getResult(B));if(k)for(let O=0;O<T.numOperands;O++)d.has(T.getOperand(O))||d.set(T.getOperand(O),k)}}let g=new Map,y=p.arguments;for(let E=0;E<y.length;E++){let T=d.get(y[E]);T&&g.set(E,T)}for(let E=0;E<p.arguments.length;E++){let T=g.get(E),B;if(T){let k=r[E];B=new Array(k.shape.length);for(let O=0;O<k.shape.length;O++)B[O]=k.shape[O]===1?new U(0):u[T[O]]}else B=to(r[E],o,u);h.set(p.arguments[E],new Z(r[E],B))}let _=new Map;for(let E of p.ops())for(let T=0;T<E.numOperands;T++){let B=E.getOperand(T);_.set(B,(_.get(B)||0)+1)}let w=new Map,S=0,b=[];function A(E){let T=h.get(E);if(T===void 0)throw new Error(`Fusion lowering: unmapped operand from '${E.definingOp?E.definingOp.opName:"unknown"}'`);if((_.get(E)||0)>1&&!GU.has(T.type)){if(!w.has(E)){let B=T.type==="CompareNode"?"i32":T.type==="CastNode"?T.toDtype:o.dtype,k=e.allocVar(`cse${S++}`,B);w.set(E,k),b.push({variable:k,value:T}),h.set(E,k)}return w.get(E)}return T}c(A,"getExpr");let x=[];for(let E of p.ops()){if(E.opName==="yield"){for(let k=0;k<E.numOperands;k++)x.push(new rt(i[k],u,A(E.getOperand(k))));break}if(Hr.has(E.opName)){let k=E.getAttr("value");h.set(E.getResult(0),new gt(typeof k=="number"?k:0));continue}if(E.opName==="iota"){let k=E.getAttr("iota_dimension")??E.getAttr("dimension")??0;h.set(E.getResult(0),u[k]);continue}let T=ar.get(E.opName);if(!T)throw new Error(`Fusion lowering: unsupported op '${E.opName}' inside fusion body`);let B=new Array(E.numOperands);for(let k=0;k<E.numOperands;k++)B[k]=A(E.getOperand(k));h.set(E.getResult(0),T(E,B,o.dtype))}let N=x.length===1?x[0]:new At(x);for(let E=b.length-1;E>=0;E--)N=new vn(b[E].variable,b[E].value,N);let I=new lt(e.blockName("fusion_block"),l,Mc(r),Mc(i),N);return Ce(I,a,o.shape,f)}function qU(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 ev(e){let t=e.regions[0];if(!t)return!1;for(let s of t.entryBlock.ops())if(s.opName!=="yield"){if(Hr.has(s.opName)){if(typeof s.getAttr("value")=="number")continue;return!1}if(!ar.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(!qU(e.getResult(n).type.shape,s))return!1}return!0}function sv(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(Hr.has(o.opName)){s.push(Lc(e,o));continue}let f=Uo(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 nv(){VU()}var ar,GU,rv=P(()=>{te();ir();wm();ar=new Map;c(il,"canInlineFuse");c(VU,"initBuiltinFusionBuilders");GU=new Set(["BufferLoadNode","VariableNode","IntImmNode","FloatImmNode"]);c(tv,"lowerFusion");c(qU,"shapesEqual");c(ev,"canLowerAsElementwiseFusion");c(sv,"lowerFusionAsIndividualOps");c(nv,"register")});function iv(e,t,s,n,r){let i=new $("-",new $("*",e,new U(t)),new U(n)),o=new $("+",i,new U(s)),a=new Ct("max",[i,new U(0)],"index"),l=new Ct("min",[o,new U(r)],"index"),u=new $("-",l,a);return new Ct("max",[u,new U(0)],"index")}function jU(e,t,s,n,r,i,o,a,l,u,f){let h=iv(e,s,r,o,l),p=iv(t,n,i,a,u),d=new $("*",h,p);return new ue(d,"index",f)}function ov(){Pt("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=$o(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],S=r.shape[g],b=i.shape[m],A=i.shape[g],x=a[0],N=a[1],I=l[0],E=l[1],T=u[0][0],B=u[1][0],k=o==="max",O=k?new gt(-1/0):new gt(0),D=vs(e,"pi",[0,1,2,3],i.shape,i),q=new rt(i,D.indices,O),z=new lt(e.blockName("pool_init"),D.ivs,[],[{buffer:i}],q),j=D.wrap(z),Y=e.allocVar("pn"),W=e.allocVar("pc"),G=e.allocVar("poh"),H=e.allocVar("pow"),kt=e.allocVar("pkh"),wt=e.allocVar("pkw"),bt=[Y,W,G,H,kt,wt],V=e.allocBindArray("pv",bt),Q=V[0].iterVar,xt=V[1].iterVar,Et=V[2].iterVar,je=V[3].iterVar,Gn=V[4].iterVar,xn=V[5].iterVar,An=new $("+",new $("*",Et,new U(I)),new $("-",Gn,new U(T))),is=new $("+",new $("*",je,new U(E)),new $("-",xn,new U(B))),Mt=new ge("ge",An,new U(0)),Le=new ge("lt",An,new U(w)),Yi=new ge("ge",is,new U(0)),os=new ge("lt",is,new U(S)),Lt=new $("*",new $("*",Mt,Le),new $("*",Yi,os)),Ne=new Array(4);Ne[p]=Q,Ne[d]=xt,Ne[m]=Et,Ne[g]=je;let Er=new Array(4);Er[p]=Q,Er[d]=xt,Er[m]=An,Er[g]=is;let pi=new Z(r,Er),Bo=new Z(i,Ne),Ji;if(k){let Tr=new Zt(Lt,pi,new gt(-1/0));Ji=new Ct("max",[Bo,Tr],i.dtype)}else{let Tr=new Zt(Lt,pi,new gt(0));Ji=new $("+",Bo,Tr)}let gc=new rt(i,Ne,Ji),zs=new lt(e.blockName("pool_acc"),V,[{buffer:r}],[{buffer:i}],gc);zs=new Tt(wt,new U(0),new U(N),K.SERIAL,zs),zs=new Tt(kt,new U(0),new U(x),K.SERIAL,zs),zs=new Tt(H,new U(0),new U(A),K.SERIAL,zs),zs=new Tt(G,new U(0),new U(b),K.SERIAL,zs),zs=new Tt(W,new U(0),new U(_),K.SERIAL,zs),zs=new Tt(Y,new U(0),new U(y),K.SERIAL,zs);let Ya=[j,zs];if(!k){let Tr=vs(e,"pd",[0,1,2,3],i.shape,i),gb=Tr.indices[m],Rd=Tr.indices[g],yb=f?new gt(x*N):jU(gb,Rd,I,E,x,N,T,B,w,S,i.dtype),vd=new Z(i,Tr.indices),Hf=f?new $("*",vd,new gt(1/(x*N))):new $("/",vd,yb),Yf=new rt(i,Tr.indices,Hf),kd=new lt(e.blockName("pool_div"),Tr.ivs,[{buffer:i}],[{buffer:i}],Yf);Ya.push(Tr.wrap(kd))}return new At(Ya)})}var av=P(()=>{te();ir();c(iv,"spatialCount");c(jU,"avgPoolDivisorExpr");c(ov,"register")});function lv(){Pt("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=vs(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 Ct("floor",[new $("*",m,new gt(y))],"f32"),x=new Ct("floor",[new $("*",g,new gt(_))],"f32"),N=new ue(new Ct("min",[new Ct("max",[A,new gt(0)],"f32"),new gt(a-1)],"f32"),"f32","i32"),I=new ue(new Ct("min",[new Ct("max",[x,new gt(0)],"f32"),new gt(l-1)],"f32"),"f32","i32");w=new Z(r,[p,d,N,I])}else{let A=new $("*",m,new gt(y)),x=new $("*",g,new gt(_)),N=new Ct("floor",[A],"f32"),I=new Ct("floor",[x],"f32"),E=new Ct("min",[new $("+",N,new gt(1)),new gt(a-1)],"f32"),T=new Ct("min",[new $("+",I,new gt(1)),new gt(l-1)],"f32"),B=new $("-",A,N),k=new $("-",x,I),O=new Ct("max",[N,new gt(0)],"f32"),D=new Ct("max",[I,new gt(0)],"f32"),q=new ue(O,"f32","i32"),z=new ue(D,"f32","i32"),j=new ue(E,"f32","i32"),Y=new ue(T,"f32","i32"),W=new Z(r,[p,d,q,z]),G=new Z(r,[p,d,q,Y]),H=new Z(r,[p,d,j,z]),kt=new Z(r,[p,d,j,Y]),wt=new $("-",new gt(1),k),bt=new $("-",new gt(1),B),V=new $("+",new $("*",W,wt),new $("*",G,k)),Q=new $("+",new $("*",H,wt),new $("*",kt,k));w=new $("+",new $("*",V,bt),new $("*",Q,B))}let S=new rt(i,h.indices,w),b=new lt(e.blockName("resize_block"),h.ivs,[{buffer:r}],[{buffer:i}],S);return h.wrap(b)})}var cv=P(()=>{te();ir();c(lv,"register")});function uv(){Pt("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 oe(e.blockName("fa_m"),[1],u,"local"),_=new oe(e.blockName("fa_l"),[1],u,"local"),w=new oe(e.blockName("fa_s"),[1],u,"local"),S=new oe(e.blockName("fa_mn"),[1],u,"local"),b=new oe(e.blockName("fa_p"),[1],u,"local"),A=new oe(e.blockName("fa_cr"),[1],u,"local"),x=new oe(e.blockName("fa_o"),[g],u,"local"),N=new ve(e.blockName("fa_b"),"int32"),I=new ve(e.blockName("fa_h"),"int32"),E=new ve(e.blockName("fa_i"),"i32"),T=new ve(e.blockName("fa_j"),"int32"),B=new ve(e.blockName("fa_d"),"int32"),k=new ve(e.blockName("fa_di"),"int32"),O=new ve(e.blockName("fa_do"),"int32"),D=new ve(e.blockName("fa_dw"),"int32"),q=new lt(e.blockName("fa_initm"),[],[],[{buffer:y}],lr(y,[re],new gt(-1/0))),z=new lt(e.blockName("fa_initl"),[],[],[{buffer:_}],lr(_,[re],new gt(0))),j=dh(k,g,new lt(e.blockName("fa_inito"),[],[],[{buffer:x}],lr(x,[k],new gt(0)))),Y=new lt(e.blockName("fa_sinit"),[],[],[{buffer:w}],lr(w,[re],new gt(0))),W=dh(B,d,new lt(e.blockName("fa_sred"),[],[{buffer:r},{buffer:i},{buffer:w}],[{buffer:w}],lr(w,[re],cr("+",Ss(w,[re]),cr("*",Ss(r,[N,I,E,B]),Ss(i,[N,I,T,B])))))),G=new At([Y,W]),H=new lt(e.blockName("fa_supd"),[],[{buffer:w},{buffer:y},{buffer:_}],[{buffer:w},{buffer:S},{buffer:b},{buffer:A},{buffer:_}],new At([lr(w,[re],cr("*",Ss(w,[re]),new gt(l))),lr(S,[re],s0("max",Ss(y,[re]),Ss(w,[re]))),lr(b,[re],s0("exp",cr("-",Ss(w,[re]),Ss(S,[re])))),lr(A,[re],s0("exp",cr("-",Ss(y,[re]),Ss(S,[re])))),lr(_,[re],cr("+",cr("*",Ss(_,[re]),Ss(A,[re])),Ss(b,[re])))])),kt=dh(O,g,new lt(e.blockName("fa_oupd"),[],[{buffer:x},{buffer:A},{buffer:b},{buffer:o}],[{buffer:x}],lr(x,[O],cr("+",cr("*",Ss(x,[O]),Ss(A,[re])),cr("*",Ss(b,[re]),Ss(o,[N,I,T,O])))))),wt=new lt(e.blockName("fa_mupd"),[],[{buffer:S}],[{buffer:y}],lr(y,[re],Ss(S,[re]))),bt=dh(T,m,new At([G,H,kt,wt])),V=dh(D,g,new lt(e.blockName("fa_finw"),[],[{buffer:x},{buffer:_}],[{buffer:a}],lr(a,[N,I,E,D],cr("/",Ss(x,[D]),Ss(_,[re]))))),Q=new At([q,z,j,bt,V]);for(let xn of[x,A,b,S,w,_,y])Q=new tn(xn,xn.scope,Q);let xt=Math.min(p,256),Et=Math.ceil(p/xt),je;if(Et===1)je=new Tt(E,re,new U(p),K.THREAD_BINDING,Q,"threadIdx.x");else{let xn=new ve(e.blockName("fa_ii"),"i32"),An=new ve(e.blockName("fa_io"),"i32"),is=cr("+",cr("*",An,new U(xt)),xn),Mt=Et*xt===p?Q:new Zt(new ge("lt",E,new U(p)),Q),Le=new Tt(xn,re,new U(xt),K.THREAD_BINDING,new vn(E,is,Mt),"threadIdx.x");je=new Tt(An,re,new U(Et),K.THREAD_BINDING,Le,"blockIdx.z")}let Gn=new Tt(I,re,new U(h),K.THREAD_BINDING,je,"blockIdx.y");return new Tt(N,re,new U(f),K.THREAD_BINDING,Gn,"blockIdx.x")})}var re,Ss,lr,cr,s0,dh,fv=P(()=>{te();rl();ir();re=new U(0),Ss=c((e,t)=>new Z(e,t),"ld"),lr=c((e,t,s)=>new rt(e,t,s),"st"),cr=c((e,t,s)=>new $(e,t,s),"mop"),s0=c((e,...t)=>new Ct(e,t),"ext"),dh=c((e,t,s,n=K.SERIAL)=>new Tt(e,re,new U(t),n,s),"forL");c(uv,"register")});function hv(e,t,s=new Set){if(!WU.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),!hv(o,a,s)))return!1}}return!0}function KU(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 mh(e,t=null){let s=new _m,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())Hr.has(m.opName)&&f.push(Lc(s,m));for(let m of KU(e)){if(m.opName==="return"||m.opName==="yield"||Hr.has(m.opName))continue;if(m.opName==="fusion"){ev(m)?f.push(tv(s,m)):sv(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(S=>hv(m.getResult(0),S))){let S=s.getOrAllocBuffer(m.getOperand(0)),b=m.getResult(0).type.shape,A=m.getAttr("broadcast_dimensions"),x;if(A&&A.length>0)x=A;else{let N=b.length-S.shape.length;x=Array.from({length:S.shape.length},(I,E)=>E+N)}S.broadcastDims=x,s.bufferMap.set(m.getResult(0),S);continue}let g=Uo(m.opName,t);if(!g)throw new Error(`No lowering rule defined for op: ${m.opName}`);let y=new Array(m.numOperands);for(let S=0;S<m.numOperands;S++)y[S]=s.getOrAllocBuffer(m.getOperand(S));let _=new Array(m.numResults);for(let S=0;S<m.numResults;S++)_[S]=s.getOrAllocBuffer(m.getResult(S));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:S}=ke(s,m.shape,m),b=new Z(m,w),A=new rt(g,w,b),x=new lt(s.blockName("copy_block"),_,[{buffer:m}],[{buffer:g}],A);f.push(Ce(x,y,m.shape,S))}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 Fo(e.name,n,f.length===1?f[0]:new At(f),r,h,new Map(s.shapeParams));return e._partitionTarget&&(d._partitionTarget=e._partitionTarget),d}var WU,Fc=P(()=>{te();ir();wm();zR();qR();KR();YR();XR();ZR();rv();av();cv();fv();wm();WU=new Set([...Object.keys(Pc),"compare","select","clamp","convert","copy_to_device","dot","fusion"]);c(hv,"broadcastViewSafeForUser");FR();$R();GR();WR();HR();JR();QR();nv();ov();lv();uv();c(KU,"topologicalOps");c(mh,"lowerGraphToPrimFunc")});function pv(e){let t=new bm(e);return t.register("dot","blas",{dtypes:["f32","f64"],minElements:64}),t}function dv(e){let t=new bm(e);return t.register("dot","cublas",{dtypes:["f32","f64","f16"]}),t.register("conv","cudnn",{dtypes:["f32","f16"]}),t}var n0,bm,mv=P(()=>{Nn();n0=class{static{c(this,"LibraryCall")}constructor(t,s,n,r={}){this.opName=t,this.libraryName=s,this.functionName=n,this.constraints=r}},bm=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=ZN(t,i.libraryName,n);if(o)return new n0(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(pv,"createCPULibrarySelector");c(dv,"createGPULibrarySelector")});var gv={};ne(gv,{CPUTarget:()=>ur,CUDATarget:()=>al,TargetFeatures:()=>ol,TargetKind:()=>Pe,WasmTarget:()=>ll,WebGPUTarget:()=>cl});var Pe,ol,ur,al,ll,cl,_i=P(()=>{Pe=Object.freeze({CPU:"cpu",CUDA:"cuda",WEBGPU:"webgpu",WASM:"wasm",ACCELERATOR:"accelerator"}),ol=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===Pe.CUDA||this.kind===Pe.WEBGPU}isWebGPU(){return this.kind===Pe.WEBGPU}isCPU(){return this.kind===Pe.CPU}isWasm(){return this.kind===Pe.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)}},ur=c((e={})=>new ol({kind:Pe.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"),al=c((e={})=>new ol({kind:Pe.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"),ll=c((e={})=>new ol({kind:Pe.WASM,name:"wasm_generic",vectorWidth:4,numCores:1,cacheLineSizeBytes:64,memoryBandwidthGBs:10,computeTFLOPs:.1,supportsInt8:!0,simd:!0,...e}),"WasmTarget"),cl=c((e={})=>new ol({kind:Pe.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 wv(e){return eo[0]=(e&32767)<<13,zo[0]*=_v,zo[0]>=65536&&(eo[0]|=2139095040),eo[0]|=(e&32768)<<16,zo[0]}function bv(e){zo[0]=e;let t=eo[0]&2147483648,s=(eo[0]^t)>>>0,n;if(s>=1199570944)n=s>2139095040?32256:31744;else if(s<947912704)eo[0]=s,zo[0]+=.5,n=eo[0]-1056964608&65535;else{let r=s>>13&1;s=s+yv>>>0,s=s+r>>>0,n=s>>13&65535}return(n|t>>>16)&65535}function Sm(e){return eo[0]=e<<16>>>0,zo[0]}function xm(e){zo[0]=e;let t=eo[0];if((t>>>23&255)===255)return t>>>16&65535;let s=32767+(t>>>16&1)>>>0;return t+s>>>0>>>16&65535}function Am(e,t){return e==="f16"?bv(t):e==="bf16"?xm(t):e==="i64"?typeof t=="bigint"?t:BigInt(Math.trunc(t)):t}function Em(e,t){return e==="f16"?wv(t):e==="bf16"?Sm(t):e==="i64"&&typeof t=="bigint"?Number(t):t}var zo,eo,yv,_v,r0,Tm,Uc=P(()=>{zo=new Float32Array(1),eo=new Uint32Array(zo.buffer),yv=(-112<<23>>>0)+4095>>>0,_v=5192296858534828e18;c(wv,"f16ToF32");c(bv,"f32ToF16");c(Sm,"bf16ToF32");c(xm,"f32ToBf16");c(Am,"coerceForStorage");c(Em,"readFromStorage");r0={F16_ADD_BIAS:yv|0,F16_MAGIC_MUL:_v},Tm=globalThis;Tm.__mlfw_f16_to_f32=wv;Tm.__mlfw_f32_to_f16=bv;Tm.__mlfw_bf16_to_f32=Sm;Tm.__mlfw_f32_to_bf16=xm});var i0,Sv,Nm,xv=P(()=>{te();Nn();Uc();i0={f16:"__mlfw_f16_to_f32",bf16:"__mlfw_bf16_to_f32"},Sv={f16:"__mlfw_f32_to_f16",bf16:"__mlfw_f32_to_bf16"},Nm=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 ${rh(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 ${rh(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=i0[t];return n?`${n}(${s})`:s}_wrapStoreVal(t,s){let n=Sv[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 ${rh(n.dtype)}(${r});`):r<0&&this._emit(`const ${n.name} = new ${rh(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===K.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)`):En(i.toDtype)?s.push(`(${o} | 0)`):i0[i.toDtype]?s.push(`${i0[i.toDtype]}(${Sv[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(HN(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 o0,Rm,Av=P(()=>{te();Nn();o0=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||[]}},Rm=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(`${Mb(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(`${Mb(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__ ${Nr(d.dtype)} ${d.name}[${m>0?m:1}];`)}for(let d of this._promotedBufferDecls)this._emit(`__shared__ ${Nr(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)*jd(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 o0(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===K.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!==K.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=Nr(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===K.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===K.UNROLLED||t.kind===K.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(`${Nr(t.buffer.dtype)} ${t.buffer.name}[${s}];`):this._emit(`${Nr(t.buffer.dtype)}* ${t.buffer.name} = (${Nr(t.buffer.dtype)}*)alloca(${this._dynamicNumel(t.buffer)} * sizeof(${Nr(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(`${Nr(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(`${Nr(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"?En(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`((${Nr(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=Lb(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`${Kd("rsqrt",i)||"rsqrtf"}(${r})`;if(t.externName==="sign"){let a=n[0],l=`0.0${Lb(i)}`;return`((${a} > ${l}) - (${a} < ${l}))`}if(t.externName==="min"||t.externName==="max"){if(En(i)){let a=t.externName==="min"?"<":">";return`((${n[0]}) ${a} (${n[1]}) ? (${n[0]}) : (${n[1]}))`}return`${Kd(t.externName,i)}(${r})`}return`${Kd(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(`${Nr(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 Vo(e){return YU[e]||"f32"}function kn(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 Vo(e.dtype);case"BufferLoadNode":return Vo(e.buffer.dtype);case"CastNode":return Vo(e.toDtype);case"CallExternNode":return Vo(e.dtype||"f32");case"CompareNode":return"i32";case"VariableNode":return Vo(e.dtype||"i32");case"MathOpNode":{let t=kn(e.a);if(Tn(t))return t;if(e.b){let s=kn(e.b);if(Tn(s))return s}return t}case"IfThenElseNode":return kn(e.thenBody);default:return"f32"}}function so(e){!e||typeof e!="object"||!e.type||(e._dtype=kn(e))}function Ev(e){return HU.has(e)}var ul,vm,gh,yh,km,$c,Cm,HU,YU,Im=P(()=>{Nn();ul=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}},vm=class extends ul{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)}},gh=class extends ul{static{c(this,"LIRFlatLoadNode")}constructor(t,s,n){super(),this.buffer=t,this.offsetExpr=s,this.dtype=n,this._setChild("offsetExpr",s)}},yh=class extends ul{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)}},km=class extends ul{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)}},$c=class extends ul{static{c(this,"LIRBindingsNode")}constructor(t,s){super(),this.bindings=t,this.body=s,this._setChild("body",s)}},Cm=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}},HU=new Set(["sqrt","abs","ceil","floor","min","max"]),YU={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(Vo,"normalizeDtype");c(kn,"inferDtype");c(so,"annotateDtype");c(Ev,"isWasmNativeOp")});var zc,Om,Tv=P(()=>{te();Nn();Im();Uc();zc=new Set(["f16","bf16"]),Om=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,Gr(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===K.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(zc.has(t)){this._emitHalfDecode(t);return}this._emit(qd(t))}_emitStoreOp(t){if(zc.has(t)){this._emitHalfEncode(t);return}this._emit(KN(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 "+r0.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 "+r0.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(zc.has(n.dtype)){s=!0;break}if(!s&&t.metadata&&t.metadata.locals){for(let[,n]of t.metadata.locals)if(zc.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&&zc.has(n.buffer.dtype)||(n.type==="LIRFlatLoadNode"||n.type==="LIRFlatStoreNode")&&zc.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=kn(s.value)||s.variable.dtype||this._defaultDtype;this._locals.set(s.variable.name,Gr(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===K.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===K.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===K.VECTORIZED&&this.target.supportsSimd()&&n!==null){this._visitVectorizedFor(t);return}if((t.kind===K.UNROLLED||t.kind===K.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,Gr(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(Tn(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,Gr(n));let r=this._constExtent(t.extent),i=t.op||"+",o=i==="+"&&r!==null&&t.loopKind===K.VECTORIZED&&this.target.supportsSimd()?Bb(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=Tn(o),u=l?"f32.add":"i32.add",f=qr(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=Lo(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=Lo(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||kn(t.thenBody),n=this._numPrefix(s);this._emitExpr(t.condition),Tn(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":Tn(t)?"f32":"i32"}_exprPrefix(t){return this._numPrefix(t&&t._dtype||kn(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")&&!Tn(t.dtype)){this._emitIntMinMax(t);return}if(t.externName==="abs"&&!Tn(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")&&!Tn(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"&&!Tn(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=Gr(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=Bb(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=Lo(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(qd(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(qd(t.dtype)),this._emit(s.simd.splat));break;case"FloatImmNode":this._emit("(f32.const "+t.value+")"),this._emit(s.simd.splat);break;case"IntImmNode":Tn(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+")"),Tn(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),Tn(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=qr(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(qr(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=qr(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=qr(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=qr(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=qr(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=qr(n,"sqrt");if(i){this._emit("(f32.const 1)"),this._emit(s.simd.splat),this._emitVecExpr(t.args[0]),this._emit(i),this._emit(qr(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,Gr(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===K.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===K.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&&!qr(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,Gr(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*Lo(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,Gr(r)),n.kind===K.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=kn(n.value)||n.variable.dtype||this._defaultDtype;this._ensureLocal(n.variable.name,Gr(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 kn(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=kn(n.value);r&&this._locals.set(n.variable.name,Gr(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||kn(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 JU,XU,QU,a0,Bm,l0,Nv=P(()=>{te();Nn();JU=new Set(["!","&&","||"]),XU=new Set(["<",">","<=",">=","==","!="]),QU={"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"},a0=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}},Bm=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[x,N]of t.metadata.threadBindings){this._threadBindings.set(x,N);for(let I of N)I.isDynamic||this._applyBindingDim(x,I.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(x=>x.numel()>0);this._packedMode=u,this._packedOffsets=null;let f=new Map;{let x=0;for(let[,N]of t.bufferMap)f.set(N.name,x++)}{let x=null;for(let[,N]of t.bufferMap)if(this._storeBuffers.has(N.name)){x=N.dtype;break}this._defaultDtype=x||(l.length?l[0].dtype:"f32")}let h={f32:"f32",i32:"i32",u32:"u32",f16:"f16"};if(u){this._packedOffsets=new Map;let x=c(E=>Math.ceil(E/4)*4,"align4"),N=new Map;for(let[,E]of t.bufferMap){n.push(E.name);let T=this._storeBuffers.has(E.name),B=Zs(E.dtype),k=(T?"w:":"r:")+B,O=N.get(k);O||(O={isWrite:T,wt:B,dtype:h[B]||E.dtype,bufs:[],off:0},N.set(k,O));let D=E.numel(),q=(T?"_pw_":"_pr_")+B;this._packedOffsets.set(E.name,{storage:q,offset:O.off}),O.bufs.push({name:E.name,offset:O.off,size:D,dtype:E.dtype,argIndex:f.get(E.name)}),O.off+=x(D)}let I=[...N.values()].sort((E,T)=>E.isWrite===T.isWrite?E.wt.localeCompare(T.wt):E.isWrite?1:-1);for(let E of I){let T=(E.isWrite?"_pw_":"_pr_")+E.wt,B=E.isWrite?"storage, read_write":"storage, read";r.push({index:i,name:T,mode:E.isWrite?"read_write":"read",packed:E.bufs,packedSize:E.off,dtype:E.dtype}),this._emit(`@group(0) @binding(${i}) var<${B}> ${T}: array<${E.wt}>;`),i++}}else for(let[,x]of t.bufferMap){n.push(x.name);let N=this._storeBuffers.has(x.name)?"read_write":"read",I=N==="read_write"?"storage, read_write":"storage, read";r.push({index:i,name:x.name,mode:N,dtype:x.dtype,argIndex:f.get(x.name)}),this._emit(`@group(0) @binding(${i}) var<${I}> ${x.name}: array<${Zs(x.dtype)}>;`),i++}if(t.shapeParams.length>0){this._emit(""),this._emit("struct ShapeParams {"),this._indent++;for(let x of t.shapeParams)n.push(x.name),this._emit(`${x.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 x of this._sharedBuffers){let N=x.numel();this._emit(`var<workgroup> ${x.name}: array<${Zs(x.dtype)}, ${N>0?N:1}>;`)}for(let x of this._promotedBufferDecls)this._emit(`var<workgroup> ${x.name}: array<${Zs(x.dtype)}, ${x.size}>;`);for(let x of this._wgPoolDecls)this._emit(`var<workgroup> ${x.pool}: array<${Zs(x.dtype)}, ${x.size>0?x.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 x=0;this._workgroupSize[1]>this._workgroupSize[x]&&(x=1),this._workgroupSize[2]>this._workgroupSize[x]&&(x=2),this._workgroupSize[x]=Math.max(1,this._workgroupSize[x]>>1)}let y=this._workgroupSize[0],_=this._workgroupSize[1],w=this._workgroupSize[2];if(this._emit(`@compute @workgroup_size(${y}, ${_}, ${w})`),this._emit(`fn ${t.name}(${p.join(", ")}) {`),this._indent++,!this._serializeThreads){let x=new Set;for(let[N,I]of this._threadBindings)if(QU[N]){for(let T of I)if(!x.has(T.varName)){let B=this._wgslBuiltinAccess(N);this._emit(`let ${T.varName}: i32 = i32(${B});`),x.add(T.varName)}}}this._assignLocalSlots(t),this._emitMissingLocalDecls(t),this._visitNode(t.body),this._indent--,this._emit("}");let S=this.target,b=[Math.min(this._workgroupSize[0],S.maxBlockDimX),Math.min(this._workgroupSize[1],S.maxBlockDimY),Math.min(this._workgroupSize[2],S.maxBlockDimZ)],A=[Math.min(this._dispatchSize[0],S.maxGridDimX),Math.min(this._dispatchSize[1],S.maxGridDimY),Math.min(this._dispatchSize[2],S.maxGridDimZ)];return new a0(t.name,this._lines.join(`
|
|
8
|
-
`),b,A,this._sharedBuffers.reduce((x,N)=>x+Math.max(N.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===K.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*(jr(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===K.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_${Zs(p)}`,y=0;for(let _ of d){let w=m.filter(b=>b.first<=_.last&&_.first<=b.last).map(b=>[b.offset,b.offset+b.size]).sort((b,A)=>b[0]-A[0]),S=0;for(let[b,A]of w){if(S+_.size<=b)break;S<A&&(S=A)}m.push({offset:S,size:_.size,first:_.first,last:_.last}),u.set(_.name,{pool:g,offset:S}),S+_.size>y&&(y=S+_.size)}f.push({pool:g,dtype:p,size:y}),h+=y*(jr(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!==K.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===K.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===K.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}: ${Zs(t.dtype)};`):this._emit(`var ${t.name}: array<${Zs(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 x=`_s${l++}`;this._slotDecls.push({name:x,dtype:m.dtype,size:1,scalar:!0}),this._localSlots.set(d,x),this._scalarSlotNames.add(x);continue}let S=o.get(m.dtype);S||(S=new l0((x,N)=>x.freeAt-N.freeAt),o.set(m.dtype,S));let b=null,A=S.peek();if(A&&A.freeAt<_&&(b=S.pop()),b)y>b.decl.size&&(b.decl.size=y);else{let x={name:`_lt${a++}`,dtype:m.dtype,size:y};this._slotDecls.push(x),b={decl:x}}b.freeAt=w,this._localSlots.set(d,b.decl.name),S.push(b)}}_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<${Zs(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=Zs(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=Zs(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"&&(JU.has(t.op)||XU.has(t.op))):!1}_numericExpr(t,s){if(this._isBoolExpr(t)){let n=s?Zs(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`${Zs(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=XN(t.externName);if(i===t.externName&&!QN(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"}},l0=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 Rv(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 vv=P(()=>{c(Rv,"buildSnippet")});function yt(e){let t=[];do{let s=e&127;e>>>=7,e&&(s|=128),t.push(s)}while(e);return t}function l$(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 c$(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 Mm(e){let t=new TextEncoder().encode(e);return[...yt(t.length),...t]}function u$(e){let t=new ArrayBuffer(4);return new Float32Array(t)[0]=e,[...new Uint8Array(t)]}function f$(e){let t=new ArrayBuffer(8);return new Float64Array(t)[0]=e,[...new Uint8Array(t)]}function wi(e,t){for(let s=0;s<t.length;s++)e.push(t[s]);return e}function Vc(e,t){let s=[e];return wi(s,yt(t.length)),wi(s,t),s}function fl(e){let t=[];wi(t,yt(e.length));for(let s of e)wi(t,s);return t}function h$(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 p$(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=[],S=[];for(;n()==="(";){t++;let b=s();if(b==="param")for(;n()!==")";){let A=s();A==="f32"?w.push(bh):A==="f64"?w.push(wh):A==="i32"?w.push(_h):A==="v128"&&w.push(Sh)}else if(b==="result")for(;n()!==")";){let A=s();A==="f32"?S.push(bh):A==="f64"?S.push(wh):A==="i32"?S.push(_h):A==="v128"&&S.push(Sh)}r(")")}r(")"),r(")"),i.push({module:y,name:_,params:w,results:S})}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 S=s();S==="i32"?u.push(_h):S==="i64"?u.push(f0):S==="f32"?u.push(bh):S==="f64"?u.push(wh):S==="v128"&&u.push(Sh)}r(")")}else if(w==="result"){for(;n()!==")";)s();r(")")}else if(w==="local"){for(;n()!==")";){let S=s();S.startsWith("$")?h.push(S.replace("$","")):S==="i32"?f.push(_h):S==="i64"?f.push(f0):S==="f32"?f.push(bh):S==="f64"?f.push(wh):S==="v128"&&f.push(Sh)}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 d$(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 S=l+1;return l=S,t.set(_,S),S}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(...l$(parseInt(o()))),m(")");else if(_==="i64.const")n.push(66),n.push(...c$(o())),m(")");else if(_==="f32.const")n.push(67),n.push(...u$(parseFloat(o()))),m(")");else if(_==="f64.const")n.push(68),n.push(...f$(parseFloat(o()))),m(")");else if(_==="local.get")n.push(32),n.push(...yt(u(o()))),m(")");else if(_==="local.set")n.push(33),n.push(...yt(u(o()))),m(")");else if(_==="block"){let w=o().replace("$","");a.push(w),n.push(2,c0),p(),n.push(11),a.pop(),m(")")}else if(_==="loop"){let w=o().replace("$","");a.push(w),n.push(3,c0),p(),n.push(11),a.pop(),m(")")}else if(_==="if"){let w=c0;if(i()==="("){let S=r;if(r++,i()==="result"){o();let b=o();w=b==="f32"?bh:b==="f64"?wh:b==="v128"?Sh:b==="i64"?f0:_h,m(")")}else r=S}for(n.push(4,w),a.push("_if");r<e.length&&i()!==")"&&i()==="(";){let S=r;r++;let b=o();if(b==="then")p(),m(")");else if(b==="else")n.push(5),p(),m(")");else{r=S;break}}n.push(11),a.pop(),m(")")}else r-=2,g();continue}if(r++,y==="br_if")n.push(13),n.push(...yt(d(f())));else if(y==="br")n.push(12),n.push(...yt(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(...yt(w))}else y==="local.get"?(n.push(32),n.push(...yt(u(f())))):y==="local.set"?(n.push(33),n.push(...yt(u(f())))):y.endsWith(".extract_lane")||y.endsWith(".replace_lane")?(n.push(...u0.get(y)),n.push(parseInt(o(),10))):u0.has(y)&&n.push(...u0.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 Lm(e){let t=h$(e),s=p$(t),n=new Map,r=s.funcParams.length;for(let x=0;x<s.funcLocalNames.length;x++)n.set(s.funcLocalNames[x],r+x);let i=new Map;for(let x=0;x<s.imports.length;x++)i.set(s.imports[x].name,x);let o=[],a=new Map;function l(x,N){let I=x.join(",")+">"+N.join(",");if(a.has(I))return a.get(I);let E=o.length;return o.push({params:x,results:N}),a.set(I,E),E}c(l,"getType");for(let x of s.imports)x.typeIdx=l(x.params,x.results);let u=l(s.funcParams,[]),f=Vc(e$,fl(o.map(x=>[a$,...yt(x.params.length),...x.params,...yt(x.results.length),...x.results]))),h=[];s.imports.length>0&&(h=Vc(s$,fl(s.imports.map(x=>[...Mm(x.module),...Mm(x.name),0,...yt(x.typeIdx)]))));let p=Vc(n$,fl([[...yt(u)]])),d=Vc(r$,fl([[1,...yt(s.memMin),...yt(s.memMax)]])),m=s.imports.length,g=Vc(i$,fl([[...Mm("memory"),2,...yt(0)],[...Mm(s.funcExportName),0,...yt(m)]])),y=[];if(s.funcLocals.length>0){let x=[],N=s.funcLocals[0],I=1;for(let E=1;E<s.funcLocals.length;E++)s.funcLocals[E]===N?I++:(x.push([...yt(I),N]),N=s.funcLocals[E],I=1);x.push([...yt(I),N]),wi(y,fl(x))}else y.push(0);let _=d$(s.bodyTokens,n,i),w=[];wi(w,y),wi(w,_),w.push(11);let S=[];wi(S,yt(w.length)),wi(S,w);let b=Vc(o$,fl([S])),A=[];for(let x of[ZU,t$,f,h,p,d,g,b])wi(A,x);return new Uint8Array(A)}var ZU,t$,e$,s$,n$,r$,i$,o$,_h,f0,wh,bh,Sh,a$,c0,u0,h0=P(()=>{ZU=[0,97,115,109],t$=[1,0,0,0],e$=1,s$=2,n$=3,r$=5,i$=7,o$=10,_h=127,f0=126,wh=124,bh=125,Sh=123,a$=96,c0=64;c(yt,"uleb");c(l$,"sleb");c(c$,"slebBig");c(Mm,"encStr");c(u$,"encF32");c(f$,"encF64");c(wi,"pushAll");c(Vc,"section");c(fl,"vec");u0=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,...yt(0),4,0]],["v128.store",[253,...yt(11),4,0]],["v128.bitselect",[253,...yt(82)]],["v128.and",[253,...yt(78)]],["v128.or",[253,...yt(80)]],["v128.not",[253,...yt(77)]],["f32x4.splat",[253,...yt(19)]],["i32x4.splat",[253,...yt(17)]],["f32x4.add",[253,...yt(228)]],["f32x4.sub",[253,...yt(229)]],["f32x4.mul",[253,...yt(230)]],["f32x4.div",[253,...yt(231)]],["f32x4.neg",[253,...yt(225)]],["f32x4.abs",[253,...yt(224)]],["f32x4.sqrt",[253,...yt(227)]],["f32x4.ceil",[253,...yt(103)]],["f32x4.floor",[253,...yt(104)]],["f32x4.min",[253,...yt(232)]],["f32x4.max",[253,...yt(233)]],["f32x4.eq",[253,...yt(65)]],["f32x4.ne",[253,...yt(66)]],["f32x4.lt",[253,...yt(67)]],["f32x4.gt",[253,...yt(68)]],["f32x4.le",[253,...yt(69)]],["f32x4.ge",[253,...yt(70)]],["f32x4.extract_lane",[253,...yt(31)]],["f32x4.replace_lane",[253,...yt(32)]],["i32x4.add",[253,...yt(174)]],["i32x4.sub",[253,...yt(177)]],["i32x4.mul",[253,...yt(181)]],["i32x4.abs",[253,...yt(160)]],["i32x4.min_s",[253,...yt(182)]],["i32x4.max_s",[253,...yt(184)]],["i32x4.eq",[253,...yt(55)]],["i32x4.ne",[253,...yt(56)]],["i32x4.lt_s",[253,...yt(57)]],["i32x4.gt_s",[253,...yt(58)]],["i32x4.le_s",[253,...yt(59)]],["i32x4.ge_s",[253,...yt(60)]],["i32x4.extract_lane",[253,...yt(27)]],["i32x4.replace_lane",[253,...yt(28)]]]);c(h$,"tokenize");c(p$,"parseModule");c(d$,"encodeBody");c(Lm,"encodeWat")});function kv(e){let{source:t,name:s,metadata:n}=e,{bufferOffsets:r,imports:i}=n,o=[...r.entries()],a=Lm(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 Cv=P(()=>{h0();c(kv,"buildSnippet")});function Iv(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"?m$(o,a):a.packed?g$(o,a):a.mode==="read_write"?y$(o,a):_$(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(""),w$(o,r),o.push(""),o.push("gpuBuffers.forEach(b => b.destroy());"),o.push("device.destroy();"),o.push("})();"),o.join(`
|
|
12
|
-
`)}function m$(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 g$(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 y$(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 _$(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 w$(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 Ov=P(()=>{c(Iv,"buildSnippet");c(m$,"emitShapeBinding");c(g$,"emitPackedBinding");c(y$,"emitOutputBinding");c(_$,"emitInputBinding");c(w$,"emitReadback")});function Bv(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 Mv=P(()=>{c(Bv,"buildSnippet")});function Pm(e,t){Lv.set(e,t),t.runtimeKind&&t.snippet&&Pv.set(t.runtimeKind,t.snippet)}function Dv(e){return Lv.get(e)||null}function Fv(e){return Pv.get(e)||null}var Lv,Pv,Uv=P(()=>{_i();xv();Av();Tv();Nv();vv();Cv();Ov();Mv();Lv=new Map,Pv=new Map;c(Pm,"registerCodegen");c(Dv,"getCodegenEntry");c(Fv,"getSnippetBuilder");Pm(Pe.CPU,{runtimeKind:"js",snippet:Rv,compile(e,t){return{source:new Nm(t).generate(e),metadata:{kind:"js",paramCount:e.params.length}}}});Pm(Pe.WASM,{runtimeKind:"wasm",snippet:kv,compile(e,t){let s=new Om(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}}});Pm(Pe.WEBGPU,{runtimeKind:"webgpu",snippet:Iv,compile(e,t){let s=new Bm(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}}}});Pm(Pe.CUDA,{runtimeKind:"cuda",snippet:Bv,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 Rm(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 $v(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 xh,bi,Ah=P(()=>{mv();Uv();c($v,"detectPureMatmul");xh=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=Fv(this.metadata.kind);if(!t)throw new Error(`No snippet for kind: ${this.metadata.kind}`);return t(this)}},bi=class{static{c(this,"BackendPipeline")}constructor(t,s={}){this.target=t,this.matmulBackend=s.matmulBackend||"native",this.librarySelector=t.isCPU()?pv(t):t.isGPU()?dv(t):null}compile(t){let s=Dv(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 xh(t.name,n,this.target,r)}compileAll(t){return t.map(s=>this.compile(s))}}});function Dm(e,t){zv.set(e,t)}function Vv(e){return zv.get(e)||null}var zv,Fm=P(()=>{zv=new Map;c(Dm,"registerMeasurer");c(Vv,"getMeasurer")});var Gv={};ne(Gv,{runWasmParallel:()=>E$});import{Worker as b$}from"node:worker_threads";import S$ from"node:os";function A$(){if(!p0){let e=Math.max(1,Math.min(S$.cpus().length-1,16));p0=new d0(e)}return p0}async function E$(e,t,s,n,r,i){let o=A$();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 x=0;x<f;x++){let N=s[x],E=(N instanceof Float32Array?N.length:0)/h;y.push(E>=1&&Number.isInteger(E)?E:0)}let _=u.slice(0,f);if(n)for(let x of n)_.push(x);let w=new SharedArrayBuffer(4*m),S=a.buffer.slice(a.byteOffset,a.byteOffset+a.byteLength),b=[];for(let x=0;x<m;x++){let N=x*g,I=Math.min(N+g,h);if(N>=h)break;let E=[];for(let B=0;B<f;B++){let k=s[B];k instanceof Float32Array&&E.push({offset:u[B],length:k.length,data:k.buffer.slice(0),fullLength:k.length,elemStart:0})}let T=[..._,N,I];b.push(o.dispatch(x,{wasmBinary:S,mathImportNames:i,bufferEntries:E,callArgs:T,controlBuffer:w,workerIdx:x,outputIndices:p||[]}))}let A=await Promise.all(b);for(let x=0;x<f;x++){if(!d.has(x)&&d.size>0)continue;let N=s[x];if(!(N instanceof Float32Array))continue;let I=y[x];if(I>0)for(let E of A){let T=E.outputs[x];if(!T)continue;let k=E.workerIdx*g,O=Math.min(k+g,h),D=new Float32Array(T),q=k*I,z=O*I;N.set(D.subarray(q,z),q)}else{let E=A.find(T=>T.workerIdx===0);E&&E.outputs[x]&&N.set(new Float32Array(E.outputs[x]))}}}var x$,p0,d0,qv=P(()=>{x$=`
|
|
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
|
-
`,p0=null;c(A$,"getPool");d0=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 b$(x$,{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(E$,"runWasmParallel")});import{readdirSync as Eh,existsSync as hl}from"fs";import{join as no,delimiter as jv}from"path";function Hv(){let e=[];return process.env.CUDA_PATH&&e.push(process.env.CUDA_PATH),!pl&&process.env.CUDA_HOME&&e.push(process.env.CUDA_HOME),e}function Yv(){let e=[];if(pl){let t="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA";if(hl(t))for(let s of Eh(t))e.push(no(t,s))}else{let t="/usr/local";if(hl(t))for(let s of Eh(t))(s==="cuda"||s.startsWith("cuda-"))&&e.push(no(t,s))}return e}function T$(e){return pl?[no(e,"bin")]:[no(e,"lib64"),no(e,"targets/x86_64-linux/lib")]}function N$(){return pl?[]:["/usr/lib/x86_64-linux-gnu","/lib/x86_64-linux-gnu"]}function Kv(e){let t=e.match(/\d+/g);return t?t.map(Number):[]}function R$(e,t){let s=Kv(e),n=Kv(t),r=Math.max(s.length,n.length);for(let i=0;i<r;i++){let o=(s[i]||0)-(n[i]||0);if(o!==0)return o}return 0}function v$(e){let t=process.env[Wv]||"";t.split(jv).includes(e)||(process.env[Wv]=t?e+jv+t:e)}function Si(e){let{pattern:t,fallback:s,extraDirs:n}=pl?e.win:e.linux,r=[];for(let i of[...Hv(),...Yv()])for(let o of T$(i))r.push(o);for(let i of N$())r.push(i);if(n)for(let i of n())r.push(i);for(let i of r){if(!hl(i))continue;let o=Eh(i).filter(a=>t.test(a)&&!a.includes(".alt"));if(o.length>0)return v$(i),o.sort(R$),no(i,o.pop())}return s}function k$(){for(let e of[...Hv(),...Yv()]){let t=no(e,"include");if(hl(t))return t}return pl?null:hl("/usr/local/cuda/include")?"/usr/local/cuda/include":null}function C$(){let e=[],t="C:/Program Files/NVIDIA/CUDNN";if(hl(t))for(let s of Eh(t).sort().reverse()){let n=no(t,s,"bin");if(hl(n))for(let r of Eh(n).filter(i=>i.startsWith("12.")).sort().reverse())e.push(no(n,r,"x64"))}return e}var pl,Wv,Um,Jv,Xv,$m,Qv,Zv,Th=P(()=>{pl=process.platform==="win32",Wv=pl?"PATH":"LD_LIBRARY_PATH";c(Hv,"envRoots");c(Yv,"toolkitRoots");c(T$,"libDirs");c(N$,"systemDirs");c(Kv,"versionKey");c(R$,"compareVersion");c(v$,"prependSearchPath");c(Si,"loadCudaLib");c(k$,"resolveIncludeDir");Um=k$(),Jv={win:{pattern:/^nvcuda\.dll$/,fallback:"nvcuda.dll"},linux:{pattern:/^libcuda\.so(\.\d+)*$/,fallback:"libcuda.so.1"}},Xv={win:{pattern:/^nvrtc64_\d+_\d+\.dll$/,fallback:"nvrtc64_120_0.dll"},linux:{pattern:/^libnvrtc\.so(\.\d+)*$/,fallback:"libnvrtc.so.12"}},$m={win:{pattern:/^cudart64_\d+\.dll$/,fallback:"cudart64_12.dll"},linux:{pattern:/^libcudart\.so(\.\d+)*$/,fallback:"libcudart.so.12"}},Qv={win:{pattern:/^cublas64_\d+\.dll$/,fallback:"cublas64_12.dll"},linux:{pattern:/^libcublas\.so(\.\d+)*$/,fallback:"libcublas.so.12"}};c(C$,"winCudnnDirs");Zv={win:{pattern:/^cudnn64_9\.dll$/,fallback:"cudnn64_9.dll",extraDirs:C$},linux:{pattern:/^libcudnn\.so(\.\d+)*$/,fallback:"libcudnn.so.9"}}});import tk from"koffi";function Vt(e,t){if(t!==0)throw new Error("CUDA driver error "+t+" in "+e)}function nk(e){let t=[0n];ro.getProgramLogSize(e,t);let s=Number(t[0]);if(s<=1)return"";let n=new Uint8Array(s);ro.getProgramLog(e,n);let r="";for(let i of n){if(i===0)break;r+=String.fromCharCode(i)}return r}var ae,dl,Rt,ro,ek,sk,xi=P(()=>{Th();ae=tk.load(Si(Jv)),dl=tk.load(Si(Xv)),Rt={init:ae.func("int cuInit(uint)"),deviceGet:ae.func("int cuDeviceGet(_Out_ int *dev, int ordinal)"),deviceGetAttribute:ae.func("int cuDeviceGetAttribute(_Out_ int *pi, int attrib, int dev)"),primaryCtxRetain:ae.func("int cuDevicePrimaryCtxRetain(_Out_ void **pctx, int dev)"),primaryCtxRelease:ae.func("int cuDevicePrimaryCtxRelease_v2(int dev)"),ctxSynchronize:ae.func("int cuCtxSynchronize()"),moduleLoadData:ae.func("int cuModuleLoadData(_Out_ void **mod, void *image)"),moduleGetFunction:ae.func("int cuModuleGetFunction(_Out_ void **func, void *mod, str name)"),memGetInfo:ae.func("int cuMemGetInfo_v2(_Out_ uint64 *free, _Out_ uint64 *total)"),memAlloc:ae.func("int cuMemAlloc_v2(_Out_ uint64 *dptr, size_t bytes)"),memFree:ae.func("int cuMemFree_v2(uint64 dptr)"),memcpyHtoD:ae.func("int cuMemcpyHtoD_v2(uint64 dst, void *src, size_t n)"),memcpyDtoH:ae.func("int cuMemcpyDtoH_v2(void *dst, uint64 src, size_t n)"),memcpyDtoD:ae.func("int cuMemcpyDtoD_v2(uint64 dst, uint64 src, size_t n)"),memcpyHtoDAsync:ae.func("int cuMemcpyHtoDAsync_v2(uint64 dst, void *src, size_t n, void *stream)"),memcpyDtoHAsync:ae.func("int cuMemcpyDtoHAsync_v2(void *dst, uint64 src, size_t n, void *stream)"),memcpyDtoDAsync:ae.func("int cuMemcpyDtoDAsync_v2(uint64 dst, uint64 src, size_t n, void *stream)"),memsetD8:ae.func("int cuMemsetD8_v2(uint64 dst, uint8 uc, size_t n)"),memsetD8Async:ae.func("int cuMemsetD8Async(uint64 dst, uint8 uc, size_t n, void *stream)"),launchKernel:ae.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:ae.func("int cuStreamCreate(_Out_ void **stream, uint flags)"),streamSynchronize:ae.func("int cuStreamSynchronize(void *stream)"),ctxSetCurrent:ae.func("int cuCtxSetCurrent(void *ctx)"),streamBeginCapture:ae.func("int cuStreamBeginCapture_v2(void *stream, int mode)"),streamEndCapture:ae.func("int cuStreamEndCapture(void *stream, _Out_ void **graph)"),graphInstantiate:ae.func("int cuGraphInstantiateWithFlags(_Out_ void **exec, void *graph, uint64 flags)"),graphLaunch:ae.func("int cuGraphLaunch(void *exec, void *stream)"),graphExecDestroy:ae.func("int cuGraphExecDestroy(void *exec)"),graphDestroy:ae.func("int cuGraphDestroy(void *graph)")},ro={createProgram:dl.func("int nvrtcCreateProgram(_Out_ void **prog, str src, str name, int n, void *h, void *inc)"),compileProgram:dl.func("int nvrtcCompileProgram(void *prog, int n, str *opts)"),destroyProgram:dl.func("int nvrtcDestroyProgram(void **prog)"),getPTXSize:dl.func("int nvrtcGetPTXSize(void *prog, _Out_ size_t *sz)"),getPTX:dl.func("int nvrtcGetPTX(void *prog, _Out_ uint8_t *ptx)"),getProgramLogSize:dl.func("int nvrtcGetProgramLogSize(void *prog, _Out_ size_t *sz)"),getProgramLog:dl.func("int nvrtcGetProgramLog(void *prog, _Out_ uint8_t *log)")},ek=75,sk=76;c(Vt,"checkCU");c(nk,"readProgramLog")});function ee(){if(Gc)return Vt("cuCtxSetCurrent",Rt.ctxSetCurrent(Gc.ctx)),Gc;Vt("cuInit",Rt.init(0));let e=[0];Vt("cuDeviceGet",Rt.deviceGet(e,0));let t=[0],s=[0];Vt("cuDeviceGetAttribute",Rt.deviceGetAttribute(t,ek,e[0])),Vt("cuDeviceGetAttribute",Rt.deviceGetAttribute(s,sk,e[0]));let n=[null];Vt("cuDevicePrimaryCtxRetain",Rt.primaryCtxRetain(n,e[0])),Vt("cuCtxSetCurrent",Rt.ctxSetCurrent(n[0]));let r=[null];Vt("cuStreamCreate",Rt.streamCreate(r,0));let i=[0n],o=[0n];return Vt("cuMemGetInfo",Rt.memGetInfo(i,o)),Gc={dev:e[0],ctx:n[0],stream:r[0],arch:"sm_"+t[0]+s[0],totalMem:Number(o[0])},process.on("exit",()=>{try{Rt.primaryCtxRelease(Gc.dev)}catch{}}),Gc}var Gc,Ai=P(()=>{xi();Gc=null;c(ee,"getDevice")});function zm(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 Vm(e,t){let s=zm(e),n=ik.get(s);if(n)return n;let{arch:r}=ee(),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+=O$),/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+I$+`extern "C" {
|
|
70
|
-
`+e+`
|
|
71
|
-
}
|
|
72
|
-
`,a=[null];Vt("nvrtcCreateProgram",ro.createProgram(a,o,t+".cu",0,null,null));let l=["--gpu-architecture="+r];if(Um&&l.push("--include-path="+Um),ro.compileProgram(a[0],l.length,l)!==0){let p=nk(a[0]);throw ro.destroyProgram(a),new Error("NVRTC compile failed for kernel "+t+`:
|
|
73
|
-
`+p+`
|
|
74
|
-
--- source ---
|
|
75
|
-
`+e)}let f=[0n];Vt("nvrtcGetPTXSize",ro.getPTXSize(a[0],f));let h=new Uint8Array(Number(f[0]));return Vt("nvrtcGetPTX",ro.getPTX(a[0],h)),ro.destroyProgram(a),ik.set(s,h),h}function m0(e){let t=ok.get(e);return t||(t=Ei(e.source,e.name),ok.set(e,t),t)}function Ei(e,t){let s=zm(e)+":"+t,n=rk.get(s);if(n)return n;let r=Vm(e,t),i=[null];Vt("cuModuleLoadData",Rt.moduleLoadData(i,r));let o=[null];Vt("cuModuleGetFunction",Rt.moduleGetFunction(o,i[0],t));let a={func:o[0],module:i[0]};return rk.set(s,a),a}var rk,I$,O$,ik,ok,g0=P(()=>{xi();Ai();rk=new Map,I$=`#ifndef INFINITY
|
|
76
|
-
#define INFINITY __int_as_float(0x7f800000)
|
|
77
|
-
#endif
|
|
78
|
-
#ifndef NAN
|
|
79
|
-
#define NAN __int_as_float(0x7fffffff)
|
|
80
|
-
#endif
|
|
81
|
-
`,O$=`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(zm,"hashSource");ik=new Map;c(Vm,"compileToPTX");ok=new WeakMap;c(m0,"getProgramFor");c(Ei,"getProgram")});var lk={};ne(lk,{acquire:()=>Ie,alloc:()=>ak,copyDeviceToHost:()=>Ti,copyDeviceToHostAsync:()=>y0,copyHostToDevice:()=>Go,copyHostToDeviceAsync:()=>Gm,drainPool:()=>qm,free:()=>io,release:()=>He});function ak(e){let t=[0n];return Vt("cuMemAlloc",Rt.memAlloc(t,e)),t[0]}function Go(e,t){Vt("cuMemcpyHtoD",Rt.memcpyHtoD(e,t,t.byteLength))}function Ti(e,t){Vt("cuMemcpyDtoH",Rt.memcpyDtoH(e,t,e.byteLength))}function Gm(e,t){Vt("cuMemcpyHtoDAsync",Rt.memcpyHtoDAsync(e,t,t.byteLength,ee().stream))}function y0(e,t){Vt("cuMemcpyDtoHAsync",Rt.memcpyDtoHAsync(e,t,e.byteLength,ee().stream))}function io(e){Rt.memFree(e)}function Ie(e){let t=Nh.get(e);return t&&t.length>0?t.pop():ak(e)}function He(e,t){let s=Nh.get(t);s||(s=[],Nh.set(t,s)),s.push(e)}function qm(){let e=0;for(let t of Nh.values())for(let s of t)io(s),e++;return Nh.clear(),e}var Nh,ml=P(()=>{xi();Ai();c(ak,"alloc");c(Go,"copyHostToDevice");c(Ti,"copyDeviceToHost");c(Gm,"copyHostToDeviceAsync");c(y0,"copyDeviceToHostAsync");c(io,"free");Nh=new Map;c(Ie,"acquire");c(He,"release");c(qm,"drainPool")});function w0(e){ck=e}function ks(){return ck}function jm(e){uk=e}function js(){return uk}function qc(e){fk=e}function hk(){return fk}function pk(e){_0=e}function b0(){_0&&_0()}var ck,_0,uk,fk,oo=P(()=>{ck=!1,_0=null,uk=!1,fk=!1;c(w0,"setEagerDeferred");c(ks,"isEagerDeferred");c(jm,"setEagerCapturing");c(js,"isEagerCapturing");c(qc,"setCudaGraphArmed");c(hk,"isCudaGraphArmed");c(pk,"setEagerFlushHook");c(b0,"eagerFlush")});function B$(e){let t=Buffer.alloc(8);return t.writeBigUInt64LE(BigInt(e)),t}function mn(e){return new Wm("f32",e)}function S0(e){let t=Buffer.alloc(4);return e instanceof Wm?(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 fr(e,t,s,n,r,i,o=!0){let{stream:a}=ee(),l=[];for(let u of r)l.push(B$(u));for(let u of i)l.push(S0(u));Vt("cuLaunchKernel",Rt.launchKernel(e,t[0],t[1],t[2],s[0],s[1],s[2],n,a,l,null)),o&&!js()&&Vt("cuStreamSynchronize",Rt.streamSynchronize(a))}var Wm,x0=P(()=>{xi();Ai();oo();c(B$,"devicePtrParam");Wm=class{static{c(this,"TypedScalar")}constructor(t,s){this.kind=t,this.value=s}};c(mn,"f32");c(S0,"scalarParam");c(fr,"launch")});import dk from"koffi";function jc(){M$(0)}function gk(){let e=P$();if(e!==0)throw new Error("cudaDeviceSynchronize failed: "+e)}function U$(e){let t=[null],s=L$(t,e);if(s!==0)throw new Error("cudaMalloc failed: "+s);return t[0]}function Km(e,t){let s=mk(e,t,t.byteLength,D$);if(s!==0)throw new Error("cudaMemcpy H2D failed: "+s)}function E0(e,t){let s=mk(e,t,e.byteLength,F$);if(s!==0)throw new Error("cudaMemcpy D2H failed: "+s)}function T0(e){return typeof e=="bigint"?e:typeof e=="number"?BigInt(e):dk.address(e)}function N0(e){let t=A0.get(e);return t&&t.length>0?t.pop():U$(e)}function yk(e,t){let s=A0.get(t);s||(s=[],A0.set(t,s)),s.push(e)}var Rh,M$,L$,mk,Q7,P$,D$,F$,A0,R0=P(()=>{Th();Rh=dk.load(Si($m)),M$=Rh.func("int cudaSetDevice(int d)"),L$=Rh.func("int cudaMalloc(_Out_ void **p, size_t s)"),mk=Rh.func("int cudaMemcpy(void *dst, void *src, size_t n, int kind)"),Q7=Rh.func("int cudaFree(void *p)"),P$=Rh.func("int cudaDeviceSynchronize()"),D$=1,F$=2;c(jc,"setDevice");c(gk,"devSync");c(U$,"devAlloc");c(Km,"devH2D");c(E0,"devD2H");c(T0,"devAddr");A0=new Map;c(N0,"acquireDevice");c(yk,"releaseDevice")});var wk={};ne(wk,{CU_STREAM_CAPTURE_MODE_GLOBAL:()=>$$,CU_STREAM_CAPTURE_MODE_RELAXED:()=>z$,CU_STREAM_CAPTURE_MODE_THREAD_LOCAL:()=>_k,beginEagerCapture:()=>v0,destroyEagerGraph:()=>I0,endEagerCapture:()=>Hm,isGraphCapturing:()=>V$,replay:()=>k0,syncStream:()=>C0});function V$(){return js()}function v0(e=_k){let{stream:t}=ee();jm(!0);try{Vt("cuStreamBeginCapture",Rt.streamBeginCapture(t,e))}catch(s){throw jm(!1),s}}function Hm(){let{stream:e}=ee(),t=[null],s=Rt.streamEndCapture(e,t);jm(!1),Vt("cuStreamEndCapture",s);let n=[null];return Vt("cuGraphInstantiateWithFlags",Rt.graphInstantiate(n,t[0],0n)),{graph:t[0],exec:n[0]}}function k0(e){let{stream:t}=ee();Vt("cuGraphLaunch",Rt.graphLaunch(e,t))}function C0(){let{stream:e}=ee();Vt("cuStreamSynchronize",Rt.streamSynchronize(e))}function I0(e){e&&(e.exec&&Rt.graphExecDestroy(e.exec),e.graph&&Rt.graphDestroy(e.graph))}var $$,_k,z$,Ym=P(()=>{xi();Ai();oo();$$=0,_k=1,z$=2;c(V$,"isGraphCapturing");c(v0,"beginEagerCapture");c(Hm,"endEagerCapture");c(k0,"replay");c(C0,"syncStream");c(I0,"destroyEagerGraph")});var Ak={};ne(Ak,{cublasGemmBatchedDevice:()=>D0,cublasGemmDevice:()=>P0,cublasMatmul:()=>X$,cublasMatmulDevice:()=>sg});import Sk from"koffi";function eg(){if(!vh){let e=[null],t=j$(e);if(t!==0)throw new Error("cublasCreate failed: "+t);vh=e[0],W$(vh,ee().stream),K$(vh,3)}return vh}function B0(e){let t=L0.get(e);if(t&&t.length>0)return t.pop();let s=[null];if(G$(s,e))throw new Error("cudaMalloc failed");return s[0]}function M0(e,t){let s=L0.get(t);s||(s=[],L0.set(t,s)),s.push(e)}function sg(e,t,s,n,r,i,o=!1){Qm(0);let a=eg(),f=xk(a,o?1:0,0,t,e,s,Zm,r,o?s:t,n,s,tg,i,t);if(f!==0)throw new Error("cublasSgemm failed: "+f)}function P0(e,t,s,n,r,i,o,a){Qm(0);let l=eg(),d=H$(l,o?Xm:Jm,r?Xm:Jm,t,e,s,Zm,i,o?s:t,n,r?e:s,tg,a,t);if(d!==0)throw new Error("cublasSgemm failed: "+d)}function D0(e,t,s,n,r,i,o,a,l,u,f,h){Qm(0);let p=eg(),d=o?Xm:Jm,m=u?Xm:Jm,g=o?t:n,_=Y$(p,m,d,s,t,n,Zm,a,u?n:s,BigInt(l),r,g,BigInt(i),tg,f,s,BigInt(h),e);if(_!==0)throw new Error("cublasSgemmStridedBatched failed: "+_)}function X$(e,t,s,n,r,i,o=!1){Qm(0);let a=eg(),l=B0(n.byteLength),u=B0(r.byteLength),f=B0(i.byteLength);try{O0(l,n,n.byteLength,bk),O0(u,r,r.byteLength,bk);let d=xk(a,o?1:0,0,t,e,s,Zm,u,o?s:t,l,s,tg,f,t);if(d!==0)throw new Error("cublasSgemm failed: "+d);q$(),O0(i,f,i.byteLength,J$)}finally{M0(l,n.byteLength),M0(u,r.byteLength),M0(f,i.byteLength)}}var kh,Wc,Qm,G$,O0,lZ,q$,j$,W$,K$,xk,H$,Y$,bk,J$,Jm,Xm,Zm,tg,vh,L0,ng=P(()=>{Ai();Th();kh=Sk.load(Si($m)),Wc=Sk.load(Si(Qv)),Qm=kh.func("int cudaSetDevice(int d)"),G$=kh.func("int cudaMalloc(_Out_ void **p, size_t s)"),O0=kh.func("int cudaMemcpy(void *dst, void *src, size_t n, int kind)"),lZ=kh.func("int cudaFree(void *p)"),q$=kh.func("int cudaDeviceSynchronize()"),j$=Wc.func("int cublasCreate_v2(_Out_ void **h)"),W$=Wc.func("int cublasSetStream_v2(void *h, void *streamId)"),K$=Wc.func("int cublasSetMathMode(void *h, int mode)"),xk=Wc.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)"),H$=Wc.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)"),Y$=Wc.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)"),bk=1,J$=2,Jm=0,Xm=1,Zm=new Float32Array([1]),tg=new Float32Array([0]),vh=null;c(eg,"handle");L0=new Map;c(B0,"acquire");c(M0,"release");c(sg,"cublasMatmulDevice");c(P0,"cublasGemmDevice");c(D0,"cublasGemmBatchedDevice");c(X$,"cublasMatmul")});function Nk(e){F0=e}function Rk(){return F0}function vk(e,t){let s=zm(e)+":"+t,n=Ek.get(s);if(n)return n;let r=Vm(e,t);jc();let i=[null];Vt("cuModuleLoadData",Rt.moduleLoadData(i,r));let o=[null];return Vt("cuModuleGetFunction",Rt.moduleGetFunction(o,i[0],t)),Ek.set(s,o[0]),o[0]}function Q$(e){let t=Buffer.alloc(8);return t.writeBigUInt64LE(e),t}function kk(e,t,s,n,r,i,o=null){let a=[];for(let l of r)a.push(Q$(l));for(let l of i)a.push(S0(l));Vt("cuLaunchKernel",Rt.launchKernel(e,t[0],t[1],t[2],s[0],s[1],s[2],n,o,a,null))}function Z$(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;sg(i,o,a,t[n[l]],t[n[u]],t[n[f]],h)}else{let i=n.map(o=>T0(t[o]));kk(tz(e),r.gridDim,r.blockDim,0,i,e.shapeValues||[],s)}}function tz(e){return Ck.get(e.name)}function ez(e,t,s){jc();let n=ee().stream,r=new Map;for(let l of s)l.kernel.metadata.cublas||r.set(l.name,vk(l.kernel.source,l.kernel.name));Ck=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=Tk.get(e);if(a)for(let l=0;l<e.numSlots;l++)t[l]&&!i.has(l)&&Km(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]=N0(Math.max(f.data.byteLength,1)),i.has(u)||Km(l[u],f.data))}v0();try{for(let f of s)Z$(f,l,n);let u=Hm();a={dptr:l,graph:u.graph,exec:u.exec}}catch(u){try{Hm()}catch{}throw u}Tk.set(e,a)}k0(a.exec),C0();for(let l=0;l<e.numSlots;l++)a.dptr[l]!==null&&i.has(l)&&o.has(l)&&E0(t[l].data,a.dptr[l])}async function Ik(e,t,s){if(F0)return ez(e,t,s);for(let l of s)l.kernel.metadata.cublas||Vm(l.kernel.source,l.kernel.name);jc();let n=new Map;for(let l of s)l.kernel.metadata.cublas||n.set(l.name,vk(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]=N0(f),r.has(l)||Km(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;sg(h,p,d,i[u[m]],i[u[g]],i[u[y]],_)}else{let h=u.map(p=>T0(i[p]));kk(n.get(l.name),f.gridDim,f.blockDim,0,h,l.shapeValues||[])}}gk();let a=new Set(e.argSlots);for(let l=0;l<e.numSlots;l++)i[l]!==null&&r.has(l)&&a.has(l)&&E0(t[l].data,i[l]);for(let l=0;l<e.numSlots;l++)i[l]!==null&&yk(i[l],o[l])}var Ek,Tk,F0,Ck,Ok=P(()=>{xi();g0();R0();Ai();Ym();ng();x0();Ek=new Map,Tk=new WeakMap,F0=!1;c(Nk,"setCudaGraphEnabled");c(Rk,"isCudaGraphEnabled");c(vk,"loadFunctionOnPrimary");c(Q$,"bufferParam");c(kk,"launchOnPrimary");c(Z$,"_runStepGraphed");Ck=null;c(tz,"funcsFor");c(ez,"runCudaPlanGraphed");c(Ik,"runCudaPlan")});var Mk={};ne(Mk,{clearCapturePins:()=>oz,deviceBufferDptr:()=>az,deviceBufferForInplace:()=>rg,deviceBufferForInput:()=>Ni,deviceBufferForOutput:()=>qo,downloadAndValidate:()=>$0,downloadStaticAsync:()=>cz,flushDeferred:()=>ig,hostReadHook:()=>j0,isEagerDeferred:()=>ks,pinResident:()=>z0,releaseAllResident:()=>q0,setCudaGraphArmed:()=>qc,setEagerDeferred:()=>w0,unpinResident:()=>iz,uploadIfStale:()=>gl,uploadStaticAsync:()=>lz});function nz(e){let t=Rr.get(e);if(t)return Rr.delete(e),Rr.set(e,t),t;if(Rr.size>=sz){let s=Rr.keys().next().value,n=Rr.get(s);Rr.delete(s),He(n.dptr,n.bytes)}return t={dptr:Ie(e.byteLength),bytes:e.byteLength,valid:!1},Rr.set(e,t),t}function gl(e){let t=nz(e);return t.valid||(Go(t.dptr,e),t.valid=!0),t.dptr}function $0(e,t){Ti(e,t);let s=Rr.get(e);s&&(s.valid=!0)}function z0(e){Ih.add(e)}function iz(e){Ih.delete(e)}function oz(){Oh.clear()}function az(e){let t=vr.get(e);return t?t.dptr:null}function lz(e,t){Gm(e,t)}function cz(e,t){y0(e,t)}function uz(){return U0||(U0=Math.floor(ee().totalMem*rz)),U0}function V0(e,t){if(js())throw new Error("illegal device sync during CUDA graph capture");Vt("cuStreamSynchronize",Rt.streamSynchronize(ee().stream)),Ti(e,t)}function fz(e){let t=uz();for(let[s,n]of vr){if(Ch+e<=t)break;Ih.has(s)||Oh.has(s)||(n.hostStale&&V0(s,n.dptr),He(n.dptr,n.bytes),Ch-=n.bytes,vr.delete(s))}}function G0(e){let t=vr.get(e);return t?(vr.delete(e),vr.set(e,t)):(js()||fz(e.byteLength),t={dptr:Ie(e.byteLength),bytes:e.byteLength,deviceFresh:!1,hostStale:!1},vr.set(e,t),Ch+=t.bytes),js()&&Oh.add(e),t}function Bk(e,t){js()?Gm(e,t):Go(e,t)}function Ni(e){let t=G0(e);return t.deviceFresh||(Bk(t.dptr,e),t.deviceFresh=!0),t.dptr}function qo(e){let t=G0(e);return t.deviceFresh=!0,t.hostStale=!0,t.dptr}function rg(e){let t=G0(e);return t.deviceFresh||(Bk(t.dptr,e),t.deviceFresh=!0),t.hostStale=!0,t.dptr}function ig(){for(let[e,t]of vr)Ih.has(e)||Oh.has(e)||(He(t.dptr,t.bytes),Ch-=t.bytes,vr.delete(e))}function q0(){for(let[e,t]of vr)t.hostStale&&V0(e,t.dptr),He(t.dptr,t.bytes);vr.clear(),Ch=0,Ih.clear(),Oh.clear();for(let e of Rr.values())He(e.dptr,e.bytes);Rr.clear()}function j0(e){let t=vr.get(e);if(js()){if(t&&t.hostStale)throw new Error("illegal host read of device-resident tensor during CUDA graph capture");return}let s=Rr.get(e);s&&(s.valid=!1),t&&(t.hostStale&&(V0(e,t.dptr),t.hostStale=!1),t.deviceFresh=!1)}var sz,Rr,rz,vr,Ih,Oh,Ch,U0,Bh=P(()=>{ml();Ai();xi();oo();sz=1024,Rr=new Map;c(nz,"_safeEntry");c(gl,"uploadIfStale");c($0,"downloadAndValidate");rz=.5,vr=new Map,Ih=new Set,Oh=new Set,Ch=0,U0=0;c(z0,"pinResident");c(iz,"unpinResident");c(oz,"clearCapturePins");c(az,"deviceBufferDptr");c(lz,"uploadStaticAsync");c(cz,"downloadStaticAsync");c(uz,"_cap");c(V0,"_syncDownload");c(fz,"_evict");c(G0,"_defEntry");c(Bk,"_upload");c(Ni,"deviceBufferForInput");c(qo,"deviceBufferForOutput");c(rg,"deviceBufferForInplace");c(ig,"flushDeferred");c(q0,"releaseAllResident");c(j0,"hostReadHook");pk(ig);w0(!0)});function Se(e){return pz[e]||Float32Array}function og(e,t){if(e===t)return e;let s=Kc.get(e)??0,n=Kc.get(t)??0;return s>=n?e:t}function Xt(e,t){let s=Dk.get(e);s||(s=new Set,Dk.set(e,s)),s.add(t)}function yl(e){return hz(e)}var X,hz,pz,Lk,Pk,Kc,Dk,hr=P(()=>{Ft();X=st,hz=Qi,pz=Object.freeze({[X.F16]:Uint16Array,[X.BF16]:Uint16Array,[X.F32]:Float32Array,[X.F64]:Float64Array,[X.I8]:Int8Array,[X.I16]:Int16Array,[X.I32]:Int32Array,[X.I64]:BigInt64Array,[X.UI8]:Uint8Array,[X.BOOL]:Uint8Array,[X.INDEX]:Int32Array});c(Se,"typedArrayCtor");Lk=[X.F16,X.BF16,X.F32,X.F64],Pk=[X.UI8,X.I8,X.I16,X.I32,X.I64],Kc=new Map;for(let e=0;e<Lk.length;e++)Kc.set(Lk[e],100+e);for(let e=0;e<Pk.length;e++)Kc.set(Pk[e],e);Kc.set(X.BOOL,-1);Kc.set(X.INDEX,50);c(og,"resultDtype");Dk=new Map;c(Xt,"_addCastPair");Xt(X.BOOL,X.I8);Xt(X.BOOL,X.I16);Xt(X.BOOL,X.I32);Xt(X.BOOL,X.I64);Xt(X.BOOL,X.F16);Xt(X.BOOL,X.F32);Xt(X.BOOL,X.F64);Xt(X.UI8,X.I16);Xt(X.UI8,X.I32);Xt(X.UI8,X.I64);Xt(X.UI8,X.F16);Xt(X.UI8,X.F32);Xt(X.UI8,X.F64);Xt(X.I8,X.I16);Xt(X.I8,X.I32);Xt(X.I8,X.I64);Xt(X.I8,X.F16);Xt(X.I8,X.F32);Xt(X.I8,X.F64);Xt(X.I16,X.I32);Xt(X.I16,X.I64);Xt(X.I16,X.F32);Xt(X.I16,X.F64);Xt(X.I32,X.I64);Xt(X.I32,X.F64);Xt(X.F16,X.F32);Xt(X.F16,X.F64);Xt(X.BF16,X.F32);Xt(X.BF16,X.F64);Xt(X.BOOL,X.BF16);Xt(X.UI8,X.BF16);Xt(X.I8,X.BF16);Xt(X.F32,X.F64);c(yl,"dtypeSize")});function bl(){return Fk}function Uk(e){Fk=e}var De,_l,Hc,wl,Yc,kr,Jc,Fk,Cn=P(()=>{rr();De=Object.freeze({CPU:"cpu",GPU:"gpu",WASM:"wasm",WEBGPU:"webgpu",META:"meta",LAZY:"lazy"}),_l=class{static{c(this,"Device")}constructor(t,s=0){this.type=t,this.index=s}dispatchKey(){return yc(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}`}},Hc=new _l(De.CPU),wl=new _l(De.GPU),Yc=new _l(De.WASM),kr=new _l(De.WEBGPU),Jc=new _l(De.META),Fk=Hc;c(bl,"getDefaultDevice");c(Uk,"setDefaultDevice")});function W0(e){return $k.get(e)||$k.get(De.CPU)}var ag,lg,$k,Sl,K0=P(()=>{hr();Cn();ag=class{static{c(this,"CPUAllocator")}allocate(t,s){let n=Se(s);return new n(t)}free(){}},lg=class{static{c(this,"MetaAllocator")}allocate(){return null}free(){}},$k=new Map([[De.CPU,new ag],[De.WASM,new ag],[De.META,new lg],[De.LAZY,new lg]]);c(W0,"getAllocator");Sl=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=W0(n.type),i=yl(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=W0(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=yl(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=W0(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 Cs(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 xs(e){let t=1;for(let s=0;s<e.length;s++)t*=e[s];return t}function H0(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 Xc(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 cg(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=Xc(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 zk(e,t,s){let n=xs(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(xs(o)!==n)return null;return H0(e,t)?{sizes:o,strides:Cs(o),needsCopy:!1}:{sizes:o,strides:Cs(o),needsCopy:!0}}var Yr=P(()=>{c(Cs,"computeStrides");c(xs,"computeNumel");c(H0,"isContiguous");c(Xc,"broadcastShapes");c(cg,"matmulOutputShape");c(zk,"inferReshape")});function Vk(e,t){if(e.length<4)return!1;let s=e.length,n=1,r=s===4?dz:mz;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 Y0,dz,mz,J0=P(()=>{Y0=Object.freeze({CONTIGUOUS:"contiguous",CHANNELS_LAST:"channels_last",PRESERVE:"preserve"}),dz=Object.freeze([0,2,3,1]),mz=Object.freeze([0,2,3,4,1]);c(Vk,"isChannelsLast")});var Fe,ao=P(()=>{rr();Yr();J0();J0();Fe=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]:Cs(n)),this._dtype=i,this._device=o,this._numel=xs(n),this._keySet=nr.fromKey(yc(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===Y0.CONTIGUOUS?(this._contiguousCache===null&&(this._contiguousCache=H0(this._sizes,this._strides)),this._contiguousCache):t===Y0.CHANNELS_LAST?Vk(this._sizes,this._strides):!1}setSizesAndStrides(t,s){this._sizes=Object.freeze([...t]),this._strides=Object.freeze(s?[...s]:Cs(t)),this._numel=xs(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=nr.fromKey(yc(this._device.type));if(this._autogradMeta&&this._autogradMeta.requiresGrad){let s=yc(this._device.type);t=t.add(UN(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 Cr,Qc=P(()=>{Cr=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 qk(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]=Em(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]=qk(e,t,s,n+l*s[r],r+1,i);return a}function gz(){if(!X0)throw new Error("Autograd engine not initialized. Import autograd/engine.js first.");return X0}function jk(e){X0=e}var Gk,Ot,X0,en=P(()=>{ao();Qc();Uc();Gk=new Set(["f16","bf16","i64"]),Ot=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 Fe(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 Gk.has(this._impl.dtype)?Em(this._impl.dtype,s):s}toArray(){let t=this.shape,s=this.strides,n=this._impl.storage.data,r=this._impl.storageOffset,i=Gk.has(this._impl.dtype)?this._impl.dtype:null;return t.length===0?i?Em(i,n[r]):n[r]:qk(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 Fe(this._impl.storage,a,i,o,this._impl.dtype,this._impl.device);return new e(l)}_ensureAutogradMeta(){if(!this._impl.autogradMeta){let t=new Cr;t.versionAtCreation=this._impl.version,this._impl.setAutogradMeta(t)}return this._impl.autogradMeta}backward(t){let{backward:s}=gz();s(this,t)}};c(qk,"_toNestedArray");X0=null;c(gz,"require_autograd_engine");c(jk,"setAutogradEngine")});var sn,xl=P(()=>{K0();sn=class e{static{c(this,"Storage")}constructor(t){this._impl=t}static allocate(t,s,n){return new e(Sl.allocate(t,s,n))}static fromData(t,s){return new e(Sl.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 Wk(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 Kk(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 Hk(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 Yk(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 Jk(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 Q0(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 Xk(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 Qk(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 Zk=P(()=>{c(Wk,"computeTranspose");c(Kk,"computePermute");c(Hk,"computeExpand");c(Yk,"computeSlice");c(Jk,"computeUnsqueeze");c(Q0,"computeSqueeze");c(Xk,"computeNarrow");c(Qk,"computeSelect")});function tC(e){Z0=e.GradMode,Al=e.ReshapeBackward,tS=e.TransposeBackward,eS=e.PermuteBackward,Mh=e.SliceBackward,sS=e.ExpandBackward,nS=e.SelectBackward,rS=e.GradAccumulator}function lo(e,t,s,...n){if(!Z0||!Z0.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&&rS&&(l=new rS(e),r.setGradAccumulator(l)),l&&i.setNextEdge(0,l,0)}let a=new Cr;a.setGradFn(i,0),a.requiresGrad=!0,t._impl.setAutogradMeta(a),t._impl._updateKeySet()}function co(e,t,s){return e.tracer.recordOp(t,[e],s)}function yz(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 Ri(e,t,s,n){let r=new Fe(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 Ot(r)}function Jr(e,t){if(e.isSymbolic&&e.tracer)return co(e,"reshape",{new_shape:yz(t,e.shape)});let s=zk(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=Ri(e,s.sizes,s.strides,0);else{let r=e.isContiguous?e:nC(e);n=Ri(r,s.sizes,s.strides,0)}return Al&&lo(e,n,Al),n}function pr(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 co(e,"transpose",{dim0:a,dim1:l})}let{sizes:n,strides:r}=Wk(e.shape,e.strides,t,s),i=Ri(e,n,r,0);return tS&&lo(e,i,tS,t,s),i}function oS(e,t){if(e.isSymbolic&&e.tracer){let i=e.shape.length,o=t.map(a=>a<0?i+a:a);return co(e,"permute",{dims:o})}let{sizes:s,strides:n}=Kk(e.shape,e.strides,t),r=Ri(e,s,n,0);return eS&&lo(e,r,eS,t),r}function eC(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 co(e,"broadcast_in_dim",{result_shape:a,broadcast_dimensions:l})}let{sizes:s,strides:n}=Hk(e.shape,e.strides,t),r=Ri(e,s,n,0);return sS&&lo(e,r,sS),r}function Lh(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 _z(e,t,s,n,r){if(e.isSymbolic&&e.tracer){let{starts:u,limits:f,strides:h}=Lh(e.shape,t,s,n,r);return co(e,"slice",{starts:u,limits:f,strides:h})}let{sizes:i,strides:o,offsetDelta:a}=Yk(e.shape,e.strides,t,s,n,r),l=Ri(e,i,o,a);if(Mh){let u=Lh(e.shape,t,s,n,r);lo(e,l,Mh,u.d,u.starts[u.d],u.limits[u.d],u.strides[u.d])}return l}function dr(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),co(e,"reshape",{new_shape:a})}let{sizes:s,strides:n}=Jk(e.shape,e.strides,t),r=Ri(e,s,n,0);return Al&&lo(e,r,Al),r}function Ph(e,t){if(e.isSymbolic&&e.tracer){let{sizes:i}=Q0(e.shape,e.strides,t);return co(e,"reshape",{new_shape:i})}let{sizes:s,strides:n}=Q0(e.shape,e.strides,t),r=Ri(e,s,n,0);return Al&&lo(e,r,Al),r}function Dh(e,t,s,n){if(e.isSymbolic&&e.tracer){let{starts:l,limits:u,strides:f}=Lh(e.shape,t,s,s+n,1);return co(e,"slice",{starts:l,limits:u,strides:f})}let{sizes:r,strides:i,offsetDelta:o}=Xk(e.shape,e.strides,t,s,n),a=Ri(e,r,i,o);if(Mh){let l=Lh(e.shape,t,s,s+n,1);lo(e,a,Mh,l.d,l.starts[l.d],l.limits[l.d],l.strides[l.d])}return a}function Ws(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}=Lh(e.shape,l,u,u+1,1),d=co(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}=Qk(e.shape,e.strides,t,s),o=Ri(e,n,r,i);if(nS){let a=e.shape.length,l=t<0?a+t:t,u=s<0?e.shape[l]+s:s;lo(e,o,nS,l,u)}return o}function sC(e){iS=e}function Ir(e){let t=e._impl;return e.isContiguous&&t.storageOffset===0&&t.storage.rawData.length===e.numel?e:nC(e)}function nC(e){if(iS){let g=iS(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=Cs(t),a=e._impl.dtype,l=sn.allocate(i*yl(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 Fe(l,0,t,o,e._impl.dtype,e._impl.device),m=e._impl.autogradMeta;return m&&d.setAutogradMeta(m),new Ot(d)}function rC(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 Jr(eC(Jr(e,o),a),l)}function wz(e,t){let s=e.shape.length,n=t.length<s?[...Array(s-t.length).fill(1),...t]:t;return rC(e,n)}function El(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(Dh(e,r,l,u)),l+=u;return a}function bz(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 El(e,o,r)}function iC(e){let t=e.prototype;t.reshape=function(...s){let n=s.length===1&&Array.isArray(s[0])?s[0]:s;return Jr(this,n)},t.transpose=function(s,n){return pr(this,s,n)},t.permute=function(...s){let n=s.length===1&&Array.isArray(s[0])?s[0]:s;return oS(this,n)},t.expand=function(...s){let n=s.length===1&&Array.isArray(s[0])?s[0]:s;return eC(this,n)},t.slice=function(s,n,r,i){return _z(this,s,n,r,i)},t.unsqueeze=function(s){return dr(this,s)},t.squeeze=function(s){return Ph(this,s)},t.narrow=function(s,n,r){return Dh(this,s,n,r)},t.select=function(s,n){return Ws(this,s,n)},t.repeat=function(...s){let n=s.length===1&&Array.isArray(s[0])?s[0]:s;return rC(this,n)},t.tile=function(...s){let n=s.length===1&&Array.isArray(s[0])?s[0]:s;return wz(this,n)},t.split=function(s,n=0){return El(this,s,n)},t.chunk=function(s,n=0){return bz(this,s,n)},t.contiguous=function(){return Ir(this)},t.t=function(){if(this.ndim!==2)throw new Error("t() expects a 2D tensor");return pr(this,0,1)}}var Z0,Al,tS,eS,Mh,sS,nS,rS,iS,Is=P(()=>{en();ao();xl();hr();Yr();Zk();Qc();Z0=null,Al=null,tS=null,eS=null,Mh=null,sS=null,nS=null,rS=null;c(tC,"_initViewAutograd");c(lo,"_wrapWithAutograd");c(co,"_traceView");c(yz,"_resolveShape");c(Ri,"_makeView");c(Jr,"reshape");c(pr,"transpose");c(oS,"permute");c(eC,"expand");c(Lh,"_sliceAttrs");c(_z,"slice");c(dr,"unsqueeze");c(Ph,"squeeze");c(Dh,"narrow");c(Ws,"select");iS=null;c(sC,"setGpuContiguousHook");c(Ir,"contiguous");c(nC,"_copyContiguous");c(rC,"repeat");c(wz,"tile");c(El,"split");c(bz,"chunk");c(iC,"installViewOps")});var Sz,ug,Bt,vi=P(()=>{Sz=0,ug=class{static{c(this,"Edge")}constructor(t,s){this.node=t,this.inputNr=s}},Bt=class{static{c(this,"AutogradNode")}constructor(t){this._id=Sz++,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 ug(t,s))}setNextEdge(t,s,n){for(;this._nextEdges.length<=t;)this._nextEdges.push(null);this._nextEdges[t]=new ug(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 aC(e){let t=oC.get(e);return t||(t=We.findOp(e),t&&oC.set(e,t)),t}var oC,lC=P(()=>{Xa();oC=new Map;c(aC,"getHandle")});function Os(e,t){let s=t?.dtype??X.F32,n=t?.device??bl(),r=t?.requiresGrad??!1;if(ArrayBuffer.isView(e))return xz(e,t?.shape,s,n,r);if(Array.isArray(e)){let{flat:i,shape:o}=Ez(e),a=t?.shape??o;return Az(i,a,s,n,r)}if(typeof e=="number")return cC(e,s,n,r);throw new Error("Unsupported data type for tensor()")}function jo(e,t,s,n){let r=n?.device??bl(),i=n?.requiresGrad??!1,o=Cs(t),a=sn.fromData(e,r),l=n?.offset??0,u=new Fe(a,l,t,o,s,r),f=new Ot(u);return i&&f.requiresGrad_(!0),f}function fg(e,t){let s=t?.dtype??X.F32,n=t?.device??bl();return cC(e,s,n,t?.requiresGrad??!1)}function cC(e,t,s,n){let r=Se(t),i=new r(1);i[0]=aS.has(t)?Am(t,e):e;let o=sn.fromData(i,s),a=new Fe(o,0,[],[],t,s),l=new Ot(a);return n&&l.requiresGrad_(!0),l}function xz(e,t,s,n,r){let i=Se(s),o=new i(e.length);if(aS.has(s))for(let p=0;p<e.length;p++)o[p]=Am(s,e[p]);else for(let p=0;p<e.length;p++)o[p]=e[p];let a=t??[e.length],l=Cs(a),u=sn.fromData(o,n),f=new Fe(u,0,a,l,s,n),h=new Ot(f);return r&&h.requiresGrad_(!0),h}function Az(e,t,s,n,r){let i=Se(s),o=new i(e.length);if(aS.has(s))for(let h=0;h<e.length;h++)o[h]=Am(s,e[h]);else for(let h=0;h<e.length;h++)o[h]=e[h];let a=Cs(t),l=sn.fromData(o,n),u=new Fe(l,0,t,a,s,n),f=new Ot(u);return r&&f.requiresGrad_(!0),f}function Ez(e){let t=[],s=e;for(;Array.isArray(s);)t.push(s.length),s=s[0];let n=[];return uC(e,n,t,0),{flat:n,shape:t}}function uC(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++)uC(e[r],t,s,n+1)}var aS,Wo=P(()=>{en();ao();xl();hr();Cn();Yr();Uc();c(Os,"tensor");c(jo,"fromBuffer");c(fg,"scalar");aS=new Set(["f16","bf16","i64"]);c(cC,"_fromScalar");c(xz,"_fromTypedArray");c(Az,"_fromFlatArray");c(Ez,"_flattenNested");c(uC,"_recurFlatten")});var es={};ne(es,{abs:()=>Ko,add:()=>ht,argmax:()=>Kh,argmin:()=>Hh,batch_norm:()=>uS,cat:()=>Zc,ceil:()=>Vh,clamp:()=>Gh,clone:()=>Xh,conv2d:()=>fS,cos:()=>$h,div:()=>fe,dot:()=>Jh,embedding:()=>Nz,eq:()=>ki,exp:()=>mr,fill:()=>Tz,floor:()=>zh,gather:()=>jh,ge:()=>ho,gelu:()=>Nl,gt:()=>Zr,index_select:()=>po,layer_norm:()=>cS,le:()=>fo,log:()=>Qr,log_softmax:()=>Il,lt:()=>Cl,matmul:()=>Ye,max:()=>ti,maximum:()=>Xr,mean:()=>gn,min:()=>Wh,minimum:()=>Tl,mul:()=>it,ne:()=>kl,neg:()=>In,one_hot:()=>qh,pad:()=>Ho,pool2d:()=>hg,pow:()=>Or,prod:()=>Yh,relu:()=>uo,remainder:()=>lS,rsqrt:()=>Fh,scatter_add:()=>Yo,sigmoid:()=>Ks,sign:()=>vl,silu:()=>Rl,sin:()=>Uh,softmax:()=>Br,sqrt:()=>gr,stack:()=>yr,sub:()=>jt,sum:()=>ye,tanh:()=>jn,transpose_op:()=>Qh,where:()=>rn});function nn(e,t){return e&&e._impl?e:fg(e,{dtype:t.dtype,device:t.device})}function St(e,...t){let s=aC(e);if(!s)return We.callOp(e,...t);let n=vb(t,s.schema);return We.dispatch(s,n,...t)}function ht(e,t){return St("add",e,nn(t,e))}function jt(e,t){return St("sub",e,nn(t,e))}function it(e,t){return St("mul",e,nn(t,e))}function fe(e,t){return St("div",e,nn(t,e))}function In(e){return St("neg",e)}function Or(e,t){return St("pow",e,nn(t,e))}function lS(e,t){return St("rem",e,nn(t,e))}function Xr(e,t){return St("maximum",e,nn(t,e))}function Tl(e,t){return St("minimum",e,nn(t,e))}function mr(e){return St("exp",e)}function Qr(e){return St("log",e)}function gr(e){return St("sqrt",e)}function Fh(e){return St("rsqrt",e)}function Ko(e){return St("abs",e)}function Uh(e){return St("sin",e)}function $h(e){return St("cos",e)}function jn(e){return St("tanh",e)}function Ks(e){return St("sigmoid",e)}function uo(e){return St("relu",e)}function Nl(e){return St("gelu",e)}function Rl(e){return St("silu",e)}function vl(e){return St("sign",e)}function zh(e){return St("floor",e)}function Vh(e){return St("ceil",e)}function ki(e,t){return St("eq",e,nn(t,e))}function kl(e,t){return St("ne",e,nn(t,e))}function Cl(e,t){return St("lt",e,nn(t,e))}function fo(e,t){return St("le",e,nn(t,e))}function Zr(e,t){return St("gt",e,nn(t,e))}function ho(e,t){return St("ge",e,nn(t,e))}function rn(e,t,s){return St("where",e,t,s)}function Gh(e,t,s){return St("clamp",e,nn(t,e),nn(s,e))}function Ho(e,t,s,n=0){return St("pad",e,nn(n,e),t,s)}function qh(e,t){return St("one_hot",e,t)}function po(e,t,s){return St("index_select",e,s,t)}function jh(e,t,s){return St("gather",e,s,t)}function Yo(e,t,s,n){return St("scatter_add",e,s,n,t)}function ye(e,t,s){return St("sum",e,t,s)}function gn(e,t,s){return St("mean",e,t,s)}function ti(e,t,s){return St("max",e,t,s)}function Wh(e,t,s){return St("min",e,t,s)}function Kh(e,t,s){return St("argmax",e,t,s)}function Hh(e,t,s){return St("argmin",e,t,s)}function Yh(e,t,s){return St("prod",e,t,s)}function Ye(e,t){let s=hS();if(s){let n=s(e,t);if(n!==null)return n}return St("matmul",e,t)}function Jh(e,t){return St("dot",e,t)}function Zc(e,t){return St("cat",e,t)}function yr(e,t){return St("stack",e,t)}function Xh(e){return St("clone",e)}function Tz(e,t){return St("fill",e,t)}function Qh(e,t,s){return St("transpose",e,t,s)}function Br(e,t){return St("softmax",e,t)}function Il(e,t){return St("log_softmax",e,t)}function cS(e,t,s,n,r){return St("layer_norm",e,t,s,n,r)}function uS(e,t,s,n,r,i,o){return St("batch_norm",e,t,s,n,r,i,o)}function fS(e,t,s,n,r,i){return St("conv2d",e,t,s,n,r,i)}function hg(e,t,s,n,r){return St("pool2d",e,t,s,n,r)}function Nz(e,t){return St("embedding",e,t)}var ie=P(()=>{Xa();lC();Wo();Mr();c(nn,"_asTensor");c(St,"_dispatch");c(ht,"add");c(jt,"sub");c(it,"mul");c(fe,"div");c(In,"neg");c(Or,"pow");c(lS,"remainder");c(Xr,"maximum");c(Tl,"minimum");c(mr,"exp");c(Qr,"log");c(gr,"sqrt");c(Fh,"rsqrt");c(Ko,"abs");c(Uh,"sin");c($h,"cos");c(jn,"tanh");c(Ks,"sigmoid");c(uo,"relu");c(Nl,"gelu");c(Rl,"silu");c(vl,"sign");c(zh,"floor");c(Vh,"ceil");c(ki,"eq");c(kl,"ne");c(Cl,"lt");c(fo,"le");c(Zr,"gt");c(ho,"ge");c(rn,"where");c(Gh,"clamp");c(Ho,"pad");c(qh,"one_hot");c(po,"index_select");c(jh,"gather");c(Yo,"scatter_add");c(ye,"sum");c(gn,"mean");c(ti,"max");c(Wh,"min");c(Kh,"argmax");c(Hh,"argmin");c(Yh,"prod");c(Ye,"matmul");c(Jh,"dot");c(Zc,"cat");c(yr,"stack");c(Xh,"clone");c(Tz,"fill");c(Qh,"transpose_op");c(Br,"softmax");c(Il,"log_softmax");c(cS,"layer_norm");c(uS,"batch_norm");c(fS,"conv2d");c(hg,"pool2d");c(Nz,"embedding")});var fC={};ne(fC,{arange:()=>dS,empty:()=>us,eye:()=>mS,full:()=>Qt,linspace:()=>yS,ones:()=>Je,randn:()=>tp,randperm:()=>gS,zeros:()=>Gt});function pg(e){return{dtype:e?.dtype??X.F32,device:e?.device??bl(),requiresGrad:e?.requiresGrad??!1}}function Zh(e,t,s,n){let r=Cs(e),o=xs(e)*yl(t),a=sn.allocate(o,t,s),l=new Fe(a,0,e,r,t,s),u=new Ot(l);return n&&u.requiresGrad_(!0),u}function us(e,t){let{dtype:s,device:n,requiresGrad:r}=pg(t);return Zh(e,s,n,r)}function pS(e,t,s){let n=us(e,t),r=n.data;if(r)return r.fill(s),n;if(n.device&&n.device.type==="meta"){let i=us(e,{dtype:t?.dtype});return i.data.fill(s),i}return n}function Gt(e,t){return pS(e,t,0)}function Je(e,t){return pS(e,t,1)}function Qt(e,t,s){return pS(e,s,t)}function tp(e,t){let s=us(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 dS(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}=pg(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 mS(e,t,s){let n=t??e,{dtype:r,device:i,requiresGrad:o}=pg(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 gS(e,t){let s=t?.dtype??X.I32,n=t?.device??bl(),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 yS(e,t,s,n){let{dtype:r,device:i,requiresGrad:o}=pg(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 Oe=P(()=>{en();ao();xl();hr();Cn();Yr();c(pg,"_defaultOpts");c(Zh,"_makeTensor");c(us,"empty");c(pS,"_filled");c(Gt,"zeros");c(Je,"ones");c(Qt,"full");c(tp,"randn");c(dS,"arange");c(mS,"eye");c(gS,"randperm");c(yS,"linspace")});function _S(e,t){return e<0?t+e:e}var dg,mg,gg,yg,tu,_g,wS=P(()=>{vi();ie();Oe();Is();c(_S,"_normDim");dg=class extends Bt{static{c(this,"CatBackward")}constructor(){super(0)}apply(t){let s=t[0],n=this.opArgs(),r=s.shape.length,i=_S(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(Ir(Dh(s,i,a,f))),a+=f,l++}return o}},mg=class extends Bt{static{c(this,"StackBackward")}constructor(){super(0)}apply(t){let s=t[0],n=this.opArgs(),r=s.shape.length,i=_S(n&&n.length>1?n[1]??0:0,r),o=[],a=0;for(;this.inputMetadata(a);)o.push(Ir(Ws(s,i,a))),a++;return o}},gg=class extends Bt{static{c(this,"ClampBackward")}constructor(){super(3)}apply(t){let s=t[0],[n,r,i]=this.savedTensors(),o=Gt(s.shape,{dtype:s.dtype,device:s.device}),a=ho(n.detach(),r.detach()),l=rn(a,s,o),u=fo(n.detach(),i.detach());return[rn(u,l,o),null,null]}},yg=class extends Bt{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=Dh(a,l,u,o[l])}return[Ir(a),null]}},tu=class e extends Bt{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=_S(a&&a.length>2?a[2]??0:0,o);if(e.#t){let N=e.#t(s,n,i,l);if(N)return[N]}let u=Gt(i,{dtype:s.dtype,device:s.device}),f=u._impl.storage.data,h=u.strides,p=Ir(s),d=p._impl.storage.data,m=p._impl.storageOffset,g=p.shape,y=p.strides,_=Ir(n),w=_._impl.storage.data,S=_._impl.storageOffset,b=g.length,A=new Int32Array(b),x=m;for(let N=0;N<p.numel;N++){let I=0;for(let E=0;E<b;E++){let T=E===l?w[S+A[E]]:A[E];I+=T*h[E]}f[I]+=d[x];for(let E=b-1;E>=0;E--){if(A[E]++,A[E]<g[E]){x+=y[E];break}x-=(g[E]-1)*y[E],A[E]=0}}return[u,null]}},_g=class extends Bt{static{c(this,"WhereBackward")}constructor(){super(3)}apply(t){let s=t[0],[n]=this.savedTensors(),r=Gt(s.shape,{dtype:s.dtype,device:s.device}),i=rn(n.detach(),s,r),o=rn(n.detach(),r,s);return[null,i,o]}}});import Rz from"koffi";function wg(e,t,s){js()?Rt.memcpyDtoDAsync(e,t,s,ee().stream):Rt.memcpyDtoD(e,t,s)}function vz(e,t){js()?Rt.memsetD8Async(e,0,t,ee().stream):Rt.memsetD8(e,0,t)}function dC(){if(ep!==null)return ep;try{let e=Rz.load(Si(Zv));fs={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)")},ep=!0}catch{ep=!1}return ep}function mC(){return dC()}function Bs(e,t){if(t!==0)throw new Error("cuDNN "+e+" failed: "+t+" ("+fs.getErrorString(t)+")")}function sp(){if(!dC())throw new Error("cuDNN not available");let e=ee();if(jc(),!bg){let t=Ol();Bs("create",fs.create(t)),bg=t[0],Bs("setStream",fs.setStream(bg,e.stream))}return bg}function Fz(){if(!bS){let e=sp(),t=Ol();Bs("createDropout",fs.createDropoutDesc(t));let s=[0n];Bs("dropStatesSize",fs.dropoutGetStatesSize(e,s));let n=Ie(Math.max(Number(s[0]),1));Bs("setDropout",fs.setDropoutDesc(t[0],e,0,n,s[0],0n)),bS=t[0]}return bS}function Uz(e,t){let s=Ol();return Bs("createTensor",fs.createTensorDesc(s)),Bs("setTensorNd",fs.setTensorNdDesc(s[0],Sg,e.length,new Int32Array(e),new Int32Array(t))),s[0]}function hC(e,t,s,n){let r=Ol();return Bs("createRNNData",fs.createRNNDataDesc(r)),Bs("setRNNData",fs.setRNNDataDesc(r[0],Sg,Lz,e,t,s,n,null)),r[0]}function $z({inputSize:e,hiddenSize:t,seqLen:s,batch:n,numLayers:r}){let i=`${e}_${t}_${s}_${n}_${r}`,o=pC.get(i);if(o)return o;let a=sp(),l=Ol();Bs("createRNN",fs.createRNNDesc(l)),Bs("setRNN",fs.setRNNDesc(l[0],kz,Cz,Iz,Oz,Bz,Sg,Sg,Mz,e,t,t,r,Fz(),0));let u=l[0],f=new Int32Array(n).fill(s),h=hC(s,n,e,f),p=hC(s,n,t,f),d=Uz([r,n,t],[n*t,t,1]),m=[0n];Bs("wss",fs.getRNNWeightSpaceSize(a,u,m));let g=[0n],y=[0n];Bs("temp",fs.getRNNTempSpaceSizes(a,u,gC,h,g,y));let _=Ie(n*4);return Rt.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},pC.set(i,o),o}function yC(e,t,s,n){let r=sp(),i=Ol(),o=Ol();Bs("cwm",fs.createTensorDesc(i)),Bs("cwb",fs.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];Bs("wparam",fs.getRNNWeightParams(r,e.rd,h,f,t,m,i[0],_,o[0],w));let b=(g?l*p:l*l)*4,A=l*4,x=BigInt(g?d.x2hW:d.h2hW)+BigInt(y*b),N=BigInt(g?d.x2hB:d.h2hB)+BigInt(y*A);n?(wg(x,_[0],b),wg(N,w[0],A)):(wg(_[0],x,b),wg(w[0],N,A))}}}function _C(e,t,s,n,r,i,o,a,l=!0){let u=sp(),f=$z({...s,numLayers:t.length}),h=Ie(f.wssN);yC(f,h,t,!1);let p=Ie(f.workN),d=l?Ie(f.reserveN):0n;return Bs("forward",fs.rnnForward(u,f.rd,l?gC:Pz,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 wC(e){e&&(He(e.weightSpace,e.p.wssN),He(e.workSpace,e.p.workN),e.reserveSpace&&e.reserveSpace!==0n&&He(e.reserveSpace,e.p.reserveN))}function bC(e,t,s,n,r,i,o,a,l,u,f,h){let{p,weightSpace:d,workSpace:m,reserveSpace:g}=e,y=sp(),_=Ie(p.wssN);vz(_,p.wssN),Bs("backwardData",fs.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)),Bs("backwardWeights",fs.rnnBackwardWeights(y,p.rd,Dz,p.devSeq,p.xDesc,t,p.hDesc,n||0n,p.yDesc,s,p.wss,_,p.workSize,m,p.reserveSize,g)),yC(p,_,h,!0),He(d,p.wssN),He(m,p.workN),He(g,p.reserveN),He(_,p.wssN)}var fs,ep,Sg,kz,Cz,Iz,Oz,Bz,Mz,Lz,gC,Pz,Dz,Ol,bg,bS,pC,SS=P(()=>{xi();Ai();R0();ml();Th();oo();c(wg,"d2d");c(vz,"memZero");ep=null;c(dC,"ensure");c(mC,"cudnnAvailable");Sg=0,kz=0,Cz=2,Iz=2,Oz=0,Bz=0,Mz=2,Lz=0,gC=1,Pz=0,Dz=0;c(Bs,"ck");Ol=c(()=>[null],"o2"),bg=null,bS=null;c(sp,"handle");c(Fz,"dropoutDesc");c(Uz,"tensorDesc");c(hC,"dataDesc");pC=new Map;c($z,"plan");c(yC,"packWeights");c(_C,"cudnnLSTMForward");c(wC,"releaseLSTMForward");c(bC,"cudnnLSTMBackward")});var xg={};ne(xg,{GradMode:()=>Lr,enableGrad:()=>AS,noGrad:()=>xS});function xS(e){let t=Jo;Jo=!1;try{return e()}finally{Jo=t}}function AS(e){let t=Jo;Jo=!0;try{return e()}finally{Jo=t}}var Jo,Lr,Ci=P(()=>{Jo=!0,Lr={isEnabled(){return Jo},setEnabled(e){Jo=e}};c(xS,"noGrad");c(AS,"enableGrad")});var mo,Ag=P(()=>{vi();ie();mo=class extends Bt{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=ht(r.grad,s),[]):[]}name(){return"GradAccumulator"}}});function Wn(e){return ks()?Ni(e):gl(e)}function Oi(e,t){if(ks())return qo(e);let s=Ie(e.byteLength);return t.push([e,s]),s}function SC(e){for(let[t,s]of e)Ti(t,s),He(s,t.byteLength)}function ES(e,t,s){let n=new Cr;n.setGradFn(e,s),n.requiresGrad=!0,t._impl.setAutogradMeta(n),t._impl._updateKeySet()}function xC(e,t,s,n=null,r=null){let{inputSize:i,hiddenSize:o,seqLen:a,batch:l}=s,u=t.length,f=[],h=Ii(e),p=Wn(h),d=t.map(j=>({x2hW:Wn(Ii(j.x2h.weight)),x2hB:Wn(Ii(j.x2h.bias)),h2hW:Wn(Ii(j.h2h.weight)),h2hB:Wn(Ii(j.h2h.bias))})),m=n?Ii(n):null,g=r?Ii(r):null,y=n?Wn(m):0n,_=r?Wn(g):0n,w=u*l*o,S=[u,l,o],b=new Float32Array(a*l*o),A=new Float32Array(w),x=new Float32Array(w),N=Oi(b,f),I=Oi(A,f),E=Oi(x,f),T=[e];for(let j of t)T.push(j.x2h.weight,j.x2h.bias,j.h2h.weight,j.h2h.bias);n&&T.push(n,r);let B=!1;for(let j of T)if(j._impl.autogradMeta&&j.requiresGrad){B=!0;break}let k=Lr.isEnabled()&&B,O=_C(p,d,s,y,_,N,I,E,k);SC(f);let D=he(b,[a,l,o],e.dtype,e.device),q=he(A,S,e.dtype,e.device),z=he(x,S,e.dtype,e.device);if(k){let j={xArr:h,yArr:b,hxArr:m,cxArr:g,opts:s,numLayers:u,hasInit:!!n,dtype:e.dtype,device:e.device,inputShape:[...e.shape],stateShape:S,weightShapes:t.map(W=>({x2hW:[...W.x2h.weight.shape],x2hB:[...W.x2h.bias.shape],h2hW:[...W.h2h.weight.shape],h2hB:[...W.h2h.bias.shape]}))},Y=new TS(O,j,T.length);for(let W=0;W<T.length;W++){Y.saveInputMetadata(W,[...T[W].shape],T[W].dtype);let G=T[W]._impl.autogradMeta;if(G&&G.requiresGrad)if(G.gradFn)Y.setNextEdge(W,G.gradFn,G.outputNr||0);else{let H=G.getGradAccumulator();H||(H=new mo(T[W]),G.setGradAccumulator(H)),Y.setNextEdge(W,H,0)}else Y.setNextEdge(W,null,0)}ES(Y,D,0),ES(Y,q,1),ES(Y,z,2)}else wC(O);return[D,q,z]}var Ii,Eg,TS,AC=P(()=>{vi();Qc();Ci();Ag();Mr();Bh();ml();SS();Ii=c(e=>NS(e),"carr"),Eg=c(e=>e.reduce((t,s)=>t*s,1),"prod");c(Wn,"devIn");c(Oi,"devOut");c(SC,"flushOut");TS=class extends Bt{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=Wn(i.xArr),m=Wn(i.yArr),g=i.hasInit?Wn(i.hxArr):0n,y=i.hasInit?Wn(i.cxArr):0n,_=Wn(Ii(s)),w=n?Wn(Ii(n)):0n,S=r?Wn(Ii(r)):0n,b=new Float32Array(o*a*l),A=Oi(b,p),x=null,N=null,I=0n,E=0n;i.hasInit&&(x=new Float32Array(h),N=new Float32Array(h),I=Oi(x,p),E=Oi(N,p));let T=i.weightShapes.map(D=>({x2hW:new Float32Array(Eg(D.x2hW)),x2hB:new Float32Array(Eg(D.x2hB)),h2hW:new Float32Array(Eg(D.h2hW)),h2hB:new Float32Array(Eg(D.h2hB))})),B=T.map(D=>({x2hW:Oi(D.x2hW,p),x2hB:Oi(D.x2hB,p),h2hW:Oi(D.h2hW,p),h2hB:Oi(D.h2hB,p)}));bC(this.fwd,d,m,g,y,_,w,S,A,I,E,B),SC(p);let k=c((D,q)=>he(D,q,i.dtype,i.device),"w"),O=[k(b,i.inputShape)];for(let D=0;D<T.length;D++){let q=T[D],z=i.weightShapes[D];O.push(k(q.x2hW,z.x2hW),k(q.x2hB,z.x2hB),k(q.h2hW,z.h2hW),k(q.h2hB,z.h2hB))}return i.hasInit&&O.push(k(x,i.stateShape),k(N,i.stateShape)),O}};c(ES,"attach");c(xC,"cudnnLSTMOp")});function EC(e){return ks()?Ni(e):gl(e)}function zz(e,t){if(ks())return qo(e);let s=Ie(e.byteLength);return t.push([e,s]),s}function Vz(e){for(let[t,s]of e)Ti(t,s),He(s,t.byteLength)}function TC(e){let t=NC(e);if(t)return{data:e._impl.storage.rawData,lay:t};let s=vS();if(ks()&&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=Ir(e);return{data:n._impl.storage.rawData,lay:NC(n)}}function NC(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 Gz(e,t){let s=new Cr;s.setGradFn(e,0),s.requiresGrad=!0,t._impl.setAutogradMeta(s),t._impl._updateKeySet()}function RC(e,t){if(!e.device||e.device.type!==De.GPU||t.device.type!==De.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=TC(e),l=TC(t),u=a.lay,f=l.lay;if(!u||!f||u.batch!==f.batch)return null;let h=u.batch,p=[],d=EC(a.data),m=EC(l.data),g=new Float32Array(h*r*o),y=zz(g,p);h>1?D0(h,r,o,i,d,u.batchStride,u.trans,m,f.batchStride,f.trans,y,r*o):P0(r,o,i,d,u.trans,m,f.trans,y),Vz(p);let _=[...e.shape.slice(0,s-2),r,o],w=he(g,_,e.dtype,e.device);if(Lr.isEnabled()&&(e._impl.autogradMeta&&e.requiresGrad||t._impl.autogradMeta&&t.requiresGrad)){let S=new RS(e,t),b=[e,t];for(let A=0;A<2;A++){S.saveInputMetadata(A,[...b[A].shape],b[A].dtype);let x=b[A]._impl.autogradMeta;if(x&&x.requiresGrad)if(x.gradFn)S.setNextEdge(A,x.gradFn,x.outputNr||0);else{let N=x.getGradAccumulator();N||(N=new mo(b[A]),x.setGradAccumulator(N)),S.setNextEdge(A,N,0)}else S.setNextEdge(A,null,0)}Gz(S,w)}return w}var RS,vC=P(()=>{vi();Qc();Ci();Ag();Mr();Is();Cn();Bh();ml();ng();ie();c(EC,"devIn");c(zz,"devOut");c(Vz,"flushOut");c(TC,"operandData");c(NC,"operandLayout");RS=class extends Bt{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=Ye(s,r.transpose(o-2,o-1)),l=Ye(n.transpose(i-2,i-1),s);return[a,l]}};c(Gz,"attach");c(RC,"gpuMatmul")});var Ng={};ne(Ng,{adamGraphKernelSource:()=>FC,deviceAdam:()=>UC,deviceClipGradNorm:()=>e2,deviceConcat:()=>MC,deviceContiguous:()=>CS,freeOptimizerDeviceState:()=>LC,isCudaGraphEnabled:()=>Rk,measureCudaKernel:()=>CC,preloadCublas:()=>IC,releaseCudaMemory:()=>s2,runCudaKernel:()=>jz,runCudaKernelResident:()=>kS,runCudaKernelSync:()=>OC,runCudaPlan:()=>Ik,setCudaGraphEnabled:()=>Nk,stepIncKernelSource:()=>DC,teardownAfterFit:()=>n2});function CC(e,t,s=[],n={}){ee();let{func:r}=Ei(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=>Ie(g));try{let g=performance.now(),y=0;for(;(y<o||performance.now()-g<l)&&(fr(r,f,h,p,m,s),!(++y>=u)););let _=[];for(let w=0;w<a;w++){let S=performance.now();fr(r,f,h,p,m,s),_.push(performance.now()-S)}return _}finally{for(let g=0;g<m.length;g++)He(m[g],d[g])}}async function IC(){return np||(np=await Promise.resolve().then(()=>(ng(),Ak))),np}function OC(e,t,s){let n=e.metadata;if(n.cublas){if(!np)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;np.cublasMatmul(f,h,p,t[d],t[m],t[g],y);return}ee();let{func:r}=m0(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=Ie(f.byteLength);Go(h,f),l.push(h)}fr(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)&&Ti(i[f],l[f]);for(let f=0;f<l.length;f++)He(l[f],i[f].byteLength)}function kS(e,t,s){let n=e.metadata;if(n.cublas)throw new Error("cuBLAS kernels are not supported on the eager device-resident path");ee();let{func:r}=m0(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=qz(n.scratch);if(ks()){for(let f=0;f<i.length;f++)l[f]=a.has(f)?qo(i[f]):Ni(i[f]);fr(r,n.gridDim,n.blockDim,n.sharedMemBytes||0,[...l,...u.ptrs],o,!1),kC(u);return}for(let f=0;f<i.length;f++)l[f]=gl(i[f]);fr(r,n.gridDim,n.blockDim,n.sharedMemBytes||0,[...l,...u.ptrs],o,!1);for(let f=0;f<i.length;f++)a.has(f)&&$0(i[f],l[f]);kC(u)}function qz(e){if(!e||e.length===0)return{ptrs:[],bufs:[]};let t=[],s=[];for(let n of e){let r=n.size*Se(n.dtype).BYTES_PER_ELEMENT,i=Ie(r);t.push(i),s.push([i,r])}return{ptrs:t,bufs:s}}function kC(e){for(let[t,s]of e.bufs)He(t,s)}async function jz(e,t,s){e.metadata.cublas&&await IC(),OC(e,t,s)}function Tg(e){return Wz[e]||"float"}function BC(e,t){return{gridDim:[Math.max(Math.ceil(e/256),1),1,1],blockDim:[256,1,1],sharedMemBytes:0,outputIndices:t}}function Kz(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 CS(e,t,s,n,r){let i=Tg(r),o=`gather_${r}`,a=Bi.get(o);a||(a=Kz(i,o),Bi.set(o,a));let l=t.length,u=1;for(let d=0;d<l;d++)u*=t[d];let f=new(Se(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 kS({source:a,name:o,metadata:BC(u,[1])},[e,f,u,l,...h,...p,n|0],null),f}function Hz(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 MC(e,t,s,n,r,i,o){let a=Tg(o),l=`catcopy_${o}`,u=Bi.get(l);u||(u=Hz(a,l),Bi.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],S=d*w*m;kS({source:u,name:l,metadata:BC(S,[1])},[t[_],i,d,w,m,g,y],null),y+=w}}function Yz(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 Jz(e){return`extern "C" __global__ void ${e}(int* t) {
|
|
110
|
-
if (threadIdx.x == 0 && blockIdx.x == 0) t[0] = t[0] + 1;
|
|
111
|
-
}`}function Xz(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 Qz(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 Zz(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 t2(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 e2(e,t,s=1e-6){ee(),go===null&&(go=Ie(8),rp=Ie(4),ip=Ie(8));let n=ee().stream;js()||Go(ip,new Float32Array([t,s])),js()?Rt.memsetD8Async(go,0,8,n):Rt.memsetD8(go,0,8);let r=Ei(Qz("clip_accum"),"clip_accum").func,i=Ei(Zz("clip_coef"),"clip_coef").func,o=Ei(t2("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=rg(u);a.push([h,f]),fr(r,[Math.ceil(f/256),1,1],[256,1,1],256*8,[h,go],[f],!1)}if(a.length!==0){fr(i,[1,1,1],[1,1,1],0,[go,ip,rp],[],!1);for(let[l,u]of a)fr(o,[Math.ceil(u/256),1,1],[256,1,1],0,[l,rp],[u],!1)}}function LC(e){let t=e&&e._state;if(!(!t||typeof t.values!="function"))for(let s of t.values())s&&(s._mDev!==void 0&&(io(s._mDev),delete s._mDev),s._vDev!==void 0&&(io(s._vDev),delete s._vDev),s._tDev!==void 0&&(io(s._tDev),delete s._tDev))}function PC(){go!==null&&(io(go),io(rp),io(ip),go=null,rp=null,ip=null)}function s2(){return q0(),PC(),qm()}function n2(e,t){let s=e&&e.__eagerGraphRunner;if(s){try{I0(s.captured)}catch{}e._cudaGraphPhase=s.phase,delete e.__eagerGraphRunner}if(t)for(let n of t)LC(n);return qc(!1),ig(),PC(),qm()}function DC(){let e="step_inc",t=Bi.get(e);return t||(t=Jz(e),Bi.set(e,t)),{source:t,name:e}}function FC(e="f32"){let t=`adam_graph_${e}`,s=Bi.get(t);return s||(s=Xz(Tg(e),t),Bi.set(t,s)),{source:s,name:t}}function r2(e,t){Go(e,new Int32Array([t|0]))}function UC(e,t,s){if(!ks())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;ee();let a=hk();t._mDev||(t._mDev=Ie(o*4),Rt.memsetD8(t._mDev,0,o*4),t._vDev=Ie(o*4),Rt.memsetD8(t._vDev,0,o*4),a&&(t._tDev=Ie(4),Rt.memsetD8(t._tDev,0,4)),z0(r));let l=rg(r),u=Ni(i);if(js()){if(t._tDev===void 0)throw new Error("CUDA graph Adam requires a device step counter; warmup must run before capture");let d=DC(),m=FC(n),g=Ei(d.source,d.name).func,y=Ei(m.source,m.name).func;return fr(g,[1,1,1],[1,1,1],0,[t._tDev],[],!1),fr(y,[Math.ceil(o/256),1,1],[256,1,1],0,[l,u,t._mDev,t._vDev,t._tDev],[o,mn(s.beta1),mn(s.beta2),mn(s.omb1),mn(s.omb2),mn(s.eps),mn(s.lr),mn(s.wd)],!1),!0}let f=`adam_${n}`,h=Bi.get(f);h||(h=Yz(Tg(n),f),Bi.set(f,h));let{func:p}=Ei(h,f);return fr(p,[Math.ceil(o/256),1,1],[256,1,1],0,[l,u,t._mDev,t._vDev],[o,mn(s.beta1),mn(s.beta2),mn(s.omb1),mn(s.omb2),mn(s.eps),mn(s.stepSize),mn(s.bc2sqrt),mn(s.wd)],!1),a&&t._tDev!==void 0&&r2(t._tDev,t.step),!0}function i2(e){if(!ks()||!e.device||e.device.type!==De.GPU)return null;let t=CS(e._impl.storage.rawData,e.shape,e.strides,e._impl.storageOffset,e.dtype);return he(t,[...e.shape],e.dtype,e.device)}function a2(e,t,s,n){if(!ks()||n!==0||s.length!==2||!e.device||e.device.type!==De.GPU||t.dtype!=="i32")return null;let r=s[0],i=s[1],o=Ir(e),a=Ir(t),l=a.numel;if(o.numel!==l*i)return null;ee();let u=new Float32Array(r*i),{func:f}=Ei(o2,"index_add0"),h=qo(u),p=ee().stream;js()?Rt.memsetD8Async(h,0,r*i*4,p):Rt.memsetD8(h,0,r*i*4);let d=Ni(o._impl.storage.rawData),m=Ni(a._impl.storage.rawData);return fr(f,[Math.ceil(l*i/256),1,1],[256,1,1],0,[d,m,h],[l,i],!1),he(u,[r,i],e.dtype,e.device)}var np,Wz,Bi,go,rp,ip,o2,Rg=P(()=>{Ai();g0();ml();x0();Ok();Bh();oo();Ym();xi();K0();Fm();Mr();Is();wS();Cn();hr();SS();AC();vC();Sl.setHostReadHook(j0);c(CC,"measureCudaKernel");np=null;c(IC,"preloadCublas");c(OC,"runCudaKernelSync");c(kS,"runCudaKernelResident");c(qz,"_acquireScratch");c(kC,"_releaseScratch");c(jz,"runCudaKernel");Wz={f32:"float",f64:"double",i64:"long long",i32:"int",i16:"short",i8:"signed char",u8:"unsigned char",bool:"unsigned char"};c(Tg,"_ctype");Bi=new Map;c(BC,"_meta");c(Kz,"_gatherKernel");c(CS,"deviceContiguous");c(Hz,"_catKernel");c(MC,"deviceConcat");c(Yz,"_adamKernel");c(Jz,"_stepIncKernel");c(Xz,"_adamGraphKernel");c(Qz,"_clipAccumKernel");c(Zz,"_clipCoefKernel");c(t2,"_clipScaleKernel");go=null,rp=null,ip=null;c(e2,"deviceClipGradNorm");c(LC,"freeOptimizerDeviceState");c(PC,"freeClipScratch");c(s2,"releaseCudaMemory");c(n2,"teardownAfterFit");c(DC,"stepIncKernelSource");c(FC,"adamGraphKernelSource");c(r2,"_setDeviceInt");c(UC,"deviceAdam");c(i2,"gpuContiguousTensor");sC(i2);o2=`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(a2,"gpuIndexSelectBackward");tu.setGpuBackward(a2);IS(CS);OS(MC);MS(UC);LS(RC);mC()&&BS(xC);Dm("cuda",CC)});var QC={};ne(QC,{ensureWebGPUEager:()=>d2,flushWebGPUEager:()=>up,instantiateWebGPU:()=>u2,prewarmPipelines:()=>JC,recordWebGPUEager:()=>Bg,resetDevice:()=>l2,runWebGPUKernel:()=>f2,runWebGPUPlan:()=>h2,webgpuEagerOp:()=>y2,webgpuEagerReady:()=>m2,webgpuRNN:()=>b2});function eu(e){switch(Zs(e)){case"i32":return Int32Array;case"u32":return Uint32Array;case"f16":return Uint16Array;default:return Float32Array}}function Ig(e,t,s,n){if(s==="bf16")for(let r=0;r<t.length;r++)e[n+r]=Sm(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 $S(e,t,s,n,r){if(s==="bf16")for(let i=0;i<r;i++)e[i]=xm(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 Mi(e){return Math.ceil(e/4)*4}async function zS(){return Be||ap||(ap=(async()=>{let e=typeof navigator<"u"&&navigator.gpu;if(!e)try{let o=await import("webgpu");jC=o,e=o.create([]),o.globals&&(lp=o.globals.GPUBufferUsage,kg=o.globals.GPUMapMode,Cg=o.globals.GPUShaderStage)}catch{throw new Error('WebGPU not available: install the "webgpu" npm package or run in a browser with WebGPU support')}!lp&&typeof GPUBufferUsage<"u"&&(lp=GPUBufferUsage,kg=GPUMapMode,Cg=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"),Be=await t.requestDevice({requiredLimits:n,requiredFeatures:i}),!$C&&typeof process<"u"&&process.on&&($C=!0,process.on("exit",()=>{Be&&(Be.destroy(),Be=null)})),Be})(),ap)}function su(){return lp}function VS(){return kg.READ}function l2(){Be&&(Be.destroy(),Be=null),ap=null,lp=null,kg=null,Cg=null,jC=null}function c2(e){return e.name==="_shapes"?"uniform":e.mode==="read_write"?"storage":"read-only-storage"}function WC(e,t){let s=e.createShaderModule({code:t.source}),n=[];for(let o of t.metadata.bindings)n.push({binding:o.index,visibility:Cg.COMPUTE,buffer:{type:c2(o)}});let r=e.createBindGroupLayout({entries:n}),i=e.createPipelineLayout({bindGroupLayouts:[r]});return{shaderModule:s,bindGroupLayout:r,pipelineLayout:i}}function KC(e,t){let{shaderModule:s,bindGroupLayout:n,pipelineLayout:r}=WC(e,t);return{pipeline:e.createComputePipeline({layout:r,compute:{module:s,entryPoint:t.name}}),bindGroupLayout:n}}function HC(e,t){let s=Og.get(t);return s||(s=KC(e,t),Og.set(t,s)),s}async function u2(e){let t=await zS(),{pipeline:s,bindGroupLayout:n}=KC(t,e);return{device:t,pipeline:s,bindGroupLayout:n,kernel:e,workgroupSize:e.metadata.workgroupSize,dispatchSize:e.metadata.dispatchSize,bindings:e.metadata.bindings}}function YC(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 f2(e,t,s){let{device:n,pipeline:r,bindGroupLayout:i,bindings:o,dispatchSize:a}=e,l=su(),u=[],f=[],h=YC(o);for(let y=0;y<o.length;y++){let _=o[y];if(_.name==="_shapes"){let T=new Uint32Array(s||[]),B=Math.max(Math.ceil(T.byteLength/16)*16,16),k=n.createBuffer({size:B,usage:l.UNIFORM|l.COPY_DST});n.queue.writeBuffer(k,0,T),u.push(k),f.push({binding:_.index,resource:{buffer:k}});continue}let w=jr(_.dtype),S=eu(_.dtype);if(_.packed){let T=Mi(_.packedSize*w),k=_.mode==="read_write"?l.STORAGE|l.COPY_SRC|l.COPY_DST:l.STORAGE|l.COPY_DST,O=n.createBuffer({size:Math.max(T,4),usage:k,mappedAtCreation:!0}),D=new S(O.getMappedRange());for(let q of _.packed){let z=h.get(q.name),j=t[z];j&&Ig(D,j,q.dtype,q.offset)}O.unmap(),u.push(O),f.push({binding:_.index,resource:{buffer:O}});continue}let b=h.get(_.name),A=t[b],x=Mi(A.length*w),I=_.mode==="read_write"?l.STORAGE|l.COPY_SRC|l.COPY_DST:l.STORAGE|l.COPY_DST,E=n.createBuffer({size:Math.max(x,4),usage:I,mappedAtCreation:!0});Ig(new S(E.getMappedRange()),A,_.dtype,0),E.unmap(),u.push(E),f.push({binding:_.index,resource:{buffer:E}})}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],S=jr(_.dtype),b=eu(_.dtype);if(_.packed)for(let A of _.packed){let x=A.offset*S,N=Mi(A.size*S),I=n.createBuffer({size:N,usage:l.MAP_READ|l.COPY_DST});d.copyBufferToBuffer(w,x,I,0,N),g.push({readBuf:I,tensorIdx:h.get(A.name),size:A.size,dtype:A.dtype,ViewCtor:b})}else{let A=w.size,x=n.createBuffer({size:A,usage:l.MAP_READ|l.COPY_DST});d.copyBufferToBuffer(w,0,x,0,A),g.push({readBuf:x,tensorIdx:h.get(_.name),size:t[h.get(_.name)].length,dtype:_.dtype,ViewCtor:b})}}n.queue.submit([d.finish()]);for(let y of g){await y.readBuf.mapAsync(VS());let _=new y.ViewCtor(y.readBuf.getMappedRange());$S(t[y.tensorIdx],_,y.dtype,0,y.size),y.readBuf.unmap(),y.readBuf.destroy()}for(let y of u)y.destroy()}async function JC(e,t){if(typeof e.createComputePipelineAsync!="function")return;let s=[],n=new Set;for(let r of t){if(!r||n.has(r)||Og.has(r))continue;n.add(r);let{shaderModule:i,bindGroupLayout:o,pipelineLayout:a}=WC(e,r);s.push(e.createComputePipelineAsync({layout:a,compute:{module:i,entryPoint:r.name}}).then(l=>Og.set(r,{pipeline:l,bindGroupLayout:o}),()=>{}))}s.length>0&&await Promise.all(s)}async function h2(e,t,s){let n=await zS(),r=su();await JC(n,s.map(w=>w.kernel));let i=new Set;for(let w of s)for(let S of w.outputSlots)i.add(S);let o=new Array(e.numSlots).fill(null),a=new Array(e.numSlots).fill("f32");for(let w=0;w<e.numSlots;w++){let S=t[w];if(!S)continue;let b=S.dtype||"f32";a[w]=b;let A=Math.max(Mi(S.data.length*jr(b)),4),x=!i.has(w),N=n.createBuffer({size:A,usage:r.STORAGE|r.COPY_DST|r.COPY_SRC,mappedAtCreation:x});x&&(Ig(new(eu(b))(N.getMappedRange()),S.data,b,0),N.unmap()),o[w]=N}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 S=h.encoder,{pipeline:b,bindGroupLayout:A}=HC(n,w.kernel),x=w.inputSlots.concat(w.outputSlots),N=[],I=[],E=0;for(let O of w.kernel.metadata.bindings)if(O.name==="_shapes"){let D=new Uint32Array(w.shapeValues||[]),q=Math.max(Math.ceil(D.byteLength/16)*16,16),z=n.createBuffer({size:q,usage:r.UNIFORM|r.COPY_DST});n.queue.writeBuffer(z,0,D),u.push(z),N.push({binding:O.index,resource:{buffer:z}})}else if(O.packed){let D=jr(O.dtype),q=n.createBuffer({size:Math.max(Mi(O.packedSize*D),4),usage:r.STORAGE|r.COPY_DST|r.COPY_SRC});f.push(q);let z=O.mode==="read_write";for(let j of O.packed){let Y=x[j.argIndex],W=Mi(j.size*D);z?I.push({slot:Y,src:q,srcOff:j.offset*D,bytes:W}):S.copyBufferToBuffer(o[Y],0,q,j.offset*D,W)}N.push({binding:O.index,resource:{buffer:q}})}else N.push({binding:O.index,resource:{buffer:o[x[E++]]}});let T=n.createBindGroup({layout:A,entries:N}),B=S.beginComputePass();B.setPipeline(b),B.setBindGroup(0,T);let k=w.kernel.metadata.dispatchSize;B.dispatchWorkgroups(k[0],k[1],k[2]),B.end();for(let O of I)S.copyBufferToBuffer(O.src,O.srcOff,o[O.slot],0,O.bytes);p()},"encodeStep"),m=e.scanLoops?[...e.scanLoops].sort((w,S)=>w.loopStart-S.loopStart):e.scanLoop?[e.scanLoop]:null;if(m&&m.length){let w=0;for(let S of m){for(;w<S.loopStart;w++)d(s[w]);for(let b of S.carry)h.encoder.copyBufferToBuffer(o[b.initSlot],0,o[b.a],0,b.bytes);for(let b=0;b<S.T;b++){for(let A of S.xs)h.encoder.copyBufferToBuffer(o[A.xsSlot],b*A.stepBytes,o[A.xtSlot],0,A.stepBytes);for(let A=S.loopStart;A<S.loopEnd;A++)d(s[A]);for(let A of S.ys)h.encoder.copyBufferToBuffer(o[A.ytSlot],0,o[A.ysSlot],b*A.stepBytes,A.stepBytes);for(let A of S.carry){let x=o[A.a];o[A.a]=o[A.b],o[A.b]=x}}for(let b of S.carry)h.encoder.copyBufferToBuffer(o[b.a],0,o[b.finalSlot],0,b.bytes);w=S.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 S=t[w],b=Math.max(Mi(S.data.length*jr(a[w])),4),A=n.createBuffer({size:b,usage:r.MAP_READ|r.COPY_DST});g.copyBufferToBuffer(o[w],0,A,0,b),_.push({rb:A,dtype:a[w],size:S.data.length,dst:S.data})}n.queue.submit([g.finish()]);for(let w of _)await w.rb.mapAsync(VS()),$S(w.dst,new(eu(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 XC(e){let t=US.get(e);if(t&&t.length)return t.pop();let s=su();return Be.createBuffer({size:e,usage:s.STORAGE|s.COPY_SRC|s.COPY_DST})}function zC(e,t){let s=US.get(t);s||(s=[],US.set(t,s)),s.push(e)}async function d2(){return await zS(),ei||(ei=Be.createCommandEncoder()),Be}function m2(){return!!Be&&!!ei}function VC(e,t,s){let n=cp.get(e);if(n)return n.buf;let r=su(),i=Math.max(Mi(e.length*jr(t)),4);if(s){let a=Be.createBuffer({size:i,usage:r.STORAGE|r.COPY_SRC|r.COPY_DST,mappedAtCreation:!0});return Ig(new(eu(t))(a.getMappedRange()),e,t,0),a.unmap(),cp.set(e,{buf:a,bytes:i,dtype:t,persistent:s}),a}let o=XC(i);return cp.set(e,{buf:o,bytes:i,dtype:t,persistent:!1}),o}function Bg(e,t,s){let{pipeline:n,bindGroupLayout:r}=HC(Be,e),{bindings:i,dispatchSize:o}=e.metadata,a=YC(i),l=su(),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=Be.createBuffer({size:_,usage:l.UNIFORM|l.COPY_DST});Be.queue.writeBuffer(w,0,y),DS.push(w),u.push({binding:d.index,resource:{buffer:w}});continue}if(d.packed){let y=jr(d.dtype),_=Math.max(Mi(d.packedSize*y),4),w=XC(_);FS.push({buf:w,bytes:_});let S=d.mode==="read_write";for(let b of d.packed){let A=VC(t[a.get(b.name)],b.dtype,!S),x=Mi(b.size*y);S?f.push({pbuf:w,off:b.offset*y,sub:A,bytes:x}):ei.copyBufferToBuffer(A,0,w,b.offset*y,x)}u.push({binding:d.index,resource:{buffer:w}});continue}let m=t[a.get(d.name)],g=VC(m,d.dtype,d.mode!=="read_write");u.push({binding:d.index,resource:{buffer:g}})}let h=Be.createBindGroup({layout:r,entries:u}),p=ei.beginComputePass();p.setPipeline(n),p.setBindGroup(0,h),p.dispatchWorkgroups(o[0],o[1],o[2]),p.end();for(let d of f)ei.copyBufferToBuffer(d.pbuf,d.off,d.sub,0,d.bytes);++PS>=p2&&(Be.queue.submit([ei.finish()]),ei=Be.createCommandEncoder(),PS=0)}async function up(){if(!Be)return;let e=su(),t=[];for(let[s,n]of cp){if(n.persistent)continue;let r=Be.createBuffer({size:n.bytes,usage:e.MAP_READ|e.COPY_DST});ei.copyBufferToBuffer(n.buf,0,r,0,n.bytes),t.push({rb:r,ha:s,buf:n.buf,bytes:n.bytes,dtype:n.dtype})}Be.queue.submit([ei.finish()]),ei=Be.createCommandEncoder(),PS=0;for(let s of DS)s.destroy();DS.length=0,await Promise.all(t.map(s=>s.rb.mapAsync(VS())));for(let s of t)$S(s.ha,new(eu(s.dtype))(s.rb.getMappedRange()),s.dtype,0,s.ha.length),s.rb.unmap(),s.rb.destroy();for(let s of FS)zC(s.buf,s.bytes);FS.length=0;for(let s of t)zC(s.buf,s.bytes),cp.delete(s.ha)}function g2(e,t,s,n,r){let i=`${e.join(",")}|${t.join(",")}|${s}|${n}`,o=GC.get(i);if(o)return o;let a=Zs(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]}},GC.set(i,o),o}function vg(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(g2(e.shape,e.strides,e._impl.storageOffset,e.dtype,e.numel),[t,s],void 0),s}function y2(e,t,s){let n=t.map(vg);n.push(s),Bg(e,n,void 0)}function _2(e,t,s){let n=`lstm|${e}|${t}|${s}`,r=qC.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(${op})
|
|
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 + ${op}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 + ${op}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 + ${op}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 + ${op}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]}},qC.set(n,r),r}function w2(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 b2(e,t,s,n,r){if(s.batch!==1)return null;let i=s.hiddenSize,o=s.seqLen,a=t.length,l=vg(e),u=[],f=[];for(let d=0;d<a;d++){let m=d===0?s.inputSize:i,g=w2(t[d],m,i),y,_;n!=null&&r!=null?(y=vg(Ws(n,0,d)),_=vg(Ws(r,0,d))):(y=new Float32Array(i),_=new Float32Array(i));let w=new Float32Array(o*i),S=new Float32Array(i),b=new Float32Array(i);Bg(_2(m,i,o),[l,g,y,_,w,S,b],void 0),l=w,u.push(S),f.push(b)}let h=he(l,[o,1,i],"f32",kr),p=c(d=>a===1?he(d[0],[1,1,i],"f32",kr):yr(d.map(m=>he(m,[1,i],"f32",kr)),0),"wrapState");return[h,p(u),p(f)]}var Be,ap,lp,kg,Cg,jC,$C,Og,ei,PS,p2,cp,DS,FS,US,GC,op,qC,Mg=P(()=>{Nn();Uc();Mr();Cn();ie();Is();Be=null,ap=null,lp=null,kg=null,Cg=null,jC=null,$C=!1;c(eu,"wgslViewCtor");c(Ig,"packTensorInto");c($S,"unpackTensorFrom");c(Mi,"align4");c(zS,"ensureDevice");c(su,"bufUsage");c(VS,"mapModeRead");c(l2,"resetDevice");c(c2,"bindingBufferType");c(WC,"pipelineParts");c(KC,"createPipeline");Og=new WeakMap;c(HC,"pipelineFor");c(u2,"instantiateWebGPU");c(YC,"buildParamIndex");c(f2,"runWebGPUKernel");c(JC,"prewarmPipelines");c(h2,"runWebGPUPlan");ei=null,PS=0,p2=64,cp=new Map,DS=[],FS=[],US=new Map;c(XC,"acquireStorage");c(zC,"releaseStorage");c(d2,"ensureWebGPUEager");c(m2,"webgpuEagerReady");c(VC,"eagerBufferFor");c(Bg,"recordWebGPUEager");c(up,"flushWebGPUEager");GC=new Map;c(g2,"contigKernel");c(vg,"webgpuEagerInput");c(y2,"webgpuEagerOp");op=256,qC=new Map;c(_2,"lstmKernelWGSL");c(w2,"lstmPackedWeights");c(b2,"webgpuRNN")});var JS={};ne(JS,{getBackend:()=>HS,hasBackend:()=>S2,preloadCudaRuntime:()=>YS,preloadWebGPU:()=>nu,registerBackend:()=>hp});function hp(e,t){KS.set(e,t)}function HS(e){return KS.get(e)||null}function S2(e){return KS.has(e)}function x2(){return GS||(GS=Promise.resolve().then(()=>(qv(),Gv))),GS}function WS(){return qS||(qS=Promise.resolve().then(()=>(Rg(),Ng))),qS}async function YS(){if(fp)return fp;let e=await WS();return e.preloadCublas&&await e.preloadCublas(),fp=e,e}async function nu(){if(jS)return jS;let e=await Promise.resolve().then(()=>(Mg(),QC)),{setWebGPUEagerFn:t,setWebgpuRNN:s}=await Promise.resolve().then(()=>(Mr(),tI));return await e.ensureWebGPUEager(),t(e.webgpuEagerOp),s(e.webgpuRNN),jS=e,e}function ZC(e){let t;try{t=Lm(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]=A2[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 Lg(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 E2(e,t,s=[],n={}){let r=n.warmup??5,i=n.repeat??30,o=ZC(e),a=t.map(h=>new Float32Array(Math.max(1,Math.ceil(h/4)))),l=s||[],u=c(()=>{o.parallel?Lg(o,a,l,0,o.parallel.extent):Lg(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 KS,GS,qS,fp,jS,A2,pp=P(()=>{h0();Fm();KS=new Map;c(hp,"registerBackend");c(HS,"getBackend");c(S2,"hasBackend");GS=null;c(x2,"getWasmPool");qS=null;c(WS,"getCudaRuntime");fp=null;c(YS,"preloadCudaRuntime");jS=null;c(nu,"preloadWebGPU");A2={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(ZC,"instantiateWasm");c(Lg,"runWasmKernel");hp("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}});hp("wasm",{instantiate(e){return ZC(e)},runSync(e,t,s){e.parallel?Lg(e,t,s,0,e.parallel.extent):Lg(e,t,s)},async runAsync(e,t,s){if(e.parallel&&e.parallel.poolSafe){let{runWasmParallel:n}=await x2();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(E2,"measureWasm");Dm("wasm",E2);hp("webgpu",{instantiate(e){return nu().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 nu();await n(e,t,s)},async runPlan(e,t,s){let{runWebGPUPlan:n}=await nu();await n(e,t,s)},isAsync(){return!0}});hp("cuda",{instantiate(e){return{kernel:e}},runSync(e,t,s){if(!fp)throw new Error("CUDA sync runtime not preloaded \u2014 call preloadCudaRuntime() before synchronous execution");fp.runCudaKernelResident(e.kernel,t,s)},async runAsync(e,t,s){let{runCudaKernel:n}=await WS();await n(e.kernel,t,s)},async runPlan(e,t,s){let{runCudaPlan:n}=await WS();await n(e,t,s)},isAsync(){return!0}})});function XS(e){return T2[e]||Float32Array}function N2(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 eI(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 T2,Li,QS,ru,Pg=P(()=>{pp();Ah();T2={f16:Uint16Array,bf16:Uint16Array,f32:Float32Array,f64:Float64Array,i8:Int8Array,i16:Int16Array,i32:Int32Array,i64:BigInt64Array,ui8:Uint8Array,bool:Uint8Array,index:Int32Array};c(XS,"typedArrayCtor");c(N2,"dtypeOfTypedArray");c(eI,"_applyReturnFixups");Li=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(XS(s))(n),t,s)}static fromArray(t,s,n="f32"){let r=XS(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}},QS=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()]}},ru=class e{static{c(this,"RuntimeModule")}constructor(t){this.name=t,this.kernels=new QS,this._instances=new Map}addCompiledKernel(t){this.kernels.register(t.name,t);let s=HS(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 Li?(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 Li?o:new Li(o,[o.length],N2(o))}for(let i of t.intermediates){let o=1;for(let a of i.shape)o*=a;n[i.slot]=new Li(new(XS(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),eI(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)}eI(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 xh(n.name,n.source,{name:n.target},n.metadata));return s}}});var Dg,sI=P(()=>{Dg=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 dp(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 iu,Bl,ZS=P(()=>{sI();_s();ws();ws();c(dp,"countOps");iu=class{static{c(this,"FixedPointGroup")}constructor(t,s,n=8){this.name=t,this.passes=s,this.maxIterations=n}},Bl=class{static{c(this,"PassManager")}constructor(){this.passes=[],this.analysisManager=new Dg,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 Ns("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 $d){let u=i?dp(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(ot.FAILED),n.errors.push(new Ns("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,dp(s)),h===ot.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===ot.FAILED&&(this.analysisManager.invalidateAll(),n.errors.push(new Ns("graphPasses",s.name||"<module>",`pass '${t.name}' failed`,t.name)),o||(l=!0))}else if(t instanceof zt){let u=!1;for(let f of s){if(n.failedFunctions.has(f.name))continue;let h=i?dp(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,dp(f)),d===ot.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===ot.FAILED&&(this.analysisManager.invalidate(f),n.errors.push(new Ns("graphPasses",f.name,`pass '${t.name}' failed`,t.name)),n.failedFunctions.add(f.name))}catch(d){n.errors.push(new Ns("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,dp(f)),d===ot.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===ot.FAILED){this.analysisManager.invalidate(f),n.errors.push(new Ns("graphPasses",f.name,`pass '${t.name}' failed`,t.name)),n.failedFunctions.add(f.name),l=!0;break}}}r.push(u?ot.CHANGED:ot.UNCHANGED),a=u}return this.instruments.length&&this._notify("runAfterPass",t,s,a?ot.CHANGED:ot.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`,ot.UNCHANGED,0,-1,-1),!1}run(t,s={}){let n={verbose:this.trace&&this.trace.level>=dt.VERBOSE,resilient:s.errorMode==="resilient",errors:[],failedFunctions:new Set,anyChanged:!1,passContext:s.passContext||new Ud},r=[];for(let i of this.passes){if(!(i instanceof iu)&&!n.passContext.shouldRun(i))continue;if(i instanceof iu?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 hs(e,t){tx.set(e,t)}function As(e,t,s,n){return e.broadcast(e.scalarConstant(t,s).getResult(0),n,[]).getResult(0)}var tx,ou,ex=P(()=>{_s();di();Ft();ws();tx=new Map;c(hs,"registerDecomposition");ou=class extends zt{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())tx.has(i.opName)&&this._shouldDecompose(i)&&s.push(i);if(s.length===0)return ot.UNCHANGED;let n=new Rs(t),r=[];for(let i of s){if(!i.parentBlock)continue;let o=tx.get(i.opName);n.block=i.parentBlock,n.setInsertionPoint(i),r.push(i.opName),o(i,n)}if(this.trace&&this.trace.level>=dt.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:dt.DEBUG})}return ot.CHANGED}};hs("stop_gradient",e=>{e.replaceAllResultsWith([e.getOperand(0)]),e.erase()});hs("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()});hs("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,S)=>S===r?m:0),y=n.map((w,S)=>S===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()});hs("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=dm(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()});hs("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=dm(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()});hs("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()});hs("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()});hs("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()});hs("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=dm(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)),S=t.broadcast(w.getResult(0),u,f),b=t.mul(d.getResult(0),S.getResult(0)),A=t.broadcast(n,u,[i]),x=t.mul(b.getResult(0),A.getResult(0)),N=t.broadcast(r,u,[i]),I=t.add(x.getResult(0),N.getResult(0));e.replaceAllResultsWith([I.getResult(0)]),e.erase()});hs("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)),S=t.broadcast(n,h,[a]),b=t.mul(w.getResult(0),S.getResult(0)),A=t.broadcast(r,h,[a]),x=t.add(b.getResult(0),A.getResult(0));e.replaceAllResultsWith([x.getResult(0)]),e.erase()});hs("where",(e,t)=>{let s=e.getOperand(0);if(s.type.dtype!==st.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()});hs("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()});hs("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 M(u,st.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),st.I32),_=t.compare(y.getResult(0),p.getResult(0),"eq"),w=t.broadcast(t.scalarConstant(i,l).getResult(0),u,[]),S=t.broadcast(t.scalarConstant(o,l).getResult(0),u,[]),b=t.select(_.getResult(0),w.getResult(0),S.getResult(0));e.replaceAllResultsWith([b.getResult(0)]),e.erase()});c(As,"bcast");hs("elu",(e,t)=>{let s=e.getOperand(0),n=e.getAttr("alpha")??1,r=s.type.dtype,i=s.type.shape,o=As(t,0,r,i),a=t.compare(s,o,"gt").getResult(0),l=As(t,1,r,i),u=t.exp(s).getResult(0),f=t.sub(u,l).getResult(0),h=As(t,n,r,i),p=t.mul(h,f).getResult(0),d=t.select(a,s,p);e.replaceAllResultsWith([d.getResult(0)]),e.erase()});hs("leaky_relu",(e,t)=>{let s=e.getOperand(0),n=e.getAttr("negative_slope")??.01,r=s.type.dtype,i=s.type.shape,o=As(t,0,r,i),a=t.compare(s,o,"gt").getResult(0),l=As(t,n,r,i),u=t.mul(l,s).getResult(0),f=t.select(a,s,u);e.replaceAllResultsWith([f.getResult(0)]),e.erase()});hs("celu",(e,t)=>{let s=e.getOperand(0),n=e.getAttr("alpha")??1,r=s.type.dtype,i=s.type.shape,o=As(t,0,r,i),a=t.maximum(s,o).getResult(0),l=As(t,n,r,i),u=t.div(s,l).getResult(0),f=t.exp(u).getResult(0),h=As(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()});hs("selu",(e,t)=>{let s=e.getOperand(0),n=s.type.dtype,r=s.type.shape,i=1.0507009873554805,o=1.6732632423543772,a=As(t,0,n,r),l=t.compare(s,a,"gt").getResult(0),u=As(t,1,n,r),f=t.exp(s).getResult(0),h=t.sub(f,u).getResult(0),p=As(t,o,n,r),d=t.mul(p,h).getResult(0),m=t.select(l,s,d).getResult(0),g=As(t,i,n,r),y=t.mul(g,m);e.replaceAllResultsWith([y.getResult(0)]),e.erase()});hs("mish",(e,t)=>{let s=e.getOperand(0),n=s.type.dtype,r=s.type.shape,i=As(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()});hs("hardswish",(e,t)=>{let s=e.getOperand(0),n=s.type.dtype,r=s.type.shape,i=As(t,3,n,r),o=As(t,0,n,r),a=As(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()});hs("hardsigmoid",(e,t)=>{let s=e.getOperand(0),n=s.type.dtype,r=s.type.shape,i=As(t,6,n,r),o=As(t,.5,n,r),a=As(t,0,n,r),l=As(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()});hs("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 R2(){if(mp)return mp;mp=new _c;for(let e of ut.allOps())if(e.getCanonicalizationPatterns){let t=e.getCanonicalizationPatterns();if(t)for(let s of t)mp.add(s)}return mp}var mp,Ml,sx=P(()=>{_s();sh();cs();mp=null;c(R2,"getCanonicalizationPatterns");Ml=class extends zt{static{c(this,"CanonicalizePass")}constructor(){super("canonicalize")}run(t,s){let n=R2();return new wc(n).applyPatterns(t,10,this.trace)}}});var Fg,nx,au,nI=P(()=>{cs();Ze();Fg=class{static{c(this,"MemoryEffect")}constructor(t,s){this.kind=t,this.value=s}},nx=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===ys.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===ys.WRITE&&n.push(s[r].op);return n}},au=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=ut.get(o.opName),l=[],u=ys.NONE;if(a&&a.sideEffects?u=a.sideEffects:o.hasSideEffects&&o.hasSideEffects()&&(u=ys.WRITE),u!==ys.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&ys.READ)for(let f=0;f<o.numOperands;f++){let h=new Fg(ys.READ,o.getOperand(f));l.push(h),r(o.getOperand(f),o,h)}if(u&ys.WRITE)for(let f=0;f<o.numResults;f++){let h=new Fg(ys.WRITE,o.getResult(f));l.push(h),r(o.getResult(f),o,h)}}s.set(o,l)}return new nx(s,n)}}});var v2,Xo,rx=P(()=>{v2={ceildiv:"ceilDiv"},Xo=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[v2[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 ix,Qo,Ug=P(()=>{cs();Ft();rx();ix=class{static{c(this,"ShapeResult")}constructor(t){this.shapes=t}shapeOf(t){return this.shapes.get(t)||null}},Qo=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!==nt)return f;if(u!==void 0){let p=i.get(`${u}:${h}`);if(p)return Xo.var(p)}return Xo.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=ut.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 ix(n)}}});var Pi,ox=P(()=>{_s();nI();Ug();ws();Pi=class extends zt{static{c(this,"DCEPass")}constructor(){super("dce"),this.preservedAnalyses=new Set([Qo]),this.requiredAnalyses=[au]}run(t,s){let n=!1,r=s?s.getAnalysis(au,t):au.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>=dt.DEBUG&&o>0&&this.trace.emit({type:"pass_detail",passName:this.name,erasedCount:o,level:dt.DEBUG}),n?ot.CHANGED:ot.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 $g,zg,rI=P(()=>{$g=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)}},zg=class e{static{c(this,"ScheduleTrace")}constructor(){this.steps=[]}record(t,s){let n=new $g(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($g.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 xe(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)xe(s,t);return;case"BufferStoreNode":if(e.indices)for(let s of e.indices)xe(s,t);xe(e.value,t);return;case"MathOpNode":xe(e.a,t),e.b&&xe(e.b,t);return;case"CompareNode":xe(e.a,t),xe(e.b,t);return;case"CastNode":xe(e.expr,t);return;case"CallExternNode":for(let s of e.args)xe(s,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"LetStmtNode":xe(e.value,t),xe(e.body,t);return;default:return}}function Vg(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)xe(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 k2(e,t){let s=new Set;if(xe(t.body,s),t.initBody&&xe(t.initBody,s),!s.has(e))return!1;let n=new Set;return Vg(t,n),!n.has(e)}function Ll(e,t){if(!(!e||typeof e!="object"))switch(e.type){case"BlockNode":t.push(e),Ll(e.body,t),e.initBody&&Ll(e.initBody,t);return;case"ForNode":case"AllocateNode":case"LetStmtNode":Ll(e.body,t);return;case"SeqNode":for(let s of e.stmts)Ll(s,t);return;case"IfThenElseNode":Ll(e.thenBody,t),e.elseBody&&Ll(e.elseBody,t);return;default:return}}function ax(e){let t=e.loopVar.name,s=[];Ll(e.body,s);for(let n of s)if(k2(t,n))return n.name;return null}var lx=P(()=>{c(xe,"collectVarsUsed");c(Vg,"collectWriteIndexVars");c(k2,"loopVarIsReductionOf");c(Ll,"collectBlocksUnder");c(ax,"loopCarriesReduction")});var lu,cx=P(()=>{te();lx();lu=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===K.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===K.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===K.PARALLEL){let o=t.extent;o&&o.type==="IntImmNode"&&!s.parallelExtents.has(o.value)&&s.parallelExtents.set(o.value,n)}(t.kind===K.PARALLEL||t.kind===K.THREAD_BINDING)&&e._checkNoNestedParallel(t.body,n,s);let r=t.kind===K.PARALLEL||t.kind===K.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===K.THREAD_BINDING&&t.threadTag&&s.threadBindings.delete(t.threadTag)}static _checkNoNestedParallel(t,s,n){t&&t.type==="ForNode"&&t.kind===K.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;Vg(t,n);let r=new Set;xe(t.body,r),xe(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;xe(t.body,n),t.initBody&&xe(t.initBody,n);let r=new Set;Vg(t,r);for(let i of s.parLoops)if(n.has(i.varName)&&!r.has(i.varName)){let o=i.kind===K.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 Pl,cu,ux=P(()=>{te();Pl=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)}},cu=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 Pl(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 Pl(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!==K.RECURRENCE).reverse():[]}replaceLoop(t,s,n){let r=this._nodeToSRef.get(t);if(!r)return;let i=r.parent,o=new Pl(s,i),a=new Pl(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 Pl(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 C2(e){let t=new Set,s=new Set;return Es(e.body,t,s),e.initBody&&Es(e.initBody,t,s),{reads:t,writes:s,accesses:I2(e)}}function iI(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 I2(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:iI(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:iI(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 oI(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 O2(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))&&oI(n.ranges,r.ranges))return!0;return!1}function Es(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)Es(n,t,s);return;case"BufferStoreNode":if(e.buffer&&s.add(e.buffer.name),e.indices)for(let n of e.indices)Es(n,t,s);Es(e.value,t,s);return;case"MathOpNode":Es(e.a,t,s),e.b&&Es(e.b,t,s);return;case"CompareNode":Es(e.a,t,s),Es(e.b,t,s);return;case"CallExternNode":for(let n of e.args)Es(n,t,s);return;case"IfThenElseNode":Es(e.condition,t,s),Es(e.thenBody,t,s),e.elseBody&&Es(e.elseBody,t,s);return;case"CastNode":Es(e.expr,t,s);return;case"LetStmtNode":Es(e.value,t,s),Es(e.body,t,s);return;case"SeqNode":for(let n of e.stmts)Es(n,t,s);return;case"ForNode":Es(e.body,t,s);return;case"BlockNode":e.initBody&&Es(e.initBody,t,s),Es(e.body,t,s);return;default:return}}var fx,hx,Gg,aI=P(()=>{fx=Object.freeze({RAW:"read-after-write",WAR:"write-after-read",WAW:"write-after-write"}),hx=class{static{c(this,"Dependency")}constructor(t,s,n,r){this.src=t,this.dst=s,this.kind=n,this.bufferName=r}};c(C2,"collectBufferAccesses");c(iI,"indexRanges");c(I2,"collectRegions");c(oI,"rangesOverlap");c(O2,"accessesConflict");c(Es,"visitExprTree");Gg=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,C2(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||!oI(a.ranges,l.ranges))continue;let u=null;if(a.kind==="write"&&l.kind==="read"?u=fx.RAW:a.kind==="write"&&l.kind==="write"?u=fx.WAW:a.kind==="read"&&l.kind==="write"&&(u=fx.WAR),!u)continue;let f=`${u}:${a.name}`;o.has(f)||(o.add(f),i.push(new hx(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:!O2(n.accesses,r.accesses)}}});var px,dx,qg,lI=P(()=>{ux();aI();te();px=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}},dx=class{static{c(this,"BlockBinding")}constructor(t,s,n,r){this.blockSRef=t,this.iterVars=s,this.readBuffers=n,this.writeBuffers=r}},qg=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 cu(this.primFunc),this._depAnalysis=new Gg(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 px(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 dx(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===K.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 pe(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=pe(e.a,t,s),e.b&&(e.b=pe(e.b,t,s));break;case"CompareNode":e.a=pe(e.a,t,s),e.b=pe(e.b,t,s);break;case"BufferLoadNode":for(let n=0;n<e.indices.length;n++)e.indices[n]=pe(e.indices[n],t,s);break;case"BufferStoreNode":for(let n=0;n<e.indices.length;n++)e.indices[n]=pe(e.indices[n],t,s);e.value=pe(e.value,t,s);break;case"CallExternNode":for(let n=0;n<e.args.length;n++)e.args[n]=pe(e.args[n],t,s);break;case"CastNode":e.expr=pe(e.expr,t,s);break;case"IfThenElseNode":e.condition=pe(e.condition,t,s),e.thenBody=pe(e.thenBody,t,s),e.elseBody&&(e.elseBody=pe(e.elseBody,t,s));break;case"ForNode":e.body=pe(e.body,t,s);break;case"BlockNode":e.body=pe(e.body,t,s),e.initBody&&(e.initBody=pe(e.initBody,t,s));for(let n=0;n<e.iterVars.length;n++)e.iterVars[n].binding&&(e.iterVars[n].binding=pe(e.iterVars[n].binding,t,s));break;case"SeqNode":for(let n=0;n<e.stmts.length;n++)e.stmts[n]=pe(e.stmts[n],t,s);break;case"LetStmtNode":e.value=pe(e.value,t,s),e.body=pe(e.body,t,s);break;case"BlockRealizeNode":e.binding&&(e.binding=pe(e.binding,t,s));break}return e}function Ms(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=Ms(e.a,t,s,n),e.b&&(e.b=Ms(e.b,t,s,n));break;case"CompareNode":e.a=Ms(e.a,t,s,n),e.b=Ms(e.b,t,s,n);break;case"BufferLoadNode":for(let r=0;r<e.indices.length;r++)e.indices[r]=Ms(e.indices[r],t,s,n);break;case"BufferStoreNode":for(let r=0;r<e.indices.length;r++)e.indices[r]=Ms(e.indices[r],t,s,n);e.value=Ms(e.value,t,s,n);break;case"CallExternNode":for(let r=0;r<e.args.length;r++)e.args[r]=Ms(e.args[r],t,s,n);break;case"CastNode":e.expr=Ms(e.expr,t,s,n);break;case"IfThenElseNode":e.condition=Ms(e.condition,t,s,n),e.thenBody=Ms(e.thenBody,t,s,n),e.elseBody&&(e.elseBody=Ms(e.elseBody,t,s,n));break;case"ForNode":e.body=Ms(e.body,t,s,n);break;case"BlockNode":e.body=Ms(e.body,t,s,n),e.initBody&&(e.initBody=Ms(e.initBody,t,s,n));break;case"SeqNode":for(let r=0;r<e.stmts.length;r++)e.stmts[r]=Ms(e.stmts[r],t,s,n);break;case"LetStmtNode":e.value=Ms(e.value,t,s,n),e.body=Ms(e.body,t,s,n);break}return e}function fu(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]&&fu(e[s],t))return!0;if(e.args){for(let s of e.args)if(fu(s,t))return!0}if(e.indices){for(let s of e.indices)if(fu(s,t))return!0}if(e.stmts){for(let s of e.stmts)if(fu(s,t))return!0}return!1}function Ut(e){if(!e||typeof e!="object")return e;if(Array.isArray(e))return e.map(Ut);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=Ut(e.min),t.extent=Ut(e.extent),t.kind=e.kind,t.body=Ut(e.body),t.threadTag=e.threadTag,t._setChild("body",t.body);break;case"BlockNode":t.name=e.name,t.iterVars=e.iterVars.map(Ut),t.reads=e.reads,t.writes=e.writes,t.body=Ut(e.body),t.initBody=e.initBody?Ut(e.initBody):null,t._setChild("body",t.body),t._setChild("initBody",t.initBody);break;case"SeqNode":t.stmts=e.stmts.map(Ut),t._setChildren("stmts",t.stmts);break;case"IfThenElseNode":t.condition=Ut(e.condition),t.thenBody=Ut(e.thenBody),t.elseBody=e.elseBody?Ut(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(Ut),t.value=Ut(e.value);break;case"BufferLoadNode":t.buffer=e.buffer,t.indices=e.indices.map(Ut);break;case"BlockRealizeNode":t.iterVar=e.iterVar,t.binding=Ut(e.binding);break;case"MathOpNode":t.op=e.op,t.a=Ut(e.a),t.b=Ut(e.b);break;case"CompareNode":t.direction=e.direction,t.a=Ut(e.a),t.b=Ut(e.b);break;case"CastNode":t.expr=Ut(e.expr),t.fromDtype=e.fromDtype,t.toDtype=e.toDtype;break;case"CallExternNode":t.externName=e.externName,t.args=e.args.map(Ut),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 Dl(e){return e.type==="IntImmNode"?e.value:null}function uu(e,t="int32"){return new ve(`${e}_${M2++}`,t)}var B2,M2,On,gp=P(()=>{te();rl();rI();cx();lI();ux();lx();B2=new Set(["+","*","min","max"]);c(pe,"substituteVar");c(Ms,"replaceBufferLoads");c(fu,"loadsBuffer");c(Ut,"cloneExprTree");c(Dl,"getConstExtent");M2=0;c(uu,"freshVar");On=class{static{c(this,"Schedule")}constructor(t){this.func=t,this.trace=new zg,this.state=new qg(t),this._replaying=!1,this._srefTree=new cu(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=Dl(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=uu(`${t.loopVar.name}_o`),o=uu(`${t.loopVar.name}_i`),a=t.loopVar.name,l=Ut(t.body),u=new Tt(o,new U(0),new U(s),t.kind,l,t.threadTag);if(n%s!==0){let p=new $("+",new $("*",i,new U(s)),o),d=new $("<",p,new U(n)),m=new Zt(d,u.body);u.body=m,u._setChild("body",m)}let h=new Tt(i,new U(0),new U(r),t.kind,u,t.threadTag);return pe(u.body,a,()=>new $("+",new $("*",i,new U(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=Dl(t.extent),r=Dl(s.extent);if(n===null||r===null)throw new Error("Cannot fuse loops with non-constant extents");let i=n*r,o=uu(`${t.loopVar.name}_${s.loopVar.name}_fused`),a=t.loopVar.name,l=s.loopVar.name,u=new Tt(o,new U(0),new U(i),t.kind,s.body);return pe(u.body,a,()=>new $("/",o,new U(r))),pe(u.body,l,()=>new $("%",o,new U(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(Dl(t.extent)===null)throw new Error("Cannot vectorize loop with non-constant extent");let n=ax(t);if(n!==null)throw new Error(`Cannot vectorize reduction loop '${t.loopVar.name}' (loop-carried dependency in block '${n}')`);t.kind=K.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=K.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=ax(t);if(s!==null)throw new Error(`Cannot parallelize reduction loop '${t.loopVar.name}' (loop-carried dependency in block '${s}')`);t.kind=K.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=K.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(q=>q.loopVar.name===s);if(!o)throw new Error(`rfactor: reduction loop '${s}' not found for block '${t}'`);let a=Dl(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(q=>q&&q.type==="BufferLoadNode"&&q.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(!B2.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 U(0),g=i.filter(q=>q.loopVar.name!==s),y=a/n,_=new oe(`${u.name}_rf`,[n,...u.shape],u.dtype,u.scope),w=uu(`${s}_rfi`),S=uu(`${s}_rfo`),b=uu(`${s}_rfp`),A=c(q=>[q,...f.map(Ut)],"cfIdx"),x=pe(Ut(d),s,()=>new $("+",new $("*",S,new U(n)),w)),N=new rt(_,A(w),new $(h,new Z(_,A(w)),x)),I=new rt(_,A(w),Ut(m)),E=new lt(`${t}_rf_p`,[],r.reads.map(q=>({buffer:q.buffer})),[{buffer:_}],N,I),T=new Tt(S,new U(0),new U(y),K.SERIAL,E);T=new Tt(w,new U(0),new U(n),K.SERIAL,T);for(let q=g.length-1;q>=0;q--)T=new Tt(g[q].loopVar,new U(0),Ut(g[q].extent),K.SERIAL,T);let B=new rt(u,f.map(Ut),new $(h,new Z(u,f.map(Ut)),new Z(_,A(b)))),k=new rt(u,f.map(Ut),Ut(m)),O=new lt(`${t}_rf_c`,[],[{buffer:_}],[{buffer:u}],B,k),D=new Tt(b,new U(0),new U(n),K.SERIAL,O);for(let q=g.length-1;q>=0;q--)D=new Tt(g[q].loopVar,new U(0),Ut(g[q].extent),K.SERIAL,D);return this._replaceNode(i[0],new At([T,D])),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)xe(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 rt(i,r.indices.map(Ut),Ut(s.initBody.value)),h=new lt(`${t}_init`,[],[],[{buffer:i}],u);for(let m=a.length-1;m>=0;m--)h=new Tt(a[m].loopVar,new U(0),Ut(a[m].extent),K.SERIAL,h);let d=new lt(`${t}_upd`,[],s.reads.map(m=>({buffer:m.buffer})),[{buffer:i}],Ut(r));for(let m=n.length-1;m>=0;m--)d=new Tt(n[m].loopVar,new U(0),Ut(n[m].extent),K.SERIAL,d);this._replaceNode(n[0],new At([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 oe(`${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 ve(`${l.name}_o${_}`,"int32")),f=new rt(a,u,new Z(l,u)),p=new lt(`${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 Tt(u[y],new U(0),new U(a.shape[y]),K.SERIAL,p);let d=i[0],m=new At([]),g=new tn(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(fu(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(fu(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=Ut(l);for(let y=0;y<o.length;y++){let _=f[y];g=pe(g,o[y],()=>new ve(_,"int32"))}for(let y=0;y<o.length;y++){let _=m.indices[y];g=pe(g,f[y],()=>Ut(_))}return g},"makeRepl");if(Ms(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 At([])),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=Dl(a.extent),u=Dl(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=Ut(a.body);pe(f,a.loopVar.name,()=>i.loopVar),this._replaceNode(a,new At([]));let h=i.body;if(h&&h.type==="SeqNode")n?h.stmts.unshift(f):h.stmts.push(f);else{let p=n?new At([f,h]):new At([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 oe(`${s}_${t}_cache`,[...a.shape],a.dtype,n),u=a.shape.map((y,_)=>new ve(`${l.name}_i${_}`,"int32")),f=new rt(l,u,new Z(a,u)),p=new lt(`${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 Tt(u[y],new U(0),new U(a.shape[y]),K.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 At([p]),g=new tn(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)xe(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=Ut(u.body),h=Ut(r.body);for(let m=0;m<o.length;m++){let g=l[m].loopVar.name;h=pe(h,o[m].loopVar.name,()=>new ve(g,"int32"))}let p=new lt(`${s}_fused`,[],r.reads.map(m=>({buffer:m.buffer})),r.writes.map(m=>({buffer:m.buffer})),h),d=new At([f,p]);for(let m=l.length-1;m>=0;m--){let g=l[m];d=new Tt(g.loopVar,new U(0),Ut(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 lu.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 Ue(e,t){let s=mx.get(e);return s||(s=new Map,L2(e.body,s,[]),mx.set(e,s)),s.get(t)||null}function cI(e){e&&mx.delete(e)}function uI(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 fI(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)uI(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(uI(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 L2(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=fI(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 hI(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 Tx(e){let t=[e.body];for(;t.length>0;){let s=t.pop();if(s){if(s.type==="BlockNode"&&(s.initBody!==null||fI(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 P2(e){let t=[e.body];for(;t.length>0;){let s=t.pop();if(s){if(s.type==="ForNode"&&s.kind===K.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 Nx(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"),P2(e.func)||e.bindThread(i,"blockIdx.x")}else e.bindThread(t,"threadIdx.x")}function hu(e,t){return t.reductionLoopVars?t.reductionLoopVars.has(e.loopVar.name):!1}function Rx(e,t){return e.body===t}function D2(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 si,mx,gx,yx,_x,wx,bx,Sx,xx,Ax,Ex,yo,Zo=P(()=>{_i();te();si=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")}},mx=new WeakMap;c(Ue,"classifyBlock");c(cI,"invalidateClassifyCache");c(uI,"collectVarNames");c(fI,"computeReductionLoopVars");c(L2,"collectBlockInfo");c(hI,"hasMultipleBlocks");gx=class extends si{static{c(this,"ElementwiseCPURule")}constructor(){super("elementwise_cpu")}matches(t,s,n){if(n.kind!==Pe.CPU)return!1;let r=Ue(t,s);if(!r||r.hasReduction||r.loopCount<1||r.loops.length>0&&hI(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(Tx,"primFuncHasReduction");c(P2,"primFuncHasRecurrence");c(Nx,"bindFusedSpatialGPU");yx=class extends si{static{c(this,"ElementwiseGPURule")}constructor(){super("elementwise_gpu")}matches(t,s,n){if(!n.isGPU())return!1;let r=Ue(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&&Rx(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(Tx(t.func)&&a<=l){t.bindThread(i,"threadIdx.x");return}Nx(t,i,n)}},_x=class extends si{static{c(this,"ReductionCPURule")}constructor(){super("reduction_cpu")}matches(t,s,n){if(n.kind!==Pe.CPU)return!1;let r=Ue(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=Ue(t.func,s);for(let l of r)a&&hu(l,a)?o.push(l):i.push(l);i.length>0&&t.parallelize(i[0])}},wx=class extends si{static{c(this,"ReductionGPURule")}constructor(){super("reduction_gpu")}matches(t,s,n){if(!n.isGPU())return!1;let r=Ue(t,s);return r?r.hasReduction:!1}apply(t,s,n){let r=t.getLoops(s);if(r.length===0)return;let i=[],o=Ue(t.func,s);for(let l of r)(!o||!hu(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&&Rx(a,f)&&(a=t.fuseLoops(a,f))}Nx(t,a,n)}},bx=class extends si{static{c(this,"MatmulTiledCPURule")}constructor(){super("matmul_tiled_cpu")}matches(t,s,n){if(n.kind!==Pe.CPU)return!1;let r=Ue(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])}},Sx=class extends si{static{c(this,"MatmulTiledGPURule")}constructor(){super("matmul_tiled_gpu")}matches(t,s,n){if(!n.isGPU())return!1;let r=Ue(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=Ue(t.func,s),o=r.filter(l=>!i||!hu(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&&Rx(a,f)&&(a=t.fuseLoops(a,f))}Nx(t,a,n)}},xx=class extends si{static{c(this,"ElementwiseWasmRule")}constructor(){super("elementwise_wasm")}matches(t,s,n){if(n.kind!==Pe.WASM)return!1;let r=Ue(t,s);return!(!r||r.hasReduction||r.loopCount<1||r.loops.length>0&&hI(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)}},Ax=class extends si{static{c(this,"ReductionWasmRule")}constructor(){super("reduction_wasm")}matches(t,s,n){if(n.kind!==Pe.WASM)return!1;let r=n.numCores>1,i=n.supportsSimd&&n.supportsSimd();if(!r&&!i)return!1;let o=Ue(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=Ue(t.func,s),o=[],a=[];for(let l of r)!i||!hu(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)}}},Ex=class extends si{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(hu,"isReductionLoop");c(Rx,"findDirectChild");yo=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 bx,new Sx,new _x,new wx,new Ax,new gx,new yx,new xx,new Ex]}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),cI(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){cI(t.func);let s=D2(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(D2,"collectAllBlockNames")});function F2(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 U2(e){let t=dI.get(e);return t||(t=new ru("jit_"+e),dI.set(e,t)),t}function z2(e){let t=1;for(let s of e.shape)t*=typeof s=="number"&&s>0?s:1;return Math.max(t,1)}function V2(e,t,s){let n=[];for(let[,r]of s.bufferMap)n.push(new(Se(r.dtype))(z2(r)));e.run(t.name,...n)}function G2(e,t,s,n){try{let r=mh(e,t);if(r.shapeParams&&r.shapeParams.length>0)return null;new yo(t).applyToAllBlocks(new On(r));let i=s.compile(r);return n.addCompiledKernel(i),t.isWebGPU()||V2(n,i,r),i}catch{return null}}function j2(e,t,s){let n=t.map(a=>new M(a.shape,a.dtype)),r=e+"_jit_"+q2++,i=ah(r,n,[],(a,l)=>{let u;if($2[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=K2(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 mI[e]=="function"?u=mI[e](a,l,s):typeof a[e]=="function"?u=W2(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 W2(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 K2(e,t){return Wd(e,t)}function gI(e,t,s,n){let r=F2(e,t,s,n),i=pI.get(r);if(i)return i;let o=j2(e,t,s),a=new dn(e+"_jit_mod");a.addFunction(o);let l=new Bl;l.addPass(new ou),l.addPass(new Ml),l.addPass(new Pi),l.run(a);let u=U2(n.name),f=new bi(n),p=typeof n.isGPU=="function"&&n.isGPU()?G2(o,n,f,u):null;p||(p=f.compile(mh(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},pI.set(r,i),i}var pI,dI,$2,mI,q2,yI=P(()=>{di();sl();Ft();Fc();Ah();Pg();ZS();ex();sx();ox();Nn();gp();Zo();hr();pI=new Map,dI=new Map;c(F2,"_cacheKey");c(U2,"_getRuntime");$2={sum:"sum",mean:"mean",max:"max",min:"min",prod:"prod"},mI={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:st.F32}),"one_hot"),index_select:c((e,t,s)=>e.gather(t[0],t[1],pm(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(z2,"_bufferNumel");c(V2,"_trialLaunch");c(G2,"_compileScheduledGPU");q2=0;c(j2,"_buildGraphFunc");c(W2,"_callBuilder");c(K2,"_reductionInit");c(gI,"jitCompile")});var tI={};ne(tI,{getCudnnLSTM:()=>kx,getGpuAdamFn:()=>Ix,getGpuContiguousFn:()=>vS,getGpuMatmul:()=>hS,getWebgpuRNN:()=>Cx,gpuContiguousArray:()=>NS,registerJITKernels:()=>Bx,setCudnnLSTM:()=>BS,setGpuAdamFn:()=>MS,setGpuConcatFn:()=>OS,setGpuContiguousFn:()=>IS,setGpuMatmul:()=>LS,setWebGPUEagerFn:()=>Z2,setWebgpuRNN:()=>iV,tensorToContiguous:()=>Bn,wrapResult:()=>he});function Z2(e){_I=e}function eV(e,t){let s=[],n={},r=tV[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 wI(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 cg(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=Xc(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]:Xc(t[0].shape,t[1].shape)||[...t[0].shape]}function Bn(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 he(e,t,s,n){let r=Cs(t),i=sn.fromData(e,n),o=new Fe(i,0,t,r,s,n);return new Ot(o)}function jg(e,t,s){let n=1;for(let r=t;r<s;r++)n*=e[r];return n}function sV(e,t,s,n){let r=t.length,i=s<0?r+1+s:s,o=jg(t,0,i),a=jg(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 nV(e,t,s,n){let r=t[0].length,i=s<0?r+s:s,o=jg(t[0],0,i),a=jg(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 rV(e,t,s){let n=wI(e,t,s),r=t[0].dtype,i=Se(r),o=new i(Math.max(xs(n),1)),a=s.dim??0;if(ks()&&vx){let u=t.map(f=>Ox(f));return vx(e,u,t.map(f=>f.shape),a,n,o,r),he(o,n,r,t[0].device)}let l=t.map(u=>Bn(u));return e==="stack"?sV(l,t[0].shape,a,o):nV(l,t.map(u=>u.shape),a,o),he(o,n,r,t[0].device)}function IS(e){Wg=e}function vS(){return Wg}function OS(e){vx=e}function BS(e){bI=e}function kx(){return bI}function iV(e){SI=e}function Cx(){return SI}function MS(e){xI=e}function Ix(){return xI}function LS(e){AI=e}function hS(){return AI}function Ox(e){if(e.isContiguous&&e._impl.storageOffset===0){let t=e._impl.storage.rawData;if(t&&t.length===e.numel)return t}return ks()&&Wg?Wg(e._impl.storage.rawData,e.shape,e.strides,e._impl.storageOffset,e.dtype):Bn(e)}function NS(e){return Ox(e)}function oV(e,t){let s=Q2[t];if(!s)return null;let n=t===ft.GPU,r=t===ft.CUSTOM_0,i=n&&(e==="stack"||e==="cat");return(o,...a)=>{let{tensors:l,scalars:u}=eV(e,a);if(l.length===0)throw new Error(`JIT dispatch: no tensor args for op '${e}'`);if(i)return rV(e,l,u);let f=s(),h=gI(e,l,u,f),p=wI(e,l,u),d=h.outDtype||og(l[0].dtype,l.length>1?l[1].dtype:l[0].dtype),m=xs(p),g=Se(d),y=new g(Math.max(m,1));if(r)return _I(h.compiled,l,y),he(y,p,d,l[0].device);let _=l.map(w=>n?Ox(w):Bn(w));return _.push(y),h.runtime.run(h.funcName,..._),he(y,p,d,l[0].device)}}function Bx(){let e=We.listOps(),t=[ft.CPU,ft.GPU,ft.WASM,ft.CUSTOM_0];for(let s of e){let n=We.findOp(s);if(!n)continue;let r=n.name;for(let i of t){if(n.entry.hasKernel(i))continue;let o=oV(r,i);o&&n.entry.registerKernel(i,qn.fromUnboxed(o))}}}var H2,Y2,J2,X2,Q2,_I,tV,Wg,vx,bI,SI,xI,AI,Mr=P(()=>{rr();Jf();Xa();yI();oo();_i();en();ao();xl();Yr();hr();Q2={[ft.CPU]:()=>H2??=ur(),[ft.GPU]:()=>Y2??=al(),[ft.WASM]:()=>J2??=ll(),[ft.CUSTOM_0]:()=>X2??=cl()},_I=null;c(Z2,"setWebGPUEagerFn");tV={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(eV,"_extractTensorsAndScalars");c(wI,"_inferOutputShape");c(Bn,"tensorToContiguous");c(he,"wrapResult");c(jg,"_prod");c(sV,"_hostStack");c(nV,"_hostCat");c(rV,"_runHostConcatLike");Wg=null;c(IS,"setGpuContiguousFn");c(vS,"getGpuContiguousFn");vx=null;c(OS,"setGpuConcatFn");bI=null;c(BS,"setCudnnLSTM");c(kx,"getCudnnLSTM");SI=null;c(iV,"setWebgpuRNN");c(Cx,"getWebgpuRNN");xI=null;c(MS,"setGpuAdamFn");c(Ix,"getGpuAdamFn");AI=null;c(LS,"setGpuMatmul");c(hS,"getGpuMatmul");c(Ox,"_gpuInputArray");c(NS,"gpuContiguousArray");c(oV,"_wrapOpForJIT");c(Bx,"registerJITKernels")});var NV,we,Ul=P(()=>{en();ao();xl();rr();Cn();Yr();NV=nr.fromKey(ft.TRACING),we=class extends Ot{static{c(this,"SymbolicTensor")}constructor(t,s,n,r,i){let o=Cs(s),a=sn.allocate(0,n,Jc),l=new Fe(a,0,s,o,n,Jc);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(NV)}get isSymbolic(){return!0}}});var DV,Py,xO=P(()=>{Ft();rx();DV={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")},Py=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]=nt,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(!DV[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 Xo?Xo.evaluate(t,this._bindings):t}get symbols(){return this._symbols}get guards(){return this._guards}get bindings(){return this._bindings}}});function Ep(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(Wd(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 zl(){return Dy}var FV,Dy,Fy,Tp=P(()=>{di();sl();Do();Ft();cs();Ul();xO();Nn();c(Ep,"_traceReduce");FV={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)=>Ep(e,t,s,"sum"),"sum"),mean:c((e,t,s)=>Ep(e,t,s,"mean"),"mean"),max:c((e,t,s)=>Ep(e,t,s,"max"),"max"),min:c((e,t,s)=>Ep(e,t,s,"min"),"min"),prod:c((e,t,s)=>Ep(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:st.F32}),"one_hot"),index_select:c((e,t,s)=>e.gather(t[0],t[1],pm(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")},Dy=null;c(zl,"getActiveTracer");Fy=class{static{c(this,"Tracer")}constructor(t){this._name=t||"traced",this._shapeEnv=new Py,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 M(i,s);return this._inputTypes.push(a),this._inputSymShapes.push(o),{shape:i,dtype:s,tensorType:a}}_initGraph(){this._func=new qs(this._name,this._inputTypes,[]),this._func.inputTypes=[...this._func.inputTypes],this._builder=new Rs(this._func),this._module=new dn(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 we(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 we&&r.push(f.irValue);let i,o=FV[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 we(a,l.shape,l.dtype,this,u)}_propagateSymbolicShape(t,s,n,r){let i=n.filter(u=>u instanceof we),o=ut.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]!==nt){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:nt}return l}scan(t,s,n){let r=c(h=>h instanceof we?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 we(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 we(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 we(u,[],t.dtype,this,[]);return this._capturedParams.set(t,f),f}let n=new M(t.shape,t.dtype);this._func.inputTypes.push(n);let i=this._func.entryBlock.addArgument(n),o=new we(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 we&&(this._builder.returnOp([t.irValue]),this._outputSymShapes=[t.symbolicShape]),this._outputTypes=[new M(t.shape,t.dtype)]}markOutputs(t){let s=t.map(n=>n.irValue);this._builder.returnOp(s),this._outputTypes=t.map(n=>new M(n.shape,n.dtype)),this._outputSymShapes=t.map(n=>n instanceof we?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(){Dy=this}deactivate(){Dy===this&&(Dy=null)}}});function IO(e){let t=new _c;return t.add(new Ac),t.add(new Ec),t.add(new Tc(e)),t.add(new Nc),t.add(new Rc(e)),t.add(new vc),t.add(new kc),t.add(new Xd),t.add(new xc),t.add(new em),t.add(new sm),t.add(new nm),t.add(new rm),e&&(t.add(new tm(e)),t.add(new Qd(e)),t.add(new Zd(e))),t}var s3,n3,jy,OO=P(()=>{_s();sh();Ug();Cc();c(IO,"buildAlgebraicPatterns");s3=IO(!1),n3=IO(!0),jy=class extends zt{static{c(this,"AlgebraicSimplificationPass")}constructor(t={}){super("algebraic_simplify"),this.preservedAnalyses=new Set([Qo]),this.patterns=t.fastMath?n3:s3}run(t,s){return new wc(this.patterns).applyPatterns(t,10,this.trace)}}});function BO(e){if(r3.has(e))return!0;let t=ut.get(e);return!!(t&&t.isConstant)}function i3(e,t){return!Ob(t)||typeof e!="number"?!0:Number.isInteger(e)&&Number.isSafeInteger(e)}function MO(e,t,s){if(s.has(e))return s.get(e);let n=o3(e,t,s);return s.set(e,n),n}function o3(e,t,s){let n=e.definingOp;if(!n)return;if(BO(n.opName))return n.getAttr("value");if(t.has(n))return;t.add(n);let r=ut.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=MO(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 r3,Wy,LO=P(()=>{_s();di();cs();Ze();ws();Ft();r3=new Set(["constant","scalar_constant"]);c(BO,"isConstantProducer");c(i3,"isFoldResultRepresentable");c(MO,"resolveConstantValue");c(o3,"computeConstantValue");Wy=class extends zt{static{c(this,"ConstantFoldPass")}constructor(){super("constant_fold")}run(t,s){let n=!1,r=0,i=new Rs(t),o=new Map;for(let a of[...t.opsRecursive()]){if(!a.parentBlock||BO(a.opName))continue;let l=ut.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=MO(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||!i3(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>=dt.DEBUG&&r>0&&this.trace.emit({type:"pass_detail",passName:this.name,foldedCount:r,level:dt.DEBUG}),n?ot.CHANGED:ot.UNCHANGED}}});var Ky,PO=P(()=>{_s();cs();Ze();Ug();ws();Ky=class extends zt{static{c(this,"CSEPass")}constructor(){super("cse"),this.preservedAnalyses=new Set([Qo])}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=ut.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>=dt.DEBUG&&r>0&&this.trace.emit({type:"pass_detail",passName:this.name,eliminated:r,level:dt.DEBUG}),n?ot.CHANGED:ot.UNCHANGED}}});function uA(e,t){let s=cA[e],n=cA[t];return s===void 0||n===void 0?!1:e===Wt.REDUCTION?t===Wt.ELEMENTWISE:t===Wt.REDUCTION?s<=cA[Wt.INJECTIVE]:!0}function Ku(e){let t=!1,s=!1,n=!1,r=!1;for(let i of e){let o=ut.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?Wt.OPAQUE:t?Wt.REDUCTION:s?Wt.INJECTIVE:r?Wt.BROADCAST:Wt.ELEMENTWISE}function Kl(e){let t=ut.get(e.opName);return!t||t.isOpaque?Wt.OPAQUE:t.isReduction?Wt.REDUCTION:t.isInjective?Wt.INJECTIVE:t.isBroadcast?Wt.BROADCAST:t.isElementwise?Wt.ELEMENTWISE:Wt.OPAQUE}function DO(e,t){if(e.length!==t.length)return!1;for(let s=0;s<e.length;s++)if(!(e[s]===nt||t[s]===nt)&&e[s]!==t[s])return!1;return!0}var Wt,cA,Wu,Hy,Hu=P(()=>{Ft();cs();Fc();Wt=Object.freeze({ELEMENTWISE:"kElementwise",BROADCAST:"kBroadcast",REDUCTION:"kReduction",INJECTIVE:"kInjective",OPAQUE:"kOpaque",HORIZONTAL:"kHorizontal"}),cA={[Wt.ELEMENTWISE]:0,[Wt.BROADCAST]:1,[Wt.INJECTIVE]:2,[Wt.REDUCTION]:3};c(uA,"canFusePatterns");c(Ku,"classifyFusionKind");c(Kl,"classifyOpPattern");Wu=Object.freeze({legal:!0}),Hy=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"||il(t)||t0(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=ut.get(t.opName),r=ut.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=Kl(t),o=Kl(s);return uA(i,o)?(i===Wt.REDUCTION||o===Wt.REDUCTION)&&!this.allowReductionFusion?{legal:!1,reason:"reduction fusion disabled by target"}:i===Wt.ELEMENTWISE&&o===Wt.ELEMENTWISE?this._checkElementwisePair(t,s):(i===Wt.BROADCAST||i===Wt.REDUCTION)&&o===Wt.ELEMENTWISE?Wu: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=ut.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=ut.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"}:Wu}_checkElementwisePair(t,s){let n=this._getOutputShape(t),r=this._getOutputShape(s);return n&&r&&!DO(n,r)?{legal:!1,reason:`elementwise shape mismatch: [${n}] vs [${r}]`}:Wu}_checkProducerConsumerShapes(t,s){let n=this._getOutputShape(t);if(!n)return Wu;for(let r=0;r<s.numOperands;r++)if(s.getOperand(r).definingOp===t){let i=s.getOperand(r).type;if(i instanceof M)return DO(n,i.shape)?Wu:{legal:!1,reason:`shape mismatch on data edge: [${n}] vs [${i.shape}]`}}return Wu}_getOutputShape(t){for(let s=0;s<t.numResults;s++){let n=t.getResult(s).type;if(n instanceof M)return n.shape}return null}};c(DO,"shapesCompatible")});function a3(e){for(let t=0;t<e.numResults;t++){let s=e.getResult(t).type;if(s instanceof M)return s.shape.join(",")}return null}var Yu,Yy,fA=P(()=>{Ft();cs();Hu();Fc();Yu=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=Ku(this.ops),this.kind}allOpsInlineFusable(){for(let t of this.ops){let s=ut.get(t.opName);if(!s)return!1;if(!(s.isReduction||s.isConstant)&&!il(t.opName))return!1}return!0}};c(a3,"outputShapeKey");Yy=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=ut.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=ut.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 Yu(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&&a3(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=ut.get(u.opName);if(!this._bucketable(u,f))continue;let h=u.getResult(0).type;a++,o.set(u,a);let p=new Yu(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 S=g.getOperand(w).definingOp;if(S&&o.get(S)===a){y=!0;break}}if(y){o.set(g,a);continue}if(p.size>=i||r.has(g))continue;let _=ut.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=Wt.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 Ju,hA=P(()=>{Ft();cs();Ju=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=ut.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 M){let o=i.numel();o!==nt&&(n=o);break}}if(s&&s.isReduction&&t.numOperands>0){let r=t.getOperand(0).type;if(r instanceof M){let i=r.numel();if(i!==nt)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 M){let i=r.sizeInBytes();i!==nt&&(s+=i)}}for(let n=0;n<t.numResults;n++){let r=t.getResult(n).type;if(r instanceof M){let i=r.sizeInBytes();i!==nt&&(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 b of t.ops){let A=this.estimateFLOPs(b);n.set(b,A),r+=A,i+=this.estimateBytes(b),this.libraryOps.has(b.opName)&&a++;let x=ut.get(b.opName);if(x){let N=this._outputElements(b);x.isReduction&&(l=!0,f+=N),x.isElementwise&&(u=!0,h+=N)}for(let N=0;N<b.numResults;N++){let I=b.getResult(N);m.add(I);let E=0,T=!1;for(let B of I.uses())s.has(B.user)?E++:T=!0;E>1&&(o+=(E-1)*A),(E>0||T)&&p++}for(let N=0;N<b.numOperands;N++){let I=b.getOperand(N);if(!m.has(I))continue;let E=!1;for(let T of I.uses())if(s.has(T.user)&&T.user!==b){E=!0;break}E||p--}p>d&&(d=p)}let g=t.getInputValues(),y=t.getOutputValues(),_=0;for(let b of g)if(b.type instanceof M){let A=b.type.sizeInBytes();A!==nt&&(_+=A)}for(let b of y)if(b.type instanceof M){let A=b.type.sizeInBytes();A!==nt&&(_+=A)}let w=0;for(let b of t.ops)for(let A=0;A<b.numResults;A++){let x=b.getResult(A),N=0;for(let I of x.uses())if(s.has(I.user)&&(N++,N>1))break;if(N>1){let I=x.type;if(I instanceof M){let E=I.sizeInBytes();E!==nt&&(w+=E)}}}let S=0;return l&&u&&h>0&&f>0&&(S=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:S,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 M){let r=n.numel();if(r!==nt)return r}}return 0}}});function Xu(e){let t=null;for(let s of e.ops())t=s;return t&&t.opName==="yield"?t:null}function Qu(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 Zu(e){let t=0,s=e.regions[0]?.entryBlock;if(!s)return 0;for(let n of s.ops()){let r=ut.get(n.opName);r&&r.isReduction&&t++}return t}function tf(e){let t=e.regions[0]?.entryBlock;if(!t)return!1;for(let s of t.ops())if(s.opName!=="yield"&&!il(s.opName))return!1;return!0}function l3(e){let t=new Map,s=0;for(let n=e.firstOp;n;n=n._next)t.set(n,s++);return t}function ef(e){let t=l3(e);return(s,n)=>{let r=t.get(s),i=t.get(n);return r!==void 0&&i!==void 0&&r<i}}function sf(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 vp=P(()=>{cs();Fc();c(Xu,"getYieldOp");c(Qu,"countInnerOps");c(Zu,"countReductions");c(tf,"allInnerOpsFusable");c(l3,"blockPositionIndex");c(ef,"makeComesBefore");c(sf,"remapOperands")});var Jy,FO=P(()=>{_s();Rn();Mo();Hu();fA();hA();vp();ws();Jy=class extends zt{static{c(this,"FusionPass")}constructor(t={}){super("FusionPass");let s=t.target||{};this.legality=new Hy({maxFusionSize:s.maxFusionSize||t.maxFusionSize,maxSharedMemory:s.sharedMemoryBytes||t.maxSharedMemory,libraryOps:s.libraryOps||t.libraryOps,allowReductionFusion:t.allowReductionFusion}),this.costModel=new Ju({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 Yy(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 ot.UNCHANGED;for(let o of i)this._materializeFusion(t,o);return ot.CHANGED}_traceDecision(t,s,n){if(!this.trace||this.trace.level<dt.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:dt.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 Re,u=new ls(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 x=0;x<_.numOperands;x++){let N=_.getOperand(x),I=f.get(N);w.push(I!==void 0?I:N)}let S=_.results.map(x=>x.type),b=_.regions.length>0?_.regions.map(x=>gi(x)):null,A=new Nt(_.opName,w,S,new Map(_.attributes),b);u.pushOp(A);for(let x=0;x<_.numResults;x++)f.set(_.getResult(x),A.getResult(x))}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 Nt("yield",h,[]);u.pushOp(p);let d=ef(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 Nt("fusion",r,a,{fusion_kind:s.kind||Wt.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 UO(e){if(pA.has(e.opName))return!0;let t=ut.get(e.opName);return t!==null&&t.isElementwise}function u3(e,t){let s=e.getOperand(0).definingOp;return s&&t.has(s)&&!pA.has(s.opName)?e.getOperand(1).definingOp:s}function f3(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 h3(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 p3(e,t){let s=ut.get(e.opName),n=s&&s.getAttr("epilogueTag");return n?n(e,t):"activation"}function d3(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)&&pA.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())UO(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)&&UO(m.user)&&i.push(m.user)}}let a=[];for(let p of t)zO.has(p.opName)||$O.has(p.opName)||a.push(p3(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 $O,zO,pA,c3,Xy,VO=P(()=>{_s();Rn();cs();ws();$O=new Set(["constant","scalar_constant"]),zO=new Set(["broadcast_in_dim","broadcast"]),pA=new Set([...$O,...zO]);c(UO,"isEpilogueCandidate");c3=new Map([["add",(e,t)=>t.has(u3(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(u3,"resolveOtherOperand");for(let[e,t]of c3)ut.has(e)&&ut.registerOpAttr(e,"epilogueTag",t);c(f3,"comesBefore");c(h3,"hasEscapingUse");c(p3,"classifyTag");c(d3,"collectChainAndAnalyze");Xy=class extends zt{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 ot.UNCHANGED;let s=!1,n=[];for(let r of t.ops()){let i=ut.get(r.opName);i&&i.isOutEWiseFusable&&n.push(r)}for(let r of n){let i=d3(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 Nt("fused_dot_epilogue",h,[p],d),g=r.parentBlock;if(!g)continue;let y=new Set(o);if(y.add(r),h3(y,l))continue;let _=null;for(let w of h){let S=w.definingOp;!S||y.has(S)||(!_||!f3(S,_))&&(_=S)}_&&_.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>=dt.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,dotsFound:n.length,changed:s,level:dt.DEBUG}),s?ot.CHANGED:ot.UNCHANGED}}});var Qy,GO=P(()=>{_s();Rn();Mo();Ft();cs();ws();Hu();vp();Qy=class extends zt{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 ot.UNCHANGED;let n=new Set(s),r=this._buildProducerConsumerEdges(s,n);if(r.length===0)return ot.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>=dt.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:s.length,edges:r.length,mergeCount:o,level:dt.DEBUG}),i?ot.CHANGED:ot.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=Qu(t),r=Qu(s);if(n+r>this.maxFusionSize)return!1;let i=0;return i+=Zu(t),i+=Zu(s),!(i>this.maxReductions||!tf(t)||!tf(s))}_shouldMerge(t,s,n){let r=0;for(let[l]of n){let u=t.getResult(l);if(u.type instanceof M){let f=u.type.sizeInBytes();f!==nt&&(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=Xu(r),a=Xu(i);if(!o||!a)return;let l=new Map;for(let[E,T]of n)l.set(T,E);let u=[],f=new Map;for(let E=0;E<t.numOperands;E++)f.set(E,u.length),u.push(t.getOperand(E));let h=new Map;for(let E=0;E<s.numOperands;E++){if(l.has(E))continue;let T=s.getOperand(E),B=-1;for(let k=0;k<u.length;k++)if(u[k]===T){B=k;break}B>=0?h.set(E,B):(h.set(E,u.length),u.push(T))}let p=u.map(E=>E.type),d=new Re,m=new ls(p);d.addBlock(m);let g=new Map;for(let E=0;E<r.arguments.length;E++)g.set(r.arguments[E],m.arguments[f.get(E)]);for(let E of r.ops()){if(E.opName==="yield")continue;let T=sf(E,g),B=E.results.map(D=>D.type),k=E.regions.length>0?E.regions.map(D=>gi(D)):null,O=new Nt(E.opName,T,B,new Map(E.attributes),k);m.pushOp(O);for(let D=0;D<E.numResults;D++)g.set(E.getResult(D),O.getResult(D))}for(let E=0;E<o.numOperands;E++){let T=o.getOperand(E),B=g.get(T)||T;for(let[k,O]of l)O===E&&g.set(i.arguments[k],B)}for(let E=0;E<i.arguments.length;E++){if(l.has(E))continue;let T=h.get(E);T!==void 0&&g.set(i.arguments[E],m.arguments[T])}for(let E of i.ops()){if(E.opName==="yield")continue;let T=sf(E,g),B=E.results.map(D=>D.type),k=E.regions.length>0?E.regions.map(D=>gi(D)):null,O=new Nt(E.opName,T,B,new Map(E.attributes),k);m.pushOp(O);for(let D=0;D<E.numResults;D++)g.set(E.getResult(D),O.getResult(D))}let y=[],_=[],w=[];for(let E=0;E<t.numResults;E++){let T=!1;for(let B of t.getResult(E).uses())if(B.user!==s){T=!0;break}if(T){let B=o.getOperand(E),k=g.get(B)||B;w.push({resultIdx:E,value:k}),y.push(k),_.push(t.getResult(E).type)}}for(let E=0;E<s.numResults;E++){let T=a.getOperand(E),B=g.get(T)||T;y.push(B),_.push(s.getResult(E).type)}let S=new Nt("yield",y,[]);m.pushOp(S);let b=[];for(let E of m.ops())E.opName!=="yield"&&b.push(E);let A=Ku(b),x=new Nt("fusion",u,_,{fusion_kind:A},[d]),N=s;if(!N.parentBlock)return;N.parentBlock.insertBefore(x,N);let I=0;for(let{resultIdx:E}of w)t.getResult(E).replaceAllUsesWith(x.getResult(I)),I++;for(let E=0;E<s.numResults;E++)s.getResult(E).replaceAllUsesWith(x.getResult(I)),I++;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=ut.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 M){let l=a.numel();l!==nt&&(s+=l);break}}}return s}}});function m3(e,t){let s=Math.min(e.id,t.id),n=Math.max(e.id,t.id);return`${s}|${n}`}var Zy,qO=P(()=>{_s();Rn();Mo();Ft();Hu();ws();vp();Zy=class extends zt{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 ot.UNCHANGED;let n=this._findCandidates(s);if(n.length===0)return ot.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>=dt.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:s.length,candidates:n.length,mergeCount:i,level:dt.DEBUG}),r?ot.CHANGED:ot.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=m3(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 M){let p=h.type.sizeInBytes();p!==nt&&(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=Qu(t),r=Qu(s);if(n+r>this.maxFusionSize||t.numResults+s.numResults>this.maxOutputs)return!1;let o=0;return o+=Zu(t),o+=Zu(s),!(o>this.maxReductions||!tf(t)||!tf(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=Xu(r),a=Xu(i);if(!o||!a)return;let l=[],u=new Map,f=new Map;for(let T=0;T<t.numOperands;T++){let B=t.getOperand(T),k=B.id,O=u.get(k);O===void 0&&(O=l.length,u.set(k,O),l.push(B)),f.set(T,O)}let h=new Map;for(let T=0;T<s.numOperands;T++){let B=s.getOperand(T),k=B.id,O=u.get(k);O===void 0&&(O=l.length,u.set(k,O),l.push(B)),h.set(T,O)}let p=l.map(T=>T.type),d=new Re,m=new ls(p);d.addBlock(m);let g=new Map;for(let T=0;T<r.arguments.length;T++)g.set(r.arguments[T],m.arguments[f.get(T)]);for(let T of r.ops()){if(T.opName==="yield")continue;let B=sf(T,g),k=T.results.map(D=>D.type),O=new Nt(T.opName,B,k,new Map(T.attributes));m.pushOp(O);for(let D=0;D<T.numResults;D++)g.set(T.getResult(D),O.getResult(D))}for(let T=0;T<i.arguments.length;T++)g.set(i.arguments[T],m.arguments[h.get(T)]);for(let T of i.ops()){if(T.opName==="yield")continue;let B=sf(T,g),k=T.results.map(D=>D.type),O=new Nt(T.opName,B,k,new Map(T.attributes));m.pushOp(O);for(let D=0;D<T.numResults;D++)g.set(T.getResult(D),O.getResult(D))}let y=[],_=[];for(let T=0;T<o.numOperands;T++){let B=o.getOperand(T);y.push(g.get(B)||B),_.push(t.getResult(T).type)}for(let T=0;T<a.numOperands;T++){let B=a.getOperand(T);y.push(g.get(B)||B),_.push(s.getResult(T).type)}let w=new Nt("yield",y,[]);m.pushOp(w);let S=[];for(let T of m.ops())T.opName!=="yield"&&S.push(T);let b=Ku(S),A=new Nt("fusion",l,_,{fusion_kind:b},[d]),x=t.parentBlock;if(!x)return;let N=ef(x),I=null;for(let T of l){let B=T.definingOp;!B||B===t||B===s||(!I||!N(B,I))&&(I=B)}I&&I.parentBlock===x?x.insertAfter(A,I):x.insertBefore(A,t);for(let T=0;T<t.numResults;T++)t.getResult(T).replaceAllUsesWith(A.getResult(T));let E=t.numResults;for(let T=0;T<s.numResults;T++)s.getResult(T).replaceAllUsesWith(A.getResult(E+T));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(m3,"pairKey")});var dA,le,Di=P(()=>{dA=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}},le=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 dA(s,n,r,l,u)}}});var mA,t_,jO=P(()=>{Di();mA=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}},t_=class{static get name(){return"post_dominance"}static get depKey(){return"postDominance"}static get dependencies(){return[le]}static compute(t,s={}){let r=(s.useDef||le.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 mA(u,r)}}});var WO,e_,KO=P(()=>{_s();Rn();Mo();cs();Hu();fA();hA();jO();ws();Di();vp();WO=new Set(["return","yield","constant","scalar_constant"]),e_=class extends zt{static{c(this,"DominatorFusionPass")}constructor(t={}){super("DominatorFusionPass"),this.requiredAnalyses=[le];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 Ju({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(le,t):le.compute(t),r=t_.compute(t,{useDef:n}),i=n.topologicalOrder,o=this._buildGroups(i,r);if(o.length===0)return ot.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 ot.UNCHANGED;for(let l of a)this._materialize(t,l);return this.trace&&this.trace.level>=dt.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,groupsBuilt:o.length,groupsFused:a.length,level:dt.DEBUG}),ot.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(WO.has(f.opName))continue;let h=Kl(f);if(h===Wt.OPAQUE||this.libraryOps.has(f.opName))continue;let p=s.immediatePDom(f);if(!p||WO.has(p.opName))continue;let d=Kl(p);if(d===Wt.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 Yu(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 uA(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),Kl(f)===Wt.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),Kl(p)===Wt.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=ut.get(n.opName);r&&r.isReduction&&s++}return s<=this.maxReductions}_checkReductionLimit(t,s){let n=ut.get(s.opName);if(!n||!n.isReduction)return!0;let r=0;for(let i of t.ops){let o=ut.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 Re,u=new ls(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 x=y.getOperand(A),N=f.get(x);_.push(N!==void 0?N:x)}let w=y.results.map(A=>A.type),S=y.regions.length>0?y.regions.map(A=>gi(A)):null,b=new Nt(y.opName,_,w,new Map(y.attributes),S);u.pushOp(b);for(let A=0;A<y.numResults;A++)f.set(y.getResult(A),b.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 Nt("yield",h,[]));let p=new Nt("fusion",r,a,{fusion_kind:s.kind||Wt.ELEMENTWISE},[l]),d=n[0].parentBlock;if(!d)return;let m=ef(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 g3(e,t){return e===t?!0:!e||!t?!1:typeof e.equals=="function"?e.equals(t):!1}var xa,s_,HO=P(()=>{Ft();xa=class{static{c(this,"LayoutPreference")}constructor(t,s,n=0){this.inputs=t,this.outputs=s,this.cost=n}},s_=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 M)||g3(t,s))return 0;let r=n.numel();return r<0?1024:r*2}estimateBenefit(t,s,n){if(!(s instanceof M))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 xa([s.preferredConvLayout,null],[s.preferredConvLayout]);if(s.isGPU()&&r===4){let i=new Ke([0,2,3,1]);return new xa([i,null],[i])}if(s.isCPU()&&r===4){let i=new Ke([0,2,3,1]);return new xa([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=Ke.rowMajor(n.rank);if(s.isCPU()&&r.rank===2){let a=Ke.columnMajor(r.rank);return new xa([i,a],[i])}let o=Ke.rowMajor(r.rank);return new xa([i,o],[i])}),this._rules.set("reduce",(t,s)=>{let n=t.getResult(0).type;return n?new xa([null],[Ke.rowMajor(n.rank)]):null})}};c(g3,"layoutEquals")});function YO(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 M?Ke.rowMajor(i.type.rank):Ke.rowMajor(1)}function y3(e,t){return e===t?!0:!e||!t?!1:typeof e.equals=="function"?e.equals(t):!1}function JO(e){if(e instanceof Ke)return e;if(e&&typeof e.toLayout=="function")try{return e.toLayout()}catch{}return e&&e.baseOrder&&!e.isBlocked?.()?new Ke(e.baseOrder):e&&e.order?new Ke(e.order):null}var gA,n_,XO=P(()=>{Di();Ft();cs();Ze();gA=class{static{c(this,"LayoutAnalysisResult")}constructor(t,s,n){this.assignments=t,this.conversions=s,this.totalCost=n}},n_=class{static get name(){return"layout"}static get depKey(){return"layout"}static get dependencies(){return[le]}static compute(t,s,n){let r=s.useDef,i=new Map,o=r.topologicalOrder;for(let u of t.args)u.type instanceof M&&i.set(u,u.type.layout||Ke.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 M))continue;let y=h.outputs[m]||null;y?i.set(g,y):i.set(g,YO(f,i))}continue}let p=ut.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 M&&(d?i.set(g,YO(f,i)):i.set(g,Ke.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 M))continue;let m=i.get(d);if(!m)continue;let g=h.inputs[p];if(!g||y3(m,g))continue;let y=JO(m),_=JO(g);!y||!_||y.equals(_)||(a.push({value:d,consumer:f,operandIdx:p,from:y,to:_}),n&&(l+=n.estimateConversionCost(y,_,d.type)))}}return new gA(i,a,l)}};c(YO,"resolveFromInputs");c(y3,"layoutEquals");c(JO,"toPlainLayout")});function _3(e,t,s){let n=e.id,r=t.hash?t.hash():0,i=s.hash?s.hash():0;return`${n}:${r}:${i}`}var r_,QO=P(()=>{_s();Rn();Ft();HO();XO();Di();ws();r_=class extends zt{static{c(this,"LayoutTransformPass")}constructor(t={}){super("LayoutTransformPass"),this.target=t.target||null,this._policy=null}run(t,s){if(!this.target)return ot.UNCHANGED;this._policy||(this._policy=new s_(this.target));let n=s?s.getAnalysis(le,t):le.compute(t),r=n_.compute(t,{useDef:n},this._policy);if(r.conversions.length===0)return ot.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=_3(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 Ke?h.order:Array.from({length:l.type.rank},(N,I)=>I),S=p instanceof Ke?p.order:Array.from({length:l.type.rank},(N,I)=>I),b=new M(l.type.shape,l.type.dtype,p),A=new Nt("layout_transform",[l],[b],{src_layout:[...w],dst_layout:[...S]}),x=l.definingOp;x&&x.parentBlock?x.parentBlock.insertAfter(A,x):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>=dt.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,conversions:r.conversions.length,uniqueTransforms:i.size,level:dt.DEBUG}),ot.CHANGED}};c(_3,"valueLayoutKey")});function ZO(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 tB(e){let t=0;for(let s=0;s<e.length;s++)t=(t<<5)-t+e.charCodeAt(s)&2147483647;return t}function S3(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 Hn,w3,b3,Yn,yA=P(()=>{Ft();Hn=Object.freeze({PER_TENSOR_SYMMETRIC:"per_tensor_symmetric",PER_TENSOR_ASYMMETRIC:"per_tensor_asymmetric",PER_CHANNEL:"per_channel",PER_GROUP:"per_group"}),w3=new Set(Object.values(Hn)),b3=new Set([st.I8,st.UI8]),Yn=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||st.I8,this.numBits=t.numBits||Qi(this.dtype)*8,this._hash=null}clampRange(){if(this.isSymmetric()){let n=2**(this.numBits-1)-1;return[-n,n]}if(this.dtype===st.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===Hn.PER_CHANNEL}isPerGroup(){return this.scheme===Hn.PER_GROUP}isSymmetric(){return this.scheme===Hn.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||!ZO(this.scale,t.scale)||!ZO(this.zeroPoint,t.zeroPoint))}hash(){if(this._hash!==null)return this._hash;let t=2166136261;return t=(t^tB(this.scheme))*16777619&2147483647,t=(t^tB(this.dtype))*16777619&2147483647,t=(t^this.numBits)*16777619&2147483647,t=(t^S3(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=st.I8,i=8){if(n===Hn.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===st.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=st.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:Hn.PER_CHANNEL,scale:a,zeroPoint:l,axis:n,dtype:r,numBits:i})}static defaultForActivation(t,s=st.I8,n=8){return e.fromRange(-6,6,t,s,n)}static fromConstantArray(t,s,n=st.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=st.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=st.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:Hn.PER_GROUP,scale:o,zeroPoint:a,groupSize:s,dtype:n,numBits:r})}static isQuantizableDtype(t){return b3.has(t)}static isValidScheme(t){return w3.has(t)}};c(ZO,"scaleEquals");c(tB,"hashStr");c(S3,"hashScaleValue")});function T3(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 N3(e,t){for(let s=0;s<e.numOperands;s++)if(!t.has(e.getOperand(s)))return!1;return e.numOperands>0}function R3(e,t,s){for(let n=0;n<e.numOperands;n++){let r=e.getOperand(n);if(!t.has(r)&&(!(r.type instanceof M)||!as(r.type.dtype)))return!1}return e.numOperands>0}var x3,A3,E3,i_,o_,eB=P(()=>{_s();Rn();Ft();cs();Ze();Di();yA();ws();x3=new Set(["softmax","sqrt","div","rsqrt","log","exp","tanh"]),A3=new Set(["dot","conv","add","mul","sub"]),E3=new Map([["dot","quantized_dot"],["conv","quantized_conv"]]);for(let[e,t]of E3)ut.has(e)&&ut.registerOpAttr(e,"quantizedVariant",t);i_=class{static{c(this,"QuantizationConfig")}constructor(t={}){this.scheme=t.scheme||Hn.PER_TENSOR_SYMMETRIC,this.calibration=t.calibration||null,this.targetDtype=t.targetDtype||st.I8,this.excludeOps=t.excludeOps||x3,this.quantizableOps=t.quantizableOps||A3,this.sensitivityThreshold=t.sensitivityThreshold||0,this.sensitivityResult=t.sensitivityResult||null,this.weightOnly=t.weightOnly||!1,this.target=t.target||null}},o_=class extends zt{static{c(this,"QuantizationPass")}constructor(t={}){super("QuantizationPass"),this.requiredAnalyses=[le],this.config=t instanceof i_?t:new i_(t)}run(t,s){let r=(s?s.getAnalysis(le,t):le.compute(t)).topologicalOrder,i=new Set,o=this.config,a=!1;if(o.target&&!o.target.supportsInt8)return ot.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&&!T3(f))continue;let h=ut.get(f.opName),p=h?h.getAttr("quantizedVariant"):null;if(p&&R3(f,i,o)){o.scheme===Hn.PER_CHANNEL&&this._canPerChannelDot(f,i)?a=this._replacePerChannelDot(f,o)||a:a=this._replaceWithNativeQuantized(f,p,i,o)||a;continue}let d=ut.get(f.opName);if(d&&d.hasTrait(J.ELEMENTWISE)&&N3(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>=dt.DEBUG&&a&&this.trace.emit({type:"pass_detail",passName:this.name,opsProcessed:r.length,changed:a,level:dt.DEBUG}),a?ot.CHANGED:ot.UNCHANGED}_getQuantParams(t,s){let n=Qi(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 Yn.fromRange(-Math.abs(i)||-1,Math.abs(i)||1,s.scheme,s.targetDtype,n);if(i&&typeof i.length=="number")return Yn.fromConstantArray(i,s.scheme,s.targetDtype,n)}return t.type instanceof M&&as(t.type.dtype)?Yn.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 M(r.type.shape,n.targetDtype),a=new Nt("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=st.F32,a=new M(n.type.shape,o),l=new Nt("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 M)||!as(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 S=h===0?"input":"kernel";o[`${S}_scale`]=_.getScalarScale(),o[`${S}_zero_point`]=_.getScalarZeroPoint()}}continue}let d=this._getQuantParams(p,r);if(!d)return!1;let m=new M(p.type.shape,r.targetDtype),g=new Nt("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 M(t.getResult(0).type.shape,st.I32),f=new Nt(s,i,[u],o);if(t.parentBlock){t.parentBlock.insertBefore(f,t);let h=new M(t.getResult(0).type.shape,t.getResult(0).type.dtype),p=o.output_scale||1,d=new Nt("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 M)||n.type.shape.length!==2||!(r.type instanceof M)||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=Qi(s.targetDtype)*8,r=Hn.PER_TENSOR_SYMMETRIC;return s.calibration&&s.calibration.hasData(t)?s.calibration.getQuantParams(t,r,s.targetDtype):Yn.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=Qi(s.targetDtype)*8,u=t.getAttr("rhs_contracting")[0]===0?1:0,f=Yn.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 M(i,s.targetDtype),_=new Nt("constant",[],[y],{value:h,tensor_type:y});g.insertBefore(_,t);let w=new M(n.type.shape,s.targetDtype),S=new Nt("quantize",[n],[w],{scale:d,zero_point:m,scheme:Hn.PER_TENSOR_SYMMETRIC,target_dtype:s.targetDtype});g.insertBefore(S,t);let b=t.getResult(0).type.shape,A={};for(let[W,G]of t.attributes||[])A[W]=G;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 x=new M(b,st.I32),N=new Nt("quantized_dot",[S.getResult(0),_.getResult(0)],[x],A);g.insertBefore(N,t);let I=new M(b,st.F32),E=new Nt("convert",[N.getResult(0)],[I],{target_dtype:st.F32});g.insertBefore(E,t);let T=i[u],B=new Array(T);for(let W=0;W<T;W++)B[W]=d*f.getScaleForChannel(W);let k=new Set(t.getAttr("lhs_contracting")||[]),O=0;for(let W=0;W<n.type.shape.length;W++)k.has(W)||O++;let D=O,q=new M([T],st.F32),z=new Nt("constant",[],[q],{value:B,tensor_type:q});g.insertBefore(z,t);let j=new Nt("broadcast_in_dim",[z.getResult(0)],[I],{broadcast_dimensions:[D],result_shape:b});g.insertBefore(j,t);let Y=new Nt("mul",[E.getResult(0),j.getResult(0)],[I],{});return g.insertBefore(Y,t),t.replaceAllResultsWith([Y.getResult(0)]),t.erase(),!0}};c(T3,"hasConstantOperand");c(N3,"allOperandsQuantized");c(R3,"allOperandsCanQuantize")});var _A,wA,a_,sB=P(()=>{_A=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}},wA=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)}},a_=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 _A(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 wA(s,n,i)}}});function v3(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 k3(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 bA,l_,nB=P(()=>{bA=class{static{c(this,"InplaceCandidate")}constructor(t,s,n){this.srcBuffer=t,this.dstBuffer=s,this.reason=n}},l_=class{static{c(this,"InplaceAnalysis")}static analyze(t,s,n=new Set){let r=[],i=[];k3(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)||!v3(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 bA(m,p,`${m.name} last used at ${g.lastUse}, ${p.name} first used at ${y.firstUse}`)),l.add(m);break}}}}}return r}};c(v3,"shapesMatch");c(k3,"collectBlocks")});function rB(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 SA,xA,c_,iB=P(()=>{c(rB,"gapSelector");SA=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}},xA=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 SA(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=rB(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)}},c_=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 S=this.inplaceMap.get(h),b=this.assignments.get(S);if(b){this.assignments.set(h,{offset:b.offset,size:f.size,scope:f.scope,pool:b.pool,inplaceOf:S});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 xA(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=rB(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 AA,u_,oB=P(()=>{sB();nB();iB();te();AA=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}}},u_=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=a_.analyze(t),n=s.getTemporaries(),r=[];this.enableInplace&&(r=l_.analyze(t,s));let i=new c_;return i.assign(n,r,this.alignment,this.allocStrategy),new AA(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 tn(a,l.isDynamic?"dynamic":l.scope,i)))}return t.body=i,t._setChild("body",i),t}}});var f_,EA,Hl,TA=P(()=>{te();f_=["iterCount","depth","parallelLoops","vectorizedLoops","unrolledLoops","threadBoundLoops","serialLoops","threadBlockSize","gridSize","underReduction","numMathOps","numExternCalls","numReads","numWrites","stride1Accesses","stridedAccesses","reuseCount","touchedBytes","arithmeticIntensity","vectorized","parallelized","innermostExtent"],EA=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}},Hl=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 EA(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 x of s){let N=x.extent&&x.extent.type==="IntImmNode"?x.extent.value:1;switch(r*=N,x.kind){case K.PARALLEL:i++;break;case K.VECTORIZED:o++;break;case K.UNROLLED:a++;break;case K.THREAD_BINDING:l++,x.threadTag&&x.threadTag.startsWith("threadIdx")?f*=N:x.threadTag&&x.threadTag.startsWith("blockIdx")&&(h*=N);break;default:u++;break}}let p={math:0,extern:0};e._countExpr(t.value,p);let d=s.map(x=>x.loopVar.name),m=[{buffer:t.buffer,indices:t.indices}];e._collectLoads(t.value,m);let g=0,y=0,_=0,w=0;for(let x of m){let N=x.buffer&&x.buffer.sizeInBytes?x.buffer.sizeInBytes():0;N>0&&(w+=N);let I=x.indices&&x.indices.length>0?x.indices[x.indices.length-1]:null;I&&I.type==="VariableNode"?g++:y++;let E=new Set;if(x.indices)for(let T of x.indices)e._collectVars(T,E);for(let T of d)E.has(T)||_++}let S=s.length>0?s[s.length-1]:null,b=S&&S.extent&&S.extent.type==="IntImmNode"?S.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:b};return f_.map(x=>A[x]||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 K.PARALLEL:s.numParallelLoops++;break;case K.VECTORIZED:s.numVectorizedLoops++;break;case K.UNROLLED:s.numUnrolledLoops++;break;case K.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 aB(e,t){if(e.length===0)return 0;let s=0;for(let n of e)s+=t[n];return s/e.length}function NA(e,t,s,n,r,i){let o=s.length;if(n>=r||o<i*2)return{leaf:aB(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 S=e[m[_]][d],b=e[m[_+1]][d];if(S===b)continue;let A=_+1,x=o-A;if(A<i||x<i)continue;let N=l-g,I=u-y,E=y-g*g/A+(I-N*N/x);(!f||E<f.cost)&&(f={f:d,thr:(S+b)/2,sIdx:_,cost:E},h=m)}}if(!f)return{leaf:aB(s,t)};let p=h;return{f:f.f,thr:f.thr,left:NA(e,t,p.slice(0,f.sIdx+1),n+1,r,i),right:NA(e,t,p.slice(f.sIdx+1),n+1,r,i)}}function lB(e,t){for(;e&&e.leaf===void 0;)e=t[e.f]<=e.thr?e.left:e.right;return e?e.leaf:0}var kp,cB=P(()=>{c(aB,"leafValue");c(NA,"buildTree");c(lB,"predictTree");kp=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=NA(t,u,o,0,this.maxDepth,this.minSamples);for(let h=0;h<n;h++)i[h]+=this.lr*lB(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*lB(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 uB(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;O3.has(r)?i>s[r]&&(s[r]=i):s[r]+=i}for(let n of B3)n<t&&(s[n]/=e.length);return s[t]=e.length,s}var C3,I3,O3,B3,RA,M3,h_,nf,rf,vA=P(()=>{TA();cB();C3=new Set(["depth","threadBlockSize","gridSize","underReduction","vectorized","parallelized","innermostExtent"]),I3=new Set(["arithmeticIntensity"]),O3=new Set(f_.map((e,t)=>C3.has(e)?t:-1).filter(e=>e>=0)),B3=new Set(f_.map((e,t)=>I3.has(e)?t:-1).filter(e=>e>=0));c(uB,"aggregateStatements");RA=class{static{c(this,"CostEstimate")}constructor(t,s){this.score=t,this.breakdown=s}},M3={parallelism:2,vectorization:1.5,memoryCoalescing:2,occupancy:1,arithmeticIntensity:1,loopOverhead:-.5,codeSize:-.3},h_=class{static{c(this,"AnalyticalCostModel")}constructor(t,s={}){this.target=t,this._weights={...M3,...t&&t.costModelWeights?t.costModelWeights:{},...s.weights||{}}}estimate(t){let s=Hl.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 RA(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}},nf=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?kp.deserialize(t):null,this._X=[],this._Y=[]}addSample(t,s){!t||t.length===0||Number.isFinite(s)&&(this._X.push(uB(t)),this._Y.push(s))}train(){if(this._X.length===0)return;let t=new kp(this.opts);t.fit(this._X,this._Y),this._gbt=t}predict(t){return!this._gbt||!t||t.length===0?0:this._gbt.predict(uB(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)}},rf=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(Hl.extractStatements(t)):this.analytical.score(t)}}});function*fB(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 hB(e,t){for(let s of fB(e))if(s.type==="BlockNode"&&s.name===t)return s;return null}function Cp(e){let t=[];for(let s of fB(e))s.type==="BlockNode"&&t.push(s.name);return t}function p_(e,t){let s=Ue(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 d_=P(()=>{Zo();c(fB,"walkStmts");c(hB,"findBlock");c(Cp,"collectAllBlockNames");c(p_,"analyzeBlockStructure")});function dB(e){return e.isGPU()?P3:L3}function kA(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 L3,P3,pB,CA=P(()=>{L3={name:"mlt_cpu",order:[["S",0],["S",1],["S",2],["S",3],["R",0]],roles:{S0:"parallelize",S3:"vectorize"}},P3={name:"mlt_gpu",order:[["S",0],["S",1],["S",2],["R",0]],roles:{S0:"blockIdx",S1:"threadIdx",S2:"unroll"}},pB={name:"ssrsrs_cpu",order:[["S",0],["S",1],["R",0],["S",2],["R",1],["S",3]],roles:{S0:"parallelize",S3:"vectorize",R1:"unroll"}};c(dB,"getTileStructure");c(kA,"levelCounts")});var Dr,Jn,m_=P(()=>{Dr=class{static{c(this,"SearchVariable")}constructor(t,s){this.name=t,this.candidates=s}sample(t){return this.candidates[t(this.candidates.length)]}},Jn=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 D3(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 F3(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 U3(e,t){let s=new Set,n=[];e.sort(F3);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 IA(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 D3(o))if(l.push(u),i(o/u,a+1,l),l.pop(),n.length>=r)return}},"rec");return i(e,0,[]),U3(n,s)}var mB=P(()=>{c(D3,"divisorsOf");c(F3,"cmpTuple");c(U3,"selectDiverse");c(IA,"enumerateFactorizations")});function g_(e){return e.extent&&e.extent.type==="IntImmNode"?e.extent.value:null}function yB(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 $3(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>=gB.length)return;let p=a<f.length?i(f[a]):null;p&&e.bindThread(p,`${l}.${gB[h]}`)})}}function _B(e,t,s,n,r,i){let o=[];for(let h=0;h<n.length;h++){let p=yB(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=yB(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),$3(e,t,s,o,a)}function wB(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 bB(e,t,s,n){let r=[];return e.forEach((i,o)=>r.push(new Dr(`s${o}`,IA(g_(i),s)))),t.forEach((i,o)=>r.push(new Dr(`r${o}`,IA(g_(i),n)))),r}function SB(e,t){let{spatialLevels:s,reductionLevels:n}=kA(t),{spatialLoops:r,reductionLoops:i}=wB(e);if(r.length===0)return null;for(let u of[...r,...i])if(g_(u)===null)return null;let o=bB(r,i,s,n),a=r.map(u=>u.loopVar.name),l=i.map(u=>u.loopVar.name);return new Jn(t.name,o,(u,f,h,p)=>{_B(u,f,t,a,l,p)})}function xB(e,t){let{spatialLevels:s,reductionLevels:n}=kA(t),{spatialLoops:r,reductionLoops:i}=wB(e);if(r.length===0||i.length===0)return null;for(let u of[...r,...i])if(g_(u)===null)return null;let o=bB(r,i,s,n),a=r.map(u=>u.loopVar.name),l=i.map(u=>u.loopVar.name);return new Jn(t.name,o,(u,f,h,p)=>{u.decomposeReduction(f),_B(u,`${f}_upd`,t,a,l,p)})}var gB,AB=P(()=>{m_();mB();CA();gB=["x","y","z"];c(g_,"staticExtent");c(yB,"multiLevelSplit");c($3,"applyRoles");c(_B,"tileBlock");c(wB,"splitLoops");c(bB,"tilingVariables");c(SB,"createMultiLevelTilingSketch");c(xB,"createSSRSRSTilingSketch")});function TB(e){return Math.min(e&&e.maxThreadsPerBlock||256,256)}function V3(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 NB(e){return new Jn("fused",[],(t,s,n,r)=>{t.fuseConsumer(s,e);let i=t.getLoops(s);i.length>0&&t.parallelize(i[0])})}function RB(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=V3(n);if(r.length===0)return null;let i=s.loopVar.name;return new Jn("rfactor",[new Dr("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 vB(){return new Jn("elementwise_cpu",[new Dr("vector_width",z3)],(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 kB(){return new Jn("elementwise_gpu",[new Dr("block_size",EB)],(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(Tx(e.func)&&a<=l){e.bindThread(i,"threadIdx.x");return}let u=Math.min(n.block_size,TB(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 CB(){return new Jn("reduction_cpu",[],(e,t,s,n)=>{let r=e.getLoops(t);r.length>0&&e.parallelize(r[0])})}function IB(){return new Jn("reduction_gpu",[new Dr("block_size",EB)],(e,t,s,n)=>{let r=e.getLoops(t);if(r.length===0)return;let i=Ue(e.func,t),o=r.filter(f=>!i||!hu(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,TB(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 EB,z3,OB=P(()=>{Zo();m_();EB=[32,64,128,256,512,1024],z3=[1,2,4,8,16];c(TB,"gpuThreadCap");c(V3,"reductionDivisors");c(NB,"createFusedTilingSketch");c(RB,"createRfactorSketch");c(vB,"createElementwiseCPUSketch");c(kB,"createElementwiseGPUSketch");c(CB,"createReductionCPUSketch");c(IB,"createReductionGPUSketch")});function LA(e,t){let s=[];for(let n=1;n<=t;n*=2)n>=e&&s.push(n);return s}function DB(e,t){if(!Ue(e,t))return null;let n=hB(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 G3(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=LA(2,8),p=LA(2,r/2),d=LA(r/4,r/2),m=2*r,g=Math.min(n,8*r),y=4*r,_=new Set,w=[];for(let S of h)for(let b of h)if(!(S*b>y)&&!(S*b+S+b+r>o))for(let A of p)for(let x of p){let N=x*A;if(N<m||N>g||N%r!==0)continue;let I=S*A,E=b*x;if(!(I>l||E>u||I>y||E>y))for(let T of d){if(T>f||(I*T+T*E)*a>i)continue;let k=`${I}_${E}_${T}_${S}_${b}`;_.has(k)||(_.add(k),w.push({BM:I,BN:E,BK:T,TM:S,TN:b,threads:N}))}}return w.sort((S,b)=>LB(b,r)-LB(S,r)),w.slice(0,s)}function LB(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 q3(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,S=Math.ceil(_/g),b=Math.ceil(w/g),A=i%l!==0,x=o%u!==0,N=a%f!==0,I=_%g!==0,E=w%g!==0,T=new oe("rb_As",[_],"f32","shared"),B=new oe("rb_Bs",[w],"f32","shared"),k=new oe("rb_acc",[h*p],"f32","local"),O=new oe("rb_af",[h],"f32","local"),D=new oe("rb_bf",[p],"f32","local"),q=ss("rb_bx"),z=ss("rb_by"),j=ss("rb_tx"),Y=ss("rb_ty"),W=ss("rb_tid"),G=ss("rb_brow"),H=ss("rb_bcol"),kt=ss("rb_k0"),wt=c((pn,WF)=>on(Xn(pn,Jt(p)),WF),"accIdx"),bt=ss("rb_im"),V=ss("rb_in"),Q=Eo(bt,h,Eo(V,p,new rt(k,[wt(bt,V)],OA()))),xt=ss("rb_la"),Et=ss("rb_aidx"),je=on(G,BB(Et,Jt(f))),Gn=on(kt,MB(Et,Jt(f))),xn=new Z(s,[je,Gn]);if(A||N){let pn=A?ni(je,Jt(i)):null;N&&(pn=pn?BA(pn,ni(Gn,Jt(a))):ni(Gn,Jt(a))),xn=new Zt(pn,xn,OA())}let An=new rt(T,[Et],xn);I&&(An=new Zt(ni(Et,Jt(_)),An));let is=MA(xt,S,new vn(Et,on(W,Xn(xt,Jt(g))),An)),Mt=ss("rb_lb"),Le=ss("rb_bidx"),Yi=on(kt,BB(Le,Jt(u))),os=on(H,MB(Le,Jt(u))),Lt=new Z(n,[Yi,os]);if(N||x){let pn=N?ni(Yi,Jt(a)):null;x&&(pn=pn?BA(pn,ni(os,Jt(o))):ni(os,Jt(o))),Lt=new Zt(pn,Lt,OA())}let Ne=new rt(B,[Le],Lt);E&&(Ne=new Zt(ni(Le,Jt(w)),Ne));let Er=MA(Mt,b,new vn(Le,on(W,Xn(Mt,Jt(g))),Ne)),pi=ss("rb_kk"),Bo=ss("rb_fi"),Ji=ss("rb_fj"),gc=Eo(Bo,h,new rt(O,[Bo],new Z(T,[on(Xn(on(Xn(Y,Jt(h)),Bo),Jt(f)),pi)]))),ON=Eo(Ji,p,new rt(D,[Ji],new Z(B,[on(Xn(pi,Jt(u)),on(Xn(j,Jt(p)),Ji))]))),zs=ss("rb_mi"),Ya=ss("rb_ni"),Tr=Eo(zs,h,Eo(Ya,p,new rt(k,[wt(zs,Ya)],on(new Z(k,[wt(zs,Ya)]),Xn(new Z(O,[zs]),new Z(D,[Ya])))))),gb=Eo(pi,f,new At([gc,ON,Tr])),Rd=ss("rb_kt"),yb=new vn(kt,Xn(Rd,Jt(f)),new At([is,Er,new nl,gb,new nl])),vd=MA(Rd,y,yb),Hf=ss("rb_wm"),Yf=ss("rb_wn"),kd=on(on(G,Xn(Y,Jt(h))),Hf),_b=on(on(H,Xn(j,Jt(p))),Yf),wb=new rt(r,[kd,_b],new Z(k,[wt(Hf,Yf)]));if(A||x){let pn=A?ni(kd,Jt(i)):null;x&&(pn=pn?BA(pn,ni(_b,Jt(o))):ni(_b,Jt(o))),wb=new Zt(pn,wb)}let UF=Eo(Hf,h,Eo(Yf,p,wb)),$F=new At([Q,vd,UF]),zF=new tn(k,"local",new tn(O,"local",new tn(D,"local",$F))),VF=new vn(W,on(Xn(Y,Jt(d)),j),new vn(G,Xn(z,Jt(l)),new vn(H,Xn(q,Jt(u)),zF))),GF=Math.ceil(o/u),qF=Math.ceil(i/l),jF=y_(z,"blockIdx.y",qF,y_(q,"blockIdx.x",GF,y_(Y,"threadIdx.y",m,y_(j,"threadIdx.x",d,VF))));return new tn(T,"shared",new tn(B,"shared",jF))}function j3(e){let t=new Dr("config_index",e.map((n,r)=>r)),s=new Jn("matmul_register_block_gpu",[t],(n,r,i,o)=>{let a=DB(n.func,r);if(!a)return;let l=e[o.config_index];if(!l)return;let u=q3(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 W3(e){let t=Cp(e.body),s=null;for(let r of t){let i=p_(e,r);if(i.hasReduction&&i.spatial===2&&i.reads>=2){if(s)return null;s=r}}if(!s)return null;let n=DB(e,s);if(!n)return null;for(let r of t){if(r===s)continue;let i=Ue(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 FB(e,t,s){let n=W3(e);if(!n)return null;let r=PB.get(e);if(r===void 0){let i=G3(s,n.dims);r=i.length>0?j3(i):null,PB.set(e,r)}return r?t===n.reductionBlock?[r]:[]:null}var Jt,OA,ss,on,Xn,BB,MB,ni,BA,MA,Eo,y_,PB,UB=P(()=>{te();rl();Zo();m_();d_();Jt=c(e=>new U(e),"I"),OA=c(()=>new gt(0),"FZERO"),ss=c(e=>new ve(e,"i32"),"IV"),on=c((e,t)=>bs("+",e,t),"ADD"),Xn=c((e,t)=>bs("*",e,t),"MUL"),BB=c((e,t)=>bs("//",e,t),"DIV"),MB=c((e,t)=>bs("%",e,t),"MOD"),ni=c((e,t)=>new ge("lt",e,t),"LT"),BA=c((e,t)=>new $("&&",e,t),"AND"),MA=c((e,t,s)=>new Tt(e,Jt(0),Jt(t),K.SERIAL,s),"forS"),Eo=c((e,t,s)=>new Tt(e,Jt(0),Jt(t),K.UNROLLED,s),"forU"),y_=c((e,t,s,n)=>new Tt(e,Jt(0),Jt(s),K.THREAD_BINDING,n,t),"forT");c(LA,"pow2Range");c(DB,"matmulTileDims");c(G3,"enumerateRegisterBlockConfigs");c(LB,"goodness");c(q3,"buildRegisterBlockedMatmul");c(j3,"createMatmulRegisterBlockGPUSketch");c(W3,"analyzePureMatmul");PB=new WeakMap;c(FB,"richMatmulSketches")});function K3(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 __(e){let t=[];K3(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 PA(e){let t=[];for(let s of e){if(!s||s.type!=="VariableNode")return null;t.push(s.name)}return t}function Ip(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]&&Ip(e[n],t,s);if(Array.isArray(e.args))for(let n of e.args)Ip(n,t,s);if(Array.isArray(e.indices))for(let n of e.indices)Ip(n,t,s);if(Array.isArray(e.stmts))for(let n of e.stmts)Ip(n,t,s);return s}function of(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]&&of(e[s],t);if(Array.isArray(e.args))for(let s of e.args)of(s,t);if(Array.isArray(e.indices))for(let s of e.indices)of(s,t);if(Array.isArray(e.stmts))for(let s of e.stmts)of(s,t);return t}function w_(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(S=>S!==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(of(r.node.body,h),of(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(S=>!l.reductionLoopVars.has(S.loopVar.name)).map(S=>S.loopVar.name),g=PA(p.indices);if(!g||g.join(",")!==m.join(","))return null;let y=u.loops.map(S=>S.loopVar.name);if(y.length!==m.length)return null;let _=PA(d.indices);if(!_||_.join(",")!==y.join(","))return null;let w=Ip(d.value,i,[]);if(w.length===0)return null;for(let S of w){let b=PA(S.indices);if(!b||b.join(",")!==_.join(","))return null}return a}var DA=P(()=>{c(K3,"walkBlocks");c(__,"buildBlockDAG");c(PA,"indexVarNames");c(Ip,"collectLoads");c(of,"collectLoadedBuffers");c(w_,"findFusibleConsumer")});function FA(e){return e.isGPU()?IB():CB()}function H3(e){return e.isGPU()?kB():vB()}function Y3(e,t,s,n){let r=Ue(e,t);if(!r)return[FA(s)];let i=[],o=SB(r,dB(s));if(o&&i.push(o),s.kind===Pe.CPU){let a=xB(r,pB);a&&i.push(a);let l=RB(r);l&&i.push(l);let u=n?w_(e,n,t,Ue):null;u&&i.push(NB(u))}return i.push(FA(s)),i}function $A(e,{priority:t=100}={}){UA.push({matches:e.matches,derive:e.derive,priority:t}),UA.sort((s,n)=>s.priority-n.priority)}function $B(e,t,s,n={}){if(n.richGpu&&s.isGPU()){let o=FB(e,t,s);if(o!==null)return o}if(s.kind!==Pe.CPU&&!s.isGPU())return[];let r=p_(e,t),i=__(e);for(let o of UA)if(o.matches(r,s))return o.derive(e,t,s,i);return[]}var UA,zB=P(()=>{_i();Zo();d_();CA();AB();OB();UB();DA();c(FA,"reductionSketch");c(H3,"elementwiseSketch");c(Y3,"deriveMultiLevel");UA=[];c($A,"registerSketchRule");$A({matches:c(e=>e.hasReduction&&e.spatial>=1&&e.reads>=2,"matches"),derive:Y3},{priority:10});$A({matches:c(e=>e.hasReduction,"matches"),derive:c((e,t,s)=>[FA(s)],"derive")},{priority:20});$A({matches:c(()=>!0,"matches"),derive:c((e,t,s)=>[H3(s)],"derive")},{priority:30});c($B,"deriveSketches")});function zA(e,t,s,n,r={}){return $B(e,t,s,r)}var VB=P(()=>{zB();c(zA,"getSketchesForBlock")});function b_(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 S_(e,t,s,n=null){let r=n||b_(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(ns(i.body,l),i.initBody&&ns(i.initBody,l),o.push(l.join(";")),i.writes.length===1){let u=i.writes[0].buffer.name,f=[];for(let h of J3(r).get(u)||[]){if(h===i)continue;let p=[];ns(h.body,p),h.initBody&&ns(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),X3(o.join("|"))}function J3(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 ns(e,t){if(!(!e||typeof e!="object"))switch(e.type){case"BufferStoreNode":if(t.push("store"),e.indices)for(let s of e.indices)ns(s,t);ns(e.value,t);return;case"BufferLoadNode":if(t.push(`load:${e.buffer.name}`),e.indices)for(let s of e.indices)ns(s,t);return;case"MathOpNode":t.push(`math:${e.op}`),ns(e.a,t),e.b&&ns(e.b,t);return;case"CallExternNode":t.push(`call:${e.externName}`);for(let s of e.args)ns(s,t);return;case"CompareNode":t.push(`cmp:${e.direction}`),ns(e.a,t),ns(e.b,t);return;case"CastNode":t.push(`cast:${e.fromDtype}->${e.toDtype}`),ns(e.expr,t);return;case"IfThenElseNode":ns(e.condition,t),ns(e.thenBody,t),e.elseBody&&ns(e.elseBody,t);return;case"SeqNode":for(let s of e.stmts)ns(s,t);return;case"ForNode":ns(e.body,t);return;case"BlockNode":e.initBody&&ns(e.initBody,t),ns(e.body,t);return;case"LetStmtNode":ns(e.value,t),ns(e.body,t);return;default:return}}function X3(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 VA=P(()=>{c(b_,"buildBlockMap");c(S_,"computeWorkloadKey");c(J3,"readersByBuffer");c(ns,"collectBlockOps");c(X3,"fnv1a")});var GB,Op,Bp,qB=P(()=>{VA();GB="mlfw-codegen-1",Op=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 S_(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:GB,entries:t}}static deserialize(t){let s=new e(t.version);if(t.codegenVersion!==void 0&&t.codegenVersion!==GB)return s;for(let n of t.entries){let r=new Op(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 Aa(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 Aa(e.body,n)}if(e.type==="BlockNode")return Math.max(t,Aa(e.body,t),e.initBody?Aa(e.initBody,t):0);if(e.type==="SeqNode"){let s=t;for(let n of e.stmts)s=Math.max(s,Aa(n,t));return s}return e.type==="IfThenElseNode"?Math.max(Aa(e.thenBody,t),e.elseBody?Aa(e.elseBody,t):t):e.type==="AllocateNode"||e.type==="LetStmtNode"?Aa(e.body,t):t}function jB(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 Q3,x_,A_,WB=P(()=>{Ah();Q3=1e6;c(Aa,"maxSerialTripCount");x_=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(jB,"robustStats");A_=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 bi(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=jB(a),!(this.maxCv<=0||l.cv<=this.maxCv));u++);return new x_(l.median,l.min,a,o,l.trimmedMean,l.cv)}_runMeasured(t){if(t.shapeParams&&t.shapeParams.length>0||Aa(t.body,1)>Q3)return null;let s;try{s=new bi(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=jB(i);return new x_(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 E_,KB=P(()=>{E_=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 To(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 GA(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 HB(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=GA(n);for(let u=r.length-1;u>=0;u--){let f=r[u],h=new Tt(f.loopVar,GA(f.min),GA(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 Fo("__tune_"+t,l,o,a,[])}var qA=P(()=>{te();c(To,"clonePrimFunc");c(GA,"cloneBlockSubtree");c(HB,"extractBlockMini")});function tG(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 YB(e={}){return e.strategy==="random"?new jA({numTrials:e.numTrials,seed:e.seed,deadline:e.deadline}):new WA({populationSize:e.populationSize,numGenerations:e.numGenerations,mutationRate:e.mutationRate,eliteRatio:e.eliteRatio,seed:e.seed,deadline:e.deadline})}var T_,jA,WA,Z3,JB=P(()=>{T_=class{static{c(this,"SearchCandidate")}constructor(t,s,n){this.sketchName=t,this.params=s,this.score=n}},jA=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 T_(r.name,o,a.score))}}return n.sort((r,i)=>i.score-r.score),{candidates:n,population:null}}},WA=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 T_(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=[tG,...Z3],i=s;for(let o of r)i=o(i,t,n);return{sketch:t,params:i}}},Z3=[];c(tG,"defaultResampleMutator");c(YB,"createSearchStrategy")});function R_(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),eG.reduce((r,i)=>r*t[i],1)}var eG,N_,XB=P(()=>{gp();cx();TA();qA();JB();vA();eG=["threadIdx.x","threadIdx.y","threadIdx.z"];c(R_,"gpuThreadBlockSize");N_=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 nf,this.costModel=new rf(t.costModel.analytical,this.learnedModel)):(this.costModel=t.costModel,this.learnedModel=t.learnedModel);let n=s?null:HB(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=YB({...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 On(To(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=To(this.evalFunc),r=new On(n);if(t.instantiate(s)(r,this.evalBlockName,this.target),lu.validate(n).length>0)return null;let o=this.target.maxThreadsPerBlock;return this.target.isGPU&&this.target.isGPU()&&o&&R_(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=To(this.primFunc),s.instantiate(t.params)(new On(n),this.blockName,this.target),r=To(this.evalFunc),s.instantiate(t.params)(new On(r),this.evalBlockName,this.target)}catch{return null}let i=this.benchmarkRunner.run(n);return i?{result:i,features:Hl.extractStatements(r)}:null}_consider(t){(!this._best||t.measuredScore>this._best.measuredScore)&&(this._best=t)}}});var KA,v_,QB=P(()=>{KA=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}},v_=class{static{c(this,"TaskScheduler")}constructor(t=null){this.policy=t||new KA}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 sG(e){if(e.isCPU())return null;let t=Vv(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 k_,C_,ZB=P(()=>{gp();vA();VB();qB();WB();KB();VA();d_();DA();Zo();XB();qA();te();QB();Fm();c(sG,"resolveMeasurer");k_=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}},C_=class{static{c(this,"Autotuner")}constructor(t,s={}){this.target=t,this.config=s instanceof k_?s:new k_(s),this.config.hardwareMeasure&&(this.config.measurer=sG(t)),this.analyticalModel=new h_(t),this.learnedModel=new nf,this.costModel=new rf(this.analyticalModel,this.learnedModel),this.db=this.config.tuningDB instanceof Bp?this.config.tuningDB:new Bp,this.benchmarkRunner=this.config.enableBenchmark?new A_(t,{warmup:this.config.benchmarkWarmup,repeat:this.config.benchmarkRepeat,maxCv:this.config.benchmarkMaxCv,measurer:this.config.measurer}):null,this.scheduler=new v_(this.config.schedulerPolicy)}tune(t,s=null){let n=s?[s]:Cp(t.body),r=b_(t.body),i=new E_(this.config.timeBudgetMs,this.config.clock),o=new Map,a=new Map;for(let f of n){let h=S_(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=zA(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 N_({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 Op(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=To(t),r=new On(n),i=b_(n.body),o=__(n),a=new Set,l=[];for(let f of s)if(f[1].sketchName==="fused"){let h=w_(n,o,f[0],Ue);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=zA(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 Cp(t.func.body))if(!(s.has(r)||this._blockIsParallelized(t,r))){n||(n=new yo(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===K.THREAD_BINDING||r.kind===K.PARALLEL||r.kind===K.VECTORIZED)return!0;return!1}_scheduleIsValid(t){return!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock?!0:R_(t)<=this.target.maxThreadsPerBlock}_buildDefaultSchedule(t){try{let s=To(t);return new yo(this.target).applyToAllBlocks(new On(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=To(t);return n.instantiate(r)(new On(i),s,this.target),R_(i)<=this.target.maxThreadsPerBlock}catch{return!1}}}});var I_,tM=P(()=>{te();I_=class{static{c(this,"TensorVerifier")}constructor(){this.errors=[],this.boundVars=new Set}verify(t){if(this.errors=[],this.boundVars.clear(),!(t instanceof Fo))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 HA(e){let t=[];e.functionCount===0&&t.push(new $e("Module has no functions"));for(let s of e)Mp(s,t);return t}function Mp(e,t=[]){if(!e.entryBlock)return t.push(new $e("Function has no entry block",null,e)),t;e.entryBlock.arguments.length!==e.inputTypes.length&&t.push(new $e(`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 $e(`Block arg ${r} type ${Qs(i.type)} != expected ${Qs(o)}`,null,e))}let s=new Set;for(let r of e.entryBlock.arguments)s.add(r);eM(e.body,s);for(let r of e.body)sM(r,e,s,t);let n=e.getReturnOp();if(!n)t.push(new $e("Missing return op",null,e));else{n.numOperands!==e.outputTypes.length&&t.push(new $e(`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 M&&i instanceof M&&(i.dtype!==o.dtype&&t.push(new $e(`Return operand ${r} dtype ${i.dtype} != expected ${o.dtype}`,n,e)),i.shapeCompatible(o)||t.push(new $e(`Return operand ${r} shape incompatible: ${Qs(i)} vs ${Qs(o)}`,n,e)))}}return t}function eM(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)eM(r,t)}}}function sM(e,t,s,n){for(let r of e)nG(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=ut.get(i);o&&o.hasRegions&&(r.isTerminator()||n.push(new $e(`Block in ${i} region must end with terminator, got '${r.opName}'`,r,t)))}}}function nG(e,t,s,n){for(let i=0;i<e.numOperands;i++){let o=e.getOperand(i);if(!o){n.push(new $e(`Operand ${i} is null`,e,t));continue}if(!(o instanceof Za)){n.push(new $e(`Operand ${i} is not a Value`,e,t));continue}s.has(o)||n.push(new $e(`Operand ${i} used before definition`,e,t))}for(let i=0;i<e.numResults;i++){let o=e.getResult(i);o||n.push(new $e(`Result ${i} is null`,e,t)),o&&o.definingOp!==e&&n.push(new $e(`Result ${i} definingOp mismatch`,e,t))}let r=ut.get(e.opName);if(!r){!["return","yield"].includes(e.opName)&&!ut.has(e.opName)&&n.push(new $e(`Unknown op '${e.opName}'`,e,t));return}r.numOperands>=0&&e.numOperands!==r.numOperands&&n.push(new $e(`'${e.opName}' expects ${r.numOperands} operands, got ${e.numOperands}`,e,t)),r.numResults>=0&&e.numResults!==r.numResults&&n.push(new $e(`'${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 $e(`'${e.opName}' missing required attribute '${i.name}'`,e,t));if(r.hasRegions&&r.numRegions>0&&e.numRegions!==r.numRegions&&n.push(new $e(`'${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 $e(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 M&&l instanceof M&&(l.dtype!==u.dtype&&n.push(new $e(`Result ${a} dtype ${l.dtype} != inferred ${u.dtype}`,e,t)),l.shapeCompatible(u)||n.push(new $e(`Result ${a} shape ${Qs(l)} incompatible with inferred ${Qs(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);sM(a,t,o,n)}}}var $e,nM=P(()=>{Ft();Pd();cs();$e=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(HA,"verifyModule");c(Mp,"verifyFunction");c(eM,"collectDefinedValues");c(sM,"verifyBlock");c(nG,"verifyOperation")});var O_,YA,B_,JA,rM=P(()=>{yA();Ft();O_=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 YA(t)}},YA=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 M&&as(r.type.dtype)){let i=new O_;(this.mode==="entropy"||this.mode==="percentile")&&i.enableHistogram(),this.observers.set(r,i)}}for(let s of t.args)if(s.type instanceof M&&as(s.type.dtype)){let n=new O_;(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 JA(this.observers,this.mode)}},JA=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===Hn.PER_TENSOR_ASYMMETRIC;if(this._mode==="percentile"&&this._observers.get(t).histogram){if(i)return Yn.fromRange(r.min,r.max,s,n);let a=this._observers.get(t).histogram.computePercentileThreshold(.999);return Yn.fromRange(-a,a,s,n)}if(this._mode==="entropy"&&this._observers.get(t).histogram){if(i)return Yn.fromRange(r.min,r.max,s,n);let o=this._observers.get(t).histogram,a=n==="ui8"?256:255,l=o.computeEntropyThreshold(a);return Yn.fromRange(-l,l,s,n)}return Yn.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 XA,M_,iM=P(()=>{Di();Ft();XA=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}},M_=class e{static get name(){return"liveness"}static get depKey(){return"liveness"}static get dependencies(){return[le]}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||le.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 b of _){let A=a.get(b);if(A)for(let x of A)g.add(x)}let w=a.get(m),S=w.size;for(let b of g)w.add(b);for(let b=0;b<m.numResults;b++)w.delete(m.getResult(b));for(let b=0;b<m.numOperands;b++)w.add(m.getOperand(b));(g.size!==y||w.size!==S)&&(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 M){let w=_.type.sizeInBytes();w!==nt&&(y+=w)}p.set(m,y),y>f&&(f=y,h=m)}return new XA(a,l,i,o,f,h,p)}}});var rG,L_,P_,oM=P(()=>{_s();Rn();Ft();cs();ws();Di();iM();rG=new Set(["return","yield","constant","scalar_constant"]),L_=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}},P_=class extends zt{static{c(this,"RematerializationPass")}constructor(t={}){super("RematerializationPass"),this.config=t instanceof L_?t:new L_(t)}run(t,s){if(this.config.memoryBudget===1/0)return ot.UNCHANGED;let n=!1,r=0,i=0;for(;r<this.config.maxIterations;){let o=le.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>=dt.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,iterations:r,peakPressure:i,budget:this.config.memoryBudget,changed:n,level:dt.DEBUG}),n?ot.CHANGED:ot.UNCHANGED}_analyzeIntervalPressure(t,s){let n=s.topologicalOrder,{intervals:r}=M_.buildIntervals(t,n),i=[];for(let[d,m]of r){if(!(d.type instanceof M))continue;let g=d.type.sizeInBytes();g===nt||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||rG.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 M))return 0;let s=t.type.sizeInBytes();return s===nt?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 M){let l=o.type.sizeInBytes();l!==nt&&(r+=l)}}return r}_estimateRecomputeCost(t){let s=ut.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 M){let o=i.numel();if(o!==nt){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 M){let i=r.numel();if(i!==nt)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 Nt(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 Yl(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 Lp(e,{labelOf:t,sameLabel:s=c((o,a)=>o===a,"sameLabel"),canMerge:n=c(()=>!0,"canMerge"),onAttach:r=c(()=>{},"onAttach"),sort:i=Yl}){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 S=l.get(w);if(S)for(let b of S)y.push(b)}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 S=a.get(w);if(!S||!s(S.label,g)||!n(S,m,g))continue;let b=!1;for(let A of p(m))if(A!==S&&h(S,A)){b=!0;break}if(!b){S.ops.push(m),S.opSet.add(m),r(S,m),a.set(m,S),d(m,S),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 D_(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 QA(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 F_=P(()=>{c(Yl,"topoSortOps");c(Lp,"buildPartitions");c(D_,"computePartitionIO");c(QA,"topoSortPartitions")});function tE(e){let t=0;for(let s=0;s<e.numResults;s++){let n=e.getResult(s).type;n instanceof M&&n.isFullyStatic&&(t+=n.sizeInBytes())}return t}var ZA,af,eE,U_,aM=P(()=>{cs();Ft();F_();ZA=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+=tE(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(tE,"estimateOpMemory");af=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||{}}}},eE=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}},U_=class{static{c(this,"GraphPartitioner")}constructor(t){this.config=t instanceof af?t:new af(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 eE(i,a,o)}_buildSupportMap(){for(let t of this.config.targets){let s=new Set;for(let n of ut.names())this._targetSupportsOp(t,n)&&s.add(n);this._supportCache.set(t,s)}}_targetSupportsOp(t,s){if(t.hasLibraryOp(s))return!0;let n=ut.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=ut.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=ut.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 M&&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}=Lp(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)+tE(a)},"onAttach")}),r=new Map;for(let o of n){let a=new ZA(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=tE(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 M&&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 $_,z_,lM=P(()=>{_s();Rn();Do();aM();F_();ws();$_=class extends zt{static{c(this,"GraphPartitionPass")}constructor(t={}){super("GraphPartitionPass"),this.partitionerConfig=new af(t),this.partitionResult=null}run(t,s){if(this.partitionerConfig.targets.length<2)return ot.UNCHANGED;let n=new U_(this.partitionerConfig);return this.partitionResult=n.partition(t),this.partitionResult.numPartitions<=1?ot.UNCHANGED:(this._annotateOps(),this._insertTransferOps(t),this.trace&&this.trace.level>=dt.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,numPartitions:this.partitionResult.numPartitions,transferEdges:this.partitionResult.transferEdges.length,level:dt.DEBUG}),t.bumpVersion(),ot.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 Nt("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}},z_=class extends zt{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 ot.UNCHANGED;let r=this._getModule(t);if(!r)return ot.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>=dt.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,partitions:n.size,subFunctions:i.length,level:dt.DEBUG}),t.bumpVersion(),ot.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}=D_(o,i.ops),u=a.map(_=>_.type),f=l.map(_=>_.type),h=`${t.name}_partition_${r}`,p=new qs(h,u,f);p._partitionTarget=i.target;let d=new Map;for(let _=0;_<a.length;_++)d.set(a[_],p.args[_]);let m=Yl(i.ops);for(let _ of m){let w=[];for(let x=0;x<_.numOperands;x++){let N=d.get(_.getOperand(x));N?w.push(N):w.push(_.getOperand(x))}let S=[];for(let x=0;x<_.numResults;x++)S.push(_.getResult(x).type);let b=_.regions.length>0?_.regions.map(x=>gi(x)):null,A=new Nt(_.opName,w,S,new Map(_.attributes),b);p.entryBlock.pushOp(A);for(let x=0;x<_.numResults;x++)d.set(_.getResult(x),A.getResult(x))}let g=l.map(_=>d.get(_)||_),y=new Nt("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 Fi(e){return iG.has(e.opName)}function cM(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 aG(e){let t=e.definingOp;if(t&&t.opName==="broadcast"){let s=t.getOperand(0);t=s&&s.definingOp}if(t&&Fi(t)){let s=t.getAttr("value");if(typeof s=="number")return s}return 0}function sE(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&&Fi(l)||u&&Fi(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 uM(e){return{labelOf:c(t=>e.get(t),"labelOf"),canMerge:c((t,s)=>Math.max(t.maxBuf||0,cM(s))<=oG,"canMerge"),onAttach:c((t,s)=>{t.maxBuf=Math.max(t.maxBuf||0,cM(s))},"onAttach")}}function G_(e,t,s){let n=e.opSet,r=Yl(e.ops),{inputs:i,outputs:o,constDefs:a}=D_(n,r,{pullConstants:!0,isConstant:Fi});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 qs(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 Nt("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 V_(e){if(lG.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(V_(n))return!0}}return!1}function cG(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 fM(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=V_(i)?cG(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(!(Pp.has(i.opName)||!V_(i))){for(let o=0;o<i.numOperands;o++)if(r(i.getOperand(o).definingOp)>t)return!0}return!1}function hM(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&&Fi(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:aG(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 lf(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())Pp.has(g.opName)||Fi(g)||(V_(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}=Lp(r,uM(i));if(a.length<2)return null;let u=QA(a,l);if(!u)return null;let f=s.name,h=[],p=new Map,d=0;for(let g of u){let y=G_(g,f+"_p"+d++,p);if(!y)return null;h.push(y)}let m=hM(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 pM(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(Pp.has(g.opName)||Fi(g))continue;let y=sE(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}=Lp(n,uM(r));if(a.length<2)return null;let u=QA(a,l);if(!u)return null;let f=t.name,h=[],p=0;for(let g of u){let y=G_(g,f+"_p"+p++,i);if(!y)return null;h.push(y)}let d=hM(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)),S=g.outputs.indexOf(g.dotOp.getResult(0));_<0||w<0||S<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+S})}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 iG,Pp,oG,lG,q_=P(()=>{Do();Rn();F_();iG=new Set(["constant","scalar_constant"]),Pp=new Set(["return","yield"]);c(Fi,"isConstantOp");oG=32*1024;c(cM,"maxResultBytes");c(aG,"constScalarOf");c(sE,"cublasDotInfo");c(uM,"bufferLimitedConfig");c(G_,"materializePartition");lG=new Set(["dot","cublas_gemm","reduce"]);c(V_,"containsBoundaryOp");c(cG,"maxBoundaryResultBytes");c(fM,"hasDependentBoundaries");c(hM,"buildExecutionPlan");c(lf,"splitGraphForNative");c(pM,"splitGraphForCublas")});function mM(e){return dM.push(e),e}var dM,j_,gM=P(()=>{_s();dM=[];c(mM,"registerBackendOpRewrite");j_=class extends zt{static{c(this,"BackendOpRewritePass")}constructor(t={}){super(t.name||"BackendOpRewritePass"),this.config=t,this.rewrites=t.rewrites||dM}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?ot.CHANGED:ot.UNCHANGED}}});var uG,W_,yM=P(()=>{Rn();q_();gM();uG=mM({name:"dot->cublas_gemm",match:c(e=>e.opName==="dot"&&e.numOperands===2&&sE(e)!==null,"match"),build:c((e,t)=>{let s=e.results.map(r=>r.type),n=new Nt("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")}),W_=class extends j_{static{c(this,"CublasRewritePass")}constructor(t={}){super({...t,name:"CublasRewritePass",rewrites:[uG]})}}});function Dp(e){let t=1;for(let s of e){if(typeof s!="number"||s<0)return-1;t*=s}return t}function Fp(e){return e==="f16"||e==="i16"?2:e==="i8"||e==="u8"?1:4}function fG(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=Dp(f.shape);if(h<0)return!0;let p=h*Fp(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?Dp(u.shape):-1;f>0&&(a+=f*Fp(u.dtype))}return 3*r+2*a>n}function hG(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 S=0;S<w.numOperands;S++){let b=w.getOperand(S),A=b.definingOp;if(!l.has(b)&&!(A&&a.has(A))){if(A&&Fi(A)){p.has(A)||(p.add(A),h.push(A));continue}f.has(b)||(f.add(b),u.push(b))}}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 qs(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 Nt("return",_,[])),{bodyFunc:g,captured:u,numCarry:r,numYs:m.length-r})}function pG(e,t,s,n,r){let{getSlot:i,newSlot:o,steps:a,addedFuncs:l}=n,u=new dn(e.name+"_mod");u.addFunction(e);let f=lf(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(b=>{let A=p+b;return d.has(A)?d.get(A):h.argSlots[A]},"retSlot"),y=new Map;for(let b=0;b<p;b++)y.set(h.argSlots[b],i(t[b]));for(let b of h.intermediates)y.set(b.slot,o(b.shape,b.dtype));for(let b=0;b<s.length;b++){let A=g(b);y.has(A)||y.set(A,i(s[b]))}let _=c(b=>{let A=y.get(b);return A===void 0?null:A},"mapSlot"),w=[],S=!0;for(let b of h.steps){let A=b.inputSlots.map(_),x=b.outputSlots.map(_);if(A.includes(null)||x.includes(null)){S=!1;break}w.push({name:b.name,inputSlots:A,outputSlots:x})}if(S){for(let b of w)a.push(b);for(let b of u.functions())l.push(b);return!0}}}return a.push({name:e.name,inputSlots:t.map(i),outputSlots:s.map(i)}),l.push(e),!0}function _M(e,t,s){let n=G_({ops:e,opSet:new Set(e)},t,new Map);return n?pG(n.subFunc,n.inputs,n.outputs,s,1):!1}function dG(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=hG(e,t);if(!u)return!1;let{bodyFunc:f,captured:h,numCarry:p,numYs:d}=u,m=e.getAttr("num_xs"),g=new dn(t+"_mod");g.addFunction(f);let y=lf(g,2),_=y?y.plan:null,w=[],S=[];for(let G=0;G<p;G++){let H=e.getOperand(m+G).type;w.push(H.shape),S.push(H.dtype)}let b=[],A=[];for(let G=0;G<m;G++){let H=e.getOperand(G).type;b.push(H.shape.slice(1)),A.push(H.dtype)}let x=[],N=[];for(let G=0;G<d;G++){let H=e.getResult(p+G).type;x.push(H.shape.slice(1)),N.push(H.dtype)}let I=w.map((G,H)=>r(G,S[H])),E=w.map((G,H)=>r(G,S[H])),T=b.map((G,H)=>r(G,A[H])),B=[],k=[],O=[],D=[];for(let G=0;G<p;G++)k.push(n(e.getOperand(m+G)));for(let G=0;G<p;G++)O.push(n(e.getResult(G)));for(let G of h)D.push(n(G));let q=[],z=[];for(let G=0;G<m;G++)q.push(n(e.getOperand(G)));for(let G=0;G<d;G++)z.push(n(e.getResult(p+G)));let j=i.length;if(!_)B=x.map((G,H)=>r(G,N[H])),i.push({name:f.name,inputSlots:[...T,...I,...D],outputSlots:[...E,...B]}),a.push(f);else{let G=m+p+h.length,H=new Map;for(let V of _.returnFixups||[]){if(V.kind!=="copy")return!1;H.set(V.pos,V.srcSlot)}let kt=c(V=>{let Q=G+V;return H.has(Q)?H.get(Q):_.argSlots[Q]},"bodyReturnSlot"),wt=new Map;for(let V=0;V<m;V++)wt.set(_.argSlots[V],T[V]);for(let V=0;V<p;V++)wt.set(_.argSlots[m+V],I[V]);for(let V=0;V<h.length;V++)wt.set(_.argSlots[m+p+V],D[V]);for(let V of _.intermediates)wt.set(V.slot,r(V.shape,V.dtype));for(let V=0;V<p;V++){let Q=kt(V);wt.has(Q)||wt.set(Q,E[V])}for(let V=0;V<d;V++){let Q=kt(p+V),xt=wt.get(Q);xt===void 0&&(xt=r(x[V],N[V]),wt.set(Q,xt)),B.push(xt)}let bt=c(V=>{let Q=wt.get(V);return Q===void 0?null:Q},"mapSlot");for(let V of _.steps){let Q=V.inputSlots.map(bt),xt=V.outputSlots.map(bt);if(Q.includes(null)||xt.includes(null))return!1;i.push({name:V.name,inputSlots:Q,outputSlots:xt})}for(let V of g.functions())a.push(V)}let Y=i.length,W=e.getOperand(0).type.shape[0];return typeof W!="number"||W<0?!1:(o.push({T:W,loopStart:j,loopEnd:Y,carry:w.map((G,H)=>({a:I[H],b:E[H],initSlot:k[H],finalSlot:O[H],bytes:Dp(G)*Fp(S[H])})),xs:T.map((G,H)=>({xtSlot:G,xsSlot:q[H],stepBytes:Dp(b[H])*Fp(A[H])})),ys:B.map((G,H)=>({ytSlot:G,ysSlot:z[H],stepBytes:Dp(x[H])*Fp(N[H])}))}),!0)}function nE(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 k of n.ops())k.opName==="scan"&&i.push(k);if(i.length===0)return null;let o=new Set,a=c(k=>{if(!(!k||o.has(k))){o.add(k);for(let O=0;O<k.numOperands;O++)a(k.getOperand(O).definingOp)}},"visit");for(let k=0;k<r.numOperands;k++)a(r.getOperand(k).definingOp);for(let k of i)if(!o.has(k))return null;if(!s){let k=!1;for(let O of i){let D=O.regions[0];if(D&&D.entryBlock&&fG(O,D,t)){k=!0;break}}if(!k)return null}let l=[];for(let k of o)Fi(k)||Pp.has(k.opName)||l.push(k);let u=Yl(l),f=new Map,h=0,p=c(k=>{let O=f.get(k);return O===void 0&&(O=h++,f.set(k,O)),O},"getSlot");for(let k of n.args)p(k);let d=[],m=c((k,O)=>{let D=h++;return d.push({slot:D,shape:[...k],dtype:O}),D},"newSlot"),g=[],y=[],_=[],w={getSlot:p,newSlot:m,steps:g,scanLoops:y,addedFuncs:_},S=new Set(i),b=[],A=0,x=0;for(let k of u)if(S.has(k)){if(b.length&&!_M(b,n.name+"_seg"+A++,w)||(b=[],!dG(k,n.name+"_scan"+x++,w)))return null}else b.push(k);if(b.length&&!_M(b,n.name+"_seg"+A++,w))return null;let N=n.args.map(p),I=[],E=new Set;for(let k=0;k<r.numOperands;k++){let O=r.getOperand(k),D=N.length;if(f.has(O)&&!(O.isBlockArgument&&O.isBlockArgument())){let q=p(O);E.has(q)?(N.push(h++),I.push({pos:D,kind:"copy",srcSlot:q})):(E.add(q),N.push(q))}else if(O.isBlockArgument&&O.isBlockArgument())N.push(h++),I.push({pos:D,kind:"copy",srcSlot:p(O)});else return null}let T=new Set(N),B=new Set;for(let[k,O]of f)if(!(T.has(O)||B.has(O))){if(B.add(O),!k.type||!k.type.isFullyStatic)return null;d.some(D=>D.slot===O)||d.push({slot:O,shape:[...k.type.shape],dtype:k.type.dtype})}e.removeFunction(n.name);for(let k of _)e.addFunction(k);return{plan:{numSlots:h,argSlots:N,intermediates:d,steps:g,returnFixups:I,scanLoops:y}}}var wM=P(()=>{Rn();Do();sl();q_();c(Dp,"numel");c(Fp,"dtypeBytes");c(fG,"isScanOversized");c(hG,"buildScanBodyFunction");c(pG,"inlineNativeSplit");c(_M,"emitSegment");c(dG,"emitScanLoop");c(nE,"splitGraphForScan")});function iE(e){rE.push(e),rE.sort((t,s)=>(t.priority??100)-(s.priority??100))}function mG(e){for(let t of rE)if(t.applies(e))return t;return null}function bM(e,t){let s=mG(t);return s?s.run(e,t):null}var rE,SM=P(()=>{q_();wM();rE=[];c(iE,"registerGraphSplitStrategy");c(mG,"selectGraphSplitStrategy");c(bM,"splitGraph");iE({name:"cublas",priority:10,applies:c(e=>e.config.matmulBackend==="cublas","applies"),run:c(e=>pM(e),"run")});iE({name:"cuda-native-matmul-chain",priority:20,applies:c(e=>e.cudaMatmulChain,"applies"),run:c(e=>lf(e),"run")});iE({name:"webgpu",priority:30,applies:c(e=>e.isWebGPU,"applies"),run:c((e,t)=>{let s=nE(e,t.target);return!s&&fM(e,t.target.maxThreadsPerBlock||256)&&(s=nE(e,t.target,!0),s||(s=lf(e,2))),s},"run")})});function oE(e,t,s){return gG.filter(n=>n.phase===e).sort((n,r)=>n.priority-r.priority).map(n=>n.factory(t,s)).filter(Boolean)}var gG,xM=P(()=>{gG=[];c(oE,"graphPassesForPhase")});function yG(e){let t=[...e.entries()];return t.sort((s,n)=>s[0]<n[0]?-1:s[0]>n[0]?1:0),t}function K_(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 M?Qs(e):Array.isArray(e)?e.length===0?"[]":Array.isArray(e[0])?"["+e.map(t=>K_(t)).join(", ")+"]":"["+e.map(t=>K_(t)).join(", ")+"]":typeof e=="object"&&e.constructor===Object?"{"+Object.entries(e).sort((s,n)=>s[0].localeCompare(n[0])).map(([s,n])=>`${s}: ${K_(n)}`).join(", ")+"}":String(e)}function AM(e){return new Up().printModule(e)}var Up,aE=P(()=>{Ft();Up=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)}: ${Qs(o.type)}`),i=t.outputTypes.map(o=>Qs(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)}: ${Qs(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 yG(t.attributes))o.push(`${a} = ${K_(l)}`);i+=" {"+o.join(", ")+"}"}if(t.numResults>0){let o=[];for(let a=0;a<t.numResults;a++)o.push(Qs(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)}: ${Qs(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(yG,"sortedEntries");c(K_,"formatAttrValue");c(AM,"printModule")});function cE(e){return new lE().print(e)}var lE,EM=P(()=>{lE=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(cE,"printTensorIR")});function TM(e,t){let s=new Cm;for(let[,n]of e.bufferMap)s.paramBuffers.add(n.name);return _G(e.body,s,t),bG(e,s,t),SG(e.body,s),s}function _G(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===K.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,Vo(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&&!Ev(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,Vo(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 bG(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*Lo(o.dtype);let u=1;for(let f of o.shape)u*=typeof f=="number"&&f>0?f:1;return Math.max(wG,u*Lo(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 SG(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 wG,NM=P(()=>{Im();Nn();te();c(TM,"scanMetadata");c(_G,"walkTree");wG=65536;c(bG,"computeMemoryLayout");c(SG,"detectZeroBuffers")});function $p(e,t,s){let n=typeof e.offset=="number"?e.offset:0;if(t.length===0)return new U(n);if(t.length===1)return n===0?t[0]:new $("+",t[0],new U(n));let r=[];n!==0&&r.push(new U(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 $("*",o,new U(a)));else{let l=xG(e,i,s);r.push(new $("*",o,l))}}return r.length===0?new U(0):r.reduce((i,o)=>new $("+",i,o))}function xG(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 U(i)):n.push(AG(e,r,s))}return n.length===0?new U(1):n.reduce((r,i)=>new $("*",r,i))}function AG(e,t,s){if(s){let n=`${e.name}:${t}`,r=s.get(n);if(r)return new ve(r.name,"index")}return new U(1)}var RM=P(()=>{te();c($p,"flattenIndex");c(xG,"computeDynamicStride");c(AG,"resolveShapeParam")});function vM(e,t){let s=TM(e,t),n={shapeParamMap:e.shapeParamMap,accCounter:0,metadata:s},r=Fr(e.body,n);return new vm(e.name,e.params,r,e.bufferMap,e.shapeParams,e.shapeParamMap,s)}function Fr(e,t){if(!e||typeof e!="object")return e;switch(e.type){case"ForNode":return EG(e,t);case"BlockNode":return TG(e,t);case"SeqNode":return NG(e,t);case"BufferStoreNode":return RG(e,t);case"LetStmtNode":return vG(e,t);case"AllocateNode":return kG(e,t);case"IfThenElseNode":return CG(e,t);case"WhileNode":return IG(e,t);case"EvaluateNode":return new mm(Mn(e.value,t));case"SyncThreadsNode":return e;default:return e}}function EG(e,t){let s=BG(e);if(s)return MG(e,s,t);let n=Fr(e.body,t);return new Tt(e.loopVar,e.min,e.extent,e.kind,n,e.threadTag)}function TG(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:Mn(i.binding,t)});let n=Fr(e.body,t),r=e.initBody?Fr(e.initBody,t):null;return s.length>0?r?(n=new At([new $c(s,r),new $c(s,n)]),r=null):n=new $c(s,n):r&&(n=new At([r,n])),n}function NG(e,t){let s=[];for(let n of e.stmts)s.push(Fr(n,t));return new At(s)}function RG(e,t){let s=$p(e.buffer,e.indices,t.shapeParamMap),n=Mn(e.value,t),r=e.buffer.dtype||kn(e.value);return new yh(e.buffer,s,n,r)}function vG(e,t){return new vn(e.variable,Mn(e.value,t),Fr(e.body,t))}function kG(e,t){return new tn(e.buffer,e.scope,Fr(e.body,t))}function CG(e,t){return new Zt(Mn(e.condition,t),Fr(e.thenBody,t),e.elseBody?Fr(e.elseBody,t):null)}function IG(e,t){return new Oc(e.condVar,Fr(e.condBody,t),Fr(e.loopBody,t))}function Mn(e,t){if(!e||typeof e!="object"||!e.type)return e;switch(e.type){case"BufferLoadNode":{let s=$p(e.buffer,e.indices,t.shapeParamMap),n=new gh(e.buffer,s,e.buffer.dtype);return so(n),n}case"MathOpNode":{let s=Mn(e.a,t),n=e.b?Mn(e.b,t):null,r={...e,a:s,b:n};return Object.setPrototypeOf(r,Object.getPrototypeOf(e)),so(r),r}case"CompareNode":{let s=Mn(e.a,t),n=Mn(e.b,t),r={...e,a:s,b:n};return Object.setPrototypeOf(r,Object.getPrototypeOf(e)),so(r),r}case"CastNode":{let s=Mn(e.expr,t),n={...e,expr:s};return Object.setPrototypeOf(n,Object.getPrototypeOf(e)),so(n),n}case"CallExternNode":{let s=e.args.map(r=>Mn(r,t)),n={...e,args:s};return Object.setPrototypeOf(n,Object.getPrototypeOf(e)),so(n),n}case"IfThenElseNode":{let s=Mn(e.condition,t),n=Mn(e.thenBody,t),r=e.elseBody?Mn(e.elseBody,t):null,i={...e,condition:s,thenBody:n,elseBody:r};return Object.setPrototypeOf(i,Object.getPrototypeOf(e)),so(i),i}default:return so(e),e}}function BG(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"||!OG.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=uE(n.indices),l=uE(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=uE(u);return h.includes("?")||h.includes("$"+f)?null:{store:n,loadSide:i,valueSide:o,outerIndices:u,block:t,op:n.value.op}}function MG(e,t,s){let n=`_acc_${s.accCounter++}`,r=kn(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?Qn(t.valueSide,i):t.valueSide,a=$p(t.store.buffer,t.outerIndices,s.shapeParamMap),l=new gh(t.store.buffer,a,t.store.buffer.dtype);so(l);let u=Mn(o,s),f=$p(t.store.buffer,t.outerIndices,s.shapeParamMap),h=new yh(t.store.buffer,f,null,t.store.buffer.dtype),p=t.block.initBody?i.size>0?zp(t.block.initBody,i):t.block.initBody:null;return new km({localName:n,dtype:r,op:t.op,initLoad:l,loopVar:e.loopVar,extent:e.extent,loopKind:e.kind,body:u,flushStore:h,initBody:p?Fr(p,s):null})}function Qn(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=>Qn(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=Qn(e.a,t),n=e.b?Qn(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=Qn(e.a,t),n=Qn(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=Qn(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=>Qn(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=Qn(e.condition,t),n=Qn(e.thenBody,t),r=e.elseBody?Qn(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 zp(e,t){if(!e||typeof e!="object"||!e.type)return e;switch(e.type){case"BufferStoreNode":{let s=e.indices.map(i=>Qn(i,t)),n=Qn(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=>zp(r,t)),n={...e,stmts:s};return Object.setPrototypeOf(n,Object.getPrototypeOf(e)),n}case"ForNode":{let s=zp(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=Qn(e.condition,t),n=zp(e.thenBody,t),r=e.elseBody?zp(e.elseBody,t):null,i={...e,condition:s,thenBody:n,elseBody:r};return Object.setPrototypeOf(i,Object.getPrototypeOf(e)),i}default:return e}}function uE(e){return e.map(fE).join(",")}function fE(e){return e?e.type==="VariableNode"?"$"+e.name:e.type==="IntImmNode"?String(e.value):e.type==="MathOpNode"?"("+fE(e.a)+e.op+(e.b?fE(e.b):"")+")":"?":"?"}var OG,kM=P(()=>{Im();NM();RM();te();c(vM,"lowerToLIR");c(Fr,"lowerStmt");c(EG,"lowerForNode");c(TG,"lowerBlockNode");c(NG,"lowerSeqNode");c(RG,"lowerBufferStore");c(vG,"lowerLetStmt");c(kG,"lowerAllocate");c(CG,"lowerIfThenElse");c(IG,"lowerWhile");c(Mn,"lowerExpr");OG=new Set(["+","*","max","min"]);c(BG,"detectAccumulator");c(MG,"lowerAccumulator");c(Qn,"substituteVars");c(zp,"substituteVarsStmt");c(uE,"indicesKey");c(fE,"exprKey")});function CM(e){let t=[],s={errors:t,boundVars:new Set,path:[],bufferNames:new Set};if(!e||e.type!=="LIRFunc")return t.push(new Ui("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 Ur(e.body,s),t}function Ur(e,t){if(!(!e||typeof e!="object")){switch(t.path.push(e.type),e.type){case"ForNode":LG(e,t);break;case"SeqNode":for(let s of e.stmts)Ur(s,t);break;case"LIRFlatStoreNode":PG(e,t);break;case"LIRAccumulatorNode":DG(e,t);break;case"LIRBindingsNode":FG(e,t);break;case"LetStmtNode":{Ls(e.value,t);let s=t.boundVars.has(e.variable.name);t.boundVars.add(e.variable.name),Ur(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),Ur(e.body,t),e.buffer&&!s&&t.bufferNames.delete(e.buffer.name);break}case"IfThenElseNode":Ls(e.condition,t),Ur(e.thenBody,t),e.elseBody&&Ur(e.elseBody,t);break;case"WhileNode":Ur(e.condBody,t),Ur(e.loopBody,t);break;case"EvaluateNode":Ls(e.value,t);break;default:break}t.path.pop()}}function LG(e,t){e.extent?Ls(e.extent,t):t.errors.push(new Ui("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 Ui("ForNode missing loopVar",[...t.path])),Ur(e.body,t),e.loopVar&&!s&&t.boundVars.delete(e.loopVar.name)}function PG(e,t){e.buffer||t.errors.push(new Ui("LIRFlatStoreNode missing buffer",[...t.path])),Ls(e.offsetExpr,t),Ls(e.value,t)}function DG(e,t){e.localName||t.errors.push(new Ui("LIRAccumulatorNode missing localName",[...t.path])),e.dtype||t.errors.push(new Ui("LIRAccumulatorNode missing dtype",[...t.path]));let s=e.localName?t.boundVars.has(e.localName):!0;e.localName&&t.boundVars.add(e.localName),Ls(e.initLoad,t),Ls(e.body,t),Ur(e.flushStore,t),e.initBody&&Ur(e.initBody,t),e.localName&&!s&&t.boundVars.delete(e.localName)}function FG(e,t){let s=[];for(let n of e.bindings)Ls(n.expr,t),t.boundVars.has(n.name)||s.push(n.name),t.boundVars.add(n.name);Ur(e.body,t);for(let n of s)t.boundVars.delete(n)}function Ls(e,t){if(!(!e||typeof e!="object"||!e.type))switch(e.type){case"LIRFlatLoadNode":e.buffer||t.errors.push(new Ui("LIRFlatLoadNode missing buffer",[...t.path])),Ls(e.offsetExpr,t);break;case"MathOpNode":Ls(e.a,t),e.b&&Ls(e.b,t);break;case"CompareNode":Ls(e.a,t),Ls(e.b,t);break;case"CastNode":Ls(e.expr,t);break;case"CallExternNode":if(e.args)for(let s of e.args)Ls(s,t);break;case"IfThenElseNode":Ls(e.condition,t),Ls(e.thenBody,t),e.elseBody&&Ls(e.elseBody,t);break;case"VariableNode":e.name!==void 0&&!t.boundVars.has(e.name)&&t.errors.push(new Ui(`unbound variable '${e.name}'`,[...t.path]));break;case"IntImmNode":case"FloatImmNode":break;default:break}}var Ui,IM=P(()=>{Ui=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(CM,"verifyLIR");c(Ur,"verifyStmt");c(LG,"verifyForNode");c(PG,"verifyFlatStore");c(DG,"verifyAccumulator");c(FG,"verifyBindings");c(Ls,"verifyExpr")});function H_(e){return e&&typeof e=="object"?e:{}}function hE(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 OM(e,t,s={}){return new No({target:t,...s}).compileFunction(e)}var Y_,pE,No,J_=P(()=>{sl();ZS();sx();OO();LO();PO();ox();FO();VO();GO();qO();KO();QO();eB();Fc();gp();Zo();oB();Ah();Pg();ZB();tM();nM();rM();ex();oM();lM();yM();SM();xM();ws();aE();EM();kM();IM();ws();c(H_,"spread");c(hE,"omit");Y_=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,...H_(t.fusionConfig),...hE(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,...H_(t.autotuneConfig),...hE(n,"enabled","autotune")},this.matmulBackend=t.matmulBackend||"native";let i=t.quantization||{};this.quantization={enabled:i.enabled??t.enableQuantization??!1,...H_(t.quantizationConfig),...hE(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??dt.SILENT,sink:u.sink??null,irSnapshot:{afterGraphPasses:!1,afterLowering:!1,afterScheduling:!1,...H_(u.irSnapshot)}}}get usePartition(){return this.partition.enabled&&this.partition.targets.length>=2}},pE=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()}},No=class{static{c(this,"Compiler")}constructor(t){if(this.config=t instanceof Y_?t:new Y_(t),!this.config.target)throw new Error("Compiler requires a target")}compile(t){let s=new zd(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=bM(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:$v(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 pE(p,s,r)}compileFunction(t){let s=new dn("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 Bl;for(let m of oE("pre",this.config,this.config.target))o.addPass(m);o.addPass(new ou(this.config.target)),o.addPass(new iu("canonicalize",[new Ml,new jy({fastMath:this.config.optimization.fastMath}),new Wy,new Ky,new Pi],this.config.optimization.maxSimplifyIterations)),this.config.optimization.layout&&this.config.target&&(o.addPass(new r_({target:this.config.target})),o.addPass(new Pi)),this.config.quantization.enabled&&(o.addPass(new o_({...this.config.quantization,target:this.config.target})),o.addPass(new Ml),o.addPass(new Pi));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 Xy({target:this.config.target})),o.addPass(new Pi)),this.config.fusion.enabled){let m=this.config.fusion;m.strategy==="dominator"?o.addPass(new e_({target:this.config.target,...m})):(o.addPass(new Jy({target:this.config.target,cost:{launchOverheadUs:5},...m})),o.addPass(new Qy({maxFusionSize:this.config.target?.maxFusionSize,...m})),o.addPass(new Zy({maxFusionSize:this.config.target?.maxFusionSize,...m}))),o.addPass(new Pi)}if(this.config.matmulBackend==="cublas"&&o.addPass(new W_),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 P_(m))}for(let m of oE("post",this.config,this.config.target))o.addPass(m);o.setTrace(s),this.config.verifyMode==="full"&&o.setVerifyHook((m,g)=>(g?HA(m):Mp(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 Up;s.irDump("afterGraphPasses",m.printModule(t))}return f}_runPartitioning(t,s){let n=new Bl;n.addPass(new $_(this.config.partition)),n.addPass(new z_({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=mh(l,this.config.target);s.functionEvent("lowering",l.name,{durationMs:performance.now()-u}),a.push(f),s.shouldSnapshot("afterLowering")&&s.irDump("afterLowering:"+l.name,cE(f))}catch(u){let f=new Ns("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 C_(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 Ns("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 yo(this.config.target,null,s);for(let u of t)if(!r.has(u.name)&&!u.cublasInfo)try{let f=performance.now(),h=new On(u);l.applyToAllBlocks(h),s.functionEvent("scheduling",u.name,{durationMs:performance.now()-f})}catch(f){let h=new Ns("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,cE(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 u_({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 Ns("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=Mp?Mp(l):[];if(u.length>0){let f=u.map(h=>h.toString()).join("; ");r.push(new Ns("verification",l.name,f)),i.add(l.name),n.errorEvent("verification",l.name,f)}}return}let a=HA(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 I_;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 Ns("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=vM(l,this.config.target);if(l.cublasInfo&&(f.cublasInfo=l.cublasInfo),l.gpuRegisterBlocked&&(f.gpuRegisterBlocked=!0),this.config.verifyMode==="full"){let h=CM(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 Ns("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 ru("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 bi(d,u)),f.get(d.name)),"getBackend"),p=l?null:new bi(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(S=>S.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 Ns("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(OM,"compileGraph")});function $G(e){return(t,...s)=>{let n=zl();if(!n)throw new Error(`TRACING dispatch key active but no tracer is set for op '${e}'`);let r=[],i={},o=UG[e],a=0,l=c(f=>f instanceof we||f&&f._impl,"isTensor"),u=c(f=>{f instanceof we||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 BM(){let e=We.listOps();for(let t of e){let s=We.findOp(t);if(!s)continue;let n=s.name,r=qn.fromUnboxed($G(n));s.entry.registerKernel(ft.TRACING,r)}}var UG,MM=P(()=>{rr();Jf();Xa();Tp();Ul();UG={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($G,"_tracingKernel");c(BM,"registerTracingDispatch")});function VG(e){return e&&e.shape&&e.shape.length>=2&&zG.has(e.dtype)}function LM(e,t,s=VG){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 Nt("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 zG,PM=P(()=>{Rn();zG=new Set(["f16","f32","f64"]);c(VG,"defaultWeightPredicate");c(LM,"foldWeightParams")});function GG(){DM||(BM(),DM=!0)}function qG(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 tw(e,t,s){GG();let n=s?.name||e.name||"traced",r=new Fy(n),i=qG(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=nr.fromKey(ft.TRACING),f=GN(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 gE(e,t,s){let n=tw(e,t,s);return n&&typeof n.then=="function"?n.then(r=>r.graph):n.graph}function jG(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]=Bn(t[g]);let a=e.capturedParams,l=new Array(a.length);for(let g=0;g<a.length;g++)l[g]=Bn(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=xs(y),S=Se(_);h[g]=new S(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 Li(o[g],t[g].shape,t[g].dtype);for(let g=0;g<l.length;g++)d[m++]=new Li(l[g],a[g].shape,a[g].dtype);for(let g=0;g<h.length;g++)d[m++]=new Li(h[g],p[g],u[g].dtype);return{funcName:r,device:i,outputTypes:u,outputArrays:h,outputShapes:p,allArgs:d}}function mE(e,t,s,n){if(t.length===1)return he(s[0],n[0],t[0].dtype,e);let r=new Array(t.length);for(let i=0;i<t.length;i++)r[i]=he(s[i],n[i],t[i].dtype,e);return r}function WG(e,t,s){let{funcName:n,device:r,outputTypes:i,outputArrays:o,outputShapes:a,allArgs:l}=jG(e,t,s),u=e.result.module.executionPlan;return u?e.result.module.runPlanAsync(u,l).then(()=>mE(r,i,o,a)):e.result.isAsync(n)?e.result.runAsync(n,...l).then(()=>mE(r,i,o,a)):(e.result.run(n,...l),mE(r,i,o,a))}function ew(e,t,s){if(s?.backward)return _compileWithBackward(e,t,s);let n=s?.target??ur(),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,S,b){if(!w||typeof w!="object"||w.repro)return w;try{w.repro={name:e.constructor?.name||"compiled",phase:b,target:n?.name,inputs:(S||[]).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 S=a?LM(w,Bn):w;return{result:new No(r).compile(S.graph),graph:S.graph,capturedParams:S.capturedParams,numUserInputs:S.numUserInputs,outputTypes:S.outputTypes,shapeEnv:S.shapeEnv,outputSymShapes:S.outputSymShapes}}c(f,"_finalize");function h(w,S){try{let b=tw((...A)=>e.forward(...A),w,{name:e.constructor.name||"compiled",dynamicShapes:S});return b&&typeof b.then=="function"?b.then(f,A=>{throw u(A,w,"compile")}):f(b)}catch(b){throw u(b,w,"compile")}}c(h,"_compileWith");function p(w){return h(w,i)}c(p,"_compile");function d(w){return w.map((S,b)=>({shape:S,dtype:t[b].dtype}))}c(d,"_bucketInputs");function m(w){for(let S=0;S<l.length;S++){let b=l[S];b.shapeEnv.bindInputShapes(w);let{passed:A}=b.shapeEnv.evaluateGuards();if(A)return b}return null}c(m,"_findCachedEntry");function g(w,S){try{let b=WG(w,S,w.shapeEnv);return b&&typeof b.then=="function"?b.then(void 0,A=>{throw u(A,S,"run")}):b}catch(b){throw u(b,S,"run")}}c(g,"_execute");function y(...w){let S=m(w);if(!S){let b=p(w);if(b&&typeof b.then=="function")return b.then(A=>(l.push(A),A.shapeEnv.bindInputShapes(w),g(A,w)));S=b,l.push(S),S.shapeEnv.bindInputShapes(w)}return g(S,w)}c(y,"compiledForward");let _=null;if(t){let w=[];if(o)for(let S of o)w.push(h(d(S),null));if(w.push(p(t)),w.some(S=>S&&typeof S.then=="function"))_=Promise.all(w).then(S=>{for(let b of S)l.push(b)});else for(let S of w)l.push(S)}return y.original=e,y.graph=w=>gE((...b)=>e.forward(...b),w||t,{name:e.constructor.name||"compiled",dynamicShapes:i}),y.source=()=>{if(l.length===0)return null;let w=l[0],S=w.result.listKernels();return S.length>0?w.result.getSource(S[0]):null},y.kernels=()=>l.length===0?[]:l[0].result.listKernels(),y.snippet=()=>{if(l.length===0)return null;let w=l[0],S=w.result.listKernels();return S.length>0?w.result.getSnippet(S[0]):null},y.result=()=>l.length>0?l[0].result:null,y._ready=_,y}var DM,sw=P(()=>{Tp();MM();rr();Tb();J_();_i();Mr();Pg();hr();Yr();PM();DM=!1;c(GG,"_ensureTracing");c(qG,"_normalizeDynamicShapes");c(tw,"_traceCore");c(gE,"trace");c(jG,"_prepareExecution");c(mE,"_wrapOutputs");c(WG,"executeCompiled");c(ew,"compile")});var ii,Vp=P(()=>{ii=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 mt(e,t){yE.set(e,t)}function Jl(e){return yE.get(e)||null}function nw(e){let t=yE.get(e);if(t)return t;if(_E.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 wE(e,t){FM.set(e,t)}function UM(e){return FM.get(e)||null}function bE(e){_E.add(e)}function rw(e){return _E.has(e)}var yE,_E,FM,zr=P(()=>{yE=new Map,_E=new Set;c(mt,"registerVJPRule");c(Jl,"getVJPRule");c(nw,"requireVJPRuleOrBarrier");FM=new Map;c(wE,"registerRegionVJP");c(UM,"getRegionVJP");c(bE,"registerGradientBarrier");c(rw,"isGradientBarrier")});function Gp(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 iw(e,t){let s=e.scalarConstant(0,t.type.dtype).getResult(0);return e.broadcast(s,t.type.shape,[]).getResult(0)}function $M(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 KG(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 ow(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 ii(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=nw(y.opName);if(!w)continue;let S={builder:e,op:y,operands:y.operands.map(f),results:y.results.map(A=>a.get(A.id)),gradOutputs:_,attrs:y.attributes},b=w(S);if(b)for(let A=0;A<y.numOperands;A++)b[A]&&p.accumulate(y.getOperand(A).id,xE(e,b[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 SE(e,t){for(let s of e.ops())if(HG.has(s.opName))throw new Error(`${t} VJP does not support nested control flow ('${s.opName}') in the body`)}function YG(e,t,s,n,r){let i=e.regions[0].blocks[0],o=e.regions[1].blocks[0];SE(i,"if"),SE(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=Gp(i),f=Gp(o),h=new Map(u.map(y=>[y.id,n(y)])),p=new Map(f.map(y=>[y.id,n(y)])),{gradFree:d}=ow(s,i,[],h,l,!1),{gradFree:m}=ow(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),S=m.get(y);if(!w&&!S)continue;let b=iw(s,_),A=s.broadcast(a,_.type.shape,[]).getResult(0);t.accumulate(y,s.select(A,w??b,S??b).getResult(0))}}function JG(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 XG(e,t,s,n,r,i=null){let o=e.regions[0].blocks[0];SE(o,"scan");let a=e.getAttr("num_carry"),l=e.getAttr("num_xs"),u=e.numResults-a,f=[];for(let T=0;T<l;T++)f.push(e.getOperand(T));let h=[];for(let T=0;T<a;T++)h.push(e.getOperand(l+T));let p=f[0].type.shape[0],d=Gp(o),m=f.map(n),g=h.map(n),y=new Map(d.map(T=>[T.id,n(T)])),_=new Map,w=c(T=>m.map(B=>$M(s,B,T)),"sliceX"),S=c((T,B)=>ow(s,o,[...T,...B],y,null,!0,_).forwardYields.slice(0,a),"stepForward"),b=[];for(let T=0;T<u;T++)b.push(t.get(e.getResult(a+T).id));let A=[];for(let T=0;T<a;T++){let B=t.get(e.getResult(T).id);A.push(B??iw(s,g[T]))}let x=new Map,N=m.map(()=>new Array(p)),I=c((T,B,k)=>{let O=[...B,...k],D=b.map(Y=>Y===null?null:$M(s,Y,T)),q=[...A,...D],{gradArgs:z,gradFree:j}=ow(s,o,O,y,q,!1,_);for(let Y=0;Y<l;Y++)N[Y][T]=z[Y]??iw(s,B[Y]);A=[];for(let Y=0;Y<a;Y++)A.push(z[l+Y]??iw(s,k[Y]));for(let[Y,W]of j){if(!W)continue;let G=x.get(Y);x.set(Y,G?s.add(G,W).getResult(0):W)}},"backwardStep"),E=JG(i,p);if(E){let T=Math.ceil(p/E),B=new Array(T),k=g;B[0]=k;for(let O=0;O<p;O++){k=S(w(O),k);let D=(O+1)/E;Number.isInteger(D)&&D<T&&(B[D]=k)}for(let O=T-1;O>=0;O--){let D=O*E,q=Math.min(D+E,p),z=[],j=[B[O]],Y=B[O];for(let W=D;W<q;W++){let G=w(W);z.push(G),Y=S(G,Y),j.push(Y)}for(let W=q-1;W>=D;W--)I(W,z[W-D],j[W-D])}}else{let T=g,B=[T],k=[];for(let O=0;O<p;O++){let D=w(O);k.push(D),T=S(D,T),B.push(T)}for(let O=p-1;O>=0;O--)I(O,k[O],B[O])}for(let T=0;T<l;T++)r.has(f[T].id)&&t.accumulate(f[T].id,KG(s,N[T],m[T].type.shape));for(let T=0;T<a;T++)r.has(h[T].id)&&t.accumulate(h[T].id,A[T]);for(let[T,B]of x)B&&r.has(T)&&t.accumulate(T,B)}var HG,zM=P(()=>{Vp();zr();qp();wE("scan",(e,t)=>XG(e,t.accumulator,t.builder,t.materialize,t.needsGrad,t.scanCheckpoint));wE("if",(e,t)=>YG(e,t.accumulator,t.builder,t.materialize,t.needsGrad));c(Gp,"regionFreeVars");c(iw,"zeroLike");c($M,"sliceStep");c(KG,"stackSteps");c(ow,"diffBodyStep");HG=new Set(["scan","while","if"]);c(SE,"assertNoNestedControlFlow");c(YG,"buildCondBackward");c(JG,"resolveSegmentLength");c(XG,"buildScanBackward")});function Wp(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=nw(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,xE(s,d[m],g.type.shape))}}}function VM(e){let t=[];for(let s of e.regions)s.blocks[0]&&t.push(...Gp(s.blocks[0]));return t}function xE(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 jp,Kp,qp=P(()=>{Do();di();Di();Vp();zr();zM();jp=new Set(["scan","if"]);c(Wp,"backpropOps");c(VM,"regionControlFlowFreeVars");c(xE,"reduceGradToOperandShape");Kp=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=le.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(x=>x.type),h=l.map(x=>x.type),p=[...f,...h],d=[];for(let x=0;x<o.length;x++)a.has(o[x].id)&&d.push(o[x].type);let m=new qs(`backward_${t.name}`,p,d),g=new Rs(m),y=m.args,_=y.slice(0,f.length),w=y.slice(f.length),S=new Map;for(let x=0;x<l.length;x++)S.set(l[x].id,w[x]);for(let x=0;x<o.length;x++)u.has(o[x].id)&&S.set(o[x].id,w[u.get(o[x].id)]);let b=new ii(g);for(let x=0;x<i.length;x++){let N=i[x];b.accumulate(N.id,_[x])}Wp(n,{accumulator:b,builder:g,needsGrad:a,resolveValue:c(x=>this._materialize(x,S,g),"resolveValue"),handleRegionOp:c(x=>{let N=UM(x.opName);return N?(N(x,{accumulator:b,builder:g,materialize:c(I=>this._materialize(I,S,g),"materialize"),needsGrad:a,scanCheckpoint:this._scanCheckpoint}),!0):!1},"handleRegionOp")});let A=[];for(let x=0;x<o.length;x++)if(a.has(o[x].id)){let N=b.get(o[x].id);if(N)A.push(N);else{let I=g.scalarConstant(0,o[x].type.dtype).getResult(0),E=g.broadcast(I,o[x].type.shape,[]).getResult(0);A.push(E)}}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(jp.has(o.opName)){for(let l=0;l<o.numOperands;l++)n.add(o.getOperand(l).id);for(let l of VM(o))n.add(l.id);continue}if(Jl(o.opName)&&!rw(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"||!Jl(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(jp.has(h.opName)){for(let p of h.operands)f(p);for(let p of VM(h))f(p);continue}if(Jl(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=le.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 T of l)for(let B of T.ops)if(jp.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(T=>T.id));for(let T of o)a.has(T.id)&&!u.has(T.id)&&(u.add(T.id),h.set(T.id,f.length),f.push(T));for(let T of l){for(let B of T.boundaryInputs)if(!p.has(B)&&!u.has(B)){u.add(B);let k=this._findValue(n,B);k&&(h.set(B,f.length),f.push(k))}for(let B of T.boundaryOutputs)if(!u.has(B)){u.add(B);let k=this._findValue(n,B);k&&(h.set(B,f.length),f.push(k))}}for(let T of i)u.has(T.id)||(u.add(T.id),h.set(T.id,f.length),f.push(T));let d=i.map(T=>T.type),m=f.map(T=>T.type),g=[...d,...m],y=[];for(let T=0;T<o.length;T++)a.has(o[T].id)&&y.push(o[T].type);let _=new qs(`backward_${t.name}`,g,y),w=new Rs(_),S=_.args,b=S.slice(0,d.length),A=S.slice(d.length),x=new Map;for(let T=0;T<f.length;T++)x.set(f[T].id,A[T]);let N=new ii(w);for(let T=0;T<i.length;T++)N.accumulate(i[T].id,b[T]);let I=new Map;for(let T of n)if(T.opName==="constant"){let B=T.getResult(0).type,k=w._buildOp("constant",[],[B],new Map(T.attributes),null);I.set(T.getResult(0).id,k.getResult(0))}for(let T=l.length-1;T>=0;T--){let B=l[T],k=new Map;for(let O of B.ops){let D=new Array(O.numOperands);for(let j=0;j<O.numOperands;j++){let Y=O.getOperand(j),W=k.get(Y.id)||x.get(Y.id)||I.get(Y.id);D[j]=W||Y}let q=O.results.map(j=>j.type),z=w._buildOp(O.opName,D,q,new Map(O.attributes),null);for(let j=0;j<O.numResults;j++)k.set(O.getResult(j).id,z.getResult(j))}Wp(B.ops,{accumulator:N,builder:w,needsGrad:a,resolveValue:c(O=>k.get(O.id)||x.get(O.id)||I.get(O.id)||O,"resolveValue")})}let E=[];for(let T=0;T<o.length;T++)if(a.has(o[T].id)){let B=N.get(o[T].id);if(B)E.push(B);else{let k=w.scalarConstant(0,o[T].type.dtype).getResult(0),O=w.broadcast(k,o[T].type.shape,[]).getResult(0);E.push(O)}}return w.returnOp(E),{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 Xl,aw=P(()=>{Xl=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 Hp,AE=P(()=>{Do();di();Di();Vp();zr();aw();qp();Hp=class{static{c(this,"JointGraphBuilder")}constructor(t={}){this._rematPolicy=t.rematPolicy||new Xl(t.remat||{}),this._checkpointPolicy=t.checkpointPolicy||null}build(t){if(this._checkpointPolicy)return this._buildCheckpointed(t);let n=le.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(b=>b.type),l=[...t.inputTypes,...a],u=[...t.outputTypes,...t.inputTypes],f=new qs(`joint_${t.name}`,l,u),h=new Rs(f),p=f.args,d=p.slice(0,t.inputTypes.length),m=p.slice(t.inputTypes.length),g=new Map;for(let b=0;b<o.length;b++)g.set(o[b].id,d[b]);for(let b of n){if(b.opName==="return")continue;let A=new Array(b.numOperands);for(let I=0;I<b.numOperands;I++){let E=b.getOperand(I);A[I]=g.get(E.id)||E}let x=b.results.map(I=>I.type),N=h._buildOp(b.opName,A,x,new Map(b.attributes),null);for(let I=0;I<b.numResults;I++)g.set(b.getResult(I).id,N.getResult(I))}let y=i.map(b=>g.get(b.id)),_=this._computeGradReachability(t,n),w=new ii(h);for(let b=0;b<i.length;b++)w.accumulate(i[b].id,m[b]);Wp(n,{accumulator:w,builder:h,needsGrad:_,resolveValue:c(b=>g.get(b.id)||b,"resolveValue")});let S=[];for(let b=0;b<o.length;b++){let A=w.get(o[b].id);if(A)S.push(A);else{let x=h.scalarConstant(0,o[b].type.dtype).getResult(0),N=h.broadcast(x,o[b].type.shape,[]).getResult(0);S.push(N)}}return h.returnOp([...y,...S]),{jointFunc:f,numForwardOutputs:i.length,numGradInputs:S.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)))&&Jl(o.opName)&&!rw(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(jp.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=le.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 qs(`joint_${t.name}`,l,u),h=new Rs(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 x=new Array(A.numOperands);for(let E=0;E<A.numOperands;E++){let T=A.getOperand(E);x[E]=g.get(T.id)||T}let N=A.results.map(E=>E.type),I=h._buildOp(A.opName,x,N,new Map(A.attributes),null);for(let E=0;E<A.numResults;E++)g.set(A.getResult(E).id,I.getResult(E))}let y=i.map(A=>g.get(A.id)),_=this._computeGradReachability(t,n),w=this._checkpointPolicy.segment(n,t),S=new ii(h);for(let A=0;A<i.length;A++)S.accumulate(i[A].id,m[A]);for(let A=w.length-1;A>=0;A--){let x=w[A],N=new Map;for(let I of x.ops){let E=new Array(I.numOperands);for(let k=0;k<I.numOperands;k++){let O=I.getOperand(k);E[k]=N.get(O.id)||g.get(O.id)||O}let T=I.results.map(k=>k.type),B=h._buildOp(I.opName,E,T,new Map(I.attributes),null);for(let k=0;k<I.numResults;k++)N.set(I.getResult(k).id,B.getResult(k))}Wp(x.ops,{accumulator:S,builder:h,needsGrad:_,resolveValue:c(I=>N.get(I.id)||g.get(I.id)||I,"resolveValue")})}let b=[];for(let A=0;A<o.length;A++){let x=S.get(o[A].id);if(x)b.push(x);else{let N=h.scalarConstant(0,o[A].type.dtype).getResult(0),I=h.broadcast(N,o[A].type.shape,[]).getResult(0);b.push(I)}}return h.returnOp([...y,...b]),{jointFunc:f,numForwardOutputs:i.length,numGradInputs:b.length}}}});var GM=P(()=>{});function qM(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 jM(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 WM=P(()=>{zr();mt("add",e=>{let t=e.gradOutputs[0];return[t,t]});mt("sub",e=>{let t=e.gradOutputs[0],s=e.builder.neg(t).getResult(0);return[t,s]});c(qM,"_minMaxVJP");mt("maximum",e=>qM(e,"ge"));mt("minimum",e=>qM(e,"le"));mt("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(jM,"_whereVJP");mt("where",jM);mt("select",jM);mt("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]});mt("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]});mt("neg",e=>{let t=e.gradOutputs[0];return[e.builder.neg(t).getResult(0)]});mt("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 KM=P(()=>{zr();mt("exp",e=>{let t=e.gradOutputs[0],s=e.results[0];return[e.builder.mul(t,s).getResult(0)]});mt("log",e=>{let t=e.gradOutputs[0],[s]=e.operands;return[e.builder.div(t,s).getResult(0)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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 HM=P(()=>{zr();mt("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]});mt("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]});mt("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(I=>I===1)&&a.every(I=>I===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]]],S=r.conv(t,_,[1,1],w).getResult(0),b=r.transpose(s,[1,0,2,3]).getResult(0),A=r.transpose(t,[1,0,2,3]).getResult(0),x=r.conv(b,A,[1,1],[g,y]).getResult(0),N=r.transpose(x,[1,0,2,3]).getResult(0);return[S,N]})});var YM=P(()=>{zr();mt("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 JM=P(()=>{zr();mt("reshape",e=>{let t=e.gradOutputs[0],[s]=e.operands,n=s.type.shape;return[e.builder.reshape(t,n).getResult(0)]});mt("reverse",e=>[e.builder.reverse(e.gradOutputs[0],e.op.getAttr("dimensions")).getResult(0)]);mt("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)]});mt("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]});mt("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)]});mt("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});mt("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]});mt("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]});mt("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 XM=P(()=>{zr();mt("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)]});mt("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)]});mt("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 bt=0;bt<a.length;bt++)l.includes(bt)||f.push(bt);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),S=e.builder.add(y,w).getResult(0),b=e.builder.rsqrt(S).getResult(0),A=e.builder.broadcast(b,a,f).getResult(0),x=[];for(let bt=0;bt<a.length;bt++)l.includes(bt)&&x.push(bt);let N=e.builder.broadcast(n,a,x).getResult(0),I=e.builder.mul(t,N).getResult(0),E=e.builder.scalarConstant(u,o).getResult(0),T=e.builder.broadcast(E,a,[]).getResult(0),B=e.builder.mul(T,I).getResult(0),k=e.builder.reduce(I,h,l,"sum").getResult(0),O=e.builder.broadcast(k,a,f).getResult(0),D=e.builder.mul(m,A).getResult(0),q=e.builder.mul(I,D).getResult(0),z=e.builder.reduce(q,h,l,"sum").getResult(0),j=e.builder.broadcast(z,a,f).getResult(0),Y=e.builder.mul(D,j).getResult(0),W=e.builder.sub(B,e.builder.add(O,Y).getResult(0)).getResult(0),G=e.builder.div(A,T).getResult(0),H=e.builder.mul(G,W).getResult(0),kt=e.builder.reduce(e.builder.mul(t,D).getResult(0),h,f,"sum").getResult(0),wt=e.builder.reduce(t,h,f,"sum").getResult(0);return[H,kt,wt]});mt("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 T=0;T<l;T++)u.push(T);u[l-2]=l-1,u[l-1]=l-2;let f=c(T=>o.transpose(T,u).getResult(0),"lastT"),h=c(T=>o.broadcast(o.scalarConstant(i,a).getResult(0),T,[]).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),S=o.reduce(w,_,[l-1],"sum").getResult(0),b=[];for(let T=0;T<l-1;T++)b.push(T);let A=o.broadcast(S,m.type.shape,b).getResult(0),x=o.mul(m,o.sub(y,A).getResult(0)).getResult(0),N=o.mul(x,h(x.type.shape)).getResult(0),I=o.matmul(N,n).getResult(0),E=o.matmul(f(N),s).getResult(0);return[I,E,g]});mt("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(N=>N[0]===0&&N[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(N=>n.reshape(n.broadcast(N,[h,p,d,g,m,y],[0,1,2,4]).getResult(0),_).getResult(0),"upsample"),S=w(t);if(r==="avg"){let N=n.broadcast(n.scalarConstant(g*y,s.type.dtype).getResult(0),_,[]).getResult(0);return[n.div(S,N).getResult(0)]}let b=w(e.results[0]),A=n.compare(s,b,"eq").getResult(0),x=n.broadcast(n.scalarConstant(0,s.type.dtype).getResult(0),_,[]).getResult(0);return[n.select(A,S,x).getResult(0)]});mt("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 O=0;O<u.length;O++)O!==o&&h.push(O);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),S=f.mul(w,g).getResult(0),b=f.mul(t,_).getResult(0),A=f.mul(b,g).getResult(0),x=f.reduce(f.mul(t,S).getResult(0),p,h,"sum").getResult(0),N=f.reduce(t,p,h,"sum").getResult(0),I=f.neg(f.reduce(A,p,h,"sum").getResult(0)).getResult(0),E=f.mul(f.mul(g,g).getResult(0),g).getResult(0),T=f.broadcast(f.scalarConstant(-.5,l).getResult(0),u,[]).getResult(0),B=f.mul(f.mul(f.mul(b,w).getResult(0),E).getResult(0),T).getResult(0),k=f.reduce(B,p,h,"sum").getResult(0);return[A,x,N,I,k]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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)]});mt("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 QM=P(()=>{zr();bE("stop_gradient");mt("stop_gradient",()=>[null]);for(let e of["compare","logical_not","argmax","argmin","iota"])bE(e)});var ZM=P(()=>{zr();Vp();qp();AE();aw();GM();WM();KM();HM();YM();JM();XM();QM()});var t1={};ne(t1,{compileWithBackward:()=>EE});function EE(e,t,s={}){let n=s.target??ur(),r=s.mode||"separate",i=s.rematPolicy||new Xl(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(E){let T=tw((...k)=>e.forward(...k),E,{name:e.constructor.name||"compiled",dynamicShapes:a}),B=c(k=>{let O=k.graph.functions().next().value,D=r==="joint"?m(O,k,i):d(O,k,i);return D.shapeEnv=k.shapeEnv,D.outputSymShapes=k.outputSymShapes,D},"finish");return T&&typeof T.then=="function"?T.then(B):B(T)}c(p,"_compile");function d(E,T,B){let k=new Kp({rematPolicy:B}),{backwardFunc:O,savedValues:D,gradInputIndices:q}=k.build(E),z=E.getReturnOp(),j=[...z.operands],Y=j.length,W=new Map(E.args.map((Et,je)=>[Et.id,je])),G=new Map(j.map((Et,je)=>[Et.id,je])),H=[],kt=new Map;for(let Et of D)W.has(Et.id)||G.has(Et.id)||kt.has(Et.id)||(kt.set(Et.id,Y+H.length),H.push(Et));H.length>0&&(z.erase(),new Rs(E).returnOp([...j,...H]),E.outputTypes=Object.freeze([...j.map(Et=>Et.type),...H.map(Et=>Et.type)]));let wt=D.map(Et=>W.has(Et.id)?{kind:"arg",index:W.get(Et.id)}:G.has(Et.id)?{kind:"output",index:G.get(Et.id)}:{kind:"output",index:kt.get(Et.id)}),bt=new dn("forward");bt.addFunction(E);let V=new No(o).compile(bt),Q=new dn("backward");Q.addFunction(O);let xt=new No(o).compile(Q);return{mode:"separate",fwdResult:V,bwdResult:xt,forwardFunc:E,backwardFunc:O,savedValues:D,savedSources:wt,numRealOutputs:Y,gradInputIndices:q,capturedParams:T.capturedParams,numUserInputs:T.numUserInputs,outputTypes:T.outputTypes}}c(d,"_compileSeparate");function m(E,T,B){let k=new Hp({rematPolicy:B}),{jointFunc:O,numForwardOutputs:D,numGradInputs:q}=k.build(E),z=new dn("joint");return z.addFunction(O),{mode:"joint",result:new No(o).compile(z),jointFunc:O,numForwardOutputs:D,numGradInputs:q,capturedParams:T.capturedParams,numUserInputs:T.numUserInputs,outputTypes:T.outputTypes,inputTypes:E.inputTypes}}c(m,"_compileJoint");function g(E,T){return E.outputSymShapes&&E.shapeEnv?E.shapeEnv.resolveSymbolicShape(E.outputSymShapes[T]):E.outputTypes[T].shape}c(g,"_resolveOutputShape");function y(E,T,B){let k=E.module||E;return k.executionPlan?k.runPlanAsync(k.executionPlan,B):E.isAsync(T)?E.runAsync(T,...B):(E.run(T,...B),null)}c(y,"_runK");function _(E,T){let k=E.fwdResult.listKernels()[0],O=T.length>0?T[0].device:"cpu",D=T.map(bt=>Bn(bt)),z=E.capturedParams.map(bt=>Bn(bt)),j=E.forwardFunc.outputTypes,Y=E.numRealOutputs,W=new Array(j.length),G=new Array(j.length);for(let bt=0;bt<j.length;bt++){let V=bt<Y?g(E,bt):j[bt].shape,Q=j[bt].dtype,xt=xs(V),Et=Se(Q);W[bt]=new Et(Math.max(xt,1)),G[bt]=V}let H=[...D,...z,...W],kt=c(()=>({results:Y===1?he(W[0],G[0],j[0].dtype,O):Array.from({length:Y},(V,Q)=>he(W[Q],G[Q],j[Q].dtype,O)),inputArrays:D,paramArrays:z,outputArrays:W,device:O}),"build"),wt=y(E.fwdResult,k,H);return wt?wt.then(kt):kt()}c(_,"_executeSeparateForward");function w(E,T,B){let O=E.bwdResult.listKernels()[0],D=T.map(Q=>Bn(Q)),q=E.savedValues,z=E.savedSources,j=[...B.inputArrays,...B.paramArrays],Y=new Array(q.length);for(let Q=0;Q<q.length;Q++){let xt=z[Q];Y[Q]=xt.kind==="arg"?j[xt.index]:B.outputArrays[xt.index]}let W=E.backwardFunc,G=W.outputTypes.length,H=new Array(G),kt=new Array(G);for(let Q=0;Q<G;Q++){let xt=W.outputTypes[Q].shape,Et=W.outputTypes[Q].dtype,je=xs(xt),Gn=Se(Et);H[Q]=new Gn(Math.max(je,1)),kt[Q]=xt}let wt=[...D,...Y,...H],bt=c(()=>H.map((Q,xt)=>he(Q,kt[xt],W.outputTypes[xt].dtype,B.device)),"build"),V=y(E.bwdResult,O,wt);return V?V.then(bt):bt()}c(w,"_executeSeparateBackward");function S(E){for(let T=0;T<l.length;T++){let B=l[T];B.shapeEnv.bindInputShapes(E);let{passed:k}=B.shapeEnv.evaluateGuards();if(k)return B}return null}c(S,"_findCachedEntry");function b(E,T){if(f=E,E.mode==="joint")return N(E,T);let B=_(E,T);return B&&B.then?B.then(k=>(u=k,k.results)):(u=B,B.results)}c(b,"_forwardWith");function A(E){let T=S(E);if(T)return b(T,E);let B=p(E);return B&&typeof B.then=="function"?B.then(k=>(l.push(k),k.shapeEnv.bindInputShapes(E),b(k,E))):(l.push(B),B.shapeEnv.bindInputShapes(E),b(B,E))}c(A,"_runForward");function x(...E){return h?h.then(()=>A(E)):A(E)}c(x,"compiledForward");function N(E,T){let k=E.result.listKernels()[0],O=T.length>0?T[0].device:"cpu",D=T.map(V=>Bn(V)),z=E.capturedParams.map(V=>Bn(V)),j=E.jointFunc,Y=j.outputTypes.length,W=new Array(Y),G=new Array(Y);for(let V=0;V<Y;V++){let Q=j.outputTypes[V].shape,xt=j.outputTypes[V].dtype,Et=xs(Q),je=Se(xt);W[V]=new je(Math.max(Et,1)),G[V]=Q}let H=new Array(E.numForwardOutputs);for(let V=0;V<E.numForwardOutputs;V++){let Q=E.outputTypes[V],xt=xs(Q.shape),Et=Se(Q.dtype);H[V]=new Et(Math.max(xt,1))}u={inputArrays:D,paramArrays:z,gradOutputArrays:H,outputArrays:W,outputShapes:G,device:O,compiled:E};let kt=[...D,...z,...H,...W],wt=c(()=>{let V=[];for(let Q=0;Q<E.numForwardOutputs;Q++)V.push(he(W[Q],G[Q],j.outputTypes[Q].dtype,O));return V.length===1?V[0]:V},"build"),bt=y(E.result,k,kt);return bt?bt.then(wt):wt()}c(N,"_executeJointForward"),x.backward=function(...E){if(!f||!u)throw new Error("Must run forward before backward");return f.mode==="joint"?I(f,E,u):w(f,E,u)};function I(E,T,B){let{inputArrays:k,paramArrays:O,outputArrays:D,outputShapes:q,device:z}=B,j=T.map(V=>Bn(V));for(let V=0;V<j.length;V++)B.gradOutputArrays[V].set(j[V]);let Y=E.jointFunc,W=Y.outputTypes.length,G=new Array(W),H=new Array(W);for(let V=0;V<W;V++){let Q=Y.outputTypes[V].shape,xt=Y.outputTypes[V].dtype,Et=xs(Q),je=Se(xt);G[V]=new je(Math.max(Et,1)),H[V]=Q}let kt=[...k,...O,...j,...G],wt=c(()=>{let V=[];for(let Q=E.numForwardOutputs;Q<W;Q++)V.push(he(G[Q],H[Q],Y.outputTypes[Q].dtype,z));return V},"build"),bt=y(E.result,E.result.listKernels()[0],kt);return bt?bt.then(wt):wt()}if(c(I,"_executeJointBackward"),x.original=e,x.backwardGraph=()=>{if(l.length===0)return null;let E=l[0];return E.mode==="joint"?E.jointFunc:E.backwardFunc},x.forwardGraph=()=>{if(l.length===0)return null;let E=l[0];return E.mode==="joint"?E.jointFunc:E.forwardFunc},x.capturedParams=()=>l.length?l[0].capturedParams:[],t){let E=p(t);E&&typeof E.then=="function"?h=E.then(T=>(l.push(T),h=null,T)):l.push(E)}return x}var TE=P(()=>{sw();J_();_i();sl();qp();di();AE();aw();ZM();Mr();hr();Yr();c(EE,"compileWithBackward")});import LF from"node:fs";var Nf={};ne(Nf,{Accuracy:()=>Oa,Adam:()=>ga,AdamW:()=>ya,AdaptiveAvgPool2d:()=>Ru,AvgPool2d:()=>Nu,BCELoss:()=>Iu,BatchNorm1d:()=>$l,BatchNorm2d:()=>Eu,BatchSampler:()=>ha,CPUTarget:()=>ur,CSVLogger:()=>Ca,CUDATarget:()=>al,Callback:()=>Ys,ConfusionMatrix:()=>Pa,ConsoleLogger:()=>zi,Conv1d:()=>sa,Conv2d:()=>ea,CosineAnnealingLR:()=>ba,CrossEntropyLoss:()=>ku,DataLoader:()=>da,Dataset:()=>la,Dropout:()=>_n,ELU:()=>Au,EarlyStopping:()=>Na,Embedding:()=>Ou,F:()=>rA,F1Score:()=>La,Flatten:()=>Du,GELU:()=>_u,GRU:()=>Mu,GRUCell:()=>Vl,GradientAccumulationScheduler:()=>ka,LRScheduler:()=>_a,LSTM:()=>Lu,LSTMCell:()=>Gl,LayerNorm:()=>Pr,LeakyReLU:()=>xu,LearningRateMonitor:()=>Ra,LightningModule:()=>Ea,Linear:()=>ps,LogSoftmax:()=>ra,Logger:()=>$i,MSELoss:()=>vu,MapDataset:()=>$u,MaxPool2d:()=>Tu,MeanMetric:()=>Ef,Metric:()=>an,MetricCollection:()=>Ia,ModelCheckpoint:()=>ai,Module:()=>ct,ModuleDict:()=>Pu,ModuleList:()=>wo,MultiheadAttention:()=>bo,NLLLoss:()=>Cu,Optimizer:()=>Kn,Parameter:()=>_e,PositionalEncoding:()=>Uu,Precision:()=>Ba,ProgressCallback:()=>oi,RandomSampler:()=>fa,ReLU:()=>yu,Recall:()=>Ma,ReduceLROnPlateau:()=>Sa,SGD:()=>ma,Sampler:()=>So,Sequential:()=>ia,SequentialSampler:()=>ua,SiLU:()=>wu,Sigmoid:()=>bu,Softmax:()=>na,StepLR:()=>wa,SumMetric:()=>Tf,Tanh:()=>Su,Tensor:()=>Ot,TensorDataset:()=>ca,Timer:()=>va,Tokenizer:()=>Ao,Trainer:()=>Ta,Transformer:()=>Fu,TransformerDecoder:()=>jl,TransformerDecoderLayer:()=>aa,TransformerEncoder:()=>ql,TransformerEncoderLayer:()=>oa,Vocab:()=>xo,WasmTarget:()=>ll,WebGPUTarget:()=>cl,abs:()=>Ko,add:()=>ht,applyCheckpoint:()=>Af,arange:()=>dS,argmax:()=>Kh,argmin:()=>Hh,argsort:()=>Ty,cat:()=>Zc,ceil:()=>Vh,clamp:()=>Gh,clipGradNorm_:()=>qu,clipGradValue_:()=>ju,clone:()=>Xh,compile:()=>ew,compileWithBackward:()=>EE,cos:()=>$h,cumsum:()=>Ay,data:()=>oA,defaultCollate:()=>pa,div:()=>fe,dot:()=>Jh,empty:()=>us,emptyLike:()=>hO,enableGrad:()=>AS,eq:()=>ki,exp:()=>mr,eye:()=>mS,flip:()=>xy,floor:()=>zh,fromBuffer:()=>jo,full:()=>Qt,fullLike:()=>mO,gather:()=>jh,ge:()=>ho,gelu:()=>Nl,gt:()=>Zr,index_select:()=>po,init:()=>bp,le:()=>fo,lightning:()=>IE,linspace:()=>yS,loadCheckpoint:()=>xf,log:()=>Qr,log_softmax:()=>Il,lt:()=>Cl,matmul:()=>Ye,max:()=>ti,maximum:()=>Xr,mean:()=>gn,min:()=>Wh,minimum:()=>Tl,mul:()=>it,ne:()=>kl,neg:()=>In,nn:()=>iA,noGrad:()=>xS,one_hot:()=>qh,ones:()=>Je,onesLike:()=>dO,optim:()=>dE,pad:()=>Ho,pow:()=>Or,prod:()=>Yh,randn:()=>tp,randnLike:()=>gO,randperm:()=>gS,relu:()=>uo,roll:()=>Sy,rsqrt:()=>Fh,scalar:()=>fg,scan:()=>Bu,scatter:()=>by,scatter_add:()=>Yo,sigmoid:()=>Ks,sign:()=>vl,silu:()=>Rl,sin:()=>Uh,softmax:()=>Br,sort:()=>Ey,sqrt:()=>gr,stack:()=>yr,sub:()=>jt,sum:()=>ye,tanh:()=>jn,tensor:()=>Os,tokenizer:()=>lA,topk:()=>Ny,trace:()=>gE,where:()=>rn,zeros:()=>Gt,zerosLike:()=>pO});Ab();rr();Mr();en();ao();xl();Cn();Yr();hr();function pu(e,t){let s=Cs(e),n=sn.allocate(0,t,Jc),r=new Fe(n,0,e,s,t,Jc);return new Ot(r)}c(pu,"_metaTensor");function ta(e,t,s){let n=Xc(t.shape,s.shape);if(!n)throw new Error(`Incompatible shapes: [${t.shape}] vs [${s.shape}]`);let r=og(t.dtype,s.dtype);return pu(n,r)}c(ta,"_metaBinary");function yn(e,t){return pu([...t.shape],t.dtype)}c(yn,"_metaUnary");function yp(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 pu(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 pu(o,t.dtype)}c(yp,"_metaReduction");var EI=ta,TI=ta,NI=ta,RI=ta,vI=ta,kI=ta,CI=ta,II=ta,OI=yn,BI=yn,MI=yn,LI=yn,PI=yn,DI=yn,FI=yn,UI=yn,$I=yn,zI=yn,VI=yn,GI=yn,qI=yn,jI=yn,WI=yn,KI=yn,HI=yp,YI=yp,JI=yp,XI=yp,QI=yp;function ZI(e,t,s){let n=cg(t.shape,s.shape);if(n===null)throw new Error("metaMatmul: unsupported shapes");return pu(n,t.dtype)}c(ZI,"metaMatmul");function tO(e,t){return pu([...t.shape],t.dtype)}c(tO,"metaClone");var aV=["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"],lV=ft.META,cV={add:EI,sub:TI,mul:NI,div:RI,pow:vI,rem:kI,maximum:CI,minimum:II,neg:OI,exp:BI,log:MI,sqrt:LI,rsqrt:PI,abs:DI,sin:FI,cos:UI,tanh:$I,sigmoid:zI,relu:VI,gelu:GI,silu:qI,sign:jI,floor:WI,ceil:KI,sum:HI,mean:YI,max:JI,min:XI,prod:QI,matmul:ZI,clone:tO},eO=!1;function sO(){if(eO)return;eO=!0;let e=new Xf("mlc","DEF");for(let s of aV)e.def(s);let t=new Xf("mlc","IMPL");for(let[s,n]of Object.entries(cV))t.impl(s,lV,n);Bx()}c(sO,"registerNativeOps");rr();Jf();Xa();Ci();vi();ie();Oe();var Kg=class extends Bt{static{c(this,"AddBackward")}constructor(){super(2)}apply(t){let s=t[0];return[s,s]}},Hg=class extends Bt{static{c(this,"SubBackward")}constructor(){super(2)}apply(t){let s=t[0];return[s,In(s)]}},Yg=class extends Bt{static{c(this,"MulBackward")}constructor(){super(2)}apply(t){let s=t[0],[n,r]=this.savedTensors();return[it(s,r.detach()),it(s,n.detach())]}},Jg=class extends Bt{static{c(this,"DivBackward")}constructor(){super(2)}apply(t){let s=t[0],[n,r]=this.savedTensors(),i=fe(s,r.detach()),o=In(fe(it(s,n.detach()),it(r.detach(),r.detach())));return[i,o]}},Xg=class extends Bt{static{c(this,"NegBackward")}constructor(){super(1)}apply(t){let s=t[0];return[In(s)]}},Qg=class extends Bt{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=it(s,it(r.detach(),Or(n.detach(),jt(r.detach(),i)))),a=it(s,it(Or(n.detach(),r.detach()),Qr(n.detach())));return[o,a]}};vi();ie();Oe();var Zg=class extends Bt{static{c(this,"ExpBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors();return[it(t[0],mr(s.detach()))]}},ty=class extends Bt{static{c(this,"LogBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors();return[fe(t[0],s.detach())]}},ey=class extends Bt{static{c(this,"SqrtBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors(),n=s.detach(),r=gr(n),i=Qt(n.shape,2,{dtype:n.dtype,device:n.device});return[fe(t[0],it(i,r))]}},sy=class extends Bt{static{c(this,"TanhBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors(),n=s.detach(),r=jn(n),i=Je(n.shape,{dtype:n.dtype,device:n.device});return[it(t[0],jt(i,it(r,r)))]}},ny=class extends Bt{static{c(this,"SigmoidBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors(),n=s.detach(),r=Ks(n),i=Je(n.shape,{dtype:n.dtype,device:n.device});return[it(t[0],it(r,jt(i,r)))]}},ry=class extends Bt{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=Br(s.detach(),r),a=ye(it(i,o),r,!0);return[it(o,jt(i,a))]}},iy=class extends Bt{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=Br(s.detach(),r),a=ye(i,r,!0);return[jt(i,it(o,a))]}},oy=class extends Bt{static{c(this,"ReluBackward")}constructor(){super(1)}apply(t){let[s]=this.savedTensors(),n=s.detach();return[it(t[0],vl(uo(n)))]}},ay=class extends Bt{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=Qt(i,1.702,{dtype:o,device:a}),u=Je(i,{dtype:o,device:a}),f=it(l,r),h=Ks(f),p=it(h,ht(u,it(f,jt(u,h))));return[it(s,p)]}},ly=class extends Bt{static{c(this,"SiluBackward")}constructor(){super(1)}apply(t){let s=t[0],[n]=this.savedTensors(),r=n.detach(),i=Ks(r),o=Je(r.shape,{dtype:r.dtype,device:r.device}),a=it(i,ht(o,it(r,jt(o,i))));return[it(s,a)]}};vi();ie();Oe();Is();function nO(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(nO,"_normalizeDims");function rO(e,t,s,n){let r=e;if(!n)for(let o of s)r=dr(r,o);let i=Gt(t,{dtype:r.dtype,device:r.device});return ht(i,r)}c(rO,"_unreduce");var cy=class extends Bt{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=nO(o,r.length);return[rO(s,r,l,a)]}},uy=class extends Bt{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=nO(o,r.length),u=1;for(let p of l)u*=r[p];let f=rO(s,r,l,a),h=Qt(r,u,{dtype:s.dtype,device:s.device});return[fe(f,h)]}};vi();ie();Is();function iO(e,t){let s=e;for(;s.ndim>t.length;)s=ye(s,0,!1);for(let n=0;n<t.length;n++)t[n]===1&&s.shape[n]!==1&&(s=ye(s,n,!0));return s}c(iO,"_sumToShape");var fy=class extends Bt{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[it(s,o),it(s,i)];if(a===2&&l===1){let h=dr(s,1),p=dr(o,0);return[Ye(h,p),Ph(Ye(pr(i,0,1),h),1)]}if(a===1&&l===2){let h=Ye(s,pr(o,0,1)),p=Ye(dr(i,1),dr(s,0));return[h,p]}let u=Ye(s,pr(o,l-2,l-1)),f=Ye(pr(i,a-2,a-1),s);return u=iO(u,i.shape),f=iO(f,o.shape),[u,f]}},hy=class extends Bt{static{c(this,"DotBackward")}constructor(){super(2)}apply(t){let s=t[0],[n,r]=this.savedTensors();return[it(s,r.detach()),it(s,n.detach())]}};wS();var Mx=new Map;function Ae(e,t){Mx.set(e,t)}c(Ae,"_register");Ae("add",()=>new Kg);Ae("sub",()=>new Hg);Ae("mul",()=>new Yg);Ae("div",()=>new Jg);Ae("neg",()=>new Xg);Ae("pow",()=>new Qg);Ae("exp",()=>new Zg);Ae("log",()=>new ty);Ae("sqrt",()=>new ey);Ae("tanh",()=>new sy);Ae("sigmoid",()=>new ny);Ae("relu",()=>new oy);Ae("softmax",()=>new ry);Ae("log_softmax",()=>new iy);Ae("gelu",()=>new ay);Ae("silu",()=>new ly);Ae("sum",()=>new cy);Ae("mean",()=>new uy);Ae("matmul",()=>new fy);Ae("dot",()=>new hy);Ae("cat",()=>new dg);Ae("stack",()=>new mg);Ae("clamp",()=>new gg);Ae("pad",()=>new yg);Ae("index_select",()=>new tu);Ae("where",()=>new _g);function oO(e){let t=Mx.get(e);return t?t():null}c(oO,"getGradFn");function aO(e){return Mx.has(e)}c(aO,"hasGradFn");Ag();Qc();ao();en();Cn();oo();en();Oe();ie();function lO(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;fV(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?uV(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]?ht(_[g.inputNr],p[m]):p[m];else{let w=[];w[g.inputNr]=p[m],o.set(y,w)}}}}c(lO,"backward");function uV(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=ye(n,0,!1);for(let i=t.length-1;i>=0;i--)t[i]===1&&n.shape[i]!==1&&(n=ye(n,i,!0));return n}c(uV,"_reduceBroadcastGrad");function fV(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(fV,"_countDeps");Is();vi();ie();Oe();Is();var py=class extends Bt{static{c(this,"ReshapeBackward")}constructor(){super(1)}apply(t){let s=this.inputMetadata(0);return[Jr(t[0],s.shape)]}},dy=class extends Bt{static{c(this,"TransposeBackward")}constructor(t,s){super(1),this._dim0=t,this._dim1=s}apply(t){return[pr(t[0],this._dim0,this._dim1)]}},my=class extends Bt{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,[Ho(s,_,w,0)]}let a=Gt(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 S=0;S<m;S++){let b=S===r?this._start+g[S]*o:g[S];w+=b*d[S]}l[w]+=u[y];for(let S=m-1;S>=0;S--){if(g[S]++,g[S]<h[S]){y+=p[S];break}y-=(h[S]-1)*p[S],g[S]=0}}return[a]}},gy=class extends Bt{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=dr(s,r),a=n.shape.map(()=>0),l=n.shape.map(()=>0);return a[r]=this._index,l[r]=i-1-this._index,[Ho(o,a,l,0)]}},yy=class extends Bt{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=ye(s,o,!0)),[Jr(l,r)]}},_y=class extends Bt{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[oS(t[0],s)]}};jk({backward:lO});tC({GradMode:Lr,ReshapeBackward:py,TransposeBackward:dy,PermuteBackward:_y,SliceBackward:my,ExpandBackward:yy,SelectBackward:gy,GradAccumulator:mo});function hV(e){let t=e._impl;if(ks()&&t.device&&t.device.type===De.GPU){t.storage.retain();let r=new Fe(t.storage,t.storageOffset,t.sizes(),t.strides(),t.dtype,t.device);return new Ot(r)}let s=t.storage.clone(),n=new Fe(s,t.storageOffset,t.sizes(),t.strides(),t.dtype,t.device);return new Ot(n)}c(hV,"_snapshotTensor");function pV(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(pV,"_anyRequiresGrad");function dV(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(dV,"_extractTensors");function mV(e){let t=e._impl.autogradMeta;if(!t)return null;let s=t.getGradAccumulator();return s||(s=new mo(e),t.setGradAccumulator(s)),s}c(mV,"_getOrCreateAccumulator");function gV(e,t){let s=We.dispatch.bind(We);return(n,...r)=>{if(!Lr.isEnabled()||!pV(r)){let u=n.subtract(Zf);return We.redispatch(t,u,...r)}let i=oO(e);if(!i){let u=n.subtract(Zf);return We.redispatch(t,u,...r)}let o=dV(r);i.setOpArgs(r);for(let u=0;u<o.length;u++)i.saveTensor(hV(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=mV(f);p&&i.setNextEdge(u,p,0)}}}let a=n.subtract(Zf),l=We.redispatch(t,a,...r);if(l&&l._impl){l._impl.autogradMeta||l._impl.setAutogradMeta(new Cr);let u=l._impl.autogradMeta;u.setGradFn(i,0),u.requiresGrad=!0,l._impl._updateKeySet()}return l}}c(gV,"wrapWithAutograd");function yV(e){return(t,...s)=>{let n=t.subtract(Zf);return We.redispatch(e,n,...s)}}c(yV,"_makePassthrough");function cO(){let e=We.listOps(),t=[ft.AUTOGRAD,ft.AUTOGRAD_CPU,ft.AUTOGRAD_GPU,ft.AUTOGRAD_WASM];for(let s of e){let n=We.findOp(s);if(!n)continue;let r=n.name,i=aO(r)?gV(r,n):yV(n),o=qn.fromUnboxed(i);for(let a of t)n.entry.registerKernel(a,o)}}c(cO,"registerAutogradKernels");ie();ie();Oe();Wo();function by(e,t,s,n){let r=Gt(e.shape,{dtype:e.dtype}),i=Yo(r,t,s,n),o=Yo(r,t,s,Je(n.shape,{dtype:e.dtype}));return rn(Zr(o,Gt(o.shape,{dtype:o.dtype})),i,e)}c(by,"scatter");function du(e,t){return e<0?t+e:e}c(du,"_normDim");function Sy(e,t,s=0){let n=e.shape.length,r=du(s,n),i=e.shape[r],o=(t%i+i)%i;return o===0?e:Zc([e.narrow(r,i-o,o),e.narrow(r,0,i-o)],r)}c(Sy,"roll");function xy(e,t){let s=Array.isArray(t)?t:[t],n=e.shape.length,r=e;for(let i of s){let o=du(i,n),a=r.shape[o],l=new Array(a);for(let u=0;u<a;u++)l[u]=a-1-u;r=po(r,o,Os(l,{dtype:"i32"}))}return r}c(xy,"flip");function Ay(e,t=0){let s=e.shape.length,n=du(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=Zc([Gt(a,{dtype:i.dtype}),i.narrow(n,0,r-o)],n);i=ht(i,l)}return i}c(Ay,"cumsum");function _V(e){let t=1;for(;t<e;)t<<=1;return t}c(_V,"_nextPow2");function wy(e,t,s){let n=e.shape.length,r=n-1,i=e.shape[r],o=_V(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=Ho(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=Os(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 b=0;b<o;b++){let A=b^p;d[b]=A;let x=(b&h)===0,N=t?!x:x;A>b?m[b]=N?1:0:m[b]=N?0:1}let g=Os(d,{dtype:"i32"}),y=po(l,r,g),_=Tl(l,y),w=Xr(l,y),S=Os(m,{dtype:"f32"}).reshape(u);if(s){let b=po(f,r,g),A=rn(S,ki(_,l),ki(w,l));f=rn(A,f,b)}l=rn(S,_,w)}return o>i&&(l=l.narrow(r,0,i),s&&(f=f.narrow(r,0,i))),s?{values:l,indices:f}:l}c(wy,"_bitonicLastDim");function Ey(e,t=-1,s=!1){let n=e.shape.length,r=du(t,n);if(r===n-1)return wy(e,s,!1);let i=e.transpose(r,n-1);return wy(i,s,!1).transpose(r,n-1)}c(Ey,"sort");function uO(e,t,s){let n=e.shape.length,r=du(t,n);if(r===n-1)return wy(e,s,!0);let i=e.transpose(r,n-1),o=wy(i,s,!0);return{values:o.values.transpose(r,n-1),indices:o.indices.transpose(r,n-1)}}c(uO,"_sortWithIndices");function Ty(e,t=-1,s=!1){return uO(e,t,s).indices}c(Ty,"argsort");function Ny(e,t,s=-1,n=!0){let r=e.shape.length,i=du(s,r),{values:o,indices:a}=uO(e,i,n);return[o.narrow(i,0,t),a.narrow(i,0,t)]}c(Ny,"topk");Is();Wo();function fO(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 jo(r,this.shape,this.dtype,{device:s})},t.add=function(s){return ht(this,s)},t.sub=function(s){return jt(this,s)},t.mul=function(s){return it(this,s)},t.div=function(s){return fe(this,s)},t.neg=function(){return In(this)},t.pow=function(s){return Or(this,s)},t.remainder=function(s){return lS(this,s)},t.maximum=function(s){return Xr(this,s)},t.minimum=function(s){return Tl(this,s)},t.exp=function(){return mr(this)},t.log=function(){return Qr(this)},t.sqrt=function(){return gr(this)},t.rsqrt=function(){return Fh(this)},t.abs=function(){return Ko(this)},t.sin=function(){return Uh(this)},t.cos=function(){return $h(this)},t.tanh=function(){return jn(this)},t.sigmoid=function(){return Ks(this)},t.relu=function(){return uo(this)},t.gelu=function(){return Nl(this)},t.silu=function(){return Rl(this)},t.sign=function(){return vl(this)},t.floor=function(){return zh(this)},t.ceil=function(){return Vh(this)},t.eq=function(s){return ki(this,s)},t.ne=function(s){return kl(this,s)},t.lt=function(s){return Cl(this,s)},t.le=function(s){return fo(this,s)},t.gt=function(s){return Zr(this,s)},t.ge=function(s){return ho(this,s)},t.sum=function(s,n){return ye(this,s,n)},t.mean=function(s,n){return gn(this,s,n)},t.max=function(s,n){return ti(this,s,n)},t.min=function(s,n){return Wh(this,s,n)},t.argmax=function(s,n){return Kh(this,s,n)},t.argmin=function(s,n){return Hh(this,s,n)},t.prod=function(s,n){return Yh(this,s,n)},t.matmul=function(s){return Ye(this,s)},t.dot=function(s){return Jh(this,s)},t.mm=function(s){return Ye(this,s)},t.softmax=function(s=-1){return Br(this,s)},t.log_softmax=function(s=-1){return Il(this,s)},t.requires_grad=function(s=!0){return this.requiresGrad_(s)},t.clone=function(){return Xh(this)},t.roll=function(s,n=0){return Sy(this,s,n)},t.flip=function(s){return xy(this,s)},t.cumsum=function(s=0){return Ay(this,s)},t.sort=function(s=-1,n=!1){return Ey(this,s,n)},t.topk=function(s,n=-1,r=!0){return Ny(this,s,n,r)},t.argsort=function(s=-1,n=!1){return Ty(this,s,n)},t.gather=function(s,n){return jh(this,s,n)},t.scatter_add=function(s,n,r){return Yo(this,s,n,r)},t.scatter=function(s,n,r){return by(this,s,n,r)},iC(e)}c(fO,"installOps");en();en();Oe();Oe();function _p(e,t){return{dtype:t?.dtype??e.dtype,device:t?.device??e.device,requiresGrad:t?.requiresGrad??!1}}c(_p,"_likeOpts");function hO(e,t){return us(e.shape,_p(e,t))}c(hO,"emptyLike");function pO(e,t){return Gt(e.shape,_p(e,t))}c(pO,"zerosLike");function dO(e,t){return Je(e.shape,_p(e,t))}c(dO,"onesLike");function mO(e,t,s){return Qt(e.shape,t,_p(e,s))}c(mO,"fullLike");function gO(e,t){return tp(e.shape,_p(e,t))}c(gO,"randnLike");Wo();ie();Ci();var iA={};ne(iA,{AdaptiveAvgPool2d:()=>Ru,AvgPool2d:()=>Nu,BCELoss:()=>Iu,BatchNorm1d:()=>$l,BatchNorm2d:()=>Eu,Conv1d:()=>sa,Conv2d:()=>ea,CrossEntropyLoss:()=>ku,Dropout:()=>_n,ELU:()=>Au,Embedding:()=>Ou,F:()=>rA,Flatten:()=>Du,GELU:()=>_u,GRU:()=>Mu,GRUCell:()=>Vl,GroupNorm:()=>Ly,LSTM:()=>Lu,LSTMCell:()=>Gl,LayerNorm:()=>Pr,LeakyReLU:()=>xu,Linear:()=>ps,LogSoftmax:()=>ra,MSELoss:()=>vu,MaxPool2d:()=>Tu,Module:()=>ct,ModuleDict:()=>Pu,ModuleList:()=>wo,MultiheadAttention:()=>bo,NLLLoss:()=>Cu,Parameter:()=>_e,PositionalEncoding:()=>Uu,ReLU:()=>yu,Sequential:()=>ia,SiLU:()=>wu,Sigmoid:()=>bu,Softmax:()=>na,Tanh:()=>Su,Transformer:()=>Fu,TransformerDecoder:()=>jl,TransformerDecoderLayer:()=>aa,TransformerEncoder:()=>ql,TransformerEncoderLayer:()=>oa,functional:()=>gu,init:()=>bp});en();var _e=class extends Ot{static{c(this,"Parameter")}constructor(t,s=!0){t instanceof Ot?super(t._impl):super(t),s&&this.requiresGrad_(!0)}get isParameter(){return!0}};var ct=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 _e))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 _e(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 _e&&!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 bp={};ne(bp,{_calculateFanInFanOut:()=>_o,constant_:()=>xV,kaiming_normal_:()=>TV,kaiming_uniform_:()=>mu,normal_:()=>wp,ones_:()=>SV,uniform_:()=>Fl,xavier_normal_:()=>EV,xavier_uniform_:()=>AV,zeros_:()=>bV});function _o(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(_o,"_calculateFanInFanOut");function Fl(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(Fl,"uniform_");function wp(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(wp,"normal_");function bV(e){let t=e._impl.storage.data;return t&&t.fill(0),e._impl.bumpVersion(),e}c(bV,"zeros_");function SV(e){let t=e._impl.storage.data;return t&&t.fill(1),e._impl.bumpVersion(),e}c(SV,"ones_");function xV(e,t){let s=e._impl.storage.data;return s&&s.fill(t),e._impl.bumpVersion(),e}c(xV,"constant_");function AV(e,t=1){let{fanIn:s,fanOut:n}=_o(e),r=t*Math.sqrt(2/(s+n)),i=Math.sqrt(3)*r;return Fl(e,-i,i)}c(AV,"xavier_uniform_");function EV(e,t=1){let{fanIn:s,fanOut:n}=_o(e),r=t*Math.sqrt(2/(s+n));return wp(e,0,r)}c(EV,"xavier_normal_");function mu(e,t=0,s="fan_in",n="leaky_relu"){let{fanIn:r,fanOut:i}=_o(e),o=s==="fan_in"?r:i,l=yO(n,t)/Math.sqrt(o),u=Math.sqrt(3)*l;return Fl(e,-u,u)}c(mu,"kaiming_uniform_");function TV(e,t=0,s="fan_in",n="leaky_relu"){let{fanIn:r,fanOut:i}=_o(e),o=s==="fan_in"?r:i,l=yO(n,t)/Math.sqrt(o);return wp(e,0,l)}c(TV,"kaiming_normal_");function yO(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(yO,"_calculateGain");var gu={};ne(gu,{elu:()=>zx,gelu:()=>xp,leaky_relu:()=>$x,log_softmax:()=>Ux,relu:()=>Sp,sigmoid:()=>Px,silu:()=>Lx,softmax:()=>Fx,tanh:()=>Dx});ie();Oe();function Sp(e){return uo(e)}c(Sp,"relu");function xp(e){return Nl(e)}c(xp,"gelu");function Lx(e){return Rl(e)}c(Lx,"silu");function Px(e){return Ks(e)}c(Px,"sigmoid");function Dx(e){return jn(e)}c(Dx,"tanh");function Fx(e,t=-1){return Br(e,t)}c(Fx,"softmax");function Ux(e,t=-1){return Il(e,t)}c(Ux,"log_softmax");function $x(e,t=.01){let s=it(e,Qt(e.shape,t,{dtype:e.dtype,device:e.device}));return Xr(e,s)}c($x,"leaky_relu");function zx(e,t=1){let s=Qt(e.shape,0,{dtype:e.dtype,device:e.device}),n=Qt(e.shape,1,{dtype:e.dtype,device:e.device}),r=Qt(e.shape,t,{dtype:e.dtype,device:e.device}),i=Zr(e,s),o=it(r,jt(mr(e),n));return rn(i,e,o)}c(zx,"elu");var vy={};ne(vy,{batch_norm:()=>qx,group_norm:()=>Gx,layer_norm:()=>Vx});ie();Oe();Ul();function Vx(e,t,s,n,r=1e-5){if((e instanceof we||e.isSymbolic)&&s&&n){let m=e.ndim-t.length;return cS(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=gn(a,o[m],!0);let l=jt(e,a),f=it(l,l);for(let m=o.length-1;m>=0;m--)f=gn(f,o[m],!0);let h=Qt([],r),p=fe(Qt([],1),gr(ht(f,h))),d=it(l,p);return s&&(d=it(d,s)),n&&(d=ht(d,n)),d}c(Vx,"layer_norm");function Gx(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 S=2;S<u.ndim;S++)f.push(S);let h=u;for(let S=f.length-1;S>=0;S--)h=gn(h,f[S],!0);let p=jt(u,h),m=it(p,p);for(let S=f.length-1;S>=0;S--)m=gn(m,f[S],!0);let g=Qt([],r),y=fe(Qt([],1),gr(ht(m,g))),_=it(p,y).reshape(i),w=[1,a,...l.map(()=>1)];return s&&(_=it(_,s.reshape(w))),n&&(_=ht(_,n.reshape(w))),_}c(Gx,"group_norm");var Ry=1;function RV(e,t){let s=new Array(e).fill(1);return s[Ry]=t,s}c(RV,"channelShape");function _O(e,t){let s=e;for(let n=t.length-1;n>=0;n--)s=gn(s,t[n],!0);return s}c(_O,"reduceMeanOver");function wO(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(wO,"blendRunning");function qx(e,t,s,n,r,i=!0,o=1e-5,a=.1){let l=e instanceof we||e.isSymbolic;if(!i)return uS(e,n,r,t,s,Ry,o);let u=[];for(let y=0;y<e.ndim;y++)y!==Ry&&u.push(y);let f=_O(e,u),h=jt(e,f),p=_O(it(h,h),u),d=fe(Qt([],1),gr(ht(p,Qt([],o)))),m=it(h,d),g=RV(e.ndim,e.shape[Ry]);return n&&(m=it(m,n.reshape(g))),r&&(m=ht(m,r.reshape(g))),l||(t&&wO(t,f,a),s&&wO(s,p,a)),m}c(qx,"batch_norm");var Wx={};ne(Wx,{linear:()=>jx});ie();Is();Ul();function jx(e,t,s){let n;e instanceof we||e.isSymbolic?n=Qh(t,0,1):n=pr(t,0,1);let r=Ye(e,n);return s?ht(r,s):r}c(jx,"linear");var Cy={};ne(Cy,{conv1d:()=>Kx,conv2d:()=>ky});ie();Is();function ky(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=vV(r),u=Array.isArray(i)?i:[i,i],f=fS(e,t,a,l,u,o);if(s){let h=Jr(s,[1,s.shape[0],1,1]);return ht(f,h)}return f}c(ky,"conv2d");function Kx(e,t,s,n=1,r=0,i=1,o=1){let a=dr(e,2),l=dr(t,2),u=[1,Array.isArray(n)?n[0]:n],f=[[0,0],kV(r)],h=[1,Array.isArray(i)?i[0]:i],p=ky(a,l,null,u,f,h,o),d=Ph(p,2);if(s){let m=Jr(s,[1,s.shape[0],1]);return ht(d,m)}return d}c(Kx,"conv1d");function vV(e){return typeof e=="number"?[[e,e],[e,e]]:Array.isArray(e)&&typeof e[0]=="number"?e.map(t=>[t,t]):e}c(vV,"_normalizePadding");function kV(e){return typeof e=="number"?[e,e]:e}c(kV,"_normalizePadding1d");var Oy={};ne(Oy,{adaptive_avg_pool2d:()=>Yx,avg_pool2d:()=>Iy,max_pool2d:()=>Hx});ie();function Hx(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=bO(n);return hg(e,"max",r,i,o)}c(Hx,"max_pool2d");function Iy(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=bO(n);return hg(e,"avg",r,i,o)}c(Iy,"avg_pool2d");function Yx(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 Iy(e,[o,a],[o,a],[[0,0],[0,0]])}c(Yx,"adaptive_avg_pool2d");function bO(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(bO,"_normalizePadding");var Jx={};ne(Jx,{dropout:()=>Ap});ie();Oe();function Ap(e,t=.5,s=!0){if(!s||t===0)return e;if(t===1)return Qt(e.shape,0,{dtype:e.dtype,device:e.device});let n=CV(e.shape,1-t,e.dtype,e.device),r=Qt(e.shape,1/(1-t),{dtype:e.dtype,device:e.device});return it(it(e,n),r)}c(Ap,"dropout");function CV(e,t,s,n){let r=us(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(CV,"_bernoulliMask");var My={};ne(My,{binary_cross_entropy:()=>Zx,cross_entropy:()=>Qx,mse_loss:()=>Xx,nll_loss:()=>By});ie();Oe();Is();Yr();function Xx(e,t,s="mean"){let n=jt(e,t),r=it(n,n);return SO(r,s)}c(Xx,"mse_loss");function IV(e,t){let s=OV(e,t),n=jt(e,s),r=mr(n),i=BV(r,t),o=Qr(i);return jt(n,o)}c(IV,"_logSoftmaxAutograd");function OV(e,t){let s=t<0?e.ndim+t:t;return ti(e,s,!0)}c(OV,"_dimMax");function BV(e,t){let s=t<0?e.ndim+t:t;return ye(e,s,!0)}c(BV,"_dimSum");function By(e,t,s="mean",n=null){let r=e.ndim-1,i=e.shape[r],o=qh(t,i),a=ye(it(e,o),r),l=a,u=null;if(n!==null){let h=Ws(o,r,n),p=ht(In(h),1);l=it(a,p),u=ye(p)}let f=In(ye(l));if(s==="sum")return f;if(s==="mean")return u!==null?fe(f,u):fe(f,xs(t.shape));throw new Error(`nll_loss: unknown reduction '${s}'`)}c(By,"nll_loss");function Qx(e,t,s="mean",n=null){let r=IV(e,-1);return By(r,t,s,n)}c(Qx,"cross_entropy");function Zx(e,t,s="mean"){let n=Qt(e.shape,1e-7,{dtype:e.dtype,device:e.device}),r=Qt(e.shape,1,{dtype:e.dtype,device:e.device}),i=Qr(ht(e,n)),o=Qr(ht(jt(r,e),n)),a=In(ht(it(t,i),it(jt(r,t),o)));return SO(a,s)}c(Zx,"binary_cross_entropy");function SO(e,t){if(t==="mean")return gn(e);if(t==="sum")return ye(e);if(t==="none")return e;throw new Error(`Unknown reduction: ${t}`)}c(SO,"_reduce");var eA={};ne(eA,{embedding:()=>tA});ie();function tA(e,t){let s=e.shape[e.shape.length-1],n=t.shape.reduce((i,o)=>i*o,1);return po(e,0,t.reshape([n])).reshape([...t.shape,s])}c(tA,"embedding");var nA={};ne(nA,{scaled_dot_product_attention:()=>sA});ie();Is();Ul();Oe();function MV(e,t){if(e instanceof we||e.isSymbolic)return Br(e,t);let s=t<0?e.ndim+t:t,n=ti(e,s,!0).detach(),r=jt(e,n),i=mr(r),o=ye(i,s,!0);return fe(i,o)}c(MV,"_softmax");function LV(e){let t=e.ndim;return e instanceof we||e.isSymbolic?Qh(e,t-2,t-1):pr(e,t-2,t-1)}c(LV,"_transposeLastTwo");function PV(e,t){let s=us([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(PV,"_generateCausalMask");function sA(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=Qt([],1/Math.sqrt(a)),h=LV(t),p=Ye(e,h);p=it(p,f),i&&(p=ht(p,PV(l,u))),n&&(p=ht(p,n));let d=MV(p,-1);return r>0&&o&&(d=Ap(d,r,!0)),Ye(d,s)}c(sA,"scaled_dot_product_attention");Oe();var ps=class extends ct{static{c(this,"Linear")}constructor(t,s,n=!0){super(),this.inFeatures=t,this.outFeatures=s,this.weight=new _e(us([s,t])),this.bias=n?new _e(Gt([s])):null,this._resetParameters()}_resetParameters(){if(mu(this.weight,Math.sqrt(5)),this.bias){let{fanIn:t}=_o(this.weight),s=1/Math.sqrt(t);Fl(this.bias,-s,s)}}forward(t){return jx(t,this.weight,this.bias)}};Oe();var ea=class extends ct{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 _e(us([s,t/this.groups,i,o])),this.bias=r.bias!==!1?new _e(Gt([s])):null,this._resetParameters()}_resetParameters(){if(mu(this.weight,Math.sqrt(5)),this.bias){let{fanIn:t}=_o(this.weight),s=1/Math.sqrt(t);Fl(this.bias,-s,s)}}forward(t){return ky(t,this.weight,this.bias,this.stride,this.padding,this.dilation,this.groups)}},sa=class extends ct{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 _e(us([s,t/this.groups,this.kernelSize])),this.bias=r.bias!==!1?new _e(Gt([s])):null,mu(this.weight,Math.sqrt(5))}forward(t){return Kx(t,this.weight,this.bias,this.stride,this.padding,this.dilation,this.groups)}};var yu=class extends ct{static{c(this,"ReLU")}forward(t){return Sp(t)}},_u=class extends ct{static{c(this,"GELU")}forward(t){return xp(t)}},wu=class extends ct{static{c(this,"SiLU")}forward(t){return Lx(t)}},bu=class extends ct{static{c(this,"Sigmoid")}forward(t){return Px(t)}},Su=class extends ct{static{c(this,"Tanh")}forward(t){return Dx(t)}},xu=class extends ct{static{c(this,"LeakyReLU")}constructor(t=.01){super(),this.negativeSlope=t}forward(t){return $x(t,this.negativeSlope)}},Au=class extends ct{static{c(this,"ELU")}constructor(t=1){super(),this.alpha=t}forward(t){return zx(t,this.alpha)}},na=class extends ct{static{c(this,"Softmax")}constructor(t=-1){super(),this.dim=t}forward(t){return Fx(t,this.dim)}},ra=class extends ct{static{c(this,"LogSoftmax")}constructor(t=-1){super(),this.dim=t}forward(t){return Ux(t,this.dim)}};Oe();var Pr=class extends ct{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 _e(Je(this.normalizedShape)):null,this.bias=n?new _e(Gt(this.normalizedShape)):null}forward(t){return Vx(t,this.normalizedShape,this.weight,this.bias,this.eps)}},Ly=class extends ct{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 _e(Je([s])):null,this.bias=r?new _e(Gt([s])):null}forward(t){return Gx(t,this.numGroups,this.weight,this.bias,this.eps)}},$l=class extends ct{static{c(this,"BatchNorm1d")}constructor(t,s=1e-5,n=!0){super(),this.numFeatures=t,this.eps=s,this.weight=n?new _e(Je([t])):null,this.bias=n?new _e(Gt([t])):null,this.runningMean=Gt([t]),this.runningVar=Je([t]),this.registerBuffer("runningMean",this.runningMean),this.registerBuffer("runningVar",this.runningVar)}forward(t){return qx(t,this.runningMean,this.runningVar,this.weight,this.bias,this.training,this.eps)}},Eu=class extends $l{static{c(this,"BatchNorm2d")}constructor(t,s=1e-5,n=!0){super(t,s,n)}};var Tu=class extends ct{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 Hx(t,this.kernelSize,this.stride,this.padding)}},Nu=class extends ct{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 Iy(t,this.kernelSize,this.stride,this.padding)}},Ru=class extends ct{static{c(this,"AdaptiveAvgPool2d")}constructor(t){super(),this.outputSize=Array.isArray(t)?t:[t,t]}forward(t){return Yx(t,this.outputSize)}};var _n=class extends ct{static{c(this,"Dropout")}constructor(t=.5){super(),this.p=t}forward(t){return Ap(t,this.p,this.training)}};var vu=class extends ct{static{c(this,"MSELoss")}constructor(t="mean"){super(),this.reduction=t}forward(t,s){return Xx(t,s,this.reduction)}},ku=class extends ct{static{c(this,"CrossEntropyLoss")}constructor(t="mean",s=-100){super(),this.reduction=t,this.ignoreIndex=s}forward(t,s){return Qx(t,s,this.reduction,this.ignoreIndex===-100?null:this.ignoreIndex)}},Cu=class extends ct{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)}},Iu=class extends ct{static{c(this,"BCELoss")}constructor(t="mean"){super(),this.reduction=t}forward(t,s){return Zx(t,s,this.reduction)}};Oe();var Ou=class extends ct{static{c(this,"Embedding")}constructor(t,s){super(),this.numEmbeddings=t,this.embeddingDim=s,this.weight=new _e(us([t,s])),wp(this.weight)}forward(t){return tA(this.weight,t)}};Oe();ie();Is();Tp();Is();ie();function UV(e){if(Array.isArray(e[0])){let t=new Array(e[0].length);for(let s=0;s<t.length;s++)t[s]=yr(e.map(n=>n[s]),0);return t}return yr(e,0)}c(UV,"stackSteps");function Bu(e,t,s){let n=Array.isArray(t),r=Array.isArray(s),i=r?s:[s],o=n?t:[t],a=zl();if(!a){let p=t,d=[],m=i[0].shape[0];for(let g=0;g<m;g++){let y=r?i.map(S=>Ws(S,0,g)):Ws(i[0],0,g),[_,w]=e(p,y);p=_,d.push(w)}return[p,UV(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(Bu,"scan");var Vl=class extends ct{static{c(this,"GRUCell")}constructor(t,s,n=!0){super(),this.inputSize=t,this.hiddenSize=s,this.x2h=new ps(t,3*s,n),this.h2h=new ps(s,3*s,n)}forward(t,s=null){let n=s!==null?s:Gt([t.shape[0],this.hiddenSize],{device:t.device}),r=this.x2h.forward(t),i=this.h2h.forward(n),[o,a,l]=El(r,this.hiddenSize,-1),[u,f,h]=El(i,this.hiddenSize,-1),p=Ks(ht(o,u)),d=Ks(ht(a,f)),m=jn(ht(l,it(p,h)));return ht(m,it(d,jt(n,m)))}},Mu=class extends ct{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 Vl(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?Ws(s,0,l):Gt([r,this.hiddenSize],{device:n.device}),f=this.cells[l],[h,p]=Bu((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,yr(o,0)]}};Oe();ie();Is();Tp();Mr();Cn();var Gl=class extends ct{static{c(this,"LSTMCell")}constructor(t,s,n=!0){super(),this.inputSize=t,this.hiddenSize=s,this.x2h=new ps(t,4*s,n),this.h2h=new ps(s,4*s,n)}forward(t,s=null){let n=s!==null?s[0]:Gt([t.shape[0],this.hiddenSize],{device:t.device}),r=s!==null?s[1]:Gt([t.shape[0],this.hiddenSize],{device:t.device}),i=ht(this.x2h.forward(t),this.h2h.forward(n)),[o,a,l,u]=El(i,this.hiddenSize,-1),f=Ks(o),h=Ks(a),p=jn(l),d=Ks(u),m=ht(it(h,r),it(f,p));return[it(d,jn(m)),m]}},Lu=class extends ct{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 Gl(o===0?t:s,s,i);this.cells.push(a),this.registerModule("cell_"+o,a)}}forward(t,s=null){let n=kx();if(n&&t.device.type===De.GPU&&!zl()){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=Cx();if(r&&t.device.type===De.WEBGPU&&!zl()){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?Ws(s[0],0,h):Gt([o,this.hiddenSize],{device:i.device}),d=s!==null?Ws(s[1],0,h):Gt([o,this.hiddenSize],{device:i.device}),m=this.cells[h],[[g,y],_]=Bu((w,S)=>{let[b,A]=m.forward(S,w);return[[b,A],b]},[p,d],a);l.push(g),u.push(y),a=_}let f=a;return this.batchFirst&&(f=f.transpose(0,1)),[f,[yr(l,0),yr(u,0)]]}};var ia=class extends ct{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}},wo=class extends ct{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()")}},Pu=class extends ct{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()")}};Is();var Du=class extends ct{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 Jr(t,a)}};ie();Oe();Oe();function AO(e){return e==="gelu"?xp:Sp}c(AO,"_getActivation");var bo=class extends ct{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 ps(t,t,r),this.kProj=new ps(i??t,t,r),this.vProj=new ps(o??t,t,r),this.outProj=new ps(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=Qt(i.shape,-1/0),y=Qt(i.shape,0),_=rn(i,g,y);_=_.unsqueeze(1).unsqueeze(2),r=r?ht(r,_):_}let d=sA(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}},oa=class extends ct{static{c(this,"TransformerEncoderLayer")}constructor(t,s,n=2048,r=.1,i="relu",o=1e-5,a=!0,l=!1){super(),this.selfAttn=new bo(t,s,r,!0,null,null,a),this.linear1=new ps(t,n),this.linear2=new ps(n,t),this.norm1=new Pr(t,o),this.norm2=new Pr(t,o),this.dropout1=new _n(r),this.dropout2=new _n(r),this.dropoutFFN=new _n(r),this._activation=AO(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(ht(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(ht(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=ht(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=ht(o,this.dropout2.forward(a)),o}},aa=class extends ct{static{c(this,"TransformerDecoderLayer")}constructor(t,s,n=2048,r=.1,i="relu",o=1e-5,a=!0,l=!1){super(),this.selfAttn=new bo(t,s,r,!0,null,null,a),this.crossAttn=new bo(t,s,r,!0,null,null,a),this.linear1=new ps(t,n),this.linear2=new ps(n,t),this.norm1=new Pr(t,o),this.norm2=new Pr(t,o),this.norm3=new Pr(t,o),this.dropout1=new _n(r),this.dropout2=new _n(r),this.dropout3=new _n(r),this.dropoutFFN=new _n(r),this._activation=AO(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(ht(t,this.dropout1.forward(l)));let u=this.crossAttn.forward(l,s,s,r,o);l=this.norm2.forward(ht(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(ht(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=ht(t,this.dropout1.forward(u));let f=this.norm2.forward(u),h=this.crossAttn.forward(f,s,s,r,o);u=ht(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=ht(u,this.dropout3.forward(p)),u}};function $V(e){return new oa(e._dModel,e._nhead,e._dimFeedforward,e._dropout,e._activationName,e._layerNormEps,e._batchFirst,e.normFirst)}c($V,"_cloneEncoderLayer");function zV(e){return new aa(e._dModel,e._nhead,e._dimFeedforward,e._dropout,e._activationName,e._layerNormEps,e._batchFirst,e.normFirst)}c(zV,"_cloneDecoderLayer");var ql=class extends ct{static{c(this,"TransformerEncoder")}constructor(t,s,n=null){super(),this.layers=new wo(Array.from({length:s},()=>$V(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}},jl=class extends ct{static{c(this,"TransformerDecoder")}constructor(t,s,n=null){super(),this.layers=new wo(Array.from({length:s},()=>zV(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}},Fu=class extends ct{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 oa(t,s,i,o,a,f,l,u),p=new aa(t,s,i,o,a,f,l,u);this.encoder=new ql(h,n),this.decoder=new jl(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=us([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}};ie();Oe();var Uu=class extends ct{static{c(this,"PositionalEncoding")}constructor(t,s=5e3,n=.1){super(),this.dropoutLayer=new _n(n);let r=Gt([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(ht(t,n))}};var rA={...gu,...vy,...Wx,...Cy,...Oy,...Jx,...My,...eA,...nA};var oA={};ne(oA,{BatchSampler:()=>ha,DataLoader:()=>da,Dataset:()=>la,MapDataset:()=>$u,RandomSampler:()=>fa,Sampler:()=>So,SequentialSampler:()=>ua,TensorDataset:()=>ca,defaultCollate:()=>pa});Is();var la=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)}},ca=class extends la{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]=Ws(this._tensors[n],0,t);return s}},$u=class extends la{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 So=class{static{c(this,"Sampler")}*[Symbol.iterator](){throw new Error("Subclass must implement [Symbol.iterator]()")}},ua=class extends So{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}},fa=class extends So{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]}},ha=class extends So{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)}};en();Wo();hr();function VV(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=Se(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 S=0;S<o;S++){l[u*o+S]=g[w];for(let b=y-1;b>=0;b--){if(_[b]++,_[b]<d[b]){w+=m[b];break}w-=(d[b]-1)*m[b],_[b]=0}}}}return jo(l,i,r)}c(VV,"stackTensors");function pa(e){let t=e[0];if(t instanceof Ot)return VV(e,0);if(typeof t=="number")return Os(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]=pa(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]=pa(o)}return n}throw new Error(`defaultCollate: unsupported element type ${typeof t}`)}c(pa,"defaultCollate");var da=class{static{c(this,"DataLoader")}constructor(t,s={}){if(this._dataset=t,this._collate=s.collate??pa,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 fa(t):new ua(t);this._batchSampler=new ha(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 lA={};ne(lA,{DEFAULT_SPECIALS:()=>qy,TOKENIZER_FORMAT:()=>Vu,TOKENIZER_VERSION:()=>Gu,Tokenizer:()=>Ao,Vocab:()=>xo});var xo=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 Uy=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 $y=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 zy(e,t){return e+""+t}c(zy,"pairKey");var Vy=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,this._encodeCache=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){this._encodeCache=new Map;let s=new Map;for(let p of t)for(let d of this._pretokenize(p))s.set(d,(s.get(d)||0)+1);let n=new Map;for(let p of s.keys())n.set(p,this._baseSymbols(p));let r=new Map,i=new Map,o=[],a=c((p,d)=>p[0]>d[0]||p[0]===d[0]&&p[1]<d[1],"higher"),l=c((p,d)=>{o.push([p,d]);let m=o.length-1;for(;m>0;){let g=m-1>>1;if(a(o[m],o[g])){let y=o[g];o[g]=o[m],o[m]=y,m=g}else break}},"pushHeap"),u=c(()=>{let p=o[0],d=o.pop();if(o.length>0){o[0]=d;let m=0,g=o.length;for(;;){let y=m,_=2*m+1,w=2*m+2;if(_<g&&a(o[_],o[y])&&(y=_),w<g&&a(o[w],o[y])&&(y=w),y===m)break;let S=o[y];o[y]=o[m],o[m]=S,m=y}}return p},"popHeap"),f=c(p=>{let d=n.get(p),m=s.get(p);for(let g=0;g+1<d.length;g++){let y=zy(d[g],d[g+1]),_=(r.get(y)||0)+m;r.set(y,_),l(_,y);let w=i.get(y);w||(w=new Set,i.set(y,w)),w.add(p)}},"addWordPairs"),h=c(p=>{let d=n.get(p),m=s.get(p);for(let g=0;g+1<d.length;g++){let y=zy(d[g],d[g+1]),_=(r.get(y)||0)-m;_<=0?r.delete(y):(r.set(y,_),l(_,y));let w=i.get(y);w&&w.delete(p)}},"removeWordPairs");for(let p of s.keys())f(p);this._ranks=new Map;for(let p=0;p<this._numMerges;p++){let d=null,m=0;for(;o.length>0;){let[b,A]=u(),x=r.get(A);if(x===b&&x>0){d=A,m=x;break}}if(d===null||m<=0)break;let g=d.indexOf(""),y=d.slice(0,g),_=d.slice(g+1),w=y+_;this._ranks.set(d,p);let S=i.get(d);if(!S||S.size===0)break;for(let b of[...S]){h(b);let A=n.get(b),x=[];for(let N=0;N<A.length;N++)N+1<A.length&&A[N]===y&&A[N+1]===_?(x.push(w),N++):x.push(A[N]);n.set(b,x),f(b)}}}_encodeWord(t){let s=this._encodeCache.get(t);if(s!==void 0)return s;let n=this._baseSymbols(t);for(;n.length>1;){let r=1/0,i=-1;for(let o=0;o+1<n.length;o++){let a=this._ranks.get(zy(n[o],n[o+1]));a!==void 0&&a<r&&(r=a,i=o)}if(i<0)break;n=n.slice(0,i).concat(n[i]+n[i+1],n.slice(i+2))}return this._encodeCache.set(t,n),n}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(zy(r[0],r[1]),n)}return s}};Wo();import{writeFileSync as EO,appendFileSync as GV,readFileSync as TO,existsSync as qV,mkdirSync as jV,readdirSync as WV,unlinkSync as KV,renameSync as HV}from"node:fs";import{resolve as Np}from"node:path";var Ee={readFile(e){return TO(Np(e),"utf8")},readBinary(e){return TO(Np(e))},writeFile(e,t){EO(e,t)},writeBinary(e,t){EO(Np(e),t)},appendFile(e,t){GV(e,t)},exists(e){return qV(e)},mkdir(e){jV(e,{recursive:!0})},readdir(e){return WV(e)},remove(e){KV(e)},rename(e,t){HV(Np(e),Np(t))}};var Vu="mlfw-tokenizer",Gu=1,qy=Object.freeze({pad:"<pad>",unk:"<unk>",bos:"<bos>",eos:"<eos>"}),NO=`${Vu}-v${Gu}`,Gy={word:Uy,char:$y,bpe:Vy},Rp=Object.freeze(["pad","unk","bos","eos"]),Ao=class e{static{c(this,"Tokenizer")}constructor(t={}){let s=t.mode??"word",n=Gy[s];if(!n)throw new Error(`Unknown tokenizer mode '${s}'. Available: ${Object.keys(Gy).join(", ")}`);this._mode=s,this._specials=aA(t.specialTokens),RO(this._specials),this._maxVocab=t.vocabSize??null,vO(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 xo(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:Vu,version:Gu,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";Ee.writeFile(s,JV(this.toJSON())),Ee.rename(s,t)}static load(t){if(typeof t!="string")throw new Error("mlfw tokenizer: load(path) requires a file path string");let s=Ee.readFile(t),n=typeof s=="string"?s:new TextDecoder().decode(s);return e.fromJSON(XV(n))}static fromJSON(t){YV(t);let s=Gy[t.mode],n=new e({mode:t.mode,vocabSize:t.config.vocabSize,specialTokens:t.specialTokens});n._strategy=s.fromJSON(t.strategy),n._vocab=xo.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 Os(l,{shape:[a,i],dtype:"i32"})}};function aA(e){if(Array.isArray(e)){let t={...qy};for(let s=0;s<e.length&&s<Rp.length;s++)t[Rp[s]]=e[s];return t}return{...qy,...e??{}}}c(aA,"normalizeSpecials");function RO(e){let t=new Set;for(let s of Rp){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(RO,"validateSpecials");function vO(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(vO,"validateVocabSize");function YV(e){if(!e||typeof e!="object")throw new Error("mlfw tokenizer: artifact must be an object");if(e.format!==Vu)throw new Error("mlfw tokenizer: unrecognized tokenizer format");if(e.version!==Gu)throw new Error(`mlfw tokenizer: unsupported tokenizer version ${e.version}`);if(!Gy[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(RO(aA(e.specialTokens)),vO(e.config.vocabSize,aA(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(YV,"validateTokenizerData");function JV(e){let t=[NO,Wl("m",[e.mode]),Wl("z",[e.config.vocabSize==null?"":String(e.config.vocabSize)]),Wl("s",Rp.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(Wl("g",[String(e.strategy.lowercase?1:0),String(e.strategy.numMerges),e.strategy.endOfWord])),t.push(Wl("r",s))}else t.push(Wl("g",[String(e.strategy.lowercase?1:0)]));return t.push(Wl("v",e.vocab)),t.join(`
|
|
203
|
-
`)}c(JV,"serializeTokenizer");function XV(e){if(!e.startsWith(NO))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),QV(u.slice(f+1)))}let s=zu(t,"m")[0],n=zu(t,"z")[0],r=zu(t,"s"),i=zu(t,"g"),o=zu(t,"v"),a=Object.fromEntries(Rp.map((u,f)=>[u,r[f]])),l={lowercase:i[0]==="1"};if(s==="bpe"){let u=zu(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:Vu,version:Gu,mode:s,config:{vocabSize:n===""?null:Number(n),...l},specialTokens:a,vocab:o,strategy:l}}c(XV,"parseTokenizer");function Wl(e,t){return`${e} ${t.map(ZV).join(" ")}`}c(Wl,"record");function QV(e){return e.split(" ").map(t3)}c(QV,"parseRecord");function ZV(e){return String(e).replace(/\\/g,"\\\\").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r")}c(ZV,"escapeField");function t3(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(t3,"unescapeField");function zu(e,t){let s=e.get(t);if(!s)throw new Error(`mlfw tokenizer: missing '${t}' record`);return s}c(zu,"requiredField");var dE={};ne(dE,{Adam:()=>ga,AdamW:()=>ya,CosineAnnealingLR:()=>ba,FusedAdam:()=>Q_,FusedOptimizer:()=>uf,FusedSGD:()=>X_,GradScaler:()=>Z_,LRScheduler:()=>_a,Optimizer:()=>Kn,ReduceLROnPlateau:()=>Sa,SGD:()=>ma,StepLR:()=>wa,clipGradNorm_:()=>qu,clipGradValue_:()=>ju});var Kn=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=e3(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]=kO(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]=kO(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 e3(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(e3,"normalizeParamGroups");function kO(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(kO,"isTypedArray");var ma=class extends Kn{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()}}}};Mr();Cn();var ga=class extends Kn{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=Ix();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===De.GPU){h.step=(h.step||0)+1;let x=h.step,N=1-Math.pow(l,x),I=1-Math.pow(u,x);if(t(f,h,{beta1:l,beta2:u,omb1:1-l,omb2:1-u,eps:i,stepSize:n/N,bc2sqrt:Math.sqrt(I),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),S=1-Math.pow(u,g),b=n/w,A=Math.sqrt(S);if(a){let x=h.maxExpAvgSq;for(let N=0;N<m;N++){let I=o!==0?d[N]+o*p[N]:d[N];y[N]=l*y[N]+(1-l)*I,_[N]=u*_[N]+(1-u)*I*I,_[N]>x[N]&&(x[N]=_[N]),p[N]-=b*y[N]/(Math.sqrt(x[N])/A+i)}}else for(let x=0;x<m;x++){let N=o!==0?d[x]+o*p[x]:d[x];y[x]=l*y[x]+(1-l)*N,_[x]=u*_[x]+(1-u)*N*N,p[x]-=b*y[x]/(Math.sqrt(_[x])/A+i)}f._impl.bumpVersion()}}}};var ya=class extends Kn{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),S=s/_,b=Math.sqrt(w);if(i!==0){let A=1-s*i;for(let x=0;x<p;x++)f[x]*=A}if(o){let A=d.maxExpAvgSq;for(let x=0;x<p;x++)g[x]=a*g[x]+(1-a)*h[x],y[x]=l*y[x]+(1-l)*h[x]*h[x],y[x]>A[x]&&(A[x]=y[x]),f[x]-=S*g[x]/(Math.sqrt(A[x])/b+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]-=S*g[A]/(Math.sqrt(y[A])/b+r);u._impl.bumpVersion()}}}};var _a=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]}},wa=class extends _a{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)}},ba=class extends _a{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)}},Sa=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)}}};ie();function CO(e){return(Array.isArray(e)?e:[...e]).filter(s=>s.grad!==null)}c(CO,"_gradParams");function qu(e,t,s=2){let n=CO(e);if(n.length===0)return 0;let r;if(s===2){let o=null;for(let a of n){let l=ye(it(a.grad,a.grad));o=o===null?l:ht(o,l)}r=Math.sqrt(o.item())}else if(s===1/0){let o=null;for(let a of n){let l=ti(Ko(a.grad));o=o===null?l:Xr(o,l)}r=o.item()}else{let o=null;for(let a of n){let l=ye(Or(Ko(a.grad),s));o=o===null?l:ht(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=it(o.grad,i);return r}c(qu,"clipGradNorm_");function ju(e,t){for(let s of CO(e))s.grad=Gh(s.grad,-t,t)}c(ju,"clipGradValue_");di();Ft();J_();_i();var ri=st.F32,ze=c(e=>new M([e],ri),"VEC"),cf=new M([],ri);function $r(e,t,s){return e.broadcast(t,[s],[]).getResult(0)}c($r,"bcast");var uf=class extends Kn{static{c(this,"FusedOptimizer")}constructor(t,s,n=null){super(t,s),this._target=n||ur(),this._kernels=new Map}_kernel(t){let s=this._kernels.get(t);if(!s){let n=this._buildGraph(t);s=OM(n,this._target,{fusion:{enabled:!0}}),this._kernels.set(t,s)}return s}_buildGraph(t){throw new Error(`${this.constructor.name}._buildGraph() not implemented`)}},X_=class extends uf{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?[ze(t),ze(t),ze(t),cf]:[ze(t),ze(t),cf],l=o?[ze(t),ze(t)]:[ze(t)];return ah("sgd_update",a,l,(f,h)=>{let p=h[0],d=h[1],m=o?h[3]:h[2],g=$r(f,m,t),y=d;if(r!==0){let N=f.scalarConstant(r,ri).getResult(0);y=f.add(d,f.mul($r(f,N,t),p).getResult(0)).getResult(0)}if(!o){let N=f.sub(p,f.mul(g,y).getResult(0)).getResult(0);f.returnOp([N]);return}let _=h[2],w=$r(f,f.scalarConstant(s,ri).getResult(0),t),S=$r(f,f.scalarConstant(1-n,ri).getResult(0),t),b=f.add(f.mul(w,_).getResult(0),f.mul(S,y).getResult(0)).getResult(0),A=b;i&&(A=f.add(y,f.mul(w,b).getResult(0)).getResult(0));let x=f.sub(p,f.mul(g,A).getResult(0)).getResult(0);f.returnOp([x,b])})}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()}}}},Q_=class extends uf{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?[ze(t),ze(t),ze(t),ze(t),ze(t),cf,cf]:[ze(t),ze(t),ze(t),ze(t),cf,cf],u=i?[ze(t),ze(t),ze(t),ze(t)]:[ze(t),ze(t),ze(t)];return ah("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],S=d;if(r!==0){let j=$r(f,f.scalarConstant(r,ri).getResult(0),t);S=f.add(d,f.mul(j,p).getResult(0)).getResult(0)}let b=$r(f,f.scalarConstant(o,ri).getResult(0),t),A=$r(f,f.scalarConstant(a,ri).getResult(0),t),x=$r(f,f.scalarConstant(1-o,ri).getResult(0),t),N=$r(f,f.scalarConstant(1-a,ri).getResult(0),t),I=$r(f,f.scalarConstant(n,ri).getResult(0),t),E=f.add(f.mul(b,m).getResult(0),f.mul(x,S).getResult(0)).getResult(0),T=f.mul(S,S).getResult(0),B=f.add(f.mul(A,g).getResult(0),f.mul(N,T).getResult(0)).getResult(0),k=B,O=null;i&&(O=f.maximum(y,B).getResult(0),k=O);let D=f.add(f.div(f.sqrt(k).getResult(0),$r(f,w,t)).getResult(0),I).getResult(0),q=f.mul($r(f,_,t),f.div(E,D).getResult(0)).getResult(0),z=f.sub(p,q).getResult(0);f.returnOp(i?[z,E,B,O]:[z,E,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)]),S=this._kernel(p),b=d.expAvg,A=d.expAvgSq;if(o){let x=d.maxExpAvgSq;S.run("adam_update",f,h,b,A,x,_,w,f,b,A,x)}else S.run("adam_update",f,h,b,A,_,w,f,b,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)}};sw();TE();_i();var IE={};ne(IE,{Accuracy:()=>Oa,CSVLogger:()=>Ca,Callback:()=>Ys,CallbackConnector:()=>df,ConfusionMatrix:()=>Pa,ConsoleLogger:()=>zi,EarlyStopping:()=>Na,EvaluationLoop:()=>wf,F1Score:()=>La,FitLoop:()=>_f,GradientAccumulationScheduler:()=>ka,HOOKS:()=>n1,LearningRateMonitor:()=>Ra,LightningModule:()=>Ea,Logger:()=>$i,LoggerConnector:()=>mf,MeanMetric:()=>Ef,Metric:()=>an,MetricAccumulator:()=>ff,MetricCollection:()=>Ia,ModelCheckpoint:()=>ai,Precision:()=>Ba,PredictionLoop:()=>bf,ProgressCallback:()=>oi,Recall:()=>Ma,SingleDeviceStrategy:()=>pf,Stage:()=>Hs,SumMetric:()=>Tf,Timer:()=>va,Trainer:()=>Ta,TrainerState:()=>hf,TrainingLoop:()=>gf,ValidationLoop:()=>yf,applyCheckpoint:()=>Af,deserializeCheckpoint:()=>uw,loadCheckpoint:()=>xf,serializeCheckpoint:()=>Sf});var Ea=class extends ct{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 s1(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(e1(r.lrScheduler))):(t.push(r),s.push(null))}return{optimizers:t,schedulerConfigs:s}}if(e.optimizer)return{optimizers:[e.optimizer],schedulerConfigs:[e1(e.lrScheduler)]};if(typeof e.step=="function")return{optimizers:[e],schedulerConfigs:[null]};throw new Error("configureOptimizers() returned an unrecognized format")}c(s1,"parseOptimizersConfig");function e1(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(e1,"normalizeSchedulerConfig");var Hs=Object.freeze({IDLE:"idle",TRAINING:"training",VALIDATING:"validating",TESTING:"testing",PREDICTING:"predicting"}),QG=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 ZG(){return{sum:0,count:0,min:1/0,max:-1/0,last:0}}c(ZG,"createAccumulator");function t4(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(t4,"updateAccumulator");var ff=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,ZG()),this._reduceFns.set(t,n)),t4(this._accumulators.get(t),s)}compute(t){let s=this._accumulators.get(t);if(!s)return;let n=QG[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}},hf=class{static{c(this,"TrainerState")}constructor(){this.stage=Hs.IDLE,this.epoch=0,this.globalStep=0,this.maxEpochs=0,this.maxSteps=-1,this.shouldStop=!1,this.stepMetrics=new ff,this.epochMetrics=new ff}resetEpochMetrics(){this.epochMetrics.reset()}resetStepMetrics(){this.stepMetrics.reset()}},pf=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 n1=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"}),df=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)}},mf=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)}};ie();oo();Ci();var gf=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=e4(n.limitTrainBatches,s.length);o.numTrainingBatches=h,o.stage=Hs.TRAINING,t.train(),Lr.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),b0(),o.globalStep++,p++}b0();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=fe(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(()=>(TE(),t1)),{CPUTarget:_,CUDATarget:w,WebGPUTarget:S}=await Promise.resolve().then(()=>(_i(),gv)),b=t._device&&t._device.type,A=b==="webgpu"?S():b==="gpu"?w():_();t.__compiledTrainStep=y({forward:u},l,{target:A,mode:r.compileMode});let x=t.log;t.log=()=>{};try{f=t.__compiledTrainStep(...l),f&&f.then&&(f=await f)}finally{t.log=x}}let h=t.__compiledTrainStep,p=h.capturedParams(),{ones:d}=await Promise.resolve().then(()=>(Oe(),fC)),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(()=>(Rg(),Ng));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(()=>(Ym(),wk)),l=await Promise.resolve().then(()=>(Bh(),Mk)),u=await Promise.resolve().then(()=>(ml(),lk)),f=t.__eagerGraphRunner;if(f||(f=t.__eagerGraphRunner={phase:"warmup",seen:0},l.setEagerDeferred(!0),qc(!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=NE(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"?qu(n,s.gradientClipVal):ju(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 NE(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)NE(s,t);return t}if(typeof e=="object"){for(let s of Object.keys(e))NE(e[s],t);return t}return t}c(NE,"_flattenTensors");function e4(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(e4,"resolveLimit");var yf=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=Hs.VALIDATING,t.eval(),t.onValidationEpochStart(),o.dispatch("onValidationStart",n,t),o.dispatch("onValidationEpochStart",n,t);let u=n4(n.limitValBatches,s.length);i.numValBatches=u;let f=0;await s4(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 s4(e){let{GradMode:t}=await Promise.resolve().then(()=>(Ci(),xg)),s=t.isEnabled();t.setEnabled(!1);try{await e()}finally{t.setEnabled(s)}}c(s4,"noGradAsync");function n4(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(n4,"resolveLimit");var _f=class{static{c(this,"FitLoop")}constructor(){this._trainingLoop=new gf,this._validationLoop=new yf}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 wf=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=Hs.TESTING,t.eval(),t.onTestEpochStart(),i.dispatch("onTestStart",n,t),i.dispatch("onTestEpochStart",n,t);let l=i4(n.limitTestBatches,s.length),u=0;await r4(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 r4(e){let{GradMode:t}=await Promise.resolve().then(()=>(Ci(),xg)),s=t.isEnabled();t.setEnabled(!1);try{await e()}finally{t.setEnabled(s)}}c(r4,"noGradAsync");function i4(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(i4,"resolveLimit");var bf=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=Hs.PREDICTING,t.eval(),i.dispatch("onPredictStart",n,t);let l=[],u=a4(n.limitTestBatches,s.length),f=0;return await o4(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 o4(e){let{GradMode:t}=await Promise.resolve().then(()=>(Ci(),xg)),s=t.isEnabled();t.setEnabled(!1);try{await e()}finally{t.setEnabled(s)}}c(o4,"noGradAsync");function a4(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(a4,"resolveLimit");var $i=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 zi=class extends $i{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]}: ${l4(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 l4(e){return typeof e!="number"||Number.isInteger(e)?String(e):Math.abs(e)<.001&&e!==0?e.toExponential(3):e.toFixed(4)}c(l4,"formatNumber");var Ys=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 c4=["\u258F","\u258E","\u258D","\u258C","\u258B","\u258A","\u2589"],oi=class extends Ys{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??r1(t.limitTrainBatches)}_valTotal(t){return t.state.numValBatches??r1(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=`${i1(h)}<${i1(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?c4[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}=${u4(i)}`);return", "+n.join(", ")}};function r1(e){return e==null?null:typeof e=="number"&&e>1?e:null}c(r1,"resolveTotal");function i1(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(i1,"fmtTime");function u4(e){return typeof e!="number"||Number.isInteger(e)?String(e):e.toFixed(4)}c(u4,"formatNum");function Ro(...e){return e.filter(t=>t!=null&&t!=="").join("/").replace(/\/+/g,"/")}c(Ro,"joinPath");en();Wo();hr();var a1="mlfw-ckpt-v1",Yp=8,f4={Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array};function h4(e){return ArrayBuffer.isView(e)&&!(e instanceof DataView)}c(h4,"isTypedArray");function RE(e){return new Uint8Array(e.buffer,e.byteOffset,e.byteLength)}c(RE,"byteView");function Sf(e){let t=[],s=lw(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:a1,tree:s,buffers:t.map(l=>l.meta)},i=new TextEncoder().encode(JSON.stringify(r)),o=new Uint8Array(Yp+i.length+n);new DataView(o.buffer).setBigUint64(0,BigInt(i.length),!0),o.set(i,Yp);let a=Yp+i.length;for(let l of t)o.set(l.bytes,a),a+=l.bytes.length;return o}c(Sf,"serializeCheckpoint");function uw(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=Yp+n,i=JSON.parse(new TextDecoder().decode(t.subarray(Yp,r)));if(i.format!==a1)throw new Error("mlfw: unrecognized checkpoint format: "+i.format);let o=i.buffers.map(a=>p4(a,t,r));return cw(i.tree,o)}c(uw,"deserializeCheckpoint");function lw(e,t){if(e instanceof Ot)return o1(t,{kind:"tensor",dtype:e.dtype,shape:e.shape},RE(e._impl.storage.data));if(h4(e))return o1(t,{kind:"array",arrayType:e.constructor.name},RE(e));if(e instanceof Map){let s=[];for(let[n,r]of e)s.push([n,lw(r,t)]);return{$map:s}}if(Array.isArray(e))return e.map(s=>lw(s,t));if(e&&typeof e=="object"){let s={};for(let n of Object.keys(e))s[n]=lw(e[n],t);return s}return e}c(lw,"encodeNode");function o1(e,t,s){let n=e.length;return e.push({meta:t,bytes:s}),{$buf:n}}c(o1,"pushBuffer");function p4(e,t,s){let n=s+e.offset,r=t.subarray(n,n+e.length),i=e.kind==="tensor"?Se(e.dtype):f4[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 RE(o).set(r),e.kind==="tensor"?jo(o,e.shape,e.dtype):o}c(p4,"decodeBuffer");function cw(e,t){if(e===null||typeof e!="object")return e;if(Array.isArray(e))return e.map(n=>cw(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,cw(i,t));return n}let s={};for(let n of Object.keys(e))s[n]=cw(e[n],t);return s}c(cw,"decodeNode");var vE=".ckpt",ai=class extends Ys{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=Ro(this._dirpath,"last"+vE);this._saveCheckpoint(s,t,f),this._lastModelPath=f}if(!this._monitor){let f=Ro(this._dirpath,r+vE);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=Ro(this._dirpath,r+vE),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";Ee.writeBinary(o,Sf(r)),Ee.rename(o,n)}_fillTemplate(t){return this._filename.replace("{epoch}",t.epoch).replace("{step}",t.globalStep)}_ensureDir(){Ee.exists(this._dirpath)||Ee.mkdir(this._dirpath)}_tryDelete(t){try{Ee.remove(t)}catch{}}};function xf(e){return uw(Ee.readBinary(e))}c(xf,"loadCheckpoint");function Af(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(Af,"applyCheckpoint");Cn();var Ta=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:S=!1,defaultRootDir:b="./lightning_logs",compile:A=!1,compileMode:x="separate",cudaGraph:N=!1,cudaGraphWarmupSteps:I=3}={}){if(this._state=new hf,this._state.maxEpochs=t,this._state.maxSteps=s,this._compile=A,this._compileMode=x,this._cudaGraph=N,this._cudaGraphWarmupSteps=I,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=b,S!==!1){let T=typeof S=="number"?S:1;this._limitTrainBatches=T,this._limitValBatches=T,this._limitTestBatches=T,this._state.maxEpochs=1}this._loggers=this._resolveLoggers(o),this._strategy=new pf,this._fitLoop=new _f,this._evaluationLoop=new wf,this._predictionLoop=new bf;let E=[...i];l&&!E.some(T=>T instanceof oi)&&E.push(new oi),a&&!E.some(T=>T instanceof ai)&&E.push(new ai({dirpath:b})),this._callbackConnector=new df(E),this._loggerConnector=new mf(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}=s1(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,Hs.TRAINING),this._callbackConnector.dispatch("onFitStart",this,t),this._state.shouldStop=!1,await this._fitLoop.run(t,s,n,this,i,o),r===wl){let{teardownAfterFit:a}=await Promise.resolve().then(()=>(Rg(),Ng));a(t,i)}this._callbackConnector.dispatch("onFitEnd",this,t),this._callbackConnector.dispatch("teardown",this,t,Hs.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,Hs.VALIDATING);let r=await this._fitLoop.validationLoop.run(t,s,this,null);this._callbackConnector.dispatch("teardown",this,t,Hs.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,Hs.TESTING);let r=await this._evaluationLoop.run(t,s,this);this._callbackConnector.dispatch("teardown",this,t,Hs.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"?wl:this._accelerator==="wasm"?Yc:this._accelerator==="webgpu"?kr:this._accelerator==="cpu"?Hc:Hc}_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(()=>(pp(),JS));await s()}else if(t.type==="webgpu"){let{preloadWebGPU:s}=await Promise.resolve().then(()=>(pp(),JS));this._webgpuMod=await s()}}async _flushEagerInference(){this._webgpuMod&&await this._webgpuMod.flushWebGPUEager()}_resolveLoggers(t){return t===!1||t===null?[]:t===!0?[new zi]: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 Na=class extends Ys{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 Ra=class extends Ys{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 va=class extends Ys{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 ka=class extends Ys{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 Ca=class extends $i{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 Ro(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=Ro(this.logDir,"hparams.json");Ee.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(",");Ee.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(","))}Ee.appendFile(t,n.join(`
|
|
207
|
-
`)+`
|
|
208
|
-
`),this._buffer.length=0}_getFilePath(){return this._filePath||(this._filePath=Ro(this.logDir,"metrics.csv")),this._filePath}_ensureDir(){let t=this.logDir;Ee.exists(t)||Ee.mkdir(t)}_resolveVersion(){if(this._version!==null)return this._version;let t=Ro(this._saveDir,this._name);if(!Ee.exists(t))return this._version=0,0;let s=-1;try{let n=Ee.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 an=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 Ef=class extends an{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}},Tf=class extends an{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 Ia=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 Oa=class extends an{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=d4(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 d4(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&&m4(r)):o>r[0].val&&(r[0]={val:o,idx:i},l1(r,0))}return r.map(i=>i.idx)}c(d4,"topKIndices");function m4(e){for(let t=(e.length>>>1)-1;t>=0;t--)l1(e,t)}c(m4,"heapify");function l1(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(l1,"siftDown");var Ba=class extends an{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){kE(t,s,this._tp,this._fp,null,this._support,this._task,this._numClasses)}compute(){return y4(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)}},Ma=class extends an{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){kE(t,s,this._tp,null,this._fn,this._support,this._task,this._numClasses)}compute(){return _4(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)}},La=class extends an{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){kE(t,s,this._tp,this._fp,this._fn,this._support,this._task,this._numClasses)}compute(){let t=c1(this._tp,this._fp,this._numClasses),s=u1(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 CE(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 kE(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=g4(l,p,a):m=l[p]|0,i[d]++,m===d?s&&s[d]++:(n&&n[m]++,r&&r[d]++)}}c(kE,"updateConfusionCounters");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");function c1(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(c1,"computePrecisionPerClass");function u1(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(u1,"computeRecallPerClass");function y4(e,t,s,n,r){let i=c1(e,t,n);return CE(i,s,n,r)}c(y4,"computePrecision");function _4(e,t,s,n,r){let i=u1(e,t,n);return CE(i,s,n,r)}c(_4,"computeRecall");function CE(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(CE,"aggregate");var Pa=class extends an{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=w4(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 w4(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(w4,"argmaxRow");sO();cO();fO(Ot);en();Cn();pp();Mg();Ci();Ul();sw();ws();var Rf=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 f1(e){return S4(b4(e))}c(f1,"tokenize");function b4(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 Rf("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 Rf(`Unexpected character '${a}'`,n,r)}return i("eof",null),t}c(b4,"tokenizeRaw");function S4(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(S4,"addIndentation");var x4={or:1,and:2,"==":3,"!=":3,"<":3,"<=":3,">":3,">=":3,"+":4,"-":4,"*":5,"/":5,"@":5,"**":6},vf={"+=":"+","-=":"-","*=":"*","/=":"/","**=":"**","@=":"@"};function fw(e){return new OE(f1(e)).parseProgram()}c(fw,"parse");var OE=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!=="="&&!vf[this.peek(1).value])return this.parseTrain();if(this.atIdentifier("validate")&&this.peek(1).value!=="="&&!vf[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(vf[n]!==void 0){let r=this.next().value,i=this.next(),o=vf[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"&&vf[this.current().value]!==void 0){let n=vf[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=x4[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 Rf(t,s.line,s.column)}locate(t,s){return t.line=s.line,t.column=s.column,t}};en();function h1(e){if(e instanceof Ot){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(h1,"formatValueCompact");function Ql(e){if(e instanceof Ot){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 ct||e instanceof Da)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 Da=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 Jp(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(Jp,"formatTrace");ie();en();Cn();Mg();aE();function A4(e,t=","){let s=E4(e);if(s.length===0)throw new Error("CSV file is empty");let n=p1(s[0],t),r=n.length,i=[];for(let o=1;o<s.length;o++){let a=p1(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]]=T4(a[u]);i.push(l)}return{headers:n,rows:i}}c(A4,"parseCsvRows");function d1(e,t=","){let s=Ee.readFile(e);return A4(s,t)}c(d1,"loadCsvRows");function E4(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(E4,"splitLines");function p1(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(p1,"parseRow");function T4(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(T4,"coerceValue");import{Worker as Cj}from"worker_threads";import{fileURLToPath as Ij}from"url";import{dirname as Oj,join as Bj}from"path";import{Worker as Gj}from"worker_threads";import{promises as qj}from"fs";import{createServer as rW,request as iW,Agent as oW}from"http";import Lf from"fs";import BH from"os";import jT from"path";import{promises as CL}from"fs";import IL from"fs";import FH from"path";import Cw from"fs";import{promises as Iw}from"fs";import $H from"path";import{readFile as GH}from"node:fs/promises";import{fileURLToPath as qH}from"node:url";import{dirname as jH,join as WH}from"node:path";var N4=Object.defineProperty,R4=Object.getOwnPropertyNames,tt=c((e,t)=>c(function(){return e&&(t=(0,e[R4(e)[0]])(e=0)),t},"__init"),"__esm"),gs=c((e,t)=>{for(var s in t)N4(e,s,{get:t[s],enumerable:!0})},"__export");function v4(e){return{kind:pt.EXPLAIN_STMT,query:e}}c(v4,"ExplainStmt");function k4({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:pt.SELECT_STMT,withClause:e,distinct:t,selectItems:s,from:n,where:r,groupBy:i,having:o,orderBy:a,limit:l,offset:u}}c(k4,"SelectStmt");function C4(e,t,s,n=!1){return{kind:pt.SET_OP,op:e,left:t,right:s,all:n}}c(C4,"SetOp");function I4(e){return{kind:pt.WITH_CLAUSE,ctes:e}}c(I4,"WithClause");function O4(e,t,s=null){return{kind:pt.CTE,name:e,query:t,columnAliases:s}}c(O4,"CTE");function BE(e,t=null){return{kind:pt.SELECT_ITEM,expr:e,alias:t}}c(BE,"SelectItem");function hw(e=null){return{kind:pt.ALL_COLUMNS,table:e}}c(hw,"AllColumns");function B4(e,t=null){return{kind:pt.TABLE_REF,name:e,alias:t||e}}c(B4,"TableRef");function ME(e,t,s,n=null){return{kind:pt.JOIN_REF,left:e,right:t,joinType:s,condition:n}}c(ME,"JoinRef");function M4(e,t){return{kind:pt.SUBQUERY_REF,query:e,alias:t}}c(M4,"SubqueryRef");function m1(e,t=null){return{kind:pt.COLUMN_REF,name:e,table:t}}c(m1,"ColumnRef");function vo(e,t=null){return{kind:pt.LITERAL,value:e,dataType:t}}c(vo,"Literal");function kf(e,t,s){return{kind:pt.BINARY_EXPR,op:e,left:t,right:s}}c(kf,"BinaryExpr");function LE(e,t){return{kind:pt.UNARY_EXPR,op:e,operand:t}}c(LE,"UnaryExpr");function L4(e,t,s,n=!1){return{kind:pt.BETWEEN_EXPR,expr:e,low:t,high:s,negated:n}}c(L4,"BetweenExpr");function g1(e,t,s=!1){return{kind:pt.IN_EXPR,expr:e,list:t,negated:s}}c(g1,"InExpr");function P4(e,t,s=!1){return{kind:pt.LIKE_EXPR,expr:e,pattern:t,negated:s}}c(P4,"LikeExpr");function D4(e,t=!1){return{kind:pt.IS_NULL_EXPR,expr:e,negated:t}}c(D4,"IsNullExpr");function y1(e,t=!1){return{kind:pt.EXISTS_EXPR,query:e,negated:t}}c(y1,"ExistsExpr");function _1(e){return{kind:pt.SUBQUERY_EXPR,query:e}}c(_1,"SubqueryExpr");function w1(e,t){return{kind:pt.FUNCTION_CALL,name:e,args:t}}c(w1,"FunctionCall");function b1(e,t,s=!1){return{kind:pt.AGGREGATE_CALL,name:e,args:t,distinct:s}}c(b1,"AggregateCall");function F4(e,t,s=null){return{kind:pt.CASE_EXPR,operand:e,whenClauses:t,elseExpr:s}}c(F4,"CaseExpr");function U4(e,t){return{kind:pt.WHEN_CLAUSE,condition:e,result:t}}c(U4,"WhenClause");function $4(e,t){return{kind:pt.CAST_EXPR,expr:e,targetType:t}}c($4,"CastExpr");function z4(e,t){return{kind:pt.EXTRACT_EXPR,field:e,source:t}}c(z4,"ExtractExpr");function V4(e,t,s=null){return{kind:pt.SUBSTRING_EXPR,expr:e,from:t,length:s}}c(V4,"SubstringExpr");function G4(e,t){return{kind:pt.INTERVAL_EXPR,value:e,unit:t}}c(G4,"IntervalExpr");function q4(e,t="ASC",s=null){return{kind:pt.ORDER_KEY,expr:e,direction:t,nullOrder:s}}c(q4,"OrderKey");function j4(e,t=[]){return{kind:pt.TYPE_NAME,name:e,params:t}}c(j4,"TypeName");function W4(e=[],t=[]){return{kind:pt.WINDOW_SPEC,partitionBy:e,orderBy:t}}c(W4,"WindowSpec");function K4(e,t,s){return{kind:pt.WINDOW_CALL,name:e,args:t,windowSpec:s}}c(K4,"WindowCall");function H4(e,t,s=!1){return{kind:pt.CREATE_TABLE_STMT,name:e,columns:t,ifNotExists:s}}c(H4,"CreateTableStmt");function Y4(e,t=!1){return{kind:pt.DROP_TABLE_STMT,name:e,ifExists:t}}c(Y4,"DropTableStmt");function J4(e,t){return{kind:pt.COLUMN_DEF,name:e,typeName:t}}c(J4,"ColumnDef");function X4(e){return{kind:pt.EXPLAIN_ANALYZE_STMT,query:e}}c(X4,"ExplainAnalyzeStmt");var pt,Zw=tt({"src/parser/ast.js"(){pt={EXPLAIN_STMT:"ExplainStmt",SELECT_STMT:"SelectStmt",SET_OP:"SetOp",WITH_CLAUSE:"WithClause",CTE:"CTE",SELECT_ITEM:"SelectItem",ALL_COLUMNS:"AllColumns",TABLE_REF:"TableRef",JOIN_REF:"JoinRef",SUBQUERY_REF:"SubqueryRef",COLUMN_REF:"ColumnRef",LITERAL:"Literal",BINARY_EXPR:"BinaryExpr",UNARY_EXPR:"UnaryExpr",BETWEEN_EXPR:"BetweenExpr",IN_EXPR:"InExpr",LIKE_EXPR:"LikeExpr",IS_NULL_EXPR:"IsNullExpr",EXISTS_EXPR:"ExistsExpr",SUBQUERY_EXPR:"SubqueryExpr",FUNCTION_CALL:"FunctionCall",AGGREGATE_CALL:"AggregateCall",CASE_EXPR:"CaseExpr",WHEN_CLAUSE:"WhenClause",CAST_EXPR:"CastExpr",EXTRACT_EXPR:"ExtractExpr",SUBSTRING_EXPR:"SubstringExpr",INTERVAL_EXPR:"IntervalExpr",ORDER_KEY:"OrderKey",TYPE_NAME:"TypeName",WINDOW_SPEC:"WindowSpec",WINDOW_CALL:"WindowCall",CREATE_TABLE_STMT:"CreateTableStmt",DROP_TABLE_STMT:"DropTableStmt",COLUMN_DEF:"ColumnDef",EXPLAIN_ANALYZE_STMT:"ExplainAnalyzeStmt"}}});function Uf(e){let t=ML[e];if(!t)throw new Error(`No TypedArray for type ${e}`);return t}c(Uf,"typedArrayCtorFor");function Q4(e,t){return new(Uf(e))(t)}c(Q4,"typedArrayFor");function $f(e){return LL[e]??0}c($f,"byteWidthFor");function ui(e){return BL.has(e)}c(ui,"isFixedWidth");function Fw(e){return e===L.INT32||e===L.INT64||e===L.FLOAT64||e===L.DECIMAL}c(Fw,"isNumeric");function S1(e){return e===L.DATE||e===L.TIMESTAMP}c(S1,"isTemporal");function Z4(e,t){return!!(e===t||Fw(e)&&Fw(t)||S1(e)&&S1(t))}c(Z4,"isComparable");function gd(e,t,s){let n=Date.UTC(e,t-1,s);return Math.floor(n/864e5)}c(gd,"dateToEpochDays");function OL(e){let t=e*864e5,s=new Date(t);return{year:s.getUTCFullYear(),month:s.getUTCMonth()+1,day:s.getUTCDate()}}c(OL,"epochDaysToDate");function t6(e,t,s,n,r,i,o=0){return Date.UTC(e,t-1,s,n,r,i,o)}c(t6,"timestampToEpochMs");function e6(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(e6,"epochMsToTimestamp");var L,BL,ML,LL,PL,rs=tt({"src/storage/data-type.js"(){L={BOOLEAN:"BOOLEAN",INT32:"INT32",INT64:"INT64",FLOAT64:"FLOAT64",DECIMAL:"DECIMAL",VARCHAR:"VARCHAR",DATE:"DATE",TIMESTAMP:"TIMESTAMP"},BL=new Set([L.BOOLEAN,L.INT32,L.INT64,L.FLOAT64,L.DECIMAL,L.DATE,L.TIMESTAMP]),ML={[L.BOOLEAN]:Uint8Array,[L.INT32]:Int32Array,[L.INT64]:BigInt64Array,[L.FLOAT64]:Float64Array,[L.DECIMAL]:BigInt64Array,[L.DATE]:Int32Array,[L.TIMESTAMP]:BigInt64Array},LL={[L.BOOLEAN]:1,[L.INT32]:4,[L.INT64]:8,[L.FLOAT64]:8,[L.DECIMAL]:8,[L.DATE]:4,[L.TIMESTAMP]:8},PL=100}});function za(e,t,s,n,r=0){return{kind:v.COLUMN_REF,tableAlias:e,columnName:t,columnIndex:s,dataType:n,depth:r,isCorrelated:r>0}}c(za,"BoundColumnRef");function de(e,t){return{kind:v.LITERAL,value:e,dataType:t}}c(de,"BoundLiteral");function Dn(e,t,s,n){return{kind:v.BINARY,op:e,left:t,right:s,resultType:n}}c(Dn,"BoundBinary");function lT(e,t,s){return{kind:v.UNARY,op:e,operand:t,resultType:s}}c(lT,"BoundUnary");function cT(e,t,s){return{kind:v.FUNCTION,name:e,args:t,resultType:s}}c(cT,"BoundFunction");function tb(e,t,s,n){return{kind:v.AGGREGATE,name:e,args:t,distinct:s,resultType:n}}c(tb,"BoundAggregate");function s6(e,t,s,n){return{kind:v.CASE,operand:e,whenClauses:t,elseExpr:s,resultType:n}}c(s6,"BoundCase");function DL(e,t){return{kind:v.CAST,expr:e,targetType:t,dataType:t}}c(DL,"BoundCast");function FL(e,t,s,n){return{kind:v.BETWEEN,expr:e,low:t,high:s,negated:n,resultType:L.BOOLEAN}}c(FL,"BoundBetween");function Uw(e,t,s){return{kind:v.IN_LIST,expr:e,list:t,negated:s,resultType:L.BOOLEAN}}c(Uw,"BoundInList");function UL(e,t,s){return{kind:v.LIKE,expr:e,pattern:t,negated:s,resultType:L.BOOLEAN}}c(UL,"BoundLike");function uT(e,t){return{kind:v.IS_NULL,expr:e,negated:t,resultType:L.BOOLEAN}}c(uT,"BoundIsNull");function x1(e,t){return{kind:v.SUBQUERY,plan:e,subqueryType:t}}c(x1,"BoundSubquery");function n6(e,t){return{kind:v.EXISTS,plan:e,negated:t,resultType:L.BOOLEAN}}c(n6,"BoundExists");function r6(e,t){return{kind:v.EXTRACT,field:e,source:t,resultType:L.INT32}}c(r6,"BoundExtract");function i6(e,t){return{kind:v.INTERVAL,value:e,unit:t,resultType:L.INT32}}c(i6,"BoundInterval");function o6(e,t,s,n,r){return{kind:v.WINDOW,name:e,args:t,partitionBy:s,orderBy:n,resultType:r}}c(o6,"BoundWindow");function bn(e){return e&&(e.resultType||e.dataType)||null}c(bn,"getExprType");var v,Ht=tt({"src/binder/expression-binder.js"(){Zw(),rs(),v={COLUMN_REF:"BoundColumnRef",LITERAL:"BoundLiteral",BINARY:"BoundBinary",UNARY:"BoundUnary",FUNCTION:"BoundFunction",AGGREGATE:"BoundAggregate",CASE:"BoundCase",CAST:"BoundCast",BETWEEN:"BoundBetween",IN_LIST:"BoundInList",LIKE:"BoundLike",IS_NULL:"BoundIsNull",SUBQUERY:"BoundSubquery",EXISTS:"BoundExists",EXTRACT:"BoundExtract",INTERVAL:"BoundInterval",COMPARISON:"BoundComparison",WINDOW:"BoundWindow"}}});function fT(e,t,s){return{type:C.SCAN,table:e,columns:t,alias:s||e}}c(fT,"LogicalScan");function Sn(e,t){return{type:C.FILTER,condition:e,children:[t]}}c(Sn,"LogicalFilter");function $w(e,t){return{type:C.PROJECT,expressions:e,children:[t]}}c($w,"LogicalProject");function Vr(e,t,s,n,r=Pn.HASH){return{type:C.JOIN,joinType:e,condition:t,children:[s,n],physicalStrategy:r}}c(Vr,"LogicalJoin");function yd(e,t,s,n=Pn.HASH){return{type:C.AGGREGATE,groupBy:e,aggregates:t,children:[s],physicalStrategy:n}}c(yd,"LogicalAggregate");function zw(e,t){return{type:C.SORT,orderKeys:e,children:[t]}}c(zw,"LogicalSort");function $L(e,t,s){return{type:C.LIMIT,count:e,offset:t||0,children:[s]}}c($L,"LogicalLimit");function zL(e){return{type:C.DISTINCT,children:[e]}}c(zL,"LogicalDistinct");function VL(e,t,s){return{type:C.UNION,all:!!s,children:[e,t]}}c(VL,"LogicalUnion");function a6(e,t){return{type:C.CTE_SCAN,cteName:e,cteId:t,children:[]}}c(a6,"LogicalCTEScan");function pw(e,t,s,n,r){return{type:C.DEPENDENT_JOIN,correlatedColumns:s,subqueryType:n,condition:r,children:[e,t]}}c(pw,"LogicalDependentJoin");function l6(e,t,s,n,r,i,o,a,l,u,f){return{type:C.INDEX_SCAN,table:e,alias:t||e,indexName:s,columnName:n,scanType:r,scanKey:i,scanLow:o,scanHigh:a,lowInc:l,highInc:u,columns:f}}c(l6,"LogicalIndexScan");function c6(e,t){return{type:C.WINDOW,windowExprs:e,children:[t]}}c(c6,"LogicalWindow");function u6(e){return{type:C.MATERIALIZE,children:[e]}}c(u6,"LogicalMaterialize");function f6(e,t,s,n){return{type:C.EXCHANGE,exchangeType:e,partitionKeys:t||[],partitionCount:s||0,children:[n]}}c(f6,"LogicalExchange");function h6(e,t,s){return{type:C.PARTIAL_AGGREGATE,groupBy:e,aggregates:t,children:[s],physicalStrategy:Pn.HASH}}c(h6,"LogicalPartialAggregate");function p6(e,t,s,n){return{type:C.FINAL_AGGREGATE,groupBy:e,aggregates:t,partialAggregates:s,children:[n],physicalStrategy:Pn.HASH}}c(p6,"LogicalFinalAggregate");function A1(e,t,s){return{type:C.MERGE_EXCHANGE,orderKeys:e,limit:t||null,children:[s]}}c(A1,"LogicalMergeExchange");function Fa(e,t){return{type:C.EXCHANGE_RECEIVE,sourceFragmentIds:e,schema:t||[],children:[]}}c(Fa,"LogicalExchangeReceive");function d6(){return{type:C.SINGLE_ROW,children:[]}}c(d6,"LogicalSingleRow");function ds(e){return e.children||[]}c(ds,"getChildren");function Fn(e,t){return{...e,children:t}}c(Fn,"setChildren");function GL(e,t=0){let n=`${" ".repeat(t)}${e.type}`;switch(e.type){case C.SCAN:n+=`(${e.table}${e.alias!==e.table?` AS ${e.alias}`:""})`;break;case C.JOIN:n+=`(${e.joinType})`;break;case C.LIMIT:n+=`(${e.count}${e.offset?`, offset=${e.offset}`:""})`;break;case C.CTE_SCAN:n+=`(${e.cteName})`;break;case C.CTE_ANCHOR:n+=`(${e.cteName})`;break;case C.DEPENDENT_JOIN:n+=`(${e.subqueryType})`;break;case C.WINDOW:n+=`(${e.windowExprs.map(r=>r.name).join(", ")})`;break;case C.TOP_N:n+=`(${e.count}${e.offset?`, offset=${e.offset}`:""})`;break;case C.INDEX_SCAN:n+=`(${e.table} using ${e.indexName})`;break;case C.EXCHANGE:n+=`(${e.exchangeType})`;break;case C.PARTIAL_AGGREGATE:n+="(partial)";break;case C.FINAL_AGGREGATE:n+="(final)";break;case C.MERGE_EXCHANGE:n+=`(${e.limit?`limit=${e.limit}`:"all"})`;break;case C.EXCHANGE_RECEIVE:n+=`(fragments=[${e.sourceFragmentIds.join(",")}])`;break}n+=`
|
|
220
|
-
`;for(let r of ds(e))n+=GL(r,t+1);return n}c(GL,"planToString");var C,F,Pn,hT,$t=tt({"src/planner/logical-plan.js"(){C={SCAN:"Scan",FILTER:"Filter",PROJECT:"Project",JOIN:"Join",AGGREGATE:"Aggregate",SORT:"Sort",LIMIT:"Limit",DISTINCT:"Distinct",UNION:"Union",CTE_SCAN:"CTEScan",CTE_ANCHOR:"CTEAnchor",DEPENDENT_JOIN:"DependentJoin",MATERIALIZE:"Materialize",EMPTY:"Empty",TOP_N:"TopN",INDEX_SCAN:"IndexScan",WINDOW:"Window",EXCHANGE:"Exchange",PARTIAL_AGGREGATE:"PartialAggregate",FINAL_AGGREGATE:"FinalAggregate",MERGE_EXCHANGE:"MergeExchange",EXCHANGE_RECEIVE:"ExchangeReceive",SINGLE_ROW:"SingleRow"},F={INNER:"INNER",LEFT:"LEFT",RIGHT:"RIGHT",FULL:"FULL",SEMI:"SEMI",ANTI:"ANTI",MARK:"MARK",SINGLE:"SINGLE",CROSS:"CROSS"},Pn={HASH:"HASH",MERGE:"MERGE",NESTED_LOOP:"NESTED_LOOP",STREAM:"STREAM",UNGROUPED:"UNGROUPED",PERFECT_HASH:"PERFECT_HASH"},hT={ASC:"ASC",DESC:"DESC"}}});function qa(e){return Math.ceil(e/32)}c(qa,"bitmapWordCount");function PE(e){return new Uint32Array(qa(e))}c(PE,"createBitmap");function ic(e,t){e[t>>>5]|=1<<(t&31)}c(ic,"setBit");function eb(e,t){e[t>>>5]&=~(1<<(t&31))}c(eb,"clearBit");function sc(e,t){return(e[t>>>5]&1<<(t&31))!==0}c(sc,"testBit");function m6(e,t,s){for(let n=t;n<s;n++)ic(e,n)}c(m6,"setBitRange");var DE,uc=tt({"src/utils/bitmap.js"(){DE=new Uint8Array(256);for(let e=0;e<256;e++)DE[e]=DE[e>>1]+(e&1)}}),E1,FE,br,sb=tt({"src/storage/dictionary-column.js"(){rs(),uc(),fc(),E1=2048,FE=65535,br=class pT{static{c(this,"_DictionaryColumn")}constructor(t=E1,s=ja){this.dataType=L.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,qa(t)),this.hasNulls=!1}static fromParts({indices:t,reverseDict:s,nullBitmap:n,length:r,hasNulls:i,allocator:o}){let a=Object.create(pT.prototype);return a.dataType=L.VARCHAR,a.capacity=r,a.length=r,a.allocator=o||ja,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&&!sc(this.nullBitmap,t))return null;let s=this.indices[t];return this.reverseDict[s]}set(t,s){if(s==null){this._setNull(t);return}ic(this.nullBitmap,t);let n=this.dictionary.get(s);if(n===void 0){if(n=this.reverseDict.length,n>FE)throw new Error(`Dictionary capacity exceeded ${FE} values per chunk`);this.dictionary.set(s,n),this.reverseDict.push(s)}this.indices[t]=n,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&&!sc(this.nullBitmap,t)}slice(t,s){let n=s-t,r=new pT(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++)sc(this.nullBitmap,t+o)&&ic(r.nullBitmap,o);return r.length=n,r.hasNulls=this.hasNulls,r}_setNull(t){this.hasNulls=!0,eb(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,qa(t));n.set(this.nullBitmap),this.nullBitmap=n,this.capacity=t}}}}),ms,It,Te=tt({"src/storage/chunk.js"(){Xe(),sb(),rs(),ms=2048,It=class Ow{static{c(this,"_DataChunk")}constructor(t,s=0){this.columns=t,this.size=s,this.selectionVector=null,this._cachedColumnCount=t.length}static fromSchema(t,s=ms){let n=t.map(({dataType:r})=>r===L.VARCHAR?new br(s):new se(r,s));return new Ow(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 Ow(s,this.size);return n.selectionVector=this.selectionVector,n}flatten(){if(!this.selectionVector)return this;let t=this.columns.map(s=>{let n=new se(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 Ow(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}}}});function g6(e,t){let s=nb?.[e];return s!==void 0?parseInt(s,10):t}c(g6,"getEnvInt");function y6(e,t){let s=nb?.[e];return s!==void 0?parseFloat(s):t}c(y6,"getEnvFloat");function _6(e,t){let s=nb?.[e];return s===void 0?t:s==="1"||s.toLowerCase()==="true"}c(_6,"getEnvFlag");function w6(){return globalThis.navigator?.hardwareConcurrency??qL}c(w6,"getCpuCount");var qL,nb,b6=tt({"src/runtime/platform.js"(){qL=1,nb=typeof process<"u"&&process.env?process.env:null}}),jL={};gs(jL,{Config:c(()=>_t,"Config")});var Kt,T1,dw,N1,_t,be=tt({"src/config.js"(){Te(),b6(),Kt=g6,T1=_6,dw=y6,N1=c(()=>{let e=Kt("QE_PARALLEL_WORKERS",0);return e>0?e:Math.max(1,w6()-1)},"resolveWorkerCount"),_t={memoryLimit:Kt("QE_MEMORY_LIMIT",2e5),hashJoinPartitions:Kt("QE_HASH_JOIN_PARTITIONS",16),flushBatchSize:Kt("QE_FLUSH_BATCH_SIZE",ms),bufferPoolPages:Kt("QE_BUFFER_POOL_PAGES",50),wasmMinChunkSize:Kt("QE_WASM_MIN_CHUNK",4096),sinkQueueCapacity:Kt("QE_SINK_QUEUE_CAPACITY",8),btreeOrder:Kt("QE_BTREE_ORDER",128),indexScanSelectivityThreshold:dw("QE_INDEX_SELECTIVITY_THRESHOLD",.3),dependentJoinConcurrency:Kt("QE_DEPENDENT_JOIN_CONCURRENCY",1),parallelWorkers:N1(),parallelThreshold:Kt("QE_PARALLEL_THRESHOLD",1e4),parallelAggThreshold:Kt("QE_PARALLEL_AGG_THRESHOLD",5e4),aggMorselRows:Kt("QE_AGG_MORSEL_ROWS",16384),sabColumns:T1("QE_SAB_COLUMNS",!1),sabArenaSegmentBytes:Kt("QE_SAB_ARENA_SEGMENT_BYTES",1<<20),parallelAggMemoryBytes:Kt("QE_PARALLEL_AGG_MEMORY_BYTES",1<<28),parallelCombineMinGroups:Kt("QE_PARALLEL_COMBINE_MIN_GROUPS",8192),aggSpillGroups:Kt("QE_AGG_SPILL_GROUPS",1<<17),vectorGroupRange:Kt("QE_VECTOR_GROUP_RANGE",1<<21),parallelJoinThreshold:Kt("QE_PARALLEL_JOIN_THRESHOLD",5e4),transportMaxBuffers:Kt("QE_TRANSPORT_MAX_BUFFERS",64),aggRadixMultiplier:Kt("QE_AGG_RADIX_MULTIPLIER",2),regionSize:Kt("QE_WASM_REGION_SIZE",16*1024*1024),morselSize:Kt("QE_MORSEL_SIZE",262144),clusterPort:Kt("QE_CLUSTER_PORT",9400),coordinatorSchemaSampleRows:Kt("QE_COORD_SCHEMA_SAMPLE_ROWS",1e3),heartbeatIntervalMs:Kt("QE_HEARTBEAT_INTERVAL",3e3),heartbeatTimeoutMs:Kt("QE_HEARTBEAT_TIMEOUT",1e4),defaultPartitionCount:Kt("QE_PARTITION_COUNT",16),broadcastThreshold:Kt("QE_BROADCAST_THRESHOLD",1e4),exchangeBatchSize:Kt("QE_EXCHANGE_BATCH_SIZE",4096),exchangeBufferCapacity:Kt("QE_EXCHANGE_BUFFER_CAPACITY",8),fragmentRetryLimit:Kt("QE_FRAGMENT_RETRY_LIMIT",3),coordinatorTimeoutMs:Kt("QE_COORDINATOR_TIMEOUT",3e5),codecCompression:Kt("QE_CODEC_COMPRESSION",0),distributedWorkers:Kt("QE_DISTRIBUTED_WORKERS",0),phiAccrualWindowSize:Kt("QE_PHI_WINDOW_SIZE",100),phiAccrualThreshold:dw("QE_PHI_THRESHOLD",8),networkCostPerByte:dw("QE_NETWORK_COST_PER_BYTE",.001)}}});function S6(e=0){return _t.sabColumns?e>0?new _d(e):new _d:ja}c(S6,"columnAllocator");function Zl(e){return!!e&&e.buffer instanceof SharedArrayBuffer}c(Zl,"isSharedView");var UE,R1,ja,_d,fc=tt({"src/storage/sab-arena.js"(){be(),UE=8,R1=class{static{c(this,"HeapAllocator")}get shared(){return!1}acquire(e,t){return new e(t)}},ja=new R1,_d=class{static{c(this,"SabArena")}constructor(e=_t.sabArenaSegmentBytes){this.nextSegmentBytes=Math.max(UE,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,UE),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}}}}),v1,k1,se,Xe=tt({"src/storage/column.js"(){rs(),uc(),fc(),v1=2048,k1=4096,se=class dT{static{c(this,"_Column")}constructor(t,s=v1,n=ja){this.dataType=t,this.capacity=s,this.length=0,this.allocator=n,ui(t)?this.data=n.acquire(Uf(t),s):(this.offsets=n.acquire(Uint32Array,s+1),this.stringBytes=n.acquire(Uint8Array,k1),this.stringBytesUsed=0),this.nullBitmap=n.acquire(Uint32Array,qa(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(dT.prototype);return f.dataType=t,f.capacity=a,f.length=a,f.allocator=u||ja,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&&!sc(this.nullBitmap,t))return null;if(this.dataType===L.VARCHAR)return this._getString(t);let s=this.data[t];return this.dataType===L.BOOLEAN?s!==0:s}set(t,s){if(s==null){this._setNull(t);return}ic(this.nullBitmap,t),this.dataType===L.VARCHAR?this._setString(t,s):this.dataType===L.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&&!sc(this.nullBitmap,t)}slice(t,s){let n=s-t,r=new dT(this.dataType,n,this.allocator);if(this.dataType===L.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++)sc(this.nullBitmap,t+i)&&ic(r.nullBitmap,i)}return r.length=n,r.hasNulls=this.hasNulls,r}_setNull(t){this.hasNulls=!0,eb(this.nullBitmap,t),this.dataType===L.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(ui(this.dataType)){let n=this.allocator.acquire(Uf(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,qa(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}}}}),Me,Ge=tt({"src/optimizer/pass.js"(){Me=class{static{c(this,"OptimizationPass")}get name(){throw new Error("Subclass must implement name")}apply(e,t){throw new Error("Subclass must implement apply()")}}}}),qe,Qe=tt({"src/planner/plan-visitor.js"(){$t(),qe=class{static{c(this,"PlanRewriter")}rewrite(e){let t=`rewrite${e.type}`;return typeof this[t]=="function"?this[t](e):this.rewriteDefault(e)}rewriteDefault(e){return this.rewriteChildren(e)}rewriteChildren(e){let t=ds(e);if(t.length===0)return e;let s=t.map(r=>this.rewrite(r));return s.some((r,i)=>r!==t[i])?Fn(e,s):e}}}});function qt(e,t){if(!e)return()=>null;switch(e.kind){case v.COLUMN_REF:{let s=E6(e,t);return(n,r)=>n.columns[s]?.get(r)??null}case v.LITERAL:return()=>e.value;case v.BINARY:{let s=qt(e.left,t),n=qt(e.right,t);return x6(e.op,s,n)}case v.UNARY:{let s=qt(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 v.BETWEEN:{let s=qt(e.expr,t),n=qt(e.low,t),r=qt(e.high,t);return(i,o)=>{let a=s(i,o);if(a==null)return null;let l=n(i,o),u=r(i,o),f=l==null?null:a>=l,h=u==null?null:a<=u,p;return f===!1||h===!1?p=!1:f===null||h===null?p=null:p=!0,e.negated?p===null?null:!p:p}}case v.IN_LIST:{let s=qt(e.expr,t);if(Array.isArray(e.list)){let n=c((o,a)=>{if(o==null)return null;let l=a(o);return l?!0:l===null?null:!1},"test");if(e.list.every(o=>o.kind===v.LITERAL)){let o=e.list.some(u=>u.value===null||u.value===void 0),a=new Set(e.list.filter(u=>u.value!=null).map(u=>I1(u.value))),l=c(u=>a.has(I1(u))?!0:o?null:!1,"has2");return(u,f)=>{let h=n(s(u,f),l);return e.negated?h===null?null:!h:h}}let r=e.list.map(o=>qt(o,t)),i=c((o,a,l)=>{let u=!1;for(let f of r){let h=f(a,l);if(h==null){u=!0;continue}if(h==o)return!0}return u?null:!1},"has");return(o,a)=>{let l=s(o,a);if(l==null)return null;let u=i(l,o,a),f=u?!0:u===null?null:!1;return e.negated?f===null?null:!f:f}}return()=>!0}case v.LIKE:{let s=qt(e.expr,t),n=qt(e.pattern,t),r=new Map,i=[];return(o,a)=>{let l=s(o,a),u=n(o,a);if(l==null||u===null||u===void 0)return null;let f=String(u),h=r.get(f);h||(h=T6(f),r.size>=KL&&r.delete(i.shift()),r.set(f,h),i.push(f));let p=h.test(String(l));return e.negated?!p:p}}case v.IS_NULL:{let s=qt(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 v.CASE:{let s=e.whenClauses.map(r=>({cond:qt(r.condition,t),result:qt(r.result,t)})),n=e.elseExpr?qt(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 v.CAST:{let s=qt(e.expr,t);return(n,r)=>R6(s(n,r),e.targetType)}case v.EXTRACT:{let s=qt(e.source,t),n=e.source?.dataType||e.source?.resultType;return(r,i)=>{let o=s(r,i);if(o===null)return null;if(n===L.TIMESTAMP||typeof o=="bigint"||o>1e5){let l=e6(typeof o=="bigint"?Number(o):o);switch(e.field){case"YEAR":return l.year;case"MONTH":return l.month;case"DAY":return l.day;case"HOUR":return l.hour;case"MINUTE":return l.minute;case"SECOND":return l.second;default:return null}}let a=OL(o);switch(e.field){case"YEAR":return a.year;case"MONTH":return a.month;case"DAY":return a.day;default:return null}}}case v.FUNCTION:{let s=e.args.map(n=>qt(n,t));return A6(e.name,s)}case v.AGGREGATE:{let s=WL(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?qt(e.args[0],t):()=>null}case v.INTERVAL:return()=>({value:e.value,unit:e.unit,_isInterval:!0});case v.WINDOW:{let s=N6(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(qt,"compileExpression");function $E(e,t,s){if(s==="DAY")return e+t;let n=OL(e);if(s==="YEAR")return gd(n.year+t,n.month,Math.min(n.day,C1(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 gd(i,r,Math.min(n.day,C1(i,r)))}return e+t}c($E,"addInterval");function C1(e,t){return new Date(e,t,0).getDate()}c(C1,"daysInMonth");function cn(e){return typeof e=="bigint"?Number(e):e}c(cn,"toNum");function I1(e){return typeof e=="bigint"?Number(e):e}c(I1,"normalizeComparable");function Xp(e,t,s){return s(cn(e),cn(t))}c(Xp,"numOp");function x6(e,t,s){switch(e){case"=":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:cn(i)==cn(o)};case"<>":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:cn(i)!=cn(o)};case"<":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:cn(i)<cn(o)};case">":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:cn(i)>cn(o)};case"<=":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:cn(i)<=cn(o)};case">=":return(n,r)=>{let i=t(n,r),o=s(n,r);return i==null||o==null?null:cn(i)>=cn(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?$E(cn(i),o.value,o.unit):i?._isInterval?$E(cn(o),i.value,i.unit):Xp(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?$E(cn(i),-o.value,o.unit):Xp(i,o,(a,l)=>a-l)};case"*":return(n,r)=>{let i=t(n,r),o=s(n,r);return i!==null&&o!==null?Xp(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?Xp(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?Xp(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(x6,"compileBinaryOp");function A6(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(A6,"compileFunction");function E6(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(E6,"resolveColumnIndex");function T6(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(T6,"likeToRegex");function WL(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=>r.kind===v.COLUMN_REF?`${r.tableAlias}.${r.columnName}`.toUpperCase():JSON.stringify(r).slice(0,30)).join(",");return`__AGG__${t}${s}(${n})`}c(WL,"aggExprKey");function N6(e){let t=e.name?.toUpperCase()||"WIN",s=(e.args||[]).map(r=>r.kind===v.COLUMN_REF?`${r.tableAlias}.${r.columnName}`.toUpperCase():JSON.stringify(r).slice(0,30)).join(","),n=(e.partitionBy||[]).map(r=>r.kind===v.COLUMN_REF?`${r.tableAlias}.${r.columnName}`.toUpperCase():"").join(",");return`__WIN__${t}(${s})[${n}]`}c(N6,"windowExprKey");function R6(e,t){if(e===null)return null;switch(t){case L.INT32:return parseInt(e,10)|0;case L.INT64:return BigInt(parseInt(e,10));case L.FLOAT64:return parseFloat(e);case L.VARCHAR:return String(typeof e=="bigint"?Number(e):e);case L.BOOLEAN:return!!e;case L.TIMESTAMP:return typeof e=="string"?new Date(e).getTime():Number(e);case L.DATE:{if(typeof e=="string"){let[s,n,r]=e.split("-").map(Number);return gd(s,n,r)}return e}default:return e}}c(R6,"castValue");var KL,wd=tt({"src/execution/expression-eval.js"(){Ht(),rs(),KL=256}});function v6(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(v6,"intersectSorted");function k6(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(k6,"unionSorted");var zE,HT,HL=tt({"src/execution/operators/filter.js"(){Te(),Ht(),rs(),zE={"=":"filterEq","<":"filterLt",">":"filterGt","<=":"filterLe",">=":"filterGe"},HT=class{static{c(this,"FilterOperator")}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 It(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===v.COMPARISON||e.kind===v.BINARY){let t=this._analyzeLogical(e);return t||this._analyzeComparison(e)}return e.kind===v.BETWEEN?this._analyzeBetween(e):null}_analyzeComparison(e){let t=e.op;if(!zE[t])return null;let{columnRef:s,literal:n}=this._extractColumnAndLiteral(e);if(!s||n===null)return null;let r=s.dataType;if(!ui(r))return null;let i=zE[t];return this.parallelDispatch.canParallelize(i,r,1)?{type:"simple",operation:i,dataType:r,columnIndex:s.columnIndex,value:n}:null}_analyzeBetween(e){if(!e.expr||e.expr.kind!==v.COLUMN_REF)return null;let t=e.expr;if(!ui(t.dataType))return null;let s=this._extractLiteralValue(e.low),n=this._extractLiteralValue(e.high);return s===null||n===null?null:{type:"between",operation:"filterBetween",dataType:t.dataType,columnIndex:t.columnIndex,low:s,high:n}}_analyzeLogical(e){if(e.op!=="AND"&&e.op!=="OR")return null;let t=this._analyze(e.left),s=this._analyze(e.right);return!t||!s?null:{type:e.op==="AND"?"and":"or",left:t,right:s}}_extractColumnAndLiteral(e){let t=null,s=null;return e.left?.kind===v.COLUMN_REF&&e.right?.kind===v.LITERAL?(t=e.left,s=e.right.value):e.right?.kind===v.COLUMN_REF&&e.left?.kind===v.LITERAL&&(t=e.right,s=e.left.value),{columnRef:t,literal:s}}_extractLiteralValue(e){return!e||e.kind!==v.LITERAL?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 It(e.columns,0);let n=await this._executeParallel(e,t.right);if(!n||n.size===0)return new It(e.columns,0);let r=s.selectionVector,i=n.selectionVector;if(!r||!i)return null;let o=v6(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=k6(r,s.size,i,n.size);return this._applySelectionVector(e,o.data,o.count)}_getColumnData(e,t){return e.selectionVector||!t.data?null:t.data.subarray(0,t.length)}_applySelectionVector(e,t,s){if(s===0)return new It(e.columns,0);if(s===e.size&&!e.selectionVector)return e;let n=new It(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 It(e.columns,0);if(n===t)return e;let r=new It(e.columns,n);return n>64?r.setSelectionVector(s.subarray(0,n),n):r.setSelectionVector(s.slice(0,n),n),r}}}}),YL={};gs(YL,{WasmDispatch:c(()=>mT,"WasmDispatch"),globalDispatch:c(()=>at,"globalDispatch")});var mT,at,bd=tt({"src/wasm/dispatch.js"(){mT=class{static{c(this,"WasmDispatch")}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()]}},at=new mT}});function hd(e){return e?e.kind===v.COLUMN_REF?JL.has(e.dataType):e.kind===v.LITERAL?typeof e.value=="number":e.kind===v.BINARY&&YT.has(e.op)?hd(e.left)&&hd(e.right):e.kind===v.UNARY&&e.op==="-"?hd(e.operand):!1:!1}c(hd,"isVectorizableExpr");function C6(e,t,s){let n=t.selectionVector;if(e.dataType==="DECIMAL"){let r=new Float64Array(s);if(n)for(let i=0;i<s;i++)r[i]=Number(e.data[n[i]])/gT;else for(let i=0;i<s;i++)r[i]=Number(e.data[i])/gT;return r}if(XL.has(e.dataType)){if(n){let r=new Int32Array(s);for(let i=0;i<s;i++)r[i]=e.data[n[i]];return r}return e.data.subarray(0,s)}if(e.dataType==="FLOAT64"){if(n){let r=new Float64Array(s);for(let i=0;i<s;i++)r[i]=e.data[n[i]];return r}return e.data.subarray(0,s)}return null}c(C6,"extractColumnF64");async function pd(e,t,s,n){if(at.kernels.size===0)return null;if(e.kind===v.COLUMN_REF){let r=I6(e,s);if(r<0)return null;let i=t.columns[r];if(!i||!i.data)return null;let o=C6(i,t,n);if(!o)return null;if(o instanceof Int32Array){let a=at.lookup("widenI32ToF64","INT32");return a?await a(o):null}return o}if(e.kind===v.LITERAL)return Number(e.value);if(e.kind===v.UNARY&&e.op==="-"){let r=await pd(e.operand,t,s,n);if(r===null)return null;if(typeof r=="number")return-r;let i=at.lookup("negF64","FLOAT64");return i?await i(r):null}if(e.kind===v.BINARY&&YT.has(e.op)){let r=await pd(e.left,t,s,n);if(r===null)return null;let i=await pd(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=at.lookup(l,"FLOAT64");return u?await u(r,i):null}if(o&&!a){let l={"+":"scalarAddF64","-":"scalarSubF64","*":"scalarMulF64","/":"scalarDivF64"}[e.op],u=at.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=at.lookup(l,"FLOAT64");return u?await u(i,Number(r)):null}if(e.op==="-"){let l=at.lookup("scalarSubRevF64","FLOAT64");return l?await l(Number(r),i):null}if(e.op==="/"){let l=at.lookup("scalarDivRevF64","FLOAT64");return l?await l(Number(r),i):null}}}return null}c(pd,"evalVectorized");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>=0?e.columnIndex:-1}c(I6,"resolveColIndex");var JL,XL,YT,gT,QL=tt({"src/execution/wasm-expr-eval.js"(){Ht(),bd(),be(),JL=new Set(["INT32","FLOAT64","DATE","DECIMAL"]),XL=new Set(["INT32","DATE"]),YT=new Set(["+","-","*","/"]),gT=100}});function O6(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(O6,"resolveColumnIndex2");function ZL(e,t,s,n){if(!(!e||typeof e!="object")){if(e.kind===v.COLUMN_REF){let r=t.columns[O6(e,s)];r&&r.hasNulls&&n.push(r);return}for(let r of["left","right","operand","expr"])e[r]&&ZL(e[r],t,s,n)}}c(ZL,"collectNullableColumns");function B6(e,t,s,n){let r=[];if(ZL(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?(eb(e.nullBitmap,a),o=!0):ic(e.nullBitmap,a)}e.hasNulls=o}c(B6,"applyNullMask");async function M6(e,t,s){if(!hd(e)||t.size<_t.wasmMinChunkSize)return null;let n=await pd(e,t,s,t.size);if(n===null||typeof n=="number")return null;let r=new se("FLOAT64",t.size);return r.data.set(n),r.length=t.size,B6(r,e,t,s),r}c(M6,"tryWasmProject");var JT,tP=tt({"src/execution/operators/projection.js"(){Xe(),Te(),Ht(),QL(),be(),uc(),JT=class{static{c(this,"ProjectionOperator")}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===v.COLUMN_REF?this._resolveColIdx(i):-1)}async init(){}async process(e){if(e.size===0)return new It([],0);let t=[],s=!!e.selectionVector;for(let n=0;n<this.evaluators.length;n++){let r=this.colRefIndices[n],i=this.resultTypes?this.resultTypes[n]:this.expressions[n]?.dataType||this.expressions[n]?.resultType||"VARCHAR";if(r>=0&&!s){let l=e.columns[r];if(l){t.push(l);continue}}if(this.expressions[n]&&!this.parallelDispatch){let l=await M6(this.expressions[n],e,this.columnMapping);if(l){t.push(l);continue}}let o=this.evaluators[n],a=new se(i,e.size||1);for(let l=0;l<e.size;l++){let u=e.activeRowIndex(l),f=o(e,u);a.set(l,typeof f=="bigint"&&i!=="INT64"?Number(f):f)}a.length=e.size,t.push(a)}return new It(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 eP(e){if(typeof e=="number"){yT[0]=e;let t=_T[0]^Math.imul(_T[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"?eP(Number(BigInt.asIntN(53,e))):typeof e=="boolean"&&e?1:0}c(eP,"hashValue");var yT,_T,sP=tt({"src/utils/hash.js"(){yT=new Float64Array(1),_T=new Uint32Array(yT.buffer)}});function L6(e){return e==null?0:eP(e)}c(L6,"hashGroupKey");function Pf(e,t=!1){if(t&&e.toUpperCase()==="COUNT")return()=>new cP;switch(e.toUpperCase()){case"SUM":return()=>new nP;case"COUNT":return()=>new rP;case"COUNT_STAR":return()=>new iP;case"AVG":return()=>new wT;case"AVG_PARTIAL":return()=>new wT;case"AVG_FINAL":return()=>new oP;case"MIN":return()=>new aP;case"MAX":return()=>new lP;default:throw new Error(`Unknown aggregate: ${e}`)}}c(Pf,"getAccumulatorFactory");var Sd,mw,nP,rP,iP,wT,oP,aP,lP,cP,uP=tt({"src/execution/operators/hash-aggregate.js"(){Xe(),Te(),rs(),bd(),sP(),Sd=class{static{c(this,"HashAggregateOperator")}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&&at&&at.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=mw;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 se(a.resultType,1),u=a.createAccumulator();return l.set(0,u.result()),l.length=1,l});return[new It(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+=ms){let l=Math.min(o+ms,i.length)-o,u=new Array(n);for(let f=0;f<t;f++)u[f]=new se(this.groupByTypes[f]||L.VARCHAR,l);for(let f=0;f<s;f++)u[t+f]=new se(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 It(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(at.has("sumF64","FLOAT64"))return{kernelKey:"sumF64",dataType:"FLOAT64",kind:"SUM"};if(at.has("sumI32","INT32"))return{kernelKey:"sumI32",dataType:"INT32",kind:"SUM"}}if(t==="MIN"){if(e.resultType==="FLOAT64"&&at.has("minF64","FLOAT64"))return{kernelKey:"minF64",dataType:"FLOAT64",kind:"MIN"};if(e.resultType==="INT32"&&at.has("minI32","INT32"))return{kernelKey:"minI32",dataType:"INT32",kind:"MIN"}}if(t==="MAX"){if(e.resultType==="FLOAT64"&&at.has("maxF64","FLOAT64"))return{kernelKey:"maxF64",dataType:"FLOAT64",kind:"MAX"};if(e.resultType==="INT32"&&at.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"&&at.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=at.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=at.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(mw);n||(n={groupValues:[],accumulators:this.aggregateDefs.map(r=>r.createAccumulator())},this.groups.set(mw,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}},mw="__ALL__",nP=class{static{c(this,"SumAccumulator")}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)}},rP=class{static{c(this,"CountAccumulator")}constructor(){this.count=0}add(e){e!=null&&this.count++}result(){return this.count}exportState(){return this.count}mergeState(e){this.count+=e}},iP=class{static{c(this,"CountStarAccumulator")}constructor(){this.count=0}add(){this.count++}result(){return this.count}exportState(){return this.count}mergeState(e){this.count+=e}},wT=class{static{c(this,"AvgAccumulator")}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}},oP=class{static{c(this,"AvgFinalAccumulator")}constructor(){this.sum=0,this.count=0}add(e){if(!e)return;let t=e[0],s=e[1];t!=null&&s!==null&&s!==void 0&&(this.sum+=Number(t),this.count+=Number(s))}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}},aP=class{static{c(this,"MinAccumulator")}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)}},lP=class{static{c(this,"MaxAccumulator")}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)}},cP=class{static{c(this,"CountDistinctAccumulator")}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 rb(e){return e===L.DECIMAL||e===L.INT64?L.FLOAT64:e}c(rb,"normalizeExecType");function fP(e){let t=(e.name||"").toUpperCase();return t==="COUNT"||t==="COUNT_STAR"?L.INT32:L.FLOAT64}c(fP,"normalizeAggResultType");function dd(e){if(!e||typeof e!="object")return String(e);switch(e.kind){case v.COLUMN_REF:return`COL:${e.tableAlias||""}.${e.columnName}`;case v.LITERAL:return`LIT:${String(e.value)}`;case v.BINARY:return`BIN:${e.op}:${dd(e.left)}:${dd(e.right)}`;case v.UNARY:return`UNARY:${e.op}:${dd(e.operand)}`;case v.CASE:return`CASE:${JSON.stringify(e)}`;default:return JSON.stringify(e)}}c(dd,"expressionCacheKey");function Io(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(Io,"schemaMappingOf");function XT(e){return e.map((t,s)=>({name:t?.outputName||t?.alias||t?.name||t?.columnName||`col${s}`,dataType:rb(t?.dataType||t?.resultType||L.VARCHAR),tableAlias:""}))}c(XT,"projectionSchemaOf");function Va(e,t=[]){if(!e||typeof e!="object")return t;if(Array.isArray(e)){for(let s of e)Va(s,t);return t}if(e.kind===v.COLUMN_REF)return t.push(e),t;for(let s of Object.values(e))Va(s,t);return t}c(Va,"collectColumnRefs");function QT(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(QT,"resolveRef");function Vw(e,t){for(let s of Va(e))if(QT(s,t)===void 0)return!1;return!0}c(Vw,"exprResolvable");function hP(e,t){let s=new Map;for(let n of e){if(!n.args||n.args.length===0)continue;let r=dd(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?qt(n.args[0],t):()=>1,o=r?dd(n.args[0]):null,a;if(r&&n.args[0].kind===v.COLUMN_REF){let l=QT(n.args[0],t);l!==void 0&&(a=l)}return{name:n.name,valueKey:o&&s.get(o)>1?o:null,resultType:fP(n),createAccumulator:Pf(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(hP,"buildAggregateDefs");function pP(e){let t=[],s=e;for(;s;)if(s.type===C.FILTER)t.push({kind:oc.FILTER,condition:s.condition}),s=s.children[0];else if(s.type===C.PROJECT)t.push({kind:oc.PROJECT,expressions:s.expressions}),s=s.children[0];else return s.type===C.SCAN?(t.reverse(),{table:s.table,alias:s.alias||s.table,scanColumns:s.columns,stages:t}):null;return null}c(pP,"extractStageChain");function P6(e){return pP(e.children[0])}c(P6,"extractAggregateFragment");function Gw(e,t){let s=e;for(let n of t)n.kind===oc.PROJECT&&(s=XT(n.expressions));return s}c(Gw,"stagedSchemaOf");function bT(e,t){let s=e,n=Io(s);for(let r of t)if(r.kind===oc.FILTER){if(!Vw(r.condition,n))return!1}else{if(!Vw(r.expressions,n))return!1;s=XT(r.expressions),n=Io(s)}return!0}c(bT,"stagesResolvable");function D6(e,t){let s=e,n=Io(s),r=[];for(let i of t)if(i.kind===oc.FILTER){let o=qt(i.condition,n);r.push(new HT(i.condition,o,n,null))}else{let o=i.expressions.map(l=>qt(l,n)),a=i.expressions.map(l=>rb(l?.dataType||l?.resultType||L.VARCHAR));r.push(new JT(i.expressions,o,a,n,null)),s=XT(i.expressions),n=Io(s)}return{operators:r,schema:s,mapping:n}}c(D6,"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===oc.FILTER)Va(p.condition,r);else{Va(p.expressions,r),i=!0;break}if(!i){Va(t.groupBy||[],r);for(let p of t.aggregates)Va(p.args||[],r)}let o=Io(n),a=new Set;for(let p of r){let d=QT(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(!U6(f))return null;let h=Math.ceil(l.length/8);for(let p of l)h+=$f(n[p].dataType)||Uint16Array.BYTES_PER_ELEMENT;return{spec:f,columnIndexes:l,estimatedRowBytes:h}}c(F6,"buildFragmentSpec");function U6(e){if(!bT(e.baseSchema,e.stages))return!1;let t=Io(Gw(e.baseSchema,e.stages));for(let s of e.groupBy)if(!Vw(s,t))return!1;for(let s of e.aggregates)if(!Vw(s.args,t))return!1;return!0}c(U6,"validateFragmentSpec");function $6(e){let{operators:t,mapping:s}=D6(e.baseSchema,e.stages),n=e.groupBy.map(o=>qt(o,s)),r=e.groupBy.map(o=>rb(o?.dataType||o?.resultType||L.VARCHAR)),i=hP(e.aggregates,s);return{operators:t,aggregate:new Sd(n,r,i)}}c($6,"instantiateFragment");function gw(e){return $6(e).aggregate}c(gw,"instantiateAggregate");function O1(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(O1,"execResolvedIndex");function VE(e,t,s){for(let n of Va(e)){let r=O1(n,t),i=O1(n,s);if(r==null||r!==i)return!1}return!0}c(VE,"refsResolveIdentically");function z6(e){return e.map(t=>({name:t.name,dataType:t.dataType,tableAlias:t.tableAlias||""}))}c(z6,"plainSchemaOf");function V6(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(V6,"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(!bT(e.baseSchema,e.stages)||!bT(t.baseSchema,t.stages))return null;let h=Io(Gw(e.baseSchema,e.stages)),p=Io(Gw(t.baseSchema,t.stages)),d=Io([...e.schema,...t.schema]);return!VE(s,l,h)||!VE(n,u,p)||r&&!VE(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 oc,ib=tt({"src/execution/fragment-spec.js"(){$t(),Ht(),rs(),wd(),HL(),tP(),uP(),oc={FILTER:"filter",PROJECT:"project"}}}),ob,ZT=tt({"src/execution/result-sink.js"(){be(),ob=class{static{c(this,"ResultSink")}constructor(e=!1){this._streaming=e,this._capacity=_t.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}}}}),tN={};gs(tN,{CancelToken:c(()=>eN,"CancelToken"),PipelineGraph:c(()=>zf,"PipelineGraph")});var zf,eN,xd=tt({"src/execution/pipeline.js"(){zf=class{static{c(this,"PipelineGraph")}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}},eN=class{static{c(this,"CancelToken")}constructor(){this.cancelled=!1}cancel(){this.cancelled=!0}get isCancelled(){return this.cancelled}}}}),sN={};gs(sN,{TaskScheduler:c(()=>Vf,"TaskScheduler")});var Vf,ab=tt({"src/execution/scheduler.js"(){Vf=class{static{c(this,"TaskScheduler")}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}}}}),dP,mP,gP,nN=tt({"src/storage/storage-constants.js"(){dP=".qdat",mP=".spill",gP="mem://query_engine"}}),yP,q6=tt({"src/storage/temp-space/memory-temp-space.js"(){nN(),yP=class{static{c(this,"MemoryTempSpace")}constructor(e={}){this.rootDir=e.baseDir||gP,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()}}}}),_P,j6=tt({"src/storage/page-store/memory-page-store.js"(){_P=class{static{c(this,"MemoryPageStore")}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 Qp(e,t,s,n){return new Uint8Array(s.buffer,s.byteOffset,n).set(e.subarray(t,t+n)),t+n}c(Qp,"copyBytesInto");function W6(e){let t=6;for(let s of e.columns){let n=s instanceof br;if(t+=7,s.hasNulls){let r=Math.ceil(s.length/32);t+=4+r*4}if(n){t+=s.length*2,t+=4;for(let r=0;r<s.reverseDict.length;r++)t+=4+Buffer.byteLength(s.reverseDict[r],"utf8")}else ui(s.dataType)?t+=s.length*$f(s.dataType):t+=4+(s.length+1)*4+s.stringBytesUsed}return t}c(W6,"computeSize");var B1,GE,qE,M1,Df,wP=tt({"src/storage/serializer.js"(){Xe(),sb(),Te(),rs(),fc(),uc(),B1=0,GE=1,qE={[L.BOOLEAN]:0,[L.INT32]:1,[L.INT64]:2,[L.FLOAT64]:3,[L.DECIMAL]:4,[L.VARCHAR]:5,[L.DATE]:6,[L.TIMESTAMP]:7},M1=Object.fromEntries(Object.entries(qE).map(([e,t])=>[t,e])),Df=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 br;if(s.writeUInt8(i?GE:B1,n),n+=1,s.writeUInt8(qE[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.length*2;Buffer.from(r.indices.buffer,r.indices.byteOffset,o).copy(s,n),n+=o;let a=r.reverseDict.length;s.writeUInt32LE(a,n),n+=4;for(let l=0;l<a;l++){let u=Buffer.from(r.reverseDict[l],"utf8");s.writeUInt32LE(u.length,n),n+=4,u.copy(s,n),n+=u.length}}else if(ui(r.dataType)){let o=$f(r.dataType),a=r.length*o;Buffer.from(r.data.buffer,r.data.byteOffset,a).copy(s,n),n+=a}else{s.writeUInt32LE(r.stringBytesUsed,n),n+=4;let o=(r.length+1)*4;Buffer.from(r.offsets.buffer,r.offsets.byteOffset,o).copy(s,n),n+=o,Buffer.from(r.stringBytes.buffer,r.stringBytes.byteOffset,r.stringBytesUsed).copy(s,n),n+=r.stringBytesUsed}}return s}static deserialize(e,t=ja){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=M1[l],p;if(f){let d=e.readUInt32LE(s);s+=4,p=t.acquire(Uint32Array,d),s=Qp(e,s,p,d*4)}else p=t.acquire(Uint32Array,qa(Math.max(u,1)));if(a===GE){let d=t.acquire(Uint16Array,u);s=Qp(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(br.fromParts({indices:d,reverseDict:g,nullBitmap:p,length:u,hasNulls:f,allocator:t}))}else if(ui(h)){let d=t.acquire(Uf(h),u);s=Qp(e,s,d,u*$f(h)),i.push(se.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=Qp(e,s,m,(u+1)*4);let g=t.acquire(Uint8Array,d);s=Qp(e,s,g,d),i.push(se.fromParts({dataType:h,offsets:m,stringBytes:g,stringBytesUsed:d,nullBitmap:p,length:u,hasNulls:f,allocator:t}))}}return new It(i,n)}}}}),jE,rN,bP=tt({"src/storage/spill-manager/spill-manager.js"(){wP(),jE=4,rN=class{static{c(this,"SpillManager")}constructor(e){this.storage=e}async appendChunk(e,t){if(!t||t.size===0)return;let s=Df.serialize(t),n=Buffer.allocUnsafe(jE);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+=jE;let r=t.subarray(s,s+n);yield Df.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)}}}}),SP,K6=tt({"src/storage/spill-manager/memory-storage.js"(){SP=class{static{c(this,"MemoryStorage")}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()}}}}),xP,H6=tt({"src/storage/backend/memory-storage-backend.js"(){q6(),j6(),bP(),K6(),xP=class{static{c(this,"MemoryStorageBackend")}constructor(e={}){this.options=e}createTempSpace(){return new yP(this.options)}createPageStore(){return new _P}createSpillManager(){return new rN(new SP)}}}}),AP,Y6=tt({"src/execution/operators/scan.js"(){AP=class{static{c(this,"ScanOperator")}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()}}}}),EP,J6=tt({"src/execution/operators/index-scan.js"(){Xe(),Te(),be(),EP=class{static{c(this,"IndexScanOperator")}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>=_t.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 se(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 It(n,e.length)}}}});async function X6(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 AP(s,r);l.setSource(u,async function*(){for await(let p of h.scan()){if(f.cancelToken?.isCancelled)break;await f.consume(p),yield p}f.finalize&&await f.finalize()})},"register")}}c(X6,"buildScan");async function Q6(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 EP(n,s,t.scanType,t.scanKey,t.scanLow,t.scanHigh,t.lowInc,t.highInc,i);u.setSource(f,async function*(){for await(let d of p.scan()){if(h.cancelToken?.isCancelled)break;await h.consume(d),yield d}h.finalize&&await h.finalize()})},"register")}}c(Q6,"buildIndexScan");async function Z6(e,t){return{schema:[],columnMapping:new Map,register:c((s,n,r)=>{s.setSource(n,async function*(){let i=new It([],1);await r.consume(i),yield i,r.finalize&&await r.finalize()})},"register")}}c(Z6,"buildSingleRow");async function tq(e,t){let s=await e.buildPipeline(t.children[0]);return{schema:s.schema,columnMapping:s.columnMapping,register:c((n,r,i)=>{n.setSource(r,async function*(){})},"register")}}c(tq,"buildEmpty");var eq=tt({"src/execution/builders/source-builders.js"(){Y6(),J6(),Te()}}),TP,sq=tt({"src/utils/priority-queue.js"(){TP=class{static{c(this,"PriorityQueue")}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}}}}),iN,NP,nq=tt({"src/execution/operators/sort.js"(){Xe(),Te(),sq(),be(),iN=class{static{c(this,"SortOperator")}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>=_t.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 TP((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>=_t.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 It([],0);let t=e[0].row.length,s=new Array(t);for(let n=0;n<t;n++){let r=new se(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 It(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}},NP=class{static{c(this,"LimitOperator")}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 It(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)}}}}),RP,rq=tt({"src/execution/operators/distinct.js"(){Xe(),Te(),RP=class{static{c(this,"DistinctOperator")}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 It(e.columns,0);if(s===e.size)return e;let n=new It(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||[]}}}}),vP,iq=tt({"src/execution/operators/union.js"(){Xe(),Te(),vP=class{static{c(this,"UnionOperator")}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 It(e.columns,0);if(s===e.size)return e;let n=new It(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||[]}}}}),kP,oq=tt({"src/execution/operators/window.js"(){Te(),Xe(),kP=class{static{c(this,"WindowOperator")}constructor(e,t,s,n){this.windowExprs=e,this.childSchema=t,this.childColumnMapping=s,this.compileExpression=n}async execute(e){let t=[];for(let i of e)for(let o=0;o<i.size;o++){let a=[];for(let l=0;l<i.columns.length;l++)a.push(i.columns[l].get(i.activeRowIndex?i.activeRowIndex(o):o));t.push(a)}if(t.length===0)return[];let s=[];for(let i of this.windowExprs)s.push(this.computeWindow(i,t,e));let n=this.childSchema.length+this.windowExprs.length,r=[];for(let i=0;i<this.childSchema.length;i++){let o=new se(this.childSchema[i].dataType,t.length);for(let a=0;a<t.length;a++)o.set(a,t[a][i]);o.length=t.length,r.push(o)}for(let i=0;i<s.length;i++){let o=this.windowExprs[i].resultType||"INT64",a=new se(o==="INT64"?"FLOAT64":o,t.length);for(let l=0;l<t.length;l++)a.set(l,s[i][l]);a.length=t.length,r.push(a)}return[new It(r,t.length)]}computeWindow(e,t,s){let n=e.partitionBy.map(f=>this.compileExpression(f,this.childColumnMapping)),r=e.orderBy.map(f=>({eval:this.compileExpression(f.expr,this.childColumnMapping),direction:f.direction||"ASC"})),i=s.length>0?s[0]:null,o=c((f,h)=>{let p=0;for(let d of s){if(f<p+d.size)return h(d,f-p);p+=d.size}return null},"getVal"),a=this.partitionRows(t,n,s);if(r.length>0)for(let f of a)f.sort((h,p)=>{for(let d of r){let m=o(h,d.eval),g=o(p,d.eval),y=m==null,_=g==null;if(y&&_)continue;if(y)return 1;if(_)return-1;let w=this.compareValues(m,g);if(w!==0)return d.direction==="DESC"?-w:w}return 0});let l=new Array(t.length),u=e.name.toUpperCase();for(let f of a)switch(u){case"ROW_NUMBER":for(let h=0;h<f.length;h++)l[f[h]]=h+1;break;case"RANK":{let h=1;for(let p=0;p<f.length;p++)p>0&&!this.sameOrderKey(f[p],f[p-1],r,s)&&(h=p+1),l[f[p]]=h;break}case"DENSE_RANK":{let h=1;for(let p=0;p<f.length;p++)p>0&&!this.sameOrderKey(f[p],f[p-1],r,s)&&h++,l[f[p]]=h;break}case"LAG":{let h=e.args.length>0?this.compileExpression(e.args[0],this.childColumnMapping):null,p=e.args.length>1?e.args[1].value:1,d=e.args.length>2?e.args[2].value:null;for(let m=0;m<f.length;m++){let g=m-p;g>=0&&g<f.length?l[f[m]]=h?o(f[g],h):null:l[f[m]]=d}break}case"LEAD":{let h=e.args.length>0?this.compileExpression(e.args[0],this.childColumnMapping):null,p=e.args.length>1?e.args[1].value:1,d=e.args.length>2?e.args[2].value:null;for(let m=0;m<f.length;m++){let g=m+p;g>=0&&g<f.length?l[f[m]]=h?o(f[g],h):null:l[f[m]]=d}break}case"SUM":{let h=this.compileExpression(e.args[0],this.childColumnMapping);if(r.length===0){let p=0,d=0;for(let g=0;g<f.length;g++){let y=o(f[g],h);y!=null&&(p+=typeof y=="bigint"?Number(y):y,d++)}let m=d===0?null:p;for(let g=0;g<f.length;g++)l[f[g]]=m}else{let p=0,d=0;for(let m=0;m<f.length;m++){let g=o(f[m],h);g!=null&&(p+=typeof g=="bigint"?Number(g):g,d++),l[f[m]]=d===0?null:p}}break}case"AVG":{let h=this.compileExpression(e.args[0],this.childColumnMapping);if(r.length===0){let p=0,d=0;for(let g=0;g<f.length;g++){let y=o(f[g],h);y!=null&&(p+=typeof y=="bigint"?Number(y):y,d++)}let m=d===0?null:p/d;for(let g=0;g<f.length;g++)l[f[g]]=m}else{let p=0,d=0;for(let m=0;m<f.length;m++){let g=o(f[m],h);g!=null&&(p+=typeof g=="bigint"?Number(g):g,d++),l[f[m]]=d===0?null:p/d}}break}case"COUNT":case"COUNT_STAR":{let h=e.args.length>0?this.compileExpression(e.args[0],this.childColumnMapping):null;if(r.length===0){let p=0;for(let d=0;d<f.length;d++)h?o(f[d],h)!==null&&p++:p++;for(let d=0;d<f.length;d++)l[f[d]]=p}else{let p=0;for(let d=0;d<f.length;d++)h?o(f[d],h)!==null&&p++:p++,l[f[d]]=p}break}case"MIN":{let h=this.compileExpression(e.args[0],this.childColumnMapping);if(r.length===0){let p=null;for(let d=0;d<f.length;d++){let m=o(f[d],h);m!==null&&(p===null||m<p)&&(p=m)}for(let d=0;d<f.length;d++)l[f[d]]=p}else{let p=null;for(let d=0;d<f.length;d++){let m=o(f[d],h);m!==null&&(p===null||m<p)&&(p=m),l[f[d]]=p}}break}case"MAX":{let h=this.compileExpression(e.args[0],this.childColumnMapping);if(r.length===0){let p=null;for(let d=0;d<f.length;d++){let m=o(f[d],h);m!==null&&(p===null||m>p)&&(p=m)}for(let d=0;d<f.length;d++)l[f[d]]=p}else{let p=null;for(let d=0;d<f.length;d++){let m=o(f[d],h);m!==null&&(p===null||m>p)&&(p=m),l[f[d]]=p}}break}default:for(let h=0;h<f.length;h++)l[f[h]]=null}return l}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 qw(...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(qw,"combinedMappingOf");function ac(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(ac,"registerBufferedChild");var oN=tt({"src/execution/builders/builder-utils.js"(){}});async function aq(e,t){let s=await e.buildPipeline(t.children[0]),n=qt(t.condition,s.columnMapping),r=e.parallelDispatch;return{schema:s.schema,columnMapping:s.columnMapping,register:c((i,o,a)=>{let l=new HT(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(aq,"buildFilter");async function lq(e,t){let s=await e.buildPipeline(t.children[0]),n=t.expressions.map(a=>qt(a,s.columnMapping)),r=t.expressions.map(a=>e.normalizeExecType(a?.dataType||a?.resultType||"VARCHAR")),i=t.expressions.map((a,l)=>({name:a?.outputName||a?.alias||a?.name||a?.columnName||`col${l}`,dataType:e.normalizeExecType(a?.dataType||a?.resultType||"VARCHAR"),tableAlias:""})),o=e.buildSchemaMapping(i,"");return{schema:i,columnMapping:o,register:c((a,l,u)=>{let f=new JT(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(lq,"buildProject");async function cq(e,t){let s=await e.buildPipeline(t.children[0]),n=t.orderKeys.map(r=>({eval:qt(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 iN(n,t.limit,t.offset||0,e.storageBackend.createSpillManager(a)),u={async consume(h){await l.consume(h)},async finalize(){}},f=r.createPipeline(u);s.register(r,f,u),r.addDependency(i,f),r.setSource(i,async function*(){let h=await l.finalize();for(let p of h)await o.consume(p),yield p;o.finalize&&await o.finalize()})},"register")}}c(cq,"buildSort");async function uq(e,t){let s=await e.buildPipeline(t.children[0]),n=t.orderKeys.map(r=>({eval:qt(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 iN(n,t.count,t.offset||0,e.storageBackend.createSpillManager(a)),u={async consume(h){await l.consume(h)},async finalize(){}},f=r.createPipeline(u);s.register(r,f,u),r.addDependency(i,f),r.setSource(i,async function*(){let h=await l.finalize();for(let p of h)await o.consume(p),yield p;o.finalize&&await o.finalize()})},"register")}}c(uq,"buildTopN");async function fq(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 NP(n,r),u=new eN,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(fq,"buildLimit");async function hq(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 RP,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(hq,"buildDistinct");async function pq(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),r.setSource(i,async function*(){})}else{let a=new vP(!1),l={async consume(h){let p=await a.process(h);p&&p.size>0&&await o.consume(p)},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),r.setSource(i,async function*(){o.finalize&&await o.finalize()})}},"register")}}c(pq,"buildUnion");async function dq(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=mq(n[a]);i.set(l,s.schema.length+a)}return{schema:r,columnMapping:i,register:c((a,l,u)=>{let f=ac(a,l,s);a.setSource(l,async function*(){let p=await new kP(n,s.schema,s.columnMapping,qt).execute(f);for(let d of p)await u.consume(d),yield d;u.finalize&&await u.finalize()})},"register")}}c(dq,"buildWindow");function mq(e){let t=e.name?.toUpperCase()||"WIN",s=(e.args||[]).map(r=>r.kind===v.COLUMN_REF?`${r.tableAlias}.${r.columnName}`.toUpperCase():JSON.stringify(r).slice(0,30)).join(","),n=(e.partitionBy||[]).map(r=>r.kind===v.COLUMN_REF?`${r.tableAlias}.${r.columnName}`.toUpperCase():"").join(",");return`__WIN__${t}(${s})[${n}]`}c(mq,"windowExprKey2");var gq=tt({"src/execution/builders/pipeline-builders.js"(){wd(),HL(),tP(),nq(),rq(),iq(),oq(),xd(),Ht(),oN()}});function ST(e){return e?e.kind===v.BINARY&&e.op==="AND"?[...ST(e.left),...ST(e.right)]:[e]:[]}c(ST,"splitAnd");function Ga(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(Ga,"hasColumn");function nd(e,t,s){if(!e)return null;if(e.kind===v.BINARY&&e.op==="OR"){let n=nd(e.left,t,s),r=nd(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===v.BINARY&&e.op==="AND"){let n=nd(e.left,t,s);return n||nd(e.right,t,s)}if(e.kind===v.BINARY&&e.op==="="&&e.left?.kind===v.COLUMN_REF&&e.right?.kind===v.COLUMN_REF){if(Ga(t,e.left)&&Ga(s,e.right))return{buildKey:e.left,probeKey:e.right};if(Ga(t,e.right)&&Ga(s,e.left))return{buildKey:e.right,probeKey:e.left}}return null}c(nd,"findCommonEquiJoinKeys");function yq(e,t,s){if(!e)return{buildKeys:[],probeKeys:[],residualCondition:null};let n=[],r=[],i=[],o=ST(e);for(let l of o){let u=!1;l.kind===v.BINARY&&l.op==="="&&l.left?.kind===v.COLUMN_REF&&l.right?.kind===v.COLUMN_REF&&(Ga(t,l.left)&&Ga(s,l.right)?(n.push(l.left),r.push(l.right),u=!0):Ga(t,l.right)&&Ga(s,l.left)&&(n.push(l.right),r.push(l.left),u=!0)),u||i.push(l)}if(n.length===0){let l=nd(e,t,s);l&&l.buildKey?(n.push(l.buildKey),r.push(l.probeKey),i.length=0,i.push(e)):(n.push({kind:v.LITERAL,value:1}),r.push({kind:v.LITERAL,value:1}))}let a=null;return i.length>0&&(a=i.reduce((l,u)=>({kind:v.BINARY,op:"AND",left:l,right:u,resultType:"BOOLEAN"}))),{buildKeys:n,probeKeys:r,residualCondition:a}}c(yq,"extractJoinKeys");var _q=tt({"src/execution/join-utils.js"(){Ht()}});function L1(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(L1,"joinKeyOf");function wq(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(wq,"createCombinedRowAdapter");function CP(e,t,s){let{joinType:n,buildColCount:r,probeColCount:i,conditionEvaluator:o,hasNullKey:a,onMatched:l}=s,u=o?wq(r+i):null,f=[];for(let h of e){let{row:p,key:d}=h;if(d===null){(n===F.LEFT||n===F.FULL||n===F.ANTI||n===F.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===F.SEMI)break;if(n===F.ANTI)break;if(n===F.SINGLE){f.push(_.concat(p));break}else{if(n===F.MARK)break;f.push(_.concat(p))}}g?n===F.SEMI?f.push(p):n===F.MARK&&f.push(p.concat([!0])):n===F.LEFT||n===F.FULL||n===F.SINGLE?f.push(new Array(r).fill(null).concat(p)):n===F.ANTI?f.push(p):n===F.MARK&&f.push(p.concat([a?null:!1]))}return f}c(CP,"probeJoinRows");function bq(e){return e===F.LEFT||e===F.FULL||e===F.SINGLE||e===F.ANTI||e===F.MARK}c(bq,"emitsOnUnmatchedProbe");function IP(e,{joinType:t,buildColCount:s,buildSchema:n,probeSchema:r},i=ja){if(e.length===0)return new It([],0);let o=t===F.SEMI||t===F.ANTI,a=t===F.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 se(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 It(u,e.length)}c(IP,"buildJoinOutputChunk");var OP=tt({"src/execution/operators/join-core.js"(){Xe(),Te(),$t(),sP(),fc()}});function Sq(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(Sq,"hashString");function P1(e){return Sq(e)%_t.hashJoinPartitions}c(P1,"getPartition");var BP,MP,xq=tt({"src/execution/operators/hash-join.js"(){Xe(),Te(),$t(),be(),OP(),BP=class{static{c(this,"HashJoinBuild")}constructor(e,t,s,n){this.keyExtractors=e,this.joinType=t||F.INNER,this.uniqueKeys=!!s,this.hashTable=new Map,this.buildSchema=null,this.hasNullKey=!1,this.spillManager=n,this.partitions=Array.from({length:_t.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;continue}let i=P1(r),o=this.partitions[i];o.rows.push({row:s[n],key:r}),o.spilled||this.totalRowsInRAM++,o.spilled&&o.rows.length>=_t.flushBatchSize&&await this.flushPartition(i)}if(this.totalRowsInRAM>_t.memoryLimit){let n=-1,r=0;for(let i=0;i<_t.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 It([],0);let t=e[0].length,s=new Array(t);for(let n=0;n<t;n++){let r=new se(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 It(s,e.length)}async finalize(){for(let e=0;e<_t.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<_t.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)}}}return t}probe(e){return this.hashTable.get(e)||null}buildKey(e,t){return L1(this.keyExtractors,e,t)}},MP=class{static{c(this,"HashJoinProbe")}constructor(e,t,s,n,r=F.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:_t.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=P1(r);this.buildSide.partitions[o].spilled?(this.spillBuffers[o].push({row:i,key:r}),this.spillBuffers[o].length>=_t.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 It([],0);let t=e[0].length,s=new Array(t);for(let n=0;n<t;n++){let r=new se(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 It(s,e.length)}executeInMemoryJoin(e){let t=CP(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<_t.hashJoinPartitions;t++)this.spillBuffers[t].length>0&&await this.flushProbePartition(t);for(let t=0;t<_t.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 L1(this.probeKeyExtractors,e,t)}buildOutputChunk(e){return IP(e,{joinType:this.joinType,buildColCount:this.buildColCount,buildSchema:this.buildSide.buildSchema,probeSchema:this.probeSchema})}}}});function yw(e){return e==null?!0:Array.isArray(e)&&e.some(t=>t==null)}c(yw,"isNullKey");var LP,Aq=tt({"src/execution/operators/merge-join.js"(){Te(),Xe(),$t(),rs(),LP=class{static{c(this,"MergeJoinOperator")}constructor(e,t,s,n,r,i,o=F.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===F.SEMI||this.joinType===F.ANTI,t=this.joinType===F.MARK,s=this._flattenAndExtractKeys(this.buildChunks,this.buildKeyExtractors),n=this._flattenAndExtractKeys(this.probeChunks,this.probeKeyExtractors),r=s.filter(d=>!yw(d.key)),i=n.filter(d=>!yw(d.key)),o=s.filter(d=>yw(d.key)),a=n.filter(d=>yw(d.key)),l=o.length>0?null:!1;r.sort((d,m)=>this._compareKeys(d.key,m.key)),i.sort((d,m)=>this._compareKeys(d.key,m.key));let u=[],f=this.conditionEvaluator?this.createAdapter():null,h=0,p=0;for(;h<r.length&&p<i.length;){let d=r[h],m=i[p],g=this._compareKeys(d.key,m.key);if(g<0)(this.joinType===F.LEFT||this.joinType===F.FULL)&&u.push(this._combineRowWithNulls(d,!0)),h++;else if(g>0)e&&this.joinType===F.ANTI?u.push(this._extractProbeRow(m)):t?u.push(this._extractProbeRow(m).concat([l])):(this.joinType===F.RIGHT||this.joinType===F.FULL)&&u.push(this._combineRowWithNulls(m,!1)),p++;else{let y=h;for(;y<r.length&&this._compareKeys(d.key,r[y].key)===0;)y++;let _=p;for(;_<i.length&&this._compareKeys(m.key,i[_].key)===0;)_++;if(e||t)for(let w=p;w<_;w++){let S=!1;for(let b=h;b<y;b++){if(f){let A=this._combineRow(r[b],i[w]);if(f.setRow(A),!this.conditionEvaluator(f,0))continue}S=!0;break}this.joinType===F.SEMI&&S?u.push(this._extractProbeRow(i[w])):this.joinType===F.ANTI&&!S?u.push(this._extractProbeRow(i[w])):t&&u.push(this._extractProbeRow(i[w]).concat([S?!0:l]))}else for(let w=h;w<y;w++){let S=!1;for(let b=p;b<_;b++){let A=this._combineRow(r[w],i[b]);f&&(f.setRow(A),!this.conditionEvaluator(f,0))||(u.push(A),S=!0)}!S&&(this.joinType===F.LEFT||this.joinType===F.FULL)&&u.push(this._combineRowWithNulls(r[w],!0))}h=y,p=_}}for(;h<r.length;)(this.joinType===F.LEFT||this.joinType===F.FULL)&&u.push(this._combineRowWithNulls(r[h],!0)),h++;for(;p<i.length;)e&&this.joinType===F.ANTI?u.push(this._extractProbeRow(i[p])):t?u.push(this._extractProbeRow(i[p]).concat([l])):(this.joinType===F.RIGHT||this.joinType===F.FULL)&&u.push(this._combineRowWithNulls(i[p],!1)),p++;for(let d of o)(this.joinType===F.LEFT||this.joinType===F.FULL)&&u.push(this._combineRowWithNulls(d,!0));for(let d of a)e&&this.joinType===F.ANTI?u.push(this._extractProbeRow(d)):t?u.push(this._extractProbeRow(d).concat([null])):(this.joinType===F.RIGHT||this.joinType===F.FULL)&&u.push(this._combineRowWithNulls(d,!1));return u.length===0?[]:[this._buildOutputChunk(u)]}_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)){for(let r=0;r<e.length;r++){let i=typeof e[r]=="bigint"?Number(e[r]):e[r],o=typeof t[r]=="bigint"?Number(t[r]):t[r];if(i<o)return-1;if(i>o)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===F.SEMI||this.joinType===F.ANTI,s=this.joinType===F.MARK,n=t?this.probeColCount:s?this.probeColCount+1:this.buildColCount+this.probeColCount,r=[];for(let i=0;i<n;i++){let o=L.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=L.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 se(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 It(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}}}}),PP,Eq=tt({"src/execution/operators/nested-loop-join.js"(){Te(),Xe(),$t(),PP=class{static{c(this,"NestedLoopJoinOperator")}constructor(e,t,s,n,r=F.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===F.FULL||this.joinType===F.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,0)))){if(a=!0,r&&(r[l]=1),this.joinType===F.SEMI){s.push(this._extractOuter(o));break}if(this.joinType===F.ANTI||this.joinType===F.MARK)break;s.push(f)}}a?this.joinType===F.MARK&&s.push(this._outerWithMark(o,!0)):this.joinType===F.LEFT||this.joinType===F.FULL?s.push(this._outerWithNulls(o)):this.joinType===F.ANTI?s.push(this._extractOuter(o)):this.joinType===F.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===F.MARK&&n===t-1)r="BOOLEAN";else if(this.joinType===F.SEMI||this.joinType===F.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 se(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 It(s,e.length)}_createAdapter(){let e=this.outerColCount+this.innerColCount,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}}}});async function Tq(e,t){let s=await e.buildPipeline(t.children[0]),n=await e.buildPipeline(t.children[1]),r=t.joinType===F.SEMI||t.joinType===F.ANTI,i=t.joinType===F.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=qw(o.schema,a.schema),{buildKeys:p,probeKeys:d,residualCondition:m}=yq(t.condition,o.columnMapping,a.columnMapping),g=m?qt(m,h):null,y=i?[...s.schema,{name:t.markColumn||"__mark",dataType:"BOOLEAN",tableAlias:""}]:null,_=r?s.schema:i?y:f,w=r?s.columnMapping:i?e.buildSchemaMapping(y,""):h;if(t.physicalStrategy===Pn.MERGE)return Nq(t,{left:s,right:n,buildInput:o,probeInput:a,buildKeys:p,probeKeys:d,conditionEvaluator:g,resultSchema:_,resultMapping:w});if(t.physicalStrategy===Pn.NESTED_LOOP)return Rq(t,{left:s,right:n,buildInput:o,probeInput:a,isSemiAnti:r,isMark:i,markSchema:y});let S=e.tempManager.allocate("spill","join"),b=c(()=>new BP(p.map(I=>qt(I,o.columnMapping)),t.joinType,!!t._dedupeBuild&&!g,e.storageBackend.createSpillManager(S)),"makeBuildSide"),A=c(I=>new MP(I,d.map(E=>qt(E,a.columnMapping)),o.schema.length,a.schema.length,t.joinType,g),"makeProbeOp"),x={schema:_,columnMapping:w,register:c((I,E,T)=>{let B=b(),k={async consume(z){await B.consume(z)},async finalize(){await B.finalize()}},O=I.createPipeline(k);o.register(I,O,k);let D=A(B),q={get cancelToken(){return T.cancelToken},async consume(z){if(this.cancelToken?.isCancelled)return;let j=await D.process(z);j&&j.size>0&&await T.consume(j)},async finalize(){if(t.joinType===F.FULL||t.joinType===F.LEFT&&l===t.children[0]){let j=B.emitUnmatched(a.schema.length);j.length>0&&await T.consume(D.buildOutputChunk(j))}D.finalize&&await D.finalize(T),T.finalize&&await T.finalize()}};I.addDependency(E,O),a.register(I,E,q)},"register")},N=e._prepareParallelJoin(t,o,a,l,u,p,d,m,h);return N?{schema:_,columnMapping:w,register:c((I,E,T)=>{let B=c((D,q)=>D.storage?null:ac(I,E,q),"registerSide"),k=B(N.buildSide,o),O=B(N.probeSide,a);I.setSource(E,async function*(){let D=c(async(V,Q)=>{if(!V.storage)return Q;let xt=[];for await(let Et of V.storage.scan())xt.push(Et);return xt},"collect"),q=await D(N.buildSide,k),z=await D(N.probeSide,O),j=c(V=>V.reduce((Q,xt)=>Q+xt.size,0),"countRows"),Y=j(q),W=j(z),G=Y+W>=_t.parallelJoinThreshold&&Y<=_t.memoryLimit,H=c(async function*(){let Q=k!==null&&O!==null?await e._runBufferedSerialJoin(b,A,q,z,t,a.schema.length):await e._executeSubPipeline(x);for(let xt of Q)!xt||xt.size===0||(await T.consume(xt),yield xt)},"emitSerial");if(!G){yield*H(),T.finalize&&await T.finalize();return}let kt=c((V,Q)=>{if(V.storage)return V.spec.schema.map(Et=>Et.dataType);let xt=Q.find(Et=>Et.size>0);return xt?xt.columns.map(Et=>Et.dataType):V.spec.schema.map(Et=>Et.dataType)},"typesOf"),wt={build:kt(N.buildSide,q),probe:kt(N.probeSide,z)},bt=!1;try{let V=e.fragmentPool.runJoinStream(N.spec,{chunks:q,columnIndexes:N.buildSide.columnIndexes},{chunks:z,columnIndexes:N.probeSide.columnIndexes},wt);for await(let Q of V)Q.size!==0&&(await T.consume(Q),yield Q,bt=!0)}catch(V){if(bt)throw V;yield*H()}T.finalize&&await T.finalize()})},"register")}:x}c(Tq,"buildJoin");function Nq(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===F.LEFT&&r!==s?(a=s,l=n,u=t.probeKeys,f=t.buildKeys,h=[...s.schema,...n.schema],p=qw(s.schema,n.schema)):e.joinType===F.RIGHT&&r!==n&&(a=n,l=s,u=t.probeKeys,f=t.buildKeys,h=[...n.schema,...s.schema],p=qw(n.schema,s.schema)),{schema:h,columnMapping:p,register:c((d,m,g)=>{let y=ac(d,m,s),_=ac(d,m,n);d.setSource(m,async function*(){let S=await new LP(a===s?y:_,l===s?y:_,u.map(b=>qt(b,a.columnMapping)),f.map(b=>qt(b,l.columnMapping)),a.schema.length,l.schema.length,e.joinType,o).execute();for(let b of S)await g.consume(b),yield b;g.finalize&&await g.finalize()})},"register")}}c(Nq,"buildMergeJoin");function Rq(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=qw(u.schema,f.schema),p=e.condition?qt(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 S=ac(y,_,s),b=ac(y,_,n);y.setSource(_,async function*(){let x=await new PP(u===s?S:b,f===s?S:b,u.schema.length,f.schema.length,e.joinType,p).execute();for(let N of x)await w.consume(N),yield N;w.finalize&&await w.finalize()})},"register")}}c(Rq,"buildNestedLoopJoin");function vq(e,t,s,n,r,i,o,a,l,u){if(!e.fragmentPool||o.length===0||t.joinType===F.CROSS||e._estimatePlanRows(t)<_t.parallelJoinThreshold)return null;let f=D1(e,r,s),h=D1(e,i,n),p=t.joinType===F.FULL||t.joinType===F.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(vq,"prepareParallelJoin");function D1(e,t,s){let n=z6(s.schema),r={spec:{schema:n,baseSchema:n,stages:[]},storage:null,columnIndexes:null},i=t?pP(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 V6(Gw(f,i.stages),n)?{spec:{schema:n,baseSchema:f,stages:i.stages},storage:o,columnIndexes:u}:r}c(D1,"prepareJoinSide");async function kq(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===F.LEFT||r.joinType===F.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(kq,"runBufferedSerialJoin");var Cq=tt({"src/execution/builders/join-builder.js"(){$t(),wd(),_q(),xq(),Aq(),Eq(),ib(),be(),oN()}}),DP,Iq=tt({"src/execution/operators/stream-aggregate.js"(){Xe(),Te(),rs(),bd(),QL(),DP=class{static{c(this,"StreamAggregateOperator")}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(at.has("sumF64","FLOAT64"))return{kernelKey:"sumF64",dataType:"FLOAT64",kind:"SUM"};if(at.has("sumI32","INT32"))return{kernelKey:"sumI32",dataType:"INT32",kind:"SUM"}}if(t==="MIN"){if(e.resultType==="FLOAT64"&&at.has("minF64","FLOAT64"))return{kernelKey:"minF64",dataType:"FLOAT64",kind:"MIN"};if(e.resultType==="INT32"&&at.has("minI32","INT32"))return{kernelKey:"minI32",dataType:"INT32",kind:"MIN"}}if(t==="MAX"){if(e.resultType==="FLOAT64"&&at.has("maxF64","FLOAT64"))return{kernelKey:"maxF64",dataType:"FLOAT64",kind:"MAX"};if(e.resultType==="INT32"&&at.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"&&at.has("sumF64","FLOAT64")?{kernelKey:"sumF64",dataType:"FLOAT64",kind:"AVG"}:null}async _tryWasmUngrouped(e){if(this.groupByExtractors.length!==0||!at||at.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=at.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=at.lookup(a.kernelKey,a.dataType);if(!d)return null;let m=await d(p),g;if(!h.hasNulls)g=r.size;else{let y=at.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&&hd(o._sourceExpr)){let h=await pd(o._sourceExpr,r,o._columnMapping,r.size);h instanceof Float64Array&&(u=h)}if(!u)return null;let f=at.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 se(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 It(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 se(this.groupByTypes[l]||L.VARCHAR,s.length));for(let l=0;l<this.aggregateDefs.length;l++)a.push(new se(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 It(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 Oq(e,t){let s=await e.buildPipeline(t.children[0]),n=(t.groupBy||[]).map(f=>qt(f,s.columnMapping)),r=(t.groupBy||[]).map(f=>e.normalizeExecType(f?.dataType||f?.resultType||"VARCHAR")),i=hP(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=aN(o,t.groupBy||[],t.aggregates);if(t.physicalStrategy===Pn.STREAM)return{schema:o,columnMapping:a,register:c((f,h,p)=>{let d=ac(f,h,s);f.setSource(h,async function*(){let g=await new DP(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 Sd(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=Bq(e,t);return u?{schema:o,columnMapping:a,register:c((f,h,p)=>{let d=u.storage.rowCount(),m=d*u.estimatedRowBytes<=_t.parallelAggMemoryBytes;if(d<_t.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(Oq,"buildAggregate");function Bq(e,t){if(!e.fragmentPool)return null;let s=P6(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(Bq,"prepareParallelAggregate");function aN(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(WL(s[o]),i+o);return n}c(aN,"aggregateSchemaMapping");async function Mq(e,t){let s=await e.buildPipeline(t.children[0]),n=(t.groupBy||[]).map(f=>qt(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?qt(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:Pf("SUM"),extractValue:d}),i.push({name:"COUNT",resultType:"FLOAT64",createAccumulator:Pf("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:Pf(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=aN(l,t.groupBy||[],a);return{schema:l,columnMapping:u,register:FP(s,()=>new Sd(n,r,i))}}c(Mq,"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)}),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:Pf("AVG_FINAL",!1),extractValue:c((w,S)=>{let b=w.columns[_]?.get(S),A=w.columns[_+1]?.get(S);return[typeof b=="bigint"?Number(b):b,typeof A=="bigint"?Number(A):A]},"extractValue")}:{name:y,resultType:e.normalizeAggResultType(m),createAccumulator:Pf(y,!1),extractValue:c((w,S)=>{let b=w.columns[_]?.get(S);return typeof b=="bigint"?Number(b):b},"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=aN(p,t.groupBy||[],o);return{schema:p,columnMapping:d,register:FP(s,()=>new Sd(r,i,h))}}c(Lq,"buildFinalAggregate");function FP(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(FP,"registerHashAggregate");var Pq=tt({"src/execution/builders/aggregate-builder.js"(){$t(),wd(),uP(),Iq(),ib(),be(),oN()}}),xT,Dq=tt({"src/execution/operators/dependent-join.js"(){Xe(),Te(),xT=class{static{c(this,"DependentJoinOperator")}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 se(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 It(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 Uq(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)=>{r.setSource(i,async function*(){let a=e.cteResults.get(t.cteName.toUpperCase());if(!a){let u=[],f={async consume(m){u.push(m)},async finalize(){}},h=new zf,p=h.createPipeline(f);n.register(h,p,f),await new Vf().schedule(h),a={chunks:u,schema:n.schema,columnMapping:n.columnMapping},e.cteResults.set(t.cteName.toUpperCase(),a)}let l=a.chunks.map(u=>{let f=u.columns.map(h=>{let p=new se(h.dataType,u.size);for(let d=0;d<u.size;d++)p.set(d,h.get(u.activeRowIndex(d)));return p.length=u.size,p});return new It(f,u.size)});for(let u of l)await o.consume(u),yield u})},"register")}}c(Uq,"buildCTEScan");async function $q(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($q,"buildMaterialize");async function zq(e,t){let s=await e.buildPipeline(t.children[0]),n=new xT(t.subqueryType,s.schema);return{schema:n.resultSchema,columnMapping:e.buildSchemaMapping(n.resultSchema,""),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),r.addDependency(i,u),r.setSource(i,async function*(){let f=new xT(t.subqueryType,s.schema),h=(t.correlatedColumns||[]).length>0,p=null;for(let m of a){let g=m.toRows();for(let y of g){if(!h&&p!==null){await f.processOuterRow(y,p);continue}let _=await e.buildPipeline(t.children[1]),w=[],S=new zf,b={consume:c(async x=>w.push(x),"consume")},A=S.createPipeline(b);_.register(S,A,b),await new Vf(_t.dependentJoinConcurrency).schedule(S),h||(p=w),await f.processOuterRow(y,w)}}let d=await f.finalize();for(let m of d)await o.consume(m),yield m})},"register")}}c(zq,"buildDependentJoin");var Vq=tt({"src/execution/builders/cte-builders.js"(){Dq(),Xe(),Te(),xd(),ab(),be()}}),UP={};gs(UP,{ExchangeType:c(()=>ce,"ExchangeType"),Fragment:c(()=>Gi,"Fragment"),FragmentPlan:c(()=>lN,"FragmentPlan"),FragmentState:c(()=>Js,"FragmentState"),resetFragmentIdCounter:c(()=>$P,"resetFragmentIdCounter")});function $P(){AT=1}c($P,"resetFragmentIdCounter");var ce,Js,AT,Gi,lN,lc=tt({"src/distributed/planner/fragment.js"(){ce={HASH_SHUFFLE:"hash_shuffle",BROADCAST:"broadcast",GATHER:"gather",PASSTHROUGH:"passthrough"},Js={PENDING:"pending",DISPATCHED:"dispatched",RUNNING:"running",COMPLETED:"completed",FAILED:"failed",CANCELLED:"cancelled"},AT=1,Gi=class{static{c(this,"Fragment")}constructor({planRoot:e,targetNodes:t,exchangeInputs:s,outputPartitioning:n,estimatedCardinality:r}){this.fragmentId=AT++,this.planRoot=e,this.targetNodes=t||[],this.exchangeInputs=s||[],this.outputPartitioning=n||null,this.estimatedCardinality=r||0,this.state=Js.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=Js.DISPATCHED,this.assignedNode=e}markRunning(){this.state=Js.RUNNING}markCompleted(){this.state=Js.COMPLETED}markFailed(e){this.state=Js.FAILED,this.error=e,this.retryCount++}markCancelled(){this.state=Js.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}}},lN=class{static{c(this,"FragmentPlan")}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!==Js.PENDING?!1:e.exchangeInputs.every(t=>{let s=this._fragmentMap.get(t.sourceFragmentId);return s&&s.state===Js.COMPLETED}))}allCompleted(){return this.fragments.every(e=>e.state===Js.COMPLETED)}hasFailed(){return this.fragments.some(e=>e.state===Js.FAILED)}}}}),WE,F1,KE,Zp,jw,zP=tt({"src/distributed/transport/chunk-codec.js"(){rs(),Xe(),sb(),Te(),uc(),WE={[L.BOOLEAN]:0,[L.INT32]:1,[L.INT64]:2,[L.FLOAT64]:3,[L.DECIMAL]:4,[L.VARCHAR]:5,[L.DATE]:6,[L.TIMESTAMP]:7},F1=Object.fromEntries(Object.entries(WE).map(([e,t])=>[t,e])),KE=1363493704,Zp=6,jw=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(KE,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=WE[p.dataType],m=p instanceof br?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)!==KE)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:F1[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 It(o,n)}_encodeColumn(e,t){return e instanceof br?this._encodeDictionaryColumn(e,t):this._encodeRegularColumn(e,t)}_encodeRegularColumn(e,t){if(e.dataType===L.VARCHAR)return this._encodeVarcharColumn(e,t);let s=Math.ceil(t/32),n=s*4,r=$f(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===L.VARCHAR?this._decodeVarcharColumn(e,n):this._decodeFixedColumn(e,t,n)}_decodeFixedColumn(e,t,s){let n=new se(t,s),r=Math.ceil(s/32),i=r*4;n.hasNulls=e.readUInt8(0)===1;let o=PE(s);for(let p=0;p<r;p++)o[p]=e.readUInt32LE(1+p*4);n.nullBitmap=o;let a=$f(t),l=1+i,u=e.subarray(l,l+s*a),f=Q4(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 se(L.VARCHAR,t),n=Math.ceil(t/32),r=n*4;s.hasNulls=e.readUInt8(0)===1;let i=PE(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 br(t),n=Math.ceil(t/32),r=n*4;s.hasNulls=e.readUInt8(0)===1;let i=PE(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 $a,cN=tt({"src/distributed/partition/partition-strategy.js"(){Xe(),Te(),rs(),be(),$a={HASH:"hash",RANGE:"range",ROUND_ROBIN:"round_robin"}}}),ET={};gs(ET,{ExchangeReceiver:c(()=>fN,"ExchangeReceiver"),ExchangeSender:c(()=>uN,"ExchangeSender")});var uN,fN,Ww=tt({"src/distributed/execution/exchange-operator.js"(){be(),lc(),zP(),Te(),Xe(),cN(),uN=class{static{c(this,"ExchangeSender")}constructor(e,t,s={}){this._transport=e,this._targetNodes=t,this._exchangeType=s.exchangeType||ce.GATHER,this._keyExtractors=s.keyExtractors||[],this._partitionCount=s.partitionCount||t.length,this._channelId=s.channelId||`exchange-${Date.now()}`,this._codec=new jw,this._batchSize=_t.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 ce.GATHER:await this._sendGather(e);break;case ce.BROADCAST:await this._sendBroadcast(e);break;case ce.HASH_SHUFFLE:await this._sendShuffle(e);break;default:await this._sendGather(e)}}async finalize(){this._closed=!0;let e=this._codec.encode(new It([],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 se(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 It(a,o.length))}return r}},fN=class{static{c(this,"ExchangeReceiver")}constructor(e,t,s={}){this._transport=e,this._sourceNodes=t,this._channelId=s.channelId||`exchange-${Date.now()}`,this._codec=new jw,this._bufferCapacity=_t.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()}}}}}),VP={};gs(VP,{MergeExchangeOperator:c(()=>GP,"MergeExchangeOperator"),MinHeap:c(()=>TT,"MinHeap")});var GP,TT,qq=tt({"src/distributed/execution/merge-exchange.js"(){be(),Te(),Xe(),rs(),uc(),zP(),GP=class{static{c(this,"MergeExchangeOperator")}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 jw,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 TT(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 se(p.dataType,ms)));let u=ms-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>=ms){for(let p of s)p.length=n;yield new It(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 It(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(ui(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++)sc(o.nullBitmap,t+l)?ic(a.nullBitmap,n+l):eb(a.nullBitmap,n+l)}else m6(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=_t.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}},TT=class{static{c(this,"MinHeap")}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 jq(e,t){return!e||e.length===0?[]:e.map(s=>{let n=qt(s,t);return(r,i)=>n(r,i)})}c(jq,"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(()=>(Ww(),ET)),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(()=>(Ww(),ET)),u=new l(n,t._targetNodes||[],{exchangeType:o||t.exchangeType,channelId:i,partitionCount:t.partitionCount,keyExtractors:jq(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 Kq(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(()=>(qq(),VP)),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(Kq,"buildMergeExchange");async function Hq(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(Hq,"buildExchangeReceive");var Yq=tt({"src/execution/builders/exchange-builders.js"(){wd()}}),U1,hN,qP=tt({"src/execution/query-executor.js"(){$t(),ib(),ZT(),xd(),ab(),be(),H6(),eq(),gq(),Cq(),Pq(),Vq(),Yq(),U1={[C.SCAN]:X6,[C.INDEX_SCAN]:Q6,[C.FILTER]:aq,[C.PROJECT]:lq,[C.JOIN]:Tq,[C.AGGREGATE]:Oq,[C.SORT]:cq,[C.LIMIT]:fq,[C.DISTINCT]:hq,[C.UNION]:pq,[C.CTE_ANCHOR]:Fq,[C.CTE_SCAN]:Uq,[C.MATERIALIZE]:$q,[C.DEPENDENT_JOIN]:zq,[C.TOP_N]:uq,[C.WINDOW]:dq,[C.EMPTY]:tq,[C.SINGLE_ROW]:Z6,[C.EXCHANGE]:Wq,[C.PARTIAL_AGGREGATE]:Mq,[C.FINAL_AGGREGATE]:Lq,[C.MERGE_EXCHANGE]:Kq,[C.EXCHANGE_RECEIVE]:Hq},hN=class{static{c(this,"QueryExecutor")}constructor(e,t,s=null){this.catalog=e,this.tempManager=t,this.storageBackend=s??new xP,this.cteResults=new Map,this.cteDefinitions=new Map,this.workerPool=null,this.parallelDispatch=null,this.fragmentPool=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()>=_t.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 zf,n=new ob(t);await n.init();let r=s.createPipeline(n);(await this.buildPipeline(e)).register(s,r,n);let o=new Vf;return t?(o.schedule(s).catch(l=>n.error(l)),n):(await o.schedule(s),n)}async buildPipeline(e){let t=U1[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===C.SCAN||n.type===C.INDEX_SCAN){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 zf,r=n.createPipeline(s);return e.register(n,r,s),await new Vf().schedule(n),t}_prepareParallelJoin(e,t,s,n,r,i,o,a,l){return vq(this,e,t,s,n,r,i,o,a,l)}_runBufferedSerialJoin(e,t,s,n,r,i){return kq(e,t,s,n,r,i)}normalizeExecType(e){return rb(e)}normalizeAggResultType(e){return fP(e)}}}}),Of,jP=tt({"src/execution/query-result.js"(){Of=class{static{c(this,"QueryResult")}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}}}}),Bw={};gs(Bw,{formatExpression:c(()=>wr,"formatExpression"),formatPlan:c(()=>WP,"formatPlan")});function wr(e){if(!e)return"";switch(e.kind){case v.COLUMN_REF:return e.tableAlias?`${e.tableAlias}.${e.columnName}`:e.columnName;case v.LITERAL:return typeof e.value=="string"?`'${e.value}'`:String(e.value);case v.BINARY:return`(${wr(e.left)} ${e.op} ${wr(e.right)})`;case v.UNARY:return`${e.op} ${wr(e.operand)}`;case v.FUNCTION:return`${e.name}(${e.args.map(wr).join(", ")})`;case v.AGGREGATE:return`${e.name}(${e.args.map(wr).join(", ")})`;default:return e.kind?`<${e.kind}>`:JSON.stringify(e)}}c(wr,"formatExpression");function WP(e,t=0){let n=`${" ".repeat(t)}-> ${Jq(e)}
|
|
221
|
-
`;if(e.children&&e.children.length>0)for(let r of e.children)n+=WP(r,t+1);return n}c(WP,"formatPlan");function Jq(e){switch(e.type){case C.SCAN:return`Seq Scan on ${e.table}${e.alias?" as "+e.alias:""}`;case C.FILTER:return`Filter (condition: ${wr(e.condition)})`;case C.PROJECT:return`Project (${e.expressions.map(n=>wr(n)).join(", ")})`;case C.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: ${wr(e.condition)})`),i}case C.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=>wr(i)).join(", ")})`),e.aggregates&&e.aggregates.length>0&&(r+=` (aggs: ${e.aggregates.map(i=>`${i.name}(${i.args.map(o=>wr(o)).join(", ")})`).join(", ")})`),r}case C.SORT:return`Sort (${e.orderKeys.map(n=>`${wr(n.expr)} ${n.direction||"ASC"}`).join(", ")})`;case C.LIMIT:return`Limit (count: ${e.count}${e.offset?", offset: "+e.offset:""})`;case C.TOP_N:{let n=e.orderKeys.map(r=>`${wr(r.expr)} ${r.direction||"ASC"}`).join(", ");return`Top-N (count: ${e.count}${e.offset?", offset: "+e.offset:""}, order: ${n})`}case C.DISTINCT:return"Distinct";case C.UNION:return`Union${e.all?" All":""}`;case C.CTE_ANCHOR:return`CTE Anchor (${e.cteName})`;case C.CTE_SCAN:return`CTE Scan (${e.cteName})`;case C.MATERIALIZE:return"Materialize";case C.DEPENDENT_JOIN:return`Dependent Join (${e.subqueryType})`;case C.INDEX_SCAN:{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 C.EMPTY:return"Empty (short-circuit)";case C.WINDOW:return`Window (${e.windowExprs.map(r=>r.name).join(", ")})`;default:return`${e.type}`}}c(Jq,"formatNode");var HE=tt({"src/planner/plan-formatter.js"(){$t(),Ht()}}),YE,_w,KP,Xq=tt({"src/wasm/region-allocator.js"(){YE=16,_w=c(e=>e+YE-1&~(YE-1),"align"),KP=class{static{c(this,"RegionAllocator")}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=_w(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=_w(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=_w(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)}}}}),$1,HP,YP,ww,bw,z1,JP,Qq=tt({"src/wasm/wasm-loader-base.js"(){Xq(),$1=65536,HP=256,YP=16384,ww=16,bw=4,z1=8,JP=class{static{c(this,"WasmLoaderBase")}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 KP(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+ww-1&~(ww-1),s=e+ww,n=t+s,r=this.memory.buffer.byteLength;if(n>r){let i=Math.ceil((n-r)/$1);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===bw?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*bw))}readF64Array(e,t){return new Float64Array(this.memory.buffer.slice(e,e+t*z1))}readF64(e){return new Float64Array(this.memory.buffer,e,1)[0]}readU32Array(e,t){return new Uint32Array(this.memory.buffer.slice(e,e+t*bw))}}}}),NT={};gs(NT,{WasmLoader:c(()=>pN,"WasmLoader"),configureWasmSource:c(()=>XP,"configureWasmSource"),getGlobalLoader:c(()=>Ad,"getGlobalLoader"),resetGlobalLoader:c(()=>Zq,"resetGlobalLoader")});function XP(e){Kw=e}c(XP,"configureWasmSource");async function Ad(e={}){if(!Bf){if(!Kw)throw new Error("WASM byte source not configured \u2014 call configureWasmSource() first");Bf=new pN(Kw),await Bf.init(e)}return Bf}c(Ad,"getGlobalLoader");function Zq(){Bf=null}c(Zq,"resetGlobalLoader");var pN,Bf,Kw,cc=tt({"src/wasm/loader.js"(){Qq(),pN=class extends JP{static{c(this,"WasmLoader")}constructor(e){super(),this.byteSource=e}async init(e={}){this.shared=!0,this.memory=new WebAssembly.Memory({initial:HP,maximum:YP,shared:!0}),this.bumpOffset=0}async fetchModuleBytes(e){return this.byteSource(e)}},Bf=null,Kw=null}});async function lb(){if(rd)return rd;let e=await Ad();return rd={loader:e,instance:await e.loadModule("core")},rd}c(lb,"getCoreInstance");async function V1(e,t){let{loader:s,instance:n}=await lb();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(V1,"wasmFilterEqI32");async function G1(e,t){let{loader:s,instance:n}=await lb();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(G1,"wasmFilterLtI32");async function q1(e,t){let{loader:s,instance:n}=await lb();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(q1,"wasmFilterGtI32");async function j1(e,t,s){let{loader:n,instance:r}=await lb();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(j1,"wasmFilterBetweenI32");var rd,tj=tt({"src/wasm/kernels/wasm-filter.js"(){cc(),rd=null}});async function Wa(){if(id)return id;let e=await Ad();return id={loader:e,instance:await e.loadModule("core")},id}c(Wa,"getCoreInstance2");async function ej(e,t){let{loader:s,instance:n}=await Wa();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(ej,"wasmFilterEqF64");async function sj(e,t){let{loader:s,instance:n}=await Wa();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(sj,"wasmFilterLtF64");async function nj(e,t){let{loader:s,instance:n}=await Wa();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(nj,"wasmFilterGtF64");async function rj(e,t){let{loader:s,instance:n}=await Wa();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(rj,"wasmFilterLeF64");async function ij(e,t){let{loader:s,instance:n}=await Wa();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(ij,"wasmFilterGeF64");async function oj(e,t,s){let{loader:n,instance:r}=await Wa();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(oj,"wasmFilterBetweenF64");async function W1(e,t){let{loader:s,instance:n}=await Wa();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(W1,"wasmFilterLeI32");async function K1(e,t){let{loader:s,instance:n}=await Wa();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(K1,"wasmFilterGeI32");var id,aj=tt({"src/wasm/kernels/wasm-filter-f64.js"(){cc(),id=null}});async function Gf(){if(od)return od;let e=await Ad();return od={loader:e,instance:await e.loadModule("core")},od}c(Gf,"getCoreInstance3");async function lj(e){let{loader:t,instance:s}=await Gf();t.reset();let n=t.resolveDataPtr(e,4);return s.exports.sumI32(n,e.length)}c(lj,"wasmSumI32");async function cj(e){let{loader:t,instance:s}=await Gf();t.reset();let n=t.resolveDataPtr(e,8);return s.exports.sumF64(n,e.length)}c(cj,"wasmSumF64");async function uj(e){let{loader:t,instance:s}=await Gf();t.reset();let n=t.resolveDataPtr(e,4);return s.exports.minI32(n,e.length)}c(uj,"wasmMinI32");async function fj(e){let{loader:t,instance:s}=await Gf();t.reset();let n=t.resolveDataPtr(e,4);return s.exports.maxI32(n,e.length)}c(fj,"wasmMaxI32");async function hj(e){let{loader:t,instance:s}=await Gf();t.reset();let n=t.resolveDataPtr(e,8);return s.exports.minF64(n,e.length)}c(hj,"wasmMinF64");async function pj(e){let{loader:t,instance:s}=await Gf();t.reset();let n=t.resolveDataPtr(e,8);return s.exports.maxF64(n,e.length)}c(pj,"wasmMaxF64");var od,dj=tt({"src/wasm/kernels/wasm-aggregate.js"(){cc(),od=null}});async function xr(){if(ad)return ad;let e=await Ad();return ad={loader:e,instance:await e.loadModule("core")},ad}c(xr,"getCoreInstance4");async function mj(e,t){let{loader:s,instance:n}=await xr();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(mj,"wasmVecAddF64");async function gj(e,t){let{loader:s,instance:n}=await xr();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(gj,"wasmVecSubF64");async function yj(e,t){let{loader:s,instance:n}=await xr();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(yj,"wasmVecMulF64");async function _j(e,t){let{loader:s,instance:n}=await xr();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(_j,"wasmVecDivF64");async function wj(e,t){let{loader:s,instance:n}=await xr();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(wj,"wasmScalarAddF64");async function bj(e,t){let{loader:s,instance:n}=await xr();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(bj,"wasmScalarSubF64");async function Sj(e,t){let{loader:s,instance:n}=await xr();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(Sj,"wasmScalarMulF64");async function xj(e,t){let{loader:s,instance:n}=await xr();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(xj,"wasmScalarDivF64");async function Aj(e,t){let{loader:s,instance:n}=await xr();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(Aj,"wasmScalarSubRevF64");async function Ej(e,t){let{loader:s,instance:n}=await xr();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(Ej,"wasmScalarDivRevF64");async function Tj(e){let{loader:t,instance:s}=await xr();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(Tj,"wasmWidenI32ToF64");async function Nj(e){let{loader:t,instance:s}=await xr();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(Nj,"wasmNegF64");async function Rj(e,t){let{loader:s,instance:n}=await xr();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(Rj,"wasmCountBits");var ad,vj=tt({"src/wasm/kernels/wasm-arithmetic.js"(){cc(),ad=null}}),RT={};gs(RT,{registerAllKernels:c(()=>kj,"registerAllKernels")});function kj(){at.register("filterEq","INT32",V1),at.register("filterLt","INT32",G1),at.register("filterGt","INT32",q1),at.register("filterLe","INT32",W1),at.register("filterGe","INT32",K1),at.register("filterBetween","INT32",j1),at.register("filterEq","FLOAT64",ej),at.register("filterLt","FLOAT64",sj),at.register("filterGt","FLOAT64",nj),at.register("filterLe","FLOAT64",rj),at.register("filterGe","FLOAT64",ij),at.register("filterBetween","FLOAT64",oj),at.register("filterEq","DATE",V1),at.register("filterLt","DATE",G1),at.register("filterGt","DATE",q1),at.register("filterLe","DATE",W1),at.register("filterGe","DATE",K1),at.register("filterBetween","DATE",j1),at.register("sumI32","INT32",lj),at.register("sumF64","FLOAT64",cj),at.register("minI32","INT32",uj),at.register("maxI32","INT32",fj),at.register("minF64","FLOAT64",hj),at.register("maxF64","FLOAT64",pj),at.register("vecAddF64","FLOAT64",mj),at.register("vecSubF64","FLOAT64",gj),at.register("vecMulF64","FLOAT64",yj),at.register("vecDivF64","FLOAT64",_j),at.register("scalarAddF64","FLOAT64",wj),at.register("scalarSubF64","FLOAT64",bj),at.register("scalarMulF64","FLOAT64",Sj),at.register("scalarDivF64","FLOAT64",xj),at.register("scalarSubRevF64","FLOAT64",Aj),at.register("scalarDivRevF64","FLOAT64",Ej),at.register("widenI32ToF64","INT32",Tj),at.register("negF64","FLOAT64",Nj),at.register("countBits","UINT8",Rj)}c(kj,"registerAllKernels");var H1=tt({"src/wasm/register-kernels.js"(){bd(),tj(),aj(),dj(),vj()}}),QP={};gs(QP,{WorkerPool:c(()=>ZP,"WorkerPool")});var Y1,J1,Zn,ZP,Mj=tt({"src/parallel/worker-pool.js"(){Y1=Oj(Ij(import.meta.url)),J1=Bj(Y1,"worker-thread.js"),Zn={IDLE:0,BUSY:1,TERMINATED:2},ZP=class{static{c(this,"WorkerPool")}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=new Cj(J1,{workerData:{workerId:e,wasmModule:this.wasmModule,wasmMemory:this.wasmMemory,regionId:t,regionStart:s.start,regionCapacity:s.end-s.start}});return new Promise((r,i)=>{let o={worker:n,id:e,regionId:t,state:Zn.IDLE};n.on("message",a=>{if(a.type==="ready"){this.workers.set(e,o),r();return}if(a.type==="result"){let l=this.pendingTasks.get(a.taskId);l&&(this.pendingTasks.delete(a.taskId),o.state=Zn.IDLE,l.resolve(a.data),this._drainQueue());return}if(a.type==="error"){let l=this.pendingTasks.get(a.taskId);l&&(this.pendingTasks.delete(a.taskId),o.state=Zn.IDLE,l.reject(new Error(a.error)),this._drainQueue())}}),n.on("error",a=>{o.state=Zn.TERMINATED;for(let[l,u]of this.pendingTasks)u.workerId===e&&(this.pendingTasks.delete(l),u.reject(a));i(a)})})}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===Zn.TERMINATED){r(new Error(`Worker ${t} not available`));return}if(a.state===Zn.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=Zn.BUSY,n.workerId=e.id,this.pendingTasks.set(t,n),e.worker.postMessage({taskId:t,...s})}_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!==Zn.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===Zn.IDLE)return e;return null}activeWorkerCount(){let e=0;for(let t of this.workers.values())t.state!==Zn.TERMINATED&&e++;return e}idleWorkerCount(){let e=0;for(let t of this.workers.values())t.state===Zn.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===Zn.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=Zn.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}}}}),tD={};gs(tD,{ParallelDispatch:c(()=>eD,"ParallelDispatch")});var JE,XE,X1,Q1,td,eD,Lj=tt({"src/parallel/parallel-dispatch.js"(){be(),JE=new Set(["INT32","FLOAT64","DATE"]),XE=new Set(["filterEq","filterLt","filterGt","filterLe","filterGe","filterBetween"]),X1=new Set(["sum","min","max"]),Q1=new Set(["scalarAddF64","scalarSubF64","scalarMulF64","scalarDivF64","scalarSubRevF64","scalarDivRevF64","vecAddF64","vecSubF64","vecMulF64","vecDivF64","negF64","widenI32ToF64"]),td={INT32:4,FLOAT64:8,DATE:4},eD=class{static{c(this,"ParallelDispatch")}constructor(e,t,s){this.workerPool=e,this.regionAllocator=t,this.globalDispatch=s}canParallelize(e,t,s){return!this.workerPool||s<_t.parallelThreshold||!JE.has(t)?!1:!!(XE.has(e)||X1.has(e))}async filterParallel(e,t,s,n,r){if(!this.canParallelize(s,n,t))return this._filterFallback(s,n,e,r);let i=td[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=td[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=>XE.has(p.operation))||!this.canParallelize(s[0].operation,r,t))return this._compoundFilterFallback(e,t,s,n,r);let o=td[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=td[s];if(!JE.has(s)||!this.workerPool||t<_t.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<_t.parallelThreshold||!Q1.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/_t.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}}}}),Mw,Pj=tt({"src/parallel/morsel-scheduler.js"(){Mw=class sD{static{c(this,"_MorselScheduler")}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(sD.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 nD(e){return e instanceof br?Zl(e.indices)&&Zl(e.nullBitmap):ui(e.dataType)?Zl(e.data)&&Zl(e.nullBitmap):Zl(e.offsets)&&Zl(e.stringBytes)&&Zl(e.nullBitmap)}c(nD,"columnIsShared");function Dj(e,t){for(let s of t)if(!nD(e.columns[s]))return!1;return!0}c(Dj,"chunkIsShared");function Fj(e,t,s){let n=qa(Math.max(t,1)),r=s.acquire(Uint32Array,n);if(r.set(e.nullBitmap.subarray(0,Math.min(n,e.nullBitmap.length))),e instanceof br){let a=s.acquire(Uint16Array,t);return a.set(e.indices.subarray(0,t)),br.fromParts({indices:a,reverseDict:e.reverseDict,nullBitmap:r,length:t,hasNulls:e.hasNulls,allocator:s})}if(ui(e.dataType)){let a=s.acquire(Uf(e.dataType),t);return a.set(e.data.subarray(0,t)),se.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)),se.fromParts({dataType:e.dataType,offsets:i,stringBytes:o,stringBytesUsed:e.stringBytesUsed,nullBitmap:r,length:t,hasNulls:e.hasNulls,allocator:s})}c(Fj,"copyColumn");function Z1(e,t,s,n=!1){let r=e.selectionVector?e.flatten():e;if(!n&&Dj(r,t))return r;let i=new Set(t),o=r.columns.map((a,l)=>i.has(l)&&(n||!nD(a))?Fj(a,r.size,s):a);return new It(o,r.size)}c(Z1,"shareChunk");function Uj(e,t){let s=t.map(a=>iD.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(Uj,"writeDictBlob");function $j(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?"":oD.decode(s.subarray(r,r+o)),r+=o}return n}c($j,"readDictBlob");function tL(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*vt.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)*vt.WORDS,g=Math.min(qa(Math.max(h.size,1)),d.nullBitmap.length);if(l[m+vt.HAS_NULLS]=d.hasNulls?1:0,l[m+vt.NULL_BUF]=o(d.nullBitmap.buffer),l[m+vt.NULL_OFF]=d.nullBitmap.byteOffset,l[m+vt.NULL_WORDS]=g,d instanceof br){let y=n.get(d.reverseDict);y||(y=Uj(s,d.reverseDict),n.set(d.reverseDict,y)),l[m+vt.KIND]=kT,l[m+vt.TYPE_ID]=Lw.get(L.VARCHAR),l[m+vt.DATA_BUF]=o(d.indices.buffer),l[m+vt.DATA_OFF]=d.indices.byteOffset,l[m+vt.DATA_LEN]=h.size,l[m+vt.AUX_BUF]=o(y.lengths.buffer),l[m+vt.AUX_OFF]=y.lengths.byteOffset,l[m+vt.AUX_LEN]=y.count,l[m+vt.AUX2_BUF]=o(y.blob.buffer),l[m+vt.AUX2_OFF]=y.blob.byteOffset,l[m+vt.AUX2_LEN]=y.totalBytes}else ui(d.dataType)?(l[m+vt.KIND]=vT,l[m+vt.TYPE_ID]=Lw.get(d.dataType),l[m+vt.DATA_BUF]=o(d.data.buffer),l[m+vt.DATA_OFF]=d.data.byteOffset,l[m+vt.DATA_LEN]=h.size):(l[m+vt.KIND]=rD,l[m+vt.TYPE_ID]=Lw.get(d.dataType),l[m+vt.DATA_BUF]=o(d.offsets.buffer),l[m+vt.DATA_OFF]=d.offsets.byteOffset,l[m+vt.DATA_LEN]=h.size+1,l[m+vt.AUX_BUF]=o(d.stringBytes.buffer),l[m+vt.AUX_OFF]=d.stringBytes.byteOffset,l[m+vt.AUX_LEN]=d.stringBytesUsed)}}return{buffers:r,meta:l,sizes:u,colCount:a}}c(tL,"encodeChunkSet");function zj(e){let t=0;for(let s of e.buffers)t+=s.byteLength;return t}c(zj,"transportBytes");var vT,rD,kT,QE,Lw,vt,iD,oD,aD,Vj=tt({"src/parallel/chunk-transport.js"(){Xe(),sb(),Te(),rs(),fc(),uc(),vT=0,rD=1,kT=2,QE=[L.BOOLEAN,L.INT32,L.INT64,L.FLOAT64,L.DECIMAL,L.VARCHAR,L.DATE,L.TIMESTAMP],Lw=new Map(QE.map((e,t)=>[e,t])),vt={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},iD=new TextEncoder,oD=new TextDecoder,aD=class{static{c(this,"ChunkSetReader")}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)*vt.WORDS,i=this.meta,o=i[r+vt.HAS_NULLS]===1,a=new Uint32Array(this.buffers[i[r+vt.NULL_BUF]],i[r+vt.NULL_OFF],i[r+vt.NULL_WORDS]),l=i[r+vt.KIND],u=QE[i[r+vt.TYPE_ID]],f=this.buffers[i[r+vt.DATA_BUF]],h=i[r+vt.DATA_OFF],p=i[r+vt.DATA_LEN];if(l===kT){let d=new Uint32Array(this.buffers[i[r+vt.AUX_BUF]],i[r+vt.AUX_OFF],Math.max(i[r+vt.AUX_LEN],1)),m=new Uint8Array(this.buffers[i[r+vt.AUX2_BUF]],i[r+vt.AUX2_OFF],Math.max(i[r+vt.AUX2_LEN],1));s[n]=br.fromParts({indices:new Uint16Array(f,h,p),reverseDict:$j(d,i[r+vt.AUX_LEN],m),nullBitmap:a,length:t,hasNulls:o})}else l===vT?s[n]=se.fromParts({dataType:u,data:new(Uf(u))(f,h,p),nullBitmap:a,length:t,hasNulls:o}):s[n]=se.fromParts({dataType:u,offsets:new Uint32Array(f,h,p),stringBytes:new Uint8Array(this.buffers[i[r+vt.AUX_BUF]],i[r+vt.AUX_OFF],i[r+vt.AUX_LEN]),stringBytesUsed:i[r+vt.AUX_LEN],nullBitmap:a,length:t,hasNulls:o})}return new It(s,t)}}}}),lD={};gs(lD,{FragmentPool:c(()=>cD,"FragmentPool")});async function*jj(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(jj,"completionOrder");function eL(e,t,s){let n=new Map,r=e.map(o=>Z1(o,t,s)),i=tL(r,t,s,n);return i.buffers.length>_t.transportMaxBuffers&&(r=e.map(o=>Z1(o,t,s,!0)),i=tL(r,t,s,n)),{shared:r,encoded:i}}c(eL,"encodeForTransport");function sL(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(sL,"concatRefs");function nL(e){let t=1;for(;t<e;)t<<=1;return t}c(nL,"nextPowerOfTwo");var cD,Wj=tt({"src/parallel/fragment-pool.js"(){be(),Te(),fc(),Pj(),Vj(),ib(),OP(),cD=class{static{c(this,"FragmentPool")}constructor(e=_t.parallelWorkers,t=_t.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=_t.parallelAggMemoryBytes,spillDir:i=null}=n,o=s.filter(g=>g.size>0);if(o.length===0)return gw(e).finalize();this._ensure();let a=new _d,{shared:l,encoded:u}=eL(o,t,a);if(r>0&&zj(u)>r)return null;let f=Math.max(1,Math.round(this.morselRows/ms)),h=new Mw(l.length,f),p=nL(this.workerCount*Math.max(1,_t.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:_t.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=>qj.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>=_t.parallelCombineMinGroups&&this.workers.length>1)){let u=gw(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=gw(e);f.absorbPartials(u),l.push(...await f.finalize())}return l.length===0?gw(e).finalize():l}async*runJoinStream(e,t,s,n){this._ensure();let r=new _d,i=c(w=>{let S=w.chunks.filter(A=>A.size>0),b=w.columnIndexes??(S.length>0?S[0].columns.map((A,x)=>x):[]);return eL(S,b,r)},"prepare"),{shared:o,encoded:a}=i(t),{shared:l,encoded:u}=i(s),f=Math.max(1,Math.round(this.morselRows/ms)),h=new Mw(o.length,f),p=new Mw(l.length,f),d=nL(this.workerCount*Math.max(1,_t.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 S of w.probeNullRows)y.push({row:S,key:null});if(y.length>0){let w=CP(y,()=>null,{joinType:e.joinType,buildColCount:e.buildColCount,probeColCount:e.probeColCount,conditionEvaluator:null,hasNullKey:g});for(let S=0;S<w.length;S+=ms)yield IP(w.slice(S,S+ms),{joinType:e.joinType,buildColCount:e.buildColCount,buildSchema:n.build,probeSchema:n.probe})}let _=[];for(let w=0;w<d;w++){let S=sL(m.map(I=>I.buildPartitions[w])),b=sL(m.map(I=>I.probePartitions[w])),A=b.length>0&&(S.length>0||bq(e.joinType)),x=S.length>0&&e.buildPreserved;if(!A&&!x)continue;let N=this.workers[_.length%this.workers.length];_.push(this._request(N,{type:"joinProbe",spec:e,buildChunks:a,probeChunks:u,buildRefs:S,probeRefs:A?b:new Uint32Array(0),hasNullKey:g,outputTypes:n}))}for await(let w of jj(_)){if(!w.chunks)continue;let S=new aD(w.chunks);for(let b=0;b<S.count;b++){let A=S.chunk(b);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()))}}}}),uD={};gs(uD,{NodeDescriptor:c(()=>fD,"NodeDescriptor"),NodeRole:c(()=>ec,"NodeRole"),NodeStatus:c(()=>un,"NodeStatus")});var ec,un,fD,dN=tt({"src/distributed/cluster/node-descriptor.js"(){ec={COORDINATOR:"coordinator",WORKER:"worker",HYBRID:"hybrid"},un={ALIVE:"alive",SUSPECT:"suspect",DEAD:"dead"},fD=class hD{static{c(this,"_NodeDescriptor")}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||ec.WORKER,this.capacity=i||{cores:1,memoryMb:512},this.status=o||un.ALIVE,this.partitions=new Set}get address(){return`${this.host}:${this.port}`}isAlive(){return this.status===un.ALIVE}canExecuteFragments(){return this.status!==un.DEAD&&(this.role===ec.WORKER||this.role===ec.HYBRID)}canCoordinate(){return this.role===ec.COORDINATOR||this.role===ec.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 hD(t);if(t.partitions)for(let n of t.partitions)s.partitions.add(n);return s}}}}),pD,rL,Kj=tt({"src/distributed/cluster/heartbeat-monitor.js"(){be(),dN(),pD=class{static{c(this,"HeartbeatMonitor")}constructor(e={}){this._windowSize=e.windowSize||_t.phiAccrualWindowSize,this._threshold=e.threshold||_t.phiAccrualThreshold,this._interval=e.intervalMs||_t.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 rL(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?un.DEAD:s>=this._threshold?un.SUSPECT:un.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)}}},rL=class{static{c(this,"ArrivalWindow")}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}}}}),dD={};gs(dD,{ClusterManager:c(()=>mD,"ClusterManager")});var mD,Hj=tt({"src/distributed/cluster/cluster-manager.js"(){dN(),Kj(),be(),mD=class{static{c(this,"ClusterManager")}constructor(e,t={}){this._localNode=e,this._nodeMap=new Map,this._nodeMap.set(e.nodeId,e),this._failureCallbacks=[],this._joinCallbacks=[],this._heartbeat=new pD({windowSize:t.phiWindowSize||_t.phiAccrualWindowSize,threshold:t.phiThreshold||_t.phiAccrualThreshold,intervalMs:t.heartbeatIntervalMs||_t.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=un.ALIVE,t;e.status=un.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!==un.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!==un.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!==un.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===un.DEAD&&(s.status=un.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===un.DEAD&&n!==un.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}}}}}),gD={};gs(gD,{PartitionMap:c(()=>yD,"PartitionMap")});var yD,Yj=tt({"src/distributed/partition/partition-map.js"(){cN(),yD=class{static{c(this,"PartitionMap")}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!==$a.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}}}}),_D={};gs(_D,{DistributionAwareJoin:c(()=>wD,"DistributionAwareJoin"),DistributionStrategy:c(()=>fn,"DistributionStrategy")});var fn,wD,iL,mN=tt({"src/distributed/optimizer/distribution-aware-join.js"(){Ge(),$t(),Qe(),Ht(),be(),fn={COLOCATED:"colocated",BROADCAST_LEFT:"broadcast_left",BROADCAST_RIGHT:"broadcast_right",SHUFFLE:"shuffle"},wD=class extends Me{static{c(this,"DistributionAwareJoin")}constructor(e,t=new Map){super(),this._partitionMap=e,this._statisticsMap=t,this._costPerByte=_t.networkCostPerByte}get name(){return"DistributionAwareJoin"}apply(e){return new iL(this._partitionMap,this._statisticsMap,this._costPerByte).rewrite(e)}},iL=class extends qe{static{c(this,"DistributionAwareJoinRewriter")}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=fn.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=fn.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:fn.SHUFFLE,cost:f},{strategy:fn.BROADCAST_LEFT,cost:h},{strategy:fn.BROADCAST_RIGHT,cost:p}];return i>_t.broadcastThreshold&&(d[1].cost=1/0),o>_t.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===v.BINARY&&r.op==="="&&r.left?.kind===v.COLUMN_REF&&r.right?.kind===v.COLUMN_REF&&(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===v.BINARY&&e.op==="AND"?[...this._splitAnd(e.left),...this._splitAnd(e.right)]:[e]:[]}_findScanTable(e){return e.type===C.SCAN||e.type===C.INDEX_SCAN?e.table:e.children&&e.children.length>0?this._findScanTable(e.children[0]):null}_estimateCardinality(e){return e._cardinality?e._cardinality:e.type===C.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,_t.defaultPartitionCount)}_isRestrictedJoinType(e){return e===F.LEFT||e===F.SEMI||e===F.ANTI}}}}),bD={};gs(bD,{DECOMPOSABLE_FUNCTIONS:c(()=>ld,"DECOMPOSABLE_FUNCTIONS"),PartialAggregatePass:c(()=>SD,"PartialAggregatePass")});var ld,SD,oL,Jj=tt({"src/distributed/optimizer/partial-aggregate.js"(){Ge(),$t(),Qe(),lc(),ld=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"}]]),SD=class extends Me{static{c(this,"PartialAggregatePass")}get name(){return"PartialAggregate"}apply(e){return e._distributed?new oL().rewrite(e):e}},oL=class extends qe{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=h6(t.groupBy,s,t.children[0]);r._cardinality=t._cardinality;let i=(t.groupBy||[]).map(l=>l),o=f6(ce.HASH_SHUFFLE,i,0,r),a=p6(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 ld.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=ld.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=ld.get(n);return n==="AVG"?{...t,func:r.final,_originalIndex:s,_inputColumns:["_sum","_count"]}:{...t,func:r.final,_originalIndex:s}})}}}}),xD={};gs(xD,{DistributedSortPass:c(()=>AD,"DistributedSortPass")});var AD,aL,Xj=tt({"src/distributed/optimizer/distributed-sort.js"(){Ge(),$t(),Qe(),AD=class extends Me{static{c(this,"DistributedSortPass")}get name(){return"DistributedSort"}apply(e){return e._distributed?new aL().rewrite(e):e}},aL=class extends qe{static{c(this,"DistributedSortRewriter")}rewriteSort(e){let t=this.rewriteChildren(e);if(this._hasExchangeBelow(t)){let s=zw(t.orderKeys,t.children[0]);s._cardinality=t._cardinality;let n=A1(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=A1(t.orderKeys,t.count,s);return n._cardinality=Math.min(t.count||1/0,t._cardinality||1/0),{type:C.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===C.EXCHANGE||s.type===C.PARTIAL_AGGREGATE)return!0;if(s.children)for(let n of s.children)t.push(n)}return!1}}}}),ED,Qj=tt({"src/distributed/planner/exchange-placement.js"(){lc(),mN(),be(),ED=class{static{c(this,"ExchangePlacement")}constructor(e,t){this._partitionMap=e,this._statisticsMap=t||new Map}determineJoinExchange(e){switch(e._distributionStrategy||fn.SHUFFLE){case fn.COLOCATED:return{left:{type:ce.PASSTHROUGH},right:{type:ce.PASSTHROUGH}};case fn.BROADCAST_LEFT:return{left:{type:ce.BROADCAST},right:{type:ce.PASSTHROUGH}};case fn.BROADCAST_RIGHT:return{left:{type:ce.PASSTHROUGH},right:{type:ce.BROADCAST}};case fn.SHUFFLE:default:{let s=this._extractShuffleKeys(e);return{left:{type:ce.HASH_SHUFFLE,keys:s.leftKeys,partitionCount:_t.defaultPartitionCount},right:{type:ce.HASH_SHUFFLE,keys:s.rightKeys,partitionCount:_t.defaultPartitionCount}}}}}determineAggregateExchange(e){let t=e.groupBy||[];return t.length===0?{type:ce.GATHER}:{type:ce.HASH_SHUFFLE,keys:t,partitionCount:_t.defaultPartitionCount}}determineSortExchange(e){return{type:ce.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]:[]}}}}),TD,Zj=tt({"src/distributed/partition/partition-pruner.js"(){Ht(),cN(),TD=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===v.BINARY&&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===v.BINARY&&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===v.BINARY&&e.op==="="?this._pruneEquality(e,t):e.kind===v.BINARY&&(e.op==="<"||e.op==="<="||e.op===">"||e.op===">=")?this._pruneRange(e,t):e.kind===v.BETWEEN?this._pruneBetween(e,t):e.kind===v.IN_LIST?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!==v.LITERAL)return null;if(t.strategy.type===$a.HASH){let r=t.strategy.partitionFor(n.value,t.partitionCount);return new Set([r])}if(t.strategy.type===$a.RANGE){let r=t.strategy.partitionFor(n.value);return new Set([r])}return null}_pruneRange(e,t){if(t.strategy.type!==$a.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!==v.LITERAL)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!==$a.RANGE||!e.expr||e.expr.kind!==v.COLUMN_REF||!this._isPartitionKey(e.expr,t)||!e.low||!e.high||e.low.kind!==v.LITERAL||e.high.kind!==v.LITERAL)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.expr||e.expr.kind!==v.COLUMN_REF||!this._isPartitionKey(e.expr,t)||!e.values||!e.values.every(n=>n.kind===v.LITERAL))return null;let s=new Set;for(let n of e.values)t.strategy.type===$a.HASH?s.add(t.strategy.partitionFor(n.value,t.partitionCount)):t.strategy.type===$a.RANGE&&s.add(t.strategy.partitionFor(n.value));return s}_extractPartitionColumn(e,t){return e.left&&e.left.kind===v.COLUMN_REF&&this._isPartitionKey(e.left,t)?{col:e.left,side:"left"}:e.right&&e.right.kind===v.COLUMN_REF&&this._isPartitionKey(e.right,t)?{col:e.right,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}}}}),ND,tW=tt({"src/distributed/planner/distributed-planner.js"(){$t(),lc(),mN(),Qj(),Zj(),be(),ND=class{static{c(this,"DistributedPlanner")}constructor(e,t,s,n=null){this._partitionMap=e,this._clusterManager=t,this._exchangePlacement=new ED(e,s),this._pruner=new TD,this._catalog=n,this._fragments=[]}fragmentize(e){$P(),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 Gi({planRoot:i,targetNodes:[t],exchangeInputs:o,outputPartitioning:null,estimatedCardinality:e._cardinality||0});return this._fragments.push(a),new lN(this._fragments,a.fragmentId)}_placeGathers(e){if(e.type===C.JOIN&&(e._distributionStrategy===fn.BROADCAST_LEFT||e._distributionStrategy===fn.BROADCAST_RIGHT)){let r=this._buildBroadcastJoin(e);if(r)return r}if(e.type===C.JOIN&&this._isShufflePushableJoin(e)){let r=this._buildShuffleJoin(e);if(r)return r}if(e.type===C.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 Gi({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:ce.GATHER})}return{plan:Fa(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 Gi({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:ce.GATHER})}return{plan:Fa(a,this._deriveSubtreeSchema(e)),exchangeInputs:l}}let t=ds(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:Fn(e,s),exchangeInputs:n}}_isPushableSubtree(e){let t=new Set([C.SCAN,C.INDEX_SCAN,C.FILTER,C.PROJECT]),s=!1,n=[e];for(;n.length>0;){let r=n.pop();if(!r||!t.has(r.type))return!1;(r.type===C.SCAN||r.type===C.INDEX_SCAN)&&this._partitionMap.getTableInfo(r.table)&&(s=!0);for(let i of ds(r))n.push(i)}return s}_findPartitionedScan(e){let t=[e];for(;t.length>0;){let s=t.pop();if((s.type===C.SCAN||s.type===C.INDEX_SCAN)&&this._partitionMap.getTableInfo(s.table))return s;for(let n of ds(s))t.push(n)}return null}_buildBroadcastJoin(e){if(e.joinType!==F.INNER)return null;let t=e._distributionStrategy===fn.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 Gi({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:ce.BROADCAST})),d=Fa(h,this._deriveSubtreeSchema(r)),m=Fn(e,t?[d,i]:[i,d]),g=[],y=[];for(let _ of f){let w=new Gi({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:ce.GATHER})}return{plan:Fa(y,this._joinOutputSchema(e)),exchangeInputs:g}}_isShufflePushableJoin(e){if(e._distributionStrategy!==fn.SHUFFLE)return!1;let t=e.joinType;return t!==F.INNER&&t!==F.LEFT&&t!==F.RIGHT&&t!==F.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,x,N)=>{let I=[];for(let E of x){let T=new Gi({planRoot:A,targetNodes:[E],exchangeInputs:[],outputPartitioning:{exchangeType:"hash_shuffle",partitionCount:p,targetNodes:h,keyColumns:N},estimatedCardinality:0});this._fragments.push(T),I.push(T.fragmentId)}return I},"mkShuffleFrags"),m=d(t,u,i.leftIdx),g=d(s,f,i.rightIdx),y=m.map(A=>({sourceFragmentId:A,exchangeType:ce.HASH_SHUFFLE})),_=g.map(A=>({sourceFragmentId:A,exchangeType:ce.HASH_SHUFFLE})),w=Fn(e,[Fa(m,n),Fa(g,r)]),S=[],b=[];for(let A of h){let x=new Gi({planRoot:w,targetNodes:[A],exchangeInputs:[...y,..._],outputPartitioning:{exchangeType:"gather",partitionCount:p},estimatedCardinality:0});this._fragments.push(x),b.push(x.fragmentId),S.push({sourceFragmentId:x.fragmentId,exchangeType:ce.GATHER})}return{plan:Fa(b,this._joinOutputSchema(e)),exchangeInputs:S}}_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!==fn.COLOCATED)return!1;let t=e.joinType;return t!==F.INNER&&t!==F.LEFT&&t!==F.RIGHT&&t!==F.FULL?!1:this._isScanFilterSubtree(e.children[0])&&this._isScanFilterSubtree(e.children[1])}_isScanFilterSubtree(e){let t=new Set([C.SCAN,C.INDEX_SCAN,C.FILTER]),s=!1,n=[e];for(;n.length>0;){let r=n.pop();if(!r||!t.has(r.type))return!1;(r.type===C.SCAN||r.type===C.INDEX_SCAN)&&this._partitionMap.getTableInfo(r.table)&&(s=!0);for(let i of ds(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===C.FILTER?this._deriveSubtreeSchema(e.children[0]):e.type===C.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 C.SCAN:case C.INDEX_SCAN:return this._processScan(e,t);case C.JOIN:return this._processJoin(e,t);case C.PARTIAL_AGGREGATE:return this._processPartialAggregate(e,t);case C.FINAL_AGGREGATE:return this._processFinalAggregate(e,t);case C.EXCHANGE:return this._processExchange(e,t);case C.MERGE_EXCHANGE: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!==ce.PASSTHROUGH)for(let a of s.exchangeInputs)a.exchangeType=r.left.type,r.left.keys&&(a.keys=r.left.keys);if(r.right.type!==ce.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 Gi({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:ce.GATHER})}return{plan:Fa(o,[]),exchangeInputs:i,workerNodes:[]}}_processMergeExchange(e,t){let s=this._processNode(e.children[0],t),n=s.exchangeInputs.map(i=>({...i,exchangeType:ce.GATHER,ordered:!0,orderKeys:e.orderKeys}));return{plan:{...e,children:[s.plan]},exchangeInputs:n,workerNodes:[]}}_processGeneric(e,t){let s=ds(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]}}}}),RD,eW=tt({"src/distributed/execution/fragment-executor.js"(){$t(),qP(),ZT(),Ww(),Ww(),lc(),RD=class{static{c(this,"FragmentExecutor")}constructor(e,t,s,n,r={}){this._catalog=e,this._tempManager=t,this._transport=s,this._activeFragments=new Map,this._localExecutor=new hN(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 ob(!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(()=>(xd(),tN)),{TaskScheduler:a}=await Promise.resolve().then(()=>(ab(),sN)),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 fN(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 uN(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)}}}}}),vD={};gs(vD,{QueryCoordinator:c(()=>kD,"QueryCoordinator")});var kD,sW=tt({"src/distributed/execution/coordinator.js"(){be(),lc(),tW(),eW(),ZT(),jP(),kD=class{static{c(this,"QueryCoordinator")}constructor(e,t,s,n){this._engine=e,this._clusterManager=t,this._partitionMap=s,this._transport=n,this._activeQueries=new Map,this._fragmentExecutor=new RD(e.catalog,e.tempManager,n,e.storageBackend),this._nextQueryId=1,this._setupFragmentHandler()}async execute(e){let t=this._nextQueryId++,s=Date.now(),n=_t.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 ND(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 Of(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===Js.RUNNING||r.state===Js.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=_t.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=Js.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 ob(!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(()=>(xd(),tN)),{TaskScheduler:l}=await Promise.resolve().then(()=>(ab(),sN)),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=_t.coordinatorTimeoutMs,s=Date.now();for(;e.state!==Js.COMPLETED&&e.state!==Js.FAILED;){if(Date.now()-s>t)throw new Error(`Fragment ${e.fragmentId} timed out`);await new Promise(n=>setTimeout(n,50))}if(e.state===Js.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(()=>(lc(),UP)),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}}}}),CD,nW=tt({"src/distributed/transport/transport.js"(){CD=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()")}}}}),ID={};gs(ID,{HttpTransport:c(()=>OD,"HttpTransport")});var OD,aW=tt({"src/distributed/transport/http-transport.js"(){nW(),be(),OD=class extends CD{static{c(this,"HttpTransport")}constructor(e={}){super(),this._port=e.port||_t.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=rW((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"}),t.end(JSON.stringify({status:"alive",nodeId:this._nodeId}));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 s=JSON.parse(e.toString());for(let n of this._heartbeatCallbacks)n(s)}catch{}t.writeHead(200),t.end(JSON.stringify({ts:Date.now()}))}_handleRegister(e,t){try{let s=JSON.parse(e.toString());for(let n of this._registerCallbacks)n(s);t.writeHead(200),t.end(JSON.stringify({ok:!0}))}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=iW(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 oW({keepAlive:!0,maxSockets:4}),this._agents.set(t,s)),s}}}}),R={IDENT:"IDENT",NUMBER:"NUMBER",STRING:"STRING",COMMA:"COMMA",DOT:"DOT",STAR:"STAR",LPAREN:"LPAREN",RPAREN:"RPAREN",EQ:"EQ",NEQ:"NEQ",LT:"LT",GT:"GT",LTE:"LTE",GTE:"GTE",PLUS:"PLUS",MINUS:"MINUS",SLASH:"SLASH",PERCENT:"PERCENT",SEMICOLON:"SEMICOLON",CONCAT:"CONCAT",COLON:"COLON",EOF:"EOF",EXPLAIN:"EXPLAIN",SELECT:"SELECT",FROM:"FROM",WHERE:"WHERE",AND:"AND",OR:"OR",NOT:"NOT",AS:"AS",ON:"ON",JOIN:"JOIN",INNER:"INNER",LEFT:"LEFT",RIGHT:"RIGHT",FULL:"FULL",OUTER:"OUTER",CROSS:"CROSS",IN:"IN",EXISTS:"EXISTS",BETWEEN:"BETWEEN",LIKE:"LIKE",IS:"IS",NULL:"NULL",TRUE:"TRUE",FALSE:"FALSE",CASE:"CASE",WHEN:"WHEN",THEN:"THEN",ELSE:"ELSE",END:"END",DISTINCT:"DISTINCT",ALL:"ALL",GROUP:"GROUP",BY:"BY",HAVING:"HAVING",ORDER:"ORDER",ASC:"ASC",DESC:"DESC",LIMIT:"LIMIT",OFFSET:"OFFSET",UNION:"UNION",EXCEPT:"EXCEPT",INTERSECT:"INTERSECT",WITH:"WITH",CAST:"CAST",INTERVAL:"INTERVAL",EXTRACT:"EXTRACT",SUBSTRING:"SUBSTRING",TRIM:"TRIM",YEAR:"YEAR",MONTH:"MONTH",DAY:"DAY",DATE:"DATE",SUM:"SUM",AVG:"AVG",COUNT:"COUNT",MIN:"MIN",MAX:"MAX",CREATE:"CREATE",VIEW:"VIEW",NULLS:"NULLS",FIRST:"FIRST",LAST:"LAST",FETCH:"FETCH",NEXT:"NEXT",ROWS:"ROWS",ONLY:"ONLY",SOME:"SOME",ANY:"ANY",LEADING:"LEADING",TRAILING:"TRAILING",BOTH:"BOTH",FOR:"FOR",TIMESTAMP:"TIMESTAMP",HOUR:"HOUR",MINUTE:"MINUTE",SECOND:"SECOND",OVER:"OVER",PARTITION:"PARTITION",RANGE:"RANGE",UNBOUNDED:"UNBOUNDED",PRECEDING:"PRECEDING",FOLLOWING:"FOLLOWING",CURRENT:"CURRENT",ROW:"ROW",NATURAL:"NATURAL",USING:"USING",TABLE:"TABLE",DROP:"DROP",IF:"IF",ANALYZE:"ANALYZE"},lW=new Set(["IDENT","NUMBER","STRING","COMMA","DOT","STAR","LPAREN","RPAREN","EQ","NEQ","LT","GT","LTE","GTE","PLUS","MINUS","SLASH","PERCENT","SEMICOLON","CONCAT","COLON","EOF"]),BD=new Map;for(let e of Object.keys(R))lW.has(e)||BD.set(e,R[e]);var Ve=class{static{c(this,"Token")}constructor(e,t,s){this.type=e,this.value=t,this.position=s}},cW=class{static{c(this,"Lexer")}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 Ve(R.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 Ve(R.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 Ve(R.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=BD.get(s);return n?new Ve(n,s,e):new Ve(R.IDENT,t,e)}_readSymbol(e){let t=this.input[this.pos];switch(this.pos++,t){case",":return new Ve(R.COMMA,",",e);case".":return new Ve(R.DOT,".",e);case"*":return new Ve(R.STAR,"*",e);case"(":return new Ve(R.LPAREN,"(",e);case")":return new Ve(R.RPAREN,")",e);case"+":return new Ve(R.PLUS,"+",e);case"-":return new Ve(R.MINUS,"-",e);case"/":return new Ve(R.SLASH,"/",e);case"%":return new Ve(R.PERCENT,"%",e);case";":return new Ve(R.SEMICOLON,";",e);case":":return new Ve(R.COLON,":",e);case"=":return new Ve(R.EQ,"=",e);case"<":if(this.pos<this.input.length){if(this.input[this.pos]==="=")return this.pos++,new Ve(R.LTE,"<=",e);if(this.input[this.pos]===">")return this.pos++,new Ve(R.NEQ,"<>",e)}return new Ve(R.LT,"<",e);case">":return this.pos<this.input.length&&this.input[this.pos]==="="?(this.pos++,new Ve(R.GTE,">=",e)):new Ve(R.GT,">",e);case"!":if(this.pos<this.input.length&&this.input[this.pos]==="=")return this.pos++,new Ve(R.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 Ve(R.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)}};Zw();var MD=class{static{c(this,"Parser")}constructor(e){let t=new cW(e);this.tokens=t.tokens,this.pos=0}parse(){if(this.isAt(R.CREATE))return this.parseCreateTable();if(this.isAt(R.DROP))return this.parseDropTable();let e=!1,t=!1;this.isAt(R.EXPLAIN)&&(this.advance(),e=!0,this.isAt(R.ANALYZE)&&(this.advance(),t=!0));let s=this.parseQueryExpr();return!this.isAt(R.EOF)&&!this.isAt(R.SEMICOLON)&&this.error(`Unexpected token ${this.peek().type}`),t?X4(s):e?v4(s):s}parseCreateTable(){this.advance(),this.expect(R.TABLE);let e=!1;this.isAt(R.IF)&&(this.advance(),this.expect(R.NOT),this.expect(R.EXISTS),e=!0);let t=this.expectIdent();this.expect(R.LPAREN);let s=[];do{let n=this.expectIdent(),r=this.parseTypeName();s.push(J4(n,r))}while(this.tryConsume(R.COMMA));return this.expect(R.RPAREN),H4(t,s,e)}parseDropTable(){this.advance(),this.expect(R.TABLE);let e=!1;this.isAt(R.IF)&&(this.advance(),this.expect(R.EXISTS),e=!0);let t=this.expectIdent();return Y4(t,e)}parseQueryExpr(){let e=this.parseSelectStmt();for(;this.isAt(R.UNION)||this.isAt(R.EXCEPT)||this.isAt(R.INTERSECT);){let t=this.advance().type,s=this.tryConsume(R.ALL);s||this.tryConsume(R.DISTINCT);let n=this.parseSelectStmt();e=C4(t,e,n,s)}return e}parseSelectStmt(){let e=null;if(this.isAt(R.WITH)&&(e=this.parseWithClause()),this.isAt(R.LPAREN)){this.advance();let f=this.parseQueryExpr();return this.expect(R.RPAREN),f}this.expect(R.SELECT);let t=this.tryConsume(R.DISTINCT);t||this.tryConsume(R.ALL);let s=this.parseSelectItems(),n=null;this.tryConsume(R.FROM)&&(n=this.parseFromClause());let r=null;this.tryConsume(R.WHERE)&&(r=this.parseExpression());let i=null;this.tryConsume(R.GROUP)&&(this.expect(R.BY),i=this.parseExpressionList());let o=null;this.tryConsume(R.HAVING)&&(o=this.parseExpression());let a=null;this.isAt(R.ORDER)&&(this.advance(),this.expect(R.BY),a=this.parseOrderByList());let l=null,u=null;return this.tryConsume(R.LIMIT)&&(l=this.parseExpression(),this.tryConsume(R.OFFSET)&&(u=this.parseExpression())),this.tryConsume(R.FETCH)&&(this.tryConsume(R.FIRST)||this.tryConsume(R.NEXT),l=this.parseExpression(),this.tryConsume(R.ROWS),this.expect(R.ONLY)),k4({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(R.COMMA));return I4(e)}parseCTE(){let e=this.expectIdent(),t=null;if(this.isAt(R.LPAREN)&&!this.isLookaheadSelect()){this.advance(),t=[];do t.push(this.expectIdent());while(this.tryConsume(R.COMMA));this.expect(R.RPAREN)}this.expect(R.AS),this.expect(R.LPAREN);let s=this.parseQueryExpr();return this.expect(R.RPAREN),O4(e,s,t)}parseSelectItems(){let e=[];do e.push(this.parseSelectItem());while(this.tryConsume(R.COMMA));return e}parseSelectItem(){if(this.isAt(R.STAR))return this.advance(),BE(hw());if(this.isAt(R.IDENT)&&this.peekAhead(1)?.type===R.DOT&&this.peekAhead(2)?.type===R.STAR){let s=this.advance().value;return this.advance(),this.advance(),BE(hw(s))}let e=this.parseExpression(),t=null;return this.tryConsume(R.AS)?t=this.expectIdent():this.isAt(R.IDENT)&&(t=this.expectIdent()),BE(e,t)}parseFromClause(){let e=this.parseTableRef();for(;this.isJoinKeyword();)e=this.parseJoin(e);for(;this.tryConsume(R.COMMA);){let t=this.parseTableRef();e=ME(e,t,"CROSS")}return e}parseTableRef(){if(this.isAt(R.LPAREN)){if(this.isSubqueryStart())return this.parseSubqueryRef();this.advance();let s=this.parseFromClause();return this.expect(R.RPAREN),s}let e=this.expectIdent(),t=e;return this.tryConsume(R.AS)?t=this.expectIdent():this.isAt(R.IDENT)&&!this.isJoinKeyword()&&!this.isClauseKeyword()&&(t=this.expectIdent()),B4(e,t)}parseSubqueryRef(){this.expect(R.LPAREN);let e=this.parseQueryExpr();this.expect(R.RPAREN);let t=null;return this.tryConsume(R.AS)?t=this.expectIdent():this.isAt(R.IDENT)&&(t=this.expectIdent()),M4(e,t)}parseJoin(e){let t="INNER",s=!1;this.tryConsume(R.NATURAL)&&(s=!0),this.tryConsume(R.LEFT)?(this.tryConsume(R.OUTER),t="LEFT"):this.tryConsume(R.RIGHT)?(this.tryConsume(R.OUTER),t="RIGHT"):this.tryConsume(R.FULL)?(this.tryConsume(R.OUTER),t="FULL"):this.tryConsume(R.CROSS)?t="CROSS":this.tryConsume(R.INNER)&&(t="INNER"),this.expect(R.JOIN);let n=this.parseTableRef(),r=null,i=null;if(s){let a=ME(e,n,t,null);return a.natural=!0,a}if(t!=="CROSS"){if(this.tryConsume(R.ON))r=this.parseExpression();else if(this.tryConsume(R.USING)){this.expect(R.LPAREN),i=[];do i.push(this.expectIdent());while(this.tryConsume(R.COMMA));this.expect(R.RPAREN)}}let o=ME(e,n,t,r);return i&&(o.usingColumns=i),o}parseExpression(){return this.parseOr()}parseOr(){let e=this.parseAnd();for(;this.isAt(R.OR);){this.advance();let t=this.parseAnd();e=kf("OR",e,t)}return e}parseAnd(){let e=this.parseNot();for(;this.isAt(R.AND);){this.advance();let t=this.parseNot();e=kf("AND",e,t)}return e}parseNot(){if(this.isAt(R.NOT)){this.advance();let e=this.parseNot();return LE("NOT",e)}return this.parseComparison()}parseComparison(){if(this.isAt(R.EXISTS))return this.parseExists();let e=this.parseAddition();if(this.isAt(R.NOT)){let s=this.pos;if(this.advance(),this.isAt(R.BETWEEN))return this.parseBetween(e,!0);if(this.isAt(R.IN))return this.parseIn(e,!0);if(this.isAt(R.LIKE))return this.parseLike(e,!0);this.pos=s}if(this.isAt(R.BETWEEN))return this.parseBetween(e,!1);if(this.isAt(R.IN))return this.parseIn(e,!1);if(this.isAt(R.LIKE))return this.parseLike(e,!1);if(this.isAt(R.IS)){this.advance();let s=this.tryConsume(R.NOT);return this.expect(R.NULL),D4(e,!!s)}let t={[R.EQ]:"=",[R.NEQ]:"<>",[R.LT]:"<",[R.GT]:">",[R.LTE]:"<=",[R.GTE]:">="};if(t[this.peek().type]){let s=t[this.advance().type];if(this.isAt(R.ALL)||this.isAt(R.SOME)||this.isAt(R.ANY)){let r=this.advance().type;this.expect(R.LPAREN);let i=this.parseQueryExpr();return this.expect(R.RPAREN),kf(s,e,{kind:"QuantifiedSubquery",quantifier:r,query:i})}let n=this.parseAddition();return kf(s,e,n)}return e}parseBetween(e,t){this.advance();let s=this.parseAddition();this.expect(R.AND);let n=this.parseAddition();return L4(e,s,n,t)}parseIn(e,t){if(this.advance(),this.expect(R.LPAREN),this.isAt(R.SELECT)||this.isAt(R.WITH)){let n=this.parseQueryExpr();return this.expect(R.RPAREN),g1(e,_1(n),t)}let s=this.parseExpressionList();return this.expect(R.RPAREN),g1(e,s,t)}parseLike(e,t){this.advance();let s=this.parseAddition();return P4(e,s,t)}parseExists(){this.advance(),this.expect(R.LPAREN);let e=this.parseQueryExpr();return this.expect(R.RPAREN),y1(e,!1)}parseAddition(){let e=this.parseMultiplication();for(;this.isAt(R.PLUS)||this.isAt(R.MINUS)||this.isAt(R.CONCAT);){let t=this.advance().value,s=this.parseMultiplication();e=kf(t,e,s)}return e}parseMultiplication(){let e=this.parseUnary();for(;this.isAt(R.STAR)||this.isAt(R.SLASH)||this.isAt(R.PERCENT);){let t=this.advance().value,s=this.parseUnary();e=kf(t,e,s)}return e}parseUnary(){if(this.isAt(R.MINUS)){this.advance();let e=this.parseUnary();return LE("-",e)}return this.isAt(R.PLUS)?(this.advance(),this.parseUnary()):this.parsePrimary()}parsePrimary(){let e=this.peek();if(e.type===R.NUMBER)return this.advance(),vo(e.value.includes(".")?parseFloat(e.value):parseInt(e.value,10));if(e.type===R.STRING)return this.advance(),vo(e.value,"VARCHAR");if(e.type===R.NULL)return this.advance(),vo(null);if(e.type===R.TRUE)return this.advance(),vo(!0,"BOOLEAN");if(e.type===R.FALSE)return this.advance(),vo(!1,"BOOLEAN");if(e.type===R.DATE){this.advance();let t=this.expect(R.STRING);return vo(t.value,"DATE")}if(e.type===R.TIMESTAMP){this.advance();let t=this.expect(R.STRING);return vo(t.value,"TIMESTAMP")}if(e.type===R.INTERVAL)return this.parseInterval();if(e.type===R.CASE)return this.parseCaseExpr();if(e.type===R.CAST)return this.parseCast();if(e.type===R.EXTRACT)return this.parseExtract();if(e.type===R.SUBSTRING)return this.parseSubstringFn();if(e.type===R.TRIM)return this.parseTrim();if(e.type===R.EXISTS)return this.parseExists();if(e.type===R.NOT){if(this.advance(),this.isAt(R.EXISTS)){this.advance(),this.expect(R.LPAREN);let s=this.parseQueryExpr();return this.expect(R.RPAREN),y1(s,!0)}let t=this.parsePrimary();return LE("NOT",t)}if(e.type===R.LPAREN){if(this.isSubqueryStart()){this.advance();let s=this.parseQueryExpr();return this.expect(R.RPAREN),_1(s)}this.advance();let t=this.parseExpression();return this.expect(R.RPAREN),t}if(e.type===R.STAR)return this.advance(),hw();if(this.isAggregateKeyword(e.type))return this.parseAggregateCall();if(e.type===R.COLON){this.advance();let t=this.expect(R.NUMBER);return vo(`:${t.value}`,"PARAM")}if(e.type===R.IDENT){let t=this.advance().value;if(this.isAt(R.LPAREN))return this.parseFunctionCallNamed(t);if(this.isAt(R.DOT)){if(this.advance(),this.isAt(R.STAR))return this.advance(),hw(t);let s=this.expectIdent();return m1(s,t)}return m1(t)}this.error(`Unexpected token ${e.type} (${e.value})`)}parseAggregateCall(){let e=this.advance().value;if(this.expect(R.LPAREN),e==="COUNT"&&this.isAt(R.STAR))return this.advance(),this.expect(R.RPAREN),this.isAt(R.OVER)?this.parseWindowCall("COUNT_STAR",[]):b1("COUNT_STAR",[],!1);let t=this.tryConsume(R.DISTINCT),s=this.parseExpressionList();return this.expect(R.RPAREN),this.isAt(R.OVER)?this.parseWindowCall(e,s):b1(e,s,!!t)}parseFunctionCallNamed(e){this.expect(R.LPAREN);let t=[];return this.isAt(R.RPAREN)||(t=this.parseExpressionList()),this.expect(R.RPAREN),this.isAt(R.OVER)?this.parseWindowCall(e,t):w1(e,t)}parseWindowCall(e,t){this.expect(R.OVER),this.expect(R.LPAREN);let s=[];this.tryConsume(R.PARTITION)&&(this.expect(R.BY),s=this.parseExpressionList());let n=[];this.isAt(R.ORDER)&&(this.advance(),this.expect(R.BY),n=this.parseOrderByList()),this.expect(R.RPAREN);let r=W4(s,n);return K4(e,t,r)}parseCaseExpr(){this.advance();let e=null;this.isAt(R.WHEN)||(e=this.parseExpression());let t=[];for(;this.tryConsume(R.WHEN);){let n=this.parseExpression();this.expect(R.THEN);let r=this.parseExpression();t.push(U4(n,r))}let s=null;return this.tryConsume(R.ELSE)&&(s=this.parseExpression()),this.expect(R.END),F4(e,t,s)}parseCast(){this.advance(),this.expect(R.LPAREN);let e=this.parseExpression();this.expect(R.AS);let t=this.parseTypeName();return this.expect(R.RPAREN),$4(e,t)}parseExtract(){this.advance(),this.expect(R.LPAREN);let e=this.peek().value;this.advance(),this.expect(R.FROM);let t=this.parseExpression();return this.expect(R.RPAREN),z4(e.toUpperCase(),t)}parseSubstringFn(){this.advance(),this.expect(R.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(R.COMMA),t=this.parseExpression(),this.tryConsume(R.COMMA)&&(s=this.parseExpression())),this.expect(R.RPAREN),V4(e,t,s)}parseTrim(){this.advance(),this.expect(R.LPAREN);let e=null;(this.isAt(R.LEADING)||this.isAt(R.TRAILING)||this.isAt(R.BOTH))&&(e=this.advance().value);let t=null;!this.isAt(R.FROM)&&e!==null&&(this.isAt(R.FROM)||(t=this.parseExpression())),e!==null&&this.tryConsume(R.FROM);let s=this.parseExpression();this.expect(R.RPAREN);let n=t?[s,vo(e),t]:[s];return w1("TRIM",n)}parseInterval(){this.advance();let e=this.expect(R.STRING),t=this.expectIdent().toUpperCase();return G4(e.value,t)}parseTypeName(){let e=this.expectIdent().toUpperCase(),t=[];if(this.tryConsume(R.LPAREN)){do t.push(parseInt(this.expect(R.NUMBER).value,10));while(this.tryConsume(R.COMMA));this.expect(R.RPAREN)}return j4(e,t)}parseExpressionList(){let e=[];do e.push(this.parseExpression());while(this.tryConsume(R.COMMA));return e}parseOrderByList(){let e=[];do{let t=this.parseExpression(),s="ASC";this.tryConsume(R.ASC)?s="ASC":this.tryConsume(R.DESC)&&(s="DESC");let n=null;this.tryConsume(R.NULLS)&&(this.tryConsume(R.FIRST)?n="FIRST":this.tryConsume(R.LAST)&&(n="LAST")),e.push(q4(t,s,n))}while(this.tryConsume(R.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===R.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[R.YEAR,R.MONTH,R.DAY,R.DATE,R.FIRST,R.LAST,R.NULLS,R.VIEW,R.ROWS,R.ONLY,R.NEXT,R.FETCH,R.SOME,R.ANY,R.LEADING,R.TRAILING,R.BOTH,R.SUM,R.AVG,R.COUNT,R.MIN,R.MAX,R.SUBSTRING,R.EXTRACT,R.TRIM,R.OFFSET,R.FOR,R.ASC,R.DESC,R.LEFT,R.RIGHT,R.FULL,R.INNER,R.OUTER,R.CROSS,R.JOIN,R.CREATE,R.INTERVAL,R.UNION,R.ALL,R.TIMESTAMP,R.HOUR,R.MINUTE,R.SECOND,R.OVER,R.PARTITION,R.RANGE,R.UNBOUNDED,R.PRECEDING,R.FOLLOWING,R.CURRENT,R.ROW,R.NATURAL,R.USING,R.TABLE,R.DROP,R.IF,R.ANALYZE].includes(e)}isAggregateKeyword(e){return[R.SUM,R.AVG,R.COUNT,R.MIN,R.MAX].includes(e)}isJoinKeyword(){let e=this.peek().type;return e===R.JOIN||e===R.INNER||e===R.LEFT||e===R.RIGHT||e===R.FULL||e===R.CROSS||e===R.NATURAL}isClauseKeyword(){let e=this.peek().type;return e===R.WHERE||e===R.GROUP||e===R.HAVING||e===R.ORDER||e===R.LIMIT||e===R.UNION||e===R.EXCEPT||e===R.INTERSECT||e===R.ON||e===R.FETCH}isSubqueryStart(){let e=0;for(let t=this.pos;t<this.tokens.length&&(this.tokens[t].type===R.LPAREN&&e++,this.tokens[t].type===R.RPAREN&&e--,!(e===0&&t>this.pos));t++)if(e===1&&(this.tokens[t].type===R.SELECT||this.tokens[t].type===R.WITH))return!0;return!1}isLookaheadSelect(){let e=1;for(let t=this.pos+1;t<this.tokens.length&&(this.tokens[t].type===R.LPAREN&&e++,!(this.tokens[t].type===R.RPAREN&&(e--,e===0)));t++)if(e===1&&this.tokens[t].type===R.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 uW(e){return new MD(e).parse()}c(uW,"parse");function fW(e){let t=new MD(e),s=t.parseExpression();return!t.isAt(R.EOF)&&!t.isAt(R.SEMICOLON)&&t.error(`Unexpected token ${t.peek().type}`),s}c(fW,"parseExpression");Zw();rs();var LD=class PD{static{c(this,"_BinderScope")}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 PD(this)}};Ht();var CT=class{static{c(this,"Binder")}constructor(e,t){this.catalog=e,this.functionRegistry=t,this.cteScopes=new Map,this.aggregatesFound=[]}bind(e){let t=new LD;return this.bindQuery(e,t)}bindQuery(e,t){return e.kind===pt.SET_OP?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===pt.COLUMN_REF&&!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:bn(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 pt.TABLE_REF:return this.bindTableRef(e,t);case pt.JOIN_REF:return this.bindJoinRef(e,t);case pt.SUBQUERY_REF: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=Dn("=",za(o.tableAlias,o.column.name,o.columnIndex,o.column.dataType),za(a.tableAlias,a.column.name,a.columnIndex,a.column.dataType),L.BOOLEAN);r=r?Dn("AND",r,l,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===pt.ALL_COLUMNS){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:za(n.tableAlias,n.column.name,n.columnIndex,n.column.dataType),alias:null,inferredName:n.column.name}))}return t.getAllColumns().map(s=>({expr:za(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 pt.COLUMN_REF:return this.bindColumnRef(e,t);case pt.LITERAL:return this.bindLiteral(e);case pt.BINARY_EXPR:return this.bindBinaryExpr(e,t);case pt.UNARY_EXPR:return this.bindUnaryExpr(e,t);case pt.AGGREGATE_CALL:return this.bindAggregateCall(e,t);case pt.FUNCTION_CALL:return this.bindFunctionCall(e,t);case pt.CASE_EXPR:return this.bindCaseExpr(e,t);case pt.CAST_EXPR:return this.bindCastExpr(e,t);case pt.BETWEEN_EXPR:return FL(this.bindExpression(e.expr,t),this.bindExpression(e.low,t),this.bindExpression(e.high,t),e.negated);case pt.IN_EXPR:return this.bindInExpr(e,t);case pt.LIKE_EXPR:return UL(this.bindExpression(e.expr,t),this.bindExpression(e.pattern,t),e.negated);case pt.IS_NULL_EXPR:return uT(this.bindExpression(e.expr,t),e.negated);case pt.EXISTS_EXPR:return this.bindExistsExpr(e,t);case pt.SUBQUERY_EXPR:return this.bindSubqueryExpr(e,t);case pt.EXTRACT_EXPR:return r6(e.field,this.bindExpression(e.source,t));case pt.SUBSTRING_EXPR:return cT("SUBSTRING",[this.bindExpression(e.expr,t),this.bindExpression(e.from,t),e.length?this.bindExpression(e.length,t):null].filter(Boolean),L.VARCHAR);case pt.INTERVAL_EXPR:return i6(parseInt(e.value,10),e.unit);case pt.WINDOW_CALL:return this.bindWindowCall(e,t);case pt.ALL_COLUMNS: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 o6(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 za(s.tableAlias,s.column.name,s.columnIndex,s.column.dataType,s.depth)}bindLiteral(e){if(e.value===null)return de(null,null);if(e.dataType==="DATE"){let[t,s,n]=e.value.split("-").map(Number);return de(gd(t,s,n),L.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 de(t6(s,n,r,i,o,a,l),L.TIMESTAMP)}return e.dataType==="BOOLEAN"?de(e.value,L.BOOLEAN):e.dataType==="VARCHAR"?de(e.value,L.VARCHAR):typeof e.value=="number"?Number.isInteger(e.value)?de(e.value,L.INT32):de(e.value,L.FLOAT64):de(e.value,L.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 Dn(r,s,n,L.BOOLEAN);if(["||"].includes(r))return Dn(r,s,n,L.VARCHAR);let i=this.inferArithmeticType(bn(s),bn(n));return Dn(r,s,n,i)}bindUnaryExpr(e,t){let s=this.bindExpression(e.operand,t);return e.op==="NOT"?lT("NOT",s,L.BOOLEAN):lT(e.op,s,bn(s))}bindAggregateCall(e,t){let s=e.args.map(i=>this.bindExpression(i,t)),n=this.inferAggregateType(e.name,s),r=tb(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 cT(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=bn(n[0]?.result)||L.VARCHAR;return s6(s,n,r,i)}bindCastExpr(e,t){let s=this.bindExpression(e.expr,t),n=this.resolveTypeName(e.targetType);return DL(s,n)}bindInExpr(e,t){let s=this.bindExpression(e.expr,t);if(e.list.kind===pt.SUBQUERY_EXPR){let r=t.child(),i=this.bindQuery(e.list.query,r);return Uw(s,x1(i,"IN"),e.negated)}let n=e.list.map(r=>this.bindExpression(r,t));return Uw(s,n,e.negated)}bindExistsExpr(e,t){let s=t.child(),n=this.bindQuery(e.query,s);return n6(n,e.negated)}bindSubqueryExpr(e,t){let s=t.child(),n=this.bindQuery(e.query,s);return x1(n,"SCALAR")}inferColumnName(e){return e.kind===pt.COLUMN_REF?e.name:e.kind===pt.AGGREGATE_CALL||e.kind===pt.FUNCTION_CALL?e.name.toLowerCase():null}inferArithmeticType(e,t){return e===L.FLOAT64||t===L.FLOAT64?L.FLOAT64:e===L.DECIMAL||t===L.DECIMAL?L.DECIMAL:e===L.INT64||t===L.INT64?L.INT64:e===L.DATE?L.DATE:L.INT32}inferAggregateType(e,t){switch(e.toUpperCase()){case"COUNT":case"COUNT_STAR":return L.INT64;case"SUM":return t[0]&&bn(t[0])||L.FLOAT64;case"AVG":return L.FLOAT64;case"MIN":case"MAX":return t[0]&&bn(t[0])||L.FLOAT64;default:return L.FLOAT64}}inferFunctionType(e,t){switch(e.toUpperCase()){case"SUBSTRING":case"TRIM":case"UPPER":case"LOWER":case"REPLACE":return L.VARCHAR;case"EXTRACT":return L.INT32;case"LENGTH":return L.INT32;case"ABS":case"ROUND":case"SQRT":return t[0]?bn(t[0]):L.FLOAT64;case"COALESCE":case"NULLIF":return t[0]?bn(t[0]):null;default:return L.VARCHAR}}inferWindowType(e,t){switch(e.toUpperCase()){case"ROW_NUMBER":case"RANK":case"DENSE_RANK":return L.INT64;case"LAG":case"LEAD":return t[0]?bn(t[0]):L.VARCHAR;case"SUM":case"AVG":case"MIN":case"MAX":case"COUNT":case"COUNT_STAR":return this.inferAggregateType(e,t);default:return L.FLOAT64}}resolveTypeName(e){let t=e.name.toUpperCase();return{INTEGER:L.INT32,INT:L.INT32,INT32:L.INT32,BIGINT:L.INT64,INT64:L.INT64,FLOAT:L.FLOAT64,DOUBLE:L.FLOAT64,REAL:L.FLOAT64,DECIMAL:L.DECIMAL,NUMERIC:L.DECIMAL,VARCHAR:L.VARCHAR,TEXT:L.VARCHAR,CHAR:L.VARCHAR,DATE:L.DATE,TIMESTAMP:L.TIMESTAMP,DATETIME:L.TIMESTAMP,BOOLEAN:L.BOOLEAN,BOOL:L.BOOLEAN}[t]||L.VARCHAR}};$t();Ht();var hW=0;function lL(e){let t=e.alias||e.inferredName;return t?{...e.expr,outputName:t}:e.expr}c(lL,"projectionExpr");var pW=class{static{c(this,"LogicalPlanner")}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 VL(t,s,e.all)}planSelect(e){let t=null;if(e.plan?t=this.planFrom(e.plan):t=d6(),e.where){let{expr:n,subqueryJoins:r}=this.extractSubqueries(e.where,t);for(let i of r)t=i(t);n&&(t=Sn(n,t))}if((e.aggregates.length>0||e.groupBy)&&(t=yd(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=Sn(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=c6(s,t)),e.distinct){let n=e.selectItems.map(lL);t=$w(n,t),t=zL(t),e.orderBy&&(t=zw(e.orderBy,t))}else{e.orderBy&&(t=zw(e.orderBy,t));let n=e.selectItems.map(lL);t=$w(n,t)}if(e.limit){let n=e.limit.value,r=e.offset?e.offset.value:0;t=$L(n,r,t)}return t}planFrom(e){switch(e.type){case"TableRef":return fT(e.tableName,e.columns,e.alias);case"CTERef":{let t=hW++,s=e.query.prebuiltPlan??this.planQuery(e.query);return this.cteMap.set(e.cteName.toUpperCase(),s),a6(e.cteName,t)}case"JoinRef":{let t=this.planFrom(e.left),s=this.planFrom(e.right),n=e.joinType==="CROSS"?F.CROSS:F[e.joinType];return Vr(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===v.UNARY&&r.op==="NOT"&&r.operand?.kind===v.EXISTS){let i=this.planQuery(r.operand.plan),o=this.findCorrelatedRefs(r.operand.plan);return s.push(a=>pw(a,i,o,"NOT_EXISTS",null)),null}if(r.kind===v.EXISTS){let i=this.planQuery(r.plan),o=this.findCorrelatedRefs(r.plan),a=r.negated?"NOT_EXISTS":"EXISTS";return s.push(l=>pw(l,i,o,a,null)),null}if(r.kind===v.IN_LIST&&r.list?.kind===v.SUBQUERY){let i=this.planQuery(r.list.plan),o=this.findCorrelatedRefs(r.list.plan),a=r.negated?"NOT_IN":"IN";return s.push(l=>pw(l,i,o,a,r.expr)),null}if(r.kind===v.SUBQUERY&&r.subqueryType==="SCALAR"){let i=this.planQuery(r.plan),o=this.findCorrelatedRefs(r.plan);return s.push(a=>pw(a,i,o,"SCALAR",null)),{kind:v.COLUMN_REF,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 v.BINARY:return{...e,left:this.walkAndReplace(e.left,t),right:this.walkAndReplace(e.right,t)};case v.UNARY:return{...e,operand:this.walkAndReplace(e.operand,t)};case v.CASE: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 v.BETWEEN: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){if(e.kind===v.WINDOW){t.push(e);return}if(e.left&&this._collectWindows(e.left,t),e.right&&this._collectWindows(e.right,t),e.operand&&this._collectWindows(e.operand,t),e.args)for(let s of e.args)this._collectWindows(s,t);if(e.whenClauses)for(let s of e.whenClauses)this._collectWindows(s.condition,t),this._collectWindows(s.result,t);e.elseExpr&&this._collectWindows(e.elseExpr,t)}}findCorrelatedRefs(e){let t=[];return this._scanForCorrelated(e,t),t}_scanForCorrelated(e,t){if(!(!e||typeof e!="object")){if(e.kind===v.COLUMN_REF&&e.isCorrelated){t.push(e);return}for(let s of Object.values(e))if(Array.isArray(s))for(let n of s)this._scanForCorrelated(n,t);else s&&typeof s=="object"&&this._scanForCorrelated(s,t)}}};function dW(e){let t=new pW,s=t.plan(e);return s._cteMap=t.cteMap,s}c(dW,"createLogicalPlan");var Sw={SCALAR:"SCALAR",AGGREGATE:"AGGREGATE"},mW=class{static{c(this,"FunctionRegistry")}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===Sw.AGGREGATE}isScalar(e){return this.lookup(e)?.type===Sw.SCALAR}_registerBuiltins(){let e=c((s,n=1,r=1)=>({name:s.toUpperCase(),type:Sw.AGGREGATE,minArgs:n,maxArgs:r}),"agg"),t=c((s,n,r)=>({name:s.toUpperCase(),type:Sw.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))}},gW=new mW;Zw();rs();Xe();Xe();Te();var yW=class{static{c(this,"LRUCache")}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}},_W=class{static{c(this,"BufferPoolManager")}constructor(e,t){this.maxPages=e,this.cache=new yW(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}};be();var wW=class{static{c(this,"Table")}constructor(e,t,s){this.name=e,this.schema=t,this.pageIds=[],this._rowCount=0,this.bufferPool=new _W(_t.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>=ms&&(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 It.fromSchema(this.schema,ms)}},bW=class{static{c(this,"Optimizer")}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)}};Ge();$t();Qe();Ht();Ge();$t();Qe();Ht();var xw=class extends Me{static{c(this,"PredicatePushdown")}get name(){return"PredicatePushdown"}apply(e){return new SW().rewrite(e)}},SW=class extends qe{static{c(this,"PushdownRewriter")}rewriteJoin(e){let t=this.rewriteChildren(e);return xW(t)}rewriteFilter(e){let t=this.rewrite(e.children[0]),s=Un(e.condition);return nc(s,t)}rewriteDefault(e){return this.rewriteChildren(e)}};function xW(e){if(!e.condition)return e;let t=Hw(e.children[1]),s=[],n=[];for(let o of Un(e.condition)){let a=gN(o),l=a.length>0&&a.every(u=>Yw(u,t));e.joinType===F.LEFT&&l?s.push(o):n.push(o)}if(s.length===0)return e;let r=e.children[1];return s.length>0&&(r=nc(s,r)),{...Vr(e.joinType===F.CROSS&&n.length>0?F.INNER:e.joinType,Ds(n),e.children[0],r),...AW(e)}}c(xW,"pushJoinConditionPredicates");function AW(e){let t={};e.markColumn&&(t.markColumn=e.markColumn);for(let s of Object.keys(e))s.startsWith("_")&&(t[s]=e[s]);return t}c(AW,"copyJoinProperties");function nc(e,t){if(t.type===C.JOIN)return TW(e,t);if(t.type===C.FILTER){let n=[...Un(t.condition),...e],r=t.children[0];return nc(n,r)}if(t.type===C.AGGREGATE&&t.groupBy&&t.groupBy.length>0){let s=new Set;for(let i of t.groupBy)i.kind===v.COLUMN_REF&&s.add(`${(i.tableAlias||"").toUpperCase()}.${(i.columnName||"").toUpperCase()}`);let n=[],r=[];for(let i of e){let o=gN(i);o.length>0&&!RW(i)&&o.every(a=>s.has(`${a.tableAlias}.${a.columnName}`))?n.push(i):r.push(i)}if(n.length>0){let i=nc(n,t.children[0]),o={...t,children:[i]};return r.length===0?o:Sn(Ds(r),o)}}if(t.type===C.PROJECT){let s=[],n=[];for(let r of e)EW(r,t)?s.push(r):n.push(r);if(s.length>0){let r=nc(s,t.children[0]),i={...t,children:[r]};return n.length===0?i:Sn(Ds(n),i)}}return e.length===0?t:Sn(Ds(e),t)}c(nc,"pushPredicates");function EW(e,t){let s=new Set;wn(e,r=>{r.kind===v.COLUMN_REF&&s.add({tableAlias:(r.tableAlias||"").toUpperCase(),columnName:(r.columnName||"").toUpperCase()})});let n=Hw(t.children[0]);for(let r of s)if(!Yw(r,n))return!1;return!0}c(EW,"canPushThroughProject");function TW(e,t){let s=Hw(t.children[0]),n=Hw(t.children[1]),r=[],i=[],o=[],a=[];for(let p of e){let d=gN(p),m=d.every(y=>Yw(y,s)),g=d.every(y=>Yw(y,n));t.joinType===F.INNER||t.joinType===F.CROSS?m?r.push(p):g?i.push(p):o.push(p):t.joinType===F.LEFT?m?r.push(p):g&&NW(p)?(i.push(p),t={...t,joinType:F.INNER}):a.push(p):t.joinType===F.SEMI||t.joinType===F.ANTI||t.joinType===F.MARK?m?r.push(p):g?i.push(p):a.push(p):a.push(p)}let l=t.children[0],u=t.children[1];r.length>0&&(l=nc(r,l)),i.length>0&&(u=nc(i,u));let f=t.condition;if(o.length>0){let p=f?[f,...o]:o;f=Ds(p)}let h=Vr(t.joinType===F.CROSS&&f?F.INNER:t.joinType,f,l,u);return t.markColumn&&(h.markColumn=t.markColumn),a.length>0&&(h=Sn(Ds(a),h)),h}c(TW,"pushIntoJoin");function NW(e){return e.kind===v.BINARY?["=","<>","<",">","<=",">="].includes(e.op):!(e.kind===v.IS_NULL&&!e.negated)}c(NW,"rejectsNulls");function Un(e){return e?e.kind===v.BINARY&&e.op==="AND"?[...Un(e.left),...Un(e.right)]:[e]:[]}c(Un,"splitConjuncts");function Ds(e){return e.length===0?null:e.length===1?e[0]:e.reduce((t,s)=>({kind:v.BINARY,op:"AND",left:t,right:s,resultType:"BOOLEAN"}))}c(Ds,"combineConjuncts");function Hw(e){let t={aliases:new Set,columns:new Set};return IT(e,t),t.aliases.delete(""),t.columns.delete(""),t}c(Hw,"collectPlanRefs");function IT(e,t){if(e){if(e.type===C.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===C.CTE_SCAN){t.aliases.add((e.alias||e.cteName||"").toUpperCase());return}if(e.type===C.PROJECT){for(let s of e.expressions||[])t.columns.add((s.outputName||s.alias||s.name||s.columnName||"").toUpperCase());return}if(e.type===C.AGGREGATE){for(let s of e.groupBy||[])t.columns.add((s.outputName||s.alias||s.name||s.columnName||"").toUpperCase());for(let s of e.aggregates||[])t.columns.add((s.outputName||s.alias||s.name||"").toUpperCase());return}if(e.type===C.JOIN||e.type===C.UNION){for(let s of ds(e))IT(s,t);return}e.children?.[0]&&IT(e.children[0],t)}}c(IT,"addOutputRefs");function Yw(e,t){return e.tableAlias?t.aliases.has(e.tableAlias):t.columns.has(e.columnName)}c(Yw,"refBelongsToPlan");function RW(e){let t=!1;return wn(e,s=>{s.kind===v.AGGREGATE&&(t=!0)}),t}c(RW,"containsAggregate");function gN(e){let t=new Set;return wn(e,s=>{s.kind===v.COLUMN_REF&&t.add(`${(s.tableAlias||"").toUpperCase()}.${(s.columnName||"").toUpperCase()}`)}),[...t].map(s=>{let[n,r]=s.split(".");return{tableAlias:n,columnName:r}})}c(gN,"collectTableRefs");function wn(e,t){if(!(!e||typeof e!="object")){if(t(e),e.left&&wn(e.left,t),e.right&&wn(e.right,t),e.operand&&wn(e.operand,t),e.expr&&wn(e.expr,t),e.low&&wn(e.low,t),e.high&&wn(e.high,t),e.args)for(let s of e.args)wn(s,t);if(e.whenClauses)for(let s of e.whenClauses)wn(s.condition,t),wn(s.result,t);if(e.elseExpr&&wn(e.elseExpr,t),e.list&&Array.isArray(e.list))for(let s of e.list)wn(s,t);e.pattern&&wn(e.pattern,t),e.source&&wn(e.source,t)}}c(wn,"_walkExpr");var vW=class extends Me{static{c(this,"SubqueryUnnesting")}get name(){return"SubqueryUnnesting"}apply(e){let t=e,s=!0;for(;s;){let n=new kW,r=n.rewrite(t);s=n.didChange,t=r}return t}},kW=class extends qe{static{c(this,"UnnestingRewriter")}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 Fn(e,[t,s])}}unnestExists(e,t,s){let{cleanedPlan:n,joinCondition:r}=this.extractCorrelation(t,s);return Vr(F.SEMI,r,e,this.removeProjection(n))}unnestNotExists(e,t,s){let{cleanedPlan:n,joinCondition:r}=this.extractCorrelation(t,s);return Vr(F.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:v.BINARY,op:"=",left:n,right:a,resultType:"BOOLEAN"})}return Vr(F.SEMI,Ds(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:v.BINARY,op:"=",left:n,right:f,resultType:"BOOLEAN"})}let a=`__mark_${this.markId++}`,l={kind:v.COLUMN_REF,tableAlias:"",columnName:a,columnIndex:-1,dataType:"BOOLEAN",depth:0,isCorrelated:!1},u={...Vr(F.MARK,Ds(o),e,this.exposeCorrelationColumns(r)),markColumn:a};return Sn({kind:v.BINARY,op:"=",left:l,right:{kind:v.LITERAL,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 Vr(F.LEFT,r,e,f)}return Vr(F.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:Ds(r),correlatedPredicates:s}}removeCorrelatedPredicates(e,t,s){if(!e)return e;if(e.type===C.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:Sn(Ds(l),o)}if(e.type===C.PROJECT){let o=this.removeCorrelatedPredicates(e.children[0],t,s);return Fn(e,[o])}let n=ds(e),r=n.map(o=>this.removeCorrelatedPredicates(o,t,s));return r.some((o,a)=>o!==n[a])?Fn(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;if(e.kind===v.COLUMN_REF&&e.isCorrelated)return!0;if(e.kind===v.COLUMN_REF)return t.some(s=>s.tableAlias===e.tableAlias&&s.columnName===e.columnName);for(let s of Object.values(e))if(Array.isArray(s)){for(let n of s)if(this.hasCorrelatedRef(n,t))return!0}else if(s&&typeof s=="object"&&this.hasCorrelatedRef(s,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===v.COLUMN_REF&&e.isCorrelated)return{...e,depth:0,isCorrelated:!1};if(e.kind===v.COLUMN_REF&&t.find(r=>r.tableAlias===e.tableAlias&&r.columnName===e.columnName))return{...e,depth:0,isCorrelated:!1};let s={};for(let[n,r]of Object.entries(e))Array.isArray(r)?s[n]=r.map(i=>i&&typeof i=="object"?this.rewriteExprRefs(i,t):i):r&&typeof r=="object"?s[n]=this.rewriteExprRefs(r,t):s[n]=r;return s}splitAnd(e){return e?e.kind===v.BINARY&&e.op==="AND"?[...this.splitAnd(e.left),...this.splitAnd(e.right)]:[e]:[]}getSubqueryOutputRef(e){let t=e;for(;t;){if(t.type===C.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!==v.BINARY||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:C.PROJECT,expressions:[...t,n],children:[e]}}hasAggregate(e){if(!e)return!1;if(e.type===C.AGGREGATE)return!0;for(let t of ds(e))if(this.hasAggregate(t))return!0;return!1}removeProjection(e){return e.type===C.PROJECT?e.children[0]:e}exposeCorrelationColumns(e){return e.type===C.PROJECT&&(e.expressions||[]).every(t=>t.kind===v.COLUMN_REF)?e.children[0]:e}addGroupBy(e,t){if(e.type===C.AGGREGATE)return{...e,groupBy:[...t,...e.groupBy||[]]};let n=ds(e).map(r=>this.addGroupBy(r,t));return Fn(e,n)}};Ge();$t();Qe();var CW=class extends Me{static{c(this,"CTEOptimization")}get name(){return"CTEOptimization"}apply(e){let t=IW(e);return new OW(t).rewrite(e)}};function IW(e){let t=new Map;return DD(e,s=>{if(s.type===C.CTE_SCAN){let n=s.cteName.toUpperCase();t.set(n,(t.get(n)||0)+1)}}),t}c(IW,"countCTERefs");var OW=class extends qe{static{c(this,"CTERewriter")}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,u6(t)),s)}rewriteCTEScan(e){let t=e.cteName.toUpperCase(),s=this.ctePlans.get(t);return s||e}};function DD(e,t){if(e){t(e);for(let s of ds(e))DD(s,t)}}c(DD,"_walkPlan");Ge();$t();Ht();var BW=class extends Me{static{c(this,"ProjectionPushdown")}get name(){return"ProjectionPushdown"}apply(e){return ci(e,null)}};function ci(e,t){if(!e)return e;switch(e.type){case C.SCAN:return MW(e,t);case C.PROJECT:return LW(e,t);case C.FILTER:return md(e,t?FD(t,e.condition):null);case C.JOIN:return FW(e,t);case C.AGGREGATE:return PW(e);case C.SORT:return DW(e,t);case C.LIMIT:case C.MATERIALIZE:return md(e,t);case C.DEPENDENT_JOIN:return UW(e,t);case C.UNION:case C.CTE_ANCHOR:return OT(e,null);case C.DISTINCT:return md(e,null);default:return OT(e,t)}}c(ci,"pruneColumns");function MW(e,t){if(!t||t.size===0)return e;let s=BT(e),n=e.columns.filter(r=>zW(t,s.aliases,r.name));return n.length>0&&n.length<e.columns.length?{...e,columns:n}:e}c(MW,"pruneScan");function LW(e,t){let s=new Set;for(let[r,i]of(e.expressions||[]).entries())(!t||$W(i,t,r))&&Oo(i,s);let n=ci(e.children[0],s);return n!==e.children[0]?Fn(e,[n]):e}c(LW,"pruneProject");function PW(e){let t=new Set;for(let n of e.groupBy||[])Oo(n,t);for(let n of e.aggregates||[])for(let r of n.args||[])Oo(r,t);let s=ci(e.children[0],t);return s!==e.children[0]?Fn(e,[s]):e}c(PW,"pruneAggregate");function DW(e,t){if(!t)return md(e,null);let s=cb(t);for(let n of e.orderKeys||[])s=FD(s,n.expr);return md(e,s)}c(DW,"pruneSort");function FW(e,t){let s=e.children[0],n=e.children[1];if(!t){let h=ci(s,null),p=ci(n,null);return h!==s||p!==n?Fn(e,[h,p]):e}let r=BT(s),i=BT(n),o=cb(t);e.condition&&Oo(e.condition,o);let a=cL(o,r),l=cL(o,i),u=ci(s,a),f=ci(n,l);return u!==s||f!==n?Fn(e,[u,f]):e}c(FW,"pruneJoin");function UW(e,t){let s=cb(t);e.condition&&Oo(e.condition,s);for(let o of e.correlatedColumns||[])Oo(o,s);let n=e.children||[];if(n.length!==2)return OT(e,s);let r=ci(n[0],s),i=ci(n[1],null);return r!==n[0]||i!==n[1]?Fn(e,[r,i]):e}c(UW,"pruneDependentJoin");function md(e,t){let s=ci(e.children?.[0],t);return s!==e.children?.[0]?Fn(e,[s]):e}c(md,"pruneUnary");function OT(e,t){let s=ds(e),n=s.map(i=>ci(i,t));return n.some((i,o)=>i!==s[o])?Fn(e,n):e}c(OT,"pruneChildren");function FD(e,t){let s=cb(e);return Oo(t,s),s}c(FD,"addExprRefs");function cb(e){return e?new Set(e):new Set}c(cb,"copyRefs");function Oo(e,t){if(!(!e||typeof e!="object")){if(e.kind===v.COLUMN_REF){t.add(uL(e.tableAlias,e.columnName)),Number.isInteger(e.columnIndex)&&e.columnIndex>=0&&t.add(uL(e.tableAlias,`#${e.columnIndex}`));return}for(let s of Object.values(e))if(Array.isArray(s))for(let n of s)Oo(n,t);else s&&typeof s=="object"&&Oo(s,t)}}c(Oo,"collectExprColumns");function BT(e){let t={aliases:new Set,columns:new Set};return Pw(e,t),t.aliases.delete(""),t.columns.delete(""),t}c(BT,"collectPlanRefs2");function Pw(e,t){if(e){if(e.type===C.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===C.CTE_SCAN){t.aliases.add((e.alias||e.cteName||"").toUpperCase());return}if(e.type===C.PROJECT){for(let s of e.expressions||[])t.columns.add(Dw(s));for(let s of ds(e))Pw(s,t);return}if(e.type===C.AGGREGATE){for(let s of e.groupBy||[])t.columns.add(Dw(s));for(let s of e.aggregates||[])t.columns.add(Dw(s));return}if(e.type===C.JOIN||e.type===C.UNION){for(let s of ds(e))Pw(s,t);return}e.children?.[0]&&Pw(e.children[0],t)}}c(Pw,"addOutputRefs2");function Dw(e){return(e?.outputName||e?.alias||e?.name||e?.columnName||"").toUpperCase()}c(Dw,"outputName");function $W(e,t,s){let n=Dw(e);if(!n&&s===void 0)return!0;for(let r of t){let{columnName:i}=yN(r);if(n&&i===n||s!==void 0&&i===`#${s}`)return!0}return!1}c($W,"outputNeeded");function cL(e,t){let s=new Set;for(let n of e||[]){let r=yN(n);VW(r,t)&&s.add(n)}return s}c(cL,"filterRefsForPlan");function zW(e,t,s){let n=(s||"").toUpperCase();for(let r of e){let i=yN(r);if(i.columnName===n&&(!i.tableAlias||i.tableAlias==="."||t.has(i.tableAlias)))return!0}return!1}c(zW,"refSetNeedsColumn");function VW(e,t){return e.tableAlias&&e.tableAlias!=="."?t.aliases.has(e.tableAlias):t.columns.has(e.columnName)}c(VW,"refBelongsToPlan2");function uL(e,t){return`${(e||"").toUpperCase()}.${(t||"").toUpperCase()}`}c(uL,"refKey");function yN(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(yN,"parseRef");Ge();$t();Qe();Ht();var GW=class{static{c(this,"HyperEdge")}constructor(e,t,s){this.leftMask=e,this.rightMask=t,this.predicate=s}},qW=class{static{c(this,"HyperGraph")}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 GW(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=KW(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 jW(e,t,s){let n=new qW;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=WW(r);if(i.size<2)continue;let o=[...i];for(let a=0;a<o.length;a++)for(let l=a+1;l<o.length;l++)n.addEdge([o[a]],[o[l]],r)}return n}c(jW,"buildHyperGraph");function WW(e){let t=new Set;return cd(e,s=>{s.kind===v.COLUMN_REF&&s.tableAlias&&t.add(s.tableAlias.toUpperCase())}),t}c(WW,"collectColumnTableRefs");function cd(e,t){if(!(!e||typeof e!="object")&&(t(e),e.left&&cd(e.left,t),e.right&&cd(e.right,t),e.operand&&cd(e.operand,t),e.args))for(let s of e.args)cd(s,t)}c(cd,"_walkExpr2");function KW(e){return e&-e}c(KW,"lowestBit");function HW(e){let t=0;for(;e;)t+=e&1,e>>>=1;return t}c(HW,"popcount");Ht();var YW=class{static{c(this,"DPhypEnumerator")}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++)HW(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=JW(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 JW(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=MT(n);s.has(r)||(s.add(r),t.push(n))}return t.length===1?t[0]:t.reduce((n,r)=>({kind:v.BINARY,op:"AND",left:n,right:r,resultType:"BOOLEAN"}))}c(JW,"combinePredicates");function MT(e){return e?e.kind===v.COLUMN_REF?`${e.tableAlias}.${e.columnName}`:e.kind===v.BINARY?`${MT(e.left)}${e.op}${MT(e.right)}`:e.kind===v.LITERAL?String(e.value):JSON.stringify(e).slice(0,50):""}c(MT,"predicateKey");function XW(e,t,s){return new YW(e,t,s).solve()}c(XW,"runDPhyp");var UD=class{static{c(this,"DefaultCostModel")}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}}};Ht();$t();var Ts=1e-4,Aw=.5,ZE=100,tT=1e3,_N=class{static{c(this,"DefaultCardinalityEstimator")}constructor(e){this.stats=e}estimateScan(e){let t=this.stats.get(e.toUpperCase());return t?t.rowCount:tT}estimatePlan(e){if(!e)return tT;switch(e.type){case C.SCAN:return this.estimateScan(e.table);case C.FILTER:return this.estimateFilter(this.estimatePlan(e.children[0]),e.condition);case C.PROJECT:case C.SORT:case C.DISTINCT:case C.MATERIALIZE:return this.estimatePlan(e.children?.[0]);case C.LIMIT:{let t=this.estimatePlan(e.children?.[0]);return Math.min(e.count||t,t)}case C.JOIN:{let t=this.estimatePlan(e.children[0]),s=this.estimatePlan(e.children[1]);return e.joinType===F.SEMI?this.estimateSemiJoin(t,s,e.condition):e.joinType===F.ANTI?this.estimateAntiJoin(t,s,e.condition):e.joinType===F.MARK?t:e.joinType===F.LEFT?this.estimateLeftJoin(t,s,e.condition):e.joinType===F.CROSS?t*s:this.estimateJoin(t,s,e.condition)}case C.AGGREGATE:return this.estimateAggregate(this.estimatePlan(e.children[0]),e.groupBy?.length||0,e.groupBy);case C.EMPTY:return 0;default:return e.children?.length?this.estimatePlan(e.children[0]):tT}}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 u=this.estimateSelectivity(s);return Math.max(1,Math.round(e*t*u))}let r=[],i=[];for(let u of n)r.push(this.getColumnNdv(u.left)),i.push(this.getColumnNdv(u.right));let o=Math.min(r.reduce((u,f)=>u*f,1),e),a=Math.min(i.reduce((u,f)=>u*f,1),t),l=Math.max(o,a,1);return Math.max(1,Math.round(e*t/l))}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){let o=this.getColumnNdv(i.left),a=this.getColumnNdv(i.right);r=Math.min(r,Math.min(1,a/Math.max(o,1)))}return Math.max(1,Math.round(e*r))}return Math.max(1,Math.round(e*.5))}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 v.BINARY:{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(Ts,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(Ts,1-t)}return .5}case v.BETWEEN:return this.estimateBetweenSelectivity(e);case v.LIKE:return this.estimateLikeSelectivity(e);case v.IN_LIST:return this.estimateInListSelectivity(e);case v.IS_NULL:return this.estimateIsNullSelectivity(e);case v.UNARY:return e.op==="NOT"?Math.max(Ts,1-this.estimateSelectivity(e.operand)):.5;case v.EXISTS:return .5;default:return .5}}estimateEqualitySelectivity(e){let t=null,s=null;if(e.left?.kind===v.COLUMN_REF&&e.right?.kind===v.LITERAL)t=e.left,s=e.right;else if(e.right?.kind===v.COLUMN_REF&&e.left?.kind===v.LITERAL)t=e.right,s=e.left;else if(e.left?.kind===v.COLUMN_REF&&e.right?.kind===v.COLUMN_REF){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)}let r=n.ndv||ZE,i=n.nullFraction||0;return Math.max(Ts,(1-i)/r)}estimateRangeSelectivity(e){let t=e.left?.kind===v.COLUMN_REF?e.left:e.right,s=e.left?.kind===v.LITERAL?e.left:e.right;if(t?.kind!==v.COLUMN_REF||s?.kind!==v.LITERAL)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(Ts,p*(1-(n.nullFraction||0)))}let r=tc(n.min),i=tc(n.max),o=tc(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(Ts,(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===v.LITERAL&&e.high?.kind===v.LITERAL){let l=t.histogram.estimateRange(e.low.value,e.high.value),u=e.negated?1-l:l;return Math.max(Ts,u*(1-(t.nullFraction||0)))}let s=tc(t.min),n=tc(t.max),r=tc(e.low?.value),i=tc(e.high?.value);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(Ts,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===v.LITERAL){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(Ts,1-f):Math.max(Ts,f)}let r=Math.min(1,e.list.length/Math.max(t,1))*(1-n);return e.negated?Math.max(Ts,1-r):Math.max(Ts,r)}return e.negated?.7:.3}estimateIsNullSelectivity(e){let s=this.getColumnStats(e.expr)?.nullFraction||.05;return e.negated?Math.max(Ts,1-s):Math.max(Ts,s)}estimateLikeSelectivity(e){let t=e.pattern?.value;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(Ts,Math.pow(s,-o))}return Math.max(Ts,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(Ts,a*(1-o*.5))}return Math.max(Ts,1/Math.max(Math.sqrt(s),1))}if(this.isSuffixPattern(t)){let i=t.length-1;return r&&r>0?Math.max(Ts,1/Math.pow(s,Math.min(1,i/r))):Math.max(Ts,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 Aw;let r=this.stats.get(s.tableAlias.toUpperCase());if(!r?.getCorrelation)return Aw;let i=r.getCorrelation(s.columnName,n.columnName);return i!==null?Math.abs(i):Aw}extractSingleColumn(e){if(e?.kind===v.COLUMN_REF)return e;if(e?.kind===v.BINARY&&["=","<",">","<=",">=","<>"].includes(e.op)){if(e.left?.kind===v.COLUMN_REF)return e.left;if(e.right?.kind===v.COLUMN_REF)return e.right}return e?.kind===v.BETWEEN&&e.expr?.kind===v.COLUMN_REF||e?.kind===v.LIKE&&e.expr?.kind===v.COLUMN_REF||e?.kind===v.IN_LIST&&e.expr?.kind===v.COLUMN_REF?e.expr:null}getColumnNdv(e){return e?.kind!==v.COLUMN_REF?ZE:this.getColumnStats(e)?.ndv||ZE}getColumnStats(e){if(!e||e.kind!==v.COLUMN_REF)return null;let t=e.columnName?.toUpperCase(),s=this.stats.get(e.tableAlias?.toUpperCase());if(s?.columnStats?.has(t))return s.columnStats.get(t);for(let n of this.stats.values())if(n.columnStats?.has(t))return n.columnStats.get(t);return null}extractEquiPredicates(e){let t=[];return this._collectEqui(e,t),t}_collectEqui(e,t){if(e){if(e.kind===v.BINARY&&e.op==="AND"){this._collectEqui(e.left,t),this._collectEqui(e.right,t);return}e.kind===v.BINARY&&e.op==="="&&e.left?.kind===v.COLUMN_REF&&e.right?.kind===v.COLUMN_REF&&t.push({left:e.left,right:e.right})}}};function tc(e){return e==null?null:typeof e=="bigint"?Number(e):typeof e=="number"?e:null}c(tc,"toNumber");Ht();var QW=class extends Me{static{c(this,"JoinReorder")}constructor(e=new Map,t=null,s=null){super(),this.statisticsMap=e,this.costModel=t||new UD,this.cardEstimator=s||new _N(this.statisticsMap)}get name(){return"JoinReorder"}apply(e){return new ZW(this.costModel,this.cardEstimator).rewrite(e)}},ZW=class extends qe{static{c(this,"JoinReorderRewriter")}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!==C.JOIN||e.joinType!==F.INNER&&e.joinType!==F.CROSS)}isNonInnerJoin(e){return e.type!==C.JOIN?!1:e.joinType===F.SEMI||e.joinType===F.ANTI||e.joinType===F.LEFT||e.joinType===F.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=jW(t,s,this.cardEstimator);if(r.size<2)return e;let i=XW(r,this.costModel,this.cardEstimator);if(!i)return e;let o=this.reconstructPlan(i.plan);return n.length>0&&(o=Sn(Ds(n),o)),o}flattenJoinTree(e,t,s,n){if(e.type===C.JOIN&&(e.joinType===F.INNER||e.joinType===F.CROSS)){if(this.flattenJoinTree(e.children[0],t,s,n),this.flattenJoinTree(e.children[1],t,s,n),e.condition){let i=Un(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===C.FILTER){let i=Un(e.condition),o=e.children[0];if(o.type===C.JOIN&&(o.joinType===F.INNER||o.joinType===F.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===C.FILTER&&e.children[0].type===C.SCAN){let i=e.children[0];t.push({name:i.table,alias:i.alias||i.table,plan:e});return}if(e.type===C.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===v.COLUMN_REF&&s.tableAlias&&t.add(s.tableAlias.toUpperCase())}),t}_walkExpr(e,t){if(!(!e||typeof e!="object")&&(t(e),e.left&&this._walkExpr(e.left,t),e.right&&this._walkExpr(e.right,t),e.operand&&this._walkExpr(e.operand,t),e.args))for(let s of e.args)this._walkExpr(s,t)}reconstructPlan(e){if(!e)return e;if(e.type==="HashJoin"){let t=this.reconstructPlan(e.buildSide),s=this.reconstructPlan(e.probeSide);return Vr(F.INNER,e.condition,t,s)}return e}inferAlias(e){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===C.SCAN)return e;for(let t of ds(e)){let s=this.findFirstScan(t);if(s)return s}return null}};Ge();$t();Qe();Ht();var li=1e3,tK=class extends Me{static{c(this,"PhysicalDesign")}constructor(e=new Map,t=null,s=null){super(),this.statisticsMap=e,this.costModel=t||new UD,this.cardEstimator=s||new _N(this.statisticsMap)}get name(){return"PhysicalDesign"}apply(e){return new eK(this.costModel,this.cardEstimator).rewrite(e)}},eK=class extends qe{static{c(this,"PhysicalDesignRewriter")}constructor(e,t){super(),this.costModel=e,this.cardEstimator=t,this.parentMap=null}rewrite(e){return this.parentMap=sK(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||li,o=r._cardinality||li;if(s.joinType!==F.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=Pn.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=Pn.NESTED_LOOP,s._sortedBy=[],s):(this.isSpecialJoinType(s.joinType)&&this.isPureEquiJoin(s.condition)&&(s._dedupeBuild=!0),this.assignBuildSide(s,i,o),s.physicalStrategy=Pn.HASH,s._sortedBy=[],s)}assignBuildSide(e,t,s){switch(e.joinType){case F.LEFT:case F.SEMI:case F.ANTI:case F.MARK:e._buildSide="right";break;case F.RIGHT:e._buildSide="left";break;case F.INNER:e._buildSide=s<t?"right":"left";break}}isSpecialJoinType(e){return e===F.SEMI||e===F.ANTI||e===F.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=Pn.STREAM,t._sortedBy=[...s._sortedBy],t}}return!t.groupBy||t.groupBy.length===0?(t.physicalStrategy=Pn.UNGROUPED,t._sortedBy=[],t):this.canUsePerfectHashAggregate(t,s)?(t.physicalStrategy=Pn.PERFECT_HASH,t._sortedBy=[],t):(t.physicalStrategy=Pn.HASH,t._sortedBy=[],t)}rewriteSort(e){let t=this.rewriteChildren(e),s=t.children[0]._cardinality||li;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===C.SORT?e.orderKeys.map(t=>({key:this.getColumnKey(t.expr),direction:(t.direction||"ASC").toUpperCase()})).filter(t=>t.key):e.type===C.INDEX_SCAN?[`${(e.alias||e.table||"").toUpperCase()}.${(e.columnName||"").toUpperCase()}`]:(e.type===C.FILTER||e.type===C.PROJECT||e.type===C.LIMIT)&&e.children&&e.children.length>0?e.children[0]._sortedBy||[]:[]}getColumnKey(e){return e&&e.kind===v.COLUMN_REF?`${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===C.SCAN||e.type===C.INDEX_SCAN)return this.cardEstimator.estimateScan(e.table);if(e.type===C.FILTER){let t=e.children[0]._cardinality||li;return this.cardEstimator.estimateFilter(t,e.condition)}if(e.type===C.JOIN){let t=e.children[0]._cardinality||li,s=e.children[1]._cardinality||li;return e.joinType===F.SEMI?this.cardEstimator.estimateSemiJoin(t,s,e.condition):e.joinType===F.ANTI?this.cardEstimator.estimateAntiJoin(t,s,e.condition):e.joinType===F.MARK?t:e.joinType===F.LEFT?this.cardEstimator.estimateLeftJoin?this.cardEstimator.estimateLeftJoin(t,s,e.condition):Math.max(t,this.cardEstimator.estimateJoin(t,s,e.condition)):e.joinType===F.CROSS?t*s:this.cardEstimator.estimateJoin(t,s,e.condition)}if(e.type===C.AGGREGATE){let t=e.children[0]._cardinality||li;return this.cardEstimator.estimateAggregate(t,e.groupBy?.length||0,e.groupBy||[])}if(e.type===C.LIMIT){let t=e.children[0]._cardinality||li;return Math.min(e.count||t,t)}if(e.type===C.DISTINCT){let t=e.children[0]._cardinality||li;return Math.max(1,Math.round(Math.sqrt(t)))}return e.children&&e.children.length>0?e.children[0]._cardinality||li:1e3}extractEquiJoinKeys(e){let t=[],s=[],n=this.splitAnd(e);for(let r of n)r.kind===v.BINARY&&r.op==="="&&r.left?.kind===v.COLUMN_REF&&r.right?.kind===v.COLUMN_REF&&(t.push(this.getColumnKey(r.left)),s.push(this.getColumnKey(r.right)));return{leftKeys:t,rightKeys:s}}splitAnd(e){return e?e.kind===v.BINARY&&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===v.COLUMN_REF))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=fL(e.min),n=fL(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(Boolean);if(o.length===0)return 0;let a=[...t,...s];if(!this.isSortedByPrefix(a,o))return 0;let l=n||li;return i.limit?this.costModel.topNSortCost(l,i.limit):this.costModel.sortCost(l)}findDownstreamSort(e,t){if(!e)return null;if(e.type===C.SORT)return e;if(e.type===C.PROJECT||e.type===C.FILTER||e.type===C.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===v.BINARY&&s.op==="="&&s.left?.kind===v.COLUMN_REF&&s.right?.kind===v.COLUMN_REF)}};function sK(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(sK,"buildParentMap");function fL(e){return e==null?null:typeof e=="bigint"?Number(e):typeof e=="number"?e:null}c(fL,"toNumber2");qP();jP();var nK=64,rK=10,iK=class{static{c(this,"ColumnStatistics")}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}},oK=class{static{c(this,"TableStatistics")}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}},aK=class{static{c(this,"EquiDepthHistogram")}constructor(e,t){this.boundaries=e,this.numBuckets=e.length,this.numRows=t,this.rowsPerBucket=t/Math.max(this.numBuckets,1)}estimateLessThan(e){if(this.numBuckets===0)return .5;let t=Cf(e);if(t===null)return .5;let s=0,n=this.numBuckets-1;for(;s<=n;){let l=s+n>>>1;Cf(this.boundaries[l])<t?s=l+1:n=l-1}if(s>=this.numBuckets)return 1;if(s===0){let l=Cf(this.boundaries[0]),u=this.numBuckets>0?Cf(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=Cf(this.boundaries[s-1]),o=Cf(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)}},$D=class ud{static{c(this,"_StatisticsCollector")}static async collect(t){let s=t.rowCount(),n=new Map;for(let i of t.getSchema()){let o=await ud._collectColumn(t,i,s);n.set(i.name.toUpperCase(),o)}let r=await ud._computeCorrelations(t,n);return new oK(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 x=0;x<A.size;x++){let N=A.getValue(x,p);if(N==null){o++;continue}let I=typeof N=="bigint"?Number(N):N;r.push(I);let E=String(I);i.set(E,(i.get(E)||0)+1),(a===null||N<a)&&(a=N),(l===null||N>l)&&(l=N),typeof N=="string"?(u+=N.length*2,f+=N.length,h++):typeof N=="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,S=null;d&&r.length>0&&(S=ud._buildHistogram(r,n));let b=null;return i.size>0&&(b=ud._buildMCV(i,r.length)),new iK({ndv:g,min:a,max:l,nullFraction:y,histogram:S,mcv:b,avgWidth:_,avgLength:w})}static _buildHistogram(t,s){let n=[...t].sort((a,l)=>{let u=typeof a=="bigint"?Number(a):a,f=typeof l=="bigint"?Number(l):l;return u-f}),r=Math.min(nK,Math.max(1,Math.floor(n.length/4))),i=[],o=Math.max(1,Math.floor(n.length/r));for(let a=1;a<=r;a++){let l=Math.min(a*o-1,n.length-1);i.push(n[l])}return new aK(i,s)}static _buildMCV(t,s){let n=[...t.entries()].sort((r,i)=>i[1]-r[1]).slice(0,rK);return n.length===0?null:{values:n.map(r=>r[0]),frequencies:n.map(r=>r[1]/s)}}},eT=1e3,lK=.3;$D._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,eT/Math.max(a,1)),u=0;for await(let f of e.scan()){for(let h=0;h<f.size&&o<eT;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>=eT)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=cK(m,g);if(Math.abs(y)>=lK){let _=p<d?`${p}:${d}`:`${d}:${p}`;s.set(_,y)}}return s};function cK(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(cK,"pearsonCorrelation");function Cf(e){return e==null?null:typeof e=="bigint"?Number(e):typeof e=="number"?e:null}c(Cf,"toNum2");Ge();Qe();$t();Ht();var uK=class extends Me{static{c(this,"ExpressionSimplifier")}get name(){return"ExpressionSimplifier"}apply(e){return new fK().rewrite(e)}},fK=class extends qe{static{c(this,"SimplifierRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]),s=_r(e.condition);return s.kind===v.LITERAL&&s.value===!0?t:s.kind===v.LITERAL&&s.value===!1?{type:C.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=_r(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=_r(e.condition),n!==e.condition&&(s=!0)),s?{...e,condition:n,children:t}:e}};function _r(e){if(!e)return e;switch(e.kind){case v.BINARY:{let t=_r(e.left),s=_r(e.right),n=e.op.toUpperCase();if(n==="AND"){if(ln(t,!1)||ln(s,!1))return de(!1,"BOOLEAN");if(ln(t,!0))return s;if(ln(s,!0)||hL(t,s))return t}else if(n==="OR"){if(ln(t,!0)||ln(s,!0))return de(!0,"BOOLEAN");if(ln(t,!1))return s;if(ln(s,!1)||hL(t,s))return t;let r=hK(t,s);if(r)return _r(r)}else{if(n==="+"||n==="-"){if(ln(s,0))return t;if(n==="+"&&ln(t,0))return s}if(n==="*"){if(ln(s,1))return t;if(ln(t,1))return s}if(n==="/"&&ln(s,1))return t;if(t.kind===v.LITERAL&&s.kind===v.LITERAL){let r=t.value,i=s.value;if(r!==null&&i!==null)try{if(n==="+")return de(r+i,typeof(r+i)=="number"?"FLOAT64":e.dataType);if(n==="-")return de(r-i,typeof(r-i)=="number"?"FLOAT64":e.dataType);if(n==="*")return de(r*i,typeof(r*i)=="number"?"FLOAT64":e.dataType);if(n==="/")return de(r/i,"FLOAT64");if(n==="=")return de(r===i,"BOOLEAN");if(n==="!=")return de(r!==i,"BOOLEAN");if(n===">")return de(r>i,"BOOLEAN");if(n===">=")return de(r>=i,"BOOLEAN");if(n==="<")return de(r<i,"BOOLEAN");if(n==="<=")return de(r<=i,"BOOLEAN")}catch{}}}return t!==e.left||s!==e.right?{...e,left:t,right:s}:e}case v.UNARY:{let t=_r(e.operand);if(e.op.toUpperCase()==="NOT"){if(ln(t,!0))return de(!1,"BOOLEAN");if(ln(t,!1))return de(!0,"BOOLEAN");if(t.kind===v.UNARY&&t.op.toUpperCase()==="NOT")return t.operand}return t!==e.operand?{...e,operand:t}:e}case v.FUNCTION:{let t=e.args.map(_r);return t.some((n,r)=>n!==e.args[r])?{...e,args:t}:e}case v.CASE:{let t=[],s=!1;for(let r of e.whenClauses){let i=_r(r.condition),o=_r(r.result);if((i!==r.condition||o!==r.result)&&(s=!0),ln(i,!1)){s=!0;continue}if(ln(i,!0))return o;t.push({condition:i,result:o})}let n=e.elseExpr?_r(e.elseExpr):null;return n!==e.elseExpr&&(s=!0),t.length===0&&n?n:t.length===0&&!n?de(null,e.resultType):s?{...e,whenClauses:t,elseExpr:n}:e}case v.CAST:{let t=_r(e.expr);if(t.kind===v.LITERAL&&t.value!==null){let s=pK(t.value,e.targetType);if(s!==void 0)return de(s,e.targetType)}return t!==e.expr?{...e,expr:t}:e}case v.EXTRACT:{let t=_r(e.source);if(t.kind===v.LITERAL&&t.value instanceof Date){let s=dK(t.value,e.field);if(s!==null)return de(s,"INT32")}return t!==e.source?{...e,source:t}:e}}return e}c(_r,"simplifyExpression");function ln(e,t){return e&&e.kind===v.LITERAL&&e.value===t}c(ln,"isLiteral");function hL(e,t){return e===t?!0:!e||!t||e.kind!==t.kind?!1:e.kind===v.COLUMN_REF?e.tableAlias===t.tableAlias&&e.columnName===t.columnName:e.kind===v.LITERAL?e.value===t.value:Ln(e)===Ln(t)}c(hL,"exprEquals");function hK(e,t){let s=Jw(e),n=Jw(t),r=new Map(n.map(p=>[Ln(p),p])),i=[],o=[],a=new Set;for(let p of s){let d=Ln(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(Ln(p))),u=sT(o)||de(!0,"BOOLEAN"),f=sT(l)||de(!0,"BOOLEAN"),h={kind:v.BINARY,op:"OR",left:u,right:f,resultType:"BOOLEAN"};return sT([...i,h])}c(hK,"factorCommonConjuncts");function Jw(e){return e?e.kind===v.BINARY&&e.op.toUpperCase()==="AND"?[...Jw(e.left),...Jw(e.right)]:[e]:[]}c(Jw,"splitConjuncts2");function sT(e){return e.length===0?null:e.reduce((t,s)=>t?{kind:v.BINARY,op:"AND",left:t,right:s,resultType:"BOOLEAN"}:s,null)}c(sT,"combineConjuncts2");function pK(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(pK,"foldCast");function dK(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(dK,"extractFromDate");function Ln(e){if(!e||typeof e!="object")return String(e);switch(e.kind){case v.COLUMN_REF:return`COL:${e.tableAlias||""}.${e.columnName}`;case v.LITERAL:return`LIT:${String(e.value)}`;case v.BINARY:return`BIN:${e.op}:${Ln(e.left)}:${Ln(e.right)}`;case v.LIKE:return`LIKE:${e.negated}:${Ln(e.expr)}:${Ln(e.pattern)}`;case v.IN_LIST:return`IN:${e.negated}:${Ln(e.expr)}:${Array.isArray(e.list)?e.list.map(Ln).join(","):Ln(e.list)}`;case v.BETWEEN:return`BETWEEN:${e.negated}:${Ln(e.expr)}:${Ln(e.low)}:${Ln(e.high)}`;default:return JSON.stringify(e)}}c(Ln,"exprKey");Ge();Qe();$t();Ht();var mK=class extends Me{static{c(this,"OuterToInnerJoin")}get name(){return"OuterToInnerJoin"}apply(e){return new gK().rewrite(e)}},gK=class extends qe{static{c(this,"OuterToInnerRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]);if(t.type===C.JOIN&&(t.joinType===F.LEFT||t.joinType===F.FULL||t.joinType===F.RIGHT||t.joinType===F.SINGLE)){let s=pL(t.children[0]),n=pL(t.children[1]),r=PT(e.condition),i=!1,o=!1;for(let l of r)dL(l,n)&&(i=!0),dL(l,s)&&(o=!0);let a=t.joinType;t.joinType===F.LEFT&&i||t.joinType===F.SINGLE&&i||t.joinType===F.RIGHT&&o?a=F.INNER:t.joinType===F.FULL&&(o&&i?a=F.INNER:o?a=F.LEFT:i&&(a=F.RIGHT)),a!==t.joinType&&(t={...t,joinType:a})}return t!==e.children[0]?{...e,children:[t]}:e}};function pL(e){let t={aliases:new Set,columns:new Set};return LT(e,t),t.aliases.delete(""),t.columns.delete(""),t}c(pL,"getPlanRefs");function LT(e,t){if(e){if(e.type===C.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===C.CTE_SCAN){t.aliases.add((e.alias||e.cteName||"").toUpperCase());return}if(e.type===C.PROJECT){for(let s of e.expressions||[])t.columns.add((s.outputName||s.alias||s.name||s.columnName||"").toUpperCase());return}if(e.type===C.AGGREGATE){for(let s of e.groupBy||[])t.columns.add((s.outputName||s.alias||s.name||s.columnName||"").toUpperCase());for(let s of e.aggregates||[])t.columns.add((s.outputName||s.alias||s.name||"").toUpperCase());return}if(e.type===C.JOIN||e.type===C.UNION){for(let s of ds(e))LT(s,t);return}e.children?.[0]&<(e.children[0],t)}}c(LT,"addOutputRefs3");function PT(e){return e?e.kind===v.BINARY&&e.op.toUpperCase()==="AND"?[...PT(e.left),...PT(e.right)]:[e]:[]}c(PT,"splitConjuncts3");function dL(e,t){let s=fd(e,t);return s===!1||s===null}c(dL,"isNullRejecting");function fd(e,t){if(e){switch(e.kind){case v.LITERAL:return e.value;case v.COLUMN_REF:return e.tableAlias&&t.aliases.has(e.tableAlias.toUpperCase())||!e.tableAlias&&t.columns.has((e.columnName||"").toUpperCase())?null:"UNKNOWN";case v.BINARY:{let s=fd(e.left,t),n=fd(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 v.UNARY:return fd(e.operand,t)===null?null:"UNKNOWN";case v.FUNCTION:case v.EXTRACT:case v.CAST:case v.INTERVAL:case v.CASE:case v.AGGREGATE:return"UNKNOWN";case v.IS_NULL:return fd(e.operand,t)===null?!0:"UNKNOWN"}return"UNKNOWN"}}c(fd,"evaluateWithNulls");Ge();Qe();$t();var yK=class extends Me{static{c(this,"LimitPushdown")}get name(){return"LimitPushdown"}apply(e){return new _K().rewrite(e)}},_K=class extends qe{static{c(this,"LimitPushdownRewriter")}rewriteLimit(e){let t=this.rewrite(e.children[0]);if(t.type===C.PROJECT){let s={...e,children:[t.children[0]]},n={...t,children:[s]},r=this.rewrite(s);return{...n,children:[r]}}if(t.type===C.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===C.SORT){let s={...t,limit:e.count,offset:e.offset||0};return{...e,children:[s]}}if(t.type===C.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}};Ge();Qe();$t();Ht();var wK=class extends Me{static{c(this,"HavingPushdown")}get name(){return"HavingPushdown"}apply(e){return new bK().rewrite(e)}},bK=class extends qe{static{c(this,"HavingPushdownRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]);if(t.type===C.AGGREGATE){let s=DT(e.condition),n=[],r=[];for(let i of s)ko(i)?r.push(i):n.push(i);if(n.length>0){let i=t.children[0],o=mL(n),a={type:C.FILTER,condition:o,children:[i]};return t={...t,children:[a]},r.length===0?t:{...e,condition:mL(r),children:[t]}}}return t!==e.children[0]?{...e,children:[t]}:e}};function DT(e){return e?e.kind===v.BINARY&&e.op?.toUpperCase()==="AND"?[...DT(e.left),...DT(e.right)]:[e]:[]}c(DT,"splitConjuncts4");function mL(e){if(!e||e.length===0)return null;let t=e[0];for(let s=1;s<e.length;s++)t={kind:v.BINARY,op:"AND",left:t,right:e[s],resultType:"BOOLEAN"};return t}c(mL,"combineConjuncts3");function ko(e){if(!e)return!1;if(e.kind===v.AGGREGATE)return!0;if(e.kind===v.BINARY)return ko(e.left)||ko(e.right);if(e.kind===v.UNARY)return ko(e.operand);if(e.kind===v.FUNCTION||e.kind===v.CASE){if(e.args)return e.args.some(ko);if(e.whenClauses){for(let t of e.whenClauses)if(ko(t.condition)||ko(t.result))return!0}if(e.operand&&ko(e.operand)||e.elseExpr&&ko(e.elseExpr))return!0}return!1}c(ko,"containsAggregate2");Ge();Qe();$t();Ht();var SK=class extends Me{static{c(this,"EmptyPropagation")}get name(){return"EmptyPropagation"}apply(e){return new xK().rewrite(e)}},xK=class extends qe{static{c(this,"EmptyPropagationRewriter")}rewriteDefault(e){let t=this.rewriteChildren(e);return t.children&&t.children.length===1&&t.children[0].type===C.EMPTY?t.type===C.AGGREGATE&&(!t.groupBy||t.groupBy.length===0)?t:t.children[0]:t}rewriteFilter(e){let t=this.rewriteChildren(e);return t.children[0].type===C.EMPTY?t.children[0]:t.condition&&t.condition.kind===v.LITERAL&&t.condition.value===!1?{type:C.EMPTY,children:[t.children[0]]}:t}rewriteLimit(e){let t=this.rewriteChildren(e);return t.children[0].type===C.EMPTY?t.children[0]:t.count===0?{type:C.EMPTY,children:[t.children[0]]}:t}rewriteJoin(e){let t=this.rewriteChildren(e),s=t.children[0],n=t.children[1],r=s.type===C.EMPTY,i=n.type===C.EMPTY;if(t.joinType===F.INNER||t.joinType===F.CROSS){if(r||i)return{type:C.EMPTY,children:[t]}}else if(t.joinType===F.LEFT){if(r)return{type:C.EMPTY,children:[t]};if(i)return t}else if(t.joinType===F.FULL&&r&&i)return{type:C.EMPTY,children:[t]};return t}rewriteUnion(e){let t=this.rewriteChildren(e),s=t.children[0],n=t.children[1],r=s.type===C.EMPTY,i=n.type===C.EMPTY;return r&&i?{type:C.EMPTY,children:[t]}:r?n:i?s:t}};Ge();Qe();$t();Ht();var AK=class extends Me{static{c(this,"NodeMerge")}get name(){return"NodeMerge"}apply(e){return new EK().rewrite(e)}},EK=class extends qe{static{c(this,"NodeMergeRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]);if(t.type===C.FILTER){let s={kind:v.BINARY,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===C.PROJECT&&TK(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===C.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 TK(e,t){return!Array.isArray(e)||!Array.isArray(t)||e.length!==t.length?!1:e.every((s,n)=>FT(s,t[n]))}c(TK,"sameProjectExpressions");function FT(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((r,i)=>FT(r,t[i]));let s=Object.keys(e).filter(gL).sort(),n=Object.keys(t).filter(gL).sort();if(s.length!==n.length)return!1;for(let r=0;r<s.length;r++)if(s[r]!==n[r]||!FT(e[s[r]],t[n[r]]))return!1;return!0}c(FT,"exprEqualsIgnoringOutput");function gL(e){return e!=="outputName"&&e!=="alias"}c(gL,"isSemanticKey");Ge();$t();Qe();Ht();var NK=class extends Me{static{c(this,"PredicateInference")}get name(){return"PredicateInference"}apply(e){return new RK().rewrite(e)}},RK=class extends qe{static{c(this,"InferenceRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]),s=Un(e.condition),n=yL(s);if(n.length===0)return t!==e.children[0]?{...e,children:[t]}:e;let r=[...s,...n];return Sn(Ds(r),t)}rewriteJoin(e){let t=this.rewriteChildren(e);if(t.joinType!==F.INNER||!t.condition)return t;let s=Un(t.condition),n=wL(t.children[0]),r=wL(t.children[1]),i=[...s];bL(t.children[0],i),bL(t.children[1],i);let o=yL(i);if(o.length===0)return t;let a=[],l=[];for(let h of o){let p=BK(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=Sn(Ds(a),u)),l.length>0&&(f=Sn(Ds(l),f)),u!==t.children[0]||f!==t.children[1]?{...t,children:[u,f]}:t}};function yL(e){let t=new Map,s=new Map,n=new Set(e.map(Co)),r=[];for(let i of e){for(let o of vK(i,n))r.push(o),n.add(Co(o));for(let o of kK(i,n))r.push(o),n.add(Co(o))}for(let i of e){if(i.kind!==v.BINARY||i.op!=="=")continue;let o=i.left?.kind===v.COLUMN_REF,a=i.right?.kind===v.COLUMN_REF,l=i.left?.kind===v.LITERAL,u=i.right?.kind===v.LITERAL;if(o&&u)s.set(qi(i.left),i.right);else if(a&&l)s.set(qi(i.right),i.left);else if(o&&a){let f=qi(i.left),h=qi(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=Tw(e,l);if(!u)continue;let f=Dn("=",u,o,"BOOLEAN"),h=Co(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=Tw(e,l);if(!u)continue;let f=Dn("=",u,a,"BOOLEAN"),h=Co(f);n.has(h)||(r.push(f),n.add(h))}}for(let i of e){if(i.kind!==v.BINARY||!["<",">","<=",">=","<>"].includes(i.op))continue;let o=i.left?.kind===v.COLUMN_REF,a=i.right?.kind===v.COLUMN_REF,l=i.left?.kind===v.LITERAL,u=i.right?.kind===v.LITERAL;if(o&&u){let f=t.get(qi(i.left));if(f)for(let h of f){let p=Tw(e,h);if(!p)continue;let d=Dn(i.op,p,i.right,"BOOLEAN"),m=Co(d);n.has(m)||(r.push(d),n.add(m))}}else if(a&&l){let f=t.get(qi(i.right));if(f)for(let h of f){let p=Tw(e,h);if(!p)continue;let d=Dn(i.op,i.left,p,"BOOLEAN"),m=Co(d);n.has(m)||(r.push(d),n.add(m))}}}return r}c(yL,"inferNewPredicates");function vK(e,t){if(!e||e.kind!==v.BINARY||e.op!=="OR")return[];let s=Qw(e);if(s.length<2)return[];let n=s.map(o=>zD(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=Uw(a.col,[...l.values()],!1),f=Co(u);t.has(f)||r.push(u)}return r}c(vK,"inferInListsFromOr");function kK(e,t){if(!e||e.kind!==v.BINARY||e.op!=="OR")return[];let s=Qw(e);if(s.length<2)return[];let n=s.map(o=>zD(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)=>Xw(m.lower.literal,d.literal)<0?m.lower:d,l[0].lower),f=l.reduce((d,m)=>Xw(m.upper.literal,d.literal)>0?m.upper:d,l[0].upper),h=Dn(u.op,a.col,u.literal,"BOOLEAN"),p=Dn(f.op,a.col,f.literal,"BOOLEAN");for(let d of[h,p]){let m=Co(d);t.has(m)||r.push(d)}}return r}c(kK,"inferRangePredicatesFromOr");function zD(e){let t=new Map;for(let s of Un(e))CK(t,s),IK(t,s),OK(t,s);return t}c(zD,"collectBranchConstraints");function Mf(e,t){let s=qi(t);return e.has(s)||e.set(s,{col:t,literals:new Map,lower:null,upper:null}),e.get(s)}c(Mf,"ensureConstraint");function CK(e,t){if(t.kind!==v.BINARY||t.op!=="=")return;let s=t.left?.kind===v.COLUMN_REF,n=t.right?.kind===v.COLUMN_REF,r=t.left?.kind===v.LITERAL,i=t.right?.kind===v.LITERAL,o=null,a=null;s&&i?(o=t.left,a=t.right):n&&r&&(o=t.right,a=t.left),!(!o||!a)&&Mf(e,o).literals.set(VD(a),a)}c(CK,"addEqualityConstraint");function IK(e,t){if(t.kind!==v.IN_LIST||t.negated||!Array.isArray(t.list)||t.expr?.kind!==v.COLUMN_REF||!t.list.every(n=>n.kind===v.LITERAL))return;let s=Mf(e,t.expr);for(let n of t.list)s.literals.set(VD(n),n)}c(IK,"addInListConstraint");function OK(e,t){if(t.kind===v.BETWEEN&&!t.negated&&t.expr?.kind===v.COLUMN_REF){t.low?.kind===v.LITERAL&&nT(Mf(e,t.expr),">=",t.low),t.high?.kind===v.LITERAL&&rT(Mf(e,t.expr),"<=",t.high);return}if(t.kind!==v.BINARY)return;let s=t.left?.kind===v.COLUMN_REF,n=t.right?.kind===v.COLUMN_REF,r=t.left?.kind===v.LITERAL,i=t.right?.kind===v.LITERAL;if(s&&i){let o=Mf(e,t.left);(t.op===">="||t.op===">")&&nT(o,t.op,t.right),(t.op==="<="||t.op==="<")&&rT(o,t.op,t.right)}else if(n&&r){let o=Mf(e,t.right);(t.op==="<="||t.op==="<")&&nT(o,_L(t.op),t.left),(t.op===">="||t.op===">")&&rT(o,_L(t.op),t.left)}}c(OK,"addRangeConstraint");function nT(e,t,s){(!e.lower||Xw(s,e.lower.literal)>0)&&(e.lower={op:t,literal:s})}c(nT,"setLower");function rT(e,t,s){(!e.upper||Xw(s,e.upper.literal)<0)&&(e.upper={op:t,literal:s})}c(rT,"setUpper");function _L(e){return e==="<="?">=":e==="<"?">":e===">="?"<=":e===">"?"<":e}c(_L,"flipRangeOp");function Xw(e,t){return typeof e.value=="number"&&typeof t.value=="number"?e.value-t.value:String(e.value).localeCompare(String(t.value))}c(Xw,"compareLiteral");function Qw(e){return e?e.kind===v.BINARY&&e.op==="OR"?[...Qw(e.left),...Qw(e.right)]:[e]:[]}c(Qw,"splitOr");function qi(e){return`${(e.tableAlias||"").toUpperCase()}.${(e.columnName||"").toUpperCase()}`}c(qi,"colKey");function Co(e){return e.kind===v.BINARY?`${e.op}:${Ew(e.left)}:${Ew(e.right)}`:e.kind===v.IN_LIST?`IN:${Ew(e.expr)}:${e.list.map(Ew).join(",")}:${e.negated}`:JSON.stringify(e).slice(0,80)}c(Co,"predKey");function Ew(e){return e?e.kind===v.COLUMN_REF?qi(e):e.kind===v.LITERAL?`LIT:${e.value}`:JSON.stringify(e).slice(0,40):"null"}c(Ew,"exprKey2");function VD(e){return`${e.dataType||""}:${String(e.value)}`}c(VD,"literalKey");function Tw(e,t){for(let s of e)if(s.kind===v.BINARY){if(s.left?.kind===v.COLUMN_REF&&qi(s.left)===t)return s.left;if(s.right?.kind===v.COLUMN_REF&&qi(s.right)===t)return s.right}return null}c(Tw,"findColExpr");function wL(e){let t=new Set;function s(n){if(n){n.type===C.SCAN&&t.add((n.alias||n.table).toUpperCase());for(let r of ds(n))s(r)}}return c(s,"walk"),s(e),t}c(wL,"collectTables");function BK(e){let t=[];return tr(e,s=>{s.kind===v.COLUMN_REF&&s.tableAlias&&t.push(s.tableAlias.toUpperCase())}),t}c(BK,"collectTableRefs2");function bL(e,t){e&&e.type===C.FILTER&&t.push(...Un(e.condition))}c(bL,"collectFiltersAbove");function tr(e,t){if(!(!e||typeof e!="object")){if(t(e),e.left&&tr(e.left,t),e.right&&tr(e.right,t),e.operand&&tr(e.operand,t),e.expr&&tr(e.expr,t),e.low&&tr(e.low,t),e.high&&tr(e.high,t),e.args)for(let s of e.args)tr(s,t);if(Array.isArray(e.list))for(let s of e.list)tr(s,t);if(e.pattern&&tr(e.pattern,t),e.source&&tr(e.source,t),e.whenClauses)for(let s of e.whenClauses)tr(s.condition,t),tr(s.result,t);e.elseExpr&&tr(e.elseExpr,t)}}c(tr,"walkExpr");Ge();Qe();$t();Ht();var MK=class extends Me{static{c(this,"SortElimination")}get name(){return"SortElimination"}apply(e){return new LK().rewrite(e)}},LK=class extends qe{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:PK(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(!DK(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 PK(e){return e&&e.kind===v.COLUMN_REF?`${e.tableAlias||""}.${e.columnName}`.toUpperCase():null}c(PK,"getColumnKey");function DK(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(DK,"columnMatches");Ge();Qe();$t();Ht();var FK=class extends Me{static{c(this,"JoinElimination")}get name(){return"JoinElimination"}apply(e){return new $K().rewrite(e)}},UK=new Set([C.PROJECT,C.AGGREGATE]),$K=class extends qe{static{c(this,"JoinEliminationRewriter")}rewriteDefault(e){let t=this.rewriteChildren(e);return UK.has(t.type)&&zK(t)?VK(t):t}};function zK(e){return e.children?e.children.some(t=>t.type===C.JOIN&&t.joinType===F.LEFT):!1}c(zK,"hasLeftJoinChild");function VK(e){let t=e.children.map(n=>{if(n.type!==C.JOIN||n.joinType!==F.LEFT)return n;let r=qK(n.children[1]),i=KK(n.children[1]),o=new Set;return GK(e,o),jK(r,o)||WK(i,o)?n:n.children[0]});return t.some((n,r)=>n!==e.children[r])?{...e,children:t}:e}c(VK,"tryEliminateLeftJoin");function GK(e,t){let s=c(n=>{if(!(!n||typeof n!="object")){if(n.kind===v.COLUMN_REF){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 C.PROJECT:for(let n of e.expressions)s(n);break;case C.FILTER:s(e.condition);break;case C.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 C.SORT:for(let n of e.orderKeys)s(n.expr);break;case C.DISTINCT:break}}c(GK,"collectNodeExprColumns");function qK(e){let t=new Set;function s(n){if(n){n.type===C.SCAN&&t.add((n.alias||n.table).toUpperCase());for(let r of ds(n))s(r)}}return c(s,"walk"),s(e),t}c(qK,"collectTableAliases");function jK(e,t){for(let s of t){let n=s.split(".")[0];if(e.has(n))return!0}return!1}c(jK,"hasAnyColumnUsed");function WK(e,t){for(let s of t){let n=s.split(".")[1];if(n&&e.has(n))return!0}return!1}c(WK,"hasAnyNameUsed");function KK(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 C.SCAN:for(let o of i.columns||[])s(o.name||o.columnName);return;case C.PROJECT:for(let o of i.expressions||[])n(o);return;case C.AGGREGATE:for(let o of i.groupBy||[])n(o);for(let o of i.aggregates||[])n(o);return;default:for(let o of ds(i))r(o)}}return c(r,"walk"),r(e),t}c(KK,"collectOutputNames");Ge();Qe();$t();Ht();var HK=class extends Me{static{c(this,"PredicateDedup")}get name(){return"PredicateDedup"}apply(e){return new YK().rewrite(e)}},YK=class extends qe{static{c(this,"DedupRewriter")}rewriteFilter(e){let t=this.rewrite(e.children[0]),s=Un(e.condition),n=SL(s);return n.length===0?t:n.length===s.length&&t===e.children[0]?e:Sn(Ds(n),t)}rewriteJoin(e){let t=this.rewriteChildren(e);if(!t.condition)return t;let s=Un(t.condition),n=SL(s);return n.length===s.length?t:n.length===0?{...t,condition:null}:{...t,condition:Ds(n)}}};function SL(e){let t=new Set,s=[];for(let n of e){let r=Ps(n);t.has(r)||(t.add(r),s.push(n))}return s}c(SL,"dedup");function Ps(e){if(!e||typeof e!="object")return String(e);switch(e.kind){case v.COLUMN_REF:return`COL:${(e.tableAlias||"").toUpperCase()}.${(e.columnName||"").toUpperCase()}`;case v.LITERAL:return`LIT:${String(e.value)}:${e.dataType||""}`;case v.BINARY:{let t=Ps(e.left),s=Ps(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 v.UNARY:return`UNARY:${e.op}:${Ps(e.operand)}`;case v.LIKE:return`LIKE:${e.negated}:${Ps(e.expr)}:${Ps(e.pattern)}`;case v.IN_LIST:return`IN:${e.negated}:${Ps(e.expr)}:${Array.isArray(e.list)?e.list.map(Ps).join(","):Ps(e.list)}`;case v.BETWEEN:return`BTW:${e.negated}:${Ps(e.expr)}:${Ps(e.low)}:${Ps(e.high)}`;case v.IS_NULL:return`ISNULL:${e.negated}:${Ps(e.expr)}`;case v.AGGREGATE:return`AGG:${e.name}:${e.distinct}:${e.args.map(Ps).join(",")}`;case v.FUNCTION:return`FN:${e.name}:${e.args.map(Ps).join(",")}`;case v.EXTRACT:return`EXT:${e.field}:${Ps(e.source)}`;case v.CASE:return`CASE:${e.whenClauses.map(t=>`${Ps(t.condition)}:${Ps(t.result)}`).join(";")}:${Ps(e.elseExpr)}`;default:return JSON.stringify(e).slice(0,100)}}c(Ps,"exprKey3");Ge();$t();Qe();Ht();var JK=class extends Me{static{c(this,"JoinResidualSplit")}get name(){return"JoinResidualSplit"}apply(e){return new XK().rewrite(e)}},XK=class extends qe{static{c(this,"JoinResidualSplitRewriter")}rewriteJoin(e){let t=this.rewriteChildren(e);if(t.joinType!==F.INNER||!t.condition)return t;let s=xL(t.children[0]),n=xL(t.children[1]),r=[],i=[];for(let a of Un(t.condition))ZK(a,s,n)?i.push(a):r.push(a);if(i.length===0||r.length===0)return t;let o=Vr(t.joinType,Ds(r),t.children[0],t.children[1],t.physicalStrategy);return Sn(Ds(i),QK(o,t))}};function QK(e,t){let s={...e};for(let n of Object.keys(t))n.startsWith("_")&&(s[n]=t[n]);return t.markColumn&&(s.markColumn=t.markColumn),s}c(QK,"copyJoinMetadata");function ZK(e,t,s){if(!e||e.kind!==v.BINARY||e.op!=="OR")return!1;let n=tH(e);return n.some(r=>AL(r,t))&&n.some(r=>AL(r,s))}c(ZK,"isCrossSideOr");function tH(e){let t=[];return er(e,s=>{s.kind===v.COLUMN_REF&&t.push({tableAlias:(s.tableAlias||"").toUpperCase(),columnName:(s.columnName||"").toUpperCase()})}),t}c(tH,"collectExprRefs");function xL(e){let t={aliases:new Set,columns:new Set};return UT(e,t),t.aliases.delete(""),t.columns.delete(""),t}c(xL,"collectPlanRefs3");function UT(e,t){if(e){if(e.type===C.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===C.CTE_SCAN){t.aliases.add((e.alias||e.cteName||"").toUpperCase());return}if(e.type===C.PROJECT){for(let s of e.expressions||[])t.columns.add(iT(s));return}if(e.type===C.AGGREGATE){for(let s of e.groupBy||[])t.columns.add(iT(s));for(let s of e.aggregates||[])t.columns.add(iT(s));return}if(e.type===C.JOIN||e.type===C.UNION){for(let s of ds(e))UT(s,t);return}e.children?.[0]&&UT(e.children[0],t)}}c(UT,"addOutputRefs4");function iT(e){return(e?.outputName||e?.alias||e?.name||e?.columnName||"").toUpperCase()}c(iT,"outputName2");function AL(e,t){return e.tableAlias?t.aliases.has(e.tableAlias):t.columns.has(e.columnName)}c(AL,"refBelongsToPlan3");function er(e,t){if(!(!e||typeof e!="object")){if(t(e),e.left&&er(e.left,t),e.right&&er(e.right,t),e.operand&&er(e.operand,t),e.expr&&er(e.expr,t),e.low&&er(e.low,t),e.high&&er(e.high,t),e.args)for(let s of e.args)er(s,t);if(e.whenClauses)for(let s of e.whenClauses)er(s.condition,t),er(s.result,t);if(e.elseExpr&&er(e.elseExpr,t),e.list&&Array.isArray(e.list))for(let s of e.list)er(s,t);e.pattern&&er(e.pattern,t),e.source&&er(e.source,t)}}c(er,"walkExpr2");Ge();Qe();$t();var eH=class extends Me{static{c(this,"TopNFusion")}get name(){return"TopNFusion"}apply(e){return new sH().rewrite(e)}},sH=class extends qe{static{c(this,"TopNFusionRewriter")}rewriteLimit(e){let t=this.rewrite(e.children[0]);if(t.type===C.SORT)return{type:C.TOP_N,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===C.PROJECT&&t.children[0]?.type===C.SORT){let s=t.children[0],n={type:C.TOP_N,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}};Ge();Qe();$t();Ht();be();var nH=class extends Me{static{c(this,"IndexSelection")}constructor(e,t){super(),this.catalog=e,this.statistics=t}get name(){return"IndexSelection"}apply(e){return new rH(this.catalog,this.statistics).rewrite(e)}},rH=class extends qe{static{c(this,"IndexSelectionRewriter")}constructor(e,t){super(),this.catalog=e,this.statistics=t}rewriteFilter(e){let t=this.rewrite(e.children[0]);if(t.type!==C.SCAN)return{...e,children:[t]};let s=t.table,n=t.alias||s,r=Un(e.condition),i=new Map,o=[];for(let S=0;S<r.length;S++){let b=this._analyzeConjunct(r[S],n);if(!b){o.push({idx:S,indexed:!1});continue}o.push({idx:S,indexed:!0,column:b.column}),i.has(b.column)||i.set(b.column,{point:null,low:null,high:null,lowInc:!1,highInc:!1,conjunctIndices:[]});let A=i.get(b.column);A.conjunctIndices.push(S),b.type==="eq"?A.point=b.value:b.type==="gt"||b.type==="gte"?(A.low=b.value,A.lowInc=b.type==="gte"):(b.type==="lt"||b.type==="lte")&&(A.high=b.value,A.highInc=b.type==="lte")}let a=null,l=null;for(let[S,b]of i)if(this.catalog.getIndexForColumn(s,S)){if(this.statistics){let x=this.statistics.get(s.toUpperCase());if(x){let N=x.getColumnStats(S);if(N&&N.ndv>0){let I;if(b.point!==null?I=1/N.ndv:I=this._estimateRangeSelectivity(N,b),I>_t.indexScanSelectivityThreshold)continue}}}a=S,l=b;break}if(!a)return{...e,children:[t]};let u=new Set(l.conjunctIndices),f=r.filter((S,b)=>!u.has(b)),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=l6(s,n,_,a,h,p,d,m,g,y,t.columns);return f.length>0?Sn(Ds(f),w):w}_estimateRangeSelectivity(e,t){let s=Nw(e.min),n=Nw(e.max);if(s===null||n===null||n<=s)return .33;let r=t.low!==null?Nw(t.low):s,i=t.high!==null?Nw(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!==v.BINARY)return null;let s=e.op;if(s!=="="&&s!==">"&&s!==">="&&s!=="<"&&s!=="<=")return null;let n=null,r=null,i=!1;if(e.left.kind===v.COLUMN_REF&&e.right.kind===v.LITERAL)n=e.left,r=e.right;else if(e.right.kind===v.COLUMN_REF&&e.left.kind===v.LITERAL)n=e.right,r=e.left,i=!0;else return null;if(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":s==="<="&&(l=i?"gte":"lte"),{column:o,value:a,type:l}}};function Nw(e){return e==null?null:typeof e=="bigint"?Number(e):typeof e=="number"?e:null}c(Nw,"toNumber3");be();var Rw=class{static{c(this,"BTreeNode")}constructor(e){this.isLeaf=e,this.keys=[],this.children=[],this.values=[],this.next=null}},iH=class{static{c(this,"BTreeIndex")}constructor(e){this.dataType=e,this.root=new Rw(!0),this.order=_t.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 Rw(!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 Rw(!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 Rw(!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}}};Ge();$t();Qe();Ht();var oH=class extends Me{static{c(this,"FilterOrdering")}constructor(e=new Map){super(),this.cardEstimator=new _N(e)}get name(){return"FilterOrdering"}apply(e){return new aH(this.cardEstimator).rewrite(e)}},aH=class extends qe{static{c(this,"FilterOrderingRewriter")}constructor(e){super(),this.cardEstimator=e}rewriteFilter(e){let t=this.rewriteChildren(e),s=$T(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=lH(n.map(i=>i.pred));return{...t,condition:r}}};function $T(e){return e?e.kind===v.BINARY&&e.op==="AND"?[...$T(e.left),...$T(e.right)]:[e]:[]}c($T,"splitConjuncts5");function lH(e){return e.length===0?null:e.length===1?e[0]:e.reduce((t,s)=>({kind:v.BINARY,op:"AND",left:t,right:s,resultType:"BOOLEAN"}))}c(lH,"combineConjuncts4");Ge();$t();Qe();Ht();var oT=new Map([["SUM",{partial:"SUM",final:"SUM"}],["COUNT",{partial:"COUNT",final:"SUM"}],["MIN",{partial:"MIN",final:"MIN"}],["MAX",{partial:"MAX",final:"MAX"}]]),cH=class extends Me{static{c(this,"AggregatePushdown")}get name(){return"AggregatePushdown"}apply(e){return new uH().rewrite(e)}},uH=class extends qe{static{c(this,"AggregatePushdownRewriter")}rewriteAggregate(e){let t=this.rewriteChildren(e),s=t.children[0];if(s.type!==C.JOIN||s.joinType!==F.INNER||!t.groupBy||t.groupBy.length===0||!t.aggregates||t.aggregates.length===0||!this.allAggregatesDecomposable(t.aggregates))return t;let n=this.collectPlanTableRefs(s.children[0]),r=this.collectPlanTableRefs(s.children[1]),i=this.classifyColumns(t.groupBy,n,r);if(i==="both"||i==="none")return t;let o=this.classifyAggInputs(t.aggregates,n,r);if(o==="both"||o!==i&&o!=="none")return t;let l=i==="left"?0:1,u=this.buildPartialAggregates(t.aggregates),f=yd([...t.groupBy],u,s.children[l]),h=[...s.children];h[l]=f;let p={...s,children:h},d=this.buildFinalAggregates(t.aggregates,u);return yd(t.groupBy,d,p)}allAggregatesDecomposable(e){return e.every(t=>{if(t.distinct)return!1;let s=(t.func||t.functionName||"").toUpperCase();return oT.has(s)})}buildPartialAggregates(e){return e.map((t,s)=>{let n=(t.func||t.functionName||"").toUpperCase(),r=oT.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=oT.get(r),o={kind:v.COLUMN_REF,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!==v.COLUMN_REF)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===v.LITERAL}walkExprRefs(e,t){if(!(!e||typeof e!="object")&&(e.kind===v.COLUMN_REF&&e.tableAlias&&t(e.tableAlias.toUpperCase()),e.left&&this.walkExprRefs(e.left,t),e.right&&this.walkExprRefs(e.right,t),e.operand&&this.walkExprRefs(e.operand,t),e.args))for(let s of e.args)this.walkExprRefs(s,t)}collectPlanTableRefs(e){let t=new Set;return this._collectRefs(e,t),t}_collectRefs(e,t){if(e){if(e.type===C.SCAN){t.add((e.alias||e.table||"").toUpperCase());return}if(e.children)for(let s of e.children)this._collectRefs(s,t)}}},fH=class{static{c(this,"StatisticsCache")}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 $D.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}};$t();$t();Ht();rs();var hH=class extends Error{static{c(this,"UnknownColumnError")}},pH=class extends Error{static{c(this,"AmbiguousColumnError")}},rc=class{static{c(this,"DFField")}constructor(e,t,s,n){this.name=e,this.dataType=t,this.index=s,this.tableAlias=n||""}};function ed(e){return e.map((t,s)=>new rc(t.name,t.dataType,s,t.tableAlias))}c(ed,"reindex");var Ff=class Ua{static{c(this,"_DFSchema")}constructor(t){this._fields=t}static fromStorageSchema(t,s){return new Ua(t.map((n,r)=>new rc(n.name,n.dataType,r,s)))}static fromFields(t){return new Ua(ed(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 pH(`Ambiguous column reference: ${t}`);i=o}if(!i){let o=s?`${s}.`:"";throw new hH(`Unknown column: ${o}${t}`)}return i}has(t,s=null){try{return this.resolve(t,s),!0}catch{return!1}}project(t){return new Ua(ed(t.map(s=>new rc(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 Ua(ed(n))}append(t){return new Ua(ed([...this._fields,...t.fields]))}requalify(t){return new Ua(this._fields.map(s=>new rc(s.name,s.dataType,s.index,t)))}static aggregateOutput(t,s){return new Ua(ed([...t,...s]))}};Ht();rs();rs();var EL={[L.INT32]:1,[L.INT64]:2,[L.DECIMAL]:3,[L.FLOAT64]:4};function GD(e){return e==null?null:typeof e=="boolean"?L.BOOLEAN:typeof e=="bigint"?L.INT64:typeof e=="number"?Number.isInteger(e)?L.INT32:L.FLOAT64:e instanceof Date?L.TIMESTAMP:L.VARCHAR}c(GD,"inferValueType");function dH(e,t){return e===null?t:t===null||e===t?e:Fw(e)&&Fw(t)?EL[e]>=EL[t]?e:t:L.VARCHAR}c(dH,"reconcileTypes");function zT(e){let t=null;for(let s of e)t=dH(t,GD(s));return t===null?L.VARCHAR:t}c(zT,"inferColumnType");function VT(e,t){if(e==null)return null;switch(t){case L.BOOLEAN:return e;case L.INT32:return Number(e);case L.FLOAT64:return Number(e);case L.INT64:return BigInt(e);case L.DECIMAL:return BigInt(Math.round(Number(e)*PL));case L.DATE:return e instanceof Date?gd(e.getUTCFullYear(),e.getUTCMonth()+1,e.getUTCDate()):Number(e);case L.TIMESTAMP:return e instanceof Date?BigInt(e.getTime()):BigInt(e);case L.VARCHAR:return String(e);default:return e}}c(VT,"coerceForColumn");rs();function vw(e,t){return e===L.FLOAT64||t===L.FLOAT64?L.FLOAT64:e===L.DECIMAL||t===L.DECIMAL?L.DECIMAL:e===L.INT64||t===L.INT64?L.INT64:e===L.DATE?L.DATE:L.INT32}c(vw,"inferArithmeticType");function If(){return L.BOOLEAN}c(If,"inferComparisonType");function TL(){return L.BOOLEAN}c(TL,"inferLogicalType");function mH(e,t){switch(e.toUpperCase()){case"COUNT":case"COUNT_STAR":return L.INT64;case"AVG":return L.FLOAT64;case"SUM":case"MIN":case"MAX":return t||L.FLOAT64;default:return L.FLOAT64}}c(mH,"inferAggregateResultType");Ht();function gH(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(gH,"groupByAlias");function yH(e){return e?e.kind===v.COLUMN_REF?e.columnName:e.kind===v.AGGREGATE||e.kind===v.FUNCTION?e.name.toLowerCase():null:null}c(yH,"deriveOutputName");function _H(e,t,s,n){let r=fW(e),i=new LD;for(let[l,u]of gH(t))i.addTable(l,{originalName:l,columns:u});let a=new CT(s,n).bindExpression(r,i);return{expr:a,dataType:bn(a),outputName:yH(a)}}c(_H,"bindScalarSql");function wH(e){return e?e.kind===v.COLUMN_REF?e.columnName:e.kind===v.AGGREGATE?e.name.toLowerCase():null:null}c(wH,"deriveName");var $n=class Vi{static{c(this,"_Col")}constructor(t,s=null){this._build=t,this._name=s,this._alias=null}alias(t){let s=new Vi(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||wH(n);return r&&(n.outputName=r),{expr:n,outputName:r,dataType:bn(n)}}_binary(t,s,n){let r=kw(s);return new Vi((i,o)=>{let a=this._build(i,o),l=r._build(i,o);return Dn(t,a,l,n(bn(a),bn(l)))})}add(t){return this._binary("+",t,vw)}sub(t){return this._binary("-",t,vw)}mul(t){return this._binary("*",t,vw)}div(t){return this._binary("/",t,vw)}eq(t){return this._binary("=",t,If)}ne(t){return this._binary("<>",t,If)}lt(t){return this._binary("<",t,If)}le(t){return this._binary("<=",t,If)}gt(t){return this._binary(">",t,If)}ge(t){return this._binary(">=",t,If)}and(t){return this._binary("AND",t,TL)}or(t){return this._binary("OR",t,TL)}not(){return new Vi((t,s)=>lT("NOT",this._build(t,s),L.BOOLEAN))}isNull(){return new Vi((t,s)=>uT(this._build(t,s),!1))}isNotNull(){return new Vi((t,s)=>uT(this._build(t,s),!0))}like(t){return new Vi((s,n)=>UL(this._build(s,n),ub(t)._build(s,n),!1))}between(t,s){let n=kw(t),r=kw(s);return new Vi((i,o)=>FL(this._build(i,o),n._build(i,o),r._build(i,o),!1))}isin(...t){let s=t.map(kw);return new Vi((n,r)=>Uw(this._build(n,r),s.map(i=>i._build(n,r)),!1))}cast(t){return new Vi((s,n)=>DL(this._build(s,n),t))}};function kw(e){return e instanceof $n?e:ub(e)}c(kw,"toCol");function ji(e){let t=e.indexOf("."),s=t>=0?e.slice(0,t):null,n=t>=0?e.slice(t+1):e;return new $n(i=>{let o=i.resolve(n,s);return za(o.tableAlias,o.name,o.index,o.dataType)},n)}c(ji,"col");function ub(e){return new $n(()=>de(e,GD(e)))}c(ub,"lit");function fb(e){return new $n((t,s)=>_H(e,t,s.catalog,s.functionRegistry).expr)}c(fb,"expr");function Ed(e){return t=>{let s=t instanceof $n?t:ji(t);return new $n((r,i)=>{let o=s._build(r,i);return tb(e,[o],!1,mH(e,bn(o)))},e.toLowerCase())}}c(Ed,"aggregate");var qD=Ed("SUM"),jD=Ed("AVG"),WD=Ed("MIN"),KD=Ed("MAX"),HD=Ed("COUNT");function YD(){return new $n(()=>tb("COUNT_STAR",[],!1,L.INT64),"count")}c(YD,"countStar");var bH="__l",SH="__r",xH="self";function sd(e){return new $n(()=>za(e.tableAlias,e.name,e.index,e.dataType),e.name)}c(sd,"fieldCol");function AH(e,t){return e===t?e:e===L.VARCHAR?t:(t===L.VARCHAR,e)}c(AH,"reconcileKeyType");function EH(e,t,s){return new $n((n,r)=>cT("COALESCE",[ji(e).bind(n,r).expr,ji(t).bind(n,r).expr],s))}c(EH,"coalesceCol");function aT(e){return e instanceof $n?e:ji(e)}c(aT,"selectArg");function TH(e){return e instanceof $n?e:fb(e)}c(TH,"predicateArg");function NH(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(NH,"uniquify");var Sr=class GT{static{c(this,"_DataFrame")}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 GT(this._engine,t,s,RL(this._cteMap,n))}columns(){return this._schema.names()}schema(){return this._schema}explain(){return GL(this._plan)}sql(t){let s=this._schema.fields.map(n=>({name:n.name,dataType:n.dataType}));return this._engine.sql(t,{frames:[{name:xH,columns:s,plan:this._plan,cteMap:this._cteMap}]})}select(...t){let s=this._ctx(),n=t.map(o=>aT(o).bind(this._schema,s)),r=n.map(o=>o.expr),i=n.map((o,a)=>new rc(o.outputName||`col${a}`,o.dataType,a,""));return this._derive($w(r,this._plan),Ff.fromFields(i))}filter(t){let{expr:s,dataType:n}=TH(t).bind(this._schema,this._ctx());if(n!==L.BOOLEAN)throw new TypeError(`filter condition must be boolean, got ${n}`);return this._derive(Sn(s,this._plan),this._schema)}where(t){return this.filter(t)}withColumn(t,s){let n=(s instanceof $n?s:fb(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:sd(o)):[...this._schema.fields.map(sd),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(sd);return this.select(...n)}groupBy(...t){let s=this._ctx(),n=t.map(r=>aT(r).bind(this._schema,s));return new vH(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 $n)?r:{col:r,desc:!1},{expr:o}=aT(i.col).bind(this._schema,s);return{expr:o,direction:i.desc?hT.DESC:hT.ASC}});return this._derive(zw(n,this._plan),this._schema)}sort(...t){return this.orderBy(...t)}limit(t,s=0){return this._derive($L(t,s,this._plan),this._schema)}distinct(){return this._derive(zL(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(!Z4(i,o))throw new TypeError(`union column ${r} type mismatch: ${i} vs ${o}`)}let n=VL(this._plan,t._plan,s);return this._derive(n,Ff.fromFields(this._schema.fields),t._cteMap)}join(t,s,n="INNER"){let r=Array.isArray(s)?s:[s],i=F[n.toUpperCase()];if(!i)throw new Error(`Unknown join type: ${n}`);let o=`${bH}${this._engine._nextDfId()}_`,a=`${SH}${this._engine._nextDfId()}_`,l=this.select(...this._schema.fields.map(x=>sd(x).alias(`${o}${x.name}`))),u=t.select(...t._schema.fields.map(x=>sd(x).alias(`${a}${x.name}`))),f=this._ctx(),h=null;for(let x of r){let N=this._schema.resolve(x),I=t._schema.resolve(x),E=Dn("=",ji(`${o}${N.name}`).bind(l._schema,f).expr,ji(`${a}${I.name}`).bind(u._schema,f).expr,L.BOOLEAN);h=h?Dn("AND",h,E,L.BOOLEAN):E}let p=i===F.RIGHT,d=p?F.LEFT:i,m=p?[u._plan,l._plan]:[l._plan,u._plan],g=Vr(d,h,m[0],m[1]),y=new GT(this._engine,g,l._schema.append(u._schema),RL(this._cteMap,t._cteMap)),_=new Set(r.map(x=>x.toUpperCase())),w=new Map(r.map(x=>{let N=this._schema.resolve(x),I=t._schema.resolve(x);return[x.toUpperCase(),{left:`${o}${N.name}`,right:`${a}${I.name}`,dataType:AH(N.dataType,I.dataType)}]})),S=[],b=[];for(let x of this._schema.fields){let N=w.get(x.name.toUpperCase());S.push(N?EH(N.left,N.right,N.dataType):ji(`${o}${x.name}`)),b.push(x.name)}for(let x of t._schema.fields)_.has(x.name.toUpperCase())||(S.push(ji(`${a}${x.name}`)),b.push(x.name));let A=NH(b);return y.select(...S.map((x,N)=>x.alias(A[N])))}_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=tb("COUNT_STAR",[],!1,L.INT64);t.outputName="count";let s=yd([],[t],this._plan),n=[{name:"count",expr:null,dataType:L.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=NL(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=>NL(u[f]))));let l=a.join(`
|
|
224
|
-
`);return console.log(l),l}};function NL(e){return e==null?"NULL":String(e)}c(NL,"formatCell");function RL(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(RL,"mergeCteMaps");function RH(e,t,s){let n=e.kind===v.COLUMN_REF?e.columnName:`group${s}`,r=e.tableAlias||"";return{ref:za(r,n,s,bn(e)),outputName:t.outputName||n,dataType:t.dataType}}c(RH,"aggregateGroupField");var vH=class{static{c(this,"GroupedData")}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=yd(n,r,this._childPlan),o=[],a=[];for(let p=0;p<this._groupBounds.length;p++){let d=RH(n[p],this._groupBounds[p],p);d.ref.outputName=d.outputName,o.push(d.ref),a.push(new rc(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 rc(m,d.dataType,this._groupBounds.length+p,""))}let f=$w([...o,...l],i),h=Ff.aggregateOutput(a,u);return new Sr(this._engine,f,h,this._cteMap)}};Te();function vL(e,t){let s=[],n=It.fromSchema(e,ms);for(let r of t)n.size>=ms&&(s.push(n),n=It.fromSchema(e,ms)),n.appendRow(r);return(n.size>0||s.length===0)&&s.push(n),s}c(vL,"buildChunks");var Td=class qT{static{c(this,"_InMemoryRelation")}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:zT(i[u])})),a=t.map(l=>o.map((u,f)=>VT(r(l,f),u.dataType)));return new qT(o,vL(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):zT(t[l])})),a=[];for(let l=0;l<r;l++)a.push(o.map(u=>VT(t[u.name][l],u.dataType)));return new qT(o,vL(o,a))}static builder(t=null){return new kH(t)}},kH=class{static{c(this,"RelationBuilder")}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:zT(e.map(i=>s(i,r)))}))}this._chunk=It.fromSchema(this.schema,ms)}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>=ms&&(this._chunks.push(this._chunk),this._chunk=It.fromSchema(t,ms)),this._chunk.appendRow(t.map((i,o)=>VT(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=It.fromSchema(this.schema,ms)),(this._chunk.size>0||this._chunks.length===0)&&this._chunks.push(this._chunk);let e=new Td(this.schema,this._chunks);return this._chunk=null,this._chunks=[],e}},CH="__DF",JD=null;function IH(e){JD=e}c(IH,"setDefaultStorageBackend");var OH=class{static{c(this,"QueryEngine")}constructor(e,t={}){this.catalog=e,this.functionRegistry=gW;let s=t.storageBackend??JD?.(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 hN(e,this.tempManager,s),this.wasmEnabled=!1,this._dfIdCounter=0,this.precomputedStats=t.statistics||null,this.statsCache=new fH(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 bW;return s.registerPass(new uK),s.registerPass(new vW),s.registerPass(new wK),s.registerPass(new CW),s.registerPass(new xw),s.registerPass(new NK),s.registerPass(new xw),s.registerPass(new mK),s.registerPass(new xw),s.registerPass(new cH),e&&(s.registerPass(new QW(e)),s.registerPass(new xw)),s.registerPass(new FK),s.registerPass(new BW),s.registerPass(new yK),s.registerPass(new SK),s.registerPass(new AK),s.registerPass(new HK),s.registerPass(new oH(t)),s.registerPass(new nH(this.catalog,e)),s.registerPass(new JK),s.registerPass(new tK(t)),s.registerPass(new MK),s.registerPass(new eH),s}parseSQL(e){return uW(e)}bind(e){return new CT(this.catalog,this.functionRegistry).bind(e)}plan(e){return dW(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=fT(t.name,t.columns,t.name),n=Ff.fromStorageSchema(t.columns,t.name);return new Sr(this,s,n)}createDataFrame(e,t=null){let s=Td.fromRows(e,t),n=`${CH}${this._nextDfId()}`,r=s.getSchema();this.catalog.registerTable(n,r),this.catalog.registerTableStorage(n,s);let i=fT(n,r,n),o=Ff.fromStorageSchema(r,n);return new Sr(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 CT(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=Ff.fromFields(r.outputColumns.map((l,u)=>({name:l.name,dataType:l.dataType,index:u,tableAlias:""})));return new Sr(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 Of(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(()=>(HE(),Bw));return{rows:[{EXPLAIN_PLAN:a(s)}],columns:["EXPLAIN_PLAN"]}}if(o){let{formatPlan:a}=await Promise.resolve().then(()=>(HE(),Bw)),l=a(s),u=performance.now();this.executor.cteDefinitions=r;let{sink:f,columnNames:h}=await this.executor.execute(s,n),d=await new Of(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 Of(l,a).toArray(),columns:l}}finally{this._activeCancel=null}}cancel(){this._activeCancel&&this._activeCancel.abort()}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}`)}executeCreateTable(e){let t=c(o=>({INTEGER:L.INT32,INT:L.INT32,INT32:L.INT32,BIGINT:L.INT64,INT64:L.INT64,FLOAT:L.FLOAT64,DOUBLE:L.FLOAT64,REAL:L.FLOAT64,DECIMAL:L.DECIMAL,NUMERIC:L.DECIMAL,VARCHAR:L.VARCHAR,TEXT:L.VARCHAR,CHAR:L.VARCHAR,DATE:L.DATE,TIMESTAMP:L.TIMESTAMP,DATETIME:L.TIMESTAMP,BOOLEAN:L.BOOLEAN,BOOL:L.BOOLEAN})[o.name.toUpperCase()]||L.VARCHAR,"resolveType"),s=e.name.toUpperCase();if(this.catalog.getTable(s)){if(e.ifNotExists)return{rows:[],columns:[],message:`Table ${s} already exists`};throw new Error(`Table ${s} already exists`)}let n=e.columns.map(o=>({name:o.name.toUpperCase(),dataType:t(o.typeName)})),r=this.storageBackend.createPageStore(this.tempManager.allocate("buffer",s)),i=new wW(s,n,r);return this.catalog.registerTable(s,n),this.catalog.registerTableStorage(s,i),{rows:[],columns:[],message:`Table ${s} created`}}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(()=>(HE(),Bw));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 Of(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 iH(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(()=>(cc(),NT));await(await e()).loadModule("core");let{registerAllKernels:s}=await Promise.resolve().then(()=>(H1(),RT));s(),this.wasmEnabled=!0}catch{this.wasmEnabled=!1}}async enableParallel(){let{Config:e}=await Promise.resolve().then(()=>(be(),jL));if(e.parallelWorkers<=1)return!1;try{let{getGlobalLoader:t}=await Promise.resolve().then(()=>(cc(),NT)),s=await t({shared:!0}),n=await s.loadModule("core"),r=s.initRegions(e.regionSize),{registerAllKernels:i}=await Promise.resolve().then(()=>(H1(),RT));i(),this.wasmEnabled=!0;let o=s.getModule("core"),{WorkerPool:a}=await Promise.resolve().then(()=>(Mj(),QP)),l=new a({maxWorkers:e.parallelWorkers,wasmModule:o,wasmMemory:s.memory,regionAllocator:r});await l.init();let{globalDispatch:u}=await Promise.resolve().then(()=>(bd(),YL)),{ParallelDispatch:f}=await Promise.resolve().then(()=>(Lj(),tD)),h=new f(l,r,u),{FragmentPool:p}=await Promise.resolve().then(()=>(Wj(),lD)),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(()=>(dN(),uD)),{ClusterManager:n}=await Promise.resolve().then(()=>(Hj(),dD)),{PartitionMap:r}=await Promise.resolve().then(()=>(Yj(),gD)),{DistributionAwareJoin:i}=await Promise.resolve().then(()=>(mN(),_D)),{PartialAggregatePass:o}=await Promise.resolve().then(()=>(Jj(),bD)),{DistributedSortPass:a}=await Promise.resolve().then(()=>(Xj(),xD)),{QueryCoordinator:l}=await Promise.resolve().then(()=>(sW(),vD)),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(()=>(aW(),ID));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()}},WT="query_engine",KT=new Set,kL=!1;function MH(){if(kL)return;kL=!0;let e=c(()=>{for(let t of KT)t.cleanup()},"cleanupAll");process.on("exit",e);for(let t of["SIGINT","SIGTERM"])process.on(t,()=>{e(),process.exit()})}c(MH,"registerProcessHooks");function LH(e){try{return process.kill(e,0),!0}catch{return!1}}c(LH,"isProcessAlive");function PH(e){let t;try{t=Lf.readdirSync(e,{withFileTypes:!0})}catch{return}let s=new RegExp(`^${WT}_(\\d+)_`);for(let n of t){if(!n.isDirectory()||!n.name.startsWith(WT+"_"))continue;let r=jT.join(e,n.name),i=n.name.match(s);if(i){let o=parseInt(i[1],10);if(LH(o))continue}try{Lf.rmSync(r,{recursive:!0,force:!0})}catch{}}}c(PH,"reapStaleDirectories");var DH=class{static{c(this,"TempDirectoryManager")}constructor(e={}){this.baseDir=e.baseDir||BH.tmpdir();let t=Math.random().toString(36).substring(2,10);this.rootName=`${WT}_${process.pid}_${t}`,this.rootDir=jT.join(this.baseDir,this.rootName),this.counters=new Map,this.initialized=!1,KT.add(this),MH(),PH(this.baseDir)}allocate(e,t){this.initialized||(Lf.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=jT.join(this.rootDir,e,n);return Lf.mkdirSync(r,{recursive:!0}),r}getRootDir(){return this.rootDir}cleanup(){KT.delete(this),this.initialized&&Lf.existsSync(this.rootDir)&&(Lf.rmSync(this.rootDir,{recursive:!0,force:!0}),this.initialized=!1)}};wP();nN();var UH=class{static{c(this,"FilePageStore")}constructor(e,t=null){this.basePath=e,this.allocatorFactory=t}getPagePath(e){return FH.join(this.basePath,`${e}${dP}`)}async write(e,t){let s=Df.serialize(t);await CL.writeFile(this.getPagePath(e),s)}async read(e){let t=await CL.readFile(this.getPagePath(e));return this.allocatorFactory?Df.deserialize(t,this.allocatorFactory(t.length)):Df.deserialize(t)}clear(){IL.existsSync(this.basePath)&&IL.rmSync(this.basePath,{recursive:!0,force:!0})}};bP();nN();var zH=class{static{c(this,"FsStorage")}constructor(e){this.basePath=e}filePath(e){return $H.join(this.basePath,`${e}${mP}`)}async append(e,t){await Iw.appendFile(this.filePath(e),t)}async read(e){let t=this.filePath(e);return Cw.existsSync(t)?Iw.readFile(t):null}exists(e){return Cw.existsSync(this.filePath(e))}async remove(e){let t=this.filePath(e);Cw.existsSync(t)&&await Iw.unlink(t)}async removeAll(){Cw.existsSync(this.basePath)&&await Iw.rm(this.basePath,{recursive:!0,force:!0})}};fc();var VH=class{static{c(this,"NodeStorageBackend")}constructor(e={}){this.options=e}createTempSpace(){return new DH(this.options.tempDir?{baseDir:this.options.tempDir}:{})}createPageStore(e){return new UH(e,S6)}createSpillManager(e){return new rN(new zH(e))}};cc();var KH=jH(qH(import.meta.url));function HH(e){return GH(WH(KH,e+".wasm"))}c(HH,"nodeByteSource");var YH=class{static{c(this,"Catalog")}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}};rs();IH(e=>new VH(e));XP(HH);function XD(e={}){let t=e.catalog||new YH;return new OH(t,e)}c(XD,"createEngine");var bN=["tensor","zeros","ones","empty","full","randn","arange","eye","linspace","randperm","zerosLike","onesLike","emptyLike","fullLike","randnLike"],SN=["where","cat","stack"],xN=["sum","min","max"],hb=["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 hc(){return QD??(QD=XD())}c(hc,"engine");var eF=0;function sF(e){let t=hc(),s=Td.fromColumns(e),n=`__upload${eF++}`;return t.catalog.registerTable(n,s.getSchema()),t.catalog.registerTableStorage(n,s),n}c(sF,"registerColumnsAsTable");function nF(e){let t=hc(),s=e.toUpperCase();t.catalog.tables?.delete(s),t.catalog.tableStorage?.delete(s)}c(nF,"dropTable");function rF(e){return hc().sql(`SELECT * FROM ${sF(e)}`)}c(rF,"createDataFrameFromColumns");var pc=new Map;function iF(e,t){pc.set(e,sF(t))}c(iF,"setUploadedCsv");function oF(e){let t=Td.builder();return{appendRows(s){s&&s.length&&t.appendRows(s)},finish(){let s=t.finish(),n=hc(),r=`__upload${eF++}`;n.catalog.registerTable(r,s.getSchema()),n.catalog.registerTableStorage(r,s);let i=pc.get(e);i&&nF(i),pc.set(e,r)}}}c(oF,"beginUploadedCsv");function aF(e){let t=pc.get(e);t&&nF(t),pc.delete(e)}c(aF,"removeUploadedCsv");var JH={sum:qD,min:WD,max:KD};function XH(e){return typeof e=="string"||e instanceof $n}c(XH,"isColumnArg");Sr.prototype.toString=function(){return`DataFrame(${this.columns().join(", ")})`};Sr.prototype.head=function(e=5){return this.limit(e)};async function QH(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 Os(a,{shape:[o,i]})}c(QH,"dfToTensor");async function ZH(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[Os(a,{shape:[a.length]}),o]}c(ZH,"dfEncode");Sr.prototype.toTensor=function(...e){return QH(this,e)};Sr.prototype.to_tensor=Sr.prototype.toTensor;Sr.prototype.to_array=function(){return this.toArray()};Sr.prototype.encode=function(e,...t){let n=hn(t).classes??t[0]??null;return ZH(this,e,n)};function lF(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";Ee.writeBinary(s,Sf({modelState:e.stateDict()})),Ee.rename(s,t)}c(lF,"saveModelCheckpoint");function pb(e,t){for(let s of bN)t(s,(...n)=>ZD(Nf[s],n));for(let s of SN)t(s,(...n)=>ZD(Nf[s]??es[s],n));for(let s of xN)t(s,n=>{if(!XH(n))throw new Error(`${s}() expects a DataFrame column; call tensor.${s}() for tensors`);return JH[s](n)});for(let s of hb)t(s,(...n)=>t8(Nf[s],n));t("Sequential",(...s)=>new ia(...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 Ot&&a.device===kr)&&await up();let r=n?.sep??" ",i=s.length>1,o=s.map(a=>i?h1(a):Ql(a)).join(r);e.output(o)}),t("trace",s=>{let n=s?._isCompiled?s._compiledView:s instanceof Da?s:null;if(!n?.events)throw new Error("trace() expects a compiled program");let r=Jp(n.events);return e.output(r),r}),t("graph",s=>{let n=s?._isCompiled?s._compiledView?.graph:s instanceof Da?s.graph:s,r=AM(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)=>zn(Ao,s)),t("load_tokenizer",s=>Ao.load(s)),t("TensorDataset",(...s)=>new ca(...s)),t("DataLoader",(...s)=>{let n=hn(s);return delete n.__named,new da(s[0],qf(n))}),t("SGD",(...s)=>wN(ma,s)),t("Adam",(...s)=>wN(ga,s)),t("AdamW",(...s)=>wN(ya,s)),t("StepLR",(...s)=>tF(wa,s,["optimizer","stepSize","gamma"])),t("CosineAnnealingLR",(...s)=>tF(ba,s,["optimizer","tMax","etaMin"])),t("ReduceLROnPlateau",(...s)=>{let n=hn(s);return delete n.__named,new Sa(s[0],qf(n))}),t("Trainer",(...s)=>{let n=hn(s);return delete n.__named,new Ta(qf(n))}),t("EarlyStopping",(...s)=>zn(Na,s)),t("ModelCheckpoint",(...s)=>zn(ai,s)),t("ProgressCallback",(...s)=>zn(oi,s)),t("LearningRateMonitor",(...s)=>zn(Ra,s)),t("Timer",(...s)=>zn(va,s)),t("GradientAccumulationScheduler",(...s)=>zn(ka,s)),t("ConsoleLogger",(...s)=>zn(zi,s)),t("CSVLogger",(...s)=>zn(Ca,s)),t("Accuracy",(...s)=>zn(Oa,s)),t("Precision",(...s)=>zn(Ba,s)),t("Recall",(...s)=>zn(Ma,s)),t("F1Score",(...s)=>zn(La,s)),t("ConfusionMatrix",(...s)=>zn(Pa,s)),t("MetricCollection",(...s)=>zn(Ia,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 Af(xf(n),s),s}),t("read_text",s=>{if(typeof s!="string")throw new Error("read_text() requires a file path string");let n=Ee.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=Ee.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=hn(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=hn(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 hc().createDataFrame(o)}),t("col",s=>ji(s)),t("lit",s=>ub(s)),t("expr",s=>fb(s)),t("avg",s=>jD(s)),t("count",s=>HD(s)),t("countStar",()=>YD()),t("load_csv",(...s)=>{let n=hn(s);delete n.__named;let r=s[0];if(typeof r!="string")throw new Error("load_csv() requires a file path string");if(pc.has(r))return hc().sql(`SELECT * FROM ${pc.get(r)}`);let i=n.separator??n.sep??",",{rows:o}=d1(r,i);return hc().createDataFrame(o)}),t("encode",(...s)=>{let n=s[0];if(n instanceof Sr)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[Os(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 Ot)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 Ot))throw new Error("normalize() expects a tensor");let r=hn(s);delete r.__named;let i=r.axis??0,o=gn(n,i,!0),a=jt(n,o),l=gn(it(a,a),i,!0),u=gr(ht(l,Os(1e-8)));return fe(a,u)}),t("train_test_split",(...s)=>{let n=hn(s);delete n.__named;let r=s[0],i=n.test_size??n.ratio??.2;if(r instanceof Ot){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(pb,"installBuiltins");function hn(e){let t=e[e.length-1];return t&&t.__named?e.pop():{}}c(hn,"takeNamed");function ZD(e,t){let s=hn(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=cF[s.device]??s.device),e(...t,s))}c(ZD,"callWithOptions");var cF={cpu:Hc,gpu:wl,wasm:Yc,webgpu:kr};function uF(e){return typeof e=="string"?cF[e]??e:e}c(uF,"resolveDeviceName");function t8(e,t){let s=hn(t);return delete s.__named,e===na||e===ra?new e(s.axis??t[0]??-1):e===sa||e===ea?new e(...t,s):new e(...t,...Object.values(s))}c(t8,"constructWithNamed");function e8(e){return e.replace(/_([a-z])/g,(t,s)=>s.toUpperCase())}c(e8,"snakeToCamel");function qf(e){let t={};for(let s of Object.keys(e))t[e8(s)]=e[s];return t}c(qf,"snakeNamedToCamel");function zn(e,t){let s=hn(t);delete s.__named;let n=qf(s);return t.length>0?new e(...t,n):Object.keys(n).length>0?new e(n):new e}c(zn,"constructWithSnakeCase");function wN(e,t){let s=hn(t);delete s.__named;let n=t[0];if(!n)throw new Error(`${e.name}() requires params as first argument`);return new e(n,qf(s))}c(wN,"constructOptimizer");function tF(e,t,s){let n=hn(t);delete n.__named;let r=qf(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(tF,"constructScheduler");var s8={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"}]},n8={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}]},r8={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}]},i8={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 fF(e){for(let[t,s]of Object.entries(s8))e.register(t,s);for(let[t,s]of Object.entries(n8))e.register(t,s);for(let[t,s]of Object.entries(i8))e.register(t,s);for(let[t,s]of Object.entries(r8))e.register(t,s)}c(fF,"installSignatures");function hF(e,t){return e<0?e+t:e}c(hF,"normalizeIndex");function o8(e,t){if(e.length===0)throw new Error("pop from empty list");let s=hF(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(o8,"popList");function a8(e,t,s){let n=hF(t,e.length);return n<0&&(n=0),n>e.length&&(n=e.length),e.splice(n,0,s),null}c(a8,"insertList");function l8(e,t){let s=e.indexOf(t);return s!==-1&&e.splice(s,1),null}c(l8,"removeList");function c8(e,t){let s=0;for(let n of e)n===t&&(s+=1);return s}c(c8,"countList");var u8={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:a8,pop:o8,remove:l8,index:c((e,t)=>e.indexOf(t),"index"),count:c8,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 f8(e,t){if(t===void 0){let s=e.trim();return s===""?[]:s.split(/\s+/)}return e.split(t)}c(f8,"splitString");var h8={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:f8,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")},p8={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 pF(e,t){let s;if(Array.isArray(e))s=u8;else if(typeof e=="string")s=h8;else if(e instanceof Map)s=p8;else return;let n=s[t];if(n)return(...r)=>n(e,...r)}c(pF,"lookupCollectionMethod");Xa();var db=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=We.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 Ka=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}'`)}},jf=class{static{c(this,"TeraRuntime")}constructor({output:t=console.log}={}){this.output=t,Uk(Yc),this.global=new Ka,this.signatureRegistry=new db,this._installBuiltins(),fF(this.signatureRegistry)}registerDataFrame(t,s){let n=rF(s);return this.global.define(t,n),n}registerUploadedCsv(t,s){iF(t,s)}beginUploadedCsv(t){return oF(t)}removeUploadedCsv(t){aF(t)}async execute(t){try{return await this.evaluateProgram(fw(t),this.global)}catch(s){throw s.line!==void 0?s:new mb(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(!Wi(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=pF(n,t.property);if(r)return r;let i=n[t.property];return typeof i!="function"?i:t.property==="to"&&(Wi(n)||n instanceof ct)?m8(n,i):(t.property==="eval"||t.property==="train")&&n instanceof ct?g8(n,i,t.property):Wi(n)?n.device===kr&&d8.has(t.property)?y8(n,i):mF(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 ct)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 Wi(t)?In(t):-t}applyUnaryNot(t){if(Wi(t)){let s=Je(t.shape,{dtype:t.dtype,device:t.device});return jt(s,t)}return!t}applyBinary(t,s,n){if(!(Wi(s)||Wi(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]=_8(s,n);let i={"+":ht,"-":jt,"*":it,"/":fe,"**":Or,"@":Ye,"==":ki,"!=":kl,"<":Cl,"<=":fo,">":Zr,">=":ho,and:it,or:c((o,a)=>jt(ht(o,a),it(o,a)),"or")}[t];if(!i)throw new Error(`Unsupported operator '${t}'`);return i(s,n)}isTruthy(t){if(Wi(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 Ka(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?Ea:ct,d=c(async(...m)=>{let g=hn(m),y=new Ka(s);t.params.forEach((S,b)=>y.define(S,g[S]??m[b]));class _ extends p{static{c(this,"LangModel")}constructor(){super(),this._langName=h}async forward(...b){let A=new Ka(y);for(let N of f)N.type==="Assign"&&A.define(N.name,this[N.name]);r.params.forEach((N,I)=>A.define(N,b[I]));let x=await n.evaluateProgram({type:"Program",body:r.body},A);return x&&x.__return?x.value:x}toString(){return`${this._langName}${super.toString().slice(this.constructor.name.length)}`}}_.prototype.save=function(S){lF(this,S)},i&&(_.prototype.trainingStep=async function(S,b){let A=AN(this,y,f,h);dF(A,this),i.params[0]&&A.define(i.params[0],S),i.params[1]&&A.define(i.params[1],b);let x=await n.evaluateProgram({type:"Program",body:i.body},A);return x&&x.__return?x.value:x}),o&&(_.prototype.validationStep=async function(S,b){let A=AN(this,y,f,h);dF(A,this),o.params[0]&&A.define(o.params[0],S),o.params[1]&&A.define(o.params[1],b);let x=await n.evaluateProgram({type:"Program",body:o.body},A);return x&&x.__return?x.value:x}),a&&(_.prototype.configureOptimizers=async function(){let S=AN(this,y,f,h),b=await n.evaluateProgram({type:"Program",body:a.body},S);return b&&b.__return?b.value:b});let w=new _;for(let S of f){let b=await n.evaluateStatement(S,y);S.type==="Assign"&&(w[S.name]=b)}return w},"factory");return d._langName=h,s.define(h,d),this.signatureRegistry.register(h,t.params.map(m=>({name:m}))),d}_installBuiltins(){pb(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 Ot))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 Ka(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 mb(r.message,t.line??1,t.column??1,r)}finally{this.currentNode=n}}compile(t,...s){if(!(t instanceof ct))throw new Error("compile() currently expects a model. Example: compile(model, input=x)");let n=hn(s),r=n.input??s[0],i=r!=null?Array.isArray(r)?r:[r]:null;if(i&&i.some(d=>!(d instanceof Ot)))throw new Error("compile() input must be a tensor, for example compile(model, input=x)");let o=n.target??"wasm",a=o==="gpu"?al():o==="wasm"?ll():o==="webgpu"?cl():ur(),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:dt.DEBUG,sink:c(d=>f.push(d),"sink"),irSnapshot:{afterGraphPasses:!0,afterLowering:!0,afterScheduling:!0}});let p=ew(t,i,h);if(p._isCompiled=!0,p._langName="compiled",Object.defineProperty(p,"_compiledView",{get:c(()=>new Da({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(Jp(f)),u&&d.output(p.snippet()),p))}return i&&(l&&this.output(Jp(f)),u&&this.output(p.snippet())),p}};function Wi(e){return e instanceof Ot||e instanceof we}c(Wi,"isTensorValue");var d8=new Set(["item","toArray","tolist"]);function m8(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=uF(r),r===kr){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 nu()}else r===wl&&await YS();return t.call(e,r)}}c(m8,"makeDeviceMove");function g8(e,t,s){return(...n)=>{let r=t.apply(e,n);return Lr.setEnabled(s==="train"?n.length===0?!0:!!n[0]:!1),r}}c(g8,"makeTrainModeToggle");function y8(e,t){return async(...s)=>(await up(),mF(e,t)(...s))}c(y8,"makeWebgpuHostRead");function mF(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(mF,"bindTensorMethod");function _8(e,t){let s=e instanceof Ot?e:t instanceof Ot?t:null;if(!s)return[e,t];let n={dtype:s.dtype,device:s.device};return Wi(e)||(e=Os(e,n)),Wi(t)||(t=Os(t,n)),[e,t]}c(_8,"promoteScalars");function AN(e,t,s,n){let r=new Ka(t);r.define(n,e);for(let i of s)i.type==="Assign"&&r.define(i.name,e[i.name]);return r}c(AN,"buildStepEnv");function dF(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 Ot)&&(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(dF,"bindLog");var mb=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 Dt=Object.freeze({kind:"any"}),Vxt=Object.freeze({kind:"unknown"}),Ki=Object.freeze({kind:"number",num:"int"}),Ha=Object.freeze({kind:"number",num:"float"}),fi=Ki,Us=Object.freeze({kind:"string"}),hi=Object.freeze({kind:"bool"}),EN=Object.freeze({kind:"null"}),Hi=Object.freeze({kind:"none"}),$s=Object.freeze({kind:"tensor"});function Xs(e){return{kind:"list",element:e??Dt}}c(Xs,"listType");function TN(e,t){return{kind:"dict",key:e??Dt,value:t??Dt}}c(TN,"dictType");function dc(e,t,s=!1,n=null,r=null){let i=e??[];return{kind:"function",params:i,ret:t??Dt,variadic:s,required:n??i.length,names:r}}c(dc,"functionType");function Ar(e=""){return{kind:"module",name:e}}c(Ar,"moduleType");function Fs(e){return!e||e.kind==="any"||e.kind==="unknown"}c(Fs,"isAny");function Yt(e){switch(e.kind){case"list":{let t=Yt(e.element);return e.element.kind==="union"||e.element.kind==="function"?`(${t})[]`:`${t}[]`}case"dict":return`Record<${Yt(e.key)}, ${Yt(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(Yt).join(", ")}${e.variadic?", ...":""}) -> ${Yt(e.ret)}`;case"union":return e.members.map(Yt).join(" | ");default:return e.kind}}c(Yt,"typeToString");function NN(e){let t=[];for(let r of e)r.kind==="union"?t.push(...r.members):t.push(r);if(t.some(Fs))return Dt;let s=new Map;for(let r of t){let i=Yt(r);s.has(i)||s.set(i,r)}let n=[...s.values()];return n.length===1?n[0]:{kind:"union",members:n}}c(NN,"unionType");function Wf(e,t){return e?t?Fs(e)||Fs(t)?Dt:e.kind==="number"&&t.kind==="number"?e.num==="float"||t.num==="float"?Ha:Ki:Yt(e)===Yt(t)?e:NN([e,t]):e:t}c(Wf,"join");function w8(e,t){let s=Math.min(e.params.length,t.params.length);for(let n=0;n<s;n++)if(!Vn(t.params[n],e.params[n]))return!1;return Vn(e.ret,t.ret)}c(w8,"functionAssignable");function Vn(e,t){if(Fs(e)||Fs(t))return!0;if(t.kind==="union")return t.members.some(s=>Vn(e,s));if(e.kind==="union")return e.members.every(s=>Vn(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 Vn(e.element,t.element);case"dict":return Vn(e.key,t.key)&&Vn(e.value,t.value);case"module":return!e.name||!t.name||e.name===t.name;case"function":return w8(e,t);default:return!0}}c(Vn,"isAssignable");var b8=new Set(["LSTM","GRU","LSTMCell","GRUCell"]);function gF(e){return b8.has(e)?Xs($s):$s}c(gF,"moduleCallReturn");function yF(e){return e?e.endsWith("[]")?Xs(yF(e.slice(0,-2))):e==="Tensor"?$s:e==="int"?Ki:e==="float"?Ha:e==="string"||e==="str"?Us:e==="boolean"||e==="bool"?hi:e==="none"?Hi:Ar(e):Dt}c(yF,"resolveReturn");function S8(e){return Array.isArray(e)?e:e?.methods??[]}c(S8,"methodsOf");function _F(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,yF(o.returns)))},"record");for(let[n,r]of Object.entries(e.pseudoTypes??{}))s(n,S8(r));for(let n of e.builtins??[])s(n.name,n.methods??[]);return t}c(_F,"buildMethodReturns");var x8=["cpu","gpu","wasm","webgpu","f16","f32","f64","i32","i64","bool"],wF=new Map([...hb,"Sequential"].map(e=>[e,gF(e)]));function A8(){return new Map([["len",fi],["range",Xs(fi)],["shape",Xs(fi)],["dtype",Us],["read_text",Us],["trace",Us],["graph",Us],["print",Hi],["backward",Hi],["load_csv",Ar("DataFrame")],["DataFrame",Ar("DataFrame")],["Tokenizer",Ar("Tokenizer")],["load_tokenizer",Ar("Tokenizer")]])}c(A8,"returnOverrides");function E8(e){return dc([],e,!0,0)}c(E8,"builtinFunction");function bF(){let e=new Map,t=A8(),s=c((n,r)=>e.set(n,E8(t.get(n)??r)),"setFn");for(let n of bN)s(n,$s);for(let n of SN)s(n,$s);for(let n of xN)s(n,Dt);for(let n of hb)s(n,Ar(n));for(let n of t.keys())e.has(n)||s(n,Dt);for(let n of x8)e.set(n,Us);return e}c(bF,"buildBuiltinTypes");var RN=null;function SF(){if(RN)return RN;let e=new Set;return pb({output(){},compile(){}},s=>e.add(s)),RN=e,e}c(SF,"collectBuiltinNames");var vN=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:Ki,float:Ha,string:Us,boolean:hi,Tensor:$s,tensor:$s,none:Hi,null:EN},tAt=Object.freeze([...Object.keys(xF),"Record"]),AF=Object.freeze(["chart"]),T8=new Map([["log","log() can only be called inside a train or validate block"]]),N8={shape:Xs(fi),ndim:fi,numel:fi,dtype:Us,device:Us,requiresGrad:hi,grad:$s};function R8(e,t){switch(e){case"append":case"extend":case"insert":case"remove":case"reverse":case"clear":return Hi;case"pop":return t.element;case"index":case"count":return Ki;case"contains":return hi;case"copy":return Xs(t.element);default:return null}}c(R8,"listMethodResult");function v8(e){switch(e){case"upper":case"lower":case"strip":case"lstrip":case"rstrip":case"replace":case"join":return Us;case"split":return Xs(Us);case"starts_with":case"ends_with":case"contains":return hi;case"find":return Ki;default:return null}}c(v8,"stringMethodResult");function k8(e,t){switch(e){case"keys":return Xs(t.key);case"values":return Xs(t.value);case"items":return Xs(Dt);case"get":return t.value;case"has":return hi;case"remove":return Hi;default:return null}}c(k8,"dictMethodResult");var C8=new Set(["==","!=","<","<=",">",">="]),I8=new Set(["-","*","/","**"]),O8=new Set(["number","string","bool","list","dict","tensor","null","none"]),mc=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)}},kN=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 mc;for(let i of t)this.root.define(i,s.get(i)??Dt);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:Dt}),this.diagnostics}report(t,s){this.diagnostics.push(new vN(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&&!Fs(n)&&this.inferred.set(`${s}:${r.line}`,Yt(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):Dt}isTensor(t){return t&&t.kind==="tensor"}resolveTypeNode(t){return t?t.kind==="ArrayType"?Xs(this.resolveTypeNode(t.element)):t.kind==="UnionType"?NN(t.members.map(s=>this.resolveTypeNode(s))):t.kind==="FunctionType"?dc(t.params.map(s=>this.resolveTypeNode(s)),this.resolveTypeNode(t.ret),!1,t.params.length):t.kind==="GenericType"?t.name==="Record"?TN(this.resolveTypeNode(t.args[0]),this.resolveTypeNode(t.args[1])):t.name==="Tensor"||t.name==="tensor"?$s:Ar(t.name):xF[t.name]??Ar(t.name):Dt}elementOf(t){return Fs(t)?Dt:t.kind==="list"?t.element:t.kind==="dict"?t.key:t.kind==="string"?Us:Dt}inferLiteral(t){let s=t.value;return typeof s=="number"?t.isFloat?Ha:Ki:typeof s=="string"?Us:typeof s=="boolean"?hi:s===null?EN:Dt}inferIdentifier(t,s){let n=s.lookup(t.name);return n===void 0?(this.report(T8.get(t.name)??`undefined name '${t.name}'`,t),Dt):n}inferArray(t,s){let n=null;for(let r of t.elements){let i=this.infer(r,s);n=n===null?i:Wf(n,i)}return Xs(n??Dt)}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:Wf(n,o),r=r===null?a:Wf(r,a)}return TN(n??Dt,r??Dt)}inferComprehension(t,s){let n=this.infer(t.iterable,s),r=new mc(s);return this.declare(r,t.variable,this.elementOf(n),t),t.condition&&this.infer(t.condition,r),Xs(this.infer(t.expr,r))}inferUnary(t,s){let n=this.infer(t.value,s);return t.op==="not"?this.isTensor(n)?$s:hi:this.isTensor(n)?$s:Fs(n)?Dt:n.kind==="number"?n:(this.report(`operator '${t.op}' cannot be applied to ${Yt(n)}`,t),Dt)}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?$s:Wf(s,n);if(C8.has(t))return i?$s:hi;if(Fs(s)||Fs(n))return Dt;if(t==="@")return i||this.report(`operator '@' requires tensors, got ${Yt(s)} and ${Yt(n)}`,r),$s;if(i)return $s;if(s.kind==="number"&&n.kind==="number")return t==="/"?Ha:s.num==="float"||n.num==="float"?Ha:Ki;if(t==="+"){if(s.kind==="string"&&n.kind==="string")return Us;if(s.kind==="list"&&n.kind==="list")return Xs(Wf(s.element,n.element))}return(t==="+"||I8.has(t))&&this.report(`operator '${t}' cannot be applied to ${Yt(s)} and ${Yt(n)}`,r),Dt}inferMember(t,s){return this.memberType(this.infer(t.object,s),t.property)}memberType(t,s){if(Fs(t))return Dt;if(t.kind==="tensor")return N8[s]??Dt;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"?fi:Dt}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=R8(s,t);if(i)return i}if(t.kind==="string"){let i=v8(s);if(i)return i}if(t.kind==="dict"){let i=k8(s,t);if(i)return i}return this.isTensor(t)?this.tensorMethodResult(s):null}tensorMethodResult(t){return t==="item"?fi:t==="toArray"||t==="tolist"?Xs(fi):$s}requireNumberIndex(t,s){Fs(t)||this.isTensor(t)||t.kind==="number"||this.report(`index must be a number, got ${Yt(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);Fs(s)||(s.kind==="list"||s.kind==="string"?this.requireNumberIndex(i,r):s.kind==="dict"&&!Vn(i,s.key)&&this.report(`index of type ${Yt(i)} is not assignable to key type ${Yt(s.key)}`,r))}}inferIndex(t,s){let n=this.infer(t.object,s);return this.checkIndexItems(t.items,n,s),Fs(n)?Dt:n.kind==="list"?n.element:n.kind==="dict"?n.value:n.kind==="string"?Us:Dt}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"?(O8.has(t.kind)&&this.report(`${Yt(t)} is not callable`,r.callee),Dt):(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++)Vn(s[o].type,t.params[o])||this.report(`argument ${o+1} expects ${Yt(t.params[o])}, got ${Yt(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}Vn(l[u].type,o[u])||this.report(`argument '${i[u]}' expects ${Yt(o[u])}, got ${Yt(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);Vn(n,r)||this.report(`cannot assign ${Yt(n)} to '${t.name}: ${Yt(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??Dt,r,t),t)}checkDestructure(t,s){let n=this.infer(t.value,s);!Fs(n)&&n.kind!=="list"&&!this.isTensor(n)&&this.report(`cannot destructure ${Yt(n)}`,t);let r=n.kind==="list"?n.element:Dt;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);Fs(n)||(n.kind==="list"&&!Vn(r,n.element)?this.report(`cannot assign ${Yt(r)} to element of ${Yt(n)}`,t):n.kind==="dict"&&!Vn(r,n.value)&&this.report(`cannot assign ${Yt(r)} to value of ${Yt(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),Dt;let u=this.resolveTypeNode(l);return Fs(u)||this.inferred.set(`${o}:${l.line??r.line}`,Yt(u)),u})}checkReturnTypes(t,s){for(let n of t)Vn(n.type,s)||this.report(`return type ${Yt(n.type)} is not assignable to declared ${Yt(s)}`,n.node)}allowsImplicitReturn(t){return Fs(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 ${Yt(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):Dt;this.declare(s,t.name,dc(n,r,!1,t.params.length,t.params),t);let i=new mc(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,dc(n,Ar(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):Dt);let i=new mc(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:Dt});let a=new Map;for(let l of t.body)l.type==="Assign"&&a.set(l.name,i.lookup(l.name)??Dt);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:Dt})}stepEnv(t,s){let n=new mc(t);return n.define(s,Ar(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",dc([Us,Dt],Hi,!0,1));let a=t.returnType?this.resolveTypeNode(t.returnType):Dt,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 EF(e,t){return new kN(t).run(e)}c(EF,"typecheck");function B8(e){return{builtinNames:new Set([...SF(),...AF]),builtinTypes:bF(),methodReturns:e,moduleCalls:wF}}c(B8,"buildEnv");function CN(e,{methodReturns:t}={}){return{diagnostics:EF(fw(e),B8(t))}}c(CN,"checkSource");function Kf(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(Kf,"formatDiagnostic");import F8 from"terminal-kit";var NF=`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.`,M8={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`},L8={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 TF(e="main"){return M8[e]||`Unknown help topic '${e}'. Try: help tensor, help model, or help compile.`}c(TF,"getHelp");function P8(){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(P8,"listExamples");function D8(e){return L8[e]||`Unknown example '${e}'. Try: examples`}c(D8,"getExample");function RF(e){let t=e.trim();return t==="help"?TF():t.startsWith("help ")?TF(t.slice(5).trim()):t==="examples"?P8():t.startsWith("example ")?D8(t.slice(8).trim()):null}c(RF,"handleReplCommand");function vF(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(vF,"parseCallContext");var IN=new Set(["model","forward","return","true","false","null","and","or","not","fn","if","else","for","in","while","break","continue"]),U8=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"]),$8=["help","help tensor","help model","help fn","help control","help compile","examples","example tensor","example linear","example custom","example compile","exit","quit"];async function IF({term:e=F8.terminal}={}){let t=c(a=>e.noFormat(String(a)+`
|
|
400
|
-
`),"write"),s=new jf({output:t}),n=[],r="",i=0,o=!1;for(e(NF+`
|
|
401
|
-
`);;){e(i>0?"^K... ^:":"^Kmlfw> ^:");let a=e.inputField({history:n,autoComplete:c(h=>z8(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:G8}),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){kF(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=RF(f);if(h!==null){t(h);continue}}if(r+=f+`
|
|
405
|
-
`,f.trim().endsWith(":")?i=1:i>0&&f.trim()===""?i=0:i+=j8(f),!(i>0)){try{let h=await s.execute(r),p=Ql(h);p&&t(p)}catch(h){e.red.noFormat(`${Kf(h,r)}
|
|
406
|
-
`)}r="",i=0}}return kF(e),{runtime:s,history:n}}c(IF,"startRepl");function kF(e){typeof e.grabInput=="function"&&e.grabInput(!1),typeof e.hideCursor=="function"&&e.hideCursor(!1),typeof e.styleReset=="function"&&e.styleReset()}c(kF,"shutdownTerminal");function z8(e,t,s=""){let n=vF(e);if(n){let h=t.signatureRegistry.lookup(n.functionName);if(h){let p=q8(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=CF(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=V8(s),u=[...new Set([...$8,...IN,...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=CF(u);return f.length>a.length?o+f:(u.prefix=o,u)}c(z8,"completeInput");function V8(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];IN.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(V8,"extractBufferNames");function CF(e){let t=e[0]||"";for(let s of e.slice(1))for(;!s.startsWith(t);)t=t.slice(0,-1);return t}c(CF,"commonPrefix");function G8(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:IN.has(e)?n.brightBlue:U8.has(e)?n.yellow:/^(?:\*\*=?|==|!=|<=|>=|[+\-*\/@][=]?|[=<>])$/.test(e)?n.brightBlack:/^[A-Z]/.test(e)?n.cyan:n}c(G8,"tokenHook");function q8(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(q8,"formatParamHint");function j8(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(j8,"bracketDelta");var Nd;function PF(){if(Nd!==void 0)return Nd;try{let e=new URL("../../vscode-ext/language-data.json",import.meta.url);Nd=_F(JSON.parse(LF.readFileSync(e,"utf8")))}catch{Nd=null}return Nd}c(PF,"methodReturns");var OF=`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 DF(e,t,s,n){for(let r of e)n(Kf(r,t,s));return e.length>0}c(DF,"reportDiagnostics");async function FF(e,{stdout:t=console.log,stderr:s=console.error,readFile:n=c(a=>LF.readFileSync(a,"utf8"),"readFile"),repl:r=IF,stdinIsTTY:i=process.stdin.isTTY,readStdin:o=c(()=>W8(process.stdin),"readStdin")}={}){let[a,l,...u]=e;if(!a||a==="repl")return l||u.length?MF(s,"repl does not accept arguments"):i?(await r(),0):await BF(await o(),{stdout:t,stderr:s,filename:"<stdin>",stripExit:!0});if(a==="--help"||a==="-h"||a==="help")return t(OF),0;let f=a==="run"||a==="check"?a:"run",h=f===a?l:a;if(!h||u.length||f!==a&&l)return MF(s,`Invalid arguments.
|
|
410
|
-
|
|
411
|
-
${OF}`);let p;try{if(p=n(h),f==="check"){let{diagnostics:d}=CN(p,{methodReturns:PF()});return DF(d,p,h,s)?1:(t(`${h}: OK`),0)}return await BF(p,{stdout:t,stderr:s,filename:h})}catch(d){return s(p===void 0?`${d.name||"Error"}: ${d.message}`:Kf(d,p,h)),1}}c(FF,"runCli");async function BF(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}=CN(e,{methodReturns:PF()});if(DF(i,e,n,s))return 1;let o=await new jf({output:t}).execute(e),a=Ql(o);return a&&t(a),0}catch(i){return s(Kf(i,e,n)),1}}c(BF,"executeSource");function MF(e,t){return e(t),1}c(MF,"fail");async function W8(e){let t="";for await(let s of e)t+=s;return t}c(W8,"readStream");process.exitCode=await FF(process.argv.slice(2));
|